Deal with disappearing cpus

Reinit if cpu is brought offline/online, e.g. `echo 0 > /sys/devices/system/cpu/cpu1/online`

Fix #737
pull/739/head
jackun 2 years ago
parent 5349226fa5
commit e4b23c937e
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -131,6 +131,7 @@ bool CPUStats::Init()
CPUData cpu = {};
cpu.totalTime = 1;
cpu.totalPeriod = 1;
sscanf(line.c_str(), "cpu%4d ", &cpu.cpu_id);
m_cpuData.push_back(cpu);
} else if (starts_with(line, "btime ")) {
@ -199,12 +200,12 @@ bool CPUStats::UpdateCPUData()
return false;
}
if ((size_t)cpuid >= m_cpuData.size()) {
SPDLOG_DEBUG("Cpu id '{}' is out of bounds, reiniting", cpuid);
if (cpu_count + 1 > m_cpuData.size() || m_cpuData[cpu_count].cpu_id != cpuid) {
SPDLOG_DEBUG("Cpu id '{}' is out of bounds or wrong index, reiniting", cpuid);
return Reinit();
}
CPUData& cpuData = m_cpuData[cpuid];
CPUData& cpuData = m_cpuData[cpu_count];
calculateCPUData(cpuData, usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice);
cpuid = -1;
cpu_count++;
@ -225,14 +226,14 @@ bool CPUStats::UpdateCPUData()
bool CPUStats::UpdateCoreMhz() {
m_coreMhz.clear();
FILE *fp;
for (size_t i = 0; i < m_cpuData.size(); i++)
for (auto& cpu : m_cpuData)
{
std::string path = "/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/scaling_cur_freq";
std::string path = "/sys/devices/system/cpu/cpu" + std::to_string(cpu.cpu_id) + "/cpufreq/scaling_cur_freq";
if ((fp = fopen(path.c_str(), "r"))){
int64_t temp;
if (fscanf(fp, "%" PRId64, &temp) != 1)
temp = 0;
m_cpuData[i].mhz = temp / 1000;
cpu.mhz = temp / 1000;
fclose(fp);
}
}

@ -36,6 +36,8 @@ typedef struct CPUData_ {
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
int cpu_id;
float percent;
int mhz;
int temp;

@ -265,14 +265,13 @@ void HudElements::cpu_stats(){
void HudElements::core_load(){
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_core_load]){
int i = 0;
for (const CPUData &cpuData : cpuStats.GetCPUData())
{
ImGui::TableNextRow(); ImGui::TableNextColumn();
ImGui::TextColored(HUDElements.colors.cpu, "CPU");
ImGui::SameLine(0, 1.0f);
ImGui::PushFont(HUDElements.sw_stats->font1);
ImGui::TextColored(HUDElements.colors.cpu,"%i", i);
ImGui::TextColored(HUDElements.colors.cpu,"%i", cpuData.cpu_id);
ImGui::PopFont();
ImGui::TableNextColumn();
auto text_color = HUDElements.colors.text;
@ -302,7 +301,6 @@ void HudElements::core_load(){
ImGui::PushFont(HUDElements.sw_stats->font1);
ImGui::Text("MHz");
ImGui::PopFont();
i++;
}
}
}

Loading…
Cancel
Save