Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
trace_event.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/trace_event.h"
6
7#include <algorithm>
8#include <atomic>
9#include <utility>
10
11#include "flutter/fml/ascii_trie.h"
12#include "flutter/fml/build_config.h"
13#include "flutter/fml/logging.h"
14
15namespace fml {
16namespace tracing {
17
18#if FLUTTER_TIMELINE_ENABLED
19
20namespace {
21
22int64_t DefaultMicrosSource() {
23 return -1;
24}
25
26AsciiTrie gAllowlist;
27std::atomic<TimelineEventHandler> gTimelineEventHandler;
28std::atomic<TimelineMicrosSource> gTimelineMicrosSource = DefaultMicrosSource;
29
30inline void FlutterTimelineEvent(const char* label,
31 int64_t timestamp0,
32 int64_t timestamp1_or_async_id,
33 intptr_t flow_id_count,
34 const int64_t* flow_ids,
36 intptr_t argument_count,
37 const char** argument_names,
38 const char** argument_values) {
39 TimelineEventHandler handler =
40 gTimelineEventHandler.load(std::memory_order_relaxed);
41 if (handler && gAllowlist.Query(label)) {
42 handler(label, timestamp0, timestamp1_or_async_id, flow_id_count, flow_ids,
43 type, argument_count, argument_names, argument_values);
44 }
45}
46} // namespace
47
48void TraceSetAllowlist(const std::vector<std::string>& allowlist) {
49 gAllowlist.Fill(allowlist);
50}
51
53 gTimelineEventHandler = handler;
54}
55
57 return static_cast<bool>(
58 gTimelineEventHandler.load(std::memory_order_relaxed));
59}
60
61int64_t TraceGetTimelineMicros() {
62 return gTimelineMicrosSource.load()();
63}
64
66 gTimelineMicrosSource = source;
67}
68
69size_t TraceNonce() {
70 static std::atomic_size_t last_item;
71 return ++last_item;
72}
73
74void TraceTimelineEvent(TraceArg category_group,
76 int64_t timestamp_micros,
78 size_t flow_id_count,
79 const uint64_t* flow_ids,
81 const std::vector<const char*>& c_names,
82 const std::vector<std::string>& values) {
83 const auto argument_count = std::min(c_names.size(), values.size());
84
85 std::vector<const char*> c_values;
86 c_values.resize(argument_count, nullptr);
87
88 for (size_t i = 0; i < argument_count; i++) {
89 c_values[i] = values[i].c_str();
90 }
91
92 FlutterTimelineEvent(
93 name, // label
94 timestamp_micros, // timestamp0
95 identifier, // timestamp1_or_async_id
96 flow_id_count, // flow_id_count
97 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
98 type, // event type
99 argument_count, // argument_count
100 const_cast<const char**>(c_names.data()), // argument_names
101 c_values.data() // argument_values
102 );
103}
104
105void TraceTimelineEvent(TraceArg category_group,
108 size_t flow_id_count,
109 const uint64_t* flow_ids,
111 const std::vector<const char*>& c_names,
112 const std::vector<std::string>& values) {
113 TraceTimelineEvent(category_group, // group
114 name, // name
115 gTimelineMicrosSource.load()(), // timestamp_micros
116 identifier, // identifier
117 flow_id_count, // flow_id_count
118 flow_ids, // flow_ids
119 type, // type
120 c_names, // names
121 values // values
122 );
123}
124
125void TraceEvent0(TraceArg category_group,
127 size_t flow_id_count,
128 const uint64_t* flow_ids) {
129 FlutterTimelineEvent(name, // label
130 gTimelineMicrosSource.load()(), // timestamp0
131 0, // timestamp1_or_async_id
132 flow_id_count, // flow_id_count
133 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
134 Dart_Timeline_Event_Begin, // event type
135 0, // argument_count
136 nullptr, // argument_names
137 nullptr // argument_values
138 );
139}
140
141void TraceEvent1(TraceArg category_group,
143 size_t flow_id_count,
144 const uint64_t* flow_ids,
145 TraceArg arg1_name,
146 TraceArg arg1_val) {
147 const char* arg_names[] = {arg1_name};
148 const char* arg_values[] = {arg1_val};
149 FlutterTimelineEvent(name, // label
150 gTimelineMicrosSource.load()(), // timestamp0
151 0, // timestamp1_or_async_id
152 flow_id_count, // flow_id_count
153 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
154 Dart_Timeline_Event_Begin, // event type
155 1, // argument_count
156 arg_names, // argument_names
157 arg_values // argument_values
158 );
159}
160
161void TraceEvent2(TraceArg category_group,
163 size_t flow_id_count,
164 const uint64_t* flow_ids,
165 TraceArg arg1_name,
166 TraceArg arg1_val,
167 TraceArg arg2_name,
168 TraceArg arg2_val) {
169 const char* arg_names[] = {arg1_name, arg2_name};
170 const char* arg_values[] = {arg1_val, arg2_val};
171 FlutterTimelineEvent(name, // label
172 gTimelineMicrosSource.load()(), // timestamp0
173 0, // timestamp1_or_async_id
174 flow_id_count, // flow_id_count
175 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
176 Dart_Timeline_Event_Begin, // event type
177 2, // argument_count
178 arg_names, // argument_names
179 arg_values // argument_values
180 );
181}
182
184 FlutterTimelineEvent(name, // label
185 gTimelineMicrosSource.load()(), // timestamp0
186 0, // timestamp1_or_async_id
187 0, // flow_id_count
188 nullptr, // flow_ids
189 Dart_Timeline_Event_End, // event type
190 0, // argument_count
191 nullptr, // argument_names
192 nullptr // argument_values
193 );
194}
195
196void TraceEventAsyncBegin0(TraceArg category_group,
198 TraceIDArg id,
199 size_t flow_id_count,
200 const uint64_t* flow_ids) {
201 FlutterTimelineEvent(name, // label
202 gTimelineMicrosSource.load()(), // timestamp0
203 id, // timestamp1_or_async_id
204 flow_id_count, // flow_id_count
205 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
207 0, // argument_count
208 nullptr, // argument_names
209 nullptr // argument_values
210 );
211}
212
213void TraceEventAsyncEnd0(TraceArg category_group,
215 TraceIDArg id) {
216 FlutterTimelineEvent(name, // label
217 gTimelineMicrosSource.load()(), // timestamp0
218 id, // timestamp1_or_async_id
219 0, // flow_id_count
220 nullptr, // flow_ids
221 Dart_Timeline_Event_Async_End, // event type
222 0, // argument_count
223 nullptr, // argument_names
224 nullptr // argument_values
225 );
226}
227
228void TraceEventAsyncBegin1(TraceArg category_group,
230 TraceIDArg id,
231 size_t flow_id_count,
232 const uint64_t* flow_ids,
233 TraceArg arg1_name,
234 TraceArg arg1_val) {
235 const char* arg_names[] = {arg1_name};
236 const char* arg_values[] = {arg1_val};
237 FlutterTimelineEvent(name, // label
238 gTimelineMicrosSource.load()(), // timestamp0
239 id, // timestamp1_or_async_id
240 flow_id_count, // flow_id_count
241 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
243 1, // argument_count
244 arg_names, // argument_names
245 arg_values // argument_values
246 );
247}
248
249void TraceEventAsyncEnd1(TraceArg category_group,
251 TraceIDArg id,
252 TraceArg arg1_name,
253 TraceArg arg1_val) {
254 const char* arg_names[] = {arg1_name};
255 const char* arg_values[] = {arg1_val};
256 FlutterTimelineEvent(name, // label
257 gTimelineMicrosSource.load()(), // timestamp0
258 id, // timestamp1_or_async_id
259 0, // flow_id_count
260 nullptr, // flow_ids
261 Dart_Timeline_Event_Async_End, // event type
262 1, // argument_count
263 arg_names, // argument_names
264 arg_values // argument_values
265 );
266}
267
268void TraceEventInstant0(TraceArg category_group,
270 size_t flow_id_count,
271 const uint64_t* flow_ids) {
272 FlutterTimelineEvent(name, // label
273 gTimelineMicrosSource.load()(), // timestamp0
274 0, // timestamp1_or_async_id
275 flow_id_count, // flow_id_count
276 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
277 Dart_Timeline_Event_Instant, // event type
278 0, // argument_count
279 nullptr, // argument_names
280 nullptr // argument_values
281 );
282}
283
284void TraceEventInstant1(TraceArg category_group,
286 size_t flow_id_count,
287 const uint64_t* flow_ids,
288 TraceArg arg1_name,
289 TraceArg arg1_val) {
290 const char* arg_names[] = {arg1_name};
291 const char* arg_values[] = {arg1_val};
292 FlutterTimelineEvent(name, // label
293 gTimelineMicrosSource.load()(), // timestamp0
294 0, // timestamp1_or_async_id
295 flow_id_count, // flow_id_count
296 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
297 Dart_Timeline_Event_Instant, // event type
298 1, // argument_count
299 arg_names, // argument_names
300 arg_values // argument_values
301 );
302}
303
304void TraceEventInstant2(TraceArg category_group,
306 size_t flow_id_count,
307 const uint64_t* flow_ids,
308 TraceArg arg1_name,
309 TraceArg arg1_val,
310 TraceArg arg2_name,
311 TraceArg arg2_val) {
312 const char* arg_names[] = {arg1_name, arg2_name};
313 const char* arg_values[] = {arg1_val, arg2_val};
314 FlutterTimelineEvent(name, // label
315 gTimelineMicrosSource.load()(), // timestamp0
316 0, // timestamp1_or_async_id
317 flow_id_count, // flow_id_count
318 reinterpret_cast<const int64_t*>(flow_ids), // flow_ids
319 Dart_Timeline_Event_Instant, // event type
320 2, // argument_count
321 arg_names, // argument_names
322 arg_values // argument_values
323 );
324}
325
326void TraceEventFlowBegin0(TraceArg category_group,
328 TraceIDArg id) {
329 FlutterTimelineEvent(name, // label
330 gTimelineMicrosSource.load()(), // timestamp0
331 id, // timestamp1_or_async_id
332 0, // flow_id_count
333 nullptr, // flow_ids
334 Dart_Timeline_Event_Flow_Begin, // event type
335 0, // argument_count
336 nullptr, // argument_names
337 nullptr // argument_values
338 );
339}
340
341void TraceEventFlowStep0(TraceArg category_group,
343 TraceIDArg id) {
344 FlutterTimelineEvent(name, // label
345 gTimelineMicrosSource.load()(), // timestamp0
346 id, // timestamp1_or_async_id
347 0, // flow_id_count
348 nullptr, // flow_ids
349 Dart_Timeline_Event_Flow_Step, // event type
350 0, // argument_count
351 nullptr, // argument_names
352 nullptr // argument_values
353 );
354}
355
356void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) {
357 FlutterTimelineEvent(name, // label
358 gTimelineMicrosSource.load()(), // timestamp0
359 id, // timestamp1_or_async_id
360 0, // flow_id_count
361 nullptr, // flow_ids
362 Dart_Timeline_Event_Flow_End, // event type
363 0, // argument_count
364 nullptr, // argument_names
365 nullptr // argument_values
366 );
367}
368
369#else // FLUTTER_TIMELINE_ENABLED
370
371void TraceSetAllowlist(const std::vector<std::string>& allowlist) {}
372
374
376 return false;
377}
378
380 return -1;
381}
382
384
385size_t TraceNonce() {
386 return 0;
387}
388
389void TraceTimelineEvent(TraceArg category_group,
391 int64_t timestamp_micros,
393 size_t flow_id_count,
394 const uint64_t* flow_ids,
396 const std::vector<const char*>& c_names,
397 const std::vector<std::string>& values) {}
398
399void TraceTimelineEvent(TraceArg category_group,
402 size_t flow_id_count,
403 const uint64_t* flow_ids,
405 const std::vector<const char*>& c_names,
406 const std::vector<std::string>& values) {}
407
408void TraceEvent0(TraceArg category_group,
410 size_t flow_id_count,
411 const uint64_t* flow_ids) {}
412
413void TraceEvent1(TraceArg category_group,
415 size_t flow_id_count,
416 const uint64_t* flow_ids,
417 TraceArg arg1_name,
418 TraceArg arg1_val) {}
419
420void TraceEvent2(TraceArg category_group,
422 size_t flow_id_count,
423 const uint64_t* flow_ids,
424 TraceArg arg1_name,
425 TraceArg arg1_val,
426 TraceArg arg2_name,
427 TraceArg arg2_val) {}
428
430
435
436void TraceEventAsyncBegin0(TraceArg category_group,
438 TraceIDArg id,
439 size_t flow_id_count,
440 const uint64_t* flow_ids) {}
441
442void TraceEventAsyncEnd0(TraceArg category_group,
444 TraceIDArg id) {}
445
446void TraceEventAsyncBegin1(TraceArg category_group,
448 TraceIDArg id,
449 size_t flow_id_count,
450 const uint64_t* flow_ids,
451 TraceArg arg1_name,
452 TraceArg arg1_val) {}
453
454void TraceEventAsyncEnd1(TraceArg category_group,
456 TraceIDArg id,
457 TraceArg arg1_name,
458 TraceArg arg1_val) {}
459
460void TraceEventInstant0(TraceArg category_group,
462 size_t flow_id_count,
463 const uint64_t* flow_ids) {}
464
465void TraceEventInstant1(TraceArg category_group,
467 size_t flow_id_count,
468 const uint64_t* flow_ids,
469 TraceArg arg1_name,
470 TraceArg arg1_val) {}
471
472void TraceEventInstant2(TraceArg category_group,
474 size_t flow_id_count,
475 const uint64_t* flow_ids,
476 TraceArg arg1_name,
477 TraceArg arg1_val,
478 TraceArg arg2_name,
479 TraceArg arg2_val) {}
480
481void TraceEventFlowBegin0(TraceArg category_group,
483 TraceIDArg id) {}
484
485void TraceEventFlowStep0(TraceArg category_group,
487 TraceIDArg id) {}
488
490}
491
492#endif // FLUTTER_TIMELINE_ENABLED
493
494} // namespace tracing
495} // namespace fml
const char * data() const
Definition SkString.h:132
A trie for looking for ASCII prefixes.
Definition ascii_trie.h:15
bool Query(const char *argument)
Returns true if argument is prefixed by the contents of the trie.
Definition ascii_trie.h:26
static SkString identifier(const FontFamilyDesc &family, const FontDesc &font)
Dart_Timeline_Event_Type
@ Dart_Timeline_Event_Async_Begin
@ Dart_Timeline_Event_Async_End
@ Dart_Timeline_Event_Begin
@ Dart_Timeline_Event_Flow_End
@ Dart_Timeline_Event_Flow_Begin
@ Dart_Timeline_Event_End
@ Dart_Timeline_Event_Instant
@ Dart_Timeline_Event_Flow_Step
static const char * begin(const StringSlice &s)
Definition editor.cpp:252
SkBitmap source
Definition examples.cpp:28
glong glong end
const char * name
Definition fuchsia.cc:50
int argument_count
Definition fuchsia.cc:52
void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id)
void TraceEventInstant0(TraceArg category_group, TraceArg name, size_t flow_id_count, const uint64_t *flow_ids)
void TraceEventFlowBegin0(TraceArg category_group, TraceArg name, TraceIDArg id)
bool TraceHasTimelineEventHandler()
void TraceEventAsyncBegin0(TraceArg category_group, TraceArg name, TraceIDArg id, size_t flow_id_count, const uint64_t *flow_ids)
void TraceSetAllowlist(const std::vector< std::string > &allowlist)
void TraceEventAsyncEnd0(TraceArg category_group, TraceArg name, TraceIDArg id)
void TraceEvent0(TraceArg category_group, TraceArg name, size_t flow_id_count, const uint64_t *flow_ids)
void TraceEventInstant1(TraceArg category_group, TraceArg name, size_t flow_id_count, const uint64_t *flow_ids, TraceArg arg1_name, TraceArg arg1_val)
const char * TraceArg
int64_t(*)() TimelineMicrosSource
void TraceEventAsyncBegin1(TraceArg category_group, TraceArg name, TraceIDArg id, size_t flow_id_count, const uint64_t *flow_ids, TraceArg arg1_name, TraceArg arg1_val)
void TraceTimelineEvent(TraceArg category_group, TraceArg name, int64_t timestamp_micros, TraceIDArg identifier, size_t flow_id_count, const uint64_t *flow_ids, Dart_Timeline_Event_Type type, const std::vector< const char * > &c_names, const std::vector< std::string > &values)
void TraceSetTimelineEventHandler(TimelineEventHandler handler)
void TraceSetTimelineMicrosSource(TimelineMicrosSource source)
void TraceEvent2(TraceArg category_group, TraceArg name, size_t flow_id_count, const uint64_t *flow_ids, TraceArg arg1_name, TraceArg arg1_val, TraceArg arg2_name, TraceArg arg2_val)
void TraceEventAsyncEnd1(TraceArg category_group, TraceArg name, TraceIDArg id, TraceArg arg1_name, TraceArg arg1_val)
int64_t TraceIDArg
size_t TraceNonce()
void TraceEventEnd(TraceArg name)
void TraceEventInstant2(TraceArg category_group, TraceArg name, size_t flow_id_count, const uint64_t *flow_ids, TraceArg arg1_name, TraceArg arg1_val, TraceArg arg2_name, TraceArg arg2_val)
int64_t TraceGetTimelineMicros()
void TraceEvent1(TraceArg category_group, TraceArg name, size_t flow_id_count, const uint64_t *flow_ids, TraceArg arg1_name, TraceArg arg1_val)
void(* TimelineEventHandler)(const char *, int64_t, int64_t, intptr_t, const int64_t *, Dart_Timeline_Event_Type, intptr_t, const char **, const char **)
void TraceEventAsyncComplete(TraceArg category_group, TraceArg name, TimePoint begin, TimePoint end)
void TraceEventFlowStep0(TraceArg category_group, TraceArg name, TraceIDArg id)