1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/tracing/test/tracing_module.h"
18
19 #include "protos/perfetto/trace/track_event/log_message.pbzero.h"
20 #include "src/tracing/test/tracing_module_categories.h"
21
22 #include <stdio.h>
23
24 // This file is for checking that multiple sets of trace event categories can be
25 // combined into the same program.
26
27 PERFETTO_TRACK_EVENT_STATIC_STORAGE_IN_NAMESPACE(tracing_module);
28 PERFETTO_TRACK_EVENT_STATIC_STORAGE_IN_NAMESPACE_WITH_ATTRS(tracing_extra,
29 [[maybe_unused]]);
30
31 namespace tracing_extra {
32 namespace {
33
EmitEventFromExtraNamespace()34 void EmitEventFromExtraNamespace() {
35 TRACE_EVENT_BEGIN("extra", "ExtraNamespaceFromModule");
36 TRACE_EVENT_BEGIN("extra2", "ExtraNamespaceFromModuleNotEnabled");
37 }
38
39 } // namespace
40 } // namespace tracing_extra
41
42 namespace tracing_module {
43
44 // The following two functions test selecting the category set on a
45 // per-namespace level.
46 namespace test_ns1 {
47 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE(tracing_extra);
48
49 void EmitEvent();
EmitEvent()50 void EmitEvent() {
51 TRACE_EVENT_BEGIN("extra", "DefaultNamespace");
52 }
53
54 } // namespace test_ns1
55
56 namespace test_ns2 {
57 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE(tracing_module);
58
59 void EmitEvent();
EmitEvent()60 void EmitEvent() {
61 TRACE_EVENT_BEGIN("cat1", "DefaultNamespace");
62 }
63
64 } // namespace test_ns2
65
InitializeCategories()66 void InitializeCategories() {
67 TrackEvent::Register();
68 tracing_extra::TrackEvent::Register();
69 }
70
AddSessionObserver(perfetto::TrackEventSessionObserver * observer)71 void AddSessionObserver(perfetto::TrackEventSessionObserver* observer) {
72 TrackEvent::AddSessionObserver(observer);
73 }
74
RemoveSessionObserver(perfetto::TrackEventSessionObserver * observer)75 void RemoveSessionObserver(perfetto::TrackEventSessionObserver* observer) {
76 TrackEvent::RemoveSessionObserver(observer);
77 }
78
IsEnabled()79 bool IsEnabled() {
80 return TrackEvent::IsEnabled();
81 }
82
EmitTrackEvents()83 void EmitTrackEvents() {
84 TRACE_EVENT_BEGIN("cat1", "DisabledEventFromModule");
85 TRACE_EVENT_END("cat1");
86 TRACE_EVENT_BEGIN("cat4", "DisabledEventFromModule");
87 TRACE_EVENT_END("cat4");
88 TRACE_EVENT_BEGIN("cat9", "DisabledEventFromModule");
89 TRACE_EVENT_END("cat9");
90 TRACE_EVENT_BEGIN("foo", "FooEventFromModule");
91 TRACE_EVENT_END("foo");
92 }
93
EmitTrackEventsFromAllNamespaces()94 void EmitTrackEventsFromAllNamespaces() {
95 // Since we're in the `tracing_module` namespace, that registry is used by
96 // default.
97 TRACE_EVENT_BEGIN("cat1", "DefaultNamespaceFromModule");
98
99 // Emit an event from the other namespace.
100 tracing_extra::EmitEventFromExtraNamespace();
101
102 // Make the other namespace the default.
103 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE_SCOPED(tracing_extra);
104 TRACE_EVENT_BEGIN("extra", "OverrideNamespaceFromModule");
105
106 test_ns1::EmitEvent();
107 test_ns2::EmitEvent();
108 }
109
GetIncrementalState()110 perfetto::internal::TrackEventIncrementalState* GetIncrementalState() {
111 perfetto::internal::TrackEventIncrementalState* state = nullptr;
112 TrackEvent::Trace([&state](TrackEvent::TraceContext ctx) {
113 state = ctx.GetIncrementalState();
114 });
115 return state;
116 }
117
FunctionWithOneTrackEvent()118 void FunctionWithOneTrackEvent() {
119 TRACE_EVENT_BEGIN("cat1", "DisabledEventFromModule");
120 // Simulates the non-tracing work of this function, which should take priority
121 // over the above trace event in terms of instruction scheduling.
122 puts("Hello");
123 }
124
FunctionWithOneTrackEventWithTypedArgument()125 void FunctionWithOneTrackEventWithTypedArgument() {
126 TRACE_EVENT_BEGIN("cat1", "EventWithArg", [](perfetto::EventContext ctx) {
127 auto log = ctx.event()->set_log_message();
128 log->set_body_iid(0x42);
129 });
130 // Simulates the non-tracing work of this function, which should take priority
131 // over the above trace event in terms of instruction scheduling.
132 puts("Hello");
133 }
134
FunctionWithOneScopedTrackEvent()135 void FunctionWithOneScopedTrackEvent() {
136 TRACE_EVENT("cat1", "ScopedEventFromModule");
137 // Simulates the non-tracing work of this function, which should take priority
138 // over the above trace event in terms of instruction scheduling.
139 puts("Hello");
140 }
141
FunctionWithOneTrackEventWithDebugAnnotations()142 void FunctionWithOneTrackEventWithDebugAnnotations() {
143 TRACE_EVENT_BEGIN("cat1", "EventWithAnnotations", "p1", 42, "p2", .5f);
144 // Simulates the non-tracing work of this function, which should take priority
145 // over the above trace event in terms of instruction scheduling.
146 puts("Hello");
147 }
148
FunctionWithOneTrackEventWithCustomTrack()149 void FunctionWithOneTrackEventWithCustomTrack() {
150 TRACE_EVENT_BEGIN("cat1", "EventWithTrack", perfetto::Track(8086));
151 // Simulates the non-tracing work of this function, which should take priority
152 // over the above trace event in terms of instruction scheduling.
153 puts("Hello");
154 }
155
FunctionWithOneLegacyEvent()156 void FunctionWithOneLegacyEvent() {
157 TRACE_EVENT_BEGIN("cat1", "LegacyEventWithArgs", "arg1", 42, "arg2", .5f);
158 // Simulates the non-tracing work of this function, which should take priority
159 // over the above trace event in terms of instruction scheduling.
160 puts("Hello");
161 }
162
FunctionWithOneScopedLegacyEvent()163 void FunctionWithOneScopedLegacyEvent() {
164 TRACE_EVENT("cat1", "ScopedLegacyEventWithArgs", "arg1", 42, "arg2", .5f);
165 // Simulates the non-tracing work of this function, which should take priority
166 // over the above trace event in terms of instruction scheduling.
167 puts("Hello");
168 }
169
FunctionWithOneCounterEvent()170 void FunctionWithOneCounterEvent() {
171 TRACE_COUNTER("cat1", "CounterName", 4096);
172 // Simulates the non-tracing work of this function, which should take priority
173 // over the above trace event in terms of instruction scheduling.
174 puts("Hello");
175 }
176
177 } // namespace tracing_module
178