1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PMU_H 3 #define __PMU_H 4 5 #include <linux/bitmap.h> 6 #include <linux/compiler.h> 7 #include <linux/perf_event.h> 8 #include <linux/list.h> 9 #include <stdbool.h> 10 #include <stdio.h> 11 #include "parse-events.h" 12 #include "pmu-events/pmu-events.h" 13 #include "map_symbol.h" 14 #include "mem-events.h" 15 16 struct evsel_config_term; 17 struct perf_cpu_map; 18 struct print_callbacks; 19 20 enum { 21 PERF_PMU_FORMAT_VALUE_CONFIG, 22 PERF_PMU_FORMAT_VALUE_CONFIG1, 23 PERF_PMU_FORMAT_VALUE_CONFIG2, 24 PERF_PMU_FORMAT_VALUE_CONFIG3, 25 PERF_PMU_FORMAT_VALUE_CONFIG_END, 26 }; 27 28 #define PERF_PMU_FORMAT_BITS 64 29 #define MAX_PMU_NAME_LEN 128 30 31 struct perf_event_attr; 32 33 struct perf_pmu_caps { 34 char *name; 35 char *value; 36 struct list_head list; 37 }; 38 39 enum { 40 PERF_PMU_TYPE_TOOL = 0xFFFFFFFE, 41 PERF_PMU_TYPE_FAKE = 0xFFFFFFFF, 42 }; 43 44 /** 45 * struct perf_pmu 46 */ 47 struct perf_pmu { 48 /** @name: The name of the PMU such as "cpu". */ 49 const char *name; 50 /** 51 * @alias_name: Optional alternate name for the PMU determined in 52 * architecture specific code. 53 */ 54 char *alias_name; 55 /** 56 * @id: Optional PMU identifier read from 57 * <sysfs>/bus/event_source/devices/<name>/identifier. 58 */ 59 const char *id; 60 /** 61 * @type: Perf event attributed type value, read from 62 * <sysfs>/bus/event_source/devices/<name>/type. 63 */ 64 __u32 type; 65 /** 66 * @selectable: Can the PMU name be selected as if it were an event? 67 */ 68 bool selectable; 69 /** 70 * @is_core: Is the PMU the core CPU PMU? Determined by the name being 71 * "cpu" or by the presence of 72 * <sysfs>/bus/event_source/devices/<name>/cpus. There may be >1 core 73 * PMU on systems like Intel hybrid. 74 */ 75 bool is_core; 76 /** 77 * @is_uncore: Is the PMU not within the CPU core? Determined by the 78 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask. 79 */ 80 bool is_uncore; 81 /** 82 * @auxtrace: Are events auxiliary events? Determined in architecture 83 * specific code. 84 */ 85 bool auxtrace; 86 /** 87 * @formats_checked: Only check PMU's formats are valid for 88 * perf_event_attr once. 89 */ 90 bool formats_checked; 91 /** @config_masks_present: Are there config format values? */ 92 bool config_masks_present; 93 /** @config_masks_computed: Set when masks are lazily computed. */ 94 bool config_masks_computed; 95 /** 96 * @max_precise: Number of levels of :ppp precision supported by the 97 * PMU, read from 98 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise. 99 */ 100 int max_precise; 101 /** 102 * @perf_event_attr_init_default: Optional function to default 103 * initialize PMU specific parts of the perf_event_attr. 104 */ 105 void (*perf_event_attr_init_default)(const struct perf_pmu *pmu, 106 struct perf_event_attr *attr); 107 /** 108 * @cpus: Empty or the contents of either of: 109 * <sysfs>/bus/event_source/devices/<name>/cpumask. 110 * <sysfs>/bus/event_source/devices/<cpu>/cpus. 111 */ 112 struct perf_cpu_map *cpus; 113 /** 114 * @format: Holds the contents of files read from 115 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify 116 * which event parameter changes what config, config1 or config2 bits. 117 */ 118 struct list_head format; 119 /** 120 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an 121 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or 122 * from json events in pmu-events.c. 123 */ 124 struct list_head aliases; 125 /** 126 * @events_table: The events table for json events in pmu-events.c. 127 */ 128 const struct pmu_events_table *events_table; 129 /** @sysfs_aliases: Number of sysfs aliases loaded. */ 130 uint32_t sysfs_aliases; 131 /** @cpu_json_aliases: Number of json event aliases loaded specific to the CPUID. */ 132 uint32_t cpu_json_aliases; 133 /** @sys_json_aliases: Number of json event aliases loaded matching the PMU's identifier. */ 134 uint32_t sys_json_aliases; 135 /** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */ 136 bool sysfs_aliases_loaded; 137 /** 138 * @cpu_aliases_added: Have all json events table entries for the PMU 139 * been added? 140 */ 141 bool cpu_aliases_added; 142 /** @caps_initialized: Has the list caps been initialized? */ 143 bool caps_initialized; 144 /** @nr_caps: The length of the list caps. */ 145 u32 nr_caps; 146 /** 147 * @caps: Holds the contents of files read from 148 * <sysfs>/bus/event_source/devices/<name>/caps/. 149 * 150 * The contents are pairs of the filename with the value of its 151 * contents, for example, max_precise (see above) may have a value of 3. 152 */ 153 struct list_head caps; 154 /** @list: Element on pmus list in pmu.c. */ 155 struct list_head list; 156 157 /** 158 * @config_masks: Derived from the PMU's format data, bits that are 159 * valid within the config value. 160 */ 161 __u64 config_masks[PERF_PMU_FORMAT_VALUE_CONFIG_END]; 162 163 /** 164 * @missing_features: Features to inhibit when events on this PMU are 165 * opened. 166 */ 167 struct { 168 /** 169 * @exclude_guest: Disables perf_event_attr exclude_guest and 170 * exclude_host. 171 */ 172 bool exclude_guest; 173 /** 174 * @checked: Are the missing features checked? 175 */ 176 bool checked; 177 } missing_features; 178 179 /** 180 * @mem_events: List of the supported mem events 181 */ 182 struct perf_mem_event *mem_events; 183 }; 184 185 struct perf_pmu_info { 186 const char *unit; 187 double scale; 188 bool per_pkg; 189 bool snapshot; 190 }; 191 192 struct pmu_event_info { 193 const struct perf_pmu *pmu; 194 const char *name; 195 const char* alias; 196 const char *scale_unit; 197 const char *desc; 198 const char *long_desc; 199 const char *encoding_desc; 200 const char *topic; 201 const char *pmu_name; 202 const char *event_type_desc; 203 const char *str; 204 bool deprecated; 205 }; 206 207 typedef int (*pmu_event_callback)(void *state, struct pmu_event_info *info); 208 typedef int (*pmu_format_callback)(void *state, const char *name, int config, 209 const unsigned long *bits); 210 211 void pmu_add_sys_aliases(struct perf_pmu *pmu); 212 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 213 struct parse_events_terms *head_terms, 214 bool apply_hardcoded, 215 struct parse_events_error *error); 216 int perf_pmu__config_terms(const struct perf_pmu *pmu, 217 struct perf_event_attr *attr, 218 struct parse_events_terms *terms, 219 bool zero, bool apply_hardcoded, 220 struct parse_events_error *error); 221 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name); 222 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name); 223 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms, 224 struct perf_pmu_info *info, bool *rewrote_terms, 225 u64 *alternate_hw_config, struct parse_events_error *err); 226 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb); 227 228 void perf_pmu_format__set_value(void *format, int config, unsigned long *bits); 229 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name); 230 int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb); 231 232 bool is_pmu_core(const char *name); 233 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu); 234 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu); 235 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name); 236 size_t perf_pmu__num_events(struct perf_pmu *pmu); 237 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus, 238 void *state, pmu_event_callback cb); 239 bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name); 240 241 /** 242 * perf_pmu_is_software - is the PMU a software PMU as in it uses the 243 * perf_sw_context in the kernel? 244 */ 245 bool perf_pmu__is_software(const struct perf_pmu *pmu); 246 247 FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name); 248 FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name); 249 250 int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt, ...) 251 __scanf(3, 4); 252 int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name, 253 const char *fmt, ...) __scanf(4, 5); 254 255 bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name); 256 257 int perf_pmu__test(void); 258 259 void perf_pmu__arch_init(struct perf_pmu *pmu); 260 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, 261 const struct pmu_events_table *table); 262 263 char *perf_pmu__getcpuid(struct perf_pmu *pmu); 264 const struct pmu_metrics_table *pmu_metrics_table__find(void); 265 bool pmu_uncore_identifier_match(const char *compat, const char *id); 266 267 int perf_pmu__convert_scale(const char *scale, char **end, double *sval); 268 269 int perf_pmu__caps_parse(struct perf_pmu *pmu); 270 271 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 272 const char *name, int config_num, 273 const char *config_name); 274 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu); 275 276 bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok); 277 278 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); 279 int perf_pmu__pathname_scnprintf(char *buf, size_t size, 280 const char *pmu_name, const char *filename); 281 int perf_pmu__event_source_devices_fd(void); 282 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); 283 284 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name, 285 bool eager_load); 286 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus); 287 void perf_pmu__delete(struct perf_pmu *pmu); 288 struct perf_pmu *perf_pmus__find_core_pmu(void); 289 290 const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config); 291 bool perf_pmu__is_fake(const struct perf_pmu *pmu); 292 293 #endif /* __PMU_H */ 294