This project has moved and is read-only. For the latest updates, please go here.

How does one get '% of CPU per Process" out of the KernelTraceEventParser?

Oct 16, 2010 at 4:01 PM

I was looking through the code for this class and I can't seem to find where it handles this data. Any advice on how to pull this out of an etl file using this class?

Oct 18, 2010 at 4:30 PM

The ETL file does not contain %CPU directly.   It CAN contain two things that can be used to compute it

  1. If you turned on sample based profiling, you get an event every 1 msec, of where the CPU time is.   If you assume that that entire 1msec is in the process that logged the event, you can get CPU time.  Dividing this by the real time over which you did the sum gives you % CPU time.  For reasoanbly long time interlas (>> 100msec) this is typically fine.   
  2. The mechanism in (1) can be made more accurate by using the context switch events.  Each of these events indicate when a processor switchs from one thread (and process) to another).   Using these you can find intervals (to 100nsec accuracy), of CPU time consumed in a process, and again divide by real time.

The PerfMonitor tool uses the technique (1) to compute % CPU (to 10% accuracy). 





Oct 19, 2010 at 1:41 AM

Thanks, can you shed light on one more thing. What is the difference between 'CPU Sampling by Process" and 'CPU Usage by Process", both are available in xperf viewer when looking at the kernel profiling. I'm guessing that 'CPU Usage by Process" is what you described above, but what about 'CPU Sampling by Process'? They both track very closely, bu the 'CPU Sampling by Process' appears less fine grained.

Also, in the Windows Performance Analyzer documentation, the function StartKernelTrace takes an argument of EVENT_TRACE_PROPERTIES, one of the fields of which is ClientContext, which seems to have 3 possible values. They are defined here:

Thing is, it doesn't quite match what you've described above. It says one can obtain 100ns, 10ms, and Cpu Cycle based granularity, which it claims is the most granular and cheapest to obtain, but fraught with hardware errors. How do you square this with you described above, a default of 1ms.

I am finding the various documentation to be very confusing and lacking a central authority. Any further help you could provide would be greatly appreciated.



May 31, 2011 at 5:43 PM

This is months late, but for sake of others, here is some information

XPERF's 'CPU Usage By Process' uses the context switch events (CSWITCH) to determine how much processor time a given thread (and process), used.   Like all ETW events, typically they set th 'ClientContext' so that high resolution timestamps are used (XPERF does this), and thus this can be very accurate , but requires that you turn on CSWITCH events (which are expensive).    Sample based profiling on the other hand simply checks what every processor does every 1 msec, and thus can not have resolutions better than that.