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();
}
bool CPUStats::Reinit()
{
m_inited = false;
return Init();
}
//TODO take sampling interval into account?
bool CPUStats::UpdateCPUData()
{
unsigned long long int usertime, nicetime, systemtime, idletime;
unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice;
int cpuid = -1;
size_t cpu_count = 0;
if (!m_inited)
return false;
@ -189,20 +196,29 @@ bool CPUStats::UpdateCPUData()
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;
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];
calculateCPUData(cpuData, usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice);
cpuid = -1;
cpu_count++;
} else {
break;
}
} while(true);
if (cpu_count < m_cpuData.size())
m_cpuData.resize(cpu_count);
m_cpuPeriod = (double)m_cpuData[0].totalPeriod / m_cpuData.size();
m_updatedCPUs = true;
return ret;

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

Loading…
Cancel
Save