|
|
@@ -2,6 +2,7 @@
|
|
|
#include <winpr/sysinfo.h>
|
|
|
#include <EventCode.h>
|
|
|
#include <unordered_map>
|
|
|
+#include <cmath>
|
|
|
|
|
|
std::string GenerateTimeStr()
|
|
|
{
|
|
|
@@ -247,110 +248,27 @@ std::pair<long, std::string> PollProcessList(long UpdateTime, system_monitor_sta
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
-#include <fstream>
|
|
|
-#include <unistd.h>
|
|
|
-#include <dirent.h>
|
|
|
-#include <cstring>
|
|
|
-#include <unordered_map>
|
|
|
-#include <sys/types.h>
|
|
|
-#include <sys/stat.h>
|
|
|
-#include <map>
|
|
|
-
|
|
|
-
|
|
|
-double get_cpu_usage(int pid) {
|
|
|
- static std::unordered_map<int, double> prev_cpu_times;
|
|
|
- std::ifstream stat_file("/proc/" + std::to_string(pid) + "/stat");
|
|
|
- std::string line;
|
|
|
- std::getline(stat_file, line);
|
|
|
- std::istringstream iss(line);
|
|
|
- std::string field;
|
|
|
- unsigned long long utime, stime;
|
|
|
- for (int i = 1; i <= 13; ++i) {
|
|
|
- iss >> field;
|
|
|
- if (i == 13) {
|
|
|
- utime = std::stoull(field);
|
|
|
- }
|
|
|
- else if (i == 14) {
|
|
|
- stime = std::stoull(field);
|
|
|
- }
|
|
|
- }
|
|
|
- stat_file.close();
|
|
|
|
|
|
- unsigned long long total_time = utime + stime;
|
|
|
- double cpu_usage = 0.0;
|
|
|
- if (prev_cpu_times.find(pid) != prev_cpu_times.end()) {
|
|
|
- unsigned long long prev_total_time = prev_cpu_times[pid];
|
|
|
- cpu_usage = (total_time - prev_total_time) / sysconf(_SC_CLK_TCK) * 100.0;
|
|
|
- }
|
|
|
- prev_cpu_times[pid] = total_time;
|
|
|
- return cpu_usage;
|
|
|
-}
|
|
|
-
|
|
|
-long get_memory_usage(int pid) {
|
|
|
- std::ifstream status_file("/proc/" + std::to_string(pid) + "/status");
|
|
|
- std::string line;
|
|
|
- while (std::getline(status_file, line)) {
|
|
|
- if (line.find("VmRSS") != std::string::npos) {
|
|
|
- std::istringstream iss(line);
|
|
|
- std::string field;
|
|
|
- long memory_usage;
|
|
|
- iss >> field >> memory_usage;
|
|
|
- status_file.close();
|
|
|
- return memory_usage * 1024; // Convert from KB to bytes
|
|
|
- }
|
|
|
- }
|
|
|
- status_file.close();
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int get_num_handles(int pid) {
|
|
|
- std::string fd_dir = "/proc/" + std::to_string(pid) + "/fd";
|
|
|
- DIR* dir = opendir(fd_dir.c_str());
|
|
|
- if (dir == nullptr) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- int count = 0;
|
|
|
- while (readdir(dir) != nullptr) {
|
|
|
- ++count;
|
|
|
- }
|
|
|
- closedir(dir);
|
|
|
- return count;
|
|
|
-}
|
|
|
-
|
|
|
-void get_process_info(std::vector<processStatus>& processes) {
|
|
|
- DIR* proc_dir = opendir("/proc");
|
|
|
- if (proc_dir == nullptr) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- struct dirent* entry;
|
|
|
- while ((entry = readdir(proc_dir)) != nullptr) {
|
|
|
- if (entry->d_type == DT_DIR && std::isdigit(entry->d_name[0])) {
|
|
|
- int pid = std::stoi(entry->d_name);
|
|
|
- std::string comm_file = "/proc/" + std::to_string(pid) + "/comm";
|
|
|
- std::ifstream ifs(comm_file.c_str());
|
|
|
- if (ifs.is_open()) {
|
|
|
- std::string name;
|
|
|
- std::getline(ifs, name);
|
|
|
- ifs.close();
|
|
|
-
|
|
|
- processStatus info;
|
|
|
- info.processName = name;
|
|
|
- info.ID = pid;
|
|
|
- info.PercentProcessorTime = get_cpu_usage(pid);
|
|
|
- info.UsedMemory = get_memory_usage(pid);
|
|
|
- info.HandleCount = get_num_handles(pid);
|
|
|
- processes.push_back(info);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- closedir(proc_dir);
|
|
|
-}
|
|
|
+#include "linux_system_monitor/system.h"
|
|
|
|
|
|
|
|
|
std::pair<long, std::string> PollProcessList(long UpdateTime, system_monitor_status& curStatus)
|
|
|
{
|
|
|
- get_process_info(curStatus.processList);
|
|
|
+ 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, "");
|