2017-01-06 14:15:30 -08:00
|
|
|
#include "cpumonitor.h"
|
|
|
|
|
2017-01-08 10:09:39 -08:00
|
|
|
int
|
|
|
|
_cpumonitor_proc_getcores(void)
|
2017-01-06 14:15:30 -08:00
|
|
|
{
|
2017-01-08 10:09:39 -08:00
|
|
|
char buf[4096], *tok;
|
2017-01-06 14:15:30 -08:00
|
|
|
FILE *f;
|
2017-01-08 10:09:39 -08:00
|
|
|
int cores = 0, i = 0;
|
2017-01-06 14:15:30 -08:00
|
|
|
|
|
|
|
f = fopen("/proc/stat", "r");
|
|
|
|
if (f)
|
|
|
|
{
|
2017-01-08 10:09:39 -08:00
|
|
|
while (fgets(buf, sizeof(buf), f))
|
2017-01-06 14:15:30 -08:00
|
|
|
{
|
2017-01-08 10:09:39 -08:00
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
tok = strtok(buf, " ");
|
|
|
|
if (!strncmp(tok, "cpu", 3))
|
|
|
|
cores++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
2017-01-06 14:15:30 -08:00
|
|
|
}
|
2017-01-08 10:09:39 -08:00
|
|
|
fclose (f);
|
|
|
|
}
|
|
|
|
return cores;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cpumonitor_proc_getusage(Instance *inst)
|
|
|
|
{
|
2017-01-08 12:44:13 -08:00
|
|
|
int k = 0, j = 0;
|
|
|
|
char buf[4096];
|
2017-01-08 10:09:39 -08:00
|
|
|
FILE *f;
|
|
|
|
CPU_Core *core;
|
2017-01-06 14:15:30 -08:00
|
|
|
|
2017-01-08 10:09:39 -08:00
|
|
|
f = fopen("/proc/stat", "r");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
while (fgets(buf, sizeof(buf), f))
|
2017-01-06 14:15:30 -08:00
|
|
|
{
|
2017-01-11 19:21:34 -08:00
|
|
|
if (k == 0)
|
|
|
|
{
|
|
|
|
long total = 0, idle = 0, use = 0, total_change = 0, idle_change = 0;
|
|
|
|
int percent = 0;
|
|
|
|
char *line, *tok;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
line = strchr(buf, ' ')+1;
|
|
|
|
tok = strtok(line, " ");
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
use = atol(tok);
|
|
|
|
total += use;
|
|
|
|
i++;
|
|
|
|
if (i == 4)
|
|
|
|
idle = use;
|
|
|
|
tok = strtok(NULL, " ");
|
|
|
|
}
|
|
|
|
total_change = total - inst->cfg->cpumonitor.total;
|
|
|
|
idle_change = idle - inst->cfg->cpumonitor.idle;
|
|
|
|
if (total_change != 0)
|
|
|
|
percent = 100 * (1 - ((float)idle_change / (float)total_change));
|
|
|
|
if (percent > 100) percent = 100;
|
|
|
|
else if (percent < 0) percent = 0;
|
|
|
|
inst->cfg->cpumonitor.total = total;
|
|
|
|
inst->cfg->cpumonitor.idle = idle;
|
|
|
|
inst->cfg->cpumonitor.percent = percent;
|
|
|
|
}
|
2017-01-08 10:09:39 -08:00
|
|
|
if (k > 0)
|
|
|
|
{
|
2017-01-08 12:44:13 -08:00
|
|
|
long total = 0, idle = 0, use = 0, total_change = 0, idle_change = 0;
|
|
|
|
int percent = 0;
|
2017-01-08 10:09:39 -08:00
|
|
|
if (!strncmp(buf, "cpu", 3))
|
|
|
|
{
|
2017-01-08 12:44:13 -08:00
|
|
|
char *line, *tok;
|
|
|
|
int i = 0;
|
2017-01-08 10:09:39 -08:00
|
|
|
line = strchr(buf, ' ');
|
|
|
|
tok = strtok(line, " ");
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
use = atol(tok);
|
|
|
|
total += use;
|
|
|
|
i++;
|
|
|
|
if (i == 4)
|
|
|
|
idle = use;
|
|
|
|
tok = strtok(NULL, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
core = eina_list_nth(inst->cfg->cpumonitor.cores, j);
|
|
|
|
total_change = total - core->total;
|
|
|
|
idle_change = idle - core->idle;
|
|
|
|
if (total_change != 0)
|
|
|
|
percent = 100 * (1 - ((float)idle_change / (float)total_change));
|
|
|
|
if (percent > 100) percent = 100;
|
|
|
|
else if (percent < 0) percent = 0;
|
|
|
|
core->percent = percent;
|
|
|
|
core->total = total;
|
|
|
|
core->idle = idle;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
k++;
|
2017-01-06 14:15:30 -08:00
|
|
|
}
|
2017-01-08 10:09:39 -08:00
|
|
|
fclose(f);
|
2017-01-06 14:15:30 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 10:09:39 -08:00
|
|
|
|