简体   繁体   English

C#:如何找到 CPU 的当前时钟速度?

[英]C#: How to find the CPU's CURRENT Clock Speed?

Currently working on creating a sorts of "task manager" in c#/wpf.目前致力于在 c#/wpf 中创建一种“任务管理器”。 I've searched around but haven't found a solution to my problem.我四处寻找,但没有找到解决我的问题的方法。

I am trying to retrieve the CURRENT clock speed of one's CPU (not utilization, base, min/max).我正在尝试检索一个 CPU 的当前时钟速度(不是利用率、基本、最小/最大)。 I have tried using ManagementObjects, but "CurrentClockSpeed" is always giving a fixed value of 3400, or 3.4GHz, which is the stock max speed of the CPU.我曾尝试使用 ManagementObjects,但“CurrentClockSpeed”始终给出固定值 3400 或 3.4GHz,这是 CPU 的最大存储速度。 I have tried many times and it gives me the same answer, so it isn't just a coincidence i think.我已经尝试了很多次,它给了我相同的答案,所以我认为这不仅仅是一个巧合。

    ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
        uint sp = (uint)(Mo["CurrentClockSpeed"]);
        System.Threading.Thread.Sleep(1000);
        sp = (uint)(Mo["CurrentClockSpeed"]);
        Mo.Dispose(); //return and such later in the code

Any suggestions on how to fix this issue (I am not bound to using ManagementObjects, I have OpenHardwareMonitor, and can use other packages if need be) are appreciated.任何有关如何解决此问题的建议(我不一定要使用 ManagementObjects,我有 OpenHardwareMonitor,如果需要可以使用其他软件包),我们将不胜感激。

On the WMI object the MaxClockSpeed property gives you the maximum speed of the core, which should be constant.在 WMI 对象上, MaxClockSpeed属性为您提供核心的最大速度,该速度应该是恒定的。 The CurrentClockSpeed property tells you the current clock speed. CurrentClockSpeed属性告诉您当前的时钟速度。 This may be leess than the MaxClockSpeed dues to cpu throttling.由于 CPU 节流,这可能小于MaxClockSpeed

I believe you can disable throttling at the BIOS level or via the Windows power management control panel applet, so it's possible that *CurrentClockSpeed** will always be the same as MaxClockSpeed .我相信您可以在 BIOS 级别或通过 Windows 电源管理控制面板小程序禁用节流,因此 *CurrentClockSpeed** 可能始终与MaxClockSpeed相同。

I had the same question eight years before you did.比你早八年我也有同样的问题。 WMI does not return the real current clock speed, and this appears to still be the case, at least through Windows 10. WMI 不会返回真实的当前时钟速度,而且这种情况似乎仍然存在,至少在 Windows 10 中是这样。

For whatever reason, WMI only returns the base clock speed as the value for both maximum and current clock speed.无论出于何种原因,WMI 只返回基本时钟速度作为最大和当前时钟速度的值。 It's not an issue of CPU support;这不是 CPU 支持的问题; CPU-Z is able to report the correct clock speed, as does Task Manager. CPU-Z 能够报告正确的时钟速度,任务管理器也是如此。 It's a piece of data the OS has at its disposal, but doesn't make readily available.这是操作系统可以使用的一段数据,但并不容易获得。 There's probably a way to get the exact value from the CPU using C++, but lots of devs aren't fluent in that language.可能有一种方法可以使用 C++ 从 CPU 中获取准确的值,但是许多开发人员并不流利地使用该语言。

This awesome answer worked perfectly for me!这个很棒的答案对我来说非常有用! I finally got this application working properly, after starting (and abandoning) it in 2010.在 2010 年开始(并放弃)它之后,我终于让这个应用程序正常工作了。

(PS This doesn't work in Windows 7; it seems the perfmon counter used didn't exist back then.) (PS 这在 Windows 7 中不起作用;似乎使用的 perfmon 计数器当时不存在。)

When running the code at an Intel CPU with access to MSRs then you may evaluate the current CPU frequency from IA32_MPERF (0xE7) TSC Frequency Clock Counter and IA32_APERF (0xE8) Actual Performance Clock Counter.在可访问 MSR 的 Intel CPU 上运行代码时,您可以从IA32_MPERF (0xE7) TSC 频率时钟计数器和IA32_APERF (0xE8)实际性能时钟计数器评估当前 CPU 频率。

aperf_t1 = read_aperf();
mperf_t1 = read_mperf();

sleep(1);   

aperf_t2 = read_aperf();
mperf_t2 = read_mperf();

printf("CPU freq: %f [Hz]\n", ((aperf_t2-aperf_t1) / (double)(mperf_t2-mperf_t1)) * nominal_freq);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM