CPU usage using performance counters (C, Windows 2000)

From LiteratePrograms
Jump to: navigation, search

This program is under development.
Please help to debug it. When debugging
is complete, remove the {{develop}} tag.

Minimum required version of Windows:
- Win 2000 SP0 (unconfirmed)
- WinXP SP2 (confirmed)

This code sample will output the current CPU usage data collected from the performance counters in Windows. The results are shown in percent where 100% means max usage and 0% means minimum usage.

Known bugs:
- ?
<<cpuusageperfcnt.c>>=
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <stdio.h>

//------------------------------------------------------------------------------------------------------------------
// Prototype(s)...
//------------------------------------------------------------------------------------------------------------------
int cpuusage(void);

//------------------------------------------------------------------------------------------------------------------
// getcpuload()
//   directly prints the CPU usage on screen. This function need to be called twice with a minimum of 1 seconds
//   delay (msdn guideline) to display something usefull.
//   Also returns the usage in percent 0-100 where 100 means the system is working at maximum capacity.
//   Note for multiprocessor systems:
//   If one CPU is working at max capacity the result will (if using (_total) for PdhAddCounter() ) show a maximum
//   workload of 50% unless the other CPU(s) is also working at max load. 
//------------------------------------------------------------------------------------------------------------------
INT getcpuload()
{
  static PDH_STATUS            status;
  static PDH_FMT_COUNTERVALUE  value;
  static HQUERY                query;
  static HCOUNTER              counter;
  static DWORD                 ret;
  static char                  runonce=1;
  char                         cput=0;

  if(runonce)
  {
    status = PdhOpenQuery(NULL, 0, &query);
    if(status != ERROR_SUCCESS)
    {
      printf("PdhOpenQuery() ***Error: 0x%X\n",status);
      return 0;
    }

    PdhAddCounter(query, TEXT("\\Processor(_Total)\\% Processor Time"),0,&counter); // A total of ALL CPU's in the system
    //PdhAddCounter(query, TEXT("\\Processor(0)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu0)
    //PdhAddCounter(query, TEXT("\\Processor(1)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu1)
    runonce=0;
    PdhCollectQueryData(query); // No error checking here
    return 0;
  }

  status = PdhCollectQueryData(query);
  if(status != ERROR_SUCCESS)
  {
    printf("PhdCollectQueryData() ***Error: 0x%X\n",status);
    if(status==PDH_INVALID_HANDLE) 
      printf("PDH_INVALID_HANDLE\n");
    else if(status==PDH_NO_DATA)
      printf("PDH_NO_DATA\n");
    else
      printf("Unknown error\n");
    return 0;
  }

  status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value);
  if(status != ERROR_SUCCESS)
  {
    printf("PdhGetFormattedCounterValue() ***Error: 0x%X\n",status);
    return 0;
  }
  cput = value.doubleValue;

  printf("\n\n"
         "CPU Total usage: %3d%%\n",cput);
  
  return cput;
}

//------------------------------------------------------------------------------------------------------------------
// Entry point
//------------------------------------------------------------------------------------------------------------------
int main(void)
{
  while(1)
  {
    getcpuload();
    sleep(1000);
  }
  return 0;
}
Download code
hijacker
hijacker
hijacker
hijacker