• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_SYSTRACE_SYSTRACE_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_SYSTRACE_SYSTRACE_PARSER_H_
19 
20 #include <ostream>
21 
22 #include "src/trace_processor/types/trace_processor_context.h"
23 
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/string_utils.h"
26 #include "perfetto/ext/base/string_view.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 
29 namespace perfetto {
30 namespace trace_processor {
31 
32 namespace systrace_utils {
33 
34 // Visible for unittesting.
35 enum class SystraceParseResult { kFailure = 0, kUnsupported, kSuccess };
36 
37 // Visible for unittesting.
38 struct SystraceTracePoint {
SystraceTracePointSystraceTracePoint39   SystraceTracePoint() {}
40 
BSystraceTracePoint41   static SystraceTracePoint B(uint32_t tgid, base::StringView name) {
42     return SystraceTracePoint('B', tgid, std::move(name), 0, {});
43   }
44 
ESystraceTracePoint45   static SystraceTracePoint E(uint32_t tgid) {
46     return SystraceTracePoint('E', tgid, {}, 0, {});
47   }
48 
CSystraceTracePoint49   static SystraceTracePoint C(uint32_t tgid,
50                               base::StringView name,
51                               int64_t value) {
52     return SystraceTracePoint('C', tgid, std::move(name), value, {});
53   }
54 
SSystraceTracePoint55   static SystraceTracePoint S(uint32_t tgid,
56                               base::StringView name,
57                               int64_t cookie) {
58     return SystraceTracePoint('S', tgid, std::move(name), cookie, {});
59   }
60 
FSystraceTracePoint61   static SystraceTracePoint F(uint32_t tgid,
62                               base::StringView name,
63                               int64_t cookie) {
64     return SystraceTracePoint('F', tgid, std::move(name), cookie, {});
65   }
66 
ISystraceTracePoint67   static SystraceTracePoint I(uint32_t tgid, base::StringView name) {
68     return SystraceTracePoint('I', tgid, std::move(name), 0, {});
69   }
70 
NSystraceTracePoint71   static SystraceTracePoint N(uint32_t tgid,
72                               base::StringView track_name,
73                               base::StringView name) {
74     return SystraceTracePoint('N', tgid, std::move(name), 0,
75                               std::move(track_name));
76   }
77 
TSystraceTracePoint78   static SystraceTracePoint T(uint32_t tgid,
79                               base::StringView track_name,
80                               base::StringView name,
81                               int64_t cookie) {
82     return SystraceTracePoint('T', tgid, std::move(name), cookie,
83                               std::move(track_name));
84   }
85 
USystraceTracePoint86   static SystraceTracePoint U(uint32_t tgid,
87                               base::StringView track_name,
88                               base::StringView name,
89                               int64_t cookie) {
90     return SystraceTracePoint('U', tgid, std::move(name), cookie,
91                               std::move(track_name));
92   }
93 
SystraceTracePointSystraceTracePoint94   SystraceTracePoint(char p,
95                      uint32_t tg,
96                      base::StringView n,
97                      int64_t v,
98                      base::StringView s)
99       : phase(p), tgid(tg), name(std::move(n)), int_value(v), str_value(s) {}
100 
101   // Phase can be one of B, E, C, S, F, I, N, T, U.
102   char phase = '\0';
103 
104   uint32_t tgid = 0;
105 
106   // For phase = B, C, S, F, N, U, T, U.
107   base::StringView name;
108 
109   // For phase = C (counter value) and B, S, F, N, T, U (async cookie).
110   int64_t int_value = 0;
111 
112   // For phase = N, T, U (track name)
113   base::StringView str_value;
114 
115   // Visible for unittesting.
116   friend std::ostream& operator<<(std::ostream& os,
117                                   const SystraceTracePoint& point) {
118     return os << "SystraceTracePoint{'" << point.phase << "', " << point.tgid
119               << ", \"" << point.name.ToStdString() << "\", " << point.int_value
120               << ", \"" << point.str_value.ToStdString() << "\""
121               << "}";
122   }
123 };
124 
125 // We have to handle trace_marker events of a few different types:
126 // 1.   some random text
127 // 2.   B|1636|pokeUserActivity
128 // 3.   E|1636
129 // 4.   C|1636|wq:monitor|0
130 // 5.   S|1636|frame_capture|123
131 // 6.   F|1636|frame_capture|456
132 // 7.   C|3209|TransfersBytesPendingOnDisk-value|0|Blob
133 // 8.   I|4820|instant
134 // 9.   N|1938|track_name|instant_name
135 // 10.  T|1339|track_name|slice_name|789
136 // 11.  U|6890|track_name|slice_name|135
137 // Counters emitted by chromium can have a further "category group" appended
138 // ("Blob" in the example below). We ignore the category group.
ParseSystraceTracePoint(base::StringView str_untrimmed,SystraceTracePoint * out)139 inline SystraceParseResult ParseSystraceTracePoint(
140     base::StringView str_untrimmed,
141     SystraceTracePoint* out) {
142   *out = {};
143 
144   // Strip trailing \n and \0. StringViews are not null-terminated, but the
145   // writer could have appended a stray \0 depending on where the trace comes
146   // from.
147   size_t len = str_untrimmed.size();
148   for (; len > 0; --len) {
149     char last_char = str_untrimmed.at(len - 1);
150     if (last_char != '\n' && last_char != '\0')
151       break;
152   }
153   base::StringView str = str_untrimmed.substr(0, len);
154 
155   size_t off = 0;
156 
157   // This function reads the next field up to the next '|', '\0' or end(). It
158   // advances |off| as it goes through fields.
159   auto read_next_field = [&off, &str, len]() {
160     for (size_t field_start = off;; ++off) {
161       char c = off >= len ? '\0' : str.at(off);
162       if (c == '|' || c == '\0') {
163         auto res = str.substr(field_start, off - field_start);
164         ++off;  // Eat the separator.
165         return res;
166       }
167     }
168   };
169 
170   auto f0_phase = read_next_field();
171   if (PERFETTO_UNLIKELY(f0_phase.empty()))
172     return SystraceParseResult::kFailure;
173   out->phase = f0_phase.at(0);
174 
175   auto f1_tgid = read_next_field();
176   auto opt_tgid = base::StringToUInt32(f1_tgid.ToStdString());
177   out->tgid = opt_tgid.value_or(0);
178   const bool has_tgid = opt_tgid.has_value();
179 
180   switch (out->phase) {
181     case 'B': {  // Begin thread-scoped synchronous slice.
182       if (!has_tgid)
183         return SystraceParseResult::kFailure;
184       auto f2_name = str.substr(off);  // It's fine even if |off| >= end().
185       if (f2_name.empty()) {
186         out->name = base::StringView("[empty slice name]");
187       } else {
188         out->name = f2_name;
189       }
190       return SystraceParseResult::kSuccess;
191     }
192     case 'E':  // End thread-scoped synchronous slice.
193       // Some non-Android traces (Flutter) use just "E" (aosp/1244409). Allow
194       // empty TGID on end slices. By design they are thread-scoped anyways.
195       return SystraceParseResult::kSuccess;
196     case 'S':    // Begin of async slice.
197     case 'F': {  // End of async slice.
198       auto f2_name = read_next_field();
199       auto f3_cookie = read_next_field();
200       auto maybe_cookie = base::StringToInt64(f3_cookie.ToStdString());
201       if (PERFETTO_UNLIKELY(!has_tgid || f2_name.empty() || f3_cookie.empty() ||
202                             !maybe_cookie)) {
203         return SystraceParseResult::kFailure;
204       }
205       out->name = f2_name;
206       out->int_value = *maybe_cookie;
207       return SystraceParseResult::kSuccess;
208     }
209     case 'I': {  // Instant.
210       auto f2_name = read_next_field();
211       if (PERFETTO_UNLIKELY(!has_tgid || f2_name.empty())) {
212         return SystraceParseResult::kFailure;
213       }
214       out->name = f2_name;
215       return SystraceParseResult::kSuccess;
216     }
217     case 'N': {  // Instant on track.
218       auto f2_track_name = read_next_field();
219       auto f3_name = read_next_field();
220       if (PERFETTO_UNLIKELY(!has_tgid || f2_track_name.empty() ||
221                             f3_name.empty())) {
222         return SystraceParseResult::kFailure;
223       }
224       out->name = f3_name;
225       out->str_value = f2_track_name;
226       return SystraceParseResult::kSuccess;
227     }
228     case 'C': {  // Counter.
229       auto f2_name = read_next_field();
230       auto f3_value = read_next_field();
231       auto maybe_value = base::StringToInt64(f3_value.ToStdString());
232       if (PERFETTO_UNLIKELY(!has_tgid || f2_name.empty() || f3_value.empty() ||
233                             !maybe_value)) {
234         return SystraceParseResult::kFailure;
235       }
236       out->name = f2_name;
237       out->int_value = *maybe_value;
238       return SystraceParseResult::kSuccess;
239     }
240     case 'T':    // Begin of async slice on track.
241     case 'U': {  // End of async slice on track.
242       auto f2_track_name = read_next_field();
243       auto f3_name = read_next_field();
244       auto maybe_cookie = base::StringToInt64(read_next_field().ToStdString());
245       if (PERFETTO_UNLIKELY(!has_tgid || f2_track_name.empty() ||
246                             f3_name.empty() || !maybe_cookie)) {
247         return SystraceParseResult::kFailure;
248       }
249       out->name = f3_name;
250       out->str_value = f2_track_name;
251       out->int_value = *maybe_cookie;
252       return SystraceParseResult::kSuccess;
253     }
254     default:
255       if (str.find("trace_event_clock_sync:") == 0)
256         return SystraceParseResult::kUnsupported;
257       return SystraceParseResult::kFailure;
258   }
259 }
260 
261 // Visible for unittesting.
262 inline bool operator==(const SystraceTracePoint& x,
263                        const SystraceTracePoint& y) {
264   return std::tie(x.phase, x.tgid, x.name, x.int_value, x.str_value) ==
265          std::tie(y.phase, y.tgid, y.name, y.int_value, y.str_value);
266 }
267 
268 }  // namespace systrace_utils
269 
270 class SystraceParser : public Destructible {
271  public:
GetOrCreate(TraceProcessorContext * context)272   static SystraceParser* GetOrCreate(TraceProcessorContext* context) {
273     if (!context->systrace_parser) {
274       context->systrace_parser.reset(new SystraceParser(context));
275     }
276     return static_cast<SystraceParser*>(context->systrace_parser.get());
277   }
278   ~SystraceParser() override;
279 
280   void ParsePrintEvent(int64_t ts, uint32_t pid, base::StringView event);
281 
282   void ParseTracingMarkWrite(int64_t ts,
283                              uint32_t pid,
284                              char trace_type,
285                              bool trace_begin,
286                              base::StringView trace_name,
287                              uint32_t tgid,
288                              int64_t value);
289 
290   void ParseZeroEvent(int64_t ts,
291                       uint32_t pid,
292                       int32_t flag,
293                       base::StringView name,
294                       uint32_t tgid,
295                       int64_t value);
296 
297  private:
298   explicit SystraceParser(TraceProcessorContext*);
299   void ParseSystracePoint(int64_t ts,
300                           uint32_t pid,
301                           systrace_utils::SystraceTracePoint event);
302   void PostProcessSpecialSliceBegin(int64_t ts, base::StringView name);
303 
304   TraceProcessorContext* const context_;
305   const StringId lmk_id_;
306   const StringId oom_score_adj_id_;
307   const StringId screen_state_id_;
308   const StringId cookie_id_;
309 };
310 
311 }  // namespace trace_processor
312 }  // namespace perfetto
313 
314 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_SYSTRACE_SYSTRACE_PARSER_H_
315