Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 1 | /* |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 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 | |
Eric Seckler | 137a467 | 2019-10-24 08:51:14 +0100 | [diff] [blame] | 17 | #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_TOKENIZER_H_ |
| 18 | #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_TOKENIZER_H_ |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 19 | |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 22 | #include "perfetto/protozero/proto_utils.h" |
| 23 | #include "perfetto/trace_processor/status.h" |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 24 | #include "perfetto/trace_processor/trace_blob.h" |
| 25 | #include "perfetto/trace_processor/trace_blob_view.h" |
Lalit Maganti | 69216ec | 2021-05-21 14:10:42 +0100 | [diff] [blame] | 26 | #include "src/trace_processor/util/gzip_utils.h" |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 27 | #include "src/trace_processor/util/status_macros.h" |
Eric Seckler | 684a4f7 | 2019-04-26 14:34:07 +0100 | [diff] [blame] | 28 | |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 29 | #include "protos/perfetto/trace/trace.pbzero.h" |
| 30 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | 6756fb0 | 2019-08-14 15:49:18 +0200 | [diff] [blame] | 31 | |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 32 | namespace perfetto { |
| 33 | namespace trace_processor { |
| 34 | |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 35 | // Reads a protobuf trace in chunks and extracts boundaries of trace packets |
| 36 | // (or subfields, for the case of ftrace) with their timestamps. |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 37 | class ProtoTraceTokenizer { |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 38 | public: |
Lalit Maganti | 1caf349 | 2020-09-10 21:00:08 +0100 | [diff] [blame] | 39 | ProtoTraceTokenizer(); |
| 40 | |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 41 | template <typename Callback = util::Status(TraceBlobView)> |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 42 | util::Status Tokenize(TraceBlobView blob, Callback callback) { |
| 43 | const uint8_t* data = blob.data(); |
| 44 | size_t size = blob.size(); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 45 | if (!partial_buf_.empty()) { |
| 46 | // It takes ~5 bytes for a proto preamble + the varint size. |
| 47 | const size_t kHeaderBytes = 5; |
| 48 | if (PERFETTO_UNLIKELY(partial_buf_.size() < kHeaderBytes)) { |
| 49 | size_t missing_len = std::min(kHeaderBytes - partial_buf_.size(), size); |
| 50 | partial_buf_.insert(partial_buf_.end(), &data[0], &data[missing_len]); |
| 51 | if (partial_buf_.size() < kHeaderBytes) |
| 52 | return util::OkStatus(); |
| 53 | data += missing_len; |
| 54 | size -= missing_len; |
| 55 | } |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 56 | |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 57 | // At this point we have enough data in |partial_buf_| to read at least |
| 58 | // the field header and know the size of the next TracePacket. |
| 59 | const uint8_t* pos = &partial_buf_[0]; |
| 60 | uint8_t proto_field_tag = *pos; |
| 61 | uint64_t field_size = 0; |
Florian Mayer | 82c68c6 | 2020-12-09 17:54:07 +0000 | [diff] [blame] | 62 | // We cannot do &partial_buf_[partial_buf_.size()] because that crashes |
| 63 | // on MSVC STL debug builds, so does &*partial_buf_.end(). |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 64 | const uint8_t* next = protozero::proto_utils::ParseVarInt( |
Florian Mayer | 82c68c6 | 2020-12-09 17:54:07 +0000 | [diff] [blame] | 65 | ++pos, &partial_buf_.front() + partial_buf_.size(), &field_size); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 66 | bool parse_failed = next == pos; |
| 67 | pos = next; |
| 68 | if (proto_field_tag != kTracePacketTag || field_size == 0 || |
| 69 | parse_failed) { |
| 70 | return util::ErrStatus( |
| 71 | "Failed parsing a TracePacket from the partial buffer"); |
| 72 | } |
| 73 | |
| 74 | // At this point we know how big the TracePacket is. |
| 75 | size_t hdr_size = static_cast<size_t>(pos - &partial_buf_[0]); |
| 76 | size_t size_incl_header = static_cast<size_t>(field_size + hdr_size); |
| 77 | PERFETTO_DCHECK(size_incl_header > partial_buf_.size()); |
| 78 | |
| 79 | // There is a good chance that between the |partial_buf_| and the new |
| 80 | // |data| of the current call we have enough bytes to parse a TracePacket. |
| 81 | if (partial_buf_.size() + size >= size_incl_header) { |
| 82 | // Create a new buffer for the whole TracePacket and copy into that: |
| 83 | // 1) The beginning of the TracePacket (including the proto header) from |
| 84 | // the partial buffer. |
| 85 | // 2) The rest of the TracePacket from the current |data| buffer (note |
| 86 | // that we might have consumed already a few bytes form |data| |
| 87 | // earlier in this function, hence we need to keep |off| into |
| 88 | // account). |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 89 | TraceBlob glued = TraceBlob::Allocate(size_incl_header); |
| 90 | memcpy(glued.data(), partial_buf_.data(), partial_buf_.size()); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 91 | // |size_missing| is the number of bytes for the rest of the TracePacket |
| 92 | // in |data|. |
| 93 | size_t size_missing = size_incl_header - partial_buf_.size(); |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 94 | memcpy(glued.data() + partial_buf_.size(), &data[0], size_missing); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 95 | data += size_missing; |
| 96 | size -= size_missing; |
| 97 | partial_buf_.clear(); |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 98 | RETURN_IF_ERROR( |
| 99 | ParseInternal(TraceBlobView(std::move(glued)), callback)); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 100 | } else { |
| 101 | partial_buf_.insert(partial_buf_.end(), data, &data[size]); |
| 102 | return util::OkStatus(); |
| 103 | } |
| 104 | } |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 105 | return ParseInternal(blob.slice(data, size), callback); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 106 | } |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 107 | |
| 108 | private: |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 109 | static constexpr uint8_t kTracePacketTag = |
| 110 | protozero::proto_utils::MakeTagLengthDelimited( |
| 111 | protos::pbzero::Trace::kPacketFieldNumber); |
| 112 | |
| 113 | template <typename Callback = util::Status(TraceBlobView)> |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 114 | util::Status ParseInternal(TraceBlobView whole_buf, Callback callback) { |
| 115 | const uint8_t* const start = whole_buf.data(); |
| 116 | protos::pbzero::Trace::Decoder decoder(whole_buf.data(), whole_buf.size()); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 117 | for (auto it = decoder.packet(); it; ++it) { |
| 118 | protozero::ConstBytes packet = *it; |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 119 | TraceBlobView sliced = whole_buf.slice(packet.data, packet.size); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 120 | RETURN_IF_ERROR(ParsePacket(std::move(sliced), callback)); |
| 121 | } |
| 122 | |
| 123 | const size_t bytes_left = decoder.bytes_left(); |
| 124 | if (bytes_left > 0) { |
| 125 | PERFETTO_DCHECK(partial_buf_.empty()); |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 126 | partial_buf_.insert(partial_buf_.end(), &start[decoder.read_offset()], |
| 127 | &start[decoder.read_offset() + bytes_left]); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 128 | } |
| 129 | return util::OkStatus(); |
| 130 | } |
| 131 | |
| 132 | template <typename Callback = util::Status(TraceBlobView)> |
| 133 | util::Status ParsePacket(TraceBlobView packet, Callback callback) { |
| 134 | protos::pbzero::TracePacket::Decoder decoder(packet.data(), |
| 135 | packet.length()); |
| 136 | if (decoder.has_compressed_packets()) { |
Lalit Maganti | 69216ec | 2021-05-21 14:10:42 +0100 | [diff] [blame] | 137 | if (!util::IsGzipSupported()) { |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 138 | return util::Status( |
| 139 | "Cannot decode compressed packets. Zlib not enabled"); |
| 140 | } |
| 141 | |
| 142 | protozero::ConstBytes field = decoder.compressed_packets(); |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 143 | TraceBlobView compressed_packets = packet.slice(field.data, field.size); |
| 144 | TraceBlobView packets; |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 145 | |
| 146 | RETURN_IF_ERROR(Decompress(std::move(compressed_packets), &packets)); |
| 147 | |
| 148 | const uint8_t* start = packets.data(); |
| 149 | const uint8_t* end = packets.data() + packets.length(); |
| 150 | const uint8_t* ptr = start; |
| 151 | while ((end - ptr) > 2) { |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 152 | const uint8_t* packet_outer = ptr; |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 153 | if (PERFETTO_UNLIKELY(*ptr != kTracePacketTag)) |
| 154 | return util::ErrStatus("Expected TracePacket tag"); |
| 155 | uint64_t packet_size = 0; |
| 156 | ptr = protozero::proto_utils::ParseVarInt(++ptr, end, &packet_size); |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 157 | const uint8_t* packet_start = ptr; |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 158 | ptr += packet_size; |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 159 | if (PERFETTO_UNLIKELY((ptr - packet_outer) < 2 || ptr > end)) |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 160 | return util::ErrStatus("Invalid packet size"); |
| 161 | |
| 162 | TraceBlobView sliced = |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 163 | packets.slice(packet_start, static_cast<size_t>(packet_size)); |
Lalit Maganti | eb63b08 | 2020-09-10 14:12:20 +0100 | [diff] [blame] | 164 | RETURN_IF_ERROR(ParsePacket(std::move(sliced), callback)); |
| 165 | } |
| 166 | return util::OkStatus(); |
| 167 | } |
| 168 | return callback(std::move(packet)); |
| 169 | } |
| 170 | |
| 171 | util::Status Decompress(TraceBlobView input, TraceBlobView* output); |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 172 | |
| 173 | // Used to glue together trace packets that span across two (or more) |
| 174 | // Parse() boundaries. |
| 175 | std::vector<uint8_t> partial_buf_; |
| 176 | |
Lalit Maganti | 9d538bd | 2020-03-12 23:48:16 +0000 | [diff] [blame] | 177 | // Allows support for compressed trace packets. |
Lalit Maganti | 69216ec | 2021-05-21 14:10:42 +0100 | [diff] [blame] | 178 | util::GzipDecompressor decompressor_; |
Primiano Tucci | d933d91 | 2018-09-04 09:15:07 +0100 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace trace_processor |
| 182 | } // namespace perfetto |
| 183 | |
Eric Seckler | 137a467 | 2019-10-24 08:51:14 +0100 | [diff] [blame] | 184 | #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_TOKENIZER_H_ |