1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
12 #include <linux/bitops.h>
13 #include <api/fs/fs.h>
14 #include <api/fs/tracing_path.h>
15 #include <traceevent/event-parse.h>
16 #include <linux/hw_breakpoint.h>
17 #include <linux/perf_event.h>
18 #include <linux/compiler.h>
19 #include <linux/err.h>
20 #include <linux/zalloc.h>
21 #include <sys/ioctl.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
26 #include <perf/evsel.h>
28 #include "callchain.h"
34 #include <perf/cpumap.h>
35 #include "thread_map.h"
37 #include "perf_regs.h"
40 #include "trace-event.h"
45 #include "../perf-sys.h"
46 #include "util/parse-branch-options.h"
47 #include <internal/xyarray.h>
48 #include <internal/lib.h>
50 #include <linux/ctype.h>
52 struct perf_missing_features perf_missing_features;
54 static clockid_t clockid;
56 static int perf_evsel__no_extra_init(struct evsel *evsel __maybe_unused)
61 void __weak test_attr__ready(void) { }
63 static void perf_evsel__no_extra_fini(struct evsel *evsel __maybe_unused)
69 int (*init)(struct evsel *evsel);
70 void (*fini)(struct evsel *evsel);
71 } perf_evsel__object = {
72 .size = sizeof(struct evsel),
73 .init = perf_evsel__no_extra_init,
74 .fini = perf_evsel__no_extra_fini,
77 int perf_evsel__object_config(size_t object_size,
78 int (*init)(struct evsel *evsel),
79 void (*fini)(struct evsel *evsel))
85 if (perf_evsel__object.size > object_size)
88 perf_evsel__object.size = object_size;
92 perf_evsel__object.init = init;
95 perf_evsel__object.fini = fini;
100 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
102 int __perf_evsel__sample_size(u64 sample_type)
104 u64 mask = sample_type & PERF_SAMPLE_MASK;
108 for (i = 0; i < 64; i++) {
109 if (mask & (1ULL << i))
119 * __perf_evsel__calc_id_pos - calculate id_pos.
120 * @sample_type: sample type
122 * This function returns the position of the event id (PERF_SAMPLE_ID or
123 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
124 * perf_record_sample.
126 static int __perf_evsel__calc_id_pos(u64 sample_type)
130 if (sample_type & PERF_SAMPLE_IDENTIFIER)
133 if (!(sample_type & PERF_SAMPLE_ID))
136 if (sample_type & PERF_SAMPLE_IP)
139 if (sample_type & PERF_SAMPLE_TID)
142 if (sample_type & PERF_SAMPLE_TIME)
145 if (sample_type & PERF_SAMPLE_ADDR)
152 * __perf_evsel__calc_is_pos - calculate is_pos.
153 * @sample_type: sample type
155 * This function returns the position (counting backwards) of the event id
156 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
157 * sample_id_all is used there is an id sample appended to non-sample events.
159 static int __perf_evsel__calc_is_pos(u64 sample_type)
163 if (sample_type & PERF_SAMPLE_IDENTIFIER)
166 if (!(sample_type & PERF_SAMPLE_ID))
169 if (sample_type & PERF_SAMPLE_CPU)
172 if (sample_type & PERF_SAMPLE_STREAM_ID)
178 void perf_evsel__calc_id_pos(struct evsel *evsel)
180 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type);
181 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type);
184 void __perf_evsel__set_sample_bit(struct evsel *evsel,
185 enum perf_event_sample_format bit)
187 if (!(evsel->core.attr.sample_type & bit)) {
188 evsel->core.attr.sample_type |= bit;
189 evsel->sample_size += sizeof(u64);
190 perf_evsel__calc_id_pos(evsel);
194 void __perf_evsel__reset_sample_bit(struct evsel *evsel,
195 enum perf_event_sample_format bit)
197 if (evsel->core.attr.sample_type & bit) {
198 evsel->core.attr.sample_type &= ~bit;
199 evsel->sample_size -= sizeof(u64);
200 perf_evsel__calc_id_pos(evsel);
204 void perf_evsel__set_sample_id(struct evsel *evsel,
205 bool can_sample_identifier)
207 if (can_sample_identifier) {
208 perf_evsel__reset_sample_bit(evsel, ID);
209 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
211 perf_evsel__set_sample_bit(evsel, ID);
213 evsel->core.attr.read_format |= PERF_FORMAT_ID;
217 * perf_evsel__is_function_event - Return whether given evsel is a function
220 * @evsel - evsel selector to be tested
222 * Return %true if event is function trace event
224 bool perf_evsel__is_function_event(struct evsel *evsel)
226 #define FUNCTION_EVENT "ftrace:function"
228 return evsel->name &&
229 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
231 #undef FUNCTION_EVENT
234 void evsel__init(struct evsel *evsel,
235 struct perf_event_attr *attr, int idx)
237 perf_evsel__init(&evsel->core, attr);
239 evsel->tracking = !idx;
240 evsel->leader = evsel;
243 evsel->max_events = ULONG_MAX;
244 evsel->evlist = NULL;
245 evsel->bpf_obj = NULL;
247 INIT_LIST_HEAD(&evsel->config_terms);
248 perf_evsel__object.init(evsel);
249 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
250 perf_evsel__calc_id_pos(evsel);
251 evsel->cmdline_group_boundary = false;
252 evsel->metric_expr = NULL;
253 evsel->metric_name = NULL;
254 evsel->metric_events = NULL;
255 evsel->collect_stat = false;
256 evsel->pmu_name = NULL;
259 struct evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
261 struct evsel *evsel = zalloc(perf_evsel__object.size);
265 evsel__init(evsel, attr, idx);
267 if (perf_evsel__is_bpf_output(evsel)) {
268 evsel->core.attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
269 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
270 evsel->core.attr.sample_period = 1;
273 if (perf_evsel__is_clock(evsel)) {
275 * The evsel->unit points to static alias->unit
276 * so it's ok to use static string in here.
278 static const char *unit = "msec";
287 static bool perf_event_can_profile_kernel(void)
289 return perf_event_paranoid_check(1);
292 struct evsel *perf_evsel__new_cycles(bool precise)
294 struct perf_event_attr attr = {
295 .type = PERF_TYPE_HARDWARE,
296 .config = PERF_COUNT_HW_CPU_CYCLES,
297 .exclude_kernel = !perf_event_can_profile_kernel(),
301 event_attr_init(&attr);
307 * Now let the usual logic to set up the perf_event_attr defaults
308 * to kick in when we return and before perf_evsel__open() is called.
311 evsel = evsel__new(&attr);
315 evsel->precise_max = true;
317 /* use asprintf() because free(evsel) assumes name is allocated */
318 if (asprintf(&evsel->name, "cycles%s%s%.*s",
319 (attr.precise_ip || attr.exclude_kernel) ? ":" : "",
320 attr.exclude_kernel ? "u" : "",
321 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0)
326 evsel__delete(evsel);
332 * Returns pointer with encoded error via <linux/err.h> interface.
334 struct evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
336 struct evsel *evsel = zalloc(perf_evsel__object.size);
342 struct perf_event_attr attr = {
343 .type = PERF_TYPE_TRACEPOINT,
344 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
345 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
348 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
351 evsel->tp_format = trace_event__tp_format(sys, name);
352 if (IS_ERR(evsel->tp_format)) {
353 err = PTR_ERR(evsel->tp_format);
357 event_attr_init(&attr);
358 attr.config = evsel->tp_format->id;
359 attr.sample_period = 1;
360 evsel__init(evsel, &attr, idx);
372 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
380 "stalled-cycles-frontend",
381 "stalled-cycles-backend",
385 static const char *__perf_evsel__hw_name(u64 config)
387 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
388 return perf_evsel__hw_names[config];
390 return "unknown-hardware";
393 static int perf_evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size)
395 int colon = 0, r = 0;
396 struct perf_event_attr *attr = &evsel->core.attr;
397 bool exclude_guest_default = false;
399 #define MOD_PRINT(context, mod) do { \
400 if (!attr->exclude_##context) { \
401 if (!colon) colon = ++r; \
402 r += scnprintf(bf + r, size - r, "%c", mod); \
405 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
406 MOD_PRINT(kernel, 'k');
407 MOD_PRINT(user, 'u');
409 exclude_guest_default = true;
412 if (attr->precise_ip) {
415 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
416 exclude_guest_default = true;
419 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
420 MOD_PRINT(host, 'H');
421 MOD_PRINT(guest, 'G');
429 static int perf_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
431 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->core.attr.config));
432 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
435 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
448 static const char *__perf_evsel__sw_name(u64 config)
450 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
451 return perf_evsel__sw_names[config];
452 return "unknown-software";
455 static int perf_evsel__sw_name(struct evsel *evsel, char *bf, size_t size)
457 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->core.attr.config));
458 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
461 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
465 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
467 if (type & HW_BREAKPOINT_R)
468 r += scnprintf(bf + r, size - r, "r");
470 if (type & HW_BREAKPOINT_W)
471 r += scnprintf(bf + r, size - r, "w");
473 if (type & HW_BREAKPOINT_X)
474 r += scnprintf(bf + r, size - r, "x");
479 static int perf_evsel__bp_name(struct evsel *evsel, char *bf, size_t size)
481 struct perf_event_attr *attr = &evsel->core.attr;
482 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
483 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
486 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
487 [PERF_EVSEL__MAX_ALIASES] = {
488 { "L1-dcache", "l1-d", "l1d", "L1-data", },
489 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
491 { "dTLB", "d-tlb", "Data-TLB", },
492 { "iTLB", "i-tlb", "Instruction-TLB", },
493 { "branch", "branches", "bpu", "btb", "bpc", },
497 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
498 [PERF_EVSEL__MAX_ALIASES] = {
499 { "load", "loads", "read", },
500 { "store", "stores", "write", },
501 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
504 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
505 [PERF_EVSEL__MAX_ALIASES] = {
506 { "refs", "Reference", "ops", "access", },
507 { "misses", "miss", },
510 #define C(x) PERF_COUNT_HW_CACHE_##x
511 #define CACHE_READ (1 << C(OP_READ))
512 #define CACHE_WRITE (1 << C(OP_WRITE))
513 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
514 #define COP(x) (1 << x)
517 * cache operartion stat
518 * L1I : Read and prefetch only
519 * ITLB and BPU : Read-only
521 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
522 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
523 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
524 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
525 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
526 [C(ITLB)] = (CACHE_READ),
527 [C(BPU)] = (CACHE_READ),
528 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
531 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
533 if (perf_evsel__hw_cache_stat[type] & COP(op))
534 return true; /* valid */
536 return false; /* invalid */
539 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
540 char *bf, size_t size)
543 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
544 perf_evsel__hw_cache_op[op][0],
545 perf_evsel__hw_cache_result[result][0]);
548 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
549 perf_evsel__hw_cache_op[op][1]);
552 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
554 u8 op, result, type = (config >> 0) & 0xff;
555 const char *err = "unknown-ext-hardware-cache-type";
557 if (type >= PERF_COUNT_HW_CACHE_MAX)
560 op = (config >> 8) & 0xff;
561 err = "unknown-ext-hardware-cache-op";
562 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
565 result = (config >> 16) & 0xff;
566 err = "unknown-ext-hardware-cache-result";
567 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
570 err = "invalid-cache";
571 if (!perf_evsel__is_cache_op_valid(type, op))
574 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
576 return scnprintf(bf, size, "%s", err);
579 static int perf_evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size)
581 int ret = __perf_evsel__hw_cache_name(evsel->core.attr.config, bf, size);
582 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
585 static int perf_evsel__raw_name(struct evsel *evsel, char *bf, size_t size)
587 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config);
588 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
591 static int perf_evsel__tool_name(char *bf, size_t size)
593 int ret = scnprintf(bf, size, "duration_time");
597 const char *perf_evsel__name(struct evsel *evsel)
607 switch (evsel->core.attr.type) {
609 perf_evsel__raw_name(evsel, bf, sizeof(bf));
612 case PERF_TYPE_HARDWARE:
613 perf_evsel__hw_name(evsel, bf, sizeof(bf));
616 case PERF_TYPE_HW_CACHE:
617 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
620 case PERF_TYPE_SOFTWARE:
621 if (evsel->tool_event)
622 perf_evsel__tool_name(bf, sizeof(bf));
624 perf_evsel__sw_name(evsel, bf, sizeof(bf));
627 case PERF_TYPE_TRACEPOINT:
628 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
631 case PERF_TYPE_BREAKPOINT:
632 perf_evsel__bp_name(evsel, bf, sizeof(bf));
636 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
637 evsel->core.attr.type);
641 evsel->name = strdup(bf);
649 const char *perf_evsel__group_name(struct evsel *evsel)
651 return evsel->group_name ?: "anon group";
655 * Returns the group details for the specified leader,
656 * with following rules.
658 * For record -e '{cycles,instructions}'
659 * 'anon group { cycles:u, instructions:u }'
661 * For record -e 'cycles,instructions' and report --group
662 * 'cycles:u, instructions:u'
664 int perf_evsel__group_desc(struct evsel *evsel, char *buf, size_t size)
668 const char *group_name = perf_evsel__group_name(evsel);
670 if (!evsel->forced_leader)
671 ret = scnprintf(buf, size, "%s { ", group_name);
673 ret += scnprintf(buf + ret, size - ret, "%s",
674 perf_evsel__name(evsel));
676 for_each_group_member(pos, evsel)
677 ret += scnprintf(buf + ret, size - ret, ", %s",
678 perf_evsel__name(pos));
680 if (!evsel->forced_leader)
681 ret += scnprintf(buf + ret, size - ret, " }");
686 static void __perf_evsel__config_callchain(struct evsel *evsel,
687 struct record_opts *opts,
688 struct callchain_param *param)
690 bool function = perf_evsel__is_function_event(evsel);
691 struct perf_event_attr *attr = &evsel->core.attr;
693 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
695 attr->sample_max_stack = param->max_stack;
697 if (opts->kernel_callchains)
698 attr->exclude_callchain_user = 1;
699 if (opts->user_callchains)
700 attr->exclude_callchain_kernel = 1;
701 if (param->record_mode == CALLCHAIN_LBR) {
702 if (!opts->branch_stack) {
703 if (attr->exclude_user) {
704 pr_warning("LBR callstack option is only available "
705 "to get user callchain information. "
706 "Falling back to framepointers.\n");
708 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
709 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
710 PERF_SAMPLE_BRANCH_CALL_STACK |
711 PERF_SAMPLE_BRANCH_NO_CYCLES |
712 PERF_SAMPLE_BRANCH_NO_FLAGS;
715 pr_warning("Cannot use LBR callstack with branch stack. "
716 "Falling back to framepointers.\n");
719 if (param->record_mode == CALLCHAIN_DWARF) {
721 perf_evsel__set_sample_bit(evsel, REGS_USER);
722 perf_evsel__set_sample_bit(evsel, STACK_USER);
723 if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) {
724 attr->sample_regs_user |= DWARF_MINIMAL_REGS;
725 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, "
726 "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
727 "so the minimal registers set (IP, SP) is explicitly forced.\n");
729 attr->sample_regs_user |= PERF_REGS_MASK;
731 attr->sample_stack_user = param->dump_size;
732 attr->exclude_callchain_user = 1;
734 pr_info("Cannot use DWARF unwind for function trace event,"
735 " falling back to framepointers.\n");
740 pr_info("Disabling user space callchains for function trace event.\n");
741 attr->exclude_callchain_user = 1;
745 void perf_evsel__config_callchain(struct evsel *evsel,
746 struct record_opts *opts,
747 struct callchain_param *param)
750 return __perf_evsel__config_callchain(evsel, opts, param);
754 perf_evsel__reset_callgraph(struct evsel *evsel,
755 struct callchain_param *param)
757 struct perf_event_attr *attr = &evsel->core.attr;
759 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
760 if (param->record_mode == CALLCHAIN_LBR) {
761 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
762 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
763 PERF_SAMPLE_BRANCH_CALL_STACK);
765 if (param->record_mode == CALLCHAIN_DWARF) {
766 perf_evsel__reset_sample_bit(evsel, REGS_USER);
767 perf_evsel__reset_sample_bit(evsel, STACK_USER);
771 static void apply_config_terms(struct evsel *evsel,
772 struct record_opts *opts, bool track)
774 struct perf_evsel_config_term *term;
775 struct list_head *config_terms = &evsel->config_terms;
776 struct perf_event_attr *attr = &evsel->core.attr;
777 /* callgraph default */
778 struct callchain_param param = {
779 .record_mode = callchain_param.record_mode,
783 const char *callgraph_buf = NULL;
785 list_for_each_entry(term, config_terms, list) {
786 switch (term->type) {
787 case PERF_EVSEL__CONFIG_TERM_PERIOD:
788 if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
789 attr->sample_period = term->val.period;
791 perf_evsel__reset_sample_bit(evsel, PERIOD);
794 case PERF_EVSEL__CONFIG_TERM_FREQ:
795 if (!(term->weak && opts->user_freq != UINT_MAX)) {
796 attr->sample_freq = term->val.freq;
798 perf_evsel__set_sample_bit(evsel, PERIOD);
801 case PERF_EVSEL__CONFIG_TERM_TIME:
803 perf_evsel__set_sample_bit(evsel, TIME);
805 perf_evsel__reset_sample_bit(evsel, TIME);
807 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
808 callgraph_buf = term->val.callgraph;
810 case PERF_EVSEL__CONFIG_TERM_BRANCH:
811 if (term->val.branch && strcmp(term->val.branch, "no")) {
812 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
813 parse_branch_str(term->val.branch,
814 &attr->branch_sample_type);
816 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
818 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
819 dump_size = term->val.stack_user;
821 case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
822 max_stack = term->val.max_stack;
824 case PERF_EVSEL__CONFIG_TERM_MAX_EVENTS:
825 evsel->max_events = term->val.max_events;
827 case PERF_EVSEL__CONFIG_TERM_INHERIT:
829 * attr->inherit should has already been set by
830 * perf_evsel__config. If user explicitly set
831 * inherit using config terms, override global
832 * opt->no_inherit setting.
834 attr->inherit = term->val.inherit ? 1 : 0;
836 case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
837 attr->write_backward = term->val.overwrite ? 1 : 0;
839 case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
841 case PERF_EVSEL__CONFIG_TERM_PERCORE:
843 case PERF_EVSEL__CONFIG_TERM_AUX_OUTPUT:
844 attr->aux_output = term->val.aux_output ? 1 : 0;
851 /* User explicitly set per-event callgraph, clear the old setting and reset. */
852 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
853 bool sample_address = false;
856 param.max_stack = max_stack;
857 if (callgraph_buf == NULL)
858 callgraph_buf = "fp";
861 /* parse callgraph parameters */
862 if (callgraph_buf != NULL) {
863 if (!strcmp(callgraph_buf, "no")) {
864 param.enabled = false;
865 param.record_mode = CALLCHAIN_NONE;
867 param.enabled = true;
868 if (parse_callchain_record(callgraph_buf, ¶m)) {
869 pr_err("per-event callgraph setting for %s failed. "
870 "Apply callgraph global setting for it\n",
874 if (param.record_mode == CALLCHAIN_DWARF)
875 sample_address = true;
879 dump_size = round_up(dump_size, sizeof(u64));
880 param.dump_size = dump_size;
883 /* If global callgraph set, clear it */
884 if (callchain_param.enabled)
885 perf_evsel__reset_callgraph(evsel, &callchain_param);
887 /* set perf-event callgraph */
889 if (sample_address) {
890 perf_evsel__set_sample_bit(evsel, ADDR);
891 perf_evsel__set_sample_bit(evsel, DATA_SRC);
892 evsel->core.attr.mmap_data = track;
894 perf_evsel__config_callchain(evsel, opts, ¶m);
899 static bool is_dummy_event(struct evsel *evsel)
901 return (evsel->core.attr.type == PERF_TYPE_SOFTWARE) &&
902 (evsel->core.attr.config == PERF_COUNT_SW_DUMMY);
906 * The enable_on_exec/disabled value strategy:
908 * 1) For any type of traced program:
909 * - all independent events and group leaders are disabled
910 * - all group members are enabled
912 * Group members are ruled by group leaders. They need to
913 * be enabled, because the group scheduling relies on that.
915 * 2) For traced programs executed by perf:
916 * - all independent events and group leaders have
918 * - we don't specifically enable or disable any event during
921 * Independent events and group leaders are initially disabled
922 * and get enabled by exec. Group members are ruled by group
923 * leaders as stated in 1).
925 * 3) For traced programs attached by perf (pid/tid):
926 * - we specifically enable or disable all events during
929 * When attaching events to already running traced we
930 * enable/disable events specifically, as there's no
931 * initial traced exec call.
933 void perf_evsel__config(struct evsel *evsel, struct record_opts *opts,
934 struct callchain_param *callchain)
936 struct evsel *leader = evsel->leader;
937 struct perf_event_attr *attr = &evsel->core.attr;
938 int track = evsel->tracking;
939 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
941 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
942 attr->inherit = !opts->no_inherit;
943 attr->write_backward = opts->overwrite ? 1 : 0;
945 perf_evsel__set_sample_bit(evsel, IP);
946 perf_evsel__set_sample_bit(evsel, TID);
948 if (evsel->sample_read) {
949 perf_evsel__set_sample_bit(evsel, READ);
952 * We need ID even in case of single event, because
953 * PERF_SAMPLE_READ process ID specific data.
955 perf_evsel__set_sample_id(evsel, false);
958 * Apply group format only if we belong to group
959 * with more than one members.
961 if (leader->core.nr_members > 1) {
962 attr->read_format |= PERF_FORMAT_GROUP;
968 * We default some events to have a default interval. But keep
969 * it a weak assumption overridable by the user.
971 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
972 opts->user_interval != ULLONG_MAX)) {
974 perf_evsel__set_sample_bit(evsel, PERIOD);
976 attr->sample_freq = opts->freq;
978 attr->sample_period = opts->default_interval;
983 * Disable sampling for all group members other
984 * than leader in case leader 'leads' the sampling.
986 if ((leader != evsel) && leader->sample_read) {
988 attr->sample_freq = 0;
989 attr->sample_period = 0;
990 attr->write_backward = 0;
993 * We don't get sample for slave events, we make them
994 * when delivering group leader sample. Set the slave
995 * event to follow the master sample_type to ease up
998 attr->sample_type = leader->core.attr.sample_type;
1001 if (opts->no_samples)
1002 attr->sample_freq = 0;
1004 if (opts->inherit_stat) {
1005 evsel->core.attr.read_format |=
1006 PERF_FORMAT_TOTAL_TIME_ENABLED |
1007 PERF_FORMAT_TOTAL_TIME_RUNNING |
1009 attr->inherit_stat = 1;
1012 if (opts->sample_address) {
1013 perf_evsel__set_sample_bit(evsel, ADDR);
1014 attr->mmap_data = track;
1018 * We don't allow user space callchains for function trace
1019 * event, due to issues with page faults while tracing page
1020 * fault handler and its overall trickiness nature.
1022 if (perf_evsel__is_function_event(evsel))
1023 evsel->core.attr.exclude_callchain_user = 1;
1025 if (callchain && callchain->enabled && !evsel->no_aux_samples)
1026 perf_evsel__config_callchain(evsel, opts, callchain);
1028 if (opts->sample_intr_regs) {
1029 attr->sample_regs_intr = opts->sample_intr_regs;
1030 perf_evsel__set_sample_bit(evsel, REGS_INTR);
1033 if (opts->sample_user_regs) {
1034 attr->sample_regs_user |= opts->sample_user_regs;
1035 perf_evsel__set_sample_bit(evsel, REGS_USER);
1038 if (target__has_cpu(&opts->target) || opts->sample_cpu)
1039 perf_evsel__set_sample_bit(evsel, CPU);
1042 * When the user explicitly disabled time don't force it here.
1044 if (opts->sample_time &&
1045 (!perf_missing_features.sample_id_all &&
1046 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
1047 opts->sample_time_set)))
1048 perf_evsel__set_sample_bit(evsel, TIME);
1050 if (opts->raw_samples && !evsel->no_aux_samples) {
1051 perf_evsel__set_sample_bit(evsel, TIME);
1052 perf_evsel__set_sample_bit(evsel, RAW);
1053 perf_evsel__set_sample_bit(evsel, CPU);
1056 if (opts->sample_address)
1057 perf_evsel__set_sample_bit(evsel, DATA_SRC);
1059 if (opts->sample_phys_addr)
1060 perf_evsel__set_sample_bit(evsel, PHYS_ADDR);
1062 if (opts->no_buffering) {
1063 attr->watermark = 0;
1064 attr->wakeup_events = 1;
1066 if (opts->branch_stack && !evsel->no_aux_samples) {
1067 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
1068 attr->branch_sample_type = opts->branch_stack;
1071 if (opts->sample_weight)
1072 perf_evsel__set_sample_bit(evsel, WEIGHT);
1076 attr->mmap2 = track && !perf_missing_features.mmap2;
1078 attr->ksymbol = track && !perf_missing_features.ksymbol;
1079 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf;
1081 if (opts->record_namespaces)
1082 attr->namespaces = track;
1084 if (opts->record_switch_events)
1085 attr->context_switch = track;
1087 if (opts->sample_transaction)
1088 perf_evsel__set_sample_bit(evsel, TRANSACTION);
1090 if (opts->running_time) {
1091 evsel->core.attr.read_format |=
1092 PERF_FORMAT_TOTAL_TIME_ENABLED |
1093 PERF_FORMAT_TOTAL_TIME_RUNNING;
1097 * XXX see the function comment above
1099 * Disabling only independent events or group leaders,
1100 * keeping group members enabled.
1102 if (perf_evsel__is_group_leader(evsel))
1106 * Setting enable_on_exec for independent events and
1107 * group leaders for traced executed by perf.
1109 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
1110 !opts->initial_delay)
1111 attr->enable_on_exec = 1;
1113 if (evsel->immediate) {
1115 attr->enable_on_exec = 0;
1118 clockid = opts->clockid;
1119 if (opts->use_clockid) {
1120 attr->use_clockid = 1;
1121 attr->clockid = opts->clockid;
1124 if (evsel->precise_max)
1125 attr->precise_ip = 3;
1127 if (opts->all_user) {
1128 attr->exclude_kernel = 1;
1129 attr->exclude_user = 0;
1132 if (opts->all_kernel) {
1133 attr->exclude_kernel = 0;
1134 attr->exclude_user = 1;
1137 if (evsel->core.own_cpus || evsel->unit)
1138 evsel->core.attr.read_format |= PERF_FORMAT_ID;
1141 * Apply event specific term settings,
1142 * it overloads any global configuration.
1144 apply_config_terms(evsel, opts, track);
1146 evsel->ignore_missing_thread = opts->ignore_missing_thread;
1148 /* The --period option takes the precedence. */
1149 if (opts->period_set) {
1151 perf_evsel__set_sample_bit(evsel, PERIOD);
1153 perf_evsel__reset_sample_bit(evsel, PERIOD);
1157 * For initial_delay, a dummy event is added implicitly.
1158 * The software event will trigger -EOPNOTSUPP error out,
1159 * if BRANCH_STACK bit is set.
1161 if (opts->initial_delay && is_dummy_event(evsel))
1162 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
1165 int perf_evsel__set_filter(struct evsel *evsel, const char *filter)
1167 char *new_filter = strdup(filter);
1169 if (new_filter != NULL) {
1170 free(evsel->filter);
1171 evsel->filter = new_filter;
1178 static int perf_evsel__append_filter(struct evsel *evsel,
1179 const char *fmt, const char *filter)
1183 if (evsel->filter == NULL)
1184 return perf_evsel__set_filter(evsel, filter);
1186 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
1187 free(evsel->filter);
1188 evsel->filter = new_filter;
1195 int perf_evsel__append_tp_filter(struct evsel *evsel, const char *filter)
1197 return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
1200 int perf_evsel__append_addr_filter(struct evsel *evsel, const char *filter)
1202 return perf_evsel__append_filter(evsel, "%s,%s", filter);
1205 int evsel__enable(struct evsel *evsel)
1207 int err = perf_evsel__enable(&evsel->core);
1210 evsel->disabled = false;
1215 int evsel__disable(struct evsel *evsel)
1217 int err = perf_evsel__disable(&evsel->core);
1219 * We mark it disabled here so that tools that disable a event can
1220 * ignore events after they disable it. I.e. the ring buffer may have
1221 * already a few more events queued up before the kernel got the stop
1225 evsel->disabled = true;
1230 int perf_evsel__alloc_id(struct evsel *evsel, int ncpus, int nthreads)
1232 if (ncpus == 0 || nthreads == 0)
1235 if (evsel->system_wide)
1238 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1239 if (evsel->sample_id == NULL)
1242 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1243 if (evsel->id == NULL) {
1244 xyarray__delete(evsel->sample_id);
1245 evsel->sample_id = NULL;
1252 static void perf_evsel__free_id(struct evsel *evsel)
1254 xyarray__delete(evsel->sample_id);
1255 evsel->sample_id = NULL;
1260 static void perf_evsel__free_config_terms(struct evsel *evsel)
1262 struct perf_evsel_config_term *term, *h;
1264 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1265 list_del_init(&term->list);
1270 void perf_evsel__exit(struct evsel *evsel)
1272 assert(list_empty(&evsel->core.node));
1273 assert(evsel->evlist == NULL);
1274 perf_evsel__free_counts(evsel);
1275 perf_evsel__free_fd(&evsel->core);
1276 perf_evsel__free_id(evsel);
1277 perf_evsel__free_config_terms(evsel);
1278 cgroup__put(evsel->cgrp);
1279 perf_cpu_map__put(evsel->core.cpus);
1280 perf_cpu_map__put(evsel->core.own_cpus);
1281 perf_thread_map__put(evsel->core.threads);
1282 zfree(&evsel->group_name);
1283 zfree(&evsel->name);
1284 perf_evsel__object.fini(evsel);
1287 void evsel__delete(struct evsel *evsel)
1289 perf_evsel__exit(evsel);
1293 void perf_evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
1294 struct perf_counts_values *count)
1296 struct perf_counts_values tmp;
1298 if (!evsel->prev_raw_counts)
1302 tmp = evsel->prev_raw_counts->aggr;
1303 evsel->prev_raw_counts->aggr = *count;
1305 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1306 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1309 count->val = count->val - tmp.val;
1310 count->ena = count->ena - tmp.ena;
1311 count->run = count->run - tmp.run;
1314 void perf_counts_values__scale(struct perf_counts_values *count,
1315 bool scale, s8 *pscaled)
1320 if (count->run == 0) {
1323 } else if (count->run < count->ena) {
1325 count->val = (u64)((double) count->val * count->ena / count->run);
1334 perf_evsel__read_one(struct evsel *evsel, int cpu, int thread)
1336 struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread);
1338 return perf_evsel__read(&evsel->core, cpu, thread, count);
1342 perf_evsel__set_count(struct evsel *counter, int cpu, int thread,
1343 u64 val, u64 ena, u64 run)
1345 struct perf_counts_values *count;
1347 count = perf_counts(counter->counts, cpu, thread);
1353 perf_counts__set_loaded(counter->counts, cpu, thread, true);
1357 perf_evsel__process_group_data(struct evsel *leader,
1358 int cpu, int thread, u64 *data)
1360 u64 read_format = leader->core.attr.read_format;
1361 struct sample_read_value *v;
1362 u64 nr, ena = 0, run = 0, i;
1366 if (nr != (u64) leader->core.nr_members)
1369 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1372 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1375 v = (struct sample_read_value *) data;
1377 perf_evsel__set_count(leader, cpu, thread,
1378 v[0].value, ena, run);
1380 for (i = 1; i < nr; i++) {
1381 struct evsel *counter;
1383 counter = perf_evlist__id2evsel(leader->evlist, v[i].id);
1387 perf_evsel__set_count(counter, cpu, thread,
1388 v[i].value, ena, run);
1395 perf_evsel__read_group(struct evsel *leader, int cpu, int thread)
1397 struct perf_stat_evsel *ps = leader->stats;
1398 u64 read_format = leader->core.attr.read_format;
1399 int size = perf_evsel__read_size(&leader->core);
1400 u64 *data = ps->group_data;
1402 if (!(read_format & PERF_FORMAT_ID))
1405 if (!perf_evsel__is_group_leader(leader))
1409 data = zalloc(size);
1413 ps->group_data = data;
1416 if (FD(leader, cpu, thread) < 0)
1419 if (readn(FD(leader, cpu, thread), data, size) <= 0)
1422 return perf_evsel__process_group_data(leader, cpu, thread, data);
1425 int perf_evsel__read_counter(struct evsel *evsel, int cpu, int thread)
1427 u64 read_format = evsel->core.attr.read_format;
1429 if (read_format & PERF_FORMAT_GROUP)
1430 return perf_evsel__read_group(evsel, cpu, thread);
1432 return perf_evsel__read_one(evsel, cpu, thread);
1435 int __perf_evsel__read_on_cpu(struct evsel *evsel,
1436 int cpu, int thread, bool scale)
1438 struct perf_counts_values count;
1439 size_t nv = scale ? 3 : 1;
1441 if (FD(evsel, cpu, thread) < 0)
1444 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1447 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
1450 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1451 perf_counts_values__scale(&count, scale, NULL);
1452 *perf_counts(evsel->counts, cpu, thread) = count;
1456 static int get_group_fd(struct evsel *evsel, int cpu, int thread)
1458 struct evsel *leader = evsel->leader;
1461 if (perf_evsel__is_group_leader(evsel))
1465 * Leader must be already processed/open,
1466 * if not it's a bug.
1468 BUG_ON(!leader->core.fd);
1470 fd = FD(leader, cpu, thread);
1481 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1483 bool first_bit = true;
1487 if (value & bits[i].bit) {
1488 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1491 } while (bits[++i].name != NULL);
1494 static void __p_sample_type(char *buf, size_t size, u64 value)
1496 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1497 struct bit_names bits[] = {
1498 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1499 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1500 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1501 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1502 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1503 bit_name(WEIGHT), bit_name(PHYS_ADDR),
1507 __p_bits(buf, size, value, bits);
1510 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1512 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1513 struct bit_names bits[] = {
1514 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1515 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1516 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1517 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1518 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1522 __p_bits(buf, size, value, bits);
1525 static void __p_read_format(char *buf, size_t size, u64 value)
1527 #define bit_name(n) { PERF_FORMAT_##n, #n }
1528 struct bit_names bits[] = {
1529 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1530 bit_name(ID), bit_name(GROUP),
1534 __p_bits(buf, size, value, bits);
1537 #define BUF_SIZE 1024
1539 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1540 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1541 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1542 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1543 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1544 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1546 #define PRINT_ATTRn(_n, _f, _p) \
1550 ret += attr__fprintf(fp, _n, buf, priv);\
1554 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1556 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1557 attr__fprintf_f attr__fprintf, void *priv)
1562 PRINT_ATTRf(type, p_unsigned);
1563 PRINT_ATTRf(size, p_unsigned);
1564 PRINT_ATTRf(config, p_hex);
1565 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1566 PRINT_ATTRf(sample_type, p_sample_type);
1567 PRINT_ATTRf(read_format, p_read_format);
1569 PRINT_ATTRf(disabled, p_unsigned);
1570 PRINT_ATTRf(inherit, p_unsigned);
1571 PRINT_ATTRf(pinned, p_unsigned);
1572 PRINT_ATTRf(exclusive, p_unsigned);
1573 PRINT_ATTRf(exclude_user, p_unsigned);
1574 PRINT_ATTRf(exclude_kernel, p_unsigned);
1575 PRINT_ATTRf(exclude_hv, p_unsigned);
1576 PRINT_ATTRf(exclude_idle, p_unsigned);
1577 PRINT_ATTRf(mmap, p_unsigned);
1578 PRINT_ATTRf(comm, p_unsigned);
1579 PRINT_ATTRf(freq, p_unsigned);
1580 PRINT_ATTRf(inherit_stat, p_unsigned);
1581 PRINT_ATTRf(enable_on_exec, p_unsigned);
1582 PRINT_ATTRf(task, p_unsigned);
1583 PRINT_ATTRf(watermark, p_unsigned);
1584 PRINT_ATTRf(precise_ip, p_unsigned);
1585 PRINT_ATTRf(mmap_data, p_unsigned);
1586 PRINT_ATTRf(sample_id_all, p_unsigned);
1587 PRINT_ATTRf(exclude_host, p_unsigned);
1588 PRINT_ATTRf(exclude_guest, p_unsigned);
1589 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1590 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1591 PRINT_ATTRf(mmap2, p_unsigned);
1592 PRINT_ATTRf(comm_exec, p_unsigned);
1593 PRINT_ATTRf(use_clockid, p_unsigned);
1594 PRINT_ATTRf(context_switch, p_unsigned);
1595 PRINT_ATTRf(write_backward, p_unsigned);
1596 PRINT_ATTRf(namespaces, p_unsigned);
1597 PRINT_ATTRf(ksymbol, p_unsigned);
1598 PRINT_ATTRf(bpf_event, p_unsigned);
1599 PRINT_ATTRf(aux_output, p_unsigned);
1601 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1602 PRINT_ATTRf(bp_type, p_unsigned);
1603 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1604 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1605 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
1606 PRINT_ATTRf(sample_regs_user, p_hex);
1607 PRINT_ATTRf(sample_stack_user, p_unsigned);
1608 PRINT_ATTRf(clockid, p_signed);
1609 PRINT_ATTRf(sample_regs_intr, p_hex);
1610 PRINT_ATTRf(aux_watermark, p_unsigned);
1611 PRINT_ATTRf(sample_max_stack, p_unsigned);
1616 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1617 void *priv __maybe_unused)
1619 return fprintf(fp, " %-32s %s\n", name, val);
1622 static void perf_evsel__remove_fd(struct evsel *pos,
1623 int nr_cpus, int nr_threads,
1626 for (int cpu = 0; cpu < nr_cpus; cpu++)
1627 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1628 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1631 static int update_fds(struct evsel *evsel,
1632 int nr_cpus, int cpu_idx,
1633 int nr_threads, int thread_idx)
1637 if (cpu_idx >= nr_cpus || thread_idx >= nr_threads)
1640 evlist__for_each_entry(evsel->evlist, pos) {
1641 nr_cpus = pos != evsel ? nr_cpus : cpu_idx;
1643 perf_evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
1646 * Since fds for next evsel has not been created,
1647 * there is no need to iterate whole event list.
1655 static bool ignore_missing_thread(struct evsel *evsel,
1656 int nr_cpus, int cpu,
1657 struct perf_thread_map *threads,
1658 int thread, int err)
1660 pid_t ignore_pid = perf_thread_map__pid(threads, thread);
1662 if (!evsel->ignore_missing_thread)
1665 /* The system wide setup does not work with threads. */
1666 if (evsel->system_wide)
1669 /* The -ESRCH is perf event syscall errno for pid's not found. */
1673 /* If there's only one thread, let it fail. */
1674 if (threads->nr == 1)
1678 * We should remove fd for missing_thread first
1679 * because thread_map__remove() will decrease threads->nr.
1681 if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread))
1684 if (thread_map__remove(threads, thread))
1687 pr_warning("WARNING: Ignored open failure for pid %d\n",
1692 static void display_attr(struct perf_event_attr *attr)
1695 fprintf(stderr, "%.60s\n", graph_dotted_line);
1696 fprintf(stderr, "perf_event_attr:\n");
1697 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
1698 fprintf(stderr, "%.60s\n", graph_dotted_line);
1702 static int perf_event_open(struct evsel *evsel,
1703 pid_t pid, int cpu, int group_fd,
1704 unsigned long flags)
1706 int precise_ip = evsel->core.attr.precise_ip;
1710 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
1711 pid, cpu, group_fd, flags);
1713 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags);
1717 /* Do not try less precise if not requested. */
1718 if (!evsel->precise_max)
1722 * We tried all the precise_ip values, and it's
1723 * still failing, so leave it to standard fallback.
1725 if (!evsel->core.attr.precise_ip) {
1726 evsel->core.attr.precise_ip = precise_ip;
1730 pr_debug2("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
1731 evsel->core.attr.precise_ip--;
1732 pr_debug2("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
1733 display_attr(&evsel->core.attr);
1739 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
1740 struct perf_thread_map *threads)
1742 int cpu, thread, nthreads;
1743 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1745 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1747 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
1748 (perf_missing_features.aux_output && evsel->core.attr.aux_output))
1752 static struct perf_cpu_map *empty_cpu_map;
1754 if (empty_cpu_map == NULL) {
1755 empty_cpu_map = perf_cpu_map__dummy_new();
1756 if (empty_cpu_map == NULL)
1760 cpus = empty_cpu_map;
1763 if (threads == NULL) {
1764 static struct perf_thread_map *empty_thread_map;
1766 if (empty_thread_map == NULL) {
1767 empty_thread_map = thread_map__new_by_tid(-1);
1768 if (empty_thread_map == NULL)
1772 threads = empty_thread_map;
1775 if (evsel->system_wide)
1778 nthreads = threads->nr;
1780 if (evsel->core.fd == NULL &&
1781 perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
1785 flags |= PERF_FLAG_PID_CGROUP;
1786 pid = evsel->cgrp->fd;
1789 fallback_missing_features:
1790 if (perf_missing_features.clockid_wrong)
1791 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */
1792 if (perf_missing_features.clockid) {
1793 evsel->core.attr.use_clockid = 0;
1794 evsel->core.attr.clockid = 0;
1796 if (perf_missing_features.cloexec)
1797 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1798 if (perf_missing_features.mmap2)
1799 evsel->core.attr.mmap2 = 0;
1800 if (perf_missing_features.exclude_guest)
1801 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
1802 if (perf_missing_features.lbr_flags)
1803 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1804 PERF_SAMPLE_BRANCH_NO_CYCLES);
1805 if (perf_missing_features.group_read && evsel->core.attr.inherit)
1806 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
1807 if (perf_missing_features.ksymbol)
1808 evsel->core.attr.ksymbol = 0;
1809 if (perf_missing_features.bpf)
1810 evsel->core.attr.bpf_event = 0;
1812 if (perf_missing_features.sample_id_all)
1813 evsel->core.attr.sample_id_all = 0;
1815 display_attr(&evsel->core.attr);
1817 for (cpu = 0; cpu < cpus->nr; cpu++) {
1819 for (thread = 0; thread < nthreads; thread++) {
1822 if (!evsel->cgrp && !evsel->system_wide)
1823 pid = perf_thread_map__pid(threads, thread);
1825 group_fd = get_group_fd(evsel, cpu, thread);
1829 fd = perf_event_open(evsel, pid, cpus->map[cpu],
1832 FD(evsel, cpu, thread) = fd;
1837 if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
1839 * We just removed 1 thread, so take a step
1840 * back on thread index and lower the upper
1846 /* ... and pretend like nothing have happened. */
1851 pr_debug2("\nsys_perf_event_open failed, error %d\n",
1856 pr_debug2(" = %d\n", fd);
1858 if (evsel->bpf_fd >= 0) {
1860 int bpf_fd = evsel->bpf_fd;
1863 PERF_EVENT_IOC_SET_BPF,
1865 if (err && errno != EEXIST) {
1866 pr_err("failed to attach bpf fd %d: %s\n",
1867 bpf_fd, strerror(errno));
1873 set_rlimit = NO_CHANGE;
1876 * If we succeeded but had to kill clockid, fail and
1877 * have perf_evsel__open_strerror() print us a nice
1880 if (perf_missing_features.clockid ||
1881 perf_missing_features.clockid_wrong) {
1892 * perf stat needs between 5 and 22 fds per CPU. When we run out
1893 * of them try to increase the limits.
1895 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1897 int old_errno = errno;
1899 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1900 if (set_rlimit == NO_CHANGE)
1901 l.rlim_cur = l.rlim_max;
1903 l.rlim_cur = l.rlim_max + 1000;
1904 l.rlim_max = l.rlim_cur;
1906 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1915 if (err != -EINVAL || cpu > 0 || thread > 0)
1919 * Must probe features in the order they were added to the
1920 * perf_event_attr interface.
1922 if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
1923 perf_missing_features.aux_output = true;
1924 pr_debug2("Kernel has no attr.aux_output support, bailing out\n");
1926 } else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
1927 perf_missing_features.bpf = true;
1928 pr_debug2("switching off bpf_event\n");
1929 goto fallback_missing_features;
1930 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
1931 perf_missing_features.ksymbol = true;
1932 pr_debug2("switching off ksymbol\n");
1933 goto fallback_missing_features;
1934 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
1935 perf_missing_features.write_backward = true;
1936 pr_debug2("switching off write_backward\n");
1938 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
1939 perf_missing_features.clockid_wrong = true;
1940 pr_debug2("switching off clockid\n");
1941 goto fallback_missing_features;
1942 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
1943 perf_missing_features.clockid = true;
1944 pr_debug2("switching off use_clockid\n");
1945 goto fallback_missing_features;
1946 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1947 perf_missing_features.cloexec = true;
1948 pr_debug2("switching off cloexec flag\n");
1949 goto fallback_missing_features;
1950 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
1951 perf_missing_features.mmap2 = true;
1952 pr_debug2("switching off mmap2\n");
1953 goto fallback_missing_features;
1954 } else if (!perf_missing_features.exclude_guest &&
1955 (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
1956 perf_missing_features.exclude_guest = true;
1957 pr_debug2("switching off exclude_guest, exclude_host\n");
1958 goto fallback_missing_features;
1959 } else if (!perf_missing_features.sample_id_all) {
1960 perf_missing_features.sample_id_all = true;
1961 pr_debug2("switching off sample_id_all\n");
1962 goto retry_sample_id;
1963 } else if (!perf_missing_features.lbr_flags &&
1964 (evsel->core.attr.branch_sample_type &
1965 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1966 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1967 perf_missing_features.lbr_flags = true;
1968 pr_debug2("switching off branch sample type no (cycles/flags)\n");
1969 goto fallback_missing_features;
1970 } else if (!perf_missing_features.group_read &&
1971 evsel->core.attr.inherit &&
1972 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
1973 perf_evsel__is_group_leader(evsel)) {
1974 perf_missing_features.group_read = true;
1975 pr_debug2("switching off group read\n");
1976 goto fallback_missing_features;
1980 threads->err_thread = thread;
1983 while (--thread >= 0) {
1984 close(FD(evsel, cpu, thread));
1985 FD(evsel, cpu, thread) = -1;
1988 } while (--cpu >= 0);
1992 void evsel__close(struct evsel *evsel)
1994 perf_evsel__close(&evsel->core);
1995 perf_evsel__free_id(evsel);
1998 int perf_evsel__open_per_cpu(struct evsel *evsel,
1999 struct perf_cpu_map *cpus)
2001 return evsel__open(evsel, cpus, NULL);
2004 int perf_evsel__open_per_thread(struct evsel *evsel,
2005 struct perf_thread_map *threads)
2007 return evsel__open(evsel, NULL, threads);
2010 static int perf_evsel__parse_id_sample(const struct evsel *evsel,
2011 const union perf_event *event,
2012 struct perf_sample *sample)
2014 u64 type = evsel->core.attr.sample_type;
2015 const __u64 *array = event->sample.array;
2016 bool swapped = evsel->needs_swap;
2019 array += ((event->header.size -
2020 sizeof(event->header)) / sizeof(u64)) - 1;
2022 if (type & PERF_SAMPLE_IDENTIFIER) {
2023 sample->id = *array;
2027 if (type & PERF_SAMPLE_CPU) {
2030 /* undo swap of u64, then swap on individual u32s */
2031 u.val64 = bswap_64(u.val64);
2032 u.val32[0] = bswap_32(u.val32[0]);
2035 sample->cpu = u.val32[0];
2039 if (type & PERF_SAMPLE_STREAM_ID) {
2040 sample->stream_id = *array;
2044 if (type & PERF_SAMPLE_ID) {
2045 sample->id = *array;
2049 if (type & PERF_SAMPLE_TIME) {
2050 sample->time = *array;
2054 if (type & PERF_SAMPLE_TID) {
2057 /* undo swap of u64, then swap on individual u32s */
2058 u.val64 = bswap_64(u.val64);
2059 u.val32[0] = bswap_32(u.val32[0]);
2060 u.val32[1] = bswap_32(u.val32[1]);
2063 sample->pid = u.val32[0];
2064 sample->tid = u.val32[1];
2071 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2074 return size > max_size || offset + size > endp;
2077 #define OVERFLOW_CHECK(offset, size, max_size) \
2079 if (overflow(endp, (max_size), (offset), (size))) \
2083 #define OVERFLOW_CHECK_u64(offset) \
2084 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2087 perf_event__check_size(union perf_event *event, unsigned int sample_size)
2090 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2091 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2092 * check the format does not go past the end of the event.
2094 if (sample_size + sizeof(event->header) > event->header.size)
2100 int perf_evsel__parse_sample(struct evsel *evsel, union perf_event *event,
2101 struct perf_sample *data)
2103 u64 type = evsel->core.attr.sample_type;
2104 bool swapped = evsel->needs_swap;
2106 u16 max_size = event->header.size;
2107 const void *endp = (void *)event + max_size;
2111 * used for cross-endian analysis. See git commit 65014ab3
2112 * for why this goofiness is needed.
2116 memset(data, 0, sizeof(*data));
2117 data->cpu = data->pid = data->tid = -1;
2118 data->stream_id = data->id = data->time = -1ULL;
2119 data->period = evsel->core.attr.sample_period;
2120 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2121 data->misc = event->header.misc;
2123 data->data_src = PERF_MEM_DATA_SRC_NONE;
2125 if (event->header.type != PERF_RECORD_SAMPLE) {
2126 if (!evsel->core.attr.sample_id_all)
2128 return perf_evsel__parse_id_sample(evsel, event, data);
2131 array = event->sample.array;
2133 if (perf_event__check_size(event, evsel->sample_size))
2136 if (type & PERF_SAMPLE_IDENTIFIER) {
2141 if (type & PERF_SAMPLE_IP) {
2146 if (type & PERF_SAMPLE_TID) {
2149 /* undo swap of u64, then swap on individual u32s */
2150 u.val64 = bswap_64(u.val64);
2151 u.val32[0] = bswap_32(u.val32[0]);
2152 u.val32[1] = bswap_32(u.val32[1]);
2155 data->pid = u.val32[0];
2156 data->tid = u.val32[1];
2160 if (type & PERF_SAMPLE_TIME) {
2161 data->time = *array;
2165 if (type & PERF_SAMPLE_ADDR) {
2166 data->addr = *array;
2170 if (type & PERF_SAMPLE_ID) {
2175 if (type & PERF_SAMPLE_STREAM_ID) {
2176 data->stream_id = *array;
2180 if (type & PERF_SAMPLE_CPU) {
2184 /* undo swap of u64, then swap on individual u32s */
2185 u.val64 = bswap_64(u.val64);
2186 u.val32[0] = bswap_32(u.val32[0]);
2189 data->cpu = u.val32[0];
2193 if (type & PERF_SAMPLE_PERIOD) {
2194 data->period = *array;
2198 if (type & PERF_SAMPLE_READ) {
2199 u64 read_format = evsel->core.attr.read_format;
2201 OVERFLOW_CHECK_u64(array);
2202 if (read_format & PERF_FORMAT_GROUP)
2203 data->read.group.nr = *array;
2205 data->read.one.value = *array;
2209 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2210 OVERFLOW_CHECK_u64(array);
2211 data->read.time_enabled = *array;
2215 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2216 OVERFLOW_CHECK_u64(array);
2217 data->read.time_running = *array;
2221 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2222 if (read_format & PERF_FORMAT_GROUP) {
2223 const u64 max_group_nr = UINT64_MAX /
2224 sizeof(struct sample_read_value);
2226 if (data->read.group.nr > max_group_nr)
2228 sz = data->read.group.nr *
2229 sizeof(struct sample_read_value);
2230 OVERFLOW_CHECK(array, sz, max_size);
2231 data->read.group.values =
2232 (struct sample_read_value *)array;
2233 array = (void *)array + sz;
2235 OVERFLOW_CHECK_u64(array);
2236 data->read.one.id = *array;
2241 if (evsel__has_callchain(evsel)) {
2242 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
2244 OVERFLOW_CHECK_u64(array);
2245 data->callchain = (struct ip_callchain *)array++;
2246 if (data->callchain->nr > max_callchain_nr)
2248 sz = data->callchain->nr * sizeof(u64);
2249 OVERFLOW_CHECK(array, sz, max_size);
2250 array = (void *)array + sz;
2253 if (type & PERF_SAMPLE_RAW) {
2254 OVERFLOW_CHECK_u64(array);
2258 * Undo swap of u64, then swap on individual u32s,
2259 * get the size of the raw area and undo all of the
2260 * swap. The pevent interface handles endianity by
2264 u.val64 = bswap_64(u.val64);
2265 u.val32[0] = bswap_32(u.val32[0]);
2266 u.val32[1] = bswap_32(u.val32[1]);
2268 data->raw_size = u.val32[0];
2271 * The raw data is aligned on 64bits including the
2272 * u32 size, so it's safe to use mem_bswap_64.
2275 mem_bswap_64((void *) array, data->raw_size);
2277 array = (void *)array + sizeof(u32);
2279 OVERFLOW_CHECK(array, data->raw_size, max_size);
2280 data->raw_data = (void *)array;
2281 array = (void *)array + data->raw_size;
2284 if (type & PERF_SAMPLE_BRANCH_STACK) {
2285 const u64 max_branch_nr = UINT64_MAX /
2286 sizeof(struct branch_entry);
2288 OVERFLOW_CHECK_u64(array);
2289 data->branch_stack = (struct branch_stack *)array++;
2291 if (data->branch_stack->nr > max_branch_nr)
2293 sz = data->branch_stack->nr * sizeof(struct branch_entry);
2294 OVERFLOW_CHECK(array, sz, max_size);
2295 array = (void *)array + sz;
2298 if (type & PERF_SAMPLE_REGS_USER) {
2299 OVERFLOW_CHECK_u64(array);
2300 data->user_regs.abi = *array;
2303 if (data->user_regs.abi) {
2304 u64 mask = evsel->core.attr.sample_regs_user;
2306 sz = hweight64(mask) * sizeof(u64);
2307 OVERFLOW_CHECK(array, sz, max_size);
2308 data->user_regs.mask = mask;
2309 data->user_regs.regs = (u64 *)array;
2310 array = (void *)array + sz;
2314 if (type & PERF_SAMPLE_STACK_USER) {
2315 OVERFLOW_CHECK_u64(array);
2318 data->user_stack.offset = ((char *)(array - 1)
2322 data->user_stack.size = 0;
2324 OVERFLOW_CHECK(array, sz, max_size);
2325 data->user_stack.data = (char *)array;
2326 array = (void *)array + sz;
2327 OVERFLOW_CHECK_u64(array);
2328 data->user_stack.size = *array++;
2329 if (WARN_ONCE(data->user_stack.size > sz,
2330 "user stack dump failure\n"))
2335 if (type & PERF_SAMPLE_WEIGHT) {
2336 OVERFLOW_CHECK_u64(array);
2337 data->weight = *array;
2341 if (type & PERF_SAMPLE_DATA_SRC) {
2342 OVERFLOW_CHECK_u64(array);
2343 data->data_src = *array;
2347 if (type & PERF_SAMPLE_TRANSACTION) {
2348 OVERFLOW_CHECK_u64(array);
2349 data->transaction = *array;
2353 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2354 if (type & PERF_SAMPLE_REGS_INTR) {
2355 OVERFLOW_CHECK_u64(array);
2356 data->intr_regs.abi = *array;
2359 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2360 u64 mask = evsel->core.attr.sample_regs_intr;
2362 sz = hweight64(mask) * sizeof(u64);
2363 OVERFLOW_CHECK(array, sz, max_size);
2364 data->intr_regs.mask = mask;
2365 data->intr_regs.regs = (u64 *)array;
2366 array = (void *)array + sz;
2370 data->phys_addr = 0;
2371 if (type & PERF_SAMPLE_PHYS_ADDR) {
2372 data->phys_addr = *array;
2379 int perf_evsel__parse_sample_timestamp(struct evsel *evsel,
2380 union perf_event *event,
2383 u64 type = evsel->core.attr.sample_type;
2386 if (!(type & PERF_SAMPLE_TIME))
2389 if (event->header.type != PERF_RECORD_SAMPLE) {
2390 struct perf_sample data = {
2394 if (!evsel->core.attr.sample_id_all)
2396 if (perf_evsel__parse_id_sample(evsel, event, &data))
2399 *timestamp = data.time;
2403 array = event->sample.array;
2405 if (perf_event__check_size(event, evsel->sample_size))
2408 if (type & PERF_SAMPLE_IDENTIFIER)
2411 if (type & PERF_SAMPLE_IP)
2414 if (type & PERF_SAMPLE_TID)
2417 if (type & PERF_SAMPLE_TIME)
2418 *timestamp = *array;
2423 struct tep_format_field *perf_evsel__field(struct evsel *evsel, const char *name)
2425 return tep_find_field(evsel->tp_format, name);
2428 void *perf_evsel__rawptr(struct evsel *evsel, struct perf_sample *sample,
2431 struct tep_format_field *field = perf_evsel__field(evsel, name);
2437 offset = field->offset;
2439 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2440 offset = *(int *)(sample->raw_data + field->offset);
2444 return sample->raw_data + offset;
2447 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
2451 void *ptr = sample->raw_data + field->offset;
2453 switch (field->size) {
2457 value = *(u16 *)ptr;
2460 value = *(u32 *)ptr;
2463 memcpy(&value, ptr, sizeof(u64));
2472 switch (field->size) {
2474 return bswap_16(value);
2476 return bswap_32(value);
2478 return bswap_64(value);
2486 u64 perf_evsel__intval(struct evsel *evsel, struct perf_sample *sample,
2489 struct tep_format_field *field = perf_evsel__field(evsel, name);
2494 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2497 bool perf_evsel__fallback(struct evsel *evsel, int err,
2498 char *msg, size_t msgsize)
2502 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2503 evsel->core.attr.type == PERF_TYPE_HARDWARE &&
2504 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2506 * If it's cycles then fall back to hrtimer based
2507 * cpu-clock-tick sw counter, which is always available even if
2510 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2513 scnprintf(msg, msgsize, "%s",
2514 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2516 evsel->core.attr.type = PERF_TYPE_SOFTWARE;
2517 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK;
2519 zfree(&evsel->name);
2521 } else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
2522 (paranoid = perf_event_paranoid()) > 1) {
2523 const char *name = perf_evsel__name(evsel);
2525 const char *sep = ":";
2527 /* Is there already the separator in the name. */
2528 if (strchr(name, '/') ||
2532 if (asprintf(&new_name, "%s%su", name, sep) < 0)
2537 evsel->name = new_name;
2538 scnprintf(msg, msgsize,
2539 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2540 evsel->core.attr.exclude_kernel = 1;
2548 static bool find_process(const char *name)
2550 size_t len = strlen(name);
2555 dir = opendir(procfs__mountpoint());
2559 /* Walk through the directory. */
2560 while (ret && (d = readdir(dir)) != NULL) {
2561 char path[PATH_MAX];
2565 if ((d->d_type != DT_DIR) ||
2566 !strcmp(".", d->d_name) ||
2567 !strcmp("..", d->d_name))
2570 scnprintf(path, sizeof(path), "%s/%s/comm",
2571 procfs__mountpoint(), d->d_name);
2573 if (filename__read_str(path, &data, &size))
2576 ret = strncmp(name, data, len);
2581 return ret ? false : true;
2584 int perf_evsel__open_strerror(struct evsel *evsel, struct target *target,
2585 int err, char *msg, size_t size)
2587 char sbuf[STRERR_BUFSIZE];
2594 printed = scnprintf(msg, size,
2595 "No permission to enable %s event.\n\n",
2596 perf_evsel__name(evsel));
2598 return scnprintf(msg + printed, size - printed,
2599 "You may not have permission to collect %sstats.\n\n"
2600 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2601 "which controls use of the performance events system by\n"
2602 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2603 "The current value is %d:\n\n"
2604 " -1: Allow use of (almost) all events by all users\n"
2605 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
2606 ">= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN\n"
2607 " Disallow raw tracepoint access by users without CAP_SYS_ADMIN\n"
2608 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2609 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2610 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2611 " kernel.perf_event_paranoid = -1\n" ,
2612 target->system_wide ? "system-wide " : "",
2613 perf_event_paranoid());
2615 return scnprintf(msg, size, "The %s event is not supported.",
2616 perf_evsel__name(evsel));
2618 return scnprintf(msg, size, "%s",
2619 "Too many events are opened.\n"
2620 "Probably the maximum number of open file descriptors has been reached.\n"
2621 "Hint: Try again after reducing the number of events.\n"
2622 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2624 if (evsel__has_callchain(evsel) &&
2625 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2626 return scnprintf(msg, size,
2627 "Not enough memory to setup event with callchain.\n"
2628 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2629 "Hint: Current value: %d", sysctl__max_stack());
2632 if (target->cpu_list)
2633 return scnprintf(msg, size, "%s",
2634 "No such device - did you specify an out-of-range profile CPU?");
2637 if (evsel->core.attr.sample_period != 0)
2638 return scnprintf(msg, size,
2639 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
2640 perf_evsel__name(evsel));
2641 if (evsel->core.attr.precise_ip)
2642 return scnprintf(msg, size, "%s",
2643 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2644 #if defined(__i386__) || defined(__x86_64__)
2645 if (evsel->core.attr.type == PERF_TYPE_HARDWARE)
2646 return scnprintf(msg, size, "%s",
2647 "No hardware sampling interrupt available.\n");
2651 if (find_process("oprofiled"))
2652 return scnprintf(msg, size,
2653 "The PMU counters are busy/taken by another profiler.\n"
2654 "We found oprofile daemon running, please stop it and try again.");
2657 if (evsel->core.attr.write_backward && perf_missing_features.write_backward)
2658 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
2659 if (perf_missing_features.clockid)
2660 return scnprintf(msg, size, "clockid feature not supported.");
2661 if (perf_missing_features.clockid_wrong)
2662 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2663 if (perf_missing_features.aux_output)
2664 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel.");
2670 return scnprintf(msg, size,
2671 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2672 "/bin/dmesg | grep -i perf may provide additional information.\n",
2673 err, str_error_r(err, sbuf, sizeof(sbuf)),
2674 perf_evsel__name(evsel));
2677 struct perf_env *perf_evsel__env(struct evsel *evsel)
2679 if (evsel && evsel->evlist)
2680 return evsel->evlist->env;
2684 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
2688 for (cpu = 0; cpu < xyarray__max_x(evsel->core.fd); cpu++) {
2689 for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
2691 int fd = FD(evsel, cpu, thread);
2693 if (perf_evlist__id_add_fd(evlist, evsel,
2694 cpu, thread, fd) < 0)
2702 int perf_evsel__store_ids(struct evsel *evsel, struct evlist *evlist)
2704 struct perf_cpu_map *cpus = evsel->core.cpus;
2705 struct perf_thread_map *threads = evsel->core.threads;
2707 if (perf_evsel__alloc_id(evsel, cpus->nr, threads->nr))
2710 return store_evsel_ids(evsel, evlist);