• 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 "src/trace_processor/importers/gzip/gzip_trace_parser.h"
18 
19 #include <string>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/string_utils.h"
23 #include "perfetto/ext/base/string_view.h"
24 #include "perfetto/trace_processor/trace_blob_view.h"
25 #include "src/trace_processor/forwarding_trace_parser.h"
26 #include "src/trace_processor/util/gzip_utils.h"
27 #include "src/trace_processor/util/status_macros.h"
28 
29 namespace perfetto {
30 namespace trace_processor {
31 
32 namespace {
33 
34 using ResultCode = util::GzipDecompressor::ResultCode;
35 
36 }  // namespace
37 
GzipTraceParser(TraceProcessorContext * context)38 GzipTraceParser::GzipTraceParser(TraceProcessorContext* context)
39     : context_(context) {}
40 
GzipTraceParser(std::unique_ptr<ChunkedTraceReader> reader)41 GzipTraceParser::GzipTraceParser(std::unique_ptr<ChunkedTraceReader> reader)
42     : context_(nullptr), inner_(std::move(reader)) {}
43 
44 GzipTraceParser::~GzipTraceParser() = default;
45 
Parse(TraceBlobView blob)46 util::Status GzipTraceParser::Parse(TraceBlobView blob) {
47   return ParseUnowned(blob.data(), blob.size());
48 }
49 
ParseUnowned(const uint8_t * data,size_t size)50 util::Status GzipTraceParser::ParseUnowned(const uint8_t* data, size_t size) {
51   const uint8_t* start = data;
52   size_t len = size;
53 
54   if (!inner_) {
55     PERFETTO_CHECK(context_);
56     inner_.reset(new ForwardingTraceParser(context_));
57   }
58 
59   if (!first_chunk_parsed_) {
60     // .ctrace files begin with: "TRACE:\n" or "done. TRACE:\n" strip this if
61     // present.
62     base::StringView beginning(reinterpret_cast<const char*>(start), size);
63 
64     static const char* kSystraceFileHeader = "TRACE:\n";
65     size_t offset = Find(kSystraceFileHeader, beginning);
66     if (offset != std::string::npos) {
67       start += strlen(kSystraceFileHeader) + offset;
68       len -= strlen(kSystraceFileHeader) + offset;
69     }
70     first_chunk_parsed_ = true;
71   }
72 
73   // Our default uncompressed buffer size is 32MB as it allows for good
74   // throughput.
75   constexpr size_t kUncompressedBufferSize = 32 * 1024 * 1024;
76 
77   needs_more_input_ = false;
78   decompressor_.Feed(start, len);
79 
80   for (auto ret = ResultCode::kOk; ret != ResultCode::kEof;) {
81     if (!buffer_) {
82       buffer_.reset(new uint8_t[kUncompressedBufferSize]);
83       bytes_written_ = 0;
84     }
85 
86     auto result =
87         decompressor_.ExtractOutput(buffer_.get() + bytes_written_,
88                                     kUncompressedBufferSize - bytes_written_);
89     ret = result.ret;
90     if (ret == ResultCode::kError)
91       return util::ErrStatus("Failed to decompress trace chunk");
92 
93     if (ret == ResultCode::kNeedsMoreInput) {
94       PERFETTO_DCHECK(result.bytes_written == 0);
95       needs_more_input_ = true;
96       return util::OkStatus();
97     }
98     bytes_written_ += result.bytes_written;
99 
100     if (bytes_written_ == kUncompressedBufferSize || ret == ResultCode::kEof) {
101       TraceBlob blob =
102           TraceBlob::TakeOwnership(std::move(buffer_), bytes_written_);
103       RETURN_IF_ERROR(inner_->Parse(TraceBlobView(std::move(blob))));
104     }
105   }
106   return util::OkStatus();
107 }
108 
NotifyEndOfFile()109 void GzipTraceParser::NotifyEndOfFile() {
110   // TODO(lalitm): this should really be an error returned to the caller but
111   // due to historical implementation, NotifyEndOfFile does not return a
112   // util::Status.
113   PERFETTO_DCHECK(!needs_more_input_);
114   PERFETTO_DCHECK(!buffer_);
115 
116   if (inner_)
117     inner_->NotifyEndOfFile();
118 }
119 
120 }  // namespace trace_processor
121 }  // namespace perfetto
122