summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-monitor-libcpufreq.c
blob: 5fe79fbe624fad3873792e7c8283155bb38c69cd (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
/*
 * 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 <stdlib.h>
#include <linux/version.h>
#include <cpufreq.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
#include <cpuidle.h>
#endif
#include "cpufreq-monitor-libcpufreq.h"
#include "cpufreq-utils.h"

static void     cpufreq_monitor_libcpufreq_class_init                (CPUFreqMonitorLibcpufreqClass *klass);

static gboolean cpufreq_monitor_libcpufreq_run                       (CPUFreqMonitor *monitor);
static GList   *cpufreq_monitor_libcpufreq_get_available_frequencies (CPUFreqMonitor *monitor);
static GList   *cpufreq_monitor_libcpufreq_get_available_governors   (CPUFreqMonitor *monitor);

G_DEFINE_TYPE (CPUFreqMonitorLibcpufreq, cpufreq_monitor_libcpufreq, CPUFREQ_TYPE_MONITOR)

typedef struct cpufreq_policy                CPUFreqPolicy;
typedef struct cpufreq_available_frequencies CPUFreqFrequencyList;
typedef struct cpufreq_available_governors   CPUFreqGovernorList;

static void
cpufreq_monitor_libcpufreq_init (CPUFreqMonitorLibcpufreq *monitor)
{
}

static GObject *
cpufreq_monitor_libcpufreq_constructor (GType                  type,
					guint                  n_construct_properties,
					GObjectConstructParam *construct_params)
{
	GObject *object;
	gulong   max_freq, min_freq;
	guint    cpu;

	object = G_OBJECT_CLASS (
		cpufreq_monitor_libcpufreq_parent_class)->constructor (type,
								       n_construct_properties,
								       construct_params);
	g_object_get (G_OBJECT (object),
		      "cpu", &cpu,
		      NULL);
	
	if (cpufreq_get_hardware_limits (cpu, &min_freq, &max_freq) != 0) {
		g_warning ("Error getting CPUINFO_MAX\n");
		max_freq = -1;
	}

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

	return object;
}

static void
cpufreq_monitor_libcpufreq_class_init (CPUFreqMonitorLibcpufreqClass *klass)
{
	GObjectClass        *object_class = G_OBJECT_CLASS (klass);
        CPUFreqMonitorClass *monitor_class = CPUFREQ_MONITOR_CLASS (klass);

	object_class->constructor = cpufreq_monitor_libcpufreq_constructor;
	
        monitor_class->run = cpufreq_monitor_libcpufreq_run;
        monitor_class->get_available_frequencies = cpufreq_monitor_libcpufreq_get_available_frequencies;
        monitor_class->get_available_governors = cpufreq_monitor_libcpufreq_get_available_governors;
}

CPUFreqMonitor *
cpufreq_monitor_libcpufreq_new (guint cpu)
{
        CPUFreqMonitorLibcpufreq *monitor;

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

        return CPUFREQ_MONITOR (monitor);
}

static gboolean
cpufreq_monitor_libcpufreq_run (CPUFreqMonitor *monitor)
{
        guint          cpu;
	CPUFreqPolicy *policy;

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

	policy = cpufreq_get_policy (cpu);
	if (!policy) {
		/* Check whether it failed because
		 * cpu is not online.
		 */
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
		if (!cpufreq_cpu_exists (cpu)) {
#else
		if (cpupower_is_cpu_online (cpu)) {
#endif
			g_object_set (G_OBJECT (monitor), "online", FALSE, NULL);
			return TRUE;
		}
		return FALSE;
	}

        g_object_set (G_OBJECT (monitor),
		      "online", TRUE,
                      "governor", policy->governor,
                      "frequency", cpufreq_get_freq_kernel (cpu),
                      NULL);

	cpufreq_put_policy (policy);
        
        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_libcpufreq_get_available_frequencies (CPUFreqMonitor *monitor)
{
	GList                *list = NULL;
	guint                 cpu;
	CPUFreqFrequencyList *freqs, *freq;

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

	freqs = cpufreq_get_available_frequencies (cpu);
	if (!freqs)
		return NULL;

	for (freq = freqs; freq; freq = freq->next) {
		gchar *frequency;

		frequency = g_strdup_printf ("%lu", freq->frequency);
		
		if (!g_list_find_custom (list, frequency, compare))
			list = g_list_prepend (list, frequency);
		else
			g_free (frequency);
	}

	cpufreq_put_available_frequencies (freqs);

        return g_list_sort (list, compare);
}

static GList *
cpufreq_monitor_libcpufreq_get_available_governors (CPUFreqMonitor *monitor)
{
	guint                cpu;
        GList               *list = NULL;
	CPUFreqGovernorList *govs, *gov;

        g_object_get (G_OBJECT (monitor),
                      "cpu", &cpu, NULL);
        
	govs = cpufreq_get_available_governors (cpu);
	if (!govs)
		return NULL;

	for (gov = govs; gov; gov = gov->next) {
		list = g_list_prepend (list, g_strdup (gov->governor));
	}

	cpufreq_put_available_governors (govs);

        return list;
}