• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_ZIP_ZIP_TRACE_READER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ZIP_ZIP_TRACE_READER_H_
19 
20 #include <cstddef>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "perfetto/base/status.h"
26 #include "perfetto/ext/base/status_or.h"
27 #include "perfetto/trace_processor/trace_blob_view.h"
28 #include "src/trace_processor/importers/common/chunked_trace_reader.h"
29 #include "src/trace_processor/util/trace_type.h"
30 #include "src/trace_processor/util/zip_reader.h"
31 
32 namespace perfetto::trace_processor {
33 
34 class ForwardingTraceParser;
35 class TraceProcessorContext;
36 
37 // Forwards files contained in a ZIP to the appropiate ChunkedTraceReader. It is
38 // guaranteed that proto traces will be parsed first.
39 class ZipTraceReader : public ChunkedTraceReader {
40  public:
41   explicit ZipTraceReader(TraceProcessorContext* context);
42   ~ZipTraceReader() override;
43 
44   // ChunkedTraceReader implementation
45   util::Status Parse(TraceBlobView) override;
46   void NotifyEndOfFile() override;
47 
48  private:
49   // Represents a file in the ZIP file. Used to sort them before sending the
50   // files one by one to a `ForwardingTraceParser` instance.
51   struct Entry {
52     // File name. Used to break ties.
53     std::string name;
54     // Position in the zip file. Used to break ties.
55     size_t index;
56     // Trace type. This is the main attribute traces are ordered by. Proto
57     // traces are always parsed first as they might contains clock sync
58     // data needed to correctly parse other traces.
59     TraceType trace_type;
60     TraceBlobView uncompressed_data;
61     // True for proto trace_types whose fist message is a ModuleSymbols packet
62     bool has_symbols = false;
63     // Comparator used to determine the order in which files in the ZIP will be
64     // read.
65     bool operator<(const Entry& rhs) const;
66   };
67 
68   base::Status NotifyEndOfFileImpl();
69   base::StatusOr<std::vector<Entry>> ExtractEntries(
70       std::vector<util::ZipFile> files) const;
71   base::Status ParseEntry(Entry entry);
72 
73   TraceProcessorContext* const context_;
74   util::ZipReader zip_reader_;
75   // For every file in the ZIP we will create a `ForwardingTraceParser`instance
76   // and send that file to it for tokenization. The instances are kept around
77   // here as some tokenizers might keep state that is later needed after
78   // sorting.
79   std::vector<std::unique_ptr<ForwardingTraceParser>> parsers_;
80 };
81 
82 }  // namespace perfetto::trace_processor
83 
84 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_ZIP_ZIP_TRACE_READER_H_
85