cpu: colorz

This commit is contained in:
Alastair Poole 2020-09-07 14:39:29 +01:00
parent af63cd9ce6
commit 60282d7076
2 changed files with 113 additions and 15 deletions

View File

@ -282,6 +282,7 @@ system_cpu_usage_get(int *ncpu)
int int
system_cpu_n_frequency_get(int n) system_cpu_n_frequency_get(int n)
{ {
#if defined(__linux__)
int freq = -1; int freq = -1;
FILE *f; FILE *f;
char buf[4096]; char buf[4096];
@ -302,13 +303,19 @@ system_cpu_n_frequency_get(int n)
} }
return freq; return freq;
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
return system_cpu_frequency_get();
#endif
return -1;
} }
int int
system_cpu_frequency_min_max_get(int *min, int *max) system_cpu_frequency_min_max_get(int *min, int *max)
{ {
char *s;
int freq_min = 0x7fffffff, freq_max = 0; int freq_min = 0x7fffffff, freq_max = 0;
#if defined(__linux__)
char *s;
s = file_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq"); s = file_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
if (s) if (s)
@ -346,6 +353,37 @@ system_cpu_frequency_min_max_get(int *min, int *max)
*max = freq_max; *max = freq_max;
return 0; return 0;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
char buf[4096];
size_t len = sizeof(buf);
char *t, *s;
if (sysctlbyname("dev.cpu.0.freq_levels", buf, &len, NULL, 0) != -1)
{
s = buf;
while (s)
{
t = strchr(s, '/');
if (!t) break;
*t = '\0';
int freq = atoi(s) * 1000;
if (freq > freq_max) freq_max = freq;
if (freq < freq_min) freq_min = freq;
s = strchr(t + 1, ' ');
}
if (freq_min == 0x7fffffff || freq_max == 0) return -1;
*min = freq_min;
*max = freq_max;
return 0;
}
#elif defined(__OpenBSD__)
#endif
return -1;
} }
int int

View File

@ -15,6 +15,10 @@ typedef struct {
Eina_Bool enabled; Eina_Bool enabled;
Eina_Bool redraw; Eina_Bool redraw;
int freq;
int freq_min;
int freq_max;
int pos; int pos;
double value; double value;
double step; double step;
@ -25,8 +29,7 @@ typedef struct {
Animate_Data *anim_data; Animate_Data *anim_data;
double *value; double *value;
Evas_Object *pb; Evas_Object *pb;
Evas_Object *lbl;
int freq;
} Progress; } Progress;
static void static void
@ -55,6 +58,34 @@ _bg_fill(Animate_Data *ad)
return EINA_TRUE; return EINA_TRUE;
} }
static int
_color_rng(int fr, int fr_min, int fr_max)
{
int rng, n;
rng = fr_max - fr_min;
n = fr - fr_min;
n = (n * 10) / rng;
if (n > 8) return 0xff26f226;
if (n > 6) return 0xfff2f226;
if (n > 4) return 0xffe21212;
if (n > 2) return 0xff471292;
return COLOR_FG;
}
static int
_color(Animate_Data *ad)
{
if (ad->freq != -1 && ad->freq_min && ad->freq_max)
{
return _color_rng(ad->freq, ad->freq_min, ad->freq_max);
}
return COLOR_FG;
}
static Eina_Bool static Eina_Bool
animate(void *data) animate(void *data)
{ {
@ -100,7 +131,7 @@ animate(void *data)
if ((x == (w - ad->pos)) && (y >= fill_y)) if ((x == (w - ad->pos)) && (y >= fill_y))
{ {
if (y % 2) if (y % 2)
*(pixels) = COLOR_FG; *(pixels) = _color(ad);
} }
pixels++; pixels++;
} }
@ -159,24 +190,33 @@ _core_times_cb(void *data, Ecore_Thread *thread)
cpu_core_t **cores; cpu_core_t **cores;
Eina_List *l; Eina_List *l;
Ui *ui; Ui *ui;
int ncpu; int ncpu, min = 0, max = 0;
ui = data; ui = data;
int min, max; system_cpu_frequency_min_max_get(&min, &max);
if (!system_cpu_frequency_min_max_get(&min, &max))
{
printf("min %d and max %d\n", min, max);
}
for (int i = 0; !ecore_thread_check(thread); i = 0) for (int i = 0; !ecore_thread_check(thread); i = 0)
{ {
cores = system_cpu_usage_get(&ncpu); cores = system_cpu_usage_get(&ncpu);
EINA_LIST_FOREACH(ui->cpu_list, l, progress) EINA_LIST_FOREACH(ui->cpu_list, l, progress)
{ {
*progress->value = cores[i]->percent; *progress->value = cores[i]->percent;
progress->freq = system_cpu_n_frequency_get(progress->anim_data->cpu_id);
ecore_thread_main_loop_begin(); ecore_thread_main_loop_begin();
if (min && max)
{
int freq = system_cpu_n_frequency_get(progress->anim_data->cpu_id);
if (freq > 1000000)
elm_object_text_set(progress->lbl, eina_slstr_printf("%1.1f GHz", (double) freq / 1000000.0));
else
elm_object_text_set(progress->lbl, eina_slstr_printf("%d MHz", freq / 1000));
progress->anim_data->freq = freq;
progress->anim_data->freq_min = min;
progress->anim_data->freq_max = max;
}
elm_progressbar_value_set(progress->pb, cores[i]->percent / 100); elm_progressbar_value_set(progress->pb, cores[i]->percent / 100);
ecore_thread_main_loop_end(); ecore_thread_main_loop_end();
free(cores[i++]); free(cores[i++]);
@ -210,7 +250,7 @@ void
ui_win_cpu_add(Ui *ui) ui_win_cpu_add(Ui *ui)
{ {
Evas_Object *win, *box, *hbox, *scroller, *frame; Evas_Object *win, *box, *hbox, *scroller, *frame;
Evas_Object *pb, *tbl, *lbox, *btn, *rect; Evas_Object *pb, *tbl, *cbox, *sbox, *lbl, *lbox, *btn, *rect;
Evas_Object *bg, *line, *obj; Evas_Object *bg, *line, *obj;
int cpu_count; int cpu_count;
@ -246,10 +286,21 @@ ui_win_cpu_add(Ui *ui)
{ {
lbox = elm_box_add(box); lbox = elm_box_add(box);
evas_object_size_hint_align_set(lbox, FILL, FILL); evas_object_size_hint_align_set(lbox, FILL, FILL);
evas_object_size_hint_weight_set(lbox, 0.1, EXPAND); evas_object_size_hint_weight_set(lbox, EXPAND, EXPAND);
evas_object_show(lbox); evas_object_show(lbox);
elm_box_horizontal_set(lbox, EINA_TRUE); elm_box_horizontal_set(lbox, EINA_TRUE);
cbox = elm_box_add(box);
evas_object_size_hint_align_set(cbox, FILL, FILL);
evas_object_size_hint_weight_set(cbox, 0.1, EXPAND);
evas_object_show(cbox);
sbox = elm_box_add(box);
evas_object_size_hint_align_set(sbox, FILL, FILL);
evas_object_size_hint_weight_set(sbox, EXPAND, EXPAND);
elm_box_horizontal_set(sbox, EINA_TRUE);
evas_object_show(sbox);
btn = elm_button_add(box); btn = elm_button_add(box);
evas_object_show(btn); evas_object_show(btn);
@ -273,8 +324,16 @@ ui_win_cpu_add(Ui *ui)
elm_progressbar_unit_format_set(pb, "%1.2f%%"); elm_progressbar_unit_format_set(pb, "%1.2f%%");
evas_object_show(pb); evas_object_show(pb);
elm_box_pack_end(lbox, btn); lbl = elm_label_add(box);
elm_box_pack_end(lbox, pb); evas_object_size_hint_align_set(lbl, 0.5, 0.0);
evas_object_size_hint_weight_set(lbl, EXPAND, EXPAND);
evas_object_show(lbl);
elm_box_pack_end(sbox, btn);
elm_box_pack_end(sbox, pb);
elm_box_pack_end(cbox, sbox);
elm_box_pack_end(cbox, lbl);
elm_box_pack_end(lbox, cbox);
tbl = elm_table_add(box); tbl = elm_table_add(box);
evas_object_size_hint_align_set(tbl, FILL, FILL); evas_object_size_hint_align_set(tbl, FILL, FILL);
@ -329,6 +388,7 @@ ui_win_cpu_add(Ui *ui)
if (progress) if (progress)
{ {
progress->pb = pb; progress->pb = pb;
progress->lbl = lbl;
progress->value = &ad->value; progress->value = &ad->value;
progress->animator = ecore_animator_add(animate, ad); progress->animator = ecore_animator_add(animate, ad);
progress->anim_data = ad; progress->anim_data = ad;