1
0
Fork 0

cogl/trace: Allow defining and setting sysprof trace counters

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3952>
This commit is contained in:
Sebastian Wick 2024-08-16 15:22:20 +02:00 committed by Marge Bot
parent 55a97c8600
commit 777c635076
2 changed files with 202 additions and 0 deletions

View file

@ -41,6 +41,8 @@
#define COGL_TRACE_OUTPUT_FILE "cogl-trace-sp-capture.syscap"
#define BUFFER_LENGTH (4096 * 4)
#define CATEGORY "mutter"
struct _CoglTraceContext
{
gatomicrefcount ref_count;
@ -385,6 +387,122 @@ cogl_trace_describe (CoglTraceHead *head,
head->description = g_strdup (description);
}
static void
cogl_trace_set_counter (unsigned int counter,
SysprofCaptureCounterValue *value)
{
SysprofTimeStamp time;
CoglTraceContext *trace_context;
CoglTraceThreadContext *trace_thread_context;
time = g_get_monotonic_time () * 1000;
trace_thread_context = g_private_get (&cogl_trace_thread_data);
trace_context = trace_thread_context->trace_context;
g_mutex_lock (&cogl_trace_mutex);
if (!sysprof_capture_writer_set_counters (trace_context->writer,
time,
trace_thread_context->cpu_id,
trace_thread_context->pid,
&counter,
value,
1))
{
/* XXX: g_main_context_get_thread_default() might be wrong, it probably
* needs to store the GMainContext in CoglTraceThreadContext when creating
* and use it here.
*/
if (errno == EPIPE)
cogl_set_tracing_disabled_on_thread (g_main_context_get_thread_default ());
}
g_mutex_unlock (&cogl_trace_mutex);
}
void
cogl_trace_set_counter_int (unsigned int counter,
int64_t value)
{
SysprofCaptureCounterValue v;
v.v64 = value;
cogl_trace_set_counter (counter, &v);
}
void
cogl_trace_set_counter_double (unsigned int counter,
double value)
{
SysprofCaptureCounterValue v;
v.vdbl = value;
cogl_trace_set_counter (counter, &v);
}
static unsigned int
cogl_trace_define_counter (const char *name,
const char *description,
uint32_t type)
{
SysprofTimeStamp time;
CoglTraceContext *trace_context;
CoglTraceThreadContext *trace_thread_context;
SysprofCaptureCounter counter;
time = g_get_monotonic_time () * 1000;
trace_thread_context = g_private_get (&cogl_trace_thread_data);
trace_context = trace_thread_context->trace_context;
counter.id = sysprof_capture_writer_request_counter (trace_context->writer, 1);
counter.type = type;
if (type == SYSPROF_CAPTURE_COUNTER_DOUBLE)
counter.value.vdbl = 0.0;
else
counter.value.v64 = 0;
g_strlcpy (counter.category, CATEGORY, sizeof counter.category);
g_strlcpy (counter.name, name, sizeof counter.name);
g_strlcpy (counter.description, description, sizeof counter.name);
g_mutex_lock (&cogl_trace_mutex);
if (!sysprof_capture_writer_define_counters (trace_context->writer,
time,
trace_thread_context->cpu_id,
trace_thread_context->pid,
&counter,
1))
{
/* XXX: g_main_context_get_thread_default() might be wrong, it probably
* needs to store the GMainContext in CoglTraceThreadContext when creating
* and use it here.
*/
if (errno == EPIPE)
cogl_set_tracing_disabled_on_thread (g_main_context_get_thread_default ());
}
g_mutex_unlock (&cogl_trace_mutex);
return counter.id;
}
unsigned int
cogl_trace_define_counter_int (const char *name,
const char *description)
{
return cogl_trace_define_counter (name, description,
SYSPROF_CAPTURE_COUNTER_INT64);
}
unsigned int
cogl_trace_define_counter_double (const char *name,
const char *description)
{
return cogl_trace_define_counter (name, description,
SYSPROF_CAPTURE_COUNTER_DOUBLE);
}
#else
#include <string.h>

View file

@ -45,6 +45,12 @@ typedef struct _CoglTraceHead
char *description;
} CoglTraceHead;
typedef struct _CoglTraceCounterData
{
const char *name;
const char *description;
} CoglTraceCounterData;
COGL_EXPORT
GPrivate cogl_trace_thread_data;
COGL_EXPORT
@ -102,6 +108,46 @@ cogl_is_tracing_enabled (void)
return !!g_private_get (&cogl_trace_thread_data);
}
COGL_EXPORT
void cogl_trace_set_counter_int (unsigned int counter,
int64_t value);
COGL_EXPORT
void cogl_trace_set_counter_double (unsigned int counter,
double value);
COGL_EXPORT
unsigned int cogl_trace_define_counter_int (const char *name,
const char *description);
COGL_EXPORT
unsigned int cogl_trace_define_counter_double (const char *name,
const char *description);
static inline gpointer
cogl_trace_counter_data_int (gpointer user_data)
{
CoglTraceCounterData *counter_data = user_data;
int counter;
counter = cogl_trace_define_counter_int (counter_data->name,
counter_data->description);
return GUINT_TO_POINTER (counter);
}
static inline gpointer
cogl_trace_counter_data_double (gpointer user_data)
{
CoglTraceCounterData *counter_data = user_data;
int counter;
counter = cogl_trace_define_counter_double (counter_data->name,
counter_data->description);
return GUINT_TO_POINTER (counter);
}
#define COGL_TRACE_BEGIN_SCOPED(Name, name) \
CoglTraceHead CoglTrace##Name = { 0 }; \
__attribute__((cleanup (cogl_auto_trace_end_helper))) \
@ -146,6 +192,44 @@ cogl_is_tracing_enabled (void)
} \
G_STMT_END
#define COGL_TRACE_INTERNAL_DEFINE_COUNTER(Name, name, description, func) \
static GOnce CoglTraceCounter##Name = G_ONCE_INIT; \
if (cogl_is_tracing_enabled ()) \
{ \
static CoglTraceCounterData CoglTraceCounterData##Name = { \
name, description, \
}; \
g_once (&CoglTraceCounter##Name, \
func, \
&CoglTraceCounterData##Name); \
}
#define COGL_TRACE_DEFINE_COUNTER_INT(Name, name, description) \
COGL_TRACE_INTERNAL_DEFINE_COUNTER(Name, name, description, \
cogl_trace_counter_data_int)
#define COGL_TRACE_DEFINE_COUNTER_DOUBLE(Name, name, description) \
COGL_TRACE_INTERNAL_DEFINE_COUNTER(Name, name, description, \
cogl_trace_counter_data_double)
#define COGL_TRACE_INTERNAL_SET_COUNTER(Name, value, func) \
G_STMT_START \
{ \
if (cogl_is_tracing_enabled ()) \
{ \
func (GPOINTER_TO_UINT (CoglTraceCounter##Name.retval), value); \
} \
} \
G_STMT_END
#define COGL_TRACE_SET_COUNTER_INT(Name, value) \
COGL_TRACE_INTERNAL_SET_COUNTER(Name, value, \
cogl_trace_set_counter_int)
#define COGL_TRACE_SET_COUNTER_DOUBLE(Name, value) \
COGL_TRACE_INTERNAL_SET_COUNTER(Name, value, \
cogl_trace_set_counter_double)
#else /* HAVE_PROFILER */
#include <stdio.h>