Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nproc: add the full support of OMP_THREAD_LIMIT #3286

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/uu/nproc/src/nproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub const _SC_NPROCESSORS_CONF: libc::c_int = 1001;
static OPT_ALL: &str = "all";
static OPT_IGNORE: &str = "ignore";

static ABOUT: &str = "Print the number of cores available to the current process.";
static ABOUT: &str = r#"Print the number of cores available to the current process.
If the OMP_NUM_THREADS or OMP_THREAD_LIMIT environment variables are set, then
they will determine the minimum and maximum returned value respectively."#;
sylvestre marked this conversation as resolved.
Show resolved Hide resolved
const USAGE: &str = "{} [OPTIONS]...";

#[uucore::main]
Expand All @@ -45,6 +47,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
None => 0,
};

let limit = match env::var("OMP_THREAD_LIMIT") {
// Uses the OpenMP variable to limit the number of threads
// If the parsing fails, returns the max size (so, no impact)
Ok(threadstr) => threadstr.parse().unwrap_or(usize::MAX),
// the variable 'OMP_THREAD_LIMIT' doesn't exist
// fallback to the max
Err(_) => usize::MAX,
};

let mut cores = if matches.is_present(OPT_ALL) {
num_cpus_all()
} else {
Expand All @@ -53,12 +64,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// 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
// the variable 'OMP_NUM_THREADS' doesn't exist
// fallback to the regular CPU detection
Err(_) => num_cpus::get(),
}
};

cores = std::cmp::min(limit, cores);
if cores <= ignore {
cores = 1;
} else {
Expand Down
34 changes: 34 additions & 0 deletions tests/by-util/test_nproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,37 @@ fn test_nproc_ignore_all_omp() {
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert!(nproc == 2);
}

#[test]
fn test_nproc_omp_limit() {
let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.env("OMP_NUM_THREADS", "42")
.env("OMP_THREAD_LIMIT", "0")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert!(nproc == 1);

let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.env("OMP_NUM_THREADS", "42")
.env("OMP_THREAD_LIMIT", "2")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert!(nproc == 2);

let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.env("OMP_NUM_THREADS", "42")
.env("OMP_THREAD_LIMIT", "2bad")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert!(nproc == 42);

let result = TestScenario::new(util_name!())
.ucmd_keepenv()
.env("OMP_THREAD_LIMIT", "1")
.succeeds();
let nproc: u8 = result.stdout_str().trim().parse().unwrap();
assert!(nproc == 1);
}