Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The correct method of TProcessInfo.PerSystem in Linux #270

Closed
zen010101 opened this issue Jul 11, 2024 · 1 comment
Closed

The correct method of TProcessInfo.PerSystem in Linux #270

zen010101 opened this issue Jul 11, 2024 · 1 comment

Comments

@zen010101
Copy link
Contributor

Here is my edition:

function TProcessInfo.PerSystem(out Idle, Kernel, User: single): boolean;
var
  P: PUtf8Char;
  U, K, I, S: cardinal;
  U2, K2, I2, S2: cardinal;
begin
  // see http://www.linuxhowtos.org/System/procstat.htm
  result := false;
  P := pointer(StringFromFile('/proc/stat', {nosize=}true));
  if P = nil then
    exit;
  // e.g. 'cpu  3418147 18140 265232 6783435 12184 0 34219 0 0 0'
  U := GetNextCardinal(P){=user} + GetNextCardinal(P){=nice};
  K := GetNextCardinal(P){=system};
  I := GetNextCardinal(P){=idle};
  S := U + K + I;
  result := S <> 0;
  if not result then
    exit;

  sleep(200);

  P := pointer(StringFromFile('/proc/stat', {nosize=}true));
  U2 := GetNextCardinal(P){=user} + GetNextCardinal(P){=nice};
  K2 := GetNextCardinal(P){=system};
  I2 := GetNextCardinal(P){=idle};
  S2 := U2 + K2 + I2;

  Kernel := {%H-}SimpleRoundTo2Digits(((K2-K) * 100) / (S2-S));
  User   := {%H-}SimpleRoundTo2Digits(((U2-U) * 100) / (S2-S));
  Idle   := 100 - Kernel - User; // ensure sum is always 100%
end; { TODO : use a diff approach for TProcessInfo.PerSystem on Linux? }
@synopse
Copy link
Owner

synopse commented Jul 11, 2024

Using a Sleep() is not possible because it should be not blocking.

@synopse synopse closed this as completed Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants