sysinfo: fix cpuclock for BSD.

This lets us use the cpuclock gadget on FreeBSD machines.

@fix T6259
This commit is contained in:
Al Poole 2018-02-24 18:19:48 +00:00 committed by Mike Blumenkrantz
parent 140f176d8e
commit da19c3cd6a
5 changed files with 63 additions and 35 deletions

View File

@ -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) ||

View File

@ -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);

View File

@ -1,4 +1,6 @@
#include "cpuclock.h"
#include <stdio.h>
#include <stdlib.h>
#if defined(__FreeBSD__) || defined(__DragonFly__)
# include <sys/sysctl.h>
#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

View File

@ -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

View File

@ -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;