1
1
#include " base/at_exit.h"
2
2
#include " base/command_line.h"
3
- #include " base/debug/stack_trace.h"
4
3
#include " base/logging.h"
5
4
#include " base/memory/ref_counted_memory.h"
6
5
#include " base/memory/scoped_refptr.h"
7
- #include " base/task/single_thread_task_executor.h"
8
6
#include " base/run_loop.h"
9
- #include " base/trace_event/common/trace_event_common .h"
7
+ #include " base/task/single_thread_task_executor .h"
10
8
#include " base/trace_event/trace_event.h"
11
9
#include " base/trace_event/trace_log.h"
12
10
13
11
void TraceMe () {
14
- TRACE_EVENT0 (" test" , " TraceMe " );
15
- TRACE_EVENT1 (" test" , " TraceMe " , " value" , 1 );
16
- TRACE_EVENT2 (" test" , " TraceMe " , " value" , 1 , " value2" , 2 );
12
+ TRACE_EVENT0 (" test" , " TraceMe1 " );
13
+ TRACE_EVENT1 (" test" , " TraceMe2 " , " value" , 1 );
14
+ TRACE_EVENT2 (" test" , " TraceMe3 " , " value" , 1 , " value2" , 2 );
17
15
}
18
16
19
17
void TraceCount (int times) {
@@ -27,9 +25,12 @@ int main(int argc, char** argv) {
27
25
// 1. 创建配置文件对象;
28
26
base::trace_event::TraceConfig trace_config;
29
27
// 手动创建TraceConfig
30
- trace_config =
31
- base::trace_event::TraceConfig (" test,testxxx" , " trace-to-console" );
32
-
28
+ // M120 在 Linux/Android 已经默认启用 perfetto client,这种模式下
29
+ // trace-to-console 和运行时捕获 trace 不兼容,所以这里改为
30
+ // record-as-much-as-possible.
31
+ trace_config = base::trace_event::TraceConfig (" test,testxxx" ,
32
+ " record-as-much-as-possible" );
33
+
33
34
// 也可以使用component模块的方法来获取TraceConfig
34
35
// 方法1 获取用于输出到控制台的TraceConfig
35
36
// trace_config = tracing::GetConfigForTraceToConsole();
@@ -50,48 +51,133 @@ int main(int argc, char** argv) {
50
51
TRACE_EVENT0 (" testxxx," , " main" );
51
52
// ==============================================================
52
53
// 以上宏展开后等价于下面的代码
53
- static_assert (
54
- base::trace_event::BuiltinCategories::IsAllowedCategory (" testxxx," ),
55
- " Unknown tracing category is used. Please register your "
56
- " category in base/trace_event/builtin_categories.h" );
57
- constexpr const unsigned char * trace_event_unique_k_category_group_enabled31 =
58
- base::trace_event::TraceLog::GetBuiltinCategoryEnabled (" testxxx," );
59
- const unsigned char * trace_event_unique_category_group_enabled31;
60
- if (trace_event_unique_k_category_group_enabled31) {
61
- trace_event_unique_category_group_enabled31 =
62
- trace_event_unique_k_category_group_enabled31;
63
- } else {
64
- static base::subtle::AtomicWord trace_event_unique_atomic31 = 0 ;
65
- trace_event_unique_category_group_enabled31 =
66
- reinterpret_cast <const unsigned char *>(
67
- base::subtle::NoBarrier_Load (&(trace_event_unique_atomic31)));
68
- // __builtin_expect 用于编译优化
69
- if (__builtin_expect (!!(!trace_event_unique_category_group_enabled31), 0 )) {
70
- trace_event_unique_category_group_enabled31 =
71
- base::trace_event::TraceLog::GetCategoryGroupEnabled (" testxxx," );
72
- base::subtle::NoBarrier_Store (
73
- &(trace_event_unique_atomic31),
74
- (reinterpret_cast <base::subtle::AtomicWord>(
75
- trace_event_unique_category_group_enabled31)));
54
+ {
55
+ #if !BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
56
+ // 旧版本不使用 perfetto client
57
+ static_assert (
58
+ base::trace_event::BuiltinCategories::IsAllowedCategory (" testxxx," ),
59
+ " Unknown tracing category is used. Please register your "
60
+ " category in base/trace_event/builtin_categories.h" );
61
+ constexpr const unsigned char *
62
+ trace_event_unique_k_category_group_enabled50 =
63
+ base::trace_event::TraceLog::GetBuiltinCategoryEnabled (" testxxx," );
64
+ const unsigned char * trace_event_unique_category_group_enabled50;
65
+ if (trace_event_unique_k_category_group_enabled50) {
66
+ trace_event_unique_category_group_enabled50 =
67
+ trace_event_unique_k_category_group_enabled50;
68
+ } else {
69
+ static base::subtle::AtomicWord trace_event_unique_atomic50 = 0 ;
70
+ trace_event_unique_category_group_enabled50 =
71
+ reinterpret_cast <const unsigned char *>(
72
+ base::subtle::Acquire_Load (&(trace_event_unique_atomic50)));
73
+ if (__builtin_expect (!!(!trace_event_unique_category_group_enabled50),
74
+ 0 )) {
75
+ trace_event_unique_category_group_enabled50 =
76
+ base::trace_event::TraceLog::GetCategoryGroupEnabled (" testxxx," );
77
+ base::subtle::Release_Store (
78
+ &(trace_event_unique_atomic50),
79
+ (reinterpret_cast <base::subtle::AtomicWord>(
80
+ trace_event_unique_category_group_enabled50)));
81
+ };
76
82
};
77
- };
78
- ;
79
- trace_event_internal::ScopedTracer trace_event_unique_tracer31;
80
- if (__builtin_expect (
81
- !!(*trace_event_unique_category_group_enabled31 &
82
- (base::trace_event::TraceCategory::ENABLED_FOR_RECORDING |
83
- base::trace_event::TraceCategory::ENABLED_FOR_ETW_EXPORT |
84
- base::trace_event::TraceCategory::ENABLED_FOR_FILTERING)),
85
- 0 )) {
86
- // 在这里输出trace日志
87
- base::trace_event::TraceEventHandle h = trace_event_internal::AddTraceEvent (
88
- (' X' ), trace_event_unique_category_group_enabled31, " main" ,
89
- trace_event_internal::kGlobalScope , trace_event_internal::kNoId ,
90
- (static_cast <unsigned int >(0 )), trace_event_internal::kNoId );
91
- trace_event_unique_tracer31.Initialize (
92
- trace_event_unique_category_group_enabled31, " main" , h);
93
- };
94
- // ==============================================================
83
+ ;
84
+ trace_event_internal::ScopedTracer trace_event_unique_tracer50;
85
+ if (__builtin_expect (
86
+ !!(*trace_event_unique_category_group_enabled50 &
87
+ (base::trace_event::TraceCategory::ENABLED_FOR_RECORDING |
88
+ base::trace_event::TraceCategory::ENABLED_FOR_ETW_EXPORT |
89
+ base::trace_event::TraceCategory::ENABLED_FOR_FILTERING)),
90
+ 0 )) {
91
+ base::trace_event::TraceEventHandle trace_event_unique_h50 =
92
+ trace_event_internal::AddTraceEvent (
93
+ (' X' ), trace_event_unique_category_group_enabled50, " main" ,
94
+ trace_event_internal::kGlobalScope , trace_event_internal::kNoId ,
95
+ (static_cast <unsigned int >(0 )), trace_event_internal::kNoId );
96
+ trace_event_unique_tracer50.Initialize (
97
+ trace_event_unique_category_group_enabled50, " main" ,
98
+ trace_event_unique_h50);
99
+ };
100
+ #else // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
101
+ // 新版本使用 perfetto client
102
+ struct ScopedEvent50 {
103
+ struct EventFinalizer {
104
+ EventFinalizer (...) {}
105
+ ~EventFinalizer () {
106
+ do {
107
+ ::perfetto::internal::ValidateEventNameType<decltype (nullptr )>();
108
+ namespace tns = perfetto_track_event;
109
+ constexpr auto
110
+ kCatIndex_ADD_TO_PERFETTO_DEFINE_CATEGORIES_IF_FAILS_50 =
111
+ perfetto_track_event::internal::kConstExprCategoryRegistry
112
+ .Find (
113
+ " testxxx," ,
114
+ ::perfetto_track_event::internal::IsDynamicCategory (
115
+ " testxxx," ));
116
+ if (::perfetto_track_event::internal ::IsDynamicCategory (
117
+ " testxxx," )) {
118
+ tns::TrackEvent::CallIfEnabled ([&](
119
+ uint32_t
120
+ instances) __attribute__ ((no_thread_safety_analysis)) {
121
+ tns::TrackEvent::TraceForCategory (
122
+ instances, " testxxx," , nullptr ,
123
+ ::perfetto::protos::pbzero::TrackEvent::TYPE_SLICE_END);
124
+ });
125
+ } else {
126
+ tns::TrackEvent::CallIfCategoryEnabled (
127
+ kCatIndex_ADD_TO_PERFETTO_DEFINE_CATEGORIES_IF_FAILS_50 ,
128
+ [&](uint32_t instances) __attribute__ ((
129
+ no_thread_safety_analysis)) {
130
+ tns::TrackEvent::TraceForCategory (
131
+ instances,
132
+ kCatIndex_ADD_TO_PERFETTO_DEFINE_CATEGORIES_IF_FAILS_50 ,
133
+ nullptr ,
134
+ ::perfetto::protos::pbzero::TrackEvent::TYPE_SLICE_END);
135
+ });
136
+ }
137
+ } while (false );
138
+ }
139
+ EventFinalizer (const EventFinalizer&) = delete ;
140
+ inline EventFinalizer& operator =(const EventFinalizer&) = delete ;
141
+ EventFinalizer (EventFinalizer&&) = delete ;
142
+ EventFinalizer& operator =(EventFinalizer&&) = delete ;
143
+ } finalizer;
144
+ } scoped_event50{[&]() {
145
+ do {
146
+ ::perfetto::internal::ValidateEventNameType<
147
+ decltype (::perfetto::internal::DecayEventNameType (
148
+ ::perfetto::internal::DecayEventNameType (" main" )))>();
149
+ namespace tns = perfetto_track_event;
150
+ constexpr auto kCatIndex_ADD_TO_PERFETTO_DEFINE_CATEGORIES_IF_FAILS_50 =
151
+ perfetto_track_event::internal::kConstExprCategoryRegistry .Find (
152
+ " testxxx," , ::perfetto_track_event::internal::IsDynamicCategory (
153
+ " testxxx," ));
154
+ if (::perfetto_track_event::internal::IsDynamicCategory (" testxxx," )) {
155
+ tns::TrackEvent::CallIfEnabled ([&](
156
+ uint32_t instances) __attribute__ ((no_thread_safety_analysis)) {
157
+ tns::TrackEvent::TraceForCategory (
158
+ instances, " testxxx," ,
159
+ ::perfetto::internal::DecayEventNameType (
160
+ ::perfetto::internal::DecayEventNameType (" main" )),
161
+ ::perfetto::protos::pbzero::TrackEvent::TYPE_SLICE_BEGIN);
162
+ });
163
+ } else {
164
+ tns::TrackEvent::CallIfCategoryEnabled (
165
+ kCatIndex_ADD_TO_PERFETTO_DEFINE_CATEGORIES_IF_FAILS_50 ,
166
+ [&](uint32_t instances) __attribute__ ((
167
+ no_thread_safety_analysis)) {
168
+ tns::TrackEvent::TraceForCategory (
169
+ instances,
170
+ kCatIndex_ADD_TO_PERFETTO_DEFINE_CATEGORIES_IF_FAILS_50 ,
171
+ ::perfetto::internal::DecayEventNameType (
172
+ ::perfetto::internal::DecayEventNameType (" main" )),
173
+ ::perfetto::protos::pbzero::TrackEvent::TYPE_SLICE_BEGIN);
174
+ });
175
+ }
176
+ } while (false );
177
+ return 0 ;
178
+ }()};
179
+ #endif // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
180
+ } // ==============================================================
95
181
96
182
TraceMe ();
97
183
int i = 0 ;
0 commit comments