GNU/Linux, Open Source, Cloud Computing, DevOps and more...

How to know how many cores and processors has a Linux box

1 comment

The simplest and shortest method to measure the number of processors present on a Linux box, which is also widely extended as it’s part of coreutils is:

~$ nproc --all
8

Another way to get the same result which also allows us to obtain additional information from our processor are the lscpu command:

~$ lscpu | grep 'CPU(s)'
CPU(s):                8
On-line CPU(s) list:   0-7
NUMA node0 CPU(s):     0-7

Or we can examine the cpuinfo file from /proc filesystem:

~$ grep processor /proc/cpuinfo | wc -l
8

There are many additional details about these processors in /proc/cpuinfo file, including the CPU model and number of cores:

~$ cat /proc/cpuinfo | grep "model name"
model name	: Intel(R) Xeon(R) CPU           E5520  @ 2.27GHz
model name	: Intel(R) Xeon(R) CPU           E5520  @ 2.27GHz
~$ cat /proc/cpuinfo | grep "cpu cores"
cpu cores	: 4
cpu cores	: 4

As you can see, the machine in our example has 2 processors with 4 cores each.

Recommended reading:
  Differences between physical CPU vs logical CPU vs Core vs Thread vs Socket

Due to HyperThreading technology and other processor enhancements, we might need to differentiate between physical and logical CPUs:

# Get number of physical cpus/cores:
~$ lscpu -p | egrep -v '^#' | wc -l
4
# Get number of logical cpus (including those reported by hyperthreading cores):
~$ lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l
8

The equivalent of the above for Mac OS X:

# Get number of physical cpus/cores:
~$ sysctl -n hw.physicalcpu_max
4
# Get number of logical cpus (including those reported by hyperthreading cores):
~$ sysctl -n hw.logicalcpu_max
8

Another portable method that works on old boxes, both Linux and Mac OS X, which don’t have nproc or lscpu commands available is using the getconf command:

~$ getconf _NPROCESSORS_ONLN
8

We also can use alternative tools to get hardware information like dmidecode, but we’ll need root privileges:

~# dmidecode -t 4 | egrep 'Socket Designation|Count'
        Socket Designation: CPUSocket
        Core Count: 8
        Thread Count: 8

If running the above commands you find out that there are multiple CPUs or multiple cores present in your system, you’ll want to also make sure that your current Linux kernel has multiprocessor support (SMP, Symmetric MultiProcessing):

~# uname -a
Linux sasser 2.6.18-028stab092.1 #1 SMP Wed Jul 20 19:47:12 MSD 2011 i686 GNU/Linux


 

About the author

Daniel López Azaña
Cloud Solutions Architect

Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, DevOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

DanielHow to know how many cores and processors has a Linux box

Related Posts

1 comment

Join the conversation
  • ronald - 28/10/2012 reply

    Or, in my case:
    grep -i processor /proc/cpuinfo | wc -l

Leave a Reply

Your email address will not be published. Required fields are marked *