如何取得线程的CPU占用率(200分相送)(200分)

  • 主题发起人 主题发起人 cyb115
  • 开始时间 开始时间
C

cyb115

Unregistered / Unconfirmed
GUEST, unregistred user!
如何取得线程的CPU占用率(200分相送)?
 
给你转两篇:
unit adCpuUsage;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
CPU Usage Measurement routines for Delphi and C++ Builder
Author: Alexey A. Dynnikov
EMail: aldyn@chat.ru
WebSite: http://www.aldyn.ru/
Support: Use the e-mail aldyn@chat.ru
or support@aldyn.ru
Creation: Jul 8, 2000
Version: 1.02
Legal issues: Copyright (C) 2000 by Alexey A. Dynnikov <aldyn@chat.ru>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the productdo
cumentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
USAGE:
1. Include this unit into project.
2. Call GetCPUCount to obtain the numbr of processors in the system
3. Each time you need to know the value of CPU usage call the CollectCPUData
to refresh the CPU usage information. then
call the GetCPUUsage to obtain
the CPU usage for given processor. Note that succesive calls of GetCPUUsage
without calling CollectCPUData will return the same CPU usage value.
Example:
procedure TTestForm.TimerTimer(Sender: TObject);
var i: Integer;
begin
CollectCPUData;
// Get the data for all processors
for i:=0 to GetCPUCount-1do
// Show data for each processor
MInfo.Lines:=Format('CPU #%d - %5.2f%%',[i,GetCPUUsage(i)*100]);
end;
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
interface
uses
Windows, SysUtils;
// Call CollectCPUData to refresh information about CPU usage
procedure CollectCPUData;
// Call it to obtain the number of CPU's in the system
function GetCPUCount: Integer;
// Call it to obtain the % of usage for given CPU
function GetCPUUsage(Index: Integer):do
uble;
// For Win9x only: call it to stop CPU usage monitoring and free system resources
procedure ReleaseCPUData;
implementation
{$ifndef ver110}
{$ifndef ver90}
{$ifndef ver100}
{$define UseInt64}
{$endif}
{$endif}

{$ifdef UseInt64}
type TInt64 = Int64;
{$else
}
type TInt64 = Comp;
{$endif}
{$else
}
type TInt64 = TLargeInteger;
{$endif}
type
PInt64 = ^TInt64;
type
TPERF_DATA_BLOCK = record
Signature : array[0..4 - 1] of WCHAR;
LittleEndian : DWORD;
Version : DWORD;
Revision : DWORD;
TotalByteLength : DWORD;
HeaderLength : DWORD;
NumObjectTypes : DWORD;
DefaultObject : Longint;
SystemTime : TSystemTime;
Reserved: DWORD;
PerfTime : TInt64;
PerfFreq : TInt64;
PerfTime100nSec : TInt64;
SystemNameLength : DWORD;
SystemNameOffset : DWORD;
end;

PPERF_DATA_BLOCK = ^TPERF_DATA_BLOCK;
TPERF_OBJECT_TYPE = record
TotalByteLength : DWORD;
DefinitionLength : DWORD;
HeaderLength : DWORD;
ObjectNameTitleIndex : DWORD;
ObjectNameTitle : LPWSTR;
ObjectHelpTitleIndex : DWORD;
ObjectHelpTitle : LPWSTR;
DetailLevel : DWORD;
NumCounters : DWORD;
DefaultCounter : Longint;
NumInstances : Longint;
CodePage : DWORD;
PerfTime : TInt64;
PerfFreq : TInt64;
end;

PPERF_OBJECT_TYPE = ^TPERF_OBJECT_TYPE;
type
TPERF_COUNTER_DEFINITION = record
ByteLength : DWORD;
CounterNameTitleIndex : DWORD;
CounterNameTitle : LPWSTR;
CounterHelpTitleIndex : DWORD;
CounterHelpTitle : LPWSTR;
DefaultScale : Longint;
DetailLevel : DWORD;
CounterType : DWORD;
CounterSize : DWORD;
CounterOffset : DWORD;
end;

PPERF_COUNTER_DEFINITION = ^TPERF_COUNTER_DEFINITION;
TPERF_COUNTER_BLOCK = record
ByteLength : DWORD;
end;

PPERF_COUNTER_BLOCK = ^TPERF_COUNTER_BLOCK;
TPERF_INSTANCE_DEFINITION = record
ByteLength : DWORD;
ParentObjectTitleIndex : DWORD;
ParentObjectInstance : DWORD;
UniqueID : Longint;
NameOffset : DWORD;
NameLength : DWORD;
end;

PPERF_INSTANCE_DEFINITION = ^TPERF_INSTANCE_DEFINITION;
//------------------------------------------------------------------------------
{$ifdef ver130}
{$L-} // The L+ causes internal error in Delphi 5 compiler
{$O-} // The O+ causes internal error in Delphi 5 compiler
{$Y-} // The Y+ causes internal error in Delphi 5 compiler
{$endif}
{$ifndef ver110}
type
TInt64F = TInt64;
{$else
}
type
TInt64F = Extended;
{$endif}
{$ifdef ver110}
function FInt64(Value: TInt64): TInt64F;
function Int64D(Value: DWORD): TInt64;
{$else
}
type
FInt64 = TInt64F;
Int64D = TInt64;
{$endif}
{$ifdef ver110}
function FInt64(Value: TInt64): TInt64F;
var V: TInt64;
begin
if (Value.HighPart and $80000000) = 0 then
// positive value
begin
result:=Value.HighPart;
result:=result*$10000*$10000;
result:=result+Value.LowPart;
end else
begin
V.HighPart:=Value.HighPart xor $FFFFFFFF;
V.LowPart:=Value.LowPart xor $FFFFFFFF;
result:= -1 - FInt64(V);
end;
end;

function Int64D(Value: DWORD): TInt64;
begin
result.LowPart:=Value;
result.HighPart := 0;
// positive only
end;
{$endif}
//------------------------------------------------------------------------------
const
Processor_IDX_Str = '238';
Processor_IDX = 238;
CPUUsageIDX = 6;
type
AInt64F = array[0..$FFFF] of TInt64F;
PAInt64F = ^AInt64F;
var
_PerfData : PPERF_DATA_BLOCK;
_BufferSize: Integer;
_POT : PPERF_OBJECT_TYPE;
_PCD: PPerf_Counter_Definition;
_ProcessorsCount: Integer;
_Counters: PAInt64F;
_PrevCounters: PAInt64F;
_SysTime: TInt64F;
_PrevSysTime: TInt64F;
_IsWinNT: Boolean;
_W9xCollecting: Boolean;
_W9xCpuUsage: DWORD;
_W9xCpuKey: HKEY;

//------------------------------------------------------------------------------
function GetCPUCount: Integer;
begin
if _IsWinNT then
begin
if _ProcessorsCount < 0 then
CollectCPUData;
result:=_ProcessorsCount;
end else
begin
result:=1;
end;

end;

//------------------------------------------------------------------------------
procedure ReleaseCPUData;
var H: HKEY;
R: DWORD;
dwDataSize, dwType: DWORD;
begin
if _IsWinNT then
exit;
if not _W9xCollecting then
exit;
_W9xCollecting:=False;
RegCloseKey(_W9xCpuKey);
R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StopStat', 0, KEY_ALL_ACCESS, H );
if R <> ERROR_SUCCESS then
exit;
dwDataSize:=sizeof(DWORD);
RegQueryValueEx ( H, 'KERNEL/CPUUsage', nil, @dwType, PBYTE(@_W9xCpuUsage), @dwDataSize);
RegCloseKey(H);
end;

//------------------------------------------------------------------------------
function GetCPUUsage(Index: Integer):do
uble;
begin
if _IsWinNT then
begin
if _ProcessorsCount < 0 then
CollectCPUData;
if (Index >= _ProcessorsCount) or (Index < 0) then
raise Exception.Create('CPU index out of bounds');
if _PrevSysTime = _SysTime then
result:=0 else
result:=1-(_Counters[index] - _PrevCounters[index])/(_SysTime-_PrevSysTime);
end else
begin
if Index <> 0 then
raise Exception.Create('CPU index out of bounds');
if not _W9xCollecting then
CollectCPUData;
result:=_W9xCpuUsage / 100;
end;
end;

var VI: TOSVERSIONINFO;
//------------------------------------------------------------------------------
procedure CollectCPUData;
var BS: integer;
i: Integer;
_PCB_Instance: PPERF_COUNTER_BLOCK;
_PID_Instance: PPERF_INSTANCE_DEFINITION;
ST: TFileTime;
var H: HKEY;
R: DWORD;
dwDataSize, dwType: DWORD;
begin
if _IsWinNT then
begin
BS:=_BufferSize;
while RegQueryValueEx( HKEY_PERFORMANCE_DATA, Processor_IDX_Str, nil, nil,
PByte(_PerfData), @BS ) = ERROR_MORE_DATAdo
begin
// Get a buffer that is big enough.
INC(_BufferSize,$1000);
BS:=_BufferSize;
ReallocMem( _PerfData, _BufferSize );
end;

// Locate the performance object
_POT := PPERF_OBJECT_TYPE(DWORD(_PerfData) + _PerfData.HeaderLength);
for i := 1 to _PerfData.NumObjectTypesdo
begin
if _POT.ObjectNameTitleIndex = Processor_IDX then
Break;
_POT := PPERF_OBJECT_TYPE(DWORD(_POT) + _POT.TotalByteLength);
end;

// Check for success
if _POT.ObjectNameTitleIndex <> Processor_IDX then
raise Exception.Create('Unable to locate the "Processor" performance object');
if _ProcessorsCount < 0 then
begin
_ProcessorsCount:=_POT.NumInstances;
GetMem(_Counters,_ProcessorsCount*SizeOf(TInt64));
GetMem(_PrevCounters,_ProcessorsCount*SizeOf(TInt64));
end;

// Locate the "% CPU usage" counter definition
_PCD := PPERF_Counter_DEFINITION(DWORD(_POT) + _POT.HeaderLength);
for i := 1 to _POT.NumCountersdo
begin
if _PCD.CounterNameTitleIndex = CPUUsageIDX then
break;
_PCD := PPERF_COUNTER_DEFINITION(DWORD(_PCD) + _PCD.ByteLength);
end;

// Check for success
if _PCD.CounterNameTitleIndex <> CPUUsageIDX then
raise Exception.Create('Unable to locate the "% of CPU usage" performance counter');
// Collecting coutners
_PID_Instance := PPERF_INSTANCE_DEFINITION(DWORD(_POT) + _POT.DefinitionLength);
for i := 0 to _ProcessorsCount-1do
begin
_PCB_Instance := PPERF_COUNTER_BLOCK(DWORD(_PID_Instance) + _PID_Instance.ByteLength );
_PrevCounters:=_Counters;
_Counters:=FInt64(PInt64(DWORD(_PCB_Instance) + _PCD.CounterOffset)^);
_PID_Instance := PPERF_INSTANCE_DEFINITION(DWORD(_PCB_Instance) + _PCB_Instance.ByteLength);
end;

_PrevSysTime:=_SysTime;
SystemTimeToFileTime(_PerfData.SystemTime, ST);
_SysTime:=FInt64(TInt64(ST));
end else
begin
if not _W9xCollecting then
begin
R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StartStat', 0, KEY_ALL_ACCESS, H );
if R <> ERROR_SUCCESS then
raise Exception.Create('Unable to start performance monitoring');
dwDataSize:=sizeof(DWORD);
RegQueryValueEx( H, 'KERNEL/CPUUsage', nil, @dwType, PBYTE(@_W9xCpuUsage), @dwDataSize );
RegCloseKey(H);
R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StatData', 0,KEY_READ, _W9xCpuKey );
if R <> ERROR_SUCCESS then
raise Exception.Create('Unable to read performance data');
_W9xCollecting:=True;
end;

dwDataSize:=sizeof(DWORD);
RegQueryValueEx( _W9xCpuKey, 'KERNEL/CPUUsage', nil,@dwType, PBYTE(@_W9xCpuUsage), @dwDataSize );
end;
end;

initialization
_ProcessorsCount:= -1;
_BufferSize:= $2000;
_PerfData := AllocMem(_BufferSize);
VI.dwOSVersionInfoSize:=SizeOf(VI);
if not GetVersionEx(VI) then
raise Exception.Create('Can''t get the Windows version');
_IsWinNT := VI.dwPlatformId = VER_PLATFORM_WIN32_NT;
finalization
ReleaseCPUData;
FreeMem(_PerfData);
end.

 
其二:
问题提出/摘要:
从注册表获取如活动进程数、CPU占用率等信息?

回答:
在注册表中,有一个分支叫HKEY_DYN_DATA,大量的系统信息都保存在此分支下的PerfStats主键中。

为了获取如CPU的使用率等信息,你需要做三件事:

第一步是开始选择的计数器,可以通过读取相应的注册表主键完成。

例如:
如果我们想知道CPU的占用率,你可以读HKEY_DYN_DATA的这个主键
'PerfStats/StartStat/KERNEL/CPUusage'
这一步是开始计数器

下一步是是读取'PerfStats/StatData/KERNEL/CPUusage'的值,它的值给出了当前CPU的使用率。如果我们将代码放在一个Timer的事件中,我们就可以看到动态的变化了。

最后一步是停止计数器,通过读取注册表的'PerfStats/StopStat/KERNEL/CPUusage'键完成。

除CPU的占用率外,这里还有很多其它的计数器。你可以打开用注册表编辑器打开PerfStats/StatData键,可以看到全部的列表。

我生成了一个构件来获取注册表HKEY_DYN_DATA分支提供的所有计数的值。以下是代码:

unit SystemInfo;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,extctrls;

type TDialupAdapterInfo = record //Information of Dialup adapter
alignment:dword;
buffer:dword;
bytesrecieved:dword;
bytesXmit:dword;
ConnectSpeed:dword;
CRC:dword;
framesrecieved:dword;
FramesXmit:dword;
Framing:dword;
runts:dword;
Overrun:dword;
timeout:dword;
totalbytesrecieved:dword;
totalbytesXmit:dword;
end;


type TKernelInfo = record
CpuUsagePcnt:dword;
Numthreads:dword;
NumVMS:dword;
end;


type TVCACHEInfo = record
ccurpages:dword;
cMacPages:dword;
cminpages:dword;
FailedRecycles:dword;
Hits:dword;
LRUBuffers:dword;
LRURecycles:dword;
Misses:dword;
RandomRecycles:dword;
end;


type TFATInfo = record
BreadsSec:dword;
BwritesSec:dword;
Dirtydata:dword;
ReadsSec:dword;
WritesSec:dword;
end;


type TVMMInfo = record
CDiscards:dword;
CInstancefaults:dword;
CPageFaults:dword;
cPageIns:dword;
cPageOuts:dword;
cpgCommit:dword;
cpgDiskCache:dword;
cpgDiskCacheMac:dword;
cpgDiskCacheMid:dword;
cpgDiskCacheMin:dword;
cpgfree:dword;

cpglocked:dword;
cpglockedNoncache:dword;
cpgother:dword;
cpgsharedpages:dword;
cpgswap:dword;
cpgswapfile:dword;
cpgswapfiledefective:dword;
cpgswapfileinuse:dword;
end;


type
TSysInfo = class(TComponent)
private
fDialupAdapterInfo:TDialupAdapterInfo;
fKernelInfo:TKernelInfo;
fVCACHEInfo:TVCACHEInfo;
fFATInfo:TFATInfo;
fVMMInfo:TVMMInfo;
ftimer:TTimer;
fupdateinterval:integer;
tmp:dword;
vsize:dword;
pkey:hkey;
regtype:pdword;
fstopped:boolean;
procedure fupdatinginfo(sender:tobject);
procedure fsetupdateinterval(aupdateinterval:integer);
protected
fsysInfoChanged:TNotifyEvent;
public
constructor Create(Aowner:Tcomponent);override;
destructor Destroy;override;

property DialupAdapterInfo: TDialupAdapterInfo read fDialupAdapterInfo;
property KernelInfo: TKernelInfo read fKernelInfo;
property VCACHEInfo: TVCACHEInfo read fVCACHEInfo;
property FATInfo: TFATInfo read fFATInfo;
property VMMInfo: TVMMInfo read fVMMInfo;
procedure StartRecievingInfo;
procedure StopRecievingInfo;
published
property SysInfoChanged:TNotifyEvent read fsysInfoChanged write
fsysInfoChanged;//this event is called after a specific interval.
property UpdateInterval:integer read fupdateInterval write
fsetupdateinterval default 5000;
end;


procedure Register;

implementation

constructor TSysInfo.Create(Aowner:Tcomponent);
begin

inherited;
ftimer:=ttimer.Create(self);
ftimer.enabled:=false;
ftimer.OnTimer:=fupdatinginfo;
vsize:=4;
fstopped:=true;
end;


procedure TSysInfo.startrecievingInfo;
var
res:integer;
begin

res:=RegOpenKeyEx(HKEY_DYN_DATA,'PerfStats/StartStat',0,KEY_ALL_ACCESS,pkey);
if res<>0 then

raise exception.Create('Could not open registry key');
fstopped:=false;
// For Dial Up Adapter
RegQueryValueEx(pkey,'Dial-Up Adapter/Alignment',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Buffer',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Framing',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Overrun ',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Timeout',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/CRC',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Runts',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/FramesXmit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/FramesRecvd',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesRecvd',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesXmit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesRecvd',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/ConnectSpeed',nil,regtype,@tmp,@vsize);

// For VCACHE
RegQueryValueEx(pkey,'VCACHE/LRUBuffers',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/FailedRecycles',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/RandomRecycles',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/LRURecycles',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/Misses',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/Hits',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/cMacPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/cMinPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/cCurPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize);

//For VFAT

RegQueryValueEx(pkey,'VFAT/DirtyData',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/BReadsSec',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/BWritesSec',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/ReadsSec',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/WritesSec',nil,regtype,@tmp,@vsize);
//For VMM

RegQueryValueEx(pkey,'VMM/cpgLockedNoncache',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgCommit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSharedPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMid',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMac',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMin',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcache',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfileDefective',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfileInUse',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfile',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cDiscards',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cPageOuts',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cPageIns',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cInstanceFaults',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cPageFaults',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgOther',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwap',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgLocked',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgFree',nil,regtype,@tmp,@vsize);
//For KERNEL
RegQueryValueEx(pkey,'KERNEL/CPUUsage',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'KERNEL/VMs',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'KERNEL/Threads',nil,regtype,@tmp,@vsize);
RegCloseKey(pkey);
ftimer.enabled:=true;
end;


procedure tsysinfo.fupdatinginfo(sender:tobject);
var
res:integer;
begin

res:=RegOpenKeyEx(HKEY_DYN_DATA,'PerfStats/StatData',0,KEY_ALL_ACCESS,pkey);
if res<>0 then

raise exception.Create('Could not open registry key');
//For Dial Up Adapter
RegQueryValueEx(pkey,'Dial-Up Adapter/Alignment',nil,regtype,@fDialupAdapterInfo.alignment,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Buffer',nil,regtype,@fDialupAdapterInfo.buffer,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Framing',nil,regtype,@fDialupAdapterInfo.framing,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Overrun ',nil,regtype,@fDialupAdapterInfo.overrun,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Timeout',nil,regtype,@fDialupAdapterInfo.timeout,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/CRC',nil,regtype,@fDialupAdapterInfo.crc,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Runts',nil,regtype,@fDialupAdapterInfo.runts,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/FramesXmit',nil,regtype,@fDialupAdapterInfo.framesxmit,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/FramesRecvd',nil,regtype,@fDialupAdapterInfo.framesrecieved,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@fDialupAdapterInfo.bytesxmit,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesRecvd',nil,regtype,@fDialupAdapterInfo.bytesrecieved,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesXmit',nil,regtype,@fDialupAdapterInfo.totalbytesxmit,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesRecvd',nil,regtype,@fDialupAdapterInfo.totalbytesrecieved,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/ConnectSpeed',nil,regtype,@fDialupAdapterInfo.connectspeed,@vsize);
// For VCACHE
RegQueryValueEx(pkey,'VCACHE/LRUBuffers',nil,regtype,@fVCACHEInfo.lrubuffers,@vsize);
RegQueryValueEx(pkey,'VCACHE/FailedRecycles',nil,regtype,@fVCACHEInfo.failedrecycles,@vsize);
RegQueryValueEx(pkey,'VCACHE/RandomRecycles',nil,regtype,@fVCACHEInfo.randomrecycles,@vsize);
RegQueryValueEx(pkey,'VCACHE/LRURecycles',nil,regtype,@fVCACHEInfo.lrurecycles,@vsize);
RegQueryValueEx(pkey,'VCACHE/Misses',nil,regtype,@fVCACHEInfo.misses,@vsize);
RegQueryValueEx(pkey,'VCACHE/Hits',nil,regtype,@fVCACHEInfo.hits,@vsize);
RegQueryValueEx(pkey,'VCACHE/cMacPages',nil,regtype,@fVCACHEInfo.cmacpages,@vsize);
RegQueryValueEx(pkey,'VCACHE/cMinPages',nil,regtype,@fVCACHEInfo.cminpages,@vsize);
RegQueryValueEx(pkey,'VCACHE/cCurPages',nil,regtype,@fVCACHEInfo.ccurpages,@vsize);
//For VFAT
RegQueryValueEx(pkey,'VFAT/DirtyData',nil,regtype,@ffatinfo.dirtydata,@vsize);
RegQueryValueEx(pkey,'VFAT/BReadsSec',nil,regtype,@ffatinfo.breadssec,@vsize);
RegQueryValueEx(pkey,'VFAT/BWritesSec',nil,regtype,@ffatinfo.bwritessec,@vsize);
RegQueryValueEx(pkey,'VFAT/ReadsSec',nil,regtype,@ffatinfo.readssec,@vsize);
RegQueryValueEx(pkey,'VFAT/WritesSec',nil,regtype,@ffatinfo.writessec,@vsize);
//For VMM
RegQueryValueEx(pkey,'VMM/cpgLockedNoncache',nil,regtype,@fvmminfo.cpglockednoncache,@vsize);
RegQueryValueEx(pkey,'VMM/cpgCommit',nil,regtype,@fvmminfo.cpgcommit,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSharedPages',nil,regtype,@fvmminfo.cpgsharedpages,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMid',nil,regtype,@fvmminfo.cpgdiskcacheMid,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMac',nil,regtype,@fvmminfo.cpgdiskcacheMac,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMin',nil,regtype,@fvmminfo.cpgdiskcacheMin,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcache',nil,regtype,@fvmminfo.cpgdiskcache,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfileDefective',nil,regtype,@fvmminfo.cpgswapfiledefective,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfileInUse',nil,regtype,@fvmminfo.cpgswapfileinuse,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfile',nil,regtype,@fvmminfo.cpgswapfile,@vsize);
RegQueryValueEx(pkey,'VMM/cDiscards',nil,regtype,@fvmminfo.cdiscards,@vsize);
RegQueryValueEx(pkey,'VMM/cPageOuts',nil,regtype,@fvmminfo.cpageouts,@vsize);
RegQueryValueEx(pkey,'VMM/cPageIns',nil,regtype,@fvmminfo.cpageins,@vsize);
RegQueryValueEx(pkey,'VMM/cInstanceFaults',nil,regtype,@fvmminfo.cinstancefaults,@vsize);
RegQueryValueEx(pkey,'VMM/cPageFaults',nil,regtype,@fvmminfo.cpagefaults,@vsize);
RegQueryValueEx(pkey,'VMM/cpgOther',nil,regtype,@fvmminfo.cpgother,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwap',nil,regtype,@fvmminfo.cpgswap,@vsize);
RegQueryValueEx(pkey,'VMM/cpgLocked',nil,regtype,@fvmminfo.cpglocked,@vsize);
RegQueryValueEx(pkey,'VMM/cpgFree',nil,regtype,@fvmminfo.cpgfree,@vsize);
//For KERNEL
RegQueryValueEx(pkey,'KERNEL/CPUUsage',nil,regtype,@fkernelinfo.cpuusagepcnt,@vsize);
RegQueryValueEx(pkey,'KERNEL/VMs',nil,regtype,@fkernelinfo.numvms,@vsize);
RegQueryValueEx(pkey,'KERNEL/Threads',nil,regtype,@fkernelinfo.numThreads,@vsize);
RegCloseKey(pkey);
if assigned(SysInfoChanged) then

SysInfoChanged(self);
end;


procedure TSysInfo.stoprecievingInfo;
var
res:integer;
begin

res:=RegOpenKeyEx(HKEY_DYN_DATA,'PerfStats/StopStat',0,KEY_ALL_ACCESS,pkey);
if not fstopped then

begin

if res<>0 then

raise exception.Create('Could not open registry key');
//For Dial Up Adapter
RegQueryValueEx(pkey,'Dial-Up Adapter/Alignment',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Buffer',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Framing',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Overrun ',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Timeout',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/CRC',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/Runts',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/FramesXmit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/FramesRecvd',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesRecvd',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesXmit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesRecvd',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/ConnectSpeed',nil,regtype,@tmp,@vsize);

// For VCACHE
RegQueryValueEx(pkey,'VCACHE/LRUBuffers',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/FailedRecycles',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/RandomRecycles',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/LRURecycles',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/Misses',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/Hits',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/cMacPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/cMinPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VCACHE/cCurPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize);

//For VFAT
RegQueryValueEx(pkey,'VFAT/DirtyData',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/BReadsSec',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/BWritesSec',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/ReadsSec',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VFAT/WritesSec',nil,regtype,@tmp,@vsize);

//For VMM
RegQueryValueEx(pkey,'VMM/cpgLockedNoncache',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgCommit',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSharedPages',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMid',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMac',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcacheMin',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgDiskcache',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfileDefective',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfileInUse',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwapfile',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cDiscards',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cPageOuts',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cPageIns',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cInstanceFaults',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cPageFaults',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgOther',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgSwap',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgLocked',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'VMM/cpgFree',nil,regtype,@tmp,@vsize);

//For KERNEL
RegQueryValueEx(pkey,'KERNEL/CPUUsage',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'KERNEL/VMs',nil,regtype,@tmp,@vsize);
RegQueryValueEx(pkey,'KERNEL/Threads',nil,regtype,@tmp,@vsize);

RegCloseKey(pkey);
ftimer.enabled:=false;
fstopped:=true;
end;

end;


procedure tsysinfo.fsetupdateinterval(aupdateinterval:integer);
begin

if (ftimer<>nil) and(aupdateinterval>0) then

begin

ftimer.Interval:=aupdateinterval;
fupdateinterval:=aupdateinterval;
end;

if (ftimer<>nil) and(aupdateinterval=0) then

begin

ftimer.Interval:=500;
fupdateinterval:=500;
end;


end;


destructor tsysinfo.Destroy;
begin

StopRecievingInfo;
ftimer.Destroy;
inherited;
end;


procedure Register;
begin

RegisterComponents('Samples', [TSysInfo]);
end;

 
soFTangeL:2000的注册表里好像没有HKEY_DYN_DATA的分支,还有我是想得到我的一个
程序里有多个线程,每一个线程的CPU占用率?
 
★★★获取线程时间:
在NT上可以用 GetThreadTimes API函数获取线程时间,其定义为:
function GetThreadTimes(hThread: THandle;
var lpCreationTime, lpExitTime, lpKernelTime, lpUserTime: TFileTime): BOOL;
stdcall;
hThread参数是线程的句柄,其他参数都是变参,由GetThreadTimes()函数返回它们的值。其中:
. lpCreationTime 线程创建的时间。
. lpExitTime 线程退出的时间。如果线程还在执行,此值无意义。
. lpKernelTime 执行操作系统代码所用的时间。
. lpUserTime 执行应用程序本身代码所用的时间。
以上四个参数都是TFileTime类型。
下面的函数把TFileTime 转换为TDateTime:
function FileTimeToDateTime(FileTime: TFileTime): TDateTime;
var
SysTime: TSystemTime;
begin
if not FileTimeToSystemTime(FileTime, SysTime) then
raise Exception.CreateFmt('FileTimeToSystemTime failed. ' +
'Error code %d', [GetLastError]);
with SysTimedo
Result := EncodeDate(SysTime.wYear, SysTime.wMonth, SysTime.wDay) +
EncodeTime(SysTime.wHour, SysTime.wMinute, SysTime.wSecond, SysTime.wMilliseconds);
end;

在95/98下好像没办法。
 
后退
顶部