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/utils.h"
18 
19 #include <inttypes.h>
20 #include <stdio.h>
21 
22 #include <memory>
23 #include <ostream>
24 #include <set>
25 #include <utility>
26 
27 #include "perfetto/base/logging.h"
28 #include "perfetto/ext/base/file_utils.h"
29 #include "perfetto/ext/base/optional.h"
30 #include "perfetto/ext/base/scoped_file.h"
31 #include "perfetto/ext/base/string_splitter.h"
32 #include "perfetto/protozero/scattered_heap_buffer.h"
33 #include "perfetto/trace_processor/trace_processor.h"
34 
35 #include "protos/perfetto/trace/profiling/deobfuscation.pbzero.h"
36 #include "protos/perfetto/trace/profiling/heap_graph.pbzero.h"
37 #include "protos/perfetto/trace/profiling/profile_common.pbzero.h"
38 #include "protos/perfetto/trace/trace.pbzero.h"
39 #include "protos/perfetto/trace/trace_packet.pbzero.h"
40 
41 namespace perfetto {
42 namespace trace_to_text {
43 namespace {
44 
45 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
46 constexpr size_t kCompressionBufferSize = 500 * 1024;
47 #endif
48 
49 }  // namespace
50 
ForEachPacketBlobInTrace(std::istream * input,const std::function<void (std::unique_ptr<char[]>,size_t)> & f)51 void ForEachPacketBlobInTrace(
52     std::istream* input,
53     const std::function<void(std::unique_ptr<char[]>, size_t)>& f) {
54   size_t bytes_processed = 0;
55   // The trace stream can be very large. We cannot just pass it in one go to
56   // libprotobuf as that will refuse to parse messages > 64MB. However we know
57   // that a trace is merely a sequence of TracePackets. Here we just manually
58   // tokenize the repeated TracePacket messages and parse them individually
59   // using libprotobuf.
60   for (uint32_t i = 0;; i++) {
61     if ((i & 0x3f) == 0) {
62       fprintf(stderr, "Processing trace: %8zu KB%c", bytes_processed / 1024,
63               kProgressChar);
64       fflush(stderr);
65     }
66     // A TracePacket consists in one byte stating its field id and type ...
67     char preamble;
68     input->get(preamble);
69     if (!input->good())
70       break;
71     bytes_processed++;
72     PERFETTO_DCHECK(preamble == 0x0a);  // Field ID:1, type:length delimited.
73 
74     // ... a varint stating its size ...
75     uint32_t field_size = 0;
76     uint32_t shift = 0;
77     for (;;) {
78       char c = 0;
79       input->get(c);
80       field_size |= static_cast<uint32_t>(c & 0x7f) << shift;
81       shift += 7;
82       bytes_processed++;
83       if (!(c & 0x80))
84         break;
85     }
86 
87     // ... and the actual TracePacket itself.
88     std::unique_ptr<char[]> buf(new char[field_size]);
89     input->read(buf.get(), static_cast<std::streamsize>(field_size));
90     bytes_processed += field_size;
91 
92     f(std::move(buf), field_size);
93   }
94 }
95 
96 
ReadTrace(trace_processor::TraceProcessor * tp,std::istream * input)97 bool ReadTrace(trace_processor::TraceProcessor* tp, std::istream* input) {
98   // 1MB chunk size seems the best tradeoff on a MacBook Pro 2013 - i7 2.8 GHz.
99   constexpr size_t kChunkSize = 1024 * 1024;
100 
101 // Printing the status update on stderr can be a perf bottleneck. On WASM print
102 // status updates more frequently because it can be slower to parse each chunk.
103 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
104   constexpr int kStderrRate = 1;
105 #else
106   constexpr int kStderrRate = 128;
107 #endif
108   uint64_t file_size = 0;
109 
110   for (int i = 0;; i++) {
111     if (i % kStderrRate == 0) {
112       fprintf(stderr, "Loading trace %.2f MB%c",
113               static_cast<double>(file_size) / 1.0e6, kProgressChar);
114       fflush(stderr);
115     }
116 
117     std::unique_ptr<uint8_t[]> buf(new uint8_t[kChunkSize]);
118     input->read(reinterpret_cast<char*>(buf.get()), kChunkSize);
119     if (input->bad()) {
120       PERFETTO_ELOG("Failed when reading trace");
121       return false;
122     }
123 
124     auto rsize = input->gcount();
125     if (rsize <= 0)
126       break;
127     file_size += static_cast<uint64_t>(rsize);
128     tp->Parse(std::move(buf), static_cast<size_t>(rsize));
129   }
130 
131   fprintf(stderr, "Loaded trace%c", kProgressChar);
132   fflush(stderr);
133   return true;
134 }
135 
IngestTraceOrDie(trace_processor::TraceProcessor * tp,const std::string & trace_proto)136 void IngestTraceOrDie(trace_processor::TraceProcessor* tp,
137                       const std::string& trace_proto) {
138   std::unique_ptr<uint8_t[]> buf(new uint8_t[trace_proto.size()]);
139   memcpy(buf.get(), trace_proto.data(), trace_proto.size());
140   auto status = tp->Parse(std::move(buf), trace_proto.size());
141   if (!status.ok()) {
142     PERFETTO_DFATAL_OR_ELOG("Failed to parse: %s", status.message().c_str());
143   }
144 }
145 
TraceWriter(std::ostream * output)146 TraceWriter::TraceWriter(std::ostream* output) : output_(output) {}
147 
~TraceWriter()148 TraceWriter::~TraceWriter() {
149   output_->flush();
150 }
151 
Write(const std::string & s)152 void TraceWriter::Write(const std::string& s) {
153   Write(s.data(), s.size());
154 }
155 
Write(const char * data,size_t sz)156 void TraceWriter::Write(const char* data, size_t sz) {
157   output_->write(data, static_cast<std::streamsize>(sz));
158 }
159 
160 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
161 
DeflateTraceWriter(std::ostream * output)162 DeflateTraceWriter::DeflateTraceWriter(std::ostream* output)
163     : TraceWriter(output),
164       buf_(base::PagedMemory::Allocate(kCompressionBufferSize)),
165       start_(static_cast<uint8_t*>(buf_.Get())),
166       end_(start_ + buf_.size()) {
167   CheckEq(deflateInit(&stream_, 9), Z_OK);
168   stream_.next_out = start_;
169   stream_.avail_out = static_cast<unsigned int>(end_ - start_);
170 }
171 
~DeflateTraceWriter()172 DeflateTraceWriter::~DeflateTraceWriter() {
173   // Drain compressor until it has no more input, and has flushed its internal
174   // buffers.
175   while (deflate(&stream_, Z_FINISH) != Z_STREAM_END) {
176     Flush();
177   }
178   // Flush any outstanding output bytes to the backing TraceWriter.
179   Flush();
180   PERFETTO_CHECK(stream_.avail_out == static_cast<size_t>(end_ - start_));
181 
182   CheckEq(deflateEnd(&stream_), Z_OK);
183 }
184 
Write(const char * data,size_t sz)185 void DeflateTraceWriter::Write(const char* data, size_t sz) {
186   stream_.next_in = reinterpret_cast<uint8_t*>(const_cast<char*>(data));
187   stream_.avail_in = static_cast<unsigned int>(sz);
188   while (stream_.avail_in > 0) {
189     CheckEq(deflate(&stream_, Z_NO_FLUSH), Z_OK);
190     if (stream_.avail_out == 0) {
191       Flush();
192     }
193   }
194 }
195 
Flush()196 void DeflateTraceWriter::Flush() {
197   TraceWriter::Write(reinterpret_cast<char*>(start_),
198                      static_cast<size_t>(stream_.next_out - start_));
199   stream_.next_out = start_;
200   stream_.avail_out = static_cast<unsigned int>(end_ - start_);
201 }
202 
CheckEq(int actual_code,int expected_code)203 void DeflateTraceWriter::CheckEq(int actual_code, int expected_code) {
204   if (actual_code == expected_code)
205     return;
206   PERFETTO_FATAL("Expected %d got %d: %s", actual_code, expected_code,
207                  stream_.msg);
208 }
209 #else
210 
DeflateTraceWriter(std::ostream * output)211 DeflateTraceWriter::DeflateTraceWriter(std::ostream* output)
212     : TraceWriter(output) {
213   PERFETTO_ELOG("Cannot compress. Zlib is not enabled in the build config");
214 }
215 DeflateTraceWriter::~DeflateTraceWriter() = default;
216 
217 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
218 
219 }  // namespace trace_to_text
220 }  // namespace perfetto
221