CPU polling finished, needs optimisation

This commit is contained in:
Ben
2018-12-13 09:39:58 +00:00
parent 348f806710
commit 6fd299c4cd
3 changed files with 18 additions and 7 deletions

Binary file not shown.

View File

@@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <regex>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
@@ -95,15 +96,24 @@ void CPU::CPU_POLL(CPU* cpu) {
std::vector<std::string> lscpu = execcommand("lscpu"); std::vector<std::string> lscpu = execcommand("lscpu");
for (unsigned int i = 0; i < lscpu.size(); i++) { for (unsigned int i = 0; i < lscpu.size(); i++) {
std::regex architecture("Architecture.*?((?:[a-z][a-z0-9_]*))");
std::regex maxMHz ("CPU max MHz.*?((?:[0-9].*))");
std::regex minMHz ("CPU min MHz.*?((?:[0-9].*))");
std::regex MHz ("CPU MHz.*?((?:[0-9].*))");
std::smatch m;
cpu->CPU_Mutex.lock(); cpu->CPU_Mutex.lock();
if (lscpu[i].find("Architecture:")) { if (std::regex_search(lscpu[i], m, architecture)) {
std::string architecture(10, ' '); cpu->cpuStat->ARCHITECTURE = m[1].str();
sscanf(lscpu[i].c_str(), "Architecture: %*s", &architecture[0], architecture.size()); } else if (std::regex_search(lscpu[i], m, maxMHz)) {
cpu->cpuStat->ARCHITECTURE = architecture; cpu->cpuStat->MAX_FREQ = std::stod(m[1].str());
std::cout << architecture; } else if (std::regex_search(lscpu[i], m, minMHz)) {
} cpu->cpuStat->MIN_FREQ = std::stod(m[1].str());
} else if (std::regex_search(lscpu[i], m, MHz)) {
cpu->cpuStat->FREQ = std::stod(m[1].str());
}
cpu->CPU_Mutex.unlock(); cpu->CPU_Mutex.unlock();
} }

View File

@@ -21,5 +21,6 @@ int main(int argc, char** argv) {
for (unsigned int thread = 0; thread <= cpu->CPU_HARDWARE_THREADS; thread++) { for (unsigned int thread = 0; thread <= cpu->CPU_HARDWARE_THREADS; thread++) {
std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl; std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl;
} }
std::cout << "CPU FREQUENCY: " << cpu->cpuStat->FREQ << "MHz" << std::endl;
} }
} }