summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-selector.c
blob: 59b3dabb04e0634c8c88693193a15f9b7e9cb75a (plain)
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
/*
 * MATE CPUFreq Applet
 * Copyright (C) 2008 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.
 */

#include <config.h>
#include <sys/sysinfo.h>

#ifdef HAVE_POLKIT
#include <dbus/dbus-glib.h>
#endif /* HAVE_POLKIT */

#include "cpufreq-selector.h"

struct _CPUFreqSelector {
	GObject parent;

#ifdef HAVE_POLKIT
	DBusGConnection *system_bus;
#endif /* HAVE_POLKIT */
};

struct _CPUFreqSelectorClass {
	GObjectClass parent_class;
};

G_DEFINE_TYPE (CPUFreqSelector, cpufreq_selector, G_TYPE_OBJECT)

static void
cpufreq_selector_finalize (GObject *object)
{
	CPUFreqSelector *selector = CPUFREQ_SELECTOR (object);

#ifdef HAVE_POLKIT
	selector->system_bus = NULL;
#endif /* HAVE_POLKIT */

	G_OBJECT_CLASS (cpufreq_selector_parent_class)->finalize (object);
}

static void
cpufreq_selector_class_init (CPUFreqSelectorClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = cpufreq_selector_finalize;
}

static void
cpufreq_selector_init (CPUFreqSelector *selector)
{
}

CPUFreqSelector *
cpufreq_selector_get_default (void)
{
	static CPUFreqSelector *selector = NULL;

	if (!selector)
		selector = CPUFREQ_SELECTOR (g_object_new (CPUFREQ_TYPE_SELECTOR, NULL));

	return selector;
}

#ifdef HAVE_POLKIT
typedef enum {
	FREQUENCY,
	GOVERNOR
} CPUFreqSelectorCall;

typedef struct {
	CPUFreqSelector *selector;
	
	CPUFreqSelectorCall call;

	guint  cpu;
	guint  frequency;
	gchar *governor;

	guint32 parent_xid;
} SelectorAsyncData;

static void selector_set_frequency_async (SelectorAsyncData *data);
static void selector_set_governor_async  (SelectorAsyncData *data);

static void
selector_async_data_free (SelectorAsyncData *data)
{
	if (!data)
		return;

	g_free (data->governor);
	g_free (data);
}

static gboolean
cpufreq_selector_connect_to_system_bus (CPUFreqSelector *selector,
					GError         **error)
{
	if (selector->system_bus)
		return TRUE;

	selector->system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, error);

	return (selector->system_bus != NULL);
}

static void
dbus_set_call_notify_cb (DBusGProxy     *proxy,
			 DBusGProxyCall *call,
			 gpointer        user_data)
{
	SelectorAsyncData *data;
	GError            *error = NULL;

	data = (SelectorAsyncData *)user_data;
	
	if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID)) {
		selector_async_data_free (data);
		return;
	}

	selector_async_data_free (data);
	g_warning ("%s", error->message);
	g_error_free (error);
}

static void
selector_set_frequency_async (SelectorAsyncData *data)
{
	DBusGProxy *proxy;
	GError     *error = NULL;

	if (!cpufreq_selector_connect_to_system_bus (data->selector, &error)) {
		g_warning ("%s", error->message);
		g_error_free (error);

		selector_async_data_free (data);

		return;
	}

	proxy = dbus_g_proxy_new_for_name (data->selector->system_bus,
					   "org.mate.CPUFreqSelector",
					   "/org/mate/cpufreq_selector/selector",
					   "org.mate.CPUFreqSelector");

	dbus_g_proxy_begin_call_with_timeout (proxy, "SetFrequency",
					      dbus_set_call_notify_cb,
					      data, NULL,
					      INT_MAX,
					      G_TYPE_UINT, data->cpu,
					      G_TYPE_UINT, data->frequency,
					      G_TYPE_INVALID,
					      G_TYPE_INVALID);
}

void
cpufreq_selector_set_frequency_async (CPUFreqSelector *selector,
				      guint            cpu,
				      guint            frequency)
{
    guint            cores;
    cores = get_nprocs() ;
	for (cpu = 0; cpu < cores; cpu = cpu+1){ 
	    SelectorAsyncData *data;

	    data = g_new0 (SelectorAsyncData, 1);

	    data->selector = selector;
	    data->call = FREQUENCY;
	    data->cpu = cpu;
	    data->frequency = frequency;

	    selector_set_frequency_async (data);
        }
}

static void
selector_set_governor_async (SelectorAsyncData *data)
{
	DBusGProxy *proxy;
	GError     *error = NULL;

	if (!cpufreq_selector_connect_to_system_bus (data->selector, &error)) {
		g_warning ("%s", error->message);
		g_error_free (error);

		selector_async_data_free (data);

		return;
	}

	proxy = dbus_g_proxy_new_for_name (data->selector->system_bus,
					   "org.mate.CPUFreqSelector",
					   "/org/mate/cpufreq_selector/selector",
					   "org.mate.CPUFreqSelector");

	dbus_g_proxy_begin_call_with_timeout (proxy, "SetGovernor",
					      dbus_set_call_notify_cb,
					      data, NULL,
					      INT_MAX,
					      G_TYPE_UINT, data->cpu,
					      G_TYPE_STRING, data->governor,
					      G_TYPE_INVALID,
					      G_TYPE_INVALID);
}

void
cpufreq_selector_set_governor_async (CPUFreqSelector *selector,
				     guint            cpu,
				     const gchar     *governor)
{
    guint            cores;
    cores = get_nprocs() ;
    for (cpu = 0; cpu < cores; cpu = cpu+1){
	    SelectorAsyncData *data;

	    data = g_new0 (SelectorAsyncData, 1);

	    data->selector = selector;
	    data->call = GOVERNOR;
	    data->cpu = cpu;
	    data->governor = g_strdup (governor);

	    selector_set_governor_async (data);
   }
}
#else /* !HAVE_POLKIT */
static void
cpufreq_selector_run_command (CPUFreqSelector *selector,
			      const gchar     *args)
{
	gchar  *command;
	gchar  *path;
	GError *error = NULL;

	path = g_find_program_in_path ("cpufreq-selector");

	if (!path)
		return;

	command = g_strdup_printf ("%s %s", path, args);
	g_free (path);

	g_spawn_command_line_async (command, &error);
	g_free (command);

	if (error) {
		g_warning ("%s", error->message);
		g_error_free (error);
	}
}

void
cpufreq_selector_set_frequency_async (CPUFreqSelector *selector,
				      guint            cpu,
				      guint            frequency)
{
    guint            cores;
    cores = get_nprocs() ;

    for (cpu = 0; cpu < cores; cpu = cpu+1){
	    gchar *args;

	    args = g_strdup_printf ("-c %u -f %u", cpu, frequency);
	    cpufreq_selector_run_command (selector, args);
	    g_free (args);
    }
}

void
cpufreq_selector_set_governor_async (CPUFreqSelector *selector,
				     guint            cpu,
				     const gchar     *governor)
{
    guint            cores;
    cores = get_nprocs() ;
    for (cpu = 0; cpu < cores; cpu = cpu+1){
	    gchar *args;

	    args = g_strdup_printf ("-c %u -g %s", cpu, governor);
	    cpufreq_selector_run_command (selector, args);
	    g_free (args);
    }
}
#endif /* HAVE_POLKIT */