Re-init cpu stats if core count changes; fix off-by one error

Helps with #438
pull/445/head
jackun 3 years ago
parent 6ad9791ceb
commit ae410f01e7
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -152,12 +152,19 @@ bool CPUStats::Init()
return UpdateCPUData(); return UpdateCPUData();
} }
bool CPUStats::Reinit()
{
m_inited = false;
return Init();
}
//TODO take sampling interval into account? //TODO take sampling interval into account?
bool CPUStats::UpdateCPUData() bool CPUStats::UpdateCPUData()
{ {
unsigned long long int usertime, nicetime, systemtime, idletime; unsigned long long int usertime, nicetime, systemtime, idletime;
unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice; unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice;
int cpuid = -1; int cpuid = -1;
size_t cpu_count = 0;
if (!m_inited) if (!m_inited)
return false; return false;
@ -189,20 +196,29 @@ bool CPUStats::UpdateCPUData()
return false; return false;
} }
if (cpuid < 0 /* can it? */ || (size_t)cpuid > m_cpuData.size()) { if (cpuid < 0 /* can it? */) {
std::cerr << "Cpu id '" << cpuid << "' is out of bounds" << std::endl; std::cerr << "Cpu id '" << cpuid << "' is out of bounds" << std::endl;
return false; return false;
} }
if ((size_t)cpuid >= m_cpuData.size()) {
std::cerr << "Cpu id '" << cpuid << "' is out of bounds, reiniting" << std::endl;
return Reinit();
}
CPUData& cpuData = m_cpuData[cpuid]; CPUData& cpuData = m_cpuData[cpuid];
calculateCPUData(cpuData, usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice); calculateCPUData(cpuData, usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice);
cpuid = -1; cpuid = -1;
cpu_count++;
} else { } else {
break; break;
} }
} while(true); } while(true);
if (cpu_count < m_cpuData.size())
m_cpuData.resize(cpu_count);
m_cpuPeriod = (double)m_cpuData[0].totalPeriod / m_cpuData.size(); m_cpuPeriod = (double)m_cpuData[0].totalPeriod / m_cpuData.size();
m_updatedCPUs = true; m_updatedCPUs = true;
return ret; return ret;

@ -112,6 +112,7 @@ public:
CPUStats(); CPUStats();
~CPUStats(); ~CPUStats();
bool Init(); bool Init();
bool Reinit();
bool Updated() bool Updated()
{ {
return m_updatedCPUs; return m_updatedCPUs;

Loading…
Cancel
Save