-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.System.CPU.pas
361 lines (298 loc) · 8.96 KB
/
GS.System.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
///-------------------------------------------------------------------------------
/// Title : GS.CPUUsage
/// Short Desc : Got CPU Usage cross platform
/// Source : https://github.com/VincentGsell
/// Aim : - Win and Linux Delphi/FPC harmonized CPU access.
///-------------------------------------------------------------------------------
/// History
/// 20180929 - VGS - Added perfocounter for win.
/// Introduce simple thread safe monitoring function.
/// 20200630 - VGS - FMX Support. (Embarcadero Mobile mainly)
///
///-------------------------------------------------------------------------------
unit GS.System.CPU;
{$I GSCore.inc}
interface
{$IFDEF IOS or ANDROID}
{$DEFINE USE_FMX}
{$ENDIF}
{$IFDEF FPC}
Uses SysUtils, Classes, SyncObjs
{$IFDEF WINDOWS}
,
Windows,
Messages
{$ELSE}
,
baseunix,
Linux,
unix,
unixtype
{$ENDIF}
;
{$ENDIF}
{$IFDEF DCC}
Uses System.SysUtils, System.Classes, System.SyncObjs, System.SysConst, System.Types
{$IFDEF USE_FMX}
,FMX.Types
,FMX.Platform
{$ENDIF}
{$IFDEF WINDOWS}
,Windows
,Messages
{$ENDIF}
{$IFDEF POSIX}
,Posix.SysTypes,
Posix.UniStd,
Posix.Signal,
Posix.Dlfcn,
Posix.Fcntl,
Posix.SysStat,
Posix.SysTime,
Posix.Time,
Posix.Locale,
Posix.Pthread
{$ENDIF}
;
{$ENDIF}
Type
//TODO : Dig into GetSystemTimes to separate Kernel time for Linux, android and MacOSX.
TgsCPUUsage = Class
Private
fvPreviousST : TThread.TSystemTimes;
fPreviousST : TThread.TSystemTimes;
{$IFDEF FPC}
{$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
//For decoding /prog/cpuinfo
Lines, LineChunks: TStringList;
{$ENDIF}
{$ENDIF}
Protected
CurrentSystemTime,DeltaSystemTime : TThread.TSystemTimes;
//Based on delta.
DeltaTotalSystemTime, DeltaUsageSystemTime, DeltaKernelUsageSystemTime : UINT64;
Function InternalGetSystemTime(var aSystemTimes: TThread.TSystemTimes) : Boolean;
Function InternalGetCPUUsage(var PrevSystemTimes: TThread.TSystemTimes) : Integer;
Public
PureIdleTimePercent : Single;
PureUserTimePercent : Single;
PureKernelTimePercent : Single;
PureNiceTimePercent : Single;
UsageCPUPercentInt : Integer;
UsageCPUPercent,
KernelUsageCPUPercent : Single;
Constructor Create; Virtual;
Destructor Destroy; Override;
Procedure Update;
End;
{$IFDEF FPC}
{$IFDEF WINDOWS}
function GetSystemTimes(var lpIdleTime, lpKernelTime, lpUserTime: TFileTime): BOOL; stdcall; external 'Kernel32.dll' name 'GetSystemTimes';
{$ENDIF}
{$ENDIF}
function gsGetTickCount : Int64;
function gsNewMonitoring : Uint32;
Procedure gsStartMonitoring(aMonIndex : UInt32);
function gsStepMonitoring(aMonIndex : UInt32) : Uint64;
implementation
var glbMon : Array of Int64;
glbIndex : integer;
glbLock : {$IFDEF FPC}SyncObjs.{$ENDIF}TCriticalSection;
{$IFDEF USE_FMX}
var PlatformTimer : IFMXTimerService;
{$ENDIF}
function gsNewMonitoring : UInt32;
begin
glbLock.Enter;
try
if glbIndex >= length(glbMon)-1 then
SetLEngth(glbMon,(length(glbMon)+1)*2);
result := glbIndex;
Inc(glbIndex);
finally
glbLock.Leave;
end;
end;
Procedure gsStartMonitoring(aMonIndex : UInt32);
begin
glbMon[aMonIndex] := gsGetTickCount;
end;
function gsStepMonitoring(aMonIndex : UInt32) : Uint64;
begin
Result := gsGetTickCount - glbMon[aMonIndex];
glbMon[aMonIndex] := gsGetTickCount;
end;
{$IFDEF FPC}
function gsGetTickCount : Int64;
{$IFDEF MSWINDOWS}
var l : Int64;
begin
if QueryPerformanceCounter(Result) and QueryPerformanceFrequency(l) then
result := Round(1000 * Result / l)
else
Result := GetTickCount64;
end;
{$ENDIF}
{$IFDEF LINUX}
var tv : timeval;
Begin
if fpgettimeofday(@tv,nil) then
result := 0
else
Result := (tv.tv_sec * 1000) + round((tv.tv_usec / 1000));
end;
{$ENDIF}
{$ELSE}
function gsGetTickCount : Int64;
begin
Result := TThread.GetTickCount;
end;
{$ENDIF}
{ TgsCPUUsage }
constructor TgsCPUUsage.Create;
begin
Inherited;
TThread.GetSystemTimes(fvPreviousST);
TThread.GetSystemTimes(fPreviousST);
{$IFDEF FPC}
{$IFDEF LINUX}
//For decoding /prog/cpuinfo
Lines := TStringList.Create;
LineChunks := TStringList.Create;
{$ENDIF}
{$ENDIF}
end;
Destructor TgsCPUUsage.Destroy;
begin
{$IFDEF FPC}
{$IFDEF LINUX}
//For decoding /prog/cpuinfo
FreeAndNil(Lines);
FreeAndNil(LineChunks);
{$ENDIF}
{$ENDIF}
Inherited;
end;
Function TgsCPUUsage.InternalGetCPUUsage(
var PrevSystemTimes: TThread.TSystemTimes) : Integer;
var
CurSystemTimes: TThread.TSystemTimes;
Usage, Idle: UInt64;
begin
Result := 0;
if InternalGetSystemTime(CurSystemTimes) then
begin
Usage :=
(CurSystemTimes.UserTime - PrevSystemTimes.UserTime) +
(CurSystemTimes.KernelTime - PrevSystemTimes.KernelTime) +
(CurSystemTimes.NiceTime - PrevSystemTimes.NiceTime);
Idle := CurSystemTimes.IdleTime - PrevSystemTimes.IdleTime;
if Usage > Idle then
Result := (Usage - Idle) * 100 div Usage;
PrevSystemTimes := CurSystemTimes;
end;
end;
Function TgsCPUUsage.InternalGetSystemTime(
var aSystemTimes: TThread.TSystemTimes) : Boolean;
{$IFDEF FPC}
Const L_CST_CPU = 'cpu ';
L_CST_PROCSTAT = '/proc/stat';
{$IFDEF WINDOWS}
var
Idle, User, Kernel: TFileTime;
begin
Result := GetSystemTimes(Idle, Kernel, User);
if Result then
begin
aSystemTimes.IdleTime := UInt64(Idle.dwHighDateTime) shl 32 or Idle.dwLowDateTime;
aSystemTimes.Usertime := UInt64(User.dwHighDateTime) shl 32 or User.dwLowDateTime;
aSystemTimes.KernelTime := UInt64(Kernel.dwHighDateTime) shl 32 or Kernel.dwLowDateTime;
aSystemTimes.NiceTime := 0;
end;
end;
{$ELSE}
{$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
var Line: string;
begin
Result := False;
Lines.loadfromFile(L_CST_PROCSTAT);
LineChunks.Delimiter := ' ';
aSystemTimes.UserTime := 0;
aSystemTimes.NiceTime := 0;
aSystemTimes.KernelTime := 0;
aSystemTimes.IdleTime := 0;
for Line in Lines do
begin
if copy(Line,1,4) = L_CST_CPU then
begin
LineChunks.DelimitedText := line;
Inc(aSystemTimes.UserTime, StrToInt64(LineChunks[1]));
Inc(aSystemTimes.NiceTime, StrToInt64(LineChunks[2]));
//Kernel time usually Kernel+Idle.
Inc(aSystemTimes.KernelTime, StrToInt64(LineChunks[3]) + StrToInt64(LineChunks[4]));
Inc(aSystemTimes.IdleTime, StrToInt64(LineChunks[4]));
Result := True
end
else
Break;
end;
end;
{$ELSE}
{$MESSAGE Fatal 'Method not implemented for FPC on current platform'}
{$IFEND}
{$ENDIF}
{$ELSE} //DELPHI
{$IF Defined(MSWINDOWS) or Defined(MACOS) or Defined(ANDROID) or Defined(POSIX)}
begin
Result := TThread.GetSystemTimes(aSystemTimes);
end;
{$ELSE OTHERPLATFORM}
{$MESSAGE Fatal 'Method not implemented for DELPHI on Platform'}
{$ENDIF OTHERPLATFORM}
{$ENDIF}
procedure TgsCPUUsage.Update;
Begin
UsageCPUPercentInt := InternalGetCPUUsage(fvPreviousST);
InternalGetSystemTime(CurrentSystemTime);
DeltaSystemTime.IdleTime := CurrentSystemTime.IdleTime - fPreviousST.IdleTime;
DeltaSystemTime.UserTime := CurrentSystemTime.UserTime - fPreviousST.UserTime;
DeltaSystemTime.KernelTime := CurrentSystemTime.KernelTime - fPreviousST.KernelTime;
DeltaSystemTime.NiceTime := CurrentSystemTime.NiceTime - fPreviousST.NiceTime;
DeltaTotalSystemTime := DeltaSystemTime.IdleTime +
DeltaSystemTime.UserTime +
DeltaSystemTime.KernelTime +
DeltaSystemTime.NiceTime;
if DeltaTotalSystemTime>0 then
begin
PureIdleTimePercent := ((DeltaSystemTime.IdleTime*100/DeltaTotalSystemTime));
PureUserTimePercent := ((DeltaSystemTime.UserTime*100/DeltaTotalSystemTime));
PureKernelTimePercent := ((DeltaSystemTime.KernelTime*100/DeltaTotalSystemTime));
PureNiceTimePercent := ((DeltaSystemTime.NiceTime*100/DeltaTotalSystemTime));
//Theoritical Usage.
DeltaUsageSystemTime := DeltaSystemTime.UserTime +
DeltaSystemTime.KernelTime +
DeltaSystemTime.NiceTime;
end;
UsageCPUPercent := 0.00;
if DeltaUsageSystemTime > DeltaSystemTime.IdleTime then
UsageCPUPercent := ((DeltaUsageSystemTime - DeltaSystemTime.IdleTime) * 100 / DeltaUsageSystemTime);
DeltaKernelUsageSystemTime := DeltaSystemTime.UserTime +
DeltaSystemTime.IdleTime +
DeltaSystemTime.NiceTime;
//Windows have not Kernel CPU time : Try to simulate ?
KernelUsageCPUPercent := 0.00;
if DeltaKernelUsageSystemTime > DeltaSystemTime.KernelTime then
KernelUsageCPUPercent := ((DeltaKernelUsageSystemTime - DeltaSystemTime.KernelTime) * 100 / DeltaKernelUsageSystemTime);
InternalGetSystemTime(fPreviousST);
end;
Initialization
glbLock := {$IFDEF FPC}SyncObjs.{$ENDIF}TCriticalSection.Create;
{$IFDEF USE_FMX}
if Not(TPlatformServices.Current.SupportsPlatformService(IFMXTimerService,IInterface(PlatformTimer)) ) then
begin
Raise Exception.Create('Timer not supported on this plateform. Abort.');
end;
{$ENDIF}
Finalization
FreeAndNil(glbLock);
end.