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
This commit is contained in:
Davide Andreoli 2017-01-28 17:52:56 +01:00
parent b55ce098bf
commit 1c8ca780da
3 changed files with 81 additions and 73 deletions

View File

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

View File

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

View File

@ -1,84 +1,73 @@
#include "memusage.h"
int _memusage_proc_getswapusage(void)
unsigned long _line_parse(const char *line)
{
long swap_total = 0, swap_free = 0, swap_used = 0;
int percent = 0;
char buf[4096], *line, *tok;
char *p, *tok;
p = strchr(line, ':') + 1;
while(isspace(*p)) p++;
tok = strtok(p, " ");
return atol(tok);
}
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)
{
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))
{
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;
*mem_total = _line_parse(line);
found++;
}
else if (!strncmp("Active:", line, 7))
{
*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);
swap_used = swap_total - swap_free;
if (swap_total > 0)
percent = 100 * ((float)swap_used / (float)swap_total);
if (found >= 6)
break;
}
if (percent > 100) percent = 100;
else if (percent < 0) percent = 0;
fclose(f);
return percent;
}
int _memusage_proc_getmemusage(void)
{
long mem_total = 0, mem_free = 0, mem_used = 0;
int percent = 0;
char buf[4096], *line, *tok;
FILE *f;
f = fopen("/proc/meminfo", "r");
if (f)
if ((tmp_swp_total != -1) && (tmp_swp_free != -1))
{
while(fgets(buf, sizeof(buf), f) != NULL)
{
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;
}
fclose(f);
mem_used = mem_total - mem_free;
if (mem_total > 0)
percent = 100 * ((float)mem_used / (float)mem_total);
*swp_total = tmp_swp_total;
*swp_active = tmp_swp_total - tmp_swp_free;
}
if (percent > 100) percent = 100;
else if (percent < 0) percent = 0;
return percent;
}