开发者

Process.TotalProcessorTime exceeds the actual time passing

I want to calculate the average CPU usage %开发者_如何学Go between two points of time. I use the ratio between t1-t0 and Process.TotalProcessorTime1 - Process.TotalProcessorTime0 (where t is the actual DateTime.Now at that point)

but sometimes when the computer is busy I get the TotalProcessorTime difference in Ticks is larger than the actual time (in Ticks) that passed, so my cpu% exceeds 100.

how can that be?

long currentLogTime = DateTime.Now.Ticks;
long currentSPUUsageTime = _process.TotalProcessorTime.Ticks;

long timeDiff= currentLogTime - m_LastLogTime;
if (timeDiff != 0)
{
    cpuUsage = (currentSPUUsageTime - m_LastCPUUsageTime) * 100 / timeDiff;
}


If a single process uses more than one processor, it can use processor time in faster than real time.


DateTime.Now is measured with a lower resolution than TotalProcessorTime.

You'll need to use a high-resolution timer to measure the elapsed time. Consider using a Stopwatch instance for this purpose.

Use the StartNew method when you start a new "log period":

        m_stopwatch = Stopwatch.StartNew();

Then simply read the ElapsedTicks property to determine how much time that has elapsed:

        long timeDiff= m_stopwatch.ElapsedTicks;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜