| 1 |
/* sysprof-battery-charge.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <ctype.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#include <glib-unix.h> |
| 32 |
#include <glib/gstdio.h> |
| 33 |
|
| 34 |
#include "line-reader-private.h" |
| 35 |
|
| 36 |
#include "sysprof-battery-charge.h" |
| 37 |
#include "sysprof-instrument-private.h" |
| 38 |
#include "sysprof-recording-private.h" |
| 39 |
|
| 40 |
#define SYS_CLASS_POWER_SUPPLY "/sys/class/power_supply/" |
| 41 |
|
| 42 |
struct _SysprofBatteryCharge |
| 43 |
{ |
| 44 |
SysprofInstrument parent_instance; |
| 45 |
}; |
| 46 |
|
| 47 |
struct _SysprofBatteryChargeClass |
| 48 |
{ |
| 49 |
SysprofInstrumentClass parent_class; |
| 50 |
}; |
| 51 |
|
| 52 |
G_DEFINE_FINAL_TYPE (SysprofBatteryCharge, sysprof_battery_charge, SYSPROF_TYPE_INSTRUMENT) |
| 53 |
|
| 54 |
typedef struct _Record |
| 55 |
{ |
| 56 |
SysprofRecording *recording; |
| 57 |
DexFuture *cancellable; |
| 58 |
} Record; |
| 59 |
|
| 60 |
static void |
| 61 |
record_free (gpointer data) |
| 62 |
{ |
| 63 |
Record *record = data; |
| 64 |
|
| 65 |
g_clear_object (&record->recording); |
| 66 |
dex_clear (&record->cancellable); |
| 67 |
g_free (record); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
clear_fd (gpointer ptr) |
| 72 |
{ |
| 73 |
int *fdptr = ptr; |
| 74 |
g_clear_fd (fdptr, NULL); |
| 75 |
} |
| 76 |
|
| 77 |
static char ** |
| 78 |
collect_battery_names (void) |
| 79 |
{ |
| 80 |
GPtrArray *ar = g_ptr_array_new (); |
| 81 |
g_autoptr(GDir) dir = NULL; |
| 82 |
|
| 83 |
if ((dir = g_dir_open (SYS_CLASS_POWER_SUPPLY, 0, NULL))) |
| 84 |
{ |
| 85 |
const char *name; |
| 86 |
|
| 87 |
while ((name = g_dir_read_name (dir))) |
| 88 |
{ |
| 89 |
if (strcmp (name, "AC") == 0) |
| 90 |
continue; |
| 91 |
|
| 92 |
g_ptr_array_add (ar, g_strdup (name)); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
g_ptr_array_add (ar, NULL); |
| 97 |
|
| 98 |
return (char **)g_ptr_array_free (ar, FALSE); |
| 99 |
} |
| 100 |
|
| 101 |
typedef struct _ReadBuffer |
| 102 |
{ |
| 103 |
char buf[32]; |
| 104 |
} ReadBuffer; |
| 105 |
|
| 106 |
static DexFuture * |
| 107 |
sysprof_battery_charge_record_fiber (gpointer user_data) |
| 108 |
{ |
| 109 |
const int invalid_fd = -1; |
| 110 |
g_autofree guint *ids = NULL; |
| 111 |
g_autofree SysprofCaptureCounterValue *values = NULL; |
| 112 |
g_autofree SysprofCaptureCounter *counters = NULL; |
| 113 |
g_autofree ReadBuffer *bufs = NULL; |
| 114 |
SysprofCaptureWriter *writer; |
| 115 |
Record *record = user_data; |
| 116 |
g_autoptr(GArray) charge_fds = NULL; |
| 117 |
g_auto(GStrv) names = NULL; |
| 118 |
guint n_names; |
| 119 |
guint n_counters = 1; |
| 120 |
|
| 121 |
g_assert (record != NULL); |
| 122 |
g_assert (SYSPROF_IS_RECORDING (record->recording)); |
| 123 |
g_assert (DEX_IS_FUTURE (record->cancellable)); |
| 124 |
|
| 125 |
writer = _sysprof_recording_writer (record->recording); |
| 126 |
names = collect_battery_names (); |
| 127 |
n_names = g_strv_length (names); |
| 128 |
|
| 129 |
/* Use some stack space for our counters and values. */ |
| 130 |
ids = g_new0 (guint, n_names + 1); |
| 131 |
counters = g_new0 (SysprofCaptureCounter, n_names + 1); |
| 132 |
values = g_new0 (SysprofCaptureCounterValue, n_names + 1); |
| 133 |
bufs = g_new0 (ReadBuffer, n_names + 1); |
| 134 |
|
| 135 |
/* Setup the combined counter which is the total charge of all of |
| 136 |
* the batteries we discover on the system. |
| 137 |
*/ |
| 138 |
counters[0].id = ids[0] = sysprof_capture_writer_request_counter (writer, 1); |
| 139 |
g_strlcpy (counters[0].category, "Battery Charge", sizeof counters[0].category); |
| 140 |
g_strlcpy (counters[0].name, "Combined", sizeof counters[0].name); |
| 141 |
g_snprintf (counters[0].description, sizeof counters[0].description, "Combined Battery Charge (µAh)"); |
| 142 |
counters[0].type = SYSPROF_CAPTURE_COUNTER_INT64; |
| 143 |
counters[0].value.v64 = 0; |
| 144 |
|
| 145 |
/* Create array to store open FDs to the charge file. We'll do a |
| 146 |
* positioned read at 0 on these so we get new data on each subsequent |
| 147 |
* read via AIO. Set the first FD to invalid since that will map to the |
| 148 |
* position of the combined-counter. |
| 149 |
*/ |
| 150 |
charge_fds = g_array_new (FALSE, FALSE, sizeof (int)); |
| 151 |
g_array_set_clear_func (charge_fds, clear_fd); |
| 152 |
g_array_append_val (charge_fds, invalid_fd); |
| 153 |
|
| 154 |
for (guint i = 0; names[i]; i++) |
| 155 |
{ |
| 156 |
SysprofCaptureCounter *counter = &counters[n_counters]; |
| 157 |
const char *name = names[i]; |
| 158 |
g_autofree char *charge_path = g_build_filename (SYS_CLASS_POWER_SUPPLY, name, "charge_now", NULL); |
| 159 |
g_autofree char *model_name_path = g_build_filename (SYS_CLASS_POWER_SUPPLY, name, "model_name", NULL); |
| 160 |
g_autofree char *type_path = g_build_filename (SYS_CLASS_POWER_SUPPLY, name, "type", NULL); |
| 161 |
g_autofree char *model_name_data = NULL; |
| 162 |
g_autofree char *type_data = NULL; |
| 163 |
g_autofd int charge_fd = -1; |
| 164 |
|
| 165 |
if (!g_file_get_contents (type_path, &type_data, NULL, NULL) || |
| 166 |
!g_str_has_prefix (type_data, "Battery") || |
| 167 |
-1 == (charge_fd = open (charge_path, O_RDONLY|O_CLOEXEC))) |
| 168 |
continue; |
| 169 |
|
| 170 |
counter->id = ids[n_counters] = sysprof_capture_writer_request_counter (writer, 1); |
| 171 |
counter->type = SYSPROF_CAPTURE_COUNTER_INT64; |
| 172 |
g_strlcpy (counter->category, "Battery Charge", sizeof counter->category); |
| 173 |
if (g_file_get_contents (model_name_path, &model_name_data, NULL, NULL)) |
| 174 |
g_strlcpy (counter->name, g_strstrip (model_name_data), sizeof counter->name); |
| 175 |
else |
| 176 |
g_strlcpy (counter->name, name, sizeof counter->name); |
| 177 |
g_snprintf (counter->description, sizeof counter->description, "%s (µAh)", counter->name); |
| 178 |
counter->value.v64 = 0; |
| 179 |
|
| 180 |
g_array_append_val (charge_fds, charge_fd); |
| 181 |
charge_fd = -1; |
| 182 |
|
| 183 |
n_counters++; |
| 184 |
} |
| 185 |
|
| 186 |
/* If we only have the combined counter, then just short-circuit and |
| 187 |
* don't record any counters. Otherwise the battery charge row might |
| 188 |
* show up in UI which we would want to omit. |
| 189 |
*/ |
| 190 |
if (n_counters == 1) |
| 191 |
return dex_future_new_for_boolean (TRUE); |
| 192 |
|
| 193 |
sysprof_capture_writer_define_counters (writer, |
| 194 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 195 |
-1, |
| 196 |
-1, |
| 197 |
counters, |
| 198 |
n_counters); |
| 199 |
|
| 200 |
/* Main recording loop */ |
| 201 |
for (;;) |
| 202 |
{ |
| 203 |
g_autoptr(GPtrArray) futures = g_ptr_array_new_with_free_func (dex_unref); |
| 204 |
|
| 205 |
/* Read the contents of all the charge buffers in a single |
| 206 |
* submission to the AIO layer. |
| 207 |
*/ |
| 208 |
g_ptr_array_add (futures, dex_future_new_for_boolean (TRUE)); |
| 209 |
for (guint i = 1; i < charge_fds->len; i++) |
| 210 |
{ |
| 211 |
int charge_fd = g_array_index (charge_fds, int, i); |
| 212 |
|
| 213 |
g_ptr_array_add (futures, |
| 214 |
dex_aio_read (NULL, |
| 215 |
charge_fd, |
| 216 |
&bufs[i].buf, |
| 217 |
sizeof bufs[i].buf-1, |
| 218 |
0)); |
| 219 |
} |
| 220 |
|
| 221 |
/* Now wait until all the reads complete */ |
| 222 |
if (futures->len > 0) |
| 223 |
dex_await (dex_future_anyv ((DexFuture **)futures->pdata, futures->len), NULL); |
| 224 |
|
| 225 |
/* Parse the current charge values */ |
| 226 |
values[0].v64 = 0; |
| 227 |
for (guint i = 1; i < charge_fds->len; i++) |
| 228 |
{ |
| 229 |
gssize len = dex_await_int64 (dex_ref (g_ptr_array_index (futures, i)), NULL); |
| 230 |
guint64 v64; |
| 231 |
|
| 232 |
if (len <= 0) |
| 233 |
continue; |
| 234 |
|
| 235 |
errno = 0; |
| 236 |
bufs[i].buf[len] = 0; |
| 237 |
v64 = g_ascii_strtoull (bufs[i].buf, NULL, 10); |
| 238 |
if (v64 == G_MAXUINT64 || errno != 0) |
| 239 |
continue; |
| 240 |
|
| 241 |
values[i].v64 = v64; |
| 242 |
values[0].v64 += v64; |
| 243 |
} |
| 244 |
|
| 245 |
/* Deliver new values to capture */ |
| 246 |
sysprof_capture_writer_set_counters (writer, |
| 247 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 248 |
-1, |
| 249 |
-1, |
| 250 |
ids, |
| 251 |
values, |
| 252 |
n_counters); |
| 253 |
|
| 254 |
/* Wait for cancellation or ½ second */ |
| 255 |
dex_await (dex_future_first (dex_ref (record->cancellable), |
| 256 |
dex_timeout_new_usec (G_USEC_PER_SEC / 2), |
| 257 |
NULL), |
| 258 |
NULL); |
| 259 |
|
| 260 |
/* If cancellable is rejected, then we're done recording */ |
| 261 |
if (dex_future_get_status (record->cancellable) != DEX_FUTURE_STATUS_PENDING) |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
return dex_future_new_for_boolean (TRUE); |
| 266 |
} |
| 267 |
|
| 268 |
static DexFuture * |
| 269 |
sysprof_battery_charge_record (SysprofInstrument *instrument, |
| 270 |
SysprofRecording *recording, |
| 271 |
GCancellable *cancellable) |
| 272 |
{ |
| 273 |
Record *record; |
| 274 |
|
| 275 |
g_assert (SYSPROF_IS_BATTERY_CHARGE (instrument)); |
| 276 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 277 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 278 |
|
| 279 |
record = g_new0 (Record, 1); |
| 280 |
record->recording = g_object_ref (recording); |
| 281 |
record->cancellable = dex_cancellable_new_from_cancellable (cancellable); |
| 282 |
|
| 283 |
return dex_scheduler_spawn (NULL, 0, |
| 284 |
sysprof_battery_charge_record_fiber, |
| 285 |
record, |
| 286 |
record_free); |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
sysprof_battery_charge_class_init (SysprofBatteryChargeClass *klass) |
| 291 |
{ |
| 292 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 293 |
|
| 294 |
instrument_class->record = sysprof_battery_charge_record; |
| 295 |
} |
| 296 |
|
| 297 |
static void |
| 298 |
sysprof_battery_charge_init (SysprofBatteryCharge *self) |
| 299 |
{ |
| 300 |
} |
| 301 |
|
| 302 |
SysprofInstrument * |
| 303 |
sysprof_battery_charge_new (void) |
| 304 |
{ |
| 305 |
return g_object_new (SYSPROF_TYPE_BATTERY_CHARGE, NULL); |
| 306 |
} |