• 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 #define UTIL_PERFETTO_CATEGORY_SLOW_STR "mesa.slow"
34 
35 PERFETTO_DEFINE_CATEGORIES(
36    perfetto::Category(UTIL_PERFETTO_CATEGORY_DEFAULT_STR)
37       .SetDescription("Mesa default events"),
38    perfetto::Category(UTIL_PERFETTO_CATEGORY_SLOW_STR)
39       .SetDescription("Mesa slow events")
40       .SetTags("slow"));
41 
42 PERFETTO_TRACK_EVENT_STATIC_STORAGE();
43 
44 int util_perfetto_category_states[UTIL_PERFETTO_CATEGORY_COUNT];
45 
46 static void
util_perfetto_update_category_states(void)47 util_perfetto_update_category_states(void)
48 {
49 #define UPDATE_CATEGORY(cat)                                                 \
50    p_atomic_set(                                                             \
51       &util_perfetto_category_states[UTIL_PERFETTO_CATEGORY_##cat],          \
52       TRACE_EVENT_CATEGORY_ENABLED(UTIL_PERFETTO_CATEGORY_##cat##_STR))
53    UPDATE_CATEGORY(DEFAULT);
54    UPDATE_CATEGORY(SLOW);
55 #undef UPDATE_CATEGORY
56 }
57 
58 void
util_perfetto_trace_begin(enum util_perfetto_category category,const char * name)59 util_perfetto_trace_begin(enum util_perfetto_category category,
60                           const char *name)
61 {
62 #define TRACE_BEGIN(cat, name)                                               \
63    TRACE_EVENT_BEGIN(                                                        \
64       UTIL_PERFETTO_CATEGORY_##cat##_STR, nullptr,                           \
65       [&](perfetto::EventContext ctx) { ctx.event()->set_name(name); })
66    switch (category) {
67    case UTIL_PERFETTO_CATEGORY_DEFAULT:
68       TRACE_BEGIN(DEFAULT, name);
69       break;
70    case UTIL_PERFETTO_CATEGORY_SLOW:
71       TRACE_BEGIN(SLOW, name);
72       break;
73    default:
74       unreachable("bad perfetto category");
75    }
76 #undef TRACE_BEGIN
77 }
78 
79 void
util_perfetto_trace_end(enum util_perfetto_category category)80 util_perfetto_trace_end(enum util_perfetto_category category)
81 {
82 #define TRACE_END(cat) TRACE_EVENT_END(UTIL_PERFETTO_CATEGORY_##cat##_STR)
83    switch (category) {
84    case UTIL_PERFETTO_CATEGORY_DEFAULT:
85       TRACE_END(DEFAULT);
86       break;
87    case UTIL_PERFETTO_CATEGORY_SLOW:
88       TRACE_END(SLOW);
89       break;
90    default:
91       unreachable("bad perfetto category");
92    }
93 #undef TRACE_END
94 
95    util_perfetto_update_category_states();
96 }
97 
98 class UtilPerfettoObserver : public perfetto::TrackEventSessionObserver {
99  public:
UtilPerfettoObserver()100    UtilPerfettoObserver() { perfetto::TrackEvent::AddSessionObserver(this); }
101 
OnStart(const perfetto::DataSourceBase::StartArgs &)102    void OnStart(const perfetto::DataSourceBase::StartArgs &) override
103    {
104       util_perfetto_update_category_states();
105    }
106 
107    /* XXX There is no PostStop callback.  We have to call
108     * util_perfetto_update_category_states occasionally to poll.
109     */
110 };
111 
112 static void
util_perfetto_fini(void)113 util_perfetto_fini(void)
114 {
115    perfetto::Tracing::Shutdown();
116 }
117 
118 static void
util_perfetto_init_once(void)119 util_perfetto_init_once(void)
120 {
121    // Connects to the system tracing service
122    perfetto::TracingInitArgs args;
123    args.backends = perfetto::kSystemBackend;
124    perfetto::Tracing::Initialize(args);
125 
126    static UtilPerfettoObserver observer;
127    perfetto::TrackEvent::Register();
128 
129    atexit(&util_perfetto_fini);
130 }
131 
132 static once_flag perfetto_once_flag = ONCE_FLAG_INIT;
133 
134 void
util_perfetto_init(void)135 util_perfetto_init(void)
136 {
137    call_once(&perfetto_once_flag, util_perfetto_init_once);
138 }
139