1 /*
2 * Copyright (C) 2018 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 #include "tools/trace_to_text/trace_to_systrace.h"
18
19 #include <inttypes.h>
20 #include <stdio.h>
21
22 #include <algorithm>
23 #include <functional>
24 #include <map>
25 #include <memory>
26 #include <utility>
27
28 #include "perfetto/base/build_config.h"
29 #include "perfetto/base/logging.h"
30 #include "perfetto/base/paged_memory.h"
31 #include "perfetto/base/string_writer.h"
32 #include "perfetto/trace_processor/trace_processor.h"
33
34 // When running in Web Assembly, fflush() is a no-op and the stdio buffering
35 // sends progress updates to JS only when a write ends with \n.
36 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
37 #define PROGRESS_CHAR "\n"
38 #else
39 #define PROGRESS_CHAR "\r"
40 #endif
41
42 namespace perfetto {
43 namespace trace_to_text {
44
45 namespace {
46
47 // Having an empty traceEvents object is necessary for trace viewer to
48 // load the json properly.
49 const char kTraceHeader[] = R"({
50 "traceEvents": [],
51 )";
52
53 const char kTraceFooter[] = R"(\n",
54 "controllerTraceDataKey": "systraceController"
55 })";
56
57 const char kProcessDumpHeader[] =
58 ""
59 "\"androidProcessDump\": "
60 "\"PROCESS DUMP\\nUSER PID PPID VSZ RSS WCHAN "
61 "PC S NAME COMM \\n";
62
63 const char kThreadHeader[] = "USER PID TID CMD \\n";
64
65 const char kSystemTraceEvents[] =
66 ""
67 " \"systemTraceEvents\": \"";
68
69 const char kFtraceHeader[] =
70 "# tracer: nop\n"
71 "#\n"
72 "# entries-in-buffer/entries-written: 30624/30624 #P:4\n"
73 "#\n"
74 "# _-----=> irqs-off\n"
75 "# / _----=> need-resched\n"
76 "# | / _---=> hardirq/softirq\n"
77 "# || / _--=> preempt-depth\n"
78 "# ||| / delay\n"
79 "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n"
80 "# | | | | |||| | |\n";
81
82 const char kFtraceJsonHeader[] =
83 "# tracer: nop\\n"
84 "#\\n"
85 "# entries-in-buffer/entries-written: 30624/30624 #P:4\\n"
86 "#\\n"
87 "# _-----=> irqs-off\\n"
88 "# / _----=> need-resched\\n"
89 "# | / _---=> hardirq/softirq\\n"
90 "# || / _--=> preempt-depth\\n"
91 "# ||| / delay\\n"
92 "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\\n"
93 "# | | | | |||| | |\\n";
94
FormatProcess(uint32_t pid,uint32_t ppid,const base::StringView & name,base::StringWriter * writer)95 inline void FormatProcess(uint32_t pid,
96 uint32_t ppid,
97 const base::StringView& name,
98 base::StringWriter* writer) {
99 writer->AppendLiteral("root ");
100 writer->AppendInt(pid);
101 writer->AppendLiteral(" ");
102 writer->AppendInt(ppid);
103 writer->AppendLiteral(" 00000 000 null 0000000000 S ");
104 writer->AppendString(name);
105 writer->AppendLiteral(" null");
106 }
107
FormatThread(uint32_t tid,uint32_t tgid,const base::StringView & name,base::StringWriter * writer)108 inline void FormatThread(uint32_t tid,
109 uint32_t tgid,
110 const base::StringView& name,
111 base::StringWriter* writer) {
112 writer->AppendLiteral("root ");
113 writer->AppendInt(tgid);
114 writer->AppendChar(' ');
115 writer->AppendInt(tid);
116 writer->AppendChar(' ');
117 if (name.empty()) {
118 writer->AppendLiteral("<...>");
119 } else {
120 writer->AppendString(name);
121 }
122 }
123
124 class QueryWriter {
125 public:
QueryWriter(trace_processor::TraceProcessor * tp,std::ostream * output)126 QueryWriter(trace_processor::TraceProcessor* tp, std::ostream* output)
127 : tp_(tp),
128 buffer_(base::PagedMemory::Allocate(kBufferSize)),
129 global_writer_(static_cast<char*>(buffer_.Get()), kBufferSize),
130 output_(output) {}
131
132 template <typename Callback>
RunQuery(const std::string & sql,Callback callback)133 bool RunQuery(const std::string& sql, Callback callback) {
134 char buffer[2048];
135 auto iterator = tp_->ExecuteQuery(sql);
136 for (uint32_t rows = 0; iterator.Next(); rows++) {
137 base::StringWriter line_writer(buffer, base::ArraySize(buffer));
138 callback(&iterator, &line_writer);
139
140 if (global_writer_.pos() + line_writer.pos() >= global_writer_.size()) {
141 fprintf(stderr, "Writing row %" PRIu32 PROGRESS_CHAR, rows);
142 auto str = global_writer_.GetStringView();
143 output_->write(str.data(), static_cast<std::streamsize>(str.size()));
144 global_writer_.reset();
145 }
146 global_writer_.AppendStringView(line_writer.GetStringView());
147 }
148
149 // Check if we have an error in the iterator and print if so.
150 auto opt_error = iterator.GetLastError();
151 if (opt_error.has_value()) {
152 PERFETTO_ELOG("Error while writing systrace %s", opt_error->c_str());
153 return false;
154 }
155
156 // Flush any dangling pieces in the global writer.
157 auto str = global_writer_.GetStringView();
158 output_->write(str.data(), static_cast<std::streamsize>(str.size()));
159 global_writer_.reset();
160 return true;
161 }
162
163 private:
164 static constexpr uint32_t kBufferSize = 1024u * 1024u * 16u;
165
166 trace_processor::TraceProcessor* tp_ = nullptr;
167 base::PagedMemory buffer_;
168 base::StringWriter global_writer_;
169 std::ostream* output_ = nullptr;
170 };
171
172 } // namespace
173
TraceToSystrace(std::istream * input,std::ostream * output,bool wrap_in_json)174 int TraceToSystrace(std::istream* input,
175 std::ostream* output,
176 bool wrap_in_json) {
177 trace_processor::Config config;
178 std::unique_ptr<trace_processor::TraceProcessor> tp =
179 trace_processor::TraceProcessor::CreateInstance(config);
180
181 // 1MB chunk size seems the best tradeoff on a MacBook Pro 2013 - i7 2.8 GHz.
182 constexpr size_t kChunkSize = 1024 * 1024;
183
184 // Printing the status update on stderr can be a perf bottleneck. On WASM print
185 // status updates more frequently because it can be slower to parse each chunk.
186 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
187 constexpr int kStderrRate = 1;
188 #else
189 constexpr int kStderrRate = 128;
190 #endif
191 uint64_t file_size = 0;
192 for (int i = 0;; i++) {
193 if (i % kStderrRate == 0) {
194 fprintf(stderr, "Loading trace %.2f MB" PROGRESS_CHAR, file_size / 1.0e6);
195 fflush(stderr);
196 }
197
198 std::unique_ptr<uint8_t[]> buf(new uint8_t[kChunkSize]);
199 input->read(reinterpret_cast<char*>(buf.get()), kChunkSize);
200 if (input->bad()) {
201 PERFETTO_ELOG("Failed when reading trace");
202 return 1;
203 }
204
205 auto rsize = input->gcount();
206 if (rsize <= 0)
207 break;
208 file_size += static_cast<uint64_t>(rsize);
209 tp->Parse(std::move(buf), static_cast<size_t>(rsize));
210 }
211 tp->NotifyEndOfFile();
212
213 fprintf(stderr, "Loaded trace" PROGRESS_CHAR);
214 fflush(stderr);
215
216 using Iterator = trace_processor::TraceProcessor::Iterator;
217
218 QueryWriter q_writer(tp.get(), output);
219 if (wrap_in_json) {
220 *output << kTraceHeader;
221
222 *output << kProcessDumpHeader;
223
224 // Write out all the processes in the trace.
225 // TODO(lalitm): change this query to actually use ppid when it is exposed
226 // by the process table.
227 static const char kPSql[] = "select pid, 0 as ppid, name from process";
228 auto p_callback = [](Iterator* it, base::StringWriter* writer) {
229 uint32_t pid = static_cast<uint32_t>(it->Get(0 /* col */).long_value);
230 uint32_t ppid = static_cast<uint32_t>(it->Get(1 /* col */).long_value);
231 const char* name = it->Get(2 /* col */).string_value;
232 FormatProcess(pid, ppid, name, writer);
233 };
234 if (!q_writer.RunQuery(kPSql, p_callback))
235 return 1;
236
237 *output << kThreadHeader;
238
239 // Write out all the threads in the trace.
240 static const char kTSql[] =
241 "select tid, COALESCE(upid, 0), thread.name "
242 "from thread inner join process using (upid)";
243 auto t_callback = [](Iterator* it, base::StringWriter* writer) {
244 uint32_t tid = static_cast<uint32_t>(it->Get(0 /* col */).long_value);
245 uint32_t tgid = static_cast<uint32_t>(it->Get(1 /* col */).long_value);
246 const char* name = it->Get(2 /* col */).string_value;
247 FormatThread(tid, tgid, name, writer);
248 };
249 if (!q_writer.RunQuery(kTSql, t_callback))
250 return 1;
251
252 *output << "\",";
253 *output << kSystemTraceEvents;
254 *output << kFtraceJsonHeader;
255 } else {
256 *output << "TRACE:\n";
257 *output << kFtraceHeader;
258 }
259
260 fprintf(stderr, "Converting trace events" PROGRESS_CHAR);
261 fflush(stderr);
262
263 static const char kRawSql[] = "select to_ftrace(id) from raw";
264 auto raw_callback = [wrap_in_json](Iterator* it, base::StringWriter* writer) {
265 const char* line = it->Get(0 /* col */).string_value;
266 if (wrap_in_json) {
267 for (uint32_t i = 0; line[i] != '\0'; i++) {
268 char c = line[i];
269 if (c == '\n') {
270 writer->AppendLiteral("\\n");
271 continue;
272 }
273
274 if (c == '\\' || c == '"')
275 writer->AppendChar('\\');
276 writer->AppendChar(c);
277 }
278 writer->AppendChar('\\');
279 writer->AppendChar('n');
280 } else {
281 writer->AppendString(line);
282 writer->AppendChar('\n');
283 }
284 };
285 if (!q_writer.RunQuery(kRawSql, raw_callback))
286 return 1;
287
288 if (wrap_in_json)
289 *output << kTraceFooter;
290
291 return 0;
292 }
293
294 } // namespace trace_to_text
295 } // namespace perfetto
296