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 14 struct evsel_config_term; 15 struct perf_cpu_map; 16 struct print_callbacks; 17 18 enum { 19 PERF_PMU_FORMAT_VALUE_CONFIG, 20 PERF_PMU_FORMAT_VALUE_CONFIG1, 21 PERF_PMU_FORMAT_VALUE_CONFIG2, 22 PERF_PMU_FORMAT_VALUE_CONFIG3, 23 PERF_PMU_FORMAT_VALUE_CONFIG_END, 24 }; 25 26 #define PERF_PMU_FORMAT_BITS 64 27 #define MAX_PMU_NAME_LEN 128 28 29 struct perf_event_attr; 30 31 struct perf_pmu_caps { 32 char *name; 33 char *value; 34 struct list_head list; 35 }; 36 37 /** 38 * struct perf_pmu 39 */ 40 struct perf_pmu { 41 /** @name: The name of the PMU such as "cpu". */ 42 char *name; 43 /** 44 * @alias_name: Optional alternate name for the PMU determined in 45 * architecture specific code. 46 */ 47 char *alias_name; 48 /** 49 * @id: Optional PMU identifier read from 50 * <sysfs>/bus/event_source/devices/<name>/identifier. 51 */ 52 char *id; 53 /** 54 * @type: Perf event attributed type value, read from 55 * <sysfs>/bus/event_source/devices/<name>/type. 56 */ 57 __u32 type; 58 /** 59 * @selectable: Can the PMU name be selected as if it were an event? 60 */ 61 bool selectable; 62 /** 63 * @is_core: Is the PMU the core CPU PMU? Determined by the name being 64 * "cpu" or by the presence of 65 * <sysfs>/bus/event_source/devices/<name>/cpus. There may be >1 core 66 * PMU on systems like Intel hybrid. 67 */ 68 bool is_core; 69 /** 70 * @is_uncore: Is the PMU not within the CPU core? Determined by the 71 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask. 72 */ 73 bool is_uncore; 74 /** 75 * @auxtrace: Are events auxiliary events? Determined in architecture 76 * specific code. 77 */ 78 bool auxtrace; 79 /** 80 * @max_precise: Number of levels of :ppp precision supported by the 81 * PMU, read from 82 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise. 83 */ 84 int max_precise; 85 /** 86 * @default_config: Optional default perf_event_attr determined in 87 * architecture specific code. 88 */ 89 struct perf_event_attr *default_config; 90 /** 91 * @cpus: Empty or the contents of either of: 92 * <sysfs>/bus/event_source/devices/<name>/cpumask. 93 * <sysfs>/bus/event_source/devices/<cpu>/cpus. 94 */ 95 struct perf_cpu_map *cpus; 96 /** 97 * @format: Holds the contents of files read from 98 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify 99 * which event parameter changes what config, config1 or config2 bits. 100 */ 101 struct list_head format; 102 /** 103 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an 104 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or 105 * from json events in pmu-events.c. 106 */ 107 struct list_head aliases; 108 /** @caps_initialized: Has the list caps been initialized? */ 109 bool caps_initialized; 110 /** @nr_caps: The length of the list caps. */ 111 u32 nr_caps; 112 /** 113 * @caps: Holds the contents of files read from 114 * <sysfs>/bus/event_source/devices/<name>/caps/. 115 * 116 * The contents are pairs of the filename with the value of its 117 * contents, for example, max_precise (see above) may have a value of 3. 118 */ 119 struct list_head caps; 120 /** @list: Element on pmus list in pmu.c. */ 121 struct list_head list; 122 /** @hybrid_list: Element on perf_pmu__hybrid_pmus. */ 123 struct list_head hybrid_list; 124 125 /** 126 * @missing_features: Features to inhibit when events on this PMU are 127 * opened. 128 */ 129 struct { 130 /** 131 * @exclude_guest: Disables perf_event_attr exclude_guest and 132 * exclude_host. 133 */ 134 bool exclude_guest; 135 } missing_features; 136 }; 137 138 /** @perf_pmu__fake: A special global PMU used for testing. */ 139 extern struct perf_pmu perf_pmu__fake; 140 141 struct perf_pmu_info { 142 const char *unit; 143 double scale; 144 bool per_pkg; 145 bool snapshot; 146 }; 147 148 #define UNIT_MAX_LEN 31 /* max length for event unit name */ 149 150 /** 151 * struct perf_pmu_alias - An event either read from sysfs or builtin in 152 * pmu-events.c, created by parsing the pmu-events json files. 153 */ 154 struct perf_pmu_alias { 155 /** @name: Name of the event like "mem-loads". */ 156 char *name; 157 /** @desc: Optional short description of the event. */ 158 char *desc; 159 /** @long_desc: Optional long description. */ 160 char *long_desc; 161 /** 162 * @topic: Optional topic such as cache or pipeline, particularly for 163 * json events. 164 */ 165 char *topic; 166 /** 167 * @str: Comma separated parameter list like 168 * "event=0xcd,umask=0x1,ldlat=0x3". 169 */ 170 char *str; 171 /** @terms: Owned list of the original parsed parameters. */ 172 struct list_head terms; 173 /** @list: List element of struct perf_pmu aliases. */ 174 struct list_head list; 175 /** @unit: Units for the event, such as bytes or cache lines. */ 176 char unit[UNIT_MAX_LEN+1]; 177 /** @scale: Value to scale read counter values by. */ 178 double scale; 179 /** 180 * @per_pkg: Does the file 181 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or 182 * equivalent json value exist and have the value 1. 183 */ 184 bool per_pkg; 185 /** 186 * @snapshot: Does the file 187 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot 188 * exist and have the value 1. 189 */ 190 bool snapshot; 191 /** 192 * @deprecated: Is the event hidden and so not shown in perf list by 193 * default. 194 */ 195 bool deprecated; 196 /** 197 * @pmu_name: The name copied from the json struct pmu_event. This can 198 * differ from the PMU name as it won't have suffixes. 199 */ 200 char *pmu_name; 201 }; 202 203 struct perf_pmu *perf_pmu__find(const char *name); 204 struct perf_pmu *perf_pmu__find_by_type(unsigned int type); 205 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu); 206 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 207 struct list_head *head_terms, 208 struct parse_events_error *error); 209 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 210 struct perf_event_attr *attr, 211 struct list_head *head_terms, 212 bool zero, struct parse_events_error *error); 213 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name); 214 int perf_pmu__format_type(struct list_head *formats, const char *name); 215 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 216 struct perf_pmu_info *info); 217 struct list_head *perf_pmu__alias(struct perf_pmu *pmu, 218 struct list_head *head_terms); 219 void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg); 220 221 int perf_pmu__new_format(struct list_head *list, char *name, 222 int config, unsigned long *bits); 223 void perf_pmu__set_format(unsigned long *bits, long from, long to); 224 int perf_pmu__format_parse(int dirfd, struct list_head *head); 225 void perf_pmu__del_formats(struct list_head *formats); 226 227 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu); 228 229 bool is_pmu_core(const char *name); 230 bool is_pmu_hybrid(const char *name); 231 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu); 232 bool perf_pmu__supports_wildcard_numeric(const struct perf_pmu *pmu); 233 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu); 234 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state); 235 bool pmu_have_event(const char *pname, const char *name); 236 237 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); 238 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name); 239 240 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4); 241 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, 242 const char *fmt, ...) __scanf(4, 5); 243 244 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name); 245 246 int perf_pmu__test(void); 247 248 struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu); 249 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu, 250 const struct pmu_events_table *table); 251 252 char *perf_pmu__getcpuid(struct perf_pmu *pmu); 253 const struct pmu_events_table *pmu_events_table__find(void); 254 const struct pmu_metrics_table *pmu_metrics_table__find(void); 255 void perf_pmu_free_alias(struct perf_pmu_alias *alias); 256 257 int perf_pmu__convert_scale(const char *scale, char **end, double *sval); 258 259 int perf_pmu__caps_parse(struct perf_pmu *pmu); 260 261 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 262 const char *name); 263 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu); 264 265 bool perf_pmu__has_hybrid(void); 266 int perf_pmu__match(char *pattern, char *name, char *tok); 267 268 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus, 269 struct perf_cpu_map **mcpus_ptr, 270 struct perf_cpu_map **ucpus_ptr); 271 272 char *pmu_find_real_name(const char *name); 273 char *pmu_find_alias_name(const char *name); 274 double perf_pmu__cpu_slots_per_cycle(void); 275 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); 276 int perf_pmu__pathname_scnprintf(char *buf, size_t size, 277 const char *pmu_name, const char *filename); 278 int perf_pmu__event_source_devices_fd(void); 279 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); 280 281 void perf_pmu__destroy(void); 282 283 #endif /* __PMU_H */ 284