Skip to content

Commit cd1ca41

Browse files
committed
Attach io_thread_count() to Process
1 parent 978c0ab commit cd1ca41

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

src/diskio/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ use std::{fmt::Debug, fs::OpenOptions};
6666
use anyhow::Result;
6767

6868
use crate::process::Process;
69-
use crate::utils;
7069
use crate::utils::notifications::Notification;
7170
use threaded::PoolReference;
7271

@@ -449,7 +448,7 @@ pub(crate) fn get_executor<'a>(
449448
process: &Process,
450449
) -> anyhow::Result<Box<dyn Executor + 'a>> {
451450
// If this gets lots of use, consider exposing via the config file.
452-
Ok(match utils::io_thread_count(process)? {
451+
Ok(match process.io_thread_count()? {
453452
0 | 1 => Box::new(immediate::ImmediateUnpacker::new()),
454453
n => Box::new(threaded::Threaded::new(notify_handler, n, ram_budget)),
455454
})

src/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl InstallMethod<'_> {
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(self.cfg().process)?)
45+
.num_threads(self.cfg().process.io_thread_count()?)
4646
.build_global();
4747
let nh = &self.cfg().notify_handler;
4848
match self {

src/process.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use std::env;
21
use std::ffi::OsString;
32
use std::fmt::Debug;
43
use std::io;
54
use std::io::IsTerminal;
65
use std::path::PathBuf;
6+
use std::str::FromStr;
77
#[cfg(feature = "test")]
88
use std::{
99
collections::HashMap,
1010
io::Cursor,
1111
path::Path,
1212
sync::{Arc, Mutex},
1313
};
14+
use std::{env, thread};
1415

15-
use anyhow::{Context, Result};
16+
use anyhow::{Context, Result, bail};
1617
#[cfg(feature = "test")]
1718
use tracing::subscriber::DefaultGuard;
1819
#[cfg(feature = "test")]
@@ -64,6 +65,26 @@ impl Process {
6465
home::env::rustup_home_with_env(self).context("failed to determine rustup home dir")
6566
}
6667

68+
pub fn io_thread_count(&self) -> anyhow::Result<usize> {
69+
if let Ok(n) = self.var("RUSTUP_IO_THREADS") {
70+
let threads = usize::from_str(&n).context(
71+
"invalid value in RUSTUP_IO_THREADS -- must be a natural number greater than zero",
72+
)?;
73+
match threads {
74+
0 => bail!("RUSTUP_IO_THREADS must be a natural number greater than zero"),
75+
_ => return Ok(threads),
76+
}
77+
};
78+
79+
Ok(match thread::available_parallelism() {
80+
// Don't spawn more than 8 I/O threads unless the user tells us to.
81+
// Feel free to increase this value if it improves performance.
82+
Ok(threads) => Ord::min(threads.get(), 8),
83+
// Unknown for target platform or no permission to query.
84+
Err(_) => 1,
85+
})
86+
}
87+
6788
pub fn var(&self, key: &str) -> Result<String, env::VarError> {
6889
match self {
6990
Process::OsProcess(_) => env::var(key),

src/utils/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ 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;
11-
use std::thread;
1210

1311
use anyhow::{Context, Result, anyhow, bail};
1412
use retry::delay::{Fibonacci, jitter};
@@ -28,26 +26,6 @@ pub(crate) mod notify;
2826
pub mod raw;
2927
pub(crate) mod units;
3028

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() {
43-
// Don't spawn more than 8 I/O threads unless the user tells us to.
44-
// Feel free to increase this value if it improves performance.
45-
Ok(threads) => Ord::min(threads.get(), 8),
46-
// Unknown for target platform or no permission to query.
47-
Err(_) => 1,
48-
})
49-
}
50-
5129
#[must_use]
5230
#[derive(Debug, PartialEq, Eq)]
5331
pub struct ExitCode(pub i32);

0 commit comments

Comments
 (0)