2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
11 #include <linux/bitops.h>
12 #include <api/fs/tracing_path.h>
13 #include <traceevent/event-parse.h>
14 #include <linux/hw_breakpoint.h>
15 #include <linux/perf_event.h>
16 #include <linux/err.h>
17 #include <sys/resource.h>
19 #include "callchain.h"
25 #include "thread_map.h"
27 #include "perf_regs.h"
29 #include "trace-event.h"
39 } perf_missing_features;
41 static clockid_t clockid;
43 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
48 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
54 int (*init)(struct perf_evsel *evsel);
55 void (*fini)(struct perf_evsel *evsel);
56 } perf_evsel__object = {
57 .size = sizeof(struct perf_evsel),
58 .init = perf_evsel__no_extra_init,
59 .fini = perf_evsel__no_extra_fini,
62 int perf_evsel__object_config(size_t object_size,
63 int (*init)(struct perf_evsel *evsel),
64 void (*fini)(struct perf_evsel *evsel))
70 if (perf_evsel__object.size > object_size)
73 perf_evsel__object.size = object_size;
77 perf_evsel__object.init = init;
80 perf_evsel__object.fini = fini;
85 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
87 int __perf_evsel__sample_size(u64 sample_type)
89 u64 mask = sample_type & PERF_SAMPLE_MASK;
93 for (i = 0; i < 64; i++) {
94 if (mask & (1ULL << i))
104 * __perf_evsel__calc_id_pos - calculate id_pos.
105 * @sample_type: sample type
107 * This function returns the position of the event id (PERF_SAMPLE_ID or
108 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
111 static int __perf_evsel__calc_id_pos(u64 sample_type)
115 if (sample_type & PERF_SAMPLE_IDENTIFIER)
118 if (!(sample_type & PERF_SAMPLE_ID))
121 if (sample_type & PERF_SAMPLE_IP)
124 if (sample_type & PERF_SAMPLE_TID)
127 if (sample_type & PERF_SAMPLE_TIME)
130 if (sample_type & PERF_SAMPLE_ADDR)
137 * __perf_evsel__calc_is_pos - calculate is_pos.
138 * @sample_type: sample type
140 * This function returns the position (counting backwards) of the event id
141 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
142 * sample_id_all is used there is an id sample appended to non-sample events.
144 static int __perf_evsel__calc_is_pos(u64 sample_type)
148 if (sample_type & PERF_SAMPLE_IDENTIFIER)
151 if (!(sample_type & PERF_SAMPLE_ID))
154 if (sample_type & PERF_SAMPLE_CPU)
157 if (sample_type & PERF_SAMPLE_STREAM_ID)
163 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
165 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
166 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
169 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
170 enum perf_event_sample_format bit)
172 if (!(evsel->attr.sample_type & bit)) {
173 evsel->attr.sample_type |= bit;
174 evsel->sample_size += sizeof(u64);
175 perf_evsel__calc_id_pos(evsel);
179 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
180 enum perf_event_sample_format bit)
182 if (evsel->attr.sample_type & bit) {
183 evsel->attr.sample_type &= ~bit;
184 evsel->sample_size -= sizeof(u64);
185 perf_evsel__calc_id_pos(evsel);
189 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
190 bool can_sample_identifier)
192 if (can_sample_identifier) {
193 perf_evsel__reset_sample_bit(evsel, ID);
194 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
196 perf_evsel__set_sample_bit(evsel, ID);
198 evsel->attr.read_format |= PERF_FORMAT_ID;
201 void perf_evsel__init(struct perf_evsel *evsel,
202 struct perf_event_attr *attr, int idx)
205 evsel->tracking = !idx;
207 evsel->leader = evsel;
210 evsel->evlist = NULL;
212 INIT_LIST_HEAD(&evsel->node);
213 INIT_LIST_HEAD(&evsel->config_terms);
214 INIT_LIST_HEAD(&evsel->drv_config_terms);
215 perf_evsel__object.init(evsel);
216 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
217 perf_evsel__calc_id_pos(evsel);
218 evsel->cmdline_group_boundary = false;
221 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
223 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
226 perf_evsel__init(evsel, attr, idx);
232 * Returns pointer with encoded error via <linux/err.h> interface.
234 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
236 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
242 struct perf_event_attr attr = {
243 .type = PERF_TYPE_TRACEPOINT,
244 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
245 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
248 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
251 evsel->tp_format = trace_event__tp_format(sys, name);
252 if (IS_ERR(evsel->tp_format)) {
253 err = PTR_ERR(evsel->tp_format);
257 event_attr_init(&attr);
258 attr.config = evsel->tp_format->id;
259 attr.sample_period = 1;
260 perf_evsel__init(evsel, &attr, idx);
272 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
280 "stalled-cycles-frontend",
281 "stalled-cycles-backend",
285 static const char *__perf_evsel__hw_name(u64 config)
287 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
288 return perf_evsel__hw_names[config];
290 return "unknown-hardware";
293 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
295 int colon = 0, r = 0;
296 struct perf_event_attr *attr = &evsel->attr;
297 bool exclude_guest_default = false;
299 #define MOD_PRINT(context, mod) do { \
300 if (!attr->exclude_##context) { \
301 if (!colon) colon = ++r; \
302 r += scnprintf(bf + r, size - r, "%c", mod); \
305 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
306 MOD_PRINT(kernel, 'k');
307 MOD_PRINT(user, 'u');
309 exclude_guest_default = true;
312 if (attr->precise_ip) {
315 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
316 exclude_guest_default = true;
319 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
320 MOD_PRINT(host, 'H');
321 MOD_PRINT(guest, 'G');
329 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
331 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
332 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
335 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
348 static const char *__perf_evsel__sw_name(u64 config)
350 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
351 return perf_evsel__sw_names[config];
352 return "unknown-software";
355 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
357 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
358 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
361 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
365 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
367 if (type & HW_BREAKPOINT_R)
368 r += scnprintf(bf + r, size - r, "r");
370 if (type & HW_BREAKPOINT_W)
371 r += scnprintf(bf + r, size - r, "w");
373 if (type & HW_BREAKPOINT_X)
374 r += scnprintf(bf + r, size - r, "x");
379 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
381 struct perf_event_attr *attr = &evsel->attr;
382 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
383 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
386 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
387 [PERF_EVSEL__MAX_ALIASES] = {
388 { "L1-dcache", "l1-d", "l1d", "L1-data", },
389 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
391 { "dTLB", "d-tlb", "Data-TLB", },
392 { "iTLB", "i-tlb", "Instruction-TLB", },
393 { "branch", "branches", "bpu", "btb", "bpc", },
397 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
398 [PERF_EVSEL__MAX_ALIASES] = {
399 { "load", "loads", "read", },
400 { "store", "stores", "write", },
401 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
404 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
405 [PERF_EVSEL__MAX_ALIASES] = {
406 { "refs", "Reference", "ops", "access", },
407 { "misses", "miss", },
410 #define C(x) PERF_COUNT_HW_CACHE_##x
411 #define CACHE_READ (1 << C(OP_READ))
412 #define CACHE_WRITE (1 << C(OP_WRITE))
413 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
414 #define COP(x) (1 << x)
417 * cache operartion stat
418 * L1I : Read and prefetch only
419 * ITLB and BPU : Read-only
421 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
422 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
423 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
424 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
425 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
426 [C(ITLB)] = (CACHE_READ),
427 [C(BPU)] = (CACHE_READ),
428 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
431 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
433 if (perf_evsel__hw_cache_stat[type] & COP(op))
434 return true; /* valid */
436 return false; /* invalid */
439 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
440 char *bf, size_t size)
443 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
444 perf_evsel__hw_cache_op[op][0],
445 perf_evsel__hw_cache_result[result][0]);
448 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
449 perf_evsel__hw_cache_op[op][1]);
452 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
454 u8 op, result, type = (config >> 0) & 0xff;
455 const char *err = "unknown-ext-hardware-cache-type";
457 if (type > PERF_COUNT_HW_CACHE_MAX)
460 op = (config >> 8) & 0xff;
461 err = "unknown-ext-hardware-cache-op";
462 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
465 result = (config >> 16) & 0xff;
466 err = "unknown-ext-hardware-cache-result";
467 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
470 err = "invalid-cache";
471 if (!perf_evsel__is_cache_op_valid(type, op))
474 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
476 return scnprintf(bf, size, "%s", err);
479 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
481 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
482 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
485 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
487 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
488 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
491 const char *perf_evsel__name(struct perf_evsel *evsel)
498 switch (evsel->attr.type) {
500 perf_evsel__raw_name(evsel, bf, sizeof(bf));
503 case PERF_TYPE_HARDWARE:
504 perf_evsel__hw_name(evsel, bf, sizeof(bf));
507 case PERF_TYPE_HW_CACHE:
508 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
511 case PERF_TYPE_SOFTWARE:
512 perf_evsel__sw_name(evsel, bf, sizeof(bf));
515 case PERF_TYPE_TRACEPOINT:
516 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
519 case PERF_TYPE_BREAKPOINT:
520 perf_evsel__bp_name(evsel, bf, sizeof(bf));
524 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
529 evsel->name = strdup(bf);
531 return evsel->name ?: "unknown";
534 const char *perf_evsel__group_name(struct perf_evsel *evsel)
536 return evsel->group_name ?: "anon group";
539 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
542 struct perf_evsel *pos;
543 const char *group_name = perf_evsel__group_name(evsel);
545 ret = scnprintf(buf, size, "%s", group_name);
547 ret += scnprintf(buf + ret, size - ret, " { %s",
548 perf_evsel__name(evsel));
550 for_each_group_member(pos, evsel)
551 ret += scnprintf(buf + ret, size - ret, ", %s",
552 perf_evsel__name(pos));
554 ret += scnprintf(buf + ret, size - ret, " }");
560 perf_evsel__config_callgraph(struct perf_evsel *evsel,
561 struct record_opts *opts,
562 struct callchain_param *param)
564 bool function = perf_evsel__is_function_event(evsel);
565 struct perf_event_attr *attr = &evsel->attr;
567 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
569 if (param->record_mode == CALLCHAIN_LBR) {
570 if (!opts->branch_stack) {
571 if (attr->exclude_user) {
572 pr_warning("LBR callstack option is only available "
573 "to get user callchain information. "
574 "Falling back to framepointers.\n");
576 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
577 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
578 PERF_SAMPLE_BRANCH_CALL_STACK;
581 pr_warning("Cannot use LBR callstack with branch stack. "
582 "Falling back to framepointers.\n");
585 if (param->record_mode == CALLCHAIN_DWARF) {
587 perf_evsel__set_sample_bit(evsel, REGS_USER);
588 perf_evsel__set_sample_bit(evsel, STACK_USER);
589 attr->sample_regs_user = PERF_REGS_MASK;
590 attr->sample_stack_user = param->dump_size;
591 attr->exclude_callchain_user = 1;
593 pr_info("Cannot use DWARF unwind for function trace event,"
594 " falling back to framepointers.\n");
599 pr_info("Disabling user space callchains for function trace event.\n");
600 attr->exclude_callchain_user = 1;
605 perf_evsel__reset_callgraph(struct perf_evsel *evsel,
606 struct callchain_param *param)
608 struct perf_event_attr *attr = &evsel->attr;
610 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
611 if (param->record_mode == CALLCHAIN_LBR) {
612 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
613 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
614 PERF_SAMPLE_BRANCH_CALL_STACK);
616 if (param->record_mode == CALLCHAIN_DWARF) {
617 perf_evsel__reset_sample_bit(evsel, REGS_USER);
618 perf_evsel__reset_sample_bit(evsel, STACK_USER);
622 static void apply_config_terms(struct perf_evsel *evsel,
623 struct record_opts *opts)
625 struct perf_evsel_config_term *term;
626 struct list_head *config_terms = &evsel->config_terms;
627 struct perf_event_attr *attr = &evsel->attr;
628 /* callgraph default */
629 struct callchain_param param = {
630 .record_mode = callchain_param.record_mode,
633 char *callgraph_buf = NULL;
635 list_for_each_entry(term, config_terms, list) {
636 switch (term->type) {
637 case PERF_EVSEL__CONFIG_TERM_PERIOD:
638 attr->sample_period = term->val.period;
641 case PERF_EVSEL__CONFIG_TERM_FREQ:
642 attr->sample_freq = term->val.freq;
645 case PERF_EVSEL__CONFIG_TERM_TIME:
647 perf_evsel__set_sample_bit(evsel, TIME);
649 perf_evsel__reset_sample_bit(evsel, TIME);
651 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
652 callgraph_buf = term->val.callgraph;
654 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
655 dump_size = term->val.stack_user;
657 case PERF_EVSEL__CONFIG_TERM_INHERIT:
659 * attr->inherit should has already been set by
660 * perf_evsel__config. If user explicitly set
661 * inherit using config terms, override global
662 * opt->no_inherit setting.
664 attr->inherit = term->val.inherit ? 1 : 0;
671 /* User explicitly set per-event callgraph, clear the old setting and reset. */
672 if ((callgraph_buf != NULL) || (dump_size > 0)) {
674 /* parse callgraph parameters */
675 if (callgraph_buf != NULL) {
676 if (!strcmp(callgraph_buf, "no")) {
677 param.enabled = false;
678 param.record_mode = CALLCHAIN_NONE;
680 param.enabled = true;
681 if (parse_callchain_record(callgraph_buf, ¶m)) {
682 pr_err("per-event callgraph setting for %s failed. "
683 "Apply callgraph global setting for it\n",
690 dump_size = round_up(dump_size, sizeof(u64));
691 param.dump_size = dump_size;
694 /* If global callgraph set, clear it */
695 if (callchain_param.enabled)
696 perf_evsel__reset_callgraph(evsel, &callchain_param);
698 /* set perf-event callgraph */
700 perf_evsel__config_callgraph(evsel, opts, ¶m);
705 * The enable_on_exec/disabled value strategy:
707 * 1) For any type of traced program:
708 * - all independent events and group leaders are disabled
709 * - all group members are enabled
711 * Group members are ruled by group leaders. They need to
712 * be enabled, because the group scheduling relies on that.
714 * 2) For traced programs executed by perf:
715 * - all independent events and group leaders have
717 * - we don't specifically enable or disable any event during
720 * Independent events and group leaders are initially disabled
721 * and get enabled by exec. Group members are ruled by group
722 * leaders as stated in 1).
724 * 3) For traced programs attached by perf (pid/tid):
725 * - we specifically enable or disable all events during
728 * When attaching events to already running traced we
729 * enable/disable events specifically, as there's no
730 * initial traced exec call.
732 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
734 struct perf_evsel *leader = evsel->leader;
735 struct perf_event_attr *attr = &evsel->attr;
736 int track = evsel->tracking;
737 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
739 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
740 attr->inherit = !opts->no_inherit;
742 perf_evsel__set_sample_bit(evsel, IP);
743 perf_evsel__set_sample_bit(evsel, TID);
745 if (evsel->sample_read) {
746 perf_evsel__set_sample_bit(evsel, READ);
749 * We need ID even in case of single event, because
750 * PERF_SAMPLE_READ process ID specific data.
752 perf_evsel__set_sample_id(evsel, false);
755 * Apply group format only if we belong to group
756 * with more than one members.
758 if (leader->nr_members > 1) {
759 attr->read_format |= PERF_FORMAT_GROUP;
765 * We default some events to have a default interval. But keep
766 * it a weak assumption overridable by the user.
768 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
769 opts->user_interval != ULLONG_MAX)) {
771 perf_evsel__set_sample_bit(evsel, PERIOD);
773 attr->sample_freq = opts->freq;
775 attr->sample_period = opts->default_interval;
780 * Disable sampling for all group members other
781 * than leader in case leader 'leads' the sampling.
783 if ((leader != evsel) && leader->sample_read) {
784 attr->sample_freq = 0;
785 attr->sample_period = 0;
788 if (opts->no_samples)
789 attr->sample_freq = 0;
791 if (opts->inherit_stat)
792 attr->inherit_stat = 1;
794 if (opts->sample_address) {
795 perf_evsel__set_sample_bit(evsel, ADDR);
796 attr->mmap_data = track;
800 * We don't allow user space callchains for function trace
801 * event, due to issues with page faults while tracing page
802 * fault handler and its overall trickiness nature.
804 if (perf_evsel__is_function_event(evsel))
805 evsel->attr.exclude_callchain_user = 1;
807 if (callchain_param.enabled && !evsel->no_aux_samples)
808 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
810 if (opts->sample_intr_regs) {
811 attr->sample_regs_intr = opts->sample_intr_regs;
812 perf_evsel__set_sample_bit(evsel, REGS_INTR);
815 if (target__has_cpu(&opts->target))
816 perf_evsel__set_sample_bit(evsel, CPU);
819 perf_evsel__set_sample_bit(evsel, PERIOD);
822 * When the user explicitely disabled time don't force it here.
824 if (opts->sample_time &&
825 (!perf_missing_features.sample_id_all &&
826 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
827 opts->sample_time_set)))
828 perf_evsel__set_sample_bit(evsel, TIME);
830 if (opts->raw_samples && !evsel->no_aux_samples) {
831 perf_evsel__set_sample_bit(evsel, TIME);
832 perf_evsel__set_sample_bit(evsel, RAW);
833 perf_evsel__set_sample_bit(evsel, CPU);
836 if (opts->sample_address)
837 perf_evsel__set_sample_bit(evsel, DATA_SRC);
839 if (opts->no_buffering) {
841 attr->wakeup_events = 1;
843 if (opts->branch_stack && !evsel->no_aux_samples) {
844 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
845 attr->branch_sample_type = opts->branch_stack;
848 if (opts->sample_weight)
849 perf_evsel__set_sample_bit(evsel, WEIGHT);
853 attr->mmap2 = track && !perf_missing_features.mmap2;
856 if (opts->record_switch_events)
857 attr->context_switch = track;
859 if (opts->sample_transaction)
860 perf_evsel__set_sample_bit(evsel, TRANSACTION);
862 if (opts->running_time) {
863 evsel->attr.read_format |=
864 PERF_FORMAT_TOTAL_TIME_ENABLED |
865 PERF_FORMAT_TOTAL_TIME_RUNNING;
869 * XXX see the function comment above
871 * Disabling only independent events or group leaders,
872 * keeping group members enabled.
874 if (perf_evsel__is_group_leader(evsel))
878 * Setting enable_on_exec for independent events and
879 * group leaders for traced executed by perf.
881 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
882 !opts->initial_delay)
883 attr->enable_on_exec = 1;
885 if (evsel->immediate) {
887 attr->enable_on_exec = 0;
890 clockid = opts->clockid;
891 if (opts->use_clockid) {
892 attr->use_clockid = 1;
893 attr->clockid = opts->clockid;
896 if (evsel->precise_max)
897 perf_event_attr__set_max_precise_ip(attr);
900 * Apply event specific term settings,
901 * it overloads any global configuration.
903 apply_config_terms(evsel, opts);
906 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
910 if (evsel->system_wide)
913 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
916 for (cpu = 0; cpu < ncpus; cpu++) {
917 for (thread = 0; thread < nthreads; thread++) {
918 FD(evsel, cpu, thread) = -1;
923 return evsel->fd != NULL ? 0 : -ENOMEM;
926 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
931 if (evsel->system_wide)
934 for (cpu = 0; cpu < ncpus; cpu++) {
935 for (thread = 0; thread < nthreads; thread++) {
936 int fd = FD(evsel, cpu, thread),
937 err = ioctl(fd, ioc, arg);
947 int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
950 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
951 PERF_EVENT_IOC_SET_FILTER,
955 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
957 char *new_filter = strdup(filter);
959 if (new_filter != NULL) {
961 evsel->filter = new_filter;
968 int perf_evsel__append_filter(struct perf_evsel *evsel,
969 const char *op, const char *filter)
973 if (evsel->filter == NULL)
974 return perf_evsel__set_filter(evsel, filter);
976 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
978 evsel->filter = new_filter;
985 int perf_evsel__apply_drv_configs(struct perf_evsel *evsel,
986 int ncpus, int nthreads,
987 struct perf_evsel_config_term **err_term)
990 struct perf_evsel_config_term *term;
992 list_for_each_entry(term, &evsel->drv_config_terms, list) {
993 err = perf_evsel__run_ioctl(evsel, ncpus, nthreads,
994 PERF_EVENT_IOC_SET_DRV_CONFIGS,
995 (void *)term->val.drv_cfg);
1006 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
1008 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1009 PERF_EVENT_IOC_ENABLE,
1013 int perf_evsel__disable(struct perf_evsel *evsel)
1015 int nthreads = thread_map__nr(evsel->threads);
1016 int ncpus = cpu_map__nr(evsel->cpus);
1018 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1019 PERF_EVENT_IOC_DISABLE,
1023 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1025 if (ncpus == 0 || nthreads == 0)
1028 if (evsel->system_wide)
1031 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1032 if (evsel->sample_id == NULL)
1035 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1036 if (evsel->id == NULL) {
1037 xyarray__delete(evsel->sample_id);
1038 evsel->sample_id = NULL;
1045 static void perf_evsel__free_fd(struct perf_evsel *evsel)
1047 xyarray__delete(evsel->fd);
1051 static void perf_evsel__free_id(struct perf_evsel *evsel)
1053 xyarray__delete(evsel->sample_id);
1054 evsel->sample_id = NULL;
1058 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1060 struct perf_evsel_config_term *term, *h;
1062 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1063 list_del(&term->list);
1068 static void perf_evsel__free_drv_config_terms(struct perf_evsel *evsel)
1070 struct perf_evsel_config_term *term, *h;
1072 list_for_each_entry_safe(term, h, &evsel->drv_config_terms, list) {
1073 list_del(&term->list);
1078 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1082 if (evsel->system_wide)
1085 for (cpu = 0; cpu < ncpus; cpu++)
1086 for (thread = 0; thread < nthreads; ++thread) {
1087 close(FD(evsel, cpu, thread));
1088 FD(evsel, cpu, thread) = -1;
1092 void perf_evsel__exit(struct perf_evsel *evsel)
1094 assert(list_empty(&evsel->node));
1095 assert(evsel->evlist == NULL);
1096 perf_evsel__free_counts(evsel);
1097 perf_evsel__free_fd(evsel);
1098 perf_evsel__free_id(evsel);
1099 perf_evsel__free_config_terms(evsel);
1100 perf_evsel__free_drv_config_terms(evsel);
1101 close_cgroup(evsel->cgrp);
1102 cpu_map__put(evsel->cpus);
1103 cpu_map__put(evsel->own_cpus);
1104 thread_map__put(evsel->threads);
1105 zfree(&evsel->group_name);
1106 zfree(&evsel->name);
1107 perf_evsel__object.fini(evsel);
1110 void perf_evsel__delete(struct perf_evsel *evsel)
1112 perf_evsel__exit(evsel);
1116 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1117 struct perf_counts_values *count)
1119 struct perf_counts_values tmp;
1121 if (!evsel->prev_raw_counts)
1125 tmp = evsel->prev_raw_counts->aggr;
1126 evsel->prev_raw_counts->aggr = *count;
1128 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1129 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1132 count->val = count->val - tmp.val;
1133 count->ena = count->ena - tmp.ena;
1134 count->run = count->run - tmp.run;
1137 void perf_counts_values__scale(struct perf_counts_values *count,
1138 bool scale, s8 *pscaled)
1143 if (count->run == 0) {
1146 } else if (count->run < count->ena) {
1148 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1151 count->ena = count->run = 0;
1157 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1158 struct perf_counts_values *count)
1160 memset(count, 0, sizeof(*count));
1162 if (FD(evsel, cpu, thread) < 0)
1165 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1171 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1172 int cpu, int thread, bool scale)
1174 struct perf_counts_values count;
1175 size_t nv = scale ? 3 : 1;
1177 if (FD(evsel, cpu, thread) < 0)
1180 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1183 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1186 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1187 perf_counts_values__scale(&count, scale, NULL);
1188 *perf_counts(evsel->counts, cpu, thread) = count;
1192 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1194 struct perf_evsel *leader = evsel->leader;
1197 if (perf_evsel__is_group_leader(evsel))
1201 * Leader must be already processed/open,
1202 * if not it's a bug.
1204 BUG_ON(!leader->fd);
1206 fd = FD(leader, cpu, thread);
1217 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1219 bool first_bit = true;
1223 if (value & bits[i].bit) {
1224 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1227 } while (bits[++i].name != NULL);
1230 static void __p_sample_type(char *buf, size_t size, u64 value)
1232 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1233 struct bit_names bits[] = {
1234 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1235 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1236 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1237 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1238 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1242 __p_bits(buf, size, value, bits);
1245 static void __p_read_format(char *buf, size_t size, u64 value)
1247 #define bit_name(n) { PERF_FORMAT_##n, #n }
1248 struct bit_names bits[] = {
1249 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1250 bit_name(ID), bit_name(GROUP),
1254 __p_bits(buf, size, value, bits);
1257 #define BUF_SIZE 1024
1259 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1260 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1261 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1262 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1263 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1265 #define PRINT_ATTRn(_n, _f, _p) \
1269 ret += attr__fprintf(fp, _n, buf, priv);\
1273 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1275 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1276 attr__fprintf_f attr__fprintf, void *priv)
1281 PRINT_ATTRf(type, p_unsigned);
1282 PRINT_ATTRf(size, p_unsigned);
1283 PRINT_ATTRf(config, p_hex);
1284 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1285 PRINT_ATTRf(sample_type, p_sample_type);
1286 PRINT_ATTRf(read_format, p_read_format);
1288 PRINT_ATTRf(disabled, p_unsigned);
1289 PRINT_ATTRf(inherit, p_unsigned);
1290 PRINT_ATTRf(pinned, p_unsigned);
1291 PRINT_ATTRf(exclusive, p_unsigned);
1292 PRINT_ATTRf(exclude_user, p_unsigned);
1293 PRINT_ATTRf(exclude_kernel, p_unsigned);
1294 PRINT_ATTRf(exclude_hv, p_unsigned);
1295 PRINT_ATTRf(exclude_idle, p_unsigned);
1296 PRINT_ATTRf(mmap, p_unsigned);
1297 PRINT_ATTRf(comm, p_unsigned);
1298 PRINT_ATTRf(freq, p_unsigned);
1299 PRINT_ATTRf(inherit_stat, p_unsigned);
1300 PRINT_ATTRf(enable_on_exec, p_unsigned);
1301 PRINT_ATTRf(task, p_unsigned);
1302 PRINT_ATTRf(watermark, p_unsigned);
1303 PRINT_ATTRf(precise_ip, p_unsigned);
1304 PRINT_ATTRf(mmap_data, p_unsigned);
1305 PRINT_ATTRf(sample_id_all, p_unsigned);
1306 PRINT_ATTRf(exclude_host, p_unsigned);
1307 PRINT_ATTRf(exclude_guest, p_unsigned);
1308 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1309 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1310 PRINT_ATTRf(mmap2, p_unsigned);
1311 PRINT_ATTRf(comm_exec, p_unsigned);
1312 PRINT_ATTRf(use_clockid, p_unsigned);
1313 PRINT_ATTRf(context_switch, p_unsigned);
1315 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1316 PRINT_ATTRf(bp_type, p_unsigned);
1317 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1318 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1319 PRINT_ATTRf(branch_sample_type, p_unsigned);
1320 PRINT_ATTRf(sample_regs_user, p_hex);
1321 PRINT_ATTRf(sample_stack_user, p_unsigned);
1322 PRINT_ATTRf(clockid, p_signed);
1323 PRINT_ATTRf(sample_regs_intr, p_hex);
1324 PRINT_ATTRf(aux_watermark, p_unsigned);
1329 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1330 void *priv __attribute__((unused)))
1332 return fprintf(fp, " %-32s %s\n", name, val);
1335 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1336 struct thread_map *threads)
1338 int cpu, thread, nthreads;
1339 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1341 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1343 if (evsel->system_wide)
1346 nthreads = threads->nr;
1348 if (evsel->fd == NULL &&
1349 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1353 flags |= PERF_FLAG_PID_CGROUP;
1354 pid = evsel->cgrp->fd;
1357 fallback_missing_features:
1358 if (perf_missing_features.clockid_wrong)
1359 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1360 if (perf_missing_features.clockid) {
1361 evsel->attr.use_clockid = 0;
1362 evsel->attr.clockid = 0;
1364 if (perf_missing_features.cloexec)
1365 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1366 if (perf_missing_features.mmap2)
1367 evsel->attr.mmap2 = 0;
1368 if (perf_missing_features.exclude_guest)
1369 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1371 if (perf_missing_features.sample_id_all)
1372 evsel->attr.sample_id_all = 0;
1375 fprintf(stderr, "%.60s\n", graph_dotted_line);
1376 fprintf(stderr, "perf_event_attr:\n");
1377 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1378 fprintf(stderr, "%.60s\n", graph_dotted_line);
1381 for (cpu = 0; cpu < cpus->nr; cpu++) {
1383 for (thread = 0; thread < nthreads; thread++) {
1386 if (!evsel->cgrp && !evsel->system_wide)
1387 pid = thread_map__pid(threads, thread);
1389 group_fd = get_group_fd(evsel, cpu, thread);
1391 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1392 pid, cpus->map[cpu], group_fd, flags);
1394 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1398 if (FD(evsel, cpu, thread) < 0) {
1400 pr_debug2("sys_perf_event_open failed, error %d\n",
1405 if (evsel->bpf_fd >= 0) {
1406 int evt_fd = FD(evsel, cpu, thread);
1407 int bpf_fd = evsel->bpf_fd;
1410 PERF_EVENT_IOC_SET_BPF,
1412 if (err && errno != EEXIST) {
1413 pr_err("failed to attach bpf fd %d: %s\n",
1414 bpf_fd, strerror(errno));
1420 set_rlimit = NO_CHANGE;
1423 * If we succeeded but had to kill clockid, fail and
1424 * have perf_evsel__open_strerror() print us a nice
1427 if (perf_missing_features.clockid ||
1428 perf_missing_features.clockid_wrong) {
1439 * perf stat needs between 5 and 22 fds per CPU. When we run out
1440 * of them try to increase the limits.
1442 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1444 int old_errno = errno;
1446 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1447 if (set_rlimit == NO_CHANGE)
1448 l.rlim_cur = l.rlim_max;
1450 l.rlim_cur = l.rlim_max + 1000;
1451 l.rlim_max = l.rlim_cur;
1453 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1462 if (err != -EINVAL || cpu > 0 || thread > 0)
1466 * Must probe features in the order they were added to the
1467 * perf_event_attr interface.
1469 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1470 perf_missing_features.clockid_wrong = true;
1471 goto fallback_missing_features;
1472 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1473 perf_missing_features.clockid = true;
1474 goto fallback_missing_features;
1475 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1476 perf_missing_features.cloexec = true;
1477 goto fallback_missing_features;
1478 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1479 perf_missing_features.mmap2 = true;
1480 goto fallback_missing_features;
1481 } else if (!perf_missing_features.exclude_guest &&
1482 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1483 perf_missing_features.exclude_guest = true;
1484 goto fallback_missing_features;
1485 } else if (!perf_missing_features.sample_id_all) {
1486 perf_missing_features.sample_id_all = true;
1487 goto retry_sample_id;
1492 while (--thread >= 0) {
1493 close(FD(evsel, cpu, thread));
1494 FD(evsel, cpu, thread) = -1;
1497 } while (--cpu >= 0);
1501 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1503 if (evsel->fd == NULL)
1506 perf_evsel__close_fd(evsel, ncpus, nthreads);
1507 perf_evsel__free_fd(evsel);
1519 struct thread_map map;
1521 } empty_thread_map = {
1526 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1527 struct thread_map *threads)
1530 /* Work around old compiler warnings about strict aliasing */
1531 cpus = &empty_cpu_map.map;
1534 if (threads == NULL)
1535 threads = &empty_thread_map.map;
1537 return __perf_evsel__open(evsel, cpus, threads);
1540 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1541 struct cpu_map *cpus)
1543 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1546 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1547 struct thread_map *threads)
1549 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1552 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1553 const union perf_event *event,
1554 struct perf_sample *sample)
1556 u64 type = evsel->attr.sample_type;
1557 const u64 *array = event->sample.array;
1558 bool swapped = evsel->needs_swap;
1561 array += ((event->header.size -
1562 sizeof(event->header)) / sizeof(u64)) - 1;
1564 if (type & PERF_SAMPLE_IDENTIFIER) {
1565 sample->id = *array;
1569 if (type & PERF_SAMPLE_CPU) {
1572 /* undo swap of u64, then swap on individual u32s */
1573 u.val64 = bswap_64(u.val64);
1574 u.val32[0] = bswap_32(u.val32[0]);
1577 sample->cpu = u.val32[0];
1581 if (type & PERF_SAMPLE_STREAM_ID) {
1582 sample->stream_id = *array;
1586 if (type & PERF_SAMPLE_ID) {
1587 sample->id = *array;
1591 if (type & PERF_SAMPLE_TIME) {
1592 sample->time = *array;
1596 if (type & PERF_SAMPLE_TID) {
1599 /* undo swap of u64, then swap on individual u32s */
1600 u.val64 = bswap_64(u.val64);
1601 u.val32[0] = bswap_32(u.val32[0]);
1602 u.val32[1] = bswap_32(u.val32[1]);
1605 sample->pid = u.val32[0];
1606 sample->tid = u.val32[1];
1613 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1616 return size > max_size || offset + size > endp;
1619 #define OVERFLOW_CHECK(offset, size, max_size) \
1621 if (overflow(endp, (max_size), (offset), (size))) \
1625 #define OVERFLOW_CHECK_u64(offset) \
1626 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1628 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1629 struct perf_sample *data)
1631 u64 type = evsel->attr.sample_type;
1632 bool swapped = evsel->needs_swap;
1634 u16 max_size = event->header.size;
1635 const void *endp = (void *)event + max_size;
1639 * used for cross-endian analysis. See git commit 65014ab3
1640 * for why this goofiness is needed.
1644 memset(data, 0, sizeof(*data));
1645 data->cpu = data->pid = data->tid = -1;
1646 data->stream_id = data->id = data->time = -1ULL;
1647 data->period = evsel->attr.sample_period;
1650 if (event->header.type != PERF_RECORD_SAMPLE) {
1651 if (!evsel->attr.sample_id_all)
1653 return perf_evsel__parse_id_sample(evsel, event, data);
1656 array = event->sample.array;
1659 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1660 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1661 * check the format does not go past the end of the event.
1663 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1667 if (type & PERF_SAMPLE_IDENTIFIER) {
1672 if (type & PERF_SAMPLE_IP) {
1677 if (type & PERF_SAMPLE_TID) {
1680 /* undo swap of u64, then swap on individual u32s */
1681 u.val64 = bswap_64(u.val64);
1682 u.val32[0] = bswap_32(u.val32[0]);
1683 u.val32[1] = bswap_32(u.val32[1]);
1686 data->pid = u.val32[0];
1687 data->tid = u.val32[1];
1691 if (type & PERF_SAMPLE_TIME) {
1692 data->time = *array;
1697 if (type & PERF_SAMPLE_ADDR) {
1698 data->addr = *array;
1702 if (type & PERF_SAMPLE_ID) {
1707 if (type & PERF_SAMPLE_STREAM_ID) {
1708 data->stream_id = *array;
1712 if (type & PERF_SAMPLE_CPU) {
1716 /* undo swap of u64, then swap on individual u32s */
1717 u.val64 = bswap_64(u.val64);
1718 u.val32[0] = bswap_32(u.val32[0]);
1721 data->cpu = u.val32[0];
1725 if (type & PERF_SAMPLE_PERIOD) {
1726 data->period = *array;
1730 if (type & PERF_SAMPLE_READ) {
1731 u64 read_format = evsel->attr.read_format;
1733 OVERFLOW_CHECK_u64(array);
1734 if (read_format & PERF_FORMAT_GROUP)
1735 data->read.group.nr = *array;
1737 data->read.one.value = *array;
1741 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1742 OVERFLOW_CHECK_u64(array);
1743 data->read.time_enabled = *array;
1747 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1748 OVERFLOW_CHECK_u64(array);
1749 data->read.time_running = *array;
1753 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1754 if (read_format & PERF_FORMAT_GROUP) {
1755 const u64 max_group_nr = UINT64_MAX /
1756 sizeof(struct sample_read_value);
1758 if (data->read.group.nr > max_group_nr)
1760 sz = data->read.group.nr *
1761 sizeof(struct sample_read_value);
1762 OVERFLOW_CHECK(array, sz, max_size);
1763 data->read.group.values =
1764 (struct sample_read_value *)array;
1765 array = (void *)array + sz;
1767 OVERFLOW_CHECK_u64(array);
1768 data->read.one.id = *array;
1773 if (type & PERF_SAMPLE_CALLCHAIN) {
1774 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1776 OVERFLOW_CHECK_u64(array);
1777 data->callchain = (struct ip_callchain *)array++;
1778 if (data->callchain->nr > max_callchain_nr)
1780 sz = data->callchain->nr * sizeof(u64);
1781 OVERFLOW_CHECK(array, sz, max_size);
1782 array = (void *)array + sz;
1785 if (type & PERF_SAMPLE_RAW) {
1786 OVERFLOW_CHECK_u64(array);
1788 if (WARN_ONCE(swapped,
1789 "Endianness of raw data not corrected!\n")) {
1790 /* undo swap of u64, then swap on individual u32s */
1791 u.val64 = bswap_64(u.val64);
1792 u.val32[0] = bswap_32(u.val32[0]);
1793 u.val32[1] = bswap_32(u.val32[1]);
1795 data->raw_size = u.val32[0];
1796 array = (void *)array + sizeof(u32);
1798 OVERFLOW_CHECK(array, data->raw_size, max_size);
1799 data->raw_data = (void *)array;
1800 array = (void *)array + data->raw_size;
1803 if (type & PERF_SAMPLE_BRANCH_STACK) {
1804 const u64 max_branch_nr = UINT64_MAX /
1805 sizeof(struct branch_entry);
1807 OVERFLOW_CHECK_u64(array);
1808 data->branch_stack = (struct branch_stack *)array++;
1810 if (data->branch_stack->nr > max_branch_nr)
1812 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1813 OVERFLOW_CHECK(array, sz, max_size);
1814 array = (void *)array + sz;
1817 if (type & PERF_SAMPLE_REGS_USER) {
1818 OVERFLOW_CHECK_u64(array);
1819 data->user_regs.abi = *array;
1822 if (data->user_regs.abi) {
1823 u64 mask = evsel->attr.sample_regs_user;
1825 sz = hweight_long(mask) * sizeof(u64);
1826 OVERFLOW_CHECK(array, sz, max_size);
1827 data->user_regs.mask = mask;
1828 data->user_regs.regs = (u64 *)array;
1829 array = (void *)array + sz;
1833 if (type & PERF_SAMPLE_STACK_USER) {
1834 OVERFLOW_CHECK_u64(array);
1837 data->user_stack.offset = ((char *)(array - 1)
1841 data->user_stack.size = 0;
1843 OVERFLOW_CHECK(array, sz, max_size);
1844 data->user_stack.data = (char *)array;
1845 array = (void *)array + sz;
1846 OVERFLOW_CHECK_u64(array);
1847 data->user_stack.size = *array++;
1848 if (WARN_ONCE(data->user_stack.size > sz,
1849 "user stack dump failure\n"))
1855 if (type & PERF_SAMPLE_WEIGHT) {
1856 OVERFLOW_CHECK_u64(array);
1857 data->weight = *array;
1861 data->data_src = PERF_MEM_DATA_SRC_NONE;
1862 if (type & PERF_SAMPLE_DATA_SRC) {
1863 OVERFLOW_CHECK_u64(array);
1864 data->data_src = *array;
1868 data->transaction = 0;
1869 if (type & PERF_SAMPLE_TRANSACTION) {
1870 OVERFLOW_CHECK_u64(array);
1871 data->transaction = *array;
1875 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1876 if (type & PERF_SAMPLE_REGS_INTR) {
1877 OVERFLOW_CHECK_u64(array);
1878 data->intr_regs.abi = *array;
1881 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1882 u64 mask = evsel->attr.sample_regs_intr;
1884 sz = hweight_long(mask) * sizeof(u64);
1885 OVERFLOW_CHECK(array, sz, max_size);
1886 data->intr_regs.mask = mask;
1887 data->intr_regs.regs = (u64 *)array;
1888 array = (void *)array + sz;
1895 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1898 size_t sz, result = sizeof(struct sample_event);
1900 if (type & PERF_SAMPLE_IDENTIFIER)
1901 result += sizeof(u64);
1903 if (type & PERF_SAMPLE_IP)
1904 result += sizeof(u64);
1906 if (type & PERF_SAMPLE_TID)
1907 result += sizeof(u64);
1909 if (type & PERF_SAMPLE_TIME)
1910 result += sizeof(u64);
1912 if (type & PERF_SAMPLE_ADDR)
1913 result += sizeof(u64);
1915 if (type & PERF_SAMPLE_ID)
1916 result += sizeof(u64);
1918 if (type & PERF_SAMPLE_STREAM_ID)
1919 result += sizeof(u64);
1921 if (type & PERF_SAMPLE_CPU)
1922 result += sizeof(u64);
1924 if (type & PERF_SAMPLE_PERIOD)
1925 result += sizeof(u64);
1927 if (type & PERF_SAMPLE_READ) {
1928 result += sizeof(u64);
1929 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1930 result += sizeof(u64);
1931 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1932 result += sizeof(u64);
1933 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1934 if (read_format & PERF_FORMAT_GROUP) {
1935 sz = sample->read.group.nr *
1936 sizeof(struct sample_read_value);
1939 result += sizeof(u64);
1943 if (type & PERF_SAMPLE_CALLCHAIN) {
1944 sz = (sample->callchain->nr + 1) * sizeof(u64);
1948 if (type & PERF_SAMPLE_RAW) {
1949 result += sizeof(u32);
1950 result += sample->raw_size;
1953 if (type & PERF_SAMPLE_BRANCH_STACK) {
1954 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1959 if (type & PERF_SAMPLE_REGS_USER) {
1960 if (sample->user_regs.abi) {
1961 result += sizeof(u64);
1962 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1965 result += sizeof(u64);
1969 if (type & PERF_SAMPLE_STACK_USER) {
1970 sz = sample->user_stack.size;
1971 result += sizeof(u64);
1974 result += sizeof(u64);
1978 if (type & PERF_SAMPLE_WEIGHT)
1979 result += sizeof(u64);
1981 if (type & PERF_SAMPLE_DATA_SRC)
1982 result += sizeof(u64);
1984 if (type & PERF_SAMPLE_TRANSACTION)
1985 result += sizeof(u64);
1987 if (type & PERF_SAMPLE_REGS_INTR) {
1988 if (sample->intr_regs.abi) {
1989 result += sizeof(u64);
1990 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1993 result += sizeof(u64);
2000 int perf_event__synthesize_sample(union perf_event *event, u64 type,
2002 const struct perf_sample *sample,
2008 * used for cross-endian analysis. See git commit 65014ab3
2009 * for why this goofiness is needed.
2013 array = event->sample.array;
2015 if (type & PERF_SAMPLE_IDENTIFIER) {
2016 *array = sample->id;
2020 if (type & PERF_SAMPLE_IP) {
2021 *array = sample->ip;
2025 if (type & PERF_SAMPLE_TID) {
2026 u.val32[0] = sample->pid;
2027 u.val32[1] = sample->tid;
2030 * Inverse of what is done in perf_evsel__parse_sample
2032 u.val32[0] = bswap_32(u.val32[0]);
2033 u.val32[1] = bswap_32(u.val32[1]);
2034 u.val64 = bswap_64(u.val64);
2041 if (type & PERF_SAMPLE_TIME) {
2042 *array = sample->time;
2046 if (type & PERF_SAMPLE_ADDR) {
2047 *array = sample->addr;
2051 if (type & PERF_SAMPLE_ID) {
2052 *array = sample->id;
2056 if (type & PERF_SAMPLE_STREAM_ID) {
2057 *array = sample->stream_id;
2061 if (type & PERF_SAMPLE_CPU) {
2062 u.val32[0] = sample->cpu;
2065 * Inverse of what is done in perf_evsel__parse_sample
2067 u.val32[0] = bswap_32(u.val32[0]);
2068 u.val64 = bswap_64(u.val64);
2074 if (type & PERF_SAMPLE_PERIOD) {
2075 *array = sample->period;
2079 if (type & PERF_SAMPLE_READ) {
2080 if (read_format & PERF_FORMAT_GROUP)
2081 *array = sample->read.group.nr;
2083 *array = sample->read.one.value;
2086 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2087 *array = sample->read.time_enabled;
2091 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2092 *array = sample->read.time_running;
2096 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2097 if (read_format & PERF_FORMAT_GROUP) {
2098 sz = sample->read.group.nr *
2099 sizeof(struct sample_read_value);
2100 memcpy(array, sample->read.group.values, sz);
2101 array = (void *)array + sz;
2103 *array = sample->read.one.id;
2108 if (type & PERF_SAMPLE_CALLCHAIN) {
2109 sz = (sample->callchain->nr + 1) * sizeof(u64);
2110 memcpy(array, sample->callchain, sz);
2111 array = (void *)array + sz;
2114 if (type & PERF_SAMPLE_RAW) {
2115 u.val32[0] = sample->raw_size;
2116 if (WARN_ONCE(swapped,
2117 "Endianness of raw data not corrected!\n")) {
2119 * Inverse of what is done in perf_evsel__parse_sample
2121 u.val32[0] = bswap_32(u.val32[0]);
2122 u.val32[1] = bswap_32(u.val32[1]);
2123 u.val64 = bswap_64(u.val64);
2126 array = (void *)array + sizeof(u32);
2128 memcpy(array, sample->raw_data, sample->raw_size);
2129 array = (void *)array + sample->raw_size;
2132 if (type & PERF_SAMPLE_BRANCH_STACK) {
2133 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2135 memcpy(array, sample->branch_stack, sz);
2136 array = (void *)array + sz;
2139 if (type & PERF_SAMPLE_REGS_USER) {
2140 if (sample->user_regs.abi) {
2141 *array++ = sample->user_regs.abi;
2142 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2143 memcpy(array, sample->user_regs.regs, sz);
2144 array = (void *)array + sz;
2150 if (type & PERF_SAMPLE_STACK_USER) {
2151 sz = sample->user_stack.size;
2154 memcpy(array, sample->user_stack.data, sz);
2155 array = (void *)array + sz;
2160 if (type & PERF_SAMPLE_WEIGHT) {
2161 *array = sample->weight;
2165 if (type & PERF_SAMPLE_DATA_SRC) {
2166 *array = sample->data_src;
2170 if (type & PERF_SAMPLE_TRANSACTION) {
2171 *array = sample->transaction;
2175 if (type & PERF_SAMPLE_REGS_INTR) {
2176 if (sample->intr_regs.abi) {
2177 *array++ = sample->intr_regs.abi;
2178 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2179 memcpy(array, sample->intr_regs.regs, sz);
2180 array = (void *)array + sz;
2189 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2191 return pevent_find_field(evsel->tp_format, name);
2194 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2197 struct format_field *field = perf_evsel__field(evsel, name);
2203 offset = field->offset;
2205 if (field->flags & FIELD_IS_DYNAMIC) {
2206 offset = *(int *)(sample->raw_data + field->offset);
2210 return sample->raw_data + offset;
2213 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2216 struct format_field *field = perf_evsel__field(evsel, name);
2223 ptr = sample->raw_data + field->offset;
2225 switch (field->size) {
2229 value = *(u16 *)ptr;
2232 value = *(u32 *)ptr;
2235 memcpy(&value, ptr, sizeof(u64));
2241 if (!evsel->needs_swap)
2244 switch (field->size) {
2246 return bswap_16(value);
2248 return bswap_32(value);
2250 return bswap_64(value);
2258 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2264 ret += fprintf(fp, ",");
2266 ret += fprintf(fp, ":");
2270 va_start(args, fmt);
2271 ret += vfprintf(fp, fmt, args);
2276 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2278 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2281 int perf_evsel__fprintf(struct perf_evsel *evsel,
2282 struct perf_attr_details *details, FILE *fp)
2287 if (details->event_group) {
2288 struct perf_evsel *pos;
2290 if (!perf_evsel__is_group_leader(evsel))
2293 if (evsel->nr_members > 1)
2294 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2296 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2297 for_each_group_member(pos, evsel)
2298 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2300 if (evsel->nr_members > 1)
2301 printed += fprintf(fp, "}");
2305 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2307 if (details->verbose) {
2308 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2309 __print_attr__fprintf, &first);
2310 } else if (details->freq) {
2311 const char *term = "sample_freq";
2313 if (!evsel->attr.freq)
2314 term = "sample_period";
2316 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2317 term, (u64)evsel->attr.sample_freq);
2324 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2325 char *msg, size_t msgsize)
2327 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2328 evsel->attr.type == PERF_TYPE_HARDWARE &&
2329 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2331 * If it's cycles then fall back to hrtimer based
2332 * cpu-clock-tick sw counter, which is always available even if
2335 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2338 scnprintf(msg, msgsize, "%s",
2339 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2341 evsel->attr.type = PERF_TYPE_SOFTWARE;
2342 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2344 zfree(&evsel->name);
2351 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2352 int err, char *msg, size_t size)
2354 char sbuf[STRERR_BUFSIZE];
2359 return scnprintf(msg, size,
2360 "You may not have permission to collect %sstats.\n\n"
2361 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2362 "which controls use of the performance events system by\n"
2363 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2364 "The default value is 1:\n\n"
2365 " -1: Allow use of (almost) all events by all users\n"
2366 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2367 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2368 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
2369 target->system_wide ? "system-wide " : "");
2371 return scnprintf(msg, size, "The %s event is not supported.",
2372 perf_evsel__name(evsel));
2374 return scnprintf(msg, size, "%s",
2375 "Too many events are opened.\n"
2376 "Probably the maximum number of open file descriptors has been reached.\n"
2377 "Hint: Try again after reducing the number of events.\n"
2378 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2380 if (target->cpu_list)
2381 return scnprintf(msg, size, "%s",
2382 "No such device - did you specify an out-of-range profile CPU?\n");
2385 if (evsel->attr.precise_ip)
2386 return scnprintf(msg, size, "%s",
2387 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2388 #if defined(__i386__) || defined(__x86_64__)
2389 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2390 return scnprintf(msg, size, "%s",
2391 "No hardware sampling interrupt available.\n"
2392 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2396 if (find_process("oprofiled"))
2397 return scnprintf(msg, size,
2398 "The PMU counters are busy/taken by another profiler.\n"
2399 "We found oprofile daemon running, please stop it and try again.");
2402 if (perf_missing_features.clockid)
2403 return scnprintf(msg, size, "clockid feature not supported.");
2404 if (perf_missing_features.clockid_wrong)
2405 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2411 return scnprintf(msg, size,
2412 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2413 "/bin/dmesg may provide additional information.\n"
2414 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2415 err, strerror_r(err, sbuf, sizeof(sbuf)),
2416 perf_evsel__name(evsel));