blob: e8e94943a7779a9105d442aed8c80a7174eeb5c4 [file] [log] [blame]
Lalit Maganti9d538bd2020-03-12 23:48:16 +00001/*
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"
Primiano Tucci3264b592021-11-08 18:20:51 +000024#include "perfetto/trace_processor/trace_blob_view.h"
Lalit Maganti9d538bd2020-03-12 23:48:16 +000025#include "src/trace_processor/forwarding_trace_parser.h"
Lalit Maganti69216ec2021-05-21 14:10:42 +010026#include "src/trace_processor/util/gzip_utils.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010027#include "src/trace_processor/util/status_macros.h"
Lalit Maganti9d538bd2020-03-12 23:48:16 +000028
29namespace perfetto {
30namespace trace_processor {
31
Lalit Maganti1caf3492020-09-10 21:00:08 +010032namespace {
33
Lalit Maganti69216ec2021-05-21 14:10:42 +010034using ResultCode = util::GzipDecompressor::ResultCode;
Lalit Maganti1caf3492020-09-10 21:00:08 +010035
36} // namespace
37
Lalit Maganti9d538bd2020-03-12 23:48:16 +000038GzipTraceParser::GzipTraceParser(TraceProcessorContext* context)
39 : context_(context) {}
40
Lalit Maganti9d06f192020-10-02 16:12:58 +010041GzipTraceParser::GzipTraceParser(std::unique_ptr<ChunkedTraceReader> reader)
42 : context_(nullptr), inner_(std::move(reader)) {}
43
Lalit Maganti9d538bd2020-03-12 23:48:16 +000044GzipTraceParser::~GzipTraceParser() = default;
45
Primiano Tucci3264b592021-11-08 18:20:51 +000046util::Status GzipTraceParser::Parse(TraceBlobView blob) {
47 return ParseUnowned(blob.data(), blob.size());
Lalit Maganti9d06f192020-10-02 16:12:58 +010048}
49
50util::Status GzipTraceParser::ParseUnowned(const uint8_t* data, size_t size) {
51 const uint8_t* start = data;
Lalit Maganti9d538bd2020-03-12 23:48:16 +000052 size_t len = size;
53
54 if (!inner_) {
Lalit Maganti9d06f192020-10-02 16:12:58 +010055 PERFETTO_CHECK(context_);
Lalit Maganti9d538bd2020-03-12 23:48:16 +000056 inner_.reset(new ForwardingTraceParser(context_));
Lalit Maganti9d06f192020-10-02 16:12:58 +010057 }
Lalit Maganti9d538bd2020-03-12 23:48:16 +000058
Lalit Maganti9d06f192020-10-02 16:12:58 +010059 if (!first_chunk_parsed_) {
Lalit Maganti9d538bd2020-03-12 23:48:16 +000060 // .ctrace files begin with: "TRACE:\n" or "done. TRACE:\n" strip this if
61 // present.
Lalit Maganti9d06f192020-10-02 16:12:58 +010062 base::StringView beginning(reinterpret_cast<const char*>(start), size);
Lalit Maganti9d538bd2020-03-12 23:48:16 +000063
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 }
Lalit Maganti9d06f192020-10-02 16:12:58 +010070 first_chunk_parsed_ = true;
Lalit Maganti9d538bd2020-03-12 23:48:16 +000071 }
Lalit Maganti1caf3492020-09-10 21:00:08 +010072
Lalit Maganti9d538bd2020-03-12 23:48:16 +000073 // Our default uncompressed buffer size is 32MB as it allows for good
74 // throughput.
Lalit Maganti9d538bd2020-03-12 23:48:16 +000075 constexpr size_t kUncompressedBufferSize = 32 * 1024 * 1024;
76
Lalit Maganti9d06f192020-10-02 16:12:58 +010077 needs_more_input_ = false;
78 decompressor_.SetInput(start, len);
79
Lalit Maganti9d538bd2020-03-12 23:48:16 +000080 for (auto ret = ResultCode::kOk; ret != ResultCode::kEof;) {
Lalit Maganti9d06f192020-10-02 16:12:58 +010081 if (!buffer_) {
82 buffer_.reset(new uint8_t[kUncompressedBufferSize]);
83 bytes_written_ = 0;
84 }
85
Lalit Maganti9d538bd2020-03-12 23:48:16 +000086 auto result =
Lalit Maganti9d06f192020-10-02 16:12:58 +010087 decompressor_.Decompress(buffer_.get() + bytes_written_,
88 kUncompressedBufferSize - bytes_written_);
Lalit Maganti9d538bd2020-03-12 23:48:16 +000089 ret = result.ret;
90 if (ret == ResultCode::kError || ret == ResultCode::kNoProgress)
Lalit Maganti1caf3492020-09-10 21:00:08 +010091 return util::ErrStatus("Failed to decompress trace chunk");
Lalit Maganti9d538bd2020-03-12 23:48:16 +000092
Lalit Maganti1caf3492020-09-10 21:00:08 +010093 if (ret == ResultCode::kNeedsMoreInput) {
Lalit Maganti9d06f192020-10-02 16:12:58 +010094 PERFETTO_DCHECK(result.bytes_written == 0);
95 needs_more_input_ = true;
Lalit Maganti1caf3492020-09-10 21:00:08 +010096 return util::OkStatus();
97 }
Lalit Maganti9d06f192020-10-02 16:12:58 +010098 bytes_written_ += result.bytes_written;
Lalit Maganti1caf3492020-09-10 21:00:08 +010099
Primiano Tucci3264b592021-11-08 18:20:51 +0000100 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 }
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000105 }
106 return util::OkStatus();
107}
108
Lalit Maganti9d06f192020-10-02 16:12:58 +0100109void 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}
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000116
117} // namespace trace_processor
118} // namespace perfetto