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>
25 #include <perf/evsel.h>
27 #include "callchain.h"
33 #include "thread_map.h"
35 #include "perf_regs.h"
37 #include "trace-event.h"
41 #include "util/parse-branch-options.h"
43 #include <linux/ctype.h>
45 struct perf_missing_features perf_missing_features;
47 static clockid_t clockid;
49 static int perf_evsel__no_extra_init(struct evsel *evsel __maybe_unused)
54 void __weak test_attr__ready(void) { }
56 static void perf_evsel__no_extra_fini(struct evsel *evsel __maybe_unused)
62 int (*init)(struct evsel *evsel);
63 void (*fini)(struct evsel *evsel);
64 } perf_evsel__object = {
65 .size = sizeof(struct evsel),
66 .init = perf_evsel__no_extra_init,
67 .fini = perf_evsel__no_extra_fini,
70 int perf_evsel__object_config(size_t object_size,
71 int (*init)(struct evsel *evsel),
72 void (*fini)(struct evsel *evsel))
78 if (perf_evsel__object.size > object_size)
81 perf_evsel__object.size = object_size;
85 perf_evsel__object.init = init;
88 perf_evsel__object.fini = fini;
93 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
95 int __perf_evsel__sample_size(u64 sample_type)
97 u64 mask = sample_type & PERF_SAMPLE_MASK;
101 for (i = 0; i < 64; i++) {
102 if (mask & (1ULL << i))
112 * __perf_evsel__calc_id_pos - calculate id_pos.
113 * @sample_type: sample type
115 * This function returns the position of the event id (PERF_SAMPLE_ID or
116 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
119 static int __perf_evsel__calc_id_pos(u64 sample_type)
123 if (sample_type & PERF_SAMPLE_IDENTIFIER)
126 if (!(sample_type & PERF_SAMPLE_ID))
129 if (sample_type & PERF_SAMPLE_IP)
132 if (sample_type & PERF_SAMPLE_TID)
135 if (sample_type & PERF_SAMPLE_TIME)
138 if (sample_type & PERF_SAMPLE_ADDR)
145 * __perf_evsel__calc_is_pos - calculate is_pos.
146 * @sample_type: sample type
148 * This function returns the position (counting backwards) of the event id
149 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
150 * sample_id_all is used there is an id sample appended to non-sample events.
152 static int __perf_evsel__calc_is_pos(u64 sample_type)
156 if (sample_type & PERF_SAMPLE_IDENTIFIER)
159 if (!(sample_type & PERF_SAMPLE_ID))
162 if (sample_type & PERF_SAMPLE_CPU)
165 if (sample_type & PERF_SAMPLE_STREAM_ID)
171 void perf_evsel__calc_id_pos(struct evsel *evsel)
173 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type);
174 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type);
177 void __perf_evsel__set_sample_bit(struct evsel *evsel,
178 enum perf_event_sample_format bit)
180 if (!(evsel->core.attr.sample_type & bit)) {
181 evsel->core.attr.sample_type |= bit;
182 evsel->sample_size += sizeof(u64);
183 perf_evsel__calc_id_pos(evsel);
187 void __perf_evsel__reset_sample_bit(struct evsel *evsel,
188 enum perf_event_sample_format bit)
190 if (evsel->core.attr.sample_type & bit) {
191 evsel->core.attr.sample_type &= ~bit;
192 evsel->sample_size -= sizeof(u64);
193 perf_evsel__calc_id_pos(evsel);
197 void perf_evsel__set_sample_id(struct evsel *evsel,
198 bool can_sample_identifier)
200 if (can_sample_identifier) {
201 perf_evsel__reset_sample_bit(evsel, ID);
202 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
204 perf_evsel__set_sample_bit(evsel, ID);
206 evsel->core.attr.read_format |= PERF_FORMAT_ID;
210 * perf_evsel__is_function_event - Return whether given evsel is a function
213 * @evsel - evsel selector to be tested
215 * Return %true if event is function trace event
217 bool perf_evsel__is_function_event(struct evsel *evsel)
219 #define FUNCTION_EVENT "ftrace:function"
221 return evsel->name &&
222 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
224 #undef FUNCTION_EVENT
227 void evsel__init(struct evsel *evsel,
228 struct perf_event_attr *attr, int idx)
230 perf_evsel__init(&evsel->core, attr);
232 evsel->tracking = !idx;
233 evsel->leader = evsel;
236 evsel->max_events = ULONG_MAX;
237 evsel->evlist = NULL;
238 evsel->bpf_obj = NULL;
240 INIT_LIST_HEAD(&evsel->config_terms);
241 perf_evsel__object.init(evsel);
242 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
243 perf_evsel__calc_id_pos(evsel);
244 evsel->cmdline_group_boundary = false;
245 evsel->metric_expr = NULL;
246 evsel->metric_name = NULL;
247 evsel->metric_events = NULL;
248 evsel->collect_stat = false;
249 evsel->pmu_name = NULL;
252 struct evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
254 struct evsel *evsel = zalloc(perf_evsel__object.size);
258 evsel__init(evsel, attr, idx);
260 if (perf_evsel__is_bpf_output(evsel)) {
261 evsel->core.attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
262 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
263 evsel->core.attr.sample_period = 1;
266 if (perf_evsel__is_clock(evsel)) {
268 * The evsel->unit points to static alias->unit
269 * so it's ok to use static string in here.
271 static const char *unit = "msec";
280 static bool perf_event_can_profile_kernel(void)
282 return geteuid() == 0 || perf_event_paranoid() == -1;
285 struct evsel *perf_evsel__new_cycles(bool precise)
287 struct perf_event_attr attr = {
288 .type = PERF_TYPE_HARDWARE,
289 .config = PERF_COUNT_HW_CPU_CYCLES,
290 .exclude_kernel = !perf_event_can_profile_kernel(),
294 event_attr_init(&attr);
300 * Now let the usual logic to set up the perf_event_attr defaults
301 * to kick in when we return and before perf_evsel__open() is called.
304 evsel = evsel__new(&attr);
308 evsel->precise_max = true;
310 /* use asprintf() because free(evsel) assumes name is allocated */
311 if (asprintf(&evsel->name, "cycles%s%s%.*s",
312 (attr.precise_ip || attr.exclude_kernel) ? ":" : "",
313 attr.exclude_kernel ? "u" : "",
314 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0)
319 evsel__delete(evsel);
325 * Returns pointer with encoded error via <linux/err.h> interface.
327 struct evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
329 struct evsel *evsel = zalloc(perf_evsel__object.size);
335 struct perf_event_attr attr = {
336 .type = PERF_TYPE_TRACEPOINT,
337 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
338 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
341 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
344 evsel->tp_format = trace_event__tp_format(sys, name);
345 if (IS_ERR(evsel->tp_format)) {
346 err = PTR_ERR(evsel->tp_format);
350 event_attr_init(&attr);
351 attr.config = evsel->tp_format->id;
352 attr.sample_period = 1;
353 evsel__init(evsel, &attr, idx);
365 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
373 "stalled-cycles-frontend",
374 "stalled-cycles-backend",
378 static const char *__perf_evsel__hw_name(u64 config)
380 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
381 return perf_evsel__hw_names[config];
383 return "unknown-hardware";
386 static int perf_evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size)
388 int colon = 0, r = 0;
389 struct perf_event_attr *attr = &evsel->core.attr;
390 bool exclude_guest_default = false;
392 #define MOD_PRINT(context, mod) do { \
393 if (!attr->exclude_##context) { \
394 if (!colon) colon = ++r; \
395 r += scnprintf(bf + r, size - r, "%c", mod); \
398 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
399 MOD_PRINT(kernel, 'k');
400 MOD_PRINT(user, 'u');
402 exclude_guest_default = true;
405 if (attr->precise_ip) {
408 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
409 exclude_guest_default = true;
412 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
413 MOD_PRINT(host, 'H');
414 MOD_PRINT(guest, 'G');
422 static int perf_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
424 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->core.attr.config));
425 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
428 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
441 static const char *__perf_evsel__sw_name(u64 config)
443 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
444 return perf_evsel__sw_names[config];
445 return "unknown-software";
448 static int perf_evsel__sw_name(struct evsel *evsel, char *bf, size_t size)
450 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->core.attr.config));
451 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
454 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
458 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
460 if (type & HW_BREAKPOINT_R)
461 r += scnprintf(bf + r, size - r, "r");
463 if (type & HW_BREAKPOINT_W)
464 r += scnprintf(bf + r, size - r, "w");
466 if (type & HW_BREAKPOINT_X)
467 r += scnprintf(bf + r, size - r, "x");
472 static int perf_evsel__bp_name(struct evsel *evsel, char *bf, size_t size)
474 struct perf_event_attr *attr = &evsel->core.attr;
475 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
476 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
479 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
480 [PERF_EVSEL__MAX_ALIASES] = {
481 { "L1-dcache", "l1-d", "l1d", "L1-data", },
482 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
484 { "dTLB", "d-tlb", "Data-TLB", },
485 { "iTLB", "i-tlb", "Instruction-TLB", },
486 { "branch", "branches", "bpu", "btb", "bpc", },
490 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
491 [PERF_EVSEL__MAX_ALIASES] = {
492 { "load", "loads", "read", },
493 { "store", "stores", "write", },
494 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
497 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
498 [PERF_EVSEL__MAX_ALIASES] = {
499 { "refs", "Reference", "ops", "access", },
500 { "misses", "miss", },
503 #define C(x) PERF_COUNT_HW_CACHE_##x
504 #define CACHE_READ (1 << C(OP_READ))
505 #define CACHE_WRITE (1 << C(OP_WRITE))
506 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
507 #define COP(x) (1 << x)
510 * cache operartion stat
511 * L1I : Read and prefetch only
512 * ITLB and BPU : Read-only
514 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
515 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
516 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
517 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
518 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
519 [C(ITLB)] = (CACHE_READ),
520 [C(BPU)] = (CACHE_READ),
521 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
524 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
526 if (perf_evsel__hw_cache_stat[type] & COP(op))
527 return true; /* valid */
529 return false; /* invalid */
532 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
533 char *bf, size_t size)
536 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
537 perf_evsel__hw_cache_op[op][0],
538 perf_evsel__hw_cache_result[result][0]);
541 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
542 perf_evsel__hw_cache_op[op][1]);
545 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
547 u8 op, result, type = (config >> 0) & 0xff;
548 const char *err = "unknown-ext-hardware-cache-type";
550 if (type >= PERF_COUNT_HW_CACHE_MAX)
553 op = (config >> 8) & 0xff;
554 err = "unknown-ext-hardware-cache-op";
555 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
558 result = (config >> 16) & 0xff;
559 err = "unknown-ext-hardware-cache-result";
560 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
563 err = "invalid-cache";
564 if (!perf_evsel__is_cache_op_valid(type, op))
567 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
569 return scnprintf(bf, size, "%s", err);
572 static int perf_evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size)
574 int ret = __perf_evsel__hw_cache_name(evsel->core.attr.config, bf, size);
575 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
578 static int perf_evsel__raw_name(struct evsel *evsel, char *bf, size_t size)
580 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config);
581 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
584 static int perf_evsel__tool_name(char *bf, size_t size)
586 int ret = scnprintf(bf, size, "duration_time");
590 const char *perf_evsel__name(struct evsel *evsel)
600 switch (evsel->core.attr.type) {
602 perf_evsel__raw_name(evsel, bf, sizeof(bf));
605 case PERF_TYPE_HARDWARE:
606 perf_evsel__hw_name(evsel, bf, sizeof(bf));
609 case PERF_TYPE_HW_CACHE:
610 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
613 case PERF_TYPE_SOFTWARE:
614 if (evsel->tool_event)
615 perf_evsel__tool_name(bf, sizeof(bf));
617 perf_evsel__sw_name(evsel, bf, sizeof(bf));
620 case PERF_TYPE_TRACEPOINT:
621 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
624 case PERF_TYPE_BREAKPOINT:
625 perf_evsel__bp_name(evsel, bf, sizeof(bf));
629 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
630 evsel->core.attr.type);
634 evsel->name = strdup(bf);
642 const char *perf_evsel__group_name(struct evsel *evsel)
644 return evsel->group_name ?: "anon group";
648 * Returns the group details for the specified leader,
649 * with following rules.
651 * For record -e '{cycles,instructions}'
652 * 'anon group { cycles:u, instructions:u }'
654 * For record -e 'cycles,instructions' and report --group
655 * 'cycles:u, instructions:u'
657 int perf_evsel__group_desc(struct evsel *evsel, char *buf, size_t size)
661 const char *group_name = perf_evsel__group_name(evsel);
663 if (!evsel->forced_leader)
664 ret = scnprintf(buf, size, "%s { ", group_name);
666 ret += scnprintf(buf + ret, size - ret, "%s",
667 perf_evsel__name(evsel));
669 for_each_group_member(pos, evsel)
670 ret += scnprintf(buf + ret, size - ret, ", %s",
671 perf_evsel__name(pos));
673 if (!evsel->forced_leader)
674 ret += scnprintf(buf + ret, size - ret, " }");
679 static void __perf_evsel__config_callchain(struct evsel *evsel,
680 struct record_opts *opts,
681 struct callchain_param *param)
683 bool function = perf_evsel__is_function_event(evsel);
684 struct perf_event_attr *attr = &evsel->core.attr;
686 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
688 attr->sample_max_stack = param->max_stack;
690 if (opts->kernel_callchains)
691 attr->exclude_callchain_user = 1;
692 if (opts->user_callchains)
693 attr->exclude_callchain_kernel = 1;
694 if (param->record_mode == CALLCHAIN_LBR) {
695 if (!opts->branch_stack) {
696 if (attr->exclude_user) {
697 pr_warning("LBR callstack option is only available "
698 "to get user callchain information. "
699 "Falling back to framepointers.\n");
701 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
702 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
703 PERF_SAMPLE_BRANCH_CALL_STACK |
704 PERF_SAMPLE_BRANCH_NO_CYCLES |
705 PERF_SAMPLE_BRANCH_NO_FLAGS;
708 pr_warning("Cannot use LBR callstack with branch stack. "
709 "Falling back to framepointers.\n");
712 if (param->record_mode == CALLCHAIN_DWARF) {
714 perf_evsel__set_sample_bit(evsel, REGS_USER);
715 perf_evsel__set_sample_bit(evsel, STACK_USER);
716 if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) {
717 attr->sample_regs_user |= DWARF_MINIMAL_REGS;
718 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, "
719 "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
720 "so the minimal registers set (IP, SP) is explicitly forced.\n");
722 attr->sample_regs_user |= PERF_REGS_MASK;
724 attr->sample_stack_user = param->dump_size;
725 attr->exclude_callchain_user = 1;
727 pr_info("Cannot use DWARF unwind for function trace event,"
728 " falling back to framepointers.\n");
733 pr_info("Disabling user space callchains for function trace event.\n");
734 attr->exclude_callchain_user = 1;
738 void perf_evsel__config_callchain(struct evsel *evsel,
739 struct record_opts *opts,
740 struct callchain_param *param)
743 return __perf_evsel__config_callchain(evsel, opts, param);
747 perf_evsel__reset_callgraph(struct evsel *evsel,
748 struct callchain_param *param)
750 struct perf_event_attr *attr = &evsel->core.attr;
752 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
753 if (param->record_mode == CALLCHAIN_LBR) {
754 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
755 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
756 PERF_SAMPLE_BRANCH_CALL_STACK);
758 if (param->record_mode == CALLCHAIN_DWARF) {
759 perf_evsel__reset_sample_bit(evsel, REGS_USER);
760 perf_evsel__reset_sample_bit(evsel, STACK_USER);
764 static void apply_config_terms(struct evsel *evsel,
765 struct record_opts *opts, bool track)
767 struct perf_evsel_config_term *term;
768 struct list_head *config_terms = &evsel->config_terms;
769 struct perf_event_attr *attr = &evsel->core.attr;
770 /* callgraph default */
771 struct callchain_param param = {
772 .record_mode = callchain_param.record_mode,
776 const char *callgraph_buf = NULL;
778 list_for_each_entry(term, config_terms, list) {
779 switch (term->type) {
780 case PERF_EVSEL__CONFIG_TERM_PERIOD:
781 if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
782 attr->sample_period = term->val.period;
784 perf_evsel__reset_sample_bit(evsel, PERIOD);
787 case PERF_EVSEL__CONFIG_TERM_FREQ:
788 if (!(term->weak && opts->user_freq != UINT_MAX)) {
789 attr->sample_freq = term->val.freq;
791 perf_evsel__set_sample_bit(evsel, PERIOD);
794 case PERF_EVSEL__CONFIG_TERM_TIME:
796 perf_evsel__set_sample_bit(evsel, TIME);
798 perf_evsel__reset_sample_bit(evsel, TIME);
800 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
801 callgraph_buf = term->val.callgraph;
803 case PERF_EVSEL__CONFIG_TERM_BRANCH:
804 if (term->val.branch && strcmp(term->val.branch, "no")) {
805 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
806 parse_branch_str(term->val.branch,
807 &attr->branch_sample_type);
809 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
811 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
812 dump_size = term->val.stack_user;
814 case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
815 max_stack = term->val.max_stack;
817 case PERF_EVSEL__CONFIG_TERM_MAX_EVENTS:
818 evsel->max_events = term->val.max_events;
820 case PERF_EVSEL__CONFIG_TERM_INHERIT:
822 * attr->inherit should has already been set by
823 * perf_evsel__config. If user explicitly set
824 * inherit using config terms, override global
825 * opt->no_inherit setting.
827 attr->inherit = term->val.inherit ? 1 : 0;
829 case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
830 attr->write_backward = term->val.overwrite ? 1 : 0;
832 case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
834 case PERF_EVSEL__CONFIG_TERM_PERCORE:
836 case PERF_EVSEL__CONFIG_TERM_AUX_OUTPUT:
837 attr->aux_output = term->val.aux_output ? 1 : 0;
844 /* User explicitly set per-event callgraph, clear the old setting and reset. */
845 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
846 bool sample_address = false;
849 param.max_stack = max_stack;
850 if (callgraph_buf == NULL)
851 callgraph_buf = "fp";
854 /* parse callgraph parameters */
855 if (callgraph_buf != NULL) {
856 if (!strcmp(callgraph_buf, "no")) {
857 param.enabled = false;
858 param.record_mode = CALLCHAIN_NONE;
860 param.enabled = true;
861 if (parse_callchain_record(callgraph_buf, ¶m)) {
862 pr_err("per-event callgraph setting for %s failed. "
863 "Apply callgraph global setting for it\n",
867 if (param.record_mode == CALLCHAIN_DWARF)
868 sample_address = true;
872 dump_size = round_up(dump_size, sizeof(u64));
873 param.dump_size = dump_size;
876 /* If global callgraph set, clear it */
877 if (callchain_param.enabled)
878 perf_evsel__reset_callgraph(evsel, &callchain_param);
880 /* set perf-event callgraph */
882 if (sample_address) {
883 perf_evsel__set_sample_bit(evsel, ADDR);
884 perf_evsel__set_sample_bit(evsel, DATA_SRC);
885 evsel->core.attr.mmap_data = track;
887 perf_evsel__config_callchain(evsel, opts, ¶m);
892 static bool is_dummy_event(struct evsel *evsel)
894 return (evsel->core.attr.type == PERF_TYPE_SOFTWARE) &&
895 (evsel->core.attr.config == PERF_COUNT_SW_DUMMY);
899 * The enable_on_exec/disabled value strategy:
901 * 1) For any type of traced program:
902 * - all independent events and group leaders are disabled
903 * - all group members are enabled
905 * Group members are ruled by group leaders. They need to
906 * be enabled, because the group scheduling relies on that.
908 * 2) For traced programs executed by perf:
909 * - all independent events and group leaders have
911 * - we don't specifically enable or disable any event during
914 * Independent events and group leaders are initially disabled
915 * and get enabled by exec. Group members are ruled by group
916 * leaders as stated in 1).
918 * 3) For traced programs attached by perf (pid/tid):
919 * - we specifically enable or disable all events during
922 * When attaching events to already running traced we
923 * enable/disable events specifically, as there's no
924 * initial traced exec call.
926 void perf_evsel__config(struct evsel *evsel, struct record_opts *opts,
927 struct callchain_param *callchain)
929 struct evsel *leader = evsel->leader;
930 struct perf_event_attr *attr = &evsel->core.attr;
931 int track = evsel->tracking;
932 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
934 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
935 attr->inherit = !opts->no_inherit;
936 attr->write_backward = opts->overwrite ? 1 : 0;
938 perf_evsel__set_sample_bit(evsel, IP);
939 perf_evsel__set_sample_bit(evsel, TID);
941 if (evsel->sample_read) {
942 perf_evsel__set_sample_bit(evsel, READ);
945 * We need ID even in case of single event, because
946 * PERF_SAMPLE_READ process ID specific data.
948 perf_evsel__set_sample_id(evsel, false);
951 * Apply group format only if we belong to group
952 * with more than one members.
954 if (leader->core.nr_members > 1) {
955 attr->read_format |= PERF_FORMAT_GROUP;
961 * We default some events to have a default interval. But keep
962 * it a weak assumption overridable by the user.
964 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
965 opts->user_interval != ULLONG_MAX)) {
967 perf_evsel__set_sample_bit(evsel, PERIOD);
969 attr->sample_freq = opts->freq;
971 attr->sample_period = opts->default_interval;
976 * Disable sampling for all group members other
977 * than leader in case leader 'leads' the sampling.
979 if ((leader != evsel) && leader->sample_read) {
981 attr->sample_freq = 0;
982 attr->sample_period = 0;
983 attr->write_backward = 0;
986 * We don't get sample for slave events, we make them
987 * when delivering group leader sample. Set the slave
988 * event to follow the master sample_type to ease up
991 attr->sample_type = leader->core.attr.sample_type;
994 if (opts->no_samples)
995 attr->sample_freq = 0;
997 if (opts->inherit_stat) {
998 evsel->core.attr.read_format |=
999 PERF_FORMAT_TOTAL_TIME_ENABLED |
1000 PERF_FORMAT_TOTAL_TIME_RUNNING |
1002 attr->inherit_stat = 1;
1005 if (opts->sample_address) {
1006 perf_evsel__set_sample_bit(evsel, ADDR);
1007 attr->mmap_data = track;
1011 * We don't allow user space callchains for function trace
1012 * event, due to issues with page faults while tracing page
1013 * fault handler and its overall trickiness nature.
1015 if (perf_evsel__is_function_event(evsel))
1016 evsel->core.attr.exclude_callchain_user = 1;
1018 if (callchain && callchain->enabled && !evsel->no_aux_samples)
1019 perf_evsel__config_callchain(evsel, opts, callchain);
1021 if (opts->sample_intr_regs) {
1022 attr->sample_regs_intr = opts->sample_intr_regs;
1023 perf_evsel__set_sample_bit(evsel, REGS_INTR);
1026 if (opts->sample_user_regs) {
1027 attr->sample_regs_user |= opts->sample_user_regs;
1028 perf_evsel__set_sample_bit(evsel, REGS_USER);
1031 if (target__has_cpu(&opts->target) || opts->sample_cpu)
1032 perf_evsel__set_sample_bit(evsel, CPU);
1035 * When the user explicitly disabled time don't force it here.
1037 if (opts->sample_time &&
1038 (!perf_missing_features.sample_id_all &&
1039 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
1040 opts->sample_time_set)))
1041 perf_evsel__set_sample_bit(evsel, TIME);
1043 if (opts->raw_samples && !evsel->no_aux_samples) {
1044 perf_evsel__set_sample_bit(evsel, TIME);
1045 perf_evsel__set_sample_bit(evsel, RAW);
1046 perf_evsel__set_sample_bit(evsel, CPU);
1049 if (opts->sample_address)
1050 perf_evsel__set_sample_bit(evsel, DATA_SRC);
1052 if (opts->sample_phys_addr)
1053 perf_evsel__set_sample_bit(evsel, PHYS_ADDR);
1055 if (opts->no_buffering) {
1056 attr->watermark = 0;
1057 attr->wakeup_events = 1;
1059 if (opts->branch_stack && !evsel->no_aux_samples) {
1060 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
1061 attr->branch_sample_type = opts->branch_stack;
1064 if (opts->sample_weight)
1065 perf_evsel__set_sample_bit(evsel, WEIGHT);
1069 attr->mmap2 = track && !perf_missing_features.mmap2;
1071 attr->ksymbol = track && !perf_missing_features.ksymbol;
1072 attr->bpf_event = track && !opts->no_bpf_event &&
1073 !perf_missing_features.bpf_event;
1075 if (opts->record_namespaces)
1076 attr->namespaces = track;
1078 if (opts->record_switch_events)
1079 attr->context_switch = track;
1081 if (opts->sample_transaction)
1082 perf_evsel__set_sample_bit(evsel, TRANSACTION);
1084 if (opts->running_time) {
1085 evsel->core.attr.read_format |=
1086 PERF_FORMAT_TOTAL_TIME_ENABLED |
1087 PERF_FORMAT_TOTAL_TIME_RUNNING;
1091 * XXX see the function comment above
1093 * Disabling only independent events or group leaders,
1094 * keeping group members enabled.
1096 if (perf_evsel__is_group_leader(evsel))
1100 * Setting enable_on_exec for independent events and
1101 * group leaders for traced executed by perf.
1103 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
1104 !opts->initial_delay)
1105 attr->enable_on_exec = 1;
1107 if (evsel->immediate) {
1109 attr->enable_on_exec = 0;
1112 clockid = opts->clockid;
1113 if (opts->use_clockid) {
1114 attr->use_clockid = 1;
1115 attr->clockid = opts->clockid;
1118 if (evsel->precise_max)
1119 attr->precise_ip = 3;
1121 if (opts->all_user) {
1122 attr->exclude_kernel = 1;
1123 attr->exclude_user = 0;
1126 if (opts->all_kernel) {
1127 attr->exclude_kernel = 0;
1128 attr->exclude_user = 1;
1131 if (evsel->core.own_cpus || evsel->unit)
1132 evsel->core.attr.read_format |= PERF_FORMAT_ID;
1135 * Apply event specific term settings,
1136 * it overloads any global configuration.
1138 apply_config_terms(evsel, opts, track);
1140 evsel->ignore_missing_thread = opts->ignore_missing_thread;
1142 /* The --period option takes the precedence. */
1143 if (opts->period_set) {
1145 perf_evsel__set_sample_bit(evsel, PERIOD);
1147 perf_evsel__reset_sample_bit(evsel, PERIOD);
1151 * For initial_delay, a dummy event is added implicitly.
1152 * The software event will trigger -EOPNOTSUPP error out,
1153 * if BRANCH_STACK bit is set.
1155 if (opts->initial_delay && is_dummy_event(evsel))
1156 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
1159 int perf_evsel__set_filter(struct evsel *evsel, const char *filter)
1161 char *new_filter = strdup(filter);
1163 if (new_filter != NULL) {
1164 free(evsel->filter);
1165 evsel->filter = new_filter;
1172 static int perf_evsel__append_filter(struct evsel *evsel,
1173 const char *fmt, const char *filter)
1177 if (evsel->filter == NULL)
1178 return perf_evsel__set_filter(evsel, filter);
1180 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
1181 free(evsel->filter);
1182 evsel->filter = new_filter;
1189 int perf_evsel__append_tp_filter(struct evsel *evsel, const char *filter)
1191 return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
1194 int perf_evsel__append_addr_filter(struct evsel *evsel, const char *filter)
1196 return perf_evsel__append_filter(evsel, "%s,%s", filter);
1199 int evsel__enable(struct evsel *evsel)
1201 int err = perf_evsel__enable(&evsel->core);
1204 evsel->disabled = false;
1209 int evsel__disable(struct evsel *evsel)
1211 int err = perf_evsel__disable(&evsel->core);
1213 * We mark it disabled here so that tools that disable a event can
1214 * ignore events after they disable it. I.e. the ring buffer may have
1215 * already a few more events queued up before the kernel got the stop
1219 evsel->disabled = true;
1224 int perf_evsel__alloc_id(struct evsel *evsel, int ncpus, int nthreads)
1226 if (ncpus == 0 || nthreads == 0)
1229 if (evsel->system_wide)
1232 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1233 if (evsel->sample_id == NULL)
1236 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1237 if (evsel->id == NULL) {
1238 xyarray__delete(evsel->sample_id);
1239 evsel->sample_id = NULL;
1246 static void perf_evsel__free_id(struct evsel *evsel)
1248 xyarray__delete(evsel->sample_id);
1249 evsel->sample_id = NULL;
1254 static void perf_evsel__free_config_terms(struct evsel *evsel)
1256 struct perf_evsel_config_term *term, *h;
1258 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1259 list_del_init(&term->list);
1264 void perf_evsel__exit(struct evsel *evsel)
1266 assert(list_empty(&evsel->core.node));
1267 assert(evsel->evlist == NULL);
1268 perf_evsel__free_counts(evsel);
1269 perf_evsel__free_fd(&evsel->core);
1270 perf_evsel__free_id(evsel);
1271 perf_evsel__free_config_terms(evsel);
1272 cgroup__put(evsel->cgrp);
1273 perf_cpu_map__put(evsel->core.cpus);
1274 perf_cpu_map__put(evsel->core.own_cpus);
1275 perf_thread_map__put(evsel->core.threads);
1276 zfree(&evsel->group_name);
1277 zfree(&evsel->name);
1278 perf_evsel__object.fini(evsel);
1281 void evsel__delete(struct evsel *evsel)
1283 perf_evsel__exit(evsel);
1287 void perf_evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
1288 struct perf_counts_values *count)
1290 struct perf_counts_values tmp;
1292 if (!evsel->prev_raw_counts)
1296 tmp = evsel->prev_raw_counts->aggr;
1297 evsel->prev_raw_counts->aggr = *count;
1299 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1300 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1303 count->val = count->val - tmp.val;
1304 count->ena = count->ena - tmp.ena;
1305 count->run = count->run - tmp.run;
1308 void perf_counts_values__scale(struct perf_counts_values *count,
1309 bool scale, s8 *pscaled)
1314 if (count->run == 0) {
1317 } else if (count->run < count->ena) {
1319 count->val = (u64)((double) count->val * count->ena / count->run);
1328 perf_evsel__read_one(struct evsel *evsel, int cpu, int thread)
1330 struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread);
1332 return perf_evsel__read(&evsel->core, cpu, thread, count);
1336 perf_evsel__set_count(struct evsel *counter, int cpu, int thread,
1337 u64 val, u64 ena, u64 run)
1339 struct perf_counts_values *count;
1341 count = perf_counts(counter->counts, cpu, thread);
1347 perf_counts__set_loaded(counter->counts, cpu, thread, true);
1351 perf_evsel__process_group_data(struct evsel *leader,
1352 int cpu, int thread, u64 *data)
1354 u64 read_format = leader->core.attr.read_format;
1355 struct sample_read_value *v;
1356 u64 nr, ena = 0, run = 0, i;
1360 if (nr != (u64) leader->core.nr_members)
1363 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1366 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1369 v = (struct sample_read_value *) data;
1371 perf_evsel__set_count(leader, cpu, thread,
1372 v[0].value, ena, run);
1374 for (i = 1; i < nr; i++) {
1375 struct evsel *counter;
1377 counter = perf_evlist__id2evsel(leader->evlist, v[i].id);
1381 perf_evsel__set_count(counter, cpu, thread,
1382 v[i].value, ena, run);
1389 perf_evsel__read_group(struct evsel *leader, int cpu, int thread)
1391 struct perf_stat_evsel *ps = leader->stats;
1392 u64 read_format = leader->core.attr.read_format;
1393 int size = perf_evsel__read_size(&leader->core);
1394 u64 *data = ps->group_data;
1396 if (!(read_format & PERF_FORMAT_ID))
1399 if (!perf_evsel__is_group_leader(leader))
1403 data = zalloc(size);
1407 ps->group_data = data;
1410 if (FD(leader, cpu, thread) < 0)
1413 if (readn(FD(leader, cpu, thread), data, size) <= 0)
1416 return perf_evsel__process_group_data(leader, cpu, thread, data);
1419 int perf_evsel__read_counter(struct evsel *evsel, int cpu, int thread)
1421 u64 read_format = evsel->core.attr.read_format;
1423 if (read_format & PERF_FORMAT_GROUP)
1424 return perf_evsel__read_group(evsel, cpu, thread);
1426 return perf_evsel__read_one(evsel, cpu, thread);
1429 int __perf_evsel__read_on_cpu(struct evsel *evsel,
1430 int cpu, int thread, bool scale)
1432 struct perf_counts_values count;
1433 size_t nv = scale ? 3 : 1;
1435 if (FD(evsel, cpu, thread) < 0)
1438 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1441 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
1444 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1445 perf_counts_values__scale(&count, scale, NULL);
1446 *perf_counts(evsel->counts, cpu, thread) = count;
1450 static int get_group_fd(struct evsel *evsel, int cpu, int thread)
1452 struct evsel *leader = evsel->leader;
1455 if (perf_evsel__is_group_leader(evsel))
1459 * Leader must be already processed/open,
1460 * if not it's a bug.
1462 BUG_ON(!leader->core.fd);
1464 fd = FD(leader, cpu, thread);
1475 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1477 bool first_bit = true;
1481 if (value & bits[i].bit) {
1482 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1485 } while (bits[++i].name != NULL);
1488 static void __p_sample_type(char *buf, size_t size, u64 value)
1490 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1491 struct bit_names bits[] = {
1492 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1493 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1494 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1495 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1496 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1497 bit_name(WEIGHT), bit_name(PHYS_ADDR),
1501 __p_bits(buf, size, value, bits);
1504 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1506 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1507 struct bit_names bits[] = {
1508 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1509 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1510 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1511 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1512 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1516 __p_bits(buf, size, value, bits);
1519 static void __p_read_format(char *buf, size_t size, u64 value)
1521 #define bit_name(n) { PERF_FORMAT_##n, #n }
1522 struct bit_names bits[] = {
1523 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1524 bit_name(ID), bit_name(GROUP),
1528 __p_bits(buf, size, value, bits);
1531 #define BUF_SIZE 1024
1533 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1534 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1535 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1536 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1537 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1538 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1540 #define PRINT_ATTRn(_n, _f, _p) \
1544 ret += attr__fprintf(fp, _n, buf, priv);\
1548 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1550 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1551 attr__fprintf_f attr__fprintf, void *priv)
1556 PRINT_ATTRf(type, p_unsigned);
1557 PRINT_ATTRf(size, p_unsigned);
1558 PRINT_ATTRf(config, p_hex);
1559 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1560 PRINT_ATTRf(sample_type, p_sample_type);
1561 PRINT_ATTRf(read_format, p_read_format);
1563 PRINT_ATTRf(disabled, p_unsigned);
1564 PRINT_ATTRf(inherit, p_unsigned);
1565 PRINT_ATTRf(pinned, p_unsigned);
1566 PRINT_ATTRf(exclusive, p_unsigned);
1567 PRINT_ATTRf(exclude_user, p_unsigned);
1568 PRINT_ATTRf(exclude_kernel, p_unsigned);
1569 PRINT_ATTRf(exclude_hv, p_unsigned);
1570 PRINT_ATTRf(exclude_idle, p_unsigned);
1571 PRINT_ATTRf(mmap, p_unsigned);
1572 PRINT_ATTRf(comm, p_unsigned);
1573 PRINT_ATTRf(freq, p_unsigned);
1574 PRINT_ATTRf(inherit_stat, p_unsigned);
1575 PRINT_ATTRf(enable_on_exec, p_unsigned);
1576 PRINT_ATTRf(task, p_unsigned);
1577 PRINT_ATTRf(watermark, p_unsigned);
1578 PRINT_ATTRf(precise_ip, p_unsigned);
1579 PRINT_ATTRf(mmap_data, p_unsigned);
1580 PRINT_ATTRf(sample_id_all, p_unsigned);
1581 PRINT_ATTRf(exclude_host, p_unsigned);
1582 PRINT_ATTRf(exclude_guest, p_unsigned);
1583 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1584 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1585 PRINT_ATTRf(mmap2, p_unsigned);
1586 PRINT_ATTRf(comm_exec, p_unsigned);
1587 PRINT_ATTRf(use_clockid, p_unsigned);
1588 PRINT_ATTRf(context_switch, p_unsigned);
1589 PRINT_ATTRf(write_backward, p_unsigned);
1590 PRINT_ATTRf(namespaces, p_unsigned);
1591 PRINT_ATTRf(ksymbol, p_unsigned);
1592 PRINT_ATTRf(bpf_event, p_unsigned);
1593 PRINT_ATTRf(aux_output, p_unsigned);
1595 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1596 PRINT_ATTRf(bp_type, p_unsigned);
1597 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1598 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1599 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
1600 PRINT_ATTRf(sample_regs_user, p_hex);
1601 PRINT_ATTRf(sample_stack_user, p_unsigned);
1602 PRINT_ATTRf(clockid, p_signed);
1603 PRINT_ATTRf(sample_regs_intr, p_hex);
1604 PRINT_ATTRf(aux_watermark, p_unsigned);
1605 PRINT_ATTRf(sample_max_stack, p_unsigned);
1610 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1611 void *priv __maybe_unused)
1613 return fprintf(fp, " %-32s %s\n", name, val);
1616 static void perf_evsel__remove_fd(struct evsel *pos,
1617 int nr_cpus, int nr_threads,
1620 for (int cpu = 0; cpu < nr_cpus; cpu++)
1621 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1622 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1625 static int update_fds(struct evsel *evsel,
1626 int nr_cpus, int cpu_idx,
1627 int nr_threads, int thread_idx)
1631 if (cpu_idx >= nr_cpus || thread_idx >= nr_threads)
1634 evlist__for_each_entry(evsel->evlist, pos) {
1635 nr_cpus = pos != evsel ? nr_cpus : cpu_idx;
1637 perf_evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
1640 * Since fds for next evsel has not been created,
1641 * there is no need to iterate whole event list.
1649 static bool ignore_missing_thread(struct evsel *evsel,
1650 int nr_cpus, int cpu,
1651 struct perf_thread_map *threads,
1652 int thread, int err)
1654 pid_t ignore_pid = thread_map__pid(threads, thread);
1656 if (!evsel->ignore_missing_thread)
1659 /* The system wide setup does not work with threads. */
1660 if (evsel->system_wide)
1663 /* The -ESRCH is perf event syscall errno for pid's not found. */
1667 /* If there's only one thread, let it fail. */
1668 if (threads->nr == 1)
1672 * We should remove fd for missing_thread first
1673 * because thread_map__remove() will decrease threads->nr.
1675 if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread))
1678 if (thread_map__remove(threads, thread))
1681 pr_warning("WARNING: Ignored open failure for pid %d\n",
1686 static void display_attr(struct perf_event_attr *attr)
1689 fprintf(stderr, "%.60s\n", graph_dotted_line);
1690 fprintf(stderr, "perf_event_attr:\n");
1691 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
1692 fprintf(stderr, "%.60s\n", graph_dotted_line);
1696 static int perf_event_open(struct evsel *evsel,
1697 pid_t pid, int cpu, int group_fd,
1698 unsigned long flags)
1700 int precise_ip = evsel->core.attr.precise_ip;
1704 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
1705 pid, cpu, group_fd, flags);
1707 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags);
1711 /* Do not try less precise if not requested. */
1712 if (!evsel->precise_max)
1716 * We tried all the precise_ip values, and it's
1717 * still failing, so leave it to standard fallback.
1719 if (!evsel->core.attr.precise_ip) {
1720 evsel->core.attr.precise_ip = precise_ip;
1724 pr_debug2("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
1725 evsel->core.attr.precise_ip--;
1726 pr_debug2("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
1727 display_attr(&evsel->core.attr);
1733 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
1734 struct perf_thread_map *threads)
1736 int cpu, thread, nthreads;
1737 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1739 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1741 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
1742 (perf_missing_features.aux_output && evsel->core.attr.aux_output))
1746 static struct perf_cpu_map *empty_cpu_map;
1748 if (empty_cpu_map == NULL) {
1749 empty_cpu_map = perf_cpu_map__dummy_new();
1750 if (empty_cpu_map == NULL)
1754 cpus = empty_cpu_map;
1757 if (threads == NULL) {
1758 static struct perf_thread_map *empty_thread_map;
1760 if (empty_thread_map == NULL) {
1761 empty_thread_map = thread_map__new_by_tid(-1);
1762 if (empty_thread_map == NULL)
1766 threads = empty_thread_map;
1769 if (evsel->system_wide)
1772 nthreads = threads->nr;
1774 if (evsel->core.fd == NULL &&
1775 perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
1779 flags |= PERF_FLAG_PID_CGROUP;
1780 pid = evsel->cgrp->fd;
1783 fallback_missing_features:
1784 if (perf_missing_features.clockid_wrong)
1785 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */
1786 if (perf_missing_features.clockid) {
1787 evsel->core.attr.use_clockid = 0;
1788 evsel->core.attr.clockid = 0;
1790 if (perf_missing_features.cloexec)
1791 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1792 if (perf_missing_features.mmap2)
1793 evsel->core.attr.mmap2 = 0;
1794 if (perf_missing_features.exclude_guest)
1795 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
1796 if (perf_missing_features.lbr_flags)
1797 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1798 PERF_SAMPLE_BRANCH_NO_CYCLES);
1799 if (perf_missing_features.group_read && evsel->core.attr.inherit)
1800 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
1801 if (perf_missing_features.ksymbol)
1802 evsel->core.attr.ksymbol = 0;
1803 if (perf_missing_features.bpf_event)
1804 evsel->core.attr.bpf_event = 0;
1806 if (perf_missing_features.sample_id_all)
1807 evsel->core.attr.sample_id_all = 0;
1809 display_attr(&evsel->core.attr);
1811 for (cpu = 0; cpu < cpus->nr; cpu++) {
1813 for (thread = 0; thread < nthreads; thread++) {
1816 if (!evsel->cgrp && !evsel->system_wide)
1817 pid = thread_map__pid(threads, thread);
1819 group_fd = get_group_fd(evsel, cpu, thread);
1823 fd = perf_event_open(evsel, pid, cpus->map[cpu],
1826 FD(evsel, cpu, thread) = fd;
1831 if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
1833 * We just removed 1 thread, so take a step
1834 * back on thread index and lower the upper
1840 /* ... and pretend like nothing have happened. */
1845 pr_debug2("\nsys_perf_event_open failed, error %d\n",
1850 pr_debug2(" = %d\n", fd);
1852 if (evsel->bpf_fd >= 0) {
1854 int bpf_fd = evsel->bpf_fd;
1857 PERF_EVENT_IOC_SET_BPF,
1859 if (err && errno != EEXIST) {
1860 pr_err("failed to attach bpf fd %d: %s\n",
1861 bpf_fd, strerror(errno));
1867 set_rlimit = NO_CHANGE;
1870 * If we succeeded but had to kill clockid, fail and
1871 * have perf_evsel__open_strerror() print us a nice
1874 if (perf_missing_features.clockid ||
1875 perf_missing_features.clockid_wrong) {
1886 * perf stat needs between 5 and 22 fds per CPU. When we run out
1887 * of them try to increase the limits.
1889 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1891 int old_errno = errno;
1893 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1894 if (set_rlimit == NO_CHANGE)
1895 l.rlim_cur = l.rlim_max;
1897 l.rlim_cur = l.rlim_max + 1000;
1898 l.rlim_max = l.rlim_cur;
1900 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1909 if (err != -EINVAL || cpu > 0 || thread > 0)
1913 * Must probe features in the order they were added to the
1914 * perf_event_attr interface.
1916 if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
1917 perf_missing_features.aux_output = true;
1918 pr_debug2("Kernel has no attr.aux_output support, bailing out\n");
1920 } else if (!perf_missing_features.bpf_event && evsel->core.attr.bpf_event) {
1921 perf_missing_features.bpf_event = true;
1922 pr_debug2("switching off bpf_event\n");
1923 goto fallback_missing_features;
1924 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
1925 perf_missing_features.ksymbol = true;
1926 pr_debug2("switching off ksymbol\n");
1927 goto fallback_missing_features;
1928 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
1929 perf_missing_features.write_backward = true;
1930 pr_debug2("switching off write_backward\n");
1932 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
1933 perf_missing_features.clockid_wrong = true;
1934 pr_debug2("switching off clockid\n");
1935 goto fallback_missing_features;
1936 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
1937 perf_missing_features.clockid = true;
1938 pr_debug2("switching off use_clockid\n");
1939 goto fallback_missing_features;
1940 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1941 perf_missing_features.cloexec = true;
1942 pr_debug2("switching off cloexec flag\n");
1943 goto fallback_missing_features;
1944 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
1945 perf_missing_features.mmap2 = true;
1946 pr_debug2("switching off mmap2\n");
1947 goto fallback_missing_features;
1948 } else if (!perf_missing_features.exclude_guest &&
1949 (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
1950 perf_missing_features.exclude_guest = true;
1951 pr_debug2("switching off exclude_guest, exclude_host\n");
1952 goto fallback_missing_features;
1953 } else if (!perf_missing_features.sample_id_all) {
1954 perf_missing_features.sample_id_all = true;
1955 pr_debug2("switching off sample_id_all\n");
1956 goto retry_sample_id;
1957 } else if (!perf_missing_features.lbr_flags &&
1958 (evsel->core.attr.branch_sample_type &
1959 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1960 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1961 perf_missing_features.lbr_flags = true;
1962 pr_debug2("switching off branch sample type no (cycles/flags)\n");
1963 goto fallback_missing_features;
1964 } else if (!perf_missing_features.group_read &&
1965 evsel->core.attr.inherit &&
1966 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
1967 perf_evsel__is_group_leader(evsel)) {
1968 perf_missing_features.group_read = true;
1969 pr_debug2("switching off group read\n");
1970 goto fallback_missing_features;
1974 threads->err_thread = thread;
1977 while (--thread >= 0) {
1978 close(FD(evsel, cpu, thread));
1979 FD(evsel, cpu, thread) = -1;
1982 } while (--cpu >= 0);
1986 void evsel__close(struct evsel *evsel)
1988 perf_evsel__close(&evsel->core);
1989 perf_evsel__free_id(evsel);
1992 int perf_evsel__open_per_cpu(struct evsel *evsel,
1993 struct perf_cpu_map *cpus)
1995 return evsel__open(evsel, cpus, NULL);
1998 int perf_evsel__open_per_thread(struct evsel *evsel,
1999 struct perf_thread_map *threads)
2001 return evsel__open(evsel, NULL, threads);
2004 static int perf_evsel__parse_id_sample(const struct evsel *evsel,
2005 const union perf_event *event,
2006 struct perf_sample *sample)
2008 u64 type = evsel->core.attr.sample_type;
2009 const u64 *array = event->sample.array;
2010 bool swapped = evsel->needs_swap;
2013 array += ((event->header.size -
2014 sizeof(event->header)) / sizeof(u64)) - 1;
2016 if (type & PERF_SAMPLE_IDENTIFIER) {
2017 sample->id = *array;
2021 if (type & PERF_SAMPLE_CPU) {
2024 /* undo swap of u64, then swap on individual u32s */
2025 u.val64 = bswap_64(u.val64);
2026 u.val32[0] = bswap_32(u.val32[0]);
2029 sample->cpu = u.val32[0];
2033 if (type & PERF_SAMPLE_STREAM_ID) {
2034 sample->stream_id = *array;
2038 if (type & PERF_SAMPLE_ID) {
2039 sample->id = *array;
2043 if (type & PERF_SAMPLE_TIME) {
2044 sample->time = *array;
2048 if (type & PERF_SAMPLE_TID) {
2051 /* undo swap of u64, then swap on individual u32s */
2052 u.val64 = bswap_64(u.val64);
2053 u.val32[0] = bswap_32(u.val32[0]);
2054 u.val32[1] = bswap_32(u.val32[1]);
2057 sample->pid = u.val32[0];
2058 sample->tid = u.val32[1];
2065 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2068 return size > max_size || offset + size > endp;
2071 #define OVERFLOW_CHECK(offset, size, max_size) \
2073 if (overflow(endp, (max_size), (offset), (size))) \
2077 #define OVERFLOW_CHECK_u64(offset) \
2078 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2081 perf_event__check_size(union perf_event *event, unsigned int sample_size)
2084 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2085 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2086 * check the format does not go past the end of the event.
2088 if (sample_size + sizeof(event->header) > event->header.size)
2094 int perf_evsel__parse_sample(struct evsel *evsel, union perf_event *event,
2095 struct perf_sample *data)
2097 u64 type = evsel->core.attr.sample_type;
2098 bool swapped = evsel->needs_swap;
2100 u16 max_size = event->header.size;
2101 const void *endp = (void *)event + max_size;
2105 * used for cross-endian analysis. See git commit 65014ab3
2106 * for why this goofiness is needed.
2110 memset(data, 0, sizeof(*data));
2111 data->cpu = data->pid = data->tid = -1;
2112 data->stream_id = data->id = data->time = -1ULL;
2113 data->period = evsel->core.attr.sample_period;
2114 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2115 data->misc = event->header.misc;
2117 data->data_src = PERF_MEM_DATA_SRC_NONE;
2119 if (event->header.type != PERF_RECORD_SAMPLE) {
2120 if (!evsel->core.attr.sample_id_all)
2122 return perf_evsel__parse_id_sample(evsel, event, data);
2125 array = event->sample.array;
2127 if (perf_event__check_size(event, evsel->sample_size))
2130 if (type & PERF_SAMPLE_IDENTIFIER) {
2135 if (type & PERF_SAMPLE_IP) {
2140 if (type & PERF_SAMPLE_TID) {
2143 /* undo swap of u64, then swap on individual u32s */
2144 u.val64 = bswap_64(u.val64);
2145 u.val32[0] = bswap_32(u.val32[0]);
2146 u.val32[1] = bswap_32(u.val32[1]);
2149 data->pid = u.val32[0];
2150 data->tid = u.val32[1];
2154 if (type & PERF_SAMPLE_TIME) {
2155 data->time = *array;
2159 if (type & PERF_SAMPLE_ADDR) {
2160 data->addr = *array;
2164 if (type & PERF_SAMPLE_ID) {
2169 if (type & PERF_SAMPLE_STREAM_ID) {
2170 data->stream_id = *array;
2174 if (type & PERF_SAMPLE_CPU) {
2178 /* undo swap of u64, then swap on individual u32s */
2179 u.val64 = bswap_64(u.val64);
2180 u.val32[0] = bswap_32(u.val32[0]);
2183 data->cpu = u.val32[0];
2187 if (type & PERF_SAMPLE_PERIOD) {
2188 data->period = *array;
2192 if (type & PERF_SAMPLE_READ) {
2193 u64 read_format = evsel->core.attr.read_format;
2195 OVERFLOW_CHECK_u64(array);
2196 if (read_format & PERF_FORMAT_GROUP)
2197 data->read.group.nr = *array;
2199 data->read.one.value = *array;
2203 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2204 OVERFLOW_CHECK_u64(array);
2205 data->read.time_enabled = *array;
2209 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2210 OVERFLOW_CHECK_u64(array);
2211 data->read.time_running = *array;
2215 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2216 if (read_format & PERF_FORMAT_GROUP) {
2217 const u64 max_group_nr = UINT64_MAX /
2218 sizeof(struct sample_read_value);
2220 if (data->read.group.nr > max_group_nr)
2222 sz = data->read.group.nr *
2223 sizeof(struct sample_read_value);
2224 OVERFLOW_CHECK(array, sz, max_size);
2225 data->read.group.values =
2226 (struct sample_read_value *)array;
2227 array = (void *)array + sz;
2229 OVERFLOW_CHECK_u64(array);
2230 data->read.one.id = *array;
2235 if (evsel__has_callchain(evsel)) {
2236 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
2238 OVERFLOW_CHECK_u64(array);
2239 data->callchain = (struct ip_callchain *)array++;
2240 if (data->callchain->nr > max_callchain_nr)
2242 sz = data->callchain->nr * sizeof(u64);
2243 OVERFLOW_CHECK(array, sz, max_size);
2244 array = (void *)array + sz;
2247 if (type & PERF_SAMPLE_RAW) {
2248 OVERFLOW_CHECK_u64(array);
2252 * Undo swap of u64, then swap on individual u32s,
2253 * get the size of the raw area and undo all of the
2254 * swap. The pevent interface handles endianity by
2258 u.val64 = bswap_64(u.val64);
2259 u.val32[0] = bswap_32(u.val32[0]);
2260 u.val32[1] = bswap_32(u.val32[1]);
2262 data->raw_size = u.val32[0];
2265 * The raw data is aligned on 64bits including the
2266 * u32 size, so it's safe to use mem_bswap_64.
2269 mem_bswap_64((void *) array, data->raw_size);
2271 array = (void *)array + sizeof(u32);
2273 OVERFLOW_CHECK(array, data->raw_size, max_size);
2274 data->raw_data = (void *)array;
2275 array = (void *)array + data->raw_size;
2278 if (type & PERF_SAMPLE_BRANCH_STACK) {
2279 const u64 max_branch_nr = UINT64_MAX /
2280 sizeof(struct branch_entry);
2282 OVERFLOW_CHECK_u64(array);
2283 data->branch_stack = (struct branch_stack *)array++;
2285 if (data->branch_stack->nr > max_branch_nr)
2287 sz = data->branch_stack->nr * sizeof(struct branch_entry);
2288 OVERFLOW_CHECK(array, sz, max_size);
2289 array = (void *)array + sz;
2292 if (type & PERF_SAMPLE_REGS_USER) {
2293 OVERFLOW_CHECK_u64(array);
2294 data->user_regs.abi = *array;
2297 if (data->user_regs.abi) {
2298 u64 mask = evsel->core.attr.sample_regs_user;
2300 sz = hweight64(mask) * sizeof(u64);
2301 OVERFLOW_CHECK(array, sz, max_size);
2302 data->user_regs.mask = mask;
2303 data->user_regs.regs = (u64 *)array;
2304 array = (void *)array + sz;
2308 if (type & PERF_SAMPLE_STACK_USER) {
2309 OVERFLOW_CHECK_u64(array);
2312 data->user_stack.offset = ((char *)(array - 1)
2316 data->user_stack.size = 0;
2318 OVERFLOW_CHECK(array, sz, max_size);
2319 data->user_stack.data = (char *)array;
2320 array = (void *)array + sz;
2321 OVERFLOW_CHECK_u64(array);
2322 data->user_stack.size = *array++;
2323 if (WARN_ONCE(data->user_stack.size > sz,
2324 "user stack dump failure\n"))
2329 if (type & PERF_SAMPLE_WEIGHT) {
2330 OVERFLOW_CHECK_u64(array);
2331 data->weight = *array;
2335 if (type & PERF_SAMPLE_DATA_SRC) {
2336 OVERFLOW_CHECK_u64(array);
2337 data->data_src = *array;
2341 if (type & PERF_SAMPLE_TRANSACTION) {
2342 OVERFLOW_CHECK_u64(array);
2343 data->transaction = *array;
2347 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2348 if (type & PERF_SAMPLE_REGS_INTR) {
2349 OVERFLOW_CHECK_u64(array);
2350 data->intr_regs.abi = *array;
2353 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2354 u64 mask = evsel->core.attr.sample_regs_intr;
2356 sz = hweight64(mask) * sizeof(u64);
2357 OVERFLOW_CHECK(array, sz, max_size);
2358 data->intr_regs.mask = mask;
2359 data->intr_regs.regs = (u64 *)array;
2360 array = (void *)array + sz;
2364 data->phys_addr = 0;
2365 if (type & PERF_SAMPLE_PHYS_ADDR) {
2366 data->phys_addr = *array;
2373 int perf_evsel__parse_sample_timestamp(struct evsel *evsel,
2374 union perf_event *event,
2377 u64 type = evsel->core.attr.sample_type;
2380 if (!(type & PERF_SAMPLE_TIME))
2383 if (event->header.type != PERF_RECORD_SAMPLE) {
2384 struct perf_sample data = {
2388 if (!evsel->core.attr.sample_id_all)
2390 if (perf_evsel__parse_id_sample(evsel, event, &data))
2393 *timestamp = data.time;
2397 array = event->sample.array;
2399 if (perf_event__check_size(event, evsel->sample_size))
2402 if (type & PERF_SAMPLE_IDENTIFIER)
2405 if (type & PERF_SAMPLE_IP)
2408 if (type & PERF_SAMPLE_TID)
2411 if (type & PERF_SAMPLE_TIME)
2412 *timestamp = *array;
2417 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
2420 size_t sz, result = sizeof(struct sample_event);
2422 if (type & PERF_SAMPLE_IDENTIFIER)
2423 result += sizeof(u64);
2425 if (type & PERF_SAMPLE_IP)
2426 result += sizeof(u64);
2428 if (type & PERF_SAMPLE_TID)
2429 result += sizeof(u64);
2431 if (type & PERF_SAMPLE_TIME)
2432 result += sizeof(u64);
2434 if (type & PERF_SAMPLE_ADDR)
2435 result += sizeof(u64);
2437 if (type & PERF_SAMPLE_ID)
2438 result += sizeof(u64);
2440 if (type & PERF_SAMPLE_STREAM_ID)
2441 result += sizeof(u64);
2443 if (type & PERF_SAMPLE_CPU)
2444 result += sizeof(u64);
2446 if (type & PERF_SAMPLE_PERIOD)
2447 result += sizeof(u64);
2449 if (type & PERF_SAMPLE_READ) {
2450 result += sizeof(u64);
2451 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2452 result += sizeof(u64);
2453 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2454 result += sizeof(u64);
2455 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2456 if (read_format & PERF_FORMAT_GROUP) {
2457 sz = sample->read.group.nr *
2458 sizeof(struct sample_read_value);
2461 result += sizeof(u64);
2465 if (type & PERF_SAMPLE_CALLCHAIN) {
2466 sz = (sample->callchain->nr + 1) * sizeof(u64);
2470 if (type & PERF_SAMPLE_RAW) {
2471 result += sizeof(u32);
2472 result += sample->raw_size;
2475 if (type & PERF_SAMPLE_BRANCH_STACK) {
2476 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2481 if (type & PERF_SAMPLE_REGS_USER) {
2482 if (sample->user_regs.abi) {
2483 result += sizeof(u64);
2484 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
2487 result += sizeof(u64);
2491 if (type & PERF_SAMPLE_STACK_USER) {
2492 sz = sample->user_stack.size;
2493 result += sizeof(u64);
2496 result += sizeof(u64);
2500 if (type & PERF_SAMPLE_WEIGHT)
2501 result += sizeof(u64);
2503 if (type & PERF_SAMPLE_DATA_SRC)
2504 result += sizeof(u64);
2506 if (type & PERF_SAMPLE_TRANSACTION)
2507 result += sizeof(u64);
2509 if (type & PERF_SAMPLE_REGS_INTR) {
2510 if (sample->intr_regs.abi) {
2511 result += sizeof(u64);
2512 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
2515 result += sizeof(u64);
2519 if (type & PERF_SAMPLE_PHYS_ADDR)
2520 result += sizeof(u64);
2525 int perf_event__synthesize_sample(union perf_event *event, u64 type,
2527 const struct perf_sample *sample)
2532 * used for cross-endian analysis. See git commit 65014ab3
2533 * for why this goofiness is needed.
2537 array = event->sample.array;
2539 if (type & PERF_SAMPLE_IDENTIFIER) {
2540 *array = sample->id;
2544 if (type & PERF_SAMPLE_IP) {
2545 *array = sample->ip;
2549 if (type & PERF_SAMPLE_TID) {
2550 u.val32[0] = sample->pid;
2551 u.val32[1] = sample->tid;
2556 if (type & PERF_SAMPLE_TIME) {
2557 *array = sample->time;
2561 if (type & PERF_SAMPLE_ADDR) {
2562 *array = sample->addr;
2566 if (type & PERF_SAMPLE_ID) {
2567 *array = sample->id;
2571 if (type & PERF_SAMPLE_STREAM_ID) {
2572 *array = sample->stream_id;
2576 if (type & PERF_SAMPLE_CPU) {
2577 u.val32[0] = sample->cpu;
2583 if (type & PERF_SAMPLE_PERIOD) {
2584 *array = sample->period;
2588 if (type & PERF_SAMPLE_READ) {
2589 if (read_format & PERF_FORMAT_GROUP)
2590 *array = sample->read.group.nr;
2592 *array = sample->read.one.value;
2595 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2596 *array = sample->read.time_enabled;
2600 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2601 *array = sample->read.time_running;
2605 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2606 if (read_format & PERF_FORMAT_GROUP) {
2607 sz = sample->read.group.nr *
2608 sizeof(struct sample_read_value);
2609 memcpy(array, sample->read.group.values, sz);
2610 array = (void *)array + sz;
2612 *array = sample->read.one.id;
2617 if (type & PERF_SAMPLE_CALLCHAIN) {
2618 sz = (sample->callchain->nr + 1) * sizeof(u64);
2619 memcpy(array, sample->callchain, sz);
2620 array = (void *)array + sz;
2623 if (type & PERF_SAMPLE_RAW) {
2624 u.val32[0] = sample->raw_size;
2626 array = (void *)array + sizeof(u32);
2628 memcpy(array, sample->raw_data, sample->raw_size);
2629 array = (void *)array + sample->raw_size;
2632 if (type & PERF_SAMPLE_BRANCH_STACK) {
2633 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2635 memcpy(array, sample->branch_stack, sz);
2636 array = (void *)array + sz;
2639 if (type & PERF_SAMPLE_REGS_USER) {
2640 if (sample->user_regs.abi) {
2641 *array++ = sample->user_regs.abi;
2642 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
2643 memcpy(array, sample->user_regs.regs, sz);
2644 array = (void *)array + sz;
2650 if (type & PERF_SAMPLE_STACK_USER) {
2651 sz = sample->user_stack.size;
2654 memcpy(array, sample->user_stack.data, sz);
2655 array = (void *)array + sz;
2660 if (type & PERF_SAMPLE_WEIGHT) {
2661 *array = sample->weight;
2665 if (type & PERF_SAMPLE_DATA_SRC) {
2666 *array = sample->data_src;
2670 if (type & PERF_SAMPLE_TRANSACTION) {
2671 *array = sample->transaction;
2675 if (type & PERF_SAMPLE_REGS_INTR) {
2676 if (sample->intr_regs.abi) {
2677 *array++ = sample->intr_regs.abi;
2678 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
2679 memcpy(array, sample->intr_regs.regs, sz);
2680 array = (void *)array + sz;
2686 if (type & PERF_SAMPLE_PHYS_ADDR) {
2687 *array = sample->phys_addr;
2694 struct tep_format_field *perf_evsel__field(struct evsel *evsel, const char *name)
2696 return tep_find_field(evsel->tp_format, name);
2699 void *perf_evsel__rawptr(struct evsel *evsel, struct perf_sample *sample,
2702 struct tep_format_field *field = perf_evsel__field(evsel, name);
2708 offset = field->offset;
2710 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2711 offset = *(int *)(sample->raw_data + field->offset);
2715 return sample->raw_data + offset;
2718 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
2722 void *ptr = sample->raw_data + field->offset;
2724 switch (field->size) {
2728 value = *(u16 *)ptr;
2731 value = *(u32 *)ptr;
2734 memcpy(&value, ptr, sizeof(u64));
2743 switch (field->size) {
2745 return bswap_16(value);
2747 return bswap_32(value);
2749 return bswap_64(value);
2757 u64 perf_evsel__intval(struct evsel *evsel, struct perf_sample *sample,
2760 struct tep_format_field *field = perf_evsel__field(evsel, name);
2765 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2768 bool perf_evsel__fallback(struct evsel *evsel, int err,
2769 char *msg, size_t msgsize)
2773 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2774 evsel->core.attr.type == PERF_TYPE_HARDWARE &&
2775 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2777 * If it's cycles then fall back to hrtimer based
2778 * cpu-clock-tick sw counter, which is always available even if
2781 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2784 scnprintf(msg, msgsize, "%s",
2785 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2787 evsel->core.attr.type = PERF_TYPE_SOFTWARE;
2788 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK;
2790 zfree(&evsel->name);
2792 } else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
2793 (paranoid = perf_event_paranoid()) > 1) {
2794 const char *name = perf_evsel__name(evsel);
2796 const char *sep = ":";
2798 /* Is there already the separator in the name. */
2799 if (strchr(name, '/') ||
2803 if (asprintf(&new_name, "%s%su", name, sep) < 0)
2808 evsel->name = new_name;
2809 scnprintf(msg, msgsize,
2810 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2811 evsel->core.attr.exclude_kernel = 1;
2819 static bool find_process(const char *name)
2821 size_t len = strlen(name);
2826 dir = opendir(procfs__mountpoint());
2830 /* Walk through the directory. */
2831 while (ret && (d = readdir(dir)) != NULL) {
2832 char path[PATH_MAX];
2836 if ((d->d_type != DT_DIR) ||
2837 !strcmp(".", d->d_name) ||
2838 !strcmp("..", d->d_name))
2841 scnprintf(path, sizeof(path), "%s/%s/comm",
2842 procfs__mountpoint(), d->d_name);
2844 if (filename__read_str(path, &data, &size))
2847 ret = strncmp(name, data, len);
2852 return ret ? false : true;
2855 int perf_evsel__open_strerror(struct evsel *evsel, struct target *target,
2856 int err, char *msg, size_t size)
2858 char sbuf[STRERR_BUFSIZE];
2865 printed = scnprintf(msg, size,
2866 "No permission to enable %s event.\n\n",
2867 perf_evsel__name(evsel));
2869 return scnprintf(msg + printed, size - printed,
2870 "You may not have permission to collect %sstats.\n\n"
2871 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2872 "which controls use of the performance events system by\n"
2873 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2874 "The current value is %d:\n\n"
2875 " -1: Allow use of (almost) all events by all users\n"
2876 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
2877 ">= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN\n"
2878 " Disallow raw tracepoint access by users without CAP_SYS_ADMIN\n"
2879 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2880 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2881 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2882 " kernel.perf_event_paranoid = -1\n" ,
2883 target->system_wide ? "system-wide " : "",
2884 perf_event_paranoid());
2886 return scnprintf(msg, size, "The %s event is not supported.",
2887 perf_evsel__name(evsel));
2889 return scnprintf(msg, size, "%s",
2890 "Too many events are opened.\n"
2891 "Probably the maximum number of open file descriptors has been reached.\n"
2892 "Hint: Try again after reducing the number of events.\n"
2893 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2895 if (evsel__has_callchain(evsel) &&
2896 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2897 return scnprintf(msg, size,
2898 "Not enough memory to setup event with callchain.\n"
2899 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2900 "Hint: Current value: %d", sysctl__max_stack());
2903 if (target->cpu_list)
2904 return scnprintf(msg, size, "%s",
2905 "No such device - did you specify an out-of-range profile CPU?");
2908 if (evsel->core.attr.sample_period != 0)
2909 return scnprintf(msg, size,
2910 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
2911 perf_evsel__name(evsel));
2912 if (evsel->core.attr.precise_ip)
2913 return scnprintf(msg, size, "%s",
2914 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2915 #if defined(__i386__) || defined(__x86_64__)
2916 if (evsel->core.attr.type == PERF_TYPE_HARDWARE)
2917 return scnprintf(msg, size, "%s",
2918 "No hardware sampling interrupt available.\n");
2922 if (find_process("oprofiled"))
2923 return scnprintf(msg, size,
2924 "The PMU counters are busy/taken by another profiler.\n"
2925 "We found oprofile daemon running, please stop it and try again.");
2928 if (evsel->core.attr.write_backward && perf_missing_features.write_backward)
2929 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
2930 if (perf_missing_features.clockid)
2931 return scnprintf(msg, size, "clockid feature not supported.");
2932 if (perf_missing_features.clockid_wrong)
2933 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2934 if (perf_missing_features.aux_output)
2935 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel.");
2941 return scnprintf(msg, size,
2942 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2943 "/bin/dmesg | grep -i perf may provide additional information.\n",
2944 err, str_error_r(err, sbuf, sizeof(sbuf)),
2945 perf_evsel__name(evsel));
2948 struct perf_env *perf_evsel__env(struct evsel *evsel)
2950 if (evsel && evsel->evlist)
2951 return evsel->evlist->env;
2955 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
2959 for (cpu = 0; cpu < xyarray__max_x(evsel->core.fd); cpu++) {
2960 for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
2962 int fd = FD(evsel, cpu, thread);
2964 if (perf_evlist__id_add_fd(evlist, evsel,
2965 cpu, thread, fd) < 0)
2973 int perf_evsel__store_ids(struct evsel *evsel, struct evlist *evlist)
2975 struct perf_cpu_map *cpus = evsel->core.cpus;
2976 struct perf_thread_map *threads = evsel->core.threads;
2978 if (perf_evsel__alloc_id(evsel, cpus->nr, threads->nr))
2981 return store_evsel_ids(evsel, evlist);