This commit is contained in:
Ben
2018-12-12 17:57:59 +00:00
parent 09f266a694
commit 348f806710
7 changed files with 57 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ project(crumpet-engine)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeFiles/) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeFiles/)
cmake_policy(SET CMP0037 OLD) cmake_policy(SET CMP0037 OLD)
set(CMAKE_BUILD_TYPE Debug)
set(BuildDir ./bin) set(BuildDir ./bin)
set(BuildExecutable resource-monitor) set(BuildExecutable resource-monitor)

View File

BIN
bin/resource-monitor Executable file

Binary file not shown.

View File

@@ -3,8 +3,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <unistd.h>
std::vector<std::string> exec(std::string command) { std::vector<std::string> execcommand(std::string command) {
char buffer[128]; char buffer[128];
std::vector<std::string> result; std::vector<std::string> result;
FILE* pipe = popen(command.c_str(), "r"); FILE* pipe = popen(command.c_str(), "r");

View File

@@ -4,12 +4,26 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
#include <map> #include <map>
#include <cstring> // For memcpy
struct CPUStat {
std::string ARCHITECTURE;
std::string MODEL_NAME;
unsigned short HARDWARE_THREADS;
long double MAX_FREQ;
long double MIN_FREQ;
long double FREQ;
long double PERCENT_USAGE;
};
class CPU { class CPU {
public: public:
CPU(); CPU();
std::mutex CPU_Mutex; std::mutex CPU_Mutex;
CPUStat* cpuStat;
CPUStat getCPUStat();
int CPU_PREVIOUS_WORK; int CPU_PREVIOUS_WORK;
int CPU_PREVIOUS_TOTAL; int CPU_PREVIOUS_TOTAL;
int CPU_WORK; int CPU_WORK;
@@ -28,6 +42,7 @@ public:
double CPU_PERCENT(int core); double CPU_PERCENT(int core);
std::vector<double> CPU_CORE_PERCENT(); std::vector<double> CPU_CORE_PERCENT();
virtual ~CPU(); virtual ~CPU();
private: private:
std::thread* m_pollThread; std::thread* m_pollThread;

View File

@@ -1,5 +1,6 @@
#include <cpu.h> #include <cpu.h>
#include <common.h>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
@@ -7,12 +8,22 @@
#include <math.h> #include <math.h>
CPU::CPU() { CPU::CPU() {
CPU_HARDWARE_THREADS = std::thread::hardware_concurrency(); this->CPU_HARDWARE_THREADS = std::thread::hardware_concurrency();
this->UPDATE_INTERVAL = 1000; this->UPDATE_INTERVAL = 1000;
this->m_isPolling = false; this->m_isPolling = false;
this->cpuStat = new CPUStat();
this->cpuStat->HARDWARE_THREADS = this->CPU_HARDWARE_THREADS;
std::cout << "Number of hardware threads supported: " << CPU_HARDWARE_THREADS << std::endl; std::cout << "Number of hardware threads supported: " << CPU_HARDWARE_THREADS << std::endl;
} }
CPUStat CPU::getCPUStat() {
CPUStat temp;
this->CPU_Mutex.lock();
memcpy(&temp, this->cpuStat, sizeof(CPUStat));
this->CPU_Mutex.unlock();
return temp;
}
void CPU::START_CPU_POLLING() { void CPU::START_CPU_POLLING() {
if (this->m_isPolling) { if (this->m_isPolling) {
return; return;
@@ -71,7 +82,28 @@ void CPU::CPU_POLL(CPU* cpu) {
cpu->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[thread][1] = cpu->CPU_CORES_WORK_AND_TOTAL[thread][1]; 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][0] = tluser + tnice + tsystem + tidle + tiowait + tirq + tsoftirq;
cpu->CPU_CORES_WORK_AND_TOTAL[thread][1] = tluser + tnice + tsystem; cpu->CPU_CORES_WORK_AND_TOTAL[thread][1] = tluser + tnice + tsystem;
cpu->CPU_Mutex.unlock();
}
cpu->CPU_Mutex.lock();
long double totalOverTime = cpu->CPU_PREVIOUS_TOTAL - cpu->CPU_TOTAL;
long double workOverTime = cpu->CPU_PREVIOUS_WORK - cpu->CPU_WORK;
cpu->cpuStat->PERCENT_USAGE = (workOverTime / totalOverTime) * 100;
cpu->CPU_Mutex.unlock();
std::vector<std::string> lscpu = execcommand("lscpu");
for (unsigned int i = 0; i < lscpu.size(); i++) {
cpu->CPU_Mutex.lock();
if (lscpu[i].find("Architecture:")) {
std::string architecture(10, ' ');
sscanf(lscpu[i].c_str(), "Architecture: %*s", &architecture[0], architecture.size());
cpu->cpuStat->ARCHITECTURE = architecture;
std::cout << architecture;
}
cpu->CPU_Mutex.unlock(); cpu->CPU_Mutex.unlock();
} }
@@ -112,6 +144,10 @@ double CPU::CPU_PERCENT(int core) {
std::vector<double> CPU::CPU_CORE_PERCENT() { std::vector<double> CPU::CPU_CORE_PERCENT() {
std::vector<double> output; std::vector<double> output;
// TODO: Return a list of percents, one index is one hardware
//thread, index 0 is total percent
return output; return output;
} }

View File

@@ -1,5 +1,6 @@
#include <sys.h> #include <sys.h>
#include <common.h> #include <memstat.h>
#include <cpu.h>
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
@@ -14,11 +15,6 @@ int main(int argc, char** argv) {
cpu->START_CPU_POLLING(); cpu->START_CPU_POLLING();
mem->START_MEMORY_POLLING(); mem->START_MEMORY_POLLING();
std::vector<std::string> lscpu = exec("lscpu");
for (unsigned int i = 0; i < lscpu.size(); i++) {
std::cout << lscpu[i] << std::endl;
}
while(1) { while(1) {
sleep(1); sleep(1);
std::cout << std::endl; std::cout << std::endl;