davemds pushed a commit to branch master.

http://git.enlightenment.org/core/enlightenment.git/commit/?id=1c8ca780daa4e294fea8550c8fa51dfcd181326c

commit 1c8ca780daa4e294fea8550c8fa51dfcd181326c
Author: Dave Andreoli <[email protected]>
Date:   Sat Jan 28 17:52:56 2017 +0100

    mem gadget: improve proc info fetching
    
    * collect more info than just 2 percentage
    * improve performance by parsing proc only onece every loop
    * use active memory to calc percentage, now the value is near the other mem 
tools I have
---
 src/modules/sysinfo/memusage/memusage.c      |  17 +++-
 src/modules/sysinfo/memusage/memusage.h      |  10 ++-
 src/modules/sysinfo/memusage/memusage_proc.c | 123 ++++++++++++---------------
 3 files changed, 79 insertions(+), 71 deletions(-)

diff --git a/src/modules/sysinfo/memusage/memusage.c 
b/src/modules/sysinfo/memusage/memusage.c
index 77e2e64..61ae187 100644
--- a/src/modules/sysinfo/memusage/memusage.c
+++ b/src/modules/sysinfo/memusage/memusage.c
@@ -8,6 +8,13 @@ struct _Thread_Config
    Instance *inst;
    int memstatus;
    int swapstatus;
+
+   unsigned long mem_total;
+   unsigned long mem_active;
+   unsigned long mem_cached;
+   unsigned long mem_buffers;
+   unsigned long swp_total;
+   unsigned long swp_active;
 };
 
 static void
@@ -130,8 +137,14 @@ _memusage_cb_usage_check_main(void *data, Ecore_Thread *th)
    for (;;)
      {
         if (ecore_thread_check(th)) break;
-        thc->memstatus = _memusage_proc_getmemusage();
-        thc->swapstatus = _memusage_proc_getswapusage();
+        _memusage_proc_getusage(&thc->mem_total, &thc->mem_active,
+                                &thc->mem_cached, &thc->mem_buffers,
+                                &thc->swp_total, &thc->swp_active);
+        if (thc->mem_total > 0)
+          thc->memstatus = 100 * ((float)thc->mem_active / 
(float)thc->mem_total);
+        if (thc->swp_total > 0)
+          thc->swapstatus = 100 * ((float)thc->swp_active / 
(float)thc->swp_total);
+
         ecore_thread_feedback(th, NULL);
         if (ecore_thread_check(th)) break;
         usleep((1000000.0 / 8.0) * (double)thc->interval);
diff --git a/src/modules/sysinfo/memusage/memusage.h 
b/src/modules/sysinfo/memusage/memusage.h
index b73f49c..c07736f 100644
--- a/src/modules/sysinfo/memusage/memusage.h
+++ b/src/modules/sysinfo/memusage/memusage.h
@@ -4,7 +4,13 @@
 #include "../sysinfo.h"
 
 void _memusage_config_updated(Instance *inst);
-int _memusage_proc_getmemusage();
-int _memusage_proc_getswapusage();
 Evas_Object *memusage_configure(Instance *inst);
+
+void _memusage_proc_getusage(unsigned long *mem_total,
+                             unsigned long *mem_active,
+                             unsigned long *mem_cached,
+                             unsigned long *mem_buffers,
+                             unsigned long *swp_total,
+                             unsigned long *swp_active);
+
 #endif
diff --git a/src/modules/sysinfo/memusage/memusage_proc.c 
b/src/modules/sysinfo/memusage/memusage_proc.c
index 9a91196..49f2bc2 100644
--- a/src/modules/sysinfo/memusage/memusage_proc.c
+++ b/src/modules/sysinfo/memusage/memusage_proc.c
@@ -1,84 +1,73 @@
 #include "memusage.h"
 
-int _memusage_proc_getswapusage(void)
-{
-   long swap_total = 0, swap_free = 0, swap_used = 0;
-   int percent = 0;
-   char buf[4096], *line, *tok;
-   FILE *f;
-
-   f = fopen("/proc/meminfo", "r");
-   if (f)
-     {
-        while(fgets(buf, sizeof(buf), f) != NULL)
-          {
-             if (!strncmp("SwapTotal:", buf, 10))
-               {
-                  line = strchr(buf, ':')+1;
-                  while(isspace(*line)) line++;
-                  tok = strtok(line, " ");
-                  swap_total = atol(tok);
-               }
-             else if (!strncmp("SwapFree:", buf, 9))
-               {
-                  line = strchr(buf, ':')+1;
-                  while(isspace(*line)) line++;
-                  tok = strtok(line, " ");
-                  swap_free = atol(tok);
-               }
-             if (swap_total && swap_free)
-               break;
-          }
-        fclose(f);
 
-        swap_used = swap_total - swap_free;
-        if (swap_total > 0)
-          percent = 100 * ((float)swap_used / (float)swap_total);
-     }
-   if (percent > 100) percent = 100;
-   else if (percent < 0) percent = 0;
+unsigned long _line_parse(const char *line)
+{
+   char *p, *tok;
 
-   return percent;
+   p = strchr(line, ':') + 1;
+   while(isspace(*p)) p++;
+   tok = strtok(p, " ");
+   return atol(tok);
 }
 
-int _memusage_proc_getmemusage(void)
+void _memusage_proc_getusage(unsigned long *mem_total,
+                             unsigned long *mem_active,
+                             unsigned long *mem_cached,
+                             unsigned long *mem_buffers,
+                             unsigned long *swp_total,
+                             unsigned long *swp_active)
 {
-   long mem_total = 0, mem_free = 0, mem_used = 0;
-   int percent = 0;
-   char buf[4096], *line, *tok;
+   char line[256];
+   int found = 0;
+   long tmp_swp_total = -1;
+   long tmp_swp_free = -1;
    FILE *f;
 
    f = fopen("/proc/meminfo", "r");
-   if (f)
+   if (!f) return;
+
+   while(fgets(line, sizeof(line), f) != NULL)
      {
-        while(fgets(buf, sizeof(buf), f) != NULL)
+        if (!strncmp("MemTotal:", line, 9))
+          {
+             *mem_total = _line_parse(line);
+             found++;
+          }
+        else if (!strncmp("Active:", line, 7))
           {
-             if (!strncmp("MemTotal:", buf, 9))
-               {
-                  line = strchr(buf, ':')+1;
-                  while(isspace(*line)) line++;
-                  tok = strtok(line, " ");
-                  mem_total = atol(tok);
-               }
-             else if (!strncmp("MemFree:", buf, 8))
-               {
-                  line = strchr(buf, ':')+1;
-                  while(isspace(*line)) line++;
-                  tok = strtok(line, " ");
-                  mem_free = atol(tok);
-               }
-             if (mem_total && mem_free)
-               break;
+             *mem_active = _line_parse(line);
+             found++;
+          }
+        else if (!strncmp("Cached:", line, 7))
+          {
+             *mem_cached = _line_parse(line);
+             found++;
+          }
+        else if (!strncmp("Buffers:", line, 8))
+          {
+             *mem_buffers = _line_parse(line);
+             found++;
+          }
+        else if (!strncmp("SwapTotal:", line, 10))
+          {
+             tmp_swp_total = _line_parse(line);
+             found++;
+          }
+        else if (!strncmp("SwapFree:", line, 9))
+          {
+             tmp_swp_free = _line_parse(line);
+             found++;
           }
-        fclose(f);
 
-        mem_used = mem_total - mem_free;
-        if (mem_total > 0)
-          percent = 100 * ((float)mem_used / (float)mem_total);
+         if (found >= 6)
+           break;
      }
-   if (percent > 100) percent = 100;
-   else if (percent < 0) percent = 0;
+   fclose(f);
 
-   return percent;
+   if ((tmp_swp_total != -1) && (tmp_swp_free != -1))
+     {
+        *swp_total = tmp_swp_total;
+        *swp_active = tmp_swp_total - tmp_swp_free;
+     }
 }
-

-- 


Reply via email to