Skip to content

Commit c8f02aa

Browse files
borsehuss
authored andcommitted
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.
1 parent 9210de2 commit c8f02aa

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
@@ -218,6 +218,7 @@ dependencies = [
218218
"getopts",
219219
"ignore",
220220
"libc",
221+
"num_cpus",
221222
"once_cell",
222223
"opener",
223224
"pretty_assertions",
@@ -247,6 +248,7 @@ dependencies = [
247248
"anyhow",
248249
"flate2",
249250
"hex 0.4.2",
251+
"num_cpus",
250252
"rayon",
251253
"serde",
252254
"serde_json",
@@ -346,6 +348,7 @@ dependencies = [
346348
"libgit2-sys",
347349
"log",
348350
"memchr",
351+
"num_cpus",
349352
"opener",
350353
"openssl",
351354
"os_info",
@@ -4338,6 +4341,7 @@ name = "rustc_session"
43384341
version = "0.0.0"
43394342
dependencies = [
43404343
"getopts",
4344+
"num_cpus",
43414345
"rustc_ast",
43424346
"rustc_data_structures",
43434347
"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
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
@@ -1345,7 +1345,7 @@ fn set<T>(field: &mut T, val: Option<T>) {
13451345

13461346
fn threads_from_config(v: u32) -> u32 {
13471347
match v {
1348-
0 => std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32,
1348+
0 => num_cpus::get() as u32,
13491349
n => n,
13501350
}
13511351
}

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
@@ -993,9 +993,7 @@ impl Build {
993993
/// Returns the number of parallel jobs that have been configured for this
994994
/// build.
995995
fn jobs(&self) -> u32 {
996-
self.config.jobs.unwrap_or_else(|| {
997-
std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
998-
})
996+
self.config.jobs.unwrap_or_else(|| num_cpus::get() as u32)
999997
}
1000998

1001999
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)