Click here to Skip to main content
11,716,249 members (86,205 online)
Click here to Skip to main content

Getting CPU Usage in a Multiprocessor Machine

, 30 May 2005 Ms-PL 127.6K 2.7K 36
Rate this:
Please Sign up or sign in to vote.
Getting CPU usage in a multiprocessor machine.

Introduction

Ever since I saw 'Performance' application on Windows 2000, I wondered if there is a way / Windows API to programmatically determine CPU usage for each of the individual CPUs in a multi-processor machine. Though there are many articles for getting the CPU usage, none helped when it came to multi-processor machines.

After some digging in MSDN, I could find WMI (Windows Management and Instrumentation) has a few performance counter classes. I chose to use the Win32_PerfRawData_PerfOS_Processor class in this case as it is supported on Windows 2000 onwards.

How to use it?

Follow these seven steps:

Step 1: Initialize COM:

CoInitializeEx(0, COINIT_MULTITHREADED);

Step 2: Set COM security levels:

CoInitializeSecurity(
        NULL, 
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );

Step 3: Obtain the initial locator to WMI:

IWbemLocator *pLoc = NULL;

hres = CoCreateInstance(
     CLSID_WbemLocator,
     0,
     CLSCTX_INPROC_SERVER,
     IID_IWbemLocator, (LPVOID *) &pLoc);

Step 4: Connect to WMI through the IWbemLocator::ConnectServer method:

    IWbemServices *pSvc = NULL;
 
    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );

Step 5: Set security levels on the proxy:

CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

Step 6: Use the IWbemServices pointer to make requests of WMI:

IEnumWbemClassObject* pEnumerator = NULL;
HRESULT hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM Win32_PerfRawData_PerfOS_Processor"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);

Step 7: Get data from the query in step 6.

IWbemClassObject *pclsObj;
while (1)
{
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

    if(0 == uReturn)
    {
        break;
    }

      VARIANT vtProp;
      VariantInit(&vtProp);

      hr = pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0);

      // Use it

       hr = pclsObj->Get(L"TimeStamp_Sys100NS", 0, &vtProp, 0, 0);

      // Use it
}

Note

I have tried to keep the source code in the attached project simple. So I have taken the number of processors as the input though you may like to programmatically get the number of processors using GetSystemInfo Win32 API.

I have tested it on Microsoft Windows 2000 Professional OS running on a dual processor machine. It should work on Windows XP/ Windows 2003 Server as well.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

BimalV
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralNot Practical Pin
VitalyTomilov6-Jun-08 7:38
memberVitalyTomilov6-Jun-08 7:38 
Questionerror C2061:syntax error : identifier 'DWORD_PTR' Pin
thaithiensu12-Feb-08 20:53
memberthaithiensu12-Feb-08 20:53 
GeneralC# Code Available Pin
Adam L. Stevenson30-Dec-07 5:25
memberAdam L. Stevenson30-Dec-07 5:25 
GeneralRe: C# Code Available Pin
Member 296291322-Jul-08 6:12
memberMember 296291322-Jul-08 6:12 
GeneralRe: C# Code Available Pin
Manish K. Agarwal29-Mar-10 21:01
memberManish K. Agarwal29-Mar-10 21:01 
GeneralRe: C# Code Available Pin
lalit.k.bhatia21-Sep-14 17:31
memberlalit.k.bhatia21-Sep-14 17:31 
GeneralDevision By Zero Pin
_pmneo_18-Nov-06 7:48
member_pmneo_18-Nov-06 7:48 
GeneralRe: Devision By Zero Pin
_pmneo_18-Nov-06 13:17
member_pmneo_18-Nov-06 13:17 
GeneralRe: Devision By Zero Pin
Finix21-Mar-07 18:16
memberFinix21-Mar-07 18:16 
GeneralRe: Devision By Zero Pin
BimalV11-Jul-07 0:44
memberBimalV11-Jul-07 0:44 
GeneralRe: Devision By Zero Pin
bren1239-Aug-07 7:43
memberbren1239-Aug-07 7:43 
GeneralRe: Devision By Zero Pin
horse199721-Nov-07 16:17
memberhorse199721-Nov-07 16:17 
GeneralRe: Devision By Zero Pin
Member 768255519-Feb-11 5:27
memberMember 768255519-Feb-11 5:27 
QuestionAccess PoolNonPagedBytes Pin
suresh_suryavanshi2-Nov-06 3:14
membersuresh_suryavanshi2-Nov-06 3:14 
QuestionPoolNonPagedBytes Pin
suresh_suryavanshi2-Nov-06 3:12
membersuresh_suryavanshi2-Nov-06 3:12 
QuestionRegarding accessing the PoolNonPagedBytes Pin
suresh_suryavanshi2-Nov-06 3:12
membersuresh_suryavanshi2-Nov-06 3:12 
QuestionPoolNonPagedBytes for only one process Pin
suresh_suryavanshi2-Nov-06 3:11
membersuresh_suryavanshi2-Nov-06 3:11 
QuestionAccess PoolNonPagedBytes for only one process Pin
suresh_suryavanshi2-Nov-06 3:11
membersuresh_suryavanshi2-Nov-06 3:11 
QuestionPoolNonPagedBytes for only one process from Win32_PerfFormattedData_PerfProc_Process Pin
suresh_suryavanshi2-Nov-06 3:10
membersuresh_suryavanshi2-Nov-06 3:10 
QuestionAccess the PoolNonPagedBytes for only one process from Win32_PerfFormattedData_PerfProc_Process Pin
suresh_suryavanshi2-Nov-06 3:10
membersuresh_suryavanshi2-Nov-06 3:10 
QuestionGetting the PoolNonPagedBytes for only one process from Win32_PerfFormattedData_PerfProc_Process Pin
suresh_suryavanshi2-Nov-06 3:09
membersuresh_suryavanshi2-Nov-06 3:09 
QuestionHow to access the PoolNonPagedBytes for only one process from Win32_PerfFormattedData_PerfProc_Process Pin
suresh_suryavanshi2-Nov-06 3:09
membersuresh_suryavanshi2-Nov-06 3:09 
GeneralWin2K3 Pin
isdi10-Mar-06 12:58
memberisdi10-Mar-06 12:58 
Generalgetting CPU Usage of each CPU for a process running on multi CPUs system. Pin
lvantin9-Feb-06 20:57
memberlvantin9-Feb-06 20:57 
GeneralRe: getting CPU Usage of each CPU for a process running on multi CPUs system. Pin
BimalV11-Jul-07 0:38
memberBimalV11-Jul-07 0:38 
Generaluse __int64 Pin
z161668-Feb-06 21:36
memberz161668-Feb-06 21:36 
QuestionWrong???? Pin
Tcpip20056-Sep-05 16:58
memberTcpip20056-Sep-05 16:58 
AnswerRe: Wrong???? Pin
Tcpip20056-Sep-05 17:11
memberTcpip20056-Sep-05 17:11 
AnswerRe: Wrong???? Pin
CodeFlatter3-Jul-07 23:06
memberCodeFlatter3-Jul-07 23:06 
Generalmemory increase, solution Pin
Salamon14-Jun-05 9:21
memberSalamon14-Jun-05 9:21 
GeneralThanks ! Pin
Marcello31-May-05 6:14
memberMarcello31-May-05 6:14 

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
Web04 | 2.8.150901.1 | Last Updated 31 May 2005
Article Copyright 2005 by BimalV
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid