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 <linux/hw_breakpoint.h>
16 #include <linux/perf_event.h>
17 #include <linux/compiler.h>
18 #include <linux/err.h>
19 #include <linux/zalloc.h>
20 #include <sys/ioctl.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
25 #include <perf/evsel.h>
27 #include "bpf_counter.h"
28 #include "callchain.h"
34 #include "util/evsel_config.h"
35 #include "util/evsel_fprintf.h"
37 #include <perf/cpumap.h>
38 #include "thread_map.h"
40 #include "perf_regs.h"
43 #include "trace-event.h"
48 #include "util/hashmap.h"
49 #include "pmu-hybrid.h"
51 #include "../perf-sys.h"
52 #include "util/parse-branch-options.h"
53 #include "util/bpf-filter.h"
54 #include <internal/xyarray.h>
55 #include <internal/lib.h>
56 #include <internal/threadmap.h>
58 #include <linux/ctype.h>
60 #ifdef HAVE_LIBTRACEEVENT
61 #include <traceevent/event-parse.h>
64 struct perf_missing_features perf_missing_features;
66 static clockid_t clockid;
68 static const char *const perf_tool_event__tool_names[PERF_TOOL_MAX] = {
75 const char *perf_tool_event__to_str(enum perf_tool_event ev)
77 if (ev > PERF_TOOL_NONE && ev < PERF_TOOL_MAX)
78 return perf_tool_event__tool_names[ev];
83 enum perf_tool_event perf_tool_event__from_str(const char *str)
87 perf_tool_event__for_each_event(i) {
88 if (!strcmp(str, perf_tool_event__tool_names[i]))
91 return PERF_TOOL_NONE;
95 static int evsel__no_extra_init(struct evsel *evsel __maybe_unused)
100 void __weak test_attr__ready(void) { }
102 static void evsel__no_extra_fini(struct evsel *evsel __maybe_unused)
108 int (*init)(struct evsel *evsel);
109 void (*fini)(struct evsel *evsel);
110 } perf_evsel__object = {
111 .size = sizeof(struct evsel),
112 .init = evsel__no_extra_init,
113 .fini = evsel__no_extra_fini,
116 int evsel__object_config(size_t object_size, int (*init)(struct evsel *evsel),
117 void (*fini)(struct evsel *evsel))
120 if (object_size == 0)
123 if (perf_evsel__object.size > object_size)
126 perf_evsel__object.size = object_size;
130 perf_evsel__object.init = init;
133 perf_evsel__object.fini = fini;
138 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
140 int __evsel__sample_size(u64 sample_type)
142 u64 mask = sample_type & PERF_SAMPLE_MASK;
146 for (i = 0; i < 64; i++) {
147 if (mask & (1ULL << i))
157 * __perf_evsel__calc_id_pos - calculate id_pos.
158 * @sample_type: sample type
160 * This function returns the position of the event id (PERF_SAMPLE_ID or
161 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
162 * perf_record_sample.
164 static int __perf_evsel__calc_id_pos(u64 sample_type)
168 if (sample_type & PERF_SAMPLE_IDENTIFIER)
171 if (!(sample_type & PERF_SAMPLE_ID))
174 if (sample_type & PERF_SAMPLE_IP)
177 if (sample_type & PERF_SAMPLE_TID)
180 if (sample_type & PERF_SAMPLE_TIME)
183 if (sample_type & PERF_SAMPLE_ADDR)
190 * __perf_evsel__calc_is_pos - calculate is_pos.
191 * @sample_type: sample type
193 * This function returns the position (counting backwards) of the event id
194 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
195 * sample_id_all is used there is an id sample appended to non-sample events.
197 static int __perf_evsel__calc_is_pos(u64 sample_type)
201 if (sample_type & PERF_SAMPLE_IDENTIFIER)
204 if (!(sample_type & PERF_SAMPLE_ID))
207 if (sample_type & PERF_SAMPLE_CPU)
210 if (sample_type & PERF_SAMPLE_STREAM_ID)
216 void evsel__calc_id_pos(struct evsel *evsel)
218 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type);
219 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type);
222 void __evsel__set_sample_bit(struct evsel *evsel,
223 enum perf_event_sample_format bit)
225 if (!(evsel->core.attr.sample_type & bit)) {
226 evsel->core.attr.sample_type |= bit;
227 evsel->sample_size += sizeof(u64);
228 evsel__calc_id_pos(evsel);
232 void __evsel__reset_sample_bit(struct evsel *evsel,
233 enum perf_event_sample_format bit)
235 if (evsel->core.attr.sample_type & bit) {
236 evsel->core.attr.sample_type &= ~bit;
237 evsel->sample_size -= sizeof(u64);
238 evsel__calc_id_pos(evsel);
242 void evsel__set_sample_id(struct evsel *evsel,
243 bool can_sample_identifier)
245 if (can_sample_identifier) {
246 evsel__reset_sample_bit(evsel, ID);
247 evsel__set_sample_bit(evsel, IDENTIFIER);
249 evsel__set_sample_bit(evsel, ID);
251 evsel->core.attr.read_format |= PERF_FORMAT_ID;
255 * evsel__is_function_event - Return whether given evsel is a function
258 * @evsel - evsel selector to be tested
260 * Return %true if event is function trace event
262 bool evsel__is_function_event(struct evsel *evsel)
264 #define FUNCTION_EVENT "ftrace:function"
266 return evsel->name &&
267 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
269 #undef FUNCTION_EVENT
272 void evsel__init(struct evsel *evsel,
273 struct perf_event_attr *attr, int idx)
275 perf_evsel__init(&evsel->core, attr, idx);
276 evsel->tracking = !idx;
277 evsel->unit = strdup("");
279 evsel->max_events = ULONG_MAX;
280 evsel->evlist = NULL;
281 evsel->bpf_obj = NULL;
283 INIT_LIST_HEAD(&evsel->config_terms);
284 INIT_LIST_HEAD(&evsel->bpf_counter_list);
285 INIT_LIST_HEAD(&evsel->bpf_filters);
286 perf_evsel__object.init(evsel);
287 evsel->sample_size = __evsel__sample_size(attr->sample_type);
288 evsel__calc_id_pos(evsel);
289 evsel->cmdline_group_boundary = false;
290 evsel->metric_events = NULL;
291 evsel->per_pkg_mask = NULL;
292 evsel->collect_stat = false;
293 evsel->pmu_name = NULL;
294 evsel->skippable = false;
297 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx)
299 struct evsel *evsel = zalloc(perf_evsel__object.size);
303 evsel__init(evsel, attr, idx);
305 if (evsel__is_bpf_output(evsel) && !attr->sample_type) {
306 evsel->core.attr.sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
307 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
308 evsel->core.attr.sample_period = 1;
311 if (evsel__is_clock(evsel)) {
312 free((char *)evsel->unit);
313 evsel->unit = strdup("msec");
320 static bool perf_event_can_profile_kernel(void)
322 return perf_event_paranoid_check(1);
325 struct evsel *evsel__new_cycles(bool precise __maybe_unused, __u32 type, __u64 config)
327 struct perf_event_attr attr = {
330 .exclude_kernel = !perf_event_can_profile_kernel(),
334 event_attr_init(&attr);
337 * Now let the usual logic to set up the perf_event_attr defaults
338 * to kick in when we return and before perf_evsel__open() is called.
340 evsel = evsel__new(&attr);
344 arch_evsel__fixup_new_cycles(&evsel->core.attr);
346 evsel->precise_max = true;
348 /* use asprintf() because free(evsel) assumes name is allocated */
349 if (asprintf(&evsel->name, "cycles%s%s%.*s",
350 (attr.precise_ip || attr.exclude_kernel) ? ":" : "",
351 attr.exclude_kernel ? "u" : "",
352 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0)
357 evsel__delete(evsel);
362 int copy_config_terms(struct list_head *dst, struct list_head *src)
364 struct evsel_config_term *pos, *tmp;
366 list_for_each_entry(pos, src, list) {
367 tmp = malloc(sizeof(*tmp));
373 tmp->val.str = strdup(pos->val.str);
374 if (tmp->val.str == NULL) {
379 list_add_tail(&tmp->list, dst);
384 static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)
386 return copy_config_terms(&dst->config_terms, &src->config_terms);
390 * evsel__clone - create a new evsel copied from @orig
391 * @orig: original evsel
393 * The assumption is that @orig is not configured nor opened yet.
394 * So we only care about the attributes that can be set while it's parsed.
396 struct evsel *evsel__clone(struct evsel *orig)
400 BUG_ON(orig->core.fd);
401 BUG_ON(orig->counts);
403 BUG_ON(orig->per_pkg_mask);
405 /* cannot handle BPF objects for now */
409 evsel = evsel__new(&orig->core.attr);
413 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus);
414 evsel->core.own_cpus = perf_cpu_map__get(orig->core.own_cpus);
415 evsel->core.threads = perf_thread_map__get(orig->core.threads);
416 evsel->core.nr_members = orig->core.nr_members;
417 evsel->core.system_wide = orig->core.system_wide;
418 evsel->core.requires_cpu = orig->core.requires_cpu;
421 evsel->name = strdup(orig->name);
422 if (evsel->name == NULL)
425 if (orig->group_name) {
426 evsel->group_name = strdup(orig->group_name);
427 if (evsel->group_name == NULL)
430 if (orig->pmu_name) {
431 evsel->pmu_name = strdup(orig->pmu_name);
432 if (evsel->pmu_name == NULL)
436 evsel->filter = strdup(orig->filter);
437 if (evsel->filter == NULL)
440 if (orig->metric_id) {
441 evsel->metric_id = strdup(orig->metric_id);
442 if (evsel->metric_id == NULL)
445 evsel->cgrp = cgroup__get(orig->cgrp);
446 #ifdef HAVE_LIBTRACEEVENT
447 evsel->tp_format = orig->tp_format;
449 evsel->handler = orig->handler;
450 evsel->core.leader = orig->core.leader;
452 evsel->max_events = orig->max_events;
453 evsel->tool_event = orig->tool_event;
454 free((char *)evsel->unit);
455 evsel->unit = strdup(orig->unit);
456 if (evsel->unit == NULL)
459 evsel->scale = orig->scale;
460 evsel->snapshot = orig->snapshot;
461 evsel->per_pkg = orig->per_pkg;
462 evsel->percore = orig->percore;
463 evsel->precise_max = orig->precise_max;
464 evsel->is_libpfm_event = orig->is_libpfm_event;
466 evsel->exclude_GH = orig->exclude_GH;
467 evsel->sample_read = orig->sample_read;
468 evsel->auto_merge_stats = orig->auto_merge_stats;
469 evsel->collect_stat = orig->collect_stat;
470 evsel->weak_group = orig->weak_group;
471 evsel->use_config_name = orig->use_config_name;
472 evsel->pmu = orig->pmu;
474 if (evsel__copy_config_terms(evsel, orig) < 0)
480 evsel__delete(evsel);
485 * Returns pointer with encoded error via <linux/err.h> interface.
487 #ifdef HAVE_LIBTRACEEVENT
488 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx)
490 struct evsel *evsel = zalloc(perf_evsel__object.size);
496 struct perf_event_attr attr = {
497 .type = PERF_TYPE_TRACEPOINT,
498 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
499 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
502 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
505 evsel->tp_format = trace_event__tp_format(sys, name);
506 if (IS_ERR(evsel->tp_format)) {
507 err = PTR_ERR(evsel->tp_format);
511 event_attr_init(&attr);
512 attr.config = evsel->tp_format->id;
513 attr.sample_period = 1;
514 evsel__init(evsel, &attr, idx);
527 const char *const evsel__hw_names[PERF_COUNT_HW_MAX] = {
535 "stalled-cycles-frontend",
536 "stalled-cycles-backend",
540 char *evsel__bpf_counter_events;
542 bool evsel__match_bpf_counter_events(const char *name)
548 if (!evsel__bpf_counter_events)
551 ptr = strstr(evsel__bpf_counter_events, name);
552 name_len = strlen(name);
554 /* check name matches a full token in evsel__bpf_counter_events */
555 match = (ptr != NULL) &&
556 ((ptr == evsel__bpf_counter_events) || (*(ptr - 1) == ',')) &&
557 ((*(ptr + name_len) == ',') || (*(ptr + name_len) == '\0'));
562 static const char *__evsel__hw_name(u64 config)
564 if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config])
565 return evsel__hw_names[config];
567 return "unknown-hardware";
570 static int evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size)
572 int colon = 0, r = 0;
573 struct perf_event_attr *attr = &evsel->core.attr;
574 bool exclude_guest_default = false;
576 #define MOD_PRINT(context, mod) do { \
577 if (!attr->exclude_##context) { \
578 if (!colon) colon = ++r; \
579 r += scnprintf(bf + r, size - r, "%c", mod); \
582 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
583 MOD_PRINT(kernel, 'k');
584 MOD_PRINT(user, 'u');
586 exclude_guest_default = true;
589 if (attr->precise_ip) {
592 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
593 exclude_guest_default = true;
596 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
597 MOD_PRINT(host, 'H');
598 MOD_PRINT(guest, 'G');
606 int __weak arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
608 return scnprintf(bf, size, "%s", __evsel__hw_name(evsel->core.attr.config));
611 static int evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
613 int r = arch_evsel__hw_name(evsel, bf, size);
614 return r + evsel__add_modifiers(evsel, bf + r, size - r);
617 const char *const evsel__sw_names[PERF_COUNT_SW_MAX] = {
630 static const char *__evsel__sw_name(u64 config)
632 if (config < PERF_COUNT_SW_MAX && evsel__sw_names[config])
633 return evsel__sw_names[config];
634 return "unknown-software";
637 static int evsel__sw_name(struct evsel *evsel, char *bf, size_t size)
639 int r = scnprintf(bf, size, "%s", __evsel__sw_name(evsel->core.attr.config));
640 return r + evsel__add_modifiers(evsel, bf + r, size - r);
643 static int evsel__tool_name(enum perf_tool_event ev, char *bf, size_t size)
645 return scnprintf(bf, size, "%s", perf_tool_event__to_str(ev));
648 static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
652 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
654 if (type & HW_BREAKPOINT_R)
655 r += scnprintf(bf + r, size - r, "r");
657 if (type & HW_BREAKPOINT_W)
658 r += scnprintf(bf + r, size - r, "w");
660 if (type & HW_BREAKPOINT_X)
661 r += scnprintf(bf + r, size - r, "x");
666 static int evsel__bp_name(struct evsel *evsel, char *bf, size_t size)
668 struct perf_event_attr *attr = &evsel->core.attr;
669 int r = __evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
670 return r + evsel__add_modifiers(evsel, bf + r, size - r);
673 const char *const evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES] = {
674 { "L1-dcache", "l1-d", "l1d", "L1-data", },
675 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
677 { "dTLB", "d-tlb", "Data-TLB", },
678 { "iTLB", "i-tlb", "Instruction-TLB", },
679 { "branch", "branches", "bpu", "btb", "bpc", },
683 const char *const evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES] = {
684 { "load", "loads", "read", },
685 { "store", "stores", "write", },
686 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
689 const char *const evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES] = {
690 { "refs", "Reference", "ops", "access", },
691 { "misses", "miss", },
694 #define C(x) PERF_COUNT_HW_CACHE_##x
695 #define CACHE_READ (1 << C(OP_READ))
696 #define CACHE_WRITE (1 << C(OP_WRITE))
697 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
698 #define COP(x) (1 << x)
701 * cache operation stat
702 * L1I : Read and prefetch only
703 * ITLB and BPU : Read-only
705 static const unsigned long evsel__hw_cache_stat[C(MAX)] = {
706 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
707 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
708 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
709 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
710 [C(ITLB)] = (CACHE_READ),
711 [C(BPU)] = (CACHE_READ),
712 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
715 bool evsel__is_cache_op_valid(u8 type, u8 op)
717 if (evsel__hw_cache_stat[type] & COP(op))
718 return true; /* valid */
720 return false; /* invalid */
723 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size)
726 return scnprintf(bf, size, "%s-%s-%s", evsel__hw_cache[type][0],
727 evsel__hw_cache_op[op][0],
728 evsel__hw_cache_result[result][0]);
731 return scnprintf(bf, size, "%s-%s", evsel__hw_cache[type][0],
732 evsel__hw_cache_op[op][1]);
735 static int __evsel__hw_cache_name(u64 config, char *bf, size_t size)
737 u8 op, result, type = (config >> 0) & 0xff;
738 const char *err = "unknown-ext-hardware-cache-type";
740 if (type >= PERF_COUNT_HW_CACHE_MAX)
743 op = (config >> 8) & 0xff;
744 err = "unknown-ext-hardware-cache-op";
745 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
748 result = (config >> 16) & 0xff;
749 err = "unknown-ext-hardware-cache-result";
750 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
753 err = "invalid-cache";
754 if (!evsel__is_cache_op_valid(type, op))
757 return __evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
759 return scnprintf(bf, size, "%s", err);
762 static int evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size)
764 int ret = __evsel__hw_cache_name(evsel->core.attr.config, bf, size);
765 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret);
768 static int evsel__raw_name(struct evsel *evsel, char *bf, size_t size)
770 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config);
771 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret);
774 const char *evsel__name(struct evsel *evsel)
784 switch (evsel->core.attr.type) {
786 evsel__raw_name(evsel, bf, sizeof(bf));
789 case PERF_TYPE_HARDWARE:
790 evsel__hw_name(evsel, bf, sizeof(bf));
793 case PERF_TYPE_HW_CACHE:
794 evsel__hw_cache_name(evsel, bf, sizeof(bf));
797 case PERF_TYPE_SOFTWARE:
798 if (evsel__is_tool(evsel))
799 evsel__tool_name(evsel->tool_event, bf, sizeof(bf));
801 evsel__sw_name(evsel, bf, sizeof(bf));
804 case PERF_TYPE_TRACEPOINT:
805 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
808 case PERF_TYPE_BREAKPOINT:
809 evsel__bp_name(evsel, bf, sizeof(bf));
813 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
814 evsel->core.attr.type);
818 evsel->name = strdup(bf);
826 bool evsel__name_is(struct evsel *evsel, const char *name)
828 return !strcmp(evsel__name(evsel), name);
831 const char *evsel__group_pmu_name(const struct evsel *evsel)
833 struct evsel *leader = evsel__leader(evsel);
837 * Software events may be in a group with other uncore PMU events. Use
838 * the pmu_name of the first non-software event to avoid breaking the
839 * software event out of the group.
841 * Aux event leaders, like intel_pt, expect a group with events from
842 * other PMUs, so substitute the AUX event's PMU in this case.
844 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE || evsel__is_aux_event(leader)) {
845 /* Starting with the leader, find the first event with a named PMU. */
846 for_each_group_evsel(pos, leader) {
848 return pos->pmu_name;
852 return evsel->pmu_name ?: "cpu";
855 const char *evsel__metric_id(const struct evsel *evsel)
857 if (evsel->metric_id)
858 return evsel->metric_id;
860 if (evsel__is_tool(evsel))
861 return perf_tool_event__to_str(evsel->tool_event);
866 const char *evsel__group_name(struct evsel *evsel)
868 return evsel->group_name ?: "anon group";
872 * Returns the group details for the specified leader,
873 * with following rules.
875 * For record -e '{cycles,instructions}'
876 * 'anon group { cycles:u, instructions:u }'
878 * For record -e 'cycles,instructions' and report --group
879 * 'cycles:u, instructions:u'
881 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size)
885 const char *group_name = evsel__group_name(evsel);
887 if (!evsel->forced_leader)
888 ret = scnprintf(buf, size, "%s { ", group_name);
890 ret += scnprintf(buf + ret, size - ret, "%s", evsel__name(evsel));
892 for_each_group_member(pos, evsel)
893 ret += scnprintf(buf + ret, size - ret, ", %s", evsel__name(pos));
895 if (!evsel->forced_leader)
896 ret += scnprintf(buf + ret, size - ret, " }");
901 static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
902 struct callchain_param *param)
904 bool function = evsel__is_function_event(evsel);
905 struct perf_event_attr *attr = &evsel->core.attr;
907 evsel__set_sample_bit(evsel, CALLCHAIN);
909 attr->sample_max_stack = param->max_stack;
911 if (opts->kernel_callchains)
912 attr->exclude_callchain_user = 1;
913 if (opts->user_callchains)
914 attr->exclude_callchain_kernel = 1;
915 if (param->record_mode == CALLCHAIN_LBR) {
916 if (!opts->branch_stack) {
917 if (attr->exclude_user) {
918 pr_warning("LBR callstack option is only available "
919 "to get user callchain information. "
920 "Falling back to framepointers.\n");
922 evsel__set_sample_bit(evsel, BRANCH_STACK);
923 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
924 PERF_SAMPLE_BRANCH_CALL_STACK |
925 PERF_SAMPLE_BRANCH_NO_CYCLES |
926 PERF_SAMPLE_BRANCH_NO_FLAGS |
927 PERF_SAMPLE_BRANCH_HW_INDEX;
930 pr_warning("Cannot use LBR callstack with branch stack. "
931 "Falling back to framepointers.\n");
934 if (param->record_mode == CALLCHAIN_DWARF) {
936 evsel__set_sample_bit(evsel, REGS_USER);
937 evsel__set_sample_bit(evsel, STACK_USER);
938 if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) {
939 attr->sample_regs_user |= DWARF_MINIMAL_REGS;
940 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, "
941 "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
942 "so the minimal registers set (IP, SP) is explicitly forced.\n");
944 attr->sample_regs_user |= arch__user_reg_mask();
946 attr->sample_stack_user = param->dump_size;
947 attr->exclude_callchain_user = 1;
949 pr_info("Cannot use DWARF unwind for function trace event,"
950 " falling back to framepointers.\n");
955 pr_info("Disabling user space callchains for function trace event.\n");
956 attr->exclude_callchain_user = 1;
960 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
961 struct callchain_param *param)
964 return __evsel__config_callchain(evsel, opts, param);
967 static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *param)
969 struct perf_event_attr *attr = &evsel->core.attr;
971 evsel__reset_sample_bit(evsel, CALLCHAIN);
972 if (param->record_mode == CALLCHAIN_LBR) {
973 evsel__reset_sample_bit(evsel, BRANCH_STACK);
974 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
975 PERF_SAMPLE_BRANCH_CALL_STACK |
976 PERF_SAMPLE_BRANCH_HW_INDEX);
978 if (param->record_mode == CALLCHAIN_DWARF) {
979 evsel__reset_sample_bit(evsel, REGS_USER);
980 evsel__reset_sample_bit(evsel, STACK_USER);
984 static void evsel__apply_config_terms(struct evsel *evsel,
985 struct record_opts *opts, bool track)
987 struct evsel_config_term *term;
988 struct list_head *config_terms = &evsel->config_terms;
989 struct perf_event_attr *attr = &evsel->core.attr;
990 /* callgraph default */
991 struct callchain_param param = {
992 .record_mode = callchain_param.record_mode,
996 const char *callgraph_buf = NULL;
998 list_for_each_entry(term, config_terms, list) {
999 switch (term->type) {
1000 case EVSEL__CONFIG_TERM_PERIOD:
1001 if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
1002 attr->sample_period = term->val.period;
1004 evsel__reset_sample_bit(evsel, PERIOD);
1007 case EVSEL__CONFIG_TERM_FREQ:
1008 if (!(term->weak && opts->user_freq != UINT_MAX)) {
1009 attr->sample_freq = term->val.freq;
1011 evsel__set_sample_bit(evsel, PERIOD);
1014 case EVSEL__CONFIG_TERM_TIME:
1016 evsel__set_sample_bit(evsel, TIME);
1018 evsel__reset_sample_bit(evsel, TIME);
1020 case EVSEL__CONFIG_TERM_CALLGRAPH:
1021 callgraph_buf = term->val.str;
1023 case EVSEL__CONFIG_TERM_BRANCH:
1024 if (term->val.str && strcmp(term->val.str, "no")) {
1025 evsel__set_sample_bit(evsel, BRANCH_STACK);
1026 parse_branch_str(term->val.str,
1027 &attr->branch_sample_type);
1029 evsel__reset_sample_bit(evsel, BRANCH_STACK);
1031 case EVSEL__CONFIG_TERM_STACK_USER:
1032 dump_size = term->val.stack_user;
1034 case EVSEL__CONFIG_TERM_MAX_STACK:
1035 max_stack = term->val.max_stack;
1037 case EVSEL__CONFIG_TERM_MAX_EVENTS:
1038 evsel->max_events = term->val.max_events;
1040 case EVSEL__CONFIG_TERM_INHERIT:
1042 * attr->inherit should has already been set by
1043 * evsel__config. If user explicitly set
1044 * inherit using config terms, override global
1045 * opt->no_inherit setting.
1047 attr->inherit = term->val.inherit ? 1 : 0;
1049 case EVSEL__CONFIG_TERM_OVERWRITE:
1050 attr->write_backward = term->val.overwrite ? 1 : 0;
1052 case EVSEL__CONFIG_TERM_DRV_CFG:
1054 case EVSEL__CONFIG_TERM_PERCORE:
1056 case EVSEL__CONFIG_TERM_AUX_OUTPUT:
1057 attr->aux_output = term->val.aux_output ? 1 : 0;
1059 case EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE:
1060 /* Already applied by auxtrace */
1062 case EVSEL__CONFIG_TERM_CFG_CHG:
1069 /* User explicitly set per-event callgraph, clear the old setting and reset. */
1070 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
1071 bool sample_address = false;
1074 param.max_stack = max_stack;
1075 if (callgraph_buf == NULL)
1076 callgraph_buf = "fp";
1079 /* parse callgraph parameters */
1080 if (callgraph_buf != NULL) {
1081 if (!strcmp(callgraph_buf, "no")) {
1082 param.enabled = false;
1083 param.record_mode = CALLCHAIN_NONE;
1085 param.enabled = true;
1086 if (parse_callchain_record(callgraph_buf, ¶m)) {
1087 pr_err("per-event callgraph setting for %s failed. "
1088 "Apply callgraph global setting for it\n",
1092 if (param.record_mode == CALLCHAIN_DWARF)
1093 sample_address = true;
1096 if (dump_size > 0) {
1097 dump_size = round_up(dump_size, sizeof(u64));
1098 param.dump_size = dump_size;
1101 /* If global callgraph set, clear it */
1102 if (callchain_param.enabled)
1103 evsel__reset_callgraph(evsel, &callchain_param);
1105 /* set perf-event callgraph */
1106 if (param.enabled) {
1107 if (sample_address) {
1108 evsel__set_sample_bit(evsel, ADDR);
1109 evsel__set_sample_bit(evsel, DATA_SRC);
1110 evsel->core.attr.mmap_data = track;
1112 evsel__config_callchain(evsel, opts, ¶m);
1117 struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type)
1119 struct evsel_config_term *term, *found_term = NULL;
1121 list_for_each_entry(term, &evsel->config_terms, list) {
1122 if (term->type == type)
1129 void __weak arch_evsel__set_sample_weight(struct evsel *evsel)
1131 evsel__set_sample_bit(evsel, WEIGHT);
1134 void __weak arch_evsel__fixup_new_cycles(struct perf_event_attr *attr __maybe_unused)
1138 void __weak arch__post_evsel_config(struct evsel *evsel __maybe_unused,
1139 struct perf_event_attr *attr __maybe_unused)
1143 static void evsel__set_default_freq_period(struct record_opts *opts,
1144 struct perf_event_attr *attr)
1148 attr->sample_freq = opts->freq;
1150 attr->sample_period = opts->default_interval;
1154 static bool evsel__is_offcpu_event(struct evsel *evsel)
1156 return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT);
1160 * The enable_on_exec/disabled value strategy:
1162 * 1) For any type of traced program:
1163 * - all independent events and group leaders are disabled
1164 * - all group members are enabled
1166 * Group members are ruled by group leaders. They need to
1167 * be enabled, because the group scheduling relies on that.
1169 * 2) For traced programs executed by perf:
1170 * - all independent events and group leaders have
1171 * enable_on_exec set
1172 * - we don't specifically enable or disable any event during
1173 * the record command
1175 * Independent events and group leaders are initially disabled
1176 * and get enabled by exec. Group members are ruled by group
1177 * leaders as stated in 1).
1179 * 3) For traced programs attached by perf (pid/tid):
1180 * - we specifically enable or disable all events during
1181 * the record command
1183 * When attaching events to already running traced we
1184 * enable/disable events specifically, as there's no
1185 * initial traced exec call.
1187 void evsel__config(struct evsel *evsel, struct record_opts *opts,
1188 struct callchain_param *callchain)
1190 struct evsel *leader = evsel__leader(evsel);
1191 struct perf_event_attr *attr = &evsel->core.attr;
1192 int track = evsel->tracking;
1193 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
1195 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
1196 attr->inherit = !opts->no_inherit;
1197 attr->write_backward = opts->overwrite ? 1 : 0;
1198 attr->read_format = PERF_FORMAT_LOST;
1200 evsel__set_sample_bit(evsel, IP);
1201 evsel__set_sample_bit(evsel, TID);
1203 if (evsel->sample_read) {
1204 evsel__set_sample_bit(evsel, READ);
1207 * We need ID even in case of single event, because
1208 * PERF_SAMPLE_READ process ID specific data.
1210 evsel__set_sample_id(evsel, false);
1213 * Apply group format only if we belong to group
1214 * with more than one members.
1216 if (leader->core.nr_members > 1) {
1217 attr->read_format |= PERF_FORMAT_GROUP;
1223 * We default some events to have a default interval. But keep
1224 * it a weak assumption overridable by the user.
1226 if ((evsel->is_libpfm_event && !attr->sample_period) ||
1227 (!evsel->is_libpfm_event && (!attr->sample_period ||
1228 opts->user_freq != UINT_MAX ||
1229 opts->user_interval != ULLONG_MAX)))
1230 evsel__set_default_freq_period(opts, attr);
1233 * If attr->freq was set (here or earlier), ask for period
1237 evsel__set_sample_bit(evsel, PERIOD);
1239 if (opts->no_samples)
1240 attr->sample_freq = 0;
1242 if (opts->inherit_stat) {
1243 evsel->core.attr.read_format |=
1244 PERF_FORMAT_TOTAL_TIME_ENABLED |
1245 PERF_FORMAT_TOTAL_TIME_RUNNING |
1247 attr->inherit_stat = 1;
1250 if (opts->sample_address) {
1251 evsel__set_sample_bit(evsel, ADDR);
1252 attr->mmap_data = track;
1256 * We don't allow user space callchains for function trace
1257 * event, due to issues with page faults while tracing page
1258 * fault handler and its overall trickiness nature.
1260 if (evsel__is_function_event(evsel))
1261 evsel->core.attr.exclude_callchain_user = 1;
1263 if (callchain && callchain->enabled && !evsel->no_aux_samples)
1264 evsel__config_callchain(evsel, opts, callchain);
1266 if (opts->sample_intr_regs && !evsel->no_aux_samples &&
1267 !evsel__is_dummy_event(evsel)) {
1268 attr->sample_regs_intr = opts->sample_intr_regs;
1269 evsel__set_sample_bit(evsel, REGS_INTR);
1272 if (opts->sample_user_regs && !evsel->no_aux_samples &&
1273 !evsel__is_dummy_event(evsel)) {
1274 attr->sample_regs_user |= opts->sample_user_regs;
1275 evsel__set_sample_bit(evsel, REGS_USER);
1278 if (target__has_cpu(&opts->target) || opts->sample_cpu)
1279 evsel__set_sample_bit(evsel, CPU);
1282 * When the user explicitly disabled time don't force it here.
1284 if (opts->sample_time &&
1285 (!perf_missing_features.sample_id_all &&
1286 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
1287 opts->sample_time_set)))
1288 evsel__set_sample_bit(evsel, TIME);
1290 if (opts->raw_samples && !evsel->no_aux_samples) {
1291 evsel__set_sample_bit(evsel, TIME);
1292 evsel__set_sample_bit(evsel, RAW);
1293 evsel__set_sample_bit(evsel, CPU);
1296 if (opts->sample_address)
1297 evsel__set_sample_bit(evsel, DATA_SRC);
1299 if (opts->sample_phys_addr)
1300 evsel__set_sample_bit(evsel, PHYS_ADDR);
1302 if (opts->no_buffering) {
1303 attr->watermark = 0;
1304 attr->wakeup_events = 1;
1306 if (opts->branch_stack && !evsel->no_aux_samples) {
1307 evsel__set_sample_bit(evsel, BRANCH_STACK);
1308 attr->branch_sample_type = opts->branch_stack;
1311 if (opts->sample_weight)
1312 arch_evsel__set_sample_weight(evsel);
1316 attr->mmap2 = track && !perf_missing_features.mmap2;
1318 attr->build_id = track && opts->build_id;
1321 * ksymbol is tracked separately with text poke because it needs to be
1322 * system wide and enabled immediately.
1324 if (!opts->text_poke)
1325 attr->ksymbol = track && !perf_missing_features.ksymbol;
1326 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf;
1328 if (opts->record_namespaces)
1329 attr->namespaces = track;
1331 if (opts->record_cgroup) {
1332 attr->cgroup = track && !perf_missing_features.cgroup;
1333 evsel__set_sample_bit(evsel, CGROUP);
1336 if (opts->sample_data_page_size)
1337 evsel__set_sample_bit(evsel, DATA_PAGE_SIZE);
1339 if (opts->sample_code_page_size)
1340 evsel__set_sample_bit(evsel, CODE_PAGE_SIZE);
1342 if (opts->record_switch_events)
1343 attr->context_switch = track;
1345 if (opts->sample_transaction)
1346 evsel__set_sample_bit(evsel, TRANSACTION);
1348 if (opts->running_time) {
1349 evsel->core.attr.read_format |=
1350 PERF_FORMAT_TOTAL_TIME_ENABLED |
1351 PERF_FORMAT_TOTAL_TIME_RUNNING;
1355 * XXX see the function comment above
1357 * Disabling only independent events or group leaders,
1358 * keeping group members enabled.
1360 if (evsel__is_group_leader(evsel))
1364 * Setting enable_on_exec for independent events and
1365 * group leaders for traced executed by perf.
1367 if (target__none(&opts->target) && evsel__is_group_leader(evsel) &&
1368 !opts->target.initial_delay)
1369 attr->enable_on_exec = 1;
1371 if (evsel->immediate) {
1373 attr->enable_on_exec = 0;
1376 clockid = opts->clockid;
1377 if (opts->use_clockid) {
1378 attr->use_clockid = 1;
1379 attr->clockid = opts->clockid;
1382 if (evsel->precise_max)
1383 attr->precise_ip = 3;
1385 if (opts->all_user) {
1386 attr->exclude_kernel = 1;
1387 attr->exclude_user = 0;
1390 if (opts->all_kernel) {
1391 attr->exclude_kernel = 0;
1392 attr->exclude_user = 1;
1395 if (evsel->core.own_cpus || evsel->unit)
1396 evsel->core.attr.read_format |= PERF_FORMAT_ID;
1399 * Apply event specific term settings,
1400 * it overloads any global configuration.
1402 evsel__apply_config_terms(evsel, opts, track);
1404 evsel->ignore_missing_thread = opts->ignore_missing_thread;
1406 /* The --period option takes the precedence. */
1407 if (opts->period_set) {
1409 evsel__set_sample_bit(evsel, PERIOD);
1411 evsel__reset_sample_bit(evsel, PERIOD);
1415 * A dummy event never triggers any actual counter and therefore
1416 * cannot be used with branch_stack.
1418 * For initial_delay, a dummy event is added implicitly.
1419 * The software event will trigger -EOPNOTSUPP error out,
1420 * if BRANCH_STACK bit is set.
1422 if (evsel__is_dummy_event(evsel))
1423 evsel__reset_sample_bit(evsel, BRANCH_STACK);
1425 if (evsel__is_offcpu_event(evsel))
1426 evsel->core.attr.sample_type &= OFFCPU_SAMPLE_TYPES;
1428 arch__post_evsel_config(evsel, attr);
1431 int evsel__set_filter(struct evsel *evsel, const char *filter)
1433 char *new_filter = strdup(filter);
1435 if (new_filter != NULL) {
1436 free(evsel->filter);
1437 evsel->filter = new_filter;
1444 static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter)
1448 if (evsel->filter == NULL)
1449 return evsel__set_filter(evsel, filter);
1451 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
1452 free(evsel->filter);
1453 evsel->filter = new_filter;
1460 int evsel__append_tp_filter(struct evsel *evsel, const char *filter)
1462 return evsel__append_filter(evsel, "(%s) && (%s)", filter);
1465 int evsel__append_addr_filter(struct evsel *evsel, const char *filter)
1467 return evsel__append_filter(evsel, "%s,%s", filter);
1470 /* Caller has to clear disabled after going through all CPUs. */
1471 int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx)
1473 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
1476 int evsel__enable(struct evsel *evsel)
1478 int err = perf_evsel__enable(&evsel->core);
1481 evsel->disabled = false;
1485 /* Caller has to set disabled after going through all CPUs. */
1486 int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx)
1488 return perf_evsel__disable_cpu(&evsel->core, cpu_map_idx);
1491 int evsel__disable(struct evsel *evsel)
1493 int err = perf_evsel__disable(&evsel->core);
1495 * We mark it disabled here so that tools that disable a event can
1496 * ignore events after they disable it. I.e. the ring buffer may have
1497 * already a few more events queued up before the kernel got the stop
1501 evsel->disabled = true;
1506 void free_config_terms(struct list_head *config_terms)
1508 struct evsel_config_term *term, *h;
1510 list_for_each_entry_safe(term, h, config_terms, list) {
1511 list_del_init(&term->list);
1513 zfree(&term->val.str);
1518 static void evsel__free_config_terms(struct evsel *evsel)
1520 free_config_terms(&evsel->config_terms);
1523 void evsel__exit(struct evsel *evsel)
1525 assert(list_empty(&evsel->core.node));
1526 assert(evsel->evlist == NULL);
1527 bpf_counter__destroy(evsel);
1528 perf_bpf_filter__destroy(evsel);
1529 evsel__free_counts(evsel);
1530 perf_evsel__free_fd(&evsel->core);
1531 perf_evsel__free_id(&evsel->core);
1532 evsel__free_config_terms(evsel);
1533 cgroup__put(evsel->cgrp);
1534 perf_cpu_map__put(evsel->core.cpus);
1535 perf_cpu_map__put(evsel->core.own_cpus);
1536 perf_thread_map__put(evsel->core.threads);
1537 zfree(&evsel->group_name);
1538 zfree(&evsel->name);
1539 zfree(&evsel->pmu_name);
1540 zfree(&evsel->unit);
1541 zfree(&evsel->metric_id);
1542 evsel__zero_per_pkg(evsel);
1543 hashmap__free(evsel->per_pkg_mask);
1544 evsel->per_pkg_mask = NULL;
1545 zfree(&evsel->metric_events);
1546 perf_evsel__object.fini(evsel);
1549 void evsel__delete(struct evsel *evsel)
1558 void evsel__compute_deltas(struct evsel *evsel, int cpu_map_idx, int thread,
1559 struct perf_counts_values *count)
1561 struct perf_counts_values tmp;
1563 if (!evsel->prev_raw_counts)
1566 tmp = *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread);
1567 *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread) = *count;
1569 count->val = count->val - tmp.val;
1570 count->ena = count->ena - tmp.ena;
1571 count->run = count->run - tmp.run;
1574 static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread)
1576 struct perf_counts_values *count = perf_counts(evsel->counts, cpu_map_idx, thread);
1578 return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count);
1581 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
1582 u64 val, u64 ena, u64 run, u64 lost)
1584 struct perf_counts_values *count;
1586 count = perf_counts(counter->counts, cpu_map_idx, thread);
1593 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true);
1596 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int thread, u64 *data)
1598 u64 read_format = leader->core.attr.read_format;
1599 struct sample_read_value *v;
1600 u64 nr, ena = 0, run = 0, lost = 0;
1604 if (nr != (u64) leader->core.nr_members)
1607 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1610 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1614 sample_read_group__for_each(v, nr, read_format) {
1615 struct evsel *counter;
1617 counter = evlist__id2evsel(leader->evlist, v->id);
1621 if (read_format & PERF_FORMAT_LOST)
1624 evsel__set_count(counter, cpu_map_idx, thread, v->value, ena, run, lost);
1630 static int evsel__read_group(struct evsel *leader, int cpu_map_idx, int thread)
1632 struct perf_stat_evsel *ps = leader->stats;
1633 u64 read_format = leader->core.attr.read_format;
1634 int size = perf_evsel__read_size(&leader->core);
1635 u64 *data = ps->group_data;
1637 if (!(read_format & PERF_FORMAT_ID))
1640 if (!evsel__is_group_leader(leader))
1644 data = zalloc(size);
1648 ps->group_data = data;
1651 if (FD(leader, cpu_map_idx, thread) < 0)
1654 if (readn(FD(leader, cpu_map_idx, thread), data, size) <= 0)
1657 return evsel__process_group_data(leader, cpu_map_idx, thread, data);
1660 int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread)
1662 u64 read_format = evsel->core.attr.read_format;
1664 if (read_format & PERF_FORMAT_GROUP)
1665 return evsel__read_group(evsel, cpu_map_idx, thread);
1667 return evsel__read_one(evsel, cpu_map_idx, thread);
1670 int __evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread, bool scale)
1672 struct perf_counts_values count;
1673 size_t nv = scale ? 3 : 1;
1675 if (FD(evsel, cpu_map_idx, thread) < 0)
1678 if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0)
1681 if (readn(FD(evsel, cpu_map_idx, thread), &count, nv * sizeof(u64)) <= 0)
1684 evsel__compute_deltas(evsel, cpu_map_idx, thread, &count);
1685 perf_counts_values__scale(&count, scale, NULL);
1686 *perf_counts(evsel->counts, cpu_map_idx, thread) = count;
1690 static int evsel__match_other_cpu(struct evsel *evsel, struct evsel *other,
1693 struct perf_cpu cpu;
1695 cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx);
1696 return perf_cpu_map__idx(other->core.cpus, cpu);
1699 static int evsel__hybrid_group_cpu_map_idx(struct evsel *evsel, int cpu_map_idx)
1701 struct evsel *leader = evsel__leader(evsel);
1703 if ((evsel__is_hybrid(evsel) && !evsel__is_hybrid(leader)) ||
1704 (!evsel__is_hybrid(evsel) && evsel__is_hybrid(leader))) {
1705 return evsel__match_other_cpu(evsel, leader, cpu_map_idx);
1711 static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread)
1713 struct evsel *leader = evsel__leader(evsel);
1716 if (evsel__is_group_leader(evsel))
1720 * Leader must be already processed/open,
1721 * if not it's a bug.
1723 BUG_ON(!leader->core.fd);
1725 cpu_map_idx = evsel__hybrid_group_cpu_map_idx(evsel, cpu_map_idx);
1726 if (cpu_map_idx == -1)
1729 fd = FD(leader, cpu_map_idx, thread);
1730 BUG_ON(fd == -1 && !leader->skippable);
1733 * When the leader has been skipped, return -2 to distinguish from no
1734 * group leader case.
1736 return fd == -1 ? -2 : fd;
1739 static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx)
1741 for (int cpu = 0; cpu < nr_cpus; cpu++)
1742 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1743 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1746 static int update_fds(struct evsel *evsel,
1747 int nr_cpus, int cpu_map_idx,
1748 int nr_threads, int thread_idx)
1752 if (cpu_map_idx >= nr_cpus || thread_idx >= nr_threads)
1755 evlist__for_each_entry(evsel->evlist, pos) {
1756 nr_cpus = pos != evsel ? nr_cpus : cpu_map_idx;
1758 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
1761 * Since fds for next evsel has not been created,
1762 * there is no need to iterate whole event list.
1770 static bool evsel__ignore_missing_thread(struct evsel *evsel,
1771 int nr_cpus, int cpu_map_idx,
1772 struct perf_thread_map *threads,
1773 int thread, int err)
1775 pid_t ignore_pid = perf_thread_map__pid(threads, thread);
1777 if (!evsel->ignore_missing_thread)
1780 /* The system wide setup does not work with threads. */
1781 if (evsel->core.system_wide)
1784 /* The -ESRCH is perf event syscall errno for pid's not found. */
1788 /* If there's only one thread, let it fail. */
1789 if (threads->nr == 1)
1793 * We should remove fd for missing_thread first
1794 * because thread_map__remove() will decrease threads->nr.
1796 if (update_fds(evsel, nr_cpus, cpu_map_idx, threads->nr, thread))
1799 if (thread_map__remove(threads, thread))
1802 pr_warning("WARNING: Ignored open failure for pid %d\n",
1807 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1808 void *priv __maybe_unused)
1810 return fprintf(fp, " %-32s %s\n", name, val);
1813 static void display_attr(struct perf_event_attr *attr)
1815 if (verbose >= 2 || debug_peo_args) {
1816 fprintf(stderr, "%.60s\n", graph_dotted_line);
1817 fprintf(stderr, "perf_event_attr:\n");
1818 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
1819 fprintf(stderr, "%.60s\n", graph_dotted_line);
1823 bool evsel__precise_ip_fallback(struct evsel *evsel)
1825 /* Do not try less precise if not requested. */
1826 if (!evsel->precise_max)
1830 * We tried all the precise_ip values, and it's
1831 * still failing, so leave it to standard fallback.
1833 if (!evsel->core.attr.precise_ip) {
1834 evsel->core.attr.precise_ip = evsel->precise_ip_original;
1838 if (!evsel->precise_ip_original)
1839 evsel->precise_ip_original = evsel->core.attr.precise_ip;
1841 evsel->core.attr.precise_ip--;
1842 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
1843 display_attr(&evsel->core.attr);
1847 static struct perf_cpu_map *empty_cpu_map;
1848 static struct perf_thread_map *empty_thread_map;
1850 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
1851 struct perf_thread_map *threads)
1853 int nthreads = perf_thread_map__nr(threads);
1855 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
1856 (perf_missing_features.aux_output && evsel->core.attr.aux_output))
1860 if (empty_cpu_map == NULL) {
1861 empty_cpu_map = perf_cpu_map__dummy_new();
1862 if (empty_cpu_map == NULL)
1866 cpus = empty_cpu_map;
1869 if (threads == NULL) {
1870 if (empty_thread_map == NULL) {
1871 empty_thread_map = thread_map__new_by_tid(-1);
1872 if (empty_thread_map == NULL)
1876 threads = empty_thread_map;
1879 if (evsel->core.fd == NULL &&
1880 perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0)
1883 evsel->open_flags = PERF_FLAG_FD_CLOEXEC;
1885 evsel->open_flags |= PERF_FLAG_PID_CGROUP;
1890 static void evsel__disable_missing_features(struct evsel *evsel)
1892 if (perf_missing_features.read_lost)
1893 evsel->core.attr.read_format &= ~PERF_FORMAT_LOST;
1894 if (perf_missing_features.weight_struct) {
1895 evsel__set_sample_bit(evsel, WEIGHT);
1896 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT);
1898 if (perf_missing_features.clockid_wrong)
1899 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */
1900 if (perf_missing_features.clockid) {
1901 evsel->core.attr.use_clockid = 0;
1902 evsel->core.attr.clockid = 0;
1904 if (perf_missing_features.cloexec)
1905 evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1906 if (perf_missing_features.mmap2)
1907 evsel->core.attr.mmap2 = 0;
1908 if (evsel->pmu && evsel->pmu->missing_features.exclude_guest)
1909 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
1910 if (perf_missing_features.lbr_flags)
1911 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1912 PERF_SAMPLE_BRANCH_NO_CYCLES);
1913 if (perf_missing_features.group_read && evsel->core.attr.inherit)
1914 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
1915 if (perf_missing_features.ksymbol)
1916 evsel->core.attr.ksymbol = 0;
1917 if (perf_missing_features.bpf)
1918 evsel->core.attr.bpf_event = 0;
1919 if (perf_missing_features.branch_hw_idx)
1920 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
1921 if (perf_missing_features.sample_id_all)
1922 evsel->core.attr.sample_id_all = 0;
1925 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
1926 struct perf_thread_map *threads)
1930 err = __evsel__prepare_open(evsel, cpus, threads);
1934 evsel__disable_missing_features(evsel);
1939 bool evsel__detect_missing_features(struct evsel *evsel)
1942 * Must probe features in the order they were added to the
1943 * perf_event_attr interface.
1945 if (!perf_missing_features.read_lost &&
1946 (evsel->core.attr.read_format & PERF_FORMAT_LOST)) {
1947 perf_missing_features.read_lost = true;
1948 pr_debug2("switching off PERF_FORMAT_LOST support\n");
1950 } else if (!perf_missing_features.weight_struct &&
1951 (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) {
1952 perf_missing_features.weight_struct = true;
1953 pr_debug2("switching off weight struct support\n");
1955 } else if (!perf_missing_features.code_page_size &&
1956 (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) {
1957 perf_missing_features.code_page_size = true;
1958 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n");
1960 } else if (!perf_missing_features.data_page_size &&
1961 (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) {
1962 perf_missing_features.data_page_size = true;
1963 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n");
1965 } else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) {
1966 perf_missing_features.cgroup = true;
1967 pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n");
1969 } else if (!perf_missing_features.branch_hw_idx &&
1970 (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) {
1971 perf_missing_features.branch_hw_idx = true;
1972 pr_debug2("switching off branch HW index support\n");
1974 } else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
1975 perf_missing_features.aux_output = true;
1976 pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n");
1978 } else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
1979 perf_missing_features.bpf = true;
1980 pr_debug2_peo("switching off bpf_event\n");
1982 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
1983 perf_missing_features.ksymbol = true;
1984 pr_debug2_peo("switching off ksymbol\n");
1986 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
1987 perf_missing_features.write_backward = true;
1988 pr_debug2_peo("switching off write_backward\n");
1990 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
1991 perf_missing_features.clockid_wrong = true;
1992 pr_debug2_peo("switching off clockid\n");
1994 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
1995 perf_missing_features.clockid = true;
1996 pr_debug2_peo("switching off use_clockid\n");
1998 } else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
1999 perf_missing_features.cloexec = true;
2000 pr_debug2_peo("switching off cloexec flag\n");
2002 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
2003 perf_missing_features.mmap2 = true;
2004 pr_debug2_peo("switching off mmap2\n");
2006 } else if (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) {
2007 if (evsel->pmu == NULL)
2008 evsel->pmu = evsel__find_pmu(evsel);
2011 evsel->pmu->missing_features.exclude_guest = true;
2013 /* we cannot find PMU, disable attrs now */
2014 evsel->core.attr.exclude_host = false;
2015 evsel->core.attr.exclude_guest = false;
2018 if (evsel->exclude_GH) {
2019 pr_debug2_peo("PMU has no exclude_host/guest support, bailing out\n");
2022 if (!perf_missing_features.exclude_guest) {
2023 perf_missing_features.exclude_guest = true;
2024 pr_debug2_peo("switching off exclude_guest, exclude_host\n");
2027 } else if (!perf_missing_features.sample_id_all) {
2028 perf_missing_features.sample_id_all = true;
2029 pr_debug2_peo("switching off sample_id_all\n");
2031 } else if (!perf_missing_features.lbr_flags &&
2032 (evsel->core.attr.branch_sample_type &
2033 (PERF_SAMPLE_BRANCH_NO_CYCLES |
2034 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
2035 perf_missing_features.lbr_flags = true;
2036 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
2038 } else if (!perf_missing_features.group_read &&
2039 evsel->core.attr.inherit &&
2040 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
2041 evsel__is_group_leader(evsel)) {
2042 perf_missing_features.group_read = true;
2043 pr_debug2_peo("switching off group read\n");
2050 bool evsel__increase_rlimit(enum rlimit_action *set_rlimit)
2055 if (*set_rlimit < INCREASED_MAX) {
2058 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
2059 if (*set_rlimit == NO_CHANGE) {
2060 l.rlim_cur = l.rlim_max;
2062 l.rlim_cur = l.rlim_max + 1000;
2063 l.rlim_max = l.rlim_cur;
2065 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
2077 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
2078 struct perf_thread_map *threads,
2079 int start_cpu_map_idx, int end_cpu_map_idx)
2081 int idx, thread, nthreads;
2082 int pid = -1, err, old_errno;
2083 enum rlimit_action set_rlimit = NO_CHANGE;
2085 err = __evsel__prepare_open(evsel, cpus, threads);
2090 cpus = empty_cpu_map;
2092 if (threads == NULL)
2093 threads = empty_thread_map;
2095 nthreads = perf_thread_map__nr(threads);
2098 pid = evsel->cgrp->fd;
2100 fallback_missing_features:
2101 evsel__disable_missing_features(evsel);
2103 display_attr(&evsel->core.attr);
2105 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) {
2107 for (thread = 0; thread < nthreads; thread++) {
2110 if (thread >= nthreads)
2113 if (!evsel->cgrp && !evsel->core.system_wide)
2114 pid = perf_thread_map__pid(threads, thread);
2116 group_fd = get_group_fd(evsel, idx, thread);
2118 if (group_fd == -2) {
2119 pr_debug("broken group leader for %s\n", evsel->name);
2126 /* Debug message used by test scripts */
2127 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
2128 pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags);
2130 fd = sys_perf_event_open(&evsel->core.attr, pid,
2131 perf_cpu_map__cpu(cpus, idx).cpu,
2132 group_fd, evsel->open_flags);
2134 FD(evsel, idx, thread) = fd;
2139 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
2144 bpf_counter__install_pe(evsel, idx, fd);
2146 if (unlikely(test_attr__enabled)) {
2147 test_attr__open(&evsel->core.attr, pid,
2148 perf_cpu_map__cpu(cpus, idx),
2149 fd, group_fd, evsel->open_flags);
2152 /* Debug message used by test scripts */
2153 pr_debug2_peo(" = %d\n", fd);
2155 if (evsel->bpf_fd >= 0) {
2157 int bpf_fd = evsel->bpf_fd;
2160 PERF_EVENT_IOC_SET_BPF,
2162 if (err && errno != EEXIST) {
2163 pr_err("failed to attach bpf fd %d: %s\n",
2164 bpf_fd, strerror(errno));
2170 set_rlimit = NO_CHANGE;
2173 * If we succeeded but had to kill clockid, fail and
2174 * have evsel__open_strerror() print us a nice error.
2176 if (perf_missing_features.clockid ||
2177 perf_missing_features.clockid_wrong) {
2187 if (evsel__precise_ip_fallback(evsel))
2190 if (evsel__ignore_missing_thread(evsel, perf_cpu_map__nr(cpus),
2191 idx, threads, thread, err)) {
2192 /* We just removed 1 thread, so lower the upper nthreads limit. */
2195 /* ... and pretend like nothing have happened. */
2200 * perf stat needs between 5 and 22 fds per CPU. When we run out
2201 * of them try to increase the limits.
2203 if (err == -EMFILE && evsel__increase_rlimit(&set_rlimit))
2206 if (err != -EINVAL || idx > 0 || thread > 0)
2209 if (evsel__detect_missing_features(evsel))
2210 goto fallback_missing_features;
2213 threads->err_thread = thread;
2217 while (--thread >= 0) {
2218 if (FD(evsel, idx, thread) >= 0)
2219 close(FD(evsel, idx, thread));
2220 FD(evsel, idx, thread) = -1;
2223 } while (--idx >= 0);
2228 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
2229 struct perf_thread_map *threads)
2231 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus));
2234 void evsel__close(struct evsel *evsel)
2236 perf_evsel__close(&evsel->core);
2237 perf_evsel__free_id(&evsel->core);
2240 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu_map_idx)
2242 if (cpu_map_idx == -1)
2243 return evsel__open_cpu(evsel, cpus, NULL, 0, perf_cpu_map__nr(cpus));
2245 return evsel__open_cpu(evsel, cpus, NULL, cpu_map_idx, cpu_map_idx + 1);
2248 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads)
2250 return evsel__open(evsel, NULL, threads);
2253 static int perf_evsel__parse_id_sample(const struct evsel *evsel,
2254 const union perf_event *event,
2255 struct perf_sample *sample)
2257 u64 type = evsel->core.attr.sample_type;
2258 const __u64 *array = event->sample.array;
2259 bool swapped = evsel->needs_swap;
2262 array += ((event->header.size -
2263 sizeof(event->header)) / sizeof(u64)) - 1;
2265 if (type & PERF_SAMPLE_IDENTIFIER) {
2266 sample->id = *array;
2270 if (type & PERF_SAMPLE_CPU) {
2273 /* undo swap of u64, then swap on individual u32s */
2274 u.val64 = bswap_64(u.val64);
2275 u.val32[0] = bswap_32(u.val32[0]);
2278 sample->cpu = u.val32[0];
2282 if (type & PERF_SAMPLE_STREAM_ID) {
2283 sample->stream_id = *array;
2287 if (type & PERF_SAMPLE_ID) {
2288 sample->id = *array;
2292 if (type & PERF_SAMPLE_TIME) {
2293 sample->time = *array;
2297 if (type & PERF_SAMPLE_TID) {
2300 /* undo swap of u64, then swap on individual u32s */
2301 u.val64 = bswap_64(u.val64);
2302 u.val32[0] = bswap_32(u.val32[0]);
2303 u.val32[1] = bswap_32(u.val32[1]);
2306 sample->pid = u.val32[0];
2307 sample->tid = u.val32[1];
2314 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2317 return size > max_size || offset + size > endp;
2320 #define OVERFLOW_CHECK(offset, size, max_size) \
2322 if (overflow(endp, (max_size), (offset), (size))) \
2326 #define OVERFLOW_CHECK_u64(offset) \
2327 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2330 perf_event__check_size(union perf_event *event, unsigned int sample_size)
2333 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2334 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2335 * check the format does not go past the end of the event.
2337 if (sample_size + sizeof(event->header) > event->header.size)
2343 void __weak arch_perf_parse_sample_weight(struct perf_sample *data,
2345 u64 type __maybe_unused)
2347 data->weight = *array;
2350 u64 evsel__bitfield_swap_branch_flags(u64 value)
2359 * mispred:1 //target mispredicted
2360 * predicted:1 //target predicted
2361 * in_tx:1 //in transaction
2362 * abort:1 //transaction abort
2363 * cycles:16 //cycle count to last branch
2364 * type:4 //branch type
2365 * spec:2 //branch speculation info
2366 * new_type:4 //additional branch type
2367 * priv:3 //privilege level
2372 * Avoid bswap64() the entire branch_flag.value,
2373 * as it has variable bit-field sizes. Instead the
2374 * macro takes the bit-field position/size,
2375 * swaps it based on the host endianness.
2377 if (host_is_bigendian()) {
2378 new_val = bitfield_swap(value, 0, 1);
2379 new_val |= bitfield_swap(value, 1, 1);
2380 new_val |= bitfield_swap(value, 2, 1);
2381 new_val |= bitfield_swap(value, 3, 1);
2382 new_val |= bitfield_swap(value, 4, 16);
2383 new_val |= bitfield_swap(value, 20, 4);
2384 new_val |= bitfield_swap(value, 24, 2);
2385 new_val |= bitfield_swap(value, 26, 4);
2386 new_val |= bitfield_swap(value, 30, 3);
2387 new_val |= bitfield_swap(value, 33, 31);
2389 new_val = bitfield_swap(value, 63, 1);
2390 new_val |= bitfield_swap(value, 62, 1);
2391 new_val |= bitfield_swap(value, 61, 1);
2392 new_val |= bitfield_swap(value, 60, 1);
2393 new_val |= bitfield_swap(value, 44, 16);
2394 new_val |= bitfield_swap(value, 40, 4);
2395 new_val |= bitfield_swap(value, 38, 2);
2396 new_val |= bitfield_swap(value, 34, 4);
2397 new_val |= bitfield_swap(value, 31, 3);
2398 new_val |= bitfield_swap(value, 0, 31);
2404 int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
2405 struct perf_sample *data)
2407 u64 type = evsel->core.attr.sample_type;
2408 bool swapped = evsel->needs_swap;
2410 u16 max_size = event->header.size;
2411 const void *endp = (void *)event + max_size;
2415 * used for cross-endian analysis. See git commit 65014ab3
2416 * for why this goofiness is needed.
2420 memset(data, 0, sizeof(*data));
2421 data->cpu = data->pid = data->tid = -1;
2422 data->stream_id = data->id = data->time = -1ULL;
2423 data->period = evsel->core.attr.sample_period;
2424 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2425 data->misc = event->header.misc;
2427 data->data_src = PERF_MEM_DATA_SRC_NONE;
2430 if (event->header.type != PERF_RECORD_SAMPLE) {
2431 if (!evsel->core.attr.sample_id_all)
2433 return perf_evsel__parse_id_sample(evsel, event, data);
2436 array = event->sample.array;
2438 if (perf_event__check_size(event, evsel->sample_size))
2441 if (type & PERF_SAMPLE_IDENTIFIER) {
2446 if (type & PERF_SAMPLE_IP) {
2451 if (type & PERF_SAMPLE_TID) {
2454 /* undo swap of u64, then swap on individual u32s */
2455 u.val64 = bswap_64(u.val64);
2456 u.val32[0] = bswap_32(u.val32[0]);
2457 u.val32[1] = bswap_32(u.val32[1]);
2460 data->pid = u.val32[0];
2461 data->tid = u.val32[1];
2465 if (type & PERF_SAMPLE_TIME) {
2466 data->time = *array;
2470 if (type & PERF_SAMPLE_ADDR) {
2471 data->addr = *array;
2475 if (type & PERF_SAMPLE_ID) {
2480 if (type & PERF_SAMPLE_STREAM_ID) {
2481 data->stream_id = *array;
2485 if (type & PERF_SAMPLE_CPU) {
2489 /* undo swap of u64, then swap on individual u32s */
2490 u.val64 = bswap_64(u.val64);
2491 u.val32[0] = bswap_32(u.val32[0]);
2494 data->cpu = u.val32[0];
2498 if (type & PERF_SAMPLE_PERIOD) {
2499 data->period = *array;
2503 if (type & PERF_SAMPLE_READ) {
2504 u64 read_format = evsel->core.attr.read_format;
2506 OVERFLOW_CHECK_u64(array);
2507 if (read_format & PERF_FORMAT_GROUP)
2508 data->read.group.nr = *array;
2510 data->read.one.value = *array;
2514 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2515 OVERFLOW_CHECK_u64(array);
2516 data->read.time_enabled = *array;
2520 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2521 OVERFLOW_CHECK_u64(array);
2522 data->read.time_running = *array;
2526 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2527 if (read_format & PERF_FORMAT_GROUP) {
2528 const u64 max_group_nr = UINT64_MAX /
2529 sizeof(struct sample_read_value);
2531 if (data->read.group.nr > max_group_nr)
2534 sz = data->read.group.nr * sample_read_value_size(read_format);
2535 OVERFLOW_CHECK(array, sz, max_size);
2536 data->read.group.values =
2537 (struct sample_read_value *)array;
2538 array = (void *)array + sz;
2540 OVERFLOW_CHECK_u64(array);
2541 data->read.one.id = *array;
2544 if (read_format & PERF_FORMAT_LOST) {
2545 OVERFLOW_CHECK_u64(array);
2546 data->read.one.lost = *array;
2552 if (type & PERF_SAMPLE_CALLCHAIN) {
2553 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
2555 OVERFLOW_CHECK_u64(array);
2556 data->callchain = (struct ip_callchain *)array++;
2557 if (data->callchain->nr > max_callchain_nr)
2559 sz = data->callchain->nr * sizeof(u64);
2560 OVERFLOW_CHECK(array, sz, max_size);
2561 array = (void *)array + sz;
2564 if (type & PERF_SAMPLE_RAW) {
2565 OVERFLOW_CHECK_u64(array);
2569 * Undo swap of u64, then swap on individual u32s,
2570 * get the size of the raw area and undo all of the
2571 * swap. The pevent interface handles endianness by
2575 u.val64 = bswap_64(u.val64);
2576 u.val32[0] = bswap_32(u.val32[0]);
2577 u.val32[1] = bswap_32(u.val32[1]);
2579 data->raw_size = u.val32[0];
2582 * The raw data is aligned on 64bits including the
2583 * u32 size, so it's safe to use mem_bswap_64.
2586 mem_bswap_64((void *) array, data->raw_size);
2588 array = (void *)array + sizeof(u32);
2590 OVERFLOW_CHECK(array, data->raw_size, max_size);
2591 data->raw_data = (void *)array;
2592 array = (void *)array + data->raw_size;
2595 if (type & PERF_SAMPLE_BRANCH_STACK) {
2596 const u64 max_branch_nr = UINT64_MAX /
2597 sizeof(struct branch_entry);
2598 struct branch_entry *e;
2601 OVERFLOW_CHECK_u64(array);
2602 data->branch_stack = (struct branch_stack *)array++;
2604 if (data->branch_stack->nr > max_branch_nr)
2607 sz = data->branch_stack->nr * sizeof(struct branch_entry);
2608 if (evsel__has_branch_hw_idx(evsel)) {
2610 e = &data->branch_stack->entries[0];
2612 data->no_hw_idx = true;
2614 * if the PERF_SAMPLE_BRANCH_HW_INDEX is not applied,
2615 * only nr and entries[] will be output by kernel.
2617 e = (struct branch_entry *)&data->branch_stack->hw_idx;
2622 * struct branch_flag does not have endian
2623 * specific bit field definition. And bswap
2624 * will not resolve the issue, since these
2627 * evsel__bitfield_swap_branch_flags() uses a
2628 * bitfield_swap macro to swap the bit position
2629 * based on the host endians.
2631 for (i = 0; i < data->branch_stack->nr; i++, e++)
2632 e->flags.value = evsel__bitfield_swap_branch_flags(e->flags.value);
2635 OVERFLOW_CHECK(array, sz, max_size);
2636 array = (void *)array + sz;
2639 if (type & PERF_SAMPLE_REGS_USER) {
2640 OVERFLOW_CHECK_u64(array);
2641 data->user_regs.abi = *array;
2644 if (data->user_regs.abi) {
2645 u64 mask = evsel->core.attr.sample_regs_user;
2647 sz = hweight64(mask) * sizeof(u64);
2648 OVERFLOW_CHECK(array, sz, max_size);
2649 data->user_regs.mask = mask;
2650 data->user_regs.regs = (u64 *)array;
2651 array = (void *)array + sz;
2655 if (type & PERF_SAMPLE_STACK_USER) {
2656 OVERFLOW_CHECK_u64(array);
2659 data->user_stack.offset = ((char *)(array - 1)
2663 data->user_stack.size = 0;
2665 OVERFLOW_CHECK(array, sz, max_size);
2666 data->user_stack.data = (char *)array;
2667 array = (void *)array + sz;
2668 OVERFLOW_CHECK_u64(array);
2669 data->user_stack.size = *array++;
2670 if (WARN_ONCE(data->user_stack.size > sz,
2671 "user stack dump failure\n"))
2676 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
2677 OVERFLOW_CHECK_u64(array);
2678 arch_perf_parse_sample_weight(data, array, type);
2682 if (type & PERF_SAMPLE_DATA_SRC) {
2683 OVERFLOW_CHECK_u64(array);
2684 data->data_src = *array;
2688 if (type & PERF_SAMPLE_TRANSACTION) {
2689 OVERFLOW_CHECK_u64(array);
2690 data->transaction = *array;
2694 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2695 if (type & PERF_SAMPLE_REGS_INTR) {
2696 OVERFLOW_CHECK_u64(array);
2697 data->intr_regs.abi = *array;
2700 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2701 u64 mask = evsel->core.attr.sample_regs_intr;
2703 sz = hweight64(mask) * sizeof(u64);
2704 OVERFLOW_CHECK(array, sz, max_size);
2705 data->intr_regs.mask = mask;
2706 data->intr_regs.regs = (u64 *)array;
2707 array = (void *)array + sz;
2711 data->phys_addr = 0;
2712 if (type & PERF_SAMPLE_PHYS_ADDR) {
2713 data->phys_addr = *array;
2718 if (type & PERF_SAMPLE_CGROUP) {
2719 data->cgroup = *array;
2723 data->data_page_size = 0;
2724 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
2725 data->data_page_size = *array;
2729 data->code_page_size = 0;
2730 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
2731 data->code_page_size = *array;
2735 if (type & PERF_SAMPLE_AUX) {
2736 OVERFLOW_CHECK_u64(array);
2739 OVERFLOW_CHECK(array, sz, max_size);
2740 /* Undo swap of data */
2742 mem_bswap_64((char *)array, sz);
2743 data->aux_sample.size = sz;
2744 data->aux_sample.data = (char *)array;
2745 array = (void *)array + sz;
2751 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
2754 u64 type = evsel->core.attr.sample_type;
2757 if (!(type & PERF_SAMPLE_TIME))
2760 if (event->header.type != PERF_RECORD_SAMPLE) {
2761 struct perf_sample data = {
2765 if (!evsel->core.attr.sample_id_all)
2767 if (perf_evsel__parse_id_sample(evsel, event, &data))
2770 *timestamp = data.time;
2774 array = event->sample.array;
2776 if (perf_event__check_size(event, evsel->sample_size))
2779 if (type & PERF_SAMPLE_IDENTIFIER)
2782 if (type & PERF_SAMPLE_IP)
2785 if (type & PERF_SAMPLE_TID)
2788 if (type & PERF_SAMPLE_TIME)
2789 *timestamp = *array;
2794 u16 evsel__id_hdr_size(struct evsel *evsel)
2796 u64 sample_type = evsel->core.attr.sample_type;
2799 if (sample_type & PERF_SAMPLE_TID)
2800 size += sizeof(u64);
2802 if (sample_type & PERF_SAMPLE_TIME)
2803 size += sizeof(u64);
2805 if (sample_type & PERF_SAMPLE_ID)
2806 size += sizeof(u64);
2808 if (sample_type & PERF_SAMPLE_STREAM_ID)
2809 size += sizeof(u64);
2811 if (sample_type & PERF_SAMPLE_CPU)
2812 size += sizeof(u64);
2814 if (sample_type & PERF_SAMPLE_IDENTIFIER)
2815 size += sizeof(u64);
2820 #ifdef HAVE_LIBTRACEEVENT
2821 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name)
2823 return tep_find_field(evsel->tp_format, name);
2826 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name)
2828 struct tep_format_field *field = evsel__field(evsel, name);
2834 offset = field->offset;
2836 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2837 offset = *(int *)(sample->raw_data + field->offset);
2839 if (tep_field_is_relative(field->flags))
2840 offset += field->offset + field->size;
2843 return sample->raw_data + offset;
2846 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
2850 void *ptr = sample->raw_data + field->offset;
2852 switch (field->size) {
2856 value = *(u16 *)ptr;
2859 value = *(u32 *)ptr;
2862 memcpy(&value, ptr, sizeof(u64));
2871 switch (field->size) {
2873 return bswap_16(value);
2875 return bswap_32(value);
2877 return bswap_64(value);
2885 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name)
2887 struct tep_format_field *field = evsel__field(evsel, name);
2892 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2896 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize)
2900 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2901 evsel->core.attr.type == PERF_TYPE_HARDWARE &&
2902 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2904 * If it's cycles then fall back to hrtimer based
2905 * cpu-clock-tick sw counter, which is always available even if
2908 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2911 scnprintf(msg, msgsize, "%s",
2912 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2914 evsel->core.attr.type = PERF_TYPE_SOFTWARE;
2915 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK;
2917 zfree(&evsel->name);
2919 } else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
2920 (paranoid = perf_event_paranoid()) > 1) {
2921 const char *name = evsel__name(evsel);
2923 const char *sep = ":";
2925 /* If event has exclude user then don't exclude kernel. */
2926 if (evsel->core.attr.exclude_user)
2929 /* Is there already the separator in the name. */
2930 if (strchr(name, '/') ||
2931 (strchr(name, ':') && !evsel->is_libpfm_event))
2934 if (asprintf(&new_name, "%s%su", name, sep) < 0)
2938 evsel->name = new_name;
2939 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
2940 "to fall back to excluding kernel and hypervisor "
2941 " samples", paranoid);
2942 evsel->core.attr.exclude_kernel = 1;
2943 evsel->core.attr.exclude_hv = 1;
2951 static bool find_process(const char *name)
2953 size_t len = strlen(name);
2958 dir = opendir(procfs__mountpoint());
2962 /* Walk through the directory. */
2963 while (ret && (d = readdir(dir)) != NULL) {
2964 char path[PATH_MAX];
2968 if ((d->d_type != DT_DIR) ||
2969 !strcmp(".", d->d_name) ||
2970 !strcmp("..", d->d_name))
2973 scnprintf(path, sizeof(path), "%s/%s/comm",
2974 procfs__mountpoint(), d->d_name);
2976 if (filename__read_str(path, &data, &size))
2979 ret = strncmp(name, data, len);
2984 return ret ? false : true;
2987 static bool is_amd(const char *arch, const char *cpuid)
2989 return arch && !strcmp("x86", arch) && cpuid && strstarts(cpuid, "AuthenticAMD");
2992 static bool is_amd_ibs(struct evsel *evsel)
2994 return evsel->core.attr.precise_ip
2995 || (evsel->pmu_name && !strncmp(evsel->pmu_name, "ibs", 3));
2998 int evsel__open_strerror(struct evsel *evsel, struct target *target,
2999 int err, char *msg, size_t size)
3001 struct perf_env *env = evsel__env(evsel);
3002 const char *arch = perf_env__arch(env);
3003 const char *cpuid = perf_env__cpuid(env);
3004 char sbuf[STRERR_BUFSIZE];
3005 int printed = 0, enforced = 0;
3010 printed += scnprintf(msg + printed, size - printed,
3011 "Access to performance monitoring and observability operations is limited.\n");
3013 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) {
3015 printed += scnprintf(msg + printed, size - printed,
3016 "Enforced MAC policy settings (SELinux) can limit access to performance\n"
3017 "monitoring and observability operations. Inspect system audit records for\n"
3018 "more perf_event access control information and adjusting the policy.\n");
3023 printed += scnprintf(msg, size,
3024 "No permission to enable %s event.\n\n", evsel__name(evsel));
3026 return scnprintf(msg + printed, size - printed,
3027 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n"
3028 "access to performance monitoring and observability operations for processes\n"
3029 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n"
3030 "More information can be found at 'Perf events and tool security' document:\n"
3031 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n"
3032 "perf_event_paranoid setting is %d:\n"
3033 " -1: Allow use of (almost) all events by all users\n"
3034 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
3035 ">= 0: Disallow raw and ftrace function tracepoint access\n"
3036 ">= 1: Disallow CPU event access\n"
3037 ">= 2: Disallow kernel profiling\n"
3038 "To make the adjusted perf_event_paranoid setting permanent preserve it\n"
3039 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)",
3040 perf_event_paranoid());
3042 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel));
3044 return scnprintf(msg, size, "%s",
3045 "Too many events are opened.\n"
3046 "Probably the maximum number of open file descriptors has been reached.\n"
3047 "Hint: Try again after reducing the number of events.\n"
3048 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
3050 if (evsel__has_callchain(evsel) &&
3051 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
3052 return scnprintf(msg, size,
3053 "Not enough memory to setup event with callchain.\n"
3054 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
3055 "Hint: Current value: %d", sysctl__max_stack());
3058 if (target->cpu_list)
3059 return scnprintf(msg, size, "%s",
3060 "No such device - did you specify an out-of-range profile CPU?");
3063 if (evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK)
3064 return scnprintf(msg, size,
3065 "%s: PMU Hardware or event type doesn't support branch stack sampling.",
3066 evsel__name(evsel));
3067 if (evsel->core.attr.aux_output)
3068 return scnprintf(msg, size,
3069 "%s: PMU Hardware doesn't support 'aux_output' feature",
3070 evsel__name(evsel));
3071 if (evsel->core.attr.sample_period != 0)
3072 return scnprintf(msg, size,
3073 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
3074 evsel__name(evsel));
3075 if (evsel->core.attr.precise_ip)
3076 return scnprintf(msg, size, "%s",
3077 "\'precise\' request may not be supported. Try removing 'p' modifier.");
3078 #if defined(__i386__) || defined(__x86_64__)
3079 if (evsel->core.attr.type == PERF_TYPE_HARDWARE)
3080 return scnprintf(msg, size, "%s",
3081 "No hardware sampling interrupt available.\n");
3085 if (find_process("oprofiled"))
3086 return scnprintf(msg, size,
3087 "The PMU counters are busy/taken by another profiler.\n"
3088 "We found oprofile daemon running, please stop it and try again.");
3091 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size)
3092 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel.");
3093 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size)
3094 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel.");
3095 if (evsel->core.attr.write_backward && perf_missing_features.write_backward)
3096 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
3097 if (perf_missing_features.clockid)
3098 return scnprintf(msg, size, "clockid feature not supported.");
3099 if (perf_missing_features.clockid_wrong)
3100 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
3101 if (perf_missing_features.aux_output)
3102 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel.");
3103 if (!target__has_cpu(target))
3104 return scnprintf(msg, size,
3105 "Invalid event (%s) in per-thread mode, enable system wide with '-a'.",
3106 evsel__name(evsel));
3107 if (is_amd(arch, cpuid)) {
3108 if (is_amd_ibs(evsel)) {
3109 if (evsel->core.attr.exclude_kernel)
3110 return scnprintf(msg, size,
3111 "AMD IBS can't exclude kernel events. Try running at a higher privilege level.");
3112 if (!evsel->core.system_wide)
3113 return scnprintf(msg, size,
3114 "AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinity");
3120 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. "
3121 "Please add an auxiliary event in front of the load latency event.");
3126 return scnprintf(msg, size,
3127 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
3128 "/bin/dmesg | grep -i perf may provide additional information.\n",
3129 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel));
3132 struct perf_env *evsel__env(struct evsel *evsel)
3134 if (evsel && evsel->evlist && evsel->evlist->env)
3135 return evsel->evlist->env;
3139 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
3141 int cpu_map_idx, thread;
3143 for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) {
3144 for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
3146 int fd = FD(evsel, cpu_map_idx, thread);
3148 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core,
3149 cpu_map_idx, thread, fd) < 0)
3157 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist)
3159 struct perf_cpu_map *cpus = evsel->core.cpus;
3160 struct perf_thread_map *threads = evsel->core.threads;
3162 if (perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr))
3165 return store_evsel_ids(evsel, evlist);
3168 void evsel__zero_per_pkg(struct evsel *evsel)
3170 struct hashmap_entry *cur;
3173 if (evsel->per_pkg_mask) {
3174 hashmap__for_each_entry(evsel->per_pkg_mask, cur, bkt)
3177 hashmap__clear(evsel->per_pkg_mask);
3181 bool evsel__is_hybrid(const struct evsel *evsel)
3183 return evsel->pmu_name && perf_pmu__is_hybrid(evsel->pmu_name);
3186 struct evsel *evsel__leader(const struct evsel *evsel)
3188 return container_of(evsel->core.leader, struct evsel, core);
3191 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader)
3193 return evsel->core.leader == &leader->core;
3196 bool evsel__is_leader(struct evsel *evsel)
3198 return evsel__has_leader(evsel, evsel);
3201 void evsel__set_leader(struct evsel *evsel, struct evsel *leader)
3203 evsel->core.leader = &leader->core;
3206 int evsel__source_count(const struct evsel *evsel)
3211 evlist__for_each_entry(evsel->evlist, pos) {
3212 if (pos->metric_leader == evsel)
3218 bool __weak arch_evsel__must_be_in_group(const struct evsel *evsel __maybe_unused)
3224 * Remove an event from a given group (leader).
3225 * Some events, e.g., perf metrics Topdown events,
3226 * must always be grouped. Ignore the events.
3228 void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader)
3230 if (!arch_evsel__must_be_in_group(evsel) && evsel != leader) {
3231 evsel__set_leader(evsel, evsel);
3232 evsel->core.nr_members = 0;
3233 leader->core.nr_members--;