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