1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
4 #include <linux/zalloc.h>
11 #include <sys/param.h>
16 #include <subcmd/parse-options.h>
17 #include "parse-events.h"
18 #include <subcmd/exec-cmd.h>
24 #include "bpf-loader.h"
26 #include <api/fs/tracing_path.h>
27 #include <perf/cpumap.h>
28 #include "parse-events-bison.h"
29 #define YY_EXTRA_TYPE int
30 #include "parse-events-flex.h"
32 #include "thread_map.h"
34 #include "probe-file.h"
36 #include "util/parse-branch-options.h"
37 #include "metricgroup.h"
39 #define MAX_NAME_LEN 100
42 extern int parse_events_debug;
44 int parse_events_parse(void *parse_state, void *scanner);
45 static int get_config_terms(struct list_head *head_config,
46 struct list_head *head_terms __maybe_unused);
48 static struct perf_pmu_event_symbol *perf_pmu_events_list;
50 * The variable indicates the number of supported pmu event symbols.
51 * 0 means not initialized and ready to init
52 * -1 means failed to init, don't try anymore
53 * >0 is the number of supported pmu event symbols
55 static int perf_pmu_events_list_num;
57 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
58 [PERF_COUNT_HW_CPU_CYCLES] = {
59 .symbol = "cpu-cycles",
62 [PERF_COUNT_HW_INSTRUCTIONS] = {
63 .symbol = "instructions",
66 [PERF_COUNT_HW_CACHE_REFERENCES] = {
67 .symbol = "cache-references",
70 [PERF_COUNT_HW_CACHE_MISSES] = {
71 .symbol = "cache-misses",
74 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
75 .symbol = "branch-instructions",
78 [PERF_COUNT_HW_BRANCH_MISSES] = {
79 .symbol = "branch-misses",
82 [PERF_COUNT_HW_BUS_CYCLES] = {
83 .symbol = "bus-cycles",
86 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
87 .symbol = "stalled-cycles-frontend",
88 .alias = "idle-cycles-frontend",
90 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
91 .symbol = "stalled-cycles-backend",
92 .alias = "idle-cycles-backend",
94 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
95 .symbol = "ref-cycles",
100 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
101 [PERF_COUNT_SW_CPU_CLOCK] = {
102 .symbol = "cpu-clock",
105 [PERF_COUNT_SW_TASK_CLOCK] = {
106 .symbol = "task-clock",
109 [PERF_COUNT_SW_PAGE_FAULTS] = {
110 .symbol = "page-faults",
113 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
114 .symbol = "context-switches",
117 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
118 .symbol = "cpu-migrations",
119 .alias = "migrations",
121 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
122 .symbol = "minor-faults",
125 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
126 .symbol = "major-faults",
129 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
130 .symbol = "alignment-faults",
133 [PERF_COUNT_SW_EMULATION_FAULTS] = {
134 .symbol = "emulation-faults",
137 [PERF_COUNT_SW_DUMMY] = {
141 [PERF_COUNT_SW_BPF_OUTPUT] = {
142 .symbol = "bpf-output",
147 #define __PERF_EVENT_FIELD(config, name) \
148 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
150 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
151 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
152 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
153 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
155 #define for_each_subsystem(sys_dir, sys_dirent) \
156 while ((sys_dirent = readdir(sys_dir)) != NULL) \
157 if (sys_dirent->d_type == DT_DIR && \
158 (strcmp(sys_dirent->d_name, ".")) && \
159 (strcmp(sys_dirent->d_name, "..")))
161 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
163 char evt_path[MAXPATHLEN];
166 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
167 fd = open(evt_path, O_RDONLY);
175 #define for_each_event(dir_path, evt_dir, evt_dirent) \
176 while ((evt_dirent = readdir(evt_dir)) != NULL) \
177 if (evt_dirent->d_type == DT_DIR && \
178 (strcmp(evt_dirent->d_name, ".")) && \
179 (strcmp(evt_dirent->d_name, "..")) && \
180 (!tp_event_has_id(dir_path, evt_dirent)))
182 #define MAX_EVENT_LENGTH 512
185 struct tracepoint_path *tracepoint_id_to_path(u64 config)
187 struct tracepoint_path *path = NULL;
188 DIR *sys_dir, *evt_dir;
189 struct dirent *sys_dirent, *evt_dirent;
193 char evt_path[MAXPATHLEN];
196 sys_dir = tracing_events__opendir();
200 for_each_subsystem(sys_dir, sys_dirent) {
201 dir_path = get_events_file(sys_dirent->d_name);
204 evt_dir = opendir(dir_path);
208 for_each_event(dir_path, evt_dir, evt_dirent) {
210 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
212 fd = open(evt_path, O_RDONLY);
215 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
222 put_events_file(dir_path);
225 path = zalloc(sizeof(*path));
228 path->system = malloc(MAX_EVENT_LENGTH);
233 path->name = malloc(MAX_EVENT_LENGTH);
235 zfree(&path->system);
239 strncpy(path->system, sys_dirent->d_name,
241 strncpy(path->name, evt_dirent->d_name,
248 put_events_file(dir_path);
255 struct tracepoint_path *tracepoint_name_to_path(const char *name)
257 struct tracepoint_path *path = zalloc(sizeof(*path));
258 char *str = strchr(name, ':');
260 if (path == NULL || str == NULL) {
265 path->system = strndup(name, str - name);
266 path->name = strdup(str+1);
268 if (path->system == NULL || path->name == NULL) {
269 zfree(&path->system);
277 const char *event_type(int type)
280 case PERF_TYPE_HARDWARE:
283 case PERF_TYPE_SOFTWARE:
286 case PERF_TYPE_TRACEPOINT:
289 case PERF_TYPE_HW_CACHE:
290 return "hardware-cache";
299 static int parse_events__is_name_term(struct parse_events_term *term)
301 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
304 static char *get_config_name(struct list_head *head_terms)
306 struct parse_events_term *term;
311 list_for_each_entry(term, head_terms, list)
312 if (parse_events__is_name_term(term))
313 return term->val.str;
318 static struct evsel *
319 __add_event(struct list_head *list, int *idx,
320 struct perf_event_attr *attr,
321 char *name, struct perf_pmu *pmu,
322 struct list_head *config_terms, bool auto_merge_stats,
323 const char *cpu_list)
326 struct perf_cpu_map *cpus = pmu ? pmu->cpus :
327 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
329 event_attr_init(attr);
331 evsel = perf_evsel__new_idx(attr, *idx);
336 evsel->core.cpus = perf_cpu_map__get(cpus);
337 evsel->core.own_cpus = perf_cpu_map__get(cpus);
338 evsel->system_wide = pmu ? pmu->is_uncore : false;
339 evsel->auto_merge_stats = auto_merge_stats;
342 evsel->name = strdup(name);
345 list_splice(config_terms, &evsel->config_terms);
347 list_add_tail(&evsel->core.node, list);
351 static int add_event(struct list_head *list, int *idx,
352 struct perf_event_attr *attr, char *name,
353 struct list_head *config_terms)
355 return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
358 static int add_event_tool(struct list_head *list, int *idx,
359 enum perf_tool_event tool_event)
362 struct perf_event_attr attr = {
363 .type = PERF_TYPE_SOFTWARE,
364 .config = PERF_COUNT_SW_DUMMY,
367 evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
370 evsel->tool_event = tool_event;
371 if (tool_event == PERF_TOOL_DURATION_TIME)
372 evsel->unit = strdup("ns");
376 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
381 for (i = 0; i < size; i++) {
382 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
383 n = strlen(names[i][j]);
384 if (n > longest && !strncasecmp(str, names[i][j], n))
394 typedef int config_term_func_t(struct perf_event_attr *attr,
395 struct parse_events_term *term,
396 struct parse_events_error *err);
397 static int config_term_common(struct perf_event_attr *attr,
398 struct parse_events_term *term,
399 struct parse_events_error *err);
400 static int config_attr(struct perf_event_attr *attr,
401 struct list_head *head,
402 struct parse_events_error *err,
403 config_term_func_t config_term);
405 int parse_events_add_cache(struct list_head *list, int *idx,
406 char *type, char *op_result1, char *op_result2,
407 struct parse_events_error *err,
408 struct list_head *head_config)
410 struct perf_event_attr attr;
411 LIST_HEAD(config_terms);
412 char name[MAX_NAME_LEN], *config_name;
413 int cache_type = -1, cache_op = -1, cache_result = -1;
414 char *op_result[2] = { op_result1, op_result2 };
418 * No fallback - if we cannot get a clear cache type
421 cache_type = parse_aliases(type, perf_evsel__hw_cache,
422 PERF_COUNT_HW_CACHE_MAX);
423 if (cache_type == -1)
426 config_name = get_config_name(head_config);
427 n = snprintf(name, MAX_NAME_LEN, "%s", type);
429 for (i = 0; (i < 2) && (op_result[i]); i++) {
430 char *str = op_result[i];
432 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
434 if (cache_op == -1) {
435 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
436 PERF_COUNT_HW_CACHE_OP_MAX);
438 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
444 if (cache_result == -1) {
445 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
446 PERF_COUNT_HW_CACHE_RESULT_MAX);
447 if (cache_result >= 0)
453 * Fall back to reads:
456 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
459 * Fall back to accesses:
461 if (cache_result == -1)
462 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
464 memset(&attr, 0, sizeof(attr));
465 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
466 attr.type = PERF_TYPE_HW_CACHE;
469 if (config_attr(&attr, head_config, err,
473 if (get_config_terms(head_config, &config_terms))
476 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
479 static void tracepoint_error(struct parse_events_error *e, int err,
480 const char *sys, const char *name)
488 * We get error directly from syscall errno ( > 0),
489 * or from encoded pointer's error ( < 0).
495 e->str = strdup("can't access trace events");
498 e->str = strdup("unknown tracepoint");
501 e->str = strdup("failed to add tracepoint");
505 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
506 e->help = strdup(help);
509 static int add_tracepoint(struct list_head *list, int *idx,
510 const char *sys_name, const char *evt_name,
511 struct parse_events_error *err,
512 struct list_head *head_config)
516 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
518 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
519 return PTR_ERR(evsel);
523 LIST_HEAD(config_terms);
525 if (get_config_terms(head_config, &config_terms))
527 list_splice(&config_terms, &evsel->config_terms);
530 list_add_tail(&evsel->core.node, list);
534 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
535 const char *sys_name, const char *evt_name,
536 struct parse_events_error *err,
537 struct list_head *head_config)
540 struct dirent *evt_ent;
542 int ret = 0, found = 0;
544 evt_path = get_events_file(sys_name);
546 tracepoint_error(err, errno, sys_name, evt_name);
549 evt_dir = opendir(evt_path);
551 put_events_file(evt_path);
552 tracepoint_error(err, errno, sys_name, evt_name);
556 while (!ret && (evt_ent = readdir(evt_dir))) {
557 if (!strcmp(evt_ent->d_name, ".")
558 || !strcmp(evt_ent->d_name, "..")
559 || !strcmp(evt_ent->d_name, "enable")
560 || !strcmp(evt_ent->d_name, "filter"))
563 if (!strglobmatch(evt_ent->d_name, evt_name))
568 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
573 tracepoint_error(err, ENOENT, sys_name, evt_name);
577 put_events_file(evt_path);
582 static int add_tracepoint_event(struct list_head *list, int *idx,
583 const char *sys_name, const char *evt_name,
584 struct parse_events_error *err,
585 struct list_head *head_config)
587 return strpbrk(evt_name, "*?") ?
588 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
590 add_tracepoint(list, idx, sys_name, evt_name,
594 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
595 const char *sys_name, const char *evt_name,
596 struct parse_events_error *err,
597 struct list_head *head_config)
599 struct dirent *events_ent;
603 events_dir = tracing_events__opendir();
605 tracepoint_error(err, errno, sys_name, evt_name);
609 while (!ret && (events_ent = readdir(events_dir))) {
610 if (!strcmp(events_ent->d_name, ".")
611 || !strcmp(events_ent->d_name, "..")
612 || !strcmp(events_ent->d_name, "enable")
613 || !strcmp(events_ent->d_name, "header_event")
614 || !strcmp(events_ent->d_name, "header_page"))
617 if (!strglobmatch(events_ent->d_name, sys_name))
620 ret = add_tracepoint_event(list, idx, events_ent->d_name,
621 evt_name, err, head_config);
624 closedir(events_dir);
628 struct __add_bpf_event_param {
629 struct parse_events_state *parse_state;
630 struct list_head *list;
631 struct list_head *head_config;
634 static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
637 LIST_HEAD(new_evsels);
638 struct __add_bpf_event_param *param = _param;
639 struct parse_events_state *parse_state = param->parse_state;
640 struct list_head *list = param->list;
644 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
645 * then don't add the tracepoint, this will be used for something else, like
646 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
648 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
653 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
656 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
657 event, parse_state->error,
660 struct evsel *evsel, *tmp;
662 pr_debug("Failed to add BPF event %s:%s\n",
664 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
665 list_del_init(&evsel->core.node);
666 evsel__delete(evsel);
670 pr_debug("adding %s:%s\n", group, event);
672 list_for_each_entry(pos, &new_evsels, core.node) {
673 pr_debug("adding %s:%s to %p\n",
678 list_splice(&new_evsels, list);
682 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
683 struct list_head *list,
684 struct bpf_object *obj,
685 struct list_head *head_config)
689 struct __add_bpf_event_param param = {parse_state, list, head_config};
690 static bool registered_unprobe_atexit = false;
692 if (IS_ERR(obj) || !obj) {
693 snprintf(errbuf, sizeof(errbuf),
694 "Internal error: load bpf obj with NULL");
700 * Register atexit handler before calling bpf__probe() so
701 * bpf__probe() don't need to unprobe probe points its already
702 * created when failure.
704 if (!registered_unprobe_atexit) {
706 registered_unprobe_atexit = true;
709 err = bpf__probe(obj);
711 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
715 err = bpf__load(obj);
717 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
721 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
723 snprintf(errbuf, sizeof(errbuf),
724 "Attach events in BPF object failed");
730 parse_state->error->help = strdup("(add -v to see detail)");
731 parse_state->error->str = strdup(errbuf);
736 parse_events_config_bpf(struct parse_events_state *parse_state,
737 struct bpf_object *obj,
738 struct list_head *head_config)
740 struct parse_events_term *term;
743 if (!head_config || list_empty(head_config))
746 list_for_each_entry(term, head_config, list) {
750 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
751 snprintf(errbuf, sizeof(errbuf),
752 "Invalid config term for BPF object");
753 errbuf[BUFSIZ - 1] = '\0';
755 parse_state->error->idx = term->err_term;
756 parse_state->error->str = strdup(errbuf);
760 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
762 bpf__strerror_config_obj(obj, term, parse_state->evlist,
763 &error_pos, err, errbuf,
765 parse_state->error->help = strdup(
766 "Hint:\tValid config terms:\n"
767 " \tmap:[<arraymap>].value<indices>=[value]\n"
768 " \tmap:[<eventmap>].event<indices>=[event]\n"
770 " \twhere <indices> is something like [0,3...5] or [all]\n"
771 " \t(add -v to see detail)");
772 parse_state->error->str = strdup(errbuf);
773 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
774 parse_state->error->idx = term->err_val;
776 parse_state->error->idx = term->err_term + error_pos;
784 * Split config terms:
785 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
786 * 'call-graph=fp' is 'evt config', should be applied to each
788 * 'map:array.value[0]=1' is 'obj config', should be processed
789 * with parse_events_config_bpf.
791 * Move object config terms from the first list to obj_head_config.
794 split_bpf_config_terms(struct list_head *evt_head_config,
795 struct list_head *obj_head_config)
797 struct parse_events_term *term, *temp;
800 * Currectly, all possible user config term
801 * belong to bpf object. parse_events__is_hardcoded_term()
802 * happends to be a good flag.
804 * See parse_events_config_bpf() and
805 * config_term_tracepoint().
807 list_for_each_entry_safe(term, temp, evt_head_config, list)
808 if (!parse_events__is_hardcoded_term(term))
809 list_move_tail(&term->list, obj_head_config);
812 int parse_events_load_bpf(struct parse_events_state *parse_state,
813 struct list_head *list,
816 struct list_head *head_config)
819 struct bpf_object *obj;
820 LIST_HEAD(obj_head_config);
823 split_bpf_config_terms(head_config, &obj_head_config);
825 obj = bpf__prepare_load(bpf_file_name, source);
832 snprintf(errbuf, sizeof(errbuf),
833 "BPF support is not compiled");
835 bpf__strerror_prepare_load(bpf_file_name,
840 parse_state->error->help = strdup("(add -v to see detail)");
841 parse_state->error->str = strdup(errbuf);
845 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
848 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
851 * Caller doesn't know anything about obj_head_config,
852 * so combine them together again before returnning.
855 list_splice_tail(&obj_head_config, head_config);
860 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
864 for (i = 0; i < 3; i++) {
865 if (!type || !type[i])
868 #define CHECK_SET_TYPE(bit) \
870 if (attr->bp_type & bit) \
873 attr->bp_type |= bit; \
878 CHECK_SET_TYPE(HW_BREAKPOINT_R);
881 CHECK_SET_TYPE(HW_BREAKPOINT_W);
884 CHECK_SET_TYPE(HW_BREAKPOINT_X);
891 #undef CHECK_SET_TYPE
893 if (!attr->bp_type) /* Default */
894 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
899 int parse_events_add_breakpoint(struct list_head *list, int *idx,
900 void *ptr, char *type, u64 len)
902 struct perf_event_attr attr;
904 memset(&attr, 0, sizeof(attr));
905 attr.bp_addr = (unsigned long) ptr;
907 if (parse_breakpoint_type(type, &attr))
910 /* Provide some defaults if len is not specified */
912 if (attr.bp_type == HW_BREAKPOINT_X)
915 len = HW_BREAKPOINT_LEN_4;
920 attr.type = PERF_TYPE_BREAKPOINT;
921 attr.sample_period = 1;
923 return add_event(list, idx, &attr, NULL, NULL);
926 static int check_type_val(struct parse_events_term *term,
927 struct parse_events_error *err,
930 if (type == term->type_val)
934 err->idx = term->err_val;
935 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
936 err->str = strdup("expected numeric value");
938 err->str = strdup("expected string value");
944 * Update according to parse-events.l
946 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
947 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
948 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
949 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
950 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
951 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
952 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
953 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
954 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
955 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
956 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
957 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
958 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
959 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
960 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
961 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
962 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
963 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
964 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
965 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
968 static bool config_term_shrinked;
971 config_term_avail(int term_type, struct parse_events_error *err)
973 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
974 err->str = strdup("Invalid term_type");
977 if (!config_term_shrinked)
981 case PARSE_EVENTS__TERM_TYPE_CONFIG:
982 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
983 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
984 case PARSE_EVENTS__TERM_TYPE_NAME:
985 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
986 case PARSE_EVENTS__TERM_TYPE_PERCORE:
992 /* term_type is validated so indexing is safe */
993 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
994 config_term_names[term_type]) < 0)
1000 void parse_events__shrink_config_terms(void)
1002 config_term_shrinked = true;
1005 static int config_term_common(struct perf_event_attr *attr,
1006 struct parse_events_term *term,
1007 struct parse_events_error *err)
1009 #define CHECK_TYPE_VAL(type) \
1011 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1015 switch (term->type_term) {
1016 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1017 CHECK_TYPE_VAL(NUM);
1018 attr->config = term->val.num;
1020 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1021 CHECK_TYPE_VAL(NUM);
1022 attr->config1 = term->val.num;
1024 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1025 CHECK_TYPE_VAL(NUM);
1026 attr->config2 = term->val.num;
1028 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1029 CHECK_TYPE_VAL(NUM);
1031 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1032 CHECK_TYPE_VAL(NUM);
1034 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1035 CHECK_TYPE_VAL(STR);
1036 if (strcmp(term->val.str, "no") &&
1037 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1038 err->str = strdup("invalid branch sample type");
1039 err->idx = term->err_val;
1043 case PARSE_EVENTS__TERM_TYPE_TIME:
1044 CHECK_TYPE_VAL(NUM);
1045 if (term->val.num > 1) {
1046 err->str = strdup("expected 0 or 1");
1047 err->idx = term->err_val;
1051 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1052 CHECK_TYPE_VAL(STR);
1054 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1055 CHECK_TYPE_VAL(NUM);
1057 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1058 CHECK_TYPE_VAL(NUM);
1060 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1061 CHECK_TYPE_VAL(NUM);
1063 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1064 CHECK_TYPE_VAL(NUM);
1066 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1067 CHECK_TYPE_VAL(NUM);
1069 case PARSE_EVENTS__TERM_TYPE_NAME:
1070 CHECK_TYPE_VAL(STR);
1072 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1073 CHECK_TYPE_VAL(NUM);
1075 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1076 CHECK_TYPE_VAL(NUM);
1078 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1079 CHECK_TYPE_VAL(NUM);
1080 if ((unsigned int)term->val.num > 1) {
1081 err->str = strdup("expected 0 or 1");
1082 err->idx = term->err_val;
1087 err->str = strdup("unknown term");
1088 err->idx = term->err_term;
1089 err->help = parse_events_formats_error_string(NULL);
1094 * Check term availbility after basic checking so
1095 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1097 * If check availbility at the entry of this function,
1098 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1099 * if an invalid config term is provided for legacy events
1100 * (for example, instructions/badterm/...), which is confusing.
1102 if (!config_term_avail(term->type_term, err))
1105 #undef CHECK_TYPE_VAL
1108 static int config_term_pmu(struct perf_event_attr *attr,
1109 struct parse_events_term *term,
1110 struct parse_events_error *err)
1112 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1113 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1115 * Always succeed for sysfs terms, as we dont know
1116 * at this point what type they need to have.
1120 return config_term_common(attr, term, err);
1123 static int config_term_tracepoint(struct perf_event_attr *attr,
1124 struct parse_events_term *term,
1125 struct parse_events_error *err)
1127 switch (term->type_term) {
1128 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1129 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1130 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1131 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1132 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1133 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1134 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1135 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1136 return config_term_common(attr, term, err);
1139 err->idx = term->err_term;
1140 err->str = strdup("unknown term");
1141 err->help = strdup("valid terms: call-graph,stack-size\n");
1149 static int config_attr(struct perf_event_attr *attr,
1150 struct list_head *head,
1151 struct parse_events_error *err,
1152 config_term_func_t config_term)
1154 struct parse_events_term *term;
1156 list_for_each_entry(term, head, list)
1157 if (config_term(attr, term, err))
1163 static int get_config_terms(struct list_head *head_config,
1164 struct list_head *head_terms __maybe_unused)
1166 #define ADD_CONFIG_TERM(__type, __name, __val) \
1168 struct perf_evsel_config_term *__t; \
1170 __t = zalloc(sizeof(*__t)); \
1174 INIT_LIST_HEAD(&__t->list); \
1175 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1176 __t->val.__name = __val; \
1177 __t->weak = term->weak; \
1178 list_add_tail(&__t->list, head_terms); \
1181 struct parse_events_term *term;
1183 list_for_each_entry(term, head_config, list) {
1184 switch (term->type_term) {
1185 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1186 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
1188 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1189 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1191 case PARSE_EVENTS__TERM_TYPE_TIME:
1192 ADD_CONFIG_TERM(TIME, time, term->val.num);
1194 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1195 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1197 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1198 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1200 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1201 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1203 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1204 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1206 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1207 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1209 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1210 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1212 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1213 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1215 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1216 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1218 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1219 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1221 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1222 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1224 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1225 ADD_CONFIG_TERM(PERCORE, percore,
1226 term->val.num ? true : false);
1232 #undef ADD_EVSEL_CONFIG
1236 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1237 const char *sys, const char *event,
1238 struct parse_events_error *err,
1239 struct list_head *head_config)
1242 struct perf_event_attr attr;
1244 if (config_attr(&attr, head_config, err,
1245 config_term_tracepoint))
1249 if (strpbrk(sys, "*?"))
1250 return add_tracepoint_multi_sys(list, idx, sys, event,
1253 return add_tracepoint_event(list, idx, sys, event,
1257 int parse_events_add_numeric(struct parse_events_state *parse_state,
1258 struct list_head *list,
1259 u32 type, u64 config,
1260 struct list_head *head_config)
1262 struct perf_event_attr attr;
1263 LIST_HEAD(config_terms);
1265 memset(&attr, 0, sizeof(attr));
1267 attr.config = config;
1270 if (config_attr(&attr, head_config, parse_state->error,
1271 config_term_common))
1274 if (get_config_terms(head_config, &config_terms))
1278 return add_event(list, &parse_state->idx, &attr,
1279 get_config_name(head_config), &config_terms);
1282 int parse_events_add_tool(struct parse_events_state *parse_state,
1283 struct list_head *list,
1284 enum perf_tool_event tool_event)
1286 return add_event_tool(list, &parse_state->idx, tool_event);
1289 static bool config_term_percore(struct list_head *config_terms)
1291 struct perf_evsel_config_term *term;
1293 list_for_each_entry(term, config_terms, list) {
1294 if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1295 return term->val.percore;
1301 int parse_events_add_pmu(struct parse_events_state *parse_state,
1302 struct list_head *list, char *name,
1303 struct list_head *head_config,
1304 bool auto_merge_stats,
1307 struct perf_event_attr attr;
1308 struct perf_pmu_info info;
1309 struct perf_pmu *pmu;
1310 struct evsel *evsel;
1311 struct parse_events_error *err = parse_state->error;
1312 bool use_uncore_alias;
1313 LIST_HEAD(config_terms);
1315 pmu = perf_pmu__find(name);
1317 if (asprintf(&err->str,
1318 "Cannot find PMU `%s'. Missing kernel support?",
1324 if (pmu->default_config) {
1325 memcpy(&attr, pmu->default_config,
1326 sizeof(struct perf_event_attr));
1328 memset(&attr, 0, sizeof(attr));
1331 use_uncore_alias = (pmu->is_uncore && use_alias);
1334 attr.type = pmu->type;
1335 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1336 auto_merge_stats, NULL);
1338 evsel->pmu_name = name;
1339 evsel->use_uncore_alias = use_uncore_alias;
1346 if (perf_pmu__check_alias(pmu, head_config, &info))
1350 * Configure hardcoded terms first, no need to check
1351 * return value when called with fail == 0 ;)
1353 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1356 if (get_config_terms(head_config, &config_terms))
1359 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
1362 evsel = __add_event(list, &parse_state->idx, &attr,
1363 get_config_name(head_config), pmu,
1364 &config_terms, auto_merge_stats, NULL);
1366 evsel->unit = info.unit;
1367 evsel->scale = info.scale;
1368 evsel->per_pkg = info.per_pkg;
1369 evsel->snapshot = info.snapshot;
1370 evsel->metric_expr = info.metric_expr;
1371 evsel->metric_name = info.metric_name;
1372 evsel->pmu_name = name;
1373 evsel->use_uncore_alias = use_uncore_alias;
1374 evsel->percore = config_term_percore(&evsel->config_terms);
1377 return evsel ? 0 : -ENOMEM;
1380 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1381 char *str, struct list_head **listp)
1383 struct list_head *head;
1384 struct parse_events_term *term;
1385 struct list_head *list;
1386 struct perf_pmu *pmu = NULL;
1390 /* Add it for all PMUs that support the alias */
1391 list = malloc(sizeof(struct list_head));
1394 INIT_LIST_HEAD(list);
1395 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1396 struct perf_pmu_alias *alias;
1398 list_for_each_entry(alias, &pmu->aliases, list) {
1399 if (!strcasecmp(alias->name, str)) {
1400 head = malloc(sizeof(struct list_head));
1403 INIT_LIST_HEAD(head);
1404 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1405 str, 1, false, &str, NULL) < 0)
1407 list_add_tail(&term->list, head);
1409 if (!parse_events_add_pmu(parse_state, list,
1412 pr_debug("%s -> %s/%s/\n", str,
1413 pmu->name, alias->str);
1417 parse_events_terms__delete(head);
1427 int parse_events__modifier_group(struct list_head *list,
1430 return parse_events__modifier_event(list, event_mod, true);
1434 * Check if the two uncore PMUs are from the same uncore block
1435 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1437 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1439 char *end_a, *end_b;
1441 end_a = strrchr(pmu_name_a, '_');
1442 end_b = strrchr(pmu_name_b, '_');
1444 if (!end_a || !end_b)
1447 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1450 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1454 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1455 struct parse_events_state *parse_state)
1457 struct evsel *evsel, *leader;
1459 bool is_leader = true;
1460 int i, nr_pmu = 0, total_members, ret = 0;
1462 leader = list_first_entry(list, struct evsel, core.node);
1463 evsel = list_last_entry(list, struct evsel, core.node);
1464 total_members = evsel->idx - leader->idx + 1;
1466 leaders = calloc(total_members, sizeof(uintptr_t));
1467 if (WARN_ON(!leaders))
1471 * Going through the whole group and doing sanity check.
1472 * All members must use alias, and be from the same uncore block.
1473 * Also, storing the leader events in an array.
1475 __evlist__for_each_entry(list, evsel) {
1477 /* Only split the uncore group which members use alias */
1478 if (!evsel->use_uncore_alias)
1481 /* The events must be from the same uncore block */
1482 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1488 * If the event's PMU name starts to repeat, it must be a new
1489 * event. That can be used to distinguish the leader from
1490 * other members, even they have the same event name.
1492 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1496 /* The name is always alias name */
1497 WARN_ON(strcmp(leader->name, evsel->name));
1499 /* Store the leader event for each PMU */
1500 leaders[nr_pmu++] = (uintptr_t) evsel;
1503 /* only one event alias */
1504 if (nr_pmu == total_members) {
1505 parse_state->nr_groups--;
1510 * An uncore event alias is a joint name which means the same event
1511 * runs on all PMUs of a block.
1512 * Perf doesn't support mixed events from different PMUs in the same
1513 * group. The big group has to be split into multiple small groups
1514 * which only include the events from the same PMU.
1516 * Here the uncore event aliases must be from the same uncore block.
1517 * The number of PMUs must be same for each alias. The number of new
1518 * small groups equals to the number of PMUs.
1519 * Setting the leader event for corresponding members in each group.
1522 __evlist__for_each_entry(list, evsel) {
1525 evsel->leader = (struct evsel *) leaders[i++];
1528 /* The number of members and group name are same for each group */
1529 for (i = 0; i < nr_pmu; i++) {
1530 evsel = (struct evsel *) leaders[i];
1531 evsel->core.nr_members = total_members / nr_pmu;
1532 evsel->group_name = name ? strdup(name) : NULL;
1535 /* Take the new small groups into account */
1536 parse_state->nr_groups += nr_pmu - 1;
1545 void parse_events__set_leader(char *name, struct list_head *list,
1546 struct parse_events_state *parse_state)
1548 struct evsel *leader;
1550 if (list_empty(list)) {
1551 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1555 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1558 __perf_evlist__set_leader(list);
1559 leader = list_entry(list->next, struct evsel, core.node);
1560 leader->group_name = name ? strdup(name) : NULL;
1563 /* list_event is assumed to point to malloc'ed memory */
1564 void parse_events_update_lists(struct list_head *list_event,
1565 struct list_head *list_all)
1568 * Called for single event definition. Update the
1569 * 'all event' list, and reinit the 'single event'
1570 * list, for next event definition.
1572 list_splice_tail(list_event, list_all);
1576 struct event_modifier {
1591 static int get_event_modifier(struct event_modifier *mod, char *str,
1592 struct evsel *evsel)
1594 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1595 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1596 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1597 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1598 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1599 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1600 int precise = evsel ? evsel->core.attr.precise_ip : 0;
1601 int precise_max = 0;
1602 int sample_read = 0;
1603 int pinned = evsel ? evsel->core.attr.pinned : 0;
1605 int exclude = eu | ek | eh;
1606 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1609 memset(mod, 0, sizeof(*mod));
1614 exclude = eu = ek = eh = 1;
1616 } else if (*str == 'k') {
1618 exclude = eu = ek = eh = 1;
1620 } else if (*str == 'h') {
1622 exclude = eu = ek = eh = 1;
1624 } else if (*str == 'G') {
1626 exclude_GH = eG = eH = 1;
1628 } else if (*str == 'H') {
1630 exclude_GH = eG = eH = 1;
1632 } else if (*str == 'I') {
1634 } else if (*str == 'p') {
1636 /* use of precise requires exclude_guest */
1639 } else if (*str == 'P') {
1641 } else if (*str == 'S') {
1643 } else if (*str == 'D') {
1645 } else if (*str == 'W') {
1656 * 0 - SAMPLE_IP can have arbitrary skid
1657 * 1 - SAMPLE_IP must have constant skid
1658 * 2 - SAMPLE_IP requested to have 0 skid
1659 * 3 - SAMPLE_IP must have 0 skid
1661 * See also PERF_RECORD_MISC_EXACT_IP
1672 mod->precise = precise;
1673 mod->precise_max = precise_max;
1674 mod->exclude_GH = exclude_GH;
1675 mod->sample_read = sample_read;
1676 mod->pinned = pinned;
1683 * Basic modifier sanity check to validate it contains only one
1684 * instance of any modifier (apart from 'p') present.
1686 static int check_modifier(char *str)
1690 /* The sizeof includes 0 byte as well. */
1691 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1695 if (*p != 'p' && strchr(p + 1, *p))
1703 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1705 struct evsel *evsel;
1706 struct event_modifier mod;
1711 if (check_modifier(str))
1714 if (!add && get_event_modifier(&mod, str, NULL))
1717 __evlist__for_each_entry(list, evsel) {
1718 if (add && get_event_modifier(&mod, str, evsel))
1721 evsel->core.attr.exclude_user = mod.eu;
1722 evsel->core.attr.exclude_kernel = mod.ek;
1723 evsel->core.attr.exclude_hv = mod.eh;
1724 evsel->core.attr.precise_ip = mod.precise;
1725 evsel->core.attr.exclude_host = mod.eH;
1726 evsel->core.attr.exclude_guest = mod.eG;
1727 evsel->core.attr.exclude_idle = mod.eI;
1728 evsel->exclude_GH = mod.exclude_GH;
1729 evsel->sample_read = mod.sample_read;
1730 evsel->precise_max = mod.precise_max;
1731 evsel->weak_group = mod.weak;
1733 if (perf_evsel__is_group_leader(evsel))
1734 evsel->core.attr.pinned = mod.pinned;
1740 int parse_events_name(struct list_head *list, char *name)
1742 struct evsel *evsel;
1744 __evlist__for_each_entry(list, evsel) {
1746 evsel->name = strdup(name);
1753 comp_pmu(const void *p1, const void *p2)
1755 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1756 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1758 return strcasecmp(pmu1->symbol, pmu2->symbol);
1761 static void perf_pmu__parse_cleanup(void)
1763 if (perf_pmu_events_list_num > 0) {
1764 struct perf_pmu_event_symbol *p;
1767 for (i = 0; i < perf_pmu_events_list_num; i++) {
1768 p = perf_pmu_events_list + i;
1771 zfree(&perf_pmu_events_list);
1772 perf_pmu_events_list_num = 0;
1776 #define SET_SYMBOL(str, stype) \
1785 * Read the pmu events list from sysfs
1786 * Save it into perf_pmu_events_list
1788 static void perf_pmu__parse_init(void)
1791 struct perf_pmu *pmu = NULL;
1792 struct perf_pmu_alias *alias;
1796 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1797 list_for_each_entry(alias, &pmu->aliases, list) {
1798 if (strchr(alias->name, '-'))
1805 perf_pmu_events_list_num = -1;
1808 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1809 if (!perf_pmu_events_list)
1811 perf_pmu_events_list_num = len;
1815 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1816 list_for_each_entry(alias, &pmu->aliases, list) {
1817 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1818 char *tmp = strchr(alias->name, '-');
1821 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1822 PMU_EVENT_SYMBOL_PREFIX);
1824 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1827 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1832 qsort(perf_pmu_events_list, len,
1833 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1837 perf_pmu__parse_cleanup();
1840 enum perf_pmu_event_symbol_type
1841 perf_pmu__parse_check(const char *name)
1843 struct perf_pmu_event_symbol p, *r;
1845 /* scan kernel pmu events from sysfs if needed */
1846 if (perf_pmu_events_list_num == 0)
1847 perf_pmu__parse_init();
1849 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1850 * cpu-cycles has been handled by hardcode.
1851 * So it must be cpu// events, not kernel pmu event.
1853 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1854 return PMU_EVENT_SYMBOL_ERR;
1856 p.symbol = strdup(name);
1857 r = bsearch(&p, perf_pmu_events_list,
1858 (size_t) perf_pmu_events_list_num,
1859 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1861 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1864 static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1866 YY_BUFFER_STATE buffer;
1870 ret = parse_events_lex_init_extra(start_token, &scanner);
1874 buffer = parse_events__scan_string(str, scanner);
1877 parse_events_debug = 1;
1879 ret = parse_events_parse(parse_state, scanner);
1881 parse_events__flush_buffer(buffer, scanner);
1882 parse_events__delete_buffer(buffer, scanner);
1883 parse_events_lex_destroy(scanner);
1888 * parse event config string, return a list of event terms.
1890 int parse_events_terms(struct list_head *terms, const char *str)
1892 struct parse_events_state parse_state = {
1897 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
1899 list_splice(parse_state.terms, terms);
1900 zfree(&parse_state.terms);
1904 parse_events_terms__delete(parse_state.terms);
1908 int parse_events(struct evlist *evlist, const char *str,
1909 struct parse_events_error *err)
1911 struct parse_events_state parse_state = {
1912 .list = LIST_HEAD_INIT(parse_state.list),
1913 .idx = evlist->core.nr_entries,
1919 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1920 perf_pmu__parse_cleanup();
1924 if (list_empty(&parse_state.list)) {
1925 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1929 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1930 evlist->nr_groups += parse_state.nr_groups;
1931 last = perf_evlist__last(evlist);
1932 last->cmdline_group_boundary = true;
1938 * There are 2 users - builtin-record and builtin-test objects.
1939 * Both call evlist__delete in case of error, so we dont
1945 #define MAX_WIDTH 1000
1946 static int get_term_width(void)
1950 get_term_dimensions(&ws);
1951 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1954 void parse_events_print_error(struct parse_events_error *err,
1957 const char *str = "invalid or unsupported event: ";
1958 char _buf[MAX_WIDTH];
1959 char *buf = (char *) event;
1963 /* -2 for extra '' in the final fprintf */
1964 int width = get_term_width() - 2;
1965 int len_event = strlen(event);
1966 int len_str, max_len, cut = 0;
1969 * Maximum error index indent, we will cut
1970 * the event string if it's bigger.
1972 int max_err_idx = 13;
1975 * Let's be specific with the message when
1976 * we have the precise error.
1978 str = "event syntax error: ";
1979 len_str = strlen(str);
1980 max_len = width - len_str;
1984 /* We're cutting from the beginning. */
1985 if (err->idx > max_err_idx)
1986 cut = err->idx - max_err_idx;
1988 strncpy(buf, event + cut, max_len);
1990 /* Mark cut parts with '..' on both sides. */
1992 buf[0] = buf[1] = '.';
1994 if ((len_event - cut) > max_len) {
1995 buf[max_len - 1] = buf[max_len - 2] = '.';
1999 idx = len_str + err->idx - cut;
2002 fprintf(stderr, "%s'%s'\n", str, buf);
2004 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2006 fprintf(stderr, "\n%s\n", err->help);
2014 int parse_events_option(const struct option *opt, const char *str,
2015 int unset __maybe_unused)
2017 struct evlist *evlist = *(struct evlist **)opt->value;
2018 struct parse_events_error err = { .idx = 0, };
2019 int ret = parse_events(evlist, str, &err);
2022 parse_events_print_error(&err, str);
2023 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2030 foreach_evsel_in_last_glob(struct evlist *evlist,
2031 int (*func)(struct evsel *evsel,
2035 struct evsel *last = NULL;
2039 * Don't return when list_empty, give func a chance to report
2040 * error when it found last == NULL.
2042 * So no need to WARN here, let *func do this.
2044 if (evlist->core.nr_entries > 0)
2045 last = perf_evlist__last(evlist);
2048 err = (*func)(last, arg);
2054 if (last->core.node.prev == &evlist->core.entries)
2056 last = list_entry(last->core.node.prev, struct evsel, core.node);
2057 } while (!last->cmdline_group_boundary);
2062 static int set_filter(struct evsel *evsel, const void *arg)
2064 const char *str = arg;
2066 int nr_addr_filters = 0;
2067 struct perf_pmu *pmu = NULL;
2069 if (evsel == NULL) {
2071 "--filter option should follow a -e tracepoint or HW tracer option\n");
2075 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2076 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2078 "not enough memory to hold filter string\n");
2085 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2086 if (pmu->type == evsel->core.attr.type) {
2092 perf_pmu__scan_file(pmu, "nr_addr_filters",
2093 "%d", &nr_addr_filters);
2095 if (!nr_addr_filters) {
2097 "This CPU does not support address filtering\n");
2101 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
2103 "not enough memory to hold filter string\n");
2110 int parse_filter(const struct option *opt, const char *str,
2111 int unset __maybe_unused)
2113 struct evlist *evlist = *(struct evlist **)opt->value;
2115 return foreach_evsel_in_last_glob(evlist, set_filter,
2119 static int add_exclude_perf_filter(struct evsel *evsel,
2120 const void *arg __maybe_unused)
2122 char new_filter[64];
2124 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2126 "--exclude-perf option should follow a -e tracepoint option\n");
2130 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2132 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
2134 "not enough memory to hold filter string\n");
2141 int exclude_perf(const struct option *opt,
2142 const char *arg __maybe_unused,
2143 int unset __maybe_unused)
2145 struct evlist *evlist = *(struct evlist **)opt->value;
2147 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2151 static const char * const event_type_descriptors[] = {
2155 "Hardware cache event",
2156 "Raw hardware event descriptor",
2157 "Hardware breakpoint",
2160 static int cmp_string(const void *a, const void *b)
2162 const char * const *as = a;
2163 const char * const *bs = b;
2165 return strcmp(*as, *bs);
2169 * Print the events from <debugfs_mount_point>/tracing/events
2172 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2175 DIR *sys_dir, *evt_dir;
2176 struct dirent *sys_dirent, *evt_dirent;
2177 char evt_path[MAXPATHLEN];
2179 char **evt_list = NULL;
2180 unsigned int evt_i = 0, evt_num = 0;
2181 bool evt_num_known = false;
2184 sys_dir = tracing_events__opendir();
2188 if (evt_num_known) {
2189 evt_list = zalloc(sizeof(char *) * evt_num);
2191 goto out_close_sys_dir;
2194 for_each_subsystem(sys_dir, sys_dirent) {
2195 if (subsys_glob != NULL &&
2196 !strglobmatch(sys_dirent->d_name, subsys_glob))
2199 dir_path = get_events_file(sys_dirent->d_name);
2202 evt_dir = opendir(dir_path);
2206 for_each_event(dir_path, evt_dir, evt_dirent) {
2207 if (event_glob != NULL &&
2208 !strglobmatch(evt_dirent->d_name, event_glob))
2211 if (!evt_num_known) {
2216 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2217 sys_dirent->d_name, evt_dirent->d_name);
2219 evt_list[evt_i] = strdup(evt_path);
2220 if (evt_list[evt_i] == NULL) {
2221 put_events_file(dir_path);
2222 goto out_close_evt_dir;
2228 put_events_file(dir_path);
2232 if (!evt_num_known) {
2233 evt_num_known = true;
2236 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2238 while (evt_i < evt_num) {
2240 printf("%s ", evt_list[evt_i++]);
2243 printf(" %-50s [%s]\n", evt_list[evt_i++],
2244 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2246 if (evt_num && pager_in_use())
2251 for (evt_i = 0; evt_i < evt_num; evt_i++)
2252 zfree(&evt_list[evt_i]);
2261 printf("FATAL: not enough memory to print %s\n",
2262 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2268 * Check whether event is in <debugfs_mount_point>/tracing/events
2271 int is_valid_tracepoint(const char *event_string)
2273 DIR *sys_dir, *evt_dir;
2274 struct dirent *sys_dirent, *evt_dirent;
2275 char evt_path[MAXPATHLEN];
2278 sys_dir = tracing_events__opendir();
2282 for_each_subsystem(sys_dir, sys_dirent) {
2283 dir_path = get_events_file(sys_dirent->d_name);
2286 evt_dir = opendir(dir_path);
2290 for_each_event(dir_path, evt_dir, evt_dirent) {
2291 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2292 sys_dirent->d_name, evt_dirent->d_name);
2293 if (!strcmp(evt_path, event_string)) {
2301 put_events_file(dir_path);
2307 static bool is_event_supported(u8 type, unsigned config)
2311 struct evsel *evsel;
2312 struct perf_event_attr attr = {
2317 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
2322 evsel = evsel__new(&attr);
2324 open_return = evsel__open(evsel, NULL, tmap);
2325 ret = open_return >= 0;
2327 if (open_return == -EACCES) {
2329 * This happens if the paranoid value
2330 * /proc/sys/kernel/perf_event_paranoid is set to 2
2331 * Re-run with exclude_kernel set; we don't do that
2332 * by default as some ARM machines do not support it.
2335 evsel->core.attr.exclude_kernel = 1;
2336 ret = evsel__open(evsel, NULL, tmap) >= 0;
2338 evsel__delete(evsel);
2341 perf_thread_map__put(tmap);
2345 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2348 struct probe_cache *pcache;
2349 struct probe_cache_entry *ent;
2350 struct strlist *bidlist, *sdtlist;
2351 struct strlist_config cfg = {.dont_dupstr = true};
2352 struct str_node *nd, *nd2;
2353 char *buf, *path, *ptr = NULL;
2354 bool show_detail = false;
2357 sdtlist = strlist__new(NULL, &cfg);
2359 pr_debug("Failed to allocate new strlist for SDT\n");
2362 bidlist = build_id_cache__list_all(true);
2364 pr_debug("Failed to get buildids: %d\n", errno);
2367 strlist__for_each_entry(nd, bidlist) {
2368 pcache = probe_cache__new(nd->s, NULL);
2371 list_for_each_entry(ent, &pcache->entries, node) {
2375 !strglobmatch(ent->pev.group, subsys_glob))
2378 !strglobmatch(ent->pev.event, event_glob))
2380 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2381 ent->pev.event, nd->s);
2383 strlist__add(sdtlist, buf);
2385 probe_cache__delete(pcache);
2387 strlist__delete(bidlist);
2389 strlist__for_each_entry(nd, sdtlist) {
2390 buf = strchr(nd->s, '@');
2394 printf("%s ", nd->s);
2397 nd2 = strlist__next(nd);
2399 ptr = strchr(nd2->s, '@');
2402 if (strcmp(nd->s, nd2->s) == 0)
2406 path = build_id_cache__origname(buf);
2407 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2409 printf(" %-50s [%s]\n", buf, "SDT event");
2414 printf(" %-50s [%s]\n", nd->s, "SDT event");
2416 if (strcmp(nd->s, nd2->s) != 0)
2417 show_detail = false;
2422 strlist__delete(sdtlist);
2425 int print_hwcache_events(const char *event_glob, bool name_only)
2427 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2429 char **evt_list = NULL;
2430 bool evt_num_known = false;
2433 if (evt_num_known) {
2434 evt_list = zalloc(sizeof(char *) * evt_num);
2439 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2440 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2441 /* skip invalid cache type */
2442 if (!perf_evsel__is_cache_op_valid(type, op))
2445 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2446 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2447 name, sizeof(name));
2448 if (event_glob != NULL && !strglobmatch(name, event_glob))
2451 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2452 type | (op << 8) | (i << 16)))
2455 if (!evt_num_known) {
2460 evt_list[evt_i] = strdup(name);
2461 if (evt_list[evt_i] == NULL)
2468 if (!evt_num_known) {
2469 evt_num_known = true;
2472 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2474 while (evt_i < evt_num) {
2476 printf("%s ", evt_list[evt_i++]);
2479 printf(" %-50s [%s]\n", evt_list[evt_i++],
2480 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2482 if (evt_num && pager_in_use())
2487 for (evt_i = 0; evt_i < evt_num; evt_i++)
2488 zfree(&evt_list[evt_i]);
2493 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2499 static void print_tool_event(const char *name, const char *event_glob,
2502 if (event_glob && !strglobmatch(name, event_glob))
2505 printf("%s ", name);
2507 printf(" %-50s [%s]\n", name, "Tool event");
2511 void print_tool_events(const char *event_glob, bool name_only)
2513 print_tool_event("duration_time", event_glob, name_only);
2518 void print_symbol_events(const char *event_glob, unsigned type,
2519 struct event_symbol *syms, unsigned max,
2522 unsigned int i, evt_i = 0, evt_num = 0;
2523 char name[MAX_NAME_LEN];
2524 char **evt_list = NULL;
2525 bool evt_num_known = false;
2528 if (evt_num_known) {
2529 evt_list = zalloc(sizeof(char *) * evt_num);
2535 for (i = 0; i < max; i++, syms++) {
2537 if (event_glob != NULL && syms->symbol != NULL &&
2538 !(strglobmatch(syms->symbol, event_glob) ||
2539 (syms->alias && strglobmatch(syms->alias, event_glob))))
2542 if (!is_event_supported(type, i))
2545 if (!evt_num_known) {
2550 if (!name_only && strlen(syms->alias))
2551 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2553 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2555 evt_list[evt_i] = strdup(name);
2556 if (evt_list[evt_i] == NULL)
2561 if (!evt_num_known) {
2562 evt_num_known = true;
2565 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2567 while (evt_i < evt_num) {
2569 printf("%s ", evt_list[evt_i++]);
2572 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2574 if (evt_num && pager_in_use())
2579 for (evt_i = 0; evt_i < evt_num; evt_i++)
2580 zfree(&evt_list[evt_i]);
2585 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2591 * Print the help text for the event symbols:
2593 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2594 bool long_desc, bool details_flag)
2596 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2597 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2599 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2600 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2601 print_tool_events(event_glob, name_only);
2603 print_hwcache_events(event_glob, name_only);
2605 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2608 if (event_glob != NULL)
2612 printf(" %-50s [%s]\n",
2614 event_type_descriptors[PERF_TYPE_RAW]);
2615 printf(" %-50s [%s]\n",
2616 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2617 event_type_descriptors[PERF_TYPE_RAW]);
2619 printf(" (see 'man perf-list' on how to encode it)\n\n");
2621 printf(" %-50s [%s]\n",
2622 "mem:<addr>[/len][:access]",
2623 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2628 print_tracepoint_events(NULL, NULL, name_only);
2630 print_sdt_events(NULL, NULL, name_only);
2632 metricgroup__print(true, true, NULL, name_only, details_flag);
2635 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2637 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2640 static int new_term(struct parse_events_term **_term,
2641 struct parse_events_term *temp,
2644 struct parse_events_term *term;
2646 term = malloc(sizeof(*term));
2651 INIT_LIST_HEAD(&term->list);
2654 switch (term->type_val) {
2655 case PARSE_EVENTS__TERM_TYPE_NUM:
2656 term->val.num = num;
2658 case PARSE_EVENTS__TERM_TYPE_STR:
2659 term->val.str = str;
2670 int parse_events_term__num(struct parse_events_term **term,
2671 int type_term, char *config, u64 num,
2673 void *loc_term_, void *loc_val_)
2675 YYLTYPE *loc_term = loc_term_;
2676 YYLTYPE *loc_val = loc_val_;
2678 struct parse_events_term temp = {
2679 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2680 .type_term = type_term,
2682 .no_value = no_value,
2683 .err_term = loc_term ? loc_term->first_column : 0,
2684 .err_val = loc_val ? loc_val->first_column : 0,
2687 return new_term(term, &temp, NULL, num);
2690 int parse_events_term__str(struct parse_events_term **term,
2691 int type_term, char *config, char *str,
2692 void *loc_term_, void *loc_val_)
2694 YYLTYPE *loc_term = loc_term_;
2695 YYLTYPE *loc_val = loc_val_;
2697 struct parse_events_term temp = {
2698 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2699 .type_term = type_term,
2701 .err_term = loc_term ? loc_term->first_column : 0,
2702 .err_val = loc_val ? loc_val->first_column : 0,
2705 return new_term(term, &temp, str, 0);
2708 int parse_events_term__sym_hw(struct parse_events_term **term,
2709 char *config, unsigned idx)
2711 struct event_symbol *sym;
2712 struct parse_events_term temp = {
2713 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2714 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2715 .config = config ?: (char *) "event",
2718 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2719 sym = &event_symbols_hw[idx];
2721 return new_term(term, &temp, (char *) sym->symbol, 0);
2724 int parse_events_term__clone(struct parse_events_term **new,
2725 struct parse_events_term *term)
2727 struct parse_events_term temp = {
2728 .type_val = term->type_val,
2729 .type_term = term->type_term,
2730 .config = term->config,
2731 .err_term = term->err_term,
2732 .err_val = term->err_val,
2735 return new_term(new, &temp, term->val.str, term->val.num);
2738 int parse_events_copy_term_list(struct list_head *old,
2739 struct list_head **new)
2741 struct parse_events_term *term, *n;
2749 *new = malloc(sizeof(struct list_head));
2752 INIT_LIST_HEAD(*new);
2754 list_for_each_entry (term, old, list) {
2755 ret = parse_events_term__clone(&n, term);
2758 list_add_tail(&n->list, *new);
2763 void parse_events_terms__purge(struct list_head *terms)
2765 struct parse_events_term *term, *h;
2767 list_for_each_entry_safe(term, h, terms, list) {
2768 if (term->array.nr_ranges)
2769 zfree(&term->array.ranges);
2770 list_del_init(&term->list);
2775 void parse_events_terms__delete(struct list_head *terms)
2779 parse_events_terms__purge(terms);
2783 void parse_events__clear_array(struct parse_events_array *a)
2788 void parse_events_evlist_error(struct parse_events_state *parse_state,
2789 int idx, const char *str)
2791 struct parse_events_error *err = parse_state->error;
2796 err->str = strdup(str);
2797 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2800 static void config_terms_list(char *buf, size_t buf_sz)
2806 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2807 const char *name = config_term_names[i];
2809 if (!config_term_avail(i, NULL))
2816 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2828 * Return string contains valid config terms of an event.
2829 * @additional_terms: For terms such as PMU sysfs terms.
2831 char *parse_events_formats_error_string(char *additional_terms)
2834 /* "no-overwrite" is the longest name */
2835 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2836 (sizeof("no-overwrite") - 1)];
2838 config_terms_list(static_terms, sizeof(static_terms));
2840 if (additional_terms) {
2841 if (asprintf(&str, "valid terms: %s,%s",
2842 additional_terms, static_terms) < 0)
2845 if (asprintf(&str, "valid terms: %s", static_terms) < 0)