]>
Commit | Line | Data |
---|---|---|
cd82a32e | 1 | #include <linux/list.h> |
c5de47f2 | 2 | #include <linux/compiler.h> |
cd82a32e | 3 | #include <sys/types.h> |
cd82a32e JO |
4 | #include <unistd.h> |
5 | #include <stdio.h> | |
dc0a6202 | 6 | #include <stdbool.h> |
7d4bdab5 | 7 | #include <stdarg.h> |
cd82a32e | 8 | #include <dirent.h> |
cd0cfad7 | 9 | #include <api/fs/fs.h> |
410136f5 | 10 | #include <locale.h> |
cd82a32e JO |
11 | #include "util.h" |
12 | #include "pmu.h" | |
13 | #include "parse-events.h" | |
7ae92e74 | 14 | #include "cpumap.h" |
933f82ff SB |
15 | #include "header.h" |
16 | #include "pmu-events/pmu-events.h" | |
cd82a32e | 17 | |
ab1bf653 ACM |
18 | struct perf_pmu_format { |
19 | char *name; | |
20 | int value; | |
21 | DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); | |
22 | struct list_head list; | |
23 | }; | |
24 | ||
50a9667c RR |
25 | #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" |
26 | ||
cd82a32e JO |
27 | int perf_pmu_parse(struct list_head *list, char *name); |
28 | extern FILE *perf_pmu_in; | |
29 | ||
30 | static LIST_HEAD(pmus); | |
31 | ||
32 | /* | |
33 | * Parse & process all the sysfs attributes located under | |
34 | * the directory specified in 'dir' parameter. | |
35 | */ | |
cff7f956 | 36 | int perf_pmu__format_parse(char *dir, struct list_head *head) |
cd82a32e JO |
37 | { |
38 | struct dirent *evt_ent; | |
39 | DIR *format_dir; | |
40 | int ret = 0; | |
41 | ||
42 | format_dir = opendir(dir); | |
43 | if (!format_dir) | |
44 | return -EINVAL; | |
45 | ||
46 | while (!ret && (evt_ent = readdir(format_dir))) { | |
47 | char path[PATH_MAX]; | |
48 | char *name = evt_ent->d_name; | |
49 | FILE *file; | |
50 | ||
51 | if (!strcmp(name, ".") || !strcmp(name, "..")) | |
52 | continue; | |
53 | ||
54 | snprintf(path, PATH_MAX, "%s/%s", dir, name); | |
55 | ||
56 | ret = -EINVAL; | |
57 | file = fopen(path, "r"); | |
58 | if (!file) | |
59 | break; | |
60 | ||
61 | perf_pmu_in = file; | |
62 | ret = perf_pmu_parse(head, name); | |
63 | fclose(file); | |
64 | } | |
65 | ||
66 | closedir(format_dir); | |
67 | return ret; | |
68 | } | |
69 | ||
70 | /* | |
71 | * Reading/parsing the default pmu format definition, which should be | |
72 | * located at: | |
73 | * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. | |
74 | */ | |
b6b96fb4 | 75 | static int pmu_format(const char *name, struct list_head *format) |
cd82a32e JO |
76 | { |
77 | struct stat st; | |
78 | char path[PATH_MAX]; | |
cf38fada | 79 | const char *sysfs = sysfs__mountpoint(); |
cd82a32e | 80 | |
cd82a32e JO |
81 | if (!sysfs) |
82 | return -1; | |
83 | ||
84 | snprintf(path, PATH_MAX, | |
50a9667c | 85 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); |
cd82a32e JO |
86 | |
87 | if (stat(path, &st) < 0) | |
9bc8f9fe | 88 | return 0; /* no error if format does not exist */ |
cd82a32e | 89 | |
cff7f956 | 90 | if (perf_pmu__format_parse(path, format)) |
cd82a32e JO |
91 | return -1; |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
410136f5 SE |
96 | static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) |
97 | { | |
98 | struct stat st; | |
99 | ssize_t sret; | |
100 | char scale[128]; | |
101 | int fd, ret = -1; | |
102 | char path[PATH_MAX]; | |
ea8f75f9 | 103 | char *lc; |
410136f5 SE |
104 | |
105 | snprintf(path, PATH_MAX, "%s/%s.scale", dir, name); | |
106 | ||
107 | fd = open(path, O_RDONLY); | |
108 | if (fd == -1) | |
109 | return -1; | |
110 | ||
111 | if (fstat(fd, &st) < 0) | |
112 | goto error; | |
113 | ||
114 | sret = read(fd, scale, sizeof(scale)-1); | |
115 | if (sret < 0) | |
116 | goto error; | |
117 | ||
9ecae065 MS |
118 | if (scale[sret - 1] == '\n') |
119 | scale[sret - 1] = '\0'; | |
120 | else | |
121 | scale[sret] = '\0'; | |
122 | ||
410136f5 SE |
123 | /* |
124 | * save current locale | |
125 | */ | |
126 | lc = setlocale(LC_NUMERIC, NULL); | |
127 | ||
f9a5978a JO |
128 | /* |
129 | * The lc string may be allocated in static storage, | |
130 | * so get a dynamic copy to make it survive setlocale | |
131 | * call below. | |
132 | */ | |
133 | lc = strdup(lc); | |
134 | if (!lc) { | |
135 | ret = -ENOMEM; | |
136 | goto error; | |
137 | } | |
138 | ||
410136f5 SE |
139 | /* |
140 | * force to C locale to ensure kernel | |
141 | * scale string is converted correctly. | |
142 | * kernel uses default C locale. | |
143 | */ | |
144 | setlocale(LC_NUMERIC, "C"); | |
145 | ||
146 | alias->scale = strtod(scale, NULL); | |
147 | ||
148 | /* restore locale */ | |
149 | setlocale(LC_NUMERIC, lc); | |
150 | ||
ea8f75f9 | 151 | free(lc); |
f9a5978a | 152 | |
410136f5 SE |
153 | ret = 0; |
154 | error: | |
155 | close(fd); | |
156 | return ret; | |
157 | } | |
158 | ||
159 | static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) | |
160 | { | |
161 | char path[PATH_MAX]; | |
162 | ssize_t sret; | |
163 | int fd; | |
164 | ||
165 | snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); | |
166 | ||
167 | fd = open(path, O_RDONLY); | |
168 | if (fd == -1) | |
169 | return -1; | |
170 | ||
d85ce830 | 171 | sret = read(fd, alias->unit, UNIT_MAX_LEN); |
410136f5 SE |
172 | if (sret < 0) |
173 | goto error; | |
174 | ||
175 | close(fd); | |
176 | ||
9ecae065 MS |
177 | if (alias->unit[sret - 1] == '\n') |
178 | alias->unit[sret - 1] = '\0'; | |
179 | else | |
180 | alias->unit[sret] = '\0'; | |
410136f5 SE |
181 | |
182 | return 0; | |
183 | error: | |
184 | close(fd); | |
185 | alias->unit[0] = '\0'; | |
186 | return -1; | |
187 | } | |
188 | ||
044330c1 MF |
189 | static int |
190 | perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) | |
191 | { | |
192 | char path[PATH_MAX]; | |
193 | int fd; | |
194 | ||
195 | snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); | |
196 | ||
197 | fd = open(path, O_RDONLY); | |
198 | if (fd == -1) | |
199 | return -1; | |
200 | ||
201 | close(fd); | |
202 | ||
203 | alias->per_pkg = true; | |
204 | return 0; | |
205 | } | |
206 | ||
1d9e446b JO |
207 | static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, |
208 | char *dir, char *name) | |
209 | { | |
210 | char path[PATH_MAX]; | |
211 | int fd; | |
212 | ||
213 | snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); | |
214 | ||
215 | fd = open(path, O_RDONLY); | |
216 | if (fd == -1) | |
217 | return -1; | |
218 | ||
219 | alias->snapshot = true; | |
220 | close(fd); | |
221 | return 0; | |
222 | } | |
223 | ||
70c646e0 SB |
224 | static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, |
225 | char *desc __maybe_unused, char *val) | |
a6146d50 | 226 | { |
5c6ccc37 | 227 | struct perf_pmu_alias *alias; |
a6146d50 ZY |
228 | int ret; |
229 | ||
a6146d50 ZY |
230 | alias = malloc(sizeof(*alias)); |
231 | if (!alias) | |
232 | return -ENOMEM; | |
233 | ||
234 | INIT_LIST_HEAD(&alias->terms); | |
410136f5 SE |
235 | alias->scale = 1.0; |
236 | alias->unit[0] = '\0'; | |
044330c1 | 237 | alias->per_pkg = false; |
84530920 | 238 | alias->snapshot = false; |
410136f5 | 239 | |
70c646e0 | 240 | ret = parse_events_terms(&alias->terms, val); |
a6146d50 | 241 | if (ret) { |
70c646e0 | 242 | pr_err("Cannot parse alias %s: %d\n", val, ret); |
a6146d50 ZY |
243 | free(alias); |
244 | return ret; | |
245 | } | |
246 | ||
247 | alias->name = strdup(name); | |
70c646e0 SB |
248 | if (dir) { |
249 | /* | |
250 | * load unit name and scale if available | |
251 | */ | |
252 | perf_pmu__parse_unit(alias, dir, name); | |
253 | perf_pmu__parse_scale(alias, dir, name); | |
254 | perf_pmu__parse_per_pkg(alias, dir, name); | |
255 | perf_pmu__parse_snapshot(alias, dir, name); | |
256 | } | |
410136f5 | 257 | |
a6146d50 | 258 | list_add_tail(&alias->list, list); |
410136f5 | 259 | |
a6146d50 ZY |
260 | return 0; |
261 | } | |
262 | ||
70c646e0 SB |
263 | static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) |
264 | { | |
265 | char buf[256]; | |
266 | int ret; | |
267 | ||
268 | ret = fread(buf, 1, sizeof(buf), file); | |
269 | if (ret == 0) | |
270 | return -EINVAL; | |
271 | ||
272 | buf[ret] = 0; | |
273 | ||
274 | return __perf_pmu__new_alias(list, dir, name, NULL, buf); | |
275 | } | |
276 | ||
46441bdc MF |
277 | static inline bool pmu_alias_info_file(char *name) |
278 | { | |
279 | size_t len; | |
280 | ||
281 | len = strlen(name); | |
282 | if (len > 5 && !strcmp(name + len - 5, ".unit")) | |
283 | return true; | |
284 | if (len > 6 && !strcmp(name + len - 6, ".scale")) | |
285 | return true; | |
044330c1 MF |
286 | if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) |
287 | return true; | |
1d9e446b JO |
288 | if (len > 9 && !strcmp(name + len - 9, ".snapshot")) |
289 | return true; | |
46441bdc MF |
290 | |
291 | return false; | |
292 | } | |
293 | ||
a6146d50 ZY |
294 | /* |
295 | * Process all the sysfs attributes located under the directory | |
296 | * specified in 'dir' parameter. | |
297 | */ | |
298 | static int pmu_aliases_parse(char *dir, struct list_head *head) | |
299 | { | |
300 | struct dirent *evt_ent; | |
301 | DIR *event_dir; | |
a6146d50 ZY |
302 | |
303 | event_dir = opendir(dir); | |
304 | if (!event_dir) | |
305 | return -EINVAL; | |
306 | ||
940db6dc | 307 | while ((evt_ent = readdir(event_dir))) { |
a6146d50 ZY |
308 | char path[PATH_MAX]; |
309 | char *name = evt_ent->d_name; | |
310 | FILE *file; | |
311 | ||
312 | if (!strcmp(name, ".") || !strcmp(name, "..")) | |
313 | continue; | |
314 | ||
410136f5 | 315 | /* |
46441bdc | 316 | * skip info files parsed in perf_pmu__new_alias() |
410136f5 | 317 | */ |
46441bdc | 318 | if (pmu_alias_info_file(name)) |
410136f5 SE |
319 | continue; |
320 | ||
a6146d50 ZY |
321 | snprintf(path, PATH_MAX, "%s/%s", dir, name); |
322 | ||
a6146d50 | 323 | file = fopen(path, "r"); |
940db6dc AK |
324 | if (!file) { |
325 | pr_debug("Cannot open %s\n", path); | |
326 | continue; | |
327 | } | |
410136f5 | 328 | |
940db6dc AK |
329 | if (perf_pmu__new_alias(head, dir, name, file) < 0) |
330 | pr_debug("Cannot set up %s\n", name); | |
a6146d50 ZY |
331 | fclose(file); |
332 | } | |
333 | ||
334 | closedir(event_dir); | |
940db6dc | 335 | return 0; |
a6146d50 ZY |
336 | } |
337 | ||
338 | /* | |
339 | * Reading the pmu event aliases definition, which should be located at: | |
340 | * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. | |
341 | */ | |
b6b96fb4 | 342 | static int pmu_aliases(const char *name, struct list_head *head) |
a6146d50 ZY |
343 | { |
344 | struct stat st; | |
345 | char path[PATH_MAX]; | |
cf38fada | 346 | const char *sysfs = sysfs__mountpoint(); |
a6146d50 | 347 | |
a6146d50 ZY |
348 | if (!sysfs) |
349 | return -1; | |
350 | ||
351 | snprintf(path, PATH_MAX, | |
352 | "%s/bus/event_source/devices/%s/events", sysfs, name); | |
353 | ||
354 | if (stat(path, &st) < 0) | |
3fded963 | 355 | return 0; /* no error if 'events' does not exist */ |
a6146d50 ZY |
356 | |
357 | if (pmu_aliases_parse(path, head)) | |
358 | return -1; | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
5c6ccc37 | 363 | static int pmu_alias_terms(struct perf_pmu_alias *alias, |
a6146d50 ZY |
364 | struct list_head *terms) |
365 | { | |
7c2f8164 | 366 | struct parse_events_term *term, *cloned; |
a6146d50 ZY |
367 | LIST_HEAD(list); |
368 | int ret; | |
369 | ||
370 | list_for_each_entry(term, &alias->terms, list) { | |
7c2f8164 | 371 | ret = parse_events_term__clone(&cloned, term); |
a6146d50 | 372 | if (ret) { |
682dc24c | 373 | parse_events_terms__purge(&list); |
a6146d50 ZY |
374 | return ret; |
375 | } | |
7c2f8164 | 376 | list_add_tail(&cloned->list, &list); |
a6146d50 ZY |
377 | } |
378 | list_splice(&list, terms); | |
379 | return 0; | |
380 | } | |
381 | ||
cd82a32e JO |
382 | /* |
383 | * Reading/parsing the default pmu type value, which should be | |
384 | * located at: | |
385 | * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. | |
386 | */ | |
b6b96fb4 | 387 | static int pmu_type(const char *name, __u32 *type) |
cd82a32e JO |
388 | { |
389 | struct stat st; | |
390 | char path[PATH_MAX]; | |
cd82a32e JO |
391 | FILE *file; |
392 | int ret = 0; | |
cf38fada | 393 | const char *sysfs = sysfs__mountpoint(); |
cd82a32e | 394 | |
cd82a32e JO |
395 | if (!sysfs) |
396 | return -1; | |
397 | ||
398 | snprintf(path, PATH_MAX, | |
50a9667c | 399 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); |
cd82a32e JO |
400 | |
401 | if (stat(path, &st) < 0) | |
402 | return -1; | |
403 | ||
404 | file = fopen(path, "r"); | |
405 | if (!file) | |
406 | return -EINVAL; | |
407 | ||
408 | if (1 != fscanf(file, "%u", type)) | |
409 | ret = -1; | |
410 | ||
411 | fclose(file); | |
412 | return ret; | |
413 | } | |
414 | ||
50a9667c RR |
415 | /* Add all pmus in sysfs to pmu list: */ |
416 | static void pmu_read_sysfs(void) | |
417 | { | |
418 | char path[PATH_MAX]; | |
50a9667c RR |
419 | DIR *dir; |
420 | struct dirent *dent; | |
cf38fada | 421 | const char *sysfs = sysfs__mountpoint(); |
50a9667c | 422 | |
50a9667c RR |
423 | if (!sysfs) |
424 | return; | |
425 | ||
426 | snprintf(path, PATH_MAX, | |
427 | "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); | |
428 | ||
429 | dir = opendir(path); | |
430 | if (!dir) | |
431 | return; | |
432 | ||
433 | while ((dent = readdir(dir))) { | |
434 | if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) | |
435 | continue; | |
436 | /* add to static LIST_HEAD(pmus): */ | |
437 | perf_pmu__find(dent->d_name); | |
438 | } | |
439 | ||
440 | closedir(dir); | |
441 | } | |
442 | ||
b6b96fb4 | 443 | static struct cpu_map *pmu_cpumask(const char *name) |
7ae92e74 YZ |
444 | { |
445 | struct stat st; | |
446 | char path[PATH_MAX]; | |
7ae92e74 YZ |
447 | FILE *file; |
448 | struct cpu_map *cpus; | |
cf38fada | 449 | const char *sysfs = sysfs__mountpoint(); |
7e3fcffe MR |
450 | const char *templates[] = { |
451 | "%s/bus/event_source/devices/%s/cpumask", | |
452 | "%s/bus/event_source/devices/%s/cpus", | |
453 | NULL | |
454 | }; | |
455 | const char **template; | |
7ae92e74 | 456 | |
7ae92e74 YZ |
457 | if (!sysfs) |
458 | return NULL; | |
459 | ||
7e3fcffe MR |
460 | for (template = templates; *template; template++) { |
461 | snprintf(path, PATH_MAX, *template, sysfs, name); | |
462 | if (stat(path, &st) == 0) | |
463 | break; | |
464 | } | |
7ae92e74 | 465 | |
7e3fcffe | 466 | if (!*template) |
7ae92e74 YZ |
467 | return NULL; |
468 | ||
469 | file = fopen(path, "r"); | |
470 | if (!file) | |
471 | return NULL; | |
472 | ||
473 | cpus = cpu_map__read(file); | |
474 | fclose(file); | |
475 | return cpus; | |
476 | } | |
477 | ||
933f82ff SB |
478 | /* |
479 | * Return the CPU id as a raw string. | |
480 | * | |
481 | * Each architecture should provide a more precise id string that | |
482 | * can be use to match the architecture's "mapfile". | |
483 | */ | |
484 | char * __weak get_cpuid_str(void) | |
485 | { | |
486 | return NULL; | |
487 | } | |
488 | ||
489 | /* | |
490 | * From the pmu_events_map, find the table of PMU events that corresponds | |
491 | * to the current running CPU. Then, add all PMU events from that table | |
492 | * as aliases. | |
493 | */ | |
494 | static void pmu_add_cpu_aliases(struct list_head *head) | |
495 | { | |
496 | int i; | |
497 | struct pmu_events_map *map; | |
498 | struct pmu_event *pe; | |
499 | char *cpuid; | |
500 | ||
501 | cpuid = get_cpuid_str(); | |
502 | if (!cpuid) | |
503 | return; | |
504 | ||
505 | i = 0; | |
506 | while (1) { | |
507 | map = &pmu_events_map[i++]; | |
508 | if (!map->table) | |
509 | goto out; | |
510 | ||
511 | if (!strcmp(map->cpuid, cpuid)) | |
512 | break; | |
513 | } | |
514 | ||
515 | /* | |
516 | * Found a matching PMU events table. Create aliases | |
517 | */ | |
518 | i = 0; | |
519 | while (1) { | |
520 | pe = &map->table[i++]; | |
521 | if (!pe->name) | |
522 | break; | |
523 | ||
524 | /* need type casts to override 'const' */ | |
525 | __perf_pmu__new_alias(head, NULL, (char *)pe->name, | |
526 | (char *)pe->desc, (char *)pe->event); | |
527 | } | |
528 | ||
529 | out: | |
530 | free(cpuid); | |
531 | } | |
532 | ||
c5de47f2 | 533 | struct perf_event_attr * __weak |
dc0a6202 AH |
534 | perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) |
535 | { | |
536 | return NULL; | |
537 | } | |
538 | ||
b6b96fb4 | 539 | static struct perf_pmu *pmu_lookup(const char *name) |
cd82a32e JO |
540 | { |
541 | struct perf_pmu *pmu; | |
542 | LIST_HEAD(format); | |
a6146d50 | 543 | LIST_HEAD(aliases); |
cd82a32e JO |
544 | __u32 type; |
545 | ||
546 | /* | |
547 | * The pmu data we store & need consists of the pmu | |
548 | * type value and format definitions. Load both right | |
549 | * now. | |
550 | */ | |
551 | if (pmu_format(name, &format)) | |
552 | return NULL; | |
553 | ||
3fded963 JO |
554 | if (pmu_aliases(name, &aliases)) |
555 | return NULL; | |
556 | ||
933f82ff SB |
557 | if (!strcmp(name, "cpu")) |
558 | pmu_add_cpu_aliases(&aliases); | |
559 | ||
cd82a32e JO |
560 | if (pmu_type(name, &type)) |
561 | return NULL; | |
562 | ||
563 | pmu = zalloc(sizeof(*pmu)); | |
564 | if (!pmu) | |
565 | return NULL; | |
566 | ||
7ae92e74 YZ |
567 | pmu->cpus = pmu_cpumask(name); |
568 | ||
cd82a32e | 569 | INIT_LIST_HEAD(&pmu->format); |
a6146d50 | 570 | INIT_LIST_HEAD(&pmu->aliases); |
cd82a32e | 571 | list_splice(&format, &pmu->format); |
a6146d50 | 572 | list_splice(&aliases, &pmu->aliases); |
cd82a32e JO |
573 | pmu->name = strdup(name); |
574 | pmu->type = type; | |
9bc8f9fe | 575 | list_add_tail(&pmu->list, &pmus); |
dc0a6202 AH |
576 | |
577 | pmu->default_config = perf_pmu__get_default_config(pmu); | |
578 | ||
cd82a32e JO |
579 | return pmu; |
580 | } | |
581 | ||
b6b96fb4 | 582 | static struct perf_pmu *pmu_find(const char *name) |
cd82a32e JO |
583 | { |
584 | struct perf_pmu *pmu; | |
585 | ||
586 | list_for_each_entry(pmu, &pmus, list) | |
587 | if (!strcmp(pmu->name, name)) | |
588 | return pmu; | |
589 | ||
590 | return NULL; | |
591 | } | |
592 | ||
50a9667c RR |
593 | struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) |
594 | { | |
595 | /* | |
596 | * pmu iterator: If pmu is NULL, we start at the begin, | |
597 | * otherwise return the next pmu. Returns NULL on end. | |
598 | */ | |
599 | if (!pmu) { | |
600 | pmu_read_sysfs(); | |
601 | pmu = list_prepare_entry(pmu, &pmus, list); | |
602 | } | |
603 | list_for_each_entry_continue(pmu, &pmus, list) | |
604 | return pmu; | |
605 | return NULL; | |
606 | } | |
607 | ||
b6b96fb4 | 608 | struct perf_pmu *perf_pmu__find(const char *name) |
cd82a32e JO |
609 | { |
610 | struct perf_pmu *pmu; | |
611 | ||
612 | /* | |
613 | * Once PMU is loaded it stays in the list, | |
614 | * so we keep us from multiple reading/parsing | |
615 | * the pmu format definitions. | |
616 | */ | |
617 | pmu = pmu_find(name); | |
618 | if (pmu) | |
619 | return pmu; | |
620 | ||
621 | return pmu_lookup(name); | |
622 | } | |
623 | ||
5c6ccc37 | 624 | static struct perf_pmu_format * |
09ff6071 | 625 | pmu_find_format(struct list_head *formats, const char *name) |
cd82a32e | 626 | { |
5c6ccc37 | 627 | struct perf_pmu_format *format; |
cd82a32e JO |
628 | |
629 | list_for_each_entry(format, formats, list) | |
630 | if (!strcmp(format->name, name)) | |
631 | return format; | |
632 | ||
633 | return NULL; | |
634 | } | |
635 | ||
09ff6071 AH |
636 | __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) |
637 | { | |
638 | struct perf_pmu_format *format = pmu_find_format(formats, name); | |
639 | __u64 bits = 0; | |
640 | int fbit; | |
641 | ||
642 | if (!format) | |
643 | return 0; | |
644 | ||
645 | for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) | |
646 | bits |= 1ULL << fbit; | |
647 | ||
648 | return bits; | |
649 | } | |
650 | ||
cd82a32e | 651 | /* |
dc0a6202 | 652 | * Sets value based on the format definition (format parameter) |
cd82a32e | 653 | * and unformated value (value parameter). |
cd82a32e | 654 | */ |
dc0a6202 AH |
655 | static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, |
656 | bool zero) | |
cd82a32e JO |
657 | { |
658 | unsigned long fbit, vbit; | |
cd82a32e JO |
659 | |
660 | for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { | |
661 | ||
662 | if (!test_bit(fbit, format)) | |
663 | continue; | |
664 | ||
dc0a6202 AH |
665 | if (value & (1llu << vbit++)) |
666 | *v |= (1llu << fbit); | |
667 | else if (zero) | |
668 | *v &= ~(1llu << fbit); | |
cd82a32e | 669 | } |
cd82a32e JO |
670 | } |
671 | ||
0efe6b67 AH |
672 | static __u64 pmu_format_max_value(const unsigned long *format) |
673 | { | |
ac0e2cd5 KL |
674 | __u64 w = 0; |
675 | int fbit; | |
0efe6b67 | 676 | |
ac0e2cd5 KL |
677 | for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS) |
678 | w |= (1ULL << fbit); | |
679 | ||
680 | return w; | |
0efe6b67 AH |
681 | } |
682 | ||
688d4dfc CS |
683 | /* |
684 | * Term is a string term, and might be a param-term. Try to look up it's value | |
685 | * in the remaining terms. | |
686 | * - We have a term like "base-or-format-term=param-term", | |
687 | * - We need to find the value supplied for "param-term" (with param-term named | |
688 | * in a config string) later on in the term list. | |
689 | */ | |
690 | static int pmu_resolve_param_term(struct parse_events_term *term, | |
691 | struct list_head *head_terms, | |
692 | __u64 *value) | |
693 | { | |
694 | struct parse_events_term *t; | |
695 | ||
696 | list_for_each_entry(t, head_terms, list) { | |
697 | if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { | |
698 | if (!strcmp(t->config, term->config)) { | |
699 | t->used = true; | |
700 | *value = t->val.num; | |
701 | return 0; | |
702 | } | |
703 | } | |
704 | } | |
705 | ||
706 | if (verbose) | |
707 | printf("Required parameter '%s' not specified\n", term->config); | |
708 | ||
709 | return -1; | |
710 | } | |
711 | ||
ffeb883e | 712 | static char *pmu_formats_string(struct list_head *formats) |
e64b020b JO |
713 | { |
714 | struct perf_pmu_format *format; | |
11db4e29 MH |
715 | char *str = NULL; |
716 | struct strbuf buf = STRBUF_INIT; | |
e64b020b JO |
717 | unsigned i = 0; |
718 | ||
ffeb883e | 719 | if (!formats) |
e64b020b JO |
720 | return NULL; |
721 | ||
722 | /* sysfs exported terms */ | |
ffeb883e | 723 | list_for_each_entry(format, formats, list) |
11db4e29 MH |
724 | if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) |
725 | goto error; | |
e64b020b | 726 | |
ffeb883e | 727 | str = strbuf_detach(&buf, NULL); |
11db4e29 | 728 | error: |
ffeb883e | 729 | strbuf_release(&buf); |
e64b020b | 730 | |
e64b020b | 731 | return str; |
e64b020b JO |
732 | } |
733 | ||
cd82a32e JO |
734 | /* |
735 | * Setup one of config[12] attr members based on the | |
88aca8d9 | 736 | * user input data - term parameter. |
cd82a32e JO |
737 | */ |
738 | static int pmu_config_term(struct list_head *formats, | |
739 | struct perf_event_attr *attr, | |
dc0a6202 | 740 | struct parse_events_term *term, |
688d4dfc | 741 | struct list_head *head_terms, |
e64b020b | 742 | bool zero, struct parse_events_error *err) |
cd82a32e | 743 | { |
5c6ccc37 | 744 | struct perf_pmu_format *format; |
cd82a32e | 745 | __u64 *vp; |
0efe6b67 | 746 | __u64 val, max_val; |
688d4dfc CS |
747 | |
748 | /* | |
749 | * If this is a parameter we've already used for parameterized-eval, | |
750 | * skip it in normal eval. | |
751 | */ | |
752 | if (term->used) | |
753 | return 0; | |
cd82a32e JO |
754 | |
755 | /* | |
cd82a32e JO |
756 | * Hardcoded terms should be already in, so nothing |
757 | * to be done for them. | |
758 | */ | |
759 | if (parse_events__is_hardcoded_term(term)) | |
760 | return 0; | |
761 | ||
cd82a32e | 762 | format = pmu_find_format(formats, term->config); |
688d4dfc CS |
763 | if (!format) { |
764 | if (verbose) | |
765 | printf("Invalid event/parameter '%s'\n", term->config); | |
e64b020b | 766 | if (err) { |
ffeb883e HK |
767 | char *pmu_term = pmu_formats_string(formats); |
768 | ||
e64b020b JO |
769 | err->idx = term->err_term; |
770 | err->str = strdup("unknown term"); | |
ffeb883e HK |
771 | err->help = parse_events_formats_error_string(pmu_term); |
772 | free(pmu_term); | |
e64b020b | 773 | } |
cd82a32e | 774 | return -EINVAL; |
688d4dfc | 775 | } |
cd82a32e JO |
776 | |
777 | switch (format->value) { | |
778 | case PERF_PMU_FORMAT_VALUE_CONFIG: | |
779 | vp = &attr->config; | |
780 | break; | |
781 | case PERF_PMU_FORMAT_VALUE_CONFIG1: | |
782 | vp = &attr->config1; | |
783 | break; | |
784 | case PERF_PMU_FORMAT_VALUE_CONFIG2: | |
785 | vp = &attr->config2; | |
786 | break; | |
787 | default: | |
788 | return -EINVAL; | |
789 | } | |
790 | ||
16fa7e82 | 791 | /* |
688d4dfc CS |
792 | * Either directly use a numeric term, or try to translate string terms |
793 | * using event parameters. | |
16fa7e82 | 794 | */ |
688d4dfc CS |
795 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) |
796 | val = term->val.num; | |
797 | else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { | |
798 | if (strcmp(term->val.str, "?")) { | |
e64b020b | 799 | if (verbose) { |
688d4dfc CS |
800 | pr_info("Invalid sysfs entry %s=%s\n", |
801 | term->config, term->val.str); | |
e64b020b JO |
802 | } |
803 | if (err) { | |
804 | err->idx = term->err_val; | |
805 | err->str = strdup("expected numeric value"); | |
806 | } | |
688d4dfc CS |
807 | return -EINVAL; |
808 | } | |
809 | ||
810 | if (pmu_resolve_param_term(term, head_terms, &val)) | |
811 | return -EINVAL; | |
812 | } else | |
813 | return -EINVAL; | |
814 | ||
0efe6b67 AH |
815 | max_val = pmu_format_max_value(format->bits); |
816 | if (val > max_val) { | |
817 | if (err) { | |
818 | err->idx = term->err_val; | |
819 | if (asprintf(&err->str, | |
820 | "value too big for format, maximum is %llu", | |
821 | (unsigned long long)max_val) < 0) | |
822 | err->str = strdup("value too big for format"); | |
823 | return -EINVAL; | |
824 | } | |
825 | /* | |
826 | * Assume we don't care if !err, in which case the value will be | |
827 | * silently truncated. | |
828 | */ | |
829 | } | |
830 | ||
688d4dfc | 831 | pmu_format_value(format->bits, val, vp, zero); |
cd82a32e JO |
832 | return 0; |
833 | } | |
834 | ||
cff7f956 JO |
835 | int perf_pmu__config_terms(struct list_head *formats, |
836 | struct perf_event_attr *attr, | |
dc0a6202 | 837 | struct list_head *head_terms, |
e64b020b | 838 | bool zero, struct parse_events_error *err) |
cd82a32e | 839 | { |
6cee6cd3 | 840 | struct parse_events_term *term; |
cd82a32e | 841 | |
688d4dfc | 842 | list_for_each_entry(term, head_terms, list) { |
e64b020b JO |
843 | if (pmu_config_term(formats, attr, term, head_terms, |
844 | zero, err)) | |
cd82a32e | 845 | return -EINVAL; |
688d4dfc | 846 | } |
cd82a32e JO |
847 | |
848 | return 0; | |
849 | } | |
850 | ||
851 | /* | |
852 | * Configures event's 'attr' parameter based on the: | |
853 | * 1) users input - specified in terms parameter | |
854 | * 2) pmu format definitions - specified by pmu parameter | |
855 | */ | |
856 | int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, | |
e64b020b JO |
857 | struct list_head *head_terms, |
858 | struct parse_events_error *err) | |
cd82a32e | 859 | { |
dc0a6202 AH |
860 | bool zero = !!pmu->default_config; |
861 | ||
cd82a32e | 862 | attr->type = pmu->type; |
e64b020b JO |
863 | return perf_pmu__config_terms(&pmu->format, attr, head_terms, |
864 | zero, err); | |
cd82a32e JO |
865 | } |
866 | ||
5c6ccc37 ACM |
867 | static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, |
868 | struct parse_events_term *term) | |
a6146d50 | 869 | { |
5c6ccc37 | 870 | struct perf_pmu_alias *alias; |
a6146d50 ZY |
871 | char *name; |
872 | ||
873 | if (parse_events__is_hardcoded_term(term)) | |
874 | return NULL; | |
875 | ||
876 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { | |
877 | if (term->val.num != 1) | |
878 | return NULL; | |
879 | if (pmu_find_format(&pmu->format, term->config)) | |
880 | return NULL; | |
881 | name = term->config; | |
882 | } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { | |
883 | if (strcasecmp(term->config, "event")) | |
884 | return NULL; | |
885 | name = term->val.str; | |
886 | } else { | |
887 | return NULL; | |
888 | } | |
889 | ||
890 | list_for_each_entry(alias, &pmu->aliases, list) { | |
891 | if (!strcasecmp(alias->name, name)) | |
892 | return alias; | |
893 | } | |
894 | return NULL; | |
895 | } | |
896 | ||
410136f5 | 897 | |
1d9e446b JO |
898 | static int check_info_data(struct perf_pmu_alias *alias, |
899 | struct perf_pmu_info *info) | |
410136f5 SE |
900 | { |
901 | /* | |
902 | * Only one term in event definition can | |
1d9e446b JO |
903 | * define unit, scale and snapshot, fail |
904 | * if there's more than one. | |
410136f5 | 905 | */ |
1d9e446b JO |
906 | if ((info->unit && alias->unit) || |
907 | (info->scale && alias->scale) || | |
908 | (info->snapshot && alias->snapshot)) | |
410136f5 SE |
909 | return -EINVAL; |
910 | ||
911 | if (alias->unit) | |
1d9e446b | 912 | info->unit = alias->unit; |
410136f5 SE |
913 | |
914 | if (alias->scale) | |
1d9e446b JO |
915 | info->scale = alias->scale; |
916 | ||
917 | if (alias->snapshot) | |
918 | info->snapshot = alias->snapshot; | |
410136f5 SE |
919 | |
920 | return 0; | |
921 | } | |
922 | ||
a6146d50 ZY |
923 | /* |
924 | * Find alias in the terms list and replace it with the terms | |
925 | * defined for the alias | |
926 | */ | |
410136f5 | 927 | int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, |
46441bdc | 928 | struct perf_pmu_info *info) |
a6146d50 | 929 | { |
6cee6cd3 | 930 | struct parse_events_term *term, *h; |
5c6ccc37 | 931 | struct perf_pmu_alias *alias; |
a6146d50 ZY |
932 | int ret; |
933 | ||
044330c1 MF |
934 | info->per_pkg = false; |
935 | ||
8a398897 SE |
936 | /* |
937 | * Mark unit and scale as not set | |
938 | * (different from default values, see below) | |
939 | */ | |
1d9e446b JO |
940 | info->unit = NULL; |
941 | info->scale = 0.0; | |
942 | info->snapshot = false; | |
410136f5 | 943 | |
a6146d50 ZY |
944 | list_for_each_entry_safe(term, h, head_terms, list) { |
945 | alias = pmu_find_alias(pmu, term); | |
946 | if (!alias) | |
947 | continue; | |
948 | ret = pmu_alias_terms(alias, &term->list); | |
949 | if (ret) | |
950 | return ret; | |
410136f5 | 951 | |
1d9e446b | 952 | ret = check_info_data(alias, info); |
410136f5 SE |
953 | if (ret) |
954 | return ret; | |
955 | ||
044330c1 MF |
956 | if (alias->per_pkg) |
957 | info->per_pkg = true; | |
958 | ||
a6146d50 ZY |
959 | list_del(&term->list); |
960 | free(term); | |
961 | } | |
8a398897 SE |
962 | |
963 | /* | |
964 | * if no unit or scale foundin aliases, then | |
965 | * set defaults as for evsel | |
966 | * unit cannot left to NULL | |
967 | */ | |
46441bdc MF |
968 | if (info->unit == NULL) |
969 | info->unit = ""; | |
8a398897 | 970 | |
46441bdc MF |
971 | if (info->scale == 0.0) |
972 | info->scale = 1.0; | |
8a398897 | 973 | |
a6146d50 ZY |
974 | return 0; |
975 | } | |
976 | ||
cd82a32e JO |
977 | int perf_pmu__new_format(struct list_head *list, char *name, |
978 | int config, unsigned long *bits) | |
979 | { | |
5c6ccc37 | 980 | struct perf_pmu_format *format; |
cd82a32e JO |
981 | |
982 | format = zalloc(sizeof(*format)); | |
983 | if (!format) | |
984 | return -ENOMEM; | |
985 | ||
986 | format->name = strdup(name); | |
987 | format->value = config; | |
988 | memcpy(format->bits, bits, sizeof(format->bits)); | |
989 | ||
990 | list_add_tail(&format->list, list); | |
991 | return 0; | |
992 | } | |
993 | ||
994 | void perf_pmu__set_format(unsigned long *bits, long from, long to) | |
995 | { | |
996 | long b; | |
997 | ||
998 | if (!to) | |
999 | to = from; | |
1000 | ||
15268138 | 1001 | memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); |
cd82a32e JO |
1002 | for (b = from; b <= to; b++) |
1003 | set_bit(b, bits); | |
1004 | } | |
dc098b35 | 1005 | |
aaea3617 CS |
1006 | static int sub_non_neg(int a, int b) |
1007 | { | |
1008 | if (b > a) | |
1009 | return 0; | |
1010 | return a - b; | |
1011 | } | |
1012 | ||
dc098b35 AK |
1013 | static char *format_alias(char *buf, int len, struct perf_pmu *pmu, |
1014 | struct perf_pmu_alias *alias) | |
1015 | { | |
aaea3617 CS |
1016 | struct parse_events_term *term; |
1017 | int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); | |
1018 | ||
1019 | list_for_each_entry(term, &alias->terms, list) { | |
1020 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) | |
1021 | used += snprintf(buf + used, sub_non_neg(len, used), | |
1022 | ",%s=%s", term->config, | |
1023 | term->val.str); | |
1024 | } | |
1025 | ||
1026 | if (sub_non_neg(len, used) > 0) { | |
1027 | buf[used] = '/'; | |
1028 | used++; | |
1029 | } | |
1030 | if (sub_non_neg(len, used) > 0) { | |
1031 | buf[used] = '\0'; | |
1032 | used++; | |
1033 | } else | |
1034 | buf[len - 1] = '\0'; | |
1035 | ||
dc098b35 AK |
1036 | return buf; |
1037 | } | |
1038 | ||
1039 | static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, | |
1040 | struct perf_pmu_alias *alias) | |
1041 | { | |
1042 | snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); | |
1043 | return buf; | |
1044 | } | |
1045 | ||
1046 | static int cmp_string(const void *a, const void *b) | |
1047 | { | |
1048 | const char * const *as = a; | |
1049 | const char * const *bs = b; | |
1050 | return strcmp(*as, *bs); | |
1051 | } | |
1052 | ||
1053 | void print_pmu_events(const char *event_glob, bool name_only) | |
1054 | { | |
1055 | struct perf_pmu *pmu; | |
1056 | struct perf_pmu_alias *alias; | |
1057 | char buf[1024]; | |
1058 | int printed = 0; | |
1059 | int len, j; | |
1060 | char **aliases; | |
1061 | ||
1062 | pmu = NULL; | |
1063 | len = 0; | |
42634bc7 | 1064 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
dc098b35 AK |
1065 | list_for_each_entry(alias, &pmu->aliases, list) |
1066 | len++; | |
42634bc7 AH |
1067 | if (pmu->selectable) |
1068 | len++; | |
1069 | } | |
7e4772dc | 1070 | aliases = zalloc(sizeof(char *) * len); |
dc098b35 | 1071 | if (!aliases) |
7e4772dc | 1072 | goto out_enomem; |
dc098b35 AK |
1073 | pmu = NULL; |
1074 | j = 0; | |
42634bc7 | 1075 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
dc098b35 AK |
1076 | list_for_each_entry(alias, &pmu->aliases, list) { |
1077 | char *name = format_alias(buf, sizeof(buf), pmu, alias); | |
1078 | bool is_cpu = !strcmp(pmu->name, "cpu"); | |
1079 | ||
1080 | if (event_glob != NULL && | |
1081 | !(strglobmatch(name, event_glob) || | |
1082 | (!is_cpu && strglobmatch(alias->name, | |
1083 | event_glob)))) | |
1084 | continue; | |
7e4772dc | 1085 | |
dc098b35 | 1086 | if (is_cpu && !name_only) |
7e4772dc ACM |
1087 | name = format_alias_or(buf, sizeof(buf), pmu, alias); |
1088 | ||
1089 | aliases[j] = strdup(name); | |
1090 | if (aliases[j] == NULL) | |
1091 | goto out_enomem; | |
dc098b35 AK |
1092 | j++; |
1093 | } | |
fa52ceab ACM |
1094 | if (pmu->selectable && |
1095 | (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { | |
7e4772dc ACM |
1096 | char *s; |
1097 | if (asprintf(&s, "%s//", pmu->name) < 0) | |
1098 | goto out_enomem; | |
1099 | aliases[j] = s; | |
42634bc7 AH |
1100 | j++; |
1101 | } | |
1102 | } | |
dc098b35 AK |
1103 | len = j; |
1104 | qsort(aliases, len, sizeof(char *), cmp_string); | |
1105 | for (j = 0; j < len; j++) { | |
1106 | if (name_only) { | |
1107 | printf("%s ", aliases[j]); | |
1108 | continue; | |
1109 | } | |
1110 | printf(" %-50s [Kernel PMU event]\n", aliases[j]); | |
dc098b35 AK |
1111 | printed++; |
1112 | } | |
dfc431cb | 1113 | if (printed && pager_in_use()) |
dc098b35 | 1114 | printf("\n"); |
7e4772dc ACM |
1115 | out_free: |
1116 | for (j = 0; j < len; j++) | |
1117 | zfree(&aliases[j]); | |
1118 | zfree(&aliases); | |
1119 | return; | |
1120 | ||
1121 | out_enomem: | |
1122 | printf("FATAL: not enough memory to print PMU events\n"); | |
1123 | if (aliases) | |
1124 | goto out_free; | |
dc098b35 | 1125 | } |
4cabc3d1 AK |
1126 | |
1127 | bool pmu_have_event(const char *pname, const char *name) | |
1128 | { | |
1129 | struct perf_pmu *pmu; | |
1130 | struct perf_pmu_alias *alias; | |
1131 | ||
1132 | pmu = NULL; | |
1133 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { | |
1134 | if (strcmp(pname, pmu->name)) | |
1135 | continue; | |
1136 | list_for_each_entry(alias, &pmu->aliases, list) | |
1137 | if (!strcmp(alias->name, name)) | |
1138 | return true; | |
1139 | } | |
1140 | return false; | |
1141 | } | |
7d4bdab5 AH |
1142 | |
1143 | static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) | |
1144 | { | |
1145 | struct stat st; | |
1146 | char path[PATH_MAX]; | |
1147 | const char *sysfs; | |
1148 | ||
1149 | sysfs = sysfs__mountpoint(); | |
1150 | if (!sysfs) | |
1151 | return NULL; | |
1152 | ||
1153 | snprintf(path, PATH_MAX, | |
1154 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); | |
1155 | ||
1156 | if (stat(path, &st) < 0) | |
1157 | return NULL; | |
1158 | ||
1159 | return fopen(path, "r"); | |
1160 | } | |
1161 | ||
1162 | int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, | |
1163 | ...) | |
1164 | { | |
1165 | va_list args; | |
1166 | FILE *file; | |
1167 | int ret = EOF; | |
1168 | ||
1169 | va_start(args, fmt); | |
1170 | file = perf_pmu__open_file(pmu, name); | |
1171 | if (file) { | |
1172 | ret = vfscanf(file, fmt, args); | |
1173 | fclose(file); | |
1174 | } | |
1175 | va_end(args); | |
1176 | return ret; | |
1177 | } |