To determine total number of CPU's (offline and online) you'd want to parse the output of lscpu -ap.įravadona's answer is awesome and correct, but it requires the presence of lscpu. I don't want to go too far down the rabbit-hole, but you can also determine the number of configured processors (as opposed to simply available/online processors) via getconf _NPROCESSORS_CONF. To determine the number of processing units available (not necessarily the same as the number of cores). To determine the number of processors available: getconf _NPROCESSORS_ONLN Just to reiterate what others have said, there are a number of related properties. You can use one of the following methods to determine the number of physical CPU cores.Ĭount the number of unique core ids (roughly equivalent to grep -P '^core id\t' /proc/cpuinfo | sort -u | wc -l).Īwk '/^core id\t/ ' Xaekai sheds light on what a book is: "a book is a module that houses a circuit board with CPU sockets, RAM sockets, IO connections along the edge, and a hook for cooling system integration.
#Less proc cpuinfo code
If you save the code above as, say, shell script cpus, make it executable with chmod +x cpus and place it in folder in your $PATH, you'll see output such as the following: $ cpus If "books" are in play on a given Linux system ( does anybody know when and how?), the physical-CPU-count command may under-report (this is based on the assumption that lscpu reports IDs that are non-unique across higher-level entities e.g.: 2 different cores from 2 different sockets could have the same ID). Tip of the hat to for helping to correct the physical-CPU-count lscpu command.Ĭaveat: lscpu -p output does NOT include a "book" column (the man page mentions "books" as an entity between socket and node in the taxonomic hierarchy). Note that BSD-derived systems other than macOS - e.g., FreeBSD - only support the hw.ncpu key for sysctl, which are deprecated on macOS I'm unclear on which of the new keys hw.npu corresponds to: hw.(logical|physical)cpu_. # Counting the *unique* cores across lines tells us the # - in the case of hyperthreading - more logical CPUs. # 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.