guiconsole_define.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "guiconsole_define.h"
  2. #include <winpr/sysinfo.h>
  3. #include <EventCode.h>
  4. #include <unordered_map>
  5. #include <cmath>
  6. std::string GenerateTimeStr()
  7. {
  8. SYSTEMTIME st;
  9. GetLocalTime(&st);
  10. CSimpleString timeStr = CSimpleString::Format("[%04d-%02d-%02d %02d:%02d:%02d]", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  11. return timeStr.GetData();
  12. }
  13. #ifdef RVC_OS_WIN
  14. #include <windows.h>
  15. #include <psapi.h>
  16. #include <lmcons.h>
  17. #include <tchar.h>
  18. #include <tlhelp32.h>
  19. #include <assert.h>
  20. #include <conio.h>
  21. #include <pdh.h>
  22. #include <math.h>
  23. // memInfo ½á¹¹Ìå
  24. struct memInfo {
  25. ULONGLONG TotalMemory;
  26. ULONGLONG UsedMemory;
  27. double UsedMemoryPerc;
  28. ULONGLONG TotalPageMemory;
  29. ULONGLONG UsedPageMemory;
  30. double UsedPageMemoryPerc;
  31. ULONGLONG UpTime;
  32. // Constructor to initialize the fields
  33. memInfo()
  34. : TotalMemory(0),
  35. UsedMemory(0),
  36. UsedMemoryPerc(0.0),
  37. TotalPageMemory(0),
  38. UsedPageMemory(0),
  39. UsedPageMemoryPerc(0.0),
  40. UpTime(0)
  41. {}
  42. };
  43. static ULONGLONG SubtractTimes(const FILETIME* A, const FILETIME* B)
  44. {
  45. LARGE_INTEGER lA, lB;
  46. lA.LowPart = A->dwLowDateTime;
  47. lA.HighPart = A->dwHighDateTime;
  48. lB.LowPart = B->dwLowDateTime;
  49. lB.HighPart = B->dwHighDateTime;
  50. return lA.QuadPart - lB.QuadPart;
  51. }
  52. void PollMemInfo(memInfo& info)
  53. {
  54. MEMORYSTATUSEX MemoryInfo;
  55. MemoryInfo.dwLength = sizeof(MEMORYSTATUSEX);
  56. GlobalMemoryStatusEx(&MemoryInfo);
  57. info.TotalMemory = TO_MB(MemoryInfo.ullTotalPhys);
  58. info.UsedMemory = TO_MB(MemoryInfo.ullTotalPhys - MemoryInfo.ullAvailPhys);
  59. info.UsedMemoryPerc = (double)info.UsedMemory / (double)info.TotalMemory;
  60. info.TotalPageMemory = TO_MB(MemoryInfo.ullTotalPageFile);
  61. info.UsedPageMemory = TO_MB(MemoryInfo.ullTotalPageFile - MemoryInfo.ullAvailPageFile);
  62. info.UsedPageMemoryPerc = (double)info.UsedPageMemory / (double)info.TotalPageMemory;
  63. info.UpTime = GetTickCount64();
  64. }
  65. std::pair<long, std::string> PollProcessList(long UpdateTime, system_monitor_status& curStatus)
  66. {
  67. HANDLE Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  68. if (!Snapshot)
  69. return std::make_pair(0, "");
  70. PROCESSENTRY32 Entry;
  71. Entry.dwSize = sizeof(Entry);
  72. BOOL Status = Process32First(Snapshot, &Entry);
  73. if (!Status)
  74. return std::make_pair(0, "");
  75. memInfo info;
  76. PollMemInfo(info);
  77. HANDLE curHandle = NULL; // curProcess Handle
  78. for (; Status; Status = Process32Next(Snapshot, &Entry)) {
  79. processStatus Process;
  80. Process.ID = Entry.th32ProcessID;
  81. if (Process.ID == 0)
  82. continue;
  83. Process.HandleCount = Entry.cntThreads;
  84. //Process.BasePriority = Entry.pcPriClassBase;
  85. //Process.ParentPID = Entry.th32ParentProcessID;
  86. Process.processName = Entry.szExeFile;
  87. _tcsncpy_s(Process.UserName, UNLEN, _T("SYSTEM"), UNLEN);
  88. curHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, Entry.th32ProcessID);
  89. if (curHandle) {
  90. PROCESS_MEMORY_COUNTERS ProcMemCounters;
  91. if (GetProcessMemoryInfo(curHandle, &ProcMemCounters, sizeof(ProcMemCounters))) {
  92. Process.UsedMemory = (unsigned __int64)ProcMemCounters.WorkingSetSize;
  93. Process.PercentMemory = (double)TO_MB(Process.UsedMemory) / (double)info.TotalMemory;
  94. }
  95. HANDLE ProcessTokenHandle;
  96. if (OpenProcessToken(curHandle, TOKEN_READ, &ProcessTokenHandle)) {
  97. DWORD ReturnLength;
  98. GetTokenInformation(ProcessTokenHandle, TokenUser, 0, 0, &ReturnLength);
  99. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  100. PTOKEN_USER TokenUserStruct = (PTOKEN_USER)malloc(ReturnLength);
  101. if (GetTokenInformation(ProcessTokenHandle, TokenUser, TokenUserStruct, ReturnLength, &ReturnLength)) {
  102. SID_NAME_USE NameUse;
  103. DWORD NameLength = UNLEN;
  104. TCHAR DomainName[MAX_PATH];
  105. DWORD DomainLength = MAX_PATH;
  106. LookupAccountSid(0, TokenUserStruct->User.Sid, Process.UserName, &NameLength, DomainName, &DomainLength, &NameUse);
  107. Process.UserName[9] = 0;
  108. }
  109. free(TokenUserStruct);
  110. }
  111. CloseHandle(ProcessTokenHandle);
  112. }
  113. }
  114. curStatus.processList.push_back(Process);
  115. }
  116. CloseHandle(Snapshot);
  117. //get total system information
  118. DWORD NewProcessCount = curStatus.processList.size();
  119. typedef struct system_times
  120. {
  121. FILETIME IdleTime, KernelTime, UserTime;
  122. } system_times;
  123. system_times PrevSysTimes;
  124. GetSystemTimes(&PrevSysTimes.IdleTime, &PrevSysTimes.KernelTime, &PrevSysTimes.UserTime);
  125. typedef struct process_times
  126. {
  127. FILETIME CreationTime, ExitTime, KernelTime, UserTime;
  128. } process_times;
  129. process_times* ProcessTimes = (process_times*)malloc(NewProcessCount * sizeof(*ProcessTimes));
  130. /*
  131. for (DWORD ProcIndex = 0; ProcIndex < NewProcessCount; ProcIndex++) {
  132. processStatus* Process = &curStatus.processList[ProcIndex];
  133. process_times* ProcessTime = &ProcessTimes[ProcIndex];
  134. if (curHandle) {
  135. GetProcessTimes(curHandle, &ProcessTime->CreationTime, &ProcessTime->ExitTime, &ProcessTime->KernelTime, &ProcessTime->UserTime);
  136. IO_COUNTERS IoCounters;
  137. if (GetProcessIoCounters(curHandle, &IoCounters)) {
  138. Process->DiskOperationsPrev = IoCounters.ReadTransferCount + IoCounters.WriteTransferCount;
  139. }
  140. }
  141. }
  142. */
  143. Sleep(UpdateTime);
  144. system_times SysTimes;
  145. GetSystemTimes(&SysTimes.IdleTime, &SysTimes.KernelTime, &SysTimes.UserTime);
  146. ULONGLONG SysKernelDiff = SubtractTimes(&SysTimes.KernelTime, &PrevSysTimes.KernelTime);
  147. ULONGLONG SysUserDiff = SubtractTimes(&SysTimes.UserTime, &PrevSysTimes.UserTime);
  148. ULONGLONG SysIdleDiff = SubtractTimes(&SysTimes.IdleTime, &PrevSysTimes.IdleTime);
  149. int RunningProcessCount = 0;
  150. for (DWORD ProcIndex = 0; ProcIndex < NewProcessCount; ProcIndex++) {
  151. process_times ProcessTime = { 0 };
  152. process_times* PrevProcessTime = &ProcessTimes[ProcIndex];
  153. processStatus* Process = &curStatus.processList[ProcIndex];
  154. if (curHandle) {
  155. GetProcessTimes(curHandle, &ProcessTime.CreationTime, &ProcessTime.ExitTime, &ProcessTime.KernelTime, &ProcessTime.UserTime);
  156. ULONGLONG ProcKernelDiff = SubtractTimes(&ProcessTime.KernelTime, &PrevProcessTime->KernelTime);
  157. ULONGLONG ProcUserDiff = SubtractTimes(&ProcessTime.UserTime, &PrevProcessTime->UserTime);
  158. ULONGLONG TotalSys = SysKernelDiff + SysUserDiff;
  159. ULONGLONG TotalProc = ProcKernelDiff + ProcUserDiff;
  160. if (TotalSys > 0) {
  161. Process->PercentProcessorTime = (double)((100.0 * (double)TotalProc) / (double)TotalSys);
  162. if (Process->PercentProcessorTime >= 0.01) {
  163. RunningProcessCount++;
  164. }
  165. }
  166. FILETIME SysTime;
  167. GetSystemTimeAsFileTime(&SysTime);
  168. //Process->UpTime = SubtractTimes(&SysTime, &ProcessTime.CreationTime) / 10000;
  169. /* get disk information
  170. IO_COUNTERS IoCounters;
  171. if (GetProcessIoCounters(curHandle, &IoCounters)) {
  172. Process->DiskOperations = IoCounters.ReadTransferCount + IoCounters.WriteTransferCount;
  173. Process->DiskUsage = (DWORD)((Process->DiskOperations - Process->DiskOperationsPrev) * (1000 / UpdateTime));
  174. }
  175. */
  176. CloseHandle(curHandle);
  177. curHandle = NULL;
  178. }
  179. }
  180. free(ProcessTimes);
  181. /* Since we have the values already we can compute CPU usage too */
  182. ULONGLONG SysTime = SysKernelDiff + SysUserDiff;
  183. if (SysTime > 0) {
  184. double Percentage = (double)(SysTime - SysIdleDiff) / (double)SysTime;
  185. curStatus.total_cpu = min(Percentage, 1.0);
  186. }
  187. curStatus.TotalMemory = info.TotalMemory;
  188. curStatus.UsedMemory = info.UsedMemory;
  189. curStatus.UsedMemoryPerc = info.UsedMemoryPerc;
  190. curStatus.TotalPageMemory = info.TotalPageMemory;
  191. curStatus.UsedPageMemory = info.UsedPageMemory;
  192. curStatus.UsedPageMemoryPerc = info.UsedPageMemoryPerc;
  193. curStatus.total_uptime = info.UpTime;
  194. return std::make_pair(0, "");
  195. }
  196. #else
  197. #include "linux_system_monitor/system.h"
  198. std::pair<long, std::string> PollProcessList(long UpdateTime, system_monitor_status& curStatus)
  199. {
  200. System system;
  201. auto processArr = system.Processes();
  202. curStatus.processList.clear();
  203. for (auto it : processArr)
  204. {
  205. processStatus cur;
  206. cur.ID = it.Pid();
  207. cur.processName = it.name();
  208. char buffer[20];
  209. std::sprintf(buffer, "%.2f", it.CpuUtilization());
  210. cur.PercentProcessorTime = std::atof(buffer);
  211. cur.UsedMemory = std::stoi(it.Ram());
  212. cur.HandleCount = it.fdHandles();
  213. curStatus.processList.push_back(cur);
  214. }
  215. return std::make_pair(0, "");
  216. }
  217. #endif
  218. static const std::unordered_map<LogTypeEnum, std::string> logTypeEnumToString = {
  219. {LogTypeEnum::Log_Ignore, "Ignore"},
  220. {LogTypeEnum::Log_Event, "Event"},
  221. {LogTypeEnum::Log_Warning, "Warning"},
  222. {LogTypeEnum::Log_Error, "Error"},
  223. {LogTypeEnum::Log_Debug, "Debug"},
  224. {LogTypeEnum::Log_Fatal, "Fatal"},
  225. {LogTypeEnum::Log_Notify, "Notify"}
  226. };
  227. static const std::unordered_map<std::string, LogTypeEnum> stringToLogTypeEnum = {
  228. {"Ignore", LogTypeEnum::Log_Ignore},
  229. {"Event", LogTypeEnum::Log_Event},
  230. {"Warning", LogTypeEnum::Log_Warning},
  231. {"Error", LogTypeEnum::Log_Error},
  232. {"Debug", LogTypeEnum::Log_Debug},
  233. {"Fatal", LogTypeEnum::Log_Fatal},
  234. {"Notify", LogTypeEnum::Log_Notify}
  235. };
  236. std::string LogTypeEnumToString(LogTypeEnum logType) {
  237. auto it = logTypeEnumToString.find(logType);
  238. if (it != logTypeEnumToString.end()) {
  239. return it->second;
  240. }
  241. else {
  242. return "Unknown";
  243. }
  244. }
  245. LogTypeEnum StringToLogTypeEnum(const std::string& logTypeStr) {
  246. auto it = stringToLogTypeEnum.find(logTypeStr);
  247. if (it != stringToLogTypeEnum.end()) {
  248. return it->second;
  249. }
  250. else
  251. return LogTypeEnum::Log_Warning;
  252. }
  253. void startProcess(std::string cmdline, bool isWait)
  254. {
  255. }