![](/img/trans.png)
[英]What is the correct Performance Counter to get CPU and Memory Usage of a Process?
[英]Performance counter CPU usage for current process is more than 100
我想顯示我的多線程應用程序的CPU使用情況(通過多核處理器工作)。 我想收到接近任務管理器的號碼。 但是我得到的數字超過100%。 甚至超過500%。 是的,我知道,我需要將計數器“類別”的計數器“%Processor Time”划分為Environment.ProcessorCount或“ NumberOfLogicalProcessors” (與我的配置相同)。 而此操作后的結果是500%。 我在具有不同硬件(i7,i5,Core2)和軟件配置(具有所有更新的Windows 7 SP1,具有所有更新的Windows 2008 R2 SP1)的不同計算機上測試了此示例,並遇到了相同的問題。
public static class SystemInfo
{
private static Process _thisProc;
private static bool HasData = false;
private static PerformanceCounter _processTimeCounter;
private static void Init()
{
if (HasData)
return;
if (CheckForPerformanceCounterCategoryExist("Process"))
{
_processTimeCounter = new PerformanceCounter();
_processTimeCounter.CategoryName = "Process";
_processTimeCounter.CounterName = "% Processor Time";
_processTimeCounter.InstanceName = FindInstanceName("Process");
_processTimeCounter.NextValue();
}
MaximumCpuUsageForCurrentProcess = 0;
HasData = true;
}
private static bool CheckForPerformanceCounterCategoryExist(string categoryName)
{
return PerformanceCounterCategory.Exists(categoryName);
}
public static string FindInstanceName(string categoryName)
{
string result = String.Empty;
_thisProc = Process.GetCurrentProcess();
if (!ReferenceEquals(_thisProc, null))
{
if (!String.IsNullOrEmpty(categoryName))
{
if (CheckForPerformanceCounterCategoryExist(categoryName))
{
PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName);
string[] instances = category.GetInstanceNames();
string processName = _thisProc.ProcessName;
if (instances != null)
{
foreach (string instance in instances)
{
if (instance.ToLower().Equals(processName.ToLower()))
{
result = instance;
break;
}
}
}
}
}
}
return result;
}
public static int CpuUsageForCurrentProcess
{
get
{
Init();
if (!ReferenceEquals(_processTimeCounter, null))
{
int result = (int) _processTimeCounter.NextValue();
result /= Environment.ProcessorCount; //NumberOfLogicalProcessors //same for me
if (MaximumCpuUsageForCurrentProcess < result)
MaximumCpuUsageForCurrentProcess = result;
return result;
}
return 0;
}
}
public static int MaximumCpuUsageForCurrentProcess { private set; get; }
}
和要執行的代碼(您需要創建帶有兩個標簽,一個BackgroundWorker和一個按鈕的Windows窗體應用程序)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
IList<Task> tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
Task t = new Task(() =>
{
do {
if (backgroundWorker1.CancellationPending)
break;
} while (true);
});
t.Start();
tasks.Add(t);
}
Task displayProgress = new Task(() => { do {
if (backgroundWorker1.CancellationPending)
break;
backgroundWorker1.ReportProgress(1);
Thread.Sleep(10);
} while (true); });
displayProgress.Start();
tasks.Add(displayProgress);
Task.WaitAll(tasks.ToArray());
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = SystemInfo.CpuUsageForCurrentProcess.ToString();
label2.Text = SystemInfo.MaximumCpuUsageForCurrentProcess.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = SystemInfo.CpuUsageForCurrentProcess.ToString();
if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
else
backgroundWorker1.RunWorkerAsync();
}
請告訴我我的錯誤。 是的,我閱讀了這篇文章,並注意到
“ \\ Process(…)\\%Processor Time”最多可以增加N * 100(其中N是CPU數量),因為它將所有CPU上請求的進程的CPU使用率相加。
這個 (有點相關)的問題建議改為使用System.Diagnostics.Process.TotalProcessorTime和System.Diagnostics.ProcessThread.TotalProcessorTime屬性,以降低開銷並易於實現。
另外,看起來您在兩次調用“ _processTimeCounter.NextValue()”之間沒有等待足夠長的時間。 根據文檔 ,您應該等待至少1秒鍾。 不知道這是否會導致您的奇怪數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.