1
0

ram: Refactor Linux perc/used

Fixes up overly complicated line, by splitting up logic
This commit is contained in:
drkhsh 2022-10-28 00:11:15 +02:00
parent 3b86e4b5ef
commit d7ea986299

View File

@ -27,6 +27,7 @@
ram_perc(const char *unused) ram_perc(const char *unused)
{ {
uintmax_t total, free, buffers, cached; uintmax_t total, free, buffers, cached;
int percent;
if (pscanf("/proc/meminfo", if (pscanf("/proc/meminfo",
"MemTotal: %ju kB\n" "MemTotal: %ju kB\n"
@ -42,8 +43,8 @@
return NULL; return NULL;
} }
return bprintf("%d", 100 * ((total - free) - (buffers + cached)) percent = 100 * ((total - free) - (buffers + cached)) / total;
/ total); return bprintf("%d", percent);
} }
const char * const char *
@ -62,7 +63,7 @@
const char * const char *
ram_used(const char *unused) ram_used(const char *unused)
{ {
uintmax_t total, free, buffers, cached; uintmax_t total, free, buffers, cached, used;
if (pscanf("/proc/meminfo", if (pscanf("/proc/meminfo",
"MemTotal: %ju kB\n" "MemTotal: %ju kB\n"
@ -70,12 +71,11 @@
"MemAvailable: %ju kB\n" "MemAvailable: %ju kB\n"
"Buffers: %ju kB\n" "Buffers: %ju kB\n"
"Cached: %ju kB\n", "Cached: %ju kB\n",
&total, &free, &buffers, &buffers, &cached) != 5) { &total, &free, &buffers, &buffers, &cached) != 5)
return NULL; return NULL;
}
return fmt_human((total - free - buffers - cached) * 1024, used = (total - free - buffers - cached);
1024); return fmt_human(used * 1024, 1024);
} }
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
#include <stdlib.h> #include <stdlib.h>