From e33a14b7e8ea92c1180b6bf5346f47f083c9f2ff Mon Sep 17 00:00:00 2001 From: Al Poole Date: Sat, 24 Feb 2018 18:19:48 +0000 Subject: [PATCH] sysinfo: fix cpuclock for BSD. This lets us use the cpuclock gadget on FreeBSD machines. @fix T6259 --- src/modules/sysinfo/cpuclock/cpuclock.c | 66 ++++++++++--------- .../sysinfo/cpuclock/cpuclock_config.c | 4 ++ .../sysinfo/cpuclock/cpuclock_sysctl.c | 15 ++++- src/modules/sysinfo/meson.build | 11 ++++ src/modules/sysinfo/sysinfo.h | 2 +- 5 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/modules/sysinfo/cpuclock/cpuclock.c b/src/modules/sysinfo/cpuclock/cpuclock.c index 7db20e348..787a86e9f 100644 --- a/src/modules/sysinfo/cpuclock/cpuclock.c +++ b/src/modules/sysinfo/cpuclock/cpuclock.c @@ -56,6 +56,7 @@ _cpuclock_status_free(Cpu_Status *s) E_FREE(s); } +#if defined(__linux__) static int _cpuclock_cb_sort(const void *item1, const void *item2) { @@ -69,27 +70,11 @@ _cpuclock_cb_sort(const void *item1, const void *item2) return 0; } -#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) -static void -_cpuclock_set_thread_frequency(void *data, Ecore_Thread *th EINA_UNUSED) -{ - const char *freq = data; - int frequency = atoi(freq); - _cpuclock_sysctl_frequency(frequency); -} - -static void -_cpuclock_set_thread_done(void *data EINA_UNUSED, Ecore_Thread *th EINA_UNUSED) -{ - return; -} - #endif - void _cpuclock_set_governor(const char *governor) { -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) return; #endif char buf[4096], exe[4096]; @@ -107,20 +92,20 @@ _cpuclock_set_governor(const char *governor) void _cpuclock_set_frequency(int frequency) { - char buf[4096]; + char buf[4096], exe[4096]; + struct stat st; -#if defined(__FreeBSD__) +#if defined(__FreeBSD__) || defined(__DragonFly__) frequency /= 1000; #endif - snprintf(buf, sizeof(buf), "%i", frequency); #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) - const char *freq; - freq = eina_stringshare_add(buf); - ecore_thread_run(_cpuclock_set_thread_frequency, _cpuclock_set_thread_done, NULL, freq); + snprintf(exe, sizeof(exe), "%s/%s/cpuclock_sysctl", + e_module_dir_get(sysinfo_config->module), MODULE_ARCH); + if (stat(exe, &st) < 0) return; + snprintf(buf, sizeof(buf), "%s %d", exe, frequency); + system(buf); #else - struct stat st; - char exe[4096]; snprintf(exe, 4096, "%s/%s/cpuclock_sysfs", e_module_dir_get(sysinfo_config->module), MODULE_ARCH); if (stat(exe, &st) < 0) return; @@ -133,7 +118,7 @@ _cpuclock_set_frequency(int frequency) void _cpuclock_set_pstate(int min, int max, int turbo) { -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) return; #endif char buf[4096], exe[4096]; @@ -456,6 +441,11 @@ _cpuclock_face_update_current(Instance *inst) } else inst->cfg->cpuclock.percent = 0; + +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) + inst->cfg->cpuclock.percent = inst->cfg->cpuclock.status->cur_percent; +#endif + /* BSD crashes here without the if-condition * since it has no governors (yet) */ if (inst->cfg->cpuclock.status->cur_governor) @@ -517,7 +507,7 @@ _cpuclock_status_check_available(Cpu_Status *s) s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p); p = 25; s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p); -#elif defined(__FreeBSD__) +#elif defined(__FreeBSD__) || defined(__DragonFly__) int freq; size_t len = sizeof(buf); char *pos, *q; @@ -542,8 +532,8 @@ _cpuclock_status_check_available(Cpu_Status *s) *q = '\0'; freq = atoi(pos); freq *= 1000; - s->frequencies = eina_list_append(s->frequencies, (void *)(long)freq); + s->frequencies = eina_list_append(s->frequencies, (void *)(long)freq); pos = q + 1; pos = strchr(pos, ' '); } @@ -711,21 +701,33 @@ _cpuclock_status_check_current(Cpu_Status *s) s->can_set_frequency = 1; s->cur_governor = NULL; -#elif defined(__FreeBSD__) +#elif defined(__FreeBSD__) || defined(__DragonFly__) + float ratio; size_t len = sizeof(frequency); s->active = 0; + _cpuclock_status_check_available(s); + + s->can_set_frequency = eina_list_count(s->frequencies); + /* frequency is stored in dev.cpu.0.freq */ if (sysctlbyname("dev.cpu.0.freq", &frequency, &len, NULL, 0) == 0) { frequency *= 1000; if (frequency != s->cur_frequency) ret = 1; + if (!frequency) frequency = 1; s->cur_frequency = frequency; s->active = 1; + if (s->can_set_frequency) + { + s->cur_max_frequency = (int) eina_list_nth(s->frequencies, 0); + if (!s->cur_max_frequency) s->cur_max_frequency = 1; + s->cur_min_frequency = (int) eina_list_nth(s->frequencies, eina_list_count(s->frequencies) - 1); + ratio = (s->cur_max_frequency) / 100.0; + s->cur_percent = s->cur_frequency / ratio; + } } - /* hardcoded for testing */ - s->can_set_frequency = 1; s->cur_governor = NULL; #else @@ -903,7 +905,7 @@ _cpuclock_cb_frequency_check_notify(void *data, if ((thc->inst->cfg->cpuclock.status) && (status) && ( -#if defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) (status->cur_percent != thc->inst->cfg->cpuclock.status->cur_percent) || #endif (status->cur_frequency != thc->inst->cfg->cpuclock.status->cur_frequency) || diff --git a/src/modules/sysinfo/cpuclock/cpuclock_config.c b/src/modules/sysinfo/cpuclock/cpuclock_config.c index 115d974de..e6964fa20 100644 --- a/src/modules/sysinfo/cpuclock/cpuclock_config.c +++ b/src/modules/sysinfo/cpuclock/cpuclock_config.c @@ -145,6 +145,9 @@ _frequency_changed(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EI const char *value = elm_object_text_get(obj); int frequency = atol(value); +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) + frequency = (int) evas_object_data_get(obj, "frequency"); +#endif if (frequency > 0) _cpuclock_set_frequency(frequency); } @@ -587,6 +590,7 @@ cpuclock_configure(Instance *inst) frequency / 1000000.); #endif elm_object_text_set(o, buf); + evas_object_data_set(o, "frequency", (void *) (int) frequency); elm_box_pack_end(box, o); evas_object_smart_callback_add(o, "changed", _frequency_changed, cc); evas_object_show(o); diff --git a/src/modules/sysinfo/cpuclock/cpuclock_sysctl.c b/src/modules/sysinfo/cpuclock/cpuclock_sysctl.c index 35e9cffb4..9ea049f40 100644 --- a/src/modules/sysinfo/cpuclock/cpuclock_sysctl.c +++ b/src/modules/sysinfo/cpuclock/cpuclock_sysctl.c @@ -1,4 +1,6 @@ -#include "cpuclock.h" +#include +#include + #if defined(__FreeBSD__) || defined(__DragonFly__) # include #endif @@ -31,5 +33,14 @@ _cpuclock_sysctl_frequency(int new_perf) return 0; } - +#endif + +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) +int main(int argc, char **argv) +{ + if (argc != 2) + exit(1 << 0); + + return _cpuclock_sysctl_frequency(atoi(argv[1])); +} #endif diff --git a/src/modules/sysinfo/meson.build b/src/modules/sysinfo/meson.build index 7d0523998..ac9dc9820 100644 --- a/src/modules/sysinfo/meson.build +++ b/src/modules/sysinfo/meson.build @@ -56,3 +56,14 @@ if get_option(m) == true ) suid_exes += join_paths(_dir_bin, 'cpuclock_sysfs') endif + +if get_option(m) == true + executable('cpuclock_sysctl', + 'cpuclock/cpuclock_sysctl.c', + c_args : suid_cflags, + link_args : suid_ldflags, + install_dir : _dir_bin, + install : true + ) + suid_exes += join_paths(_dir_bin, 'cpuclock_sysctl') +endif diff --git a/src/modules/sysinfo/sysinfo.h b/src/modules/sysinfo/sysinfo.h index ed641f774..c6a75daeb 100644 --- a/src/modules/sysinfo/sysinfo.h +++ b/src/modules/sysinfo/sysinfo.h @@ -88,7 +88,7 @@ struct _Cpu_Status Eina_List *frequencies; Eina_List *governors; int cur_frequency; -#if defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) int cur_percent; #endif int cur_min_frequency;