diff --git a/bin/resource-monitor b/bin/resource-monitor index 4813aaa..24d2188 100755 Binary files a/bin/resource-monitor and b/bin/resource-monitor differ diff --git a/include/cpu.h b/include/cpu.h index 13bdabc..d78d858 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -16,17 +16,20 @@ public: int UPDATE_INTERVAL; // s + std::vector> CPU_PREVIOUS_CORES_WORK_AND_TOTAL; std::vector> CPU_CORES_WORK_AND_TOTAL; void START_CPU_POLLING(); static void CPU_POLL(CPU* cpu); void END_CPU_POLLING(); - double CPU_PERCENT(); + double CPU_PERCENT(int core); std::vector CPU_CORE_PERCENT(); + unsigned int CPU_HARDWARE_THREADS = std::thread::hardware_concurrency(); + virtual ~CPU(); -private: +private: std::thread* m_pollThread; bool m_isPolling; }; diff --git a/platform/linux/monitoring/cpu.cpp b/platform/linux/monitoring/cpu.cpp index 29675ff..8d62d0a 100644 --- a/platform/linux/monitoring/cpu.cpp +++ b/platform/linux/monitoring/cpu.cpp @@ -1,13 +1,26 @@ #include +#include +#include #include #include -#include #include CPU::CPU() { this->UPDATE_INTERVAL = 1000; this->m_isPolling = false; + std::cout << "Number of hardware threads supported: " << CPU_HARDWARE_THREADS << std::endl; + for (unsigned int thread; thread < CPU_HARDWARE_THREADS; thread++) { + std::vector temp1, temp2; + + for (int i = 0; i <= 2; i++) { + temp1.push_back(0); + temp2.push_back(0); + } + + CPU_PREVIOUS_CORES_WORK_AND_TOTAL.push_back(temp1); + CPU_CORES_WORK_AND_TOTAL.push_back(temp2); + } } void CPU::START_CPU_POLLING() { @@ -34,10 +47,9 @@ void CPU::CPU_POLL(CPU* cpu) { // READ TOTAL CPU unsigned long long luser, nice, system, idle, iowait, irq, softirq; - FILE* file = fopen("/proc/stat", "r"); - fscanf(file, "cpu %llu %llu %llu %llu %llu %llu %llu", &luser, + FILE* ProcStat = fopen("/proc/stat", "r"); + fscanf(ProcStat, "cpu %llu %llu %llu %llu %llu %llu %llu", &luser, &nice, &system, &idle, &iowait, &irq, &softirq); - fclose(file); cpu->CPU_Mutex.lock(); @@ -48,6 +60,28 @@ void CPU::CPU_POLL(CPU* cpu) { cpu->CPU_WORK = luser + nice + system; cpu->CPU_Mutex.unlock(); + + for (unsigned int thread = 0; thread < cpu->CPU_HARDWARE_THREADS; thread++) { + unsigned long long tluser, tnice, tsystem, tidle, tiowait, + tirq, tsoftirq; + + std::stringstream pattern; + pattern << "cpu" << thread << " %llu %llu %llu %llu %llu %llu %llu"; + + fscanf(ProcStat, pattern.str().c_str(), &tluser, &tnice, &tsystem, + &tidle, &tiowait, &tirq, &tsoftirq); + + cpu->CPU_Mutex.lock(); + + cpu->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[thread][0] = cpu->CPU_CORES_WORK_AND_TOTAL[thread][0]; + cpu->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[thread][1] = cpu->CPU_CORES_WORK_AND_TOTAL[thread][1]; + cpu->CPU_CORES_WORK_AND_TOTAL[thread][0] = tluser + tnice + tsystem + tidle + tiowait + tirq + tsoftirq; + cpu->CPU_CORES_WORK_AND_TOTAL[thread][1] = tluser + tnice + tsystem; + + cpu->CPU_Mutex.unlock(); + } + + fclose(ProcStat); sleep(1); } } @@ -61,10 +95,21 @@ void CPU::END_CPU_POLLING() { delete m_pollThread; } -double CPU::CPU_PERCENT() { +double CPU::CPU_PERCENT(int core) { + core--; + this->CPU_Mutex.lock(); - long double workOverTime = this->CPU_PREVIOUS_WORK - this->CPU_WORK; - long double totalOverTime = this->CPU_PREVIOUS_TOTAL - this->CPU_TOTAL; + + long double totalOverTime = 0.0; + long double workOverTime = 0.0; + if (core == -1) { + totalOverTime = this->CPU_PREVIOUS_TOTAL - this->CPU_TOTAL; + workOverTime = this->CPU_PREVIOUS_WORK - this->CPU_WORK; + } else { + totalOverTime = this->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[core][0] - this->CPU_CORES_WORK_AND_TOTAL[core][0]; + workOverTime = this->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[core][1] - this->CPU_CORES_WORK_AND_TOTAL[core][1]; + } + this->CPU_Mutex.unlock(); double percent = (workOverTime / totalOverTime) * 100; diff --git a/src/main.cpp b/src/main.cpp index b4c6799..a5a5733 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,12 @@ int main(int argc, char** argv) { cpu->START_CPU_POLLING(); while(1) { - std::cout << "TOTAL CPU USAGE: " << cpu->CPU_PERCENT() << std::endl; + std::cout << "TOTAL CPU USAGE: " << cpu->CPU_PERCENT(0) << std::endl; + // std::cout << "CORE 1 USAGE: " << cpu->CPU_PERCENT(1) << std::endl; + // std::cout << "CORE 2 USAGE: " << cpu->CPU_PERCENT(2) << std::endl; + // std::cout << "CORE 3 USAGE: " << cpu->CPU_PERCENT(3) << std::endl; + // std::cout << "CORE 4 USAGE: " << cpu->CPU_PERCENT(4) << std::endl; + std::cout << std::endl; sleep(1); } }