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

时间:2016-02-19 15:27:55

标签: c#

我尝试在具有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使用率。

1 个答案:

答案 0 :(得分:1)

结果不正确。当您的流程使用多个CPU并且各个值加起来“超过”100%时,NextValue()将返回大于100的值。

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

如果你想要一个值将处理器的数量变为acount(即在双核机器上映射11055),请尝试将检索的值除以处理器的数量:

_cpuCounter.NextValue() / Environment.ProcessorCount