xref: /aosp_15_r20/external/mesa3d/src/util/perf/u_perfetto.cc (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Google, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "u_perfetto.h"
25 
26 #include <perfetto.h>
27 
28 #include "c11/threads.h"
29 #include "util/macros.h"
30 
31 /* perfetto requires string literals */
32 #define UTIL_PERFETTO_CATEGORY_DEFAULT_STR "mesa.default"
33 
34 PERFETTO_DEFINE_CATEGORIES(
35    perfetto::Category(UTIL_PERFETTO_CATEGORY_DEFAULT_STR)
36       .SetDescription("Mesa default events"));
37 
38 PERFETTO_TRACK_EVENT_STATIC_STORAGE();
39 
40 int util_perfetto_tracing_state;
41 
42 static uint64_t util_perfetto_unique_id = 1;
43 
44 static void
util_perfetto_update_tracing_state(void)45 util_perfetto_update_tracing_state(void)
46 {
47    p_atomic_set(&util_perfetto_tracing_state,
48                 TRACE_EVENT_CATEGORY_ENABLED(UTIL_PERFETTO_CATEGORY_DEFAULT_STR));
49 }
50 
51 void
util_perfetto_trace_begin(const char * name)52 util_perfetto_trace_begin(const char *name)
53 {
54    TRACE_EVENT_BEGIN(
55       UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr,
56       [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); });
57 }
58 
59 void
util_perfetto_trace_end(void)60 util_perfetto_trace_end(void)
61 {
62    TRACE_EVENT_END(UTIL_PERFETTO_CATEGORY_DEFAULT_STR);
63 
64    util_perfetto_update_tracing_state();
65 }
66 
67 void
util_perfetto_trace_begin_flow(const char * fname,uint64_t id)68 util_perfetto_trace_begin_flow(const char *fname, uint64_t id)
69 {
70    TRACE_EVENT_BEGIN(
71       UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, perfetto::Flow::ProcessScoped(id),
72       [&](perfetto::EventContext ctx) { ctx.event()->set_name(fname); });
73 }
74 
75 void
util_perfetto_trace_full_begin(const char * fname,uint64_t track_id,uint64_t id,uint64_t timestamp)76 util_perfetto_trace_full_begin(const char *fname, uint64_t track_id, uint64_t id, uint64_t timestamp)
77 {
78    TRACE_EVENT_BEGIN(
79       UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, perfetto::Track(track_id),
80       timestamp, perfetto::Flow::ProcessScoped(id),
81       [&](perfetto::EventContext ctx) { ctx.event()->set_name(fname); });
82 }
83 
84 uint64_t
util_perfetto_new_track(const char * name)85 util_perfetto_new_track(const char *name)
86 {
87    uint64_t track_id = util_perfetto_next_id();
88    auto track = perfetto::Track(track_id);
89    auto desc = track.Serialize();
90    desc.set_name(name);
91    perfetto::TrackEvent::SetTrackDescriptor(track, desc);
92    return track_id;
93 }
94 
95 void
util_perfetto_trace_full_end(const char * name,uint64_t track_id,uint64_t timestamp)96 util_perfetto_trace_full_end(const char *name, uint64_t track_id, uint64_t timestamp)
97 {
98    TRACE_EVENT_END(UTIL_PERFETTO_CATEGORY_DEFAULT_STR, perfetto::Track(track_id), timestamp);
99 
100    util_perfetto_update_tracing_state();
101 }
102 
103 void
util_perfetto_counter_set(const char * name,double value)104 util_perfetto_counter_set(const char *name, double value)
105 {
106    TRACE_COUNTER(UTIL_PERFETTO_CATEGORY_DEFAULT_STR,
107                  perfetto::DynamicString(name), value);
108 }
109 
110 uint64_t
util_perfetto_next_id(void)111 util_perfetto_next_id(void)
112 {
113    return p_atomic_inc_return(&util_perfetto_unique_id);
114 }
115 
116 class UtilPerfettoObserver : public perfetto::TrackEventSessionObserver {
117  public:
UtilPerfettoObserver()118    UtilPerfettoObserver() { perfetto::TrackEvent::AddSessionObserver(this); }
119 
OnStart(const perfetto::DataSourceBase::StartArgs &)120    void OnStart(const perfetto::DataSourceBase::StartArgs &) override
121    {
122       util_perfetto_update_tracing_state();
123    }
124 
125    /* XXX There is no PostStop callback.  We have to call
126     * util_perfetto_update_tracing_state occasionally to poll.
127     */
128 };
129 
130 static void
util_perfetto_fini(void)131 util_perfetto_fini(void)
132 {
133    perfetto::Tracing::Shutdown();
134 }
135 
136 static void
util_perfetto_init_once(void)137 util_perfetto_init_once(void)
138 {
139    // Connects to the system tracing service
140    perfetto::TracingInitArgs args;
141    args.backends = perfetto::kSystemBackend;
142    perfetto::Tracing::Initialize(args);
143 
144    static UtilPerfettoObserver observer;
145    perfetto::TrackEvent::Register();
146 
147    atexit(&util_perfetto_fini);
148 }
149 
150 static once_flag perfetto_once_flag = ONCE_FLAG_INIT;
151 
152 void
util_perfetto_init(void)153 util_perfetto_init(void)
154 {
155    call_once(&perfetto_once_flag, util_perfetto_init_once);
156 }
157