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

Article translations Article translations
Article ID: 262938 - View products that this article applies to.
This article was previously published under Q262938
Expand all | Collapse all

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.
Collapse this tableExpand this table
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: November 21, 2006 - Revision: 4.2
APPLIES TO
  • Microsoft Win32 Application Programming Interface, when used with:
    • Microsoft Windows NT 4.0
    • Microsoft Windows 2000 Standard Edition
    • the operating system: Microsoft Windows XP
Keywords: 
kbhowto kbapi kbkernbase kbperfmon KB262938

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com