Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi CPU-Auslastung (https://www.delphipraxis.net/8736-cpu-auslastung.html)

Dax 11. Sep 2003 06:22


CPU-Auslastung
 
Wie kann ich die Auslastung der CPU herausfinden, so wie es die RAM_Räumer machen?

Luckie 11. Sep 2003 07:02

Re: CPU-Auslastung
 
Geht zuverlässing nur unter NT ff. Systemen.

Delphi-Quellcode:
var
  Form1: TForm1;
  nOldIdleTime: Int64 = 0;
  nOldSystemTime : INT64 = 0;
  nNewCPUTime   : ULONG = 0;

const
  SYS_BASIC_INFO           = 0;
  SYS_PERFORMANCE_INFO     = 2;
  SYS_TIME_INFO            = 3;

type
  SYSTEM_BASIC_INFORMATION = packed record
    dwUnknown1              : DWORD;
    uKeMaximumIncrement    : ULONG;
    uPageSize              : ULONG;
    uMmNumberOfPhysicalPages: ULONG;
    uMmLowestPhysicalPage  : ULONG;
    uMmHighestPhysicalPage : ULONG;
    uAllocationGranularity : ULONG;
    pLowestUserAddress     : POINTER;
    pMmHighestUserAddress  : POINTER;
    uKeActiveProcessors    : POINTER;
    bKeNumberProcessors    : BYTE;
    bUnknown2               : BYTE;
    wUnknown3               : WORD;
  end;

  SYSTEM_PERFORMANCE_INFORMATION = packed record
    nIdleTime              : INT64;
    dwSpare                : array[0..75]of DWORD;
  end;

  SYSTEM_TIME_INFORMATION = packed record
    nKeBootTime            : INT64;
    nKeSystemTime          : INT64;
    nExpTimeZoneBias       : INT64;
    uCurrentTimeZoneId     : ULONG;
    dwReserved             : DWORD;
  end;

  function NTQuerySystemInformation(SystemInformationClass: Longint;
                                    SystemInformation: Pointer;
                                    SystemInformationLength: Longint;
                                    ReturnLength: Longint): Longint; stdcall;
                                    external 'ntdll.dll' name 'NtQuerySystemInformation';

implementation

{$R *.dfm}


function GetCPUUsage: Integer;
var
  spi : SYSTEM_PERFORMANCE_INFORMATION;
  sti : SYSTEM_TIME_INFORMATION;
  sbi : SYSTEM_BASIC_INFORMATION;
begin
  if (NTQuerySystemInformation(SYS_BASIC_INFO, @sbi, sizeof(SYSTEM_BASIC_INFORMATION), 0) = NO_ERROR) then
  begin
    if (NTQuerySystemInformation(SYS_TIME_INFO, @sti, sizeof(SYSTEM_TIME_INFORMATION), 0) = NO_ERROR) then
    if (NTQuerySystemInformation(SYS_PERFORMANCE_INFO, @spi, sizeof(SYSTEM_PERFORMANCE_INFORMATION), 0)= NO_ERROR) then
    begin
      if (nOldIdleTime <> 0) then
      begin
        nNewCPUTime:= trunc(100-((spi.nIdleTime-nOldIdleTime)/(sti.nKeSystemTime-nOldSystemTime)*100)/sbi.bKeNumberProcessors+0.5);
        if (nNewCPUTime <> nOldIdleTime) then
        begin
          Result := nNewCPUTIME;
        end;
      end;
      nOldIdleTime  := spi.nIdleTime;
      nOldSystemTime := sti.nKeSystemTime;
    end;
  end;
end;

Alexander 11. Sep 2003 15:03

Re: CPU-Auslastung
 
Eine Suche hätte ähnliches ergeben ;-)
Ich glaube das steht sogar in der Code-Lib hier, und wenn nicht, sollte es schnellstens reingesetzt werden ;-)

Dax 12. Sep 2003 06:24

Geht das auch irgendwie anders, z.B. für 9x-Systeme?

Alexander 12. Sep 2003 15:36

Re: CPU-Auslastung
 
Ja.
Ich habe mal eine Unit gepostet. Muss mal suchen, ich glaube in einem Thread über ein Programm von Matze
Allerdings stimmt sie nciht 100%zig

Matze 12. Sep 2003 15:39

Re: CPU-Auslastung
 
Hier ist dieser Link! :D

http://www.delphipraxis.net/internal...ect.php?t=8556

Alexander 12. Sep 2003 16:16

Re: CPU-Auslastung
 
genau die ;-)

Assarbad 15. Sep 2003 17:14

Re: CPU-Auslastung
 
"offene Frage"???


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz