|
| 1 | +// A small, standalone code, to quickly record CPU-vs-T |
| 2 | +// for a running process, so that one can make plots. |
| 3 | +// Simply compile with g++ -O2 monitorCPU.cpp -o monitor.exe and run. |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <fstream> |
| 7 | +#include <sstream> |
| 8 | +#include <chrono> |
| 9 | +#include <thread> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +double getProcessCpuUtilization(int pid) |
| 13 | +{ |
| 14 | + std::ifstream statFile("/proc/" + std::to_string(pid) + "/stat"); |
| 15 | + if (!statFile.is_open()) { |
| 16 | + std::cerr << "Failed to open stat file for PID " << pid << std::endl; |
| 17 | + return -1.0; // Error indicator |
| 18 | + } |
| 19 | + |
| 20 | + std::string line; |
| 21 | + std::getline(statFile, line); |
| 22 | + statFile.close(); |
| 23 | + |
| 24 | + std::istringstream iss(line); |
| 25 | + std::string token; |
| 26 | + // We only need the 14th and 15th fields: utime and stime |
| 27 | + for (int i = 0; i < 13; ++i) { |
| 28 | + iss >> token; |
| 29 | + } |
| 30 | + unsigned long utime, stime; |
| 31 | + iss >> utime >> stime; |
| 32 | + |
| 33 | + unsigned long process_total = utime + stime; |
| 34 | + static unsigned long last_process_total = 0; |
| 35 | + |
| 36 | + // Read total CPU time from /proc/stat |
| 37 | + unsigned long total_cpu_time = 0; |
| 38 | + std::ifstream stat("/proc/stat"); |
| 39 | + if (stat.is_open()) { |
| 40 | + std::string cpuLabel; |
| 41 | + while (stat >> cpuLabel && cpuLabel != "cpu") { |
| 42 | + // Skip non-cpu lines |
| 43 | + stat.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 44 | + } |
| 45 | + unsigned long value; |
| 46 | + for (int i = 0; i < 10; ++i) { |
| 47 | + stat >> value; |
| 48 | + total_cpu_time += value; |
| 49 | + } |
| 50 | + } else { |
| 51 | + std::cerr << "Failed to open /proc/stat" << std::endl; |
| 52 | + return -1.0; // Error indicator |
| 53 | + } |
| 54 | + stat.close(); |
| 55 | + |
| 56 | + // Calculate CPU utilization over the last 5 seconds |
| 57 | + static unsigned long last_total_cpu_time = total_cpu_time; |
| 58 | + double utilization = (process_total - last_process_total) * 1.0 / (total_cpu_time - last_total_cpu_time); |
| 59 | + |
| 60 | + // Update last total CPU time for next calculation |
| 61 | + last_total_cpu_time = total_cpu_time; |
| 62 | + last_process_total = process_total; |
| 63 | + |
| 64 | + return utilization; |
| 65 | +} |
| 66 | + |
| 67 | +int getNumberOfCores() |
| 68 | +{ |
| 69 | + return sysconf(_SC_NPROCESSORS_ONLN); |
| 70 | +} |
| 71 | + |
| 72 | +int main(int argc, char* argv[]) |
| 73 | +{ |
| 74 | + int pid = -1; |
| 75 | + |
| 76 | + if (argc > 1) { |
| 77 | + pid = atoi(argv[1]); |
| 78 | + } |
| 79 | + |
| 80 | + while (true) { |
| 81 | + double cpuUtilization = getProcessCpuUtilization(pid); |
| 82 | + if (cpuUtilization >= 0.0) { |
| 83 | + std::cerr << "CPU(" << pid << ") " << cpuUtilization * 100 * getNumberOfCores() << "\n"; |
| 84 | + } else { |
| 85 | + std::cerr << "Error retrieving CPU utilization." << std::endl; |
| 86 | + } |
| 87 | + |
| 88 | + // Wait for 2 seconds |
| 89 | + std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 90 | + } |
| 91 | + return 0; |
| 92 | +} |
0 commit comments