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
);
int
GetUsage( int* pSystemUsage, TKTime* pUpTime );
History
20
th
December, 2004: Initial version
[CPU.cpp]
Line 052: 'if( s_hKernel == NULL )' should be 'if( s_hKernel )'
Line 191: in case 'sys == 0 || div == 0' you should return old values (fail)
Line 207~208: 's_index' incremented twice. suggested:
s_cpu[s_index] = cpu;
s_cpuProcess[s_index] = cpuProcess;
s_index = (s_index +
1
) % _countof(s_cpu);
and better to lock whole function if you don't need any atomic field update.
Sign In
·
View Thread
what is the type and where have u defined last_userTime, last_kernelTime, last_idleTime, usr, ker, idl,sys in the code. sorry I couldn't compile the code. any help can be so useful. tnx
Sign In
·
View Thread
look in TKTime.h you've got the type definition
-----------------------------
ejor @ http://dev.jesover.net
Sign In
·
View Thread
Huummmm how could say that.....
did you opened the zip an look at files ?
-----------------------------
ejor @ http://dev.jesover.net
Sign In
·
View Thread
tnx. i download the rar and i could run it. could u please tell me what is in the process and system variable? and how can i get total cpu usage in percent? i want to get when is system in ideal time(for example it is 10 min that cpu usage is less than 5 %). tnx a lot
Sign In
·
View Thread
When I use this function, I encountered an error that this function is undeclared, do you know why?
love_fairytale
13-Mar-12 22:12
love_fairytale
13-Mar-12 22:12
Hey ejor:
I want to ask you a question.
When I use this function, I encountered an error that this function is undeclared.
I'm sure I have included the windows.h and according to the introduction of MSDN, defined the _WIN32_WINNT as 0x0501, but the error is still existent.
Can you give me some help to solve it?
Thank you very much~
Sign In
·
View Thread
Re: When I use this function, I encountered an error that this function is undeclared, do you know why?
ejor
20-Mar-12 3:13
ejor
20-Mar-12 3:13
Hi Ejor
Could you please let me know how to get the CPU Usage in % of a particular running process say 'taskmgr.exe'?
I would like to get the CPU Usage of a particular process running in PC.
Please help me.
Thanks
Sanjib
Sign In
·
View Thread
Here's the code that I got after throwing away about 90% of junk from your code.
This code allows getting CPU usage, and works not only on Windows XPSP1 or later, but also on Windows 2000/XP/2003/Vista/2008.
I did this to include CPU usage into
ProSysLib
as one of the CPU parameters.
Just two files, header + cpp.
ProcessorUsage.h:
class
CProcessorUsage
typedef
BOOL (WINAPI * pfnGetSystemTimes)(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime );
typedef
LONG (WINAPI * pfnNtQuerySystemInformation)(ULONG SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
struct
PROC_PERF_INFO
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER Reserved1[
2
];
ULONG Reserved2;
public:
CProcessorUsage();
~CProcessorUsage();
USHORT GetUsage();
private:
void
GetSysTimes(
__int64
& idleTime,
__int64
& kernelTime,
__int64
& userTime);
static
DWORD s_TickMark;
static
__int64
s_time;
static
__int64
s_idleTime;
static
__int64
s_kernelTime;
static
__int64
s_userTime;
static
int
s_lastCpu;
static
int
s_cpu[
5
];
static
__int64
s_kernelTimeProcess;
static
__int64
s_userTimeProcess;
static
int
s_cpuProcess[
5
];
static
int
s_count;
static
int
s_index;