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