7

Of course we know cat /proc/cpuinfo |grep "cpu cores" will give an output

[root@14:47 ~]#  cat /proc/cpuinfo |grep "cpu cores"
cpu cores       : 2
cpu cores       : 2
cpu cores       : 2
cpu cores       : 2

But actually I want to get the total number of cpu cores. I want the result to be

cpu cores: 8

How can I get such a result?

5
  • I don't think summing those will get you the total number of cores on the system, the cpu cores line seems to show the number of cores on the processor (socket) that particular core is on. I get output identical to your example on one dual-socket dual-core system (that has a total of 4 cores, not 8).
    – ilkkachu
    Jul 12, 2017 at 9:57
  • @ilkkachu: I was also wondering about the output (see my answer below). I now think, that the four rows stem from the fact, that all 4 cores (2xphysical, 2x hyperthreading) are considered and the cpu cores: 2 line stems from the fact, that a total of 2xphysical cores are on the host machine.
    – FloHe
    Jul 12, 2017 at 10:06
  • @yode Please specify which CPU you have. This simplifies the interpretation of your result. Do you expect to have 8 physical cores (with hyperthreading they could be counted as 16 virtual cores) or do you expect to have 4 physical but 8 virtual cores? cat /proc/cpuinfo should print out the number of physical cores. But it prints out this information for each virtual core. Thus I would expect that you have a dual core CPU with hyperthreading (or how its called by non-intel processors). Jul 12, 2017 at 11:39
  • Example: my Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz has 4 physical cores and supports hyperthreading. If I print cat /proc/cpuinfo I get 8 times the line cpu cores : 4 because I have 8 virtual cores (2 per physical core) and this cpu cores information is printed out once per virtual core. Jul 12, 2017 at 11:42
  • And please also specify if you have one CPU (one piece of hardware) or maybe two CPUs on your mainboard. Jul 12, 2017 at 12:09

7 Answers 7

12

Try this,

As per man lscpu

CORE   The logical core number. A core can contain several CPUs.

SOCKET The logical socket number. A socket can contain several cores.

cores as well as sockets are physical where as CPU(s) are logical number. So to find the number of cores your system has, do number of cores x number of sockets

A sample output of lscpu is as follows :

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

So the total number of cores: 16

The total number of CPU(s): 32 (Since number of threads per core is 2)

As @Durga mentioned , the nproc gives total number of CPUs.

For more , refer this answer , to get interpretation of /proc/cpuinfo refer this

I'd like to thank @Kusalananda for helping me to understand the same.

4
  • Thanks for your lesson.I'm very green on this area.And I read your answer many times.As my understand, one Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz mean 1 socket,two core and four thread.So the CPU(s) is equivalent to thread?
    – yode
    Jul 12, 2017 at 10:57
  • @yode If your system has 1 socket , 2 cores , and 2 threads per core, then yes you totally have 4 CPU(s) , each CPU being able to execute a thread , that counts to 4 threads
    – ss_iwe
    Jul 12, 2017 at 11:26
  • So the nproc will give the number of CPU(s) actually?And that mean one computer can have a lot of sockets(physical cpu)?
    – yode
    Jul 12, 2017 at 11:32
  • @yode Well Almost all processors these days have more than one core.
    – ss_iwe
    Jul 12, 2017 at 14:03
8

In Terminal nproc, OP: total cpu cores

7
  • 1
    It will give an empty output here..
    – yode
    Jul 12, 2017 at 7:02
  • And I'm after a method that can process that output to get the result. :)
    – yode
    Jul 12, 2017 at 7:08
  • 1
    @yode did you type the # because it's not part of the command it's meant to represent the shell prompt. If it's still not working, what os are you using?
    – M4rty
    Jul 12, 2017 at 7:08
  • 2
    @yode dont use that #, and as your question there is only 2 cores is there in your system as i guess, not 8
    – Durga
    Jul 12, 2017 at 7:11
  • Oh, sorry,it works.But it seem will give the number of CUP but not core number.See this
    – yode
    Jul 12, 2017 at 7:12
7

If you are only interested in the sum, you can also use GNU awk:

cat /proc/cpuinfo |grep "cpu cores" | awk -F: '{ num+=$2 } END{ print "cpu cores", num }'

Edit: This is the correct answer for the OP's intention to sum the numbers of the last column. However the question about finding out how many cores (physical/virtual) are on the machine is given in the other answers to the question.

9
  • Please Correct me if I am wrong , The command sums all the cpu cores from the /proc/cpuinfo , but the total sum doesn't translate to actual cores in the system does it ?
    – ss_iwe
    Jul 12, 2017 at 9:30
  • Afaik yes. As I mentioned in my answer, I just followed what the OP asked for, i.e. summing up the 2nd column.
    – FloHe
    Jul 12, 2017 at 9:48
  • 1
    My notebook posseses e.g. an Intel Core i5-4210M dual core processor, which thanks to Hyperthreading has a total sum of 4 virtual cores. . I think, that the four rows stem from the fact, that all 4 cores (2xphysical, 2x hyperthreading) are considered and the cpu cores: 2 line stems from the fact, that a total of 2xphysical cores are on the host machine.
    – FloHe
    Jul 12, 2017 at 10:09
  • 1
    awk can do pattern matching, you rarely need both grep and awk: awk -F: '/cpu cores/ {num+=..., also, both grep and awk can read files directly, so no eed for cat Jul 12, 2017 at 12:40
  • 1
    This gives me 16 cpu cores, though my machine has 4.
    – einpoklum
    Nov 9, 2018 at 19:07
5

If you have glibc and a reasonably recent kernel you can use

getconf _NPROCESSORS_ONLN

to return the number of currently online1 processors. That includes virtual and hyper-thread processors.


1 On most systems that will be identical to the number of overall processors. The exception are systems that have one or more processors disabled for whatever reason (e. g. because it's faulty or because of artificial restrictions like leased/licensed hardware) or systems that support "hot-plugging" CPUs, had new CPUs added but not yet enabled.

4

Short Answer

The number of physical cores:

> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores   : 2

The number of virtual cores (e.g. 2x number of physical cores with hyper threading):

> cat /proc/cpuinfo | grep -c "cpu cores"
4

If you have more than one CPU/processor (in this sense) on your mainboard this does not properly work. See section "Several CPUs per mainboard". This may be the case if you work on a computing cluster or on a high-end desktop workstation for CAD/Engineering-purposes.

Long Answer

The command cat /proc/cpuinfo should print out the number of physical cores in the line cpu cores. But it prints out this information for each virtual core. Thus, if you want to have the number of physical core, you just take the first occurrence of the line cpu cores, which is

> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores   : 2

Alternatively, if you are looking for the number of virtual cores, you count the number of times the line cpu cores is found, which is

> cat /proc/cpuinfo | grep -c "cpu cores"
4

Example

I have got a Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz (4 physical cores, hyperthreading). Lets see what I get:

> cat /proc/cpuinfo | grep "cpu cores"
cpu cores   : 4
cpu cores   : 4
cpu cores   : 4
cpu cores   : 4
cpu cores   : 4
cpu cores   : 4
cpu cores   : 4
cpu cores   : 4

Number of physical cores:

> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores   : 4

Number of virtual cores:

> cat /proc/cpuinfo | grep -l "cpu cores"
8

You could also take the last found processor number and increment it by one

> cat /proc/cpuinfo | grep "processor" | tail -1
processor   : 7
# +1

Several CPUs per mainboard

I just looked onto one node of a computing cluster, on which I am currently working: 1 node has 4 CPUs (Intel Xeon) with each 8 physical cores; each CPU supports hyptherthreading; therefore, each CPU has 16 virtual cores; summing it up, the one node has 32 physical and 64 virtual cores;

cat /proc/cpuinfo prints out information for each virtual core. Thus, we get 64 'packages' of information. If we have such a setup, we need to consider the row physical id in cat /proc/cpuinfo.

This is the output I get:

> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores   : 8

> cat /proc/cpuinfo | grep -c "cpu cores"
64

> cat /proc/cpuinfo | grep "physical id" | tail -1
physical id   : 3
# this result +1 => number of CPUs

Thus, "number behind cpu cores" x "number behind physical id + 1" is the number of physical cores one our node (8 x (3 + 1)). The 64 is the number of virtual cores.

Nomenclature

Since everyone uses cores, CPU and processor in another context, I introduce a nomenclature for my answer here:

  • processor: the whole piece of hardware (e.g. my Intel® Core™ i7-3740QM Processor)
  • CPU: CPU = processor
  • core or physical core: number of physical calculation units in the CPU
  • thread or virtual core: number of threads that can run on parallel on one CPU; if the (intel-)processor is able to perform hyperthreading the number of virtual cores is twice the number of physical cores (I am not sure how other processor vendors call this feature);

__Example: __

  • processor: Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
  • source: Intel Ark

Data:

  • CPU number: 1
  • processor number: 1
  • cores (or physical cores): 4
  • threads (or virtual cores): 8
4
  • The question about how to sum that core number,but the follow answer(include yours) give me a lot of information about hardware.I'm afraid to make this question too board,acutally I have ping you in this room.Anyway,thanks very very much.And you get my upvote. :)
    – yode
    Jul 12, 2017 at 12:14
  • An as the terdon say here ,the cat /proc/cpuinfo | grep -c "cpu cores" is equivalent to grep -c "cpu cores" /proc/cpuinfo
    – yode
    Jul 12, 2017 at 12:25
  • 1
    you are assuming a system where all CPU are equal. You could teoretically have several CPU with different number of cores (or some of them disabled). That would be very strange, and you would know, probably some kind of clustered system with NUMA memory, that is actually composed by multiple nodes.
    – Ángel
    Apr 8, 2020 at 10:29
  • @Ángel Yes :-) . How about another more general answer considering this theoretical exercise? Apr 8, 2020 at 17:14
0

Use lscpu to get mumber of cores per socket; then multiply by the number of sockets;then by threads per core.

1
  • Welcome to the site. Please consider expanding your answer by adding an example to it.
    – Haxiel
    Dec 19, 2018 at 7:21
0

These answers either ignore hyperthreading and/or multiple sockets, or they involve text processing. I keep finding this page while looking for a simple(ish) and complete answer, so I'm posting one here.

For number of physical CPU cores

lscpu --parse='core,socket' | grep -v '^#' | sort -u | wc -l

For the number of CPU threads

lscpu --parse='core,socket' | grep -v '^#' | wc -l

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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