summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAl Poole <netstar@gmail.com>2018-02-24 20:35:06 +0000
committerAl Poole <netstar@gmail.com>2018-02-24 20:35:06 +0000
commiteabeb771129531397a18e69a3119d7faaed2f6dd (patch)
tree11a758128950745956f5282e182de4ab8eefded8
parentd17faadda6b2964e8fac8c61b8218b60af398e4b (diff)
Fix cpuclock for BSD
Tested on FreeBSD.
-rw-r--r--cpuclock/perms.sh3
-rw-r--r--cpuclock/src/cpuclock.c43
-rw-r--r--cpuclock/src/cpuclock_config.c5
-rw-r--r--cpuclock/src/cpuclock_sysctl.c20
-rw-r--r--meson.build17
-rw-r--r--sysinfo.h2
6 files changed, 72 insertions, 18 deletions
diff --git a/cpuclock/perms.sh b/cpuclock/perms.sh
new file mode 100644
index 0000000..f8798d0
--- /dev/null
+++ b/cpuclock/perms.sh
@@ -0,0 +1,3 @@
1#!/bin/sh
2
3chmod +sx "$1/cpuclock_sysctl"
diff --git a/cpuclock/src/cpuclock.c b/cpuclock/src/cpuclock.c
index e09a5f6..6b88f06 100644
--- a/cpuclock/src/cpuclock.c
+++ b/cpuclock/src/cpuclock.c
@@ -48,6 +48,7 @@ _cpuclock_status_free(Cpu_Status *s)
48 free(s); 48 free(s);
49} 49}
50 50
51#if defined(__linux__)
51static int 52static int
52_cpuclock_cb_sort(const void *item1, const void *item2) 53_cpuclock_cb_sort(const void *item1, const void *item2)
53{ 54{
@@ -61,14 +62,22 @@ _cpuclock_cb_sort(const void *item1, const void *item2)
61 return 0; 62 return 0;
62} 63}
63 64
65#endif
66
64void 67void
65_cpuclock_set_frequency(int frequency) 68_cpuclock_set_frequency(int frequency)
66{ 69{
67#if defined(__FreeBSD__)
68 frequency /= 1000;
69#endif
70#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) 70#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
71// _cpuclock_sysctl_frequency(frequency); 71 char exe[PATH_MAX], buf[PATH_MAX];
72 struct stat st;
73
74 frequency /= 1000;
75
76 snprintf(exe, sizeof(exe), "%s/cpuclock_sysctl", CPUCLOCKSYSCTLDIR);
77 if (stat(exe, &st) < 0) return;
78 snprintf(buf, sizeof(buf),
79 "%s %i", exe, frequency);
80 system(buf);
72 return; 81 return;
73#else 82#else
74 _cpuclock_sysfs_frequency(frequency); 83 _cpuclock_sysfs_frequency(frequency);
@@ -80,6 +89,7 @@ void
80_cpuclock_set_governor(const char *governor) 89_cpuclock_set_governor(const char *governor)
81{ 90{
82#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) 91#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
92 (void) governor;
83 return; 93 return;
84#else 94#else
85 _cpuclock_sysfs_governor(governor); 95 _cpuclock_sysfs_governor(governor);
@@ -90,6 +100,7 @@ void
90_cpuclock_set_pstate(int min, int max, int turbo) 100_cpuclock_set_pstate(int min, int max, int turbo)
91{ 101{
92#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) 102#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
103 (void) min; (void) max; (void) turbo;
93 return; 104 return;
94#else 105#else
95 _cpuclock_sysfs_pstate(min, max, turbo); 106 _cpuclock_sysfs_pstate(min, max, turbo);
@@ -388,6 +399,11 @@ _cpuclock_face_update_current(Instance *inst)
388 inst->cfg->cpuclock.percent = 0; 399 inst->cfg->cpuclock.percent = 0;
389 /* BSD crashes here without the if-condition 400 /* BSD crashes here without the if-condition
390 * since it has no governors (yet) */ 401 * since it has no governors (yet) */
402
403#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
404 inst->cfg->cpuclock.percent = inst->cfg->cpuclock.status->cur_percent;
405#endif
406
391 if (inst->cfg->cpuclock.status->cur_governor) 407 if (inst->cfg->cpuclock.status->cur_governor)
392 { 408 {
393 governor_msg.str = inst->cfg->cpuclock.status->cur_governor; 409 governor_msg.str = inst->cfg->cpuclock.status->cur_governor;
@@ -447,7 +463,7 @@ _cpuclock_status_check_available(Cpu_Status *s)
447 s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p); 463 s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p);
448 p = 25; 464 p = 25;
449 s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p); 465 s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p);
450#elif defined(__FreeBSD__) 466#elif defined(__FreeBSD__) || defined(__DragonFly__)
451 int freq; 467 int freq;
452 size_t len = sizeof(buf); 468 size_t len = sizeof(buf);
453 char *pos, *q; 469 char *pos, *q;
@@ -642,9 +658,14 @@ _cpuclock_status_check_current(Cpu_Status *s)
642 s->cur_governor = NULL; 658 s->cur_governor = NULL;
643 659
644#elif defined(__FreeBSD__) 660#elif defined(__FreeBSD__)
661 float ratio;
645 size_t len = sizeof(frequency); 662 size_t len = sizeof(frequency);
646 s->active = 0; 663 s->active = 0;
647 664
665 _cpuclock_status_check_available(s);
666
667 s->can_set_frequency = eina_list_count(s->frequencies);
668
648 /* frequency is stored in dev.cpu.0.freq */ 669 /* frequency is stored in dev.cpu.0.freq */
649 if (sysctlbyname("dev.cpu.0.freq", &frequency, &len, NULL, 0) == 0) 670 if (sysctlbyname("dev.cpu.0.freq", &frequency, &len, NULL, 0) == 0)
650 { 671 {
@@ -652,10 +673,16 @@ _cpuclock_status_check_current(Cpu_Status *s)
652 if (frequency != s->cur_frequency) ret = 1; 673 if (frequency != s->cur_frequency) ret = 1;
653 s->cur_frequency = frequency; 674 s->cur_frequency = frequency;
654 s->active = 1; 675 s->active = 1;
676 if (s->can_set_frequency)
677 {
678 s->cur_max_frequency = (int) eina_list_nth(s->frequencies, 0);
679 if (!s->cur_max_frequency) s->cur_max_frequency = 1;
680 s->cur_min_frequency = (int) eina_list_nth(s->frequencies, eina_list_count(s->frequencies) -1);
681 ratio = s->cur_max_frequency / 100.0;
682 s->cur_percent = s->cur_frequency / ratio;
683 }
655 } 684 }
656 685
657 /* hardcoded for testing */
658 s->can_set_frequency = 1;
659 s->cur_governor = NULL; 686 s->cur_governor = NULL;
660 687
661#else 688#else
@@ -813,7 +840,7 @@ _cpuclock_cb_frequency_update_poll(void *data)
813 if (inst->cfg->esm != E_SYSINFO_MODULE_CPUCLOCK && inst->cfg->esm != E_SYSINFO_MODULE_SYSINFO) return EINA_TRUE; 840 if (inst->cfg->esm != E_SYSINFO_MODULE_CPUCLOCK && inst->cfg->esm != E_SYSINFO_MODULE_SYSINFO) return EINA_TRUE;
814 if ((inst->cfg->cpuclock.status) && (status) && 841 if ((inst->cfg->cpuclock.status) && (status) &&
815 ( 842 (
816#if defined(__OpenBSD__) 843#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
817 (status->cur_percent != inst->cfg->cpuclock.status->cur_percent) || 844 (status->cur_percent != inst->cfg->cpuclock.status->cur_percent) ||
818#endif 845#endif
819 (status->cur_frequency != inst->cfg->cpuclock.status->cur_frequency) || 846 (status->cur_frequency != inst->cfg->cpuclock.status->cur_frequency) ||
diff --git a/cpuclock/src/cpuclock_config.c b/cpuclock/src/cpuclock_config.c
index c8c831d..7a260ab 100644
--- a/cpuclock/src/cpuclock_config.c
+++ b/cpuclock/src/cpuclock_config.c
@@ -147,6 +147,10 @@ _frequency_changed(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EI
147 const char *value = elm_object_text_get(obj); 147 const char *value = elm_object_text_get(obj);
148 int frequency = atol(value); 148 int frequency = atol(value);
149 149
150#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
151 frequency = (int) evas_object_data_get(obj, "frequency");
152#endif
153
150 if (frequency > 0) 154 if (frequency > 0)
151 _cpuclock_set_frequency(frequency); 155 _cpuclock_set_frequency(frequency);
152} 156}
@@ -596,6 +600,7 @@ cpuclock_configure(Instance *inst)
596 frequency / 1000000.); 600 frequency / 1000000.);
597#endif 601#endif
598 elm_object_text_set(o, buf); 602 elm_object_text_set(o, buf);
603 evas_object_data_set(o, "frequency", (void *) (long) frequency);
599 elm_box_pack_end(box, o); 604 elm_box_pack_end(box, o);
600 evas_object_smart_callback_add(o, "changed", _frequency_changed, cc); 605 evas_object_smart_callback_add(o, "changed", _frequency_changed, cc);
601 evas_object_show(o); 606 evas_object_show(o);
diff --git a/cpuclock/src/cpuclock_sysctl.c b/cpuclock/src/cpuclock_sysctl.c
index 6e5beb2..2699978 100644
--- a/cpuclock/src/cpuclock_sysctl.c
+++ b/cpuclock/src/cpuclock_sysctl.c
@@ -1,4 +1,6 @@
1#include "cpuclock.h" 1#include <stdio.h>
2#include <stdlib.h>
3
2#if defined(__FreeBSD__) || defined(__DragonFly__) 4#if defined(__FreeBSD__) || defined(__DragonFly__)
3# include <sys/sysctl.h> 5# include <sys/sysctl.h>
4#endif 6#endif
@@ -10,20 +12,28 @@
10#endif 12#endif
11 13
12#if defined(__OpenBSD__) 14#if defined(__OpenBSD__)
13void 15int
14_cpuclock_sysctl_frequency(int new_perf) 16_cpuclock_sysctl_frequency(int new_perf)
15{ 17{
16 int mib[] = {CTL_HW, HW_SETPERF}; 18 int mib[] = {CTL_HW, HW_SETPERF};
17 size_t len = sizeof(new_perf); 19 size_t len = sizeof(new_perf);
18 20
19 sysctl(mib, 2, NULL, 0, &new_perf, len); 21 return sysctl(mib, 2, NULL, 0, &new_perf, len);
20} 22}
21 23
22#elif defined(__FreeBSD__) || defined(__DragonFly__) 24#elif defined(__FreeBSD__) || defined(__DragonFly__)
23void 25int
24_cpuclock_sysctl_frequency(int new_perf) 26_cpuclock_sysctl_frequency(int new_perf)
25{ 27{
26 sysctlbyname("dev.cpu.0.freq", NULL, NULL, &new_perf, sizeof(new_perf)); 28 return sysctlbyname("dev.cpu.0.freq", NULL, NULL, &new_perf, sizeof(new_perf));
27} 29}
28 30
29#endif 31#endif
32
33int main(int argc, char **argv)
34{
35 if (argc != 2)
36 exit(1 << 0);
37
38 return _cpuclock_sysctl_frequency(atoi(argv[1]));
39}
diff --git a/meson.build b/meson.build
index 68096f8..f9b9004 100644
--- a/meson.build
+++ b/meson.build
@@ -84,6 +84,7 @@ config_h.set_quoted('PACKAGE_NAME', meson.project_name())
84config_h.set_quoted('BINDIR', dir_bin) 84config_h.set_quoted('BINDIR', dir_bin)
85config_h.set_quoted('DATADIR', dir_data) 85config_h.set_quoted('DATADIR', dir_data)
86config_h.set_quoted('BATMANTHEMEDIR', join_paths([dir_gadgets, 'batman'])) 86config_h.set_quoted('BATMANTHEMEDIR', join_paths([dir_gadgets, 'batman']))
87config_h.set_quoted('CPUCLOCKSYSCTLDIR', join_paths([dir_gadgets, 'cpuclock']))
87config_h.set_quoted('CPUCLOCKTHEMEDIR', join_paths([dir_gadgets, 'cpuclock'])) 88config_h.set_quoted('CPUCLOCKTHEMEDIR', join_paths([dir_gadgets, 'cpuclock']))
88config_h.set_quoted('CPUMONITORTHEMEDIR', join_paths([dir_gadgets, 'cpumonitor'])) 89config_h.set_quoted('CPUMONITORTHEMEDIR', join_paths([dir_gadgets, 'cpumonitor']))
89config_h.set_quoted('MEMUSAGETHEMEDIR', join_paths([dir_gadgets, 'memusage'])) 90config_h.set_quoted('MEMUSAGETHEMEDIR', join_paths([dir_gadgets, 'memusage']))
@@ -239,10 +240,7 @@ else
239 'batman/src/batman_upower.c') 240 'batman/src/batman_upower.c')
240endif 241endif
241 242
242if host_machine.system().contains('bsd') == true 243if host_machine.system().contains('bsd') == false
243 sysinfo_build_files += files(
244 'cpuclock/src/cpuclock_sysctl.c')
245else
246 sysinfo_build_files += files( 244 sysinfo_build_files += files(
247 'cpuclock/src/cpuclock_sysfs.c') 245 'cpuclock/src/cpuclock_sysfs.c')
248endif 246endif
@@ -474,3 +472,14 @@ configure_file(input: 'sysinfo.desktop.in',
474 install: true, 472 install: true,
475 install_dir: join_paths([dir_gadgets, 'sysinfo']), 473 install_dir: join_paths([dir_gadgets, 'sysinfo']),
476 configuration: desktop_data) 474 configuration: desktop_data)
475
476if host_os.startswith('freebsd') or host_os.startswith('dragonfly')
477 executable('cpuclock_sysctl',
478 'cpuclock/src/cpuclock_sysctl.c',
479 c_args : ['-fPIE', '-fPIC'],
480 link_args : ['-rdynamic', '-pie'],
481 install_dir : join_paths([dir_gadgets, 'cpuclock']),
482 install: true
483 )
484 meson.add_install_script('cpuclock/perms.sh', join_paths([dir_gadgets, 'cpuclock']))
485endif
diff --git a/sysinfo.h b/sysinfo.h
index e257918..ec5c297 100644
--- a/sysinfo.h
+++ b/sysinfo.h
@@ -101,7 +101,7 @@ struct _Cpu_Status
101 Eina_List *frequencies; 101 Eina_List *frequencies;
102 Eina_List *governors; 102 Eina_List *governors;
103 int cur_frequency; 103 int cur_frequency;
104#if defined(__OpenBSD__) 104#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
105 int cur_percent; 105 int cur_percent;
106#endif 106#endif
107 int cur_min_frequency; 107 int cur_min_frequency;