439

How can you tell, from the command line, how many cores are on the machine when you're running Mac OS X? On Linux, I use:

x=$(awk '/^processor/ {++n} END {print n+1}' /proc/cpuinfo)

It's not perfect, but it's close. This is intended to get fed to make, which is why it gives a result 1 higher than the actual number. And I know the above code can be written denser in Perl or can be written using grep, wc, and cut, but I decided the above was a good tradeoff between conciseness and readability.

VERY LATE EDIT: Just to clarify: I'm asking how many logical cores are available, because this corresponds with how many simultaneous jobs I want make to spawn. jkp's answer, further refined by Chris Lloyd, was exactly what I needed. YMMV.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • For linux, I avoid having to add one by using: `grep ^processor /proc/cpuinfo | wc -l` – courtlandj Jul 01 '14 at 21:30
  • 3
    See http://stackoverflow.com/questions/6481005/obtain-the-number-of-cpus-cores-in-linux Note the `nproc` command. – Mike DeSimone Jul 01 '14 at 22:57
  • sorry if this is a noob question but when you mean logical cores you mean physical cores? How are the terms different (or not)? Just to understand what your asking better. – Charlie Parker Oct 31 '16 at 16:26
  • No, I don't mean physical cores. A physical core is a group of transistors on a chip that implements a core. A logical core is something that appears to be a core to the operating system and user programs. In the vast number of cases, these are the same. However, Intel's Hyperthreading technology (and maybe others) makes each physical core appear as two logical cores. – Mike DeSimone Nov 20 '16 at 14:27
  • 1
    As a FYI, on Linux nowadays the nproc utility is the preferred way to find out this information, rather than trying to count processor information. nproc lets you find out the total cores and the maximum your process is allowed to spawn. That's an important distinction sometimes. – Greg Smith Jun 25 '20 at 19:44

15 Answers15

920

You can do this using the sysctl utility:

sysctl -n hw.ncpu
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
jkp
  • 78,960
  • 28
  • 103
  • 104
  • 8
    On a mid-2011 1.7GHz MacBook Air, this command says 4. However, I suspected it really only has 2 cores, and system_profiler SPHardwareDataType seems to agree. Can you explain the discrepancy? – Joshua Flanagan Sep 24 '11 at 01:42
  • 15
    @JoshuaFlanagan how many physical cores does the machine have and what chip is it? If it's a core i7 with 2 physical cores for example, it will show as 4 because the chip supports hyper-threading and presents itself to the OS as if it has 4 addressable cores. – jkp Sep 26 '11 at 08:51
  • 155
    If you really care, try `sysctl hw.physicalcpu` or `sysctl hw.logicalcpu`, and in general `sysctl hw` for all of the options. – Variable Length Coder Nov 01 '11 at 00:25
  • 30
    Editors: Please don't change the answer years after I've accepted it. – Mike DeSimone May 13 '14 at 17:04
  • 2
    Doesnt work on Early 2015 Mac running El Capitan. I get "error: "hw.ncpu" is an unknown key". There is no cpu related entry in /etc/sysctl.conf as well. Frozen Flame's answer worked. – TheGT Oct 05 '16 at 23:39
  • @JoshuaFlanagan Apple's i5s support hyper threading, even though it's not standard. – William T Froggard Jan 03 '17 at 20:03
145

Even easier:

sysctl -n hw.ncpu
Matt
  • 74,352
  • 26
  • 153
  • 180
mlbright
  • 2,181
  • 2
  • 17
  • 14
  • 3
    The number reported by this is actually double the number of cores is your processor supports hyper-threading. – mikeazo Apr 24 '14 at 15:31
  • 3
    this gives me 4, but when I use `system_profiler SPHardwareDataType` which i can believe ` Model Identifier: MacBookPro9,2 Processor Name: Intel Core i5 Processor Speed: 2.5 GHz Number of Processors: 1 Total Number of Cores: 2` – Ciasto piekarz Jun 11 '14 at 13:30
37

This should be cross platform. At least for Linux and Mac OS X.

python -c 'import multiprocessing as mp; print(mp.cpu_count())'

A little bit slow but works.

Frozen Flame
  • 3,135
  • 2
  • 23
  • 35
21

system_profiler SPHardwareDataType shows I have 1 processor and 4 cores.

[~] system_profiler SPHardwareDataType
Hardware:

    Hardware Overview:

      Model Name: MacBook Pro
      Model Identifier: MacBookPro9,1
      Processor Name: Intel Core i7
      Processor Speed: 2.6 GHz
      Number of Processors: 1
      Total Number of Cores: 4

      <snip>

[~] 

However, sysctl disagrees:

[~] sysctl -n hw.logicalcpu
8
[~] sysctl -n hw.physicalcpu
4
[~] 

But sysctl appears correct, as when I run a program that should take up all CPU slots, I see this program taking close to 800% of CPU time (in top):

PID   COMMAND      %CPU  
4306  top          5.6   
4304  java         745.7 
4296  locationd    0.0  
adrin
  • 4,511
  • 3
  • 34
  • 50
user1706991
  • 287
  • 2
  • 4
  • 4
    You're seeing Hyperthreading at work. This lets each core handle two execution threads (not necessarily the same as OS threads, such as pthreads). As a result, MacOS treats each physical core as two logical cores. – Mike DeSimone Jun 11 '13 at 13:27
  • Hello @MikeDeSimone can you show me how to check if hyper threading at work in case of CentOS ? – Ciasto piekarz Jun 11 '14 at 13:32
  • 1
    http://stackoverflow.com/questions/2901694/programatically-detect-number-of-physical-processors-cores-or-if-hyper-threading – Mike DeSimone Jun 12 '14 at 03:17
  • @Ciastopiekarz I am fairly certain hyper threading is a feature of the CPU architecture (Intel) rather than something the OS does, I think the whole point is to make the OS think it has physical cpu:s x 2 logical cores without having to do anything special to access them. So if your CPU is an Intel (all Macs come with Intel) then yes, you have hyper threading. If, however you run CentOS on a PC, you may have an AMD processor. They don't have hyper threading but since they usually come with twice as many cores at the same (or lower) price I'm not sure there's a practical difference. – Erk Sep 08 '16 at 22:38
  • @Erk The OS knows the difference, in fact, it knows the difference between CPUs and Cores as well as multiple threads running on a physical. `sysctl hw.physicalcpu` will show how many physical cpu cores are available to you, `sysctl hw.logicalcpu` will show how many logical cpus available to you. OS's like Windows/OSX/Linux do all work of making it seems like you have multiple CPUs (regardless of what type it is). OS's like linux allow you to configure which cpu, cores, threading can be used. – Rahly Sep 23 '18 at 23:29
  • @Rahly thanks for the commands! Nice to have. I was replying to Ciasto ... trying to assure them that if they had an Intel processor and hadn't done any hardcore configuration they would have hyperthreading. I hope that's how it works... or maybe I have an explanation for why my computers run out of steam after just a couple of years! :D (yes, `sysctl hw.logicalcpu` returns what I was expecting!) – Erk Sep 26 '18 at 21:12
  • @Erk np, i was just saying that although it is architectural, the OS has to know about it or it can't use that functionality. AMD uses SMT in Ryzen chips which essentially does the same thing (Intel just uses HyperThreading name to represent SMT) History: https://en.wikipedia.org/wiki/Simultaneous_multithreading – Rahly Sep 28 '18 at 20:14
20

To do this in C you can use the sysctl(3) family of functions:

int count;
size_t count_len = sizeof(count);
sysctlbyname("hw.logicalcpu", &count, &count_len, NULL, 0);
fprintf(stderr,"you have %i cpu cores", count);

Interesting values to use in place of "hw.logicalcpu", which counts cores, are (from this comment in the kernel source):

  • hw.ncpu: The maximum number of processors that could be available this boot.

    Use this value for sizing of static per processor arrays; i.e. processor load statistics.

  • hw.activecpu: The number of processors currently available for executing threads.

    Use this number to determine the number threads to create in SMP aware applications.

    This number can change when power management modes are changed.

  • hw.physicalcpu: The number of physical processors available in the current power management mode.

  • hw.physicalcpu_max: The maximum number of physical processors that could be available this boot.

  • hw.logicalcpu: The number of logical processors available in the current power management mode.

  • hw.logicalcpu_max: The maximum number of logical processors that could be available this boot.

tbodt
  • 16,609
  • 6
  • 58
  • 83
alfwatt
  • 2,010
  • 2
  • 18
  • 29
17

getconf works both in Mac OS X and Linux, just in case you need it to be compatible with both systems:

$ getconf _NPROCESSORS_ONLN
12
Pere Joan Martorell
  • 2,608
  • 30
  • 29
10
$ system_profiler | grep 'Total Number Of Cores'
kent
  • 6,286
  • 4
  • 27
  • 32
8

Use the system_profiler | grep "Cores" command.

I have a:

MacBook Pro Retina, Mid 2012.

Processor: 2.6 GHz Intel Core i7

user$ system_profiler | grep "Cores"
      Total Number of Cores: 4

user$ sysctl -n hw.ncpu
8

According to Wikipedia, (http://en.wikipedia.org/wiki/Intel_Core#Core_i7) there is no Core i7 with 8 physical cores so the Hyperthreading idea must be the case. Ignore sysctl and use the system_profiler value for accuracy. The real question is whether or not you can efficiently run applications with 4 cores (long compile jobs?) without interrupting other processes.

Running a compiler parallelized with 4 cores doesn't appear to dramatically affect regular OS operations. So perhaps treating it as 8 cores is not so bad.

Community
  • 1
  • 1
NetworkN8
  • 81
  • 1
  • 1
  • 1
    Spawning a number of processes equal to the number of *logical* cores (twice the number of physical cores in the case of HyperThreading, as you note) has performed quite well for me. – Mike DeSimone Jun 27 '13 at 13:57
7

It wasn't specified in the original question (although I saw OP post in comments that this wasn't an option), but many developers on macOS have the Homebrew package manager installed.

For future developers who stumble upon this question, as long as the assumption (or requirement) of Homebrew being installed exists (e.g., in an engineering organization in a company), nproc is one of the common GNU binaries that is included in the coreutils package.

brew install coreutils

If you have scripts that you would prefer to write once (for Linux + macOS) instead of twice, or to avoid having if blocks where you need to detect the OS to know whether or not to call nproc vs sysctl -n hw.logicalcpu, this may be a better option.

Ryan Parman
  • 6,855
  • 1
  • 29
  • 43
7

As jkp said in a comment, that doesn't show the actual number of physical cores. to get the number of physical cores you can use the following command:

system_profiler SPHardwareDataType
Tohid
  • 21,535
  • 8
  • 30
  • 43
  • The goal was to determine how many processes to spawn with `make`, so jkp's answer (number of logical cores) fits best. – Mike DeSimone Nov 01 '11 at 17:37
5

The following command gives you all information about your CPU

$ sysctl -a | sort | grep cpu
asmaier
  • 11,132
  • 11
  • 76
  • 103
1

CLARIFICATION

When this question was asked the OP did not say that he wanted the number of LOGICAL cores rather than the actual number of cores, so this answer logically (no pun intended) answers with a way to get the actual number of real physical cores, not the number that the OS tries to virtualize through hyperthreading voodoo.

UPDATE TO HANDLE FLAW IN YOSEMITE

Due to a weird bug in OS X Yosemite (and possibly newer versions, such as the upcoming El Capitan), I've made a small modification. (The old version still worked perfectly well if you just ignore STDERR, which is all the modification does for you.)


Every other answer given here either

  1. gives incorrect information
  2. gives no information, due to an error in the command implementation
  3. runs unbelievably slowly (taking the better part of a minute to complete), or
  4. gives too much data, and thus might be useful for interactive use, but is useless if you want to use the data programmatically (for instance, as input to a command like bundle install --jobs 3 where you want the number in place of 3 to be one less than the number of cores you've got, or at least not more than the number of cores)

The way to get just the number of cores, reliably, correctly, reasonably quickly, and without extra information or even extra characters around the answer, is this:

system_profiler SPHardwareDataType 2> /dev/null | grep 'Total Number of Cores' | cut -d: -f2 | tr -d ' '
iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • 3
    I just ran `sysctl -n hw.ncpu` on my Mac Pro and had *none* of those problems. – Mike DeSimone Jan 10 '14 at 12:11
  • I obviously have not tested on all types of Macs, but I'll believe you that it is *sometimes* accurate. However, `sysctl` does not give accurate results on my machine, and others have reported the same thing. If it can't be relied on to *always* give accurate results, then it can't be considered reliable and accurate. – iconoclast Jan 10 '14 at 15:50
  • You could also explain the erroneous behavior you see, and what platform(s) you see it on, so the bounds of `sysctl`'s proper operation are better known, like user1706991 did. – Mike DeSimone Jan 12 '14 at 20:01
  • Others have suggested that this is due to hyper threading, so the number you see with sysctl is the number of cores the system is trying to simulate, even though it does not actually have that many. If you want accurate numbers, use my method. If you want to know how many cores the computer is pretending to have, use sysctl. – iconoclast Jan 12 '14 at 20:09
  • @iconoclast in testing this I found that 'Total Number of Cores' is localized. If OS X is set to anything but English your solution fails. Ended up using: `sysctl -n hw.physicalcpu_max` – C.O. Jul 01 '18 at 15:14
1

On a MacBook Pro running Mavericks, sysctl -a | grep hw.cpu will only return some cryptic details. Much more detailed and accessible information is revealed in the machdep.cpu section, ie:

sysctl -a | grep machdep.cpu

In particular, for processors with HyperThreading (HT), you'll see the total enumerated CPU count (logical_per_package) as double that of the physical core count (cores_per_package).

sysctl -a | grep machdep.cpu  | grep per_package
nbro
  • 15,395
  • 32
  • 113
  • 196
Karl Ehr
  • 19
  • 1
1

Comments for 2 good replies above:

1) re the accepted reply (and comments) by jkp: hw.ncpu is apparently deprecated in favor of hw.logicalcpu (https://ghc.haskell.org/trac/ghc/ticket/8594)

2) re the 2014 update by Karl Ehr: on my computer (with 2.5 ghz intel core i7),sysctl -a | grep machdep.cpu | grep per_package returns different numbers:

machdep.cpu.logical_per_package: 16

machdep.cpu.cores_per_package: 8

The desired values are:

machdep.cpu.core_count: 4

machdep.cpu.thread_count: 8

Which match:

hw.physicalcpu: 4

hw.logicalcpu: 8

Mark
  • 597
  • 5
  • 8
  • I also get what seems like nonsense values for X_per_package on a 6-core 2018 Intel Mac Mini: machdep.cpu.logical_per_package: 16 machdep.cpu.cores_per_package: 8 machdep.cpu.core_count: 6 machdep.cpu.thread_count: 6 – craig65535 Jul 20 '22 at 03:28
0

This can be done in a more portable way:

$ nproc --all
32

Compatible with macOS and Linux.

Jay Taylor
  • 13,185
  • 11
  • 60
  • 85