xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/archive/tar_trace_reader.h (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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_ARCHIVE_TAR_TRACE_READER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ARCHIVE_TAR_TRACE_READER_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <map>
23 
24 #include "perfetto/base/status.h"
25 #include "perfetto/ext/base/status_or.h"
26 #include "perfetto/trace_processor/trace_blob_view.h"
27 #include "src/trace_processor/importers/archive/archive_entry.h"
28 #include "src/trace_processor/importers/common/chunked_trace_reader.h"
29 #include "src/trace_processor/tables/metadata_tables_py.h"
30 #include "src/trace_processor/util/trace_blob_view_reader.h"
31 
32 namespace perfetto::trace_processor {
33 
34 class TraceProcessorContext;
35 
36 class TarTraceReader : public ChunkedTraceReader {
37  public:
38   explicit TarTraceReader(TraceProcessorContext*);
39   ~TarTraceReader() override;
40 
41   // ChunkedTraceReader implementation
42   base::Status Parse(TraceBlobView) override;
43   base::Status NotifyEndOfFile() override;
44 
45  private:
46   struct Metadata {
47     std::string name;
48     uint64_t size;
49     char type_flag;
50   };
51   enum class ParseResult {
52     kOk,
53     kNeedsMoreData,
54   };
55 
56   struct File {
57     tables::TraceFileTable::Id id;
58     std::vector<TraceBlobView> data;
59   };
60 
61   enum class State { kMetadata, kContent, kZeroMetadata, kDone };
62 
63   base::StatusOr<ParseResult> ParseMetadata();
64   base::StatusOr<ParseResult> ParseContent();
65   base::StatusOr<ParseResult> ParseLongName();
66   base::StatusOr<ParseResult> ParsePadding();
67 
68   void AddFile(const Metadata& metadata,
69                TraceBlobView header,
70                std::vector<TraceBlobView> data);
71 
72   TraceProcessorContext* const context_;
73   State state_{State::kMetadata};
74   util::TraceBlobViewReader buffer_;
75   std::optional<Metadata> metadata_;
76   std::optional<std::string> long_name_;
77   std::map<ArchiveEntry, File> ordered_files_;
78 };
79 
80 }  // namespace perfetto::trace_processor
81 
82 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_ARCHIVE_TAR_TRACE_READER_H_
83