Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 2 | #include <linux/list.h> |
Sukadev Bhattiprolu | c5de47f | 2015-06-10 00:25:07 -0700 | [diff] [blame] | 3 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 3285848 | 2019-06-26 11:42:03 -0300 | [diff] [blame] | 4 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 5 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | fa0d984 | 2019-08-30 12:52:25 -0300 | [diff] [blame] | 6 | #include <subcmd/pager.h> |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 7 | #include <sys/types.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 8 | #include <errno.h> |
Arnaldo Carvalho de Melo | c23c2a0 | 2017-09-11 10:50:26 -0300 | [diff] [blame] | 9 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 10 | #include <sys/stat.h> |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | #include <stdio.h> |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 13 | #include <stdbool.h> |
Adrian Hunter | 7d4bdab | 2014-07-31 09:00:50 +0300 | [diff] [blame] | 14 | #include <stdarg.h> |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 15 | #include <dirent.h> |
Borislav Petkov | cd0cfad | 2013-12-09 17:14:24 +0100 | [diff] [blame] | 16 | #include <api/fs/fs.h> |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 17 | #include <locale.h> |
William Cohen | fbc2844 | 2017-12-04 09:57:28 -0500 | [diff] [blame] | 18 | #include <regex.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 19 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | 5e51b0b | 2019-08-22 10:48:31 -0300 | [diff] [blame] | 20 | #include "debug.h" |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 21 | #include "pmu.h" |
| 22 | #include "parse-events.h" |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 23 | #include "header.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 24 | #include "string2.h" |
Arnaldo Carvalho de Melo | fa0d984 | 2019-08-30 12:52:25 -0300 | [diff] [blame] | 25 | #include "strbuf.h" |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 26 | #include "fncache.h" |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 27 | |
Arnaldo Carvalho de Melo | ab1bf653 | 2013-01-18 17:05:09 -0300 | [diff] [blame] | 28 | struct perf_pmu_format { |
| 29 | char *name; |
| 30 | int value; |
| 31 | DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); |
| 32 | struct list_head list; |
| 33 | }; |
| 34 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 35 | int perf_pmu_parse(struct list_head *list, char *name); |
| 36 | extern FILE *perf_pmu_in; |
| 37 | |
| 38 | static LIST_HEAD(pmus); |
| 39 | |
| 40 | /* |
| 41 | * Parse & process all the sysfs attributes located under |
| 42 | * the directory specified in 'dir' parameter. |
| 43 | */ |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 44 | int perf_pmu__format_parse(char *dir, struct list_head *head) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 45 | { |
| 46 | struct dirent *evt_ent; |
| 47 | DIR *format_dir; |
| 48 | int ret = 0; |
| 49 | |
| 50 | format_dir = opendir(dir); |
| 51 | if (!format_dir) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | while (!ret && (evt_ent = readdir(format_dir))) { |
| 55 | char path[PATH_MAX]; |
| 56 | char *name = evt_ent->d_name; |
| 57 | FILE *file; |
| 58 | |
| 59 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 60 | continue; |
| 61 | |
| 62 | snprintf(path, PATH_MAX, "%s/%s", dir, name); |
| 63 | |
| 64 | ret = -EINVAL; |
| 65 | file = fopen(path, "r"); |
| 66 | if (!file) |
| 67 | break; |
| 68 | |
| 69 | perf_pmu_in = file; |
| 70 | ret = perf_pmu_parse(head, name); |
| 71 | fclose(file); |
| 72 | } |
| 73 | |
| 74 | closedir(format_dir); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Reading/parsing the default pmu format definition, which should be |
| 80 | * located at: |
| 81 | * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. |
| 82 | */ |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 83 | static int pmu_format(const char *name, struct list_head *format) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 84 | { |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 85 | char path[PATH_MAX]; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 86 | const char *sysfs = sysfs__mountpoint(); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 87 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 88 | if (!sysfs) |
| 89 | return -1; |
| 90 | |
| 91 | snprintf(path, PATH_MAX, |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 92 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 93 | |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 94 | if (!file_available(path)) |
| 95 | return 0; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 96 | |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 97 | if (perf_pmu__format_parse(path, format)) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 98 | return -1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Jin Yao | a55ab7c | 2019-08-28 13:59:29 +0800 | [diff] [blame] | 103 | int perf_pmu__convert_scale(const char *scale, char **end, double *sval) |
Andi Kleen | d02fc6b | 2017-01-03 07:08:23 -0800 | [diff] [blame] | 104 | { |
| 105 | char *lc; |
| 106 | int ret = 0; |
| 107 | |
| 108 | /* |
| 109 | * save current locale |
| 110 | */ |
| 111 | lc = setlocale(LC_NUMERIC, NULL); |
| 112 | |
| 113 | /* |
| 114 | * The lc string may be allocated in static storage, |
| 115 | * so get a dynamic copy to make it survive setlocale |
| 116 | * call below. |
| 117 | */ |
| 118 | lc = strdup(lc); |
| 119 | if (!lc) { |
| 120 | ret = -ENOMEM; |
| 121 | goto out; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * force to C locale to ensure kernel |
| 126 | * scale string is converted correctly. |
| 127 | * kernel uses default C locale. |
| 128 | */ |
| 129 | setlocale(LC_NUMERIC, "C"); |
| 130 | |
| 131 | *sval = strtod(scale, end); |
| 132 | |
| 133 | out: |
| 134 | /* restore locale */ |
| 135 | setlocale(LC_NUMERIC, lc); |
| 136 | free(lc); |
| 137 | return ret; |
| 138 | } |
| 139 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 140 | static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) |
| 141 | { |
| 142 | struct stat st; |
| 143 | ssize_t sret; |
| 144 | char scale[128]; |
| 145 | int fd, ret = -1; |
| 146 | char path[PATH_MAX]; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 147 | |
Ben Hutchings | 11a64a0 | 2018-11-11 18:45:24 +0000 | [diff] [blame] | 148 | scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 149 | |
| 150 | fd = open(path, O_RDONLY); |
| 151 | if (fd == -1) |
| 152 | return -1; |
| 153 | |
| 154 | if (fstat(fd, &st) < 0) |
| 155 | goto error; |
| 156 | |
| 157 | sret = read(fd, scale, sizeof(scale)-1); |
| 158 | if (sret < 0) |
| 159 | goto error; |
| 160 | |
Madhavan Srinivasan | 9ecae06 | 2015-05-31 11:36:23 +0530 | [diff] [blame] | 161 | if (scale[sret - 1] == '\n') |
| 162 | scale[sret - 1] = '\0'; |
| 163 | else |
| 164 | scale[sret] = '\0'; |
| 165 | |
Jin Yao | a55ab7c | 2019-08-28 13:59:29 +0800 | [diff] [blame] | 166 | ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 167 | error: |
| 168 | close(fd); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) |
| 173 | { |
| 174 | char path[PATH_MAX]; |
| 175 | ssize_t sret; |
| 176 | int fd; |
| 177 | |
Ben Hutchings | 11a64a0 | 2018-11-11 18:45:24 +0000 | [diff] [blame] | 178 | scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 179 | |
| 180 | fd = open(path, O_RDONLY); |
| 181 | if (fd == -1) |
| 182 | return -1; |
| 183 | |
Markus Trippelsdorf | d85ce83 | 2015-12-14 16:44:40 +0100 | [diff] [blame] | 184 | sret = read(fd, alias->unit, UNIT_MAX_LEN); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 185 | if (sret < 0) |
| 186 | goto error; |
| 187 | |
| 188 | close(fd); |
| 189 | |
Madhavan Srinivasan | 9ecae06 | 2015-05-31 11:36:23 +0530 | [diff] [blame] | 190 | if (alias->unit[sret - 1] == '\n') |
| 191 | alias->unit[sret - 1] = '\0'; |
| 192 | else |
| 193 | alias->unit[sret] = '\0'; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 194 | |
| 195 | return 0; |
| 196 | error: |
| 197 | close(fd); |
| 198 | alias->unit[0] = '\0'; |
| 199 | return -1; |
| 200 | } |
| 201 | |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 202 | static int |
| 203 | perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) |
| 204 | { |
| 205 | char path[PATH_MAX]; |
| 206 | int fd; |
| 207 | |
Ben Hutchings | 11a64a0 | 2018-11-11 18:45:24 +0000 | [diff] [blame] | 208 | scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 209 | |
| 210 | fd = open(path, O_RDONLY); |
| 211 | if (fd == -1) |
| 212 | return -1; |
| 213 | |
| 214 | close(fd); |
| 215 | |
| 216 | alias->per_pkg = true; |
| 217 | return 0; |
| 218 | } |
| 219 | |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 220 | static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, |
| 221 | char *dir, char *name) |
| 222 | { |
| 223 | char path[PATH_MAX]; |
| 224 | int fd; |
| 225 | |
Ben Hutchings | 11a64a0 | 2018-11-11 18:45:24 +0000 | [diff] [blame] | 226 | scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 227 | |
| 228 | fd = open(path, O_RDONLY); |
| 229 | if (fd == -1) |
| 230 | return -1; |
| 231 | |
| 232 | alias->snapshot = true; |
| 233 | close(fd); |
| 234 | return 0; |
| 235 | } |
| 236 | |
Thomas Richter | 6dde642 | 2018-06-15 12:11:05 +0200 | [diff] [blame] | 237 | static void perf_pmu_assign_str(char *name, const char *field, char **old_str, |
| 238 | char **new_str) |
| 239 | { |
| 240 | if (!*old_str) |
| 241 | goto set_new; |
| 242 | |
| 243 | if (*new_str) { /* Have new string, check with old */ |
| 244 | if (strcasecmp(*old_str, *new_str)) |
| 245 | pr_debug("alias %s differs in field '%s'\n", |
| 246 | name, field); |
| 247 | zfree(old_str); |
| 248 | } else /* Nothing new --> keep old string */ |
| 249 | return; |
| 250 | set_new: |
| 251 | *old_str = *new_str; |
| 252 | *new_str = NULL; |
| 253 | } |
| 254 | |
| 255 | static void perf_pmu_update_alias(struct perf_pmu_alias *old, |
| 256 | struct perf_pmu_alias *newalias) |
| 257 | { |
| 258 | perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); |
| 259 | perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, |
| 260 | &newalias->long_desc); |
| 261 | perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); |
| 262 | perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr, |
| 263 | &newalias->metric_expr); |
| 264 | perf_pmu_assign_str(old->name, "metric_name", &old->metric_name, |
| 265 | &newalias->metric_name); |
| 266 | perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); |
| 267 | old->scale = newalias->scale; |
| 268 | old->per_pkg = newalias->per_pkg; |
| 269 | old->snapshot = newalias->snapshot; |
| 270 | memcpy(old->unit, newalias->unit, sizeof(old->unit)); |
| 271 | } |
| 272 | |
| 273 | /* Delete an alias entry. */ |
| 274 | static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) |
| 275 | { |
| 276 | zfree(&newalias->name); |
| 277 | zfree(&newalias->desc); |
| 278 | zfree(&newalias->long_desc); |
| 279 | zfree(&newalias->topic); |
| 280 | zfree(&newalias->str); |
| 281 | zfree(&newalias->metric_expr); |
| 282 | zfree(&newalias->metric_name); |
| 283 | parse_events_terms__purge(&newalias->terms); |
| 284 | free(newalias); |
| 285 | } |
| 286 | |
| 287 | /* Merge an alias, search in alias list. If this name is already |
| 288 | * present merge both of them to combine all information. |
| 289 | */ |
| 290 | static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, |
| 291 | struct list_head *alist) |
| 292 | { |
| 293 | struct perf_pmu_alias *a; |
| 294 | |
| 295 | list_for_each_entry(a, alist, list) { |
| 296 | if (!strcasecmp(newalias->name, a->name)) { |
| 297 | perf_pmu_update_alias(a, newalias); |
| 298 | perf_pmu_free_alias(newalias); |
| 299 | return true; |
| 300 | } |
| 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
Sukadev Bhattiprolu | 70c646e | 2015-06-10 00:25:08 -0700 | [diff] [blame] | 305 | static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 306 | char *desc, char *val, |
| 307 | char *long_desc, char *topic, |
Andi Kleen | 00636c3 | 2017-03-20 13:17:07 -0700 | [diff] [blame] | 308 | char *unit, char *perpkg, |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 309 | char *metric_expr, |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 310 | char *metric_name, |
| 311 | char *deprecated) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 312 | { |
Thomas Richter | 0c24d6f | 2018-06-15 12:11:04 +0200 | [diff] [blame] | 313 | struct parse_events_term *term; |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 314 | struct perf_pmu_alias *alias; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 315 | int ret; |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 316 | int num; |
Thomas Richter | 0c24d6f | 2018-06-15 12:11:04 +0200 | [diff] [blame] | 317 | char newval[256]; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 318 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 319 | alias = malloc(sizeof(*alias)); |
| 320 | if (!alias) |
| 321 | return -ENOMEM; |
| 322 | |
| 323 | INIT_LIST_HEAD(&alias->terms); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 324 | alias->scale = 1.0; |
| 325 | alias->unit[0] = '\0'; |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 326 | alias->per_pkg = false; |
Stephane Eranian | 8453092 | 2016-01-06 19:50:01 +0100 | [diff] [blame] | 327 | alias->snapshot = false; |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 328 | alias->deprecated = false; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 329 | |
Sukadev Bhattiprolu | 70c646e | 2015-06-10 00:25:08 -0700 | [diff] [blame] | 330 | ret = parse_events_terms(&alias->terms, val); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 331 | if (ret) { |
Sukadev Bhattiprolu | 70c646e | 2015-06-10 00:25:08 -0700 | [diff] [blame] | 332 | pr_err("Cannot parse alias %s: %d\n", val, ret); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 333 | free(alias); |
| 334 | return ret; |
| 335 | } |
| 336 | |
Thomas Richter | 0c24d6f | 2018-06-15 12:11:04 +0200 | [diff] [blame] | 337 | /* Scan event and remove leading zeroes, spaces, newlines, some |
| 338 | * platforms have terms specified as |
| 339 | * event=0x0091 (read from files ../<PMU>/events/<FILE> |
| 340 | * and terms specified as event=0x91 (read from JSON files). |
| 341 | * |
| 342 | * Rebuild string to make alias->str member comparable. |
| 343 | */ |
| 344 | memset(newval, 0, sizeof(newval)); |
| 345 | ret = 0; |
| 346 | list_for_each_entry(term, &alias->terms, list) { |
| 347 | if (ret) |
| 348 | ret += scnprintf(newval + ret, sizeof(newval) - ret, |
| 349 | ","); |
| 350 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) |
| 351 | ret += scnprintf(newval + ret, sizeof(newval) - ret, |
| 352 | "%s=%#x", term->config, term->val.num); |
| 353 | else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) |
| 354 | ret += scnprintf(newval + ret, sizeof(newval) - ret, |
| 355 | "%s=%s", term->config, term->val.str); |
| 356 | } |
| 357 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 358 | alias->name = strdup(name); |
Sukadev Bhattiprolu | 70c646e | 2015-06-10 00:25:08 -0700 | [diff] [blame] | 359 | if (dir) { |
| 360 | /* |
| 361 | * load unit name and scale if available |
| 362 | */ |
| 363 | perf_pmu__parse_unit(alias, dir, name); |
| 364 | perf_pmu__parse_scale(alias, dir, name); |
| 365 | perf_pmu__parse_per_pkg(alias, dir, name); |
| 366 | perf_pmu__parse_snapshot(alias, dir, name); |
| 367 | } |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 368 | |
Andi Kleen | 00636c3 | 2017-03-20 13:17:07 -0700 | [diff] [blame] | 369 | alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 370 | alias->metric_name = metric_name ? strdup(metric_name): NULL; |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 371 | alias->desc = desc ? strdup(desc) : NULL; |
Sukadev Bhattiprolu | c8d6828 | 2016-09-15 15:24:48 -0700 | [diff] [blame] | 372 | alias->long_desc = long_desc ? strdup(long_desc) : |
| 373 | desc ? strdup(desc) : NULL; |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 374 | alias->topic = topic ? strdup(topic) : NULL; |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 375 | if (unit) { |
Jin Yao | a55ab7c | 2019-08-28 13:59:29 +0800 | [diff] [blame] | 376 | if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0) |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 377 | return -1; |
| 378 | snprintf(alias->unit, sizeof(alias->unit), "%s", unit); |
| 379 | } |
| 380 | alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; |
Thomas Richter | 0c24d6f | 2018-06-15 12:11:04 +0200 | [diff] [blame] | 381 | alias->str = strdup(newval); |
Andi Kleen | f236102 | 2017-01-27 18:03:40 -0800 | [diff] [blame] | 382 | |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 383 | if (deprecated) |
| 384 | alias->deprecated = true; |
| 385 | |
Thomas Richter | 6dde642 | 2018-06-15 12:11:05 +0200 | [diff] [blame] | 386 | if (!perf_pmu_merge_alias(alias, list)) |
| 387 | list_add_tail(&alias->list, list); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 388 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
Sukadev Bhattiprolu | 70c646e | 2015-06-10 00:25:08 -0700 | [diff] [blame] | 392 | static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) |
| 393 | { |
| 394 | char buf[256]; |
| 395 | int ret; |
| 396 | |
| 397 | ret = fread(buf, 1, sizeof(buf), file); |
| 398 | if (ret == 0) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | buf[ret] = 0; |
| 402 | |
Thomas Richter | ea23ac7 | 2018-06-15 12:11:03 +0200 | [diff] [blame] | 403 | /* Remove trailing newline from sysfs file */ |
Arnaldo Carvalho de Melo | 13c230a | 2019-06-26 12:13:13 -0300 | [diff] [blame] | 404 | strim(buf); |
Thomas Richter | ea23ac7 | 2018-06-15 12:11:03 +0200 | [diff] [blame] | 405 | |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 406 | return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL, |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 407 | NULL, NULL, NULL, NULL); |
Sukadev Bhattiprolu | 70c646e | 2015-06-10 00:25:08 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 410 | static inline bool pmu_alias_info_file(char *name) |
| 411 | { |
| 412 | size_t len; |
| 413 | |
| 414 | len = strlen(name); |
| 415 | if (len > 5 && !strcmp(name + len - 5, ".unit")) |
| 416 | return true; |
| 417 | if (len > 6 && !strcmp(name + len - 6, ".scale")) |
| 418 | return true; |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 419 | if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) |
| 420 | return true; |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 421 | if (len > 9 && !strcmp(name + len - 9, ".snapshot")) |
| 422 | return true; |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 423 | |
| 424 | return false; |
| 425 | } |
| 426 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 427 | /* |
| 428 | * Process all the sysfs attributes located under the directory |
| 429 | * specified in 'dir' parameter. |
| 430 | */ |
| 431 | static int pmu_aliases_parse(char *dir, struct list_head *head) |
| 432 | { |
| 433 | struct dirent *evt_ent; |
| 434 | DIR *event_dir; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 435 | |
| 436 | event_dir = opendir(dir); |
| 437 | if (!event_dir) |
| 438 | return -EINVAL; |
| 439 | |
Andi Kleen | 940db6d | 2016-02-17 14:44:55 -0800 | [diff] [blame] | 440 | while ((evt_ent = readdir(event_dir))) { |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 441 | char path[PATH_MAX]; |
| 442 | char *name = evt_ent->d_name; |
| 443 | FILE *file; |
| 444 | |
| 445 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 446 | continue; |
| 447 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 448 | /* |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 449 | * skip info files parsed in perf_pmu__new_alias() |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 450 | */ |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 451 | if (pmu_alias_info_file(name)) |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 452 | continue; |
| 453 | |
Jiri Olsa | 77f1815 | 2018-03-19 09:29:01 +0100 | [diff] [blame] | 454 | scnprintf(path, PATH_MAX, "%s/%s", dir, name); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 455 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 456 | file = fopen(path, "r"); |
Andi Kleen | 940db6d | 2016-02-17 14:44:55 -0800 | [diff] [blame] | 457 | if (!file) { |
| 458 | pr_debug("Cannot open %s\n", path); |
| 459 | continue; |
| 460 | } |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 461 | |
Andi Kleen | 940db6d | 2016-02-17 14:44:55 -0800 | [diff] [blame] | 462 | if (perf_pmu__new_alias(head, dir, name, file) < 0) |
| 463 | pr_debug("Cannot set up %s\n", name); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 464 | fclose(file); |
| 465 | } |
| 466 | |
| 467 | closedir(event_dir); |
Andi Kleen | 940db6d | 2016-02-17 14:44:55 -0800 | [diff] [blame] | 468 | return 0; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Reading the pmu event aliases definition, which should be located at: |
| 473 | * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. |
| 474 | */ |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 475 | static int pmu_aliases(const char *name, struct list_head *head) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 476 | { |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 477 | char path[PATH_MAX]; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 478 | const char *sysfs = sysfs__mountpoint(); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 479 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 480 | if (!sysfs) |
| 481 | return -1; |
| 482 | |
| 483 | snprintf(path, PATH_MAX, |
| 484 | "%s/bus/event_source/devices/%s/events", sysfs, name); |
| 485 | |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 486 | if (!file_available(path)) |
| 487 | return 0; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 488 | |
| 489 | if (pmu_aliases_parse(path, head)) |
| 490 | return -1; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 495 | static int pmu_alias_terms(struct perf_pmu_alias *alias, |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 496 | struct list_head *terms) |
| 497 | { |
Jiri Olsa | 7c2f816 | 2014-04-16 20:49:02 +0200 | [diff] [blame] | 498 | struct parse_events_term *term, *cloned; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 499 | LIST_HEAD(list); |
| 500 | int ret; |
| 501 | |
| 502 | list_for_each_entry(term, &alias->terms, list) { |
Jiri Olsa | 7c2f816 | 2014-04-16 20:49:02 +0200 | [diff] [blame] | 503 | ret = parse_events_term__clone(&cloned, term); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 504 | if (ret) { |
Arnaldo Carvalho de Melo | 682dc24 | 2016-02-12 16:48:00 -0300 | [diff] [blame] | 505 | parse_events_terms__purge(&list); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 506 | return ret; |
| 507 | } |
Andi Kleen | c2f1cea | 2017-10-20 13:27:55 -0700 | [diff] [blame] | 508 | /* |
| 509 | * Weak terms don't override command line options, |
| 510 | * which we don't want for implicit terms in aliases. |
| 511 | */ |
| 512 | cloned->weak = true; |
Jiri Olsa | 7c2f816 | 2014-04-16 20:49:02 +0200 | [diff] [blame] | 513 | list_add_tail(&cloned->list, &list); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 514 | } |
| 515 | list_splice(&list, terms); |
| 516 | return 0; |
| 517 | } |
| 518 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 519 | /* |
| 520 | * Reading/parsing the default pmu type value, which should be |
| 521 | * located at: |
| 522 | * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. |
| 523 | */ |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 524 | static int pmu_type(const char *name, __u32 *type) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 525 | { |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 526 | char path[PATH_MAX]; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 527 | FILE *file; |
| 528 | int ret = 0; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 529 | const char *sysfs = sysfs__mountpoint(); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 530 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 531 | if (!sysfs) |
| 532 | return -1; |
| 533 | |
| 534 | snprintf(path, PATH_MAX, |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 535 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 536 | |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 537 | if (access(path, R_OK) < 0) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 538 | return -1; |
| 539 | |
| 540 | file = fopen(path, "r"); |
| 541 | if (!file) |
| 542 | return -EINVAL; |
| 543 | |
| 544 | if (1 != fscanf(file, "%u", type)) |
| 545 | ret = -1; |
| 546 | |
| 547 | fclose(file); |
| 548 | return ret; |
| 549 | } |
| 550 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 551 | /* Add all pmus in sysfs to pmu list: */ |
| 552 | static void pmu_read_sysfs(void) |
| 553 | { |
| 554 | char path[PATH_MAX]; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 555 | DIR *dir; |
| 556 | struct dirent *dent; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 557 | const char *sysfs = sysfs__mountpoint(); |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 558 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 559 | if (!sysfs) |
| 560 | return; |
| 561 | |
| 562 | snprintf(path, PATH_MAX, |
| 563 | "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); |
| 564 | |
| 565 | dir = opendir(path); |
| 566 | if (!dir) |
| 567 | return; |
| 568 | |
| 569 | while ((dent = readdir(dir))) { |
| 570 | if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) |
| 571 | continue; |
| 572 | /* add to static LIST_HEAD(pmus): */ |
| 573 | perf_pmu__find(dent->d_name); |
| 574 | } |
| 575 | |
| 576 | closedir(dir); |
| 577 | } |
| 578 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 579 | static struct perf_cpu_map *__pmu_cpumask(const char *path) |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 580 | { |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 581 | FILE *file; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 582 | struct perf_cpu_map *cpus; |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 583 | |
| 584 | file = fopen(path, "r"); |
| 585 | if (!file) |
| 586 | return NULL; |
| 587 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 588 | cpus = perf_cpu_map__read(file); |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 589 | fclose(file); |
| 590 | return cpus; |
| 591 | } |
| 592 | |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 593 | /* |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 594 | * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) |
| 595 | * may have a "cpus" file. |
| 596 | */ |
| 597 | #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask" |
| 598 | #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus" |
| 599 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 600 | static struct perf_cpu_map *pmu_cpumask(const char *name) |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 601 | { |
| 602 | char path[PATH_MAX]; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 603 | struct perf_cpu_map *cpus; |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 604 | const char *sysfs = sysfs__mountpoint(); |
| 605 | const char *templates[] = { |
| 606 | CPUS_TEMPLATE_UNCORE, |
| 607 | CPUS_TEMPLATE_CPU, |
| 608 | NULL |
| 609 | }; |
| 610 | const char **template; |
| 611 | |
| 612 | if (!sysfs) |
| 613 | return NULL; |
| 614 | |
| 615 | for (template = templates; *template; template++) { |
| 616 | snprintf(path, PATH_MAX, *template, sysfs, name); |
| 617 | cpus = __pmu_cpumask(path); |
| 618 | if (cpus) |
| 619 | return cpus; |
| 620 | } |
| 621 | |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | static bool pmu_is_uncore(const char *name) |
| 626 | { |
| 627 | char path[PATH_MAX]; |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 628 | const char *sysfs; |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 629 | |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 630 | sysfs = sysfs__mountpoint(); |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 631 | snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name); |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 632 | return file_available(path); |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
Ganapatrao Kulkarni | 14b22ae | 2017-08-24 16:30:58 +0530 | [diff] [blame] | 636 | * PMU CORE devices have different name other than cpu in sysfs on some |
Kan Liang | 292c34c | 2018-04-24 11:20:10 -0700 | [diff] [blame] | 637 | * platforms. |
| 638 | * Looking for possible sysfs files to identify the arm core device. |
Ganapatrao Kulkarni | 14b22ae | 2017-08-24 16:30:58 +0530 | [diff] [blame] | 639 | */ |
Kan Liang | 292c34c | 2018-04-24 11:20:10 -0700 | [diff] [blame] | 640 | static int is_arm_pmu_core(const char *name) |
Ganapatrao Kulkarni | 14b22ae | 2017-08-24 16:30:58 +0530 | [diff] [blame] | 641 | { |
Ganapatrao Kulkarni | 14b22ae | 2017-08-24 16:30:58 +0530 | [diff] [blame] | 642 | char path[PATH_MAX]; |
| 643 | const char *sysfs = sysfs__mountpoint(); |
| 644 | |
| 645 | if (!sysfs) |
| 646 | return 0; |
| 647 | |
Ganapatrao Kulkarni | 14b22ae | 2017-08-24 16:30:58 +0530 | [diff] [blame] | 648 | /* Look for cpu sysfs (specific to arm) */ |
| 649 | scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus", |
| 650 | sysfs, name); |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 651 | return file_available(path); |
Ganapatrao Kulkarni | 14b22ae | 2017-08-24 16:30:58 +0530 | [diff] [blame] | 652 | } |
| 653 | |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 654 | static char *perf_pmu__getcpuid(struct perf_pmu *pmu) |
Andi Kleen | d77ade9 | 2017-08-31 12:40:30 -0700 | [diff] [blame] | 655 | { |
| 656 | char *cpuid; |
| 657 | static bool printed; |
| 658 | |
| 659 | cpuid = getenv("PERF_CPUID"); |
| 660 | if (cpuid) |
| 661 | cpuid = strdup(cpuid); |
| 662 | if (!cpuid) |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 663 | cpuid = get_cpuid_str(pmu); |
Andi Kleen | d77ade9 | 2017-08-31 12:40:30 -0700 | [diff] [blame] | 664 | if (!cpuid) |
| 665 | return NULL; |
| 666 | |
| 667 | if (!printed) { |
| 668 | pr_debug("Using CPUID %s\n", cpuid); |
| 669 | printed = true; |
| 670 | } |
| 671 | return cpuid; |
| 672 | } |
| 673 | |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 674 | struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu) |
Andi Kleen | d77ade9 | 2017-08-31 12:40:30 -0700 | [diff] [blame] | 675 | { |
| 676 | struct pmu_events_map *map; |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 677 | char *cpuid = perf_pmu__getcpuid(pmu); |
Andi Kleen | d77ade9 | 2017-08-31 12:40:30 -0700 | [diff] [blame] | 678 | int i; |
| 679 | |
Ganapatrao Kulkarni | de3d0f1 | 2017-10-17 00:02:22 +0530 | [diff] [blame] | 680 | /* on some platforms which uses cpus map, cpuid can be NULL for |
| 681 | * PMUs other than CORE PMUs. |
| 682 | */ |
| 683 | if (!cpuid) |
| 684 | return NULL; |
| 685 | |
Andi Kleen | d77ade9 | 2017-08-31 12:40:30 -0700 | [diff] [blame] | 686 | i = 0; |
| 687 | for (;;) { |
| 688 | map = &pmu_events_map[i++]; |
| 689 | if (!map->table) { |
| 690 | map = NULL; |
| 691 | break; |
| 692 | } |
| 693 | |
Thomas Richter | 4cb7d3e | 2018-02-13 16:14:18 +0100 | [diff] [blame] | 694 | if (!strcmp_cpuid_str(map->cpuid, cpuid)) |
Andi Kleen | d77ade9 | 2017-08-31 12:40:30 -0700 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | free(cpuid); |
| 698 | return map; |
| 699 | } |
| 700 | |
John Garry | 730670b1 | 2019-06-28 22:35:49 +0800 | [diff] [blame] | 701 | static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) |
| 702 | { |
| 703 | char *tmp = NULL, *tok, *str; |
| 704 | bool res; |
| 705 | |
| 706 | str = strdup(pmu_name); |
| 707 | if (!str) |
| 708 | return false; |
| 709 | |
| 710 | /* |
| 711 | * uncore alias may be from different PMU with common prefix |
| 712 | */ |
| 713 | tok = strtok_r(str, ",", &tmp); |
| 714 | if (strncmp(pmu_name, tok, strlen(tok))) { |
| 715 | res = false; |
| 716 | goto out; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Match more complex aliases where the alias name is a comma-delimited |
| 721 | * list of tokens, orderly contained in the matching PMU name. |
| 722 | * |
| 723 | * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we |
| 724 | * match "socket" in "socketX_pmunameY" and then "pmuname" in |
| 725 | * "pmunameY". |
| 726 | */ |
| 727 | for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) { |
| 728 | name = strstr(name, tok); |
| 729 | if (!name) { |
| 730 | res = false; |
| 731 | goto out; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | res = true; |
| 736 | out: |
| 737 | free(str); |
| 738 | return res; |
| 739 | } |
| 740 | |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 741 | /* |
| 742 | * From the pmu_events_map, find the table of PMU events that corresponds |
| 743 | * to the current running CPU. Then, add all PMU events from that table |
| 744 | * as aliases. |
| 745 | */ |
John Garry | e45ad70 | 2020-03-17 19:02:15 +0800 | [diff] [blame^] | 746 | void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu, |
| 747 | struct pmu_events_map *map) |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 748 | { |
| 749 | int i; |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 750 | const char *name = pmu->name; |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 751 | /* |
| 752 | * Found a matching PMU events table. Create aliases |
| 753 | */ |
| 754 | i = 0; |
| 755 | while (1) { |
John Garry | 599ee18 | 2019-06-14 22:07:59 +0800 | [diff] [blame] | 756 | const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu"; |
| 757 | struct pmu_event *pe = &map->table[i++]; |
| 758 | const char *pname = pe->pmu ? pe->pmu : cpu_name; |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 759 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 760 | if (!pe->name) { |
| 761 | if (pe->metric_group || pe->metric_name) |
| 762 | continue; |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 763 | break; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 764 | } |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 765 | |
John Garry | 599ee18 | 2019-06-14 22:07:59 +0800 | [diff] [blame] | 766 | if (pmu_is_uncore(name) && |
John Garry | 730670b1 | 2019-06-28 22:35:49 +0800 | [diff] [blame] | 767 | pmu_uncore_alias_match(pname, name)) |
John Garry | 599ee18 | 2019-06-14 22:07:59 +0800 | [diff] [blame] | 768 | goto new_alias; |
Kan Liang | e94d6b7 | 2019-03-15 11:00:14 -0700 | [diff] [blame] | 769 | |
John Garry | 599ee18 | 2019-06-14 22:07:59 +0800 | [diff] [blame] | 770 | if (strcmp(pname, name)) |
| 771 | continue; |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 772 | |
Kan Liang | e94d6b7 | 2019-03-15 11:00:14 -0700 | [diff] [blame] | 773 | new_alias: |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 774 | /* need type casts to override 'const' */ |
| 775 | __perf_pmu__new_alias(head, NULL, (char *)pe->name, |
Sukadev Bhattiprolu | c8d6828 | 2016-09-15 15:24:48 -0700 | [diff] [blame] | 776 | (char *)pe->desc, (char *)pe->event, |
Andi Kleen | fedb2b5 | 2017-01-27 18:03:37 -0800 | [diff] [blame] | 777 | (char *)pe->long_desc, (char *)pe->topic, |
Andi Kleen | 00636c3 | 2017-03-20 13:17:07 -0700 | [diff] [blame] | 778 | (char *)pe->unit, (char *)pe->perpkg, |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 779 | (char *)pe->metric_expr, |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 780 | (char *)pe->metric_name, |
| 781 | (char *)pe->deprecated); |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 782 | } |
Sukadev Bhattiprolu | 933f82f | 2016-09-15 15:24:40 -0700 | [diff] [blame] | 783 | } |
| 784 | |
John Garry | e45ad70 | 2020-03-17 19:02:15 +0800 | [diff] [blame^] | 785 | static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) |
| 786 | { |
| 787 | struct pmu_events_map *map; |
| 788 | |
| 789 | map = perf_pmu__find_map(pmu); |
| 790 | if (!map) |
| 791 | return; |
| 792 | |
| 793 | pmu_add_cpu_aliases_map(head, pmu, map); |
| 794 | } |
| 795 | |
Sukadev Bhattiprolu | c5de47f | 2015-06-10 00:25:07 -0700 | [diff] [blame] | 796 | struct perf_event_attr * __weak |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 797 | perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) |
| 798 | { |
| 799 | return NULL; |
| 800 | } |
| 801 | |
Jiri Olsa | 90a86bd | 2019-03-05 16:25:32 +0100 | [diff] [blame] | 802 | static int pmu_max_precise(const char *name) |
| 803 | { |
| 804 | char path[PATH_MAX]; |
| 805 | int max_precise = -1; |
| 806 | |
| 807 | scnprintf(path, PATH_MAX, |
| 808 | "bus/event_source/devices/%s/caps/max_precise", |
| 809 | name); |
| 810 | |
| 811 | sysfs__read_int(path, &max_precise); |
| 812 | return max_precise; |
| 813 | } |
| 814 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 815 | static struct perf_pmu *pmu_lookup(const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 816 | { |
| 817 | struct perf_pmu *pmu; |
| 818 | LIST_HEAD(format); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 819 | LIST_HEAD(aliases); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 820 | __u32 type; |
| 821 | |
| 822 | /* |
| 823 | * The pmu data we store & need consists of the pmu |
| 824 | * type value and format definitions. Load both right |
| 825 | * now. |
| 826 | */ |
| 827 | if (pmu_format(name, &format)) |
| 828 | return NULL; |
| 829 | |
Andi Kleen | 15b22ed | 2017-01-27 18:03:38 -0800 | [diff] [blame] | 830 | /* |
| 831 | * Check the type first to avoid unnecessary work. |
| 832 | */ |
| 833 | if (pmu_type(name, &type)) |
| 834 | return NULL; |
| 835 | |
Jiri Olsa | 3fded96 | 2012-10-10 14:53:16 +0200 | [diff] [blame] | 836 | if (pmu_aliases(name, &aliases)) |
| 837 | return NULL; |
| 838 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 839 | pmu = zalloc(sizeof(*pmu)); |
| 840 | if (!pmu) |
| 841 | return NULL; |
| 842 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 843 | pmu->cpus = pmu_cpumask(name); |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 844 | pmu->name = strdup(name); |
| 845 | pmu->type = type; |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 846 | pmu->is_uncore = pmu_is_uncore(name); |
Jiri Olsa | 90a86bd | 2019-03-05 16:25:32 +0100 | [diff] [blame] | 847 | pmu->max_precise = pmu_max_precise(name); |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 848 | pmu_add_cpu_aliases(&aliases, pmu); |
Mark Rutland | 66ec1191 | 2017-10-06 19:38:22 +0100 | [diff] [blame] | 849 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 850 | INIT_LIST_HEAD(&pmu->format); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 851 | INIT_LIST_HEAD(&pmu->aliases); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 852 | list_splice(&format, &pmu->format); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 853 | list_splice(&aliases, &pmu->aliases); |
Robert Richter | 9bc8f9f | 2012-06-14 22:38:37 +0200 | [diff] [blame] | 854 | list_add_tail(&pmu->list, &pmus); |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 855 | |
| 856 | pmu->default_config = perf_pmu__get_default_config(pmu); |
| 857 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 858 | return pmu; |
| 859 | } |
| 860 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 861 | static struct perf_pmu *pmu_find(const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 862 | { |
| 863 | struct perf_pmu *pmu; |
| 864 | |
| 865 | list_for_each_entry(pmu, &pmus, list) |
| 866 | if (!strcmp(pmu->name, name)) |
| 867 | return pmu; |
| 868 | |
| 869 | return NULL; |
| 870 | } |
| 871 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 872 | struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) |
| 873 | { |
| 874 | /* |
| 875 | * pmu iterator: If pmu is NULL, we start at the begin, |
| 876 | * otherwise return the next pmu. Returns NULL on end. |
| 877 | */ |
| 878 | if (!pmu) { |
| 879 | pmu_read_sysfs(); |
| 880 | pmu = list_prepare_entry(pmu, &pmus, list); |
| 881 | } |
| 882 | list_for_each_entry_continue(pmu, &pmus, list) |
| 883 | return pmu; |
| 884 | return NULL; |
| 885 | } |
| 886 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 887 | struct perf_pmu *perf_pmu__find(const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 888 | { |
| 889 | struct perf_pmu *pmu; |
| 890 | |
| 891 | /* |
| 892 | * Once PMU is loaded it stays in the list, |
| 893 | * so we keep us from multiple reading/parsing |
| 894 | * the pmu format definitions. |
| 895 | */ |
| 896 | pmu = pmu_find(name); |
| 897 | if (pmu) |
| 898 | return pmu; |
| 899 | |
| 900 | return pmu_lookup(name); |
| 901 | } |
| 902 | |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 903 | static struct perf_pmu_format * |
Adrian Hunter | 09ff607 | 2015-07-17 19:33:49 +0300 | [diff] [blame] | 904 | pmu_find_format(struct list_head *formats, const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 905 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 906 | struct perf_pmu_format *format; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 907 | |
| 908 | list_for_each_entry(format, formats, list) |
| 909 | if (!strcmp(format->name, name)) |
| 910 | return format; |
| 911 | |
| 912 | return NULL; |
| 913 | } |
| 914 | |
Adrian Hunter | 09ff607 | 2015-07-17 19:33:49 +0300 | [diff] [blame] | 915 | __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) |
| 916 | { |
| 917 | struct perf_pmu_format *format = pmu_find_format(formats, name); |
| 918 | __u64 bits = 0; |
| 919 | int fbit; |
| 920 | |
| 921 | if (!format) |
| 922 | return 0; |
| 923 | |
| 924 | for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) |
| 925 | bits |= 1ULL << fbit; |
| 926 | |
| 927 | return bits; |
| 928 | } |
| 929 | |
Adrian Hunter | a1ac7de | 2019-11-15 14:42:22 +0200 | [diff] [blame] | 930 | int perf_pmu__format_type(struct list_head *formats, const char *name) |
| 931 | { |
| 932 | struct perf_pmu_format *format = pmu_find_format(formats, name); |
| 933 | |
| 934 | if (!format) |
| 935 | return -1; |
| 936 | |
| 937 | return format->value; |
| 938 | } |
| 939 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 940 | /* |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 941 | * Sets value based on the format definition (format parameter) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 942 | * and unformated value (value parameter). |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 943 | */ |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 944 | static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, |
| 945 | bool zero) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 946 | { |
| 947 | unsigned long fbit, vbit; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 948 | |
| 949 | for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { |
| 950 | |
| 951 | if (!test_bit(fbit, format)) |
| 952 | continue; |
| 953 | |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 954 | if (value & (1llu << vbit++)) |
| 955 | *v |= (1llu << fbit); |
| 956 | else if (zero) |
| 957 | *v &= ~(1llu << fbit); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 958 | } |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 959 | } |
| 960 | |
Adrian Hunter | 0efe6b6 | 2015-07-17 19:33:50 +0300 | [diff] [blame] | 961 | static __u64 pmu_format_max_value(const unsigned long *format) |
| 962 | { |
Jiri Olsa | 1b9caa1 | 2018-10-03 09:20:46 +0200 | [diff] [blame] | 963 | int w; |
Adrian Hunter | 0efe6b6 | 2015-07-17 19:33:50 +0300 | [diff] [blame] | 964 | |
Jiri Olsa | 1b9caa1 | 2018-10-03 09:20:46 +0200 | [diff] [blame] | 965 | w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); |
| 966 | if (!w) |
| 967 | return 0; |
| 968 | if (w < 64) |
| 969 | return (1ULL << w) - 1; |
| 970 | return -1; |
Adrian Hunter | 0efe6b6 | 2015-07-17 19:33:50 +0300 | [diff] [blame] | 971 | } |
| 972 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 973 | /* |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 974 | * Term is a string term, and might be a param-term. Try to look up it's value |
| 975 | * in the remaining terms. |
| 976 | * - We have a term like "base-or-format-term=param-term", |
| 977 | * - We need to find the value supplied for "param-term" (with param-term named |
| 978 | * in a config string) later on in the term list. |
| 979 | */ |
| 980 | static int pmu_resolve_param_term(struct parse_events_term *term, |
| 981 | struct list_head *head_terms, |
| 982 | __u64 *value) |
| 983 | { |
| 984 | struct parse_events_term *t; |
| 985 | |
| 986 | list_for_each_entry(t, head_terms, list) { |
| 987 | if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { |
| 988 | if (!strcmp(t->config, term->config)) { |
| 989 | t->used = true; |
| 990 | *value = t->val.num; |
| 991 | return 0; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 996 | if (verbose > 0) |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 997 | printf("Required parameter '%s' not specified\n", term->config); |
| 998 | |
| 999 | return -1; |
| 1000 | } |
| 1001 | |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1002 | static char *pmu_formats_string(struct list_head *formats) |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1003 | { |
| 1004 | struct perf_pmu_format *format; |
Masami Hiramatsu | 11db4e2 | 2016-05-10 14:47:44 +0900 | [diff] [blame] | 1005 | char *str = NULL; |
| 1006 | struct strbuf buf = STRBUF_INIT; |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1007 | unsigned i = 0; |
| 1008 | |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1009 | if (!formats) |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1010 | return NULL; |
| 1011 | |
| 1012 | /* sysfs exported terms */ |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1013 | list_for_each_entry(format, formats, list) |
Masami Hiramatsu | 11db4e2 | 2016-05-10 14:47:44 +0900 | [diff] [blame] | 1014 | if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) |
| 1015 | goto error; |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1016 | |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1017 | str = strbuf_detach(&buf, NULL); |
Masami Hiramatsu | 11db4e2 | 2016-05-10 14:47:44 +0900 | [diff] [blame] | 1018 | error: |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1019 | strbuf_release(&buf); |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1020 | |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1021 | return str; |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1024 | /* |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1025 | * Setup one of config[12] attr members based on the |
Cody P Schafer | 88aca8d | 2014-01-08 08:43:51 -0800 | [diff] [blame] | 1026 | * user input data - term parameter. |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1027 | */ |
| 1028 | static int pmu_config_term(struct list_head *formats, |
| 1029 | struct perf_event_attr *attr, |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 1030 | struct parse_events_term *term, |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1031 | struct list_head *head_terms, |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1032 | bool zero, struct parse_events_error *err) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1033 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 1034 | struct perf_pmu_format *format; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1035 | __u64 *vp; |
Adrian Hunter | 0efe6b6 | 2015-07-17 19:33:50 +0300 | [diff] [blame] | 1036 | __u64 val, max_val; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1037 | |
| 1038 | /* |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1039 | * If this is a parameter we've already used for parameterized-eval, |
| 1040 | * skip it in normal eval. |
| 1041 | */ |
| 1042 | if (term->used) |
| 1043 | return 0; |
| 1044 | |
| 1045 | /* |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1046 | * Hardcoded terms should be already in, so nothing |
| 1047 | * to be done for them. |
| 1048 | */ |
| 1049 | if (parse_events__is_hardcoded_term(term)) |
| 1050 | return 0; |
| 1051 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1052 | format = pmu_find_format(formats, term->config); |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1053 | if (!format) { |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 1054 | if (verbose > 0) |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1055 | printf("Invalid event/parameter '%s'\n", term->config); |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1056 | if (err) { |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1057 | char *pmu_term = pmu_formats_string(formats); |
| 1058 | |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1059 | parse_events__handle_error(err, term->err_term, |
| 1060 | strdup("unknown term"), |
| 1061 | parse_events_formats_error_string(pmu_term)); |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 1062 | free(pmu_term); |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1063 | } |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1064 | return -EINVAL; |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1065 | } |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1066 | |
| 1067 | switch (format->value) { |
| 1068 | case PERF_PMU_FORMAT_VALUE_CONFIG: |
| 1069 | vp = &attr->config; |
| 1070 | break; |
| 1071 | case PERF_PMU_FORMAT_VALUE_CONFIG1: |
| 1072 | vp = &attr->config1; |
| 1073 | break; |
| 1074 | case PERF_PMU_FORMAT_VALUE_CONFIG2: |
| 1075 | vp = &attr->config2; |
| 1076 | break; |
| 1077 | default: |
| 1078 | return -EINVAL; |
| 1079 | } |
| 1080 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1081 | /* |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1082 | * Either directly use a numeric term, or try to translate string terms |
| 1083 | * using event parameters. |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1084 | */ |
Jiri Olsa | 99e7138 | 2017-02-17 15:00:56 +0100 | [diff] [blame] | 1085 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { |
| 1086 | if (term->no_value && |
| 1087 | bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { |
| 1088 | if (err) { |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1089 | parse_events__handle_error(err, term->err_val, |
| 1090 | strdup("no value assigned for term"), |
| 1091 | NULL); |
Jiri Olsa | 99e7138 | 2017-02-17 15:00:56 +0100 | [diff] [blame] | 1092 | } |
| 1093 | return -EINVAL; |
| 1094 | } |
| 1095 | |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1096 | val = term->val.num; |
Jiri Olsa | 99e7138 | 2017-02-17 15:00:56 +0100 | [diff] [blame] | 1097 | } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1098 | if (strcmp(term->val.str, "?")) { |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 1099 | if (verbose > 0) { |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1100 | pr_info("Invalid sysfs entry %s=%s\n", |
| 1101 | term->config, term->val.str); |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1102 | } |
| 1103 | if (err) { |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1104 | parse_events__handle_error(err, term->err_val, |
| 1105 | strdup("expected numeric value"), |
| 1106 | NULL); |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1107 | } |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1108 | return -EINVAL; |
| 1109 | } |
| 1110 | |
| 1111 | if (pmu_resolve_param_term(term, head_terms, &val)) |
| 1112 | return -EINVAL; |
| 1113 | } else |
| 1114 | return -EINVAL; |
| 1115 | |
Adrian Hunter | 0efe6b6 | 2015-07-17 19:33:50 +0300 | [diff] [blame] | 1116 | max_val = pmu_format_max_value(format->bits); |
| 1117 | if (val > max_val) { |
| 1118 | if (err) { |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1119 | char *err_str; |
| 1120 | |
| 1121 | parse_events__handle_error(err, term->err_val, |
| 1122 | asprintf(&err_str, |
| 1123 | "value too big for format, maximum is %llu", |
| 1124 | (unsigned long long)max_val) < 0 |
| 1125 | ? strdup("value too big for format") |
| 1126 | : err_str, |
| 1127 | NULL); |
Adrian Hunter | 0efe6b6 | 2015-07-17 19:33:50 +0300 | [diff] [blame] | 1128 | return -EINVAL; |
| 1129 | } |
| 1130 | /* |
| 1131 | * Assume we don't care if !err, in which case the value will be |
| 1132 | * silently truncated. |
| 1133 | */ |
| 1134 | } |
| 1135 | |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1136 | pmu_format_value(format->bits, val, vp, zero); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
| 1139 | |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 1140 | int perf_pmu__config_terms(struct list_head *formats, |
| 1141 | struct perf_event_attr *attr, |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 1142 | struct list_head *head_terms, |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1143 | bool zero, struct parse_events_error *err) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1144 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1145 | struct parse_events_term *term; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1146 | |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1147 | list_for_each_entry(term, head_terms, list) { |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1148 | if (pmu_config_term(formats, attr, term, head_terms, |
| 1149 | zero, err)) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1150 | return -EINVAL; |
Cody P Schafer | 688d4df | 2015-01-07 17:13:50 -0800 | [diff] [blame] | 1151 | } |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Configures event's 'attr' parameter based on the: |
| 1158 | * 1) users input - specified in terms parameter |
| 1159 | * 2) pmu format definitions - specified by pmu parameter |
| 1160 | */ |
| 1161 | int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1162 | struct list_head *head_terms, |
| 1163 | struct parse_events_error *err) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1164 | { |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 1165 | bool zero = !!pmu->default_config; |
| 1166 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1167 | attr->type = pmu->type; |
Jiri Olsa | e64b020 | 2015-04-22 21:10:21 +0200 | [diff] [blame] | 1168 | return perf_pmu__config_terms(&pmu->format, attr, head_terms, |
| 1169 | zero, err); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 1172 | static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, |
| 1173 | struct parse_events_term *term) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1174 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 1175 | struct perf_pmu_alias *alias; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1176 | char *name; |
| 1177 | |
| 1178 | if (parse_events__is_hardcoded_term(term)) |
| 1179 | return NULL; |
| 1180 | |
| 1181 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { |
| 1182 | if (term->val.num != 1) |
| 1183 | return NULL; |
| 1184 | if (pmu_find_format(&pmu->format, term->config)) |
| 1185 | return NULL; |
| 1186 | name = term->config; |
| 1187 | } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { |
| 1188 | if (strcasecmp(term->config, "event")) |
| 1189 | return NULL; |
| 1190 | name = term->val.str; |
| 1191 | } else { |
| 1192 | return NULL; |
| 1193 | } |
| 1194 | |
| 1195 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 1196 | if (!strcasecmp(alias->name, name)) |
| 1197 | return alias; |
| 1198 | } |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1202 | |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1203 | static int check_info_data(struct perf_pmu_alias *alias, |
| 1204 | struct perf_pmu_info *info) |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1205 | { |
| 1206 | /* |
| 1207 | * Only one term in event definition can |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1208 | * define unit, scale and snapshot, fail |
| 1209 | * if there's more than one. |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1210 | */ |
Arnaldo Carvalho de Melo | b30a7d1 | 2017-02-15 10:06:20 -0300 | [diff] [blame] | 1211 | if ((info->unit && alias->unit[0]) || |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1212 | (info->scale && alias->scale) || |
| 1213 | (info->snapshot && alias->snapshot)) |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1214 | return -EINVAL; |
| 1215 | |
Arnaldo Carvalho de Melo | b30a7d1 | 2017-02-15 10:06:20 -0300 | [diff] [blame] | 1216 | if (alias->unit[0]) |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1217 | info->unit = alias->unit; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (alias->scale) |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1220 | info->scale = alias->scale; |
| 1221 | |
| 1222 | if (alias->snapshot) |
| 1223 | info->snapshot = alias->snapshot; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1228 | /* |
| 1229 | * Find alias in the terms list and replace it with the terms |
| 1230 | * defined for the alias |
| 1231 | */ |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1232 | int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1233 | struct perf_pmu_info *info) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1234 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1235 | struct parse_events_term *term, *h; |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 1236 | struct perf_pmu_alias *alias; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1237 | int ret; |
| 1238 | |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 1239 | info->per_pkg = false; |
| 1240 | |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 1241 | /* |
| 1242 | * Mark unit and scale as not set |
| 1243 | * (different from default values, see below) |
| 1244 | */ |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1245 | info->unit = NULL; |
| 1246 | info->scale = 0.0; |
| 1247 | info->snapshot = false; |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame] | 1248 | info->metric_expr = NULL; |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 1249 | info->metric_name = NULL; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1250 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1251 | list_for_each_entry_safe(term, h, head_terms, list) { |
| 1252 | alias = pmu_find_alias(pmu, term); |
| 1253 | if (!alias) |
| 1254 | continue; |
| 1255 | ret = pmu_alias_terms(alias, &term->list); |
| 1256 | if (ret) |
| 1257 | return ret; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1258 | |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 1259 | ret = check_info_data(alias, info); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1260 | if (ret) |
| 1261 | return ret; |
| 1262 | |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 1263 | if (alias->per_pkg) |
| 1264 | info->per_pkg = true; |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame] | 1265 | info->metric_expr = alias->metric_expr; |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 1266 | info->metric_name = alias->metric_name; |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 1267 | |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 1268 | list_del_init(&term->list); |
Ian Rogers | 1dc9255 | 2019-10-30 15:34:47 -0700 | [diff] [blame] | 1269 | parse_events_term__delete(term); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1270 | } |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 1271 | |
| 1272 | /* |
| 1273 | * if no unit or scale foundin aliases, then |
| 1274 | * set defaults as for evsel |
| 1275 | * unit cannot left to NULL |
| 1276 | */ |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1277 | if (info->unit == NULL) |
| 1278 | info->unit = ""; |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 1279 | |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1280 | if (info->scale == 0.0) |
| 1281 | info->scale = 1.0; |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 1282 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1283 | return 0; |
| 1284 | } |
| 1285 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1286 | int perf_pmu__new_format(struct list_head *list, char *name, |
| 1287 | int config, unsigned long *bits) |
| 1288 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 1289 | struct perf_pmu_format *format; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1290 | |
| 1291 | format = zalloc(sizeof(*format)); |
| 1292 | if (!format) |
| 1293 | return -ENOMEM; |
| 1294 | |
| 1295 | format->name = strdup(name); |
| 1296 | format->value = config; |
| 1297 | memcpy(format->bits, bits, sizeof(format->bits)); |
| 1298 | |
| 1299 | list_add_tail(&format->list, list); |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | void perf_pmu__set_format(unsigned long *bits, long from, long to) |
| 1304 | { |
| 1305 | long b; |
| 1306 | |
| 1307 | if (!to) |
| 1308 | to = from; |
| 1309 | |
Sukadev Bhattiprolu | 1526813 | 2013-01-17 09:11:30 -0800 | [diff] [blame] | 1310 | memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1311 | for (b = from; b <= to; b++) |
| 1312 | set_bit(b, bits); |
| 1313 | } |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1314 | |
Cody P Schafer | aaea361 | 2015-01-07 17:13:51 -0800 | [diff] [blame] | 1315 | static int sub_non_neg(int a, int b) |
| 1316 | { |
| 1317 | if (b > a) |
| 1318 | return 0; |
| 1319 | return a - b; |
| 1320 | } |
| 1321 | |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1322 | static char *format_alias(char *buf, int len, struct perf_pmu *pmu, |
| 1323 | struct perf_pmu_alias *alias) |
| 1324 | { |
Cody P Schafer | aaea361 | 2015-01-07 17:13:51 -0800 | [diff] [blame] | 1325 | struct parse_events_term *term; |
| 1326 | int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); |
| 1327 | |
| 1328 | list_for_each_entry(term, &alias->terms, list) { |
| 1329 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) |
| 1330 | used += snprintf(buf + used, sub_non_neg(len, used), |
| 1331 | ",%s=%s", term->config, |
| 1332 | term->val.str); |
| 1333 | } |
| 1334 | |
| 1335 | if (sub_non_neg(len, used) > 0) { |
| 1336 | buf[used] = '/'; |
| 1337 | used++; |
| 1338 | } |
| 1339 | if (sub_non_neg(len, used) > 0) { |
| 1340 | buf[used] = '\0'; |
| 1341 | used++; |
| 1342 | } else |
| 1343 | buf[len - 1] = '\0'; |
| 1344 | |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1345 | return buf; |
| 1346 | } |
| 1347 | |
| 1348 | static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, |
| 1349 | struct perf_pmu_alias *alias) |
| 1350 | { |
| 1351 | snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); |
| 1352 | return buf; |
| 1353 | } |
| 1354 | |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1355 | struct sevent { |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1356 | char *name; |
| 1357 | char *desc; |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1358 | char *topic; |
Andi Kleen | f236102 | 2017-01-27 18:03:40 -0800 | [diff] [blame] | 1359 | char *str; |
| 1360 | char *pmu; |
Andi Kleen | 7f372a6 | 2017-03-20 13:17:09 -0700 | [diff] [blame] | 1361 | char *metric_expr; |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 1362 | char *metric_name; |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1363 | }; |
| 1364 | |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1365 | static int cmp_sevent(const void *a, const void *b) |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1366 | { |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1367 | const struct sevent *as = a; |
| 1368 | const struct sevent *bs = b; |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1369 | |
| 1370 | /* Put extra events last */ |
| 1371 | if (!!as->desc != !!bs->desc) |
| 1372 | return !!as->desc - !!bs->desc; |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1373 | if (as->topic && bs->topic) { |
| 1374 | int n = strcmp(as->topic, bs->topic); |
| 1375 | |
| 1376 | if (n) |
| 1377 | return n; |
| 1378 | } |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1379 | return strcmp(as->name, bs->name); |
| 1380 | } |
| 1381 | |
| 1382 | static void wordwrap(char *s, int start, int max, int corr) |
| 1383 | { |
| 1384 | int column = start; |
| 1385 | int n; |
| 1386 | |
| 1387 | while (*s) { |
| 1388 | int wlen = strcspn(s, " \t"); |
| 1389 | |
| 1390 | if (column + wlen >= max && column > start) { |
| 1391 | printf("\n%*s", start, ""); |
| 1392 | column = start + corr; |
| 1393 | } |
| 1394 | n = printf("%s%.*s", column > start ? " " : "", wlen, s); |
| 1395 | if (n <= 0) |
| 1396 | break; |
| 1397 | s += wlen; |
| 1398 | column += n; |
Arnaldo Carvalho de Melo | 3285848 | 2019-06-26 11:42:03 -0300 | [diff] [blame] | 1399 | s = skip_spaces(s); |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1400 | } |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Sukadev Bhattiprolu | c8d6828 | 2016-09-15 15:24:48 -0700 | [diff] [blame] | 1403 | void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 1404 | bool long_desc, bool details_flag, bool deprecated) |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1405 | { |
| 1406 | struct perf_pmu *pmu; |
| 1407 | struct perf_pmu_alias *alias; |
| 1408 | char buf[1024]; |
| 1409 | int printed = 0; |
| 1410 | int len, j; |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1411 | struct sevent *aliases; |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1412 | int numdesc = 0; |
Andi Kleen | 61eb2eb | 2016-09-15 15:24:44 -0700 | [diff] [blame] | 1413 | int columns = pager_get_columns(); |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1414 | char *topic = NULL; |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1415 | |
| 1416 | pmu = NULL; |
| 1417 | len = 0; |
Adrian Hunter | 42634bc | 2014-10-23 13:45:10 +0300 | [diff] [blame] | 1418 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1419 | list_for_each_entry(alias, &pmu->aliases, list) |
| 1420 | len++; |
Adrian Hunter | 42634bc | 2014-10-23 13:45:10 +0300 | [diff] [blame] | 1421 | if (pmu->selectable) |
| 1422 | len++; |
| 1423 | } |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1424 | aliases = zalloc(sizeof(struct sevent) * len); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1425 | if (!aliases) |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1426 | goto out_enomem; |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1427 | pmu = NULL; |
| 1428 | j = 0; |
Adrian Hunter | 42634bc | 2014-10-23 13:45:10 +0300 | [diff] [blame] | 1429 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1430 | list_for_each_entry(alias, &pmu->aliases, list) { |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1431 | char *name = alias->desc ? alias->name : |
| 1432 | format_alias(buf, sizeof(buf), pmu, alias); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1433 | bool is_cpu = !strcmp(pmu->name, "cpu"); |
| 1434 | |
Jin Yao | a7f6c8c | 2019-10-15 10:53:57 +0800 | [diff] [blame] | 1435 | if (alias->deprecated && !deprecated) |
| 1436 | continue; |
| 1437 | |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1438 | if (event_glob != NULL && |
Andi Kleen | 38d14f0 | 2016-10-19 10:50:01 -0700 | [diff] [blame] | 1439 | !(strglobmatch_nocase(name, event_glob) || |
| 1440 | (!is_cpu && strglobmatch_nocase(alias->name, |
Andi Kleen | 67bdc35 | 2016-10-19 11:45:23 -0700 | [diff] [blame] | 1441 | event_glob)) || |
| 1442 | (alias->topic && |
| 1443 | strglobmatch_nocase(alias->topic, event_glob)))) |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1444 | continue; |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1445 | |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1446 | if (is_cpu && !name_only && !alias->desc) |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1447 | name = format_alias_or(buf, sizeof(buf), pmu, alias); |
| 1448 | |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1449 | aliases[j].name = name; |
| 1450 | if (is_cpu && !name_only && !alias->desc) |
| 1451 | aliases[j].name = format_alias_or(buf, |
| 1452 | sizeof(buf), |
| 1453 | pmu, alias); |
| 1454 | aliases[j].name = strdup(aliases[j].name); |
| 1455 | if (!aliases[j].name) |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1456 | goto out_enomem; |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1457 | |
Sukadev Bhattiprolu | c8d6828 | 2016-09-15 15:24:48 -0700 | [diff] [blame] | 1458 | aliases[j].desc = long_desc ? alias->long_desc : |
| 1459 | alias->desc; |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1460 | aliases[j].topic = alias->topic; |
Andi Kleen | f236102 | 2017-01-27 18:03:40 -0800 | [diff] [blame] | 1461 | aliases[j].str = alias->str; |
| 1462 | aliases[j].pmu = pmu->name; |
Andi Kleen | 7f372a6 | 2017-03-20 13:17:09 -0700 | [diff] [blame] | 1463 | aliases[j].metric_expr = alias->metric_expr; |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 1464 | aliases[j].metric_name = alias->metric_name; |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1465 | j++; |
| 1466 | } |
Arnaldo Carvalho de Melo | fa52cea | 2015-10-02 15:28:16 -0300 | [diff] [blame] | 1467 | if (pmu->selectable && |
| 1468 | (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1469 | char *s; |
| 1470 | if (asprintf(&s, "%s//", pmu->name) < 0) |
| 1471 | goto out_enomem; |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1472 | aliases[j].name = s; |
Adrian Hunter | 42634bc | 2014-10-23 13:45:10 +0300 | [diff] [blame] | 1473 | j++; |
| 1474 | } |
| 1475 | } |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1476 | len = j; |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1477 | qsort(aliases, len, sizeof(struct sevent), cmp_sevent); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1478 | for (j = 0; j < len; j++) { |
Andi Kleen | 15b22ed | 2017-01-27 18:03:38 -0800 | [diff] [blame] | 1479 | /* Skip duplicates */ |
| 1480 | if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) |
| 1481 | continue; |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1482 | if (name_only) { |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1483 | printf("%s ", aliases[j].name); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1484 | continue; |
| 1485 | } |
Andi Kleen | 1c5f01f | 2016-09-15 15:24:45 -0700 | [diff] [blame] | 1486 | if (aliases[j].desc && !quiet_flag) { |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1487 | if (numdesc++ == 0) |
| 1488 | printf("\n"); |
Andi Kleen | dd5f103 | 2016-09-15 15:24:50 -0700 | [diff] [blame] | 1489 | if (aliases[j].topic && (!topic || |
| 1490 | strcmp(topic, aliases[j].topic))) { |
| 1491 | printf("%s%s:\n", topic ? "\n" : "", |
| 1492 | aliases[j].topic); |
| 1493 | topic = aliases[j].topic; |
| 1494 | } |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1495 | printf(" %-50s\n", aliases[j].name); |
| 1496 | printf("%*s", 8, "["); |
| 1497 | wordwrap(aliases[j].desc, 8, columns, 0); |
| 1498 | printf("]\n"); |
Andi Kleen | bf874fc | 2017-03-20 13:17:11 -0700 | [diff] [blame] | 1499 | if (details_flag) { |
Andi Kleen | 7f372a6 | 2017-03-20 13:17:09 -0700 | [diff] [blame] | 1500 | printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); |
Andi Kleen | 9628481 | 2017-03-20 13:17:10 -0700 | [diff] [blame] | 1501 | if (aliases[j].metric_name) |
| 1502 | printf(" MetricName: %s", aliases[j].metric_name); |
Andi Kleen | 7f372a6 | 2017-03-20 13:17:09 -0700 | [diff] [blame] | 1503 | if (aliases[j].metric_expr) |
| 1504 | printf(" MetricExpr: %s", aliases[j].metric_expr); |
| 1505 | putchar('\n'); |
| 1506 | } |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1507 | } else |
| 1508 | printf(" %-50s [Kernel PMU event]\n", aliases[j].name); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1509 | printed++; |
| 1510 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 1511 | if (printed && pager_in_use()) |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1512 | printf("\n"); |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1513 | out_free: |
| 1514 | for (j = 0; j < len; j++) |
Andi Kleen | 08e60ed | 2016-09-15 15:24:43 -0700 | [diff] [blame] | 1515 | zfree(&aliases[j].name); |
Arnaldo Carvalho de Melo | 7e4772d | 2014-10-24 10:25:09 -0300 | [diff] [blame] | 1516 | zfree(&aliases); |
| 1517 | return; |
| 1518 | |
| 1519 | out_enomem: |
| 1520 | printf("FATAL: not enough memory to print PMU events\n"); |
| 1521 | if (aliases) |
| 1522 | goto out_free; |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1523 | } |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1524 | |
| 1525 | bool pmu_have_event(const char *pname, const char *name) |
| 1526 | { |
| 1527 | struct perf_pmu *pmu; |
| 1528 | struct perf_pmu_alias *alias; |
| 1529 | |
| 1530 | pmu = NULL; |
| 1531 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
| 1532 | if (strcmp(pname, pmu->name)) |
| 1533 | continue; |
| 1534 | list_for_each_entry(alias, &pmu->aliases, list) |
| 1535 | if (!strcmp(alias->name, name)) |
| 1536 | return true; |
| 1537 | } |
| 1538 | return false; |
| 1539 | } |
Adrian Hunter | 7d4bdab | 2014-07-31 09:00:50 +0300 | [diff] [blame] | 1540 | |
| 1541 | static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) |
| 1542 | { |
Adrian Hunter | 7d4bdab | 2014-07-31 09:00:50 +0300 | [diff] [blame] | 1543 | char path[PATH_MAX]; |
| 1544 | const char *sysfs; |
| 1545 | |
| 1546 | sysfs = sysfs__mountpoint(); |
| 1547 | if (!sysfs) |
| 1548 | return NULL; |
| 1549 | |
| 1550 | snprintf(path, PATH_MAX, |
| 1551 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); |
Andi Kleen | d966458 | 2019-11-20 16:15:11 -0800 | [diff] [blame] | 1552 | if (!file_available(path)) |
Adrian Hunter | 7d4bdab | 2014-07-31 09:00:50 +0300 | [diff] [blame] | 1553 | return NULL; |
Adrian Hunter | 7d4bdab | 2014-07-31 09:00:50 +0300 | [diff] [blame] | 1554 | return fopen(path, "r"); |
| 1555 | } |
| 1556 | |
| 1557 | int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, |
| 1558 | ...) |
| 1559 | { |
| 1560 | va_list args; |
| 1561 | FILE *file; |
| 1562 | int ret = EOF; |
| 1563 | |
| 1564 | va_start(args, fmt); |
| 1565 | file = perf_pmu__open_file(pmu, name); |
| 1566 | if (file) { |
| 1567 | ret = vfscanf(file, fmt, args); |
| 1568 | fclose(file); |
| 1569 | } |
| 1570 | va_end(args); |
| 1571 | return ret; |
| 1572 | } |