xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/spe_record_parser.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_PERF_SPE_RECORD_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_SPE_RECORD_PARSER_H_
19 
20 #include <array>
21 #include <cstddef>
22 #include <cstdint>
23 
24 #include "perfetto/trace_processor/trace_blob_view.h"
25 #include "src/trace_processor/importers/common/trace_parser.h"
26 #include "src/trace_processor/importers/common/virtual_memory_mapping.h"
27 #include "src/trace_processor/importers/perf/reader.h"
28 #include "src/trace_processor/importers/perf/spe.h"
29 #include "src/trace_processor/storage/trace_storage.h"
30 #include "src/trace_processor/tables/perf_tables_py.h"
31 
32 namespace perfetto::trace_processor {
33 class TraceProcessorContext;
34 namespace perf_importer {
35 
36 class SpeRecordParserImpl : public SpeRecordParser {
37  public:
38   explicit SpeRecordParserImpl(TraceProcessorContext* context);
39 
40   void ParseSpeRecord(int64_t, TraceBlobView) override;
41 
42  private:
43   template <typename Enum>
44   class CachedStringIdArray {
45    public:
46     static constexpr size_t size = static_cast<size_t>(Enum::kMax) + 1;
CachedStringIdArray()47     explicit CachedStringIdArray() { cache_.fill(kNullStringId); }
48     StringId& operator[](Enum e) { return cache_[static_cast<size_t>(e)]; }
49 
50    private:
51     std::array<StringId, size> cache_;
52   };
53 
54   struct InflightSpeRecord {
55     std::optional<spe::InstructionVirtualAddress> instruction_address;
56   };
57 
58   enum class OperationName {
59     kOther,
60     kSveVecOp,
61     kLoad,
62     kStore,
63     kBranch,
64     kUnknown,
65     kMax = kUnknown
66   };
67 
68   static const char* ToString(OperationName name);
69   static const char* ToString(spe::ExceptionLevel el);
70   static const char* ToString(spe::DataSource ds);
71 
72   StringId ToStringId(OperationName name);
73   StringId ToStringId(spe::ExceptionLevel el);
74   StringId ToStringId(spe::DataSource ds);
75 
76   void ReadShortPacket(spe::ShortHeader short_header);
77   void ReadExtendedPacket(spe::ExtendedHeader extended_header);
78 
79   void ReadAddressPacket(spe::AddressIndex index);
80   void ReadCounterPacket(spe::CounterIndex index);
81 
82   void ReadEventsPacket(spe::ShortHeader short_header);
83   void ReadContextPacket(spe::ShortHeader short_header);
84   void ReadOperationTypePacket(spe::ShortHeader short_header);
85   void ReadDataSourcePacket(spe::ShortHeader short_header);
86 
87   uint64_t ReadPayload(spe::ShortHeader short_header);
88 
89   OperationName GetOperationName(spe::ShortHeader short_header,
90                                  uint8_t payload) const;
91 
92   VirtualMemoryMapping* GetDummyMapping();
93 
94   TraceProcessorContext* const context_;
95   CachedStringIdArray<OperationName> operation_name_strings_;
96   CachedStringIdArray<spe::DataSource> data_source_strings_;
97   CachedStringIdArray<spe::ExceptionLevel> exception_level_strings_;
98 
99   Reader reader_;
100   tables::SpeRecordTable::Row inflight_row_;
101   InflightSpeRecord inflight_record_;
102 
103   VirtualMemoryMapping* dummy_mapping_ = nullptr;
104 };
105 
106 }  // namespace perf_importer
107 }  // namespace perfetto::trace_processor
108 
109 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_SPE_RECORD_PARSER_H_
110