Skip to content
This repository was archived by the owner on Aug 31, 2022. It is now read-only.

Commit 2994ded

Browse files
committed
Default to number of physical cores on Intel systems
Using the number of logical threads seems to cause drastic performance issues, making the miner unusable unless the thread count is set from the command line by the user
1 parent 445c27e commit 2994ded

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ itertools = "0.8.2"
2929
num_cpus = "1.11.1"
3030
crossbeam = "0.7.3"
3131
ring = "0.16.11"
32+
raw-cpuid = "7.0.3"
3233

3334
[profile.release]
3435
lto = true

src/miner/cpu/mod.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crossbeam::atomic::AtomicCell;
88
use crossbeam::channel::RecvTimeoutError;
99
use enumset::{EnumSet, EnumSetType};
1010
use itertools::Itertools;
11+
use raw_cpuid::CpuId;
12+
use std::cmp::max;
1113
use std::fmt::{self, Display, Formatter};
1214
use std::num::Wrapping;
1315
use std::str::FromStr;
@@ -68,7 +70,9 @@ impl Display for KernelType {
6870

6971
#[derive(Debug)]
7072
pub struct CpuInfo {
73+
cores: usize,
7174
threads: usize,
75+
default_miner_threads: usize,
7276
supported: EnumSet<KernelType>,
7377
}
7478

@@ -77,10 +81,13 @@ impl Display for CpuInfo {
7781
write!(
7882
f,
7983
"CPU:\n\
80-
\tThreads: {threads}\n\
84+
\tCores/threads: {cores}/{threads}\n\
85+
\tDefault miner threads: {miner_threads}\n\
8186
\tSupported kernels: {supported}\n\
8287
\tUnsupported kernels: {available}",
88+
cores = self.cores,
8389
threads = self.threads,
90+
miner_threads = self.default_miner_threads,
8491
supported = self.supported.iter().join(", "),
8592
available = (!self.supported).iter().join(", "),
8693
)
@@ -101,11 +108,33 @@ fn get_best_kernel() -> KernelType {
101108
Iterator::max(get_supported_kernels().iter()).unwrap_or_default()
102109
}
103110

111+
/// Some CPUs seem to really struggle when every thread is in use, to the point
112+
/// of making the miner entirely unusable. This is particularly prevalent on
113+
/// Intel CPUs, presumably due to the impact of spectre mitigations on
114+
/// hyperthreading. Thus, we choose a lower default number of threads on Intel
115+
/// hardware.
116+
fn get_best_thread_count() -> usize {
117+
let cores = num_cpus::get_physical();
118+
let threads = num_cpus::get();
119+
120+
match CpuId::new().get_vendor_info() {
121+
Some(v) if v.as_string().to_lowercase().contains("intel") => max(threads - 2, cores),
122+
_ => threads,
123+
}
124+
}
125+
104126
pub fn get_cpu_info() -> CpuInfo {
127+
let cores = num_cpus::get_physical();
105128
let threads = num_cpus::get();
129+
let default_miner_threads = get_best_thread_count();
106130
let supported = get_supported_kernels();
107131

108-
CpuInfo { threads, supported }
132+
CpuInfo {
133+
cores,
134+
threads,
135+
default_miner_threads,
136+
supported,
137+
}
109138
}
110139

111140
pub struct CpuMiner {
@@ -122,7 +151,7 @@ impl CpuMiner {
122151
}: &MinerConfig,
123152
) -> CpuMiner {
124153
CpuMiner {
125-
threads: cpu_threads.unwrap_or_else(num_cpus::get),
154+
threads: cpu_threads.unwrap_or_else(get_best_thread_count),
126155
kernel_type: cpu_kernel.unwrap_or_else(get_best_kernel),
127156
}
128157
}

0 commit comments

Comments
 (0)