无法在 Powershell 或 Python 中获取当前 CPU 频率

Tom*_*Tom 4 windows cpu powershell frequency

我试图以某种方式以编程方式记录我的 Windows 10 机器的 CPU 频率。但是,我显然无法获得任务管理器中显示的当前频率。

在 Powershell 中,使用

get-wmiobject Win32_Processor -Property CurrentClockSpeed
Run Code Online (Sandbox Code Playgroud)

只返回一个恰好是最大时钟速度的时钟速度(即使我可以在任务管理器中看到它没有运行那么高)

我什至尝试过这个解决方案:https : //www.remkoweijnen.nl/blog/2014/07/18/get-actual-cpu-clock-speed-powershell/ 但它没有给我任何东西,但静态值 = 最大值.

甚至 python 的 psutil 也只返回一个静态值。

有谁知道如何解决这个问题,实际上每 x 秒记录一次 CPU 频率?

任何帮助将不胜感激,谢谢!

HAL*_*256 5

TLDR:要查找当前处理器频率,您必须使用% Processor Performance性能计数器:

$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
Run Code Online (Sandbox Code Playgroud)

关于为什么查询 WMI Win32_ProcessorforCurrentClockSpeed似乎总是返回最大频率而不是实际的“当前时钟速度”的更深入的解释?事实上,为什么所有的几十个 WMI/CMI/Perfmon 计数器似乎都返回“错误”的频率?如果 CPU-Z 和任务管理器都可以得到,我们要怎么做才能得到“实际”频率?要回答这个问题,我们需要了解什么CurrentClockSpeed真正返回的。

来自Win32_Processor 的 WMI 文档CurrentClockSpeed

处理器的当前速度,以 MHz 为单位。该值来自 SMBIOS 信息中处理器信息结构的当前速度成员。

伟大的!有人会认为这个简单的查询应该让我们知道当前的频率。这在十几年前很有效,但现在不行了;因为它实际上只适用于两种非常具体的情况:

  1. 当您的处理器仅以其定义的库存速度运行时。
  2. 当 Windows 要求移动处理器以不同的速度运行时(例如切换到电池模式)。

在启动时,Widows 获取处理器信息并获取当前时钟速度。大多数人都在推荐的设置下运行他们的处理器Current Clock Speed == Max Clock Speed,这意味着这两个数字始终匹配。当您更改电源状态时,Windows 将更改频率,并且CurrentClockSpeed也会更改。

现在,十几年前发生了什么基本上使CurrentClockSpeed完全不准确/不相关?您最终可以感谢英特尔。由于一项名为Turbo Boost的新技术,他们基本上将整个理想值从水中吹了出来

Turbo Boost 与此有什么关系?

Turbo Boost 根据处理器上的当前负载在电压、电流和热包络范围内动态更改处理器频率。几乎所有现代处理器现在也具有省电模式,并且可以根据当前的营销流行语(例如 Turbo Boost(向上)、Cool'N'Quiet(向下))动态更改其频率。

关键是:所有这些向上/向下/关闭/打开的频率都是自动完成的,Windows 不知道。因为 Windows 不知道它,所以CurrentClockSpeed大多数时候该值可能完全不准确。事实上,微软知道这一点,当你打开你的性能监视器,你看看下面的描述Processor Performance/Processor Frequency

Processor Frequency 是当前处理器的频率,以兆赫为单位。某些处理器能够在 Windows 控制之外调节其频率。处理器频率不会 准确反映这些系统上的实际处理器频率。请改用处理器信息\% 处理器性能。

幸运的是,这个描述给了我们一个提示,我们必须使用什么来获取实际值:Processor Information\% Processor Performance

我们可以Get-Counter像这样访问当前的处理器性能:

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
Run Code Online (Sandbox Code Playgroud)

在这里,您可以看到我的处理器以 153% 的性能运行,也就是处理器频率的 153%(对于 Turbo Boost 来说是的!)。然后我们查询MaxClockSpeedfromCIM_Processor类(您也可以使用WMI_Processor):

PS C:\> (Get-CimInstance CIM_Processor).MaxClockSpeed
2592
Run Code Online (Sandbox Code Playgroud)

为了计算出实际时钟速度:

$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
Run Code Online (Sandbox Code Playgroud)

如果您需要它每 2 秒运行一次(Ctrl+C停止),则将其包装在一个循环中:

$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
}
Run Code Online (Sandbox Code Playgroud)