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