blob: fb2938b6b6bf15be2bb077a3a6f8e4032203969f [file] [log] [blame]
Eric Seckler8f70bbf2019-10-09 09:37:43 +01001/*
2 * Copyright (C) 2019 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 "perfetto/trace_processor/read_trace.h"
18
Lalit Maganti1272d4c2020-08-28 14:14:10 +010019#include "perfetto/base/logging.h"
Primiano Tucciab293f52020-12-08 11:46:52 +010020#include "perfetto/ext/base/file_utils.h"
Eric Seckler8f70bbf2019-10-09 09:37:43 +010021#include "perfetto/ext/base/scoped_file.h"
Lalit Maganti9d538bd2020-03-12 23:48:16 +000022#include "perfetto/ext/base/utils.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010023#include "perfetto/protozero/proto_utils.h"
Eric Seckler8f70bbf2019-10-09 09:37:43 +010024#include "perfetto/trace_processor/trace_processor.h"
25
Primiano Tucci3264b592021-11-08 18:20:51 +000026#include "perfetto/trace_processor/trace_blob.h"
27#include "perfetto/trace_processor/trace_blob_view.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010028#include "src/trace_processor/forwarding_trace_parser.h"
29#include "src/trace_processor/importers/gzip/gzip_trace_parser.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010030#include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
Lalit Maganti69216ec2021-05-21 14:10:42 +010031#include "src/trace_processor/util/gzip_utils.h"
Lalit Maganti1272d4c2020-08-28 14:14:10 +010032#include "src/trace_processor/util/status_macros.h"
Lalit Maganti9d538bd2020-03-12 23:48:16 +000033
34#include "protos/perfetto/trace/trace.pbzero.h"
35#include "protos/perfetto/trace/trace_packet.pbzero.h"
36
Eric Seckler8f70bbf2019-10-09 09:37:43 +010037#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
Primiano Tucci15f5e872020-07-27 23:08:05 +020038 PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
Eric Seckler8f70bbf2019-10-09 09:37:43 +010039#define PERFETTO_HAS_AIO_H() 1
40#else
41#define PERFETTO_HAS_AIO_H() 0
42#endif
43
44#if PERFETTO_HAS_AIO_H()
45#include <aio.h>
46#endif
47
48namespace perfetto {
49namespace trace_processor {
Lalit Maganti1272d4c2020-08-28 14:14:10 +010050namespace {
51
52// 1MB chunk size seems the best tradeoff on a MacBook Pro 2013 - i7 2.8 GHz.
53constexpr size_t kChunkSize = 1024 * 1024;
54
55util::Status ReadTraceUsingRead(
56 TraceProcessor* tp,
57 int fd,
58 uint64_t* file_size,
59 const std::function<void(uint64_t parsed_size)>& progress_callback) {
60 // Load the trace in chunks using ordinary read().
61 for (int i = 0;; i++) {
62 if (progress_callback && i % 128 == 0)
63 progress_callback(*file_size);
64
Primiano Tucci3264b592021-11-08 18:20:51 +000065 TraceBlob blob = TraceBlob::Allocate(kChunkSize);
66 auto rsize = base::Read(fd, blob.data(), blob.size());
Lalit Maganti1272d4c2020-08-28 14:14:10 +010067 if (rsize == 0)
68 break;
69
70 if (rsize < 0) {
71 return util::ErrStatus("Reading trace file failed (errno: %d, %s)", errno,
72 strerror(errno));
73 }
74
75 *file_size += static_cast<uint64_t>(rsize);
Primiano Tucci3264b592021-11-08 18:20:51 +000076 TraceBlobView blob_view(std::move(blob), 0, static_cast<size_t>(rsize));
77 RETURN_IF_ERROR(tp->Parse(std::move(blob_view)));
Lalit Maganti1272d4c2020-08-28 14:14:10 +010078 }
79 return util::OkStatus();
80}
81
Lalit Maganti1caf3492020-09-10 21:00:08 +010082class SerializingProtoTraceReader : public ChunkedTraceReader {
83 public:
Primiano Tucci3264b592021-11-08 18:20:51 +000084 explicit SerializingProtoTraceReader(std::vector<uint8_t>* output)
85 : output_(output) {}
Lalit Maganti1caf3492020-09-10 21:00:08 +010086
Primiano Tucci3264b592021-11-08 18:20:51 +000087 util::Status Parse(TraceBlobView blob) override {
88 return tokenizer_.Tokenize(std::move(blob), [this](TraceBlobView packet) {
89 uint8_t buffer[protozero::proto_utils::kMaxSimpleFieldEncodedSize];
Lalit Maganti1caf3492020-09-10 21:00:08 +010090
Primiano Tucci3264b592021-11-08 18:20:51 +000091 uint8_t* pos = buffer;
92 pos = protozero::proto_utils::WriteVarInt(kTracePacketTag, pos);
93 pos = protozero::proto_utils::WriteVarInt(packet.length(), pos);
94 output_->insert(output_->end(), buffer, pos);
Lalit Maganti1caf3492020-09-10 21:00:08 +010095
Primiano Tucci3264b592021-11-08 18:20:51 +000096 output_->insert(output_->end(), packet.data(),
97 packet.data() + packet.length());
98 return util::OkStatus();
99 });
Lalit Maganti1caf3492020-09-10 21:00:08 +0100100 }
101
102 void NotifyEndOfFile() override {}
103
104 private:
105 static constexpr uint8_t kTracePacketTag =
106 protozero::proto_utils::MakeTagLengthDelimited(
107 protos::pbzero::Trace::kPacketFieldNumber);
108
109 ProtoTraceTokenizer tokenizer_;
110 std::vector<uint8_t>* output_;
111};
112
Lalit Maganti1272d4c2020-08-28 14:14:10 +0100113} // namespace
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100114
115util::Status ReadTrace(
116 TraceProcessor* tp,
117 const char* filename,
118 const std::function<void(uint64_t parsed_size)>& progress_callback) {
119 base::ScopedFile fd(base::OpenFile(filename, O_RDONLY));
120 if (!fd)
121 return util::ErrStatus("Could not open trace file (path: %s)", filename);
122
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100123 uint64_t file_size = 0;
124
125#if PERFETTO_HAS_AIO_H()
126 // Load the trace in chunks using async IO. We create a simple pipeline where,
127 // at each iteration, we parse the current chunk and asynchronously start
128 // reading the next chunk.
129 struct aiocb cb {};
130 cb.aio_nbytes = kChunkSize;
131 cb.aio_fildes = *fd;
132
133 std::unique_ptr<uint8_t[]> aio_buf(new uint8_t[kChunkSize]);
134#if defined(MEMORY_SANITIZER)
135 // Just initialize the memory to make the memory sanitizer happy as it
136 // cannot track aio calls below.
137 memset(aio_buf.get(), 0, kChunkSize);
138#endif // defined(MEMORY_SANITIZER)
139 cb.aio_buf = aio_buf.get();
140
141 PERFETTO_CHECK(aio_read(&cb) == 0);
142 struct aiocb* aio_list[1] = {&cb};
143
144 for (int i = 0;; i++) {
145 if (progress_callback && i % 128 == 0)
146 progress_callback(file_size);
147
148 // Block waiting for the pending read to complete.
149 PERFETTO_CHECK(aio_suspend(aio_list, 1, nullptr) == 0);
150 auto rsize = aio_return(&cb);
151 if (rsize <= 0)
152 break;
153 file_size += static_cast<uint64_t>(rsize);
154
155 // Take ownership of the completed buffer and enqueue a new async read
156 // with a fresh buffer.
157 std::unique_ptr<uint8_t[]> buf(std::move(aio_buf));
158 aio_buf.reset(new uint8_t[kChunkSize]);
159#if defined(MEMORY_SANITIZER)
160 // Just initialize the memory to make the memory sanitizer happy as it
161 // cannot track aio calls below.
162 memset(aio_buf.get(), 0, kChunkSize);
163#endif // defined(MEMORY_SANITIZER)
164 cb.aio_buf = aio_buf.get();
165 cb.aio_offset += rsize;
166 PERFETTO_CHECK(aio_read(&cb) == 0);
167
168 // Parse the completed buffer while the async read is in-flight.
Primiano Tucci3264b592021-11-08 18:20:51 +0000169 TraceBlob blob =
170 TraceBlob::TakeOwnership(std::move(buf), static_cast<size_t>(rsize));
171 RETURN_IF_ERROR(tp->Parse(TraceBlobView(std::move(blob))));
Lalit Maganti1272d4c2020-08-28 14:14:10 +0100172 }
173
174 if (file_size == 0) {
175 PERFETTO_ILOG(
176 "Failed to read any data using AIO. This is expected and not an error "
177 "on WSL. Falling back to read()");
178 RETURN_IF_ERROR(ReadTraceUsingRead(tp, *fd, &file_size, progress_callback));
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100179 }
180#else // PERFETTO_HAS_AIO_H()
Lalit Maganti1272d4c2020-08-28 14:14:10 +0100181 RETURN_IF_ERROR(ReadTraceUsingRead(tp, *fd, &file_size, progress_callback));
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100182#endif // PERFETTO_HAS_AIO_H()
183
184 tp->NotifyEndOfFile();
Primiano Tucciee2ce1d2019-11-01 19:14:17 +0100185 tp->SetCurrentTraceName(filename);
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100186
187 if (progress_callback)
188 progress_callback(file_size);
189 return util::OkStatus();
190}
191
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000192util::Status DecompressTrace(const uint8_t* data,
193 size_t size,
194 std::vector<uint8_t>* output) {
Lalit Maganti1caf3492020-09-10 21:00:08 +0100195 TraceType type = GuessTraceType(data, size);
196 if (type != TraceType::kGzipTraceType && type != TraceType::kProtoTraceType) {
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000197 return util::ErrStatus(
Lalit Maganti1caf3492020-09-10 21:00:08 +0100198 "Only GZIP and proto trace types are supported by DecompressTrace");
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000199 }
200
Lalit Maganti1caf3492020-09-10 21:00:08 +0100201 if (type == TraceType::kGzipTraceType) {
Lalit Maganti9d06f192020-10-02 16:12:58 +0100202 std::unique_ptr<ChunkedTraceReader> reader(
203 new SerializingProtoTraceReader(output));
204 GzipTraceParser parser(std::move(reader));
Lalit Maganti1caf3492020-09-10 21:00:08 +0100205
Lalit Maganti9d06f192020-10-02 16:12:58 +0100206 RETURN_IF_ERROR(parser.ParseUnowned(data, size));
207 if (parser.needs_more_input())
Lalit Maganti1caf3492020-09-10 21:00:08 +0100208 return util::ErrStatus("Cannot decompress partial trace file");
209
Lalit Maganti9d06f192020-10-02 16:12:58 +0100210 parser.NotifyEndOfFile();
Lalit Maganti1caf3492020-09-10 21:00:08 +0100211 return util::OkStatus();
212 }
213
214 PERFETTO_CHECK(type == TraceType::kProtoTraceType);
215
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000216 protos::pbzero::Trace::Decoder decoder(data, size);
Lalit Maganti69216ec2021-05-21 14:10:42 +0100217 util::GzipDecompressor decompressor;
Hector Dearman4aed97a2020-09-09 15:24:23 +0100218 if (size > 0 && !decoder.packet()) {
219 return util::ErrStatus("Trace does not contain valid packets");
220 }
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000221 for (auto it = decoder.packet(); it; ++it) {
222 protos::pbzero::TracePacket::Decoder packet(*it);
223 if (!packet.has_compressed_packets()) {
224 it->SerializeAndAppendTo(output);
225 continue;
226 }
227
228 // Make sure that to reset the stream between the gzip streams.
229 auto bytes = packet.compressed_packets();
230 decompressor.Reset();
231 decompressor.SetInput(bytes.data, bytes.size);
232
Lalit Maganti69216ec2021-05-21 14:10:42 +0100233 using ResultCode = util::GzipDecompressor::ResultCode;
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000234 uint8_t out[4096];
235 for (auto ret = ResultCode::kOk; ret != ResultCode::kEof;) {
236 auto res = decompressor.Decompress(out, base::ArraySize(out));
237 ret = res.ret;
238 if (ret == ResultCode::kError || ret == ResultCode::kNoProgress ||
239 ret == ResultCode::kNeedsMoreInput) {
240 return util::ErrStatus("Failed while decompressing stream");
241 }
242 output->insert(output->end(), out, out + res.bytes_written);
243 }
244 }
245 return util::OkStatus();
246}
247
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100248} // namespace trace_processor
249} // namespace perfetto