Author Topic: windows CPU Usage  (Read 5731 times)

seyed_farid

  • Posts: 15
windows CPU Usage
« on: 26 Feb '06 - 20:00 »
I cant find button of NEW TOPIC on this site , then i write my quession hear.
 :o ??? ??? ??? ;D
--------------------------------

Do you know any command to determine the windows CPU Usage?

Torkell

  • Posts: 1169
windows CPU Usage
« Reply #1 on: 26 Feb '06 - 21:01 »
I cant find button of NEW TOPIC on this site
It's in the top-right of the topic list, just above the "Last post" heading.

Quote
Do you know any command to determine the windows CPU Usage?
Under Windows NT/2000/XP/2003, press Ctrl+Alt+Del and pick Task Manager.

Tsorovan

  • Posts: 1247
windows CPU Usage
« Reply #2 on: 26 Feb '06 - 21:19 »
Or just Ctrl+Shift+Esc

seyed_farid

  • Posts: 15
Re: windows CPU Usage
« Reply #3 on: 3 Mar '06 - 14:05 »
I mean Code of getting CPU Usage in Delphi Programing.

Wraith

  • Posts: 190
Re: windows CPU Usage
« Reply #4 on: 11 Mar '06 - 20:33 »
I don't use Delphi, but this code might help you. The following code returns CPU usage of the current process:
Code: [Select]

float GetProcessorLoad()
{
static __int64 userTime = 0, kernelTime = 0, normalTime = 0, freq = 0;
static float v = -1.0f;
__int64 ut, kt, nt;
if(!GetProcessTimesObj( GetCurrentProcess(),
&FILETIME(), &FILETIME(),
(FILETIME*)&ut,(FILETIME*) &kt )
)
return -2.0f;

QueryPerformanceCounter( (LARGE_INTEGER*)& nt );
QueryPerformanceFrequency( (LARGE_INTEGER*)& freq );

if( userTime && kernelTime )
{
float dt = float(nt - normalTime)/freq;
if( dt>=0.5f )
{
v = ((ut - userTime) + (kt - kernelTime))/10000000.0f;
v /= dt;
v *= 100.0f; // percent
}
}
userTime = ut;
kernelTime = kt;
normalTime = nt;
return v;
}
The return values are:
-2.0f - not available (won't run under Win9x)
-1.0f - not yet available
0..100.0f - CPU Load

The idea was to see how many time the process spends both in kernel and user mode over some period of time, and divide that time by the period length.

The more correct way, I guess, would be querying WMI counters:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_performance_counter_types.asp