Merge pull request #3499 from gergo-salyi/cpuhotplug
Handle offline CPUs and CPU hotplug
This commit is contained in:
commit
4ae144a724
|
@ -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::vector<std::tuple<size_t, size_t>> curr_times = CpuUsage::parseCpuinfo();
|
||||||
std::string tooltip;
|
std::string tooltip;
|
||||||
std::vector<uint16_t> usage;
|
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) {
|
for (size_t i = 0; i < curr_times.size(); ++i) {
|
||||||
auto [curr_idle, curr_total] = curr_times[i];
|
auto [curr_idle, curr_total] = curr_times[i];
|
||||||
auto [prev_idle, prev_total] = prev_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_idle = curr_idle - prev_idle;
|
||||||
const float delta_total = curr_total - prev_total;
|
const float delta_total = curr_total - prev_total;
|
||||||
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
|
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
|
||||||
|
|
|
@ -3,6 +3,23 @@
|
||||||
#include "modules/cpu_usage.hpp"
|
#include "modules/cpu_usage.hpp"
|
||||||
|
|
||||||
std::vector<std::tuple<size_t, size_t>> waybar::modules::CpuUsage::parseCpuinfo() {
|
std::vector<std::tuple<size_t, size_t>> waybar::modules::CpuUsage::parseCpuinfo() {
|
||||||
|
// Get the "existing CPU count" from /sys/devices/system/cpu/present
|
||||||
|
// Probably this is what the user wants the offline CPUs accounted from
|
||||||
|
// For further details see:
|
||||||
|
// https://www.kernel.org/doc/html/latest/core-api/cpu_hotplug.html
|
||||||
|
const std::string sys_cpu_present_path = "/sys/devices/system/cpu/present";
|
||||||
|
size_t cpu_present_last = 0;
|
||||||
|
std::ifstream cpu_present_file(sys_cpu_present_path);
|
||||||
|
std::string cpu_present_text;
|
||||||
|
if (cpu_present_file.is_open()) {
|
||||||
|
getline(cpu_present_file, cpu_present_text);
|
||||||
|
// This is a comma-separated list of ranges, eg. 0,2-4,7
|
||||||
|
size_t last_separator = cpu_present_text.find_last_of("-,");
|
||||||
|
if (last_separator < cpu_present_text.size()) {
|
||||||
|
std::stringstream(cpu_present_text.substr(last_separator + 1)) >> cpu_present_last;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const std::string data_dir_ = "/proc/stat";
|
const std::string data_dir_ = "/proc/stat";
|
||||||
std::ifstream info(data_dir_);
|
std::ifstream info(data_dir_);
|
||||||
if (!info.is_open()) {
|
if (!info.is_open()) {
|
||||||
|
@ -10,14 +27,23 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::CpuUsage::parseCpuinfo(
|
||||||
}
|
}
|
||||||
std::vector<std::tuple<size_t, size_t>> cpuinfo;
|
std::vector<std::tuple<size_t, size_t>> cpuinfo;
|
||||||
std::string line;
|
std::string line;
|
||||||
|
size_t current_cpu_number = -1; // First line is total, second line is cpu 0
|
||||||
while (getline(info, line)) {
|
while (getline(info, line)) {
|
||||||
if (line.substr(0, 3).compare("cpu") != 0) {
|
if (line.substr(0, 3).compare("cpu") != 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
size_t line_cpu_number;
|
||||||
|
if (current_cpu_number >= 0) {
|
||||||
|
std::stringstream(line.substr(3)) >> line_cpu_number;
|
||||||
|
while (line_cpu_number > current_cpu_number) {
|
||||||
|
// Fill in 0 for offline CPUs missing inside the lines of /proc/stat
|
||||||
|
cpuinfo.emplace_back(0, 0);
|
||||||
|
current_cpu_number++;
|
||||||
|
}
|
||||||
|
}
|
||||||
std::stringstream sline(line.substr(5));
|
std::stringstream sline(line.substr(5));
|
||||||
std::vector<size_t> times;
|
std::vector<size_t> times;
|
||||||
for (size_t time = 0; sline >> time; times.push_back(time))
|
for (size_t time = 0; sline >> time; times.push_back(time));
|
||||||
;
|
|
||||||
|
|
||||||
size_t idle_time = 0;
|
size_t idle_time = 0;
|
||||||
size_t total_time = 0;
|
size_t total_time = 0;
|
||||||
|
@ -27,6 +53,14 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::CpuUsage::parseCpuinfo(
|
||||||
total_time = std::accumulate(times.begin(), times.end(), 0);
|
total_time = std::accumulate(times.begin(), times.end(), 0);
|
||||||
}
|
}
|
||||||
cpuinfo.emplace_back(idle_time, total_time);
|
cpuinfo.emplace_back(idle_time, total_time);
|
||||||
|
current_cpu_number++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (cpu_present_last >= current_cpu_number) {
|
||||||
|
// Fill in 0 for offline CPUs missing after the lines of /proc/stat
|
||||||
|
cpuinfo.emplace_back(0, 0);
|
||||||
|
current_cpu_number++;
|
||||||
|
}
|
||||||
|
|
||||||
return cpuinfo;
|
return cpuinfo;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue