Link to home
Start Free TrialLog in
Avatar of bogieman_
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?
Avatar of inthe
inthe

for the gdi sytuff you need to thunk to a dll to use it functions..

i can send you a demo program to do this when i get home ,leave you mail address
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
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(LibraryName: PChar): THandle; stdcall; external kernel32 index 35;
procedure ThunkFreeLibrary(HInstance: THandle); stdcall; external kernel32 index 36;
function ThunkGetProcAddress(Hinstance: 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,'GetFreeSystemResources');
  if P = nil then
    raise Exception.Create('GetSystemResources 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(SystemPercentage)+'%'+#13#10+
'USER : '+inttostr(UserPercentage)+'%'+#13#10+
'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 ButtonGlobalMemoryStatusClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  GlobalMemoryStatusForm: TGlobalMemoryStatusForm;

implementation

{$R *.DFM}

procedure TGlobalMemoryStatusForm.ButtonGlobalMemoryStatusClick(
  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(GlobalMemoryInfo);

  {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.dwMemoryLoad)+'%';
  Label4.caption := 'Total physical memory: '+Format('%.0n',[
                     GlobalMemoryInfo.dwTotalPhys/1])+' bytes';
  Label5.caption := 'Total available physical memory: '+Format('%.0n',[
                     GlobalMemoryInfo.dwAvailPhys/1])+' bytes';
  Label6.caption := 'Total paging file size: '+Format('%.0n',[
                     GlobalMemoryInfo.dwTotalPageFile/1])+' bytes';
  Label7.Caption := 'Total available paging file memory: '+Format('%.0n',[
                     GlobalMemoryInfo.dwAvailPageFile/1])+' bytes';
  Label8.caption := 'Total virtual memory: '+Format('%.0n',[
                     GlobalMemoryInfo.dwTotalVirtual/1])+' bytes';
  Label9.caption := 'Total available virtual memory: '+Format('%.0n',[
                     GlobalMemoryInfo.dwAvailVirtual/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

Listening
listening
Avatar of bogieman_

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 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
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CPU Speed:


function GetCPUSpeed: Double;
const
  DelayTime = 500; // measure time in ms
var
  TimerHi, TimerLo: DWORD;
  PriorityClass, Priority: Integer;
begin
  PriorityClass := GetPriorityClass(GetCurrentProcess);
  Priority := GetThreadPriority(GetCurrentThread);

  SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
  SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);

  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(GetCurrentThread, Priority);
  SetPriorityClass(GetCurrentProcess, PriorityClass);

  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.