-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stats.CPU.pas
155 lines (141 loc) · 3.45 KB
/
Stats.CPU.pas
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 Stats.CPU;
interface
uses
System.Classes, System.SysUtils, Linux.Utils, System.JSON.Serializers;
type
TLoadAVG = record
[JSONName('Minute')]
F1minute : Double;
[JSONName('FiveMinutes')]
F5minute : Double;
[JSONName('FiftenMinutes')]
F15minute: Double;
end;
TFrequency = array of Double;
TCPUInfo = class
private
[JSONName('Model')]
FModel: string;
[JSONName('Processors')]
FProcessors: Integer;
[JSONName('LoadAVG')]
FLoadAVG: TLoadAVG;
[JSONName('Frequency')]
FFrequency: TFrequency;
[JSONName('CacheSize')]
FCacheSize: String;
procedure GetModel;
procedure GetProcessors;
procedure GetCacheSize;
procedure GetFrequency;
procedure GetLoadAvg;
public
property LoadAVG : TLoadAVG read FLoadAVG;
property Processors : Integer read FProcessors;
property Model : string read FModel;
property Frequency : TFrequency read FFrequency;
property CacheSize : String read FCacheSize;
constructor Create;
end;
implementation
{ TCPUInfo }
constructor TCPUInfo.Create;
begin
GetModel;
GetProcessors;
GetCacheSize;
GetFrequency;
GetLoadAvg;
end;
procedure TCPUInfo.GetCacheSize;
var
Cmd : String;
begin
try
Cmd := 'cat /proc/cpuinfo | grep -i "^cache size" | awk -F": " ''{print $2}'' | head -1';
TLinuxUtils.RunCommand(Cmd,FCacheSize);
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TCPUInfo.GetCacheSize - ' + E.Message);
{$ENDIF}
end;
end;
end;
procedure TCPUInfo.GetFrequency;
var
Cmd: String;
CmdResult : TStringList;
I: Integer;
begin
try
Cmd := 'cat /proc/cpuinfo | grep -i "^cpu MHz" | awk -F": " ''{print $2}''';
CmdResult := TLinuxUtils.RunCommandLine(Cmd);
try
SetLength(FFrequency,CmdResult.Count);
for I := 0 to Pred(CmdResult.Count) do
begin
FFrequency[I] := Round(StrToFloatDef(CmdResult[I].Replace('.',','),0));
end;
finally
CmdResult.Free;
end;
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TCPUInfo.GetFrequency - ' + E.Message);
{$ENDIF}
end;
end;
end;
procedure TCPUInfo.GetLoadAvg;
var
Cmd, CmdResult:String;
begin
try
Cmd := 'cat /proc/loadavg';
TLinuxUtils.RunCommand(Cmd,CmdResult);
FLoadAVG.F1minute := StrToFloatDef(CmdResult.Split([' '])[0].Replace('.',','),0);
FLoadAVG.F5minute := StrToFloatDef(CmdResult.Split([' '])[1].Replace('.',','),0);
FLoadAVG.F15minute := StrToFloatDef(CmdResult.Split([' '])[2].Replace('.',','),0);
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TCPUInfo.GetLoadAvg - ' + E.Message);
{$ENDIF}
end;
end;
end;
procedure TCPUInfo.GetModel;
var
Cmd : String;
begin
try
Cmd := 'cat /proc/cpuinfo | grep -i "^model name" | awk -F": " ''{print $2}'' | head -1 | sed ''s/ \+/ /g''';
TLinuxUtils.RunCommand(Cmd,FModel);
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TCPUInfo.GetModel - ' + E.Message);
{$ENDIF}
end;
end;
end;
procedure TCPUInfo.GetProcessors;
begin
try
FProcessors := System.CPUCount;
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TCPUInfo.GetProcessors - ' + E.Message);
{$ENDIF}
end;
end;
end;
end.