Skip to content

Commit 978c0ab

Browse files
committed
Always consider RUSTUP_IO_THREADS as input for thread count
1 parent c8d8585 commit 978c0ab

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/diskio/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use std::sync::mpsc::Receiver;
6363
use std::time::{Duration, Instant};
6464
use std::{fmt::Debug, fs::OpenOptions};
6565

66-
use anyhow::{Context, Result};
66+
use anyhow::Result;
6767

6868
use crate::process::Process;
6969
use crate::utils;
@@ -447,15 +447,9 @@ pub(crate) fn get_executor<'a>(
447447
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
448448
ram_budget: usize,
449449
process: &Process,
450-
) -> Result<Box<dyn Executor + 'a>> {
450+
) -> anyhow::Result<Box<dyn Executor + 'a>> {
451451
// If this gets lots of use, consider exposing via the config file.
452-
let thread_count = match process.var("RUSTUP_IO_THREADS") {
453-
Err(_) => utils::io_thread_count(),
454-
Ok(n) => n
455-
.parse::<usize>()
456-
.context("invalid value in RUSTUP_IO_THREADS. Must be a natural number")?,
457-
};
458-
Ok(match thread_count {
452+
Ok(match utils::io_thread_count(process)? {
459453
0 | 1 => Box::new(immediate::ImmediateUnpacker::new()),
460454
n => Box::new(threaded::Threaded::new(notify_handler, n, ram_budget)),
461455
})

src/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ pub(crate) enum InstallMethod<'a> {
3838
impl InstallMethod<'_> {
3939
// Install a toolchain
4040
#[tracing::instrument(level = "trace", err(level = "trace"), skip_all)]
41-
pub(crate) async fn install(&self) -> Result<UpdateStatus> {
41+
pub(crate) async fn install(&self) -> anyhow::Result<UpdateStatus> {
4242
// Initialize rayon for use by the remove_dir_all crate limiting the number of threads.
4343
// This will error if rayon is already initialized but it's fine to ignore that.
4444
let _ = rayon::ThreadPoolBuilder::new()
45-
.num_threads(utils::io_thread_count())
45+
.num_threads(utils::io_thread_count(self.cfg().process)?)
4646
.build_global();
4747
let nh = &self.cfg().notify_handler;
4848
match self {

src/utils/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::io::{self, BufReader, Write};
77
use std::ops::{BitAnd, BitAndAssign};
88
use std::path::{Path, PathBuf};
99
use std::process::ExitStatus;
10+
use std::str::FromStr;
1011
use std::thread;
1112

1213
use anyhow::{Context, Result, anyhow, bail};
@@ -27,14 +28,24 @@ pub(crate) mod notify;
2728
pub mod raw;
2829
pub(crate) mod units;
2930

30-
pub fn io_thread_count() -> usize {
31-
match thread::available_parallelism() {
31+
pub fn io_thread_count(process: &Process) -> anyhow::Result<usize> {
32+
if let Ok(n) = process.var("RUSTUP_IO_THREADS") {
33+
let threads = usize::from_str(&n).context(
34+
"invalid value in RUSTUP_IO_THREADS -- must be a natural number greater than zero",
35+
)?;
36+
match threads {
37+
0 => bail!("RUSTUP_IO_THREADS must be a natural number greater than zero"),
38+
_ => return Ok(threads),
39+
}
40+
};
41+
42+
Ok(match thread::available_parallelism() {
3243
// Don't spawn more than 8 I/O threads unless the user tells us to.
3344
// Feel free to increase this value if it improves performance.
3445
Ok(threads) => Ord::min(threads.get(), 8),
3546
// Unknown for target platform or no permission to query.
3647
Err(_) => 1,
37-
}
48+
})
3849
}
3950

4051
#[must_use]

0 commit comments

Comments
 (0)