trace: Add support for integer counters

This will be used in GTask.
This commit is contained in:
Matthias Clasen 2020-08-21 11:48:40 -04:00 committed by Philip Withnall
parent ab65fb1951
commit 0a4803a063
2 changed files with 90 additions and 0 deletions

View File

@ -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

View File

@ -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
}