|
| 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