如何发现 Mac OS X 上的“逻辑”核心数量?
当您运行 Mac OS X 时,如何从命令行得知计算机上有多少个内核?在 Linux 上,我使用:
x=$(awk '/^processor/ {++n} END {print n+1}' /proc/cpuinfo)
它并不完美,但已经很接近了。这是为了提供给 make
,这就是为什么它给出的结果比实际数字高 1。我知道上面的代码可以用 Perl 编写得更密集,或者可以使用 grep、wc 和 cut 编写,但我认为上面的代码是简洁性和可读性之间的一个很好的权衡。
非常晚的编辑:只是为了澄清:我问有多少逻辑核心可用,因为这对应于我想要make 产卵。 jkp 的答案经过 Chris Lloyd 的进一步完善,正是我所需要的。 YMMV。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您可以使用 sysctl 实用程序来执行此操作:
You can do this using the sysctl utility:
更简单:
Even easier:
这应该是跨平台的。至少对于 Linux 和 Mac OS X 来说是这样。
虽然有点慢,但是可以用。
This should be cross platform. At least for Linux and Mac OS X.
A little bit slow but works.
getconf
同时适用于 Mac OS X 和 Linux,以防万一您需要它与这两个系统兼容:getconf
works both in Mac OS X and Linux, just in case you need it to be compatible with both systems:system_profiler SPHardwareDataType
显示我有 1 个处理器和 4 个内核。然而,sysctl 不同意:
但 sysctl 看起来是正确的,因为当我运行一个应该占用所有 CPU 插槽的程序时,我发现该程序占用了接近 800% 的 CPU 时间(在
top
中):system_profiler SPHardwareDataType
shows I have 1 processor and 4 cores.However, sysctl disagrees:
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
):要在 C 中执行此操作,您可以使用 sysctl(3) 系列函数:
用于代替计算核心的“hw.logiccpu”的有趣值是(来自 内核源代码中的此注释):
hw.ncpu:本次启动可用的最大处理器数量。
使用该值来调整每个处理器静态数组的大小;即处理器负载统计。
hw.activecpu
:当前可用于执行线程的处理器数量。使用此数字来确定要在 SMP 感知应用程序中创建的线程数。
当电源管理模式更改时,此数字可能会更改。
hw.physicalcpu
:当前电源管理模式下可用的物理处理器数量。hw.physicalcpu_max
:本次启动可用的物理处理器的最大数量。hw.logiccpu
:当前电源管理模式下可用的逻辑处理器数量。hw.ticalcpu_max
:本次启动可用的逻辑处理器的最大数量。To do this in C you can use the sysctl(3) family of functions:
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.使用system_profiler | grep“核心”命令。
我有一个:
根据 Wikipedia,(http:// en.wikipedia.org/wiki/Intel_Core#Core_i7)没有具有 8 个物理核心的 Core i7,因此超线程的想法一定是这种情况。忽略
sysctl
并使用system_profiler
值来确保准确性。真正的问题是您是否可以在不中断其他进程的情况下有效地运行具有 4 个核心的应用程序(长时间的编译作业?)。运行具有 4 个内核的并行编译器似乎不会显着影响常规操作系统操作。因此,也许将其视为 8 核也不是那么糟糕。
Use the
system_profiler | grep "Cores"
command.I have a:
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 thesystem_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.
正如 jkp 在评论中所说,这并没有显示实际的物理核心数量。要获取物理核心的数量,您可以使用以下命令:
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:
原始问题中没有指定(尽管我在评论中看到 OP 帖子说这不是一个选项),但 macOS 上的许多开发人员都安装了 Homebrew 包管理器。
对于未来偶然发现这个问题的开发人员来说,只要存在安装 Homebrew 的假设(或要求)(例如,在公司的工程组织中),nproc 就是常见的 GNU 二进制文件之一它包含在
coreutils
包中。如果您希望编写一次脚本(适用于 Linux + macOS)而不是两次,或者避免使用
if
块来检测操作系统以了解是否调用nproc
与sysctl -n hw.logiccpu
相比,这可能是更好的选择。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 thecoreutils
package.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 callnproc
vssysctl -n hw.logicalcpu
, this may be a better option.以下命令为您提供有关 CPU 的所有信息
The following command gives you all information about your CPU
对上面 2 个好的回复的评论:
1)关于 jkp 接受的回复(和评论):hw.ncpu 显然已被弃用,有利于 hw.logiccpu (https://ghc.haskell.org/trac/ghc/ticket/8594)
2) 关于 Karl Ehr 的 2014 年更新:在我的计算机上(使用 2.5 ghz 英特尔酷睿 i7),
sysctl -a | grep machdep.cpu | grep machdep.cpu | grep per_package
返回不同的数字:machdep.cpu.logic_per_package: 16
machdep.cpu.cores_per_package: 8
所需的值为:
machdep.cpu.core_count: 4
machdep.cpu.thread_count: 8
哪个匹配:
hw.physicalcpu: 4
硬件逻辑CPU:8
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
澄清
当提出这个问题时,OP并没有说他想要逻辑核心的数量,而不是实际 核心数量,因此这个答案在逻辑上(无双关语)是通过获取实际 真实物理核心数量的方法来回答的,而不是操作系统尝试获取的数量通过超线程巫毒进行虚拟化。
更新以处理 YOSEMITE 中的缺陷
由于奇怪的 bug,我做了一个小修改。 (如果您忽略 STDERR,旧版本仍然可以很好地工作,这就是所有修改为您所做的。)
此处给出的所有其他答案要么
bundle install --jobs 3
这样的命令,您希望代替3
的数字比您拥有的核心数少 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
bundle install --jobs 3
where you want the number in place of3
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:
在运行 Mavericks 的 MacBook Pro 上,
sysctl -a | grep hw.cpu 只会返回一些神秘的细节。
machdep.cpu
部分显示了更多详细且易于访问的信息,即:特别是,对于具有
HyperThreading
(HT) 的处理器,您将看到枚举的 CPU 总数计数 (logic_per_package
) 是物理核心计数 (cores_per_package
) 的两倍。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 themachdep.cpu
section, ie: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
).跨平台解决方案:
Cross-platform solution:
这可以通过更便携的方式来完成:
与 macOS 和 Linux 兼容。
This can be done in a more portable way:
Compatible with macOS and Linux.
为了跟进 @alfwatt 的回答,我继续使用
sysctlnametomib
来获取 MIB 值对于所有这些条目(此处为 macOS 13.6)。根据手册页:
因此,我认为采取这条路线是值得的。以下是每个相应字符串值的 MIB 值:
hw.ncpu
int mib[2] = {6, 3};
hw.activecpu
int mib[2] = {6, 25};
hw.physicalcpu
int mib[2] = {6, 104 };
hw.physicalcpu_max
int mib[2] = {6, 105};
hw.logiccpu
int mib [2] = {6, 106};
hw.logiccpu_max
int mib[2] = {6, 107};
以下是如何使用它们(< em>我将使用第一个作为示例):
To follow up on @alfwatt's answer, I went ahead and used
sysctlnametomib
to get the MIB values for all of these entries (macOS 13.6 here).According to the man page:
So, I think it's worthwhile to take this route. Here are the MIB values for each corresponding string value:
hw.ncpu
int mib[2] = {6, 3};
hw.activecpu
int mib[2] = {6, 25};
hw.physicalcpu
int mib[2] = {6, 104};
hw.physicalcpu_max
int mib[2] = {6, 105};
hw.logicalcpu
int mib[2] = {6, 106};
hw.logicalcpu_max
int mib[2] = {6, 107};
Here's how you use them (I'll use the first one as an example):