请问,有没有可以获取cpu信息的windows api?比如,cpu占有率,cpu空闲时间等(100分)

  • 主题发起人 主题发起人 bobbycai
  • 开始时间 开始时间
<br>D:/Program Files/Borland/Borland Shared/MSHelp/pdh.hlp<br>慢慢看...
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=565450
 
getsysteminfo
 
在注册表中<br>"HKEY_LOCAL_MACHINE/hardware/DESCRIPTION/System/CentralProcessor/0"<br>对应VendorIndentifier的值 <br><br>program CpuSpeed;<br>uses SysUtils, Windows, Dialogs;<br>function GetCpuSpeed: Comp;<br>var<br>&nbsp; t: DWORD;<br>&nbsp; mhi, mlo, nhi, nlo: DWORD;<br>&nbsp; t0, t1, chi, clo, shr32: Comp;<br>begin<br>&nbsp; shr32 := 65536;<br>&nbsp; shr32 := shr32 * 65536;<br>&nbsp; t := GetTickCount;<br>&nbsp; while t = GetTickCount do begin end;<br>&nbsp; asm<br>&nbsp; &nbsp; DB 0FH<br>&nbsp; &nbsp; DB 031H<br>&nbsp; &nbsp; mov mhi,edx<br>&nbsp; &nbsp; mov mlo,eax<br>&nbsp; end;<br>&nbsp; while GetTickCount &lt; (t + 1000) do begin end;<br>&nbsp; asm<br>&nbsp; &nbsp; DB 0FH<br>&nbsp; &nbsp; DB 031H<br>&nbsp; &nbsp; mov nhi,edx<br>&nbsp; &nbsp; mov nlo,eax<br>&nbsp; end;<br>&nbsp; chi := mhi; if mhi &lt; 0 then chi := chi + shr32;<br>&nbsp; clo := mlo; if mlo &lt; 0 then clo := clo + shr32;<br>&nbsp; t0 := chi * shr32 + clo;<br>&nbsp; chi := nhi; if nhi &lt; 0 then chi := chi + shr32;<br>&nbsp; clo := nlo; if nlo &lt; 0 then clo := clo + shr32;<br>&nbsp; t1 := chi * shr32 + clo;<br>&nbsp; Result := (t1 - t0) / 1E6;<br>end;<br>begin<br>&nbsp; MessageDlg(Format('%.1f MHz', [GetCpuSpeed]), mtConfirmation, [mbOk], 0);<br>end.<br><br><br><br><br>--------------------------------------------------------------------------------<br><br><br>function GetCPUSpeed: Double;<br>const<br>&nbsp; DelayTime = 500; // measure time in ms<br>var<br>&nbsp; TimerHi, TimerLo: DWORD;<br>&nbsp; PriorityClass, Priority: Integer;<br>begin<br>&nbsp; PriorityClass := GetPriorityClass(GetCurrentProcess);<br>&nbsp; Priority := GetThreadPriority(GetCurrentThread);<br>&nbsp;<br>&nbsp; SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);<br>&nbsp; SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);<br>&nbsp;<br>&nbsp; Sleep(10);<br>&nbsp; asm<br>&nbsp; dw 310Fh // rdtsc<br>&nbsp; mov TimerLo, eax<br>&nbsp; mov TimerHi, edx<br>&nbsp; end;<br>&nbsp; Sleep(DelayTime);<br>&nbsp; asm<br>&nbsp; dw 310Fh // rdtsc<br>&nbsp; sub eax, TimerLo<br>&nbsp; sbb edx, TimerHi<br>&nbsp; mov TimerLo, eax<br>&nbsp; mov TimerHi, edx<br>&nbsp; end;<br>&nbsp;<br>&nbsp; SetThreadPriority(GetCurrentThread, Priority);<br>&nbsp; SetPriorityClass(GetCurrentProcess, PriorityClass);<br>&nbsp;<br><br>&nbsp; Result := TimerLo / (1000.0 * DelayTime);<br>end;<br>&nbsp;<br><br><br><br><br>--------------------------------------------------------------------------------<br><br>function RDTSC : Int64; assembler;<br>asm<br>&nbsp; db $0F, $31 // opcode for RDTSC<br>end;<br><br>function RDQPC : Int64;<br>begin<br>&nbsp; QueryPerformanceCounter(result);<br>end;<br><br>function CPUSpeed : Integer;<br>var<br>&nbsp; f,tsc,pc : Int64;<br>begin<br>&nbsp; if QueryPerformanceFrequency(f) then<br>&nbsp; begin<br>&nbsp; Sleep(0);<br>&nbsp; pc := RDQPC;<br>&nbsp; tsc := RDTSC;<br>&nbsp; Sleep(100);<br>&nbsp; pc := RDQPC-pc;<br>&nbsp; tsc := RDTSC-tsc;<br>&nbsp; result := round(tsc*f/(pc*1000000));<br>&nbsp; end<br>&nbsp; else<br>&nbsp; result := -1;<br>end;<br><br><br><br><br>--------------------------------------------------------------------------------<br><br><br>&nbsp;<br><br>&nbsp; 由于使用RDTSC操作码,因此以下函数只能返回Pentium或者更高芯片的速度。另一种解决方法是使用WinAPI中的QueryPerformanceCounter或者QueryPerformanceFrequency函数。<br><br>&nbsp; <br><br>&nbsp; function GetCPUSpeed: real; <br><br>&nbsp; <br><br>&nbsp; function IsCPUID_Available: Boolean; assembler; register; <br><br>&nbsp; asm <br><br>&nbsp; PUSHFD { direct access to flags not possible, only via stack } <br><br>&nbsp; POP EAX { flags to EAX } <br><br>&nbsp; MOV EDX,EAX { save current flags } <br><br>&nbsp; XOR EAX,$200000 { not ID bit } <br><br>&nbsp; PUSH EAX { onto stack } <br><br>&nbsp; POPFD { from stack to flags, with not ID bit } <br><br>&nbsp; PUSHFD { back to stack } <br><br>&nbsp; POP EAX { get back to EAX } <br><br>&nbsp; XOR EAX,EDX { check if ID bit affected } <br><br>&nbsp; JZ @exit { no, CPUID not availavle } <br><br>&nbsp; MOV AL,True { Result=True } <br><br>&nbsp; @exit: <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; function hasTSC: Boolean; <br><br>&nbsp; var <br><br>&nbsp; Features: Longword; <br><br>&nbsp; begin <br><br>&nbsp; asm <br><br>&nbsp; MOV Features,0 { Features = 0 } <br><br>&nbsp; <br><br>&nbsp; PUSH EBX <br><br>&nbsp; XOR EAX,EAX <br><br>&nbsp; DW $A20F <br><br>&nbsp; POP EBX <br><br>&nbsp; <br><br>&nbsp; CMP EAX,$01 <br><br>&nbsp; JL @Fail <br><br>&nbsp; <br><br>&nbsp; XOR EAX,EAX <br><br>&nbsp; MOV EAX,$01 <br><br>&nbsp; PUSH EBX <br><br>&nbsp; DW $A20F <br><br>&nbsp; MOV Features,EDX <br><br>&nbsp; POP EBX <br><br>&nbsp; @Fail: <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; hasTSC := (Features and $10) &lt;&gt; 0; <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; const <br><br>&nbsp; DELAY = 500; <br><br>&nbsp; var <br><br>&nbsp; TimerHi, TimerLo: Integer; <br><br>&nbsp; PriorityClass, Priority: Integer; <br><br>&nbsp; begin <br><br>&nbsp; Result := 0; <br><br>&nbsp; if not (IsCPUID_Available and hasTSC) then Exit; <br><br>&nbsp; PriorityClass := GetPriorityClass(GetCurrentProcess); <br><br>&nbsp; Priority := GetThreadPriority(GetCurrentThread); <br><br>&nbsp; <br><br>&nbsp; SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS); <br><br>&nbsp; SetThreadPriority(GetCurrentThread, <br><br>&nbsp; THREAD_PRIORITY_TIME_CRITICAL); <br><br>&nbsp; <br><br>&nbsp; SleepEx(10, FALSE); <br><br>&nbsp; <br><br>&nbsp; asm <br><br>&nbsp; DB $0F { $0F31 操作码是Pentium指令集中的RDTSC } <br><br>&nbsp; DB $31 { 返回64位整数 } <br><br>&nbsp; MOV TimerLo,EAX <br><br>&nbsp; MOV TimerHi,EDX <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; SleepEx(DELAY, FALSE); <br><br>&nbsp; <br><br>&nbsp; asm <br><br>&nbsp; DB $0F { $0F31 操作码是Pentium指令集中的RDTSC } <br><br>&nbsp; DB $31 { 返回64位整数 } <br><br>&nbsp; SUB EAX,TimerLo <br><br>&nbsp; SBB EDX,TimerHi <br><br>&nbsp; MOV TimerLo,EAX <br><br>&nbsp; MOV TimerHi,EDX <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; SetThreadPriority(GetCurrentThread, Priority); <br><br>&nbsp; SetPriorityClass(GetCurrentProcess, PriorityClass); <br><br>&nbsp; Result := TimerLo / (1000 * DELAY); <br><br>&nbsp; end; <br><br>&nbsp; <br>
 
后退
顶部