From fdb60a480c40532144e3d90b9c830709d85fdd4a Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Mon, 11 May 2026 13:14:43 -0500 Subject: [PATCH] trace: Put the annotation count inside a struct This will make it easy to pass the count and annotations at the same time later when we have complex groupings of annotations. While we're doing this, make the count an unsigned char instead of an int, because we'll never have that many. Signed-off-by: Derek Foreman --- libweston/perfetto/u_perfetto.cc | 37 +++++++------- libweston/perfetto/u_perfetto.h | 20 ++++---- libweston/weston-trace.h | 83 +++++++++++++++----------------- 3 files changed, 69 insertions(+), 71 deletions(-) diff --git a/libweston/perfetto/u_perfetto.cc b/libweston/perfetto/u_perfetto.cc index b7a96fd2a..7879ffbb3 100644 --- a/libweston/perfetto/u_perfetto.cc +++ b/libweston/perfetto/u_perfetto.cc @@ -153,22 +153,25 @@ util_perfetto_next_id(void) static void util_perfetto_flush_debug_annotation(perfetto::EventContext *ctx, - unsigned int nr_entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { - if (nr_entries == 0) + struct weston_debug_annotation *annot; + + if (annots->count == 0) return; - for (unsigned int idx = 0; idx < nr_entries; idx++) { - switch (annots[idx].type) { + for (unsigned char idx = 0; idx < annots->count; idx++) { + annot = &annots->annots[idx]; + + switch (annot->type) { case WESTON_DEBUG_ANNOTATION_INT_VAL: - ctx->AddDebugAnnotation(annots[idx].key, annots[idx].ivalue); + ctx->AddDebugAnnotation(annot->key, annot->ivalue); break; case WESTON_DEBUG_ANNOTATION_FLOAT_VAL: - ctx->AddDebugAnnotation(annots[idx].key, annots[idx].fvalue); + ctx->AddDebugAnnotation(annot->key, annot->fvalue); break; case WESTON_DEBUG_ANNOTATION_STR_VAL: - ctx->AddDebugAnnotation(annots[idx].key, annots[idx].svalue); + ctx->AddDebugAnnotation(annot->key, annot->svalue); break; default: break; @@ -179,8 +182,7 @@ util_perfetto_flush_debug_annotation(perfetto::EventContext *ctx, void util_perfetto_trace_commit_debug_annots(uint64_t id, const char *name, - unsigned int nr_entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { if (id) { TRACE_EVENT_INSTANT(UTIL_PERFETTO_CATEGORY_DEFAULT_STR, @@ -188,7 +190,7 @@ util_perfetto_trace_commit_debug_annots(uint64_t id, const char *name, perfetto::Flow::ProcessScoped(id), [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); - util_perfetto_flush_debug_annotation(&ctx, nr_entries, annots); + util_perfetto_flush_debug_annotation(&ctx, annots); }); return; } @@ -197,33 +199,32 @@ util_perfetto_trace_commit_debug_annots(uint64_t id, const char *name, nullptr, [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); - util_perfetto_flush_debug_annotation(&ctx, nr_entries, annots); + util_perfetto_flush_debug_annotation(&ctx, annots); }); } void -util_perfetto_trace_commit_annotate_func(const char *name, unsigned int nr_entries, - struct weston_debug_annotation *annots) +util_perfetto_trace_commit_annotate_func(const char *name, + struct weston_debug_annotations *annots) { TRACE_EVENT_BEGIN(UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); - util_perfetto_flush_debug_annotation(&ctx, nr_entries, annots); + util_perfetto_flush_debug_annotation(&ctx, annots); }); } void util_perfetto_trace_commit_annotate_func_flow(uint64_t id, const char *name, - unsigned int nr_entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { TRACE_EVENT_BEGIN(UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, perfetto::Flow::ProcessScoped(id), [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); - util_perfetto_flush_debug_annotation(&ctx, nr_entries, annots); + util_perfetto_flush_debug_annotation(&ctx, annots); }); } diff --git a/libweston/perfetto/u_perfetto.h b/libweston/perfetto/u_perfetto.h index 4c84b7250..8341e3f91 100644 --- a/libweston/perfetto/u_perfetto.h +++ b/libweston/perfetto/u_perfetto.h @@ -55,6 +55,11 @@ struct weston_debug_annotation { }; }; +struct weston_debug_annotations { + struct weston_debug_annotation *annots; + unsigned char count; +}; + #ifdef HAVE_PERFETTO extern int util_perfetto_tracing_state; @@ -80,13 +85,13 @@ void util_perfetto_trace_full_begin(const char *name, uint64_t track_id, uint64_ void util_perfetto_trace_full_end(const char *name, uint64_t track_id, clockid_t clock, uint64_t timestamp); void util_perfetto_trace_commit_debug_annots(uint64_t id, const char *name, - unsigned int entries, struct weston_debug_annotation *annots); + struct weston_debug_annotations *annots); void util_perfetto_trace_commit_annotate_func(const char *name, - unsigned int entries, struct weston_debug_annotation *annots); + struct weston_debug_annotations *annots); void util_perfetto_trace_commit_annotate_func_flow(uint64_t id, const char *name, - unsigned int entries, struct weston_debug_annotation *annots); + struct weston_debug_annotations *annots); uint64_t util_perfetto_next_id(void); @@ -131,22 +136,19 @@ util_perfetto_trace_full_end(const char *name, uint64_t track_id, clockid_t cloc static inline void util_perfetto_trace_commit_debug_annots(uint64_t id, const char *name, - unsigned int entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { } static inline void util_perfetto_trace_commit_annotate_func(const char *name, - unsigned int entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { } static inline void util_perfetto_trace_commit_annotate_func_flow(uint64_t id, const char *name, - unsigned int entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { } diff --git a/libweston/weston-trace.h b/libweston/weston-trace.h index b2ba55492..bc07941d2 100644 --- a/libweston/weston-trace.h +++ b/libweston/weston-trace.h @@ -73,50 +73,53 @@ clock, timestamp); \ } while (0) -#define _WESTON_TRACE_BEGIN_ANNOTATION() \ - struct weston_debug_annotation __pd_annots[WESTON_MAX_DEBUG_ANNOTS]; \ - unsigned int __pd_i = 0 +#define _WESTON_TRACE_BEGIN_ANNOTATION() \ + struct weston_debug_annotation __pd_annot[WESTON_MAX_DEBUG_ANNOTS]; \ + struct weston_debug_annotations __pd_annots = { \ + .annots = __pd_annot, \ + .count = 0, \ + } #define _WESTON_TRACE_ANNOTATE_ADD_INT(k, v) \ - weston_assert_u32_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_i); \ - __pd_annots[__pd_i].type = WESTON_DEBUG_ANNOTATION_INT_VAL; \ - __pd_annots[__pd_i].ivalue = v; \ - __pd_annots[__pd_i].key = k; \ - __pd_i++ + weston_assert_u8_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_annots.count); \ + __pd_annots.annots[__pd_annots.count].type = WESTON_DEBUG_ANNOTATION_INT_VAL; \ + __pd_annots.annots[__pd_annots.count].ivalue = v; \ + __pd_annots.annots[__pd_annots.count].key = k; \ + __pd_annots.count++ #define _WESTON_TRACE_ANNOTATE_ADD_FLOAT(k, v) \ - weston_assert_u32_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_i); \ - __pd_annots[__pd_i].type = WESTON_DEBUG_ANNOTATION_FLOAT_VAL; \ - __pd_annots[__pd_i].fvalue = v; \ - __pd_annots[__pd_i].key = k; \ - __pd_i++ + weston_assert_u8_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_annots.count); \ + __pd_annots.annots[__pd_annots.count].type = WESTON_DEBUG_ANNOTATION_FLOAT_VAL; \ + __pd_annots.annots[__pd_annots.count].fvalue = v; \ + __pd_annots.annots[__pd_annots.count].key = k; \ + __pd_annots.count++ #define _WESTON_TRACE_ANNOTATE_ADD_STR(k, v) \ - weston_assert_u32_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_i); \ - __pd_annots[__pd_i].type = WESTON_DEBUG_ANNOTATION_STR_VAL; \ - __pd_annots[__pd_i].svalue = v; \ - __pd_annots[__pd_i].key = k; \ - __pd_i++ + weston_assert_u8_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_annots.count); \ + __pd_annots.annots[__pd_annots.count].type = WESTON_DEBUG_ANNOTATION_STR_VAL; \ + __pd_annots.annots[__pd_annots.count].svalue = v; \ + __pd_annots.annots[__pd_annots.count].key = k; \ + __pd_annots.count++ #define _WESTON_TRACE_COMMIT_ANNOTATION(id, name) \ do { \ if (unlikely(util_perfetto_is_tracing_enabled())) { \ - _weston_trace_scope_annotate_commit(id, name, &__pd_i, __pd_annots); \ + _weston_trace_scope_annotate_commit(id, name, &__pd_annots); \ } \ } while (0) /* annotated funcs */ -#define _WESTON_TRACE_ANNOTATE_FUNC_BEGIN(name, nr_entries, annots) \ +#define _WESTON_TRACE_ANNOTATE_FUNC_BEGIN(name, annots) \ do { \ if (unlikely(util_perfetto_is_tracing_enabled())) { \ - util_perfetto_trace_commit_annotate_func(name, *nr_entries, annots); \ + util_perfetto_trace_commit_annotate_func(name, annots); \ } \ } while (0) -#define _WESTON_TRACE_ANNOTATE_FUNC_BEGIN_FLOW(name, id, nr_entries, annots) \ +#define _WESTON_TRACE_ANNOTATE_FUNC_BEGIN_FLOW(name, id, annots) \ do { \ if (unlikely(util_perfetto_is_tracing_enabled())) { \ - util_perfetto_trace_commit_annotate_func_flow(id, name, *nr_entries, annots); \ + util_perfetto_trace_commit_annotate_func_flow(id, name, annots); \ } \ } while (0) @@ -146,12 +149,12 @@ #define _WESTON_TRACE_ANNOTATE_FUNC(name) \ int _WESTON_TRACE_SCOPE_VAR(__LINE__) \ __attribute__((cleanup(_weston_trace_scope_end), unused)) = \ - _weston_trace_annotate_func_begin(name, &__pd_i, __pd_annots) + _weston_trace_annotate_func_begin(name, &__pd_annots) #define _WESTON_TRACE_ANNOTATE_FUNC_FLOW(id, name) \ int _WESTON_TRACE_SCOPE_VAR(__LINE__) \ __attribute__((cleanup(_weston_trace_scope_end), unused)) = \ - _weston_trace_annotate_func_begin_flow(name, id, &__pd_i, __pd_annots) + _weston_trace_annotate_func_begin_flow(name, id, &__pd_annots) static inline int _weston_trace_scope_begin(const char *name) @@ -171,48 +174,40 @@ _weston_trace_scope_flow_begin(const char *name, uint64_t *id) static inline void _weston_trace_scope_annotate_commit(uint64_t *id, const char *name, - unsigned int *nr_entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { if (id && *id == 0) { *id = util_perfetto_next_id(); - util_perfetto_trace_commit_debug_annots(*id, name, *nr_entries, annots); + util_perfetto_trace_commit_debug_annots(*id, name, annots); goto reset_entries; } - util_perfetto_trace_commit_debug_annots(0, name, *nr_entries, annots); + util_perfetto_trace_commit_debug_annots(0, name, annots); - /* reset the array and counter */ reset_entries: - memset(annots, 0, sizeof(struct weston_debug_annotation) * *nr_entries); - *nr_entries = 0; + annots->count = 0; } static inline int -_weston_trace_annotate_func_begin(const char *name, unsigned int *nr_entries, - struct weston_debug_annotation *annots) +_weston_trace_annotate_func_begin(const char *name, + struct weston_debug_annotations *annots) { - _WESTON_TRACE_ANNOTATE_FUNC_BEGIN(name, nr_entries, annots); + _WESTON_TRACE_ANNOTATE_FUNC_BEGIN(name, annots); - /* reset the array and counter */ - memset(annots, 0, sizeof(struct weston_debug_annotation) * *nr_entries); - *nr_entries = 0; + annots->count = 0; return 0; } static inline int _weston_trace_annotate_func_begin_flow(const char *name, uint64_t *id, - unsigned int *nr_entries, - struct weston_debug_annotation *annots) + struct weston_debug_annotations *annots) { if (*id == 0) *id = util_perfetto_next_id(); - _WESTON_TRACE_ANNOTATE_FUNC_BEGIN_FLOW(name, *id, nr_entries, annots); + _WESTON_TRACE_ANNOTATE_FUNC_BEGIN_FLOW(name, *id, annots); - /* reset the array and counter */ - memset(annots, 0, sizeof(struct weston_debug_annotation) * *nr_entries); - *nr_entries = 0; + annots->count = 0; return 0; }