Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sstream>
#include <dirent.h>
#include <string.h>
Expand Down Expand Up @@ -108,9 +109,9 @@ CPUStats::CPUStats()

CPUStats::~CPUStats()
{
if (m_cpuTempFile) {
fclose(m_cpuTempFile);
m_cpuTempFile = nullptr;
if (m_cpuTempFd != -1) {
close(m_cpuTempFd);
m_cpuTempFd = -1;
}
}

Expand Down Expand Up @@ -281,14 +282,26 @@ bool CPUStats::UpdateCoreMhz() {
}

bool CPUStats::ReadcpuTempFile(int& temp) {
if (!m_cpuTempFile)
if (m_cpuTempFd == -1)
return false;

rewind(m_cpuTempFile);
fflush(m_cpuTempFile);
bool ret = (fscanf(m_cpuTempFile, "%d", &temp) == 1);
temp = temp / 1000;

int ret = -1;
char buf[21];
/* lseek + read */
ssize_t read_sz = pread(m_cpuTempFd, buf, sizeof(buf), 0);
/* valid temp */
if (read_sz > 3) {
if (buf[read_sz] == '\n') /* sysfs guarantees newline? */
--read_sz;
/* extract degrees */
read_sz -= 3;
buf[read_sz] = '\0';
char *buf_e;
const long n = strtol(buf, &buf_e, 10);
if (n >= INT_MIN && n <= INT_MAX) {
temp = n;
ret = buf_e - buf;
}
}
return ret;
}

Expand Down Expand Up @@ -559,7 +572,7 @@ static void check_thermal_zones(std::string& path, std::string& input) {
}

bool CPUStats::GetCpuFile() {
if (m_cpuTempFile)
if (m_cpuTempFd != -1)
return true;

std::string name, path, input;
Expand Down Expand Up @@ -634,7 +647,7 @@ bool CPUStats::GetCpuFile() {
}

SPDLOG_INFO("hwmon: using input: {}", input);
m_cpuTempFile = fopen(input.c_str(), "r");
m_cpuTempFd = open(input.c_str(), O_RDONLY);

return true;
}
Expand Down Expand Up @@ -739,7 +752,6 @@ bool CPUStats::InitCpuPowerData() {
return true;

retries++;

std::string name, path;
std::string hwmon = "/sys/class/hwmon/";

Expand Down Expand Up @@ -789,7 +801,6 @@ bool CPUStats::InitCpuPowerData() {
}
}
}

if(cpuPowerData == nullptr) {
SPDLOG_ERROR("Failed to initialize CPU power data");
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class CPUStats
double m_cpuPeriod = 0;
bool m_updatedCPUs = false; // TODO use caching or just update?
bool m_inited = false;
FILE *m_cpuTempFile = nullptr;
int m_cpuTempFd = -1;
std::unique_ptr<CPUPowerData> m_cpuPowerData;

const std::map<std::string, std::string> intel_cores = {
Expand Down