nprocx
Print the number of processing units available.
Mirrors GNU nproc. Run it as nprocx, or as gnux nprocx.
Synopsis
nprocx [OPTION]...GNU reference
Description
nprocx prints the number of processing units available to the current process. As in GNU nproc, the OMP_NUM_THREADS environment variable (its first value, when it is a comma-separated list) replaces that count when it is a positive number, and OMP_THREAD_LIMIT caps the result. --all prints the number of installed processors instead and ignores both variables. --ignore=N subtracts N, but the result is never less than 1. gnux has no cgo, so both counts come from Go's runtime.NumCPU: the processors this process may run on. A machine with processors taken offline, or a process restricted to fewer of them, prints the same number with and without --all.
Permissions
Needs no permissions. Reads only the process's CPU affinity and its environment.
Use cases
- Choosing how many parallel jobs a build should run.
- Leaving a core free for the rest of the system with
--ignore=1. - Respecting OMP_NUM_THREADS when a job scheduler has set it.
- Reporting the machine's size in a CI log or a bug report.
- Sizing a worker pool in a portable script.
Flags
--all print the number of installed processors
--help display this help and exit
--ignore N if possible, exclude N processing units (default "0")
--version output version information and exitExamples
Print the number of processing units
nprocxPrint the number of installed processors
nprocx --allLeave one core free
nprocx --ignore=1Run a build with one job per core
make -j"$(nprocx)"Respect an OpenMP thread count
OMP_NUM_THREADS=4 nprocxCap the count with OMP_THREAD_LIMIT
OMP_THREAD_LIMIT=2 nprocxSet the count through envx, on any platform
envx OMP_NUM_THREADS=3 nprocxSave the count in a variable
jobs=$(nprocx --ignore=2); echox "using $jobs jobs"Print the count with a label
printfx 'cores: %d\n' "$(nprocx)"Show the count in the portable gnux shell
gnux shell -c 'nprocx --all'Options
Each option has a page of its own, with its usage and examples.
Exit status
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake |
Help in the terminal
nprocx --help # options, first examples, exit status
gnux help nprocx # this page: the full manual
gnux help nprocx --OPTION # one option in depth, with its examples