Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 1 | /* |
| 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 Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 19 | #include "perfetto/base/logging.h" |
Primiano Tucci | ab293f5 | 2020-12-08 11:46:52 +0100 | [diff] [blame] | 20 | #include "perfetto/ext/base/file_utils.h" |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 21 | #include "perfetto/ext/base/scoped_file.h" |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 22 | #include "perfetto/ext/base/utils.h" |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 23 | #include "perfetto/protozero/proto_utils.h" |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 24 | #include "perfetto/trace_processor/trace_processor.h" |
| 25 | |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 26 | #include "perfetto/trace_processor/trace_blob.h" |
| 27 | #include "perfetto/trace_processor/trace_blob_view.h" |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 28 | #include "src/trace_processor/forwarding_trace_parser.h" |
| 29 | #include "src/trace_processor/importers/gzip/gzip_trace_parser.h" |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 30 | #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h" |
Lalit Maganti | 69216ec | 2021-05-21 14:10:42 +0100 | [diff] [blame] | 31 | #include "src/trace_processor/util/gzip_utils.h" |
Lalit Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 32 | #include "src/trace_processor/util/status_macros.h" |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 33 | |
| 34 | #include "protos/perfetto/trace/trace.pbzero.h" |
| 35 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
| 36 | |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 37 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ |
Primiano Tucci | 15f5e87 | 2020-07-27 23:08:05 +0200 | [diff] [blame] | 38 | PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 39 | #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 | |
| 48 | namespace perfetto { |
| 49 | namespace trace_processor { |
Lalit Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 50 | namespace { |
| 51 | |
| 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 | util::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 Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 65 | TraceBlob blob = TraceBlob::Allocate(kChunkSize); |
| 66 | auto rsize = base::Read(fd, blob.data(), blob.size()); |
Lalit Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 67 | 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 Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 76 | TraceBlobView blob_view(std::move(blob), 0, static_cast<size_t>(rsize)); |
| 77 | RETURN_IF_ERROR(tp->Parse(std::move(blob_view))); |
Lalit Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 78 | } |
| 79 | return util::OkStatus(); |
| 80 | } |
| 81 | |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 82 | class SerializingProtoTraceReader : public ChunkedTraceReader { |
| 83 | public: |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 84 | explicit SerializingProtoTraceReader(std::vector<uint8_t>* output) |
| 85 | : output_(output) {} |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 86 | |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 87 | 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 Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 90 | |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 91 | 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 Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 95 | |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 96 | output_->insert(output_->end(), packet.data(), |
| 97 | packet.data() + packet.length()); |
| 98 | return util::OkStatus(); |
| 99 | }); |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 100 | } |
| 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 Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 113 | } // namespace |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 114 | |
| 115 | util::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 Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 123 | 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 Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 169 | TraceBlob blob = |
| 170 | TraceBlob::TakeOwnership(std::move(buf), static_cast<size_t>(rsize)); |
| 171 | RETURN_IF_ERROR(tp->Parse(TraceBlobView(std::move(blob)))); |
Lalit Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 172 | } |
| 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 Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 179 | } |
| 180 | #else // PERFETTO_HAS_AIO_H() |
Lalit Maganti | 1272d4c | 2020-08-28 14:14:10 +0100 | [diff] [blame] | 181 | RETURN_IF_ERROR(ReadTraceUsingRead(tp, *fd, &file_size, progress_callback)); |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 182 | #endif // PERFETTO_HAS_AIO_H() |
| 183 | |
| 184 | tp->NotifyEndOfFile(); |
Primiano Tucci | ee2ce1d | 2019-11-01 19:14:17 +0100 | [diff] [blame] | 185 | tp->SetCurrentTraceName(filename); |
Eric Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 186 | |
| 187 | if (progress_callback) |
| 188 | progress_callback(file_size); |
| 189 | return util::OkStatus(); |
| 190 | } |
| 191 | |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 192 | util::Status DecompressTrace(const uint8_t* data, |
| 193 | size_t size, |
| 194 | std::vector<uint8_t>* output) { |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 195 | TraceType type = GuessTraceType(data, size); |
| 196 | if (type != TraceType::kGzipTraceType && type != TraceType::kProtoTraceType) { |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 197 | return util::ErrStatus( |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 198 | "Only GZIP and proto trace types are supported by DecompressTrace"); |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 201 | if (type == TraceType::kGzipTraceType) { |
Lalit Maganti | 9d06f19 | 2020-10-02 16:12:58 +0100 | [diff] [blame] | 202 | std::unique_ptr<ChunkedTraceReader> reader( |
| 203 | new SerializingProtoTraceReader(output)); |
| 204 | GzipTraceParser parser(std::move(reader)); |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 205 | |
Lalit Maganti | 9d06f19 | 2020-10-02 16:12:58 +0100 | [diff] [blame] | 206 | RETURN_IF_ERROR(parser.ParseUnowned(data, size)); |
| 207 | if (parser.needs_more_input()) |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 208 | return util::ErrStatus("Cannot decompress partial trace file"); |
| 209 | |
Lalit Maganti | 9d06f19 | 2020-10-02 16:12:58 +0100 | [diff] [blame] | 210 | parser.NotifyEndOfFile(); |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 211 | return util::OkStatus(); |
| 212 | } |
| 213 | |
| 214 | PERFETTO_CHECK(type == TraceType::kProtoTraceType); |
| 215 | |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 216 | protos::pbzero::Trace::Decoder decoder(data, size); |
Lalit Maganti | 69216ec | 2021-05-21 14:10:42 +0100 | [diff] [blame] | 217 | util::GzipDecompressor decompressor; |
Hector Dearman | 4aed97a | 2020-09-09 15:24:23 +0100 | [diff] [blame] | 218 | if (size > 0 && !decoder.packet()) { |
| 219 | return util::ErrStatus("Trace does not contain valid packets"); |
| 220 | } |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 221 | 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 Maganti | 69216ec | 2021-05-21 14:10:42 +0100 | [diff] [blame] | 233 | using ResultCode = util::GzipDecompressor::ResultCode; |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 234 | 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 Seckler | 8f70bbf | 2019-10-09 09:37:43 +0100 | [diff] [blame] | 248 | } // namespace trace_processor |
| 249 | } // namespace perfetto |