April 15, 2024

How to Find CPU Cores in Linux

Sometimes, you may need to check how many CPUs a Linux system has for various reasons. I can name a few such as purchasing a software license or optimizing your system for an application workloads, etc,.

The main configuration file of the CPU and system architecture information’s are stored in the '/proc/cpuinfo' file. It’s a pseudo-file system used as an interface to kernel data structures, which is usually mounted at /proc.

Linux systems provide several ways to obtain these information, and this article will discuss best three easiest ways to get it.

1) Checking CPU cores with lscpu command in Linux

The lscpu command is one of the easiest commands to get complete details of CPU architecture information in Linux system. It displays CPU details such as the number of CPUs, threads, cores, sockets, Vendor ID, Model name, CPU min/max MHz, Virtualization features, Architecture and CPU caches.

lscpu
How to Check CPU Cores in Linux

1.1) How to find Physical CPUs & Virtual CPUs

You can easily calculate Physical CPUs & Virtual CPUs based on the lscpu command output using the below formula.

CPU(s):              8
Thread(s) per core:  2
Core(s) per socket:  4
Socket(s):           1

The ‘CPU(s)’ value indicates the number of logical cores nor virtual cores, which is 8 in our case.

  • Virtual CPUs = Socket(s) x Core(s) per socket x Thread(s) per core
  • Physical CPUs = Socket(s) x Core(s) per socket

Based on the above formula, our machine has '4' physical cores, and '8' logical cores.

2) Finding CPU cores from /proc/cpuinfo file in Linux

The lscpu command gathers CPU architecture information from the /proc/cpuinfo file and shows summarized output. You can also get this information directly by parsing the file using cat, less and more command, but it will show you separate information for each CPUs.

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	: 0xf0
cpu MHz		: 2600.000
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
bogomips	: 5199.98
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
.
.
processor	: 7
vendor_id	: GenuineIntel
cpu family	: 6
model		: 94
model name	: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
stepping	: 3
microcode	: 0xf0
cpu MHz		: 2600.000
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
bogomips	: 5199.98
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

3) Checking CPU cores in Linux using nproc command

The nproc command shows the number of processing units available to the current process on your Linux system, run:

# nproc
8

Bonus Tips: What is Hyper-threading

Hyper-threading (Simultaneous multithreading – SMT) is a feature first introduced in Intel processors. A single CPU core allows multiple threads to be processed simultaneously to make better use of the resources provided by modern processor architectures.

We can identify which core has SMT (hyper-threading) enabled, Run:

lscpu --all --extended

CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE    MAXMHZ   MINMHZ      MHZ
  0    0      0    0 0:0:0:0          yes 3500.0000 800.0000 2600.000
  1    0      0    1 1:1:1:0          yes 3500.0000 800.0000 1180.252
  2    0      0    2 2:2:2:0          yes 3500.0000 800.0000 2600.000
  3    0      0    3 3:3:3:0          yes 3500.0000 800.0000 2600.000
  4    0      0    0 0:0:0:0          yes 3500.0000 800.0000 2600.000
  5    0      0    1 1:1:1:0          yes 3500.0000 800.0000 2600.000
  6    0      0    2 2:2:2:0          yes 3500.0000 800.0000 1258.902
  7    0      0    3 3:3:3:0          yes 3500.0000 800.0000 2600.000

You can identify this by looking at the ‘CPU’ column and ‘CORE’ column.

For example: CPU 0 and CPU 4 belong to CORE 0. Therefore, CORE 0 is a P-core (Performance Core) with SMT.

Conclusion

In this tutorial, we learned three ways to check the number of processors or cores available on a Linux system. Also, learned other information about the CPU architecture, vendor ID, model name, etc,.

Leave a Reply

%d bloggers like this: