xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/perf_data_tokenizer.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 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_PERF_PERF_DATA_TOKENIZER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_DATA_TOKENIZER_H_
19 
20 #include <stdint.h>
21 #include <cstdint>
22 #include <map>
23 #include <memory>
24 #include <optional>
25 #include <vector>
26 
27 #include "perfetto/base/flat_set.h"
28 #include "perfetto/base/status.h"
29 #include "perfetto/ext/base/circular_queue.h"
30 #include "perfetto/ext/base/flat_hash_map.h"
31 #include "perfetto/ext/base/status_or.h"
32 #include "perfetto/trace_processor/ref_counted.h"
33 #include "perfetto/trace_processor/trace_blob_view.h"
34 #include "src/trace_processor/importers/common/chunked_trace_reader.h"
35 #include "src/trace_processor/importers/perf/aux_data_tokenizer.h"
36 #include "src/trace_processor/importers/perf/aux_stream_manager.h"
37 #include "src/trace_processor/importers/perf/auxtrace_info_record.h"
38 #include "src/trace_processor/importers/perf/auxtrace_record.h"
39 #include "src/trace_processor/importers/perf/perf_file.h"
40 #include "src/trace_processor/importers/perf/perf_session.h"
41 #include "src/trace_processor/util/trace_blob_view_reader.h"
42 
43 namespace perfetto {
44 namespace trace_processor {
45 class TraceProcessorContext;
46 
47 namespace perf_importer {
48 
49 class AuxDataTokenizer;
50 class AuxDataTokenizerFactory;
51 struct Record;
52 class SampleId;
53 struct AuxRecord;
54 
55 class PerfDataTokenizer : public ChunkedTraceReader {
56  public:
57   explicit PerfDataTokenizer(TraceProcessorContext*);
58   ~PerfDataTokenizer() override;
59   PerfDataTokenizer(const PerfDataTokenizer&) = delete;
60   PerfDataTokenizer& operator=(const PerfDataTokenizer&) = delete;
61 
62   // ChunkedTraceReader implementation
63   base::Status Parse(TraceBlobView) override;
64   base::Status NotifyEndOfFile() override;
65 
66  private:
67   enum class ParsingState {
68     kParseHeader,
69     kParseAttrs,
70     kSeekRecords,
71     kParseRecords,
72     kParseAuxtraceData,
73     kParseFeatureSections,
74     kParseFeatures,
75     kDone,
76   };
77   enum class ParsingResult { kMoreDataNeeded = 0, kSuccess = 1 };
78 
79   base::StatusOr<ParsingResult> ParseHeader();
80   base::StatusOr<ParsingResult> ParseAttrs();
81   base::StatusOr<ParsingResult> SeekRecords();
82   base::StatusOr<ParsingResult> ParseRecords();
83   base::StatusOr<ParsingResult> ParseAuxtraceData();
84   base::StatusOr<ParsingResult> ParseFeatureSections();
85   base::StatusOr<ParsingResult> ParseFeatures();
86 
87   base::StatusOr<PerfDataTokenizer::ParsingResult> ParseRecord(Record& record);
88   void MaybePushRecord(Record record);
89   base::Status ParseFeature(uint8_t feature_id, TraceBlobView payload);
90 
91   base::Status ProcessRecord(Record record);
92   base::Status ProcessAuxRecord(Record record);
93   base::Status ProcessAuxtraceInfoRecord(Record record);
94   base::Status ProcessTimeConvRecord(Record record);
95   base::Status ProcessItraceStartRecord(Record record);
96 
97   base::StatusOr<int64_t> ExtractTraceTimestamp(const Record& record);
98 
99   TraceProcessorContext* context_;
100 
101   ParsingState parsing_state_ = ParsingState::kParseHeader;
102 
103   PerfFile::Header header_;
104   base::FlatSet<uint8_t> feature_ids_;
105   PerfFile::Section feature_headers_section_;
106   // Sections for the features present in the perf file sorted by descending
107   // section offset. This is done so that we can pop from the back as we process
108   // the sections.
109   std::vector<std::pair<uint8_t, PerfFile::Section>> feature_sections_;
110 
111   RefPtr<PerfSession> perf_session_;
112 
113   util::TraceBlobViewReader buffer_;
114 
115   int64_t latest_timestamp_ = 0;
116 
117   std::optional<AuxtraceRecord> current_auxtrace_;
118   AuxStreamManager aux_manager_;
119 };
120 
121 }  // namespace perf_importer
122 }  // namespace trace_processor
123 }  // namespace perfetto
124 
125 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_DATA_TOKENIZER_H_
126