summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-monitor-cpuinfo.c
blob: 6f1f9535f91c6e80d067d45bf506fce68ca70b84 (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
/*
 * 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>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

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

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

static void     cpufreq_monitor_cpuinfo_class_init (CPUFreqMonitorCPUInfoClass *klass);

static gboolean cpufreq_monitor_cpuinfo_run        (CPUFreqMonitor *monitor);

G_DEFINE_TYPE (CPUFreqMonitorCPUInfo, cpufreq_monitor_cpuinfo, CPUFREQ_TYPE_MONITOR)

static void
cpufreq_monitor_cpuinfo_init (CPUFreqMonitorCPUInfo *monitor)
{
}

static void
cpufreq_monitor_cpuinfo_class_init (CPUFreqMonitorCPUInfoClass *klass)
{
        CPUFreqMonitorClass *monitor_class = CPUFREQ_MONITOR_CLASS (klass);

        monitor_class->run = cpufreq_monitor_cpuinfo_run;
}

CPUFreqMonitor *
cpufreq_monitor_cpuinfo_new (guint cpu)
{
        CPUFreqMonitorCPUInfo *monitor;

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

        return CPUFREQ_MONITOR (monitor);
}

static gboolean
cpufreq_monitor_cpuinfo_run (CPUFreqMonitor *monitor)
{
        gchar *file;
        gchar                   **lines;
        gchar *buffer = NULL;
        gchar                    *p;
        gint                      cpu, i;
        gint cur_freq, max_freq;
        gchar *governor;
        GError *error = NULL;

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

                g_free (file);

                return FALSE;
        }
        g_free (file);
                
        /* TODO: SMP support */
        lines = g_strsplit (buffer, "\n", -1);
        for (i = 0; lines[i]; i++) {
                if (g_ascii_strncasecmp ("cpu MHz", lines[i], strlen ("cpu MHz")) == 0) {
                        p = g_strrstr (lines[i], ":");

                        if (p == NULL) {
                                g_strfreev (lines);
                                g_free (buffer);
                                                  
                                return FALSE;
                        }

                        if (strlen (lines[i]) < (size_t)(p - lines[i])) {
                                g_strfreev (lines);
                                g_free (buffer);
                                                  
                                return FALSE;
                        }

                        if ((sscanf (p + 1, "%d.", &cpu)) != 1) {
                                g_strfreev (lines);
                                g_free (buffer);
                                                  
                                return FALSE;
                        }

                        break;
                }
        }

        g_strfreev (lines);
        g_free (buffer);
           
        governor = g_strdup (_("Frequency Scaling Unsupported"));
        cur_freq = cpu * 1000;
        max_freq = cur_freq;

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

        g_free (governor);

        return TRUE;
}