Skip to content

Commit 420c970

Browse files
committed
Auto merge of rust-lang#97911 - dtolnay:numcpu, r=Mark-Simulacrum
Revert "remove num_cpus dependency" in rustc and update cargo Fixes rust-lang#97549. This PR reverts rust-lang#94524 and does a Cargo update to pull in rust-lang/cargo#10737. Rust 1.61.0 has a regression in which it misidentifies the number of available CPUs in some environments, leading to enormously increased memory usage and failing builds. In between Rust 1.60 and 1.61 both rustc and cargo replaced some uses of `num_cpus` with `available_parallelism`, which eliminated support for cgroupv1, still apparently in common use. This PR switches both rustc and cargo back to using `num_cpus` in order to support environments where the available parallelism is controlled by cgroupv1. Both can use `available_parallism` again once it handles cgroupv1 (if ever). I have confirmed that the rustc part of this PR fixes the memory usage regression in my non-Cargo environment, and others have confirmed in rust-lang#97549 that the Cargo regression was at fault for the memory usage regression in their environments.
2 parents d7b8d77 + fbc86e0 commit 420c970

File tree

10 files changed

+13
-8
lines changed

10 files changed

+13
-8
lines changed

Cargo.lock

+4
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ dependencies = [
219219
"hex 0.4.2",
220220
"ignore",
221221
"libc",
222+
"num_cpus",
222223
"once_cell",
223224
"opener",
224225
"pretty_assertions 0.7.2",
@@ -250,6 +251,7 @@ dependencies = [
250251
"anyhow",
251252
"flate2",
252253
"hex 0.4.2",
254+
"num_cpus",
253255
"rayon",
254256
"serde",
255257
"serde_json",
@@ -349,6 +351,7 @@ dependencies = [
349351
"libgit2-sys",
350352
"log",
351353
"memchr",
354+
"num_cpus",
352355
"opener",
353356
"openssl",
354357
"os_info",
@@ -4419,6 +4422,7 @@ name = "rustc_session"
44194422
version = "0.0.0"
44204423
dependencies = [
44214424
"getopts",
4425+
"num_cpus",
44224426
"rustc_ast",
44234427
"rustc_data_structures",
44244428
"rustc_errors",

compiler/rustc_session/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ rustc_serialize = { path = "../rustc_serialize" }
1515
rustc_data_structures = { path = "../rustc_data_structures" }
1616
rustc_span = { path = "../rustc_span" }
1717
rustc_fs_util = { path = "../rustc_fs_util" }
18+
num_cpus = "1.0"
1819
rustc_ast = { path = "../rustc_ast" }
1920
rustc_lint_defs = { path = "../rustc_lint_defs" }

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ mod parse {
578578
pub(crate) fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool {
579579
match v.and_then(|s| s.parse().ok()) {
580580
Some(0) => {
581-
*slot = std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get);
581+
*slot = ::num_cpus::get();
582582
true
583583
}
584584
Some(i) => {

src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ test = false
3737
[dependencies]
3838
cmake = "0.1.38"
3939
filetime = "0.2"
40+
num_cpus = "1.0"
4041
getopts = "0.2.19"
4142
cc = "1.0.69"
4243
libc = "0.2"

src/bootstrap/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ fn set<T>(field: &mut T, val: Option<T>) {
14181418

14191419
fn threads_from_config(v: u32) -> u32 {
14201420
match v {
1421-
0 => std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32,
1421+
0 => num_cpus::get() as u32,
14221422
n => n,
14231423
}
14241424
}

src/bootstrap/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
210210
let j_msg = format!(
211211
"number of jobs to run in parallel; \
212212
defaults to {} (this host's logical CPU count)",
213-
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
213+
num_cpus::get()
214214
);
215215
opts.optopt("j", "jobs", &j_msg, "JOBS");
216216
opts.optflag("h", "help", "print this help message");

src/bootstrap/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,7 @@ impl Build {
10321032
/// Returns the number of parallel jobs that have been configured for this
10331033
/// build.
10341034
fn jobs(&self) -> u32 {
1035-
self.config.jobs.unwrap_or_else(|| {
1036-
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1037-
})
1035+
self.config.jobs.unwrap_or_else(|| num_cpus::get() as u32)
10381036
}
10391037

10401038
fn debuginfo_map_to(&self, which: GitRepo) -> Option<String> {

src/tools/build-manifest/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ tar = "0.4.29"
1313
sha2 = "0.10.1"
1414
rayon = "1.5.1"
1515
hex = "0.4.2"
16+
num_cpus = "1.13.0"

src/tools/build-manifest/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn main() {
210210
let num_threads = if let Some(num) = env::var_os("BUILD_MANIFEST_NUM_THREADS") {
211211
num.to_str().unwrap().parse().expect("invalid number for BUILD_MANIFEST_NUM_THREADS")
212212
} else {
213-
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
213+
num_cpus::get()
214214
};
215215
rayon::ThreadPoolBuilder::new()
216216
.num_threads(num_threads)

0 commit comments

Comments
 (0)