I want to calculate the average CPU usage % 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.