Skip to content

Commit a541840

Browse files
committed
fix(硬件监控): 优先使用聚合CPU负载传感器并修正性能计数器
优先使用 LHM 的 "Total" 聚合传感器获取 CPU 负载,使其与任务管理器行为一致。将性能计数器从 "% Processor Time" 改为 "% Processor Utility",并在超过 100% 时进行截断,以准确反映睿频下的 CPU 使用率。
1 parent 7c839e0 commit a541840

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/System/HardwareServices/HardwareValueProvider.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,19 @@ public void OnUpdateTickStarted()
248248

249249
if (result == null)
250250
{
251-
result = _componentProcessor.GetCpuLoad();
252-
if (result == null && _manualSensorCache.TryGetValue("CPU.Load", out var fallbackS))
251+
// [Fix] 优先使用 LHM 的 "Total" 聚合传感器(与任务管理器一致)
252+
// 然后才回退到各核心平均值计算
253+
if (_manualSensorCache.TryGetValue("CPU.Load", out var totalSensor) && totalSensor.Value.HasValue)
253254
{
254-
result = fallbackS.Value;
255+
result = totalSensor.Value.Value;
255256
}
257+
258+
// 如果 Total 传感器不可用,才回退到核心平均值
259+
if (result == null)
260+
{
261+
result = _componentProcessor.GetCpuLoad();
262+
}
263+
256264
if (result == null) result = 0f;
257265
}
258266
break;

src/System/HardwareServices/PerformanceCounterManager.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ public void InitializeAsync()
6262
// 注意:CategoryName 即使在中文系统通常也支持英文,为了兼容性优先用英文
6363

6464
// CPU 负载:使用 Processor Information 类别 (Win8+ 推荐),兼容性更好
65-
_cpuLoadCounter = CreateCounter("Processor Information", "% Processor Time", "_Total");
65+
// [Fix] 使用 "% Processor Utility" 而非 "% Processor Time"
66+
// 任务管理器在 Win8+ 显示的是 "Utility" (考虑睿频),而非 "Time"
67+
_cpuLoadCounter = CreateCounter("Processor Information", "% Processor Utility", "_Total");
68+
69+
if (_cpuLoadCounter == null)
70+
_cpuLoadCounter = CreateCounter("Processor Information", "% Processor Time", "_Total"); // 回退1
71+
6672
if (_cpuLoadCounter == null)
6773
_cpuLoadCounter = CreateCounter("Processor", "% Processor Time", "_Total"); // 旧系统回退
6874

@@ -175,7 +181,12 @@ private void InitStaticHardwareInfo()
175181
// 安全取值方法 (内部消化异常,失败返回 null)
176182
// ==========================================
177183

178-
public float? GetCpuLoad() => SafeRead(_cpuLoadCounter);
184+
public float? GetCpuLoad()
185+
{
186+
var val = SafeRead(_cpuLoadCounter);
187+
// [Fix] Utility 计数器在睿频时可能超过 100%,需截断以匹配任务管理器行为
188+
return val > 100f ? 100f : val;
189+
}
179190

180191
public float? GetCpuFreq()
181192
{

0 commit comments

Comments
 (0)