From 0a4803a06336814d663b9823039ba637be5b66e2 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 21 Aug 2020 11:48:40 -0400 Subject: [PATCH] trace: Add support for integer counters This will be used in GTask. --- glib/gtrace-private.h | 11 ++++++ glib/gtrace.c | 79 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/glib/gtrace-private.h b/glib/gtrace-private.h index 78bf04af4..5bb5b5847 100644 --- a/glib/gtrace-private.h +++ b/glib/gtrace-private.h @@ -67,4 +67,15 @@ void (g_trace_mark) (gint64 begin_time_nsec, #endif #endif +guint (g_trace_define_int64_counter) (const char *group, + const char *name, + const char *description); +void (g_trace_set_int64_counter) (guint id, + gint64 value); + +#ifndef HAVE_SYSPROF +#define g_trace_define_int64_counter(g, n, d) ((guint) -1) +#define g_trace_set_int64_counter(i,v) +#endif + G_END_DECLS diff --git a/glib/gtrace.c b/glib/gtrace.c index 29726778d..c3c36533d 100644 --- a/glib/gtrace.c +++ b/glib/gtrace.c @@ -96,3 +96,82 @@ void va_end (args); #endif /* HAVE_SYSPROF */ } + +/* + * g_trace_define_int64_counter: + * @group: name of the group for categorising this counter + * @name: name of the counter + * @description: description for the counter + * + * Defines a new counter with integer values. + * + * The name should be unique within all counters defined with + * the same @group. The description will be shown in the sysprof UI. + * + * To add entries for this counter to a trace, use + * g_trace_set_int64_counter(). + * + * Returns: ID of the counter, for use with g_trace_set_int64_counter(), + * guaranteed to never be zero + * + * Since: 2.68 + */ +guint +(g_trace_define_int64_counter) (const char *group, + const char *name, + const char *description) +{ +#ifdef HAVE_SYSPROF + SysprofCaptureCounter counter; + + counter.id = sysprof_collector_request_counters (1); + + /* sysprof not enabled? */ + if (counter.id == 0) + return (guint) -1; + + counter.type = SYSPROF_CAPTURE_COUNTER_INT64; + counter.value.v64 = 0; + g_strlcpy (counter.category, group, sizeof counter.category); + g_strlcpy (counter.name, name, sizeof counter.name); + g_strlcpy (counter.description, description, sizeof counter.description); + + sysprof_collector_define_counters (&counter, 1); + + g_assert (counter.id != 0); + + return counter.id; +#else + return (guint) -1; +#endif +} + +/* + * g_trace_set_int64_counter: + * @id: ID of the counter + * @val: the value to set the counter to + * + * Adds a counter value to a trace. + * + * The ID must be obtained via g_trace_define_int64_counter() + * before using this function. + * + * Since: 2.68 + */ +void +(g_trace_set_int64_counter) (guint id, + gint64 val) +{ +#ifdef HAVE_SYSPROF + SysprofCaptureCounterValue value; + + g_return_if_fail (id != 0); + + /* Ignore setting the counter if we failed to define it in the first place. */ + if (id == (guint) -1) + return; + + value.v64 = val; + sysprof_collector_set_counters (&id, &value, 1); +#endif +}