Enlightenment: Fix cpumonitor gadget to do per core.

This commit is contained in:
Stephen 'Okra' Houston 2017-01-08 12:09:39 -06:00
parent 4c2116ac0f
commit ea9dbba34d
4 changed files with 138 additions and 46 deletions

View File

@ -5,21 +5,26 @@ typedef struct _Thread_Config Thread_Config;
struct _Thread_Config
{
int interval;
int cores;
Instance *inst;
int status;
};
static void
_cpumonitor_face_update(Instance *inst, int status)
_cpumonitor_face_update(Instance *inst)
{
Edje_Message_Int_Set *usage_msg;
Eina_List *l;
CPU_Core *core;
usage_msg = malloc(sizeof(Edje_Message_Int_Set) + 1 * sizeof(int));
EINA_SAFETY_ON_NULL_RETURN(usage_msg);
EINA_LIST_FOREACH(inst->cfg->cpumonitor.cores, l, core)
{
usage_msg->count = 1;
usage_msg->val[0] = status;
edje_object_message_send(elm_layout_edje_get(inst->cfg->cpumonitor.o_gadget), EDJE_MESSAGE_INT_SET, 1,
usage_msg->val[0] = core->percent;
edje_object_message_send(elm_layout_edje_get(core->layout), EDJE_MESSAGE_INT_SET, 1,
usage_msg);
}
}
static void
@ -29,7 +34,7 @@ _cpumonitor_cb_usage_check_main(void *data, Ecore_Thread *th)
for (;;)
{
if (ecore_thread_check(th)) break;
thc->status = _cpumonitor_proc_getusage(thc->inst);
_cpumonitor_proc_getusage(thc->inst);
ecore_thread_feedback(th, NULL);
if (ecore_thread_check(th)) break;
usleep((1000000.0 / 8.0) * (double)thc->interval);
@ -47,16 +52,39 @@ _cpumonitor_cb_usage_check_notify(void *data,
if (inst->cfg->esm != E_SYSINFO_MODULE_CPUMONITOR && inst->cfg->esm != E_SYSINFO_MODULE_SYSINFO) return;
if (!inst->cfg) return;
_cpumonitor_face_update(inst, thc->status);
_cpumonitor_face_update(inst);
}
Evas_Object *
_cpumonitor_add_layout(Instance *inst)
{
Evas_Object *layout;
layout = elm_layout_add(inst->cfg->cpumonitor.o_gadget);
e_theme_edje_object_set(layout, "base/theme/modules/cpumonitor",
"e/modules/cpumonitor/main");
E_EXPAND(layout);
E_FILL(layout);
elm_box_pack_end(inst->cfg->cpumonitor.o_gadget, layout);
evas_object_show(layout);
return layout;
}
void
_cpumonitor_config_updated(Instance *inst)
{
Thread_Config *thc;
CPU_Core *core;
int i = 0;
if (inst->cfg->cpumonitor.usage_check_thread)
{
EINA_LIST_FREE(inst->cfg->cpumonitor.cores, core)
{
evas_object_del(core->layout);
E_FREE_FUNC(core, free);
}
ecore_thread_cancel(inst->cfg->cpumonitor.usage_check_thread);
inst->cfg->cpumonitor.usage_check_thread = NULL;
}
@ -65,7 +93,16 @@ _cpumonitor_config_updated(Instance *inst)
{
thc->inst = inst;
thc->interval = inst->cfg->cpumonitor.poll_interval;
thc->status = 0;
thc->cores = _cpumonitor_proc_getcores();
for (i = 0; i < thc->cores; i++)
{
core = E_NEW(CPU_Core, 1);
core->layout = _cpumonitor_add_layout(inst);
core->percent = 0;
core->total = 0;
core->idle = 0;
inst->cfg->cpumonitor.cores = eina_list_append(inst->cfg->cpumonitor.cores, core);
}
inst->cfg->cpumonitor.usage_check_thread =
ecore_thread_feedback_run(_cpumonitor_cb_usage_check_main,
_cpumonitor_cb_usage_check_notify,
@ -78,6 +115,7 @@ static void
_cpumonitor_removed_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_data)
{
Instance *inst = data;
CPU_Core *core;
if (inst->o_main != event_data) return;
@ -86,7 +124,11 @@ _cpumonitor_removed_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_dat
ecore_thread_cancel(inst->cfg->cpumonitor.usage_check_thread);
inst->cfg->cpumonitor.usage_check_thread = NULL;
}
EINA_LIST_FREE(inst->cfg->cpumonitor.cores, core)
{
evas_object_del(core->layout);
E_FREE_FUNC(core, free);
}
sysinfo_config->items = eina_list_remove(sysinfo_config->items, inst->cfg);
E_FREE(inst->cfg);
}
@ -94,11 +136,18 @@ _cpumonitor_removed_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_dat
void
sysinfo_cpumonitor_remove(Instance *inst)
{
CPU_Core *core;
if (inst->cfg->cpumonitor.usage_check_thread)
{
ecore_thread_cancel(inst->cfg->cpumonitor.usage_check_thread);
inst->cfg->cpumonitor.usage_check_thread = NULL;
}
EINA_LIST_FREE(inst->cfg->cpumonitor.cores, core)
{
evas_object_del(core->layout);
E_FREE_FUNC(core, free);
}
}
static void
@ -106,9 +155,7 @@ _cpumonitor_created_cb(void *data, Evas_Object *obj, void *event_data EINA_UNUSE
{
Instance *inst = data;
inst->cfg->cpumonitor.o_gadget = elm_layout_add(inst->o_main);
e_theme_edje_object_set(inst->cfg->cpumonitor.o_gadget, "base/theme/modules/cpumonitor",
"e/modules/cpumonitor/main");
inst->cfg->cpumonitor.o_gadget = elm_box_add(inst->o_main);
E_EXPAND(inst->cfg->cpumonitor.o_gadget);
E_FILL(inst->cfg->cpumonitor.o_gadget);
elm_box_pack_end(inst->o_main, inst->cfg->cpumonitor.o_gadget);
@ -120,9 +167,7 @@ _cpumonitor_created_cb(void *data, Evas_Object *obj, void *event_data EINA_UNUSE
Evas_Object *
sysinfo_cpumonitor_create(Evas_Object *parent, Instance *inst)
{
inst->cfg->cpumonitor.o_gadget = elm_layout_add(parent);
e_theme_edje_object_set(inst->cfg->cpumonitor.o_gadget, "base/theme/modules/cpumonitor",
"e/modules/cpumonitor/main");
inst->cfg->cpumonitor.o_gadget = elm_box_add(parent);
E_EXPAND(inst->cfg->cpumonitor.o_gadget);
E_FILL(inst->cfg->cpumonitor.o_gadget);
evas_object_show(inst->cfg->cpumonitor.o_gadget);

View File

@ -4,6 +4,7 @@
#include "../sysinfo.h"
void _cpuclock_config_updated(Instance *inst);
int _cpumonitor_proc_getusage(Instance *inst);
int _cpumonitor_proc_getcores(void);
void _cpumonitor_proc_getusage(Instance *inst);
#endif

View File

@ -1,23 +1,51 @@
#include "cpumonitor.h"
int _cpumonitor_proc_getusage(Instance *inst)
int
_cpumonitor_proc_getcores(void)
{
long total = 0, i = 0, idle = 0, use, total_change, idle_change;
int percent = 0;
char buf[4096], *line, *tok;
char buf[4096], *tok;
FILE *f;
int cores = 0, i = 0;
f = fopen("/proc/stat", "r");
if (f)
{
if (fgets(buf, sizeof(buf), f) == NULL)
while (fgets(buf, sizeof(buf), f))
{
fclose(f);
return 0;
if (i > 0)
{
tok = strtok(buf, " ");
if (!strncmp(tok, "cpu", 3))
cores++;
else
break;
}
fclose(f);
i++;
}
fclose (f);
}
return cores;
}
line = strchr(buf, ' ')+1;
void
_cpumonitor_proc_getusage(Instance *inst)
{
long total = 0, idle = 0, use, total_change, idle_change;
int percent = 0, i = 0, j = 0, k = 0;
char buf[4096], *line, *tok;
FILE *f;
CPU_Core *core;
f = fopen("/proc/stat", "r");
if (f)
{
while (fgets(buf, sizeof(buf), f))
{
if (k > 0)
{
if (!strncmp(buf, "cpu", 3))
{
line = strchr(buf, ' ');
tok = strtok(line, " ");
while (tok)
{
@ -29,14 +57,22 @@ int _cpumonitor_proc_getusage(Instance *inst)
tok = strtok(NULL, " ");
}
}
total_change = total - inst->cfg->cpumonitor.total;
idle_change = idle - inst->cfg->cpumonitor.idle;
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;
inst->cfg->cpumonitor.total = total;
inst->cfg->cpumonitor.idle = idle;
return percent;
core->percent = percent;
core->total = total;
core->idle = idle;
j++;
}
k++;
}
fclose(f);
}
}

View File

@ -54,6 +54,7 @@ typedef enum _Unit
typedef struct _Tempthread Tempthread;
typedef struct _Cpu_Status Cpu_Status;
typedef struct _CPU_Core CPU_Core;
typedef struct _Config Config;
typedef struct _Config_Item Config_Item;
typedef struct _Instance Instance;
@ -92,6 +93,14 @@ struct _Cpu_Status
unsigned char pstate_turbo;
};
struct _CPU_Core
{
int percent;
long total;
long idle;
Evas_Object *layout;
};
struct _Config
{
Eina_List *items;
@ -173,6 +182,7 @@ struct _Config_Item
long total;
long idle;
Ecore_Thread *usage_check_thread;
Eina_List *cores;
} cpumonitor;
struct
{