Re: [OpenWrt-Devel] [PATCH] procd: Add cached and available to memory table

2019-02-11 Thread Michael Heimpold
Hi,

a few nitpicks inline...

Am Montag, 11. Februar 2019, 15:00:33 CET schrieb Zachary Cook:
> 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 
> ---
>  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;

f leaks in case of return here

> @@ -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

please stay consistent with current style and use /* comments*/ 

> + 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);

since "available" is unsigned long long, why not use atoll here?

> + else if (!strcasecmp(key, "Cached"))
> + cached =1024 * atol(val);

dito

> + }
> +
> + 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");

mhei




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


[OpenWrt-Devel] [PATCH] procd: Add cached and available to memory table

2019-02-11 Thread Zachary Cook
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 
---
 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