summaryrefslogtreecommitdiffstats
path: root/x11/gnomeapplets2/files/patch-cpufreq-selector-freebsd
blob: d25df35e2b5dc1382ab7f27b6696bb1f2a959684 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
--- cpufreq/src/cpufreq-selector/cpufreq-sysctl.h.orig  Mon Feb 14 00:38:57 2005
+++ cpufreq/src/cpufreq-selector/cpufreq-sysctl.h   Mon Feb 14 00:21:39 2005
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2001, 2002 Free Software Foundation
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public
+ *  License along with this library; if not, write to the Free
+ *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Authors : Joe Marcus Clarke <marcus@FreeBSD.org>
+ */
+
+#ifndef __CPUFREQ_SYSCTL_H__
+#define __CPUFREQ_SYSCTL_H__
+
+#include <glib-object.h>
+
+#include "cpufreq.h"
+
+#define TYPE_CPUFREQ_SYSCTL            (cpufreq_sysctl_get_type ())
+#define CPUFREQ_SYSCTL(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CPUFREQ_SYSCTL, CPUFreqSysctl))
+#define CPUFREQ_SYSCTL_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_CPUFREQ_SYSCTL, CPUFreqSysctlClass))
+#define IS_CPUFREQ_SYSCTL(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CPUFREQ_SYSCTL))
+#define IS_CPUFREQ_SYSCTL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CPUFREQ_SYSCTL))
+#define CPUFREQ_SYSCTL_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CPUFREQ_SYSCTL, CPUFreqSysctlClass))
+
+typedef struct _CPUFreqSysctl      CPUFreqSysctl;
+typedef struct _CPUFreqSysctlClass CPUFreqSysctlClass;
+typedef struct _CPUFreqSysctlPriv  CPUFreqSysctlPriv;
+
+struct _CPUFreqSysctl {
+      CPUFreq parent;
+};
+
+struct _CPUFreqSysctlClass {
+      CPUFreqClass parent_class;
+};
+
+
+GType          cpufreq_sysctl_get_type ();
+CPUFreqSysctl *cpufreq_sysctl_new      ();
+
+#endif /* __CPUFREQ_SYSCTL_H__ */
--- cpufreq/src/cpufreq-selector/cpufreq-sysctl.c.orig  Mon Feb 14 00:38:52 2005
+++ cpufreq/src/cpufreq-selector/cpufreq-sysctl.c   Mon Feb 14 00:44:43 2005
@@ -0,0 +1,235 @@
+/*
+ * Copyright (C) 2001, 2002 Free Software Foundation
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public
+ *  License along with this library; if not, write to the Free
+ *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Authors : Joe Marcus Clarke <marcus@FreeBSD.org>
+ */
+
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif /* __FreeBSD__ */
+
+#include "cpufreq-sysctl.h"
+
+#define PARENT_TYPE TYPE_CPUFREQ
+
+#define CPUFREQ_SYSCTL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), TYPE_CPUFREQ_SYSCTL, CPUFreqSysctlPrivate))
+
+static void cpufreq_sysctl_init           (CPUFreqSysctl *cfq);
+static void cpufreq_sysctl_class_init     (CPUFreqSysctlClass *klass);
+static void cpufreq_sysctl_finalize       (GObject *object);
+
+static void  cpufreq_sysctl_set_governor  (CPUFreq *cfq, const gchar *governor);
+static void  cpufreq_sysctl_set_frequency (CPUFreq *cfq, gint frequency);
+
+static void  cpufreq_sysctl_setup         (CPUFreqSysctl *cfq);
+
+static GObjectClass *parent_class = NULL;
+
+typedef struct _CPUFreqSysctlPrivate CPUFreqSysctlPrivate;
+
+struct _CPUFreqSysctlPrivate
+{
+      gint pmax;
+};
+
+GType cpufreq_sysctl_get_type ()
+{
+      static GType type = 0;
+
+      if (!type) {
+            static const GTypeInfo info = {
+                   sizeof (CPUFreqSysctlClass),
+                   (GBaseInitFunc) NULL,
+                   (GBaseFinalizeFunc) NULL,
+                   (GClassInitFunc) cpufreq_sysctl_class_init,
+                   NULL,
+                   NULL,
+                   sizeof (CPUFreqSysctl),
+                   0,
+                   (GInstanceInitFunc) cpufreq_sysctl_init
+            };
+
+            type = g_type_register_static (PARENT_TYPE, "CPUFreqSysctl",
+                                     &info, 0);
+      }
+
+      return type;
+}
+
+static void
+cpufreq_sysctl_init (CPUFreqSysctl *cfq)
+{
+      CPUFreqSysctlPrivate *private;
+      
+      g_return_if_fail (IS_CPUFREQ_SYSCTL (cfq));
+
+      private = CPUFREQ_SYSCTL_GET_PRIVATE (cfq);
+}
+
+static void
+cpufreq_sysctl_class_init (CPUFreqSysctlClass *klass)
+{
+      GObjectClass *object_class = G_OBJECT_CLASS (klass);
+      CPUFreqClass *cfq_class = CPUFREQ_CLASS (klass);
+
+      parent_class = g_type_class_peek_parent (klass);
+
+      g_type_class_add_private (klass, sizeof (CPUFreqSysctlPrivate));
+
+      cfq_class->set_governor = cpufreq_sysctl_set_governor;
+      cfq_class->set_frequency = cpufreq_sysctl_set_frequency;
+
+      object_class->finalize = cpufreq_sysctl_finalize;
+}
+
+static void
+cpufreq_sysctl_finalize (GObject *object)
+{
+      if (G_OBJECT_CLASS (parent_class)->finalize)
+            (* G_OBJECT_CLASS (parent_class)->finalize) (object);
+}
+
+CPUFreqSysctl *
+cpufreq_sysctl_new ()
+{
+      CPUFreqSysctl *cfq;
+
+      cfq = g_object_new (TYPE_CPUFREQ_SYSCTL, NULL);
+
+      cpufreq_sysctl_setup (cfq);
+
+      return cfq;
+}
+
+static void
+cpufreq_sysctl_set_governor (CPUFreq *cfq, const gchar *governor)
+{
+    /* Not implemented. */
+}
+
+static void
+cpufreq_sysctl_set_frequency (CPUFreq *cfq, gint frequency)
+{
+      gint                  cpu, i;
+      size_t                len;
+      gchar                *freq_oid;
+
+      g_return_if_fail (IS_CPUFREQ_SYSCTL (cfq));
+
+      g_object_get (G_OBJECT (cfq), "n_cpu", &cpu, NULL);
+
+      frequency = (gint) ((gdouble) frequency / 1000); /* Convert back to MHz*/
+
+      for (i = 0; i < cpu; i++) {
+                freq_oid = g_strdup_printf ("dev.cpu.%d.freq", i);
+
+            sysctlbyname (freq_oid, NULL, &len, &frequency, sizeof (frequency));
+            g_free (freq_oid);
+      }
+}
+   
+
+static void
+cpufreq_sysctl_setup (CPUFreqSysctl *cfq)
+{
+      guint                 cpu;
+      gint                  fmax, fmin;
+      gint                  pmax;
+      gint                  ifreq;
+      gint                  mib[4];
+      gchar                *governor, *levels;
+      gchar               **levelsp, **frpr, **l;
+      size_t                len;
+      CPUFreqSysctlPrivate *private;
+
+      g_return_if_fail (IS_CPUFREQ_SYSCTL (cfq));
+
+      private = CPUFREQ_SYSCTL_GET_PRIVATE (cfq);
+
+      pmax = 100;
+      fmax = 0;
+      fmin = 0;
+      ifreq = 0;
+
+      private->pmax = pmax;
+
+      len = sizeof (cpu);
+
+      if (sysctlbyname ("hw.ncpu", &cpu, &len, NULL, 0) == -1)
+                cpu = 1;
+      
+      len = 4;
+      sysctlnametomib ("dev.cpu.0.freq_levels", mib, &len);
+      len = sizeof (levels);
+
+      if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1) {
+                g_warning ("Failed to fetch dev.cpu.0.freq_levels");
+            return;
+      }
+
+      levels = g_malloc (len);
+      if (sysctl (mib, 4, levels, &len, NULL, 0) == -1) {
+                g_warning ("Failed to fetch data for dev.cpu.0.freq_levels");
+            return;
+      }
+
+      levelsp = g_strsplit (levels, " ", 0);
+      g_free (levels);
+
+      frpr = g_strsplit (levelsp[0], "/", 0); /* MAX */
+      if (frpr && frpr[0] != NULL)
+                fmax = atoi (frpr[0]);
+      g_strfreev (frpr);
+
+      for (l = levelsp; l && *l; l++) /* Walk to the last frequency */
+                ;
+
+      l --;
+      frpr = g_strsplit (*l, "/", 0);
+      if (frpr && frpr[0] != NULL)
+                fmin = atoi (frpr[0]);
+      
+      g_strfreev (frpr);
+      g_strfreev (levelsp);
+
+      len = sizeof (ifreq);
+      if (sysctlbyname ("dev.cpu.0.freq", &ifreq, &len, NULL, 0) == -1) {
+                g_warning ("Failed to fetch data for dev.cpu.0.freq");
+            return;
+      }
+
+      if (ifreq == fmax)
+                governor = g_strdup ("performance");
+      else if (ifreq == fmin)
+                governor = g_strdup ("economy");
+      else
+                governor = g_strdup ("other");
+      
+      fmax *= 1000;
+      fmin *= 1000;
+
+      g_object_set (G_OBJECT (cfq), "n_cpu", cpu, 
+            "sc_max", fmax, "sc_min", fmin,
+            "governor", governor, NULL);
+
+      g_free (governor);
+}
--- cpufreq/src/cpufreq-selector/main.c.orig    Sun Apr  3 12:03:13 2005
+++ cpufreq/src/cpufreq-selector/main.c Sun Jul  3 17:16:19 2005
@@ -17,6 +17,7 @@
  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  * Authors : Carlos García Campos <carlosgc@gnome.org>
+ *           Joe Marcus Clarke <marcus@FreeBSD.org>
  */
 
 #include <glib-object.h>
@@ -26,6 +27,9 @@
 #include "cpufreq.h"
 #include "cpufreq-sysfs.h"
 #include "cpufreq-procfs.h"
+#ifdef __FreeBSD__
+#include "cpufreq-sysctl.h"
+#endif /* __FreeBSD__ */
 
 gint
 main (gint argc, gchar **argv)
@@ -34,6 +38,9 @@ main (gint argc, gchar **argv)
         static gint     cpu = 0;
         static gchar   *governor = NULL;
         static gulong   frequency = 0;
+#ifdef __FreeBSD__
+   size_t          len;
+#endif /* __FreeBSD__ */
    GError         *error = NULL;
         CPUFreq        *cfq;
 
@@ -71,10 +78,15 @@ main (gint argc, gchar **argv)
    
    g_option_context_free (context);
    
+#ifndef __FreeBSD__
         if (g_file_test ("/sys/devices/system/cpu/cpu0/cpufreq", G_FILE_TEST_EXISTS)) { /* 2.6 kernel */
                 cfq = CPUFREQ (cpufreq_sysfs_new ());
         } else if (g_file_test ("/proc/cpufreq", G_FILE_TEST_EXISTS)) { /* 2.4 kernel */
                 cfq = CPUFREQ (cpufreq_procfs_new ());
+#else
+   if (sysctlbyname ("dev.cpu.0.freq", NULL, &len, NULL, 0) == 0) {
+       cfq = CPUFREQ (cpufreq_sysctl_new ());
+#endif /* __FreeBSD__ */
         } else {
                 g_print ("No cpufreq support\n");
                 return 1;
--- cpufreq/src/cpufreq-selector/Makefile.in.orig   Tue Oct  4 00:21:25 2005
+++ cpufreq/src/cpufreq-selector/Makefile.in    Tue Oct  4 00:27:54 2005
@@ -55,7 +55,7 @@
 PROGRAMS = $(bin_PROGRAMS)
 am_cpufreq_selector_OBJECTS = cpufreq.$(OBJEXT) \
    cpufreq-sysfs.$(OBJEXT) cpufreq-procfs.$(OBJEXT) \
-   main.$(OBJEXT)
+   cpufreq-sysctl.$(OBJEXT) main.$(OBJEXT)
 cpufreq_selector_OBJECTS = $(am_cpufreq_selector_OBJECTS)
 am__DEPENDENCIES_1 =
 cpufreq_selector_DEPENDENCIES = $(am__DEPENDENCIES_1)
@@ -64,6 +64,7 @@
 am__depfiles_maybe = depfiles
 @AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/cpufreq-procfs.Po \
 @AMDEP_TRUE@   ./$(DEPDIR)/cpufreq-sysfs.Po \
+@AMDEP_TRUE@   ./$(DEPDIR)/cpufreq-sysctl.Po \
 @AMDEP_TRUE@   ./$(DEPDIR)/cpufreq.Po ./$(DEPDIR)/main.Po
 COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
    $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
@@ -332,6 +333,7 @@
    cpufreq.c               cpufreq.h        \
    cpufreq-sysfs.c         cpufreq-sysfs.h  \
    cpufreq-procfs.c        cpufreq-procfs.h \
+   cpufreq-sysctl.c        cpufreq-sysctl.h \
    main.c
 
 cpufreq_selector_LDADD = $(CPUFREQ_SELECTOR_LIBS) -lpopt
@@ -407,6 +409,7 @@
    -rm -f *.tab.c
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpufreq-procfs.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpufreq-sysctl.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpufreq-sysfs.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpufreq.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@