

# Linux: The 2nd column contains the core ID, with each core ID having 1 or # which tells us the number of *logical* CPUs. # Linux: Simply count the number of (non-comment) output lines from `lscpu -p`, # Number of LOGICAL CPUs (includes those reported by hyper-threading cores) # systems, currently disabled (offline) CPUs are NOT

# `-p` reports *online* CPUs only - i.e., on hot-pluggable # generated by `-p` to support older distros, too. # formats, but we stick with the parseable legacy format # Note: Newer versions of `lscpu` support more flexible output # Linux: Parse output from `lscpu -p`, where each output line represents # number of *currently* available ones see below. # available dropping the "_max" suffix would report the # current power-management mode could make *fewer* CPUs # available number of CPUs is reported, whereas the # CAVEAT: Using the "_max" key suffixes means that the *maximum* # macOS: Use `sysctl -n hw.*cpu_max`, which returns the values of With each level comprising 1 or more instances of the next lower level. Linux uses the following taxonomy, starting with the smallest unit: Non-hyper-threading cores each correspond to 1 CPU, whereas hyper-threading cores contain more than 1 (typically: 2) - logical - CPU.

Terminology note: CPU refers to the smallest processing unit as seen by the OS. Uses lscpu for Linux, and sysctl for macOS. Here's a sh (POSIX-compliant) snippet that works on Linux and macOS for determining the number of - online - logical or physical CPUs see the comments for details. Getconf _NPROCESSORS_ONLN / getconf NPROCESSORS_ONLN doesn't distinguish between logical and physical CPUs. The problem with the /proc/cpuinfo-based answers is that they parse information that was meant for human consumption and thus lacks a stable format designed for machine parsing: the output format can differ across platforms and runtime conditions using lscpu -p on Linux (and sysctl on macOS) bypasses that problem.
