Skip to content

Commit 6224d4f

Browse files
committed
+ possibilidade de alterar o nome padrão do arquivo config/lazarus/config.sqlite.zeos.pas
+ Classe com funções de monitoramento de CPU e Memória da aplicação para Windows e Linux em Lazarus.
1 parent 0ed3dc0 commit 6224d4f

2 files changed

Lines changed: 192 additions & 10 deletions

File tree

config/lazarus/config.sqlite.zeos.pas

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
interface
99

1010
uses
11-
fpJSON, SysUtils, Classes, DB,
12-
Forms, StdCtrls, ExtCtrls, ValEdit,
11+
fpJSON, SysUtils, Classes, DB, Forms, StdCtrls, ExtCtrls, ValEdit,
1312
ZConnection, ZDataSet;
1413

1514
type
15+
16+
{ TSQLiteConfig }
17+
1618
TSQLiteConfig = class
1719
private
1820
FConn: TZConnection;
1921
FDataSet: TZQuery;
2022
function Validate: boolean;
2123
function GetDefaultDir(aFileName: string): string;
2224
public
23-
constructor Create;
25+
constructor Create(aFileName: string = 'config.db');
2426
destructor Destroy; override;
2527
function getValue(pKey: string): string;
2628
procedure UpdateConfig(aJSON: TJSONObject); overload;
@@ -35,11 +37,11 @@ implementation
3537

3638
{ TSQLiteConfig }
3739

38-
constructor TSQLiteConfig.Create;
40+
constructor TSQLiteConfig.Create(aFileName: string);
3941
begin
4042
FConn := TZConnection.Create(nil);
4143
FConn.Protocol := 'sqlite-3';
42-
FConn.Database := ExtractFilePath(ParamStr(0)) + 'config.db';
44+
FConn.Database := ExtractFilePath(ParamStr(0)) + aFileName;
4345
FConn.Properties.Add('LockingMode=normal');
4446
FConn.LibraryLocation := GetDefaultDir('sqlite3.dll');
4547

@@ -261,12 +263,15 @@ function TSQLiteConfig.ValidaBanco: boolean;
261263
begin
262264
Result := False;
263265
try
264-
FDataSet.SQL.Text := 'select count(*) from config';
265-
FDataSet.Open;
266+
try
267+
FDataSet.SQL.Text := 'PRAGMA table_info("Config")';
268+
FDataSet.ExecSQL;
269+
Result := True;
270+
except
271+
Result := False;
272+
end;
273+
finally
266274
FDataSet.Close;
267-
Result := True;
268-
except
269-
Result := False;
270275
end;
271276
end;
272277

other/Lazarus/hwmonitorutils.pas

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
unit HWMonitorUtils;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
{$IFDEF WINDOWS}
9+
Windows, JwaWinBase, JwaPsApi,
10+
{$ENDIF}
11+
{$IFDEF LINUX}
12+
Unix, BaseUnix, process,
13+
{$ENDIF}
14+
Classes, SysUtils, LazUTF8;
15+
16+
type
17+
18+
TRAMData = record
19+
TotalRAM: int64;
20+
FreeRAM: int64;
21+
UsedRAM: int64;
22+
FreeRAMPerc: double;
23+
UsedRamPerc: double;
24+
AppUsedRAM: int64;
25+
AppUsedRAMPerc: double;
26+
end;
27+
28+
{ THWMonitor }
29+
30+
THWMonitor = class
31+
private
32+
LastSysKernelTime, LastSysUserTime, LastProcKernelTime, LastProcUserTime: int64;
33+
public
34+
constructor Create;
35+
function GetCPULoad: string;
36+
function GetRAMData: TRAMData;
37+
end;
38+
39+
implementation
40+
41+
{ THWMonitor }
42+
43+
constructor THWMonitor.Create;
44+
begin
45+
LastSysKernelTime := 0;
46+
LastSysUserTime := 0;
47+
LastProcKernelTime := 0;
48+
LastProcUserTime := 0;
49+
end;
50+
51+
{$IFDEF WINDOWS}
52+
function THWMonitor.GetCPULoad: string;
53+
var
54+
hProcess: THandle;
55+
56+
NotUsed, SysKernelTime, ProcKernelTime, SysUserTime, ProcUserTime: FILETIME;
57+
DiffSysKernel, DiffSysUser, DiffProcKernel, DiffProcUser, TotalSysTime,
58+
TotalProcTime: int64;
59+
calc: double;
60+
begin
61+
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION, False, GetCurrentProcessId);
62+
try
63+
GetSystemTimes(@NotUsed, @SysKernelTime, @SysUserTime);
64+
65+
DiffSysKernel := int64(SysKernelTime) - LastSysKernelTime;
66+
DiffSysUser := int64(SysUserTime) - LastSysUserTime;
67+
TotalSysTime := DiffSysKernel + DiffSysUser;
68+
69+
GetProcessTimes(hProcess, NotUsed, NotUsed, ProcKernelTime, ProcUserTime);
70+
71+
DiffProcKernel := int64(ProcKernelTime) - LastProcKernelTime;
72+
DiffProcUser := int64(ProcUserTime) - LastProcUserTime;
73+
TotalProcTime := DiffProcKernel + DiffProcUser;
74+
75+
calc := TotalProcTime / TotalSysTime * 100;
76+
77+
LastSysKernelTime := int64(SysKernelTime);
78+
LastSysUserTime := int64(SysUserTime);
79+
LastProcKernelTime := int64(ProcKernelTime);
80+
LastProcUserTime := int64(ProcUserTime);
81+
82+
Result := FloatToStrF(calc, ffFixed, 16, 2) + ' %';
83+
finally
84+
CloseHandle(hProcess);
85+
end;
86+
end;
87+
88+
function THWMonitor.GetRAMData: TRAMData;
89+
var
90+
MemoryStatusEx: TMemoryStatusEx;
91+
usedmem: int64;
92+
memcount: TProcessMemoryCounters;
93+
begin
94+
// get system ram info
95+
MemoryStatusEx.dwLength := SizeOf(MemoryStatusEx);
96+
GlobalMemoryStatusEx(MemoryStatusEx);
97+
98+
// get process ram info
99+
memcount.cb := SizeOf(memcount);
100+
GetProcessMemoryInfo(GetCurrentProcess, memcount, SizeOf(memcount));
101+
102+
// output results
103+
Result.FreeRAM := MemoryStatusEx.ullAvailPhys;
104+
Result.TotalRAM := MemoryStatusEx.ullTotalPhys;
105+
Result.FreeRAMPerc := Result.FreeRAM / result.TotalRAM;
106+
Result.UsedRAM := MemoryStatusEx.ullTotalPhys - MemoryStatusEx.ullAvailPhys;
107+
Result.UsedRamPerc := Result.UsedRAM / Result.TotalRAM;
108+
Result.AppUsedRAM := memcount.WorkingSetSize;
109+
Result.AppUsedRAMPerc := Result.AppUsedRAM / Result.UsedRAM;
110+
end;
111+
{$ENDIF}
112+
{$IFDEF LINUX}
113+
function THWMonitor.GetCPULoad: string;
114+
var
115+
OutputList: TStringList;
116+
begin
117+
with TProcess.Create(nil) do
118+
try
119+
CommandLine := 'ps -p ' + GetProcessID.ToString + ' -o %cpu';
120+
Options := Options + [poWaitOnExit, poUsePipes];
121+
Execute;
122+
OutputList := TStringList.Create;
123+
OutputList.LoadFromStream(Output);
124+
Result := trim(OutputList[1]);
125+
finally
126+
Free;
127+
OutputList.Free;
128+
end;
129+
end;
130+
131+
function THWMonitor.GetRAMData: TRAMData;
132+
var
133+
statfile: TextFile;
134+
currline, info: string;
135+
begin
136+
// get system total memory info
137+
AssignFile(statfile, '/proc/meminfo');
138+
Reset(statfile);
139+
while not EOF(statfile) do
140+
begin
141+
ReadLn(statfile, currline);
142+
if Pos('MemTotal:', currline) > 0 then
143+
begin
144+
info := trim(Copy(currline, pos(':', currline) + 1, Length(currline)));
145+
Result.TotalRAM := StrToInt64(Copy(info, 0, length(info) - 3));
146+
end
147+
else if Pos('MemAvailable:', currline) > 0 then
148+
begin
149+
info := trim(Copy(currline, pos(':', currline) + 1, Length(currline)));
150+
Result.FreeRAM := StrToInt64(Copy(info, 0, length(info) - 3));
151+
end;
152+
end;
153+
CloseFile(statfile);
154+
155+
// get application memory info
156+
AssignFile(statfile, '/proc/self/status');
157+
Reset(statfile);
158+
while not EOF(statfile) do
159+
begin
160+
ReadLn(statfile, currline);
161+
if Pos('RssAnon:', currline) > 0 then
162+
begin
163+
info := trim(Copy(currline, pos(':', currline) + 1, Length(currline)));
164+
Result.AppUsedRAM := StrToInt64(Copy(info, 0, length(info) - 3));
165+
break;
166+
end;
167+
end;
168+
CloseFile(statfile);
169+
170+
// calculating the remaining stats
171+
Result.UsedRAM := Result.TotalRAM - result.FreeRAM;
172+
Result.AppUsedRAMPerc := Result.AppUsedRAM / Result.TotalRAM;
173+
Result.UsedRamPerc := Result.UsedRAM / Result.TotalRAM;
174+
end;
175+
{$ENDIF}
176+
177+
end.

0 commit comments

Comments
 (0)