treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / perf / util / pmu.c
blob8b99fd312aae642ed3ca9a422ca3bef8473e47ad
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <subcmd/pager.h>
7 #include <sys/types.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdbool.h>
14 #include <stdarg.h>
15 #include <dirent.h>
16 #include <api/fs/fs.h>
17 #include <locale.h>
18 #include <regex.h>
19 #include <perf/cpumap.h>
20 #include "debug.h"
21 #include "pmu.h"
22 #include "parse-events.h"
23 #include "header.h"
24 #include "pmu-events/pmu-events.h"
25 #include "string2.h"
26 #include "strbuf.h"
27 #include "fncache.h"
29 struct perf_pmu_format {
30 char *name;
31 int value;
32 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
33 struct list_head list;
36 int perf_pmu_parse(struct list_head *list, char *name);
37 extern FILE *perf_pmu_in;
39 static LIST_HEAD(pmus);
42 * Parse & process all the sysfs attributes located under
43 * the directory specified in 'dir' parameter.
45 int perf_pmu__format_parse(char *dir, struct list_head *head)
47 struct dirent *evt_ent;
48 DIR *format_dir;
49 int ret = 0;
51 format_dir = opendir(dir);
52 if (!format_dir)
53 return -EINVAL;
55 while (!ret && (evt_ent = readdir(format_dir))) {
56 char path[PATH_MAX];
57 char *name = evt_ent->d_name;
58 FILE *file;
60 if (!strcmp(name, ".") || !strcmp(name, ".."))
61 continue;
63 snprintf(path, PATH_MAX, "%s/%s", dir, name);
65 ret = -EINVAL;
66 file = fopen(path, "r");
67 if (!file)
68 break;
70 perf_pmu_in = file;
71 ret = perf_pmu_parse(head, name);
72 fclose(file);
75 closedir(format_dir);
76 return ret;
80 * Reading/parsing the default pmu format definition, which should be
81 * located at:
82 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
84 static int pmu_format(const char *name, struct list_head *format)
86 char path[PATH_MAX];
87 const char *sysfs = sysfs__mountpoint();
89 if (!sysfs)
90 return -1;
92 snprintf(path, PATH_MAX,
93 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
95 if (!file_available(path))
96 return 0;
98 if (perf_pmu__format_parse(path, format))
99 return -1;
101 return 0;
104 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
106 char *lc;
107 int ret = 0;
110 * save current locale
112 lc = setlocale(LC_NUMERIC, NULL);
115 * The lc string may be allocated in static storage,
116 * so get a dynamic copy to make it survive setlocale
117 * call below.
119 lc = strdup(lc);
120 if (!lc) {
121 ret = -ENOMEM;
122 goto out;
126 * force to C locale to ensure kernel
127 * scale string is converted correctly.
128 * kernel uses default C locale.
130 setlocale(LC_NUMERIC, "C");
132 *sval = strtod(scale, end);
134 out:
135 /* restore locale */
136 setlocale(LC_NUMERIC, lc);
137 free(lc);
138 return ret;
141 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
143 struct stat st;
144 ssize_t sret;
145 char scale[128];
146 int fd, ret = -1;
147 char path[PATH_MAX];
149 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
151 fd = open(path, O_RDONLY);
152 if (fd == -1)
153 return -1;
155 if (fstat(fd, &st) < 0)
156 goto error;
158 sret = read(fd, scale, sizeof(scale)-1);
159 if (sret < 0)
160 goto error;
162 if (scale[sret - 1] == '\n')
163 scale[sret - 1] = '\0';
164 else
165 scale[sret] = '\0';
167 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
168 error:
169 close(fd);
170 return ret;
173 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
175 char path[PATH_MAX];
176 ssize_t sret;
177 int fd;
179 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
181 fd = open(path, O_RDONLY);
182 if (fd == -1)
183 return -1;
185 sret = read(fd, alias->unit, UNIT_MAX_LEN);
186 if (sret < 0)
187 goto error;
189 close(fd);
191 if (alias->unit[sret - 1] == '\n')
192 alias->unit[sret - 1] = '\0';
193 else
194 alias->unit[sret] = '\0';
196 return 0;
197 error:
198 close(fd);
199 alias->unit[0] = '\0';
200 return -1;
203 static int
204 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
206 char path[PATH_MAX];
207 int fd;
209 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
211 fd = open(path, O_RDONLY);
212 if (fd == -1)
213 return -1;
215 close(fd);
217 alias->per_pkg = true;
218 return 0;
221 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
222 char *dir, char *name)
224 char path[PATH_MAX];
225 int fd;
227 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
229 fd = open(path, O_RDONLY);
230 if (fd == -1)
231 return -1;
233 alias->snapshot = true;
234 close(fd);
235 return 0;
238 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
239 char **new_str)
241 if (!*old_str)
242 goto set_new;
244 if (*new_str) { /* Have new string, check with old */
245 if (strcasecmp(*old_str, *new_str))
246 pr_debug("alias %s differs in field '%s'\n",
247 name, field);
248 zfree(old_str);
249 } else /* Nothing new --> keep old string */
250 return;
251 set_new:
252 *old_str = *new_str;
253 *new_str = NULL;
256 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
257 struct perf_pmu_alias *newalias)
259 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
260 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
261 &newalias->long_desc);
262 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
263 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
264 &newalias->metric_expr);
265 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
266 &newalias->metric_name);
267 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
268 old->scale = newalias->scale;
269 old->per_pkg = newalias->per_pkg;
270 old->snapshot = newalias->snapshot;
271 memcpy(old->unit, newalias->unit, sizeof(old->unit));
274 /* Delete an alias entry. */
275 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
277 zfree(&newalias->name);
278 zfree(&newalias->desc);
279 zfree(&newalias->long_desc);
280 zfree(&newalias->topic);
281 zfree(&newalias->str);
282 zfree(&newalias->metric_expr);
283 zfree(&newalias->metric_name);
284 parse_events_terms__purge(&newalias->terms);
285 free(newalias);
288 /* Merge an alias, search in alias list. If this name is already
289 * present merge both of them to combine all information.
291 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
292 struct list_head *alist)
294 struct perf_pmu_alias *a;
296 list_for_each_entry(a, alist, list) {
297 if (!strcasecmp(newalias->name, a->name)) {
298 perf_pmu_update_alias(a, newalias);
299 perf_pmu_free_alias(newalias);
300 return true;
303 return false;
306 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
307 char *desc, char *val,
308 char *long_desc, char *topic,
309 char *unit, char *perpkg,
310 char *metric_expr,
311 char *metric_name,
312 char *deprecated)
314 struct parse_events_term *term;
315 struct perf_pmu_alias *alias;
316 int ret;
317 int num;
318 char newval[256];
320 alias = malloc(sizeof(*alias));
321 if (!alias)
322 return -ENOMEM;
324 INIT_LIST_HEAD(&alias->terms);
325 alias->scale = 1.0;
326 alias->unit[0] = '\0';
327 alias->per_pkg = false;
328 alias->snapshot = false;
329 alias->deprecated = false;
331 ret = parse_events_terms(&alias->terms, val);
332 if (ret) {
333 pr_err("Cannot parse alias %s: %d\n", val, ret);
334 free(alias);
335 return ret;
338 /* Scan event and remove leading zeroes, spaces, newlines, some
339 * platforms have terms specified as
340 * event=0x0091 (read from files ../<PMU>/events/<FILE>
341 * and terms specified as event=0x91 (read from JSON files).
343 * Rebuild string to make alias->str member comparable.
345 memset(newval, 0, sizeof(newval));
346 ret = 0;
347 list_for_each_entry(term, &alias->terms, list) {
348 if (ret)
349 ret += scnprintf(newval + ret, sizeof(newval) - ret,
350 ",");
351 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
352 ret += scnprintf(newval + ret, sizeof(newval) - ret,
353 "%s=%#x", term->config, term->val.num);
354 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
355 ret += scnprintf(newval + ret, sizeof(newval) - ret,
356 "%s=%s", term->config, term->val.str);
359 alias->name = strdup(name);
360 if (dir) {
362 * load unit name and scale if available
364 perf_pmu__parse_unit(alias, dir, name);
365 perf_pmu__parse_scale(alias, dir, name);
366 perf_pmu__parse_per_pkg(alias, dir, name);
367 perf_pmu__parse_snapshot(alias, dir, name);
370 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
371 alias->metric_name = metric_name ? strdup(metric_name): NULL;
372 alias->desc = desc ? strdup(desc) : NULL;
373 alias->long_desc = long_desc ? strdup(long_desc) :
374 desc ? strdup(desc) : NULL;
375 alias->topic = topic ? strdup(topic) : NULL;
376 if (unit) {
377 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
378 return -1;
379 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
381 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
382 alias->str = strdup(newval);
384 if (deprecated)
385 alias->deprecated = true;
387 if (!perf_pmu_merge_alias(alias, list))
388 list_add_tail(&alias->list, list);
390 return 0;
393 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
395 char buf[256];
396 int ret;
398 ret = fread(buf, 1, sizeof(buf), file);
399 if (ret == 0)
400 return -EINVAL;
402 buf[ret] = 0;
404 /* Remove trailing newline from sysfs file */
405 strim(buf);
407 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
408 NULL, NULL, NULL, NULL);
411 static inline bool pmu_alias_info_file(char *name)
413 size_t len;
415 len = strlen(name);
416 if (len > 5 && !strcmp(name + len - 5, ".unit"))
417 return true;
418 if (len > 6 && !strcmp(name + len - 6, ".scale"))
419 return true;
420 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
421 return true;
422 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
423 return true;
425 return false;
429 * Process all the sysfs attributes located under the directory
430 * specified in 'dir' parameter.
432 static int pmu_aliases_parse(char *dir, struct list_head *head)
434 struct dirent *evt_ent;
435 DIR *event_dir;
437 event_dir = opendir(dir);
438 if (!event_dir)
439 return -EINVAL;
441 while ((evt_ent = readdir(event_dir))) {
442 char path[PATH_MAX];
443 char *name = evt_ent->d_name;
444 FILE *file;
446 if (!strcmp(name, ".") || !strcmp(name, ".."))
447 continue;
450 * skip info files parsed in perf_pmu__new_alias()
452 if (pmu_alias_info_file(name))
453 continue;
455 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
457 file = fopen(path, "r");
458 if (!file) {
459 pr_debug("Cannot open %s\n", path);
460 continue;
463 if (perf_pmu__new_alias(head, dir, name, file) < 0)
464 pr_debug("Cannot set up %s\n", name);
465 fclose(file);
468 closedir(event_dir);
469 return 0;
473 * Reading the pmu event aliases definition, which should be located at:
474 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
476 static int pmu_aliases(const char *name, struct list_head *head)
478 char path[PATH_MAX];
479 const char *sysfs = sysfs__mountpoint();
481 if (!sysfs)
482 return -1;
484 snprintf(path, PATH_MAX,
485 "%s/bus/event_source/devices/%s/events", sysfs, name);
487 if (!file_available(path))
488 return 0;
490 if (pmu_aliases_parse(path, head))
491 return -1;
493 return 0;
496 static int pmu_alias_terms(struct perf_pmu_alias *alias,
497 struct list_head *terms)
499 struct parse_events_term *term, *cloned;
500 LIST_HEAD(list);
501 int ret;
503 list_for_each_entry(term, &alias->terms, list) {
504 ret = parse_events_term__clone(&cloned, term);
505 if (ret) {
506 parse_events_terms__purge(&list);
507 return ret;
510 * Weak terms don't override command line options,
511 * which we don't want for implicit terms in aliases.
513 cloned->weak = true;
514 list_add_tail(&cloned->list, &list);
516 list_splice(&list, terms);
517 return 0;
521 * Reading/parsing the default pmu type value, which should be
522 * located at:
523 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
525 static int pmu_type(const char *name, __u32 *type)
527 char path[PATH_MAX];
528 FILE *file;
529 int ret = 0;
530 const char *sysfs = sysfs__mountpoint();
532 if (!sysfs)
533 return -1;
535 snprintf(path, PATH_MAX,
536 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
538 if (access(path, R_OK) < 0)
539 return -1;
541 file = fopen(path, "r");
542 if (!file)
543 return -EINVAL;
545 if (1 != fscanf(file, "%u", type))
546 ret = -1;
548 fclose(file);
549 return ret;
552 /* Add all pmus in sysfs to pmu list: */
553 static void pmu_read_sysfs(void)
555 char path[PATH_MAX];
556 DIR *dir;
557 struct dirent *dent;
558 const char *sysfs = sysfs__mountpoint();
560 if (!sysfs)
561 return;
563 snprintf(path, PATH_MAX,
564 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
566 dir = opendir(path);
567 if (!dir)
568 return;
570 while ((dent = readdir(dir))) {
571 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
572 continue;
573 /* add to static LIST_HEAD(pmus): */
574 perf_pmu__find(dent->d_name);
577 closedir(dir);
580 static struct perf_cpu_map *__pmu_cpumask(const char *path)
582 FILE *file;
583 struct perf_cpu_map *cpus;
585 file = fopen(path, "r");
586 if (!file)
587 return NULL;
589 cpus = perf_cpu_map__read(file);
590 fclose(file);
591 return cpus;
595 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
596 * may have a "cpus" file.
598 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
599 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
601 static struct perf_cpu_map *pmu_cpumask(const char *name)
603 char path[PATH_MAX];
604 struct perf_cpu_map *cpus;
605 const char *sysfs = sysfs__mountpoint();
606 const char *templates[] = {
607 CPUS_TEMPLATE_UNCORE,
608 CPUS_TEMPLATE_CPU,
609 NULL
611 const char **template;
613 if (!sysfs)
614 return NULL;
616 for (template = templates; *template; template++) {
617 snprintf(path, PATH_MAX, *template, sysfs, name);
618 cpus = __pmu_cpumask(path);
619 if (cpus)
620 return cpus;
623 return NULL;
626 static bool pmu_is_uncore(const char *name)
628 char path[PATH_MAX];
629 const char *sysfs;
631 sysfs = sysfs__mountpoint();
632 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
633 return file_available(path);
637 * PMU CORE devices have different name other than cpu in sysfs on some
638 * platforms.
639 * Looking for possible sysfs files to identify the arm core device.
641 static int is_arm_pmu_core(const char *name)
643 char path[PATH_MAX];
644 const char *sysfs = sysfs__mountpoint();
646 if (!sysfs)
647 return 0;
649 /* Look for cpu sysfs (specific to arm) */
650 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
651 sysfs, name);
652 return file_available(path);
655 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
657 char *cpuid;
658 static bool printed;
660 cpuid = getenv("PERF_CPUID");
661 if (cpuid)
662 cpuid = strdup(cpuid);
663 if (!cpuid)
664 cpuid = get_cpuid_str(pmu);
665 if (!cpuid)
666 return NULL;
668 if (!printed) {
669 pr_debug("Using CPUID %s\n", cpuid);
670 printed = true;
672 return cpuid;
675 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
677 struct pmu_events_map *map;
678 char *cpuid = perf_pmu__getcpuid(pmu);
679 int i;
681 /* on some platforms which uses cpus map, cpuid can be NULL for
682 * PMUs other than CORE PMUs.
684 if (!cpuid)
685 return NULL;
687 i = 0;
688 for (;;) {
689 map = &pmu_events_map[i++];
690 if (!map->table) {
691 map = NULL;
692 break;
695 if (!strcmp_cpuid_str(map->cpuid, cpuid))
696 break;
698 free(cpuid);
699 return map;
702 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
704 char *tmp = NULL, *tok, *str;
705 bool res;
707 str = strdup(pmu_name);
708 if (!str)
709 return false;
712 * uncore alias may be from different PMU with common prefix
714 tok = strtok_r(str, ",", &tmp);
715 if (strncmp(pmu_name, tok, strlen(tok))) {
716 res = false;
717 goto out;
721 * Match more complex aliases where the alias name is a comma-delimited
722 * list of tokens, orderly contained in the matching PMU name.
724 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
725 * match "socket" in "socketX_pmunameY" and then "pmuname" in
726 * "pmunameY".
728 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
729 name = strstr(name, tok);
730 if (!name) {
731 res = false;
732 goto out;
736 res = true;
737 out:
738 free(str);
739 return res;
743 * From the pmu_events_map, find the table of PMU events that corresponds
744 * to the current running CPU. Then, add all PMU events from that table
745 * as aliases.
747 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
749 int i;
750 struct pmu_events_map *map;
751 const char *name = pmu->name;
753 map = perf_pmu__find_map(pmu);
754 if (!map)
755 return;
758 * Found a matching PMU events table. Create aliases
760 i = 0;
761 while (1) {
762 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
763 struct pmu_event *pe = &map->table[i++];
764 const char *pname = pe->pmu ? pe->pmu : cpu_name;
766 if (!pe->name) {
767 if (pe->metric_group || pe->metric_name)
768 continue;
769 break;
772 if (pmu_is_uncore(name) &&
773 pmu_uncore_alias_match(pname, name))
774 goto new_alias;
776 if (strcmp(pname, name))
777 continue;
779 new_alias:
780 /* need type casts to override 'const' */
781 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
782 (char *)pe->desc, (char *)pe->event,
783 (char *)pe->long_desc, (char *)pe->topic,
784 (char *)pe->unit, (char *)pe->perpkg,
785 (char *)pe->metric_expr,
786 (char *)pe->metric_name,
787 (char *)pe->deprecated);
791 struct perf_event_attr * __weak
792 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
794 return NULL;
797 static int pmu_max_precise(const char *name)
799 char path[PATH_MAX];
800 int max_precise = -1;
802 scnprintf(path, PATH_MAX,
803 "bus/event_source/devices/%s/caps/max_precise",
804 name);
806 sysfs__read_int(path, &max_precise);
807 return max_precise;
810 static struct perf_pmu *pmu_lookup(const char *name)
812 struct perf_pmu *pmu;
813 LIST_HEAD(format);
814 LIST_HEAD(aliases);
815 __u32 type;
818 * The pmu data we store & need consists of the pmu
819 * type value and format definitions. Load both right
820 * now.
822 if (pmu_format(name, &format))
823 return NULL;
826 * Check the type first to avoid unnecessary work.
828 if (pmu_type(name, &type))
829 return NULL;
831 if (pmu_aliases(name, &aliases))
832 return NULL;
834 pmu = zalloc(sizeof(*pmu));
835 if (!pmu)
836 return NULL;
838 pmu->cpus = pmu_cpumask(name);
839 pmu->name = strdup(name);
840 pmu->type = type;
841 pmu->is_uncore = pmu_is_uncore(name);
842 pmu->max_precise = pmu_max_precise(name);
843 pmu_add_cpu_aliases(&aliases, pmu);
845 INIT_LIST_HEAD(&pmu->format);
846 INIT_LIST_HEAD(&pmu->aliases);
847 list_splice(&format, &pmu->format);
848 list_splice(&aliases, &pmu->aliases);
849 list_add_tail(&pmu->list, &pmus);
851 pmu->default_config = perf_pmu__get_default_config(pmu);
853 return pmu;
856 static struct perf_pmu *pmu_find(const char *name)
858 struct perf_pmu *pmu;
860 list_for_each_entry(pmu, &pmus, list)
861 if (!strcmp(pmu->name, name))
862 return pmu;
864 return NULL;
867 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
870 * pmu iterator: If pmu is NULL, we start at the begin,
871 * otherwise return the next pmu. Returns NULL on end.
873 if (!pmu) {
874 pmu_read_sysfs();
875 pmu = list_prepare_entry(pmu, &pmus, list);
877 list_for_each_entry_continue(pmu, &pmus, list)
878 return pmu;
879 return NULL;
882 struct perf_pmu *perf_pmu__find(const char *name)
884 struct perf_pmu *pmu;
887 * Once PMU is loaded it stays in the list,
888 * so we keep us from multiple reading/parsing
889 * the pmu format definitions.
891 pmu = pmu_find(name);
892 if (pmu)
893 return pmu;
895 return pmu_lookup(name);
898 static struct perf_pmu_format *
899 pmu_find_format(struct list_head *formats, const char *name)
901 struct perf_pmu_format *format;
903 list_for_each_entry(format, formats, list)
904 if (!strcmp(format->name, name))
905 return format;
907 return NULL;
910 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
912 struct perf_pmu_format *format = pmu_find_format(formats, name);
913 __u64 bits = 0;
914 int fbit;
916 if (!format)
917 return 0;
919 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
920 bits |= 1ULL << fbit;
922 return bits;
925 int perf_pmu__format_type(struct list_head *formats, const char *name)
927 struct perf_pmu_format *format = pmu_find_format(formats, name);
929 if (!format)
930 return -1;
932 return format->value;
936 * Sets value based on the format definition (format parameter)
937 * and unformated value (value parameter).
939 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
940 bool zero)
942 unsigned long fbit, vbit;
944 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
946 if (!test_bit(fbit, format))
947 continue;
949 if (value & (1llu << vbit++))
950 *v |= (1llu << fbit);
951 else if (zero)
952 *v &= ~(1llu << fbit);
956 static __u64 pmu_format_max_value(const unsigned long *format)
958 int w;
960 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
961 if (!w)
962 return 0;
963 if (w < 64)
964 return (1ULL << w) - 1;
965 return -1;
969 * Term is a string term, and might be a param-term. Try to look up it's value
970 * in the remaining terms.
971 * - We have a term like "base-or-format-term=param-term",
972 * - We need to find the value supplied for "param-term" (with param-term named
973 * in a config string) later on in the term list.
975 static int pmu_resolve_param_term(struct parse_events_term *term,
976 struct list_head *head_terms,
977 __u64 *value)
979 struct parse_events_term *t;
981 list_for_each_entry(t, head_terms, list) {
982 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
983 if (!strcmp(t->config, term->config)) {
984 t->used = true;
985 *value = t->val.num;
986 return 0;
991 if (verbose > 0)
992 printf("Required parameter '%s' not specified\n", term->config);
994 return -1;
997 static char *pmu_formats_string(struct list_head *formats)
999 struct perf_pmu_format *format;
1000 char *str = NULL;
1001 struct strbuf buf = STRBUF_INIT;
1002 unsigned i = 0;
1004 if (!formats)
1005 return NULL;
1007 /* sysfs exported terms */
1008 list_for_each_entry(format, formats, list)
1009 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1010 goto error;
1012 str = strbuf_detach(&buf, NULL);
1013 error:
1014 strbuf_release(&buf);
1016 return str;
1020 * Setup one of config[12] attr members based on the
1021 * user input data - term parameter.
1023 static int pmu_config_term(struct list_head *formats,
1024 struct perf_event_attr *attr,
1025 struct parse_events_term *term,
1026 struct list_head *head_terms,
1027 bool zero, struct parse_events_error *err)
1029 struct perf_pmu_format *format;
1030 __u64 *vp;
1031 __u64 val, max_val;
1034 * If this is a parameter we've already used for parameterized-eval,
1035 * skip it in normal eval.
1037 if (term->used)
1038 return 0;
1041 * Hardcoded terms should be already in, so nothing
1042 * to be done for them.
1044 if (parse_events__is_hardcoded_term(term))
1045 return 0;
1047 format = pmu_find_format(formats, term->config);
1048 if (!format) {
1049 if (verbose > 0)
1050 printf("Invalid event/parameter '%s'\n", term->config);
1051 if (err) {
1052 char *pmu_term = pmu_formats_string(formats);
1054 parse_events__handle_error(err, term->err_term,
1055 strdup("unknown term"),
1056 parse_events_formats_error_string(pmu_term));
1057 free(pmu_term);
1059 return -EINVAL;
1062 switch (format->value) {
1063 case PERF_PMU_FORMAT_VALUE_CONFIG:
1064 vp = &attr->config;
1065 break;
1066 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1067 vp = &attr->config1;
1068 break;
1069 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1070 vp = &attr->config2;
1071 break;
1072 default:
1073 return -EINVAL;
1077 * Either directly use a numeric term, or try to translate string terms
1078 * using event parameters.
1080 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1081 if (term->no_value &&
1082 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1083 if (err) {
1084 parse_events__handle_error(err, term->err_val,
1085 strdup("no value assigned for term"),
1086 NULL);
1088 return -EINVAL;
1091 val = term->val.num;
1092 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1093 if (strcmp(term->val.str, "?")) {
1094 if (verbose > 0) {
1095 pr_info("Invalid sysfs entry %s=%s\n",
1096 term->config, term->val.str);
1098 if (err) {
1099 parse_events__handle_error(err, term->err_val,
1100 strdup("expected numeric value"),
1101 NULL);
1103 return -EINVAL;
1106 if (pmu_resolve_param_term(term, head_terms, &val))
1107 return -EINVAL;
1108 } else
1109 return -EINVAL;
1111 max_val = pmu_format_max_value(format->bits);
1112 if (val > max_val) {
1113 if (err) {
1114 char *err_str;
1116 parse_events__handle_error(err, term->err_val,
1117 asprintf(&err_str,
1118 "value too big for format, maximum is %llu",
1119 (unsigned long long)max_val) < 0
1120 ? strdup("value too big for format")
1121 : err_str,
1122 NULL);
1123 return -EINVAL;
1126 * Assume we don't care if !err, in which case the value will be
1127 * silently truncated.
1131 pmu_format_value(format->bits, val, vp, zero);
1132 return 0;
1135 int perf_pmu__config_terms(struct list_head *formats,
1136 struct perf_event_attr *attr,
1137 struct list_head *head_terms,
1138 bool zero, struct parse_events_error *err)
1140 struct parse_events_term *term;
1142 list_for_each_entry(term, head_terms, list) {
1143 if (pmu_config_term(formats, attr, term, head_terms,
1144 zero, err))
1145 return -EINVAL;
1148 return 0;
1152 * Configures event's 'attr' parameter based on the:
1153 * 1) users input - specified in terms parameter
1154 * 2) pmu format definitions - specified by pmu parameter
1156 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1157 struct list_head *head_terms,
1158 struct parse_events_error *err)
1160 bool zero = !!pmu->default_config;
1162 attr->type = pmu->type;
1163 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1164 zero, err);
1167 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1168 struct parse_events_term *term)
1170 struct perf_pmu_alias *alias;
1171 char *name;
1173 if (parse_events__is_hardcoded_term(term))
1174 return NULL;
1176 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1177 if (term->val.num != 1)
1178 return NULL;
1179 if (pmu_find_format(&pmu->format, term->config))
1180 return NULL;
1181 name = term->config;
1182 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1183 if (strcasecmp(term->config, "event"))
1184 return NULL;
1185 name = term->val.str;
1186 } else {
1187 return NULL;
1190 list_for_each_entry(alias, &pmu->aliases, list) {
1191 if (!strcasecmp(alias->name, name))
1192 return alias;
1194 return NULL;
1198 static int check_info_data(struct perf_pmu_alias *alias,
1199 struct perf_pmu_info *info)
1202 * Only one term in event definition can
1203 * define unit, scale and snapshot, fail
1204 * if there's more than one.
1206 if ((info->unit && alias->unit[0]) ||
1207 (info->scale && alias->scale) ||
1208 (info->snapshot && alias->snapshot))
1209 return -EINVAL;
1211 if (alias->unit[0])
1212 info->unit = alias->unit;
1214 if (alias->scale)
1215 info->scale = alias->scale;
1217 if (alias->snapshot)
1218 info->snapshot = alias->snapshot;
1220 return 0;
1224 * Find alias in the terms list and replace it with the terms
1225 * defined for the alias
1227 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1228 struct perf_pmu_info *info)
1230 struct parse_events_term *term, *h;
1231 struct perf_pmu_alias *alias;
1232 int ret;
1234 info->per_pkg = false;
1237 * Mark unit and scale as not set
1238 * (different from default values, see below)
1240 info->unit = NULL;
1241 info->scale = 0.0;
1242 info->snapshot = false;
1243 info->metric_expr = NULL;
1244 info->metric_name = NULL;
1246 list_for_each_entry_safe(term, h, head_terms, list) {
1247 alias = pmu_find_alias(pmu, term);
1248 if (!alias)
1249 continue;
1250 ret = pmu_alias_terms(alias, &term->list);
1251 if (ret)
1252 return ret;
1254 ret = check_info_data(alias, info);
1255 if (ret)
1256 return ret;
1258 if (alias->per_pkg)
1259 info->per_pkg = true;
1260 info->metric_expr = alias->metric_expr;
1261 info->metric_name = alias->metric_name;
1263 list_del_init(&term->list);
1264 parse_events_term__delete(term);
1268 * if no unit or scale foundin aliases, then
1269 * set defaults as for evsel
1270 * unit cannot left to NULL
1272 if (info->unit == NULL)
1273 info->unit = "";
1275 if (info->scale == 0.0)
1276 info->scale = 1.0;
1278 return 0;
1281 int perf_pmu__new_format(struct list_head *list, char *name,
1282 int config, unsigned long *bits)
1284 struct perf_pmu_format *format;
1286 format = zalloc(sizeof(*format));
1287 if (!format)
1288 return -ENOMEM;
1290 format->name = strdup(name);
1291 format->value = config;
1292 memcpy(format->bits, bits, sizeof(format->bits));
1294 list_add_tail(&format->list, list);
1295 return 0;
1298 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1300 long b;
1302 if (!to)
1303 to = from;
1305 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1306 for (b = from; b <= to; b++)
1307 set_bit(b, bits);
1310 static int sub_non_neg(int a, int b)
1312 if (b > a)
1313 return 0;
1314 return a - b;
1317 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1318 struct perf_pmu_alias *alias)
1320 struct parse_events_term *term;
1321 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1323 list_for_each_entry(term, &alias->terms, list) {
1324 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1325 used += snprintf(buf + used, sub_non_neg(len, used),
1326 ",%s=%s", term->config,
1327 term->val.str);
1330 if (sub_non_neg(len, used) > 0) {
1331 buf[used] = '/';
1332 used++;
1334 if (sub_non_neg(len, used) > 0) {
1335 buf[used] = '\0';
1336 used++;
1337 } else
1338 buf[len - 1] = '\0';
1340 return buf;
1343 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1344 struct perf_pmu_alias *alias)
1346 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1347 return buf;
1350 struct sevent {
1351 char *name;
1352 char *desc;
1353 char *topic;
1354 char *str;
1355 char *pmu;
1356 char *metric_expr;
1357 char *metric_name;
1360 static int cmp_sevent(const void *a, const void *b)
1362 const struct sevent *as = a;
1363 const struct sevent *bs = b;
1365 /* Put extra events last */
1366 if (!!as->desc != !!bs->desc)
1367 return !!as->desc - !!bs->desc;
1368 if (as->topic && bs->topic) {
1369 int n = strcmp(as->topic, bs->topic);
1371 if (n)
1372 return n;
1374 return strcmp(as->name, bs->name);
1377 static void wordwrap(char *s, int start, int max, int corr)
1379 int column = start;
1380 int n;
1382 while (*s) {
1383 int wlen = strcspn(s, " \t");
1385 if (column + wlen >= max && column > start) {
1386 printf("\n%*s", start, "");
1387 column = start + corr;
1389 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1390 if (n <= 0)
1391 break;
1392 s += wlen;
1393 column += n;
1394 s = skip_spaces(s);
1398 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1399 bool long_desc, bool details_flag, bool deprecated)
1401 struct perf_pmu *pmu;
1402 struct perf_pmu_alias *alias;
1403 char buf[1024];
1404 int printed = 0;
1405 int len, j;
1406 struct sevent *aliases;
1407 int numdesc = 0;
1408 int columns = pager_get_columns();
1409 char *topic = NULL;
1411 pmu = NULL;
1412 len = 0;
1413 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1414 list_for_each_entry(alias, &pmu->aliases, list)
1415 len++;
1416 if (pmu->selectable)
1417 len++;
1419 aliases = zalloc(sizeof(struct sevent) * len);
1420 if (!aliases)
1421 goto out_enomem;
1422 pmu = NULL;
1423 j = 0;
1424 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1425 list_for_each_entry(alias, &pmu->aliases, list) {
1426 char *name = alias->desc ? alias->name :
1427 format_alias(buf, sizeof(buf), pmu, alias);
1428 bool is_cpu = !strcmp(pmu->name, "cpu");
1430 if (alias->deprecated && !deprecated)
1431 continue;
1433 if (event_glob != NULL &&
1434 !(strglobmatch_nocase(name, event_glob) ||
1435 (!is_cpu && strglobmatch_nocase(alias->name,
1436 event_glob)) ||
1437 (alias->topic &&
1438 strglobmatch_nocase(alias->topic, event_glob))))
1439 continue;
1441 if (is_cpu && !name_only && !alias->desc)
1442 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1444 aliases[j].name = name;
1445 if (is_cpu && !name_only && !alias->desc)
1446 aliases[j].name = format_alias_or(buf,
1447 sizeof(buf),
1448 pmu, alias);
1449 aliases[j].name = strdup(aliases[j].name);
1450 if (!aliases[j].name)
1451 goto out_enomem;
1453 aliases[j].desc = long_desc ? alias->long_desc :
1454 alias->desc;
1455 aliases[j].topic = alias->topic;
1456 aliases[j].str = alias->str;
1457 aliases[j].pmu = pmu->name;
1458 aliases[j].metric_expr = alias->metric_expr;
1459 aliases[j].metric_name = alias->metric_name;
1460 j++;
1462 if (pmu->selectable &&
1463 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1464 char *s;
1465 if (asprintf(&s, "%s//", pmu->name) < 0)
1466 goto out_enomem;
1467 aliases[j].name = s;
1468 j++;
1471 len = j;
1472 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1473 for (j = 0; j < len; j++) {
1474 /* Skip duplicates */
1475 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1476 continue;
1477 if (name_only) {
1478 printf("%s ", aliases[j].name);
1479 continue;
1481 if (aliases[j].desc && !quiet_flag) {
1482 if (numdesc++ == 0)
1483 printf("\n");
1484 if (aliases[j].topic && (!topic ||
1485 strcmp(topic, aliases[j].topic))) {
1486 printf("%s%s:\n", topic ? "\n" : "",
1487 aliases[j].topic);
1488 topic = aliases[j].topic;
1490 printf(" %-50s\n", aliases[j].name);
1491 printf("%*s", 8, "[");
1492 wordwrap(aliases[j].desc, 8, columns, 0);
1493 printf("]\n");
1494 if (details_flag) {
1495 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1496 if (aliases[j].metric_name)
1497 printf(" MetricName: %s", aliases[j].metric_name);
1498 if (aliases[j].metric_expr)
1499 printf(" MetricExpr: %s", aliases[j].metric_expr);
1500 putchar('\n');
1502 } else
1503 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1504 printed++;
1506 if (printed && pager_in_use())
1507 printf("\n");
1508 out_free:
1509 for (j = 0; j < len; j++)
1510 zfree(&aliases[j].name);
1511 zfree(&aliases);
1512 return;
1514 out_enomem:
1515 printf("FATAL: not enough memory to print PMU events\n");
1516 if (aliases)
1517 goto out_free;
1520 bool pmu_have_event(const char *pname, const char *name)
1522 struct perf_pmu *pmu;
1523 struct perf_pmu_alias *alias;
1525 pmu = NULL;
1526 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1527 if (strcmp(pname, pmu->name))
1528 continue;
1529 list_for_each_entry(alias, &pmu->aliases, list)
1530 if (!strcmp(alias->name, name))
1531 return true;
1533 return false;
1536 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1538 char path[PATH_MAX];
1539 const char *sysfs;
1541 sysfs = sysfs__mountpoint();
1542 if (!sysfs)
1543 return NULL;
1545 snprintf(path, PATH_MAX,
1546 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1547 if (!file_available(path))
1548 return NULL;
1549 return fopen(path, "r");
1552 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1553 ...)
1555 va_list args;
1556 FILE *file;
1557 int ret = EOF;
1559 va_start(args, fmt);
1560 file = perf_pmu__open_file(pmu, name);
1561 if (file) {
1562 ret = vfscanf(file, fmt, args);
1563 fclose(file);
1565 va_end(args);
1566 return ret;