From 02fcb8c34a18f96574f05750e32c7cf861341e20 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Tue, 5 Oct 2021 09:18:04 +0200 Subject: [PATCH] Changed: Total system memory is checked at every update instead of once at start --- src/btop_draw.cpp | 10 +++++---- src/btop_shared.hpp | 5 +++-- src/linux/btop_collect.cpp | 44 ++++++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index 6a8cf68..d290832 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -698,6 +698,7 @@ namespace Mem { auto& tty_mode = Config::getB("tty_mode"); 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 totalMem = Mem::get_totalMem(); string out; 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) : ""); 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 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()); for (auto name : comb_names) { @@ -1107,6 +1108,7 @@ namespace Proc { const int y = show_detailed ? Proc::y + 8 : Proc::y; const int height = show_detailed ? Proc::height - 8 : Proc::height; const int select_max = show_detailed ? Proc::select_max - 8 : Proc::select_max; + auto totalMem = Mem::get_totalMem(); int numpids = Proc::numpids; if (force_redraw) redraw = true; string out; @@ -1312,7 +1314,7 @@ namespace Proc { 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); 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) @@ -1370,7 +1372,7 @@ namespace Proc { if (proc_colors) { end = Theme::c("main_fg") + Fx::ub; array 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) { int val = (min(v, 100) + 100) - calc * 100 / select_max; 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) : ""); 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); if (mem_str.size() < 4) mem_str = "0"; else mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4)); diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 4d0f323..832208c 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -70,8 +70,6 @@ namespace Shared { void init(); extern long coreCount, page_size, clk_tck; - extern int totalMem_len; - extern uint64_t totalMem; } @@ -149,6 +147,9 @@ namespace Mem { vector disks_order; }; + //?* Get total system memory + uint64_t get_totalMem(); + //* Collect mem & disks stats auto collect(const bool no_update=false) -> mem_info&; diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 56730e7..ea36b89 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -78,9 +78,7 @@ namespace Mem { namespace Shared { fs::path procPath, passwd_path; - uint64_t totalMem; long pageSize, clkTck, coreCount; - int totalMem_len; void init() { @@ -110,17 +108,7 @@ namespace Shared { clkTck = 100; 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 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, {}); @@ -673,11 +661,26 @@ namespace 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& { 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& swap_disk = Config::getB("swap_disk"); auto& show_disks = Config::getB("show_disks"); + auto totalMem = get_totalMem(); auto& mem = current_mem; static const bool snapped = (getenv("BTOP_SNAPPED") != NULL); @@ -714,7 +717,7 @@ namespace Mem { meminfo.ignore(SSmax, '\n'); } 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"); } else @@ -724,7 +727,7 @@ namespace Mem { //? Calculate percentages 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(); } @@ -1259,7 +1262,7 @@ namespace Proc { 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) { - 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; } @@ -1324,6 +1327,9 @@ namespace Proc { else { 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 if (not Shared::passwd_path.empty() and fs::last_write_time(Shared::passwd_path) != passwd_time) { string r_uid, r_user; @@ -1460,8 +1466,8 @@ namespace Proc { next_x = 24; continue; 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)) - new_proc.mem = Shared::totalMem; + if (cmp_greater(short_str.size(), totalMem_len)) + new_proc.mem = totalMem; else new_proc.mem = stoull(short_str) * Shared::pageSize; } @@ -1477,7 +1483,7 @@ namespace Proc { if (x-offset < 24) continue; //? 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"); if (not pread.good()) continue; pread.ignore(SSmax, ' ');