From e9596149cfcf13816e047c5d49122febb01f1397 Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Fri, 5 Apr 2024 10:44:24 -0500 Subject: [PATCH] perfetto: Add some functions for timestamped events This can be useful if we know when an event happened, but our code isn't running at that time (such as reporting when an image was presented in the wayland wsi). We can't really mix these with events that we log at the current time, because there could be overlap, so also add a function for creating custom tracks. Signed-off-by: Derek Foreman Part-of: --- src/util/perf/cpu_trace.h | 21 ++++++++++++++++++++- src/util/perf/u_perfetto.cc | 28 ++++++++++++++++++++++++++++ src/util/perf/u_perfetto.h | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/util/perf/cpu_trace.h b/src/util/perf/cpu_trace.h index 6664f559290..b72218d55a5 100644 --- a/src/util/perf/cpu_trace.h +++ b/src/util/perf/cpu_trace.h @@ -41,6 +41,18 @@ util_perfetto_counter_set(name, value); \ } while (0) +#define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, timestamp) \ + do { \ + if (unlikely(util_perfetto_is_tracing_enabled())) \ + util_perfetto_trace_full_begin(name, track_id, flow_id, timestamp); \ + } while (0) + +#define _MESA_TRACE_TIMESTAMP_END(name, track_id, timestamp) \ + do { \ + if (unlikely(util_perfetto_is_tracing_enabled())) \ + util_perfetto_trace_full_end(name, track_id, timestamp); \ + } while (0) + /* NOTE: for now disable atrace for C++ to workaround a ndk bug with ordering * between stdatomic.h and atomic.h. See: * @@ -56,13 +68,16 @@ #define _MESA_TRACE_FLOW_BEGIN(name, id) \ atrace_begin(ATRACE_TAG_GRAPHICS, name) #define _MESA_TRACE_SET_COUNTER(name, value) - +#define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, timestamp) +#define _MESA_TRACE_TIMESTAMP_END(name, track_id, timestamp) #else #define _MESA_TRACE_BEGIN(name) #define _MESA_TRACE_END() #define _MESA_TRACE_FLOW_BEGIN(name, id) #define _MESA_TRACE_SET_COUNTER(name, value) +#define _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, timestamp) +#define _MESA_TRACE_TIMESTAMP_END(name, track_id, timestamp) #endif /* HAVE_PERFETTO */ @@ -137,6 +152,10 @@ _mesa_trace_scope_end(UNUSED int *scope) #define MESA_TRACE_FUNC() _MESA_TRACE_SCOPE(__func__) #define MESA_TRACE_FUNC_FLOW(id) _MESA_TRACE_SCOPE_FLOW(__func__, id) #define MESA_TRACE_SET_COUNTER(name, value) _MESA_TRACE_SET_COUNTER(name, value) +#define MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, timestamp) \ + _MESA_TRACE_TIMESTAMP_BEGIN(name, track_id, flow_id, timestamp) +#define MESA_TRACE_TIMESTAMP_END(name, track_id, timestamp) \ + _MESA_TRACE_TIMESTAMP_END(name, track_id, timestamp) static inline void util_cpu_trace_init() diff --git a/src/util/perf/u_perfetto.cc b/src/util/perf/u_perfetto.cc index a0084a7b247..c52d2382c3b 100644 --- a/src/util/perf/u_perfetto.cc +++ b/src/util/perf/u_perfetto.cc @@ -72,6 +72,34 @@ util_perfetto_trace_begin_flow(const char *fname, uint64_t id) [&](perfetto::EventContext ctx) { ctx.event()->set_name(fname); }); } +void +util_perfetto_trace_full_begin(const char *fname, uint64_t track_id, uint64_t id, uint64_t timestamp) +{ + TRACE_EVENT_BEGIN( + UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, perfetto::Track(track_id), + timestamp, perfetto::Flow::ProcessScoped(id), + [&](perfetto::EventContext ctx) { ctx.event()->set_name(fname); }); +} + +uint64_t +util_perfetto_new_track(const char *name) +{ + uint64_t track_id = util_perfetto_next_id(); + auto track = perfetto::Track(track_id); + auto desc = track.Serialize(); + desc.set_name(name); + perfetto::TrackEvent::SetTrackDescriptor(track, desc); + return track_id; +} + +void +util_perfetto_trace_full_end(const char *name, uint64_t track_id, uint64_t timestamp) +{ + TRACE_EVENT_END(UTIL_PERFETTO_CATEGORY_DEFAULT_STR, perfetto::Track(track_id), timestamp); + + util_perfetto_update_tracing_state(); +} + void util_perfetto_counter_set(const char *name, double value) { diff --git a/src/util/perf/u_perfetto.h b/src/util/perf/u_perfetto.h index 79ce330a5d5..25db962c864 100644 --- a/src/util/perf/u_perfetto.h +++ b/src/util/perf/u_perfetto.h @@ -50,8 +50,14 @@ void util_perfetto_trace_begin_flow(const char *fname, uint64_t id); void util_perfetto_counter_set(const char *name, double value); +void util_perfetto_trace_full_begin(const char *name, uint64_t track_id, uint64_t id, uint64_t timestamp); + +void util_perfetto_trace_full_end(const char *name, uint64_t track_id, uint64_t timestamp); + uint64_t util_perfetto_next_id(void); +uint64_t util_perfetto_new_track(const char *name); + #else /* HAVE_PERFETTO */ static inline void @@ -79,6 +85,16 @@ static inline void util_perfetto_trace_begin_flow(const char *fname, uint64_t id { } +static inline void +util_perfetto_trace_full_begin(const char *name, uint64_t track_id, uint64_t id, uint64_t timestamp) +{ +} + +static inline void +util_perfetto_trace_full_end(const char *name, uint64_t track_id) +{ +} + static inline void util_perfetto_counter_set(const char *name, double value) { } @@ -88,6 +104,11 @@ static inline uint64_t util_perfetto_next_id(void) return 0; } +static inline uint64_t util_perfetto_new_track(const char *name) +{ + return 0; +} + #endif /* HAVE_PERFETTO */ #ifdef __cplusplus