summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-monitor-sysfs.c
blob: 0d7f378a65a6590cb647187ae3a61405147971f2 (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
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
/*
 * 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/gi18n.h>

#include <string.h>
#include <stdlib.h>

#include "cpufreq-monitor-sysfs.h"
#include "cpufreq-utils.h"

enum {
    SCALING_MAX,
    SCALING_MIN,
    GOVERNOR,
    CPUINFO_MAX,
    SCALING_SETSPEED,
    SCALING_CUR_FREQ,
    N_FILES
};

static gboolean cpufreq_monitor_sysfs_run                       (CPUFreqMonitor *monitor);
static GList   *cpufreq_monitor_sysfs_get_available_frequencies (CPUFreqMonitor *monitor);
static GList   *cpufreq_monitor_sysfs_get_available_governors   (CPUFreqMonitor *monitor);

static gchar   *cpufreq_sysfs_read                              (const gchar    *path,
                                                                 GError        **error);

/* /sys/devices/system/cpu/cpu[0]/cpufreq/scaling_max_freq
 * /sys/devices/system/cpu/cpu[0]/cpufreq/scaling_min_freq
 * /sys/devices/system/cpu/cpu[0]/cpufreq/scaling_governor
 * /sys/devices/system/cpu/cpu[0]/cpufreq/cpuinfo_max_freq
 * /sys/devices/system/cpu/cpu[0]/cpufreq/scaling_setspeed (userspace)
 * /sys/devices/system/cpu/cpu[0]/cpufreq/scaling_cur_freq (new governors)
 */
const gchar *monitor_sysfs_files[] = {
    "scaling_max_freq",
    "scaling_min_freq",
    "scaling_governor",
    "cpuinfo_max_freq",
    "scaling_setspeed",
    "scaling_cur_freq",
    NULL
};

#define CPUFREQ_SYSFS_BASE_PATH "/sys/devices/system/cpu/cpu%u/cpufreq/%s"

G_DEFINE_TYPE (CPUFreqMonitorSysfs, cpufreq_monitor_sysfs, CPUFREQ_TYPE_MONITOR)

static void
cpufreq_monitor_sysfs_init (CPUFreqMonitorSysfs *monitor)
{
}

static GObject *
cpufreq_monitor_sysfs_constructor (GType                  type,
                                   guint                  n_construct_properties,
                                   GObjectConstructParam *construct_params)
{
    GObject *object;
    gchar   *path;
    gchar   *frequency;
    gint     max_freq;
    guint    cpu;
    GError  *error = NULL;

    object = G_OBJECT_CLASS (
        cpufreq_monitor_sysfs_parent_class)->constructor (type,
                                                          n_construct_properties,
                                                          construct_params);
    g_object_get (G_OBJECT (object),
                  "cpu", &cpu,
                  NULL);

    path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH,
                            cpu, monitor_sysfs_files[CPUINFO_MAX]);

    frequency = cpufreq_sysfs_read (path, &error);
    if (!frequency) {
        g_warning ("%s", error->message);
        g_error_free (error);
        max_freq = -1;
    } else {
        max_freq = atoi (frequency);
    }

    g_free (path);
    g_free (frequency);

    g_object_set (G_OBJECT (object),
                  "max-frequency", max_freq,
                  NULL);

    return object;
}

static void
cpufreq_monitor_sysfs_class_init (CPUFreqMonitorSysfsClass *klass)
{
    GObjectClass        *object_class = G_OBJECT_CLASS (klass);
    CPUFreqMonitorClass *monitor_class = CPUFREQ_MONITOR_CLASS (klass);

    object_class->constructor = cpufreq_monitor_sysfs_constructor;

    monitor_class->run = cpufreq_monitor_sysfs_run;
    monitor_class->get_available_frequencies = cpufreq_monitor_sysfs_get_available_frequencies;
    monitor_class->get_available_governors = cpufreq_monitor_sysfs_get_available_governors;
}

CPUFreqMonitor *
cpufreq_monitor_sysfs_new (guint cpu)
{
    CPUFreqMonitorSysfs *monitor;

    monitor = g_object_new (CPUFREQ_TYPE_MONITOR_SYSFS,
                            "cpu", cpu, NULL);

    return CPUFREQ_MONITOR (monitor);
}

static gchar *
cpufreq_sysfs_read (const gchar *path,
                    GError     **error)
{
    gchar *buffer = NULL;

    if (!cpufreq_file_get_contents (path, &buffer, NULL, error)) {
        return NULL;
    }

    return g_strchomp (buffer);
}

static gboolean
cpufreq_sysfs_cpu_is_online (guint cpu)
{
    gchar   *path;
    gboolean retval;

    path = g_strdup_printf ("/sys/devices/system/cpu/cpu%u/", cpu);
    retval = g_file_test (path, G_FILE_TEST_IS_DIR);
    g_free (path);

    return retval;
}

static gboolean
cpufreq_monitor_sysfs_run (CPUFreqMonitor *monitor)
{
    guint   cpu;
    gchar  *frequency;
    gchar  *governor;
    gchar  *path;
    GError *error = NULL;

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

    path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH,
                            cpu, monitor_sysfs_files[GOVERNOR]);
    governor = cpufreq_sysfs_read (path, &error);
    if (!governor) {
        gboolean retval = FALSE;

        /* Check whether it failed because
         * cpu is not online.
         */
        if (!cpufreq_sysfs_cpu_is_online (cpu)) {
            g_object_set (G_OBJECT (monitor), "online", FALSE, NULL);
            retval = TRUE;
        } else {
            g_warning ("%s", error->message);
        }

        g_error_free (error);
        g_free (path);

        return retval;
    }

    g_free (path);

    if (g_ascii_strcasecmp (governor, "userspace") == 0) {
        path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH,
                                cpu, monitor_sysfs_files[SCALING_SETSPEED]);
    } else if (g_ascii_strcasecmp (governor, "powersave") == 0) {
        path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH,
                                cpu, monitor_sysfs_files[SCALING_MIN]);
    } else if (g_ascii_strcasecmp (governor, "performance") == 0) {
        path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH,
                                cpu, monitor_sysfs_files[SCALING_MAX]);
    } else { /* Ondemand, Conservative, ... */
        path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH,
                                cpu, monitor_sysfs_files[SCALING_CUR_FREQ]);
    }

    frequency = cpufreq_sysfs_read (path, &error);
    if (!frequency) {
        g_warning ("%s", error->message);
        g_error_free (error);
        g_free (path);
        g_free (governor);

        return FALSE;
    }

    g_free (path);

    g_object_set (G_OBJECT (monitor),
                  "online", TRUE,
                  "governor", governor,
                  "frequency", atoi (frequency),
                  NULL);

    g_free (governor);
    g_free (frequency);

    return TRUE;
}

static gint
compare (gconstpointer a, gconstpointer b)
{
    gint aa, bb;

    aa = atoi ((gchar *) a);
    bb = atoi ((gchar *) b);

    if (aa == bb)
        return 0;
    else if (aa > bb)
        return -1;
    else
        return 1;
}

static GList *
cpufreq_monitor_sysfs_get_available_frequencies (CPUFreqMonitor *monitor)
{
    gchar  *path;
    GList  *list = NULL;
    gchar **frequencies = NULL;
    gint    i;
    guint   cpu;
    gchar  *buffer = NULL;
    GError *error = NULL;

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

    path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH, cpu,
                            "scaling_available_frequencies");

    if (!cpufreq_file_get_contents (path, &buffer, NULL, &error)) {
        g_warning ("%s", error->message);
        g_error_free (error);

        g_free (path);

        return NULL;
    }

    g_free (path);

    buffer = g_strchomp (buffer);
    frequencies = g_strsplit (buffer, " ", -1);

    i = 0;
    while (frequencies[i]) {
        if (!g_list_find_custom (list, frequencies[i], compare))
            list = g_list_prepend (list, g_strdup (frequencies[i]));
        i++;
    }

    g_strfreev (frequencies);
    g_free (buffer);

    return g_list_sort (list, compare);
}

static GList *
cpufreq_monitor_sysfs_get_available_governors (CPUFreqMonitor *monitor)
{
    gchar   *path;
    GList   *list = NULL;
    gchar  **governors = NULL;
    gint     i;
    guint    cpu;
    gchar   *buffer = NULL;
    GError  *error = NULL;

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

    path = g_strdup_printf (CPUFREQ_SYSFS_BASE_PATH, cpu,
                            "scaling_available_governors");

    if (!cpufreq_file_get_contents (path, &buffer, NULL, &error)) {
        g_warning ("%s", error->message);
        g_error_free (error);

        g_free (path);

        return NULL;
    }

    g_free (path);

    buffer = g_strchomp (buffer);

    governors = g_strsplit (buffer, " ", -1);

    i = 0;
    while (governors[i] != NULL) {
        list = g_list_prepend (list, g_strdup (governors[i]));
        i++;
    }

    g_strfreev (governors);
    g_free (buffer);

    return list;
}