Click here to Skip to main content
6,822,613 members and growing! (16,506 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » General     Intermediate License: A Public Domain dedication

Get CPU Usage with GetSystemTimes

By ejor

Get CPU usage with GetSystemTimes.
VC6, VC7, VC7.1WinXP, Win2003VS.NET2003, Dev
Posted:20 Dec 2004
Views:116,460
Bookmarked:39 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 5.09 Rating: 3.85 out of 5
1 vote, 4.8%
1
2 votes, 9.5%
2
2 votes, 9.5%
3
5 votes, 23.8%
4
11 votes, 52.4%
5
  • 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

      About the Author

      ejor


      Member
      http://dev.jesover.net
      Occupation: Web Developer
      Location: United Kingdom United Kingdom

      Other popular Threads, Processes & IPC articles:

      Article Top
      You must Sign In to use this message board.
      FAQ FAQ 
       
      Noise Tolerance  Layout  Per page   
       Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
      QuestionHow to get CPU Usage for a particular process?? Pinmembersanjibmail12:20 2 Jan '09  
      GeneralThe way it's supposed to be... PinmemberVitaly Tomilov6:53 2 Sep '08  
      GeneralRe: The way it's supposed to be... [modified] Pinmemberejor9:40 2 Sep '08  
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov9:44 2 Sep '08  
      GeneralRe: The way it's supposed to be... Pinmemberejor10:06 2 Sep '08  
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov10:13 2 Sep '08  
      GeneralRe: The way it's supposed to be... Pinmemberejor10:19 2 Sep '08  
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov10:23 2 Sep '08  
      GeneralRe: The way it's supposed to be... PinmemberMember 41446493:28 3 Dec '08  
      GeneralRe: The way it's supposed to be... PinmemberVitaly Tomilov3:41 3 Dec '08  
      AnswerRe: The way it's supposed to be... PinmemberMr Scotty23:43 18 Nov '09  
      GeneralMessy PinmemberVitaly Tomilov6:44 2 Sep '08  
      GeneralNice! PinmemberVitaly Tomilov8:15 23 Aug '08  
      GeneralRe: Nice! Pinmemberejor9:27 23 Aug '08  
      GeneralRe: Nice! PinmemberVitaly Tomilov9:38 23 Aug '08  
      GeneralDoesn't compile... PinmemberPaul Conrad13:31 9 Dec '07  
      GeneralCurrent Process % cpu utilisation Pinmemberkintz5:10 6 Jul '06  
      AnswerRe: Current Process % cpu utilisation Pinmemberejor11:06 29 Oct '06  
      GeneralCode bug? Pinmemberkintz0:24 6 Jul '06  
      GeneralRe: Code bug? Pinmemberejor2:31 6 Jul '06  
      Generalmultiple processor Pinmemberhxtking11:45 6 Apr '06  
      GeneralRe: multiple processor Pinmemberejor12:02 6 Apr '06  
      Generalhow about cygwin ? Pinmemberpersonalcomputer0:23 25 Feb '06  
      GeneralRe: how about cygwin ? Pinmemberejor0:29 26 Feb '06  
      GeneralWhy is "System Idle" process 100% of CPU? Pinmemberdmtwpi2110:59 20 Jan '06  

      General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

      Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

      PermaLink | Privacy | Terms of Use
      Last Updated: 20 Dec 2004
      Editor: Sumalatha K.R.
      Copyright 2004 by ejor
      Everything else Copyright © CodeProject, 1999-2010
      Web10 | Advertise on the Code Project