• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/build_config.h"
12 #include "flutter/fml/logging.h"
13 
14 namespace fml {
15 namespace tracing {
16 
TraceNonce()17 size_t TraceNonce() {
18   static std::atomic_size_t gLastItem;
19   return ++gLastItem;
20 }
21 
TraceBegin(const char * label,intptr_t count,const char ** names,const char ** values)22 void TraceBegin(const char* label,
23                 intptr_t count,
24                 const char** names,
25                 const char** values) {
26 }
27 
TraceEnd()28 void TraceEnd() {
29 }
30 
TimelineEvent(const char * label,int64_t timestamp0,int64_t timestamp1_or_async_id,Event_Type type,intptr_t argument_count,const char ** argument_names,const char ** argument_values)31 void TimelineEvent(const char* label,
32                    int64_t timestamp0,
33                    int64_t timestamp1_or_async_id,
34                    Event_Type type,
35                    intptr_t argument_count,
36                    const char** argument_names,
37                    const char** argument_values) {
38 }
39 
TraceTimelineEvent(TraceArg category_group,TraceArg name,TraceIDArg identifier,Event_Type type,const std::vector<const char * > & c_names,const std::vector<std::string> & values)40 void TraceTimelineEvent(TraceArg category_group,
41                         TraceArg name,
42                         TraceIDArg identifier,
43                         Event_Type type,
44                         const std::vector<const char*>& c_names,
45                         const std::vector<std::string>& values) {
46   const auto argument_count = std::min(c_names.size(), values.size());
47 
48   std::vector<const char*> c_values;
49   c_values.resize(argument_count, nullptr);
50 
51   for (size_t i = 0; i < argument_count; i++) {
52     c_values[i] = values[i].c_str();
53   }
54 
55   TimelineEvent(name,            // label
56                 0,               // timestamp0
57                 identifier,      // timestamp1_or_async_id
58                 type,            // event type
59                 argument_count,  // argument_count
60                 const_cast<const char**>(c_names.data()),  // argument_names
61                 c_values.data()                            // argument_values
62   );
63 }
64 
TraceEvent0(TraceArg category_group,TraceArg name)65 void TraceEvent0(TraceArg category_group, TraceArg name) {
66   TimelineEvent(name,         // label
67                 0,            // timestamp0
68                 0,            // timestamp1_or_async_id
69                 Event_Begin,  // event type
70                 0,            // argument_count
71                 nullptr,      // argument_names
72                 nullptr       // argument_values
73   );
74 }
75 
TraceEvent1(TraceArg category_group,TraceArg name,TraceArg arg1_name,TraceArg arg1_val)76 void TraceEvent1(TraceArg category_group,
77                  TraceArg name,
78                  TraceArg arg1_name,
79                  TraceArg arg1_val) {
80   const char* arg_names[] = {arg1_name};
81   const char* arg_values[] = {arg1_val};
82   TimelineEvent(name,         // label
83                 0,            // timestamp0
84                 0,            // timestamp1_or_async_id
85                 Event_Begin,  // event type
86                 1,            // argument_count
87                 arg_names,    // argument_names
88                 arg_values    // argument_values
89   );
90 }
91 
TraceEvent2(TraceArg category_group,TraceArg name,TraceArg arg1_name,TraceArg arg1_val,TraceArg arg2_name,TraceArg arg2_val)92 void TraceEvent2(TraceArg category_group,
93                  TraceArg name,
94                  TraceArg arg1_name,
95                  TraceArg arg1_val,
96                  TraceArg arg2_name,
97                  TraceArg arg2_val) {
98   const char* arg_names[] = {arg1_name, arg2_name};
99   const char* arg_values[] = {arg1_val, arg2_val};
100   TimelineEvent(name,         // label
101                 0,            // timestamp0
102                 0,            // timestamp1_or_async_id
103                 Event_Begin,  // event type
104                 2,            // argument_count
105                 arg_names,    // argument_names
106                 arg_values    // argument_values
107   );
108 }
109 
TraceEventEnd(TraceArg name)110 void TraceEventEnd(TraceArg name) {
111   TimelineEvent(name,       // label
112                 0,          // timestamp0
113                 0,          // timestamp1_or_async_id
114                 Event_End,  // event type
115                 0,          // argument_count
116                 nullptr,    // argument_names
117                 nullptr     // argument_values
118   );
119 }
120 
TraceEventAsyncComplete(TraceArg category_group,TraceArg name,TimePoint begin,TimePoint end)121 void TraceEventAsyncComplete(TraceArg category_group,
122                              TraceArg name,
123                              TimePoint begin,
124                              TimePoint end) {
125   auto identifier = TraceNonce();
126 
127   if (begin > end) {
128     std::swap(begin, end);
129   }
130 
131   TimelineEvent(name,                                   // label
132                 begin.ToEpochDelta().ToMicroseconds(),  // timestamp0
133                 identifier,    // timestamp1_or_async_id
134                 Event_UnKnow,  // event type
135                 0,             // argument_count
136                 nullptr,       // argument_names
137                 nullptr        // argument_values
138   );
139   TimelineEvent(name,                                 // label
140                 end.ToEpochDelta().ToMicroseconds(),  // timestamp0
141                 identifier,                           // timestamp1_or_async_id
142                 Event_UnKnow,                         // event type
143                 0,                                    // argument_count
144                 nullptr,                              // argument_names
145                 nullptr                               // argument_values
146   );
147 }
148 
TraceEventAsyncBegin0(TraceArg category_group,TraceArg name,TraceIDArg id)149 void TraceEventAsyncBegin0(TraceArg category_group,
150                            TraceArg name,
151                            TraceIDArg id) {
152   TimelineEvent(name,          // label
153                 0,             // timestamp0
154                 id,            // timestamp1_or_async_id
155                 Event_UnKnow,  // event type
156                 0,             // argument_count
157                 nullptr,       // argument_names
158                 nullptr        // argument_values
159   );
160 }
161 
TraceEventAsyncEnd0(TraceArg category_group,TraceArg name,TraceIDArg id)162 void TraceEventAsyncEnd0(TraceArg category_group,
163                          TraceArg name,
164                          TraceIDArg id) {
165   TimelineEvent(name,          // label
166                 0,             // timestamp0
167                 id,            // timestamp1_or_async_id
168                 Event_UnKnow,  // event type
169                 0,             // argument_count
170                 nullptr,       // argument_names
171                 nullptr        // argument_values
172   );
173 }
174 
TraceEventAsyncBegin1(TraceArg category_group,TraceArg name,TraceIDArg id,TraceArg arg1_name,TraceArg arg1_val)175 void TraceEventAsyncBegin1(TraceArg category_group,
176                            TraceArg name,
177                            TraceIDArg id,
178                            TraceArg arg1_name,
179                            TraceArg arg1_val) {
180   const char* arg_names[] = {arg1_name};
181   const char* arg_values[] = {arg1_val};
182   TimelineEvent(name,          // label
183                 0,             // timestamp0
184                 id,            // timestamp1_or_async_id
185                 Event_UnKnow,  // event type
186                 1,             // argument_count
187                 arg_names,     // argument_names
188                 arg_values     // argument_values
189   );
190 }
191 
TraceEventAsyncEnd1(TraceArg category_group,TraceArg name,TraceIDArg id,TraceArg arg1_name,TraceArg arg1_val)192 void TraceEventAsyncEnd1(TraceArg category_group,
193                          TraceArg name,
194                          TraceIDArg id,
195                          TraceArg arg1_name,
196                          TraceArg arg1_val) {
197   const char* arg_names[] = {arg1_name};
198   const char* arg_values[] = {arg1_val};
199   TimelineEvent(name,          // label
200                 0,             // timestamp0
201                 id,            // timestamp1_or_async_id
202                 Event_UnKnow,  // event type
203                 1,             // argument_count
204                 arg_names,     // argument_names
205                 arg_values     // argument_values
206   );
207 }
208 
TraceEventInstant0(TraceArg category_group,TraceArg name)209 void TraceEventInstant0(TraceArg category_group, TraceArg name) {
210   TimelineEvent(name,          // label
211                 0,             // timestamp0
212                 0,             // timestamp1_or_async_id
213                 Event_UnKnow,  // event type
214                 0,             // argument_count
215                 nullptr,       // argument_names
216                 nullptr        // argument_values
217   );
218 }
219 
TraceEventFlowBegin0(TraceArg category_group,TraceArg name,TraceIDArg id)220 void TraceEventFlowBegin0(TraceArg category_group,
221                           TraceArg name,
222                           TraceIDArg id) {
223   TimelineEvent(name,          // label
224                 0,             // timestamp0
225                 id,            // timestamp1_or_async_id
226                 Event_UnKnow,  // event type
227                 0,             // argument_count
228                 nullptr,       // argument_names
229                 nullptr        // argument_values
230   );
231 }
232 
TraceEventFlowStep0(TraceArg category_group,TraceArg name,TraceIDArg id)233 void TraceEventFlowStep0(TraceArg category_group,
234                          TraceArg name,
235                          TraceIDArg id) {
236   TimelineEvent(name,          // label
237                 0,             // timestamp0
238                 id,            // timestamp1_or_async_id
239                 Event_UnKnow,  // event type
240                 0,             // argument_count
241                 nullptr,       // argument_names
242                 nullptr        // argument_values
243   );
244 }
245 
TraceEventFlowEnd0(TraceArg category_group,TraceArg name,TraceIDArg id)246 void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) {
247   TimelineEvent(name,          // label
248                 0,             // timestamp0
249                 id,            // timestamp1_or_async_id
250                 Event_UnKnow,  // event type
251                 0,             // argument_count
252                 nullptr,       // argument_names
253                 nullptr        // argument_values
254   );
255 }
256 
257 }  // namespace tracing
258 }  // namespace fml
259