I am working on a large application and want to get an idea of how loaded the CPU was between two points in time. I am not just interested in my own process, but all processes.
My idea is to get the Total CPU Time of the Idle Process between two time periods. Then compare that value to the real time difference. It is then possible to calculate the average % CPU Time spent on the Idle process, and hence the average % CPU spent on actual processes.
For instance:
- Real Time Samples: 2012/07/30 13:56:1 and 2012/07/30 13:56:21
- Idle Process Total CPU Time Samples: 56:23:10 and 56:23:15
- Difference in Real Time: 20s
- Difference in Idle Process Total CPU Time: 5s
- Average % CPU Time in Idle Process: 25%
- Average % CPU Time in Actual Processes: 75%
In order to have as little performance impact as possible, I only want to take two samples. One at the beginning and one at the end.
I also do not want to look at other processes in order to calculate the total CPU load, only the Idle Process. (For instance I don’t want to calculate the sum of Total CPU Time of other processes)
I have tried to use the Kernel32.dll OpenProcess. But apperantly you cannot access the Idle Process. It throws an ERROR_ACCESS_DENIED exception.
This must be possible as Task Manager displays the Total CPU Time of the Idle Process.
NOTE: I am NOT interested in the Instantaneous % CPU Time of the Processes. Only the Total CPU Time in order to calculate the average later.
You are probably looking for something like this:
totalCpuUsagePercentage will then contain the total load in percentage since the last cpuCounter.NextValue().
If you don’t want the percentage you can use CounterName = “Processor Time” instead.
Hope that this will help you on your quest.