diff --git a/src/common.cc b/src/common.cc index 9f15602a..34851f23 100644 --- a/src/common.cc +++ b/src/common.cc @@ -293,6 +293,14 @@ void update_stuff() { /* if you registered a callback with conky::register_cb, this will run it */ conky::run_all_callbacks(); + +#if !defined(__linux__) + /* XXX: move the following into the update_meminfo() functions? */ + if (no_buffers.get(*state)) { + info.mem -= info.bufmem; + info.memeasyfree += info.bufmem; + } +#endif } /* Ohkie to return negative values for temperatures */ diff --git a/src/linux.cc b/src/linux.cc index c1e0634e..deef45b6 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -61,6 +61,7 @@ #define _LINUX_IF_H #endif #include +#include #include #include #include @@ -186,7 +187,7 @@ int update_meminfo(void) { * These variables keep the calculations local to the function and finish off * the function by assigning the results to the information struct */ unsigned long long shmem = 0, sreclaimable = 0, curmem = 0, curbufmem = 0, - cureasyfree = 0; + cureasyfree = 0, memavail = 0; info.memmax = info.memdirty = info.swap = info.swapfree = info.swapmax = info.memwithbuffers = info.buffers = info.cached = info.memfree = @@ -211,6 +212,8 @@ int update_meminfo(void) { sscanf(buf, "%*s %llu", &info.cached); } else if (strncmp(buf, "Dirty:", 6) == 0) { sscanf(buf, "%*s %llu", &info.memdirty); + } else if (strncmp(buf, "MemAvailable:", 13) == 0) { + sscanf(buf, "%*s %llu", &memavail); } else if (strncmp(buf, "Shmem:", 6) == 0) { sscanf(buf, "%*s %llu", &shmem); } else if (strncmp(buf, "SReclaimable:", 13) == 0) { @@ -229,14 +232,25 @@ int update_meminfo(void) { */ curbufmem = (info.cached - shmem) + info.buffers + sreclaimable; - /* Now ('info.mem' - 'info.bufmem') is the *really used* (aka unreclaimable) - memory. When this value reaches the size of the physical RAM, and swap is - full or non-present, OOM happens. Therefore this is the value users want to - monitor, regarding their RAM. - */ + /* Calculate the memory usage. + * + * The Linux Kernel introduced a new field for memory available, + * when possible, use that. + * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 + */ if (no_buffers.get(*state)) { +#if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 14, 0) + /* Now ('info.mem' - 'info.bufmem') is the *really used* (aka unreclaimable) + memory. When this value reaches the size of the physical RAM, and swap is + full or non-present, OOM happens. Therefore this is the value users want + to monitor, regarding their RAM. + */ curmem -= curbufmem; cureasyfree += curbufmem; +#else + curmem = info.memmax - memavail; + cureasyfree += curbufmem; +#endif } /* Now that we know that every calculation is finished we can wrap up