Added: Dynamic updating of max number of CPU cores

This commit is contained in:
aristocratos 2022-10-07 20:45:12 +02:00
parent 14444760c4
commit 4f45b9b25f
3 changed files with 48 additions and 8 deletions

View file

@ -318,6 +318,7 @@ namespace Runner {
atomic<bool> stopping (false); atomic<bool> stopping (false);
atomic<bool> waiting (false); atomic<bool> waiting (false);
atomic<bool> redraw (false); atomic<bool> redraw (false);
atomic<bool> coreNum_reset (false);
//* Setup semaphore for triggering thread to do work //* Setup semaphore for triggering thread to do work
#if __GNUC__ < 11 #if __GNUC__ < 11
@ -475,6 +476,14 @@ namespace Runner {
//? Start collect //? Start collect
auto cpu = Cpu::collect(conf.no_update); auto cpu = Cpu::collect(conf.no_update);
if (coreNum_reset) {
coreNum_reset = false;
Cpu::core_mapping = Cpu::get_core_mapping();
Global::resized = true;
Input::interrupt = true;
continue;
}
if (Global::debug) debug_timer("cpu", draw_begin); if (Global::debug) debug_timer("cpu", draw_begin);
//? Draw box //? Draw box

View file

@ -63,6 +63,7 @@ namespace Runner {
extern atomic<bool> reading; extern atomic<bool> reading;
extern atomic<bool> stopping; extern atomic<bool> stopping;
extern atomic<bool> redraw; extern atomic<bool> redraw;
extern atomic<bool> coreNum_reset;
extern pthread_t runner_id; extern pthread_t runner_id;
extern bool pause_output; extern bool pause_output;
extern string debug_bg; extern string debug_bg;

View file

@ -135,6 +135,7 @@ namespace Shared {
Cpu::core_old_totals.insert(Cpu::core_old_totals.begin(), Shared::coreCount, 0); Cpu::core_old_totals.insert(Cpu::core_old_totals.begin(), Shared::coreCount, 0);
Cpu::core_old_idles.insert(Cpu::core_old_idles.begin(), Shared::coreCount, 0); Cpu::core_old_idles.insert(Cpu::core_old_idles.begin(), Shared::coreCount, 0);
Cpu::collect(); Cpu::collect();
if (Runner::coreNum_reset) Runner::coreNum_reset = false;
for (auto& [field, vec] : Cpu::current_cpu.cpu_percent) { for (auto& [field, vec] : Cpu::current_cpu.cpu_percent) {
if (not vec.empty()) Cpu::available_fields.push_back(field); if (not vec.empty()) Cpu::available_fields.push_back(field);
} }
@ -681,22 +682,37 @@ namespace Cpu {
string cpu_name; string cpu_name;
cread.open(Shared::procPath / "stat"); cread.open(Shared::procPath / "stat");
int i = 0; int i = 0;
for (; i <= Shared::coreCount; i++) { int target = Shared::coreCount;
for (; i <= target or (cread.good() and cread.peek() == 'c'); i++) {
//? Make sure to add zero value for missing core values if at end of file //? Make sure to add zero value for missing core values if at end of file
if ((not cread.good() or cread.peek() != 'c') and i <= Shared::coreCount) { if ((not cread.good() or cread.peek() != 'c') and i <= target) {
if (i == 0) throw std::runtime_error("Failed to parse /proc/stat"); if (i == 0) throw std::runtime_error("Failed to parse /proc/stat");
else cpu.core_percent.at(i-1).push_back(0); else {
//? Fix container sizes if new cores are detected
while (cmp_less(cpu.core_percent.size(), i)) {
core_old_totals.push_back(0);
core_old_idles.push_back(0);
cpu.core_percent.push_back({});
}
cpu.core_percent.at(i-1).push_back(0);
}
} }
else { else {
if (i == 0) cread.ignore(SSmax, ' '); if (i == 0) cread.ignore(SSmax, ' ');
else { else {
cread >> cpu_name; cread >> cpu_name;
int cpuNum = std::stoi(cpu_name.substr(3)); int cpuNum = std::stoi(cpu_name.substr(3));
if (cpuNum > Shared::coreCount - 1) throw std::runtime_error("Mismatch betweeen /proc/stat core count and previously detected core count"); if (cpuNum >= target - 1) target = cpuNum + (cread.peek() == 'c' ? 2 : 1);
//? Add zero value for core if core number is missing from /proc/stat //? Add zero value for core if core number is missing from /proc/stat
while (i - 1 < cpuNum) { while (i - 1 < cpuNum) {
cpu.core_percent.at(i-1).push_back(0); //? Fix container sizes if new cores are detected
while (cmp_less(cpu.core_percent.size(), i)) {
core_old_totals.push_back(0);
core_old_idles.push_back(0);
cpu.core_percent.push_back({});
}
cpu.core_percent[i-1].push_back(0);
if (cpu.core_percent.at(i-1).size() > 40) cpu.core_percent.at(i-1).pop_front(); if (cpu.core_percent.at(i-1).size() > 40) cpu.core_percent.at(i-1).pop_front();
i++; i++;
} }
@ -745,7 +761,12 @@ namespace Cpu {
} }
//? Calculate cpu total for each core //? Calculate cpu total for each core
else { else {
if (i > Shared::coreCount) break; //? Fix container sizes if new cores are detected
while (cmp_less(cpu.core_percent.size(), i)) {
core_old_totals.push_back(0);
core_old_idles.push_back(0);
cpu.core_percent.push_back({});
}
const long long calc_totals = max(0ll, totals - core_old_totals.at(i-1)); const long long calc_totals = max(0ll, totals - core_old_totals.at(i-1));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i-1)); const long long calc_idles = max(0ll, idles - core_old_idles.at(i-1));
core_old_totals.at(i-1) = totals; core_old_totals.at(i-1) = totals;
@ -759,7 +780,14 @@ namespace Cpu {
if (cpu.core_percent.at(i-1).size() > 40) cpu.core_percent.at(i-1).pop_front(); if (cpu.core_percent.at(i-1).size() > 40) cpu.core_percent.at(i-1).pop_front();
} }
if (i < Shared::coreCount + 1) throw std::runtime_error("Failed to parse /proc/stat"); //? Notify main thread to redraw screen if we found more cores than previously detected
if (cmp_greater(cpu.core_percent.size(), Shared::coreCount)) {
Logger::debug("Changing CPU max corecount from " + to_string(Shared::coreCount) + " to " + to_string(cpu.core_percent.size()) + ".");
Runner::coreNum_reset = true;
Shared::coreCount = cpu.core_percent.size();
while (cmp_less(current_cpu.temp.size(), cpu.core_percent.size() + 1)) current_cpu.temp.push_back({0});
}
} }
catch (const std::exception& e) { catch (const std::exception& e) {
Logger::debug("Cpu::collect() : " + string{e.what()}); Logger::debug("Cpu::collect() : " + string{e.what()});
@ -1329,6 +1357,7 @@ namespace Net {
}; };
auto collect(bool no_update) -> net_info& { auto collect(bool no_update) -> net_info& {
if (Runner::stopping) return empty_net;
auto& net = current_net; auto& net = current_net;
auto& config_iface = Config::getS("net_iface"); auto& config_iface = Config::getS("net_iface");
auto net_sync = Config::getB("net_sync"); auto net_sync = Config::getB("net_sync");
@ -1617,6 +1646,7 @@ namespace Proc {
//* Collects and sorts process information from /proc //* Collects and sorts process information from /proc
auto collect(bool no_update) -> vector<proc_info>& { auto collect(bool no_update) -> vector<proc_info>& {
if (Runner::stopping) return current_procs;
const auto& sorting = Config::getS("proc_sorting"); const auto& sorting = Config::getS("proc_sorting");
auto reverse = Config::getB("proc_reversed"); auto reverse = Config::getB("proc_reversed");
const auto& filter = Config::getS("proc_filter"); const auto& filter = Config::getS("proc_filter");