354

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?

bandrade
  • 103
Mr ASquare
  • 3,735
  • 3
    my simple command for all users: $ grep precessor /proc/cpuinfo | wc -l – สมหวัง แนวหน้า May 18 '16 at 10:55
  • 7
    Your image of text isn't very helpful. It can't be copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please [edit] your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). – Toby Speight Dec 14 '16 at 13:57
  • 4
    @สมหวังแนวหน้า kind of a nitpick, but the the grep arg should be processor, not precessor, correct? . Thanks for the help! – patrick Feb 26 '20 at 13:45

12 Answers12

367

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
htaccess
  • 3,839
  • I think adding lscpu --all --extended would be a useful addition but I no longer have access to these particular machines. – htaccess May 17 '19 at 01:13
  • 1
    You can also avoid awk and an explicit multiplication: lscpu --all --parse=CORE,SOCKET | grep -Ev "^#" | sort -u | wc -l – Jakub Klinkovský Jul 05 '19 at 07:35
  • @Jakub Klinkovský, thanks for pointing that out but in this case I want to actually demonstrate the multiplication of Socket(s) * Core(s) per socket. Given what I just said I guess echo Cores = $(( $(lscpu | awk '/^Socket\(s\)/{ print $2 }') * $(lscpu | awk '/^Core\(s\) per socket/{ print $4 }') )) would be better so that people can see exactly what is being multiplied ... – htaccess Jul 06 '19 at 01:36
  • In CentOS 6.x, dmidecode does not output Core/Thread count info, and it actually regards 'CPU' as 'CPU' or 'Core' in lscpu, not 'socket'. – PickBoy Jan 05 '17 at 04:51
  • Thank you @htaccess for this very detailed answer. This answer should be the accepted answer and is way better for someone looking deep into this matter. – avia May 14 '21 at 06:08
145

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

slm
  • 369,824
user1403360
  • 2,020
  • 2
    I have one socket, two cpus per socket, so that would mean a total of 2. However, it says 4 cpus. If I try nproc I get 4 back. Using cat /proc/cpuinfo I get 4, and thus it seems this answer is wrong, for my situation at least. – SPRBRN May 18 '16 at 13:05
  • 18
    Not if you have hyper threading. You still have 2 physical cores but each core shows in /proc/cpuinfo twice. If you look carefully under coreid you can see that you have each coreid listed twice. Also under flags you can see the ht flag. – user1403360 May 19 '16 at 14:01
  • 1
    So what does CPU(s): 4 refer to in this case? – An Ignorant Wanderer May 02 '20 at 16:23
  • CPU (s) is the number of logical CPUs. If a cpu has hyperthreading enabled then this number is higher than the number of actual cores. – user1403360 May 04 '20 at 06:59
77

You can get this information by nproc(1) command

$ nproc --all
12

It does not require root privileges.

Anthony Ananich
  • 7,334
  • 5
  • 33
  • 45
  • 13
    This is incorrect, the output of nproc corresponds to the CPU count from lscpu. If you have multithreaded cores then the output of nproc will not match the number of cores you have. – htaccess Apr 27 '16 at 04:21
  • 4
    For a non-root solution, a hack would be to use top and hit 1. This will show the number of available cores. Why I saw "available" is because the underlying hardware might have more cores like a cloud infra. – Hopping Bunny May 23 '18 at 02:13
  • @htaccess At least on Ubuntu, this outputs the correct number of on-line logical CPUs – Wil May 16 '19 at 06:48
  • @htaccess nproc --all on Ubuntu 18.04 with i5-6200U returns 4 as expected, so perhaps you can provide a little more context re incorrect. – Wil May 16 '19 at 06:52
  • 1
    @Wil the question asks "how many cores my system has" not "number of on-line logical CPUs" My laptop has 4 logical CPUs with 1 socket, 2 cores per socket and 2 threads per core. It has 2 cores and 4 CPUs. – htaccess May 16 '19 at 23:28
  • Just discovered lscpu --all --extended which gives another view of this info, might add it to my answer. – htaccess May 16 '19 at 23:36
  • 2
    @htaccess ok I get it. Usually when a linux duffer asks how many cores they mean how many logical threads are available. Technically speaking, linux is wrong to call hardware threads 'cpus'. Your machine has 1 CPU with 2 Cores and 4 SMT hardware threads. There's a huge amount of ambiguity in what CPUs and Cores mean as a result. But I get what you meant now, ty. – Wil May 17 '19 at 20:26
  • 2
    @Wil yea I call this out in my answer above: "You need to be careful of the term CPU as it means different things in different contexts". Personally I don't equate "cores" to logical processing units but to physical cores in the processor (ie the physical processing units). – htaccess May 17 '19 at 21:43
37

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.

  • 1
    wow thank you, that was a great explanation! – Robert Oct 21 '19 at 09:08
  • 2
    Only thing; you use "CPU" as virtual CPUs for the kernel; "normally" a CPU has multiple cores. Not wrong just confusing I want to say. –  Nov 22 '19 at 16:05
  • @rastafile:. it's an interesting point you raise. different people use "CPU" to mean different things. I believe what I say here is consistent with the output of the lscpu command which the OP referenced. In my experience, computer engineers are referring to what you call a core when they say CPU, but even there I'm sure someone will disagree with me – stochastic Nov 22 '19 at 17:22
  • I have now 4 CPUs. Before, I had hyperthreading on (via BIOS), and it showed 8, from cpu0 to cpu7. I have one "quadcore processor", the vendor tells me on the box. Confusing, because I can easily say I have "a CPU that can run 8 threads" at a time (yes, with hyperthreading, so it is more 5 or 6 threads' equivalent). –  Nov 22 '19 at 17:39
  • right, so both before and after hyperthreading you had four cores. Before, you had 8 CPUs: eight pieces of hardware that could accept instructions to be run (but with only 4 full sets of execution hardware). By turning off hyperthreading, you disabled half of those CPUs (one CPU on each core is now disabled).

    You are certainly correct that it is a little confusing.

    – stochastic Nov 22 '19 at 18:10
16

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.

dr_
  • 29,602
  • Thanks this command describing the information in detail.:) – Mr ASquare Jul 24 '15 at 08:31
  • 1
    For a one-liner command, you might use grep -c ^processor /proc/cpuinfo – Wildcard Apr 27 '16 at 05:51
  • Similar to https://unix.stackexchange.com/a/248063/22812, this answer only provides the correct number of cores if none of the cores have multiple concurrent threads of execution, i.e., multi-threading (SMT, HTT) is not enabled for any of the cores. On a Linux system, /proc/cpuinfo lists information about each logical CPU. – Anthony Geoghegan Nov 22 '19 at 12:33
9
getconf _NPROCESSORS_ONLN

(getconf is part of glibc)

L.R.
  • 293
  • 1
    That's slightly different (and useful for some needs). It's the number of processors that are online now (i.e. available for running processes). It may be less than the total number of CPUs if one or more have been taken offline (e.g. during hibernation or whilst adding/removing processors). – Toby Speight Feb 14 '17 at 13:34
  • And getconf _NPROCESSORS_CONF gets you the number of processors configured. You can get the value of both these macros inside of a C source code too, by using the sysconf() function. Type in man sysconf for more information. – Galaxy Apr 17 '19 at 05:13
5
$ 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.

HalosGhost
  • 4,790
jwc
  • 51
  • 4
    This is simple, but I'm not sure it's accurate to the question. He asked for the number of cores: (cores != cpus) if hyperthreading is enabled. My system has 24 cores: 48 cpus when hyper-threaded. Also, using the second command provided ls -d /sys/devices/system/cpu/cpu* | wc -l showed 49 because there is a directory cpuidle among the cpuN directories. – Andrew Falanga Dec 20 '18 at 15:54
4
[root@xxxxx ~]#  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
reli
  • 41
1

I found this way:

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

Install and run neofetch in the CLI.

`.-::---..                     jeremy@hal 
      .:++++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

Jeremy Boden
  • 1,320
-2

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.

Tomasz Klim
  • 1,042
-3

A simple way to figure out the number of CPUs is by issuing the commands below:

cat /proc/interrupts | egrep -i 'cpu'
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255