macos 如何在 Mac OS X 上发现*逻辑*内核的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1715580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to discover number of *logical* cores on Mac OS X?
提问by Mike DeSimone
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:
当您运行 Mac OS X 时,您如何从命令行判断机器上有多少个内核?在 Linux 上,我使用:
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.
这并不完美,但已经接近了。这是为了得到喂食make
,这就是为什么它给出的结果比实际数字高 1。我知道上面的代码可以用 Perl 写得更密集,也可以用 grep、wc 和 cut 编写,但我认为上面的代码是简洁性和可读性之间的一个很好的折衷。
VERY LATE EDIT:Just to clarify: I'm asking how many logicalcores are available, because this corresponds with how many simultaneous jobs I want make
to spawn. jkp's answer, further refined by Chris Lloyd, was exactlywhat I needed. YMMV.
非常晚的编辑:澄清一下:我在问有多少逻辑内核可用,因为这对应于我想要make
生成的并发作业数。jkp 的答案,由 Chris Lloyd 进一步完善,正是我所需要的。天啊。
回答by mlbright
Even easier:
更简单:
sysctl -n hw.ncpu
回答by Frozen Flame
This should be cross platform. At least for Linux and Mac OS X.
这应该是跨平台的。至少对于 Linux 和 Mac OS X。
python -c 'import multiprocessing as mp; print(mp.cpu_count())'
A little bit slow but works.
有点慢,但有效。
回答by alfwatt
To do this in C you can use the sysctl(3) family of functions:
要在 C 中执行此操作,您可以使用 sysctl(3) 系列函数:
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.logicalcpu”的有趣值是:
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.
回答by user1706991
system_profiler SPHardwareDataType
shows I have 1 processor and 4 cores.
system_profiler SPHardwareDataType
显示我有 1 个处理器和 4 个内核。
[~] 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 不同意:
[~] 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
):
但是 sysctl 看起来是正确的,因为当我运行一个应该占用所有 CPU 插槽的程序时,我看到这个程序占用了接近 800% 的 CPU 时间(在top
):
PID COMMAND %CPU
4306 top 5.6
4304 java 745.7
4296 locationd 0.0
回答by kent
$ system_profiler | grep 'Total Number Of Cores'
回答by NetworkN8
Use the system_profiler | grep "Cores"
command.
使用system_profiler | grep "Cores"
命令。
I have a:
我有一个:
MacBook Pro Retina, Mid 2012.
Processor: 2.6 GHz Intel Core i7
MacBook Pro Retina,2012 年中。
处理器: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 Hyperthreadingidea 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.
根据维基百科,(http://en.wikipedia.org/wiki/Intel_Core#Core_i7)没有具有 8 个物理内核的 Core i7,所以超线程的想法一定是这样的。忽略sysctl
并使用该system_profiler
值以获得准确性。真正的问题是您是否可以在不中断其他进程的情况下有效地运行具有 4 个内核的应用程序(长编译作业?)。
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.
运行具有 4 个内核的并行编译器似乎不会显着影响常规操作系统操作。所以也许将其视为 8 核也不错。
回答by Tohid
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:
正如 jkp 在评论中所说,这并没有显示物理内核的实际数量。要获取物理内核的数量,您可以使用以下命令:
system_profiler SPHardwareDataType
回答by asmaier
The following command gives you all information about your CPU
以下命令为您提供有关 CPU 的所有信息
$ sysctl -a | sort | grep cpu
回答by Karl Ehr
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:
在运行 Mavericks的MacBook Pro 上,sysctl -a | grep hw.cpu
只会返回一些神秘的细节。该machdep.cpu
部分显示了更详细和可访问的信息,即:
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
).
特别是,对于具有HyperThreading
(HT) 的处理器,您会看到总枚举 CPU 计数 ( logical_per_package
) 是物理内核计数 ( cores_per_package
) 的两倍。
sysctl -a | grep machdep.cpu | grep per_package