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:
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.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 );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 );
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
look in TKTime.h you've got the type definition
Huummmm how could say that.....
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
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
Hey ejor:
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
Hi Ejor
Here's the code that I got after throwing away about 90% of junk from your code.
Just don't forget a link where you find the code in your project
Thank you!
just an heuristic to have something similar to task manager
Got you!
I just wanted to know, since you did not document those things.
I've documented the important part of the algo....
Well, you see, i started with the article, tried to implement exactly as the article suggested, but it didn't work, iwas always getting 0% for some reasons that i couldn't understand. So, i ended up taking the original code you wrote and throwing away the parts i didn't need. That told me the article wasn't complete...
, it just does.
s_cpu[(s_index++) %5] = cpu;<br />
s_cpuProcess[(s_index++) %5] = cpuProcess;<br />
s_count ++;<br />
if
( s_count >
5
) <br />
s_count =
5
;<br />
The author commented on this just up as "...just an heuristic to have something similar to task manager".
Member 4144649 is correct this is a little strange.
sorry to awaken this dead beast but i am having trouble compiling either pices of code, both the code in the article and the code provided by Vitaly Tomilov.
\microsoft sdks\windows\v6.0a\include\sdkddkver.h(200) : fatal error C1012: unmatched parenthesis : missing ')'
..\ProcessorUsage.cpp|89|error: `CCritSecLock' was not declared in this scope|
CCritSecLock is a standard class from ATL. See file atlbase.h
in stdafx.h line 6,
if
(hModule = GetModuleHandle(_T(
"
kernel32.dll"
)))
if
(s_pfnGetSystemTimes = (pfnGetSystemTimes)GetProcAddress(hModule,
"
GetSystemTimes"
))
return
;
if
(!(hModule = GetModuleHandle(_T(
"
ntdll.dll"
)))
return
;
if
(!(s_pfnNtQuerySystemInformation = (pfnNtQuerySystemInformation)GetProcAddress(hModule,
"
NtQuerySystemInformation"
)))
return
;
More readable and in case you want a return value you don't need an else for each if.