Click here to Skip to main content
11,636,778 members (77,616 online)
Click here to Skip to main content

Get CPU Usage with GetSystemTimes

, 20 Dec 2004 Public Domain 226.6K 4.7K 57
Rate this:
Please Sign up or sign in to vote.
Get CPU usage with GetSystemTimes.
  • Download source code - 25.4 Kb

      Introduction

      From a very long time, a lot of people wanted to get the CPU usage from the computer.

      You've a lot of way to do this like calling registry like key or PerfCounter. But TaskManager doesn't call any of these ... If you're looking from the import table of TaskManager you can find:

      ntdll.dll
        Import Adress Table: 0x00001414
        Import Name Table: 0x00013C2C
            0x7C90E213 260 NtQueryVirtualMemory
            0x7C90DDF9 209 NtOpenThread
            0x7C90D586 103 NtClose
            ....
            0x7C90E1AA 255 NtQuerySystemInformation
            .... 
      

      So there is no other solution to have this information to dig into undocumented NtQuerySystemInformation. With this nice warning at the beginning of the article:[ NtQuerySystemInformation is available for use in Windows 2000 and Windows XP. It may be altered or unavailable in subsequent versions. Applications should use the alternate functions listed in this topic.]

      No other solution?

      Well GetSystemTimes is a good function if you have the Requirements

      Client         Requires Windows XP SP1. 
      Server         Requires Windows Server 2003. 
      Header         Declared in Winbase.h; include Windows.h.
      Library        Link to Kernel32.lib.
      DLL            Requires Kernel32.dll. 
      

      How to use it?

      Call this :

      FILETIME idleTime;
      FILETIME kernelTime;
      FILETIME userTime;
      BOOL res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
      

      and voilà you have almost what you need.

      Now you have to poll this function and make a little calculus.

      usr = userTime - last_userTime;
      ker = kernelTime - last_kernelTime;
      idl = idleTime - last_idleTime;

      System time is:

      sys = kerl + usr

      idleTime is the rest because "System Idle" process is taking 100 % of CPU.

      So CPU is :

      cpu = int( (sys - idl) *100 / sys );

      Conclusion

      It was a very long wait before Microsoft gave us this function. Now it's done but there is still one problem. For multiple processor system, you don't have the right information.

      In the sample you can find the use of GetSystemTimes and GetProcessTimes and a little class to do everything.

      class CPU
      {
      public:
        CPU( void );
        ~CPU( void );
      
        // return :
        // % of cpu usage for this process 
        // % cpu systemUsage 
        // uptime for this process
        int GetUsage( int* pSystemUsage, TKTime* pUpTime );
    • License

      This article, along with any associated source code and files, is licensed under A Public Domain dedication

      Share

      About the Author

      ejor
      Web Developer
      United Kingdom United Kingdom
      http://dev.jesover.net

      You may also be interested in...

      Comments and Discussions

       
      GeneralGood coding practices Pin
      Abbas Ibn Fernas23-Mar-10 23:46
      memberAbbas Ibn Fernas23-Mar-10 23:46 
      GeneralRe: The way it's supposed to be... Pin
      Vider20-Dec-11 1:29
      memberVider20-Dec-11 1:29 
      GeneralRe: The way it's supposed to be... Pin
      Dan Bloomquist29-May-13 16:56
      memberDan Bloomquist29-May-13 16:56 
      GeneralMessy Pin
      Vitaly Tomilov2-Sep-08 5:44
      memberVitaly Tomilov2-Sep-08 5:44 
      GeneralNice! Pin
      Vitaly Tomilov23-Aug-08 7:15
      memberVitaly Tomilov23-Aug-08 7:15 
      GeneralRe: Nice! Pin
      ejor23-Aug-08 8:27
      memberejor23-Aug-08 8:27 
      GeneralRe: Nice! Pin
      Vitaly Tomilov23-Aug-08 8:38
      memberVitaly Tomilov23-Aug-08 8:38 
      GeneralDoesn't compile... Pin
      Paul Conrad9-Dec-07 12:31
      memberPaul Conrad9-Dec-07 12:31 
      The source code does not compile. Complains of a mismatched ) in one of the header files.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer

      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon


      GeneralCurrent Process % cpu utilisation Pin
      kintz6-Jul-06 4:10
      memberkintz6-Jul-06 4:10 
      AnswerRe: Current Process % cpu utilisation Pin
      ejor29-Oct-06 10:06
      memberejor29-Oct-06 10:06 
      QuestionCode bug? Pin
      kintz5-Jul-06 23:24
      memberkintz5-Jul-06 23:24 
      AnswerRe: Code bug? Pin
      ejor6-Jul-06 1:31
      memberejor6-Jul-06 1:31 
      Generalmultiple processor Pin
      hxtking6-Apr-06 10:45
      memberhxtking6-Apr-06 10:45 
      GeneralRe: multiple processor Pin
      ejor6-Apr-06 11:02
      memberejor6-Apr-06 11:02 
      Questionhow about cygwin ? Pin
      personalcomputer24-Feb-06 23:23
      memberpersonalcomputer24-Feb-06 23:23 
      AnswerRe: how about cygwin ? Pin
      ejor25-Feb-06 23:29
      memberejor25-Feb-06 23:29 
      QuestionWhy is "System Idle" process 100% of CPU? Pin
      dmtwpi2120-Jan-06 9:59
      memberdmtwpi2120-Jan-06 9:59 
      AnswerRe: Why is "System Idle" process 100% of CPU? Pin
      ejor20-Jan-06 10:04
      memberejor20-Jan-06 10:04 
      GeneralWindows CE support Pin
      ronannaughton25-Oct-05 14:18
      memberronannaughton25-Oct-05 14:18 
      GeneralRe: Windows CE support Pin
      Shanmuga Sundar.V8-May-08 3:57
      memberShanmuga Sundar.V8-May-08 3:57 
      Generalremote computers Pin
      PeterK200325-Aug-05 9:11
      memberPeterK200325-Aug-05 9:11 
      GeneralRe: remote computers Pin
      ejor25-Aug-05 9:21
      memberejor25-Aug-05 9:21 
      Generalwhen to get last times Pin
      ferhatmantar9-Aug-05 0:00
      memberferhatmantar9-Aug-05 0:00 
      GeneralRe: when to get last times Pin
      ejor9-Aug-05 2:23
      memberejor9-Aug-05 2:23 
      QuestionGreat! do you know function like those used in your class for memory monitoring ? Pin
      mamelouk10-Jun-05 4:15
      membermamelouk10-Jun-05 4:15 

      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
      Web02 | 2.8.150728.1 | Last Updated 20 Dec 2004
      Article Copyright 2004 by ejor
      Everything else Copyright © CodeProject, 1999-2015
      Layout: fixed | fluid