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
|
/*
* MATE CPUFreq Applet
* Copyright (C) 2004 Carlos Garcia Campos <carlosgc@gnome.org>
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors : Carlos Garc�a Campos <carlosgc@gnome.org>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include <glib/gstdio.h>
#include <cpufreq.h>
#include <stdlib.h>
#include <linux/version.h>
#include "cpufreq-selector-libcpufreq.h"
static gboolean
cpufreq_selector_libcpufreq_set_frequency (CPUFreqSelector *selector,
guint frequency,
GError **error);
static gboolean
cpufreq_selector_libcpufreq_set_governor (CPUFreqSelector *selector,
const gchar *governor,
GError **error);
G_DEFINE_TYPE (CPUFreqSelectorLibcpufreq, cpufreq_selector_libcpufreq, CPUFREQ_TYPE_SELECTOR)
#ifdef HAVE_GET_FREQUENCIES
typedef struct cpufreq_frequencies CPUFreqFrequencyList;
#define cpufreq_get_available_frequencies(cpu) cpufreq_get_frequencies ("available", cpu)
#define cpufreq_put_available_frequencies(first) cpufreq_put_frequencies (first)
#else
typedef struct cpufreq_available_frequencies CPUFreqFrequencyList;
#endif
typedef struct cpufreq_policy CPUFreqPolicy;
typedef struct cpufreq_available_governors CPUFreqGovernorList;
static void
cpufreq_selector_libcpufreq_init (CPUFreqSelectorLibcpufreq *selector)
{
}
static void
cpufreq_selector_libcpufreq_class_init (CPUFreqSelectorLibcpufreqClass *klass)
{
CPUFreqSelectorClass *selector_class = CPUFREQ_SELECTOR_CLASS (klass);
selector_class->set_frequency = cpufreq_selector_libcpufreq_set_frequency;
selector_class->set_governor = cpufreq_selector_libcpufreq_set_governor;
}
CPUFreqSelector *
cpufreq_selector_libcpufreq_new (guint cpu)
{
CPUFreqSelector *selector;
selector = CPUFREQ_SELECTOR (g_object_new (CPUFREQ_TYPE_SELECTOR_LIBCPUFREQ,
"cpu", cpu,
NULL));
return selector;
}
static guint
cpufreq_selector_libcpufreq_get_valid_frequency (CPUFreqSelectorLibcpufreq *selector,
guint frequency)
{
guint cpu;
gint dist = G_MAXINT;
guint retval = 0;
CPUFreqFrequencyList *freqs, *freq;
g_object_get (G_OBJECT (selector),
"cpu", &cpu,
NULL);
freqs = cpufreq_get_available_frequencies (cpu);
if (!freqs)
return 0;
for (freq = freqs; freq; freq = freq->next) {
guint current_dist;
if (freq->frequency == frequency) {
cpufreq_put_available_frequencies (freqs);
return frequency;
}
current_dist = abs ((int)freq->frequency - (int)frequency);
if (current_dist < dist) {
dist = current_dist;
retval = freq->frequency;
}
}
return retval;
}
static gboolean
cpufreq_selector_libcpufreq_set_frequency (CPUFreqSelector *selector,
guint frequency,
GError **error)
{
guint freq;
guint cpu;
g_object_get (G_OBJECT (selector),
"cpu", &cpu,
NULL);
freq = cpufreq_selector_libcpufreq_get_valid_frequency (CPUFREQ_SELECTOR_LIBCPUFREQ (selector),
frequency);
if (cpufreq_set_frequency (cpu, freq) != 0) {
g_set_error (error,
CPUFREQ_SELECTOR_ERROR,
SELECTOR_ERROR_SET_FREQUENCY,
"Cannot set frequency '%d'",
frequency);
return FALSE;
}
return TRUE;
}
static gboolean
cpufreq_selector_libcpufreq_validate_governor (CPUFreqSelectorLibcpufreq *selector,
const gchar *governor)
{
guint cpu;
CPUFreqGovernorList *govs, *gov;
g_object_get (G_OBJECT (selector),
"cpu", &cpu,
NULL);
govs = cpufreq_get_available_governors (cpu);
if (!govs)
return FALSE;
for (gov = govs; gov; gov = gov->next) {
if (g_ascii_strcasecmp (gov->governor, governor) == 0) {
cpufreq_put_available_governors (govs);
return TRUE;
}
}
cpufreq_put_available_governors (govs);
return FALSE;
}
static gboolean
cpufreq_selector_libcpufreq_set_governor (CPUFreqSelector *selector,
const gchar *governor,
GError **error)
{
CPUFreqSelectorLibcpufreq *selector_libcpufreq;
guint cpu;
selector_libcpufreq = CPUFREQ_SELECTOR_LIBCPUFREQ (selector);
if (!cpufreq_selector_libcpufreq_validate_governor (selector_libcpufreq, governor)) {
g_set_error (error,
CPUFREQ_SELECTOR_ERROR,
SELECTOR_ERROR_INVALID_GOVERNOR,
"Invalid governor '%s'",
governor);
return FALSE;
}
g_object_get (G_OBJECT (selector),
"cpu", &cpu,
NULL);
if (cpufreq_modify_policy_governor (cpu, (gchar *)governor) != 0) {
g_set_error (error,
CPUFREQ_SELECTOR_ERROR,
SELECTOR_ERROR_INVALID_GOVERNOR,
"Invalid governor '%s'",
governor);
return FALSE;
}
return TRUE;
}
|