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