summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c
blob: 3ee43c47c6950b8d667c13e99eb5aebcb9a9268b (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
/*
 * 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>
 */

#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "cpufreq-selector-procfs.h"

static void     cpufreq_selector_procfs_init          (CPUFreqSelectorProcfs      *selector);
static void     cpufreq_selector_procfs_class_init    (CPUFreqSelectorProcfsClass *klass);

static gboolean cpufreq_selector_procfs_set_frequency (CPUFreqSelector            *selector,
						       guint                       frequency,
						       GError                    **error);
static gboolean cpufreq_selector_procfs_set_governor  (CPUFreqSelector            *selector,
						       const gchar                *governor,
						       GError                    **error);

G_DEFINE_TYPE (CPUFreqSelectorProcfs, cpufreq_selector_procfs, CPUFREQ_TYPE_SELECTOR)

static void
cpufreq_selector_procfs_init (CPUFreqSelectorProcfs *selector)
{
}

static void
cpufreq_selector_procfs_class_init (CPUFreqSelectorProcfsClass *klass)
{
        CPUFreqSelectorClass *selector_class = CPUFREQ_SELECTOR_CLASS (klass);

	selector_class->set_frequency = cpufreq_selector_procfs_set_frequency;
        selector_class->set_governor = cpufreq_selector_procfs_set_governor;
}

CPUFreqSelector *
cpufreq_selector_procfs_new (guint cpu)
{
        CPUFreqSelector *selector;

        selector = CPUFREQ_SELECTOR (g_object_new (CPUFREQ_TYPE_SELECTOR_PROCFS,
						   "cpu", cpu,
						   NULL));

        return selector;
}

static gboolean
cpufreq_procfs_read (guint    selector_cpu,
		     guint   *fmax,
		     guint   *pmin,
		     guint   *pmax,
		     guint   *fmin,
		     gchar   *mode,
		     GError **error)
{
	gchar  **lines;
	gchar   *buffer = NULL;
	gint     i;
	guint    cpu;
	gboolean found = FALSE;
	
	if (!g_file_get_contents ("/proc/cpufreq", &buffer, NULL, error)) {
		return FALSE;
	}

	lines = g_strsplit (buffer, "\n", -1);
	for (i = 0; lines[i]; i++) {
		if (g_ascii_strncasecmp (lines[i], "CPU", 3) == 0) {
			/* CPU  0       650000 kHz ( 81 %)  -     800000 kHz (100 %)  -  powersave */
			sscanf (lines[i], "CPU %u %u kHz (%u %%) - %u kHz (%u %%) - %20s",
				&cpu, fmin, pmin, fmax, pmax, mode);

			if (cpu == selector_cpu) {
				found = TRUE;
				break;
			}
		}
	}

	g_strfreev (lines);
	g_free (buffer);

	if (!found) {
		g_set_error (error,
			     CPUFREQ_SELECTOR_ERROR,
			     SELECTOR_ERROR_INVALID_CPU,
			     "Invalid CPU number '%d'",
			     selector_cpu);
		
		return FALSE;
	}

	return TRUE;
}

static gboolean
cpufreq_procfs_write (const gchar *path,
		      const gchar *setting,
		      GError     **error)
{
	FILE *fd;

	fd = g_fopen (path, "w");

	if (!fd) {
		g_set_error (error,
			     G_FILE_ERROR,
			     g_file_error_from_errno (errno),
			     "Failed to open '%s' for writing: "
			     "g_fopen() failed: %s",
			     path,
			     g_strerror (errno));

		return FALSE;
	}

	if (g_fprintf (fd, "%s", setting) < 0) {
		g_set_error (error,
			     G_FILE_ERROR,
			     g_file_error_from_errno (errno),
			     "Failed to write '%s': "
			     "g_fprintf() failed: %s",
			     path,
			     g_strerror (errno));

		fclose (fd);

		return FALSE;
	}

	fclose (fd);

	return TRUE;
}

static gboolean 
cpufreq_selector_procfs_set_frequency (CPUFreqSelector *selector,
				       guint            frequency,
				       GError         **error)
{
        gchar *str;
	gchar *path;
	guint  freq;
	guint  cpu;
	guint  pmin, pmax;
	guint  sc_max, sc_min;
	gchar  mode[21];

	g_object_get (G_OBJECT (selector),
		      "cpu", &cpu,
		      NULL);

	if (!cpufreq_procfs_read (cpu, &sc_max, &pmin, &pmax, &sc_min, mode, error)) {
		return FALSE;
	}

	if (g_ascii_strcasecmp (mode, "userspace") != 0) {
		if (!cpufreq_selector_procfs_set_governor (selector,
							   "userspace",
							   error)) {
			return FALSE;
		}
	}

	if (frequency != sc_max && frequency != sc_min) {
		if (abs (sc_max - frequency) < abs (frequency - sc_min))
			freq = sc_max;
		else
			freq = sc_min;
	} else {
		freq = frequency;
	}
	
	path = g_strdup_printf ("/proc/sys/cpu/%u/speed", cpu);
	str = g_strdup_printf ("%u", freq);
	if (!cpufreq_procfs_write (path, str, error)) {
		g_free (path);
		g_free (str);

		return FALSE;
	}

	g_free (path);
	g_free (str);
	
	return TRUE;
}
        
static gboolean
cpufreq_selector_procfs_set_governor (CPUFreqSelector *selector,
				      const gchar     *governor,
				      GError         **error)
{
	gchar *str;
	guint  cpu;
	guint  pmin, pmax;
	guint  sc_max, sc_min;
	gchar  mode[21];

	g_object_get (G_OBJECT (selector),
		      "cpu", &cpu,
		      NULL);

	if (!cpufreq_procfs_read (cpu, &sc_max, &pmin, &pmax, &sc_min, mode, error)) {
		return FALSE;
	}

	if (g_ascii_strcasecmp (governor, mode) == 0)
		return TRUE;

	str = g_strdup_printf ("%u:%u:%u:%s", cpu, sc_min, sc_max, governor);

	if (!cpufreq_procfs_write ("/proc/cpufreq", str, error)) {
		g_free (str);

		return FALSE;
	}

	g_free (str);

	return TRUE;
}