Click here to Skip to main content
15,397,981 members
Articles / Programming Languages / C++
Posted 22 Dec 2002


122 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.60/5 (66 votes)
10 Feb 2005CPOL2 min read
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

QuestionUnable to Compile on VC12 Pin
Kshitij Mayekar20-May-18 20:59
MemberKshitij Mayekar20-May-18 20:59 
QuestionHow Can I use this with WMI? Pin
kunal301527-Jan-16 22:13
Memberkunal301527-Jan-16 22:13 
GeneralMy vote of 5 Pin
Member 103907709-Dec-14 5:53
MemberMember 103907709-Dec-14 5:53 
GeneralMy vote of 2 Pin
MrShadowGames21-Jul-12 7:02
professionalMrShadowGames21-Jul-12 7:02 
GeneralThank you very much Pin
Hans,HUANG22-May-11 22:40
MemberHans,HUANG22-May-11 22:40 
Generalnot working on vista and win7 Pin
john56323-Mar-10 23:05
Memberjohn56323-Mar-10 23:05 
GeneralSecurity context Pin
Jordi M23-Dec-08 23:34
MemberJordi M23-Dec-08 23:34 
GeneralWrong method Pin
kilt11-Dec-08 6:04
Memberkilt11-Dec-08 6:04 
GeneralThe dll was crashed when conducting Stress Test Pin
Arsenal_Lee31-Oct-08 0:06
MemberArsenal_Lee31-Oct-08 0:06 
Questioncpu usage when there are more then one core Pin
Vadim Winebrand7-Jul-08 23:13
MemberVadim Winebrand7-Jul-08 23:13 
GeneralDoesn't work on Vista Pin
Vitaly Tomilov6-Jun-08 4:45
MemberVitaly Tomilov6-Jun-08 4:45 
QuestionCan I use same code on WinPE? Pin
sam32716-Apr-08 23:05
Membersam32716-Apr-08 23:05 
QuestionRefactor ? Pin
Swinefeaster18-Mar-08 14:05
MemberSwinefeaster18-Mar-08 14:05 
QuestionExe works fine on Win 2003 server. But under ISAPI it doesn't? Pin
Sukar3-Oct-07 7:19
MemberSukar3-Oct-07 7:19 
Generaluser time, kernel time index... Pin
pulse_27-Sep-07 2:02
Memberpulse_27-Sep-07 2:02 
Question=100% Pin
ru-ikari-kun9-Aug-07 5:56
Memberru-ikari-kun9-Aug-07 5:56 
QuestionDoes this code work in Vista? Pin
Dezo3-Aug-07 9:11
MemberDezo3-Aug-07 9:11 
AnswerRe: Does this code work in Vista? Pin
Dudi Avramov7-Aug-07 22:30
MemberDudi Avramov7-Aug-07 22:30 
GeneralRe: Does this code work in Vista? Pin
Howard Anderson14-Dec-07 5:15
MemberHoward Anderson14-Dec-07 5:15 
GeneralRe: Does this code work in Vista? Pin
Swinefeaster18-Mar-08 13:41
MemberSwinefeaster18-Mar-08 13:41 
Questionhow to get the CPU usage data using Turbo C++? Pin
Khathar28-Apr-07 0:55
MemberKhathar28-Apr-07 0:55 
GeneralGcc Pin
Aenema20-Feb-07 21:46
MemberAenema20-Feb-07 21:46 
GeneralI may be retarded Pin
obyman7-Sep-06 5:43
Memberobyman7-Sep-06 5:43 
GeneralRe: I may be retarded Pin
Dudi Avramov10-Sep-06 4:02
MemberDudi Avramov10-Sep-06 4:02 
GeneralRe: I may be retarded Pin
obyman11-Sep-06 0:53
Memberobyman11-Sep-06 0:53 

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

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