add combined battery counter

this is useful for situations where we have multiple batteries inserted
and we might only care about total charge
This commit is contained in:
Christian Hergert
2019-06-24 10:01:27 -07:00
parent 4792a43887
commit 47337e6c4b

View File

@ -37,6 +37,7 @@ struct _SysprofBatterySource
SysprofCaptureWriter *writer; SysprofCaptureWriter *writer;
GArray *batteries; GArray *batteries;
guint combined_id;
guint poll_source; guint poll_source;
}; };
@ -138,13 +139,28 @@ sysprof_battery_source_prepare (SysprofSource *source)
g_array_append_val (counters, ctr); g_array_append_val (counters, ctr);
} }
if (counters->len) if (counters->len > 0)
sysprof_capture_writer_define_counters (self->writer, {
SYSPROF_CAPTURE_CURRENT_TIME, SysprofCaptureCounter ctr = {0};
-1,
-1, self->combined_id = sysprof_capture_writer_request_counter (self->writer, 1);
(gpointer)counters->data,
counters->len); /* Add combined counter */
g_strlcpy (ctr.category, "Battery Charge", sizeof ctr.category);
g_strlcpy (ctr.name, "Combined", sizeof ctr.name);
g_snprintf (ctr.description, sizeof ctr.description, "Combined Battery Charge (µAh)");
ctr.id = self->combined_id;
ctr.type = SYSPROF_CAPTURE_COUNTER_INT64;
g_array_append_val (counters, ctr);
sysprof_capture_writer_define_counters (self->writer,
SYSPROF_CAPTURE_CURRENT_TIME,
-1,
-1,
(gpointer)counters->data,
counters->len);
}
#undef BAT_BASE_PATH #undef BAT_BASE_PATH
@ -201,6 +217,7 @@ sysprof_battery_source_poll_cb (gpointer data)
SysprofBatterySource *self = data; SysprofBatterySource *self = data;
g_autoptr(GArray) values = NULL; g_autoptr(GArray) values = NULL;
g_autoptr(GArray) ids = NULL; g_autoptr(GArray) ids = NULL;
gint64 combined = 0;
g_assert (SYSPROF_IS_BATTERY_SOURCE (self)); g_assert (SYSPROF_IS_BATTERY_SOURCE (self));
@ -214,19 +231,30 @@ sysprof_battery_source_poll_cb (gpointer data)
if G_LIKELY (battery_poll (battery, &value)) if G_LIKELY (battery_poll (battery, &value))
{ {
combined += value.v64;
g_array_append_val (ids, battery->counter_id); g_array_append_val (ids, battery->counter_id);
g_array_append_val (values, value); g_array_append_val (values, value);
} }
} }
if (values->len > 0) if (values->len > 0)
sysprof_capture_writer_set_counters (self->writer, {
SYSPROF_CAPTURE_CURRENT_TIME, if (self->combined_id != 0)
-1, {
-1, SysprofCaptureCounterValue value = { .v64 = combined, };
(gconstpointer)ids->data,
(gconstpointer)values->data, g_array_append_val (ids, self->combined_id);
ids->len); g_array_append_val (values, value);
}
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; return G_SOURCE_CONTINUE;
} }