Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 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 <inttypes.h> |
| 18 | |
| 19 | #include <stdio.h> |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 20 | #include <sys/ioctl.h> |
| 21 | #include <unistd.h> |
| 22 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <fstream> |
Lalit Maganti | 910852d | 2018-03-28 13:16:45 +0100 | [diff] [blame] | 25 | #include <functional> |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 26 | #include <iostream> |
| 27 | #include <istream> |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 28 | #include <limits> |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <memory> |
| 31 | #include <ostream> |
| 32 | #include <sstream> |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 33 | #include <unordered_map> |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 34 | #include <utility> |
| 35 | |
| 36 | #include <google/protobuf/compiler/importer.h> |
| 37 | #include <google/protobuf/dynamic_message.h> |
| 38 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 39 | #include <google/protobuf/text_format.h> |
| 40 | #include <google/protobuf/util/field_comparator.h> |
| 41 | #include <google/protobuf/util/message_differencer.h> |
| 42 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 43 | #include "perfetto/base/build_config.h" |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 44 | #include "perfetto/base/logging.h" |
Hector Dearman | f3f8b8f | 2018-04-30 15:00:51 +0100 | [diff] [blame] | 45 | #include "perfetto/trace/ftrace/ftrace_stats.pb.h" |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 46 | #include "perfetto/trace/trace.pb.h" |
| 47 | #include "perfetto/trace/trace_packet.pb.h" |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 48 | #include "perfetto/traced/sys_stats_counters.h" |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 49 | #include "tools/trace_to_text/ftrace_event_formatter.h" |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 50 | #include "tools/trace_to_text/ftrace_inode_handler.h" |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 51 | #include "tools/trace_to_text/process_formatter.h" |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 52 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 53 | // When running in Web Assembly, fflush() is a no-op and the stdio buffering |
| 54 | // sends progress updates to JS only when a write ends with \n. |
| 55 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM) |
| 56 | #define PROGRESS_CHAR "\n" |
| 57 | #else |
| 58 | #define PROGRESS_CHAR "\r" |
| 59 | #endif |
| 60 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 61 | namespace perfetto { |
| 62 | namespace { |
| 63 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 64 | using google::protobuf::Descriptor; |
| 65 | using google::protobuf::DynamicMessageFactory; |
| 66 | using google::protobuf::FileDescriptor; |
| 67 | using google::protobuf::Message; |
| 68 | using google::protobuf::TextFormat; |
| 69 | using google::protobuf::compiler::DiskSourceTree; |
| 70 | using google::protobuf::compiler::Importer; |
| 71 | using google::protobuf::compiler::MultiFileErrorCollector; |
| 72 | using google::protobuf::io::OstreamOutputStream; |
| 73 | |
| 74 | using protos::FtraceEvent; |
| 75 | using protos::FtraceEventBundle; |
| 76 | using protos::InodeFileMap; |
| 77 | using protos::PrintFtraceEvent; |
| 78 | using protos::ProcessTree; |
| 79 | using protos::Trace; |
| 80 | using protos::TracePacket; |
| 81 | using protos::FtraceStats; |
| 82 | using protos::FtraceStats_Phase_START_OF_TRACE; |
| 83 | using protos::FtraceStats_Phase_END_OF_TRACE; |
| 84 | using protos::SysStats; |
| 85 | using Entry = protos::InodeFileMap::Entry; |
| 86 | using Process = protos::ProcessTree::Process; |
| 87 | |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 88 | // Having an empty traceEvents object is necessary for trace viewer to |
| 89 | // load the json properly. |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 90 | const char kTraceHeader[] = R"({ |
| 91 | "traceEvents": [], |
| 92 | )"; |
| 93 | |
| 94 | const char kTraceFooter[] = R"(\n", |
| 95 | "controllerTraceDataKey": "systraceController" |
| 96 | })"; |
| 97 | |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 98 | const char kProcessDumpHeader[] = |
| 99 | "" |
| 100 | "\"androidProcessDump\": " |
| 101 | "\"PROCESS DUMP\\nUSER PID PPID VSZ RSS WCHAN " |
| 102 | "PC S NAME COMM \\n"; |
| 103 | |
| 104 | const char kThreadHeader[] = "USER PID TID CMD \\n"; |
| 105 | |
Isabelle Taylor | 1ebefdc | 2018-10-18 11:02:53 +0100 | [diff] [blame] | 106 | const char kSystemTraceEvents[] = |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 107 | "" |
Isabelle Taylor | 1ebefdc | 2018-10-18 11:02:53 +0100 | [diff] [blame] | 108 | " \"systemTraceEvents\": \""; |
| 109 | |
| 110 | const char kFtraceHeader[] = |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 111 | "# tracer: nop\\n" |
| 112 | "#\\n" |
| 113 | "# entries-in-buffer/entries-written: 30624/30624 #P:4\\n" |
| 114 | "#\\n" |
| 115 | "# _-----=> irqs-off\\n" |
| 116 | "# / _----=> need-resched\\n" |
| 117 | "# | / _---=> hardirq/softirq\\n" |
| 118 | "# || / _--=> preempt-depth\\n" |
| 119 | "# ||| / delay\\n" |
| 120 | "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\\n" |
| 121 | "# | | | | |||| | |\\n"; |
| 122 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 123 | bool g_output_is_tty = false; |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 124 | |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 125 | size_t GetWidth() { |
| 126 | if (!isatty(STDOUT_FILENO)) |
| 127 | return 80; |
| 128 | struct winsize win_size; |
| 129 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &win_size); |
| 130 | return win_size.ws_col; |
| 131 | } |
| 132 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 133 | class MFE : public MultiFileErrorCollector { |
| 134 | virtual void AddError(const std::string& filename, |
| 135 | int line, |
| 136 | int column, |
| 137 | const std::string& message) { |
| 138 | PERFETTO_ELOG("Error %s %d:%d: %s", filename.c_str(), line, column, |
| 139 | message.c_str()); |
| 140 | } |
| 141 | |
| 142 | virtual void AddWarning(const std::string& filename, |
| 143 | int line, |
| 144 | int column, |
| 145 | const std::string& message) { |
| 146 | PERFETTO_ELOG("Error %s %d:%d: %s", filename.c_str(), line, column, |
| 147 | message.c_str()); |
| 148 | } |
| 149 | }; |
| 150 | |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 151 | void ForEachPacketInTrace( |
| 152 | std::istream* input, |
| 153 | const std::function<void(const protos::TracePacket&)>& f) { |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 154 | size_t bytes_processed = 0; |
| 155 | // The trace stream can be very large. We cannot just pass it in one go to |
| 156 | // libprotobuf as that will refuse to parse messages > 64MB. However we know |
| 157 | // that a trace is merely a sequence of TracePackets. Here we just manually |
| 158 | // tokenize the repeated TracePacket messages and parse them individually |
| 159 | // using libprotobuf. |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 160 | for (uint32_t i = 0;; i++) { |
| 161 | if ((i & 0x3f) == 0) { |
| 162 | fprintf(stderr, "Processing trace: %8zu KB" PROGRESS_CHAR, |
| 163 | bytes_processed / 1024); |
| 164 | fflush(stderr); |
| 165 | } |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 166 | // A TracePacket consists in one byte stating its field id and type ... |
| 167 | char preamble; |
| 168 | input->get(preamble); |
| 169 | if (!input->good()) |
| 170 | break; |
| 171 | bytes_processed++; |
| 172 | PERFETTO_DCHECK(preamble == 0x0a); // Field ID:1, type:length delimited. |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 173 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 174 | // ... a varint stating its size ... |
| 175 | uint32_t field_size = 0; |
| 176 | uint32_t shift = 0; |
| 177 | for (;;) { |
| 178 | char c = 0; |
| 179 | input->get(c); |
| 180 | field_size |= static_cast<uint32_t>(c & 0x7f) << shift; |
| 181 | shift += 7; |
| 182 | bytes_processed++; |
| 183 | if (!(c & 0x80)) |
| 184 | break; |
| 185 | } |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 186 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 187 | // ... and the actual TracePacket itself. |
| 188 | std::unique_ptr<char[]> buf(new char[field_size]); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 189 | input->read(buf.get(), static_cast<std::streamsize>(field_size)); |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 190 | bytes_processed += field_size; |
| 191 | |
| 192 | protos::TracePacket packet; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 193 | auto res = packet.ParseFromArray(buf.get(), static_cast<int>(field_size)); |
Primiano Tucci | de2476b | 2018-08-23 00:53:20 +0200 | [diff] [blame] | 194 | if (!res) { |
| 195 | PERFETTO_ELOG("Skipping invalid packet"); |
| 196 | continue; |
| 197 | } |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 198 | f(packet); |
| 199 | } |
| 200 | } |
| 201 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 202 | int TraceToSystrace(std::istream* input, |
| 203 | std::ostream* output, |
| 204 | bool wrap_in_json) { |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 205 | std::multimap<uint64_t, std::string> ftrace_sorted; |
| 206 | std::vector<std::string> proc_dump; |
| 207 | std::vector<std::string> thread_dump; |
| 208 | std::unordered_map<uint32_t /*tid*/, uint32_t /*tgid*/> thread_map; |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 209 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 210 | std::vector<const char*> meminfo_strs = BuildMeminfoCounterNames(); |
| 211 | std::vector<const char*> vmstat_strs = BuildVmstatCounterNames(); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 212 | |
Isabelle Taylor | 543a7e6 | 2018-10-16 16:17:30 +0100 | [diff] [blame] | 213 | std::vector<protos::TracePacket> packets_to_process; |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 214 | |
| 215 | ForEachPacketInTrace( |
| 216 | input, [&thread_map, &packets_to_process, &proc_dump, |
| 217 | &thread_dump](const protos::TracePacket& packet) { |
| 218 | if (!packet.has_process_tree()) { |
| 219 | packets_to_process.emplace_back(std::move(packet)); |
| 220 | return; |
| 221 | } |
| 222 | const ProcessTree& process_tree = packet.process_tree(); |
| 223 | for (const auto& process : process_tree.processes()) { |
| 224 | // Main threads will have the same pid as tgid. |
| 225 | thread_map[static_cast<uint32_t>(process.pid())] = |
| 226 | static_cast<uint32_t>(process.pid()); |
| 227 | std::string p = FormatProcess(process); |
| 228 | proc_dump.emplace_back(p); |
| 229 | } |
| 230 | for (const auto& thread : process_tree.threads()) { |
| 231 | // Populate thread map for matching tids to tgids. |
| 232 | thread_map[static_cast<uint32_t>(thread.tid())] = |
| 233 | static_cast<uint32_t>(thread.tgid()); |
| 234 | std::string t = FormatThread(thread); |
| 235 | thread_dump.emplace_back(t); |
| 236 | } |
| 237 | }); |
| 238 | |
| 239 | for (const auto& packet : packets_to_process) { |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 240 | if (packet.has_ftrace_events()) { |
| 241 | const FtraceEventBundle& bundle = packet.ftrace_events(); |
| 242 | for (const FtraceEvent& event : bundle.event()) { |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 243 | std::string line = FormatFtraceEvent(event.timestamp(), bundle.cpu(), |
| 244 | event, thread_map); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 245 | if (line == "") |
| 246 | continue; |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 247 | ftrace_sorted.emplace(event.timestamp(), line); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 248 | } |
| 249 | } // packet.has_ftrace_events |
| 250 | |
| 251 | if (packet.has_sys_stats()) { |
| 252 | const SysStats& sys_stats = packet.sys_stats(); |
| 253 | for (const auto& meminfo : sys_stats.meminfo()) { |
| 254 | FtraceEvent event; |
| 255 | uint64_t ts = static_cast<uint64_t>(packet.timestamp()); |
| 256 | char str[256]; |
| 257 | event.set_timestamp(ts); |
| 258 | event.set_pid(1); |
| 259 | sprintf(str, "C|1|%s|%" PRIu64, meminfo_strs[meminfo.key()], |
| 260 | static_cast<uint64_t>(meminfo.value())); |
| 261 | event.mutable_print()->set_buf(str); |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 262 | ftrace_sorted.emplace(ts, FormatFtraceEvent(ts, 0, event, thread_map)); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 263 | } |
| 264 | for (const auto& vmstat : sys_stats.vmstat()) { |
| 265 | FtraceEvent event; |
| 266 | uint64_t ts = static_cast<uint64_t>(packet.timestamp()); |
| 267 | char str[256]; |
| 268 | event.set_timestamp(ts); |
| 269 | event.set_pid(1); |
| 270 | sprintf(str, "C|1|%s|%" PRIu64, vmstat_strs[vmstat.key()], |
| 271 | static_cast<uint64_t>(vmstat.value())); |
| 272 | event.mutable_print()->set_buf(str); |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 273 | ftrace_sorted.emplace(ts, FormatFtraceEvent(ts, 0, event, thread_map)); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 274 | } |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 275 | } |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 276 | } |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 277 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 278 | if (wrap_in_json) { |
| 279 | *output << kTraceHeader; |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 280 | *output << kProcessDumpHeader; |
| 281 | for (const auto& process : proc_dump) { |
| 282 | *output << process << "\\n"; |
| 283 | } |
| 284 | *output << kThreadHeader; |
| 285 | for (const auto& thread : thread_dump) { |
| 286 | *output << thread << "\\n"; |
| 287 | } |
| 288 | *output << "\","; |
Isabelle Taylor | 1ebefdc | 2018-10-18 11:02:53 +0100 | [diff] [blame] | 289 | *output << kSystemTraceEvents; |
| 290 | *output << kFtraceHeader; |
| 291 | } else { |
| 292 | *output << "TRACE:\n"; |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 293 | *output << kFtraceHeader; |
| 294 | } |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 295 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 296 | fprintf(stderr, "\n"); |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 297 | size_t total_events = ftrace_sorted.size(); |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 298 | size_t written_events = 0; |
Isabelle Taylor | 0a6b4c7 | 2018-10-15 11:30:52 +0100 | [diff] [blame] | 299 | for (auto it = ftrace_sorted.begin(); it != ftrace_sorted.end(); it++) { |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 300 | *output << it->second; |
| 301 | *output << (wrap_in_json ? "\\n" : "\n"); |
| 302 | if (!g_output_is_tty && (written_events++ % 1000 == 0 || |
| 303 | written_events == ftrace_sorted.size())) { |
| 304 | fprintf(stderr, "Writing trace: %.2f %%" PROGRESS_CHAR, |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 305 | written_events * 100.0 / total_events); |
| 306 | fflush(stderr); |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 307 | output->flush(); |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 310 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 311 | if (wrap_in_json) |
| 312 | *output << kTraceFooter; |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 317 | int TraceToText(std::istream* input, std::ostream* output) { |
| 318 | DiskSourceTree dst; |
| 319 | dst.MapPath("perfetto", "protos/perfetto"); |
| 320 | MFE mfe; |
| 321 | Importer importer(&dst, &mfe); |
| 322 | const FileDescriptor* parsed_file = |
| 323 | importer.Import("perfetto/trace/trace.proto"); |
| 324 | |
| 325 | DynamicMessageFactory dmf; |
| 326 | const Descriptor* trace_descriptor = parsed_file->message_type(0); |
| 327 | const Message* msg_root = dmf.GetPrototype(trace_descriptor); |
| 328 | Message* msg = msg_root->New(); |
| 329 | |
| 330 | if (!msg->ParseFromIstream(input)) { |
| 331 | PERFETTO_ELOG("Could not parse input."); |
| 332 | return 1; |
| 333 | } |
| 334 | OstreamOutputStream zero_copy_output(output); |
| 335 | TextFormat::Print(*msg, &zero_copy_output); |
| 336 | return 0; |
| 337 | } |
| 338 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 339 | void PrintFtraceTrack(std::ostream* output, |
| 340 | const uint64_t& start, |
| 341 | const uint64_t& end, |
| 342 | const std::multiset<uint64_t>& ftrace_timestamps) { |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 343 | constexpr char kFtraceTrackName[] = "ftrace "; |
| 344 | size_t width = GetWidth(); |
| 345 | size_t bucket_count = width - strlen(kFtraceTrackName); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 346 | size_t bucket_size = static_cast<size_t>(end - start) / bucket_count; |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 347 | size_t max = 0; |
| 348 | std::vector<size_t> buckets(bucket_count); |
| 349 | for (size_t i = 0; i < bucket_count; i++) { |
| 350 | auto low = ftrace_timestamps.lower_bound(i * bucket_size + start); |
| 351 | auto high = ftrace_timestamps.upper_bound((i + 1) * bucket_size + start); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 352 | buckets[i] = static_cast<size_t>(std::distance(low, high)); |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 353 | max = std::max(max, buckets[i]); |
| 354 | } |
| 355 | |
| 356 | std::vector<std::string> out = |
| 357 | std::vector<std::string>({" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇"}); |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 358 | *output << "-------------------- " << kFtraceTrackName |
| 359 | << "--------------------\n"; |
| 360 | char line[2048]; |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 361 | for (size_t i = 0; i < bucket_count; i++) { |
| 362 | sprintf( |
| 363 | line, "%s", |
| 364 | out[std::min(buckets[i] / (max / out.size()), out.size() - 1)].c_str()); |
| 365 | *output << std::string(line); |
| 366 | } |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 367 | *output << "\n\n"; |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 368 | } |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 369 | |
Lalit Maganti | c35fcd5 | 2018-04-20 15:52:35 +0100 | [diff] [blame] | 370 | void PrintFtraceStats(std::ostream* output, |
| 371 | uint64_t overwrite_count, |
Lalit Maganti | 13c6177 | 2018-04-23 12:51:48 +0100 | [diff] [blame] | 372 | std::map<FtraceEvent::EventCase, uint64_t> event_counts, |
Hector Dearman | f3f8b8f | 2018-04-30 15:00:51 +0100 | [diff] [blame] | 373 | const FtraceStats& before_stats, |
| 374 | const FtraceStats& after_stats, |
Lalit Maganti | c35fcd5 | 2018-04-20 15:52:35 +0100 | [diff] [blame] | 375 | bool compact_output) { |
| 376 | if (!compact_output) |
| 377 | *output << "--------------------Ftrace Stats-------------------\n"; |
| 378 | |
| 379 | char line[2048]; |
| 380 | if (compact_output) { |
| 381 | sprintf(line, "ftrace_overwrite_count,%" PRIu64 "\n", overwrite_count); |
| 382 | } else { |
| 383 | sprintf(line, "Events overwritten: %" PRIu64 "\n", overwrite_count); |
| 384 | } |
| 385 | *output << std::string(line); |
| 386 | |
Lalit Maganti | 13c6177 | 2018-04-23 12:51:48 +0100 | [diff] [blame] | 387 | DiskSourceTree dst; |
| 388 | dst.MapPath("perfetto", "protos/perfetto"); |
| 389 | MFE mfe; |
| 390 | Importer importer(&dst, &mfe); |
| 391 | const FileDescriptor* parsed_file = |
| 392 | importer.Import("perfetto/trace/ftrace/ftrace_event.proto"); |
| 393 | |
| 394 | DynamicMessageFactory dmf; |
| 395 | const Descriptor* ftrace_descriptor = parsed_file->message_type(0); |
| 396 | for (const auto& event_to_count : event_counts) { |
| 397 | const std::string& event_name = |
| 398 | ftrace_descriptor->FindFieldByNumber(event_to_count.first)->name(); |
| 399 | uint64_t count = event_to_count.second; |
| 400 | if (compact_output) { |
| 401 | sprintf(line, "%s,%" PRIu64 "\n", event_name.c_str(), count); |
| 402 | } else { |
| 403 | sprintf(line, "%s count: %" PRIu64 "\n", event_name.c_str(), count); |
| 404 | } |
| 405 | *output << std::string(line); |
| 406 | } |
| 407 | |
Hector Dearman | f3f8b8f | 2018-04-30 15:00:51 +0100 | [diff] [blame] | 408 | uint64_t before_total_overrun = 0; |
| 409 | uint64_t after_total_overrun = 0; |
| 410 | for (const auto& cpu_stats : before_stats.cpu_stats()) { |
| 411 | before_total_overrun += cpu_stats.overrun(); |
| 412 | } |
| 413 | for (const auto& cpu_stats : after_stats.cpu_stats()) { |
| 414 | after_total_overrun += cpu_stats.overrun(); |
| 415 | } |
| 416 | |
| 417 | if (compact_output) { |
| 418 | sprintf(line, "total_overrun,%" PRIu64 "\n", |
| 419 | after_total_overrun - before_total_overrun); |
| 420 | } else { |
| 421 | sprintf(line, "total_overrun: %" PRIu64 " (= %" PRIu64 " - %" PRIu64 ")\n", |
| 422 | after_total_overrun - before_total_overrun, after_total_overrun, |
| 423 | before_total_overrun); |
| 424 | } |
| 425 | *output << std::string(line); |
| 426 | |
Lalit Maganti | c35fcd5 | 2018-04-20 15:52:35 +0100 | [diff] [blame] | 427 | if (!compact_output) |
| 428 | *output << "\n"; |
| 429 | } |
| 430 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 431 | void PrintInodeStats(std::ostream* output, |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 432 | const std::set<uint64_t>& ftrace_inodes, |
| 433 | const uint64_t& ftrace_inode_count, |
| 434 | const std::set<uint64_t>& resolved_map_inodes, |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 435 | const std::set<uint64_t>& resolved_scan_inodes, |
| 436 | bool compact_output) { |
| 437 | if (!compact_output) |
| 438 | *output << "--------------------Inode Stats-------------------\n"; |
| 439 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 440 | char line[2048]; |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 441 | if (compact_output) { |
| 442 | sprintf(line, "events_inodes,%" PRIu64 "\n", ftrace_inode_count); |
| 443 | } else { |
| 444 | sprintf(line, "Events with inodes: %" PRIu64 "\n", ftrace_inode_count); |
| 445 | } |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 446 | *output << std::string(line); |
| 447 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 448 | if (compact_output) { |
| 449 | sprintf(line, "events_unique_inodes,%zu\n", ftrace_inodes.size()); |
| 450 | } else { |
| 451 | sprintf(line, "Unique inodes from events: %zu\n", ftrace_inodes.size()); |
| 452 | } |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 453 | *output << std::string(line); |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 454 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 455 | if (compact_output) { |
| 456 | sprintf(line, "resolved_inodes_static,%zu\n", resolved_map_inodes.size()); |
| 457 | } else { |
| 458 | sprintf(line, "Resolved inodes from static map: %zu\n", |
| 459 | resolved_map_inodes.size()); |
| 460 | } |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 461 | *output << std::string(line); |
| 462 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 463 | if (compact_output) { |
| 464 | sprintf(line, "resolved_inodes_scan_cache,%zu\n", |
| 465 | resolved_scan_inodes.size()); |
| 466 | } else { |
| 467 | sprintf(line, "Resolved inodes from scan and cache: %zu\n", |
| 468 | resolved_scan_inodes.size()); |
| 469 | } |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 470 | *output << std::string(line); |
| 471 | |
| 472 | std::set<uint64_t> resolved_inodes; |
| 473 | set_union(resolved_map_inodes.begin(), resolved_map_inodes.end(), |
| 474 | resolved_scan_inodes.begin(), resolved_scan_inodes.end(), |
| 475 | std::inserter(resolved_inodes, resolved_inodes.begin())); |
| 476 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 477 | if (compact_output) { |
| 478 | sprintf(line, "total_resolved_inodes,%zu\n", resolved_inodes.size()); |
| 479 | } else { |
| 480 | sprintf(line, "Total resolved inodes: %zu\n", resolved_inodes.size()); |
| 481 | } |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 482 | *output << std::string(line); |
| 483 | |
| 484 | std::set<uint64_t> intersect; |
| 485 | set_intersection(resolved_inodes.begin(), resolved_inodes.end(), |
| 486 | ftrace_inodes.begin(), ftrace_inodes.end(), |
| 487 | std::inserter(intersect, intersect.begin())); |
| 488 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 489 | size_t unresolved_inodes = ftrace_inodes.size() - intersect.size(); |
| 490 | if (compact_output) { |
| 491 | sprintf(line, "unresolved_inodes,%zu\n", unresolved_inodes); |
| 492 | } else { |
| 493 | sprintf(line, "Unresolved inodes: %zu\n", unresolved_inodes); |
| 494 | } |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 495 | *output << std::string(line); |
| 496 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 497 | size_t unexpected_inodes = resolved_inodes.size() - intersect.size(); |
| 498 | if (compact_output) { |
| 499 | sprintf(line, "unexpected_inodes_fs,%zu\n", unexpected_inodes); |
| 500 | } else { |
| 501 | sprintf(line, "Unexpected inodes from filesystem: %zu\n", |
| 502 | unexpected_inodes); |
| 503 | } |
| 504 | *output << std::string(line); |
| 505 | |
| 506 | if (!compact_output) |
| 507 | *output << "\n"; |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void PrintProcessStats(std::ostream* output, |
| 511 | const std::set<pid_t>& tids_in_tree, |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 512 | const std::set<pid_t>& tids_in_events, |
| 513 | bool compact_output) { |
| 514 | if (!compact_output) |
| 515 | *output << "----------------Process Tree Stats----------------\n"; |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 516 | |
| 517 | char tid[2048]; |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 518 | if (compact_output) { |
| 519 | sprintf(tid, "unique_thread_process,%zu\n", tids_in_tree.size()); |
| 520 | } else { |
| 521 | sprintf(tid, "Unique thread ids in process tree: %zu\n", |
| 522 | tids_in_tree.size()); |
| 523 | } |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 524 | *output << std::string(tid); |
| 525 | |
| 526 | char tid_event[2048]; |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 527 | if (compact_output) { |
| 528 | sprintf(tid_event, "unique_thread_ftrace,%zu\n", tids_in_events.size()); |
| 529 | } else { |
| 530 | sprintf(tid_event, "Unique thread ids in ftrace events: %zu\n", |
| 531 | tids_in_events.size()); |
| 532 | } |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 533 | *output << std::string(tid_event); |
| 534 | |
| 535 | std::set<pid_t> intersect; |
| 536 | set_intersection(tids_in_tree.begin(), tids_in_tree.end(), |
| 537 | tids_in_events.begin(), tids_in_events.end(), |
| 538 | std::inserter(intersect, intersect.begin())); |
| 539 | |
| 540 | char matching[2048]; |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 541 | size_t thread_id_process_info = |
| 542 | (intersect.size() * 100) / tids_in_events.size(); |
| 543 | if (compact_output) { |
| 544 | sprintf(matching, |
| 545 | "tids_with_pinfo,%zu\ntids,%zu\ntids_with_pinfo_percentage,%zu\n", |
| 546 | intersect.size(), tids_in_events.size(), thread_id_process_info); |
| 547 | } else { |
Lalit Maganti | 13c6177 | 2018-04-23 12:51:48 +0100 | [diff] [blame] | 548 | sprintf(matching, "Thread ids with process info: %zu/%zu -> %zu %%\n", |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 549 | intersect.size(), tids_in_events.size(), thread_id_process_info); |
| 550 | } |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 551 | *output << std::string(matching); |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 552 | |
| 553 | if (!compact_output) |
| 554 | *output << "\n"; |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 557 | void PrintTraceStats(std::ostream* output, |
| 558 | const protos::TraceStats& stats, |
| 559 | bool compact_output) { |
| 560 | if (compact_output) |
| 561 | return; |
| 562 | *output << "--------------------Trace Stats-------------------\n"; |
| 563 | size_t buf_num = 0; |
| 564 | for (const auto& buf : stats.buffer_stats()) { |
| 565 | *output << "Buffer " << buf_num++ << "\n" |
| 566 | << " bytes_written: " << buf.bytes_written() << "\n" |
| 567 | << " chunks_written: " << buf.chunks_written() << "\n" |
| 568 | << " chunks_overwritten: " << buf.chunks_overwritten() << "\n" |
| 569 | << " write_wrap_count: " << buf.write_wrap_count() << "\n" |
| 570 | << " patches_succeeded: " << buf.patches_succeeded() << "\n" |
| 571 | << " patches_failed: " << buf.patches_failed() << "\n" |
| 572 | << " readaheads_succeeded: " << buf.readaheads_succeeded() << "\n" |
| 573 | << " readaheads_failed: " << buf.readaheads_failed() << "\n" |
| 574 | << " abi_violations: " << buf.abi_violations() << "\n"; |
| 575 | } |
| 576 | *output << "producers_connected: " << stats.producers_connected() << "\n" |
| 577 | << "producers_seen: " << stats.producers_seen() << "\n" |
| 578 | << "data_sources_reg: " << stats.data_sources_registered() << "\n" |
| 579 | << "data_sources_seen: " << stats.data_sources_seen() << "\n" |
| 580 | << "tracing_sessions: " << stats.tracing_sessions() << "\n" |
| 581 | << "total_buffers: " << stats.total_buffers() << "\n"; |
| 582 | } |
| 583 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 584 | int TraceToSummary(std::istream* input, |
| 585 | std::ostream* output, |
| 586 | bool compact_output) { |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 587 | uint64_t ftrace_start = std::numeric_limits<uint64_t>::max(); |
| 588 | uint64_t ftrace_end = 0; |
| 589 | uint64_t boottime_start = std::numeric_limits<uint64_t>::max(); |
| 590 | uint64_t boottime_end = 0; |
Lalit Maganti | c35fcd5 | 2018-04-20 15:52:35 +0100 | [diff] [blame] | 591 | uint64_t ftrace_overwrites = 0; |
Lalit Maganti | 13c6177 | 2018-04-23 12:51:48 +0100 | [diff] [blame] | 592 | std::map<FtraceEvent::EventCase, uint64_t> ftrace_event_counts; |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 593 | std::multiset<uint64_t> ftrace_timestamps; |
| 594 | std::set<pid_t> tids_in_tree; |
| 595 | std::set<pid_t> tids_in_events; |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 596 | std::set<uint64_t> ftrace_inodes; |
| 597 | uint64_t ftrace_inode_count = 0; |
| 598 | std::set<uint64_t> resolved_map_inodes; |
| 599 | std::set<uint64_t> resolved_scan_inodes; |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 600 | protos::TraceStats last_stats; |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 601 | |
Hector Dearman | f3f8b8f | 2018-04-30 15:00:51 +0100 | [diff] [blame] | 602 | FtraceStats before_stats; |
| 603 | FtraceStats after_stats; |
| 604 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 605 | ForEachPacketInTrace( |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 606 | input, |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 607 | [&ftrace_start, &ftrace_end, &ftrace_overwrites, &ftrace_event_counts, |
| 608 | &before_stats, &after_stats, &ftrace_timestamps, &tids_in_tree, |
| 609 | &tids_in_events, &ftrace_inodes, &ftrace_inode_count, |
| 610 | &resolved_map_inodes, &resolved_scan_inodes, &last_stats, |
| 611 | &boottime_start, &boottime_end](const protos::TracePacket& packet) { |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 612 | if (packet.has_process_tree()) { |
| 613 | const ProcessTree& tree = packet.process_tree(); |
| 614 | for (Process process : tree.processes()) { |
Isabelle Taylor | b249947 | 2018-04-03 15:21:04 +0100 | [diff] [blame] | 615 | tids_in_tree.insert(process.pid()); |
Primiano Tucci | bd0c02f | 2018-04-25 14:00:59 +0100 | [diff] [blame] | 616 | for (ProcessTree::Thread thread : process.threads_deprecated()) |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 617 | tids_in_tree.insert(thread.tid()); |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 618 | } |
Primiano Tucci | bd0c02f | 2018-04-25 14:00:59 +0100 | [diff] [blame] | 619 | for (ProcessTree::Thread thread : tree.threads()) |
| 620 | tids_in_tree.insert(thread.tid()); |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 623 | if (packet.has_inode_file_map()) { |
| 624 | const InodeFileMap& inode_file_map = packet.inode_file_map(); |
| 625 | const auto& mount_points = inode_file_map.mount_points(); |
| 626 | bool from_scan = std::find(mount_points.begin(), mount_points.end(), |
| 627 | "/data") != mount_points.end(); |
| 628 | for (const auto& entry : inode_file_map.entries()) |
| 629 | if (from_scan) |
| 630 | resolved_scan_inodes.insert(entry.inode_number()); |
| 631 | else |
| 632 | resolved_map_inodes.insert(entry.inode_number()); |
| 633 | } |
| 634 | |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 635 | if (packet.has_trace_stats()) |
| 636 | last_stats = packet.trace_stats(); |
| 637 | |
Hector Dearman | f3f8b8f | 2018-04-30 15:00:51 +0100 | [diff] [blame] | 638 | if (packet.has_ftrace_stats()) { |
| 639 | const auto& ftrace_stats = packet.ftrace_stats(); |
| 640 | if (ftrace_stats.phase() == FtraceStats_Phase_START_OF_TRACE) { |
| 641 | before_stats = ftrace_stats; |
| 642 | // TODO(hjd): Check not yet set. |
| 643 | } else if (ftrace_stats.phase() == FtraceStats_Phase_END_OF_TRACE) { |
| 644 | after_stats = ftrace_stats; |
| 645 | // TODO(hjd): Check not yet set. |
| 646 | } else { |
| 647 | // TODO(hjd): Error here. |
| 648 | } |
| 649 | } |
| 650 | |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 651 | if (packet.has_clock_snapshot()) { |
| 652 | for (const auto& clock : packet.clock_snapshot().clocks()) { |
| 653 | if (clock.type() == protos::ClockSnapshot_Clock_Type_MONOTONIC) { |
| 654 | boottime_start = |
| 655 | std::min<uint64_t>(boottime_start, clock.timestamp()); |
| 656 | boottime_end = |
| 657 | std::max<uint64_t>(boottime_end, clock.timestamp()); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 662 | if (!packet.has_ftrace_events()) |
| 663 | return; |
| 664 | |
| 665 | const FtraceEventBundle& bundle = packet.ftrace_events(); |
Lalit Maganti | c35fcd5 | 2018-04-20 15:52:35 +0100 | [diff] [blame] | 666 | ftrace_overwrites += bundle.overwrite_count(); |
| 667 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 668 | uint64_t inode_number = 0; |
| 669 | for (const FtraceEvent& event : bundle.event()) { |
Lalit Maganti | 13c6177 | 2018-04-23 12:51:48 +0100 | [diff] [blame] | 670 | ftrace_event_counts[event.event_case()] += 1; |
| 671 | |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 672 | if (ParseInode(event, &inode_number)) { |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 673 | ftrace_inodes.insert(inode_number); |
| 674 | ftrace_inode_count++; |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 675 | } |
| 676 | if (event.pid()) { |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 677 | tids_in_events.insert(static_cast<int>(event.pid())); |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 678 | } |
| 679 | if (event.timestamp()) { |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 680 | ftrace_start = std::min<uint64_t>(ftrace_start, event.timestamp()); |
| 681 | ftrace_end = std::max<uint64_t>(ftrace_end, event.timestamp()); |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 682 | ftrace_timestamps.insert(event.timestamp()); |
| 683 | } |
| 684 | } |
| 685 | }); |
| 686 | |
| 687 | fprintf(stderr, "\n"); |
| 688 | |
| 689 | char line[2048]; |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 690 | uint64_t ftrace_duration = (ftrace_end - ftrace_start) / (1000 * 1000); |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 691 | if (compact_output) { |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 692 | sprintf(line, "ftrace duration,%" PRIu64 "\n", ftrace_duration); |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 693 | } else { |
Florian Mayer | f6d1bfa | 2018-05-18 10:47:18 +0100 | [diff] [blame] | 694 | sprintf(line, "Ftrace duration: %" PRIu64 "ms\n", ftrace_duration); |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 695 | } |
| 696 | *output << std::string(line); |
| 697 | |
| 698 | uint64_t boottime_duration = (boottime_end - boottime_start) / (1000 * 1000); |
| 699 | if (compact_output) { |
Florian Mayer | f6d1bfa | 2018-05-18 10:47:18 +0100 | [diff] [blame] | 700 | sprintf(line, "boottime duration,%" PRIu64 "\n", boottime_duration); |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 701 | } else { |
Florian Mayer | f6d1bfa | 2018-05-18 10:47:18 +0100 | [diff] [blame] | 702 | sprintf(line, "Boottime duration: %" PRIu64 "ms\n", boottime_duration); |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 703 | } |
Anna Zappone | 7c10462 | 2018-03-28 16:35:18 +0100 | [diff] [blame] | 704 | *output << std::string(line); |
| 705 | |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 706 | if (!compact_output) |
Florian Mayer | e932d18 | 2018-05-17 18:26:37 +0100 | [diff] [blame] | 707 | PrintFtraceTrack(output, ftrace_start, ftrace_end, ftrace_timestamps); |
Hector Dearman | f3f8b8f | 2018-04-30 15:00:51 +0100 | [diff] [blame] | 708 | PrintFtraceStats(output, ftrace_overwrites, ftrace_event_counts, before_stats, |
| 709 | after_stats, compact_output); |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 710 | PrintProcessStats(output, tids_in_tree, tids_in_events, compact_output); |
Anna Zappone | 5e8fa4b | 2018-03-29 12:56:22 +0100 | [diff] [blame] | 711 | PrintInodeStats(output, ftrace_inodes, ftrace_inode_count, |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 712 | resolved_map_inodes, resolved_scan_inodes, compact_output); |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 713 | PrintTraceStats(output, last_stats, compact_output); |
Isabelle Taylor | d496ea5 | 2018-03-28 11:59:59 +0100 | [diff] [blame] | 714 | |
Hector Dearman | 85ef536 | 2018-03-27 14:48:47 +0100 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 718 | } // namespace |
| 719 | } // namespace perfetto |
| 720 | |
| 721 | namespace { |
| 722 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 723 | int Usage(const char* argv0) { |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 724 | printf( |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 725 | "Usage: %s systrace|json|text|summary|short_summary [trace.proto] " |
| 726 | "[trace.txt]\n", |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 727 | argv0); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 728 | return 1; |
| 729 | } |
| 730 | |
| 731 | } // namespace |
| 732 | |
| 733 | int main(int argc, char** argv) { |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 734 | if (argc < 2) |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 735 | return Usage(argv[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 736 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 737 | std::istream* input_stream; |
| 738 | std::ifstream file_istream; |
| 739 | if (argc > 2) { |
| 740 | const char* file_path = argv[2]; |
| 741 | file_istream.open(file_path, std::ios_base::in | std::ios_base::binary); |
| 742 | if (!file_istream.is_open()) |
| 743 | PERFETTO_FATAL("Could not open %s", file_path); |
| 744 | input_stream = &file_istream; |
| 745 | } else { |
| 746 | if (isatty(STDIN_FILENO)) { |
| 747 | PERFETTO_ELOG("Reading from stdin but it's connected to a TTY"); |
| 748 | PERFETTO_LOG("It is unlikely that you want to type in some binary."); |
| 749 | PERFETTO_LOG("Either pass a file path to the cmdline or pipe stdin"); |
| 750 | return Usage(argv[0]); |
| 751 | } |
| 752 | input_stream = &std::cin; |
| 753 | } |
| 754 | |
| 755 | std::ostream* output_stream; |
| 756 | std::ofstream file_ostream; |
| 757 | if (argc > 3) { |
| 758 | const char* file_path = argv[3]; |
| 759 | file_ostream.open(file_path, std::ios_base::out | std::ios_base::trunc); |
| 760 | if (!file_ostream.is_open()) |
| 761 | PERFETTO_FATAL("Could not open %s", file_path); |
| 762 | output_stream = &file_ostream; |
| 763 | } else { |
| 764 | output_stream = &std::cout; |
| 765 | perfetto::g_output_is_tty = isatty(STDOUT_FILENO); |
| 766 | } |
| 767 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 768 | std::string format(argv[1]); |
| 769 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 770 | if (format == "json") |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 771 | return perfetto::TraceToSystrace(input_stream, output_stream, |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 772 | /*wrap_in_json=*/true); |
| 773 | if (format == "systrace") |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 774 | return perfetto::TraceToSystrace(input_stream, output_stream, |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 775 | /*wrap_in_json=*/false); |
| 776 | if (format == "text") |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 777 | return perfetto::TraceToText(input_stream, output_stream); |
Primiano Tucci | f026990 | 2018-04-13 11:13:19 +0100 | [diff] [blame] | 778 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 779 | if (format == "summary") |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 780 | return perfetto::TraceToSummary(input_stream, output_stream, |
Primiano Tucci | f026990 | 2018-04-13 11:13:19 +0100 | [diff] [blame] | 781 | /* compact_output */ false); |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 782 | if (format == "short_summary") |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame^] | 783 | return perfetto::TraceToSummary(input_stream, output_stream, |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 784 | /* compact_output */ true); |
| 785 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 786 | return Usage(argv[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 787 | } |