• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 uint32_t
clockid_to_perfetto_clock(UNUSED perfetto_clock_id clock)45 clockid_to_perfetto_clock(UNUSED perfetto_clock_id clock)
46 {
47 #ifndef _WIN32
48    switch (clock) {
49       case CLOCK_REALTIME:         return perfetto::protos::pbzero::BUILTIN_CLOCK_REALTIME;
50       case CLOCK_REALTIME_COARSE:  return perfetto::protos::pbzero::BUILTIN_CLOCK_REALTIME_COARSE;
51       case CLOCK_MONOTONIC:        return perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC;
52       case CLOCK_MONOTONIC_COARSE: return perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC_COARSE;
53       case CLOCK_MONOTONIC_RAW:    return perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC_RAW;
54       case CLOCK_BOOTTIME:         return perfetto::protos::pbzero::BUILTIN_CLOCK_BOOTTIME;
55    }
56    return perfetto::protos::pbzero::BUILTIN_CLOCK_UNKNOWN;
57 #else
58    return perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC; // perfetto always uses QueryPerformanceCounter & marks this as CLOCK_MONOTONIC on Windows
59 #endif
60 }
61 
62 static void
util_perfetto_update_tracing_state(void)63 util_perfetto_update_tracing_state(void)
64 {
65    p_atomic_set(&util_perfetto_tracing_state,
66                 TRACE_EVENT_CATEGORY_ENABLED(UTIL_PERFETTO_CATEGORY_DEFAULT_STR));
67 }
68 
69 void
util_perfetto_trace_begin(const char * name)70 util_perfetto_trace_begin(const char *name)
71 {
72    TRACE_EVENT_BEGIN(
73       UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr,
74       [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); });
75 }
76 
77 void
util_perfetto_trace_end(void)78 util_perfetto_trace_end(void)
79 {
80    TRACE_EVENT_END(UTIL_PERFETTO_CATEGORY_DEFAULT_STR);
81 
82    util_perfetto_update_tracing_state();
83 }
84 
85 void
util_perfetto_trace_begin_flow(const char * fname,uint64_t id)86 util_perfetto_trace_begin_flow(const char *fname, uint64_t id)
87 {
88    TRACE_EVENT_BEGIN(
89       UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, perfetto::Flow::ProcessScoped(id),
90       [&](perfetto::EventContext ctx) { ctx.event()->set_name(fname); });
91 }
92 
93 void
util_perfetto_trace_full_begin(const char * fname,uint64_t track_id,uint64_t id,perfetto_clock_id clock,uint64_t timestamp)94 util_perfetto_trace_full_begin(const char *fname, uint64_t track_id, uint64_t id, perfetto_clock_id clock, uint64_t timestamp)
95 {
96    TRACE_EVENT_BEGIN(
97       UTIL_PERFETTO_CATEGORY_DEFAULT_STR, nullptr, perfetto::Track(track_id),
98       perfetto::TraceTimestamp{clockid_to_perfetto_clock(clock), timestamp},
99       perfetto::Flow::ProcessScoped(id),
100       [&](perfetto::EventContext ctx) { ctx.event()->set_name(fname); });
101 }
102 
103 uint64_t
util_perfetto_new_track(const char * name)104 util_perfetto_new_track(const char *name)
105 {
106    uint64_t track_id = util_perfetto_next_id();
107    auto track = perfetto::Track(track_id);
108    auto desc = track.Serialize();
109    desc.set_name(name);
110    perfetto::TrackEvent::SetTrackDescriptor(track, desc);
111    return track_id;
112 }
113 
114 void
util_perfetto_trace_full_end(const char * name,uint64_t track_id,perfetto_clock_id clock,uint64_t timestamp)115 util_perfetto_trace_full_end(const char *name, uint64_t track_id, perfetto_clock_id clock, uint64_t timestamp)
116 {
117    TRACE_EVENT_END(
118       UTIL_PERFETTO_CATEGORY_DEFAULT_STR,
119       perfetto::Track(track_id),
120       perfetto::TraceTimestamp{clockid_to_perfetto_clock(clock), timestamp});
121 
122    util_perfetto_update_tracing_state();
123 }
124 
125 void
util_perfetto_counter_set(const char * name,double value)126 util_perfetto_counter_set(const char *name, double value)
127 {
128    TRACE_COUNTER(UTIL_PERFETTO_CATEGORY_DEFAULT_STR,
129                  perfetto::DynamicString(name), value);
130 }
131 
132 uint64_t
util_perfetto_next_id(void)133 util_perfetto_next_id(void)
134 {
135    return p_atomic_inc_return(&util_perfetto_unique_id);
136 }
137 
138 class UtilPerfettoObserver : public perfetto::TrackEventSessionObserver {
139  public:
UtilPerfettoObserver()140    UtilPerfettoObserver() { perfetto::TrackEvent::AddSessionObserver(this); }
141 
OnStart(const perfetto::DataSourceBase::StartArgs &)142    void OnStart(const perfetto::DataSourceBase::StartArgs &) override
143    {
144       util_perfetto_update_tracing_state();
145    }
146 
147    /* XXX There is no PostStop callback.  We have to call
148     * util_perfetto_update_tracing_state occasionally to poll.
149     */
150 };
151 
152 static void
util_perfetto_fini(void)153 util_perfetto_fini(void)
154 {
155    perfetto::Tracing::Shutdown();
156 }
157 
158 static void
util_perfetto_init_once(void)159 util_perfetto_init_once(void)
160 {
161    // Connects to the system tracing service
162    perfetto::TracingInitArgs args;
163    args.backends = perfetto::kSystemBackend;
164    perfetto::Tracing::Initialize(args);
165 
166    static UtilPerfettoObserver observer;
167    perfetto::TrackEvent::Register();
168 
169    atexit(&util_perfetto_fini);
170 }
171 
172 static once_flag perfetto_once_flag = ONCE_FLAG_INIT;
173 
174 void
util_perfetto_init(void)175 util_perfetto_init(void)
176 {
177    call_once(&perfetto_once_flag, util_perfetto_init_once);
178 }
179