macos 在 OS X 上以编程方式检测当前 CPU 时钟速度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9948987/
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
Detect current CPU Clock Speed Programmatically on OS X?
提问by Tim
I just bought a nifty MBA 13" Core i7. I'm told the CPU speed varies automatically, and pretty wildly, too. I'd really like to be able to monitor this with a simple app.
我刚买了一个漂亮的 MBA 13" Core i7。有人告诉我 CPU 速度会自动变化,而且变化很大。我真的很想用一个简单的应用程序来监控它。
Are there any Cocoa or C calls to find the current clock speed, without actually affecting it?
是否有任何 Cocoa 或 C 调用来查找当前时钟速度而不实际影响它?
Edit: I'm OK with answers using Terminal calls, as well as programmatic.
编辑:我可以使用终端调用以及程序化来回答。
Thanks!
谢谢!
采纳答案by Yevgeni
Try this tool called "Intel Power Gadget". It displays IA frequency and IA power in real time.
试试这个名为“Intel Power Gadget”的工具。实时显示IA频率和IA功率。
http://software.intel.com/sites/default/files/article/184535/intel-power-gadget-2.zip
http://software.intel.com/sites/default/files/article/184535/intel-power-gadget-2.zip
回答by DarkDust
You can query the CPU speed easily via sysctl
, either by command line:
您可以sysctl
通过命令行轻松查询 CPU 速度:
sysctl hw.cpufrequency
Or via C:
或通过 C:
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
int main() {
int mib[2];
unsigned int freq;
size_t len;
mib[0] = CTL_HW;
mib[1] = HW_CPU_FREQ;
len = sizeof(freq);
sysctl(mib, 2, &freq, &len, NULL, 0);
printf("%u\n", freq);
return 0;
}
回答by Tommy
Since it's an Intel processor, you could always use RDTSC. That's an assembler instruction that returns the current cycle counter — a 64bit counter that increments every cycle. It'd be a little approximate but e.g.
由于它是 Intel 处理器,因此您始终可以使用 RDTSC。这是一条汇编指令,它返回当前周期计数器——一个 64 位计数器,每个周期递增。它会有点近似,但例如
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
uint64_t rdtsc(void)
{
uint32_t ret0[2];
__asm__ __volatile__("rdtsc" : "=a"(ret0[0]), "=d"(ret0[1]));
return ((uint64_t)ret0[1] << 32) | ret0[0];
}
int main(int argc, const char * argv[])
{
uint64_t startCount = rdtsc();
sleep(1);
uint64_t endCount = rdtsc();
printf("Clocks per second: %llu", endCount - startCount);
return 0;
}
Output 'Clocks per second: 2002120630' on my 2Ghz MacBook Pro.
在我的 2Ghz MacBook Pro 上输出“每秒时钟数:2002120630”。
回答by nurbs999
There is a kernel extension written by "flAked" which logs the cpu p-state to the kernel log. http://www.insanelymac.com/forum/index.php?showtopic=258612
有一个由“flAked”编写的内核扩展,它将 cpu p 状态记录到内核日志中。 http://www.insanelymac.com/forum/index.php?showtopic=258612
maybe you could contact him for the code.
也许你可以联系他获取代码。
回答by tbh
This seems to work correctly on OSX. However, it doesn't work on Linux, where sysctl is deprecated and KERN_CLOCKRATE is undefined.
这似乎在 OSX 上正常工作。但是,它不适用于 Linux,其中 sysctl 已弃用且 KERN_CLOCKRATE 未定义。
#include <sys/sysctl.h>
#include <sys/time.h>
int mib[2];
size_t len;
mib[0] = CTL_KERN;
mib[1] = KERN_CLOCKRATE;
struct clockinfo clockinfo;
len = sizeof(clockinfo);
int result = sysctl(mib, 2, &clockinfo, &len, NULL, 0);
assert(result != -1);
log_trace("clockinfo.hz: %d\n", clockinfo.hz);
log_trace("clockinfo.tick: %d\n", clockinfo.tick);