Number of CPU cores on Linux and OS XPublished 2015-6-29
sysctl -n hw.ncpu: OS X
I can see the number of cores or the number of threads.
sysctl -n hw.ncpu 8 sysctl -n hw.physicalcpu 4 sysctl -n hw.logicalcpu 8
nproc: Raspberry Pi & Linux
On a Raspberry Pi 2 I can see that I have 4 cores:
This isn't fool-proof. If a core is "unavailable" (whatever that means), it won't be reported.
I believe this also reports "logical" (hyperthread) cores too, but I'm not certain.
grep -c proc /proc/cpuinfo
This will count logical cores.
That usually means
num_cores * 2 -
because each hyperthread-enabled core counts twice.
grep -c processor /proc/cpuinfo
This counts the occurances of the word 'processor' in the text of
which looks like this:
cat /proc/cpuinfo processor : 0 model name : ARMv7 Processor rev 5 (v7l) BogoMIPS : 51.20 Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc07 CPU revision : 5 ... Hardware : BCM2709 Revision : a01041 Serial : 00000000e3a06ac9
cat /proc/cpuinfo | grep cores
This doesn't work on Raspberry Pi, but it will on most HyperThread-enabled systems.
cat /proc/cpuinfo | grep 'cpu cores' | cut -d ':' -f2 | cut -d' ' -f2
I don't have a multi-core machine to test this on, but this should work in most cases:
NUM=$(lscpu --parse=core | grep -v '^#' | sort -nr | head -n 1) NUM=$(($NUM + 1))
If you had a really weird setup - say a 2 socket machine where 1 cpu had 2 cores that don't support hyper-threading and the other had 3 cores that do support it and you needed an accurate count... you could parse this output and get accurate results.
lscpu --parse=cpu,core,socket | grep -v '^#'
lscpu uses the term cpu to mean thread (on a cpu core).
It uses socket to mean a physical cpu die and core to mean a cpu core.
There are approximately 1 trillion brazillion million billion eleventy six other ways to do this,
but they pretty much all involve somehow parsing either
By AJ ONeal
Did I make your day?
Buy me a coffee