You are currently offline, waiting for your internet to reconnect

How To Get % Processor Time Correctly By Using the Performance Data Helper (PDH) API

This article was previously published under Q262938
SUMMARY
Performance Data Helper (PDH) functions can be used to retrieve performance data. This article explains how an application can obtain the % (percent) Processor Time correctly, based on the underlying calculation performed by the PDH library.
MORE INFORMATION
The % Processor Time performance counter is the percentage of time that the processor is running a non-Idle thread. This counter is designed as a primary indicator of processor activity. It is calculated by measuring the time that the processor spends running the thread of the Idle process in each sample interval, and subtracting that value from 100 percent. Each processor has an Idle thread, which consumes cycles when no other threads are ready to run. It can be viewed as the fraction of the time that is spent doing useful work. The calculation for this performance counter is performed as follows.
Counter Type: PERF_100NSEC_TIMER_INV

PERF_100NSEC_TIMER_INV - The inverse of the timer. Used when the object is not in use.
ElementValue
XCounterData
Y100NsTime
Data Size8 Bytes
Display Suffix%
Time base100Ns
Calculation100*(1-(X1-X0)/(Y1-Y0))


When the application calls the PdhCollectQueryData PDH function the first time, the PDH library collects X0 (the total time taken by the processor that is running the thread of the idle process) and Y0 (the time measured in Time base units). The second time the application calls PdhCollectQueryData, the previous sample becomes X0 and Y0, and the current call becomes X1 and Y1.

When the application calls the PdhGetFormattedCounterValue PDH function, the calculation described in the previous paragraph is performed to calculate the % Processor Time. This calculation relies on a previous sample, which is not present if PdhCollectQueryData is called only once (there is no X0 and Y0). Therefore, if any performance counter requires two sample collections, PdhGetFormattedCounterValue can be called only after the second PdhCollectQueryData is called.

Furthermore, when retrieving the % Processor Time, there must be a delay between sample collections, because just running the code will have a slight impact on the calculation of % Processor Time. The application can delay by calling Sleep(few 100 milliseconds) in between PdhCollectQueryData function calls.
Properties

Article ID: 262938 - Last Review: 11/21/2006 15:37:00 - Revision: 4.2

Microsoft Win32 Application Programming Interface

  • kbhowto kbapi kbkernbase kbperfmon KB262938
Feedback
m=document.createElement('meta');m.name='ms.dqp0';m.content='true';document.getElementsByTagName('head')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> " class="ng-binding" id="language-es-py">Paraguay - Español
Venezuela - Español
/html>microsoft.com/c.gif?DI=4050&did=1&t="> var varClickTracking = 1; var varCustomerTracking = 1; var Route = "76500"; var Ctrl = ""; document.write(" r m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> >>