Skip to content

Commit

Permalink
Fixed: Use /proc/pid/statm if RSS memory from /proc/pid/stat is faulty
Browse files Browse the repository at this point in the history
  • Loading branch information
aristocratos committed Sep 21, 2021
1 parent 8288d7c commit 6d11c8b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace Shared {
void init();

extern long coreCount, page_size, clk_tck;
extern int totalMem_len;
extern uint64_t totalMem;
}

Expand Down
17 changes: 16 additions & 1 deletion src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace Shared {
fs::path procPath, passwd_path;
uint64_t totalMem;
long pageSize, clkTck, coreCount;
int totalMem_len;

void init() {

Expand Down Expand Up @@ -114,6 +115,7 @@ namespace Shared {
if (meminfo.good()) {
meminfo.ignore(SSmax, ':');
meminfo >> totalMem;
totalMem_len = to_string(totalMem).size();
totalMem <<= 10;
}
if (not meminfo.good() or totalMem == 0)
Expand Down Expand Up @@ -1451,7 +1453,10 @@ namespace Proc {
next_x = 24;
continue;
case 24: //? RSS memory (can be inaccurate, but parsing smaps increases total cpu usage by ~20x)
new_proc.mem = stoull(short_str) * Shared::pageSize;
if (cmp_greater(short_str.size(), Shared::totalMem_len))
new_proc.mem = Shared::totalMem;
else
new_proc.mem = stoull(short_str) * Shared::pageSize;
}
break;
}
Expand All @@ -1464,6 +1469,16 @@ 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) {
pread.open(d.path() / "statm");
if (not pread.good()) continue;
pread.ignore(SSmax, ' ');
pread >> new_proc.mem;
new_proc.mem *= Shared::pageSize;
pread.close();
}

//? Process cpu usage since last update
new_proc.cpu_p = clamp(round(cmult * 1000 * (cpu_t - new_proc.cpu_t) / max((uint64_t)1, cputimes - old_cputimes)) / 10.0, 0.0, 100.0 * Shared::coreCount);

Expand Down

0 comments on commit 6d11c8b

Please sign in to comment.