summaryrefslogtreecommitdiff
path: root/systemunit.pas
blob: de0e699b3b06c422feefe322111e1ff6bb0d9665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
unit systemunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Math;

function cpuUtilization: extended;
function numCpus: int64;
function momentanFreieCpus: int64;
function belegterSpeicher: int64;
function minCache: int64;
function shellSubst(s: string): string;
function mkTemp(s: string): string;

implementation

uses
  process, lowlevelunit;

var _cpuLastUsed,_cpuLastIdle: int64;

function cpuUtilization: extended;
var
  procstat:  textfile;
  s:         string;
  used,idle: int64;
  i:         integer;
begin
  result:=0;
  s:='';
  assignfile(procstat,'/proc/stat');
  reset(procstat);
  while not eof(procstat) do begin
    readln(procstat,s);
    if pos('cpu ',s)=1 then break;
  end;
  closefile(procstat);
  if pos('cpu ',s)<>1 then exit;
  delete(s,1,pos(' ',s));
  s:=trim(s);
  used:=0;
  idle:=0;
  for i:=0 to 3 do begin
    used:=used+idle;
    idle:=strtoint(copy(s,1,pos(' ',s)-1));
    delete(s,1,pos(' ',s));
    s:=trim(s);
  end;
  result:=(used-_cpuLastUsed)/max(1,used-_cpuLastUsed + idle-_cpuLastIdle);
  _cpuLastUsed:=used;
  _cpuLastIdle:=idle;
end;

function numCpus: int64;
var
  procstat:  textfile;
  s:         string;
begin
  result:=0;
  s:='';
  assignfile(procstat,'/proc/stat');
  reset(procstat);
  while not eof(procstat) do begin
    readln(procstat,s);
    if (pos('cpu',s)=1) and
       (pos('cpu ',s)<>1) then
      inc(result);
  end;
  closefile(procstat);
end;

function momentanFreieCpus: int64;
begin
  result:=floor(numCpus*(1-cpuUtilization));
end;

function belegterSpeicher: int64;
var
  f:    textFile;
  s:    string;
begin
 s:='/proc/'+inttostr(getProcessId)+'/smaps';
 result:=0;
 if not fileexists(s) then exit;
 assignfile(f,s);
 reset(f);
 while not eof(f) do begin
   readln(f,s);
   if (leftStr(s,4)='Rss:') and (rightStr(s,3)=' kB') then
     result:=result+strtoint(trim(copy(s,5,length(s)-4-3)));
 end;
 closefile(f);
end;

function minCache: int64;
var
  f:    textFile;
  s:    string;
begin
 s:='/proc/cpuinfo';
 result:=0;
 if not fileexists(s) then exit;
 assignfile(f,s);
 reset(f);
 while not eof(f) do begin
   readln(f,s);
   if (leftStr(s,10)='cache size') and (rightStr(s,3)=' kB') then begin
     delete(s,1,pos(':',s));
     delete(s,length(s)-2,3);
     s:=trim(s);
     if result=0 then result:=strtoint(s)
     else result:=min(result,strtoint(s));
   end;
 end;
 closefile(f);
end;

function shellSubst(s: string): string;
var
  name: string;
begin
  result:=s;
  while pos('${',result)>0 do begin
    s:=leftStr(result,pos('${',result)-1);
    delete(result,1,pos('${',result)-1+length('${'));
    name:=leftStr(result,pos('}',result)-1);
    delete(result,1,length(name+'}'));
    result:=s+GetEnvironmentVariable(name)+result;
  end;
end;

function mkTemp(s: string): string;
var
  args: array of string;
begin
  setlength(args,0);
  while s<>'' do begin
    setlength(args,length(args)+1);
    args[length(args)-1]:=erstesArgument(s);
  end;
  result:='';
  if not runCommand('/usr/bin/mktemp',args,result) then
    raise exception.create('Fehler beim Ausführen von ''/usr/bin/mktemp '+s+'''!');
  result:=trim(result);
end;

begin
  _cpuLastUsed:=0;
  _cpuLastIdle:=0;
  cpuUtilization;
end.