From 200b8acb2047bdfa04d0ebd27a78a61c8ae8ca03 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 6 Jun 2023 11:19:03 -0700 Subject: [PATCH] libsysprof-profile: add scaffolding for battery charge This is a SysprofInstrument that will eventually contain the contents of the sysprof-battery-source.c to get battery charge/rate/etc. Note that this is different from energy usage which will come from RAPL. --- src/libsysprof-profile/meson.build | 2 + .../sysprof-battery-charge.c | 121 ++++++++++++++++++ .../sysprof-battery-charge.h | 42 ++++++ src/libsysprof-profile/sysprof-profile.h | 1 + src/libsysprof-profile/tests/test-profiler.c | 1 + 5 files changed, 167 insertions(+) create mode 100644 src/libsysprof-profile/sysprof-battery-charge.c create mode 100644 src/libsysprof-profile/sysprof-battery-charge.h diff --git a/src/libsysprof-profile/meson.build b/src/libsysprof-profile/meson.build index 340cefd5..9d31c715 100644 --- a/src/libsysprof-profile/meson.build +++ b/src/libsysprof-profile/meson.build @@ -1,4 +1,5 @@ libsysprof_profile_public_sources = [ + 'sysprof-battery-charge.c', 'sysprof-cpu-usage.c', 'sysprof-disk-usage.c', 'sysprof-instrument.c', @@ -21,6 +22,7 @@ libsysprof_profile_private_sources = [ libsysprof_profile_public_headers = [ 'sysprof-profile.h', + 'sysprof-battery-charge.h', 'sysprof-cpu-usage.h', 'sysprof-disk-usage.h', 'sysprof-instrument.h', diff --git a/src/libsysprof-profile/sysprof-battery-charge.c b/src/libsysprof-profile/sysprof-battery-charge.c new file mode 100644 index 00000000..18e858b4 --- /dev/null +++ b/src/libsysprof-profile/sysprof-battery-charge.c @@ -0,0 +1,121 @@ +/* sysprof-battery-charge.c + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "line-reader-private.h" + +#include "sysprof-battery-charge.h" +#include "sysprof-instrument-private.h" +#include "sysprof-recording-private.h" + +struct _SysprofBatteryCharge +{ + SysprofInstrument parent_instance; +}; + +struct _SysprofBatteryChargeClass +{ + SysprofInstrumentClass parent_class; +}; + +G_DEFINE_FINAL_TYPE (SysprofBatteryCharge, sysprof_battery_charge, SYSPROF_TYPE_INSTRUMENT) + +typedef struct _Record +{ + SysprofRecording *recording; + DexFuture *cancellable; +} Record; + +static void +record_free (gpointer data) +{ + Record *record = data; + + g_clear_object (&record->recording); + dex_clear (&record->cancellable); + g_free (record); +} + +static DexFuture * +sysprof_battery_charge_record_fiber (gpointer user_data) +{ + G_GNUC_UNUSED SysprofCaptureWriter *writer; + Record *record = user_data; + + g_assert (record != NULL); + g_assert (SYSPROF_IS_RECORDING (record->recording)); + g_assert (DEX_IS_FUTURE (record->cancellable)); + + writer = _sysprof_recording_writer (record->recording); + + return dex_future_new_for_boolean (TRUE); +} + +static DexFuture * +sysprof_battery_charge_record (SysprofInstrument *instrument, + SysprofRecording *recording, + GCancellable *cancellable) +{ + Record *record; + + g_assert (SYSPROF_IS_BATTERY_CHARGE (instrument)); + g_assert (SYSPROF_IS_RECORDING (recording)); + g_assert (G_IS_CANCELLABLE (cancellable)); + + record = g_new0 (Record, 1); + record->recording = g_object_ref (recording); + record->cancellable = dex_cancellable_new_from_cancellable (cancellable); + + return dex_scheduler_spawn (NULL, 0, + sysprof_battery_charge_record_fiber, + record, + record_free); +} + +static void +sysprof_battery_charge_class_init (SysprofBatteryChargeClass *klass) +{ + SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); + + instrument_class->record = sysprof_battery_charge_record; +} + +static void +sysprof_battery_charge_init (SysprofBatteryCharge *self) +{ +} + +SysprofInstrument * +sysprof_battery_charge_new (void) +{ + return g_object_new (SYSPROF_TYPE_BATTERY_CHARGE, NULL); +} diff --git a/src/libsysprof-profile/sysprof-battery-charge.h b/src/libsysprof-profile/sysprof-battery-charge.h new file mode 100644 index 00000000..1d3093d1 --- /dev/null +++ b/src/libsysprof-profile/sysprof-battery-charge.h @@ -0,0 +1,42 @@ +/* sysprof-battery-charge.h + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include "sysprof-instrument.h" + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_BATTERY_CHARGE (sysprof_battery_charge_get_type()) +#define SYSPROF_IS_BATTERY_CHARGE(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, SYSPROF_TYPE_BATTERY_CHARGE) +#define SYSPROF_BATTERY_CHARGE(obj) G_TYPE_CHECK_INSTANCE_CAST(obj, SYSPROF_TYPE_BATTERY_CHARGE, SysprofBatteryCharge) +#define SYSPROF_BATTERY_CHARGE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST(klass, SYSPROF_TYPE_BATTERY_CHARGE, SysprofBatteryChargeClass) + +typedef struct _SysprofBatteryCharge SysprofBatteryCharge; +typedef struct _SysprofBatteryChargeClass SysprofBatteryChargeClass; + +SYSPROF_AVAILABLE_IN_ALL +GType sysprof_battery_charge_get_type (void) G_GNUC_CONST; +SYSPROF_AVAILABLE_IN_ALL +SysprofInstrument *sysprof_battery_charge_new (void); + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofBatteryCharge, g_object_unref) + +G_END_DECLS diff --git a/src/libsysprof-profile/sysprof-profile.h b/src/libsysprof-profile/sysprof-profile.h index 8ad775e2..cdc527df 100644 --- a/src/libsysprof-profile/sysprof-profile.h +++ b/src/libsysprof-profile/sysprof-profile.h @@ -25,6 +25,7 @@ G_BEGIN_DECLS #define SYSPROF_PROFILE_INSIDE +# include "sysprof-battery-charge.h" # include "sysprof-cpu-usage.h" # include "sysprof-disk-usage.h" # include "sysprof-instrument.h" diff --git a/src/libsysprof-profile/tests/test-profiler.c b/src/libsysprof-profile/tests/test-profiler.c index a57adbee..365a09d4 100644 --- a/src/libsysprof-profile/tests/test-profiler.c +++ b/src/libsysprof-profile/tests/test-profiler.c @@ -116,6 +116,7 @@ main (int argc, profiler = sysprof_profiler_new (); + sysprof_profiler_add_instrument (profiler, sysprof_battery_charge_new ()); sysprof_profiler_add_instrument (profiler, sysprof_cpu_usage_new ()); sysprof_profiler_add_instrument (profiler, sysprof_disk_usage_new ()); sysprof_profiler_add_instrument (profiler, sysprof_memory_usage_new ());