summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZachary Cook2019-10-08 05:02:50 +0000
committerHans Dedecker2019-10-17 19:36:48 +0000
commit258aa04328a20213b12228d01d494c1e22f8d510 (patch)
tree724e3d977004c8e265c26a4121dab64fe940dc79
parent8e9fb51fa66e614620a97d371176610e7e1d0010 (diff)
downloadprocd-258aa04328a20213b12228d01d494c1e22f8d510.tar.gz
procd: Add cached and available to memory table
Provides a better measure of actual system memory usage for Luci/users. "cached" will be used to add a new progress bar, "available" is the kernel's estimate of memory that is actually useable, and is more accurate than (memory.free + memory.buffered) that Luci currently uses to calculate available memory. Signed-off-by: Zachary Cook <zachcook1991@gmail.com>
-rw-r--r--system.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/system.c b/system.c
index 751a016..9879bca 100644
--- a/system.c
+++ b/system.c
@@ -230,9 +230,36 @@ 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 (sysinfo(&info))
return UBUS_STATUS_UNKNOWN_ERROR;
+
+ if ((f = fopen("/proc/meminfo", "r")) == NULL)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ /* 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 * atoll(val);
+ else if (!strcasecmp(key, "Cached"))
+ cached = 1024 * atoll(val);
+ }
+
+ fclose(f);
#endif
now = time(NULL);
@@ -262,6 +289,8 @@ static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
(uint64_t)info.mem_unit * (uint64_t)info.sharedram);
blobmsg_add_u64(&b, "buffered",
(uint64_t)info.mem_unit * (uint64_t)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");