[sysprof] libsysprof: start on battery charge capture
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [sysprof] libsysprof: start on battery charge capture
- Date: Tue, 11 Jun 2019 04:32:45 +0000 (UTC)
commit 1d0a269b80f2369e49539345f9fb49cef2a7b71f
Author: Christian Hergert <chergert redhat com>
Date: Mon Jun 10 15:40:26 2019 -0700
libsysprof: start on battery charge capture
src/libsysprof-ui/ui/sysprof-profiler-assistant.ui | 14 +
src/libsysprof/meson.build | 2 +
src/libsysprof/sysprof-battery-source.c | 304 +++++++++++++++++++++
src/libsysprof/sysprof-battery-source.h | 35 +++
src/libsysprof/sysprof.h | 1 +
5 files changed, 356 insertions(+)
---
diff --git a/src/libsysprof-ui/ui/sysprof-profiler-assistant.ui
b/src/libsysprof-ui/ui/sysprof-profiler-assistant.ui
index 699a796..03cfe3f 100644
--- a/src/libsysprof-ui/ui/sysprof-profiler-assistant.ui
+++ b/src/libsysprof-ui/ui/sysprof-profiler-assistant.ui
@@ -43,6 +43,13 @@
</object>
</child>
</object>
+ <object class="SysprofAid" id="battery_aid">
+ <property name="display-name">Battery</property>
+ <property name="icon-name">battery-low-charging-symbolic</property>
+ <child>
+ <object class="SysprofBatterySource"/>
+ </child>
+ </object>
<template class="SysprofProfilerAssistant" parent="GtkBin">
<child>
<object class="GtkScrolledWindow">
@@ -140,6 +147,13 @@
<property name="visible">true</property>
</object>
</child>
+ <child>
+ <object class="SysprofAidIcon">
+ <property name="aid">battery_aid</property>
+ <property name="selected">false</property>
+ <property name="visible">true</property>
+ </object>
+ </child>
</object>
</child>
</object>
diff --git a/src/libsysprof/meson.build b/src/libsysprof/meson.build
index 32f4552..2968cc2 100644
--- a/src/libsysprof/meson.build
+++ b/src/libsysprof/meson.build
@@ -3,6 +3,7 @@ if get_option('libsysprof')
libsysprof_c_args = [ '-DSYSPROF_COMPILATION' ]
libsysprof_public_sources = [
+ 'sysprof-battery-source.c',
'sysprof-callgraph-profile.c',
'sysprof-capture-gobject.c',
'sysprof-capture-symbol-resolver.c',
@@ -27,6 +28,7 @@ libsysprof_public_sources = [
]
libsysprof_public_headers = [
+ 'sysprof-battery-source.h',
'sysprof-callgraph-profile.h',
'sysprof-capture-gobject.h',
'sysprof-capture-symbol-resolver.h',
diff --git a/src/libsysprof/sysprof-battery-source.c b/src/libsysprof/sysprof-battery-source.c
new file mode 100644
index 0000000..82824da
--- /dev/null
+++ b/src/libsysprof/sysprof-battery-source.c
@@ -0,0 +1,304 @@
+/* sysprof-battery-source.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "sysprof-battery-source"
+
+#include "config.h"
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "sysprof-battery-source.h"
+#include "sysprof-helpers.h"
+
+struct _SysprofBatterySource
+{
+ GObject parent_instance;
+
+ SysprofCaptureWriter *writer;
+ GArray *batteries;
+
+ guint poll_source;
+};
+
+typedef struct
+{
+ gchar id[32];
+ gchar name[52];
+ guint charge_full;
+ guint charge_now;
+ gint charge_now_fd;
+ guint counter_id;
+} Battery;
+
+static void source_iface_init (SysprofSourceInterface *);
+
+G_DEFINE_TYPE_WITH_CODE (SysprofBatterySource, sysprof_battery_source, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (SYSPROF_TYPE_SOURCE, source_iface_init))
+
+static void
+battery_clear (gpointer data)
+{
+ Battery *b = data;
+
+ if (b->charge_now_fd != -1)
+ close (b->charge_now_fd);
+}
+
+static gboolean
+sysprof_battery_source_get_is_ready (SysprofSource *source)
+{
+ return TRUE;
+}
+
+static void
+sysprof_battery_source_prepare (SysprofSource *source)
+{
+ SysprofBatterySource *self = (SysprofBatterySource *)source;
+ g_autoptr(GDir) dir = NULL;
+ g_autoptr(GArray) counters = NULL;
+ const gchar *name;
+
+ g_assert (SYSPROF_IS_BATTERY_SOURCE (self));
+
+#define BAT_BASE_PATH "/sys/class/power_supply/"
+
+ counters = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounter));
+
+ if (!(dir = g_dir_open (BAT_BASE_PATH, 0, NULL)))
+ goto emit_ready;
+
+ while ((name = g_dir_read_name (dir)))
+ {
+ g_autofree gchar *type_path = g_strdup_printf (BAT_BASE_PATH "%s/type", name);
+ g_autofree gchar *model_path = g_strdup_printf (BAT_BASE_PATH "%s/model_name", name);
+ g_autofree gchar *charge_now_path = g_strdup_printf (BAT_BASE_PATH "%s/charge_now", name);
+ g_autofree gchar *charge_full_path = g_strdup_printf (BAT_BASE_PATH "%s/charge_full", name);
+ g_autofree gchar *type_data = NULL;
+ g_autofree gchar *model_data = NULL;
+ g_autofree gchar *charge_full_data = NULL;
+ g_autofree gchar *charge_now_data = NULL;
+ SysprofCaptureCounter ctr;
+ Battery bat = {0};
+
+ /* We dn't care about AC */
+ if (g_strcmp0 (name, "AC") == 0)
+ continue;
+
+ if (!g_file_get_contents (type_path, &type_data, NULL, NULL) ||
+ !g_str_has_prefix (type_data, "Battery"))
+ continue;
+
+ g_strlcpy (bat.id, name, sizeof bat.id);
+
+ if (g_file_get_contents (model_path, &model_data, NULL, NULL))
+ g_strlcpy (bat.name, model_data, sizeof bat.name);
+
+ if (g_file_get_contents (charge_full_path, &charge_full_data, NULL, NULL))
+ bat.charge_full = atoi (charge_full_data);
+
+ if (g_file_get_contents (charge_now_path, &charge_now_data, NULL, NULL))
+ bat.charge_now = atoi (charge_now_data);
+
+ g_strstrip (bat.id);
+ g_strstrip (bat.name);
+
+ bat.charge_now_fd = open (charge_now_path, O_RDONLY);
+
+ if (bat.charge_now_fd == -1)
+ continue;
+
+ bat.counter_id = sysprof_capture_writer_request_counter (self->writer, 1);
+
+ g_strlcpy (ctr.category, "Battery Change", sizeof ctr.category);
+ g_strlcpy (ctr.name, bat.id, sizeof ctr.name);
+ g_strlcpy (ctr.description, bat.name, sizeof ctr.description);
+ ctr.id = bat.counter_id;
+ ctr.type = SYSPROF_CAPTURE_COUNTER_INT64;
+
+ g_array_append_val (self->batteries, bat);
+ g_array_append_val (counters, ctr);
+ }
+
+ if (counters->len)
+ sysprof_capture_writer_define_counters (self->writer,
+ SYSPROF_CAPTURE_CURRENT_TIME,
+ -1,
+ -1,
+ (gpointer)counters->data,
+ counters->len);
+
+#undef BAT_BASE_PATH
+
+emit_ready:
+ sysprof_source_emit_ready (source);
+}
+
+static gboolean
+battery_poll (Battery *battery,
+ SysprofCaptureCounterValue *value)
+{
+ gssize len;
+ gchar buf[32];
+
+ g_assert (battery != NULL);
+
+ if (battery->charge_now_fd == -1)
+ return FALSE;
+
+ if (lseek (battery->charge_now_fd, 0, SEEK_SET) != 0)
+ {
+ close (battery->charge_now_fd);
+ battery->charge_now_fd = -1;
+ return FALSE;
+ }
+
+ len = read (battery->charge_now_fd, buf, sizeof buf - 1);
+
+ if (len < 0)
+ {
+ close (battery->charge_now_fd);
+ battery->charge_now_fd = -1;
+ return FALSE;
+ }
+
+ buf [len] = 0;
+
+ battery->charge_now = atoi (buf);
+
+ value->v64 = battery->charge_now;
+
+ return TRUE;
+}
+
+static gboolean
+sysprof_battery_source_poll_cb (gpointer data)
+{
+ SysprofBatterySource *self = data;
+ g_autoptr(GArray) values = NULL;
+ g_autoptr(GArray) ids = NULL;
+
+ g_assert (SYSPROF_IS_BATTERY_SOURCE (self));
+
+ values = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounterValue));
+ ids = g_array_new (FALSE, FALSE, sizeof (guint));
+
+ for (guint i = 0; i < self->batteries->len; i++)
+ {
+ Battery *battery = &g_array_index (self->batteries, Battery, i);
+ SysprofCaptureCounterValue value;
+
+ if G_LIKELY (battery_poll (battery, &value))
+ {
+ g_array_append_val (ids, battery->counter_id);
+ g_array_append_val (values, value);
+ }
+ }
+
+ if (values->len > 0)
+ sysprof_capture_writer_set_counters (self->writer,
+ SYSPROF_CAPTURE_CURRENT_TIME,
+ -1,
+ -1,
+ (gconstpointer)ids->data,
+ (gconstpointer)values->data,
+ ids->len);
+
+ return G_SOURCE_CONTINUE;
+}
+
+static void
+sysprof_battery_source_start (SysprofSource *source)
+{
+ SysprofBatterySource *self = (SysprofBatterySource *)source;
+
+ self->poll_source = g_timeout_add_seconds (1, sysprof_battery_source_poll_cb, self);
+
+ g_assert (SYSPROF_IS_BATTERY_SOURCE (self));
+
+}
+
+static void
+sysprof_battery_source_stop (SysprofSource *source)
+{
+ SysprofBatterySource *self = (SysprofBatterySource *)source;
+
+ g_assert (SYSPROF_IS_BATTERY_SOURCE (self));
+
+ g_clear_handle_id (&self->poll_source, g_source_remove);
+
+ sysprof_source_emit_finished (source);
+}
+
+static void
+sysprof_battery_source_set_writer (SysprofSource *source,
+ SysprofCaptureWriter *writer)
+{
+ SysprofBatterySource *self = (SysprofBatterySource *)source;
+
+ g_assert (SYSPROF_IS_BATTERY_SOURCE (self));
+ g_assert (writer != NULL);
+
+ g_clear_pointer (&self->writer, sysprof_capture_writer_unref);
+ self->writer = sysprof_capture_writer_ref (writer);
+}
+
+static void
+source_iface_init (SysprofSourceInterface *iface)
+{
+ iface->get_is_ready = sysprof_battery_source_get_is_ready;
+ iface->prepare = sysprof_battery_source_prepare;
+ iface->set_writer = sysprof_battery_source_set_writer;
+ iface->start = sysprof_battery_source_start;
+ iface->stop = sysprof_battery_source_stop;
+}
+
+static void
+sysprof_battery_source_finalize (GObject *object)
+{
+ SysprofBatterySource *self = (SysprofBatterySource *)object;
+
+ g_clear_pointer (&self->batteries, g_array_unref);
+ g_clear_pointer (&self->writer, sysprof_capture_writer_unref);
+
+ G_OBJECT_CLASS (sysprof_battery_source_parent_class)->finalize (object);
+}
+
+static void
+sysprof_battery_source_class_init (SysprofBatterySourceClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = sysprof_battery_source_finalize;
+}
+
+static void
+sysprof_battery_source_init (SysprofBatterySource *self)
+{
+ self->batteries = g_array_new (FALSE, FALSE, sizeof (Battery));
+ g_array_set_clear_func (self->batteries, battery_clear);
+}
+
+SysprofSource *
+sysprof_battery_source_new (void)
+{
+ return g_object_new (SYSPROF_TYPE_SOURCE, NULL);
+}
diff --git a/src/libsysprof/sysprof-battery-source.h b/src/libsysprof/sysprof-battery-source.h
new file mode 100644
index 0000000..468169f
--- /dev/null
+++ b/src/libsysprof/sysprof-battery-source.h
@@ -0,0 +1,35 @@
+/* sysprof-battery-source.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include "sysprof-source.h"
+
+G_BEGIN_DECLS
+
+#define SYSPROF_TYPE_BATTERY_SOURCE (sysprof_battery_source_get_type())
+
+SYSPROF_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (SysprofBatterySource, sysprof_battery_source, SYSPROF, BATTERY_SOURCE, GObject)
+
+SYSPROF_AVAILABLE_IN_ALL
+SysprofSource *sysprof_battery_source_new (void);
+
+G_END_DECLS
diff --git a/src/libsysprof/sysprof.h b/src/libsysprof/sysprof.h
index 867c35b..213e03a 100644
--- a/src/libsysprof/sysprof.h
+++ b/src/libsysprof/sysprof.h
@@ -24,6 +24,7 @@ G_BEGIN_DECLS
#define SYSPROF_INSIDE
+# include "sysprof-battery-source.h"
# include "sysprof-callgraph-profile.h"
# include "sysprof-capture-gobject.h"
# include "sysprof-capture-symbol-resolver.h"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]