C# 测量多个处理器 Windows 服务器上的进程 CPU 使用率



我试图在具有24个处理器的Windows服务器上监视Windows服务CPU使用情况。我用了

    _cpuCounter = new PerformanceCounter(
                        "Process", "% Processor Time", Process.GetCurrentProcess().ProcessName, true);
    _cpuCounter.NextValue()/100 //most time, it is more than 100%, which is not correct.

但它没有给我正确的进程 CPU 使用率。

结果没有错。只是当您的进程使用多个 CPU 并且各个值加起来"超过"100% 时,NextValue()将返回大于 100% 的值。

例如,如果进程使用 50% 的 CPU0 和 60% 的 CPU1,则结果将是所有 CPU/内核上所有潜在可用 CPU 周期的使用率为 110%(双核处理器表示为 200%)。

如果您想要一个将处理器数计入计数的值(即将110映射到双核计算机上的55),请尝试将检索到的值除以处理器数:

_cpuCounter.NextValue() / Environment.ProcessorCount 

最新更新