I wanted to find out how many cores my system has, so I searched the same question in Google. I got some commands such as the lscpu command.
When I tried this command, it gave me the following result:
$ 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: 1 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 23 Stepping: 10 CPU MHz: 1998.000 BogoMIPS: 5302.48 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 2048K NUMA node0 CPU(s): 0-3
In particular, this output shows:
- CPU(s): 4
- Core(s) per socket: 4
- CPU family: 6
Which of those indicates cores of a Linux system?
Is there any other command to tell the number of cores, or am I assuming it is completely wrong?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To get a complete picture you need to look at the number of threads per core, cores per socket and sockets. If you multiply these numbers you will get the number of CPUs on your system.
CPUs = Threads per core X cores per socket X sockets
CPUs are what you see when you run htop (these do not equate to physical CPUs).
Here is an example from a desktop machine:
$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU('
CPU(s): 8
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
And a server:
$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU('
CPU(s): 32
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 2
The output of nproc corresponds to the CPU count from lscpu. For the desktop machine above this should match the 8 CPU(s) reported by lscpu:
$ nproc --all 8
The output of /proc/cpuinfo should match this information, for example on the desktop system above we can see there are 8 processors (CPUs) and 4 cores (core id 0-3):
$ grep -E 'processor|core id' /proc/cpuinfo processor : 0 core id : 0 processor : 1 core id : 0 processor : 2 core id : 1 processor : 3 core id : 1 processor : 4 core id : 2 processor : 5 core id : 2 processor : 6 core id : 3 processor : 7 core id : 3
The cpu cores reported by /proc/cpuinfo corresponds to the Core(s) per socket reported by lscpu. For the desktop machine above this should match the 4 Core(s) per socket reported by lscpu:
$ grep -m 1 'cpu cores' /proc/cpuinfo cpu cores : 4
To specifically answer your question you tell how many cores you have by multiplying the number of cores you have per socket by the number of sockets you have.
Cores = Cores per socket X Sockets
For the example systems above the desktop has 4 cores:
$ echo "Cores = $(( $(lscpu | awk '/^Socket(s)/{ print $2 }') * $(lscpu | awk '/^Core(s) per socket/{ print $4 }') ))"
Cores = 4
While the server has 16:
$ echo "Cores = $(( $(lscpu | awk '/^Socket(s)/{ print $2 }') * $(lscpu | awk '/^Core(s) per socket/{ print $4 }') ))"
Cores = 16
Another useful utility is dmidecode which outputs per socket information. In the case of the server system listed above we expect to see 8 cores per socket and 16 threads per socket:
$ sudo dmidecode -t 4 | grep -E 'Socket Designation|Count'
Socket Designation: CPU1
Core Count: 8
Thread Count: 16
Socket Designation: CPU2
Core Count: 8
Thread Count: 16
The lscpu command has a number of useful options that you may like to check out, for example:
$ lscpu --all --extended $ lscpu --all --parse=CPU,SOCKET,CORE | grep -v '^#'
See man lscpu for details.
In summary:
- You need to be aware of sockets, cores and threads
- You need to be careful of the term CPU as it means different things in different contexts
Method 2
You have to look at sockets and cores per socket. In this case you have 1 physical CPU (socket) which has 4 cores (cores per socket).
Method 3
You can get this information by nproc(1) command
$ nproc --all 12
It does not require root privileges.
Method 4
For the answer not to be confusing, you need to understand a couple of simple computer architecture concepts:
- You run processes (“programs”) on your linux system. Each process consists of one or more threads
- Each thread is a separate sequence of instructions. Two threads can be executed in parallel.
- Each instruction is given to a CPU to be executed. A CPU has logic that figures out what the bits of an instruction mean and decides what to do with it.
- There are different types of instructions. The decision logic inside a CPU will dispatch the different instructions to different hardware units. For instance, arithmetic instructions are actually performed by an ALU (arithmetic/logic unit), while instructions that load/store from memory are executed by some sort of memory unit.
- A core refers to a set of actual execution hardware (i.e. every core has an ALU, a memory unit, etc…)
-
You can have multiple CPUs that share one core – this is called hyperthreading.
- The idea: thread A is currently doing arithmetic, while thread B is loading something from memory. When that’s true, threads A and B can efficiently share a single core without getting in each other’s way (A uses the ALU, B uses the memory unit). Of course, sometimes both programs will want the ALU, and then they have to wait for each other…
- A socket is the physical slot on the motherboard into which a chip is inserted. This chip has a certain number of cores on it.
Examples:
The OP’s example:
CPU(s): 4 Thread(s) per core: 1 Core(s) per socket: 4 Socket(s): 1
- one physical socket, which contains a chip with
- 4 physical cores (think 4 ALUs and 4 memory units total)
- Only 1 thread can issue instructions to a core (no hyperthreading), which means there is
- one CPU per core, or 4 * 1 = 4 CPUs
Another example:
CPU(s): 16 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 2
Two physical sockets, each containing a chip with 4 physical cores, making 8 cores total. Two threads get to issue instructions to each core (this machine has hyperthreading), meaning there must be two CPUs attached to each core, making a total of 8 * 2 = 16 CPUs
The first machine can be executing precisely four instructions at any given time, period. The second machine can execute between 8 and 16 instructions at any given time: 16 will be achieved only when each pair of CPUs is executing different types of instructions and so can share a core without waiting.
Method 5
You can also use the command cat /proc/cpuinfo which will output a chunk of data for each core. Each chunk starts with this info:
processor : 3 vendor_id : GenuineIntel cpu family : 6 model : 60 model name : Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz (...)
Cores are numbered starting from 0, so if the last chunk says processor : 3 as in this case, your machine has 4 cores.
Method 6
getconf _NPROCESSORS_ONLN
(getconf is part of glibc)
Method 7
$ grep -c processor /proc/cpuinfo 8
That’s all you need. It is the number of core online, regardless if hyperthreading is on or off.
$ ls -d /sys/devices/system/cpu/cpu* | wc -l 8
Another easy way.
Method 8
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2934342f1b2323232323">[email protected]</a> ~]# dmidecode -t 4 | egrep -i "Designation|Intel|core|thread"
Socket Designation: CPU1
Manufacturer: Intel
HTT (Multi-threading)
Version: Intel(R) Xeon(R) CPU L5640 @ 2.27GHz
Core Count: 6
Core Enabled: 6
Thread Count: 12
Socket Designation: CPU2
Manufacturer: Intel
HTT (Multi-threading)
Version: Intel(R) Xeon(R) CPU L5640 @ 2.27GHz
Core Count: 6
Core Enabled: 6
Thread Count: 12
Method 9
I found this way:
echo $((`cat /sys/devices/system/cpu/present | sed 's/0-//'` + 1))
Method 10
Install and run neofetch in the CLI.
`.-::---.. <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8de7e8ffe8e0f4cde5ece1">[email protected]</a>
.:++++ooooosssoo:. ----------
.+o++::. `.:oos+. OS: LMDE 4 (debbie) x86_64
:oo:.` -+oo: Kernel: 5.10.0-0.bpo.7-amd64
`+o/` .::::::-. .++-` Uptime: 7 hours, 8 mins
`/s/ .yyyyyyyyyyo: +o-` Packages: 2752 (dpkg)
`so .ss ohyo` :s-: Shell: bash 5.0.3
`s/ .ss h m myy/ /s`` Resolution: 1920x1080 @ 60.00Hz
`s: `oo s m Myy+-o:` DE: Cinnamon 5.0.5
`oo :+sdoohyoydyso/. WM: Mutter (Muffin)
:o. .:////////++: WM Theme: Mint-Y-Dark (Mint-Y)
`/++ -:::::- Theme: Mint-Y [GTK2/3]
`++- Icons: Mint-X [GTK2/3]
`/+- Terminal: gnome-terminal
.+/. CPU: AMD Ryzen 7 5800X 8- (16) @ 3.800GHz
.:+-. GPU: NVIDIA NVIDIA Corporation GP108
`--.`` Memory: 1968MiB / 32087MiB
Method 11
CPU family is irrelevant here.
- CPU(s) = physical sockets
- Core(s) per socket – as it says
- so total number of cores = CPU(s) * Core(s) per socket
In your case, you have total 4 full cores.
What also can be important, is “Thread(s) per core”. But you have 1, so not in your case.
Method 12
A simple way to figure out the number of CPUs is by issuing the commands below:
cat /proc/interrupts | egrep -i 'cpu'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0