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 123 /** 124 * @missing_features: Features to inhibit when events on this PMU are 125 * opened. 126 */ 127 struct { 128 /** 129 * @exclude_guest: Disables perf_event_attr exclude_guest and 130 * exclude_host. 131 */ 132 bool exclude_guest; 133 } missing_features; 134 }; 135 136 /** @perf_pmu__fake: A special global PMU used for testing. */ 137 extern struct perf_pmu perf_pmu__fake; 138 139 struct perf_pmu_info { 140 const char *unit; 141 double scale; 142 bool per_pkg; 143 bool snapshot; 144 }; 145 146 #define UNIT_MAX_LEN 31 /* max length for event unit name */ 147 148 /** 149 * struct perf_pmu_alias - An event either read from sysfs or builtin in 150 * pmu-events.c, created by parsing the pmu-events json files. 151 */ 152 struct perf_pmu_alias { 153 /** @name: Name of the event like "mem-loads". */ 154 char *name; 155 /** @desc: Optional short description of the event. */ 156 char *desc; 157 /** @long_desc: Optional long description. */ 158 char *long_desc; 159 /** 160 * @topic: Optional topic such as cache or pipeline, particularly for 161 * json events. 162 */ 163 char *topic; 164 /** 165 * @str: Comma separated parameter list like 166 * "event=0xcd,umask=0x1,ldlat=0x3". 167 */ 168 char *str; 169 /** @terms: Owned list of the original parsed parameters. */ 170 struct list_head terms; 171 /** @list: List element of struct perf_pmu aliases. */ 172 struct list_head list; 173 /** @unit: Units for the event, such as bytes or cache lines. */ 174 char unit[UNIT_MAX_LEN+1]; 175 /** @scale: Value to scale read counter values by. */ 176 double scale; 177 /** 178 * @per_pkg: Does the file 179 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or 180 * equivalent json value exist and have the value 1. 181 */ 182 bool per_pkg; 183 /** 184 * @snapshot: Does the file 185 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot 186 * exist and have the value 1. 187 */ 188 bool snapshot; 189 /** 190 * @deprecated: Is the event hidden and so not shown in perf list by 191 * default. 192 */ 193 bool deprecated; 194 /** 195 * @pmu_name: The name copied from the json struct pmu_event. This can 196 * differ from the PMU name as it won't have suffixes. 197 */ 198 char *pmu_name; 199 }; 200 201 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu); 202 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 203 struct list_head *head_terms, 204 struct parse_events_error *error); 205 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 206 struct perf_event_attr *attr, 207 struct list_head *head_terms, 208 bool zero, struct parse_events_error *error); 209 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name); 210 int perf_pmu__format_type(struct list_head *formats, const char *name); 211 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 212 struct perf_pmu_info *info); 213 struct list_head *perf_pmu__alias(struct perf_pmu *pmu, 214 struct list_head *head_terms); 215 void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg); 216 217 int perf_pmu__new_format(struct list_head *list, char *name, 218 int config, unsigned long *bits); 219 void perf_pmu__set_format(unsigned long *bits, long from, long to); 220 int perf_pmu__format_parse(int dirfd, struct list_head *head); 221 void perf_pmu__del_formats(struct list_head *formats); 222 223 bool is_pmu_core(const char *name); 224 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu); 225 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu); 226 bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name); 227 228 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); 229 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name); 230 231 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4); 232 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, 233 const char *fmt, ...) __scanf(4, 5); 234 235 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name); 236 237 int perf_pmu__test(void); 238 239 struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu); 240 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu, 241 const struct pmu_events_table *table); 242 243 char *perf_pmu__getcpuid(struct perf_pmu *pmu); 244 const struct pmu_events_table *pmu_events_table__find(void); 245 const struct pmu_metrics_table *pmu_metrics_table__find(void); 246 void perf_pmu_free_alias(struct perf_pmu_alias *alias); 247 248 int perf_pmu__convert_scale(const char *scale, char **end, double *sval); 249 250 int perf_pmu__caps_parse(struct perf_pmu *pmu); 251 252 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 253 const char *name); 254 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu); 255 256 int perf_pmu__match(char *pattern, char *name, char *tok); 257 258 char *pmu_find_real_name(const char *name); 259 char *pmu_find_alias_name(const char *name); 260 double perf_pmu__cpu_slots_per_cycle(void); 261 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); 262 int perf_pmu__pathname_scnprintf(char *buf, size_t size, 263 const char *pmu_name, const char *filename); 264 int perf_pmu__event_source_devices_fd(void); 265 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); 266 267 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name); 268 void perf_pmu__delete(struct perf_pmu *pmu); 269 270 #endif /* __PMU_H */ 271