Click here to Skip to main content
Click here to Skip to main content

Get CPU Usage with GetSystemTimes

, 20 Dec 2004 Public Domain
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

      Comments and Discussions

       
      Questionhow to compile? PinmemberMojtaba Setoodeh10-Sep-13 1:10 
      AnswerRe: how to compile? Pinmemberejor10-Sep-13 2:53 
      GeneralRe: how to compile? PinmemberMojtaba Setoodeh10-Sep-13 19:53 
      GeneralRe: how to compile? Pinmemberejor10-Sep-13 20:49 
      GeneralRe: how to compile? PinmemberMojtaba Setoodeh11-Sep-13 0:19 
      QuestionWhen I use this function, I encountered an error that this function is undeclared, do you know why? Pinmemberlove_fairytale13-Mar-12 23:12 
      AnswerRe: When I use this function, I encountered an error that this function is undeclared, do you know why? Pinmemberejor20-Mar-12 4:13 
      Generalapplication usage Pinmemberjohn56324-Mar-10 2:01 
      QuestionHow to get CPU Usage for a particular process?? Pinmembersanjibmail2-Jan-09 12:20 
      GeneralThe way it's supposed to be... PinmemberVitaly Tomilov2-Sep-08 6:53 
      GeneralRe: The way it's supposed to be... [modified] Pinmemberejor2-Sep-08 9:40 
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov2-Sep-08 9:44 
      GeneralRe: The way it's supposed to be... Pinmemberejor2-Sep-08 10:06 
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov2-Sep-08 10:13 
      GeneralRe: The way it's supposed to be... Pinmemberejor2-Sep-08 10:19 
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov2-Sep-08 10:23 
      GeneralRe: The way it's supposed to be... PinmemberMember 41446493-Dec-08 3:28 
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov3-Dec-08 3:41 
      AnswerRe: The way it's supposed to be... PinmemberMr Scotty18-Nov-09 23:43 
      GeneralRe: The way it's supposed to be... PinmemberMember 442588919-Mar-10 11:39 
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov19-Mar-10 12:09 
      GeneralRe: The way it's supposed to be... Pinmemberdxj1983102921-Jul-10 19:45 
      GeneralGood coding practices PinmemberAbbas Ibn Fernas24-Mar-10 0:46 
      GeneralRe: The way it's supposed to be... PinmemberVider20-Dec-11 2:29 
      GeneralRe: The way it's supposed to be... PinmemberDan Bloomquist29-May-13 17:56 
      GeneralMessy PinmemberVitaly Tomilov2-Sep-08 6:44 
      GeneralNice! PinmemberVitaly Tomilov23-Aug-08 8:15 
      GeneralRe: Nice! Pinmemberejor23-Aug-08 9:27 
      GeneralRe: Nice! PinmemberVitaly Tomilov23-Aug-08 9:38 
      GeneralDoesn't compile... PinmemberPaul Conrad9-Dec-07 13:31 
      GeneralCurrent Process % cpu utilisation Pinmemberkintz6-Jul-06 5:10 
      AnswerRe: Current Process % cpu utilisation Pinmemberejor29-Oct-06 11:06 
      QuestionCode bug? Pinmemberkintz6-Jul-06 0:24 
      AnswerRe: Code bug? Pinmemberejor6-Jul-06 2:31 
      Generalmultiple processor Pinmemberhxtking6-Apr-06 11:45 
      GeneralRe: multiple processor Pinmemberejor6-Apr-06 12:02 
      Questionhow about cygwin ? Pinmemberpersonalcomputer25-Feb-06 0:23 
      AnswerRe: how about cygwin ? Pinmemberejor26-Feb-06 0:29 
      QuestionWhy is "System Idle" process 100% of CPU? Pinmemberdmtwpi2120-Jan-06 10:59 
      AnswerRe: Why is "System Idle" process 100% of CPU? Pinmemberejor20-Jan-06 11:04 
      GeneralWindows CE support Pinmemberronannaughton25-Oct-05 15:18 
      GeneralRe: Windows CE support PinmemberShanmuga Sundar.V8-May-08 4:57 
      Generalremote computers PinmemberPeterK200325-Aug-05 10:11 
      GeneralRe: remote computers Pinmemberejor25-Aug-05 10:21 
      Generalwhen to get last times Pinmemberferhatmantar9-Aug-05 1:00 
      GeneralRe: when to get last times Pinmemberejor9-Aug-05 3:23 
      QuestionGreat! do you know function like those used in your class for memory monitoring ? Pinmembermamelouk10-Jun-05 5:15 
      GeneralIt's good, and TaskManager uses a look like method PinsussStrom_DCASM20-Feb-05 5:50 
      GeneralI think its not working on my pc !! Pinmembersandeepsulakhe13-Feb-05 19:48 
      GeneralRe: I think its not working on my pc !! Pinmembersandeepsulakhe14-Feb-05 1:49 

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