1,183
社区成员
发帖
与我相关
我的任务
分享
unit GetCPUUse;
interface
uses
Windows, SysUtils;
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_Performance_Information = packed record
liIdleTime: LARGE_INTEGER; {LARGE_INTEGER}
dwSpare: array[0..75] of DWORD;
end;
Type
TSystem_Time_Information = packed record
liKeBootTime: LARGE_INTEGER;
liKeSystemTime: LARGE_INTEGER;
liExpTimeZoneBias: LARGE_INTEGER;
uCurrentTimeZoneId: ULONG;
dwReserved: DWORD;
end;
Var
NtQuerySystemInformation: function(infoClass: DWORD;
buffer: Pointer;
bufSize: DWORD;
returnSize: TPDword): DWORD; stdcall = nil;
liOldIdleTime: LARGE_INTEGER = ();
liOldSystemTime: LARGE_INTEGER = ();
Function GetCPUUsage: Double;
implementation
function Li2Double(x: LARGE_INTEGER): Double;
begin
Result := x.HighPart * 4.294967296E9 + x.LowPart
end;
Function GetCPUUsage: Double;
Var
SysBaseInfo : TSystem_Basic_Information;
SysPerfInfo : TSystem_Performance_Information;
SysTimeInfo : TSystem_Time_Information;
Status : LongInt;
dbSystemTime : Double;
dbIdleTime : Double;
Temp : String;
Begin
if @NtQuerySystemInformation = nil then
NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'),
'NtQuerySystemInformation');
status := NtQuerySystemInformation(0, @SysBaseInfo, SizeOf(SysBaseInfo), nil);
if status <> 0 then
Exit;
status := NtQuerySystemInformation(3, @SysTimeInfo, SizeOf(SysTimeInfo), 0);
if status <> 0 then
Exit;
status := NtQuerySystemInformation(2, @SysPerfInfo, SizeOf(SysPerfInfo), nil);
if status <> 0 then
Exit;
if (liOldIdleTime.QuadPart <> 0) then
begin
dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
dbIdleTime := dbIdleTime / dbSystemTime;
dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5;
End;
liOldIdleTime := SysPerfInfo.liIdleTime;
liOldSystemTime := SysTimeInfo.liKeSystemTime;
// wait one second
Result := dbIdleTime;
End;
end.
unit CPUUsage;
interface
uses
Windows, SysUtils;
type
_SYSTEM_PERFORMANCE_INFORMATION = record
IdleTime: LARGE_INTEGER;
Reserved: array[0..75] of DWORD;
end;
PSystemPerformanceInformation = ^TSystemPerformanceInformation;
TSystemPerformanceInformation = _SYSTEM_PERFORMANCE_INFORMATION;
_SYSTEM_BASIC_INFORMATION = record
Reserved1: array[0..23] of Byte;
Reserved2: array[0..3] of Pointer;
NumberOfProcessors: UCHAR;
end;
PSystemBasicInformation = ^TSystemBasicInformation;
TSystemBasicInformation = _SYSTEM_BASIC_INFORMATION;
_SYSTEM_TIME_INFORMATION = record
KeBootTime: LARGE_INTEGER;
KeSystemTime: LARGE_INTEGER;
ExpTimeZoneBias: LARGE_INTEGER;
CurrentTimeZoneId: ULONG;
end;
PSystemTimeInformation = ^TSystemTimeInformation;
TSystemTimeInformation = _SYSTEM_TIME_INFORMATION;
function GetCPURate: Byte;
function NtQuerySystemInformation(
SystemInformationClass: UINT;
SystemInformation: Pointer;
SystemInformationLength: ULONG;
ReturnLength: PULONG): Integer; stdcall; external 'ntdll.dll';
var
FOldIdleTime: LARGE_INTEGER;
FOldSystemTime: LARGE_INTEGER;
implementation
function GetCPURate: Byte;
var
PerfInfo: TSystemPerformanceInformation;
TimeInfo: TSystemTimeInformation;
BaseInfo: TSystemBasicInformation;
IdleTime: INT64;
SystemTime: INT64;
begin
Result := 0;
if NtQuerySystemInformation(3, @TimeInfo, SizeOf(TimeInfo), nil) <> NO_ERROR then Exit;
if NtQuerySystemInformation(2, @PerfInfo, SizeOf(PerfInfo), nil) <> NO_ERROR then Exit;
if NtQuerySystemInformation(0, @BaseInfo, SizeOf(BaseInfo), nil) <> NO_ERROR then Exit;
if (FOldIdleTime.QuadPart <> 0) and (BaseInfo.NumberOfProcessors <> 0) then
begin
IdleTime := PerfInfo.IdleTime.QuadPart - FOldIdleTime.QuadPart;
SystemTime := TimeInfo.KeSystemTime.QuadPart - FOldSystemTime.QuadPart;
if SystemTime <> 0 then
Result := Trunc(100.0 - (IdleTime / SystemTime) * 100.0 / BaseInfo.NumberOfProcessors);
end;
FOldIdleTime := PerfInfo.IdleTime;
FOldSystemTime := TimeInfo.KeSystemTime;
end;
end.