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 static void perf_evsel__free_config_terms(struct evsel *evsel)
1232 struct perf_evsel_config_term *term, *h;
1234 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1235 list_del_init(&term->list);
1240 void perf_evsel__exit(struct evsel *evsel)
1242 assert(list_empty(&evsel->core.node));
1243 assert(evsel->evlist == NULL);
1244 perf_evsel__free_counts(evsel);
1245 perf_evsel__free_fd(&evsel->core);
1246 perf_evsel__free_id(&evsel->core);
1247 perf_evsel__free_config_terms(evsel);
1248 cgroup__put(evsel->cgrp);
1249 perf_cpu_map__put(evsel->core.cpus);
1250 perf_cpu_map__put(evsel->core.own_cpus);
1251 perf_thread_map__put(evsel->core.threads);
1252 zfree(&evsel->group_name);
1253 zfree(&evsel->name);
1254 perf_evsel__object.fini(evsel);
1257 void evsel__delete(struct evsel *evsel)
1259 perf_evsel__exit(evsel);
1263 void perf_evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
1264 struct perf_counts_values *count)
1266 struct perf_counts_values tmp;
1268 if (!evsel->prev_raw_counts)
1272 tmp = evsel->prev_raw_counts->aggr;
1273 evsel->prev_raw_counts->aggr = *count;
1275 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1276 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1279 count->val = count->val - tmp.val;
1280 count->ena = count->ena - tmp.ena;
1281 count->run = count->run - tmp.run;
1284 void perf_counts_values__scale(struct perf_counts_values *count,
1285 bool scale, s8 *pscaled)
1290 if (count->run == 0) {
1293 } else if (count->run < count->ena) {
1295 count->val = (u64)((double) count->val * count->ena / count->run);
1304 perf_evsel__read_one(struct evsel *evsel, int cpu, int thread)
1306 struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread);
1308 return perf_evsel__read(&evsel->core, cpu, thread, count);
1312 perf_evsel__set_count(struct evsel *counter, int cpu, int thread,
1313 u64 val, u64 ena, u64 run)
1315 struct perf_counts_values *count;
1317 count = perf_counts(counter->counts, cpu, thread);
1323 perf_counts__set_loaded(counter->counts, cpu, thread, true);
1327 perf_evsel__process_group_data(struct evsel *leader,
1328 int cpu, int thread, u64 *data)
1330 u64 read_format = leader->core.attr.read_format;
1331 struct sample_read_value *v;
1332 u64 nr, ena = 0, run = 0, i;
1336 if (nr != (u64) leader->core.nr_members)
1339 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1342 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1345 v = (struct sample_read_value *) data;
1347 perf_evsel__set_count(leader, cpu, thread,
1348 v[0].value, ena, run);
1350 for (i = 1; i < nr; i++) {
1351 struct evsel *counter;
1353 counter = perf_evlist__id2evsel(leader->evlist, v[i].id);
1357 perf_evsel__set_count(counter, cpu, thread,
1358 v[i].value, ena, run);
1365 perf_evsel__read_group(struct evsel *leader, int cpu, int thread)
1367 struct perf_stat_evsel *ps = leader->stats;
1368 u64 read_format = leader->core.attr.read_format;
1369 int size = perf_evsel__read_size(&leader->core);
1370 u64 *data = ps->group_data;
1372 if (!(read_format & PERF_FORMAT_ID))
1375 if (!perf_evsel__is_group_leader(leader))
1379 data = zalloc(size);
1383 ps->group_data = data;
1386 if (FD(leader, cpu, thread) < 0)
1389 if (readn(FD(leader, cpu, thread), data, size) <= 0)
1392 return perf_evsel__process_group_data(leader, cpu, thread, data);
1395 int perf_evsel__read_counter(struct evsel *evsel, int cpu, int thread)
1397 u64 read_format = evsel->core.attr.read_format;
1399 if (read_format & PERF_FORMAT_GROUP)
1400 return perf_evsel__read_group(evsel, cpu, thread);
1402 return perf_evsel__read_one(evsel, cpu, thread);
1405 int __perf_evsel__read_on_cpu(struct evsel *evsel,
1406 int cpu, int thread, bool scale)
1408 struct perf_counts_values count;
1409 size_t nv = scale ? 3 : 1;
1411 if (FD(evsel, cpu, thread) < 0)
1414 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1417 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
1420 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1421 perf_counts_values__scale(&count, scale, NULL);
1422 *perf_counts(evsel->counts, cpu, thread) = count;
1426 static int get_group_fd(struct evsel *evsel, int cpu, int thread)
1428 struct evsel *leader = evsel->leader;
1431 if (perf_evsel__is_group_leader(evsel))
1435 * Leader must be already processed/open,
1436 * if not it's a bug.
1438 BUG_ON(!leader->core.fd);
1440 fd = FD(leader, cpu, thread);
1451 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1453 bool first_bit = true;
1457 if (value & bits[i].bit) {
1458 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1461 } while (bits[++i].name != NULL);
1464 static void __p_sample_type(char *buf, size_t size, u64 value)
1466 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1467 struct bit_names bits[] = {
1468 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1469 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1470 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1471 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1472 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1473 bit_name(WEIGHT), bit_name(PHYS_ADDR),
1477 __p_bits(buf, size, value, bits);
1480 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1482 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1483 struct bit_names bits[] = {
1484 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1485 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1486 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1487 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1488 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1492 __p_bits(buf, size, value, bits);
1495 static void __p_read_format(char *buf, size_t size, u64 value)
1497 #define bit_name(n) { PERF_FORMAT_##n, #n }
1498 struct bit_names bits[] = {
1499 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1500 bit_name(ID), bit_name(GROUP),
1504 __p_bits(buf, size, value, bits);
1507 #define BUF_SIZE 1024
1509 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1510 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1511 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1512 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1513 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1514 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1516 #define PRINT_ATTRn(_n, _f, _p) \
1520 ret += attr__fprintf(fp, _n, buf, priv);\
1524 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1526 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1527 attr__fprintf_f attr__fprintf, void *priv)
1532 PRINT_ATTRf(type, p_unsigned);
1533 PRINT_ATTRf(size, p_unsigned);
1534 PRINT_ATTRf(config, p_hex);
1535 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1536 PRINT_ATTRf(sample_type, p_sample_type);
1537 PRINT_ATTRf(read_format, p_read_format);
1539 PRINT_ATTRf(disabled, p_unsigned);
1540 PRINT_ATTRf(inherit, p_unsigned);
1541 PRINT_ATTRf(pinned, p_unsigned);
1542 PRINT_ATTRf(exclusive, p_unsigned);
1543 PRINT_ATTRf(exclude_user, p_unsigned);
1544 PRINT_ATTRf(exclude_kernel, p_unsigned);
1545 PRINT_ATTRf(exclude_hv, p_unsigned);
1546 PRINT_ATTRf(exclude_idle, p_unsigned);
1547 PRINT_ATTRf(mmap, p_unsigned);
1548 PRINT_ATTRf(comm, p_unsigned);
1549 PRINT_ATTRf(freq, p_unsigned);
1550 PRINT_ATTRf(inherit_stat, p_unsigned);
1551 PRINT_ATTRf(enable_on_exec, p_unsigned);
1552 PRINT_ATTRf(task, p_unsigned);
1553 PRINT_ATTRf(watermark, p_unsigned);
1554 PRINT_ATTRf(precise_ip, p_unsigned);
1555 PRINT_ATTRf(mmap_data, p_unsigned);
1556 PRINT_ATTRf(sample_id_all, p_unsigned);
1557 PRINT_ATTRf(exclude_host, p_unsigned);
1558 PRINT_ATTRf(exclude_guest, p_unsigned);
1559 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1560 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1561 PRINT_ATTRf(mmap2, p_unsigned);
1562 PRINT_ATTRf(comm_exec, p_unsigned);
1563 PRINT_ATTRf(use_clockid, p_unsigned);
1564 PRINT_ATTRf(context_switch, p_unsigned);
1565 PRINT_ATTRf(write_backward, p_unsigned);
1566 PRINT_ATTRf(namespaces, p_unsigned);
1567 PRINT_ATTRf(ksymbol, p_unsigned);
1568 PRINT_ATTRf(bpf_event, p_unsigned);
1569 PRINT_ATTRf(aux_output, p_unsigned);
1571 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1572 PRINT_ATTRf(bp_type, p_unsigned);
1573 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1574 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1575 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
1576 PRINT_ATTRf(sample_regs_user, p_hex);
1577 PRINT_ATTRf(sample_stack_user, p_unsigned);
1578 PRINT_ATTRf(clockid, p_signed);
1579 PRINT_ATTRf(sample_regs_intr, p_hex);
1580 PRINT_ATTRf(aux_watermark, p_unsigned);
1581 PRINT_ATTRf(sample_max_stack, p_unsigned);
1586 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1587 void *priv __maybe_unused)
1589 return fprintf(fp, " %-32s %s\n", name, val);
1592 static void perf_evsel__remove_fd(struct evsel *pos,
1593 int nr_cpus, int nr_threads,
1596 for (int cpu = 0; cpu < nr_cpus; cpu++)
1597 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1598 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1601 static int update_fds(struct evsel *evsel,
1602 int nr_cpus, int cpu_idx,
1603 int nr_threads, int thread_idx)
1607 if (cpu_idx >= nr_cpus || thread_idx >= nr_threads)
1610 evlist__for_each_entry(evsel->evlist, pos) {
1611 nr_cpus = pos != evsel ? nr_cpus : cpu_idx;
1613 perf_evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
1616 * Since fds for next evsel has not been created,
1617 * there is no need to iterate whole event list.
1625 static bool ignore_missing_thread(struct evsel *evsel,
1626 int nr_cpus, int cpu,
1627 struct perf_thread_map *threads,
1628 int thread, int err)
1630 pid_t ignore_pid = perf_thread_map__pid(threads, thread);
1632 if (!evsel->ignore_missing_thread)
1635 /* The system wide setup does not work with threads. */
1636 if (evsel->core.system_wide)
1639 /* The -ESRCH is perf event syscall errno for pid's not found. */
1643 /* If there's only one thread, let it fail. */
1644 if (threads->nr == 1)
1648 * We should remove fd for missing_thread first
1649 * because thread_map__remove() will decrease threads->nr.
1651 if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread))
1654 if (thread_map__remove(threads, thread))
1657 pr_warning("WARNING: Ignored open failure for pid %d\n",
1662 static void display_attr(struct perf_event_attr *attr)
1665 fprintf(stderr, "%.60s\n", graph_dotted_line);
1666 fprintf(stderr, "perf_event_attr:\n");
1667 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
1668 fprintf(stderr, "%.60s\n", graph_dotted_line);
1672 static int perf_event_open(struct evsel *evsel,
1673 pid_t pid, int cpu, int group_fd,
1674 unsigned long flags)
1676 int precise_ip = evsel->core.attr.precise_ip;
1680 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
1681 pid, cpu, group_fd, flags);
1683 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags);
1687 /* Do not try less precise if not requested. */
1688 if (!evsel->precise_max)
1692 * We tried all the precise_ip values, and it's
1693 * still failing, so leave it to standard fallback.
1695 if (!evsel->core.attr.precise_ip) {
1696 evsel->core.attr.precise_ip = precise_ip;
1700 pr_debug2("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
1701 evsel->core.attr.precise_ip--;
1702 pr_debug2("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
1703 display_attr(&evsel->core.attr);
1709 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
1710 struct perf_thread_map *threads)
1712 int cpu, thread, nthreads;
1713 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1715 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1717 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
1718 (perf_missing_features.aux_output && evsel->core.attr.aux_output))
1722 static struct perf_cpu_map *empty_cpu_map;
1724 if (empty_cpu_map == NULL) {
1725 empty_cpu_map = perf_cpu_map__dummy_new();
1726 if (empty_cpu_map == NULL)
1730 cpus = empty_cpu_map;
1733 if (threads == NULL) {
1734 static struct perf_thread_map *empty_thread_map;
1736 if (empty_thread_map == NULL) {
1737 empty_thread_map = thread_map__new_by_tid(-1);
1738 if (empty_thread_map == NULL)
1742 threads = empty_thread_map;
1745 if (evsel->core.system_wide)
1748 nthreads = threads->nr;
1750 if (evsel->core.fd == NULL &&
1751 perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
1755 flags |= PERF_FLAG_PID_CGROUP;
1756 pid = evsel->cgrp->fd;
1759 fallback_missing_features:
1760 if (perf_missing_features.clockid_wrong)
1761 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */
1762 if (perf_missing_features.clockid) {
1763 evsel->core.attr.use_clockid = 0;
1764 evsel->core.attr.clockid = 0;
1766 if (perf_missing_features.cloexec)
1767 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1768 if (perf_missing_features.mmap2)
1769 evsel->core.attr.mmap2 = 0;
1770 if (perf_missing_features.exclude_guest)
1771 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
1772 if (perf_missing_features.lbr_flags)
1773 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1774 PERF_SAMPLE_BRANCH_NO_CYCLES);
1775 if (perf_missing_features.group_read && evsel->core.attr.inherit)
1776 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
1777 if (perf_missing_features.ksymbol)
1778 evsel->core.attr.ksymbol = 0;
1779 if (perf_missing_features.bpf)
1780 evsel->core.attr.bpf_event = 0;
1782 if (perf_missing_features.sample_id_all)
1783 evsel->core.attr.sample_id_all = 0;
1785 display_attr(&evsel->core.attr);
1787 for (cpu = 0; cpu < cpus->nr; cpu++) {
1789 for (thread = 0; thread < nthreads; thread++) {
1792 if (!evsel->cgrp && !evsel->core.system_wide)
1793 pid = perf_thread_map__pid(threads, thread);
1795 group_fd = get_group_fd(evsel, cpu, thread);
1799 fd = perf_event_open(evsel, pid, cpus->map[cpu],
1802 FD(evsel, cpu, thread) = fd;
1807 if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
1809 * We just removed 1 thread, so take a step
1810 * back on thread index and lower the upper
1816 /* ... and pretend like nothing have happened. */
1821 pr_debug2("\nsys_perf_event_open failed, error %d\n",
1826 pr_debug2(" = %d\n", fd);
1828 if (evsel->bpf_fd >= 0) {
1830 int bpf_fd = evsel->bpf_fd;
1833 PERF_EVENT_IOC_SET_BPF,
1835 if (err && errno != EEXIST) {
1836 pr_err("failed to attach bpf fd %d: %s\n",
1837 bpf_fd, strerror(errno));
1843 set_rlimit = NO_CHANGE;
1846 * If we succeeded but had to kill clockid, fail and
1847 * have perf_evsel__open_strerror() print us a nice
1850 if (perf_missing_features.clockid ||
1851 perf_missing_features.clockid_wrong) {
1862 * perf stat needs between 5 and 22 fds per CPU. When we run out
1863 * of them try to increase the limits.
1865 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1867 int old_errno = errno;
1869 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1870 if (set_rlimit == NO_CHANGE)
1871 l.rlim_cur = l.rlim_max;
1873 l.rlim_cur = l.rlim_max + 1000;
1874 l.rlim_max = l.rlim_cur;
1876 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1885 if (err != -EINVAL || cpu > 0 || thread > 0)
1889 * Must probe features in the order they were added to the
1890 * perf_event_attr interface.
1892 if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
1893 perf_missing_features.aux_output = true;
1894 pr_debug2("Kernel has no attr.aux_output support, bailing out\n");
1896 } else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
1897 perf_missing_features.bpf = true;
1898 pr_debug2("switching off bpf_event\n");
1899 goto fallback_missing_features;
1900 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
1901 perf_missing_features.ksymbol = true;
1902 pr_debug2("switching off ksymbol\n");
1903 goto fallback_missing_features;
1904 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
1905 perf_missing_features.write_backward = true;
1906 pr_debug2("switching off write_backward\n");
1908 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
1909 perf_missing_features.clockid_wrong = true;
1910 pr_debug2("switching off clockid\n");
1911 goto fallback_missing_features;
1912 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
1913 perf_missing_features.clockid = true;
1914 pr_debug2("switching off use_clockid\n");
1915 goto fallback_missing_features;
1916 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1917 perf_missing_features.cloexec = true;
1918 pr_debug2("switching off cloexec flag\n");
1919 goto fallback_missing_features;
1920 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
1921 perf_missing_features.mmap2 = true;
1922 pr_debug2("switching off mmap2\n");
1923 goto fallback_missing_features;
1924 } else if (!perf_missing_features.exclude_guest &&
1925 (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
1926 perf_missing_features.exclude_guest = true;
1927 pr_debug2("switching off exclude_guest, exclude_host\n");
1928 goto fallback_missing_features;
1929 } else if (!perf_missing_features.sample_id_all) {
1930 perf_missing_features.sample_id_all = true;
1931 pr_debug2("switching off sample_id_all\n");
1932 goto retry_sample_id;
1933 } else if (!perf_missing_features.lbr_flags &&
1934 (evsel->core.attr.branch_sample_type &
1935 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1936 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1937 perf_missing_features.lbr_flags = true;
1938 pr_debug2("switching off branch sample type no (cycles/flags)\n");
1939 goto fallback_missing_features;
1940 } else if (!perf_missing_features.group_read &&
1941 evsel->core.attr.inherit &&
1942 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
1943 perf_evsel__is_group_leader(evsel)) {
1944 perf_missing_features.group_read = true;
1945 pr_debug2("switching off group read\n");
1946 goto fallback_missing_features;
1950 threads->err_thread = thread;
1953 while (--thread >= 0) {
1954 close(FD(evsel, cpu, thread));
1955 FD(evsel, cpu, thread) = -1;
1958 } while (--cpu >= 0);
1962 void evsel__close(struct evsel *evsel)
1964 perf_evsel__close(&evsel->core);
1965 perf_evsel__free_id(&evsel->core);
1968 int perf_evsel__open_per_cpu(struct evsel *evsel,
1969 struct perf_cpu_map *cpus)
1971 return evsel__open(evsel, cpus, NULL);
1974 int perf_evsel__open_per_thread(struct evsel *evsel,
1975 struct perf_thread_map *threads)
1977 return evsel__open(evsel, NULL, threads);
1980 static int perf_evsel__parse_id_sample(const struct evsel *evsel,
1981 const union perf_event *event,
1982 struct perf_sample *sample)
1984 u64 type = evsel->core.attr.sample_type;
1985 const __u64 *array = event->sample.array;
1986 bool swapped = evsel->needs_swap;
1989 array += ((event->header.size -
1990 sizeof(event->header)) / sizeof(u64)) - 1;
1992 if (type & PERF_SAMPLE_IDENTIFIER) {
1993 sample->id = *array;
1997 if (type & PERF_SAMPLE_CPU) {
2000 /* undo swap of u64, then swap on individual u32s */
2001 u.val64 = bswap_64(u.val64);
2002 u.val32[0] = bswap_32(u.val32[0]);
2005 sample->cpu = u.val32[0];
2009 if (type & PERF_SAMPLE_STREAM_ID) {
2010 sample->stream_id = *array;
2014 if (type & PERF_SAMPLE_ID) {
2015 sample->id = *array;
2019 if (type & PERF_SAMPLE_TIME) {
2020 sample->time = *array;
2024 if (type & PERF_SAMPLE_TID) {
2027 /* undo swap of u64, then swap on individual u32s */
2028 u.val64 = bswap_64(u.val64);
2029 u.val32[0] = bswap_32(u.val32[0]);
2030 u.val32[1] = bswap_32(u.val32[1]);
2033 sample->pid = u.val32[0];
2034 sample->tid = u.val32[1];
2041 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2044 return size > max_size || offset + size > endp;
2047 #define OVERFLOW_CHECK(offset, size, max_size) \
2049 if (overflow(endp, (max_size), (offset), (size))) \
2053 #define OVERFLOW_CHECK_u64(offset) \
2054 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2057 perf_event__check_size(union perf_event *event, unsigned int sample_size)
2060 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2061 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2062 * check the format does not go past the end of the event.
2064 if (sample_size + sizeof(event->header) > event->header.size)
2070 int perf_evsel__parse_sample(struct evsel *evsel, union perf_event *event,
2071 struct perf_sample *data)
2073 u64 type = evsel->core.attr.sample_type;
2074 bool swapped = evsel->needs_swap;
2076 u16 max_size = event->header.size;
2077 const void *endp = (void *)event + max_size;
2081 * used for cross-endian analysis. See git commit 65014ab3
2082 * for why this goofiness is needed.
2086 memset(data, 0, sizeof(*data));
2087 data->cpu = data->pid = data->tid = -1;
2088 data->stream_id = data->id = data->time = -1ULL;
2089 data->period = evsel->core.attr.sample_period;
2090 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2091 data->misc = event->header.misc;
2093 data->data_src = PERF_MEM_DATA_SRC_NONE;
2095 if (event->header.type != PERF_RECORD_SAMPLE) {
2096 if (!evsel->core.attr.sample_id_all)
2098 return perf_evsel__parse_id_sample(evsel, event, data);
2101 array = event->sample.array;
2103 if (perf_event__check_size(event, evsel->sample_size))
2106 if (type & PERF_SAMPLE_IDENTIFIER) {
2111 if (type & PERF_SAMPLE_IP) {
2116 if (type & PERF_SAMPLE_TID) {
2119 /* undo swap of u64, then swap on individual u32s */
2120 u.val64 = bswap_64(u.val64);
2121 u.val32[0] = bswap_32(u.val32[0]);
2122 u.val32[1] = bswap_32(u.val32[1]);
2125 data->pid = u.val32[0];
2126 data->tid = u.val32[1];
2130 if (type & PERF_SAMPLE_TIME) {
2131 data->time = *array;
2135 if (type & PERF_SAMPLE_ADDR) {
2136 data->addr = *array;
2140 if (type & PERF_SAMPLE_ID) {
2145 if (type & PERF_SAMPLE_STREAM_ID) {
2146 data->stream_id = *array;
2150 if (type & PERF_SAMPLE_CPU) {
2154 /* undo swap of u64, then swap on individual u32s */
2155 u.val64 = bswap_64(u.val64);
2156 u.val32[0] = bswap_32(u.val32[0]);
2159 data->cpu = u.val32[0];
2163 if (type & PERF_SAMPLE_PERIOD) {
2164 data->period = *array;
2168 if (type & PERF_SAMPLE_READ) {
2169 u64 read_format = evsel->core.attr.read_format;
2171 OVERFLOW_CHECK_u64(array);
2172 if (read_format & PERF_FORMAT_GROUP)
2173 data->read.group.nr = *array;
2175 data->read.one.value = *array;
2179 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2180 OVERFLOW_CHECK_u64(array);
2181 data->read.time_enabled = *array;
2185 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2186 OVERFLOW_CHECK_u64(array);
2187 data->read.time_running = *array;
2191 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2192 if (read_format & PERF_FORMAT_GROUP) {
2193 const u64 max_group_nr = UINT64_MAX /
2194 sizeof(struct sample_read_value);
2196 if (data->read.group.nr > max_group_nr)
2198 sz = data->read.group.nr *
2199 sizeof(struct sample_read_value);
2200 OVERFLOW_CHECK(array, sz, max_size);
2201 data->read.group.values =
2202 (struct sample_read_value *)array;
2203 array = (void *)array + sz;
2205 OVERFLOW_CHECK_u64(array);
2206 data->read.one.id = *array;
2211 if (evsel__has_callchain(evsel)) {
2212 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
2214 OVERFLOW_CHECK_u64(array);
2215 data->callchain = (struct ip_callchain *)array++;
2216 if (data->callchain->nr > max_callchain_nr)
2218 sz = data->callchain->nr * sizeof(u64);
2219 OVERFLOW_CHECK(array, sz, max_size);
2220 array = (void *)array + sz;
2223 if (type & PERF_SAMPLE_RAW) {
2224 OVERFLOW_CHECK_u64(array);
2228 * Undo swap of u64, then swap on individual u32s,
2229 * get the size of the raw area and undo all of the
2230 * swap. The pevent interface handles endianity by
2234 u.val64 = bswap_64(u.val64);
2235 u.val32[0] = bswap_32(u.val32[0]);
2236 u.val32[1] = bswap_32(u.val32[1]);
2238 data->raw_size = u.val32[0];
2241 * The raw data is aligned on 64bits including the
2242 * u32 size, so it's safe to use mem_bswap_64.
2245 mem_bswap_64((void *) array, data->raw_size);
2247 array = (void *)array + sizeof(u32);
2249 OVERFLOW_CHECK(array, data->raw_size, max_size);
2250 data->raw_data = (void *)array;
2251 array = (void *)array + data->raw_size;
2254 if (type & PERF_SAMPLE_BRANCH_STACK) {
2255 const u64 max_branch_nr = UINT64_MAX /
2256 sizeof(struct branch_entry);
2258 OVERFLOW_CHECK_u64(array);
2259 data->branch_stack = (struct branch_stack *)array++;
2261 if (data->branch_stack->nr > max_branch_nr)
2263 sz = data->branch_stack->nr * sizeof(struct branch_entry);
2264 OVERFLOW_CHECK(array, sz, max_size);
2265 array = (void *)array + sz;
2268 if (type & PERF_SAMPLE_REGS_USER) {
2269 OVERFLOW_CHECK_u64(array);
2270 data->user_regs.abi = *array;
2273 if (data->user_regs.abi) {
2274 u64 mask = evsel->core.attr.sample_regs_user;
2276 sz = hweight64(mask) * sizeof(u64);
2277 OVERFLOW_CHECK(array, sz, max_size);
2278 data->user_regs.mask = mask;
2279 data->user_regs.regs = (u64 *)array;
2280 array = (void *)array + sz;
2284 if (type & PERF_SAMPLE_STACK_USER) {
2285 OVERFLOW_CHECK_u64(array);
2288 data->user_stack.offset = ((char *)(array - 1)
2292 data->user_stack.size = 0;
2294 OVERFLOW_CHECK(array, sz, max_size);
2295 data->user_stack.data = (char *)array;
2296 array = (void *)array + sz;
2297 OVERFLOW_CHECK_u64(array);
2298 data->user_stack.size = *array++;
2299 if (WARN_ONCE(data->user_stack.size > sz,
2300 "user stack dump failure\n"))
2305 if (type & PERF_SAMPLE_WEIGHT) {
2306 OVERFLOW_CHECK_u64(array);
2307 data->weight = *array;
2311 if (type & PERF_SAMPLE_DATA_SRC) {
2312 OVERFLOW_CHECK_u64(array);
2313 data->data_src = *array;
2317 if (type & PERF_SAMPLE_TRANSACTION) {
2318 OVERFLOW_CHECK_u64(array);
2319 data->transaction = *array;
2323 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2324 if (type & PERF_SAMPLE_REGS_INTR) {
2325 OVERFLOW_CHECK_u64(array);
2326 data->intr_regs.abi = *array;
2329 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2330 u64 mask = evsel->core.attr.sample_regs_intr;
2332 sz = hweight64(mask) * sizeof(u64);
2333 OVERFLOW_CHECK(array, sz, max_size);
2334 data->intr_regs.mask = mask;
2335 data->intr_regs.regs = (u64 *)array;
2336 array = (void *)array + sz;
2340 data->phys_addr = 0;
2341 if (type & PERF_SAMPLE_PHYS_ADDR) {
2342 data->phys_addr = *array;
2349 int perf_evsel__parse_sample_timestamp(struct evsel *evsel,
2350 union perf_event *event,
2353 u64 type = evsel->core.attr.sample_type;
2356 if (!(type & PERF_SAMPLE_TIME))
2359 if (event->header.type != PERF_RECORD_SAMPLE) {
2360 struct perf_sample data = {
2364 if (!evsel->core.attr.sample_id_all)
2366 if (perf_evsel__parse_id_sample(evsel, event, &data))
2369 *timestamp = data.time;
2373 array = event->sample.array;
2375 if (perf_event__check_size(event, evsel->sample_size))
2378 if (type & PERF_SAMPLE_IDENTIFIER)
2381 if (type & PERF_SAMPLE_IP)
2384 if (type & PERF_SAMPLE_TID)
2387 if (type & PERF_SAMPLE_TIME)
2388 *timestamp = *array;
2393 struct tep_format_field *perf_evsel__field(struct evsel *evsel, const char *name)
2395 return tep_find_field(evsel->tp_format, name);
2398 void *perf_evsel__rawptr(struct evsel *evsel, struct perf_sample *sample,
2401 struct tep_format_field *field = perf_evsel__field(evsel, name);
2407 offset = field->offset;
2409 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2410 offset = *(int *)(sample->raw_data + field->offset);
2414 return sample->raw_data + offset;
2417 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
2421 void *ptr = sample->raw_data + field->offset;
2423 switch (field->size) {
2427 value = *(u16 *)ptr;
2430 value = *(u32 *)ptr;
2433 memcpy(&value, ptr, sizeof(u64));
2442 switch (field->size) {
2444 return bswap_16(value);
2446 return bswap_32(value);
2448 return bswap_64(value);
2456 u64 perf_evsel__intval(struct evsel *evsel, struct perf_sample *sample,
2459 struct tep_format_field *field = perf_evsel__field(evsel, name);
2464 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2467 bool perf_evsel__fallback(struct evsel *evsel, int err,
2468 char *msg, size_t msgsize)
2472 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2473 evsel->core.attr.type == PERF_TYPE_HARDWARE &&
2474 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2476 * If it's cycles then fall back to hrtimer based
2477 * cpu-clock-tick sw counter, which is always available even if
2480 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2483 scnprintf(msg, msgsize, "%s",
2484 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2486 evsel->core.attr.type = PERF_TYPE_SOFTWARE;
2487 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK;
2489 zfree(&evsel->name);
2491 } else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
2492 (paranoid = perf_event_paranoid()) > 1) {
2493 const char *name = perf_evsel__name(evsel);
2495 const char *sep = ":";
2497 /* Is there already the separator in the name. */
2498 if (strchr(name, '/') ||
2502 if (asprintf(&new_name, "%s%su", name, sep) < 0)
2507 evsel->name = new_name;
2508 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
2509 "to fall back to excluding kernel and hypervisor "
2510 " samples", paranoid);
2511 evsel->core.attr.exclude_kernel = 1;
2512 evsel->core.attr.exclude_hv = 1;
2520 static bool find_process(const char *name)
2522 size_t len = strlen(name);
2527 dir = opendir(procfs__mountpoint());
2531 /* Walk through the directory. */
2532 while (ret && (d = readdir(dir)) != NULL) {
2533 char path[PATH_MAX];
2537 if ((d->d_type != DT_DIR) ||
2538 !strcmp(".", d->d_name) ||
2539 !strcmp("..", d->d_name))
2542 scnprintf(path, sizeof(path), "%s/%s/comm",
2543 procfs__mountpoint(), d->d_name);
2545 if (filename__read_str(path, &data, &size))
2548 ret = strncmp(name, data, len);
2553 return ret ? false : true;
2556 int perf_evsel__open_strerror(struct evsel *evsel, struct target *target,
2557 int err, char *msg, size_t size)
2559 char sbuf[STRERR_BUFSIZE];
2566 printed = scnprintf(msg, size,
2567 "No permission to enable %s event.\n\n",
2568 perf_evsel__name(evsel));
2570 return scnprintf(msg + printed, size - printed,
2571 "You may not have permission to collect %sstats.\n\n"
2572 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2573 "which controls use of the performance events system by\n"
2574 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2575 "The current value is %d:\n\n"
2576 " -1: Allow use of (almost) all events by all users\n"
2577 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
2578 ">= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN\n"
2579 " Disallow raw tracepoint access by users without CAP_SYS_ADMIN\n"
2580 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2581 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2582 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2583 " kernel.perf_event_paranoid = -1\n" ,
2584 target->system_wide ? "system-wide " : "",
2585 perf_event_paranoid());
2587 return scnprintf(msg, size, "The %s event is not supported.",
2588 perf_evsel__name(evsel));
2590 return scnprintf(msg, size, "%s",
2591 "Too many events are opened.\n"
2592 "Probably the maximum number of open file descriptors has been reached.\n"
2593 "Hint: Try again after reducing the number of events.\n"
2594 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2596 if (evsel__has_callchain(evsel) &&
2597 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2598 return scnprintf(msg, size,
2599 "Not enough memory to setup event with callchain.\n"
2600 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2601 "Hint: Current value: %d", sysctl__max_stack());
2604 if (target->cpu_list)
2605 return scnprintf(msg, size, "%s",
2606 "No such device - did you specify an out-of-range profile CPU?");
2609 if (evsel->core.attr.sample_period != 0)
2610 return scnprintf(msg, size,
2611 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
2612 perf_evsel__name(evsel));
2613 if (evsel->core.attr.precise_ip)
2614 return scnprintf(msg, size, "%s",
2615 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2616 #if defined(__i386__) || defined(__x86_64__)
2617 if (evsel->core.attr.type == PERF_TYPE_HARDWARE)
2618 return scnprintf(msg, size, "%s",
2619 "No hardware sampling interrupt available.\n");
2623 if (find_process("oprofiled"))
2624 return scnprintf(msg, size,
2625 "The PMU counters are busy/taken by another profiler.\n"
2626 "We found oprofile daemon running, please stop it and try again.");
2629 if (evsel->core.attr.write_backward && perf_missing_features.write_backward)
2630 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
2631 if (perf_missing_features.clockid)
2632 return scnprintf(msg, size, "clockid feature not supported.");
2633 if (perf_missing_features.clockid_wrong)
2634 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2635 if (perf_missing_features.aux_output)
2636 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel.");
2642 return scnprintf(msg, size,
2643 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2644 "/bin/dmesg | grep -i perf may provide additional information.\n",
2645 err, str_error_r(err, sbuf, sizeof(sbuf)),
2646 perf_evsel__name(evsel));
2649 struct perf_env *perf_evsel__env(struct evsel *evsel)
2651 if (evsel && evsel->evlist)
2652 return evsel->evlist->env;
2656 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
2660 for (cpu = 0; cpu < xyarray__max_x(evsel->core.fd); cpu++) {
2661 for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
2663 int fd = FD(evsel, cpu, thread);
2665 if (perf_evlist__id_add_fd(evlist, evsel,
2666 cpu, thread, fd) < 0)
2674 int perf_evsel__store_ids(struct evsel *evsel, struct evlist *evlist)
2676 struct perf_cpu_map *cpus = evsel->core.cpus;
2677 struct perf_thread_map *threads = evsel->core.threads;
2679 if (perf_evsel__alloc_id(&evsel->core, cpus->nr, threads->nr))
2682 return store_evsel_ids(evsel, evlist);