Changed: Total system memory is checked at every update instead of once at start

This commit is contained in:
aristocratos 2021-10-05 09:18:04 +02:00
parent a139869216
commit 02fcb8c34a
3 changed files with 34 additions and 25 deletions

View file

@ -698,6 +698,7 @@ namespace Mem {
auto& tty_mode = Config::getB("tty_mode"); auto& tty_mode = Config::getB("tty_mode");
auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_mem")); auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_mem"));
auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6); auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6);
auto totalMem = Mem::get_totalMem();
string out; string out;
out.reserve(height * width); out.reserve(height * width);
@ -791,7 +792,7 @@ namespace Mem {
string up = (graph_height >= 2 ? Mv::l(mem_width - 2) + Mv::u(graph_height - 1) : ""); string up = (graph_height >= 2 ? Mv::l(mem_width - 2) + Mv::u(graph_height - 1) : "");
bool big_mem = mem_width > 21; bool big_mem = mem_width > 21;
out += Mv::to(y + 1, x + 2) + Theme::c("title") + Fx::b + "Total:" + rjust(floating_humanizer(Shared::totalMem), mem_width - 9) + Fx::ub + Theme::c("main_fg"); out += Mv::to(y + 1, x + 2) + Theme::c("title") + Fx::b + "Total:" + rjust(floating_humanizer(totalMem), mem_width - 9) + Fx::ub + Theme::c("main_fg");
vector<string> comb_names (mem_names.begin(), mem_names.end()); vector<string> comb_names (mem_names.begin(), mem_names.end());
if (show_swap and has_swap and not swap_disk) comb_names.insert(comb_names.end(), swap_names.begin(), swap_names.end()); if (show_swap and has_swap and not swap_disk) comb_names.insert(comb_names.end(), swap_names.begin(), swap_names.end());
for (auto name : comb_names) { for (auto name : comb_names) {
@ -1107,6 +1108,7 @@ namespace Proc {
const int y = show_detailed ? Proc::y + 8 : Proc::y; const int y = show_detailed ? Proc::y + 8 : Proc::y;
const int height = show_detailed ? Proc::height - 8 : Proc::height; const int height = show_detailed ? Proc::height - 8 : Proc::height;
const int select_max = show_detailed ? Proc::select_max - 8 : Proc::select_max; const int select_max = show_detailed ? Proc::select_max - 8 : Proc::select_max;
auto totalMem = Mem::get_totalMem();
int numpids = Proc::numpids; int numpids = Proc::numpids;
if (force_redraw) redraw = true; if (force_redraw) redraw = true;
string out; string out;
@ -1312,7 +1314,7 @@ namespace Proc {
if (item_fit >= 8) out += cjust(to_string(detailed.entry.p_nice), item_width); if (item_fit >= 8) out += cjust(to_string(detailed.entry.p_nice), item_width);
const double mem_p = (double)detailed.mem_bytes.back() * 100 / Shared::totalMem; const double mem_p = (double)detailed.mem_bytes.back() * 100 / totalMem;
string mem_str = to_string(mem_p); string mem_str = to_string(mem_p);
mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4)); mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4));
out += Mv::to(d_y + 4, d_x + 1) + Theme::c("title") + Fx::b + rjust((item_fit > 4 ? "Memory: " : "M:") + mem_str + "% ", (d_width / 3) - 2) out += Mv::to(d_y + 4, d_x + 1) + Theme::c("title") + Fx::b + rjust((item_fit > 4 ? "Memory: " : "M:") + mem_str + "% ", (d_width / 3) - 2)
@ -1370,7 +1372,7 @@ namespace Proc {
if (proc_colors) { if (proc_colors) {
end = Theme::c("main_fg") + Fx::ub; end = Theme::c("main_fg") + Fx::ub;
array<string, 3> colors; array<string, 3> colors;
for (int i = 0; int v : {(int)round(p.cpu_p), (int)round(p.mem * 100 / Shared::totalMem), (int)p.threads / 3}) { for (int i = 0; int v : {(int)round(p.cpu_p), (int)round(p.mem * 100 / totalMem), (int)p.threads / 3}) {
if (proc_gradient) { if (proc_gradient) {
int val = (min(v, 100) + 100) - calc * 100 / select_max; int val = (min(v, 100) + 100) - calc * 100 / select_max;
if (val < 100) colors[i++] = Theme::g("proc_color").at(max(0, val)); if (val < 100) colors[i++] = Theme::g("proc_color").at(max(0, val));
@ -1424,7 +1426,7 @@ namespace Proc {
} }
string mem_str = (mem_bytes ? floating_humanizer(p.mem, true) : ""); string mem_str = (mem_bytes ? floating_humanizer(p.mem, true) : "");
if (not mem_bytes) { if (not mem_bytes) {
double mem_p = clamp((double)p.mem * 100 / Shared::totalMem, 0.0, 100.0); double mem_p = clamp((double)p.mem * 100 / totalMem, 0.0, 100.0);
mem_str = to_string(mem_p); mem_str = to_string(mem_p);
if (mem_str.size() < 4) mem_str = "0"; if (mem_str.size() < 4) mem_str = "0";
else mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4)); else mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4));

View file

@ -70,8 +70,6 @@ namespace Shared {
void init(); void init();
extern long coreCount, page_size, clk_tck; extern long coreCount, page_size, clk_tck;
extern int totalMem_len;
extern uint64_t totalMem;
} }
@ -149,6 +147,9 @@ namespace Mem {
vector<string> disks_order; vector<string> disks_order;
}; };
//?* Get total system memory
uint64_t get_totalMem();
//* Collect mem & disks stats //* Collect mem & disks stats
auto collect(const bool no_update=false) -> mem_info&; auto collect(const bool no_update=false) -> mem_info&;

View file

@ -78,9 +78,7 @@ namespace Mem {
namespace Shared { namespace Shared {
fs::path procPath, passwd_path; fs::path procPath, passwd_path;
uint64_t totalMem;
long pageSize, clkTck, coreCount; long pageSize, clkTck, coreCount;
int totalMem_len;
void init() { void init() {
@ -110,17 +108,7 @@ namespace Shared {
clkTck = 100; clkTck = 100;
Logger::warning("Could not get system clock ticks per second. Defaulting to 100, processes cpu usage might be incorrect."); Logger::warning("Could not get system clock ticks per second. Defaulting to 100, processes cpu usage might be incorrect.");
} }
ifstream meminfo(Shared::procPath / "meminfo");
if (meminfo.good()) {
meminfo.ignore(SSmax, ':');
meminfo >> totalMem;
totalMem_len = to_string(totalMem).size();
totalMem <<= 10;
}
if (not meminfo.good() or totalMem == 0)
throw std::runtime_error("Could not get total memory size from /proc/meminfo");
//? Init for namespace Cpu //? Init for namespace Cpu
if (not fs::exists(Cpu::freq_path) or access(Cpu::freq_path.c_str(), R_OK) == -1) Cpu::freq_path.clear(); if (not fs::exists(Cpu::freq_path) or access(Cpu::freq_path.c_str(), R_OK) == -1) Cpu::freq_path.clear();
Cpu::current_cpu.core_percent.insert(Cpu::current_cpu.core_percent.begin(), Shared::coreCount, {}); Cpu::current_cpu.core_percent.insert(Cpu::current_cpu.core_percent.begin(), Shared::coreCount, {});
@ -673,11 +661,26 @@ namespace Mem {
mem_info current_mem {}; mem_info current_mem {};
uint64_t get_totalMem() {
ifstream meminfo(Shared::procPath / "meminfo");
int64_t totalMem;
if (meminfo.good()) {
meminfo.ignore(SSmax, ':');
meminfo >> totalMem;
totalMem <<= 10;
}
if (not meminfo.good() or totalMem == 0)
throw std::runtime_error("Could not get total memory size from /proc/meminfo");
return totalMem;
}
auto collect(const bool no_update) -> mem_info& { auto collect(const bool no_update) -> mem_info& {
if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem; if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) return current_mem;
auto& show_swap = Config::getB("show_swap"); auto& show_swap = Config::getB("show_swap");
auto& swap_disk = Config::getB("swap_disk"); auto& swap_disk = Config::getB("swap_disk");
auto& show_disks = Config::getB("show_disks"); auto& show_disks = Config::getB("show_disks");
auto totalMem = get_totalMem();
auto& mem = current_mem; auto& mem = current_mem;
static const bool snapped = (getenv("BTOP_SNAPPED") != NULL); static const bool snapped = (getenv("BTOP_SNAPPED") != NULL);
@ -714,7 +717,7 @@ namespace Mem {
meminfo.ignore(SSmax, '\n'); meminfo.ignore(SSmax, '\n');
} }
if (not got_avail) mem.stats.at("available") = mem.stats.at("free") + mem.stats.at("cached"); if (not got_avail) mem.stats.at("available") = mem.stats.at("free") + mem.stats.at("cached");
mem.stats.at("used") = Shared::totalMem - mem.stats.at("available"); mem.stats.at("used") = totalMem - mem.stats.at("available");
if (mem.stats.at("swap_total") > 0) mem.stats.at("swap_used") = mem.stats.at("swap_total") - mem.stats.at("swap_free"); if (mem.stats.at("swap_total") > 0) mem.stats.at("swap_used") = mem.stats.at("swap_total") - mem.stats.at("swap_free");
} }
else else
@ -724,7 +727,7 @@ namespace Mem {
//? Calculate percentages //? Calculate percentages
for (const auto& name : mem_names) { for (const auto& name : mem_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / Shared::totalMem)); mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / totalMem));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front(); while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front();
} }
@ -1259,7 +1262,7 @@ namespace Proc {
detailed.memory = floating_humanizer(detailed.entry.mem); detailed.memory = floating_humanizer(detailed.entry.mem);
} }
if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) { if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) {
detailed.first_mem = min((uint64_t)detailed.mem_bytes.back() * 2, Shared::totalMem); detailed.first_mem = min((uint64_t)detailed.mem_bytes.back() * 2, Mem::get_totalMem());
redraw = true; redraw = true;
} }
@ -1324,6 +1327,9 @@ namespace Proc {
else { else {
should_filter = true; should_filter = true;
auto totalMem = Mem::get_totalMem();
int totalMem_len = to_string(totalMem >> 10).size();
//? Update uid_user map if /etc/passwd changed since last run //? Update uid_user map if /etc/passwd changed since last run
if (not Shared::passwd_path.empty() and fs::last_write_time(Shared::passwd_path) != passwd_time) { if (not Shared::passwd_path.empty() and fs::last_write_time(Shared::passwd_path) != passwd_time) {
string r_uid, r_user; string r_uid, r_user;
@ -1460,8 +1466,8 @@ namespace Proc {
next_x = 24; next_x = 24;
continue; continue;
case 24: //? RSS memory (can be inaccurate, but parsing smaps increases total cpu usage by ~20x) case 24: //? RSS memory (can be inaccurate, but parsing smaps increases total cpu usage by ~20x)
if (cmp_greater(short_str.size(), Shared::totalMem_len)) if (cmp_greater(short_str.size(), totalMem_len))
new_proc.mem = Shared::totalMem; new_proc.mem = totalMem;
else else
new_proc.mem = stoull(short_str) * Shared::pageSize; new_proc.mem = stoull(short_str) * Shared::pageSize;
} }
@ -1477,7 +1483,7 @@ namespace Proc {
if (x-offset < 24) continue; if (x-offset < 24) continue;
//? Get RSS memory from /proc/[pid]/statm if value from /proc/[pid]/stat looks wrong //? Get RSS memory from /proc/[pid]/statm if value from /proc/[pid]/stat looks wrong
if (new_proc.mem >= Shared::totalMem) { if (new_proc.mem >= totalMem) {
pread.open(d.path() / "statm"); pread.open(d.path() / "statm");
if (not pread.good()) continue; if (not pread.good()) continue;
pread.ignore(SSmax, ' '); pread.ignore(SSmax, ' ');