217

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?

  • 1
    my simple command for all users: $ grep precessor /proc/cpuinfo | wc -l – สมหวัง แนวหน้า May 18 '16 at 10:55
  • 5
    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

12 Answers 12

113

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

  • 1
    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
  • 13
    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
217

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
  • 10
    This should be the accepted answer – gansub Jun 18 '17 at 15:30
  • 2
    I agree. This should be the accepted answer as it better explain the subject. – Rafael Oct 5 '18 at 14:38
  • I think adding lscpu --all --extended would be a useful addition but I no longer have access to these particular machines. – htaccess May 17 at 1:13
  • You can also avoid awk and an explicit multiplication: lscpu --all --parse=CORE,SOCKET | grep -Ev "^#" | sort -u | wc -l – Jakub Klinkovský Jul 5 at 7: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 6 at 1:36
60

You can get this information by nproc(1) command

$ nproc --all
12

It does not require root privileges.

  • 10
    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 4:21
  • 2
    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 2:13
  • @htaccess At least on Ubuntu, this outputs the correct number of on-line logical CPUs – Wil May 16 at 6:48
  • 1
    @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 at 20:26
  • 1
    @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 at 21:43
17

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.

14

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.

  • Thanks this command describing the information in detail.:) – Mr ASquare Jul 24 '15 at 8:31
  • For a one-liner command, you might use grep -c ^processor /proc/cpuinfo – Wildcard Apr 27 '16 at 5:51
5
getconf _NPROCESSORS_ONLN

(getconf is part of glibc)

  • 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 at 5:13
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
  • What's the t 4 do for dmidecode? How do the counts & enabled lines multiply out? Does your example show 2 cores, or 6, or 12, or 24, or some other number? – Xen2050 Mar 28 at 18:23
  • @Xen2050 4 is processor, see linux.die.net/man/8/dmidecode. But using 4 as argument is a bad practice. – JohnKoch Jul 10 at 17:24
3
$ 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.

  • 1
    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
0

I found this way:

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

Just want to add some info to @htaccess 's answer.

In CentOS 6.x, dmidecode does not output Core/Thread count info, and it actually regard 'CPU' as 'CPU' or 'Core' in lscpu, not 'socket'.

  • If this isn't the answer then add this as comment. – Mongrel Jan 5 '17 at 5:09
  • @Mongrel I wanted to add as comment but commenting requires 50 reputation – PickBoy Jan 6 '17 at 8:08
-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.

  • 1
    You are confusing CPU(s) with Socket(s) in your description. – jlliagre Dec 8 '15 at 9:28
  • 1
    As jlliagre says this is incorrect. Cores = Cores per socket X Sockets, see the description in my response. – htaccess Apr 27 '16 at 4:13
-3

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

cat /proc/interrupts | egrep -i 'cpu'

protected by Community Jun 12 '17 at 22:07

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.