111

I am running the following command to get the number of processors/cores in Linux:

cat /proc/cpuinfo | grep processor | wc -l

It works but it does not look elegant. How would you suggest improve it ?

Michael
  • 41,026
  • 70
  • 193
  • 341

11 Answers11

144

nproc is what you are looking for.

More here : http://www.cyberciti.biz/faq/linux-get-number-of-cpus-core-command/

ajduke
  • 4,991
  • 7
  • 36
  • 56
pax162
  • 4,735
  • 2
  • 22
  • 28
  • 11
    Nice - but not as ubiquitous as /proc/cpuinfo. `nproc` is there on my ubuntu VM, but not on my RedHat 5.5-based machine. – Digital Trauma Oct 27 '13 at 15:41
  • 8
    Make sure to `nproc --all` for all installed Processing Units. Without `--all`, `nproc` only shows Processing Units available to the current process. Read the man page for more details. MMV. – JamesThomasMoon Dec 02 '14 at 01:36
  • You should link to: https://www.gnu.org/software/coreutils/manual/html_node/nproc-invocation.html#nproc-invocation The cyberciti page does not any more information than what is already mentioned here except showing more ads. – Harald Hoyer Jul 22 '21 at 08:13
103

The most simplest tool comes with glibc and is called getconf:

$ getconf _NPROCESSORS_ONLN
4
Harald Hoyer
  • 1,303
  • 1
  • 8
  • 8
  • 9
    Even better, it's a POSIX utility: http://pubs.opengroup.org/onlinepubs/009604499/utilities/getconf.html . – BCran Nov 18 '14 at 06:38
  • 3
    unlike `nproc`, this works (by default) on mac os x. – Alec Jacobson Jul 30 '15 at 20:08
  • Unfortunately, while this utility is available on Solaris, `_NPROCESSORS_ONLN` (or anything with cpu, proc, etc) is listed in its output. I was hoping since it's a posix tool it'd work on Linux/Solaris so I didn't have to use branching. – Brian Vandenberg Sep 11 '15 at 16:59
40

I think the method you give is the most portable on Linux. Instead of spawning unnecessary cat and wc processes, you can shorten it a bit:

$ grep --count ^processor /proc/cpuinfo
2
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
27

If you want to do this so it works on linux and OS X, you can do:

CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)
tim_yates
  • 167,322
  • 27
  • 342
  • 338
14

On newer kernels you could also possibly use the the /sys/devices/system/cpu/ interface to get a bit more information:

$ ls /sys/devices/system/cpu/
cpu0  cpufreq  kernel_max  offline  possible  present  release
cpu1  cpuidle  modalias    online   power     probe    uevent
$ cat /sys/devices/system/cpu/kernel_max 
255
$ cat /sys/devices/system/cpu/offline 
2-63
$ cat /sys/devices/system/cpu/possible 
0-63
$ cat /sys/devices/system/cpu/present 
0-1
$ cat /sys/devices/system/cpu/online 
0-1

See the official docs for more information on what all these mean.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
  • 2
    nproc uses this, mostly ($OMP_NUM_THREADS then readdir sysfs). I think this one is the better interface. – Tobu Jan 07 '15 at 11:55
  • 2
    And getconf also relies on this interface (the `online` file, simpler than what nproc does) – Tobu Jan 07 '15 at 11:56
8

When someone asks for "the number of processors/cores" there are 2 answers being requested. The number of "processors" would be the physical number installed in sockets on the machine.

The number of "cores" would be physical cores. Hyperthreaded (virtual) cores would not be included (at least to my mind). As someone who writes a lot of programs with thread pools, you really need to know the count of physical cores vs cores/hyperthreads. That said, you can modify the following script to get the answers that you need.

#!/bin/bash

MODEL=`cat /cpu/procinfo | grep "model name" | sort | uniq`
ALL=`cat /proc/cpuinfo | grep "bogo" | wc -l`
PHYSICAL=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`
CORES=`cat /proc/cpuinfo | grep "cpu cores" | sort | uniq | cut -d':' -f2`
PHY_CORES=$(($PHYSICAL * $CORES))
echo "Type $MODEL"
echo "Processors $PHYSICAL"
echo "Physical cores $PHY_CORES"
echo "Including hyperthreading cores $ALL"

The result on a machine with 2 model Xeon X5650 physical processors each with 6 physical cores that also support hyperthreading:

Type model name : Intel(R) Xeon(R) CPU           X5650  @ 2.67GHz
Processors 2
Physical cores 12
Including hyperthreading cores 24

On a machine with 2 mdeol Xeon E5472 processors each with 4 physical cores that doesn't support hyperthreading

Type model name : Intel(R) Xeon(R) CPU           E5472  @ 3.00GHz
Processors 2
Physical cores 8
Including hyperthreading cores 8
rabinnh
  • 178
  • 1
  • 8
5

The lscpu(1) command provided by the util-linux project might also be useful:

$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 58
Model name:            Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
Stepping:              9
CPU MHz:               3406.253
CPU max MHz:           3600.0000
CPU min MHz:           1200.0000
BogoMIPS:              5787.10
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              4096K
NUMA node0 CPU(s):     0-3
mtk
  • 542
  • 5
  • 9
2

If you need an os independent method that works across Windows and Linux, you can use Python:

$ python -c 'import multiprocessing as m; print(m.cpu_count())'
16
Dave Liu
  • 906
  • 1
  • 11
  • 31
Meitham
  • 9,178
  • 5
  • 34
  • 45
1

This is for those who want to a portable way to count cpu cores on *bsd, *nix or solaris (haven't tested on aix and hp-ux but should work). It has always worked for me.

dmesg | \
egrep 'cpu[. ]?[0-9]+' | \
sed 's/^.*\(cpu[. ]*[0-9]*\).*$/\1/g' | \
sort -u | \
wc -l | \
tr -d ' '

solaris grep & egrep don't have -o option so sed is used instead.

gwillie
  • 1,893
  • 1
  • 12
  • 14
1

Another one-liner, without counting hyper-threaded cores:

lscpu | awk -F ":" '/Core/ { c=$2; }; /Socket/ { print c*$2 }' 
Marco
  • 824
  • 9
  • 13
0

Another portable way of doing this would be

node -p 'os.cpus().length'
xuesheng
  • 3,396
  • 2
  • 29
  • 38