簡體   English   中英

使用性能計數器的任務管理器中的 CPU 使用率

[英]CPU Usage in Task Manager using Performance Counters

[我的嘗試]

已經通過

  1. 如何在 C# 中獲取 CPU 使用率? 但是“_Total”處理器實例會給我 CPU 的總消耗,而不是特定的應用程序或“進程”

  2. 在 C# 程序中,我試圖獲取應用程序的 CPU 使用率百分比,但它始終顯示 100

  3. 任務管理器中的 CPU 時間到底是什么? , 解釋了它,但沒有說明如何檢索這個值。

參考后http://social.technet.microsoft.com/wiki/contents/articles/12984.understanding-processor-processor-time-and-process-processor-time.aspx

我明白了

TotalProcessorTimeCounter = new PerformanceCounter("Process", "% Processor Time", processName);

有一個基線 (No.of Logical CPU*100) 基本上,這並沒有給我一個超過 CPU 消耗的 100% 的比例。

嘗試挖掘任務管理器,發現任務管理器->處理器-> CPU使用率在100的范圍內。

Processor\\% Processor Time 對象不將進程名稱作為輸入。 它只有“_Total”作為輸入。

[題]

對於多核系統的特定進程,如何使用超過 100 的性能計數器獲取此數據(CPU 消耗)?

閱讀此性能計數器文檔https://social.technet.microsoft.com/wiki/contents/articles/12984.understanding-processor-processor-time-and-process-processor-time.aspx 后,我意識到正確的方法得到這個值其實就是取100減去所有處理器的空閑時間。

% Processor Time 是處理器執行非空閑線程所花費的時間百分比。

var allIdle = new PerformanceCounter(
    "Processor", 
    "% Idle Time", 
    "_Total"
);

int cpu = 100 - allIdle;

這給出了一個與任務管理器顯示的非常接近的值,並且可能只是由於舍入或輪詢計數器的特定時間而在某些點上有所不同。

如果您使用它,您可以使用任務管理器獲得相同的結果:

cpuCounter = new PerformanceCounter(
        "Processor Information",
        "% Processor Utility",
        "_Total",
        true
    );

這為我提供了您在任務管理器(在Details選項卡中)獲得的確切數字,這是您想要的嗎?

// Declare the counter somewhere
var process_cpu = new PerformanceCounter(
                                   "Process", 
                                   "% Processor Time", 
                                   Process.GetCurrentProcess().ProcessName
                                        );
// Read periodically
var processUsage = process_cpu.NextValue() / Environment.ProcessorCount;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM