667

I have this script, but I do not know how to get the last element in the printout:

cat /proc/cpuinfo | awk '/^processor/{print $3}'

The last element should be the number of CPUs, minus 1.

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Richard
  • 14,642
  • 18
  • 56
  • 77
  • 15
    You don't need to `cat` before `awk`, anyway: just `awk ' – Tomasz Gandor Aug 01 '14 at 08:12
  • 1
    To answer the question directly, pipe that to `tail -n 1` which takes the last line and prints it. – Fábio Santos Dec 22 '19 at 17:55
  • 1
    See also https://unix.stackexchange.com/q/564494/5132 . – JdeBP Jan 28 '20 at 11:32

31 Answers31

798

Processing the contents of /proc/cpuinfo is needlessly baroque. Use nproc which is part of coreutils, so it should be available on most Linux installs.

Command nproc prints the number of processing units available to the current process, which may be less than the number of online processors.

To find the number of all installed cores/processors use nproc --all

On my 8-core machine:

$ nproc --all
8
Dominic Motuka
  • 319
  • 1
  • 5
  • 13
uckelman
  • 25,298
  • 8
  • 64
  • 82
  • 11
    does it distinguish between virtual core and physical core? – Richard Jun 13 '13 at 20:06
  • Not that I see. `nproc` reports "2" on my VM which has two virtual cores, and "8" on my real machine which has 8. – uckelman Jun 14 '13 at 08:47
  • 20
    This doesn't work with hyperthreading if I need the number of physical cores. Returns 8 on my quad core i7 box. – pratnala Jul 23 '13 at 15:23
  • 1
    @pratnala - [teambob's answer](http://stackoverflow.com/a/18051445/877069) purports to give you the number of physical cores. – Nick Chammas Mar 31 '14 at 17:41
  • 1
    On my old ubuntu (10.10) nproc is not available. It must be a new-ish addition. – bukzor Oct 22 '14 at 00:13
  • @pratnala: Actually it does work as expected, `whatis nproc` says _print the number of processing units available_, which is 2 per physical core if hyperthreading is available. – Benoit Duffez Apr 12 '15 at 19:14
  • 4
    Unfortunatelly, `nproc` is not a part of boot2docker – kgadek Aug 08 '16 at 15:36
  • 1
    This works for me on Debian and appears to query the processor instead of grep-ing a text file. – jaylweb Feb 28 '17 at 18:44
  • 1
    That means you need to install the package which provides nproc. – uckelman May 21 '19 at 09:15
  • In my case it works well without the --all flag – thanos.a Apr 23 '21 at 08:32
  • 1
    yes, in LXC just `nproc` shows correct number of cores (assigned to the container), while `nproc --all` shows all cores of the host. – Alek Nov 30 '21 at 00:57
  • So are these `nproc`'s cores physical or logical? P.S. Copypasting the man should be prohibited. It's useless. – Pavel Vlasov Dec 14 '21 at 13:49
  • For what its worth this works on alpine 3.16 `make all -j\`nproc --all\`; # use all the cores` .... from `nproc --help` Print the number of processing units available to the current process may be less than the number of online processors `nproc --all` print the number of installed processors `nproc --ignore=N` if possible, exclude N processing units – Exo Flame Nov 20 '22 at 02:20
795
grep -c ^processor /proc/cpuinfo

will count the number of lines starting with "processor" in /proc/cpuinfo

For systems with hyper-threading, you can use

grep ^cpu\\scores /proc/cpuinfo | uniq |  awk '{print $4}'

which should return (for example) 8 (whereas the command above would return 16)

ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91
unbeli
  • 29,501
  • 5
  • 55
  • 57
  • 45
    Note that both of these will end up counting twice as many cores as actually exist if you're on a system with hyperthreading (e.g, P4, or Core i7). –  Jun 26 '11 at 00:04
  • 31
    @duskwuff: which is precisely what you want in most cases. – Frank Kusters May 17 '13 at 10:52
  • 12
    `grep -c '^processor' /proc/cpuinfo` on zsh. – Steven Lu Jul 21 '13 at 20:23
  • Will also fail on systems where CPU numbering is not necessarily sequential/contiguous... – twalberg Jan 20 '14 at 20:27
  • 40
    `cat /proc/cpuinfo | awk '/^processor/{print $3}' | tail -1` also will return the wrong number if the CPU numbers are 0-based. – Phazor May 04 '15 at 14:37
  • 3
    The first line return 1 Core less then existing. Better cat /proc/cpuinfo | awk '/^processor/{print $3}'| wc -l – Mirko Ebert May 13 '15 at 11:24
  • 2
    The first line does not work. The second line is better. Isn't there a tool to query the actual processor itself and not grep some text file? – jaylweb Feb 28 '17 at 18:43
  • grep -c ^processor /proc/cpuinfo worked for me on RHEL 7.2 – mmarquezvacas Aug 24 '17 at 19:01
  • 2
    Those two answer give answers that differ by one for me. – Simd Oct 09 '17 at 21:31
  • 2
    Just do `grep '^processor' /proc/cpuinfo | wc -l` which will give a count of the number of rows starting with "processor", rather than relying on the 0-based numbering system Alternatively `grep -c '^processor' /proc/cpuinfo` gives the same result in one command. – CaffeineConnoisseur Mar 01 '18 at 23:06
  • The first one tells me the number of threads. The second one tells me the number of cores per CPU. Neither of these are correct. – jbo5112 Jun 19 '20 at 20:09
  • I believe https://stackoverflow.com/a/47435156/1593842 is the correct answer, when the system has lscpu, and https://stackoverflow.com/a/21241550/1593842 is the correct answer when it doesn't. `nproc` is cool, but unlike what Frank Kusters said, it is precisely not the thing I want to know in most cases. – user1593842 Aug 04 '20 at 14:24
313

The most portable solution I have found is the getconf command:

getconf _NPROCESSORS_ONLN

This works on both Linux and Mac OS X. Another benefit of this over some of the other approaches is that getconf has been around for a long time. Some of the older Linux machines I have to do development on don't have the nproc or lscpu commands available, but they have getconf.

Editor's note: While the getconf utility is POSIX-mandated, the specific _NPROCESSORS_ONLN and _NPROCESSORS_CONF values are not. That said, as stated, they work on Linux platforms as well as on macOS; on FreeBSD/PC-BSD, you must omit the leading _.

mklement0
  • 382,024
  • 64
  • 607
  • 775
mshildt
  • 8,782
  • 3
  • 34
  • 41
  • 11
    This worked for me on Red Hat Entreprise Linux 5.4, Centos 6.5 & 7 and Mac OSX 10.9 (Mavericks). It seems this it the most portable, as lscpu is not installed by default on these systems. Thanks! – big_gie Aug 28 '14 at 18:50
  • 8
    So portable it's in POSIX :) http://pubs.opengroup.org/onlinepubs/009604499/utilities/getconf.html – BCran Nov 19 '14 at 08:54
  • I like this one. It works on both my CentOS 5.8 and CentOS 7.0. – Soli Dec 01 '14 at 09:30
  • 1
    @BCran I could not find `_NPROCESSORS_ONLN` in POSIX. Can you link to it? – Ciro Santilli OurBigBook.com Aug 09 '15 at 08:08
  • 4
    @CiroSantilli六四事件法轮功纳米比亚威视 From https://github.com/gstrauss/plasma/blob/master/plasma_sysconf.c it looks like I was wrong: it's only optional. "sysconf _SC_NPROCESSORS_ONLN and _SC_NPROCESSORS_CONF are not required by standards, but are provided on numerous unix platforms and documented as optional by Open Group." – BCran Oct 04 '15 at 20:36
  • This one works for me on both RHEL and OS X El Capitan. – TxAG98 Jun 02 '16 at 15:29
  • I am using the following command and it doesn't provide required result. /bin/sh -c getconf _NPROCESSORS_ONLN What is the reason? I am trying to run a process and get result by redirecting its output. – Haseeb Jadoon Aug 16 '17 at 11:43
  • 1
    @HaseebJadoon, you missed enclosing the getconf command in quotes `/bin/sh -c 'getconf _NPROCESSORS_ONLN'`. Though you asked a long time ago, just wanted to share my suggestion if it is helpful for you or someone else. – Junaid Jun 13 '18 at 11:18
  • Works on Cygwin too. – tricasse Feb 13 '19 at 00:46
  • This reports `4` on my **2-core** hyperthreaded i5-3230M… – JamesTheAwesomeDude Feb 07 '21 at 18:44
  • It doesn't give you the number of "physical cores" of hyper-threaded CPUs. Though It can be useful with prehistoric hardware or systems with hyper-threading turned off. – Fravadona Apr 02 '21 at 14:23
  • This even works exactly if we offline CPUs and find only online CPUs. :) – Jeevan Chaitanya Dec 20 '21 at 11:18
135

Preface:

  • The problem with the /proc/cpuinfo-based answers is that they parse information that was meant for human consumption and thus lacks a stable format designed for machine parsing: the output format can differ across platforms and runtime conditions; using lscpu -p on Linux (and sysctl on macOS) bypasses that problem.

  • getconf _NPROCESSORS_ONLN / getconf NPROCESSORS_ONLN doesn't distinguish between logical and physical CPUs.


Here's a sh (POSIX-compliant) snippet that works on Linux and macOS for determining the number of - online - logical or physical CPUs; see the comments for details.

Uses lscpu for Linux, and sysctl for macOS.

Terminology note: CPU refers to the smallest processing unit as seen by the OS. Non-hyper-threading cores each correspond to 1 CPU, whereas hyper-threading cores contain more than 1 (typically: 2) - logical - CPU.
Linux uses the following taxonomy[1], starting with the smallest unit:
CPU < core < socket < book < node
with each level comprising 1 or more instances of the next lower level.

#!/bin/sh

# macOS:           Use `sysctl -n hw.*cpu_max`, which returns the values of 
#                  interest directly.
#                  CAVEAT: Using the "_max" key suffixes means that the *maximum*
#                          available number of CPUs is reported, whereas the
#                          current power-management mode could make *fewer* CPUs 
#                          available; dropping the "_max" suffix would report the
#                          number of *currently* available ones; see [1] below.
#
# Linux:           Parse output from `lscpu -p`, where each output line represents
#                  a distinct (logical) CPU.
#                  Note: Newer versions of `lscpu` support more flexible output
#                        formats, but we stick with the parseable legacy format 
#                        generated by `-p` to support older distros, too.
#                        `-p` reports *online* CPUs only - i.e., on hot-pluggable 
#                        systems, currently disabled (offline) CPUs are NOT
#                        reported.

# Number of LOGICAL CPUs (includes those reported by hyper-threading cores)
  # Linux: Simply count the number of (non-comment) output lines from `lscpu -p`, 
  # which tells us the number of *logical* CPUs.
logicalCpuCount=$([ $(uname) = 'Darwin' ] && 
                       sysctl -n hw.logicalcpu_max || 
                       lscpu -p | egrep -v '^#' | wc -l)

# Number of PHYSICAL CPUs (cores).
  # Linux: The 2nd column contains the core ID, with each core ID having 1 or
  #        - in the case of hyperthreading - more logical CPUs.
  #        Counting the *unique* cores across lines tells us the
  #        number of *physical* CPUs (cores).
physicalCpuCount=$([ $(uname) = 'Darwin' ] && 
                       sysctl -n hw.physicalcpu_max ||
                       lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l)

# Print the values.
cat <<EOF
# of logical CPUs:  $logicalCpuCount
# of physical CPUS: $physicalCpuCount
EOF

[1] macOS sysctl (3) documentation

Note that BSD-derived systems other than macOS - e.g., FreeBSD - only support the hw.ncpu key for sysctl, which are deprecated on macOS; I'm unclear on which of the new keys hw.npu corresponds to: hw.(logical|physical)cpu_[max].

Tip of the hat to @teambob for helping to correct the physical-CPU-count lscpu command.

Caveat: lscpu -p output does NOT include a "book" column (the man page mentions "books" as an entity between socket and node in the taxonomic hierarchy). If "books" are in play on a given Linux system (does anybody know when and how?), the physical-CPU-count command may under-report (this is based on the assumption that lscpu reports IDs that are non-unique across higher-level entities; e.g.: 2 different cores from 2 different sockets could have the same ID).


If you save the code above as, say, shell script cpus, make it executable with chmod +x cpus and place it in folder in your $PATH, you'll see output such as the following:

$ cpus
logical  4
physical 4

[1] Xaekai sheds light on what a book is: "a book is a module that houses a circuit board with CPU sockets, RAM sockets, IO connections along the edge, and a hook for cooling system integration. They are used in IBM mainframes. Further info: http://ewh.ieee.org/soc/cpmt/presentations/cpmt0810a.pdf"

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Yes sorry you are correct about the sort command. I can't find any information about books beyond the lscpu manual. I think it is related to NUMA http://en.wikipedia.org/wiki/Non-uniform_memory_access – teambob Apr 30 '14 at 21:06
  • 2
    I think most solutions here ignore multi-socket machines, unlike this one. Thanks! – dividebyzero Oct 16 '18 at 09:24
  • 1
    In terms of linux, there are many contexts where lscpu isn't available, such as installer images. I like it.. wish it were ubiquitous. – Brian Chrisman Sep 07 '19 at 01:59
49

lscpu gathers CPU architecture information form /proc/cpuinfon in human-read-able format:

# lscpu


Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    4
CPU socket(s):         2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 15
Stepping:              7
CPU MHz:               1866.669
BogoMIPS:              3732.83
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0-7

See also https://unix.stackexchange.com/questions/468766/understanding-output-of-lscpu.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
Sh4msi
  • 1,231
  • 13
  • 15
18

Here's the way I use for counting the number of physical cores that are online on Linux:

lscpu --online --parse=Core,Socket | grep --invert-match '^#' | sort --unique | wc --lines

or in short:

lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l

Example (1 socket):

> lscpu
...
CPU(s):                28
Thread(s) per core:    2
Core(s) per socket:    14
Socket(s):             1
....
> lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l
14

Example (2 sockets):

> lscpu
...
CPU(s):                56
Thread(s) per core:    2
Core(s) per socket:    14
Socket(s):             2
...
> lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l
28

Example (4 sockets):

> lscpu
...
CPU(s):                64
Thread(s) per core:    2
Core(s) per socket:    8
Socket(s):             4
...
> lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l
32
Fravadona
  • 13,917
  • 1
  • 23
  • 35
17

You can also use Python! To get the number of physical cores:

$ python -c "import psutil; print(psutil.cpu_count(logical=False))"
4

To get the number of hyperthreaded cores:

$ python -c "import psutil; print(psutil.cpu_count(logical=True))"
8
ostrokach
  • 17,993
  • 11
  • 78
  • 90
12

For the total number of physical cores:

grep '^core id' /proc/cpuinfo |sort -u|wc -l

On multiple-socket machines (or always), multiply the above result by the number of sockets:

echo $(($(grep "^physical id" /proc/cpuinfo | awk '{print $4}' | sort -un | tail -1)+1))

@mklement0 has quite a nice answer below using lscpu. I have written a more succinct version in the comments

teambob
  • 1,994
  • 14
  • 19
10

Using getconf is indeed the most portable way, however the variable has different names in BSD and Linux to getconf, so you have to test both, as this gist suggests: https://gist.github.com/jj1bdx/5746298 (also includes a Solaris fix using ksh)

I personally use:

$ getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || echo 1

And if you want this in python you can just use the syscall getconf uses by importing the os module:

$ python -c 'import os; print os.sysconf(os.sysconf_names["SC_NPROCESSORS_ONLN"]);'

As for nproc, it's part of GNU Coreutils, so not available in BSD by default. It uses sysconf() as well after some other methods.

mmu_man
  • 109
  • 1
  • 2
10

Crossplatform solution for Linux, MacOS, Windows:

CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu || echo "$NUMBER_OF_PROCESSORS")
Sergey
  • 1,552
  • 20
  • 18
7

If you want to do this so it works on linux and OS X, you can do:

CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • On mine, this returns the number of hyperthreads. I'd also need to know the number of physical cores and number of sockets. – user2465201 Sep 23 '19 at 16:56
  • OK - the exact same command with hw.physicalcpu yields the core count. Not sure yet about the socket count... There's also a "logical" and "active" cpu count, but I'm not sure what that means. – user2465201 Sep 23 '19 at 17:01
  • Quick update - this doesn't seem so portable. I tried it on a Linux box, and it seems to just read the /proc directories, which are all different for the Linux system than the Mac - for example, there's no hw subdirectory. That being said, you can still parse /proc/cpuinfo – user2465201 Sep 23 '19 at 17:15
6

You can use one of the following methods to determine the number of physical CPU cores.

  • Count the number of unique core ids (roughly equivalent to grep -P '^core id\t' /proc/cpuinfo | sort -u | wc -l).

    awk '/^core id\t/ {cores[$NF]++} END {print length(cores)}' /proc/cpuinfo

  • Multiply the number of 'cores per socket' by the number of sockets.

    lscpu | awk '/^Core\(s\) per socket:/ {cores=$NF}; /^Socket\(s\):/ {sockets=$NF}; END{print cores*sockets}'

  • Count the number of unique logical CPU's as used by the Linux kernel. The -p option generates output for easy parsing and is compatible with earlier versions of lscpu.

    lscpu -p | awk -F, '$0 !~ /^#/ {cores[$1]++} END {print length(cores)}'


Just to reiterate what others have said, there are a number of related properties.

To determine the number of processors available:

getconf _NPROCESSORS_ONLN
grep -cP '^processor\t' /proc/cpuinfo

To determine the number of processing units available (not necessarily the same as the number of cores). This is hyperthreading-aware.

nproc

I don't want to go too far down the rabbit-hole, but you can also determine the number of configured processors (as opposed to simply available/online processors) via getconf _NPROCESSORS_CONF. To determine total number of CPU's (offline and online) you'd want to parse the output of lscpu -ap.

Six
  • 5,122
  • 3
  • 29
  • 38
6

It is very simple. Just use this command:

lscpu
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
bhanu mantri
  • 85
  • 1
  • 1
4

The above answers are applicable to most situations, but if you are in a docker container environment and your container is limited by CpusetCpus, then you can't actually get the real cpu cores through the above method.

In this case, you need do this to get the real cpu cores:

grep -c 'cpu[0-9]' /proc/stat
aka.ecc
  • 153
  • 3
  • 12
  • 1
    Great solution ;-) Can't you get rid of the first `grep` in `| grep cpu | grep -E 'cpu[0-9]+' |`? – Fravadona Sep 02 '21 at 21:30
  • 1
    Nit 1: you don't need the `+` (and so also don't need the `-E`). You just need `cpu` to be followed by at least one digit. Nit 2: you don't need `cat`: you can just `grep 'cpu[0-9]' /proc/stat`. – Paul J. Lucas Sep 19 '21 at 14:21
3

The following should give you the number of "real" cores on both a hyperthreaded and non-hyperthreaded system. At least it worked in all my tests.

awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^cpu cores/ { CORES=$2 };  END { print CORES*P }' /proc/cpuinfo
foomiser
  • 47
  • 2
  • 1
    -1, this returns `0` on a single core with an Opteron 4170 HE, but returns `4` on an eight core box with an Opteron 3280. ...part of me really wishes this one-liner would work! – zamnuts Feb 19 '14 at 01:26
3

I also thought cat /proc/cpuinfo would give me the correct answer, however I recently saw that my ARM quad core Cortex A53 system only showed a single core. It seems that /proc/cpuinfo only shows the active cores, whereas:

cat /sys/devices/system/cpu/present

is a better measure of what's there. You can also

cat /sys/devices/system/cpu/online

to see which cores are online, and

cat /sys/devices/system/cpu/offline

to see which cores are offline. The online, offline, and present sysfs entries return the index of the CPUS, so a return value of 0 just means core 0, whereas a return value of 1-3 means cores 1,2, and 3.

See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu

3

In case anybody was wondering, here is what the Python psutil.cpu_count(logical=False) call does on Linux in equivalent shell script:

cat /sys/devices/system/cpu/cpu[0-9]*/topology/core_cpus_list | sort -u | wc -l

And here’s a slightly longer version that falls back to the information from the deprecated thread_siblings_list file if core_cpus_list isn’t available (psutil has this fallback):

cat /sys/devices/system/cpu/cpu[0-9]*/topology/{core_cpus_list,thread_siblings_list} | sort -u | wc -l
ntninja
  • 1,204
  • 16
  • 20
  • In the first command you should use `sort -u` (or `sort | uniq`) instead of `uniq` – Fravadona Aug 17 '21 at 17:37
  • @Fravadona: You're right! Using just `uniq` will work since the list of cores will evaluate in order with hyperthreads generally having been added at the same time, but it might not be guaranteed…! – ntninja Aug 31 '21 at 16:12
  • 1
    There's no guaranty indeed: I got one wrong result while testing the command on a few 4-socket servers; now all's good ;-) – Fravadona Sep 02 '21 at 21:09
2

Not my web page, but this command from http://www.ixbrian.com/blog/?p=64&cm_mc_uid=89402252817914508279022&cm_mc_sid_50200000=1450827902 works nicely for me on centos. It will show actual cpus even when hyperthreading is enabled.

cat /proc/cpuinfo | egrep "core id|physical id" | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l

Chaim Leib Halbert
  • 2,194
  • 20
  • 23
Chaim Geretz
  • 826
  • 5
  • 23
  • 1
    +1, longer than the solution with "lscpu -p=Core,Socket" but works directly parsing /proc/cpuinfo, no need for lscpu. – Fravadona Jul 12 '19 at 17:31
  • arrrgh! I did not see this one and wasted a bunch of time trying to come with my own. I had the same idea because `lscpu`is not available on my system, but yours is simpler than mine (https://stackoverflow.com/a/63248763/1593842). Would you mind explaining the `grep -v` part? PS: we could save some cycles by replacing `sort | uniq' with `sort -u`, right? – user1593842 Aug 04 '20 at 14:17
1

Count "core id" per "physical id" method using awk with fall-back on "processor" count if "core id" are not available (like raspberry)

echo $(awk '{ if ($0~/^physical id/) { p=$NF }; if ($0~/^core id/) { cores[p$NF]=p$NF }; if ($0~/processor/) { cpu++ } } END { for (key in cores) { n++ } } END { if (n) {print n} else {print cpu} }' /proc/cpuinfo)
1
cat /proc/cpuinfo | grep processor

This worked fine. When I tried the first answer I got 3 CPU's as the output. I know that I have 4 CPUs on the system so I just did a grep for processor and the output looked like this:

[root@theservername ~]# cat /proc/cpuinfo | grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3
derHugo
  • 83,094
  • 9
  • 75
  • 115
1

If it's okay that you can use Python, then numexpr module has a function for this:

In [5]: import numexpr as ne

In [6]: ne.detect_number_of_cores()
Out[6]: 8

also this:

In [7]: ne.ncores
Out[7]: 8

To query this information from the command prompt use:

# runs whatever valid Python code given as a string with `-c` option
$ python -c "import numexpr as ne; print(ne.ncores)"
8

Or simply it is possible to get this info from multiprocessing.cpu_count() function

$ python -c "import multiprocessing; print(multiprocessing.cpu_count())"

Or even more simply use os.cpu_count()

$ python -c "import os; print(os.cpu_count())"
kmario23
  • 57,311
  • 13
  • 161
  • 150
1

If you just want to count physical cores, this command did it for me.

lscpu -e | tail -n +2 | tr -s " " | cut -d " " -f 4 | sort | uniq | wc -w

Pretty basic, but seems to count actual physical cores, ignoring the logical count

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

Use below query to get core details

[oracle@orahost](TESTDB)$ grep -c ^processor /proc/cpuinfo
8
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Srikant Patra
  • 399
  • 4
  • 5
1

Fravadona's answer is awesome and correct, but it requires the presence of lscpu. Since it is not present on the system where I need the number of physical cores, I tried to come up with one that relies only on proc/cpuinfo

cat /proc/cpuinfo | grep -B2 'core id' | sed 's/siblings.*/'/ | tr -d '[:space:]' | sed 's/--/\n/'g | sort -u | wc -l

It works perfectly, but unfortunately it isn't as robust as Fravadona's, since it will break if

  • the name or order of the fields inside /proc/cpuinfo changes
  • grep replaces the line separator it inserts (currently --) by some other string.

BUT, other than that, it works flawlessly :)

Here is a quick explanation of everything that is happening

grep -B2 'core id'

get only the lines we are interested (i.e "core id" and the 2 preceding lines)

sed 's/siblings.*/'/

remove the "siblings..." line

tr -d '[:space:]'

replace spacing chars

sed 's/--/\n/'g

replace the '--' char, which was inserted by grep, by a line break

sort -u

group by "physical id,core id"

wc -l

count the number of lines

Being a total noobie, I was very pleased with myself when this worked. I never thought I would be able to join the required lines together to group by "physical id" and "core id". It is kind of hacky, but works.

If any guru knows a way to simplify this mess, please let me know.

user1593842
  • 358
  • 2
  • 9
  • 1
    You can also try [ntninja's answer](https://stackoverflow.com/a/68273982/3387716), it may work for your case ;-) – Fravadona Aug 17 '21 at 17:44
1

Most answers in this thread pertain to logical cores.

Using BaSH on Ubuntu 18.x, I find this works well to determine number of physical CPUs:

numcpu="$(lscpu | grep -i 'socket(s)' | awk '{print $(2)}')"

It should work on most Linux distros.

MrPotatoHead
  • 1,035
  • 14
  • 11
1

One more answer among the numerous previous ones. It is possible to use the cgroups when they are available. The cpuset sub-system provides the list of actives cpus. This can be listed in the top most cgroup of the hierarchy in /sys/fs/cgroup. For example:

$ cat /sys/fs/cgroup/cpuset/cpuset.effective_cpus
0-3

Then, a parsing of the latter would be necessary to get the number of active CPUs. The content of this file is a comma separated list of CPU sets.

Here is an example using tr to break the list into single expressions and using sed to translate the intervals into arithmetic operations passed to expr:

#!/bin/sh

# For test purposes, the CPU sets are passed as parameters
#cpuset=`cat /sys/fs/cgroup/cpuset/cpuset.effective_cpus`
cpuset=$1

ncpu=0
for e in `echo $cpuset | tr ',' ' '`
do
  case $e in

    # CPU interval ==> Make an arithmetic operation
    *-*) op=`echo $e | sed -E 's/([0-9]+)-([0-9]+)/\2 - \1 + 1/'`;;

    # Single CPU number
    *) op=1;;

  esac

  ncpu=`expr $ncpu + $op`

done

echo $ncpu

Here are some examples of executions with several flavors of CPU sets:

$ for cpuset in "0" "0,3" "0-3" "0-3,67" "0-3,67,70-75" "0,1-3,67,70-75"
> do
>   ncpu.sh $cpuset
> done
1
2
4
5
11
11
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
0
 dmidecode  | grep -i cpu | grep Version

gives me

Version: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz

Version: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz

Which is correct socket count - looking up the E5-2667 tells me each socket has 8 cores, so multiply and end up with 16 cores across 2 sockets.

Where lscpu give me 20 CPUs - which is totally incorrect - not sure why. (same goes for cat /proc/cpu - ends up with 20.

Community
  • 1
  • 1
Hank42
  • 9
  • 1
  • While that may be technically correct, the code you posted does not yield the number of cores (physical or logical), but instead prints the list of sockets and requires you to manually look up the processors specs. Obviously this isn't even remotely useful for scripted usage, requires root privileges and I just noticed it doesn't even work on my system since *Version: AMD FX-8800P Radeon R7, 12 Compute Cores 4C+8G* does not contain the word CPU… – ntninja Aug 31 '21 at 16:25
0

Quicker, without fork

This works with almost all .

ncore=0
while read line ;do
    [ "$line" ] && [ -z "${line%processor*}" ] && ncore=$((ncore+1))
  done </proc/cpuinfo
echo $ncore
4

In order to stay compatible with , , and others, I've used ncore=$((ncore+1)) instead of ((ncore++)).

version

ncore=0
while read -a line ;do
    [ "$line" = "processor" ] && ((ncore++))
  done </proc/cpuinfo
echo $ncore
4
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
0

Python 3 also provide a few simple ways to get it:

$ python3 -c "import os; print(os.cpu_count());"

4

$ python3 -c "import multiprocessing; print(multiprocessing.cpu_count())"

4

Lê Tư Thành
  • 1,063
  • 2
  • 10
  • 19
0

Summary: to get physical CPUs do this:

grep 'core id' /proc/cpuinfo | sort -u

to get physical and logical CPUs do this:

grep -c ^processor /proc/cpuinfo

/proc << this is the golden source of any info you need about processes and

/proc/cpuinfo << is the golden source of any CPU information.

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
syed
  • 1
  • 1
    The physical CPU counting doesn't work with multi-socket computers; for that you need to associate each 'core id' to its 'physical id'. – Fravadona Jun 06 '19 at 11:12
0

Considering your specific sample code snippet that prints information on available CPU cores e.g.

$ cat /proc/cpuinfo | awk '/^processor/{print $3}'

Two approaches

Derive total value based on the highest cpu core id value

Taking an an advantage of the fact that CPU cores have a sequential number attached to them and stored under a processor field. Because of the fact that the information about each individual CPU core stores in proc file is in a list ordered by processor field ascending, you can simply grab the last entry of that sequence and observe the numerical value in the processor field e.g.

$ cat /proc/cpuinfo | awk '/^processor/{print $3}' | tail -n 1

NOTE: Since /proc/cpuinfo holds a number of entries corresponding to the cpus count, a processor field initial value is 0, don't forget to increment the value of the last cpu core by 1.

$ last_cpu_core_id=$(/proc/cpuinfo | awk '/^processor/{print $3}' | tail -n 1)
$ echo $((last_cpu_core_id + 1))

Count number of lines from the output

This approach contrasts with the first one in that, we are not really concerned about a particular value of cpu core id, we just counting output lines. When it comes to counting, the process normally starts from 1 which simplifies our solution.

Using grep

$ cat /proc/cpuinfo | grep processor | wc -l

Alternatively, we shouldn't necessarily remember the processor field per se, but taking an advantage of the fact that each entry describing individual cpu core details is delimited by a newline e.g.

cat /proc/cpuinfo | grep -v '^\w' | wc -l

using awk

cat /proc/cpuinfo | awk '($1 == "processor") {count++ } END { print count }'
nakhodkin
  • 1,327
  • 1
  • 17
  • 27