From 217b65f0e96d921b64069a6d65e06a8f56853546 Mon Sep 17 00:00:00 2001 From: jackun Date: Tue, 4 Feb 2020 10:11:51 +0200 Subject: [PATCH] Some cpu/gpu stats parsing fixes --- src/cpu.cpp | 22 ++++++++---------- src/cpu_gpu.h | 63 ++++++++++++++++++++++++--------------------------- 2 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/cpu.cpp b/src/cpu.cpp index 6ad559ed..8e749e42 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -7,6 +7,7 @@ #include #include #include +#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; } diff --git a/src/cpu_gpu.h b/src/cpu_gpu.h index 821bf65a..5f1f3269 100644 --- a/src/cpu_gpu.h +++ b/src/cpu_gpu.h @@ -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 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; }