xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/archive/zip_trace_reader.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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/archive/zip_trace_reader.h"
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <tuple>
25 #include <utility>
26 #include <vector>
27 
28 #include "perfetto/base/logging.h"
29 #include "perfetto/base/status.h"
30 #include "perfetto/ext/base/status_or.h"
31 #include "perfetto/ext/base/string_view.h"
32 #include "perfetto/trace_processor/trace_blob.h"
33 #include "perfetto/trace_processor/trace_blob_view.h"
34 #include "src/trace_processor/forwarding_trace_parser.h"
35 #include "src/trace_processor/importers/android_bugreport/android_bugreport_reader.h"
36 #include "src/trace_processor/importers/archive/archive_entry.h"
37 #include "src/trace_processor/importers/common/trace_file_tracker.h"
38 #include "src/trace_processor/types/trace_processor_context.h"
39 #include "src/trace_processor/util/status_macros.h"
40 #include "src/trace_processor/util/trace_type.h"
41 #include "src/trace_processor/util/zip_reader.h"
42 
43 namespace perfetto::trace_processor {
44 
ZipTraceReader(TraceProcessorContext * context)45 ZipTraceReader::ZipTraceReader(TraceProcessorContext* context)
46     : context_(context) {}
47 ZipTraceReader::~ZipTraceReader() = default;
48 
Parse(TraceBlobView blob)49 base::Status ZipTraceReader::Parse(TraceBlobView blob) {
50   return zip_reader_.Parse(std::move(blob));
51 }
52 
NotifyEndOfFile()53 base::Status ZipTraceReader::NotifyEndOfFile() {
54   std::vector<util::ZipFile> files = zip_reader_.TakeFiles();
55 
56   // Android bug reports are ZIP files and its files do not get handled
57   // separately.
58   if (AndroidBugreportReader::IsAndroidBugReport(files)) {
59     return AndroidBugreportReader::Parse(context_, std::move(files));
60   }
61 
62   // TODO(carlscab): There is a lot of unnecessary copying going on here.
63   // ZipTraceReader can directly parse the ZIP file and given that we know the
64   // decompressed size we could directly decompress into TraceBlob chunks and
65   // send them to the tokenizer.
66   std::vector<uint8_t> buffer;
67   std::map<ArchiveEntry, File> ordered_files;
68   for (size_t i = 0; i < files.size(); ++i) {
69     util::ZipFile& zip_file = files[i];
70     auto id = context_->trace_file_tracker->AddFile(zip_file.name());
71     context_->trace_file_tracker->SetSize(id, zip_file.compressed_size());
72     RETURN_IF_ERROR(files[i].Decompress(&buffer));
73     TraceBlobView data(TraceBlob::CopyFrom(buffer.data(), buffer.size()));
74     ArchiveEntry entry{zip_file.name(), i,
75                        GuessTraceType(data.data(), data.size())};
76     ordered_files.emplace(entry, File{id, std::move(data)});
77   }
78 
79   for (auto& file : ordered_files) {
80     auto chunk_reader =
81         std::make_unique<ForwardingTraceParser>(context_, file.second.id);
82     auto& parser = *chunk_reader;
83     context_->chunk_readers.push_back(std::move(chunk_reader));
84 
85     RETURN_IF_ERROR(parser.Parse(std::move(file.second.data)));
86     RETURN_IF_ERROR(parser.NotifyEndOfFile());
87     // Make sure the ForwardingTraceParser determined the same trace type as we
88     // did.
89     PERFETTO_CHECK(parser.trace_type() == file.first.trace_type);
90   }
91 
92   return base::OkStatus();
93 }
94 
95 }  // namespace perfetto::trace_processor
96