Handle offline CPUs and CPU hotplug

First of all in case when the number CPUs change
prevent out-of-bound index access in
waybar::modules::CpuUsage::getCpuUsage()

Secondly on Linux when updating CPU usage
read /sys/devices/system/cpu/present
and use it to detect the offline CPUs missing from /proc/stat

For offline CPUs report 0 usage and "offline" in the tooltip

Fixes issue #3498

On Linux one can test this functionality with:
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online

On non-Linux OSes I'm not sure how to detect offline CPUs,
so I didn't add the offline CPU detection there
but at least CPU number change should not cause a crash there anymore
or cause memory safety issues after this fix
This commit is contained in:
Gergő Sályi
2024-08-02 15:21:01 +02:00
parent 003dd3a9a2
commit 24a9886952
2 changed files with 63 additions and 2 deletions

View File

@ -61,9 +61,36 @@ std::tuple<std::vector<uint16_t>, std::string> waybar::modules::CpuUsage::getCpu
std::vector<std::tuple<size_t, size_t>> curr_times = CpuUsage::parseCpuinfo();
std::string tooltip;
std::vector<uint16_t> usage;
if (curr_times.size() != prev_times.size()) {
// The number of CPUs has changed, eg. due to CPU hotplug
// We don't know which CPU came up or went down
// so only give total usage (if we can)
if (!curr_times.empty() && !prev_times.empty()) {
auto [curr_idle, curr_total] = curr_times[0];
auto [prev_idle, prev_total] = prev_times[0];
const float delta_idle = curr_idle - prev_idle;
const float delta_total = curr_total - prev_total;
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
tooltip = fmt::format("Total: {}%\nCores: (pending)", tmp);
usage.push_back(tmp);
} else {
tooltip = "(pending)";
usage.push_back(0);
}
prev_times = curr_times;
return {usage, tooltip};
}
for (size_t i = 0; i < curr_times.size(); ++i) {
auto [curr_idle, curr_total] = curr_times[i];
auto [prev_idle, prev_total] = prev_times[i];
if (i > 0 && (curr_total == 0 || prev_total == 0)) {
// This CPU is offline
tooltip = tooltip + fmt::format("\nCore{}: offline", i - 1);
usage.push_back(0);
continue;
}
const float delta_idle = curr_idle - prev_idle;
const float delta_total = curr_total - prev_total;
uint16_t tmp = 100 * (1 - delta_idle / delta_total);