无法在Powershell或Python中获得当前的CPU频率 [英] Unable to get current CPU frequency in Powershell or Python
问题描述
我正在尝试以编程方式记录Windows 10计算机的CPU频率.但是,我显然无法获得任务管理器中所示的当前频率.
I am trying to somehow programamtically log the CPU frequency of my windows 10 machine. However I apparently fail to get the current frequency as shown in the task manager.
在Powershell中,使用
in Powershell, using
get-wmiobject Win32_Processor -Property CurrentClockSpeed
仅返回恰好最大的时钟速度(即使我在任务管理器中看到它没有运行得那么快)
does only return a clock speed that is exactly the maximum one (even though i can see in task manager that it is not running that high)
I even tried this solution: https://www.remkoweijnen.nl/blog/2014/07/18/get-actual-cpu-clock-speed-powershell/ but it did not give me anything but a static value = max value.
即使python的psutil也仅返回静态值.
Even python's psutil does only return a static value.
有人知道如何解决这个问题,实际上每隔几秒钟就以某种方式记录CPU频率吗?
Does anybody know how to get around this and actually somehow log the CPU frequency each x seconds?
我们将不胜感激,谢谢!
any help would be appreciated, thanks!
推荐答案
TLDR:要找到当前处理器频率,您必须使用%Processor Performance
性能计数器:
TLDR: To find the Current Processor Frequency, you have to use the % Processor Performance
performance counter:
$MaxClockSpeed = (Get-CimInstance CIM_Processor).MaxClockSpeed
$ProcessorPerformance = (Get-Counter -Counter "\Processor Information(_Total)\% Processor Performance").CounterSamples.CookedValue
$CurrentClockSpeed = $MaxClockSpeed*($ProcessorPerformance/100)
Write-Host "Current Processor Speed: " -ForegroundColor Yellow -NoNewLine
Write-Host $CurrentClockSpeed
关于为什么为什么向WMI Win32_Processor
查询 CurrentClockSpeed
似乎总是返回最大频率而不是实际的当前时钟速度"的更深入的解释?实际上,为什么所有数十个WMI/CMI/Perfmon计数器似乎都返回错误"的频率?如果CPU-Z和任务管理器可以得到它,我们该怎么做才能获得实际"频率?要回答这个问题,我们需要了解实际上返回的 CurrentClockSpeed
.
The more in depth explanation as to why does querying WMI Win32_Processor
for CurrentClockSpeed
seem to always return the maximum frequency rather than the actual "Current Clock Speed"? In fact, why do all of the dozens of WMI/CMI/Perfmon counters all seem to return the "wrong" frequency? If CPU-Z and Task Manager can get it, what do we have to do to get the "actual" frequency? To answer that, we need to understand what CurrentClockSpeed
is actually returning.
从
处理器的当前速度,以MHz为单位.此值来自处理器信息结构中的当前速度"成员SMBIOS信息.
Current speed of the processor, in MHz. This value comes from the Current Speed member of the Processor Information structure in the SMBIOS information.
太好了!有人会认为,这种简单的查询应该为我们提供当前的频率.这项功能在十多年前就发挥了很大的作用,但如今却行不通了.因为它实际上仅适用于两种非常特殊的情况:
Great! One would think that this simple query should get us the current frequency. This worked great a dozen years ago, but nowadays it doesn't; because it really only works for two very specific cases:
- 当您的处理器只能以其定义的库存速度运行时.
- Windows要求移动处理器以不同的速度运行(例如,进入电池模式).
在启动时,寡妇获得处理器信息并获得当前时钟速度.大多数人都在建议的设置下运行处理器,因此 Current Clock Speed == Max Clock Speed
,这意味着两个数字始终匹配.当您更改电源状态时,Windows将更改频率,并且 CurrentClockSpeed
也将更改.
At startup, Widows gets the processor information and gets the Current Clock Speed. Most people are running their processor at the recommended settings so Current Clock Speed == Max Clock Speed
, which mean that the two numbers match all the time. When you change power states, Windows will change the frequency, and CurrentClockSpeed
will be changed as well.
现在,十二年前发生了什么事情,使得 CurrentClockSpeed
完全不准确/不相关?您最终可以感谢英特尔.由于采用了称为 Turbo Boost 的新技术,他们从本质上使整个理想价值浮出水面.
Now, what happened a dozen years ago to essentially make CurrentClockSpeed
completely inaccurate/irrelevant? You can ultimately thank Intel. They essentially blew this whole ideal value out of the water thanks to a new technology called Turbo Boost.
Turbo Boost与这有什么关系?
What does Turbo Boost have to do with this?
Turbo Boost在电压,电流和热包络范围内,根据处理器上的当前负载动态更改处理器频率.现在,几乎所有现代处理器都具有省电模式,并且可以根据当前的营销流行语(例如Turbo Boost(向上),Cool'N'Quiet(向下))动态更改其频率.
Turbo Boost dynamically changes the processor frequency based on the current load on the processor within the confines of voltage, current, and thermal envelopes. Almost all modern processors also now have power saving modes and can dynamically change their frequencies based on their current marketing buzzword (e.g. Turbo Boost (up), Cool'N'Quiet (down)).
关键点是:上/下/下/上/下所有这些频率都是自动完成的,而 Windows都不知道 .由于Windows对此不了解,因此 CurrentClockSpeed
值在大多数情况下可能完全不准确.实际上,Microsoft知道这一点,当您打开性能监视器时,您会在处理器性能/处理器频率
下查看说明:
The key point is: all this frequency moving up/down/off/on is all automatically done without Windows knowing about it. Because Windows doesn't know about it, the CurrentClockSpeed
value could be completely inaccurate most of the time. In fact, Microsoft knows this, and when you open your Performance Monitor, and you look at the description under Processor Performance/Processor Frequency
:
处理器频率是当前处理器的频率,以兆赫.一些处理器能够调节其频率Windows无法控制的范围.处理器频率不会准确反映这些系统上的实际处理器频率.处理器信息\处理器性能%.
Processor Frequency is the frequency of the current processor in megahertz. Some processors are capable of regulating their frequency outside of the control of Windows. Processor Frequency will not accurately reflect actual processor frequency on these systems. Use Processor Information\% Processor Performance instead.
幸运的是,此描述为我们提供了获取实际值所必须使用的提示: Processor Information \%Processor Performance
Fortunately this description gives us a hint of what we have to use to get the actual value: Processor Information\% Processor Performance
我们可以使用 获取-Counter
即可访问当前的处理器性能,如下所示:
We can use Get-Counter
to access the current Processor performance like so:
PS C:\> Get-Counter -Counter "\Processor Information(_Total)\% Processor Performance"
Timestamp CounterSamples
--------- --------------
2020-01-01 1:23:45 AM \\HAL9256\processor information(_total)\% processor performance :
153.697654229441
在这里,您可以看到我的处理器正在以153%的速度运行,也就是处理器频率的153%(是Turbo Boost!).然后,我们从 CIM_Processor
类中查询 MaxClockSpeed
(也可以使用 WMI_Processor
):
Here, you can see that my processor is running at 153% performance a.k.a. 153% of the frequency of the processor (yay for Turbo Boost!). We then query the MaxClockSpeed
from CIM_Processor
class (you can use WMI_Processor
as well):
PS C:\> (Get-CimInstance CIM_Processor).MaxClockSpeed
2592
为了计算出实际时钟速度:
$MaxClockSpeed = (Get-CimInstance CIM_Processor).MaxClockSpeed
$ProcessorPerformance = (Get-Counter -Counter "\Processor Information(_Total)\% Processor Performance").CounterSamples.CookedValue
$CurrentClockSpeed = $MaxClockSpeed*($ProcessorPerformance/100)
Write-Host "Current Processor Speed: " -ForegroundColor Yellow -NoNewLine
Write-Host $CurrentClockSpeed
如果需要每2秒运行一次,则将其包装成一个循环( Ctrl + C
停止):
Then wrapping it up in a loop if you need it to run every 2 seconds (Ctrl+C
to stop):
$MaxClockSpeed = (Get-CimInstance CIM_Processor).MaxClockSpeed
While($true){
$ProcessorPerformance = (Get-Counter -Counter "\Processor Information(_Total)\% Processor Performance").CounterSamples.CookedValue
$CurrentClockSpeed = $MaxClockSpeed*($ProcessorPerformance/100)
Write-Host "Current Processor Speed: " -ForegroundColor Yellow -NoNewLine
Write-Host $CurrentClockSpeed
Sleep -Seconds 2
}
这篇关于无法在Powershell或Python中获得当前的CPU频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!