Skip to content

Commit 3a17953

Browse files
committed
Auto merge of #10969 - joshtriplett:available-parallelism, r=epage
Switch back to `available_parallelism` This reverts commit 8345cf5 now that rust-lang/rust#97925 is merged. Since that time, there are now multiple calls to get the number of CPUs, to handle the `-j -NUM` case, so factor out a helper function.
2 parents 1fcbca8 + 8616384 commit 3a17953

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ libc = "0.2"
4444
log = "0.4.6"
4545
libgit2-sys = "0.13.2"
4646
memchr = "2.1.3"
47-
num_cpus = "1.0"
4847
opener = "0.5"
4948
os_info = "3.5.0"
5049
pathdiff = "0.2"

src/cargo/core/compiler/build_config.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::core::compiler::CompileKind;
22
use crate::util::interning::InternedString;
33
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
4-
use anyhow::bail;
4+
use anyhow::{bail, Context as _};
55
use cargo_util::ProcessBuilder;
66
use serde::ser;
77
use std::cell::RefCell;
88
use std::path::PathBuf;
9+
use std::thread::available_parallelism;
910

1011
/// Configuration information for a rustc build.
1112
#[derive(Debug)]
@@ -45,6 +46,12 @@ pub struct BuildConfig {
4546
pub timing_outputs: Vec<TimingOutput>,
4647
}
4748

49+
fn default_parallelism() -> CargoResult<u32> {
50+
Ok(available_parallelism()
51+
.context("failed to determine the amount of parallelism available")?
52+
.get() as u32)
53+
}
54+
4855
impl BuildConfig {
4956
/// Parses all config files to learn about build configuration. Currently
5057
/// configured options are:
@@ -71,9 +78,9 @@ impl BuildConfig {
7178
)?;
7279
}
7380
let jobs = match jobs.or(cfg.jobs) {
74-
None => ::num_cpus::get() as u32,
81+
None => default_parallelism()?,
7582
Some(0) => anyhow::bail!("jobs may not be 0"),
76-
Some(j) if j < 0 => (::num_cpus::get() as i32 + j).max(1) as u32,
83+
Some(j) if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32,
7784
Some(j) => j as u32,
7885
};
7986

src/cargo/core/compiler/timings.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use anyhow::Context as _;
1313
use cargo_util::paths;
1414
use std::collections::HashMap;
1515
use std::io::{BufWriter, Write};
16+
use std::thread::available_parallelism;
1617
use std::time::{Duration, Instant, SystemTime};
1718

1819
pub struct Timings<'cfg> {
@@ -380,6 +381,9 @@ impl<'cfg> Timings<'cfg> {
380381
};
381382
let total_time = format!("{:.1}s{}", duration, time_human);
382383
let max_concurrency = self.concurrency.iter().map(|c| c.active).max().unwrap();
384+
let num_cpus = available_parallelism()
385+
.map(|x| x.get().to_string())
386+
.unwrap_or_else(|_| "n/a".into());
383387
let max_rustc_concurrency = self
384388
.concurrency
385389
.iter()
@@ -442,7 +446,7 @@ impl<'cfg> Timings<'cfg> {
442446
self.total_fresh + self.total_dirty,
443447
max_concurrency,
444448
bcx.jobs(),
445-
num_cpus::get(),
449+
num_cpus,
446450
self.start_str,
447451
total_time,
448452
rustc_info,

0 commit comments

Comments
 (0)