【发布时间】:2011-08-22 18:45:43
【问题描述】:
或者问题更像是“我在这里做错了什么?”
我有一个测试应用程序,它只观察自己的 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。我假设它与多核和超线程有关。我能做些什么来获得我正在寻找的实际价值? (此特定进程的总处理器时间百分比)
【问题讨论】:
标签: .net multicore performancecounter