From 2c6bbcf71628d20d1cec270a3479d58b098771f3 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 12 Mar 2022 00:33:18 +0100 Subject: [PATCH 1/2] nproc: support --ignore=' 1' as GNU --- src/uu/nproc/src/nproc.rs | 2 +- tests/by-util/test_nproc.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/uu/nproc/src/nproc.rs b/src/uu/nproc/src/nproc.rs index de8f0c3ed..72ae9cd65 100644 --- a/src/uu/nproc/src/nproc.rs +++ b/src/uu/nproc/src/nproc.rs @@ -33,7 +33,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().get_matches_from(args); let mut ignore = match matches.value_of(OPT_IGNORE) { - Some(numstr) => match numstr.parse() { + Some(numstr) => match numstr.trim().parse() { Ok(num) => num, Err(e) => { return Err(USimpleError::new( diff --git a/tests/by-util/test_nproc.rs b/tests/by-util/test_nproc.rs index abf758829..43cfaa2d7 100644 --- a/tests/by-util/test_nproc.rs +++ b/tests/by-util/test_nproc.rs @@ -33,15 +33,22 @@ fn test_nproc_all_omp() { #[test] fn test_nproc_ignore() { let result = new_ucmd!().succeeds(); - let nproc: u8 = result.stdout_str().trim().parse().unwrap(); - if nproc > 1 { + let nproc_total: u8 = result.stdout_str().trim().parse().unwrap(); + if nproc_total > 1 { // Ignore all CPU but one let result = TestScenario::new(util_name!()) .ucmd_keepenv() .arg("--ignore") - .arg((nproc - 1).to_string()) + .arg((nproc_total - 1).to_string()) .succeeds(); let nproc: u8 = result.stdout_str().trim().parse().unwrap(); assert!(nproc == 1); + // Ignore all CPU but one with a string + let result = TestScenario::new(util_name!()) + .ucmd_keepenv() + .arg("--ignore= 1") + .succeeds(); + let nproc: u8 = result.stdout_str().trim().parse().unwrap(); + assert!(nproc_total - 1 == nproc); } } From 24b4af768c98616908c4757a2a30d7bc2b9a4ebc Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 13 Mar 2022 18:13:48 +0100 Subject: [PATCH 2/2] nproc: Improve the support of OMP_NUM_THREADS --- src/uu/nproc/src/nproc.rs | 20 ++++++++++---------- tests/by-util/test_nproc.rs | 24 ++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/uu/nproc/src/nproc.rs b/src/uu/nproc/src/nproc.rs index 72ae9cd65..f10b41e40 100644 --- a/src/uu/nproc/src/nproc.rs +++ b/src/uu/nproc/src/nproc.rs @@ -32,7 +32,7 @@ const USAGE: &str = "{} [OPTIONS]..."; pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().get_matches_from(args); - let mut ignore = match matches.value_of(OPT_IGNORE) { + let ignore = match matches.value_of(OPT_IGNORE) { Some(numstr) => match numstr.trim().parse() { Ok(num) => num, Err(e) => { @@ -45,18 +45,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { None => 0, }; - if !matches.is_present(OPT_ALL) { - // OMP_NUM_THREADS doesn't have an impact on --all - ignore += match env::var("OMP_NUM_THREADS") { - Ok(threadstr) => threadstr.parse().unwrap_or(0), - Err(_) => 0, - }; - } - let mut cores = if matches.is_present(OPT_ALL) { num_cpus_all() } else { - num_cpus::get() + // OMP_NUM_THREADS doesn't have an impact on --all + match env::var("OMP_NUM_THREADS") { + // Uses the OpenMP variable to force the number of threads + // If the parsing fails, returns the number of CPU + Ok(threadstr) => threadstr.parse().unwrap_or_else(|_| num_cpus::get()), + // the variable 'OMP_NUM_THREADS' doesn't exit + // fallback to the regular CPU detection + Err(_) => num_cpus::get(), + } }; if cores <= ignore { diff --git a/tests/by-util/test_nproc.rs b/tests/by-util/test_nproc.rs index 43cfaa2d7..5657e6b7e 100644 --- a/tests/by-util/test_nproc.rs +++ b/tests/by-util/test_nproc.rs @@ -1,3 +1,4 @@ +// spell-checker:ignore incorrectnumber use crate::common::util::*; #[test] @@ -15,11 +16,11 @@ fn test_nproc_all_omp() { let result = TestScenario::new(util_name!()) .ucmd_keepenv() - .env("OMP_NUM_THREADS", "1") + .env("OMP_NUM_THREADS", "60") .succeeds(); let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap(); - assert!(nproc - 1 == nproc_omp); + assert!(nproc_omp == 60); let result = TestScenario::new(util_name!()) .ucmd_keepenv() @@ -28,6 +29,14 @@ fn test_nproc_all_omp() { .succeeds(); let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap(); assert!(nproc == nproc_omp); + + // If the parsing fails, returns the number of CPU + let result = TestScenario::new(util_name!()) + .ucmd_keepenv() + .env("OMP_NUM_THREADS", "incorrectnumber") // returns the number CPU + .succeeds(); + let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap(); + assert!(nproc == nproc_omp); } #[test] @@ -52,3 +61,14 @@ fn test_nproc_ignore() { assert!(nproc_total - 1 == nproc); } } + +#[test] +fn test_nproc_ignore_all_omp() { + let result = TestScenario::new(util_name!()) + .ucmd_keepenv() + .env("OMP_NUM_THREADS", "42") + .arg("--ignore=40") + .succeeds(); + let nproc: u8 = result.stdout_str().trim().parse().unwrap(); + assert!(nproc == 2); +}