123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- #include "guiconsole_define.h"
- #include <winpr/sysinfo.h>
- #include <EventCode.h>
- #include <unordered_map>
- #include <cmath>
- 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 <windows.h>
- #include <psapi.h>
- #include <lmcons.h>
- #include <tchar.h>
- #include <tlhelp32.h>
- #include <assert.h>
- #include <conio.h>
- #include <pdh.h>
- #include <math.h>
- #include <map>
- #include <chrono>
- #include <thread>
- // 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<long, std::string> 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<int, process_times> 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<long, std::string> 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<LogTypeEnum, std::string> 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<std::string, LogTypeEnum> 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)
- {
- }
|