xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/features.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_FEATURES_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_FEATURES_H_
19 
20 #include <cstdint>
21 #include <functional>
22 #include <limits>
23 #include <string>
24 #include <vector>
25 
26 #include "perfetto/ext/base/flat_hash_map.h"
27 #include "perfetto/ext/base/hash.h"
28 #include "perfetto/ext/base/status_or.h"
29 #include "perfetto/trace_processor/status.h"
30 #include "src/trace_processor/importers/perf/perf_event.h"
31 
32 namespace perfetto ::trace_processor {
33 class TraceBlobView;
34 
35 namespace perf_importer::feature {
36 
37 enum Id : uint8_t {
38   ID_RESERVED = 0,
39   ID_TRACING_DATA = 1,
40   ID_BUILD_ID = 2,
41   ID_HOSTNAME = 3,
42   ID_OS_RELEASE = 4,
43   ID_VERSION = 5,
44   ID_ARCH = 6,
45   ID_NR_CPUS = 7,
46   ID_CPU_DESC = 8,
47   ID_CPU_ID = 9,
48   ID_TOTAL_MEM = 10,
49   ID_CMD_LINE = 11,
50   ID_EVENT_DESC = 12,
51   ID_CPU_TOPOLOGY = 13,
52   ID_NUMA_TOPOLOGY = 14,
53   ID_BRANCH_STACK = 15,
54   ID_PMU_MAPPINGS = 16,
55   ID_GROUP_DESC = 17,
56   ID_AUX_TRACE = 18,
57   ID_STAT = 19,
58   ID_CACHE = 20,
59   ID_SAMPLE_TIME = 21,
60   ID_SAMPLE_TOPOLOGY = 22,
61   ID_CLOCK_ID = 23,
62   ID_DIR_FORMAT = 24,
63   ID_BPF_PROG_INFO = 25,
64   ID_BPF_BTF = 26,
65   ID_COMPRESSED = 27,
66   ID_CPU_PUM_CAPS = 28,
67   ID_CLOCK_DATA = 29,
68   ID_HYBRID_TOPOLOGY = 30,
69   ID_PMU_CAPS = 31,
70   ID_SIMPLEPERF_FILE = 128,
71   ID_SIMPLEPERF_META_INFO = 129,
72   ID_SIMPLEPERF_FILE2 = 132,
73   ID_MAX = std::numeric_limits<uint8_t>::max(),
74 };
75 
76 struct BuildId {
77   static util::Status Parse(TraceBlobView,
78                             std::function<util::Status(BuildId)> cb);
79   int32_t pid;
80   std::string build_id;
81   std::string filename;
82 };
83 
84 struct HeaderGroupDesc {
85   static util::Status Parse(TraceBlobView, HeaderGroupDesc& out);
86   struct Entry {
87     std::string string;
88     uint32_t leader_idx;
89     uint32_t nr_members;
90   };
91   std::vector<Entry> entries;
92 };
93 
94 struct EventDescription {
95   static util::Status Parse(TraceBlobView,
96                             std::function<util::Status(EventDescription)> cb);
97   perf_event_attr attr;
98   std::string event_string;
99   std::vector<uint64_t> ids;
100 };
101 
102 struct SimpleperfMetaInfo {
103   static util::Status Parse(const TraceBlobView&, SimpleperfMetaInfo& out);
104   base::FlatHashMap<std::string, std::string> entries;
105   struct EventTypeAndConfig {
106     uint32_t type;
107     uint64_t config;
108     bool operator==(const EventTypeAndConfig& other) {
109       return type == other.type && config == other.config;
110     }
111     bool operator!=(const EventTypeAndConfig& other) {
112       return !(*this == other);
113     }
114     struct Hasher {
operatorSimpleperfMetaInfo::EventTypeAndConfig::Hasher115       size_t operator()(const EventTypeAndConfig& o) const {
116         return static_cast<size_t>(base::Hasher::Combine(o.config, o.type));
117       }
118     };
119   };
120   using EventName = std::string;
121   base::FlatHashMap<EventTypeAndConfig, EventName, EventTypeAndConfig::Hasher>
122       event_type_info;
123 };
124 
125 util::Status ParseSimpleperfFile2(TraceBlobView,
126                                   std::function<void(TraceBlobView)> cb);
127 
128 base::StatusOr<std::vector<std::string>> ParseCmdline(TraceBlobView blob);
129 
130 }  // namespace perf_importer::feature
131 
132 }  // namespace perfetto::trace_processor
133 
134 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_FEATURES_H_
135