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