CPU usage (C, Windows XP)

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:
- WinXP SP1

This code sample will output the current CPU usage in percent where 100% means max usage.

Known bugs: None

<<cpuusage.c>>=
#include <windows.h>
#include <stdio.h>

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

//----------------------------------------------------------------------------------------------------------------
// cpuusage(void)
// ==============
// Return a CHAR value in the range 0 - 100 representing actual CPU usage in percent.
//----------------------------------------------------------------------------------------------------------------

CHAR cpuusage()
{
  FILETIME               ft_sys_idle;
  FILETIME               ft_sys_kernel;
  FILETIME               ft_sys_user;

  ULARGE_INTEGER         ul_sys_idle;
  ULARGE_INTEGER         ul_sys_kernel;
  ULARGE_INTEGER         ul_sys_user;

  static ULARGE_INTEGER	 ul_sys_idle_old;
  static ULARGE_INTEGER  ul_sys_kernel_old;
  static ULARGE_INTEGER  ul_sys_user_old;

  CHAR  usage = 0;

  GetSystemTimes(&ft_sys_idle,    /* System idle time */
                 &ft_sys_kernel,  /* system kernel time */
                 &ft_sys_user);   /* System user time */

  CopyMemory(&ul_sys_idle  , &ft_sys_idle  , sizeof(FILETIME)); // Could been optimized away...
  CopyMemory(&ul_sys_kernel, &ft_sys_kernel, sizeof(FILETIME)); // Could been optimized away...
  CopyMemory(&ul_sys_user  , &ft_sys_user  , sizeof(FILETIME)); // Could been optimized away...

  usage  =
          (
           (
            (
             (
              (ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
              (ul_sys_user.QuadPart   - ul_sys_user_old.QuadPart)
             )
             -
             (ul_sys_idle.QuadPart-ul_sys_idle_old.QuadPart)
            )
            *
            (100)
           )
           /
           (
            (ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
            (ul_sys_user.QuadPart   - ul_sys_user_old.QuadPart)
           )
          );

  ul_sys_idle_old.QuadPart   = ul_sys_idle.QuadPart;
  ul_sys_user_old.QuadPart   = ul_sys_user.QuadPart;
  ul_sys_kernel_old.QuadPart = ul_sys_kernel.QuadPart;

  return usage;
}
//------------------------------------------------------------------------------------------------------------------
// Entry point
//------------------------------------------------------------------------------------------------------------------
int main(void)
{
  int n;
  for(n=0;n<20;n++)
  {
    printf("CPU Usage: %3d%%\r",cpuusage());
    sleep(500);
  }
  printf("\n");
  return 0;
}

Download code
hijacker
hijacker
hijacker
hijacker