#include "guiconsole_define.h" #include #include #include #include std::string GenerateTimeStr() { SYSTEMTIME st; GetLocalTime(&st); CSimpleString timeStr = CSimpleString::Format("[%04d-%02d-%02d %02d:%02d:%02d]", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); return timeStr.GetData(); } #ifdef RVC_OS_WIN #include #include #include #include #include #include #include #include #include #include #include #include // memInfo 结构体 struct memInfo { ULONGLONG TotalMemory; ULONGLONG UsedMemory; double UsedMemoryPerc; ULONGLONG TotalPageMemory; ULONGLONG UsedPageMemory; double UsedPageMemoryPerc; ULONGLONG UpTime; // Constructor to initialize the fields memInfo() : TotalMemory(0), UsedMemory(0), UsedMemoryPerc(0.0), TotalPageMemory(0), UsedPageMemory(0), UsedPageMemoryPerc(0.0), UpTime(0) {} }; static ULONGLONG SubtractTimes(const FILETIME* A, const FILETIME* B) { LARGE_INTEGER lA, lB; lA.LowPart = A->dwLowDateTime; lA.HighPart = A->dwHighDateTime; lB.LowPart = B->dwLowDateTime; lB.HighPart = B->dwHighDateTime; return lA.QuadPart - lB.QuadPart; } void PollMemInfo(memInfo& info) { MEMORYSTATUSEX MemoryInfo; MemoryInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&MemoryInfo); info.TotalMemory = TO_MB(MemoryInfo.ullTotalPhys); info.UsedMemory = TO_MB(MemoryInfo.ullTotalPhys - MemoryInfo.ullAvailPhys); info.UsedMemoryPerc = (double)info.UsedMemory / (double)info.TotalMemory; info.TotalPageMemory = TO_MB(MemoryInfo.ullTotalPageFile); info.UsedPageMemory = TO_MB(MemoryInfo.ullTotalPageFile - MemoryInfo.ullAvailPageFile); info.UsedPageMemoryPerc = (double)info.UsedPageMemory / (double)info.TotalPageMemory; info.UpTime = GetTickCount64(); } typedef struct system_times { FILETIME IdleTime, KernelTime, UserTime; } system_times; typedef struct process_times { FILETIME CreationTime, ExitTime, KernelTime, UserTime; } process_times; __int64 Filetime2Int64(const FILETIME& ftime) { LARGE_INTEGER li; li.LowPart = ftime.dwLowDateTime; li.HighPart = ftime.dwHighDateTime; return li.QuadPart; } __int64 CompareFileTime2(const FILETIME& preTime, const FILETIME& nowTime) { return Filetime2Int64(nowTime) - Filetime2Int64(preTime); } std::pair PollProcessList(long UpdateTime, system_monitor_status& curStatus) { HANDLE Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (!Snapshot) return std::make_pair(0, ""); PROCESSENTRY32 Entry; Entry.dwSize = sizeof(Entry); BOOL Status = Process32First(Snapshot, &Entry); if (!Status) return std::make_pair(0, ""); memInfo info; PollMemInfo(info); HANDLE curHandle = NULL; // curProcess Handle std::map cpuTimes;//calculate cpu time for (; Status; Status = Process32Next(Snapshot, &Entry)) { processStatus Process; Process.ID = Entry.th32ProcessID; if (Process.ID == 0) continue; //Process.HandleCount = Entry.cntThreads; //Process.BasePriority = Entry.pcPriClassBase; //Process.ParentPID = Entry.th32ParentProcessID; Process.processName = Entry.szExeFile; _tcsncpy_s(Process.UserName, UNLEN, _T("SYSTEM"), UNLEN); curHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, Entry.th32ProcessID); if (curHandle) { DWORD handleCount = 0; GetProcessHandleCount(curHandle, &handleCount); Process.HandleCount = handleCount; PROCESS_MEMORY_COUNTERS_EX ProcMemCounters; if (GetProcessMemoryInfo(curHandle, (PROCESS_MEMORY_COUNTERS*)&ProcMemCounters, sizeof(ProcMemCounters))) { Process.UsedMemory = (unsigned __int64)TO_MB(ProcMemCounters.WorkingSetSize); Process.PercentMemory = (double)Process.UsedMemory / (double)info.TotalMemory; } HANDLE ProcessTokenHandle; if (OpenProcessToken(curHandle, TOKEN_READ, &ProcessTokenHandle)) { DWORD ReturnLength; GetTokenInformation(ProcessTokenHandle, TokenUser, 0, 0, &ReturnLength); if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { PTOKEN_USER TokenUserStruct = (PTOKEN_USER)malloc(ReturnLength); if (GetTokenInformation(ProcessTokenHandle, TokenUser, TokenUserStruct, ReturnLength, &ReturnLength)) { SID_NAME_USE NameUse; DWORD NameLength = UNLEN; TCHAR DomainName[MAX_PATH]; DWORD DomainLength = MAX_PATH; LookupAccountSid(0, TokenUserStruct->User.Sid, Process.UserName, &NameLength, DomainName, &DomainLength, &NameUse); Process.UserName[9] = 0; } free(TokenUserStruct); } CloseHandle(ProcessTokenHandle); } CloseHandle(curHandle); } curStatus.processList.push_back(Process); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Entry.th32ProcessID); if (hProcess) { // 获取初始 CPU 时间 FILETIME ftCreation, ftExit, ftKernel, ftUser; if (!GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser)) { CloseHandle(hProcess); continue; } process_times srcTime; srcTime.CreationTime = ftCreation; srcTime.ExitTime = ftExit; srcTime.KernelTime = ftKernel; srcTime.UserTime = ftUser; cpuTimes[Process.ID] = srcTime; CloseHandle(hProcess); } } CloseHandle(Snapshot); FILETIME preIdleTime; FILETIME preKernelTime; FILETIME preUserTime; GetSystemTimes(&preIdleTime, &preKernelTime, &preUserTime); std::this_thread::sleep_for(std::chrono::seconds(UpdateTime)); FILETIME idleTime; FILETIME kernelTime; FILETIME userTime; GetSystemTimes(&idleTime, &kernelTime, &userTime); auto idle = CompareFileTime2(preIdleTime, idleTime); auto kernel = CompareFileTime2(preKernelTime, kernelTime); auto user = CompareFileTime2(preUserTime, userTime); for (auto &it : curStatus.processList) { FILETIME ftCreation, ftExit, ftKernel, ftUser; HANDLE hProcess; ULARGE_INTEGER ulKernelStart, ulUserStart, ulKernelEnd, ulUserEnd; hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, it.ID); if (hProcess == NULL) { continue; } if (!GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser)) { CloseHandle(hProcess); continue; } if (cpuTimes.find(it.ID) == cpuTimes.end()) continue; ulKernelStart.LowPart = cpuTimes[it.ID].KernelTime.dwLowDateTime; ulKernelStart.HighPart = cpuTimes[it.ID].KernelTime.dwHighDateTime; ulUserStart.LowPart = cpuTimes[it.ID].UserTime.dwLowDateTime; ulUserStart.HighPart = cpuTimes[it.ID].UserTime.dwHighDateTime; ulKernelEnd.LowPart = ftKernel.dwLowDateTime; ulKernelEnd.HighPart = ftKernel.dwHighDateTime; ulUserEnd.LowPart = ftUser.dwLowDateTime; ulUserEnd.HighPart = ftUser.dwHighDateTime; ULONGLONG kernelDiff = ulKernelEnd.QuadPart - ulKernelStart.QuadPart; ULONGLONG userDiff = ulUserEnd.QuadPart - ulUserStart.QuadPart; ULONGLONG totalDiff = kernelDiff + userDiff; double cpuUsage = 100.0 * totalDiff / (kernel + user); it.PercentProcessorTime = cpuUsage / 100.0; CloseHandle(hProcess); } /* //get total system information DWORD NewProcessCount = curStatus.processList.size(); process_times* ProcessTimes = (process_times*)malloc(NewProcessCount * sizeof(*ProcessTimes)); for (DWORD ProcIndex = 0; ProcIndex < NewProcessCount; ProcIndex++) { processStatus* Process = &curStatus.processList[ProcIndex]; process_times* ProcessTime = &ProcessTimes[ProcIndex]; if (curHandle) { GetProcessTimes(curHandle, &ProcessTime->CreationTime, &ProcessTime->ExitTime, &ProcessTime->KernelTime, &ProcessTime->UserTime); IO_COUNTERS IoCounters; if (GetProcessIoCounters(curHandle, &IoCounters)) { Process->DiskOperationsPrev = IoCounters.ReadTransferCount + IoCounters.WriteTransferCount; } } } Sleep(UpdateTime); system_times SysTimes; GetSystemTimes(&SysTimes.IdleTime, &SysTimes.KernelTime, &SysTimes.UserTime); ULONGLONG SysKernelDiff = SubtractTimes(&SysTimes.KernelTime, &PrevSysTimes.KernelTime); ULONGLONG SysUserDiff = SubtractTimes(&SysTimes.UserTime, &PrevSysTimes.UserTime); ULONGLONG SysIdleDiff = SubtractTimes(&SysTimes.IdleTime, &PrevSysTimes.IdleTime); int RunningProcessCount = 0; for (DWORD ProcIndex = 0; ProcIndex < NewProcessCount; ProcIndex++) { process_times ProcessTime = { 0 }; process_times* PrevProcessTime = &ProcessTimes[ProcIndex]; processStatus* Process = &curStatus.processList[ProcIndex]; if (curHandle) { GetProcessTimes(curHandle, &ProcessTime.CreationTime, &ProcessTime.ExitTime, &ProcessTime.KernelTime, &ProcessTime.UserTime); ULONGLONG ProcKernelDiff = SubtractTimes(&ProcessTime.KernelTime, &PrevProcessTime->KernelTime); ULONGLONG ProcUserDiff = SubtractTimes(&ProcessTime.UserTime, &PrevProcessTime->UserTime); ULONGLONG TotalSys = SysKernelDiff + SysUserDiff; ULONGLONG TotalProc = ProcKernelDiff + ProcUserDiff; if (TotalSys > 0) { Process->PercentProcessorTime = (double)((100.0 * (double)TotalProc) / (double)TotalSys); if (Process->PercentProcessorTime >= 0.01) { RunningProcessCount++; } } FILETIME SysTime; GetSystemTimeAsFileTime(&SysTime); //Process->UpTime = SubtractTimes(&SysTime, &ProcessTime.CreationTime) / 10000; /* get disk information IO_COUNTERS IoCounters; if (GetProcessIoCounters(curHandle, &IoCounters)) { Process->DiskOperations = IoCounters.ReadTransferCount + IoCounters.WriteTransferCount; Process->DiskUsage = (DWORD)((Process->DiskOperations - Process->DiskOperationsPrev) * (1000 / UpdateTime)); } CloseHandle(curHandle); curHandle = NULL; } } free(ProcessTimes); // Since we have the values already we can compute CPU usage too ULONGLONG SysTime = SysKernelDiff + SysUserDiff; if (SysTime > 0) { double Percentage = (double)(SysTime - SysIdleDiff) / (double)SysTime; curStatus.total_cpu = min(Percentage, 1.0); } curStatus.TotalMemory = info.TotalMemory; curStatus.UsedMemory = info.UsedMemory; curStatus.UsedMemoryPerc = info.UsedMemoryPerc; curStatus.TotalPageMemory = info.TotalPageMemory; curStatus.UsedPageMemory = info.UsedPageMemory; curStatus.UsedPageMemoryPerc = info.UsedPageMemoryPerc; curStatus.total_uptime = info.UpTime; */ return std::make_pair(0, ""); } #else #include "linux_system_monitor/system.h" std::pair PollProcessList(long UpdateTime, system_monitor_status& curStatus) { System system; auto processArr = system.Processes(); curStatus.processList.clear(); for (auto it : processArr) { processStatus cur; cur.ID = it.Pid(); cur.processName = it.name(); char buffer[20]; std::sprintf(buffer, "%.2f", it.CpuUtilization()); cur.PercentProcessorTime = std::atof(buffer); cur.UsedMemory = std::stoi(it.Ram()); cur.HandleCount = it.fdHandles(); curStatus.processList.push_back(cur); } return std::make_pair(0, ""); } #endif static const std::unordered_map logTypeEnumToString = { {LogTypeEnum::Log_Ignore, "Ignore"}, {LogTypeEnum::Log_Event, "Event"}, {LogTypeEnum::Log_Warning, "Warning"}, {LogTypeEnum::Log_Error, "Error"}, {LogTypeEnum::Log_Debug, "Debug"}, {LogTypeEnum::Log_Fatal, "Fatal"} }; static const std::unordered_map stringToLogTypeEnum = { {"Ignore", LogTypeEnum::Log_Ignore}, {"Event", LogTypeEnum::Log_Event}, {"Warning", LogTypeEnum::Log_Warning}, {"Error", LogTypeEnum::Log_Error}, {"Debug", LogTypeEnum::Log_Debug}, {"Fatal", LogTypeEnum::Log_Fatal} }; std::string LogTypeEnumToString(LogTypeEnum logType) { auto it = logTypeEnumToString.find(logType); if (it != logTypeEnumToString.end()) { return it->second; } else { return "Unknown"; } } LogTypeEnum StringToLogTypeEnum(const std::string& logTypeStr) { auto it = stringToLogTypeEnum.find(logTypeStr); if (it != stringToLogTypeEnum.end()) { return it->second; } else return LogTypeEnum::Log_Warning; } void startProcess(std::string cmdline, bool isWait) { }