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