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 <stdio.h>
20
21 #include <cinttypes>
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
ReadTrace(trace_processor::TraceProcessor * tp,std::istream * input)51 bool ReadTrace(trace_processor::TraceProcessor* tp, std::istream* input) {
52 // 1MB chunk size seems the best tradeoff on a MacBook Pro 2013 - i7 2.8 GHz.
53 constexpr size_t kChunkSize = 1024 * 1024;
54
55 // Printing the status update on stderr can be a perf bottleneck. On WASM print
56 // status updates more frequently because it can be slower to parse each chunk.
57 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
58 constexpr int kStderrRate = 1;
59 #else
60 constexpr int kStderrRate = 128;
61 #endif
62 uint64_t file_size = 0;
63
64 for (int i = 0;; i++) {
65 if (i % kStderrRate == 0) {
66 fprintf(stderr, "Loading trace %.2f MB%c",
67 static_cast<double>(file_size) / 1.0e6, kProgressChar);
68 fflush(stderr);
69 }
70
71 std::unique_ptr<uint8_t[]> buf(new uint8_t[kChunkSize]);
72 input->read(reinterpret_cast<char*>(buf.get()), kChunkSize);
73 if (input->bad()) {
74 PERFETTO_ELOG("Failed when reading trace");
75 return false;
76 }
77
78 auto rsize = input->gcount();
79 if (rsize <= 0)
80 break;
81 file_size += static_cast<uint64_t>(rsize);
82 tp->Parse(std::move(buf), static_cast<size_t>(rsize));
83 }
84
85 fprintf(stderr, "Loaded trace%c", kProgressChar);
86 fflush(stderr);
87 return true;
88 }
89
IngestTraceOrDie(trace_processor::TraceProcessor * tp,const std::string & trace_proto)90 void IngestTraceOrDie(trace_processor::TraceProcessor* tp,
91 const std::string& trace_proto) {
92 std::unique_ptr<uint8_t[]> buf(new uint8_t[trace_proto.size()]);
93 memcpy(buf.get(), trace_proto.data(), trace_proto.size());
94 auto status = tp->Parse(std::move(buf), trace_proto.size());
95 if (!status.ok()) {
96 PERFETTO_DFATAL_OR_ELOG("Failed to parse: %s", status.message().c_str());
97 }
98 }
99
TraceWriter(std::ostream * output)100 TraceWriter::TraceWriter(std::ostream* output) : output_(output) {}
101
~TraceWriter()102 TraceWriter::~TraceWriter() {
103 output_->flush();
104 }
105
Write(const std::string & s)106 void TraceWriter::Write(const std::string& s) {
107 Write(s.data(), s.size());
108 }
109
Write(const char * data,size_t sz)110 void TraceWriter::Write(const char* data, size_t sz) {
111 output_->write(data, static_cast<std::streamsize>(sz));
112 }
113
114 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
115
DeflateTraceWriter(std::ostream * output)116 DeflateTraceWriter::DeflateTraceWriter(std::ostream* output)
117 : TraceWriter(output),
118 buf_(base::PagedMemory::Allocate(kCompressionBufferSize)),
119 start_(static_cast<uint8_t*>(buf_.Get())),
120 end_(start_ + buf_.size()) {
121 CheckEq(deflateInit(&stream_, 9), Z_OK);
122 stream_.next_out = start_;
123 stream_.avail_out = static_cast<unsigned int>(end_ - start_);
124 }
125
~DeflateTraceWriter()126 DeflateTraceWriter::~DeflateTraceWriter() {
127 // Drain compressor until it has no more input, and has flushed its internal
128 // buffers.
129 while (deflate(&stream_, Z_FINISH) != Z_STREAM_END) {
130 Flush();
131 }
132 // Flush any outstanding output bytes to the backing TraceWriter.
133 Flush();
134 PERFETTO_CHECK(stream_.avail_out == static_cast<size_t>(end_ - start_));
135
136 CheckEq(deflateEnd(&stream_), Z_OK);
137 }
138
Write(const char * data,size_t sz)139 void DeflateTraceWriter::Write(const char* data, size_t sz) {
140 stream_.next_in = reinterpret_cast<uint8_t*>(const_cast<char*>(data));
141 stream_.avail_in = static_cast<unsigned int>(sz);
142 while (stream_.avail_in > 0) {
143 CheckEq(deflate(&stream_, Z_NO_FLUSH), Z_OK);
144 if (stream_.avail_out == 0) {
145 Flush();
146 }
147 }
148 }
149
Flush()150 void DeflateTraceWriter::Flush() {
151 TraceWriter::Write(reinterpret_cast<char*>(start_),
152 static_cast<size_t>(stream_.next_out - start_));
153 stream_.next_out = start_;
154 stream_.avail_out = static_cast<unsigned int>(end_ - start_);
155 }
156
CheckEq(int actual_code,int expected_code)157 void DeflateTraceWriter::CheckEq(int actual_code, int expected_code) {
158 if (actual_code == expected_code)
159 return;
160 PERFETTO_FATAL("Expected %d got %d: %s", actual_code, expected_code,
161 stream_.msg);
162 }
163 #else
164
DeflateTraceWriter(std::ostream * output)165 DeflateTraceWriter::DeflateTraceWriter(std::ostream* output)
166 : TraceWriter(output) {
167 PERFETTO_ELOG("Cannot compress. Zlib is not enabled in the build config");
168 }
169 DeflateTraceWriter::~DeflateTraceWriter() = default;
170
171 #endif // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
172
173 } // namespace trace_to_text
174 } // namespace perfetto
175