bogieman_
asked on
Get CPU usage in %, memory usage levels...
I have seen alot of programs that can display the CPU usage in percent in a fancy little graph, and other things like the number of threads running, and the number of VM's (virtual machines, like a dos window, or windows itself), or GDI memory etc. How can I get these values?
listening...
listn
you can get some of this by calling:
VOID GetSystemInfo(
LPSYSTEM_INFO lpSystemInfo // address of system information structure
);
and more from this m$ article on writing performance monitors:
http://support.microsoft.com/support/kb/articles/q174/6/31.asp
GL
Mike
VOID GetSystemInfo(
LPSYSTEM_INFO lpSystemInfo // address of system information structure
);
and more from this m$ article on writing performance monitors:
http://support.microsoft.com/support/kb/articles/q174/6/31.asp
GL
Mike
the following example display free user,gdi,and system resources(same as win9* resource moniter)and will work on win9* pc's
unit Thunker;
interface
const
SYSres = 0;
USERres = 2;
GDIres = 1;
function GetSystemResources(Res: Word): Word;
function UserPercentage : Integer;
function SystemPercentage : Integer;
function GDIPercentage : Integer;
procedure DisplayFreeResources;
implementation
uses Windows,SysUtils,Dialogs;
function ThunkLoadLibrary(LibraryNa me: PChar): THandle; stdcall; external kernel32 index 35;
procedure ThunkFreeLibrary(HInstance : THandle); stdcall; external kernel32 index 36;
function ThunkGetProcAddress(Hinsta nce: THandle; ProcName: PChar): Pointer; stdcall; external kernel32 index 37;
procedure Thunk; cdecl; external kernel32 name 'QT_Thunk';
{$StackFrames On}
var
Th : THandle = 0;
P: Pointer = nil;
function GetSystemResources(Res: Word): Word;
var
Ar : array[0..$40] of Word;
begin
Ar[0] := Th; //get major error without this ?
Th := ThunkLoadLibrary('user.exe ');
if Th < 32 then
raise Exception.Create('Error loading User.exe');
ThunkFreeLibrary(Th);
P := ThunkGetProcAddress(Th,'Ge tFreeSyste mResources ');
if P = nil then
raise Exception.Create('GetSyste mResources function not found');
asm
push Res
mov edx, P
call Thunk
mov Result, ax
end;
end;
function UserPercentage : Integer;
begin
Result := GetSystemResources(USERres );
end;
function SystemPercentage : Integer;
begin
Result := GetSystemResources(SYSres) ;
end;
function GDIPercentage : Integer;
begin
Result := GetSystemResources(GDIres) ;
end;
Procedure DisplayFreeResources;
begin
showmessage(
'SYSTEM : '+inttostr(SystemPercentag e)+'%'+#13 #10+
'USER : '+inttostr(UserPercentage) +'%'+#13#1 0+
'GDI : '+inttostr(GDIPercentage)+ '%');
end;
end.
example:
uses Thunker;
procedure TForm1.Button1Click(Sender : TObject);
begin
DisplayFreeResources;
end;
a example to get various memory stuff:
unit GlobalMemoryStatusU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TGlobalMemoryStatusForm = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
ButtonGlobalMemoryStatus: TButton;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
procedure ButtonGlobalMemoryStatusCl ick(Sender : TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
GlobalMemoryStatusForm: TGlobalMemoryStatusForm;
implementation
{$R *.DFM}
procedure TGlobalMemoryStatusForm.Bu ttonGlobal MemoryStat usClick(
Sender: TObject);
var
GlobalMemoryInfo : TMemoryStatus; // holds the global memory status information
begin
{set the size of the structure before the call.}
GlobalMemoryInfo.dwLength := SizeOf(GlobalMemoryInfo);
{retrieve the global memory status...}
GlobalMemoryStatus(GlobalM emoryInfo) ;
{and display the information}
Label1.caption := 'Results of GlobalMemoryStatus:';
Label2.caption := 'Record structure size: '+IntToStr(
GlobalMemoryInfo.dwLength) +' bytes';
Label3.caption := 'Current memory load: '+IntToStr(
GlobalMemoryInfo.dwMemoryL oad)+'%';
Label4.caption := 'Total physical memory: '+Format('%.0n',[
GlobalMemoryInfo.dwTotalPh ys/1])+' bytes';
Label5.caption := 'Total available physical memory: '+Format('%.0n',[
GlobalMemoryInfo.dwAvailPh ys/1])+' bytes';
Label6.caption := 'Total paging file size: '+Format('%.0n',[
GlobalMemoryInfo.dwTotalPa geFile/1]) +' bytes';
Label7.Caption := 'Total available paging file memory: '+Format('%.0n',[
GlobalMemoryInfo.dwAvailPa geFile/1]) +' bytes';
Label8.caption := 'Total virtual memory: '+Format('%.0n',[
GlobalMemoryInfo.dwTotalVi rtual/1])+ ' bytes';
Label9.caption := 'Total available virtual memory: '+Format('%.0n',[
GlobalMemoryInfo.dwAvailVi rtual/1])+ ' bytes';
end;
end.
for nt maybe want to see these stuff (nt performance monitering):
http://members.tripod.com/~aldyn/PerfUtilsD4Src.zip
(example apps)
http://members.tripod.com/~aldyn/pus_frame.html
also have a look here :
http://www.wilsonc.demon.co.uk/delphi.htm
(components that enscapulate performance datahelper dll and a demo:
more specifically:
http://www.wilsonc.demon.co.uk/Delphi%20Components/pdh/pdh.zip
and some stuff from jgsoftware:
http://www.jgsoftware.com/files/ntperf.zip
http://www.jgsoftware.com/files/pdhd10.zip
http://www.jgsoftware.com/files/perfmon.zip
well that should keep you happy a while ;-)
Regards Barry
unit Thunker;
interface
const
SYSres = 0;
USERres = 2;
GDIres = 1;
function GetSystemResources(Res: Word): Word;
function UserPercentage : Integer;
function SystemPercentage : Integer;
function GDIPercentage : Integer;
procedure DisplayFreeResources;
implementation
uses Windows,SysUtils,Dialogs;
function ThunkLoadLibrary(LibraryNa
procedure ThunkFreeLibrary(HInstance
function ThunkGetProcAddress(Hinsta
procedure Thunk; cdecl; external kernel32 name 'QT_Thunk';
{$StackFrames On}
var
Th : THandle = 0;
P: Pointer = nil;
function GetSystemResources(Res: Word): Word;
var
Ar : array[0..$40] of Word;
begin
Ar[0] := Th; //get major error without this ?
Th := ThunkLoadLibrary('user.exe
if Th < 32 then
raise Exception.Create('Error loading User.exe');
ThunkFreeLibrary(Th);
P := ThunkGetProcAddress(Th,'Ge
if P = nil then
raise Exception.Create('GetSyste
asm
push Res
mov edx, P
call Thunk
mov Result, ax
end;
end;
function UserPercentage : Integer;
begin
Result := GetSystemResources(USERres
end;
function SystemPercentage : Integer;
begin
Result := GetSystemResources(SYSres)
end;
function GDIPercentage : Integer;
begin
Result := GetSystemResources(GDIres)
end;
Procedure DisplayFreeResources;
begin
showmessage(
'SYSTEM : '+inttostr(SystemPercentag
'USER : '+inttostr(UserPercentage)
'GDI : '+inttostr(GDIPercentage)+
end;
end.
example:
uses Thunker;
procedure TForm1.Button1Click(Sender
begin
DisplayFreeResources;
end;
a example to get various memory stuff:
unit GlobalMemoryStatusU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TGlobalMemoryStatusForm = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
ButtonGlobalMemoryStatus: TButton;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
procedure ButtonGlobalMemoryStatusCl
private
{ Private declarations }
public
{ Public declarations }
end;
var
GlobalMemoryStatusForm: TGlobalMemoryStatusForm;
implementation
{$R *.DFM}
procedure TGlobalMemoryStatusForm.Bu
Sender: TObject);
var
GlobalMemoryInfo : TMemoryStatus; // holds the global memory status information
begin
{set the size of the structure before the call.}
GlobalMemoryInfo.dwLength := SizeOf(GlobalMemoryInfo);
{retrieve the global memory status...}
GlobalMemoryStatus(GlobalM
{and display the information}
Label1.caption := 'Results of GlobalMemoryStatus:';
Label2.caption := 'Record structure size: '+IntToStr(
GlobalMemoryInfo.dwLength)
Label3.caption := 'Current memory load: '+IntToStr(
GlobalMemoryInfo.dwMemoryL
Label4.caption := 'Total physical memory: '+Format('%.0n',[
GlobalMemoryInfo.dwTotalPh
Label5.caption := 'Total available physical memory: '+Format('%.0n',[
GlobalMemoryInfo.dwAvailPh
Label6.caption := 'Total paging file size: '+Format('%.0n',[
GlobalMemoryInfo.dwTotalPa
Label7.Caption := 'Total available paging file memory: '+Format('%.0n',[
GlobalMemoryInfo.dwAvailPa
Label8.caption := 'Total virtual memory: '+Format('%.0n',[
GlobalMemoryInfo.dwTotalVi
Label9.caption := 'Total available virtual memory: '+Format('%.0n',[
GlobalMemoryInfo.dwAvailVi
end;
end.
for nt maybe want to see these stuff (nt performance monitering):
http://members.tripod.com/~aldyn/PerfUtilsD4Src.zip
(example apps)
http://members.tripod.com/~aldyn/pus_frame.html
also have a look here :
http://www.wilsonc.demon.co.uk/delphi.htm
(components that enscapulate performance datahelper dll and a demo:
more specifically:
http://www.wilsonc.demon.co.uk/Delphi%20Components/pdh/pdh.zip
and some stuff from jgsoftware:
http://www.jgsoftware.com/files/ntperf.zip
http://www.jgsoftware.com/files/pdhd10.zip
http://www.jgsoftware.com/files/perfmon.zip
well that should keep you happy a while ;-)
Regards Barry
Listening
listening
ASKER
That works so far, but how can I get CPU usage in percent??
Hi bogieman,
i found a great project and was wrote in delphi and comes with source:
ftp://ftp.zdnet.com/pcmag/1996/1008/monsys.zip
Regards Barry
i found a great project and was wrote in delphi and comes with source:
ftp://ftp.zdnet.com/pcmag/1996/1008/monsys.zip
Regards Barry
ASKER
I looked at that briefly in notepad, and it looks complicated, but I have figured out how to tell windows to moniter the CPU and it stores this value in a binary registry entry - something like "a2 00 00 00", so how can I get the first hexedecimal value which is the CPU percetage (64 as maximum)? I will still give the points if you do this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
CPU Speed:
function GetCPUSpeed: Double;
const
DelayTime = 500; // measure time in ms
var
TimerHi, TimerLo: DWORD;
PriorityClass, Priority: Integer;
begin
PriorityClass := GetPriorityClass(GetCurren tProcess);
Priority := GetThreadPriority(GetCurre ntThread);
SetPriorityClass(GetCurren tProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurre ntThread, THREAD_PRIORITY_TIME_CRITI CAL);
Sleep(10);
asm
dw 310Fh // rdtsc
mov TimerLo, eax
mov TimerHi, edx
end;
Sleep(DelayTime);
asm
dw 310Fh // rdtsc
sub eax, TimerLo
sbb edx, TimerHi
mov TimerLo, eax
mov TimerHi, edx
end;
SetThreadPriority(GetCurre ntThread, Priority);
SetPriorityClass(GetCurren tProcess, PriorityClass);
Result := TimerLo / (1000.0 * DelayTime);
end;
ShowMessage(Format('CPU speed: %f MHz', [GetCPUSpeed]));
bryan
function GetCPUSpeed: Double;
const
DelayTime = 500; // measure time in ms
var
TimerHi, TimerLo: DWORD;
PriorityClass, Priority: Integer;
begin
PriorityClass := GetPriorityClass(GetCurren
Priority := GetThreadPriority(GetCurre
SetPriorityClass(GetCurren
SetThreadPriority(GetCurre
Sleep(10);
asm
dw 310Fh // rdtsc
mov TimerLo, eax
mov TimerHi, edx
end;
Sleep(DelayTime);
asm
dw 310Fh // rdtsc
sub eax, TimerLo
sbb edx, TimerHi
mov TimerLo, eax
mov TimerHi, edx
end;
SetThreadPriority(GetCurre
SetPriorityClass(GetCurren
Result := TimerLo / (1000.0 * DelayTime);
end;
ShowMessage(Format('CPU speed: %f MHz', [GetCPUSpeed]));
bryan
did it work ?
bryan isnt that is too show the cpu's speed ie 166,200,266 etc ?,
bogieman needed the percentages in use of the cpu .. ie 56% ,78%...ie the performance stats.
bogieman needed the percentages in use of the cpu .. ie 56% ,78%...ie the performance stats.
i can send you a demo program to do this when i get home ,leave you mail address