1 #include <linux/hw_breakpoint.h>
11 #include <subcmd/parse-options.h>
12 #include "parse-events.h"
13 #include <subcmd/exec-cmd.h>
19 #include "bpf-loader.h"
21 #include <api/fs/tracing_path.h>
22 #include "parse-events-bison.h"
23 #define YY_EXTRA_TYPE int
24 #include "parse-events-flex.h"
26 #include "thread_map.h"
28 #include "probe-file.h"
30 #include "util/parse-branch-options.h"
31 #include "metricgroup.h"
33 #define MAX_NAME_LEN 100
36 extern int parse_events_debug
;
38 int parse_events_parse(void *parse_state
, void *scanner
);
39 static int get_config_terms(struct list_head
*head_config
,
40 struct list_head
*head_terms __maybe_unused
);
42 static struct perf_pmu_event_symbol
*perf_pmu_events_list
;
44 * The variable indicates the number of supported pmu event symbols.
45 * 0 means not initialized and ready to init
46 * -1 means failed to init, don't try anymore
47 * >0 is the number of supported pmu event symbols
49 static int perf_pmu_events_list_num
;
51 struct event_symbol event_symbols_hw
[PERF_COUNT_HW_MAX
] = {
52 [PERF_COUNT_HW_CPU_CYCLES
] = {
53 .symbol
= "cpu-cycles",
56 [PERF_COUNT_HW_INSTRUCTIONS
] = {
57 .symbol
= "instructions",
60 [PERF_COUNT_HW_CACHE_REFERENCES
] = {
61 .symbol
= "cache-references",
64 [PERF_COUNT_HW_CACHE_MISSES
] = {
65 .symbol
= "cache-misses",
68 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = {
69 .symbol
= "branch-instructions",
72 [PERF_COUNT_HW_BRANCH_MISSES
] = {
73 .symbol
= "branch-misses",
76 [PERF_COUNT_HW_BUS_CYCLES
] = {
77 .symbol
= "bus-cycles",
80 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = {
81 .symbol
= "stalled-cycles-frontend",
82 .alias
= "idle-cycles-frontend",
84 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = {
85 .symbol
= "stalled-cycles-backend",
86 .alias
= "idle-cycles-backend",
88 [PERF_COUNT_HW_REF_CPU_CYCLES
] = {
89 .symbol
= "ref-cycles",
94 struct event_symbol event_symbols_sw
[PERF_COUNT_SW_MAX
] = {
95 [PERF_COUNT_SW_CPU_CLOCK
] = {
96 .symbol
= "cpu-clock",
99 [PERF_COUNT_SW_TASK_CLOCK
] = {
100 .symbol
= "task-clock",
103 [PERF_COUNT_SW_PAGE_FAULTS
] = {
104 .symbol
= "page-faults",
107 [PERF_COUNT_SW_CONTEXT_SWITCHES
] = {
108 .symbol
= "context-switches",
111 [PERF_COUNT_SW_CPU_MIGRATIONS
] = {
112 .symbol
= "cpu-migrations",
113 .alias
= "migrations",
115 [PERF_COUNT_SW_PAGE_FAULTS_MIN
] = {
116 .symbol
= "minor-faults",
119 [PERF_COUNT_SW_PAGE_FAULTS_MAJ
] = {
120 .symbol
= "major-faults",
123 [PERF_COUNT_SW_ALIGNMENT_FAULTS
] = {
124 .symbol
= "alignment-faults",
127 [PERF_COUNT_SW_EMULATION_FAULTS
] = {
128 .symbol
= "emulation-faults",
131 [PERF_COUNT_SW_DUMMY
] = {
135 [PERF_COUNT_SW_BPF_OUTPUT
] = {
136 .symbol
= "bpf-output",
141 #define __PERF_EVENT_FIELD(config, name) \
142 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
144 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
145 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
146 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
147 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
149 #define for_each_subsystem(sys_dir, sys_dirent) \
150 while ((sys_dirent = readdir(sys_dir)) != NULL) \
151 if (sys_dirent->d_type == DT_DIR && \
152 (strcmp(sys_dirent->d_name, ".")) && \
153 (strcmp(sys_dirent->d_name, "..")))
155 static int tp_event_has_id(struct dirent
*sys_dir
, struct dirent
*evt_dir
)
157 char evt_path
[MAXPATHLEN
];
160 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", tracing_events_path
,
161 sys_dir
->d_name
, evt_dir
->d_name
);
162 fd
= open(evt_path
, O_RDONLY
);
170 #define for_each_event(sys_dirent, evt_dir, evt_dirent) \
171 while ((evt_dirent = readdir(evt_dir)) != NULL) \
172 if (evt_dirent->d_type == DT_DIR && \
173 (strcmp(evt_dirent->d_name, ".")) && \
174 (strcmp(evt_dirent->d_name, "..")) && \
175 (!tp_event_has_id(sys_dirent, evt_dirent)))
177 #define MAX_EVENT_LENGTH 512
180 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
182 struct tracepoint_path
*path
= NULL
;
183 DIR *sys_dir
, *evt_dir
;
184 struct dirent
*sys_dirent
, *evt_dirent
;
188 char evt_path
[MAXPATHLEN
];
189 char dir_path
[MAXPATHLEN
];
191 sys_dir
= opendir(tracing_events_path
);
195 for_each_subsystem(sys_dir
, sys_dirent
) {
197 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", tracing_events_path
,
199 evt_dir
= opendir(dir_path
);
203 for_each_event(sys_dirent
, evt_dir
, evt_dirent
) {
205 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
207 fd
= open(evt_path
, O_RDONLY
);
210 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
219 path
= zalloc(sizeof(*path
));
222 path
->system
= malloc(MAX_EVENT_LENGTH
);
227 path
->name
= malloc(MAX_EVENT_LENGTH
);
229 zfree(&path
->system
);
233 strncpy(path
->system
, sys_dirent
->d_name
,
235 strncpy(path
->name
, evt_dirent
->d_name
,
247 struct tracepoint_path
*tracepoint_name_to_path(const char *name
)
249 struct tracepoint_path
*path
= zalloc(sizeof(*path
));
250 char *str
= strchr(name
, ':');
252 if (path
== NULL
|| str
== NULL
) {
257 path
->system
= strndup(name
, str
- name
);
258 path
->name
= strdup(str
+1);
260 if (path
->system
== NULL
|| path
->name
== NULL
) {
261 zfree(&path
->system
);
269 const char *event_type(int type
)
272 case PERF_TYPE_HARDWARE
:
275 case PERF_TYPE_SOFTWARE
:
278 case PERF_TYPE_TRACEPOINT
:
281 case PERF_TYPE_HW_CACHE
:
282 return "hardware-cache";
291 static int parse_events__is_name_term(struct parse_events_term
*term
)
293 return term
->type_term
== PARSE_EVENTS__TERM_TYPE_NAME
;
296 static char *get_config_name(struct list_head
*head_terms
)
298 struct parse_events_term
*term
;
303 list_for_each_entry(term
, head_terms
, list
)
304 if (parse_events__is_name_term(term
))
305 return term
->val
.str
;
310 static struct perf_evsel
*
311 __add_event(struct list_head
*list
, int *idx
,
312 struct perf_event_attr
*attr
,
313 char *name
, struct cpu_map
*cpus
,
314 struct list_head
*config_terms
, bool auto_merge_stats
)
316 struct perf_evsel
*evsel
;
318 event_attr_init(attr
);
320 evsel
= perf_evsel__new_idx(attr
, *idx
);
325 evsel
->cpus
= cpu_map__get(cpus
);
326 evsel
->own_cpus
= cpu_map__get(cpus
);
327 evsel
->system_wide
= !!cpus
;
328 evsel
->auto_merge_stats
= auto_merge_stats
;
331 evsel
->name
= strdup(name
);
334 list_splice(config_terms
, &evsel
->config_terms
);
336 list_add_tail(&evsel
->node
, list
);
340 static int add_event(struct list_head
*list
, int *idx
,
341 struct perf_event_attr
*attr
, char *name
,
342 struct list_head
*config_terms
)
344 return __add_event(list
, idx
, attr
, name
, NULL
, config_terms
, false) ? 0 : -ENOMEM
;
347 static int parse_aliases(char *str
, const char *names
[][PERF_EVSEL__MAX_ALIASES
], int size
)
352 for (i
= 0; i
< size
; i
++) {
353 for (j
= 0; j
< PERF_EVSEL__MAX_ALIASES
&& names
[i
][j
]; j
++) {
354 n
= strlen(names
[i
][j
]);
355 if (n
> longest
&& !strncasecmp(str
, names
[i
][j
], n
))
365 typedef int config_term_func_t(struct perf_event_attr
*attr
,
366 struct parse_events_term
*term
,
367 struct parse_events_error
*err
);
368 static int config_term_common(struct perf_event_attr
*attr
,
369 struct parse_events_term
*term
,
370 struct parse_events_error
*err
);
371 static int config_attr(struct perf_event_attr
*attr
,
372 struct list_head
*head
,
373 struct parse_events_error
*err
,
374 config_term_func_t config_term
);
376 int parse_events_add_cache(struct list_head
*list
, int *idx
,
377 char *type
, char *op_result1
, char *op_result2
,
378 struct parse_events_error
*err
,
379 struct list_head
*head_config
)
381 struct perf_event_attr attr
;
382 LIST_HEAD(config_terms
);
383 char name
[MAX_NAME_LEN
], *config_name
;
384 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
385 char *op_result
[2] = { op_result1
, op_result2
};
389 * No fallback - if we cannot get a clear cache type
392 cache_type
= parse_aliases(type
, perf_evsel__hw_cache
,
393 PERF_COUNT_HW_CACHE_MAX
);
394 if (cache_type
== -1)
397 config_name
= get_config_name(head_config
);
398 n
= snprintf(name
, MAX_NAME_LEN
, "%s", type
);
400 for (i
= 0; (i
< 2) && (op_result
[i
]); i
++) {
401 char *str
= op_result
[i
];
403 n
+= snprintf(name
+ n
, MAX_NAME_LEN
- n
, "-%s", str
);
405 if (cache_op
== -1) {
406 cache_op
= parse_aliases(str
, perf_evsel__hw_cache_op
,
407 PERF_COUNT_HW_CACHE_OP_MAX
);
409 if (!perf_evsel__is_cache_op_valid(cache_type
, cache_op
))
415 if (cache_result
== -1) {
416 cache_result
= parse_aliases(str
, perf_evsel__hw_cache_result
,
417 PERF_COUNT_HW_CACHE_RESULT_MAX
);
418 if (cache_result
>= 0)
424 * Fall back to reads:
427 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
430 * Fall back to accesses:
432 if (cache_result
== -1)
433 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
435 memset(&attr
, 0, sizeof(attr
));
436 attr
.config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
437 attr
.type
= PERF_TYPE_HW_CACHE
;
440 if (config_attr(&attr
, head_config
, err
,
444 if (get_config_terms(head_config
, &config_terms
))
447 return add_event(list
, idx
, &attr
, config_name
? : name
, &config_terms
);
450 static void tracepoint_error(struct parse_events_error
*e
, int err
,
451 const char *sys
, const char *name
)
459 * We get error directly from syscall errno ( > 0),
460 * or from encoded pointer's error ( < 0).
466 e
->str
= strdup("can't access trace events");
469 e
->str
= strdup("unknown tracepoint");
472 e
->str
= strdup("failed to add tracepoint");
476 tracing_path__strerror_open_tp(err
, help
, sizeof(help
), sys
, name
);
477 e
->help
= strdup(help
);
480 static int add_tracepoint(struct list_head
*list
, int *idx
,
481 const char *sys_name
, const char *evt_name
,
482 struct parse_events_error
*err
,
483 struct list_head
*head_config
)
485 struct perf_evsel
*evsel
;
487 evsel
= perf_evsel__newtp_idx(sys_name
, evt_name
, (*idx
)++);
489 tracepoint_error(err
, PTR_ERR(evsel
), sys_name
, evt_name
);
490 return PTR_ERR(evsel
);
494 LIST_HEAD(config_terms
);
496 if (get_config_terms(head_config
, &config_terms
))
498 list_splice(&config_terms
, &evsel
->config_terms
);
501 list_add_tail(&evsel
->node
, list
);
505 static int add_tracepoint_multi_event(struct list_head
*list
, int *idx
,
506 const char *sys_name
, const char *evt_name
,
507 struct parse_events_error
*err
,
508 struct list_head
*head_config
)
510 char evt_path
[MAXPATHLEN
];
511 struct dirent
*evt_ent
;
513 int ret
= 0, found
= 0;
515 snprintf(evt_path
, MAXPATHLEN
, "%s/%s", tracing_events_path
, sys_name
);
516 evt_dir
= opendir(evt_path
);
518 tracepoint_error(err
, errno
, sys_name
, evt_name
);
522 while (!ret
&& (evt_ent
= readdir(evt_dir
))) {
523 if (!strcmp(evt_ent
->d_name
, ".")
524 || !strcmp(evt_ent
->d_name
, "..")
525 || !strcmp(evt_ent
->d_name
, "enable")
526 || !strcmp(evt_ent
->d_name
, "filter"))
529 if (!strglobmatch(evt_ent
->d_name
, evt_name
))
534 ret
= add_tracepoint(list
, idx
, sys_name
, evt_ent
->d_name
,
539 tracepoint_error(err
, ENOENT
, sys_name
, evt_name
);
547 static int add_tracepoint_event(struct list_head
*list
, int *idx
,
548 const char *sys_name
, const char *evt_name
,
549 struct parse_events_error
*err
,
550 struct list_head
*head_config
)
552 return strpbrk(evt_name
, "*?") ?
553 add_tracepoint_multi_event(list
, idx
, sys_name
, evt_name
,
555 add_tracepoint(list
, idx
, sys_name
, evt_name
,
559 static int add_tracepoint_multi_sys(struct list_head
*list
, int *idx
,
560 const char *sys_name
, const char *evt_name
,
561 struct parse_events_error
*err
,
562 struct list_head
*head_config
)
564 struct dirent
*events_ent
;
568 events_dir
= opendir(tracing_events_path
);
570 tracepoint_error(err
, errno
, sys_name
, evt_name
);
574 while (!ret
&& (events_ent
= readdir(events_dir
))) {
575 if (!strcmp(events_ent
->d_name
, ".")
576 || !strcmp(events_ent
->d_name
, "..")
577 || !strcmp(events_ent
->d_name
, "enable")
578 || !strcmp(events_ent
->d_name
, "header_event")
579 || !strcmp(events_ent
->d_name
, "header_page"))
582 if (!strglobmatch(events_ent
->d_name
, sys_name
))
585 ret
= add_tracepoint_event(list
, idx
, events_ent
->d_name
,
586 evt_name
, err
, head_config
);
589 closedir(events_dir
);
593 struct __add_bpf_event_param
{
594 struct parse_events_state
*parse_state
;
595 struct list_head
*list
;
596 struct list_head
*head_config
;
599 static int add_bpf_event(const char *group
, const char *event
, int fd
,
602 LIST_HEAD(new_evsels
);
603 struct __add_bpf_event_param
*param
= _param
;
604 struct parse_events_state
*parse_state
= param
->parse_state
;
605 struct list_head
*list
= param
->list
;
606 struct perf_evsel
*pos
;
609 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
612 err
= parse_events_add_tracepoint(&new_evsels
, &parse_state
->idx
, group
,
613 event
, parse_state
->error
,
616 struct perf_evsel
*evsel
, *tmp
;
618 pr_debug("Failed to add BPF event %s:%s\n",
620 list_for_each_entry_safe(evsel
, tmp
, &new_evsels
, node
) {
621 list_del(&evsel
->node
);
622 perf_evsel__delete(evsel
);
626 pr_debug("adding %s:%s\n", group
, event
);
628 list_for_each_entry(pos
, &new_evsels
, node
) {
629 pr_debug("adding %s:%s to %p\n",
633 list_splice(&new_evsels
, list
);
637 int parse_events_load_bpf_obj(struct parse_events_state
*parse_state
,
638 struct list_head
*list
,
639 struct bpf_object
*obj
,
640 struct list_head
*head_config
)
644 struct __add_bpf_event_param param
= {parse_state
, list
, head_config
};
645 static bool registered_unprobe_atexit
= false;
647 if (IS_ERR(obj
) || !obj
) {
648 snprintf(errbuf
, sizeof(errbuf
),
649 "Internal error: load bpf obj with NULL");
655 * Register atexit handler before calling bpf__probe() so
656 * bpf__probe() don't need to unprobe probe points its already
657 * created when failure.
659 if (!registered_unprobe_atexit
) {
661 registered_unprobe_atexit
= true;
664 err
= bpf__probe(obj
);
666 bpf__strerror_probe(obj
, err
, errbuf
, sizeof(errbuf
));
670 err
= bpf__load(obj
);
672 bpf__strerror_load(obj
, err
, errbuf
, sizeof(errbuf
));
676 err
= bpf__foreach_event(obj
, add_bpf_event
, ¶m
);
678 snprintf(errbuf
, sizeof(errbuf
),
679 "Attach events in BPF object failed");
685 parse_state
->error
->help
= strdup("(add -v to see detail)");
686 parse_state
->error
->str
= strdup(errbuf
);
691 parse_events_config_bpf(struct parse_events_state
*parse_state
,
692 struct bpf_object
*obj
,
693 struct list_head
*head_config
)
695 struct parse_events_term
*term
;
698 if (!head_config
|| list_empty(head_config
))
701 list_for_each_entry(term
, head_config
, list
) {
705 if (term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
) {
706 snprintf(errbuf
, sizeof(errbuf
),
707 "Invalid config term for BPF object");
708 errbuf
[BUFSIZ
- 1] = '\0';
710 parse_state
->error
->idx
= term
->err_term
;
711 parse_state
->error
->str
= strdup(errbuf
);
715 err
= bpf__config_obj(obj
, term
, parse_state
->evlist
, &error_pos
);
717 bpf__strerror_config_obj(obj
, term
, parse_state
->evlist
,
718 &error_pos
, err
, errbuf
,
720 parse_state
->error
->help
= strdup(
721 "Hint:\tValid config terms:\n"
722 " \tmap:[<arraymap>].value<indices>=[value]\n"
723 " \tmap:[<eventmap>].event<indices>=[event]\n"
725 " \twhere <indices> is something like [0,3...5] or [all]\n"
726 " \t(add -v to see detail)");
727 parse_state
->error
->str
= strdup(errbuf
);
728 if (err
== -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE
)
729 parse_state
->error
->idx
= term
->err_val
;
731 parse_state
->error
->idx
= term
->err_term
+ error_pos
;
739 * Split config terms:
740 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
741 * 'call-graph=fp' is 'evt config', should be applied to each
743 * 'map:array.value[0]=1' is 'obj config', should be processed
744 * with parse_events_config_bpf.
746 * Move object config terms from the first list to obj_head_config.
749 split_bpf_config_terms(struct list_head
*evt_head_config
,
750 struct list_head
*obj_head_config
)
752 struct parse_events_term
*term
, *temp
;
755 * Currectly, all possible user config term
756 * belong to bpf object. parse_events__is_hardcoded_term()
757 * happends to be a good flag.
759 * See parse_events_config_bpf() and
760 * config_term_tracepoint().
762 list_for_each_entry_safe(term
, temp
, evt_head_config
, list
)
763 if (!parse_events__is_hardcoded_term(term
))
764 list_move_tail(&term
->list
, obj_head_config
);
767 int parse_events_load_bpf(struct parse_events_state
*parse_state
,
768 struct list_head
*list
,
771 struct list_head
*head_config
)
774 struct bpf_object
*obj
;
775 LIST_HEAD(obj_head_config
);
778 split_bpf_config_terms(head_config
, &obj_head_config
);
780 obj
= bpf__prepare_load(bpf_file_name
, source
);
787 snprintf(errbuf
, sizeof(errbuf
),
788 "BPF support is not compiled");
790 bpf__strerror_prepare_load(bpf_file_name
,
795 parse_state
->error
->help
= strdup("(add -v to see detail)");
796 parse_state
->error
->str
= strdup(errbuf
);
800 err
= parse_events_load_bpf_obj(parse_state
, list
, obj
, head_config
);
803 err
= parse_events_config_bpf(parse_state
, obj
, &obj_head_config
);
806 * Caller doesn't know anything about obj_head_config,
807 * so combine them together again before returnning.
810 list_splice_tail(&obj_head_config
, head_config
);
815 parse_breakpoint_type(const char *type
, struct perf_event_attr
*attr
)
819 for (i
= 0; i
< 3; i
++) {
820 if (!type
|| !type
[i
])
823 #define CHECK_SET_TYPE(bit) \
825 if (attr->bp_type & bit) \
828 attr->bp_type |= bit; \
833 CHECK_SET_TYPE(HW_BREAKPOINT_R
);
836 CHECK_SET_TYPE(HW_BREAKPOINT_W
);
839 CHECK_SET_TYPE(HW_BREAKPOINT_X
);
846 #undef CHECK_SET_TYPE
848 if (!attr
->bp_type
) /* Default */
849 attr
->bp_type
= HW_BREAKPOINT_R
| HW_BREAKPOINT_W
;
854 int parse_events_add_breakpoint(struct list_head
*list
, int *idx
,
855 void *ptr
, char *type
, u64 len
)
857 struct perf_event_attr attr
;
859 memset(&attr
, 0, sizeof(attr
));
860 attr
.bp_addr
= (unsigned long) ptr
;
862 if (parse_breakpoint_type(type
, &attr
))
865 /* Provide some defaults if len is not specified */
867 if (attr
.bp_type
== HW_BREAKPOINT_X
)
870 len
= HW_BREAKPOINT_LEN_4
;
875 attr
.type
= PERF_TYPE_BREAKPOINT
;
876 attr
.sample_period
= 1;
878 return add_event(list
, idx
, &attr
, NULL
, NULL
);
881 static int check_type_val(struct parse_events_term
*term
,
882 struct parse_events_error
*err
,
885 if (type
== term
->type_val
)
889 err
->idx
= term
->err_val
;
890 if (type
== PARSE_EVENTS__TERM_TYPE_NUM
)
891 err
->str
= strdup("expected numeric value");
893 err
->str
= strdup("expected string value");
899 * Update according to parse-events.l
901 static const char *config_term_names
[__PARSE_EVENTS__TERM_TYPE_NR
] = {
902 [PARSE_EVENTS__TERM_TYPE_USER
] = "<sysfs term>",
903 [PARSE_EVENTS__TERM_TYPE_CONFIG
] = "config",
904 [PARSE_EVENTS__TERM_TYPE_CONFIG1
] = "config1",
905 [PARSE_EVENTS__TERM_TYPE_CONFIG2
] = "config2",
906 [PARSE_EVENTS__TERM_TYPE_NAME
] = "name",
907 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
] = "period",
908 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
] = "freq",
909 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
] = "branch_type",
910 [PARSE_EVENTS__TERM_TYPE_TIME
] = "time",
911 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH
] = "call-graph",
912 [PARSE_EVENTS__TERM_TYPE_STACKSIZE
] = "stack-size",
913 [PARSE_EVENTS__TERM_TYPE_NOINHERIT
] = "no-inherit",
914 [PARSE_EVENTS__TERM_TYPE_INHERIT
] = "inherit",
915 [PARSE_EVENTS__TERM_TYPE_MAX_STACK
] = "max-stack",
916 [PARSE_EVENTS__TERM_TYPE_OVERWRITE
] = "overwrite",
917 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
] = "no-overwrite",
918 [PARSE_EVENTS__TERM_TYPE_DRV_CFG
] = "driver-config",
921 static bool config_term_shrinked
;
924 config_term_avail(int term_type
, struct parse_events_error
*err
)
926 if (term_type
< 0 || term_type
>= __PARSE_EVENTS__TERM_TYPE_NR
) {
927 err
->str
= strdup("Invalid term_type");
930 if (!config_term_shrinked
)
934 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
935 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
936 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
937 case PARSE_EVENTS__TERM_TYPE_NAME
:
938 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
944 /* term_type is validated so indexing is safe */
945 if (asprintf(&err
->str
, "'%s' is not usable in 'perf stat'",
946 config_term_names
[term_type
]) < 0)
952 void parse_events__shrink_config_terms(void)
954 config_term_shrinked
= true;
957 static int config_term_common(struct perf_event_attr
*attr
,
958 struct parse_events_term
*term
,
959 struct parse_events_error
*err
)
961 #define CHECK_TYPE_VAL(type) \
963 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
967 switch (term
->type_term
) {
968 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
970 attr
->config
= term
->val
.num
;
972 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
974 attr
->config1
= term
->val
.num
;
976 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
978 attr
->config2
= term
->val
.num
;
980 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
983 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
986 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
988 if (strcmp(term
->val
.str
, "no") &&
989 parse_branch_str(term
->val
.str
, &attr
->branch_sample_type
)) {
990 err
->str
= strdup("invalid branch sample type");
991 err
->idx
= term
->err_val
;
995 case PARSE_EVENTS__TERM_TYPE_TIME
:
997 if (term
->val
.num
> 1) {
998 err
->str
= strdup("expected 0 or 1");
999 err
->idx
= term
->err_val
;
1003 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1004 CHECK_TYPE_VAL(STR
);
1006 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1007 CHECK_TYPE_VAL(NUM
);
1009 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1010 CHECK_TYPE_VAL(NUM
);
1012 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1013 CHECK_TYPE_VAL(NUM
);
1015 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1016 CHECK_TYPE_VAL(NUM
);
1018 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1019 CHECK_TYPE_VAL(NUM
);
1021 case PARSE_EVENTS__TERM_TYPE_NAME
:
1022 CHECK_TYPE_VAL(STR
);
1024 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1025 CHECK_TYPE_VAL(NUM
);
1028 err
->str
= strdup("unknown term");
1029 err
->idx
= term
->err_term
;
1030 err
->help
= parse_events_formats_error_string(NULL
);
1035 * Check term availbility after basic checking so
1036 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1038 * If check availbility at the entry of this function,
1039 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1040 * if an invalid config term is provided for legacy events
1041 * (for example, instructions/badterm/...), which is confusing.
1043 if (!config_term_avail(term
->type_term
, err
))
1046 #undef CHECK_TYPE_VAL
1049 static int config_term_pmu(struct perf_event_attr
*attr
,
1050 struct parse_events_term
*term
,
1051 struct parse_events_error
*err
)
1053 if (term
->type_term
== PARSE_EVENTS__TERM_TYPE_USER
||
1054 term
->type_term
== PARSE_EVENTS__TERM_TYPE_DRV_CFG
)
1056 * Always succeed for sysfs terms, as we dont know
1057 * at this point what type they need to have.
1061 return config_term_common(attr
, term
, err
);
1064 static int config_term_tracepoint(struct perf_event_attr
*attr
,
1065 struct parse_events_term
*term
,
1066 struct parse_events_error
*err
)
1068 switch (term
->type_term
) {
1069 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1070 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1071 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1072 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1073 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1074 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1075 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1076 return config_term_common(attr
, term
, err
);
1079 err
->idx
= term
->err_term
;
1080 err
->str
= strdup("unknown term");
1081 err
->help
= strdup("valid terms: call-graph,stack-size\n");
1089 static int config_attr(struct perf_event_attr
*attr
,
1090 struct list_head
*head
,
1091 struct parse_events_error
*err
,
1092 config_term_func_t config_term
)
1094 struct parse_events_term
*term
;
1096 list_for_each_entry(term
, head
, list
)
1097 if (config_term(attr
, term
, err
))
1103 static int get_config_terms(struct list_head
*head_config
,
1104 struct list_head
*head_terms __maybe_unused
)
1106 #define ADD_CONFIG_TERM(__type, __name, __val) \
1108 struct perf_evsel_config_term *__t; \
1110 __t = zalloc(sizeof(*__t)); \
1114 INIT_LIST_HEAD(&__t->list); \
1115 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1116 __t->val.__name = __val; \
1117 list_add_tail(&__t->list, head_terms); \
1120 struct parse_events_term
*term
;
1122 list_for_each_entry(term
, head_config
, list
) {
1123 switch (term
->type_term
) {
1124 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1125 ADD_CONFIG_TERM(PERIOD
, period
, term
->val
.num
);
1127 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1128 ADD_CONFIG_TERM(FREQ
, freq
, term
->val
.num
);
1130 case PARSE_EVENTS__TERM_TYPE_TIME
:
1131 ADD_CONFIG_TERM(TIME
, time
, term
->val
.num
);
1133 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1134 ADD_CONFIG_TERM(CALLGRAPH
, callgraph
, term
->val
.str
);
1136 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1137 ADD_CONFIG_TERM(BRANCH
, branch
, term
->val
.str
);
1139 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1140 ADD_CONFIG_TERM(STACK_USER
, stack_user
, term
->val
.num
);
1142 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1143 ADD_CONFIG_TERM(INHERIT
, inherit
, term
->val
.num
? 1 : 0);
1145 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1146 ADD_CONFIG_TERM(INHERIT
, inherit
, term
->val
.num
? 0 : 1);
1148 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1149 ADD_CONFIG_TERM(MAX_STACK
, max_stack
, term
->val
.num
);
1151 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1152 ADD_CONFIG_TERM(OVERWRITE
, overwrite
, term
->val
.num
? 1 : 0);
1154 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1155 ADD_CONFIG_TERM(OVERWRITE
, overwrite
, term
->val
.num
? 0 : 1);
1157 case PARSE_EVENTS__TERM_TYPE_DRV_CFG
:
1158 ADD_CONFIG_TERM(DRV_CFG
, drv_cfg
, term
->val
.str
);
1164 #undef ADD_EVSEL_CONFIG
1168 int parse_events_add_tracepoint(struct list_head
*list
, int *idx
,
1169 const char *sys
, const char *event
,
1170 struct parse_events_error
*err
,
1171 struct list_head
*head_config
)
1174 struct perf_event_attr attr
;
1176 if (config_attr(&attr
, head_config
, err
,
1177 config_term_tracepoint
))
1181 if (strpbrk(sys
, "*?"))
1182 return add_tracepoint_multi_sys(list
, idx
, sys
, event
,
1185 return add_tracepoint_event(list
, idx
, sys
, event
,
1189 int parse_events_add_numeric(struct parse_events_state
*parse_state
,
1190 struct list_head
*list
,
1191 u32 type
, u64 config
,
1192 struct list_head
*head_config
)
1194 struct perf_event_attr attr
;
1195 LIST_HEAD(config_terms
);
1197 memset(&attr
, 0, sizeof(attr
));
1199 attr
.config
= config
;
1202 if (config_attr(&attr
, head_config
, parse_state
->error
,
1203 config_term_common
))
1206 if (get_config_terms(head_config
, &config_terms
))
1210 return add_event(list
, &parse_state
->idx
, &attr
,
1211 get_config_name(head_config
), &config_terms
);
1214 static int __parse_events_add_pmu(struct parse_events_state
*parse_state
,
1215 struct list_head
*list
, char *name
,
1216 struct list_head
*head_config
, bool auto_merge_stats
)
1218 struct perf_event_attr attr
;
1219 struct perf_pmu_info info
;
1220 struct perf_pmu
*pmu
;
1221 struct perf_evsel
*evsel
;
1222 LIST_HEAD(config_terms
);
1224 pmu
= perf_pmu__find(name
);
1228 if (pmu
->default_config
) {
1229 memcpy(&attr
, pmu
->default_config
,
1230 sizeof(struct perf_event_attr
));
1232 memset(&attr
, 0, sizeof(attr
));
1236 attr
.type
= pmu
->type
;
1237 evsel
= __add_event(list
, &parse_state
->idx
, &attr
, NULL
, pmu
->cpus
, NULL
, auto_merge_stats
);
1238 return evsel
? 0 : -ENOMEM
;
1241 if (perf_pmu__check_alias(pmu
, head_config
, &info
))
1245 * Configure hardcoded terms first, no need to check
1246 * return value when called with fail == 0 ;)
1248 if (config_attr(&attr
, head_config
, parse_state
->error
, config_term_pmu
))
1251 if (get_config_terms(head_config
, &config_terms
))
1254 if (perf_pmu__config(pmu
, &attr
, head_config
, parse_state
->error
))
1257 evsel
= __add_event(list
, &parse_state
->idx
, &attr
,
1258 get_config_name(head_config
), pmu
->cpus
,
1259 &config_terms
, auto_merge_stats
);
1261 evsel
->unit
= info
.unit
;
1262 evsel
->scale
= info
.scale
;
1263 evsel
->per_pkg
= info
.per_pkg
;
1264 evsel
->snapshot
= info
.snapshot
;
1265 evsel
->metric_expr
= info
.metric_expr
;
1266 evsel
->metric_name
= info
.metric_name
;
1269 return evsel
? 0 : -ENOMEM
;
1272 int parse_events_add_pmu(struct parse_events_state
*parse_state
,
1273 struct list_head
*list
, char *name
,
1274 struct list_head
*head_config
)
1276 return __parse_events_add_pmu(parse_state
, list
, name
, head_config
, false);
1279 int parse_events_multi_pmu_add(struct parse_events_state
*parse_state
,
1280 char *str
, struct list_head
**listp
)
1282 struct list_head
*head
;
1283 struct parse_events_term
*term
;
1284 struct list_head
*list
;
1285 struct perf_pmu
*pmu
= NULL
;
1289 /* Add it for all PMUs that support the alias */
1290 list
= malloc(sizeof(struct list_head
));
1293 INIT_LIST_HEAD(list
);
1294 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1295 struct perf_pmu_alias
*alias
;
1297 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1298 if (!strcasecmp(alias
->name
, str
)) {
1299 head
= malloc(sizeof(struct list_head
));
1302 INIT_LIST_HEAD(head
);
1303 if (parse_events_term__num(&term
, PARSE_EVENTS__TERM_TYPE_USER
,
1304 str
, 1, false, &str
, NULL
) < 0)
1306 list_add_tail(&term
->list
, head
);
1308 if (!__parse_events_add_pmu(parse_state
, list
,
1309 pmu
->name
, head
, true)) {
1310 pr_debug("%s -> %s/%s/\n", str
,
1311 pmu
->name
, alias
->str
);
1315 parse_events_terms__delete(head
);
1325 int parse_events__modifier_group(struct list_head
*list
,
1328 return parse_events__modifier_event(list
, event_mod
, true);
1331 void parse_events__set_leader(char *name
, struct list_head
*list
)
1333 struct perf_evsel
*leader
;
1335 if (list_empty(list
)) {
1336 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1340 __perf_evlist__set_leader(list
);
1341 leader
= list_entry(list
->next
, struct perf_evsel
, node
);
1342 leader
->group_name
= name
? strdup(name
) : NULL
;
1345 /* list_event is assumed to point to malloc'ed memory */
1346 void parse_events_update_lists(struct list_head
*list_event
,
1347 struct list_head
*list_all
)
1350 * Called for single event definition. Update the
1351 * 'all event' list, and reinit the 'single event'
1352 * list, for next event definition.
1354 list_splice_tail(list_event
, list_all
);
1358 struct event_modifier
{
1373 static int get_event_modifier(struct event_modifier
*mod
, char *str
,
1374 struct perf_evsel
*evsel
)
1376 int eu
= evsel
? evsel
->attr
.exclude_user
: 0;
1377 int ek
= evsel
? evsel
->attr
.exclude_kernel
: 0;
1378 int eh
= evsel
? evsel
->attr
.exclude_hv
: 0;
1379 int eH
= evsel
? evsel
->attr
.exclude_host
: 0;
1380 int eG
= evsel
? evsel
->attr
.exclude_guest
: 0;
1381 int eI
= evsel
? evsel
->attr
.exclude_idle
: 0;
1382 int precise
= evsel
? evsel
->attr
.precise_ip
: 0;
1383 int precise_max
= 0;
1384 int sample_read
= 0;
1385 int pinned
= evsel
? evsel
->attr
.pinned
: 0;
1387 int exclude
= eu
| ek
| eh
;
1388 int exclude_GH
= evsel
? evsel
->exclude_GH
: 0;
1391 memset(mod
, 0, sizeof(*mod
));
1396 exclude
= eu
= ek
= eh
= 1;
1398 } else if (*str
== 'k') {
1400 exclude
= eu
= ek
= eh
= 1;
1402 } else if (*str
== 'h') {
1404 exclude
= eu
= ek
= eh
= 1;
1406 } else if (*str
== 'G') {
1408 exclude_GH
= eG
= eH
= 1;
1410 } else if (*str
== 'H') {
1412 exclude_GH
= eG
= eH
= 1;
1414 } else if (*str
== 'I') {
1416 } else if (*str
== 'p') {
1418 /* use of precise requires exclude_guest */
1421 } else if (*str
== 'P') {
1423 } else if (*str
== 'S') {
1425 } else if (*str
== 'D') {
1427 } else if (*str
== 'W') {
1438 * 0 - SAMPLE_IP can have arbitrary skid
1439 * 1 - SAMPLE_IP must have constant skid
1440 * 2 - SAMPLE_IP requested to have 0 skid
1441 * 3 - SAMPLE_IP must have 0 skid
1443 * See also PERF_RECORD_MISC_EXACT_IP
1454 mod
->precise
= precise
;
1455 mod
->precise_max
= precise_max
;
1456 mod
->exclude_GH
= exclude_GH
;
1457 mod
->sample_read
= sample_read
;
1458 mod
->pinned
= pinned
;
1465 * Basic modifier sanity check to validate it contains only one
1466 * instance of any modifier (apart from 'p') present.
1468 static int check_modifier(char *str
)
1472 /* The sizeof includes 0 byte as well. */
1473 if (strlen(str
) > (sizeof("ukhGHpppPSDIW") - 1))
1477 if (*p
!= 'p' && strchr(p
+ 1, *p
))
1485 int parse_events__modifier_event(struct list_head
*list
, char *str
, bool add
)
1487 struct perf_evsel
*evsel
;
1488 struct event_modifier mod
;
1493 if (check_modifier(str
))
1496 if (!add
&& get_event_modifier(&mod
, str
, NULL
))
1499 __evlist__for_each_entry(list
, evsel
) {
1500 if (add
&& get_event_modifier(&mod
, str
, evsel
))
1503 evsel
->attr
.exclude_user
= mod
.eu
;
1504 evsel
->attr
.exclude_kernel
= mod
.ek
;
1505 evsel
->attr
.exclude_hv
= mod
.eh
;
1506 evsel
->attr
.precise_ip
= mod
.precise
;
1507 evsel
->attr
.exclude_host
= mod
.eH
;
1508 evsel
->attr
.exclude_guest
= mod
.eG
;
1509 evsel
->attr
.exclude_idle
= mod
.eI
;
1510 evsel
->exclude_GH
= mod
.exclude_GH
;
1511 evsel
->sample_read
= mod
.sample_read
;
1512 evsel
->precise_max
= mod
.precise_max
;
1513 evsel
->weak_group
= mod
.weak
;
1515 if (perf_evsel__is_group_leader(evsel
))
1516 evsel
->attr
.pinned
= mod
.pinned
;
1522 int parse_events_name(struct list_head
*list
, char *name
)
1524 struct perf_evsel
*evsel
;
1526 __evlist__for_each_entry(list
, evsel
) {
1528 evsel
->name
= strdup(name
);
1535 comp_pmu(const void *p1
, const void *p2
)
1537 struct perf_pmu_event_symbol
*pmu1
= (struct perf_pmu_event_symbol
*) p1
;
1538 struct perf_pmu_event_symbol
*pmu2
= (struct perf_pmu_event_symbol
*) p2
;
1540 return strcasecmp(pmu1
->symbol
, pmu2
->symbol
);
1543 static void perf_pmu__parse_cleanup(void)
1545 if (perf_pmu_events_list_num
> 0) {
1546 struct perf_pmu_event_symbol
*p
;
1549 for (i
= 0; i
< perf_pmu_events_list_num
; i
++) {
1550 p
= perf_pmu_events_list
+ i
;
1553 zfree(&perf_pmu_events_list
);
1554 perf_pmu_events_list_num
= 0;
1558 #define SET_SYMBOL(str, stype) \
1567 * Read the pmu events list from sysfs
1568 * Save it into perf_pmu_events_list
1570 static void perf_pmu__parse_init(void)
1573 struct perf_pmu
*pmu
= NULL
;
1574 struct perf_pmu_alias
*alias
;
1578 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1579 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1580 if (strchr(alias
->name
, '-'))
1587 perf_pmu_events_list_num
= -1;
1590 perf_pmu_events_list
= malloc(sizeof(struct perf_pmu_event_symbol
) * len
);
1591 if (!perf_pmu_events_list
)
1593 perf_pmu_events_list_num
= len
;
1597 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1598 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1599 struct perf_pmu_event_symbol
*p
= perf_pmu_events_list
+ len
;
1600 char *tmp
= strchr(alias
->name
, '-');
1603 SET_SYMBOL(strndup(alias
->name
, tmp
- alias
->name
),
1604 PMU_EVENT_SYMBOL_PREFIX
);
1606 SET_SYMBOL(strdup(++tmp
), PMU_EVENT_SYMBOL_SUFFIX
);
1609 SET_SYMBOL(strdup(alias
->name
), PMU_EVENT_SYMBOL
);
1614 qsort(perf_pmu_events_list
, len
,
1615 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
1619 perf_pmu__parse_cleanup();
1622 enum perf_pmu_event_symbol_type
1623 perf_pmu__parse_check(const char *name
)
1625 struct perf_pmu_event_symbol p
, *r
;
1627 /* scan kernel pmu events from sysfs if needed */
1628 if (perf_pmu_events_list_num
== 0)
1629 perf_pmu__parse_init();
1631 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1632 * cpu-cycles has been handled by hardcode.
1633 * So it must be cpu// events, not kernel pmu event.
1635 if ((perf_pmu_events_list_num
<= 0) || !strcmp(name
, "cpu"))
1636 return PMU_EVENT_SYMBOL_ERR
;
1638 p
.symbol
= strdup(name
);
1639 r
= bsearch(&p
, perf_pmu_events_list
,
1640 (size_t) perf_pmu_events_list_num
,
1641 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
1643 return r
? r
->type
: PMU_EVENT_SYMBOL_ERR
;
1646 static int parse_events__scanner(const char *str
, void *parse_state
, int start_token
)
1648 YY_BUFFER_STATE buffer
;
1652 ret
= parse_events_lex_init_extra(start_token
, &scanner
);
1656 buffer
= parse_events__scan_string(str
, scanner
);
1659 parse_events_debug
= 1;
1661 ret
= parse_events_parse(parse_state
, scanner
);
1663 parse_events__flush_buffer(buffer
, scanner
);
1664 parse_events__delete_buffer(buffer
, scanner
);
1665 parse_events_lex_destroy(scanner
);
1670 * parse event config string, return a list of event terms.
1672 int parse_events_terms(struct list_head
*terms
, const char *str
)
1674 struct parse_events_state parse_state
= {
1679 ret
= parse_events__scanner(str
, &parse_state
, PE_START_TERMS
);
1681 list_splice(parse_state
.terms
, terms
);
1682 zfree(&parse_state
.terms
);
1686 parse_events_terms__delete(parse_state
.terms
);
1690 int parse_events(struct perf_evlist
*evlist
, const char *str
,
1691 struct parse_events_error
*err
)
1693 struct parse_events_state parse_state
= {
1694 .list
= LIST_HEAD_INIT(parse_state
.list
),
1695 .idx
= evlist
->nr_entries
,
1701 ret
= parse_events__scanner(str
, &parse_state
, PE_START_EVENTS
);
1702 perf_pmu__parse_cleanup();
1704 struct perf_evsel
*last
;
1706 if (list_empty(&parse_state
.list
)) {
1707 WARN_ONCE(true, "WARNING: event parser found nothing");
1711 perf_evlist__splice_list_tail(evlist
, &parse_state
.list
);
1712 evlist
->nr_groups
+= parse_state
.nr_groups
;
1713 last
= perf_evlist__last(evlist
);
1714 last
->cmdline_group_boundary
= true;
1720 * There are 2 users - builtin-record and builtin-test objects.
1721 * Both call perf_evlist__delete in case of error, so we dont
1727 #define MAX_WIDTH 1000
1728 static int get_term_width(void)
1732 get_term_dimensions(&ws
);
1733 return ws
.ws_col
> MAX_WIDTH
? MAX_WIDTH
: ws
.ws_col
;
1736 static void parse_events_print_error(struct parse_events_error
*err
,
1739 const char *str
= "invalid or unsupported event: ";
1740 char _buf
[MAX_WIDTH
];
1741 char *buf
= (char *) event
;
1745 /* -2 for extra '' in the final fprintf */
1746 int width
= get_term_width() - 2;
1747 int len_event
= strlen(event
);
1748 int len_str
, max_len
, cut
= 0;
1751 * Maximum error index indent, we will cut
1752 * the event string if it's bigger.
1754 int max_err_idx
= 13;
1757 * Let's be specific with the message when
1758 * we have the precise error.
1760 str
= "event syntax error: ";
1761 len_str
= strlen(str
);
1762 max_len
= width
- len_str
;
1766 /* We're cutting from the beginning. */
1767 if (err
->idx
> max_err_idx
)
1768 cut
= err
->idx
- max_err_idx
;
1770 strncpy(buf
, event
+ cut
, max_len
);
1772 /* Mark cut parts with '..' on both sides. */
1774 buf
[0] = buf
[1] = '.';
1776 if ((len_event
- cut
) > max_len
) {
1777 buf
[max_len
- 1] = buf
[max_len
- 2] = '.';
1781 idx
= len_str
+ err
->idx
- cut
;
1784 fprintf(stderr
, "%s'%s'\n", str
, buf
);
1786 fprintf(stderr
, "%*s\\___ %s\n", idx
+ 1, "", err
->str
);
1788 fprintf(stderr
, "\n%s\n", err
->help
);
1793 fprintf(stderr
, "Run 'perf list' for a list of valid events\n");
1798 int parse_events_option(const struct option
*opt
, const char *str
,
1799 int unset __maybe_unused
)
1801 struct perf_evlist
*evlist
= *(struct perf_evlist
**)opt
->value
;
1802 struct parse_events_error err
= { .idx
= 0, };
1803 int ret
= parse_events(evlist
, str
, &err
);
1806 parse_events_print_error(&err
, str
);
1812 foreach_evsel_in_last_glob(struct perf_evlist
*evlist
,
1813 int (*func
)(struct perf_evsel
*evsel
,
1817 struct perf_evsel
*last
= NULL
;
1821 * Don't return when list_empty, give func a chance to report
1822 * error when it found last == NULL.
1824 * So no need to WARN here, let *func do this.
1826 if (evlist
->nr_entries
> 0)
1827 last
= perf_evlist__last(evlist
);
1830 err
= (*func
)(last
, arg
);
1836 if (last
->node
.prev
== &evlist
->entries
)
1838 last
= list_entry(last
->node
.prev
, struct perf_evsel
, node
);
1839 } while (!last
->cmdline_group_boundary
);
1844 static int set_filter(struct perf_evsel
*evsel
, const void *arg
)
1846 const char *str
= arg
;
1848 int nr_addr_filters
= 0;
1849 struct perf_pmu
*pmu
= NULL
;
1854 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) {
1855 if (perf_evsel__append_tp_filter(evsel
, str
) < 0) {
1857 "not enough memory to hold filter string\n");
1864 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
)
1865 if (pmu
->type
== evsel
->attr
.type
) {
1871 perf_pmu__scan_file(pmu
, "nr_addr_filters",
1872 "%d", &nr_addr_filters
);
1874 if (!nr_addr_filters
)
1877 if (perf_evsel__append_addr_filter(evsel
, str
) < 0) {
1879 "not enough memory to hold filter string\n");
1887 "--filter option should follow a -e tracepoint or HW tracer option\n");
1892 int parse_filter(const struct option
*opt
, const char *str
,
1893 int unset __maybe_unused
)
1895 struct perf_evlist
*evlist
= *(struct perf_evlist
**)opt
->value
;
1897 return foreach_evsel_in_last_glob(evlist
, set_filter
,
1901 static int add_exclude_perf_filter(struct perf_evsel
*evsel
,
1902 const void *arg __maybe_unused
)
1904 char new_filter
[64];
1906 if (evsel
== NULL
|| evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
1908 "--exclude-perf option should follow a -e tracepoint option\n");
1912 snprintf(new_filter
, sizeof(new_filter
), "common_pid != %d", getpid());
1914 if (perf_evsel__append_tp_filter(evsel
, new_filter
) < 0) {
1916 "not enough memory to hold filter string\n");
1923 int exclude_perf(const struct option
*opt
,
1924 const char *arg __maybe_unused
,
1925 int unset __maybe_unused
)
1927 struct perf_evlist
*evlist
= *(struct perf_evlist
**)opt
->value
;
1929 return foreach_evsel_in_last_glob(evlist
, add_exclude_perf_filter
,
1933 static const char * const event_type_descriptors
[] = {
1937 "Hardware cache event",
1938 "Raw hardware event descriptor",
1939 "Hardware breakpoint",
1942 static int cmp_string(const void *a
, const void *b
)
1944 const char * const *as
= a
;
1945 const char * const *bs
= b
;
1947 return strcmp(*as
, *bs
);
1951 * Print the events from <debugfs_mount_point>/tracing/events
1954 void print_tracepoint_events(const char *subsys_glob
, const char *event_glob
,
1957 DIR *sys_dir
, *evt_dir
;
1958 struct dirent
*sys_dirent
, *evt_dirent
;
1959 char evt_path
[MAXPATHLEN
];
1960 char dir_path
[MAXPATHLEN
];
1961 char **evt_list
= NULL
;
1962 unsigned int evt_i
= 0, evt_num
= 0;
1963 bool evt_num_known
= false;
1966 sys_dir
= opendir(tracing_events_path
);
1970 if (evt_num_known
) {
1971 evt_list
= zalloc(sizeof(char *) * evt_num
);
1973 goto out_close_sys_dir
;
1976 for_each_subsystem(sys_dir
, sys_dirent
) {
1977 if (subsys_glob
!= NULL
&&
1978 !strglobmatch(sys_dirent
->d_name
, subsys_glob
))
1981 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", tracing_events_path
,
1982 sys_dirent
->d_name
);
1983 evt_dir
= opendir(dir_path
);
1987 for_each_event(sys_dirent
, evt_dir
, evt_dirent
) {
1988 if (event_glob
!= NULL
&&
1989 !strglobmatch(evt_dirent
->d_name
, event_glob
))
1992 if (!evt_num_known
) {
1997 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
1998 sys_dirent
->d_name
, evt_dirent
->d_name
);
2000 evt_list
[evt_i
] = strdup(evt_path
);
2001 if (evt_list
[evt_i
] == NULL
)
2002 goto out_close_evt_dir
;
2009 if (!evt_num_known
) {
2010 evt_num_known
= true;
2013 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2015 while (evt_i
< evt_num
) {
2017 printf("%s ", evt_list
[evt_i
++]);
2020 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2021 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2023 if (evt_num
&& pager_in_use())
2028 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2029 zfree(&evt_list
[evt_i
]);
2038 printf("FATAL: not enough memory to print %s\n",
2039 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2045 * Check whether event is in <debugfs_mount_point>/tracing/events
2048 int is_valid_tracepoint(const char *event_string
)
2050 DIR *sys_dir
, *evt_dir
;
2051 struct dirent
*sys_dirent
, *evt_dirent
;
2052 char evt_path
[MAXPATHLEN
];
2053 char dir_path
[MAXPATHLEN
];
2055 sys_dir
= opendir(tracing_events_path
);
2059 for_each_subsystem(sys_dir
, sys_dirent
) {
2061 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", tracing_events_path
,
2062 sys_dirent
->d_name
);
2063 evt_dir
= opendir(dir_path
);
2067 for_each_event(sys_dirent
, evt_dir
, evt_dirent
) {
2068 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2069 sys_dirent
->d_name
, evt_dirent
->d_name
);
2070 if (!strcmp(evt_path
, event_string
)) {
2082 static bool is_event_supported(u8 type
, unsigned config
)
2086 struct perf_evsel
*evsel
;
2087 struct perf_event_attr attr
= {
2092 struct thread_map
*tmap
= thread_map__new_by_tid(0);
2097 evsel
= perf_evsel__new(&attr
);
2099 open_return
= perf_evsel__open(evsel
, NULL
, tmap
);
2100 ret
= open_return
>= 0;
2102 if (open_return
== -EACCES
) {
2104 * This happens if the paranoid value
2105 * /proc/sys/kernel/perf_event_paranoid is set to 2
2106 * Re-run with exclude_kernel set; we don't do that
2107 * by default as some ARM machines do not support it.
2110 evsel
->attr
.exclude_kernel
= 1;
2111 ret
= perf_evsel__open(evsel
, NULL
, tmap
) >= 0;
2113 perf_evsel__delete(evsel
);
2119 void print_sdt_events(const char *subsys_glob
, const char *event_glob
,
2122 struct probe_cache
*pcache
;
2123 struct probe_cache_entry
*ent
;
2124 struct strlist
*bidlist
, *sdtlist
;
2125 struct strlist_config cfg
= {.dont_dupstr
= true};
2126 struct str_node
*nd
, *nd2
;
2127 char *buf
, *path
, *ptr
= NULL
;
2128 bool show_detail
= false;
2131 sdtlist
= strlist__new(NULL
, &cfg
);
2133 pr_debug("Failed to allocate new strlist for SDT\n");
2136 bidlist
= build_id_cache__list_all(true);
2138 pr_debug("Failed to get buildids: %d\n", errno
);
2141 strlist__for_each_entry(nd
, bidlist
) {
2142 pcache
= probe_cache__new(nd
->s
, NULL
);
2145 list_for_each_entry(ent
, &pcache
->entries
, node
) {
2149 !strglobmatch(ent
->pev
.group
, subsys_glob
))
2152 !strglobmatch(ent
->pev
.event
, event_glob
))
2154 ret
= asprintf(&buf
, "%s:%s@%s", ent
->pev
.group
,
2155 ent
->pev
.event
, nd
->s
);
2157 strlist__add(sdtlist
, buf
);
2159 probe_cache__delete(pcache
);
2161 strlist__delete(bidlist
);
2163 strlist__for_each_entry(nd
, sdtlist
) {
2164 buf
= strchr(nd
->s
, '@');
2168 printf("%s ", nd
->s
);
2171 nd2
= strlist__next(nd
);
2173 ptr
= strchr(nd2
->s
, '@');
2176 if (strcmp(nd
->s
, nd2
->s
) == 0)
2180 path
= build_id_cache__origname(buf
);
2181 ret
= asprintf(&buf
, "%s@%s(%.12s)", nd
->s
, path
, buf
);
2183 printf(" %-50s [%s]\n", buf
, "SDT event");
2187 printf(" %-50s [%s]\n", nd
->s
, "SDT event");
2189 if (strcmp(nd
->s
, nd2
->s
) != 0)
2190 show_detail
= false;
2195 strlist__delete(sdtlist
);
2198 int print_hwcache_events(const char *event_glob
, bool name_only
)
2200 unsigned int type
, op
, i
, evt_i
= 0, evt_num
= 0;
2202 char **evt_list
= NULL
;
2203 bool evt_num_known
= false;
2206 if (evt_num_known
) {
2207 evt_list
= zalloc(sizeof(char *) * evt_num
);
2212 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
2213 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
2214 /* skip invalid cache type */
2215 if (!perf_evsel__is_cache_op_valid(type
, op
))
2218 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
2219 __perf_evsel__hw_cache_type_op_res_name(type
, op
, i
,
2220 name
, sizeof(name
));
2221 if (event_glob
!= NULL
&& !strglobmatch(name
, event_glob
))
2224 if (!is_event_supported(PERF_TYPE_HW_CACHE
,
2225 type
| (op
<< 8) | (i
<< 16)))
2228 if (!evt_num_known
) {
2233 evt_list
[evt_i
] = strdup(name
);
2234 if (evt_list
[evt_i
] == NULL
)
2241 if (!evt_num_known
) {
2242 evt_num_known
= true;
2245 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2247 while (evt_i
< evt_num
) {
2249 printf("%s ", evt_list
[evt_i
++]);
2252 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2253 event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2255 if (evt_num
&& pager_in_use())
2260 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2261 zfree(&evt_list
[evt_i
]);
2266 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2272 void print_symbol_events(const char *event_glob
, unsigned type
,
2273 struct event_symbol
*syms
, unsigned max
,
2276 unsigned int i
, evt_i
= 0, evt_num
= 0;
2277 char name
[MAX_NAME_LEN
];
2278 char **evt_list
= NULL
;
2279 bool evt_num_known
= false;
2282 if (evt_num_known
) {
2283 evt_list
= zalloc(sizeof(char *) * evt_num
);
2289 for (i
= 0; i
< max
; i
++, syms
++) {
2291 if (event_glob
!= NULL
&& syms
->symbol
!= NULL
&&
2292 !(strglobmatch(syms
->symbol
, event_glob
) ||
2293 (syms
->alias
&& strglobmatch(syms
->alias
, event_glob
))))
2296 if (!is_event_supported(type
, i
))
2299 if (!evt_num_known
) {
2304 if (!name_only
&& strlen(syms
->alias
))
2305 snprintf(name
, MAX_NAME_LEN
, "%s OR %s", syms
->symbol
, syms
->alias
);
2307 strncpy(name
, syms
->symbol
, MAX_NAME_LEN
);
2309 evt_list
[evt_i
] = strdup(name
);
2310 if (evt_list
[evt_i
] == NULL
)
2315 if (!evt_num_known
) {
2316 evt_num_known
= true;
2319 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2321 while (evt_i
< evt_num
) {
2323 printf("%s ", evt_list
[evt_i
++]);
2326 printf(" %-50s [%s]\n", evt_list
[evt_i
++], event_type_descriptors
[type
]);
2328 if (evt_num
&& pager_in_use())
2333 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2334 zfree(&evt_list
[evt_i
]);
2339 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[type
]);
2345 * Print the help text for the event symbols:
2347 void print_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
2348 bool long_desc
, bool details_flag
)
2350 print_symbol_events(event_glob
, PERF_TYPE_HARDWARE
,
2351 event_symbols_hw
, PERF_COUNT_HW_MAX
, name_only
);
2353 print_symbol_events(event_glob
, PERF_TYPE_SOFTWARE
,
2354 event_symbols_sw
, PERF_COUNT_SW_MAX
, name_only
);
2356 print_hwcache_events(event_glob
, name_only
);
2358 print_pmu_events(event_glob
, name_only
, quiet_flag
, long_desc
,
2361 if (event_glob
!= NULL
)
2365 printf(" %-50s [%s]\n",
2367 event_type_descriptors
[PERF_TYPE_RAW
]);
2368 printf(" %-50s [%s]\n",
2369 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2370 event_type_descriptors
[PERF_TYPE_RAW
]);
2372 printf(" (see 'man perf-list' on how to encode it)\n\n");
2374 printf(" %-50s [%s]\n",
2375 "mem:<addr>[/len][:access]",
2376 event_type_descriptors
[PERF_TYPE_BREAKPOINT
]);
2381 print_tracepoint_events(NULL
, NULL
, name_only
);
2383 print_sdt_events(NULL
, NULL
, name_only
);
2385 metricgroup__print(true, true, NULL
, name_only
);
2388 int parse_events__is_hardcoded_term(struct parse_events_term
*term
)
2390 return term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
;
2393 static int new_term(struct parse_events_term
**_term
,
2394 struct parse_events_term
*temp
,
2397 struct parse_events_term
*term
;
2399 term
= malloc(sizeof(*term
));
2404 INIT_LIST_HEAD(&term
->list
);
2406 switch (term
->type_val
) {
2407 case PARSE_EVENTS__TERM_TYPE_NUM
:
2408 term
->val
.num
= num
;
2410 case PARSE_EVENTS__TERM_TYPE_STR
:
2411 term
->val
.str
= str
;
2422 int parse_events_term__num(struct parse_events_term
**term
,
2423 int type_term
, char *config
, u64 num
,
2425 void *loc_term_
, void *loc_val_
)
2427 YYLTYPE
*loc_term
= loc_term_
;
2428 YYLTYPE
*loc_val
= loc_val_
;
2430 struct parse_events_term temp
= {
2431 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
2432 .type_term
= type_term
,
2434 .no_value
= no_value
,
2435 .err_term
= loc_term
? loc_term
->first_column
: 0,
2436 .err_val
= loc_val
? loc_val
->first_column
: 0,
2439 return new_term(term
, &temp
, NULL
, num
);
2442 int parse_events_term__str(struct parse_events_term
**term
,
2443 int type_term
, char *config
, char *str
,
2444 void *loc_term_
, void *loc_val_
)
2446 YYLTYPE
*loc_term
= loc_term_
;
2447 YYLTYPE
*loc_val
= loc_val_
;
2449 struct parse_events_term temp
= {
2450 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
2451 .type_term
= type_term
,
2453 .err_term
= loc_term
? loc_term
->first_column
: 0,
2454 .err_val
= loc_val
? loc_val
->first_column
: 0,
2457 return new_term(term
, &temp
, str
, 0);
2460 int parse_events_term__sym_hw(struct parse_events_term
**term
,
2461 char *config
, unsigned idx
)
2463 struct event_symbol
*sym
;
2464 struct parse_events_term temp
= {
2465 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
2466 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
2467 .config
= config
?: (char *) "event",
2470 BUG_ON(idx
>= PERF_COUNT_HW_MAX
);
2471 sym
= &event_symbols_hw
[idx
];
2473 return new_term(term
, &temp
, (char *) sym
->symbol
, 0);
2476 int parse_events_term__clone(struct parse_events_term
**new,
2477 struct parse_events_term
*term
)
2479 struct parse_events_term temp
= {
2480 .type_val
= term
->type_val
,
2481 .type_term
= term
->type_term
,
2482 .config
= term
->config
,
2483 .err_term
= term
->err_term
,
2484 .err_val
= term
->err_val
,
2487 return new_term(new, &temp
, term
->val
.str
, term
->val
.num
);
2490 int parse_events_copy_term_list(struct list_head
*old
,
2491 struct list_head
**new)
2493 struct parse_events_term
*term
, *n
;
2501 *new = malloc(sizeof(struct list_head
));
2504 INIT_LIST_HEAD(*new);
2506 list_for_each_entry (term
, old
, list
) {
2507 ret
= parse_events_term__clone(&n
, term
);
2510 list_add_tail(&n
->list
, *new);
2515 void parse_events_terms__purge(struct list_head
*terms
)
2517 struct parse_events_term
*term
, *h
;
2519 list_for_each_entry_safe(term
, h
, terms
, list
) {
2520 if (term
->array
.nr_ranges
)
2521 zfree(&term
->array
.ranges
);
2522 list_del_init(&term
->list
);
2527 void parse_events_terms__delete(struct list_head
*terms
)
2531 parse_events_terms__purge(terms
);
2535 void parse_events__clear_array(struct parse_events_array
*a
)
2540 void parse_events_evlist_error(struct parse_events_state
*parse_state
,
2541 int idx
, const char *str
)
2543 struct parse_events_error
*err
= parse_state
->error
;
2548 err
->str
= strdup(str
);
2549 WARN_ONCE(!err
->str
, "WARNING: failed to allocate error string");
2552 static void config_terms_list(char *buf
, size_t buf_sz
)
2558 for (i
= 0; i
< __PARSE_EVENTS__TERM_TYPE_NR
; i
++) {
2559 const char *name
= config_term_names
[i
];
2561 if (!config_term_avail(i
, NULL
))
2568 if (strlen(buf
) + strlen(name
) + 2 >= buf_sz
)
2580 * Return string contains valid config terms of an event.
2581 * @additional_terms: For terms such as PMU sysfs terms.
2583 char *parse_events_formats_error_string(char *additional_terms
)
2586 /* "no-overwrite" is the longest name */
2587 char static_terms
[__PARSE_EVENTS__TERM_TYPE_NR
*
2588 (sizeof("no-overwrite") - 1)];
2590 config_terms_list(static_terms
, sizeof(static_terms
));
2592 if (additional_terms
) {
2593 if (asprintf(&str
, "valid terms: %s,%s",
2594 additional_terms
, static_terms
) < 0)
2597 if (asprintf(&str
, "valid terms: %s", static_terms
) < 0)