This should allow Luci to show a better measure of system memory usage
Cached memory will be used to add a new progress bar
MemAvailable is the kernel's estimate of memory that is useable
by userspace without swapping, and is more accurate than the current
memory.free + memory.buffered calculation for total memory available

Signed-off-by: Zachary Cook <zachcook1...@gmail.com>
---
 system.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/system.c b/system.c
index 8ed3f93..501d589 100644
--- a/system.c
+++ b/system.c
@@ -228,6 +228,13 @@ static int system_info(struct ubus_context *ctx, struct 
ubus_object *obj,
 #ifdef linux
        struct sysinfo info;
        void *c;
+       char line[256];
+       char *key, *val;
+       unsigned long long available, cached;
+       FILE *f;
+
+       if ((f = fopen("/proc/meminfo", "r")) == NULL)
+               return UBUS_STATUS_UNKNOWN_ERROR;
 
        if (sysinfo(&info))
                return UBUS_STATUS_UNKNOWN_ERROR;
@@ -251,11 +258,33 @@ static int system_info(struct ubus_context *ctx, struct 
ubus_object *obj,
        blobmsg_add_u32(&b, NULL, info.loads[2]);
        blobmsg_close_array(&b, c);
 
+       //if linux < 3.14 MemAvailable is not in meminfo
+       available = 0;
+       cached = 0;
+
+       while(fgets(line, sizeof(line), f))
+       {
+               key = strtok(line, " :");
+               val = strtok(NULL, " ");
+
+               if (!key || !val)
+                       continue;
+
+               if (!strcasecmp(key, "MemAvailable"))
+                       available = 1024 * atol(val);
+               else if (!strcasecmp(key, "Cached"))
+                       cached =    1024 * atol(val);
+       }
+
+       fclose(f);
+
        c = blobmsg_open_table(&b, "memory");
        blobmsg_add_u64(&b, "total",    info.mem_unit * info.totalram);
        blobmsg_add_u64(&b, "free",     info.mem_unit * info.freeram);
        blobmsg_add_u64(&b, "shared",   info.mem_unit * info.sharedram);
        blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram);
+       blobmsg_add_u64(&b, "available",available);
+       blobmsg_add_u64(&b, "cached",   cached);
        blobmsg_close_table(&b, c);
 
        c = blobmsg_open_table(&b, "swap");
-- 
2.20.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to