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)
501 switch (evsel->attr.type) {
503 perf_evsel__raw_name(evsel, bf, sizeof(bf));
506 case PERF_TYPE_HARDWARE:
507 perf_evsel__hw_name(evsel, bf, sizeof(bf));
510 case PERF_TYPE_HW_CACHE:
511 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
514 case PERF_TYPE_SOFTWARE:
515 perf_evsel__sw_name(evsel, bf, sizeof(bf));
518 case PERF_TYPE_TRACEPOINT:
519 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
522 case PERF_TYPE_BREAKPOINT:
523 perf_evsel__bp_name(evsel, bf, sizeof(bf));
527 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
532 evsel->name = strdup(bf);
540 const char *perf_evsel__group_name(struct perf_evsel *evsel)
542 return evsel->group_name ?: "anon group";
545 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
548 struct perf_evsel *pos;
549 const char *group_name = perf_evsel__group_name(evsel);
551 ret = scnprintf(buf, size, "%s", group_name);
553 ret += scnprintf(buf + ret, size - ret, " { %s",
554 perf_evsel__name(evsel));
556 for_each_group_member(pos, evsel)
557 ret += scnprintf(buf + ret, size - ret, ", %s",
558 perf_evsel__name(pos));
560 ret += scnprintf(buf + ret, size - ret, " }");
566 perf_evsel__config_callgraph(struct perf_evsel *evsel,
567 struct record_opts *opts,
568 struct callchain_param *param)
570 bool function = perf_evsel__is_function_event(evsel);
571 struct perf_event_attr *attr = &evsel->attr;
573 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
575 if (param->record_mode == CALLCHAIN_LBR) {
576 if (!opts->branch_stack) {
577 if (attr->exclude_user) {
578 pr_warning("LBR callstack option is only available "
579 "to get user callchain information. "
580 "Falling back to framepointers.\n");
582 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
583 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
584 PERF_SAMPLE_BRANCH_CALL_STACK;
587 pr_warning("Cannot use LBR callstack with branch stack. "
588 "Falling back to framepointers.\n");
591 if (param->record_mode == CALLCHAIN_DWARF) {
593 perf_evsel__set_sample_bit(evsel, REGS_USER);
594 perf_evsel__set_sample_bit(evsel, STACK_USER);
595 attr->sample_regs_user = PERF_REGS_MASK;
596 attr->sample_stack_user = param->dump_size;
597 attr->exclude_callchain_user = 1;
599 pr_info("Cannot use DWARF unwind for function trace event,"
600 " falling back to framepointers.\n");
605 pr_info("Disabling user space callchains for function trace event.\n");
606 attr->exclude_callchain_user = 1;
611 perf_evsel__reset_callgraph(struct perf_evsel *evsel,
612 struct callchain_param *param)
614 struct perf_event_attr *attr = &evsel->attr;
616 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
617 if (param->record_mode == CALLCHAIN_LBR) {
618 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
619 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
620 PERF_SAMPLE_BRANCH_CALL_STACK);
622 if (param->record_mode == CALLCHAIN_DWARF) {
623 perf_evsel__reset_sample_bit(evsel, REGS_USER);
624 perf_evsel__reset_sample_bit(evsel, STACK_USER);
628 static void apply_config_terms(struct perf_evsel *evsel,
629 struct record_opts *opts)
631 struct perf_evsel_config_term *term;
632 struct list_head *config_terms = &evsel->config_terms;
633 struct perf_event_attr *attr = &evsel->attr;
634 /* callgraph default */
635 struct callchain_param param = {
636 .record_mode = callchain_param.record_mode,
639 char *callgraph_buf = NULL;
641 list_for_each_entry(term, config_terms, list) {
642 switch (term->type) {
643 case PERF_EVSEL__CONFIG_TERM_PERIOD:
644 attr->sample_period = term->val.period;
647 case PERF_EVSEL__CONFIG_TERM_FREQ:
648 attr->sample_freq = term->val.freq;
651 case PERF_EVSEL__CONFIG_TERM_TIME:
653 perf_evsel__set_sample_bit(evsel, TIME);
655 perf_evsel__reset_sample_bit(evsel, TIME);
657 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
658 callgraph_buf = term->val.callgraph;
660 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
661 dump_size = term->val.stack_user;
663 case PERF_EVSEL__CONFIG_TERM_INHERIT:
665 * attr->inherit should has already been set by
666 * perf_evsel__config. If user explicitly set
667 * inherit using config terms, override global
668 * opt->no_inherit setting.
670 attr->inherit = term->val.inherit ? 1 : 0;
677 /* User explicitly set per-event callgraph, clear the old setting and reset. */
678 if ((callgraph_buf != NULL) || (dump_size > 0)) {
680 /* parse callgraph parameters */
681 if (callgraph_buf != NULL) {
682 if (!strcmp(callgraph_buf, "no")) {
683 param.enabled = false;
684 param.record_mode = CALLCHAIN_NONE;
686 param.enabled = true;
687 if (parse_callchain_record(callgraph_buf, ¶m)) {
688 pr_err("per-event callgraph setting for %s failed. "
689 "Apply callgraph global setting for it\n",
696 dump_size = round_up(dump_size, sizeof(u64));
697 param.dump_size = dump_size;
700 /* If global callgraph set, clear it */
701 if (callchain_param.enabled)
702 perf_evsel__reset_callgraph(evsel, &callchain_param);
704 /* set perf-event callgraph */
706 perf_evsel__config_callgraph(evsel, opts, ¶m);
711 * The enable_on_exec/disabled value strategy:
713 * 1) For any type of traced program:
714 * - all independent events and group leaders are disabled
715 * - all group members are enabled
717 * Group members are ruled by group leaders. They need to
718 * be enabled, because the group scheduling relies on that.
720 * 2) For traced programs executed by perf:
721 * - all independent events and group leaders have
723 * - we don't specifically enable or disable any event during
726 * Independent events and group leaders are initially disabled
727 * and get enabled by exec. Group members are ruled by group
728 * leaders as stated in 1).
730 * 3) For traced programs attached by perf (pid/tid):
731 * - we specifically enable or disable all events during
734 * When attaching events to already running traced we
735 * enable/disable events specifically, as there's no
736 * initial traced exec call.
738 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
740 struct perf_evsel *leader = evsel->leader;
741 struct perf_event_attr *attr = &evsel->attr;
742 int track = evsel->tracking;
743 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
745 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
746 attr->inherit = !opts->no_inherit;
748 perf_evsel__set_sample_bit(evsel, IP);
749 perf_evsel__set_sample_bit(evsel, TID);
751 if (evsel->sample_read) {
752 perf_evsel__set_sample_bit(evsel, READ);
755 * We need ID even in case of single event, because
756 * PERF_SAMPLE_READ process ID specific data.
758 perf_evsel__set_sample_id(evsel, false);
761 * Apply group format only if we belong to group
762 * with more than one members.
764 if (leader->nr_members > 1) {
765 attr->read_format |= PERF_FORMAT_GROUP;
771 * We default some events to have a default interval. But keep
772 * it a weak assumption overridable by the user.
774 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
775 opts->user_interval != ULLONG_MAX)) {
777 perf_evsel__set_sample_bit(evsel, PERIOD);
779 attr->sample_freq = opts->freq;
781 attr->sample_period = opts->default_interval;
786 * Disable sampling for all group members other
787 * than leader in case leader 'leads' the sampling.
789 if ((leader != evsel) && leader->sample_read) {
790 attr->sample_freq = 0;
791 attr->sample_period = 0;
794 if (opts->no_samples)
795 attr->sample_freq = 0;
797 if (opts->inherit_stat)
798 attr->inherit_stat = 1;
800 if (opts->sample_address) {
801 perf_evsel__set_sample_bit(evsel, ADDR);
802 attr->mmap_data = track;
806 * We don't allow user space callchains for function trace
807 * event, due to issues with page faults while tracing page
808 * fault handler and its overall trickiness nature.
810 if (perf_evsel__is_function_event(evsel))
811 evsel->attr.exclude_callchain_user = 1;
813 if (callchain_param.enabled && !evsel->no_aux_samples)
814 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
816 if (opts->sample_intr_regs) {
817 attr->sample_regs_intr = opts->sample_intr_regs;
818 perf_evsel__set_sample_bit(evsel, REGS_INTR);
821 if (target__has_cpu(&opts->target))
822 perf_evsel__set_sample_bit(evsel, CPU);
825 perf_evsel__set_sample_bit(evsel, PERIOD);
828 * When the user explicitely disabled time don't force it here.
830 if (opts->sample_time &&
831 (!perf_missing_features.sample_id_all &&
832 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
833 opts->sample_time_set)))
834 perf_evsel__set_sample_bit(evsel, TIME);
836 if (opts->raw_samples && !evsel->no_aux_samples) {
837 perf_evsel__set_sample_bit(evsel, TIME);
838 perf_evsel__set_sample_bit(evsel, RAW);
839 perf_evsel__set_sample_bit(evsel, CPU);
842 if (opts->sample_address)
843 perf_evsel__set_sample_bit(evsel, DATA_SRC);
845 if (opts->no_buffering) {
847 attr->wakeup_events = 1;
849 if (opts->branch_stack && !evsel->no_aux_samples) {
850 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
851 attr->branch_sample_type = opts->branch_stack;
854 if (opts->sample_weight)
855 perf_evsel__set_sample_bit(evsel, WEIGHT);
859 attr->mmap2 = track && !perf_missing_features.mmap2;
862 if (opts->record_switch_events)
863 attr->context_switch = track;
865 if (opts->sample_transaction)
866 perf_evsel__set_sample_bit(evsel, TRANSACTION);
868 if (opts->running_time) {
869 evsel->attr.read_format |=
870 PERF_FORMAT_TOTAL_TIME_ENABLED |
871 PERF_FORMAT_TOTAL_TIME_RUNNING;
875 * XXX see the function comment above
877 * Disabling only independent events or group leaders,
878 * keeping group members enabled.
880 if (perf_evsel__is_group_leader(evsel))
884 * Setting enable_on_exec for independent events and
885 * group leaders for traced executed by perf.
887 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
888 !opts->initial_delay)
889 attr->enable_on_exec = 1;
891 if (evsel->immediate) {
893 attr->enable_on_exec = 0;
896 clockid = opts->clockid;
897 if (opts->use_clockid) {
898 attr->use_clockid = 1;
899 attr->clockid = opts->clockid;
902 if (evsel->precise_max)
903 perf_event_attr__set_max_precise_ip(attr);
906 * Apply event specific term settings,
907 * it overloads any global configuration.
909 apply_config_terms(evsel, opts);
912 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
916 if (evsel->system_wide)
919 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
922 for (cpu = 0; cpu < ncpus; cpu++) {
923 for (thread = 0; thread < nthreads; thread++) {
924 FD(evsel, cpu, thread) = -1;
929 return evsel->fd != NULL ? 0 : -ENOMEM;
932 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
937 if (evsel->system_wide)
940 for (cpu = 0; cpu < ncpus; cpu++) {
941 for (thread = 0; thread < nthreads; thread++) {
942 int fd = FD(evsel, cpu, thread),
943 err = ioctl(fd, ioc, arg);
953 int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
956 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
957 PERF_EVENT_IOC_SET_FILTER,
961 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
963 char *new_filter = strdup(filter);
965 if (new_filter != NULL) {
967 evsel->filter = new_filter;
974 int perf_evsel__append_filter(struct perf_evsel *evsel,
975 const char *op, const char *filter)
979 if (evsel->filter == NULL)
980 return perf_evsel__set_filter(evsel, filter);
982 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
984 evsel->filter = new_filter;
991 int perf_evsel__apply_drv_configs(struct perf_evsel *evsel,
992 int ncpus, int nthreads,
993 struct perf_evsel_config_term **err_term)
996 struct perf_evsel_config_term *term;
998 list_for_each_entry(term, &evsel->drv_config_terms, list) {
999 err = perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1000 PERF_EVENT_IOC_SET_DRV_CONFIGS,
1001 (void *)term->val.drv_cfg);
1012 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
1014 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1015 PERF_EVENT_IOC_ENABLE,
1019 int perf_evsel__disable(struct perf_evsel *evsel)
1021 int nthreads = thread_map__nr(evsel->threads);
1022 int ncpus = cpu_map__nr(evsel->cpus);
1024 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1025 PERF_EVENT_IOC_DISABLE,
1029 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1031 if (ncpus == 0 || nthreads == 0)
1034 if (evsel->system_wide)
1037 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1038 if (evsel->sample_id == NULL)
1041 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1042 if (evsel->id == NULL) {
1043 xyarray__delete(evsel->sample_id);
1044 evsel->sample_id = NULL;
1051 static void perf_evsel__free_fd(struct perf_evsel *evsel)
1053 xyarray__delete(evsel->fd);
1057 static void perf_evsel__free_id(struct perf_evsel *evsel)
1059 xyarray__delete(evsel->sample_id);
1060 evsel->sample_id = NULL;
1064 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1066 struct perf_evsel_config_term *term, *h;
1068 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1069 list_del(&term->list);
1074 static void perf_evsel__free_drv_config_terms(struct perf_evsel *evsel)
1076 struct perf_evsel_config_term *term, *h;
1078 list_for_each_entry_safe(term, h, &evsel->drv_config_terms, list) {
1079 list_del(&term->list);
1084 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1088 if (evsel->system_wide)
1091 for (cpu = 0; cpu < ncpus; cpu++)
1092 for (thread = 0; thread < nthreads; ++thread) {
1093 close(FD(evsel, cpu, thread));
1094 FD(evsel, cpu, thread) = -1;
1098 void perf_evsel__exit(struct perf_evsel *evsel)
1100 assert(list_empty(&evsel->node));
1101 assert(evsel->evlist == NULL);
1102 perf_evsel__free_counts(evsel);
1103 perf_evsel__free_fd(evsel);
1104 perf_evsel__free_id(evsel);
1105 perf_evsel__free_config_terms(evsel);
1106 perf_evsel__free_drv_config_terms(evsel);
1107 close_cgroup(evsel->cgrp);
1108 cpu_map__put(evsel->cpus);
1109 cpu_map__put(evsel->own_cpus);
1110 thread_map__put(evsel->threads);
1111 zfree(&evsel->group_name);
1112 zfree(&evsel->name);
1113 perf_evsel__object.fini(evsel);
1116 void perf_evsel__delete(struct perf_evsel *evsel)
1118 perf_evsel__exit(evsel);
1122 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1123 struct perf_counts_values *count)
1125 struct perf_counts_values tmp;
1127 if (!evsel->prev_raw_counts)
1131 tmp = evsel->prev_raw_counts->aggr;
1132 evsel->prev_raw_counts->aggr = *count;
1134 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1135 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1138 count->val = count->val - tmp.val;
1139 count->ena = count->ena - tmp.ena;
1140 count->run = count->run - tmp.run;
1143 void perf_counts_values__scale(struct perf_counts_values *count,
1144 bool scale, s8 *pscaled)
1149 if (count->run == 0) {
1152 } else if (count->run < count->ena) {
1154 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1157 count->ena = count->run = 0;
1163 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1164 struct perf_counts_values *count)
1166 memset(count, 0, sizeof(*count));
1168 if (FD(evsel, cpu, thread) < 0)
1171 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1177 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1178 int cpu, int thread, bool scale)
1180 struct perf_counts_values count;
1181 size_t nv = scale ? 3 : 1;
1183 if (FD(evsel, cpu, thread) < 0)
1186 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1189 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1192 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1193 perf_counts_values__scale(&count, scale, NULL);
1194 *perf_counts(evsel->counts, cpu, thread) = count;
1198 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1200 struct perf_evsel *leader = evsel->leader;
1203 if (perf_evsel__is_group_leader(evsel))
1207 * Leader must be already processed/open,
1208 * if not it's a bug.
1210 BUG_ON(!leader->fd);
1212 fd = FD(leader, cpu, thread);
1223 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1225 bool first_bit = true;
1229 if (value & bits[i].bit) {
1230 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1233 } while (bits[++i].name != NULL);
1236 static void __p_sample_type(char *buf, size_t size, u64 value)
1238 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1239 struct bit_names bits[] = {
1240 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1241 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1242 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1243 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1244 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1248 __p_bits(buf, size, value, bits);
1251 static void __p_read_format(char *buf, size_t size, u64 value)
1253 #define bit_name(n) { PERF_FORMAT_##n, #n }
1254 struct bit_names bits[] = {
1255 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1256 bit_name(ID), bit_name(GROUP),
1260 __p_bits(buf, size, value, bits);
1263 #define BUF_SIZE 1024
1265 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1266 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1267 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1268 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1269 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1271 #define PRINT_ATTRn(_n, _f, _p) \
1275 ret += attr__fprintf(fp, _n, buf, priv);\
1279 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1281 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1282 attr__fprintf_f attr__fprintf, void *priv)
1287 PRINT_ATTRf(type, p_unsigned);
1288 PRINT_ATTRf(size, p_unsigned);
1289 PRINT_ATTRf(config, p_hex);
1290 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1291 PRINT_ATTRf(sample_type, p_sample_type);
1292 PRINT_ATTRf(read_format, p_read_format);
1294 PRINT_ATTRf(disabled, p_unsigned);
1295 PRINT_ATTRf(inherit, p_unsigned);
1296 PRINT_ATTRf(pinned, p_unsigned);
1297 PRINT_ATTRf(exclusive, p_unsigned);
1298 PRINT_ATTRf(exclude_user, p_unsigned);
1299 PRINT_ATTRf(exclude_kernel, p_unsigned);
1300 PRINT_ATTRf(exclude_hv, p_unsigned);
1301 PRINT_ATTRf(exclude_idle, p_unsigned);
1302 PRINT_ATTRf(mmap, p_unsigned);
1303 PRINT_ATTRf(comm, p_unsigned);
1304 PRINT_ATTRf(freq, p_unsigned);
1305 PRINT_ATTRf(inherit_stat, p_unsigned);
1306 PRINT_ATTRf(enable_on_exec, p_unsigned);
1307 PRINT_ATTRf(task, p_unsigned);
1308 PRINT_ATTRf(watermark, p_unsigned);
1309 PRINT_ATTRf(precise_ip, p_unsigned);
1310 PRINT_ATTRf(mmap_data, p_unsigned);
1311 PRINT_ATTRf(sample_id_all, p_unsigned);
1312 PRINT_ATTRf(exclude_host, p_unsigned);
1313 PRINT_ATTRf(exclude_guest, p_unsigned);
1314 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1315 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1316 PRINT_ATTRf(mmap2, p_unsigned);
1317 PRINT_ATTRf(comm_exec, p_unsigned);
1318 PRINT_ATTRf(use_clockid, p_unsigned);
1319 PRINT_ATTRf(context_switch, p_unsigned);
1321 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1322 PRINT_ATTRf(bp_type, p_unsigned);
1323 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1324 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1325 PRINT_ATTRf(branch_sample_type, p_unsigned);
1326 PRINT_ATTRf(sample_regs_user, p_hex);
1327 PRINT_ATTRf(sample_stack_user, p_unsigned);
1328 PRINT_ATTRf(clockid, p_signed);
1329 PRINT_ATTRf(sample_regs_intr, p_hex);
1330 PRINT_ATTRf(aux_watermark, p_unsigned);
1335 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1336 void *priv __attribute__((unused)))
1338 return fprintf(fp, " %-32s %s\n", name, val);
1341 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1342 struct thread_map *threads)
1344 int cpu, thread, nthreads;
1345 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1347 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1349 if (evsel->system_wide)
1352 nthreads = threads->nr;
1354 if (evsel->fd == NULL &&
1355 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1359 flags |= PERF_FLAG_PID_CGROUP;
1360 pid = evsel->cgrp->fd;
1363 fallback_missing_features:
1364 if (perf_missing_features.clockid_wrong)
1365 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1366 if (perf_missing_features.clockid) {
1367 evsel->attr.use_clockid = 0;
1368 evsel->attr.clockid = 0;
1370 if (perf_missing_features.cloexec)
1371 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1372 if (perf_missing_features.mmap2)
1373 evsel->attr.mmap2 = 0;
1374 if (perf_missing_features.exclude_guest)
1375 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1377 if (perf_missing_features.sample_id_all)
1378 evsel->attr.sample_id_all = 0;
1381 fprintf(stderr, "%.60s\n", graph_dotted_line);
1382 fprintf(stderr, "perf_event_attr:\n");
1383 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1384 fprintf(stderr, "%.60s\n", graph_dotted_line);
1387 for (cpu = 0; cpu < cpus->nr; cpu++) {
1389 for (thread = 0; thread < nthreads; thread++) {
1392 if (!evsel->cgrp && !evsel->system_wide)
1393 pid = thread_map__pid(threads, thread);
1395 group_fd = get_group_fd(evsel, cpu, thread);
1397 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1398 pid, cpus->map[cpu], group_fd, flags);
1400 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1404 if (FD(evsel, cpu, thread) < 0) {
1406 pr_debug2("sys_perf_event_open failed, error %d\n",
1411 if (evsel->bpf_fd >= 0) {
1412 int evt_fd = FD(evsel, cpu, thread);
1413 int bpf_fd = evsel->bpf_fd;
1416 PERF_EVENT_IOC_SET_BPF,
1418 if (err && errno != EEXIST) {
1419 pr_err("failed to attach bpf fd %d: %s\n",
1420 bpf_fd, strerror(errno));
1426 set_rlimit = NO_CHANGE;
1429 * If we succeeded but had to kill clockid, fail and
1430 * have perf_evsel__open_strerror() print us a nice
1433 if (perf_missing_features.clockid ||
1434 perf_missing_features.clockid_wrong) {
1445 * perf stat needs between 5 and 22 fds per CPU. When we run out
1446 * of them try to increase the limits.
1448 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1450 int old_errno = errno;
1452 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1453 if (set_rlimit == NO_CHANGE)
1454 l.rlim_cur = l.rlim_max;
1456 l.rlim_cur = l.rlim_max + 1000;
1457 l.rlim_max = l.rlim_cur;
1459 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1468 if (err != -EINVAL || cpu > 0 || thread > 0)
1472 * Must probe features in the order they were added to the
1473 * perf_event_attr interface.
1475 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1476 perf_missing_features.clockid_wrong = true;
1477 goto fallback_missing_features;
1478 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1479 perf_missing_features.clockid = true;
1480 goto fallback_missing_features;
1481 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1482 perf_missing_features.cloexec = true;
1483 goto fallback_missing_features;
1484 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1485 perf_missing_features.mmap2 = true;
1486 goto fallback_missing_features;
1487 } else if (!perf_missing_features.exclude_guest &&
1488 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1489 perf_missing_features.exclude_guest = true;
1490 goto fallback_missing_features;
1491 } else if (!perf_missing_features.sample_id_all) {
1492 perf_missing_features.sample_id_all = true;
1493 goto retry_sample_id;
1498 while (--thread >= 0) {
1499 close(FD(evsel, cpu, thread));
1500 FD(evsel, cpu, thread) = -1;
1503 } while (--cpu >= 0);
1507 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1509 if (evsel->fd == NULL)
1512 perf_evsel__close_fd(evsel, ncpus, nthreads);
1513 perf_evsel__free_fd(evsel);
1525 struct thread_map map;
1527 } empty_thread_map = {
1532 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1533 struct thread_map *threads)
1536 /* Work around old compiler warnings about strict aliasing */
1537 cpus = &empty_cpu_map.map;
1540 if (threads == NULL)
1541 threads = &empty_thread_map.map;
1543 return __perf_evsel__open(evsel, cpus, threads);
1546 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1547 struct cpu_map *cpus)
1549 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1552 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1553 struct thread_map *threads)
1555 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1558 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1559 const union perf_event *event,
1560 struct perf_sample *sample)
1562 u64 type = evsel->attr.sample_type;
1563 const u64 *array = event->sample.array;
1564 bool swapped = evsel->needs_swap;
1567 array += ((event->header.size -
1568 sizeof(event->header)) / sizeof(u64)) - 1;
1570 if (type & PERF_SAMPLE_IDENTIFIER) {
1571 sample->id = *array;
1575 if (type & PERF_SAMPLE_CPU) {
1578 /* undo swap of u64, then swap on individual u32s */
1579 u.val64 = bswap_64(u.val64);
1580 u.val32[0] = bswap_32(u.val32[0]);
1583 sample->cpu = u.val32[0];
1587 if (type & PERF_SAMPLE_STREAM_ID) {
1588 sample->stream_id = *array;
1592 if (type & PERF_SAMPLE_ID) {
1593 sample->id = *array;
1597 if (type & PERF_SAMPLE_TIME) {
1598 sample->time = *array;
1602 if (type & PERF_SAMPLE_TID) {
1605 /* undo swap of u64, then swap on individual u32s */
1606 u.val64 = bswap_64(u.val64);
1607 u.val32[0] = bswap_32(u.val32[0]);
1608 u.val32[1] = bswap_32(u.val32[1]);
1611 sample->pid = u.val32[0];
1612 sample->tid = u.val32[1];
1619 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1622 return size > max_size || offset + size > endp;
1625 #define OVERFLOW_CHECK(offset, size, max_size) \
1627 if (overflow(endp, (max_size), (offset), (size))) \
1631 #define OVERFLOW_CHECK_u64(offset) \
1632 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1634 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1635 struct perf_sample *data)
1637 u64 type = evsel->attr.sample_type;
1638 bool swapped = evsel->needs_swap;
1640 u16 max_size = event->header.size;
1641 const void *endp = (void *)event + max_size;
1645 * used for cross-endian analysis. See git commit 65014ab3
1646 * for why this goofiness is needed.
1650 memset(data, 0, sizeof(*data));
1651 data->cpu = data->pid = data->tid = -1;
1652 data->stream_id = data->id = data->time = -1ULL;
1653 data->period = evsel->attr.sample_period;
1656 if (event->header.type != PERF_RECORD_SAMPLE) {
1657 if (!evsel->attr.sample_id_all)
1659 return perf_evsel__parse_id_sample(evsel, event, data);
1662 array = event->sample.array;
1665 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1666 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1667 * check the format does not go past the end of the event.
1669 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1673 if (type & PERF_SAMPLE_IDENTIFIER) {
1678 if (type & PERF_SAMPLE_IP) {
1683 if (type & PERF_SAMPLE_TID) {
1686 /* undo swap of u64, then swap on individual u32s */
1687 u.val64 = bswap_64(u.val64);
1688 u.val32[0] = bswap_32(u.val32[0]);
1689 u.val32[1] = bswap_32(u.val32[1]);
1692 data->pid = u.val32[0];
1693 data->tid = u.val32[1];
1697 if (type & PERF_SAMPLE_TIME) {
1698 data->time = *array;
1703 if (type & PERF_SAMPLE_ADDR) {
1704 data->addr = *array;
1708 if (type & PERF_SAMPLE_ID) {
1713 if (type & PERF_SAMPLE_STREAM_ID) {
1714 data->stream_id = *array;
1718 if (type & PERF_SAMPLE_CPU) {
1722 /* undo swap of u64, then swap on individual u32s */
1723 u.val64 = bswap_64(u.val64);
1724 u.val32[0] = bswap_32(u.val32[0]);
1727 data->cpu = u.val32[0];
1731 if (type & PERF_SAMPLE_PERIOD) {
1732 data->period = *array;
1736 if (type & PERF_SAMPLE_READ) {
1737 u64 read_format = evsel->attr.read_format;
1739 OVERFLOW_CHECK_u64(array);
1740 if (read_format & PERF_FORMAT_GROUP)
1741 data->read.group.nr = *array;
1743 data->read.one.value = *array;
1747 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1748 OVERFLOW_CHECK_u64(array);
1749 data->read.time_enabled = *array;
1753 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1754 OVERFLOW_CHECK_u64(array);
1755 data->read.time_running = *array;
1759 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1760 if (read_format & PERF_FORMAT_GROUP) {
1761 const u64 max_group_nr = UINT64_MAX /
1762 sizeof(struct sample_read_value);
1764 if (data->read.group.nr > max_group_nr)
1766 sz = data->read.group.nr *
1767 sizeof(struct sample_read_value);
1768 OVERFLOW_CHECK(array, sz, max_size);
1769 data->read.group.values =
1770 (struct sample_read_value *)array;
1771 array = (void *)array + sz;
1773 OVERFLOW_CHECK_u64(array);
1774 data->read.one.id = *array;
1779 if (type & PERF_SAMPLE_CALLCHAIN) {
1780 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1782 OVERFLOW_CHECK_u64(array);
1783 data->callchain = (struct ip_callchain *)array++;
1784 if (data->callchain->nr > max_callchain_nr)
1786 sz = data->callchain->nr * sizeof(u64);
1787 OVERFLOW_CHECK(array, sz, max_size);
1788 array = (void *)array + sz;
1791 if (type & PERF_SAMPLE_RAW) {
1792 OVERFLOW_CHECK_u64(array);
1794 if (WARN_ONCE(swapped,
1795 "Endianness of raw data not corrected!\n")) {
1796 /* undo swap of u64, then swap on individual u32s */
1797 u.val64 = bswap_64(u.val64);
1798 u.val32[0] = bswap_32(u.val32[0]);
1799 u.val32[1] = bswap_32(u.val32[1]);
1801 data->raw_size = u.val32[0];
1802 array = (void *)array + sizeof(u32);
1804 OVERFLOW_CHECK(array, data->raw_size, max_size);
1805 data->raw_data = (void *)array;
1806 array = (void *)array + data->raw_size;
1809 if (type & PERF_SAMPLE_BRANCH_STACK) {
1810 const u64 max_branch_nr = UINT64_MAX /
1811 sizeof(struct branch_entry);
1813 OVERFLOW_CHECK_u64(array);
1814 data->branch_stack = (struct branch_stack *)array++;
1816 if (data->branch_stack->nr > max_branch_nr)
1818 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1819 OVERFLOW_CHECK(array, sz, max_size);
1820 array = (void *)array + sz;
1823 if (type & PERF_SAMPLE_REGS_USER) {
1824 OVERFLOW_CHECK_u64(array);
1825 data->user_regs.abi = *array;
1828 if (data->user_regs.abi) {
1829 u64 mask = evsel->attr.sample_regs_user;
1831 sz = hweight_long(mask) * sizeof(u64);
1832 OVERFLOW_CHECK(array, sz, max_size);
1833 data->user_regs.mask = mask;
1834 data->user_regs.regs = (u64 *)array;
1835 array = (void *)array + sz;
1839 if (type & PERF_SAMPLE_STACK_USER) {
1840 OVERFLOW_CHECK_u64(array);
1843 data->user_stack.offset = ((char *)(array - 1)
1847 data->user_stack.size = 0;
1849 OVERFLOW_CHECK(array, sz, max_size);
1850 data->user_stack.data = (char *)array;
1851 array = (void *)array + sz;
1852 OVERFLOW_CHECK_u64(array);
1853 data->user_stack.size = *array++;
1854 if (WARN_ONCE(data->user_stack.size > sz,
1855 "user stack dump failure\n"))
1861 if (type & PERF_SAMPLE_WEIGHT) {
1862 OVERFLOW_CHECK_u64(array);
1863 data->weight = *array;
1867 data->data_src = PERF_MEM_DATA_SRC_NONE;
1868 if (type & PERF_SAMPLE_DATA_SRC) {
1869 OVERFLOW_CHECK_u64(array);
1870 data->data_src = *array;
1874 data->transaction = 0;
1875 if (type & PERF_SAMPLE_TRANSACTION) {
1876 OVERFLOW_CHECK_u64(array);
1877 data->transaction = *array;
1881 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1882 if (type & PERF_SAMPLE_REGS_INTR) {
1883 OVERFLOW_CHECK_u64(array);
1884 data->intr_regs.abi = *array;
1887 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1888 u64 mask = evsel->attr.sample_regs_intr;
1890 sz = hweight_long(mask) * sizeof(u64);
1891 OVERFLOW_CHECK(array, sz, max_size);
1892 data->intr_regs.mask = mask;
1893 data->intr_regs.regs = (u64 *)array;
1894 array = (void *)array + sz;
1901 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1904 size_t sz, result = sizeof(struct sample_event);
1906 if (type & PERF_SAMPLE_IDENTIFIER)
1907 result += sizeof(u64);
1909 if (type & PERF_SAMPLE_IP)
1910 result += sizeof(u64);
1912 if (type & PERF_SAMPLE_TID)
1913 result += sizeof(u64);
1915 if (type & PERF_SAMPLE_TIME)
1916 result += sizeof(u64);
1918 if (type & PERF_SAMPLE_ADDR)
1919 result += sizeof(u64);
1921 if (type & PERF_SAMPLE_ID)
1922 result += sizeof(u64);
1924 if (type & PERF_SAMPLE_STREAM_ID)
1925 result += sizeof(u64);
1927 if (type & PERF_SAMPLE_CPU)
1928 result += sizeof(u64);
1930 if (type & PERF_SAMPLE_PERIOD)
1931 result += sizeof(u64);
1933 if (type & PERF_SAMPLE_READ) {
1934 result += sizeof(u64);
1935 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1936 result += sizeof(u64);
1937 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1938 result += sizeof(u64);
1939 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1940 if (read_format & PERF_FORMAT_GROUP) {
1941 sz = sample->read.group.nr *
1942 sizeof(struct sample_read_value);
1945 result += sizeof(u64);
1949 if (type & PERF_SAMPLE_CALLCHAIN) {
1950 sz = (sample->callchain->nr + 1) * sizeof(u64);
1954 if (type & PERF_SAMPLE_RAW) {
1955 result += sizeof(u32);
1956 result += sample->raw_size;
1959 if (type & PERF_SAMPLE_BRANCH_STACK) {
1960 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1965 if (type & PERF_SAMPLE_REGS_USER) {
1966 if (sample->user_regs.abi) {
1967 result += sizeof(u64);
1968 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1971 result += sizeof(u64);
1975 if (type & PERF_SAMPLE_STACK_USER) {
1976 sz = sample->user_stack.size;
1977 result += sizeof(u64);
1980 result += sizeof(u64);
1984 if (type & PERF_SAMPLE_WEIGHT)
1985 result += sizeof(u64);
1987 if (type & PERF_SAMPLE_DATA_SRC)
1988 result += sizeof(u64);
1990 if (type & PERF_SAMPLE_TRANSACTION)
1991 result += sizeof(u64);
1993 if (type & PERF_SAMPLE_REGS_INTR) {
1994 if (sample->intr_regs.abi) {
1995 result += sizeof(u64);
1996 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1999 result += sizeof(u64);
2006 int perf_event__synthesize_sample(union perf_event *event, u64 type,
2008 const struct perf_sample *sample,
2014 * used for cross-endian analysis. See git commit 65014ab3
2015 * for why this goofiness is needed.
2019 array = event->sample.array;
2021 if (type & PERF_SAMPLE_IDENTIFIER) {
2022 *array = sample->id;
2026 if (type & PERF_SAMPLE_IP) {
2027 *array = sample->ip;
2031 if (type & PERF_SAMPLE_TID) {
2032 u.val32[0] = sample->pid;
2033 u.val32[1] = sample->tid;
2036 * Inverse of what is done in perf_evsel__parse_sample
2038 u.val32[0] = bswap_32(u.val32[0]);
2039 u.val32[1] = bswap_32(u.val32[1]);
2040 u.val64 = bswap_64(u.val64);
2047 if (type & PERF_SAMPLE_TIME) {
2048 *array = sample->time;
2052 if (type & PERF_SAMPLE_ADDR) {
2053 *array = sample->addr;
2057 if (type & PERF_SAMPLE_ID) {
2058 *array = sample->id;
2062 if (type & PERF_SAMPLE_STREAM_ID) {
2063 *array = sample->stream_id;
2067 if (type & PERF_SAMPLE_CPU) {
2068 u.val32[0] = sample->cpu;
2071 * Inverse of what is done in perf_evsel__parse_sample
2073 u.val32[0] = bswap_32(u.val32[0]);
2074 u.val64 = bswap_64(u.val64);
2080 if (type & PERF_SAMPLE_PERIOD) {
2081 *array = sample->period;
2085 if (type & PERF_SAMPLE_READ) {
2086 if (read_format & PERF_FORMAT_GROUP)
2087 *array = sample->read.group.nr;
2089 *array = sample->read.one.value;
2092 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2093 *array = sample->read.time_enabled;
2097 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2098 *array = sample->read.time_running;
2102 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2103 if (read_format & PERF_FORMAT_GROUP) {
2104 sz = sample->read.group.nr *
2105 sizeof(struct sample_read_value);
2106 memcpy(array, sample->read.group.values, sz);
2107 array = (void *)array + sz;
2109 *array = sample->read.one.id;
2114 if (type & PERF_SAMPLE_CALLCHAIN) {
2115 sz = (sample->callchain->nr + 1) * sizeof(u64);
2116 memcpy(array, sample->callchain, sz);
2117 array = (void *)array + sz;
2120 if (type & PERF_SAMPLE_RAW) {
2121 u.val32[0] = sample->raw_size;
2122 if (WARN_ONCE(swapped,
2123 "Endianness of raw data not corrected!\n")) {
2125 * Inverse of what is done in perf_evsel__parse_sample
2127 u.val32[0] = bswap_32(u.val32[0]);
2128 u.val32[1] = bswap_32(u.val32[1]);
2129 u.val64 = bswap_64(u.val64);
2132 array = (void *)array + sizeof(u32);
2134 memcpy(array, sample->raw_data, sample->raw_size);
2135 array = (void *)array + sample->raw_size;
2138 if (type & PERF_SAMPLE_BRANCH_STACK) {
2139 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2141 memcpy(array, sample->branch_stack, sz);
2142 array = (void *)array + sz;
2145 if (type & PERF_SAMPLE_REGS_USER) {
2146 if (sample->user_regs.abi) {
2147 *array++ = sample->user_regs.abi;
2148 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2149 memcpy(array, sample->user_regs.regs, sz);
2150 array = (void *)array + sz;
2156 if (type & PERF_SAMPLE_STACK_USER) {
2157 sz = sample->user_stack.size;
2160 memcpy(array, sample->user_stack.data, sz);
2161 array = (void *)array + sz;
2166 if (type & PERF_SAMPLE_WEIGHT) {
2167 *array = sample->weight;
2171 if (type & PERF_SAMPLE_DATA_SRC) {
2172 *array = sample->data_src;
2176 if (type & PERF_SAMPLE_TRANSACTION) {
2177 *array = sample->transaction;
2181 if (type & PERF_SAMPLE_REGS_INTR) {
2182 if (sample->intr_regs.abi) {
2183 *array++ = sample->intr_regs.abi;
2184 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2185 memcpy(array, sample->intr_regs.regs, sz);
2186 array = (void *)array + sz;
2195 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2197 return pevent_find_field(evsel->tp_format, name);
2200 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2203 struct format_field *field = perf_evsel__field(evsel, name);
2209 offset = field->offset;
2211 if (field->flags & FIELD_IS_DYNAMIC) {
2212 offset = *(int *)(sample->raw_data + field->offset);
2216 return sample->raw_data + offset;
2219 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2222 struct format_field *field = perf_evsel__field(evsel, name);
2229 ptr = sample->raw_data + field->offset;
2231 switch (field->size) {
2235 value = *(u16 *)ptr;
2238 value = *(u32 *)ptr;
2241 memcpy(&value, ptr, sizeof(u64));
2247 if (!evsel->needs_swap)
2250 switch (field->size) {
2252 return bswap_16(value);
2254 return bswap_32(value);
2256 return bswap_64(value);
2264 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2270 ret += fprintf(fp, ",");
2272 ret += fprintf(fp, ":");
2276 va_start(args, fmt);
2277 ret += vfprintf(fp, fmt, args);
2282 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2284 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2287 int perf_evsel__fprintf(struct perf_evsel *evsel,
2288 struct perf_attr_details *details, FILE *fp)
2293 if (details->event_group) {
2294 struct perf_evsel *pos;
2296 if (!perf_evsel__is_group_leader(evsel))
2299 if (evsel->nr_members > 1)
2300 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2302 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2303 for_each_group_member(pos, evsel)
2304 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2306 if (evsel->nr_members > 1)
2307 printed += fprintf(fp, "}");
2311 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2313 if (details->verbose) {
2314 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2315 __print_attr__fprintf, &first);
2316 } else if (details->freq) {
2317 const char *term = "sample_freq";
2319 if (!evsel->attr.freq)
2320 term = "sample_period";
2322 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2323 term, (u64)evsel->attr.sample_freq);
2330 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2331 char *msg, size_t msgsize)
2333 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2334 evsel->attr.type == PERF_TYPE_HARDWARE &&
2335 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2337 * If it's cycles then fall back to hrtimer based
2338 * cpu-clock-tick sw counter, which is always available even if
2341 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2344 scnprintf(msg, msgsize, "%s",
2345 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2347 evsel->attr.type = PERF_TYPE_SOFTWARE;
2348 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2350 zfree(&evsel->name);
2357 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2358 int err, char *msg, size_t size)
2360 char sbuf[STRERR_BUFSIZE];
2365 return scnprintf(msg, size,
2366 "You may not have permission to collect %sstats.\n\n"
2367 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2368 "which controls use of the performance events system by\n"
2369 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2370 "The default value is 1:\n\n"
2371 " -1: Allow use of (almost) all events by all users\n"
2372 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2373 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2374 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
2375 target->system_wide ? "system-wide " : "");
2377 return scnprintf(msg, size, "The %s event is not supported.",
2378 perf_evsel__name(evsel));
2380 return scnprintf(msg, size, "%s",
2381 "Too many events are opened.\n"
2382 "Probably the maximum number of open file descriptors has been reached.\n"
2383 "Hint: Try again after reducing the number of events.\n"
2384 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2386 if (target->cpu_list)
2387 return scnprintf(msg, size, "%s",
2388 "No such device - did you specify an out-of-range profile CPU?\n");
2391 if (evsel->attr.precise_ip)
2392 return scnprintf(msg, size, "%s",
2393 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2394 #if defined(__i386__) || defined(__x86_64__)
2395 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2396 return scnprintf(msg, size, "%s",
2397 "No hardware sampling interrupt available.\n"
2398 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2402 if (find_process("oprofiled"))
2403 return scnprintf(msg, size,
2404 "The PMU counters are busy/taken by another profiler.\n"
2405 "We found oprofile daemon running, please stop it and try again.");
2408 if (perf_missing_features.clockid)
2409 return scnprintf(msg, size, "clockid feature not supported.");
2410 if (perf_missing_features.clockid_wrong)
2411 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2417 return scnprintf(msg, size,
2418 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2419 "/bin/dmesg may provide additional information.\n"
2420 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2421 err, strerror_r(err, sbuf, sizeof(sbuf)),
2422 perf_evsel__name(evsel));