-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stats.Memory.pas
103 lines (77 loc) · 2.22 KB
/
Stats.Memory.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
unit Stats.Memory;
interface
uses
System.Classes, System.SysUtils, Linux.Utils, System.JSON.Serializers, System.Math;
type
TSizeType = (stKB, stMB, stGB);
TMemoryInfo = class(TObject)
private
[JsonName('Total')]
FTotal: Double;
[JsonName('Used')]
FUsed: Double;
[JsonName('Free')]
FFree: Double;
[JsonIgnore]
FSizeType : TSizeType;
procedure GetMemoryData;
public
property Total : Double read FTotal;
property MemFree : Double read FFree;
property Used : Double read FUsed;
constructor Create(ASizeType : TSizeType);
end;
implementation
{ TMemoryInfo }
constructor TMemoryInfo.Create(ASizeType : TSizeType);
begin
inherited Create;
FSizeType := ASizeType;
GetMemoryData;
end;
procedure TMemoryInfo.GetMemoryData;
var
Cmd : String;
SizeArgument : string;
Buffers, Cached : Double;
begin
try
case FSizeType of
stKB: SizeArgument := 'k';
stMB: SizeArgument := 'm';
stGB: SizeArgument := 'm';
end;
//Memory in buffers + cached is actually available, so we count it
//as free. See http://www.linuxatemyram.com/ for details
Cmd := 'cat /proc/meminfo | grep "^MemFree:" | awk ''{print $2}''';
TLinuxUtils.RunCommand(Cmd,FFree);
Cmd := 'cat /proc/meminfo | grep "^Buffers:" | awk ''{print $2}''';
TLinuxUtils.RunCommand(Cmd,Buffers);
Cmd := 'cat /proc/meminfo | grep "^Cached:" | awk ''{print $2}''';
TLinuxUtils.RunCommand(Cmd,Cached);
FFree := FFree + Buffers + Cached;
//Pegando dados de memória total
Cmd := 'cat /proc/meminfo | grep "^MemTotal:" | awk ''{print $2}''';
TLinuxUtils.RunCommand(Cmd,FTotal);
if not (FSizeType = stKB) then
FTotal := FTotal / 1024;
if not (FSizeType = stKB) then
FFree := FFree / 1024;
if FSizeType = stGB then
begin
FTotal := FTotal / 1024;
FFree := FFree / 1024;
end;
FTotal := Round(FTotal);
FFree := Round(FFree);
FUsed := FTotal - FFree;
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TMemoryInfo.GetMemoryData - ' + E.Message);
{$ENDIF}
end;
end;
end;
end.