Click here to Skip to main content
16,019,976 members
Articles / Programming Languages / C++

Get CPU Usage with GetSystemTimes

Rate me:
Please Sign up or sign in to vote.
4.27/5 (26 votes)
20 Dec 2004Public Domain1 min read 357.3K   7.5K   59   59
How to get CPU usage with GetSystemTimes
In this article, you will find a solution for getting CPU usage with the help of GetSystemTimes.

Introduction

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

You've a lot of ways 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 );

History

  • 20th December, 2004: Initial version

License

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


Written By
Web Developer
United Kingdom United Kingdom
http://dev.jesover.net

Comments and Discussions

 
Bugbug fix Pin
Member 18343684-Jun-16 17:30
Member 18343684-Jun-16 17:30 
Questionhow to compile? Pin
Mojtaba Setoodeh10-Sep-13 0:10
Mojtaba Setoodeh10-Sep-13 0:10 
AnswerRe: how to compile? Pin
ejor10-Sep-13 1:53
ejor10-Sep-13 1:53 
GeneralRe: how to compile? Pin
Mojtaba Setoodeh10-Sep-13 18:53
Mojtaba Setoodeh10-Sep-13 18:53 
GeneralRe: how to compile? Pin
ejor10-Sep-13 19:49
ejor10-Sep-13 19:49 
GeneralRe: how to compile? Pin
Mojtaba Setoodeh10-Sep-13 23:19
Mojtaba Setoodeh10-Sep-13 23:19 
QuestionWhen I use this function, I encountered an error that this function is undeclared, do you know why? Pin
love_fairytale13-Mar-12 22:12
love_fairytale13-Mar-12 22:12 
AnswerRe: When I use this function, I encountered an error that this function is undeclared, do you know why? Pin
ejor20-Mar-12 3:13
ejor20-Mar-12 3:13 
Generalapplication usage Pin
john56324-Mar-10 1:01
john56324-Mar-10 1:01 
QuestionHow to get CPU Usage for a particular process?? Pin
sanjibmail2-Jan-09 11:20
sanjibmail2-Jan-09 11:20 
GeneralThe way it's supposed to be... PinPopular
Vitaly Tomilov2-Sep-08 5:53
Vitaly Tomilov2-Sep-08 5:53 
GeneralRe: The way it's supposed to be... [modified] Pin
ejor2-Sep-08 8:40
ejor2-Sep-08 8:40 
GeneralRe: The way it's supposed to be... Pin
Vitaly Tomilov2-Sep-08 8:44
Vitaly Tomilov2-Sep-08 8:44 
GeneralRe: The way it's supposed to be... Pin
ejor2-Sep-08 9:06
ejor2-Sep-08 9:06 
GeneralRe: The way it's supposed to be... Pin
Vitaly Tomilov2-Sep-08 9:13
Vitaly Tomilov2-Sep-08 9:13 
GeneralRe: The way it's supposed to be... Pin
ejor2-Sep-08 9:19
ejor2-Sep-08 9:19 
GeneralRe: The way it's supposed to be... Pin
Vitaly Tomilov2-Sep-08 9:23
Vitaly Tomilov2-Sep-08 9:23 
GeneralRe: The way it's supposed to be... Pin
Member 41446493-Dec-08 2:28
Member 41446493-Dec-08 2:28 
GeneralRe: The way it's supposed to be... Pin
Vitaly Tomilov3-Dec-08 2:41
Vitaly Tomilov3-Dec-08 2:41 
AnswerRe: The way it's supposed to be... Pin
Mr Scotty18-Nov-09 22:43
Mr Scotty18-Nov-09 22:43 
GeneralRe: The way it's supposed to be... Pin
Member 442588919-Mar-10 10:39
Member 442588919-Mar-10 10:39 
GeneralRe: The way it's supposed to be... Pin
Vitaly Tomilov19-Mar-10 11:09
Vitaly Tomilov19-Mar-10 11:09 
GeneralRe: The way it's supposed to be... Pin
dxj1983102921-Jul-10 18:45
dxj1983102921-Jul-10 18:45 
GeneralGood coding practices Pin
Youssef-Gamal-523-Mar-10 23:46
Youssef-Gamal-523-Mar-10 23:46 
GeneralRe: The way it's supposed to be... Pin
Vider20-Dec-11 1:29
Vider20-Dec-11 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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