• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/file_utils.h"
21 #include "perfetto/ext/base/scoped_file.h"
22 #include "perfetto/ext/base/utils.h"
23 #include "perfetto/protozero/proto_utils.h"
24 #include "perfetto/trace_processor/trace_processor.h"
25 
26 #include "perfetto/trace_processor/trace_blob.h"
27 #include "perfetto/trace_processor/trace_blob_view.h"
28 #include "src/trace_processor/importers/gzip/gzip_trace_parser.h"
29 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
30 #include "src/trace_processor/read_trace_internal.h"
31 #include "src/trace_processor/util/gzip_utils.h"
32 #include "src/trace_processor/util/status_macros.h"
33 #include "src/trace_processor/util/trace_type.h"
34 
35 #include "protos/perfetto/trace/trace.pbzero.h"
36 #include "protos/perfetto/trace/trace_packet.pbzero.h"
37 
38 namespace perfetto {
39 namespace trace_processor {
40 namespace {
41 
42 class SerializingProtoTraceReader : public ChunkedTraceReader {
43  public:
SerializingProtoTraceReader(std::vector<uint8_t> * output)44   explicit SerializingProtoTraceReader(std::vector<uint8_t>* output)
45       : output_(output) {}
46 
Parse(TraceBlobView blob)47   util::Status Parse(TraceBlobView blob) override {
48     return tokenizer_.Tokenize(std::move(blob), [this](TraceBlobView packet) {
49       uint8_t buffer[protozero::proto_utils::kMaxSimpleFieldEncodedSize];
50 
51       uint8_t* pos = buffer;
52       pos = protozero::proto_utils::WriteVarInt(kTracePacketTag, pos);
53       pos = protozero::proto_utils::WriteVarInt(packet.length(), pos);
54       output_->insert(output_->end(), buffer, pos);
55 
56       output_->insert(output_->end(), packet.data(),
57                       packet.data() + packet.length());
58       return util::OkStatus();
59     });
60   }
61 
NotifyEndOfFile()62   void NotifyEndOfFile() override {}
63 
64  private:
65   static constexpr uint8_t kTracePacketTag =
66       protozero::proto_utils::MakeTagLengthDelimited(
67           protos::pbzero::Trace::kPacketFieldNumber);
68 
69   ProtoTraceTokenizer tokenizer_;
70   std::vector<uint8_t>* output_;
71 };
72 
73 }  // namespace
74 
ReadTrace(TraceProcessor * tp,const char * filename,const std::function<void (uint64_t parsed_size)> & progress_callback)75 util::Status ReadTrace(
76     TraceProcessor* tp,
77     const char* filename,
78     const std::function<void(uint64_t parsed_size)>& progress_callback) {
79   ReadTraceUnfinalized(tp, filename, progress_callback);
80   tp->NotifyEndOfFile();
81   return util::OkStatus();
82 }
83 
DecompressTrace(const uint8_t * data,size_t size,std::vector<uint8_t> * output)84 util::Status DecompressTrace(const uint8_t* data,
85                              size_t size,
86                              std::vector<uint8_t>* output) {
87   TraceType type = GuessTraceType(data, size);
88   if (type != TraceType::kGzipTraceType && type != TraceType::kProtoTraceType) {
89     return util::ErrStatus(
90         "Only GZIP and proto trace types are supported by DecompressTrace");
91   }
92 
93   if (type == TraceType::kGzipTraceType) {
94     std::unique_ptr<ChunkedTraceReader> reader(
95         new SerializingProtoTraceReader(output));
96     GzipTraceParser parser(std::move(reader));
97 
98     RETURN_IF_ERROR(parser.ParseUnowned(data, size));
99     if (parser.needs_more_input())
100       return util::ErrStatus("Cannot decompress partial trace file");
101 
102     parser.NotifyEndOfFile();
103     return util::OkStatus();
104   }
105 
106   PERFETTO_CHECK(type == TraceType::kProtoTraceType);
107 
108   protos::pbzero::Trace::Decoder decoder(data, size);
109   util::GzipDecompressor decompressor;
110   if (size > 0 && !decoder.packet()) {
111     return util::ErrStatus("Trace does not contain valid packets");
112   }
113   for (auto it = decoder.packet(); it; ++it) {
114     protos::pbzero::TracePacket::Decoder packet(*it);
115     if (!packet.has_compressed_packets()) {
116       it->SerializeAndAppendTo(output);
117       continue;
118     }
119 
120     // Make sure that to reset the stream between the gzip streams.
121     auto bytes = packet.compressed_packets();
122     decompressor.Reset();
123     using ResultCode = util::GzipDecompressor::ResultCode;
124     ResultCode ret = decompressor.FeedAndExtract(
125         bytes.data, bytes.size, [&output](const uint8_t* buf, size_t buf_len) {
126           output->insert(output->end(), buf, buf + buf_len);
127         });
128     if (ret == ResultCode::kError || ret == ResultCode::kNeedsMoreInput) {
129       return util::ErrStatus("Failed while decompressing stream");
130     }
131   }
132   return util::OkStatus();
133 }
134 
135 }  // namespace trace_processor
136 }  // namespace perfetto
137