Flutter Engine
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 
15 namespace fml {
16 namespace tracing {
17 
18 #if FLUTTER_TIMELINE_ENABLED
19 
20 namespace {
21 AsciiTrie gAllowlist;
22 TimelineEventHandler gTimelineEventHandler;
23 
24 inline void FlutterTimelineEvent(const char* label,
25  int64_t timestamp0,
26  int64_t timestamp1_or_async_id,
27  Dart_Timeline_Event_Type type,
28  intptr_t argument_count,
29  const char** argument_names,
30  const char** argument_values) {
31  if (gTimelineEventHandler && gAllowlist.Query(label)) {
32  gTimelineEventHandler(label, timestamp0, timestamp1_or_async_id, type,
33  argument_count, argument_names, argument_values);
34  }
35 }
36 } // namespace
37 
38 void TraceSetAllowlist(const std::vector<std::string>& allowlist) {
39  gAllowlist.Fill(allowlist);
40 }
41 
43  gTimelineEventHandler = handler;
44 }
45 
46 size_t TraceNonce() {
47  static std::atomic_size_t gLastItem;
48  return ++gLastItem;
49 }
50 
51 void TraceTimelineEvent(TraceArg category_group,
52  TraceArg name,
53  int64_t timestamp_micros,
54  TraceIDArg identifier,
55  Dart_Timeline_Event_Type type,
56  const std::vector<const char*>& c_names,
57  const std::vector<std::string>& values) {
58  const auto argument_count = std::min(c_names.size(), values.size());
59 
60  std::vector<const char*> c_values;
61  c_values.resize(argument_count, nullptr);
62 
63  for (size_t i = 0; i < argument_count; i++) {
64  c_values[i] = values[i].c_str();
65  }
66 
67  FlutterTimelineEvent(
68  name, // label
69  timestamp_micros, // timestamp0
70  identifier, // timestamp1_or_async_id
71  type, // event type
72  argument_count, // argument_count
73  const_cast<const char**>(c_names.data()), // argument_names
74  c_values.data() // argument_values
75  );
76 }
77 
78 void TraceTimelineEvent(TraceArg category_group,
79  TraceArg name,
80  TraceIDArg identifier,
81  Dart_Timeline_Event_Type type,
82  const std::vector<const char*>& c_names,
83  const std::vector<std::string>& values) {
84  TraceTimelineEvent(category_group, // group
85  name, // name
86  Dart_TimelineGetMicros(), // timestamp_micros
87  identifier, // identifier
88  type, // type
89  c_names, // names
90  values // values
91  );
92 }
93 
94 void TraceEvent0(TraceArg category_group, TraceArg name) {
95  FlutterTimelineEvent(name, // label
96  Dart_TimelineGetMicros(), // timestamp0
97  0, // timestamp1_or_async_id
98  Dart_Timeline_Event_Begin, // event type
99  0, // argument_count
100  nullptr, // argument_names
101  nullptr // argument_values
102  );
103 }
104 
105 void TraceEvent1(TraceArg category_group,
106  TraceArg name,
107  TraceArg arg1_name,
108  TraceArg arg1_val) {
109  const char* arg_names[] = {arg1_name};
110  const char* arg_values[] = {arg1_val};
111  FlutterTimelineEvent(name, // label
112  Dart_TimelineGetMicros(), // timestamp0
113  0, // timestamp1_or_async_id
114  Dart_Timeline_Event_Begin, // event type
115  1, // argument_count
116  arg_names, // argument_names
117  arg_values // argument_values
118  );
119 }
120 
121 void TraceEvent2(TraceArg category_group,
122  TraceArg name,
123  TraceArg arg1_name,
124  TraceArg arg1_val,
125  TraceArg arg2_name,
126  TraceArg arg2_val) {
127  const char* arg_names[] = {arg1_name, arg2_name};
128  const char* arg_values[] = {arg1_val, arg2_val};
129  FlutterTimelineEvent(name, // label
130  Dart_TimelineGetMicros(), // timestamp0
131  0, // timestamp1_or_async_id
132  Dart_Timeline_Event_Begin, // event type
133  2, // argument_count
134  arg_names, // argument_names
135  arg_values // argument_values
136  );
137 }
138 
139 void TraceEventEnd(TraceArg name) {
140  FlutterTimelineEvent(name, // label
141  Dart_TimelineGetMicros(), // timestamp0
142  0, // timestamp1_or_async_id
143  Dart_Timeline_Event_End, // event type
144  0, // argument_count
145  nullptr, // argument_names
146  nullptr // argument_values
147  );
148 }
149 
150 void TraceEventAsyncBegin0(TraceArg category_group,
151  TraceArg name,
152  TraceIDArg id) {
153  FlutterTimelineEvent(name, // label
154  Dart_TimelineGetMicros(), // timestamp0
155  id, // timestamp1_or_async_id
156  Dart_Timeline_Event_Async_Begin, // event type
157  0, // argument_count
158  nullptr, // argument_names
159  nullptr // argument_values
160  );
161 }
162 
163 void TraceEventAsyncEnd0(TraceArg category_group,
164  TraceArg name,
165  TraceIDArg id) {
166  FlutterTimelineEvent(name, // label
167  Dart_TimelineGetMicros(), // timestamp0
168  id, // timestamp1_or_async_id
169  Dart_Timeline_Event_Async_End, // event type
170  0, // argument_count
171  nullptr, // argument_names
172  nullptr // argument_values
173  );
174 }
175 
176 void TraceEventAsyncBegin1(TraceArg category_group,
177  TraceArg name,
178  TraceIDArg id,
179  TraceArg arg1_name,
180  TraceArg arg1_val) {
181  const char* arg_names[] = {arg1_name};
182  const char* arg_values[] = {arg1_val};
183  FlutterTimelineEvent(name, // label
184  Dart_TimelineGetMicros(), // timestamp0
185  id, // timestamp1_or_async_id
186  Dart_Timeline_Event_Async_Begin, // event type
187  1, // argument_count
188  arg_names, // argument_names
189  arg_values // argument_values
190  );
191 }
192 
193 void TraceEventAsyncEnd1(TraceArg category_group,
194  TraceArg name,
195  TraceIDArg id,
196  TraceArg arg1_name,
197  TraceArg arg1_val) {
198  const char* arg_names[] = {arg1_name};
199  const char* arg_values[] = {arg1_val};
200  FlutterTimelineEvent(name, // label
201  Dart_TimelineGetMicros(), // timestamp0
202  id, // timestamp1_or_async_id
203  Dart_Timeline_Event_Async_End, // event type
204  1, // argument_count
205  arg_names, // argument_names
206  arg_values // argument_values
207  );
208 }
209 
210 void TraceEventInstant0(TraceArg category_group, TraceArg name) {
211  FlutterTimelineEvent(name, // label
212  Dart_TimelineGetMicros(), // timestamp0
213  0, // timestamp1_or_async_id
214  Dart_Timeline_Event_Instant, // event type
215  0, // argument_count
216  nullptr, // argument_names
217  nullptr // argument_values
218  );
219 }
220 
221 void TraceEventInstant1(TraceArg category_group,
222  TraceArg name,
223  TraceArg arg1_name,
224  TraceArg arg1_val) {
225  const char* arg_names[] = {arg1_name};
226  const char* arg_values[] = {arg1_val};
227  FlutterTimelineEvent(name, // label
228  Dart_TimelineGetMicros(), // timestamp0
229  0, // timestamp1_or_async_id
230  Dart_Timeline_Event_Instant, // event type
231  1, // argument_count
232  arg_names, // argument_names
233  arg_values // argument_values
234  );
235 }
236 
237 void TraceEventInstant2(TraceArg category_group,
238  TraceArg name,
239  TraceArg arg1_name,
240  TraceArg arg1_val,
241  TraceArg arg2_name,
242  TraceArg arg2_val) {
243  const char* arg_names[] = {arg1_name, arg2_name};
244  const char* arg_values[] = {arg1_val, arg2_val};
245  FlutterTimelineEvent(name, // label
246  Dart_TimelineGetMicros(), // timestamp0
247  0, // timestamp1_or_async_id
248  Dart_Timeline_Event_Instant, // event type
249  2, // argument_count
250  arg_names, // argument_names
251  arg_values // argument_values
252  );
253 }
254 
255 void TraceEventFlowBegin0(TraceArg category_group,
256  TraceArg name,
257  TraceIDArg id) {
258  FlutterTimelineEvent(name, // label
259  Dart_TimelineGetMicros(), // timestamp0
260  id, // timestamp1_or_async_id
261  Dart_Timeline_Event_Flow_Begin, // event type
262  0, // argument_count
263  nullptr, // argument_names
264  nullptr // argument_values
265  );
266 }
267 
268 void TraceEventFlowStep0(TraceArg category_group,
269  TraceArg name,
270  TraceIDArg id) {
271  FlutterTimelineEvent(name, // label
272  Dart_TimelineGetMicros(), // timestamp0
273  id, // timestamp1_or_async_id
274  Dart_Timeline_Event_Flow_Step, // event type
275  0, // argument_count
276  nullptr, // argument_names
277  nullptr // argument_values
278  );
279 }
280 
281 void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) {
282  FlutterTimelineEvent(name, // label
283  Dart_TimelineGetMicros(), // timestamp0
284  id, // timestamp1_or_async_id
285  Dart_Timeline_Event_Flow_End, // event type
286  0, // argument_count
287  nullptr, // argument_names
288  nullptr // argument_values
289  );
290 }
291 
292 #else // FLUTTER_TIMELINE_ENABLED
293 
294 void TraceSetAllowlist(const std::vector<std::string>& allowlist) {}
295 
297 
298 size_t TraceNonce() {
299  return 0;
300 }
301 
302 void TraceTimelineEvent(TraceArg category_group,
303  TraceArg name,
304  int64_t timestamp_micros,
305  TraceIDArg identifier,
306  Dart_Timeline_Event_Type type,
307  const std::vector<const char*>& c_names,
308  const std::vector<std::string>& values) {}
309 
310 void TraceTimelineEvent(TraceArg category_group,
311  TraceArg name,
312  TraceIDArg identifier,
313  Dart_Timeline_Event_Type type,
314  const std::vector<const char*>& c_names,
315  const std::vector<std::string>& values) {}
316 
317 void TraceEvent0(TraceArg category_group, TraceArg name) {}
318 
319 void TraceEvent1(TraceArg category_group,
320  TraceArg name,
321  TraceArg arg1_name,
322  TraceArg arg1_val) {}
323 
324 void TraceEvent2(TraceArg category_group,
325  TraceArg name,
326  TraceArg arg1_name,
327  TraceArg arg1_val,
328  TraceArg arg2_name,
329  TraceArg arg2_val) {}
330 
331 void TraceEventEnd(TraceArg name) {}
332 
333 void TraceEventAsyncComplete(TraceArg category_group,
334  TraceArg name,
335  TimePoint begin,
336  TimePoint end) {}
337 
338 void TraceEventAsyncBegin0(TraceArg category_group,
339  TraceArg name,
340  TraceIDArg id) {}
341 
342 void TraceEventAsyncEnd0(TraceArg category_group,
343  TraceArg name,
344  TraceIDArg id) {}
345 
346 void TraceEventAsyncBegin1(TraceArg category_group,
347  TraceArg name,
348  TraceIDArg id,
349  TraceArg arg1_name,
350  TraceArg arg1_val) {}
351 
352 void TraceEventAsyncEnd1(TraceArg category_group,
353  TraceArg name,
354  TraceIDArg id,
355  TraceArg arg1_name,
356  TraceArg arg1_val) {}
357 
358 void TraceEventInstant0(TraceArg category_group, TraceArg name) {}
359 
360 void TraceEventInstant1(TraceArg category_group,
361  TraceArg name,
362  TraceArg arg1_name,
363  TraceArg arg1_val) {}
364 
365 void TraceEventInstant2(TraceArg category_group,
366  TraceArg name,
367  TraceArg arg1_name,
368  TraceArg arg1_val,
369  TraceArg arg2_name,
370  TraceArg arg2_val) {}
371 
372 void TraceEventFlowBegin0(TraceArg category_group,
373  TraceArg name,
374  TraceIDArg id) {}
375 
376 void TraceEventFlowStep0(TraceArg category_group,
377  TraceArg name,
378  TraceIDArg id) {}
379 
380 void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) {
381 }
382 
383 #endif // FLUTTER_TIMELINE_ENABLED
384 
385 } // namespace tracing
386 } // namespace fml
void TraceEventInstant1(TraceArg category_group, TraceArg name, TraceArg arg1_name, TraceArg arg1_val)
Definition: trace_event.cc:360
KeyCallType type
void TraceEventAsyncEnd0(TraceArg category_group, TraceArg name, TraceIDArg id)
Definition: trace_event.cc:342
std::function< void(const char *, int64_t, int64_t, Dart_Timeline_Event_Type, intptr_t, const char **, const char **)> TimelineEventHandler
Definition: trace_event.h:156
void TraceEventEnd(TraceArg name)
Definition: trace_event.cc:331
void TraceEventFlowBegin0(TraceArg category_group, TraceArg name, TraceIDArg id)
Definition: trace_event.cc:372
int argument_count
Definition: fuchsia.cc:52
void TraceTimelineEvent(TraceArg category_group, TraceArg name, int64_t timestamp_micros, TraceIDArg identifier, Dart_Timeline_Event_Type type, const std::vector< const char *> &c_names, const std::vector< std::string > &values)
Definition: trace_event.cc:302
Definition: ascii_trie.cc:9
void TraceEvent1(TraceArg category_group, TraceArg name, TraceArg arg1_name, TraceArg arg1_val)
Definition: trace_event.cc:319
const char * TraceArg
Definition: trace_event.h:145
void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id)
Definition: trace_event.cc:380
void TraceEventInstant0(TraceArg category_group, TraceArg name)
Definition: trace_event.cc:358
void TraceEventAsyncEnd1(TraceArg category_group, TraceArg name, TraceIDArg id, TraceArg arg1_name, TraceArg arg1_val)
Definition: trace_event.cc:352
void TraceEventFlowStep0(TraceArg category_group, TraceArg name, TraceIDArg id)
Definition: trace_event.cc:376
void TraceSetTimelineEventHandler(TimelineEventHandler handler)
Definition: trace_event.cc:296
bool Query(const char *argument)
Returns true if argument is prefixed by the contents of the trie.
Definition: ascii_trie.h:26
int64_t TraceIDArg
Definition: trace_event.h:146
void TraceSetAllowlist(const std::vector< std::string > &allowlist)
Definition: trace_event.cc:294
void TraceEvent2(TraceArg category_group, TraceArg name, TraceArg arg1_name, TraceArg arg1_val, TraceArg arg2_name, TraceArg arg2_val)
Definition: trace_event.cc:324
void TraceEventAsyncBegin0(TraceArg category_group, TraceArg name, TraceIDArg id)
Definition: trace_event.cc:338
const char * name
Definition: fuchsia.cc:50
void TraceEventInstant2(TraceArg category_group, TraceArg name, TraceArg arg1_name, TraceArg arg1_val, TraceArg arg2_name, TraceArg arg2_val)
Definition: trace_event.cc:365
A trie for looking for ASCII prefixes.
Definition: ascii_trie.h:15
void TraceEventAsyncComplete(TraceArg category_group, TraceArg name, TimePoint begin, TimePoint end)
Definition: trace_event.cc:333
size_t TraceNonce()
Definition: trace_event.cc:298
void TraceEvent0(TraceArg category_group, TraceArg name)
Definition: trace_event.cc:317
void TraceEventAsyncBegin1(TraceArg category_group, TraceArg name, TraceIDArg id, TraceArg arg1_name, TraceArg arg1_val)
Definition: trace_event.cc:346