14 Methods To Check Number Of CPU Cores On Linux

Most of the system running in Multi-core CPU processors so, many of us get confused to find or understand about CPU, Cores, and Threads information. I hope this tutorial will help you to understand exactly what it is.

What's CPU Socket: CPU socket or CPU slot is the connector on the motherboard that allows a computer processor to be connected to a motherboard. It’s called a physical CPU (central processing unit).

What's CPU Core: Originally, CPUs had a single core but manufacturers added additional cores in to that to increase performance, that’s why core came to picture. For example, a dual-core CPU has two central processing units, so it appears to the operating system as two CPUs.

like that, a quad-core CPU has four central processing units and an octa-core CPU has eight central processing units.

What's CPU Thread: Intel Hyper-Threading Technology uses processor resources more efficiently by enabling multiple threads to run on each core (each core can run two threads). It also increases processor throughput and improving overall performance on threaded software.

Refer the following details to understand this in real time.

CPU(s):                32
On-line CPU(s) list:   0-31
Thread(s) per core:    2
Core(s) per socket:    8
Socket(s):             2

Two Physical CPU (2), Each CPU had (8) cores, and Per core had (2) Threads.

The calculation is below: CPUs = Threads per core X cores per socket X sockets.
2 x 8 x 2 = 32

Method-1 : Using /proc/cpuinfo file

/proc/cpuinfo is a virtual text file that contains information about the CPUs (central processing units) on a computer. We can get a number of CPUs by grepping processor parameter.

# grep -c ^processor /proc/cpuinfo

Method-2 : Using nproc Command

nproc – print the number of processing units available to the current process. It’s part of GNU Coreutils.

# nproc

Method-3 : Using lscpu Command

lscpu – display information on CPU architecture and gathers CPU architecture information like number of CPUs, threads, cores, sockets, NUMA nodes, information about CPU caches, CPU family, model and prints it in a human-readable format.

# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                32
On-line CPU(s) list:   0-31
Thread(s) per core:    2
Core(s) per socket:    8
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 45
Model name:            Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00GHz
Stepping:              7
CPU MHz:               1200.000
BogoMIPS:              3999.42
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              20480K
NUMA node0 CPU(s):     0-7,16-23
NUMA node1 CPU(s):     8-15,24-31

Alternately use the lscpu command to print only number of processor.

# lscpu | grep 'CPU(s):' | head -1 | awk '{print $2}'

Method-4 : Using getconf command

getconf stands for get configuration values. getconf utility used to write the value of the variable specified by the system_var & path_var operand. The value of each configuration variable were obtained from IEEE Std 1003.1-2001


Method-5 : Using dmidecode command

Dmidecode is a tool which reads a computer’s DMI (stands for Desktop Management Interface) (some say SMBIOS – stands for System Management BIOS) table contents and display system hardware information in a human-readable format.

Suggested Read : Dmidecode – Easy Way To Get Linux System Hardware Information

This table contains a description of the system’s hardware components, as well as other useful information such as serial number, Manufacturer information, Release Date, and BIOS revision, etc,.,

# dmidecode -t processor | egrep 'Designation|Count'
        Socket Designation: Socket 1
        Core Count: 8
        Thread Count: 16
        Socket Designation: Socket 2
        Core Count: 8
        Thread Count: 16

Total CPUs = CPU1 Thread Count + CPU2 Thread Count

Method-6 : Using inxi command

inxi is a nifty tool to check hardware information on Linux and offers wide range of option to get all the hardware information on Linux system that i never found in any other utility which are available in Linux. It was forked from the ancient and mindbendingly perverse yet ingenius infobash, by locsmif.

Suggested Read : inxi – A Great Tool to Check Hardware Information on Linux

# inxi -C
CPU(s):    2 Octa core Intel Xeon E5-2650 0s (-HT-MCP-SMP-) cache: 40960 KB
           clock speeds: max: 2001 MHz 1: 1200 MHz 2: 1200 MHz 3: 1200 MHz 4: 1200 MHz 5: 1200 MHz 6: 1200 MHz
           7: 1200 MHz 8: 1200 MHz 9: 1200 MHz 10: 1200 MHz 11: 1200 MHz 12: 1200 MHz 13: 1200 MHz 14: 1200 MHz
           15: 1200 MHz 16: 1200 MHz 17: 1200 MHz 18: 1200 MHz 19: 1200 MHz 20: 1200 MHz 21: 1200 MHz
           22: 1200 MHz 23: 1200 MHz 24: 1200 MHz 25: 1200 MHz 26: 1200 MHz 27: 1200 MHz 28: 1200 MHz
           29: 1200 MHz 30: 1200 MHz 31: 1200 MHz 32: 1200 MHz

Total 32 CPUs listed above

Method-7 : Using hwinfo command

hwinfo stands for hardware information tool is another great utility that used to probe for the hardware present in the system and display detailed information about varies hardware components in human readable format.

Suggested Read : hwinfo (Hardware Info) – A Nifty Tool To Detect System Hardware Information On Linux

It reports information about CPU, RAM, keyboard, mouse, graphics card, sound, storage, network interface, disk, partition, bios, and bridge, etc,.,

# hwinfo --short --cpu
                       Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz, 2000 MHz
                       Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz, 2000 MHz
                       Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz, 2666 MHz
                       Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz, 2000 MHz

Total 4 CPUs listed above

Method-8 : Using Top command

The top command provides a dynamic real-time view of a running system process as well as a list of tasks currently being managed by the Linux kernel. By default the top command shows combined of all cpu’s, if you want to print all the cpu’s output in the top command press 1 “Numeric One” while running top utility.

Suggested Read : TOP Command Examples to Monitor Server Performance

Method-9 : Using htop command

Htop is a open-source ncurses-based process viewer for Linux. It is similar to top, but allows you to scroll vertically and horizontally, so you can see all the processes running on the system, along with their full command lines. Tasks related to processes (killing, renicing) can be done without entering their PIDs.

Suggested Read : Monitor system resources using Htop command

Method-10 : Using CorFreq Tool

CoreFreq is a CPU monitoring software designed for Intel 64-bits Processors and supported architectures are Atom, Core2, Nehalem, SandyBridge and superior, AMD Family 0F. CoreFreq provides a framework to retrieve CPU data with a high degree of precision. By default CoreFreq output displays number of CUP cores in the system.

Suggested Read : CoreFreq – A Powerful CPU monitoring Tool for Linux Systems

Method-11 : Using /sys/devices/system/cpu/ file

Use the /sys/devices/system/cpu/ interface to get a processor information. It’s a collection of both global and individual CPU attributes. Individual CPU attributes are contained in sub-directories named by the kernel’s logical CPU number

  • online: cpus that are online and being scheduled.
  • possible: cpus that have been allocated resources and can be brought online if they are present.
  • present: cpus that have been identified as being present in the system.
# cat /sys/devices/system/cpu/online
# cat /sys/devices/system/cpu/possible
# cat /sys/devices/system/cpu/present

# echo $((`cat /sys/devices/system/cpu/present | sed 's/0-//'` + 1))
# echo $((`cat /sys/devices/system/cpu/online | sed 's/0-//'` + 1))
# echo $((`cat /sys/devices/system/cpu/possible | sed 's/0-//'` + 1))

Method-12 : Using /proc/interrupts file

This file /proc/interrupts contains information about the interrupts, like how many times processor has been interrupted.

# cat /proc/interrupts | egrep -i 'cpu'
            CPU0       CPU1       CPU2       CPU3       CPU4       CPU5       CPU6       CPU7       CPU8       CPU9       CPU10      CPU11      CPU12      CPU13     
CPU14      CPU15      CPU16      CPU17      CPU18      CPU19      CPU20      CPU21      CPU22      CPU23      CPU24      CPU25      CPU26      CPU27      CPU28     
CPU29      CPU30      CPU31

Method-13 : Using Python multiprocessing.cpu_count() method

You can count the number of cores on the system using python multiprocessing.cpu_count() method. Alternatively import os module using syscall getconf.

# python -c 'import multiprocessing as m; print m.cpu_count()'


# python -c 'import os; print os.sysconf(os.sysconf_names["SC_NPROCESSORS_ONLN"]);'

Method-14 : Using Python psutil library

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python.

# python
>>> import psutil
>>> psutil.cpu_count()

You may also like...