How to discover number of *logical* cores on Mac OS X?

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

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.

回答1:

You can do this using the sysctl utility:

sysctl -n hw.ncpu



回答2:

Even easier:

sysctl -n hw.ncpu 


回答3:

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:

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. 


回答4:

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.



回答5:

$ system_profiler | grep 'Total Number Of Cores' 


回答6:

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    

[~]

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



回答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 


回答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.



回答9:

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 


回答10:

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



回答11:

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 ' ' 


回答12:

The following command gives you all information about your CPU

$ sysctl -a | sort | grep cpu 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!