Главная » Delphi » Основная » Посмотреть на загрузку процессора

Посмотреть на загрузку процессора

14:58
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
const
  SystemBasicInformation = 0;
  SystemPerformanceInformation = 2;
  SystemTimeInformation = 3;
 
type
  TPDWord = ^DWORD;
 
  TSystem_Basic_Information = packed record
    dwUnknown1: DWORD;
    uKeMaximumIncrement: ULONG;
    uPageSize: ULONG;
    uMmNumberOfPhysicalPages: ULONG;
    uMmLowestPhysicalPage: ULONG;
    uMmHighestPhysicalPage: ULONG;
    uAllocationGranularity: ULONG;
    pLowestUserAddress: Pointer;
    pMmHighestUserAddress: Pointer;
    uKeActiveProcessors: ULONG;
    bKeNumberProcessors: byte;
    bUnknown2: byte;
    wUnknown3: word;
  end;
type
  TSystem_Time_Information = packed record
    liKeBootTime: LARGE_INTEGER;
    liKeSystemTime: LARGE_INTEGER;
    liExpTimeZoneBias: LARGE_INTEGER;
    uCurrentTimeZoneId: ULONG;
    dwReserved: DWORD;
  end;
type
  TSystem_Performance_Information = packed record
    liIdleTime: LARGE_INTEGER; {LARGE_INTEGER}
    dwSpare: array[0..75] of DWORD;
  end;
type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  NtQuerySystemInformation:
  function(infoClass: DWORD; buffer: Pointer; bufSize: DWORD;
    returnSize: TPDword): DWORD; stdcall = nil;
 
implementation
 
{$R *.dfm}
 
function GetProcessId(h: HWND): DWORD;
begin
  GetWindowThreadProcessId(h, @result);
end;
 
function CPUUsage: string;
var SysBaseInfo: TSystem_Basic_Information;
  SysPerfInfo: TSystem_Performance_Information;
  SysTimeInfo: TSystem_Time_Information;
  status: Longint; {long}
  liOldIdleTime, liOldSystemTime: LARGE_INTEGER;
  dbSystemTime, dbIdleTime, dbIdleTimePercent: Double;
 
  function Li2Double(x: LARGE_INTEGER): Double;
  begin
    Result := x.HighPart * 4.294967296E9 + x.LowPart
  end;
 
begin
  result := '';
// if not IsNT then exit;
  if @NtQuerySystemInformation = nil then
    NtQuerySystemInformation :=
      GetProcAddress(GetModuleHandle('ntdll.dll'),
      'NtQuerySystemInformation');
 
// get number of processors in the system
  status := NtQuerySystemInformation(SystemBasicInformation,
    @SysBaseInfo, SizeOf(SysBaseInfo),
    nil);
  if status <> 0 then exit;
 
// get baseline snapshot (time and idle)
  status := NtQuerySystemInformation(SystemTimeInformation,
    @SysTimeInfo, SizeOf(SysTimeInfo),
    nil);
  if status <> 0 then Exit;
  status := NtQuerySystemInformation(SystemPerformanceInformation,
    @SysPerfInfo, SizeOf(SysPerfInfo),
    nil);
  if status <> 0 then Exit;
 
  liOldIdleTime := SysPerfInfo.liIdleTime;
  liOldSystemTime := SysTimeInfo.liKeSystemTime;
 
  Sleep(500); // wait
 
// get new snapshot
  status := NtQuerySystemInformation(SystemTimeInformation,
    @SysTimeInfo, SizeOf(SysTimeInfo),
    nil);
  if status <> 0 then Exit;
  status := NtQuerySystemInformation(SystemPerformanceInformation,
    @SysPerfInfo, SizeOf(SysPerfInfo),
    nil);
  if status <> 0 then Exit;
 
// get difference - & calc usage % during the 500msec
  dbIdleTime :=
    Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
  dbSystemTime :=
    Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
  dbIdleTimePercent := dbIdleTime / dbSystemTime * 100;
 
  if SysBaseInfo.bKeNumberProcessors > 1
    then result := 'CPUs: ' + IntToStr(SysBaseInfo.bKeNumberProcessors) +
    ' Av. Usage: ' + FormatFloat('0%',
 
      (100.0 - dbIdleTimePercent) / SysBaseInfo.bKeNumberProcessors)
  else result := 'CPU Usage: ' +
    FormatFloat('0%',
 
      (100.0 - dbIdleTimePercent) / SysBaseInfo.bKeNumberProcessors);
end;
 
function InsFile(filename: string): string;
var
  TF: TextFile;
begin
  AssignFile(TF, ExtractFileDir(ParamStr(0)) + '\' + filename);
  read(TF, Result);
  CloseFile(TF);
end;
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  caption := CPUUsage;
end;
 
end.