summaryrefslogtreecommitdiff
path: root/systemunit.pas
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2015-07-10 15:10:15 +0200
committerErich Eckner <git@eckner.net>2015-07-10 15:20:53 +0200
commitffb35ffece4f0239cf9fcecbfba5329d7b5dc98d (patch)
treebc07a061ee40ed18106cd5a13528d074d1228960 /systemunit.pas
parentaddd58de0c9311f791355231d53e4b280ff3537d (diff)
downloadunits-ffb35ffece4f0239cf9fcecbfba5329d7b5dc98d.tar.xz
neue Dateien: matheunit.pas, mlockunit.pas, mystringlistunit.pas,
pseudohadamard.pas, randomunit.pas, systemunit.pas
Diffstat (limited to 'systemunit.pas')
-rw-r--r--systemunit.pas123
1 files changed, 123 insertions, 0 deletions
diff --git a/systemunit.pas b/systemunit.pas
new file mode 100644
index 0000000..645f09e
--- /dev/null
+++ b/systemunit.pas
@@ -0,0 +1,123 @@
+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;
+
+implementation
+
+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 begin
+ delete(s,1,4);
+ delete(s,length(s)-2,3);
+ s:=trim(s);
+ result:=result+strtoint(s);
+ end;
+ 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;
+
+begin
+ _cpuLastUsed:=0;
+ _cpuLastIdle:=0;
+ cpuUtilization;
+end.
+