Multithreaded CPU stat
This commit is contained in:
Binary file not shown.
@@ -16,17 +16,20 @@ public:
|
||||
|
||||
int UPDATE_INTERVAL; // s
|
||||
|
||||
std::vector<std::vector<int>> CPU_PREVIOUS_CORES_WORK_AND_TOTAL;
|
||||
std::vector<std::vector<int>> 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<double> CPU_CORE_PERCENT();
|
||||
|
||||
unsigned int CPU_HARDWARE_THREADS = std::thread::hardware_concurrency();
|
||||
|
||||
virtual ~CPU();
|
||||
private:
|
||||
private:
|
||||
std::thread* m_pollThread;
|
||||
bool m_isPolling;
|
||||
};
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
#include <cpu.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
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<int> 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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user