Skip to content

Commit c497659

Browse files
committed
sanity checks, error handling
1 parent 678fd08 commit c497659

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

src/hasher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn hash(tx: Sender<u64>, hasher_task: HasherTaskInfo, simd_ext: String) -> i
102102
hasher_task.numeric_id,
103103
hasher_task.local_startnonce,
104104
hasher_task.local_nonces,
105-
),
105+
),
106106
_ => noncegen(
107107
hasher_task.cache.ptr,
108108
hasher_task.cache_size,

src/plotter.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,32 @@ impl Plotter {
8484
if !task.quiet {
8585
println!(
8686
"CPU: {} [using {} of {} cores{}{}]",
87-
cpu_name, task.cpu_threads, cores, if simd_ext != "" {" + "} else {""}, simd_ext
87+
cpu_name,
88+
task.cpu_threads,
89+
cores,
90+
if simd_ext != "" { " + " } else { "" },
91+
simd_ext
8892
);
8993
}
9094

95+
let file = Path::new(&task.output_path).join(format!(
96+
"{}_{}_{}",
97+
task.numeric_id, task.start_nonce, task.nonces
98+
));
99+
100+
if !file.parent().unwrap().exists() {
101+
println!(
102+
"Error: specified target path does not exist, path={}",
103+
&task.output_path
104+
);
105+
println!("Shutting down...");
106+
return;
107+
}
108+
91109
// use all avaiblable disk space if nonce parameter has been omitted
110+
let free_disk_space = free_disk_space(&task.output_path);
92111
if task.nonces == 0 {
93-
task.nonces = free_disk_space(&task.output_path) / NONCE_SIZE;
112+
task.nonces = free_disk_space / NONCE_SIZE;
94113
}
95114

96115
// align number of nonces with sector size if direct i/o
@@ -108,8 +127,22 @@ impl Plotter {
108127

109128
let plotsize = task.nonces * NONCE_SIZE;
110129

130+
// check available disk space
131+
if free_disk_space < plotsize {
132+
println!(
133+
"Error: insufficient disk space, MiB_required={}, MiB_available={}",
134+
plotsize as f64 / 1024.0 / 1024.0,
135+
free_disk_space as f64 / 1024.0 / 1024.0
136+
);
137+
println!("Shutting down...");
138+
return;
139+
}
140+
111141
// calculate memory usage
112-
let mem = calculate_mem_to_use(&task, &memory, nonces_per_sector);
142+
let mem = match calculate_mem_to_use(&task, &memory, nonces_per_sector){
143+
Ok(x) => x,
144+
Err(_) => return
145+
};
113146

114147
if !task.quiet {
115148
println!(
@@ -132,11 +165,6 @@ impl Plotter {
132165
);
133166
}
134167

135-
let file = Path::new(&task.output_path).join(format!(
136-
"{}_{}_{}",
137-
task.numeric_id, task.start_nonce, task.nonces
138-
));
139-
140168
if !task.quiet {
141169
println!("Output File: {}\n", file.display());
142170
}
@@ -218,9 +246,9 @@ impl Plotter {
218246
let task = Arc::new(task);
219247

220248
// hi bold! might make this optional in future releases.
249+
let thread_pinning = true;
221250
let mut core_ids: Vec<core_affinity::CoreId> = Vec::new();
222251

223-
let thread_pinning = true;
224252
if thread_pinning {
225253
core_ids = core_affinity::get_core_ids().unwrap();
226254
}
@@ -288,9 +316,23 @@ fn calculate_mem_to_use(
288316
task: &PlotterTask,
289317
memory: &sys_info::MemInfo,
290318
nonces_per_sector: u64,
291-
) -> u64 {
319+
) -> Result<u64, &'static str> {
292320
let plotsize = task.nonces * NONCE_SIZE;
293-
let mut mem = task.mem.parse::<Bytes>().unwrap().size() as u64;
321+
322+
let mut mem = match task.mem.parse::<Bytes>() {
323+
Ok(x) => x.size() as u64,
324+
Err(_) => { println!(
325+
"Error: Can't parse memory limit parameter, input={}",
326+
task.mem,
327+
);
328+
println!("\nPlease specify a number followed by a unit. If no unit is provided, bytes will be assumed.");
329+
println!("Supported units: B, KiB, MiB, GiB, TiB, PiB, EiB, KB, MB, GB, TB, PB, EB");
330+
println!("Example: --mem 10GiB\n");
331+
println!("Shutting down...");
332+
return Err("invalid unit");
333+
}
334+
};
335+
294336
if mem == 0 {
295337
mem = plotsize;
296338
}
@@ -306,7 +348,7 @@ fn calculate_mem_to_use(
306348

307349
// ensure a minimum buffer
308350
mem = max(mem, num_buffer * NONCE_SIZE * nonces_per_sector);
309-
mem
351+
Ok(mem)
310352
}
311353

312354
fn detect_simd() -> String {

src/writer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub fn create_writer_task(
4747

4848
let mut local_addr = scoop * buffer_size / NONCE_SIZE * SCOOP_SIZE;
4949
for _ in 0..nonces_to_write / TASK_SIZE {
50-
5150
file.write_all(
5251
&bs[local_addr as usize..(local_addr + TASK_SIZE * SCOOP_SIZE) as usize],
5352
).unwrap();
@@ -59,7 +58,8 @@ pub fn create_writer_task(
5958
if nonces_to_write % TASK_SIZE > 0 {
6059
file.write_all(
6160
&bs[local_addr as usize
62-
..(local_addr + ( nonces_to_write % TASK_SIZE ) * SCOOP_SIZE) as usize],
61+
..(local_addr + (nonces_to_write % TASK_SIZE) * SCOOP_SIZE)
62+
as usize],
6363
).unwrap();
6464
}
6565

0 commit comments

Comments
 (0)