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 struct callchain_param param;
630 char *callgraph_buf = NULL;
632 /* callgraph default */
633 param.record_mode = callchain_param.record_mode;
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_fd(evsel);
1097 perf_evsel__free_id(evsel);
1098 perf_evsel__free_config_terms(evsel);
1099 perf_evsel__free_drv_config_terms(evsel);
1100 close_cgroup(evsel->cgrp);
1101 cpu_map__put(evsel->cpus);
1102 cpu_map__put(evsel->own_cpus);
1103 thread_map__put(evsel->threads);
1104 zfree(&evsel->group_name);
1105 zfree(&evsel->name);
1106 perf_evsel__object.fini(evsel);
1109 void perf_evsel__delete(struct perf_evsel *evsel)
1111 perf_evsel__exit(evsel);
1115 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1116 struct perf_counts_values *count)
1118 struct perf_counts_values tmp;
1120 if (!evsel->prev_raw_counts)
1124 tmp = evsel->prev_raw_counts->aggr;
1125 evsel->prev_raw_counts->aggr = *count;
1127 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1128 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1131 count->val = count->val - tmp.val;
1132 count->ena = count->ena - tmp.ena;
1133 count->run = count->run - tmp.run;
1136 void perf_counts_values__scale(struct perf_counts_values *count,
1137 bool scale, s8 *pscaled)
1142 if (count->run == 0) {
1145 } else if (count->run < count->ena) {
1147 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1150 count->ena = count->run = 0;
1156 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1157 struct perf_counts_values *count)
1159 memset(count, 0, sizeof(*count));
1161 if (FD(evsel, cpu, thread) < 0)
1164 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1170 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1171 int cpu, int thread, bool scale)
1173 struct perf_counts_values count;
1174 size_t nv = scale ? 3 : 1;
1176 if (FD(evsel, cpu, thread) < 0)
1179 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1182 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1185 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1186 perf_counts_values__scale(&count, scale, NULL);
1187 *perf_counts(evsel->counts, cpu, thread) = count;
1191 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1193 struct perf_evsel *leader = evsel->leader;
1196 if (perf_evsel__is_group_leader(evsel))
1200 * Leader must be already processed/open,
1201 * if not it's a bug.
1203 BUG_ON(!leader->fd);
1205 fd = FD(leader, cpu, thread);
1216 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1218 bool first_bit = true;
1222 if (value & bits[i].bit) {
1223 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1226 } while (bits[++i].name != NULL);
1229 static void __p_sample_type(char *buf, size_t size, u64 value)
1231 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1232 struct bit_names bits[] = {
1233 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1234 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1235 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1236 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1237 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1241 __p_bits(buf, size, value, bits);
1244 static void __p_read_format(char *buf, size_t size, u64 value)
1246 #define bit_name(n) { PERF_FORMAT_##n, #n }
1247 struct bit_names bits[] = {
1248 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1249 bit_name(ID), bit_name(GROUP),
1253 __p_bits(buf, size, value, bits);
1256 #define BUF_SIZE 1024
1258 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1259 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1260 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1261 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1262 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1264 #define PRINT_ATTRn(_n, _f, _p) \
1268 ret += attr__fprintf(fp, _n, buf, priv);\
1272 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1274 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1275 attr__fprintf_f attr__fprintf, void *priv)
1280 PRINT_ATTRf(type, p_unsigned);
1281 PRINT_ATTRf(size, p_unsigned);
1282 PRINT_ATTRf(config, p_hex);
1283 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1284 PRINT_ATTRf(sample_type, p_sample_type);
1285 PRINT_ATTRf(read_format, p_read_format);
1287 PRINT_ATTRf(disabled, p_unsigned);
1288 PRINT_ATTRf(inherit, p_unsigned);
1289 PRINT_ATTRf(pinned, p_unsigned);
1290 PRINT_ATTRf(exclusive, p_unsigned);
1291 PRINT_ATTRf(exclude_user, p_unsigned);
1292 PRINT_ATTRf(exclude_kernel, p_unsigned);
1293 PRINT_ATTRf(exclude_hv, p_unsigned);
1294 PRINT_ATTRf(exclude_idle, p_unsigned);
1295 PRINT_ATTRf(mmap, p_unsigned);
1296 PRINT_ATTRf(comm, p_unsigned);
1297 PRINT_ATTRf(freq, p_unsigned);
1298 PRINT_ATTRf(inherit_stat, p_unsigned);
1299 PRINT_ATTRf(enable_on_exec, p_unsigned);
1300 PRINT_ATTRf(task, p_unsigned);
1301 PRINT_ATTRf(watermark, p_unsigned);
1302 PRINT_ATTRf(precise_ip, p_unsigned);
1303 PRINT_ATTRf(mmap_data, p_unsigned);
1304 PRINT_ATTRf(sample_id_all, p_unsigned);
1305 PRINT_ATTRf(exclude_host, p_unsigned);
1306 PRINT_ATTRf(exclude_guest, p_unsigned);
1307 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1308 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1309 PRINT_ATTRf(mmap2, p_unsigned);
1310 PRINT_ATTRf(comm_exec, p_unsigned);
1311 PRINT_ATTRf(use_clockid, p_unsigned);
1312 PRINT_ATTRf(context_switch, p_unsigned);
1314 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1315 PRINT_ATTRf(bp_type, p_unsigned);
1316 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1317 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1318 PRINT_ATTRf(branch_sample_type, p_unsigned);
1319 PRINT_ATTRf(sample_regs_user, p_hex);
1320 PRINT_ATTRf(sample_stack_user, p_unsigned);
1321 PRINT_ATTRf(clockid, p_signed);
1322 PRINT_ATTRf(sample_regs_intr, p_hex);
1323 PRINT_ATTRf(aux_watermark, p_unsigned);
1328 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1329 void *priv __attribute__((unused)))
1331 return fprintf(fp, " %-32s %s\n", name, val);
1334 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1335 struct thread_map *threads)
1337 int cpu, thread, nthreads;
1338 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1340 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1342 if (evsel->system_wide)
1345 nthreads = threads->nr;
1347 if (evsel->fd == NULL &&
1348 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1352 flags |= PERF_FLAG_PID_CGROUP;
1353 pid = evsel->cgrp->fd;
1356 fallback_missing_features:
1357 if (perf_missing_features.clockid_wrong)
1358 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1359 if (perf_missing_features.clockid) {
1360 evsel->attr.use_clockid = 0;
1361 evsel->attr.clockid = 0;
1363 if (perf_missing_features.cloexec)
1364 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1365 if (perf_missing_features.mmap2)
1366 evsel->attr.mmap2 = 0;
1367 if (perf_missing_features.exclude_guest)
1368 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1370 if (perf_missing_features.sample_id_all)
1371 evsel->attr.sample_id_all = 0;
1374 fprintf(stderr, "%.60s\n", graph_dotted_line);
1375 fprintf(stderr, "perf_event_attr:\n");
1376 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1377 fprintf(stderr, "%.60s\n", graph_dotted_line);
1380 for (cpu = 0; cpu < cpus->nr; cpu++) {
1382 for (thread = 0; thread < nthreads; thread++) {
1385 if (!evsel->cgrp && !evsel->system_wide)
1386 pid = thread_map__pid(threads, thread);
1388 group_fd = get_group_fd(evsel, cpu, thread);
1390 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1391 pid, cpus->map[cpu], group_fd, flags);
1393 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1397 if (FD(evsel, cpu, thread) < 0) {
1399 pr_debug2("sys_perf_event_open failed, error %d\n",
1404 if (evsel->bpf_fd >= 0) {
1405 int evt_fd = FD(evsel, cpu, thread);
1406 int bpf_fd = evsel->bpf_fd;
1409 PERF_EVENT_IOC_SET_BPF,
1411 if (err && errno != EEXIST) {
1412 pr_err("failed to attach bpf fd %d: %s\n",
1413 bpf_fd, strerror(errno));
1419 set_rlimit = NO_CHANGE;
1422 * If we succeeded but had to kill clockid, fail and
1423 * have perf_evsel__open_strerror() print us a nice
1426 if (perf_missing_features.clockid ||
1427 perf_missing_features.clockid_wrong) {
1438 * perf stat needs between 5 and 22 fds per CPU. When we run out
1439 * of them try to increase the limits.
1441 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1443 int old_errno = errno;
1445 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1446 if (set_rlimit == NO_CHANGE)
1447 l.rlim_cur = l.rlim_max;
1449 l.rlim_cur = l.rlim_max + 1000;
1450 l.rlim_max = l.rlim_cur;
1452 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1461 if (err != -EINVAL || cpu > 0 || thread > 0)
1465 * Must probe features in the order they were added to the
1466 * perf_event_attr interface.
1468 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1469 perf_missing_features.clockid_wrong = true;
1470 goto fallback_missing_features;
1471 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1472 perf_missing_features.clockid = true;
1473 goto fallback_missing_features;
1474 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1475 perf_missing_features.cloexec = true;
1476 goto fallback_missing_features;
1477 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1478 perf_missing_features.mmap2 = true;
1479 goto fallback_missing_features;
1480 } else if (!perf_missing_features.exclude_guest &&
1481 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1482 perf_missing_features.exclude_guest = true;
1483 goto fallback_missing_features;
1484 } else if (!perf_missing_features.sample_id_all) {
1485 perf_missing_features.sample_id_all = true;
1486 goto retry_sample_id;
1491 while (--thread >= 0) {
1492 close(FD(evsel, cpu, thread));
1493 FD(evsel, cpu, thread) = -1;
1496 } while (--cpu >= 0);
1500 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1502 if (evsel->fd == NULL)
1505 perf_evsel__close_fd(evsel, ncpus, nthreads);
1506 perf_evsel__free_fd(evsel);
1518 struct thread_map map;
1520 } empty_thread_map = {
1525 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1526 struct thread_map *threads)
1529 /* Work around old compiler warnings about strict aliasing */
1530 cpus = &empty_cpu_map.map;
1533 if (threads == NULL)
1534 threads = &empty_thread_map.map;
1536 return __perf_evsel__open(evsel, cpus, threads);
1539 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1540 struct cpu_map *cpus)
1542 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1545 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1546 struct thread_map *threads)
1548 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1551 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1552 const union perf_event *event,
1553 struct perf_sample *sample)
1555 u64 type = evsel->attr.sample_type;
1556 const u64 *array = event->sample.array;
1557 bool swapped = evsel->needs_swap;
1560 array += ((event->header.size -
1561 sizeof(event->header)) / sizeof(u64)) - 1;
1563 if (type & PERF_SAMPLE_IDENTIFIER) {
1564 sample->id = *array;
1568 if (type & PERF_SAMPLE_CPU) {
1571 /* undo swap of u64, then swap on individual u32s */
1572 u.val64 = bswap_64(u.val64);
1573 u.val32[0] = bswap_32(u.val32[0]);
1576 sample->cpu = u.val32[0];
1580 if (type & PERF_SAMPLE_STREAM_ID) {
1581 sample->stream_id = *array;
1585 if (type & PERF_SAMPLE_ID) {
1586 sample->id = *array;
1590 if (type & PERF_SAMPLE_TIME) {
1591 sample->time = *array;
1595 if (type & PERF_SAMPLE_TID) {
1598 /* undo swap of u64, then swap on individual u32s */
1599 u.val64 = bswap_64(u.val64);
1600 u.val32[0] = bswap_32(u.val32[0]);
1601 u.val32[1] = bswap_32(u.val32[1]);
1604 sample->pid = u.val32[0];
1605 sample->tid = u.val32[1];
1612 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1615 return size > max_size || offset + size > endp;
1618 #define OVERFLOW_CHECK(offset, size, max_size) \
1620 if (overflow(endp, (max_size), (offset), (size))) \
1624 #define OVERFLOW_CHECK_u64(offset) \
1625 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1627 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1628 struct perf_sample *data)
1630 u64 type = evsel->attr.sample_type;
1631 bool swapped = evsel->needs_swap;
1633 u16 max_size = event->header.size;
1634 const void *endp = (void *)event + max_size;
1638 * used for cross-endian analysis. See git commit 65014ab3
1639 * for why this goofiness is needed.
1643 memset(data, 0, sizeof(*data));
1644 data->cpu = data->pid = data->tid = -1;
1645 data->stream_id = data->id = data->time = -1ULL;
1646 data->period = evsel->attr.sample_period;
1649 if (event->header.type != PERF_RECORD_SAMPLE) {
1650 if (!evsel->attr.sample_id_all)
1652 return perf_evsel__parse_id_sample(evsel, event, data);
1655 array = event->sample.array;
1658 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1659 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1660 * check the format does not go past the end of the event.
1662 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1666 if (type & PERF_SAMPLE_IDENTIFIER) {
1671 if (type & PERF_SAMPLE_IP) {
1676 if (type & PERF_SAMPLE_TID) {
1679 /* undo swap of u64, then swap on individual u32s */
1680 u.val64 = bswap_64(u.val64);
1681 u.val32[0] = bswap_32(u.val32[0]);
1682 u.val32[1] = bswap_32(u.val32[1]);
1685 data->pid = u.val32[0];
1686 data->tid = u.val32[1];
1690 if (type & PERF_SAMPLE_TIME) {
1691 data->time = *array;
1696 if (type & PERF_SAMPLE_ADDR) {
1697 data->addr = *array;
1701 if (type & PERF_SAMPLE_ID) {
1706 if (type & PERF_SAMPLE_STREAM_ID) {
1707 data->stream_id = *array;
1711 if (type & PERF_SAMPLE_CPU) {
1715 /* undo swap of u64, then swap on individual u32s */
1716 u.val64 = bswap_64(u.val64);
1717 u.val32[0] = bswap_32(u.val32[0]);
1720 data->cpu = u.val32[0];
1724 if (type & PERF_SAMPLE_PERIOD) {
1725 data->period = *array;
1729 if (type & PERF_SAMPLE_READ) {
1730 u64 read_format = evsel->attr.read_format;
1732 OVERFLOW_CHECK_u64(array);
1733 if (read_format & PERF_FORMAT_GROUP)
1734 data->read.group.nr = *array;
1736 data->read.one.value = *array;
1740 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1741 OVERFLOW_CHECK_u64(array);
1742 data->read.time_enabled = *array;
1746 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1747 OVERFLOW_CHECK_u64(array);
1748 data->read.time_running = *array;
1752 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1753 if (read_format & PERF_FORMAT_GROUP) {
1754 const u64 max_group_nr = UINT64_MAX /
1755 sizeof(struct sample_read_value);
1757 if (data->read.group.nr > max_group_nr)
1759 sz = data->read.group.nr *
1760 sizeof(struct sample_read_value);
1761 OVERFLOW_CHECK(array, sz, max_size);
1762 data->read.group.values =
1763 (struct sample_read_value *)array;
1764 array = (void *)array + sz;
1766 OVERFLOW_CHECK_u64(array);
1767 data->read.one.id = *array;
1772 if (type & PERF_SAMPLE_CALLCHAIN) {
1773 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1775 OVERFLOW_CHECK_u64(array);
1776 data->callchain = (struct ip_callchain *)array++;
1777 if (data->callchain->nr > max_callchain_nr)
1779 sz = data->callchain->nr * sizeof(u64);
1780 OVERFLOW_CHECK(array, sz, max_size);
1781 array = (void *)array + sz;
1784 if (type & PERF_SAMPLE_RAW) {
1785 OVERFLOW_CHECK_u64(array);
1787 if (WARN_ONCE(swapped,
1788 "Endianness of raw data not corrected!\n")) {
1789 /* undo swap of u64, then swap on individual u32s */
1790 u.val64 = bswap_64(u.val64);
1791 u.val32[0] = bswap_32(u.val32[0]);
1792 u.val32[1] = bswap_32(u.val32[1]);
1794 data->raw_size = u.val32[0];
1795 array = (void *)array + sizeof(u32);
1797 OVERFLOW_CHECK(array, data->raw_size, max_size);
1798 data->raw_data = (void *)array;
1799 array = (void *)array + data->raw_size;
1802 if (type & PERF_SAMPLE_BRANCH_STACK) {
1803 const u64 max_branch_nr = UINT64_MAX /
1804 sizeof(struct branch_entry);
1806 OVERFLOW_CHECK_u64(array);
1807 data->branch_stack = (struct branch_stack *)array++;
1809 if (data->branch_stack->nr > max_branch_nr)
1811 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1812 OVERFLOW_CHECK(array, sz, max_size);
1813 array = (void *)array + sz;
1816 if (type & PERF_SAMPLE_REGS_USER) {
1817 OVERFLOW_CHECK_u64(array);
1818 data->user_regs.abi = *array;
1821 if (data->user_regs.abi) {
1822 u64 mask = evsel->attr.sample_regs_user;
1824 sz = hweight_long(mask) * sizeof(u64);
1825 OVERFLOW_CHECK(array, sz, max_size);
1826 data->user_regs.mask = mask;
1827 data->user_regs.regs = (u64 *)array;
1828 array = (void *)array + sz;
1832 if (type & PERF_SAMPLE_STACK_USER) {
1833 OVERFLOW_CHECK_u64(array);
1836 data->user_stack.offset = ((char *)(array - 1)
1840 data->user_stack.size = 0;
1842 OVERFLOW_CHECK(array, sz, max_size);
1843 data->user_stack.data = (char *)array;
1844 array = (void *)array + sz;
1845 OVERFLOW_CHECK_u64(array);
1846 data->user_stack.size = *array++;
1847 if (WARN_ONCE(data->user_stack.size > sz,
1848 "user stack dump failure\n"))
1854 if (type & PERF_SAMPLE_WEIGHT) {
1855 OVERFLOW_CHECK_u64(array);
1856 data->weight = *array;
1860 data->data_src = PERF_MEM_DATA_SRC_NONE;
1861 if (type & PERF_SAMPLE_DATA_SRC) {
1862 OVERFLOW_CHECK_u64(array);
1863 data->data_src = *array;
1867 data->transaction = 0;
1868 if (type & PERF_SAMPLE_TRANSACTION) {
1869 OVERFLOW_CHECK_u64(array);
1870 data->transaction = *array;
1874 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1875 if (type & PERF_SAMPLE_REGS_INTR) {
1876 OVERFLOW_CHECK_u64(array);
1877 data->intr_regs.abi = *array;
1880 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1881 u64 mask = evsel->attr.sample_regs_intr;
1883 sz = hweight_long(mask) * sizeof(u64);
1884 OVERFLOW_CHECK(array, sz, max_size);
1885 data->intr_regs.mask = mask;
1886 data->intr_regs.regs = (u64 *)array;
1887 array = (void *)array + sz;
1894 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1897 size_t sz, result = sizeof(struct sample_event);
1899 if (type & PERF_SAMPLE_IDENTIFIER)
1900 result += sizeof(u64);
1902 if (type & PERF_SAMPLE_IP)
1903 result += sizeof(u64);
1905 if (type & PERF_SAMPLE_TID)
1906 result += sizeof(u64);
1908 if (type & PERF_SAMPLE_TIME)
1909 result += sizeof(u64);
1911 if (type & PERF_SAMPLE_ADDR)
1912 result += sizeof(u64);
1914 if (type & PERF_SAMPLE_ID)
1915 result += sizeof(u64);
1917 if (type & PERF_SAMPLE_STREAM_ID)
1918 result += sizeof(u64);
1920 if (type & PERF_SAMPLE_CPU)
1921 result += sizeof(u64);
1923 if (type & PERF_SAMPLE_PERIOD)
1924 result += sizeof(u64);
1926 if (type & PERF_SAMPLE_READ) {
1927 result += sizeof(u64);
1928 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1929 result += sizeof(u64);
1930 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1931 result += sizeof(u64);
1932 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1933 if (read_format & PERF_FORMAT_GROUP) {
1934 sz = sample->read.group.nr *
1935 sizeof(struct sample_read_value);
1938 result += sizeof(u64);
1942 if (type & PERF_SAMPLE_CALLCHAIN) {
1943 sz = (sample->callchain->nr + 1) * sizeof(u64);
1947 if (type & PERF_SAMPLE_RAW) {
1948 result += sizeof(u32);
1949 result += sample->raw_size;
1952 if (type & PERF_SAMPLE_BRANCH_STACK) {
1953 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1958 if (type & PERF_SAMPLE_REGS_USER) {
1959 if (sample->user_regs.abi) {
1960 result += sizeof(u64);
1961 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1964 result += sizeof(u64);
1968 if (type & PERF_SAMPLE_STACK_USER) {
1969 sz = sample->user_stack.size;
1970 result += sizeof(u64);
1973 result += sizeof(u64);
1977 if (type & PERF_SAMPLE_WEIGHT)
1978 result += sizeof(u64);
1980 if (type & PERF_SAMPLE_DATA_SRC)
1981 result += sizeof(u64);
1983 if (type & PERF_SAMPLE_TRANSACTION)
1984 result += sizeof(u64);
1986 if (type & PERF_SAMPLE_REGS_INTR) {
1987 if (sample->intr_regs.abi) {
1988 result += sizeof(u64);
1989 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1992 result += sizeof(u64);
1999 int perf_event__synthesize_sample(union perf_event *event, u64 type,
2001 const struct perf_sample *sample,
2007 * used for cross-endian analysis. See git commit 65014ab3
2008 * for why this goofiness is needed.
2012 array = event->sample.array;
2014 if (type & PERF_SAMPLE_IDENTIFIER) {
2015 *array = sample->id;
2019 if (type & PERF_SAMPLE_IP) {
2020 *array = sample->ip;
2024 if (type & PERF_SAMPLE_TID) {
2025 u.val32[0] = sample->pid;
2026 u.val32[1] = sample->tid;
2029 * Inverse of what is done in perf_evsel__parse_sample
2031 u.val32[0] = bswap_32(u.val32[0]);
2032 u.val32[1] = bswap_32(u.val32[1]);
2033 u.val64 = bswap_64(u.val64);
2040 if (type & PERF_SAMPLE_TIME) {
2041 *array = sample->time;
2045 if (type & PERF_SAMPLE_ADDR) {
2046 *array = sample->addr;
2050 if (type & PERF_SAMPLE_ID) {
2051 *array = sample->id;
2055 if (type & PERF_SAMPLE_STREAM_ID) {
2056 *array = sample->stream_id;
2060 if (type & PERF_SAMPLE_CPU) {
2061 u.val32[0] = sample->cpu;
2064 * Inverse of what is done in perf_evsel__parse_sample
2066 u.val32[0] = bswap_32(u.val32[0]);
2067 u.val64 = bswap_64(u.val64);
2073 if (type & PERF_SAMPLE_PERIOD) {
2074 *array = sample->period;
2078 if (type & PERF_SAMPLE_READ) {
2079 if (read_format & PERF_FORMAT_GROUP)
2080 *array = sample->read.group.nr;
2082 *array = sample->read.one.value;
2085 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2086 *array = sample->read.time_enabled;
2090 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2091 *array = sample->read.time_running;
2095 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2096 if (read_format & PERF_FORMAT_GROUP) {
2097 sz = sample->read.group.nr *
2098 sizeof(struct sample_read_value);
2099 memcpy(array, sample->read.group.values, sz);
2100 array = (void *)array + sz;
2102 *array = sample->read.one.id;
2107 if (type & PERF_SAMPLE_CALLCHAIN) {
2108 sz = (sample->callchain->nr + 1) * sizeof(u64);
2109 memcpy(array, sample->callchain, sz);
2110 array = (void *)array + sz;
2113 if (type & PERF_SAMPLE_RAW) {
2114 u.val32[0] = sample->raw_size;
2115 if (WARN_ONCE(swapped,
2116 "Endianness of raw data not corrected!\n")) {
2118 * Inverse of what is done in perf_evsel__parse_sample
2120 u.val32[0] = bswap_32(u.val32[0]);
2121 u.val32[1] = bswap_32(u.val32[1]);
2122 u.val64 = bswap_64(u.val64);
2125 array = (void *)array + sizeof(u32);
2127 memcpy(array, sample->raw_data, sample->raw_size);
2128 array = (void *)array + sample->raw_size;
2131 if (type & PERF_SAMPLE_BRANCH_STACK) {
2132 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2134 memcpy(array, sample->branch_stack, sz);
2135 array = (void *)array + sz;
2138 if (type & PERF_SAMPLE_REGS_USER) {
2139 if (sample->user_regs.abi) {
2140 *array++ = sample->user_regs.abi;
2141 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2142 memcpy(array, sample->user_regs.regs, sz);
2143 array = (void *)array + sz;
2149 if (type & PERF_SAMPLE_STACK_USER) {
2150 sz = sample->user_stack.size;
2153 memcpy(array, sample->user_stack.data, sz);
2154 array = (void *)array + sz;
2159 if (type & PERF_SAMPLE_WEIGHT) {
2160 *array = sample->weight;
2164 if (type & PERF_SAMPLE_DATA_SRC) {
2165 *array = sample->data_src;
2169 if (type & PERF_SAMPLE_TRANSACTION) {
2170 *array = sample->transaction;
2174 if (type & PERF_SAMPLE_REGS_INTR) {
2175 if (sample->intr_regs.abi) {
2176 *array++ = sample->intr_regs.abi;
2177 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2178 memcpy(array, sample->intr_regs.regs, sz);
2179 array = (void *)array + sz;
2188 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2190 return pevent_find_field(evsel->tp_format, name);
2193 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2196 struct format_field *field = perf_evsel__field(evsel, name);
2202 offset = field->offset;
2204 if (field->flags & FIELD_IS_DYNAMIC) {
2205 offset = *(int *)(sample->raw_data + field->offset);
2209 return sample->raw_data + offset;
2212 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2215 struct format_field *field = perf_evsel__field(evsel, name);
2222 ptr = sample->raw_data + field->offset;
2224 switch (field->size) {
2228 value = *(u16 *)ptr;
2231 value = *(u32 *)ptr;
2234 memcpy(&value, ptr, sizeof(u64));
2240 if (!evsel->needs_swap)
2243 switch (field->size) {
2245 return bswap_16(value);
2247 return bswap_32(value);
2249 return bswap_64(value);
2257 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2263 ret += fprintf(fp, ",");
2265 ret += fprintf(fp, ":");
2269 va_start(args, fmt);
2270 ret += vfprintf(fp, fmt, args);
2275 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2277 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2280 int perf_evsel__fprintf(struct perf_evsel *evsel,
2281 struct perf_attr_details *details, FILE *fp)
2286 if (details->event_group) {
2287 struct perf_evsel *pos;
2289 if (!perf_evsel__is_group_leader(evsel))
2292 if (evsel->nr_members > 1)
2293 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2295 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2296 for_each_group_member(pos, evsel)
2297 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2299 if (evsel->nr_members > 1)
2300 printed += fprintf(fp, "}");
2304 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2306 if (details->verbose) {
2307 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2308 __print_attr__fprintf, &first);
2309 } else if (details->freq) {
2310 const char *term = "sample_freq";
2312 if (!evsel->attr.freq)
2313 term = "sample_period";
2315 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2316 term, (u64)evsel->attr.sample_freq);
2323 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2324 char *msg, size_t msgsize)
2326 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2327 evsel->attr.type == PERF_TYPE_HARDWARE &&
2328 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2330 * If it's cycles then fall back to hrtimer based
2331 * cpu-clock-tick sw counter, which is always available even if
2334 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2337 scnprintf(msg, msgsize, "%s",
2338 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2340 evsel->attr.type = PERF_TYPE_SOFTWARE;
2341 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2343 zfree(&evsel->name);
2350 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2351 int err, char *msg, size_t size)
2353 char sbuf[STRERR_BUFSIZE];
2358 return scnprintf(msg, size,
2359 "You may not have permission to collect %sstats.\n"
2360 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2361 " -1 - Not paranoid at all\n"
2362 " 0 - Disallow raw tracepoint access for unpriv\n"
2363 " 1 - Disallow cpu events for unpriv\n"
2364 " 2 - Disallow kernel profiling for unpriv",
2365 target->system_wide ? "system-wide " : "");
2367 return scnprintf(msg, size, "The %s event is not supported.",
2368 perf_evsel__name(evsel));
2370 return scnprintf(msg, size, "%s",
2371 "Too many events are opened.\n"
2372 "Probably the maximum number of open file descriptors has been reached.\n"
2373 "Hint: Try again after reducing the number of events.\n"
2374 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2376 if (target->cpu_list)
2377 return scnprintf(msg, size, "%s",
2378 "No such device - did you specify an out-of-range profile CPU?\n");
2381 if (evsel->attr.precise_ip)
2382 return scnprintf(msg, size, "%s",
2383 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2384 #if defined(__i386__) || defined(__x86_64__)
2385 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2386 return scnprintf(msg, size, "%s",
2387 "No hardware sampling interrupt available.\n"
2388 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2392 if (find_process("oprofiled"))
2393 return scnprintf(msg, size,
2394 "The PMU counters are busy/taken by another profiler.\n"
2395 "We found oprofile daemon running, please stop it and try again.");
2398 if (perf_missing_features.clockid)
2399 return scnprintf(msg, size, "clockid feature not supported.");
2400 if (perf_missing_features.clockid_wrong)
2401 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2407 return scnprintf(msg, size,
2408 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2409 "/bin/dmesg may provide additional information.\n"
2410 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2411 err, strerror_r(err, sbuf, sizeof(sbuf)),
2412 perf_evsel__name(evsel));