forked from enlightenment/enlightenment
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:
parent
b55ce098bf
commit
1c8ca780da
|
@ -8,6 +8,13 @@ struct _Thread_Config
|
||||||
Instance *inst;
|
Instance *inst;
|
||||||
int memstatus;
|
int memstatus;
|
||||||
int swapstatus;
|
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
|
static void
|
||||||
|
@ -130,8 +137,14 @@ _memusage_cb_usage_check_main(void *data, Ecore_Thread *th)
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
if (ecore_thread_check(th)) break;
|
if (ecore_thread_check(th)) break;
|
||||||
thc->memstatus = _memusage_proc_getmemusage();
|
_memusage_proc_getusage(&thc->mem_total, &thc->mem_active,
|
||||||
thc->swapstatus = _memusage_proc_getswapusage();
|
&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);
|
ecore_thread_feedback(th, NULL);
|
||||||
if (ecore_thread_check(th)) break;
|
if (ecore_thread_check(th)) break;
|
||||||
usleep((1000000.0 / 8.0) * (double)thc->interval);
|
usleep((1000000.0 / 8.0) * (double)thc->interval);
|
||||||
|
|
|
@ -4,7 +4,13 @@
|
||||||
#include "../sysinfo.h"
|
#include "../sysinfo.h"
|
||||||
|
|
||||||
void _memusage_config_updated(Instance *inst);
|
void _memusage_config_updated(Instance *inst);
|
||||||
int _memusage_proc_getmemusage();
|
|
||||||
int _memusage_proc_getswapusage();
|
|
||||||
Evas_Object *memusage_configure(Instance *inst);
|
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
|
#endif
|
||||||
|
|
|
@ -1,84 +1,73 @@
|
||||||
#include "memusage.h"
|
#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;
|
char *p, *tok;
|
||||||
int percent = 0;
|
|
||||||
char buf[4096], *line, *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;
|
FILE *f;
|
||||||
|
|
||||||
f = fopen("/proc/meminfo", "r");
|
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))
|
*mem_total = _line_parse(line);
|
||||||
{
|
found++;
|
||||||
line = strchr(buf, ':')+1;
|
}
|
||||||
while(isspace(*line)) line++;
|
else if (!strncmp("Active:", line, 7))
|
||||||
tok = strtok(line, " ");
|
{
|
||||||
swap_total = atol(tok);
|
*mem_active = _line_parse(line);
|
||||||
}
|
found++;
|
||||||
else if (!strncmp("SwapFree:", buf, 9))
|
}
|
||||||
{
|
else if (!strncmp("Cached:", line, 7))
|
||||||
line = strchr(buf, ':')+1;
|
{
|
||||||
while(isspace(*line)) line++;
|
*mem_cached = _line_parse(line);
|
||||||
tok = strtok(line, " ");
|
found++;
|
||||||
swap_free = atol(tok);
|
}
|
||||||
}
|
else if (!strncmp("Buffers:", line, 8))
|
||||||
if (swap_total && swap_free)
|
{
|
||||||
break;
|
*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 (found >= 6)
|
||||||
if (swap_total > 0)
|
break;
|
||||||
percent = 100 * ((float)swap_used / (float)swap_total);
|
|
||||||
}
|
}
|
||||||
if (percent > 100) percent = 100;
|
fclose(f);
|
||||||
else if (percent < 0) percent = 0;
|
|
||||||
|
|
||||||
return percent;
|
if ((tmp_swp_total != -1) && (tmp_swp_free != -1))
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
while(fgets(buf, sizeof(buf), f) != NULL)
|
*swp_total = tmp_swp_total;
|
||||||
{
|
*swp_active = tmp_swp_total - tmp_swp_free;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
if (percent > 100) percent = 100;
|
|
||||||
else if (percent < 0) percent = 0;
|
|
||||||
|
|
||||||
return percent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue