Some cpu/gpu stats parsing fixes

pull/20/head
jackun 4 years ago
parent 6400ab3cdc
commit 217b65f0e9
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -7,6 +7,7 @@
#include <string.h>
#include <algorithm>
#include <regex>
#include "string_utils.h"
#ifndef PROCDIR
#define PROCDIR "/proc"
@ -24,10 +25,6 @@
#define PROCCPUINFOFILE PROCDIR "/cpuinfo"
#endif
static bool starts_with(const std::string& s, const char *t){
return s.rfind(t, 0) == 0;
}
void calculateCPUData(CPUData& cpuData,
unsigned long long int usertime,
unsigned long long int nicetime,
@ -204,15 +201,14 @@ bool CPUStats::UpdateCPUData()
bool CPUStats::UpdateCoreMhz() {
m_coreMhz.clear();
std::ifstream cpuInfo(PROCCPUINFOFILE);
std::string row;
int i = 0;
while (std::getline(cpuInfo, row)) {
CPUData& cpuData = m_cpuData[i];
if (row.find("MHz") != std::string::npos){
row = std::regex_replace(row, std::regex(R"([^0-9.])"), "");
cpuData.mhz = stoi(row);
i++;
}
std::string row;
size_t i = 0;
while (std::getline(cpuInfo, row) && i < m_cpuData.size()) {
if (row.find("MHz") != std::string::npos){
row = std::regex_replace(row, std::regex(R"([^0-9.])"), "");
if (!try_stoi(m_cpuData[i++].mhz, row))
m_cpuData[i++].mhz = 0;
}
}
return true;
}

@ -18,13 +18,10 @@
using namespace std;
int gpuLoad, gpuTemp, cpuTemp;
string gpuLoadDisplay, cpuTempLocation;
FILE *amdGpuFile, *amdTempFile, *cpuTempFile;
FILE *amdGpuFile = nullptr, *amdTempFile = nullptr, *cpuTempFile = nullptr;
int numCpuCores = std::thread::hardware_concurrency();
size_t arraySize = numCpuCores + 1;
// std::vector<Cpus> cpuArray;
pthread_t cpuThread, gpuThread, cpuInfoThread, nvidiaSmiThread;
string exec(string command) {
@ -51,44 +48,42 @@ string exec(string command) {
void *cpuInfo(void *){
char buff[6];
rewind(cpuTempFile);
rewind(cpuTempFile);
fflush(cpuTempFile);
fscanf(cpuTempFile, "%s", buff);
cpuTemp = stoi(buff) / 1000;
pthread_detach(cpuInfoThread);
return NULL;
if (fscanf(cpuTempFile, "%d", &cpuTemp) != 1)
cpuTemp = 0;
cpuTemp /= 1000;
pthread_detach(cpuInfoThread);
return NULL;
}
void *getNvidiaGpuInfo(void *){
if (!nvmlSuccess)
checkNvidia();
if (nvmlSuccess){
getNvidiaInfo();
gpuLoad = nvidiaUtilization.gpu;
gpuLoadDisplay = gpuLoad;
gpuTemp = nvidiaTemp;
}
pthread_detach(nvidiaSmiThread);
return NULL;
if (!nvmlSuccess)
checkNvidia();
if (nvmlSuccess){
getNvidiaInfo();
gpuLoad = nvidiaUtilization.gpu;
gpuTemp = nvidiaTemp;
}
pthread_detach(nvidiaSmiThread);
return NULL;
}
void *getAmdGpuUsage(void *){
char buff[5];
rewind(amdGpuFile);
rewind(amdGpuFile);
fflush(amdGpuFile);
fscanf(amdGpuFile, "%s", buff);
gpuLoadDisplay = buff;
gpuLoad = stoi(buff);
rewind(amdTempFile);
if (fscanf(amdGpuFile, "%d", &gpuLoad) != 1)
gpuLoad = 0;
rewind(amdTempFile);
fflush(amdTempFile);
fscanf(amdTempFile, "%s", buff);
gpuTemp = (stoi(buff) / 1000);
if (fscanf(amdTempFile, "%d", &gpuTemp) != 1)
gpuTemp = 0;
gpuTemp /= 1000;
pthread_detach(gpuThread);
return NULL;
pthread_detach(gpuThread);
return NULL;
}

Loading…
Cancel
Save