From 1988bc8d3782ce29fcd223b184fb78a141f5f4f8 Mon Sep 17 00:00:00 2001
From: Weihang Lo <me@weihanglo.tw>
Date: Sat, 26 Feb 2022 15:47:33 +0800
Subject: [PATCH 1/2] Use `available_parallelism` instead of num_cpus

`std::thread::available_parallelism` has been stabilized since 1.59.0.

Also, we don't want to block timing data output, so if parallelism
data is not available the table will display `ncpu=n/a` instead.
---
 Cargo.toml                              | 1 -
 src/cargo/core/compiler/build_config.rs | 6 +++++-
 src/cargo/core/compiler/timings.rs      | 6 +++++-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 9110ab5aba6..baa20a8c7b8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -43,7 +43,6 @@ libc = "0.2"
 log = "0.4.6"
 libgit2-sys = "0.12.24"
 memchr = "2.1.3"
-num_cpus = "1.0"
 opener = "0.5"
 os_info = "3.0.7"
 percent-encoding = "2.0"
diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs
index e7964afe72b..54b2fd9b12c 100644
--- a/src/cargo/core/compiler/build_config.rs
+++ b/src/cargo/core/compiler/build_config.rs
@@ -6,6 +6,7 @@ use cargo_util::ProcessBuilder;
 use serde::ser;
 use std::cell::RefCell;
 use std::path::PathBuf;
+use std::thread::available_parallelism;
 
 /// Configuration information for a rustc build.
 #[derive(Debug)]
@@ -70,7 +71,10 @@ impl BuildConfig {
                  its environment, ignoring the `-j` parameter",
             )?;
         }
-        let jobs = jobs.or(cfg.jobs).unwrap_or(::num_cpus::get() as u32);
+        let jobs = match jobs.or(cfg.jobs) {
+            Some(j) => j,
+            None => available_parallelism()?.get() as u32,
+        };
         if jobs == 0 {
             anyhow::bail!("jobs may not be 0");
         }
diff --git a/src/cargo/core/compiler/timings.rs b/src/cargo/core/compiler/timings.rs
index 77dcec94f6d..9242af9d83c 100644
--- a/src/cargo/core/compiler/timings.rs
+++ b/src/cargo/core/compiler/timings.rs
@@ -13,6 +13,7 @@ use anyhow::Context as _;
 use cargo_util::paths;
 use std::collections::HashMap;
 use std::io::{BufWriter, Write};
+use std::thread::available_parallelism;
 use std::time::{Duration, Instant, SystemTime};
 
 pub struct Timings<'cfg> {
@@ -380,6 +381,9 @@ impl<'cfg> Timings<'cfg> {
         };
         let total_time = format!("{:.1}s{}", duration, time_human);
         let max_concurrency = self.concurrency.iter().map(|c| c.active).max().unwrap();
+        let num_cpus = available_parallelism()
+            .map(|x| x.get().to_string())
+            .unwrap_or_else(|_| "n/a".into());
         let max_rustc_concurrency = self
             .concurrency
             .iter()
@@ -442,7 +446,7 @@ impl<'cfg> Timings<'cfg> {
             self.total_fresh + self.total_dirty,
             max_concurrency,
             bcx.build_config.jobs,
-            num_cpus::get(),
+            num_cpus,
             self.start_str,
             total_time,
             rustc_info,

From 4f706ae8d5877b09520e1ebc616165cac3c499e2 Mon Sep 17 00:00:00 2001
From: Weihang Lo <me@weihanglo.tw>
Date: Wed, 2 Mar 2022 19:41:31 +0800
Subject: [PATCH 2/2] Wrap an error context for determining the amount of
 parallelism

---
 src/cargo/core/compiler/build_config.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs
index 54b2fd9b12c..31cd3e3da53 100644
--- a/src/cargo/core/compiler/build_config.rs
+++ b/src/cargo/core/compiler/build_config.rs
@@ -1,7 +1,7 @@
 use crate::core::compiler::CompileKind;
 use crate::util::interning::InternedString;
 use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
-use anyhow::bail;
+use anyhow::{bail, Context as _};
 use cargo_util::ProcessBuilder;
 use serde::ser;
 use std::cell::RefCell;
@@ -73,7 +73,9 @@ impl BuildConfig {
         }
         let jobs = match jobs.or(cfg.jobs) {
             Some(j) => j,
-            None => available_parallelism()?.get() as u32,
+            None => available_parallelism()
+                .context("failed to determine the amount of parallelism available")?
+                .get() as u32,
         };
         if jobs == 0 {
             anyhow::bail!("jobs may not be 0");