14 Methods to check the number of CPUs in Linux

All Linux systems run on multi-core CPU processors, but many of us are confused to find or understand CPU, cores, and Threads information.

Hope this article helps you understand exactly what it is.

What’s a CPU Socket?

A CPU socket or CPU slot is a connector on the motherboard that allows a computer processor to connect to the motherboard. This is called a physical CPU (central processing unit).

What is the CPU Core?

Initially, CPUs use to come with single core, but manufacturers added more cores to increase performance, which is why the core came into the picture.

For example, a dual-core CPU has two central processing units, so it appears to be two CPUs per operating system.

Similarly, a quad core CPU has four central processing units and an octa core CPU has eight central processing units.

What is CPU Thread?

Intel’s hyper-threading technology makes it possible to use processor resources more efficiently by running multiple threads in each core (each core has two threads).

This improves processor throughput and improves overall performance in threaded software.

See the following details for better understanding of this in real-time. This is the sample data I took from my system.

CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    2
Core(s) per socket:    4
Socket(s):             1

A physical CPU (1), which has 4 CPU cores, and has 2 threads in each core.

The calculation is below: CPUs = Threads Per Core X Number of cores per socket X Number of CPU (Sockets).

Output: 2 x 4 x 1 = 8

Method-1: How to find the number Of CPUs on Linux using the “/proc/cpuinfo” file

The “/proc/cpuinfo” file is a virtual text file that contains information about CPUs (central processing units) on a computer.

It contains useful information like CPU architecture, vendor name, model name. You can only get a number of CPUs by using the grep command as follows.

$ grep -c ^processor /proc/cpuinfo

8

To view detailed information about the CPU, run the following command.

$ cat /proc/cpuinfo

processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 94
model name	: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
stepping	: 3
microcode	: 0xd6
cpu MHz		: 2577.190
cache size	: 6144 KB
physical id	: 0
siblings	: 8
core id		: 0
cpu cores	: 4
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit
bogomips	: 5184.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

Method-2: How to check the number of CPUs on Linux using the nproc Command

It prints the number of processing units available for the current process. It is part of the GNU Coreutils package, which is installed by default on most Linux systems.

$ nproc

8

Method-3: How to find the number of CPUs on Linux using the lscpu Command

The lscpu command displays information about the CPU architecture, which is collected from the sysfs and the /proc/cpuinfo file.

The information includes, number of CPUs, threads, cores, sockets, NUMA nodes, CPU caches, CPU family and model, which print infomration in human readable format.

$ lscpu

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           94
Model name:                      Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Stepping:                        3
CPU MHz:                         800.010
CPU max MHz:                     3500.0000
CPU min MHz:                     800.0000
BogoMIPS:                        5184.00
Virtualization:                  VT-x
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        1 MiB
L3 cache:                        6 MiB
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     KVM: Mitigation: Split huge pages
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Full generic retpoline, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT vulnerable
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon
                                  pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2a
                                 pic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept v
                                 pid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_not
                                 ify hwp_act_window hwp_epp md_clear flush_l1d

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

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

Method-4: How to check the number of CPUs on Linux using the 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

$ getconf _NPROCESSORS_ONLN
8

Method-5: How to find the number of CPUs on Linux using the 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.

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,.,

$ sudo dmidecode -t processor | egrep 'Designation|Count'
    
	Socket Designation: U3E1
	Core Count: 4
	Thread Count: 8

Total CPUs = CPU1 Thread Count
8=8

Method-6: How to check the number of CPUs on Linux using the inxi Command

inxi command is a nifty tool for verifying hardware information on Linux, and offers a wide range of options to get all the hardware information on a Linux system, which I haven’t seen in any other application available on Linux.

It shows system hardware, CPU, drivers, Xorg, Desktop, Kernel, gcc version(s), Processes, RAM usage, and a wide variety of other useful information.

$ inxi -C

CPU:       Topology: Quad Core model: Intel Core i7-6700HQ bits: 64 type: MT MCP L2 cache: 6144 KiB 
           Speed: 800 MHz min/max: 800/3500 MHz Core speeds (MHz): 1: 800 2: 800 3: 800 4: 800 5: 800 6: 800 7: 800 8: 800 

Totally 8 CPUs are listed above

Method-7: How to find the number of CPUs on Linux using the hwinfo Command

The hwinfo command is a hardware information tool that is used to analyze the hardware on a computer and to display detailed information about the variety of hardware components in a human readable format.

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

$ hwinfo --short --cpu
cpu:                                                            
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2666 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2666 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2636 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2675 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2650 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2690 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2646 MHz
                       Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2609 MHz

A total of 8 CPUs are listed above

Method-8: How to check the number of CPUs on Linux using the top Command

The top command provides a dynamic real-time view of the running system processes and a list of tasks currently managed by the Linux kernel.

By default, the top command displays the average of output for all CPUs, and if you want to print everything separately to the top command output, press 1 “number one” when running the top application.

Method-9: How to find the number of CPUs on Linux using the htop Command

The Htop command is an open source ncurses-based process viewer for Linux. This is similar to the top command, but allows you to scroll vertically and horizontally, so you can see all the processes running the system with their entire command line.

Process-related tasks (Killing, renicing) can be performed without entering their PIDs.

Method-10: How to check the number of CPUs on Linux using the CorFreq Command

CoreFreq is a CPU monitoring software designed for Intel 64-bits processors and supported architectures such as Atom, Core 2, Nehalem, SandyBridge and superior, AMD Family 0F.

CoreFreq provides a framework for retrieving CPU data with a high degree of accuracy. By default the corefreq output displays the number of CUP cores in the system.

Method-11: How to find the number of CPUs on Linux using the “/sys/devices/system/cpu/” file

Alternatively, you can get a processor information from the “/sys/devices/system/cpu/” file. It is a set of global and unique CPU attributes. Individual CPU attributes are in subdirectories 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
0-7

# 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))
8

Method-12: How to find the number of CPUs on Linux using the “/proc/interrupts” File

This file contains information about how many times “/proc/interrupts” interrupts the processor.

$ cat /proc/interrupts | egrep -i 'cpu'
            CPU0       CPU1       CPU2       CPU3       CPU4       CPU5       CPU6       CPU7

Method-13: How to find number of CPUs on Linux using the Python multiprocessing.cpu_count() method

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

# python -c 'import multiprocessing as m; print m.cpu_count()'
or
# python -c 'import os; print os.sysconf(os.sysconf_names["SC_NPROCESSORS_ONLN"]);'

8

Method-14: How to check the number of CPUs on Linux using the Python psutil library

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

It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes. It implements many functionalities offered by classic UNIX command line tools such as ps, top, iotop, lsof, netstat, ifconfig, free and others.

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

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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