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?
LVL 1
bogieman_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
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
0
MadshiCommented:
listening...
0
bryan7Commented:
listn
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

edeyCommented:
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
0
intheCommented:
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

0
rwilson032697Commented:
Listening
0
bozo7Commented:
listening
0
bogieman_Author Commented:
That works so far, but how can I get CPU usage in percent??
0
intheCommented:
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
0
bogieman_Author Commented:
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.
0
intheCommented:
did you install the perfstats component that comes with it (monsys) ?,then use that to pull info from registry as thatwhat its job is..and its complicated but so is anything like this ,its not a simple button click stuff.i couldnt do better than what the monsys example does.
yu should start by opening the monsys in delphi and hacking away at the code and component settings till it does only what you need.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bryan7Commented:
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
0
bryan7Commented:
did it work ?
0
intheCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.