Skip to content

Commit

Permalink
Removing dependency to num_cpus (as the info is available in the std (#…
Browse files Browse the repository at this point in the history
…4947)

* Removing dependency to num_cpus (as the info is available in the std
nowadays).

* Added error behavior following CR
  • Loading branch information
fulmicoton authored May 8, 2024
1 parent 1cf04aa commit 1b0c876
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 18 deletions.
3 changes: 0 additions & 3 deletions quickwit/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion quickwit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ mockall = "0.11"
mrecordlog = { git = "https://github.com/quickwit-oss/mrecordlog", rev = "306c0a7" }
new_string_template = "1.5.1"
nom = "7.1.3"
num_cpus = "1"
numfmt = "1.1.1"
once_cell = "1"
oneshot = "0.1.5"
Expand Down
1 change: 0 additions & 1 deletion quickwit/quickwit-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ hostname = { workspace = true }
http = { workspace = true }
hyper = { workspace = true }
itertools = { workspace = true }
num_cpus = { workspace = true }
once_cell = { workspace = true }
pin-project = { workspace = true }
pnet = { workspace = true }
Expand Down
12 changes: 12 additions & 0 deletions quickwit/quickwit-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ pub const fn div_ceil(lhs: i64, rhs: i64) -> i64 {
}
}

/// Return the number of vCPU/hyperthreads available.
/// This number is usually not equal to the number of cpu cores
pub fn num_cpus() -> usize {
match std::thread::available_parallelism() {
Ok(num_cpus) => num_cpus.get(),
Err(io_err) => {
error!(err=?io_err, "fail to detect the amount of threads available. arbitrarily returning 2");
2
}
}
}

// The following are helpers to build named tasks.
//
// Named tasks require the tokio feature `tracing` to be enabled.
Expand Down
2 changes: 1 addition & 1 deletion quickwit/quickwit-common/src/runtimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl RuntimesConfig {

impl Default for RuntimesConfig {
fn default() -> Self {
let num_cpus = num_cpus::get();
let num_cpus = crate::num_cpus();
Self::with_num_cpus(num_cpus)
}
}
Expand Down
1 change: 0 additions & 1 deletion quickwit/quickwit-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ humantime = { workspace = true }
itertools = { workspace = true }
json_comments = { workspace = true }
new_string_template = { workspace = true }
num_cpus = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions quickwit/quickwit-config/src/node_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ impl IndexerConfig {
}

pub fn default_merge_concurrency() -> NonZeroUsize {
NonZeroUsize::new(num_cpus::get() * 2 / 3).unwrap_or(NonZeroUsize::new(1).unwrap())
NonZeroUsize::new(quickwit_common::num_cpus() * 2 / 3)
.unwrap_or(NonZeroUsize::new(1).unwrap())
}

fn default_cpu_capacity() -> CpuCapacity {
CpuCapacity::one_cpu_thread() * (num_cpus::get() as u32)
CpuCapacity::one_cpu_thread() * (quickwit_common::num_cpus() as u32)
}

#[cfg(any(test, feature = "testsuite"))]
Expand Down
1 change: 0 additions & 1 deletion quickwit/quickwit-serve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ humantime = { workspace = true }
hyper = { workspace = true }
itertools = { workspace = true }
mime_guess = { workspace = true }
num_cpus = { workspace = true }
once_cell = { workspace = true }
opentelemetry = { workspace = true }
percent-encoding = { workspace = true }
Expand Down
13 changes: 6 additions & 7 deletions quickwit/quickwit-serve/src/build_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ impl BuildInfo {

#[derive(Debug, Eq, PartialEq, Serialize, utoipa::ToSchema)]
pub struct RuntimeInfo {
pub num_cpus_logical: usize,
pub num_cpus_physical: usize,
// This is a number of logical cpus: vCPU or hyperthread depending on where you are running.
// This is usually NOT necessarily the number of cores.
pub num_cpus: usize,
pub num_threads_blocking: usize,
pub num_threads_non_blocking: usize,
}
Expand All @@ -108,12 +109,10 @@ impl RuntimeInfo {
static INSTANCE: OnceCell<RuntimeInfo> = OnceCell::new();

INSTANCE.get_or_init(|| {
let num_cpus_logical = num_cpus::get();
let runtimes_config = RuntimesConfig::with_num_cpus(num_cpus_logical);

let num_cpus = quickwit_common::num_cpus();
let runtimes_config = RuntimesConfig::with_num_cpus(num_cpus);
Self {
num_cpus_logical,
num_cpus_physical: num_cpus::get_physical(),
num_cpus,
num_threads_blocking: runtimes_config.num_threads_blocking,
num_threads_non_blocking: runtimes_config.num_threads_non_blocking,
}
Expand Down
2 changes: 1 addition & 1 deletion quickwit/quickwit-serve/src/node_info_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod tests {

let runtime_info_json = info_json.get("runtime").unwrap();
let expected_runtime_info_json = serde_json::json!({
"num_cpus_physical": runtime_info.num_cpus_physical,
"num_cpus": runtime_info.num_cpus,
});
assert_json_include!(
actual: runtime_info_json,
Expand Down

0 comments on commit 1b0c876

Please sign in to comment.