util/trace: add context helpers

These allow gpuvis to link two events and compute the duration.
This commit is contained in:
Simon Ser 2022-09-24 10:00:21 +02:00
parent 79b5dcb17d
commit 6531bff140
2 changed files with 36 additions and 0 deletions

View file

@ -1,9 +1,17 @@
#ifndef UTIL_TRACE_H #ifndef UTIL_TRACE_H
#define UTIL_TRACE_H #define UTIL_TRACE_H
#include <stdint.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
void wlr_trace(const char *format, ...) _WLR_ATTRIB_PRINTF(1, 2); void wlr_trace(const char *format, ...) _WLR_ATTRIB_PRINTF(1, 2);
void wlr_vtrace(const char *format, va_list args) _WLR_ATTRIB_PRINTF(1, 0); void wlr_vtrace(const char *format, va_list args) _WLR_ATTRIB_PRINTF(1, 0);
struct wlr_trace_ctx {
uint32_t seq;
};
void wlr_trace_begin_ctx(struct wlr_trace_ctx *ctx, const char *format, ...) _WLR_ATTRIB_PRINTF(2, 3);
void wlr_trace_end_ctx(struct wlr_trace_ctx *ctx, const char *format, ...) _WLR_ATTRIB_PRINTF(2, 3);
#endif #endif

View file

@ -1,8 +1,10 @@
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include "util/trace.h" #include "util/trace.h"
static bool trace_initialized = false; static bool trace_initialized = false;
static FILE *trace_file = NULL; static FILE *trace_file = NULL;
static uint32_t prev_ctx = 0;
void wlr_vtrace(const char *fmt, va_list args) { void wlr_vtrace(const char *fmt, va_list args) {
if (!trace_initialized) { if (!trace_initialized) {
@ -28,3 +30,29 @@ void wlr_trace(const char *fmt, ...) {
wlr_vtrace(fmt, args); wlr_vtrace(fmt, args);
va_end(args); va_end(args);
} }
void wlr_trace_begin_ctx(struct wlr_trace_ctx *ctx, const char *fmt, ...) {
*ctx = (struct wlr_trace_ctx){
.seq = prev_ctx++,
};
static char new_fmt[512];
snprintf(new_fmt, sizeof(new_fmt), "%s (begin_ctx=%"PRIu32")", fmt, ctx->seq);
va_list args;
va_start(args, fmt);
wlr_vtrace(new_fmt, args);
va_end(args);
}
void wlr_trace_end_ctx(struct wlr_trace_ctx *ctx, const char *fmt, ...) {
static char new_fmt[512];
snprintf(new_fmt, sizeof(new_fmt), "%s (end_ctx=%"PRIu32")", fmt, ctx->seq);
va_list args;
va_start(args, fmt);
wlr_vtrace(new_fmt, args);
va_end(args);
ctx->seq = 0;
}