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