diff options
Diffstat (limited to 'cpufreq/src/cpufreq-monitor-procfs.c')
-rw-r--r-- | cpufreq/src/cpufreq-monitor-procfs.c | 284 |
1 files changed, 142 insertions, 142 deletions
diff --git a/cpufreq/src/cpufreq-monitor-procfs.c b/cpufreq/src/cpufreq-monitor-procfs.c index f43da9dc..0c7b3b9c 100644 --- a/cpufreq/src/cpufreq-monitor-procfs.c +++ b/cpufreq/src/cpufreq-monitor-procfs.c @@ -42,178 +42,178 @@ 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; + 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; + CPUFreqMonitorProcfs *monitor; - monitor = g_object_new (TYPE_CPUFREQ_MONITOR_PROCFS, "cpu", cpu, NULL); + monitor = g_object_new (TYPE_CPUFREQ_MONITOR_PROCFS, "cpu", cpu, NULL); - return CPUFREQ_MONITOR (monitor); + 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; + 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) + 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); -} + 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); + 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; + 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); + 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); } |