diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2021-06-07 09:39:30 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2021-06-07 09:39:30 +0800 |
commit | fe9611a7fd9a1592cc1a4517948b4a32fba904c9 (patch) | |
tree | 88d3e1f5095760d3ded974711e81aa22abcf94a7 | |
parent | c3c103eebd82fc729788694a9f3bfd4ded855cf8 (diff) |
Set default processor count on Apple Silicon in a way that won't be totally wrong in future
Soon new hardware will be revealed which probably acts differently.
-rw-r--r-- | Cargo.lock | 34 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/main.rs | 38 | ||||
-rw-r--r-- | src/options.rs | 17 |
4 files changed, 76 insertions, 16 deletions
@@ -103,6 +103,12 @@ dependencies = [ ] [[package]] +name = "core-foundation-sys" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" + +[[package]] name = "crossbeam" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -224,6 +230,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" [[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + +[[package]] name = "dua-cli" version = "2.12.1" dependencies = [ @@ -240,6 +252,7 @@ dependencies = [ "open", "petgraph", "pretty_assertions", + "sysinfo", "tui", "tui-react", "unicode-segmentation", @@ -422,6 +435,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" [[package]] +name = "once_cell" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" + +[[package]] name = "open" version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -628,6 +647,21 @@ dependencies = [ ] [[package]] +name = "sysinfo" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d404aefa651a24a7f2a1190fec9fb6380ba84ac511a6fefad79eb0e63d39a97d" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "doc-comment", + "libc", + "ntapi", + "once_cell", + "winapi", +] + +[[package]] name = "termcolor" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -36,6 +36,9 @@ tui-react = { version = "0.15.0", optional = true } open = { version = "1.2.2", optional = true } wild = "2.0.4" +[target.'cfg(all(target_os = "macos", target_arch = "aarch64"))'.dependencies] +sysinfo = { version = "0.18.2", default-features = false } + [[bin]] name="dua" path="src/main.rs" diff --git a/src/main.rs b/src/main.rs index aa9d82e..1f523ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,12 +17,48 @@ fn stderr_if_tty() -> Option<io::Stderr> { } } +#[cfg(not(all(target_os = "macos", target_arch = "aarch64"),))] +fn derive_default_threads(threads: usize) -> usize { + threads +} + +/// On Apple Silicon, high-efficiency cores make file accesses slower over all, so avoid over committing for +/// this one. +/// +/// On macos with apple silicon, the IO subsystem is entirely different and one thread can mostly max it out. +/// Thus using more threads just burns energy unnecessarily. +/// It's notable that `du` is very fast even on a single core and more power efficient than dua with a single core. +/// The default of '4' seems related to the amount of performance cores present in the system. +/// On everything else, it's usually a good idea to use as many threads as possible for noticeable speedups. +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] +fn derive_default_threads(threads: usize) -> usize { + use sysinfo::{ProcessorExt, RefreshKind, SystemExt}; + if threads == 0 { + sysinfo::System::new_with_specifics(RefreshKind::new().with_cpu()) + .get_processors() + .get(0) + .map_or(0, |p| match p.get_brand() { + "Apple M1" => 4, + other => { + eprintln!( + "Couldn't auto-configure correct amount of threads for {}", + other + ); + 0 + } + }) + } else { + threads + } +} + fn main() -> Result<()> { use options::Command::*; let opt: options::Args = options::Args::parse_from(wild::args_os()); + let threads = derive_default_threads(opt.threads); let walk_options = dua::WalkOptions { - threads: opt.threads, + threads, byte_format: opt.format.map(Into::into).unwrap_or(ByteFormat::Metric), apparent_size: opt.apparent_size, count_hard_links: opt.count_hard_links, diff --git a/src/options.rs b/src/options.rs index d8167c5..30cf865 100644 --- a/src/options.rs +++ b/src/options.rs @@ -59,22 +59,9 @@ pub struct Args { #[clap(subcommand)] pub command: Option<Command>, - /// The amount of threads to use. Defaults to the amount of logical processors. + /// The amount of threads to use. Defaults to 0, indicating the amount of logical processors. /// Set to 1 to use only a single thread. - #[clap(short = 't', long = "threads")] - // On macos with apple silicon, the IO subsystem is entirely different and one thread can mostly max it out. - // Thus using more threads just burns energy unnecessarily. - // It's notable that `du` is very fast even on a single core and more power efficient than dua with a single core. - // The default of '4' seems related to the amount of performance cores present in the system. - #[cfg_attr( - all(target_os = "macos", target_arch = "aarch64"), - clap(default_value = "4") - )] - // On everything else, it's usually a good idea to use as many threads as possible for noticeable speedups. - #[cfg_attr( - not(all(target_os = "macos", target_arch = "aarch64")), - clap(default_value = "0") - )] + #[clap(short = 't', long = "threads", default_value = "0")] pub threads: usize, /// The format with which to print byte counts. |