5
或者這個問題更像是「我在做什麼錯公然在這裏?」上如何獲得準確的CPU使用率的過程的多處理器/多核系統
我有一個測試應用程序,它什麼都不做,但看它自己的CPU使用率。它看起來有點像這樣:
protected PerformanceTrace()
{
Process currentProcess = Process.GetCurrentProcess();
this.cpuCounter = new PerformanceCounter("Process", "% Processor Time", currentProcess.ProcessName);
this.coreCount = Environment.ProcessorCount;
}
private int coreCount;
private DateTime lastCpuRead = DateTime.MinValue;
private float _cpuUsage;
private float CpuUsage
{
get
{
if ((DateTime.Now - this.lastCpuRead) > TimeSpan.FromSeconds(1.0))
{
this._cpuUsage = this.cpuCounter.NextValue();
}
return this._cpuUsage/this.coreCount;
}
}
CpuUsage屬性被讀取得非常頻繁。這裏的東西:
在我的機器上,Environment.ProcessorCount
產生的值爲2.但是,來自計數器的值通常高達800.我假設它與多核和超線程有關。我能做些什麼來獲得我正在尋找的實際價值? (總%的處理器時間,這個特殊的過程)
這就是我的想法。現在我該如何確定每個CPU的線程數? – MojoFilter 2011-05-10 20:21:58
或者核心的數量,而是。 – MojoFilter 2011-05-10 20:30:41
@MojoFilter - 請參閱編輯我的答案 – 2011-05-10 20:31:35