summaryrefslogtreecommitdiff
path: root/cpufreq/src/cpufreq-monitor.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpufreq/src/cpufreq-monitor.c')
-rw-r--r--cpufreq/src/cpufreq-monitor.c467
1 files changed, 223 insertions, 244 deletions
diff --git a/cpufreq/src/cpufreq-monitor.c b/cpufreq/src/cpufreq-monitor.c
index 9eeb4d1ab..cf4be34be 100644
--- a/cpufreq/src/cpufreq-monitor.c
+++ b/cpufreq/src/cpufreq-monitor.c
@@ -19,79 +19,68 @@
* Authors : Carlos García Campos <carlosgc@gnome.org>
*/
-
-#include <gnome.h>
-
#include "cpufreq-monitor.h"
-#include "cpufreq-monitor-protected.h"
-#define PARENT_TYPE G_TYPE_OBJECT
+#define CPUFREQ_MONITOR_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE((obj), CPUFREQ_TYPE_MONITOR, CPUFreqMonitorPrivate))
-#define CPUFREQ_MONITOR_GET_PROTECTED(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), TYPE_CPUFREQ_MONITOR, CPUFreqMonitorProtected))
+#define CPUFREQ_MONITOR_INTERVAL 1000
+/* Properties */
enum {
PROP_0,
- PROP_CPU
+ PROP_CPU,
+ PROP_FREQUENCY,
+ PROP_MAX_FREQUENCY,
+ PROP_GOVERNOR
};
-static void cpufreq_monitor_init (CPUFreqMonitor *monitor);
-static void cpufreq_monitor_class_init (CPUFreqMonitorClass *klass);
-static void cpufreq_monitor_finalize (GObject *object);
+/* Signals */
+enum {
+ SIGNAL_CHANGED,
+ N_SIGNALS
+};
-static void cpufreq_monitor_set_property (GObject *object, guint prop_id,
- const GValue *value, GParamSpec *spec);
-static void cpufreq_monitor_get_property (GObject *object, guint prop_id,
- GValue *value, GParamSpec *spec);
+struct _CPUFreqMonitorPrivate {
+ guint cpu;
+ gint cur_freq;
+ gint max_freq;
+ gchar *governor;
+ GList *available_freqs;
+ GList *available_govs;
+ guint timeout_handler;
-static gchar *cpufreq_monitor_get_human_readable_freq (gint freq);
-static gchar *cpufreq_monitor_get_human_readable_unit (gint freq);
-static gchar *cpufreq_monitor_get_human_readable_perc (gint fmax, gint fmin);
+ gboolean changed;
+};
-static void cpufreq_monitor_free_data (CPUFreqMonitor *monitor);
+static void cpufreq_monitor_init (CPUFreqMonitor *monitor);
+static void cpufreq_monitor_class_init (CPUFreqMonitorClass *klass);
+static void cpufreq_monitor_finalize (GObject *object);
-static GObjectClass *parent_class = NULL;
+static void cpufreq_monitor_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *spec);
+static void cpufreq_monitor_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *spec);
-typedef struct _CPUFreqMonitorProtected CPUFreqMonitorProtected;
+static guint signals[N_SIGNALS];
-GType cpufreq_monitor_get_type (void)
-{
- static GType type = 0;
-
- if (!type) {
- static const GTypeInfo info = {
- sizeof (CPUFreqMonitorClass),
- (GBaseInitFunc) NULL,
- (GBaseFinalizeFunc) NULL,
- (GClassInitFunc) cpufreq_monitor_class_init,
- NULL,
- NULL,
- sizeof (CPUFreqMonitor),
- 0,
- (GInstanceInitFunc) cpufreq_monitor_init
- };
-
- type = g_type_register_static (PARENT_TYPE, "CPUFreqMonitor",
- &info, G_TYPE_FLAG_ABSTRACT);
- }
-
- return type;
-}
+G_DEFINE_ABSTRACT_TYPE (CPUFreqMonitor, cpufreq_monitor, G_TYPE_OBJECT)
static void
cpufreq_monitor_init (CPUFreqMonitor *monitor)
{
- CPUFreqMonitorProtected *private;
-
- g_return_if_fail (IS_CPUFREQ_MONITOR (monitor));
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
- private->freq = NULL;
- private->perc = NULL;
- private->unit = NULL;
- private->governor = NULL;
- private->available_freqs = NULL;
- private->available_govs = NULL;
- private->timeout_handler = 0;
+ monitor->priv = CPUFREQ_MONITOR_GET_PRIVATE (monitor);
+
+ monitor->priv->governor = NULL;
+ monitor->priv->available_freqs = NULL;
+ monitor->priv->available_govs = NULL;
+ monitor->priv->timeout_handler = 0;
+
+ monitor->priv->changed = FALSE;
}
static void
@@ -100,36 +89,55 @@ cpufreq_monitor_class_init (CPUFreqMonitorClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass);
CPUFreqMonitorClass *monitor_class = CPUFREQ_MONITOR_CLASS (klass);
- parent_class = g_type_class_peek_parent (klass);
-
object_class->set_property = cpufreq_monitor_set_property;
object_class->get_property = cpufreq_monitor_get_property;
/* Public virtual methods */
monitor_class->run = NULL;
monitor_class->get_available_frequencies = NULL;
- monitor_class->get_available_governors = NULL;
-
- /* Protected methods */
- monitor_class->get_human_readable_freq = cpufreq_monitor_get_human_readable_freq;
- monitor_class->get_human_readable_unit = cpufreq_monitor_get_human_readable_unit;
- monitor_class->get_human_readable_perc = cpufreq_monitor_get_human_readable_perc;
-
- monitor_class->free_data = cpufreq_monitor_free_data;
+ monitor_class->get_available_governors = NULL;
+ g_type_class_add_private (klass, sizeof (CPUFreqMonitorPrivate));
+
/* Porperties */
- g_object_class_install_property (object_class, PROP_CPU,
- g_param_spec_uint ("cpu", NULL, NULL,
- 0, /* MIN_UINT */
+ g_object_class_install_property (object_class,
+ PROP_CPU,
+ g_param_spec_uint ("cpu",
+ "CPU",
+ "The cpu to monitor",
+ 0,
G_MAXUINT,
- 0, /* Default */
- G_PARAM_CONSTRUCT | G_PARAM_WRITABLE ));
-
- /* Protected attributes */
- g_type_class_add_private (klass, sizeof (CPUFreqMonitorProtected));
+ 0,
+ G_PARAM_CONSTRUCT |
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_FREQUENCY,
+ g_param_spec_int ("frequency",
+ "Frequency",
+ "The current cpu frequency",
+ 0,
+ G_MAXINT,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_MAX_FREQUENCY,
+ g_param_spec_int ("max-frequency",
+ "MaxFrequency",
+ "The max cpu frequency",
+ 0,
+ G_MAXINT,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_GOVERNOR,
+ g_param_spec_string ("governor",
+ "Governor",
+ "The current cpufreq governor",
+ NULL,
+ G_PARAM_READWRITE));
/* Signals */
- monitor_class->signals[CHANGED] =
+ signals[SIGNAL_CHANGED] =
g_signal_new ("changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
@@ -142,259 +150,230 @@ cpufreq_monitor_class_init (CPUFreqMonitorClass *klass)
}
static void
-free_string (gpointer str, gpointer gdata)
-{
- if (str) g_free (str);
-}
-
-static void
cpufreq_monitor_finalize (GObject *object)
{
- CPUFreqMonitorProtected *private;
+ CPUFreqMonitor *monitor = CPUFREQ_MONITOR (object);
- g_return_if_fail (IS_CPUFREQ_MONITOR (object));
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (object);
-
- if (private->timeout_handler > 0)
- g_source_remove (private->timeout_handler);
-
- cpufreq_monitor_free_data (CPUFREQ_MONITOR (object));
-
- if (private->available_freqs) {
- g_list_foreach (private->available_freqs,
- free_string, NULL);
- g_list_free (private->available_freqs);
- private->available_freqs = NULL;
+ if (monitor->priv->timeout_handler > 0) {
+ g_source_remove (monitor->priv->timeout_handler);
+ monitor->priv->timeout_handler = 0;
}
- if (private->available_govs) {
- g_list_foreach (private->available_govs,
- free_string, NULL);
- g_list_free (private->available_govs);
- private->available_govs = NULL;
- }
-
- if (G_OBJECT_CLASS (parent_class)->finalize)
- (* G_OBJECT_CLASS (parent_class)->finalize) (object);
-}
-
-static gchar *
-cpufreq_monitor_get_human_readable_freq (gint freq)
-{
- gint divisor;
-
- if (freq > 999999) /* freq (kHz) */
- divisor = (1000 * 1000);
- else
- divisor = 1000;
+ if (monitor->priv->governor) {
+ g_free (monitor->priv->governor);
+ monitor->priv->governor = NULL;
+ }
- if (((freq % divisor) == 0) || divisor == 1000) /* integer */
- return g_strdup_printf ("%d", freq / divisor);
- else /* float */
- return g_strdup_printf ("%3.2f", ((gfloat)freq / divisor));
-}
+ if (monitor->priv->available_freqs) {
+ g_list_foreach (monitor->priv->available_freqs,
+ (GFunc) g_free,
+ NULL);
+ g_list_free (monitor->priv->available_freqs);
+ monitor->priv->available_freqs = NULL;
+ }
-static gchar *
-cpufreq_monitor_get_human_readable_unit (gint freq)
-{
- if (freq > 999999) /* freq (kHz) */
- return g_strdup ("GHz");
- else
- return g_strdup ("MHz");
-}
+ if (monitor->priv->available_govs) {
+ g_list_foreach (monitor->priv->available_govs,
+ (GFunc) g_free,
+ NULL);
+ g_list_free (monitor->priv->available_govs);
+ monitor->priv->available_govs = NULL;
+ }
-static gchar *
-cpufreq_monitor_get_human_readable_perc (gint fmax, gint fmin)
-{
- if (fmax > 0)
- return g_strdup_printf ("%d%%", (fmin * 100) / fmax);
- else
- return NULL;
+ G_OBJECT_CLASS (cpufreq_monitor_parent_class)->finalize (object);
}
static void
-cpufreq_monitor_set_property (GObject *object, guint prop_id, const GValue *value,
- GParamSpec *spec)
+cpufreq_monitor_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *spec)
{
- CPUFreqMonitor *monitor;
- CPUFreqMonitorProtected *private;
-
- g_return_if_fail (IS_CPUFREQ_MONITOR (object));
+ CPUFreqMonitor *monitor;
monitor = CPUFREQ_MONITOR (object);
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
switch (prop_id) {
- case PROP_CPU:
- private->cpu = g_value_get_uint (value);
+ case PROP_CPU: {
+ guint cpu = g_value_get_uint (value);
+
+ if (cpu != monitor->priv->cpu) {
+ monitor->priv->cpu = cpu;
+ monitor->priv->changed = TRUE;
+ }
+ }
+ break;
+ case PROP_FREQUENCY: {
+ gint freq = g_value_get_int (value);
+
+ if (freq != monitor->priv->cur_freq) {
+ monitor->priv->cur_freq = freq;
+ monitor->priv->changed = TRUE;
+ }
+ }
+ break;
+ case PROP_MAX_FREQUENCY: {
+ gint freq = g_value_get_int (value);
+
+ if (freq != monitor->priv->max_freq) {
+ monitor->priv->max_freq = freq;
+ monitor->priv->changed = TRUE;
+ }
+ }
+ break;
+ case PROP_GOVERNOR: {
+ const gchar *gov = g_value_get_string (value);
+
+ if (monitor->priv->governor) {
+ if (g_ascii_strcasecmp (gov, monitor->priv->governor) != 0) {
+ g_free (monitor->priv->governor);
+ monitor->priv->governor = gov ? g_strdup (gov) : NULL;
+ monitor->priv->changed = TRUE;
+ }
+ } else {
+ monitor->priv->governor = gov ? g_strdup (gov) : NULL;
+ monitor->priv->changed = TRUE;
+ }
+ }
break;
default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
break;
}
}
-/* Is it neccesary?? */
static void
-cpufreq_monitor_get_property (GObject *object, guint prop_id, GValue *value,
+cpufreq_monitor_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
GParamSpec *spec)
{
- CPUFreqMonitor *monitor;
- CPUFreqMonitorProtected *private;
-
- g_return_if_fail (IS_CPUFREQ_MONITOR (object));
+ CPUFreqMonitor *monitor;
monitor = CPUFREQ_MONITOR (object);
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
switch (prop_id) {
case PROP_CPU:
- g_value_set_uint (value, private->cpu);
+ g_value_set_uint (value, monitor->priv->cpu);
+ break;
+ case PROP_FREQUENCY:
+ g_value_set_int (value, monitor->priv->cur_freq);
+ break;
+ case PROP_MAX_FREQUENCY:
+ g_value_set_int (value, monitor->priv->max_freq);
+ break;
+ case PROP_GOVERNOR:
+ g_value_set_string (value, monitor->priv->governor);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+ break;
}
}
-static void
-cpufreq_monitor_free_data (CPUFreqMonitor *monitor)
+static gboolean
+cpufreq_monitor_run_cb (CPUFreqMonitor *monitor)
{
- CPUFreqMonitorProtected *private;
-
- g_return_if_fail (IS_CPUFREQ_MONITOR (monitor));
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
-
- if (private->freq) {
- g_free (private->freq);
- private->freq = NULL;
- }
-
- if (private->perc) {
- g_free (private->perc);
- private->perc = NULL;
- }
-
- if (private->unit) {
- g_free (private->unit);
- private->unit = NULL;
+ gboolean retval = FALSE;
+
+ if (CPUFREQ_MONITOR_GET_CLASS (monitor)->run)
+ retval = CPUFREQ_MONITOR_GET_CLASS (monitor)->run (monitor);
+
+ if (monitor->priv->changed) {
+ g_signal_emit (monitor, signals[SIGNAL_CHANGED], 0);
+ monitor->priv->changed = FALSE;
}
- if (private->governor) {
- g_free (private->governor);
- private->governor = NULL;
- }
+ return retval;
}
void
cpufreq_monitor_run (CPUFreqMonitor *monitor)
{
- g_return_if_fail (IS_CPUFREQ_MONITOR (monitor));
+ g_return_if_fail (CPUFREQ_IS_MONITOR (monitor));
- if (CPUFREQ_MONITOR_GET_CLASS (monitor)->run) {
- return CPUFREQ_MONITOR_GET_CLASS (monitor)->run (monitor);
- } else {
+ if (monitor->priv->timeout_handler > 0)
return;
- }
+
+ monitor->priv->timeout_handler =
+ g_timeout_add (CPUFREQ_MONITOR_INTERVAL,
+ (GSourceFunc) cpufreq_monitor_run_cb,
+ (gpointer) monitor);
}
GList *
cpufreq_monitor_get_available_frequencies (CPUFreqMonitor *monitor)
{
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), NULL);
+ g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), NULL);
+
+ if (monitor->priv->available_freqs)
+ return monitor->priv->available_freqs;
if (CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_frequencies) {
- return CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_frequencies (monitor);
- } else {
- return NULL;
+ monitor->priv->available_freqs =
+ CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_frequencies (monitor);
}
+
+ return monitor->priv->available_freqs;
}
GList *
cpufreq_monitor_get_available_governors (CPUFreqMonitor *monitor)
{
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), NULL);
+ g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), NULL);
+
+ if (monitor->priv->available_govs)
+ return monitor->priv->available_govs;
+
+ if (CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_governors) {
+ monitor->priv->available_govs =
+ CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_governors (monitor);
+ }
- if (CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_governors) {
- return CPUFREQ_MONITOR_GET_CLASS (monitor)->get_available_governors (monitor);
- } else {
- return NULL;
- }
+ return monitor->priv->available_govs;
}
guint
cpufreq_monitor_get_cpu (CPUFreqMonitor *monitor)
{
- CPUFreqMonitorProtected *private;
-
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), 0);
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
+ g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), 0);
- return private->cpu;
+ return monitor->priv->cpu;
}
-gchar *
-cpufreq_monitor_get_governor (CPUFreqMonitor *monitor)
+void
+cpufreq_monitor_set_cpu (CPUFreqMonitor *monitor, guint cpu)
{
- CPUFreqMonitorProtected *private;
-
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), NULL);
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
+ g_return_if_fail (CPUFREQ_IS_MONITOR (monitor));
- return g_strdup (private->governor);
+ g_object_set (G_OBJECT (monitor),
+ "cpu", cpu, NULL);
}
-gchar *
+gint
cpufreq_monitor_get_frequency (CPUFreqMonitor *monitor)
{
- CPUFreqMonitorProtected *private;
-
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), NULL);
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
-
- return g_strdup (private->freq);
-}
-
-gchar *
-cpufreq_monitor_get_percentage (CPUFreqMonitor *monitor)
-{
- CPUFreqMonitorProtected *private;
-
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), NULL);
+ g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), -1);
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
-
- return g_strdup (private->perc);
+ return monitor->priv->cur_freq;
}
gchar *
-cpufreq_monitor_get_unit (CPUFreqMonitor *monitor)
+cpufreq_monitor_get_governor (CPUFreqMonitor *monitor)
{
- CPUFreqMonitorProtected *private;
-
- g_return_val_if_fail (IS_CPUFREQ_MONITOR (monitor), NULL);
+ g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), NULL);
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
-
- return g_strdup (private->unit);
+ return g_strdup (monitor->priv->governor);
}
-void
-cpufreq_monitor_set_cpu (CPUFreqMonitor *monitor, guint cpu)
+gint
+cpufreq_monitor_get_percentage (CPUFreqMonitor *monitor)
{
- CPUFreqMonitorProtected *private;
-
- g_return_if_fail (IS_CPUFREQ_MONITOR (monitor));
-
- private = CPUFREQ_MONITOR_GET_PROTECTED (monitor);
+ g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), -1);
- if (private->cpu != cpu) {
- private->cpu = cpu;
- g_signal_emit (monitor, CPUFREQ_MONITOR_GET_CLASS (monitor)->signals[CHANGED], 0);
+ if (monitor->priv->max_freq > 0) {
+ return ((monitor->priv->cur_freq * 100) / monitor->priv->max_freq);
}
-}
-
+ return -1;
+}