summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-monitor-procfs.c
blob: 97c635ba9c720ae6479db9be582f910f5f3da6e4 (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
/*
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Authors : Carlos Garc�a Campos <carlosgc@gnome.org>
 */

#include <glib.h>
#include <glib/gi18n.h>

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

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

static void     cpufreq_monitor_procfs_class_init                (CPUFreqMonitorProcfsClass *klass);

static gboolean cpufreq_monitor_procfs_run                       (CPUFreqMonitor       *monitor);
static GList   *cpufreq_monitor_procfs_get_available_frequencies (CPUFreqMonitor       *monitor);

G_DEFINE_TYPE (CPUFreqMonitorProcfs, cpufreq_monitor_procfs, CPUFREQ_TYPE_MONITOR)

static void
cpufreq_monitor_procfs_init (CPUFreqMonitorProcfs *monitor)
{
}

static void
cpufreq_monitor_procfs_class_init (CPUFreqMonitorProcfsClass *klass)
{
	CPUFreqMonitorClass *monitor_class = CPUFREQ_MONITOR_CLASS (klass);
	
	monitor_class->run = cpufreq_monitor_procfs_run;
	monitor_class->get_available_frequencies = cpufreq_monitor_procfs_get_available_frequencies;
}

CPUFreqMonitor *
cpufreq_monitor_procfs_new (guint cpu)
{
	   CPUFreqMonitorProcfs *monitor;

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

	   return CPUFREQ_MONITOR (monitor);
}

static gint
cpufreq_monitor_procfs_get_freq_from_userspace (guint cpu)
{
	gchar  *buffer = NULL;
	gchar  *path;
	gchar  *p;
	gchar  *frequency;
	gint    freq;
	gint    len;
	GError *error = NULL;
	
	path = g_strdup_printf ("/proc/sys/cpu/%u/speed", cpu);

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

		g_free (path);

		return -1;
	}

	g_free (path);
	
	/* Try to remove the '\n' */
	p = g_strrstr (buffer, "\n");
	len = strlen (buffer);
	if (p)
		len -= strlen (p);

	frequency = g_strndup (buffer, len);
	g_free (buffer);
	
	freq = atoi (frequency);
	g_free (frequency);
	
	return freq;
}

static gboolean
cpufreq_monitor_procfs_parse (CPUFreqMonitorProcfs *monitor,
			      gint                 *cpu,
			      gint                 *fmax,
			      gint                 *pmin,
			      gint                 *pmax,
			      gint                 *fmin,
			      gchar                *mode)
{
	gchar **lines;
	gchar  *buffer = NULL;
	gint    i, count;
	guint   mon_cpu;
	GError *error = NULL;

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

		return FALSE;
	}
	
	g_object_get (G_OBJECT (monitor),
		      "cpu", &mon_cpu, NULL);
	
	count = 0;
	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 */
			count = sscanf (lines[i], "CPU %d %d kHz (%d %%) - %d kHz (%d %%) - %20s",
					cpu, fmin, pmin, fmax, pmax, mode);
			
			if ((guint)(*cpu) == mon_cpu)
				break;
		}
	}
	
	g_strfreev (lines);
	g_free (buffer);

	return (count == 6);
}	   

static gboolean
cpufreq_procfs_cpu_is_online (void)
{
	return g_file_test ("/proc/cpufreq",
			    G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
}

static gboolean
cpufreq_monitor_procfs_run (CPUFreqMonitor *monitor)
{
	gint   fmax, fmin, cpu;
	gint   pmin, pmax;
	gchar  mode[21];
	gint   cur_freq, max_freq;
	gchar *governor;

	if (!cpufreq_monitor_procfs_parse (CPUFREQ_MONITOR_PROCFS (monitor),
					   &cpu, &fmax, &pmin, &pmax, &fmin, mode)) {
		/* Check whether it failed because
		 * cpu is not online.
		 */
		if (!cpufreq_procfs_cpu_is_online ()) {
			g_object_set (G_OBJECT (monitor), "online", FALSE, NULL);
			return TRUE;
		}
		return FALSE;
	}
	
	governor = mode;
	max_freq = fmax;
	
	if (g_ascii_strcasecmp (governor, "powersave") == 0) {
		cur_freq = fmin;
	} else if (g_ascii_strcasecmp (governor, "performance") == 0) {
		cur_freq = fmax;
	} else if (g_ascii_strcasecmp (governor, "userspace") == 0) {
		cur_freq = cpufreq_monitor_procfs_get_freq_from_userspace (cpu);
	} else {
		cur_freq = fmax;
	}

	g_object_set (G_OBJECT (monitor),
		      "online", TRUE,
		      "governor", governor,
		      "frequency", cur_freq,
		      "max-frequency", max_freq,
		      NULL);

	return TRUE;
}

static GList *
cpufreq_monitor_procfs_get_available_frequencies (CPUFreqMonitor *monitor)
{
	gint   fmax, fmin, cpu, freq;
	gint   pmin, pmax;
	gchar  mode[21];
	GList *list = NULL;
	
	if (!cpufreq_monitor_procfs_parse (CPUFREQ_MONITOR_PROCFS (monitor), &cpu,
					   &fmax, &pmin, &pmax, &fmin, mode)) {
		return NULL;
	}
	
	if ((pmax > 0) && (pmax != 100)) {
		freq = (fmax * 100) / pmax;
		list = g_list_prepend (list, g_strdup_printf ("%d", freq));
	}
	
	list = g_list_prepend (list, g_strdup_printf ("%d", fmax));
	if (fmax != fmin)
		list = g_list_prepend (list, g_strdup_printf ("%d", fmin));

	return g_list_reverse (list);
}