Click here to Skip to main content
11,409,097 members (69,094 online)
Click here to Skip to main content

How to get CPU usage by performance counters (without PDH)

, 10 Feb 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
Get CPU usage by performance counters without using PDH.dll.


The information in this article applies to Windows NT, Win2K/XP. There is no specific Win32 API that retrieves the CPU usage. An undocumented API, NtQuerySystemInformation in ntdll.dll, would help us retrieve the CPU usage. However, CPU usage can be retrieved by using performance counters. Since PDH.dll (Performance Data Helper) is not distributed with the Visual Studio, and not everyone has this file, I decided to do it without the help of PDH.dll.

The CPU usage counter is of type PERF_100NSEC_TIMER_INV which has the following calculation:

  X - CounterData
  Y - 100NsTime
  Time base - 100Ns

where the denominator (Y) represents the total elapsed time of the sample interval and the numerator (X) represents the time during the interval when the monitored components were inactive.

My CCpuUsage class has a method called GetCpuUsage which runs through the performance objects and counters and retrieves the CPU usage. Since the CPU usage can be determined by two samplings, the first call to GetCpuUsage() returns 0, and all calls thereafter returns the CPU usage.


On Windows NT, CPU usage counter is '% Total processor time' whose index is 240 under 'System' object whose index is 2. However, in Win2K/XP, Microsoft moved that counter to '% processor time' whose index is 6 under '_Total' instance of 'Processor' object whose index is 238. Read 'INFO: Percent Total Performance Counter Changes on Windows 2000' (Q259390) in MSDN.

There is no difference between WinNT and Win2K/XP in the performance counters for getting CPU usage for a specific process. The counter '% processor time' whose index is 6 under the object 'Process' whose index is 230.

The Sample

#include <span class="code-string">"CpuUsage.h"</span>

int main(int argc, char* argv[])
    int processID=0;
    CCpuUsage usageA;
    CCpuUsage usageB;
    CCpuUsage usageC;

    printf("SystemWide Cpu Usage     " 
           "Explorer cpu usage       " 
           "Cpu Usage for processID 0\n"); 
    printf("====================     " 
           "==================       "
    while (true)
        // Display the system-wide cpu usage and the "Explorer" cpu usage

        int SystemWideCpuUsage = usageA.GetCpuUsage();
        int ProcessCpuUsageByName = usageB.GetCpuUsage("explorer");
        int ProcessCpuUsageByID = usageC.GetCpuUsage(processID);
               ProcessCpuUsageByName, ProcessCpuUsageByID);

    return 0;


  • 6-May-2003 - Fixed bug when looking for an instance. The bug occurred because the instances in the performance counters are in Unicode. Added a sample function to get CPU usage for a specific process.
  • 12-Jun-2003 - Fixed bug in realloc method.
  • 22-Jun-2003 - Defined structure alignment to be 8 (by using #pragma directive). Other sizes cause the function to return 100% CPU usage. (Thanks to Scolver tip.)
  • 12-Feb-2004 - Enabled performance counters of perfOS.dll (which holds processor counters) automatically for Win2K/XP.
  • 20-May-2004 - Retrieving only the specified counter data (instead of retrieving all counters and iterating till finding the specified object) (for more information, refer to OscarTV comments).
  • 03-Jun-2004 - Enabled performance counters of perfProc.dll (which holds process counters) automatically for Win2K/XP.
  • 06-Jun-2004 - Wrapped the code into a class in order to enable getting CPU usage for different processes at the same iteration.
  • 18-Nov-2004 - Extended variable from 8 bytes to 32 bytes.
  • 03-Feb-2005 - Enabled getting performance counters by process ID.
  • 09-Feb-2005 - Returns 0 when the specified process-ID doesn't exist.

More of my articles using performance counters


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dudi Avramov
Web Developer
Israel Israel
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberMember 103907709-Dec-14 6:53 
GeneralMy vote of 2 PinmemberMrShadowGames21-Jul-12 8:02 
GeneralThank you very much PinmemberHans,HUANG22-May-11 23:40 
Generalnot working on vista and win7 Pinmemberjohn56324-Mar-10 0:05 
GeneralSecurity context PinmemberJordi M24-Dec-08 0:34 
GeneralWrong method Pinmemberkilt11-Dec-08 7:04 
GeneralThe dll was crashed when conducting Stress Test PinmemberArsenal_Lee31-Oct-08 1:06 
Questioncpu usage when there are more then one core PinmemberVadim Winebrand8-Jul-08 0:13 
GeneralDoesn't work on Vista PinmemberVitalyTomilov6-Jun-08 5:45 
QuestionCan I use same code on WinPE? Pinmembersam32717-Apr-08 0:05 
QuestionRefactor ? PinmemberSwinefeaster18-Mar-08 15:05 
QuestionExe works fine on Win 2003 server. But under ISAPI it doesn't? PinmemberSukar3-Oct-07 8:19 
Generaluser time, kernel time index... Pinmemberpulse_27-Sep-07 3:02 
Question=100% Pinmemberru-ikari-kun9-Aug-07 6:56 
QuestionDoes this code work in Vista? PinmemberDezo3-Aug-07 10:11 
AnswerRe: Does this code work in Vista? PinmemberDudi Avramov7-Aug-07 23:30 
GeneralRe: Does this code work in Vista? PinmemberHoward Anderson14-Dec-07 6:15 
GeneralRe: Does this code work in Vista? PinmemberSwinefeaster18-Mar-08 14:41 
Questionhow to get the CPU usage data using Turbo C++? PinmemberKhathar28-Apr-07 1:55 
GeneralGcc PinmemberAenema20-Feb-07 22:46 
GeneralI may be retarded Pinmemberobyman7-Sep-06 6:43 
GeneralRe: I may be retarded PinmemberDudi Avramov10-Sep-06 5:02 
GeneralRe: I may be retarded Pinmemberobyman11-Sep-06 1:53 
GeneralRe: I may be retarded Pinmemberru-ikari-kun14-Aug-07 1:16 
GeneralIt won't compile - PinmemberRaven Caelladore2-Jul-06 11:39 
GeneralRe: It won't compile - PinmemberRaven Caelladore6-Jul-06 4:23 
GeneralRe: It won't compile - Pinmembermadhav_sigdel13-Oct-07 0:45 
General[problem] only return 0 in thread Pinmemberopenkwang1-May-06 23:43 
GeneralReally Like the Code But Problem with % > 100 Pinmemberchris17510-Apr-06 4:03 
GeneralRe: Really Like the Code But Problem with % > 100 PinmemberDudi Avramov10-Apr-06 21:38 
GeneralRe: Really Like the Code But Problem with % > 100 PinmemberDaver20823-May-07 8:51 
GeneralMultiple Processors PinmemberBiShopx31-Dec-05 13:07 
GeneralRe: Multiple Processors Pinmembercpp_prgmer11-Sep-06 6:05 
Questiongetting disk throughput with CPerfCounters? PinmemberT1TAN12-Dec-05 23:53 
AnswerRe: getting disk throughput with CPerfCounters? PinmemberDudi Avramov13-Dec-05 6:44 
GeneralRe: getting disk throughput with CPerfCounters? Pinmembersupergoofy13-Feb-06 0:43 
GeneralRe: getting disk throughput with CPerfCounters? PinmemberLexaPublic16-Jan-07 0:59 
QuestionError compiling the source code Pinmembermpbejo29-Oct-05 1:20 
AnswerRe: Error compiling the source code PinmemberDudi Avramov29-Oct-05 23:41 
AnswerRe: Error compiling the source code PinmemberArtur_ABC29-Jun-06 5:55 
GeneralWindows CE support Pinmemberronannaughton25-Oct-05 16:50 
GeneralRe: Windows CE support Pinmemberchiru_tati29-May-08 23:32 
Generalas he would be indice of: Pinmemberhormigatroy5-Sep-05 5:26 
GeneralWin 98/ME Pinmemberjohnny2pints15-Jul-05 8:58 
GeneralRe: Win 98/ME PinmemberDudi Avramov17-Jul-05 2:09 
GeneralVery Useful Work PinmemberPescador17-Jun-05 13:25 
GeneralRe: Very Useful Work PinmemberPescador20-Jun-05 7:23 
GeneralRe: Very Useful Work PinmemberDudi Avramov27-Jun-05 2:19 
GeneralModifying System Counter Values PinmemberRenecito24-May-05 11:38 
GeneralRe: Modifying System Counter Values PinmemberDudi Avramov28-May-05 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 10 Feb 2005
Article Copyright 2002 by Dudi Avramov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid