summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c')
-rw-r--r--cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c316
1 files changed, 159 insertions, 157 deletions
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c b/cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c
index 4a2c36f0..002c387c 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-procfs.c
@@ -27,12 +27,14 @@
#include "cpufreq-selector-procfs.h"
-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);
+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)
@@ -44,197 +46,197 @@ cpufreq_selector_procfs_init (CPUFreqSelectorProcfs *selector)
static void
cpufreq_selector_procfs_class_init (CPUFreqSelectorProcfsClass *klass)
{
- CPUFreqSelectorClass *selector_class = CPUFREQ_SELECTOR_CLASS (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;
+ 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;
+ CPUFreqSelector *selector;
- selector = CPUFREQ_SELECTOR (g_object_new (CPUFREQ_TYPE_SELECTOR_PROCFS,
- "cpu", cpu,
- NULL));
+ selector = CPUFREQ_SELECTOR (g_object_new (CPUFREQ_TYPE_SELECTOR_PROCFS,
+ "cpu", cpu,
+ NULL));
- return selector;
+ return selector;
}
static gboolean
cpufreq_procfs_read (guint selector_cpu,
- guint *fmax,
- guint *pmin,
- guint *pmax,
- guint *fmin,
- gchar *mode,
- GError **error)
+ 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;
+ 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)
+ const gchar *setting,
+ GError **error)
{
- FILE *fd;
+ FILE *fd;
- fd = g_fopen (path, "w");
+ 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));
+ 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;
- }
+ 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));
+ 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);
+ fclose (fd);
- return FALSE;
- }
+ return FALSE;
+ }
- fclose (fd);
+ fclose (fd);
- return TRUE;
+ return TRUE;
}
-static gboolean
+static gboolean
cpufreq_selector_procfs_set_frequency (CPUFreqSelector *selector,
- guint frequency,
- GError **error)
+ 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 ((int)sc_max - (int)frequency) < abs ((int)frequency - (int)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;
+ 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 ((int)sc_max - (int)frequency) < abs ((int)frequency - (int)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)
+ const gchar *governor,
+ GError **error)
{
- gchar *str;
- guint cpu;
- guint pmin, pmax;
- guint sc_max, sc_min;
- gchar mode[21];
+ gchar *str;
+ guint cpu;
+ guint pmin, pmax;
+ guint sc_max, sc_min;
+ gchar mode[21];
- g_object_get (G_OBJECT (selector),
- "cpu", &cpu,
- NULL);
+ 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 (!cpufreq_procfs_read (cpu, &sc_max, &pmin, &pmax, &sc_min, mode, error)) {
+ return FALSE;
+ }
- if (g_ascii_strcasecmp (governor, mode) == 0)
- return TRUE;
+ if (g_ascii_strcasecmp (governor, mode) == 0)
+ return TRUE;
- str = g_strdup_printf ("%u:%u:%u:%s", cpu, sc_min, sc_max, governor);
+ 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);
+ if (!cpufreq_procfs_write ("/proc/cpufreq", str, error)) {
+ g_free (str);
- return FALSE;
- }
+ return FALSE;
+ }
- g_free (str);
+ g_free (str);
- return TRUE;
+ return TRUE;
}