Skip to content

Commit

Permalink
sanitize parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
plebhash committed Oct 8, 2024
1 parent 785fcf0 commit 1142146
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions roles/test-utils/mining-device/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
Arc,
},
time::Duration,
thread::available_parallelism
};
use tokio::net::TcpStream;

Expand Down Expand Up @@ -198,8 +199,7 @@ fn open_channel(device_id: Option<String>, nominal_hashrate_multiplier: Option<f
let user_identity = device_id.unwrap_or_default().try_into().unwrap();
let id: u32 = 10;
info!("Measuring CPU hashrate");
let p = std::thread::available_parallelism().unwrap().get() as u32 - 3;
let measured_hashrate = measure_hashrate(5, handicap) as f32 * p as f32;
let measured_hashrate = measure_hashrate(5, handicap) as f32;
info!("Measured CPU hashrate is {}", measured_hashrate);
let nominal_hash_rate = match nominal_hashrate_multiplier {
Some(m) => {
Expand Down Expand Up @@ -640,7 +640,13 @@ fn measure_hashrate(duration_secs: u64, handicap: u32) -> f64 {
}

let elapsed_secs = start_time.elapsed().as_secs_f64();
hashes as f64 / elapsed_secs
let hashrate_single_thread = hashes as f64 / elapsed_secs;

// we just measured for a single thread, need to multiply by the available parallelism
let p = available_parallelism().unwrap().get();


hashrate_single_thread * p as f64
}
fn generate_random_32_byte_array() -> [u8; 32] {
let mut rng = thread_rng();
Expand All @@ -657,11 +663,7 @@ fn start_mining_threads(
tokio::task::spawn(async move {
let mut killers: Vec<Arc<AtomicBool>> = vec![];
loop {
let available_parallelism = u32::max(
2,
std::thread::available_parallelism().unwrap().get() as u32,
);
let p = available_parallelism - 1;
let p = available_parallelism().unwrap().get() as u32;
let unit = u32::MAX / p;
while have_new_job.recv().await.is_ok() {
while let Some(killer) = killers.pop() {
Expand Down

0 comments on commit 1142146

Please sign in to comment.