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 perf_evsel__object.init(evsel);
215 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
216 perf_evsel__calc_id_pos(evsel);
217 evsel->cmdline_group_boundary = false;
220 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
222 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
225 perf_evsel__init(evsel, attr, idx);
231 * Returns pointer with encoded error via <linux/err.h> interface.
233 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
235 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
241 struct perf_event_attr attr = {
242 .type = PERF_TYPE_TRACEPOINT,
243 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
244 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
247 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
250 evsel->tp_format = trace_event__tp_format(sys, name);
251 if (IS_ERR(evsel->tp_format)) {
252 err = PTR_ERR(evsel->tp_format);
256 event_attr_init(&attr);
257 attr.config = evsel->tp_format->id;
258 attr.sample_period = 1;
259 perf_evsel__init(evsel, &attr, idx);
271 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
279 "stalled-cycles-frontend",
280 "stalled-cycles-backend",
284 static const char *__perf_evsel__hw_name(u64 config)
286 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
287 return perf_evsel__hw_names[config];
289 return "unknown-hardware";
292 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
294 int colon = 0, r = 0;
295 struct perf_event_attr *attr = &evsel->attr;
296 bool exclude_guest_default = false;
298 #define MOD_PRINT(context, mod) do { \
299 if (!attr->exclude_##context) { \
300 if (!colon) colon = ++r; \
301 r += scnprintf(bf + r, size - r, "%c", mod); \
304 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
305 MOD_PRINT(kernel, 'k');
306 MOD_PRINT(user, 'u');
308 exclude_guest_default = true;
311 if (attr->precise_ip) {
314 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
315 exclude_guest_default = true;
318 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
319 MOD_PRINT(host, 'H');
320 MOD_PRINT(guest, 'G');
328 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
330 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
331 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
334 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
347 static const char *__perf_evsel__sw_name(u64 config)
349 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
350 return perf_evsel__sw_names[config];
351 return "unknown-software";
354 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
356 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
357 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
360 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
364 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
366 if (type & HW_BREAKPOINT_R)
367 r += scnprintf(bf + r, size - r, "r");
369 if (type & HW_BREAKPOINT_W)
370 r += scnprintf(bf + r, size - r, "w");
372 if (type & HW_BREAKPOINT_X)
373 r += scnprintf(bf + r, size - r, "x");
378 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
380 struct perf_event_attr *attr = &evsel->attr;
381 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
382 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
385 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
386 [PERF_EVSEL__MAX_ALIASES] = {
387 { "L1-dcache", "l1-d", "l1d", "L1-data", },
388 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
390 { "dTLB", "d-tlb", "Data-TLB", },
391 { "iTLB", "i-tlb", "Instruction-TLB", },
392 { "branch", "branches", "bpu", "btb", "bpc", },
396 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
397 [PERF_EVSEL__MAX_ALIASES] = {
398 { "load", "loads", "read", },
399 { "store", "stores", "write", },
400 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
403 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
404 [PERF_EVSEL__MAX_ALIASES] = {
405 { "refs", "Reference", "ops", "access", },
406 { "misses", "miss", },
409 #define C(x) PERF_COUNT_HW_CACHE_##x
410 #define CACHE_READ (1 << C(OP_READ))
411 #define CACHE_WRITE (1 << C(OP_WRITE))
412 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
413 #define COP(x) (1 << x)
416 * cache operartion stat
417 * L1I : Read and prefetch only
418 * ITLB and BPU : Read-only
420 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
421 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
422 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
423 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
424 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
425 [C(ITLB)] = (CACHE_READ),
426 [C(BPU)] = (CACHE_READ),
427 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
430 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
432 if (perf_evsel__hw_cache_stat[type] & COP(op))
433 return true; /* valid */
435 return false; /* invalid */
438 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
439 char *bf, size_t size)
442 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
443 perf_evsel__hw_cache_op[op][0],
444 perf_evsel__hw_cache_result[result][0]);
447 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
448 perf_evsel__hw_cache_op[op][1]);
451 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
453 u8 op, result, type = (config >> 0) & 0xff;
454 const char *err = "unknown-ext-hardware-cache-type";
456 if (type > PERF_COUNT_HW_CACHE_MAX)
459 op = (config >> 8) & 0xff;
460 err = "unknown-ext-hardware-cache-op";
461 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
464 result = (config >> 16) & 0xff;
465 err = "unknown-ext-hardware-cache-result";
466 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
469 err = "invalid-cache";
470 if (!perf_evsel__is_cache_op_valid(type, op))
473 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
475 return scnprintf(bf, size, "%s", err);
478 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
480 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
481 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
484 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
486 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
487 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
490 const char *perf_evsel__name(struct perf_evsel *evsel)
497 switch (evsel->attr.type) {
499 perf_evsel__raw_name(evsel, bf, sizeof(bf));
502 case PERF_TYPE_HARDWARE:
503 perf_evsel__hw_name(evsel, bf, sizeof(bf));
506 case PERF_TYPE_HW_CACHE:
507 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
510 case PERF_TYPE_SOFTWARE:
511 perf_evsel__sw_name(evsel, bf, sizeof(bf));
514 case PERF_TYPE_TRACEPOINT:
515 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
518 case PERF_TYPE_BREAKPOINT:
519 perf_evsel__bp_name(evsel, bf, sizeof(bf));
523 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
528 evsel->name = strdup(bf);
530 return evsel->name ?: "unknown";
533 const char *perf_evsel__group_name(struct perf_evsel *evsel)
535 return evsel->group_name ?: "anon group";
538 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
541 struct perf_evsel *pos;
542 const char *group_name = perf_evsel__group_name(evsel);
544 ret = scnprintf(buf, size, "%s", group_name);
546 ret += scnprintf(buf + ret, size - ret, " { %s",
547 perf_evsel__name(evsel));
549 for_each_group_member(pos, evsel)
550 ret += scnprintf(buf + ret, size - ret, ", %s",
551 perf_evsel__name(pos));
553 ret += scnprintf(buf + ret, size - ret, " }");
559 perf_evsel__config_callgraph(struct perf_evsel *evsel,
560 struct record_opts *opts,
561 struct callchain_param *param)
563 bool function = perf_evsel__is_function_event(evsel);
564 struct perf_event_attr *attr = &evsel->attr;
566 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
568 if (param->record_mode == CALLCHAIN_LBR) {
569 if (!opts->branch_stack) {
570 if (attr->exclude_user) {
571 pr_warning("LBR callstack option is only available "
572 "to get user callchain information. "
573 "Falling back to framepointers.\n");
575 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
576 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
577 PERF_SAMPLE_BRANCH_CALL_STACK;
580 pr_warning("Cannot use LBR callstack with branch stack. "
581 "Falling back to framepointers.\n");
584 if (param->record_mode == CALLCHAIN_DWARF) {
586 perf_evsel__set_sample_bit(evsel, REGS_USER);
587 perf_evsel__set_sample_bit(evsel, STACK_USER);
588 attr->sample_regs_user = PERF_REGS_MASK;
589 attr->sample_stack_user = param->dump_size;
590 attr->exclude_callchain_user = 1;
592 pr_info("Cannot use DWARF unwind for function trace event,"
593 " falling back to framepointers.\n");
598 pr_info("Disabling user space callchains for function trace event.\n");
599 attr->exclude_callchain_user = 1;
604 perf_evsel__reset_callgraph(struct perf_evsel *evsel,
605 struct callchain_param *param)
607 struct perf_event_attr *attr = &evsel->attr;
609 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
610 if (param->record_mode == CALLCHAIN_LBR) {
611 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
612 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
613 PERF_SAMPLE_BRANCH_CALL_STACK);
615 if (param->record_mode == CALLCHAIN_DWARF) {
616 perf_evsel__reset_sample_bit(evsel, REGS_USER);
617 perf_evsel__reset_sample_bit(evsel, STACK_USER);
621 static void apply_config_terms(struct perf_evsel *evsel,
622 struct record_opts *opts)
624 struct perf_evsel_config_term *term;
625 struct list_head *config_terms = &evsel->config_terms;
626 struct perf_event_attr *attr = &evsel->attr;
627 /* callgraph default */
628 struct callchain_param param = {
629 .record_mode = callchain_param.record_mode,
632 char *callgraph_buf = NULL;
634 list_for_each_entry(term, config_terms, list) {
635 switch (term->type) {
636 case PERF_EVSEL__CONFIG_TERM_PERIOD:
637 attr->sample_period = term->val.period;
640 case PERF_EVSEL__CONFIG_TERM_FREQ:
641 attr->sample_freq = term->val.freq;
644 case PERF_EVSEL__CONFIG_TERM_TIME:
646 perf_evsel__set_sample_bit(evsel, TIME);
648 perf_evsel__reset_sample_bit(evsel, TIME);
650 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
651 callgraph_buf = term->val.callgraph;
653 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
654 dump_size = term->val.stack_user;
656 case PERF_EVSEL__CONFIG_TERM_INHERIT:
658 * attr->inherit should has already been set by
659 * perf_evsel__config. If user explicitly set
660 * inherit using config terms, override global
661 * opt->no_inherit setting.
663 attr->inherit = term->val.inherit ? 1 : 0;
670 /* User explicitly set per-event callgraph, clear the old setting and reset. */
671 if ((callgraph_buf != NULL) || (dump_size > 0)) {
673 /* parse callgraph parameters */
674 if (callgraph_buf != NULL) {
675 if (!strcmp(callgraph_buf, "no")) {
676 param.enabled = false;
677 param.record_mode = CALLCHAIN_NONE;
679 param.enabled = true;
680 if (parse_callchain_record(callgraph_buf, ¶m)) {
681 pr_err("per-event callgraph setting for %s failed. "
682 "Apply callgraph global setting for it\n",
689 dump_size = round_up(dump_size, sizeof(u64));
690 param.dump_size = dump_size;
693 /* If global callgraph set, clear it */
694 if (callchain_param.enabled)
695 perf_evsel__reset_callgraph(evsel, &callchain_param);
697 /* set perf-event callgraph */
699 perf_evsel__config_callgraph(evsel, opts, ¶m);
704 * The enable_on_exec/disabled value strategy:
706 * 1) For any type of traced program:
707 * - all independent events and group leaders are disabled
708 * - all group members are enabled
710 * Group members are ruled by group leaders. They need to
711 * be enabled, because the group scheduling relies on that.
713 * 2) For traced programs executed by perf:
714 * - all independent events and group leaders have
716 * - we don't specifically enable or disable any event during
719 * Independent events and group leaders are initially disabled
720 * and get enabled by exec. Group members are ruled by group
721 * leaders as stated in 1).
723 * 3) For traced programs attached by perf (pid/tid):
724 * - we specifically enable or disable all events during
727 * When attaching events to already running traced we
728 * enable/disable events specifically, as there's no
729 * initial traced exec call.
731 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
733 struct perf_evsel *leader = evsel->leader;
734 struct perf_event_attr *attr = &evsel->attr;
735 int track = evsel->tracking;
736 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
738 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
739 attr->inherit = !opts->no_inherit;
741 perf_evsel__set_sample_bit(evsel, IP);
742 perf_evsel__set_sample_bit(evsel, TID);
744 if (evsel->sample_read) {
745 perf_evsel__set_sample_bit(evsel, READ);
748 * We need ID even in case of single event, because
749 * PERF_SAMPLE_READ process ID specific data.
751 perf_evsel__set_sample_id(evsel, false);
754 * Apply group format only if we belong to group
755 * with more than one members.
757 if (leader->nr_members > 1) {
758 attr->read_format |= PERF_FORMAT_GROUP;
764 * We default some events to have a default interval. But keep
765 * it a weak assumption overridable by the user.
767 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
768 opts->user_interval != ULLONG_MAX)) {
770 perf_evsel__set_sample_bit(evsel, PERIOD);
772 attr->sample_freq = opts->freq;
774 attr->sample_period = opts->default_interval;
779 * Disable sampling for all group members other
780 * than leader in case leader 'leads' the sampling.
782 if ((leader != evsel) && leader->sample_read) {
783 attr->sample_freq = 0;
784 attr->sample_period = 0;
787 if (opts->no_samples)
788 attr->sample_freq = 0;
790 if (opts->inherit_stat)
791 attr->inherit_stat = 1;
793 if (opts->sample_address) {
794 perf_evsel__set_sample_bit(evsel, ADDR);
795 attr->mmap_data = track;
799 * We don't allow user space callchains for function trace
800 * event, due to issues with page faults while tracing page
801 * fault handler and its overall trickiness nature.
803 if (perf_evsel__is_function_event(evsel))
804 evsel->attr.exclude_callchain_user = 1;
806 if (callchain_param.enabled && !evsel->no_aux_samples)
807 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
809 if (opts->sample_intr_regs) {
810 attr->sample_regs_intr = opts->sample_intr_regs;
811 perf_evsel__set_sample_bit(evsel, REGS_INTR);
814 if (target__has_cpu(&opts->target))
815 perf_evsel__set_sample_bit(evsel, CPU);
818 perf_evsel__set_sample_bit(evsel, PERIOD);
821 * When the user explicitely disabled time don't force it here.
823 if (opts->sample_time &&
824 (!perf_missing_features.sample_id_all &&
825 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
826 opts->sample_time_set)))
827 perf_evsel__set_sample_bit(evsel, TIME);
829 if (opts->raw_samples && !evsel->no_aux_samples) {
830 perf_evsel__set_sample_bit(evsel, TIME);
831 perf_evsel__set_sample_bit(evsel, RAW);
832 perf_evsel__set_sample_bit(evsel, CPU);
835 if (opts->sample_address)
836 perf_evsel__set_sample_bit(evsel, DATA_SRC);
838 if (opts->no_buffering) {
840 attr->wakeup_events = 1;
842 if (opts->branch_stack && !evsel->no_aux_samples) {
843 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
844 attr->branch_sample_type = opts->branch_stack;
847 if (opts->sample_weight)
848 perf_evsel__set_sample_bit(evsel, WEIGHT);
852 attr->mmap2 = track && !perf_missing_features.mmap2;
855 if (opts->record_switch_events)
856 attr->context_switch = track;
858 if (opts->sample_transaction)
859 perf_evsel__set_sample_bit(evsel, TRANSACTION);
861 if (opts->running_time) {
862 evsel->attr.read_format |=
863 PERF_FORMAT_TOTAL_TIME_ENABLED |
864 PERF_FORMAT_TOTAL_TIME_RUNNING;
868 * XXX see the function comment above
870 * Disabling only independent events or group leaders,
871 * keeping group members enabled.
873 if (perf_evsel__is_group_leader(evsel))
877 * Setting enable_on_exec for independent events and
878 * group leaders for traced executed by perf.
880 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
881 !opts->initial_delay)
882 attr->enable_on_exec = 1;
884 if (evsel->immediate) {
886 attr->enable_on_exec = 0;
889 clockid = opts->clockid;
890 if (opts->use_clockid) {
891 attr->use_clockid = 1;
892 attr->clockid = opts->clockid;
895 if (evsel->precise_max)
896 perf_event_attr__set_max_precise_ip(attr);
899 * Apply event specific term settings,
900 * it overloads any global configuration.
902 apply_config_terms(evsel, opts);
905 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
909 if (evsel->system_wide)
912 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
915 for (cpu = 0; cpu < ncpus; cpu++) {
916 for (thread = 0; thread < nthreads; thread++) {
917 FD(evsel, cpu, thread) = -1;
922 return evsel->fd != NULL ? 0 : -ENOMEM;
925 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
930 if (evsel->system_wide)
933 for (cpu = 0; cpu < ncpus; cpu++) {
934 for (thread = 0; thread < nthreads; thread++) {
935 int fd = FD(evsel, cpu, thread),
936 err = ioctl(fd, ioc, arg);
946 int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
949 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
950 PERF_EVENT_IOC_SET_FILTER,
954 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
956 char *new_filter = strdup(filter);
958 if (new_filter != NULL) {
960 evsel->filter = new_filter;
967 int perf_evsel__append_filter(struct perf_evsel *evsel,
968 const char *op, const char *filter)
972 if (evsel->filter == NULL)
973 return perf_evsel__set_filter(evsel, filter);
975 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
977 evsel->filter = new_filter;
984 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
986 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
987 PERF_EVENT_IOC_ENABLE,
991 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
993 if (ncpus == 0 || nthreads == 0)
996 if (evsel->system_wide)
999 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1000 if (evsel->sample_id == NULL)
1003 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1004 if (evsel->id == NULL) {
1005 xyarray__delete(evsel->sample_id);
1006 evsel->sample_id = NULL;
1013 static void perf_evsel__free_fd(struct perf_evsel *evsel)
1015 xyarray__delete(evsel->fd);
1019 static void perf_evsel__free_id(struct perf_evsel *evsel)
1021 xyarray__delete(evsel->sample_id);
1022 evsel->sample_id = NULL;
1026 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1028 struct perf_evsel_config_term *term, *h;
1030 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1031 list_del(&term->list);
1036 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1040 if (evsel->system_wide)
1043 for (cpu = 0; cpu < ncpus; cpu++)
1044 for (thread = 0; thread < nthreads; ++thread) {
1045 close(FD(evsel, cpu, thread));
1046 FD(evsel, cpu, thread) = -1;
1050 void perf_evsel__exit(struct perf_evsel *evsel)
1052 assert(list_empty(&evsel->node));
1053 assert(evsel->evlist == NULL);
1054 perf_evsel__free_counts(evsel);
1055 perf_evsel__free_fd(evsel);
1056 perf_evsel__free_id(evsel);
1057 perf_evsel__free_config_terms(evsel);
1058 close_cgroup(evsel->cgrp);
1059 cpu_map__put(evsel->cpus);
1060 cpu_map__put(evsel->own_cpus);
1061 thread_map__put(evsel->threads);
1062 zfree(&evsel->group_name);
1063 zfree(&evsel->name);
1064 perf_evsel__object.fini(evsel);
1067 void perf_evsel__delete(struct perf_evsel *evsel)
1069 perf_evsel__exit(evsel);
1073 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1074 struct perf_counts_values *count)
1076 struct perf_counts_values tmp;
1078 if (!evsel->prev_raw_counts)
1082 tmp = evsel->prev_raw_counts->aggr;
1083 evsel->prev_raw_counts->aggr = *count;
1085 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1086 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1089 count->val = count->val - tmp.val;
1090 count->ena = count->ena - tmp.ena;
1091 count->run = count->run - tmp.run;
1094 void perf_counts_values__scale(struct perf_counts_values *count,
1095 bool scale, s8 *pscaled)
1100 if (count->run == 0) {
1103 } else if (count->run < count->ena) {
1105 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1108 count->ena = count->run = 0;
1114 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1115 struct perf_counts_values *count)
1117 memset(count, 0, sizeof(*count));
1119 if (FD(evsel, cpu, thread) < 0)
1122 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1128 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1129 int cpu, int thread, bool scale)
1131 struct perf_counts_values count;
1132 size_t nv = scale ? 3 : 1;
1134 if (FD(evsel, cpu, thread) < 0)
1137 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1140 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1143 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1144 perf_counts_values__scale(&count, scale, NULL);
1145 *perf_counts(evsel->counts, cpu, thread) = count;
1149 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1151 struct perf_evsel *leader = evsel->leader;
1154 if (perf_evsel__is_group_leader(evsel))
1158 * Leader must be already processed/open,
1159 * if not it's a bug.
1161 BUG_ON(!leader->fd);
1163 fd = FD(leader, cpu, thread);
1174 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1176 bool first_bit = true;
1180 if (value & bits[i].bit) {
1181 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1184 } while (bits[++i].name != NULL);
1187 static void __p_sample_type(char *buf, size_t size, u64 value)
1189 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1190 struct bit_names bits[] = {
1191 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1192 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1193 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1194 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1195 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1199 __p_bits(buf, size, value, bits);
1202 static void __p_read_format(char *buf, size_t size, u64 value)
1204 #define bit_name(n) { PERF_FORMAT_##n, #n }
1205 struct bit_names bits[] = {
1206 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1207 bit_name(ID), bit_name(GROUP),
1211 __p_bits(buf, size, value, bits);
1214 #define BUF_SIZE 1024
1216 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1217 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1218 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1219 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1220 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1222 #define PRINT_ATTRn(_n, _f, _p) \
1226 ret += attr__fprintf(fp, _n, buf, priv);\
1230 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1232 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1233 attr__fprintf_f attr__fprintf, void *priv)
1238 PRINT_ATTRf(type, p_unsigned);
1239 PRINT_ATTRf(size, p_unsigned);
1240 PRINT_ATTRf(config, p_hex);
1241 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1242 PRINT_ATTRf(sample_type, p_sample_type);
1243 PRINT_ATTRf(read_format, p_read_format);
1245 PRINT_ATTRf(disabled, p_unsigned);
1246 PRINT_ATTRf(inherit, p_unsigned);
1247 PRINT_ATTRf(pinned, p_unsigned);
1248 PRINT_ATTRf(exclusive, p_unsigned);
1249 PRINT_ATTRf(exclude_user, p_unsigned);
1250 PRINT_ATTRf(exclude_kernel, p_unsigned);
1251 PRINT_ATTRf(exclude_hv, p_unsigned);
1252 PRINT_ATTRf(exclude_idle, p_unsigned);
1253 PRINT_ATTRf(mmap, p_unsigned);
1254 PRINT_ATTRf(comm, p_unsigned);
1255 PRINT_ATTRf(freq, p_unsigned);
1256 PRINT_ATTRf(inherit_stat, p_unsigned);
1257 PRINT_ATTRf(enable_on_exec, p_unsigned);
1258 PRINT_ATTRf(task, p_unsigned);
1259 PRINT_ATTRf(watermark, p_unsigned);
1260 PRINT_ATTRf(precise_ip, p_unsigned);
1261 PRINT_ATTRf(mmap_data, p_unsigned);
1262 PRINT_ATTRf(sample_id_all, p_unsigned);
1263 PRINT_ATTRf(exclude_host, p_unsigned);
1264 PRINT_ATTRf(exclude_guest, p_unsigned);
1265 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1266 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1267 PRINT_ATTRf(mmap2, p_unsigned);
1268 PRINT_ATTRf(comm_exec, p_unsigned);
1269 PRINT_ATTRf(use_clockid, p_unsigned);
1270 PRINT_ATTRf(context_switch, p_unsigned);
1272 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1273 PRINT_ATTRf(bp_type, p_unsigned);
1274 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1275 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1276 PRINT_ATTRf(branch_sample_type, p_unsigned);
1277 PRINT_ATTRf(sample_regs_user, p_hex);
1278 PRINT_ATTRf(sample_stack_user, p_unsigned);
1279 PRINT_ATTRf(clockid, p_signed);
1280 PRINT_ATTRf(sample_regs_intr, p_hex);
1281 PRINT_ATTRf(aux_watermark, p_unsigned);
1286 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1287 void *priv __attribute__((unused)))
1289 return fprintf(fp, " %-32s %s\n", name, val);
1292 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1293 struct thread_map *threads)
1295 int cpu, thread, nthreads;
1296 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1298 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1300 if (evsel->system_wide)
1303 nthreads = threads->nr;
1305 if (evsel->fd == NULL &&
1306 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1310 flags |= PERF_FLAG_PID_CGROUP;
1311 pid = evsel->cgrp->fd;
1314 fallback_missing_features:
1315 if (perf_missing_features.clockid_wrong)
1316 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1317 if (perf_missing_features.clockid) {
1318 evsel->attr.use_clockid = 0;
1319 evsel->attr.clockid = 0;
1321 if (perf_missing_features.cloexec)
1322 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1323 if (perf_missing_features.mmap2)
1324 evsel->attr.mmap2 = 0;
1325 if (perf_missing_features.exclude_guest)
1326 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1328 if (perf_missing_features.sample_id_all)
1329 evsel->attr.sample_id_all = 0;
1332 fprintf(stderr, "%.60s\n", graph_dotted_line);
1333 fprintf(stderr, "perf_event_attr:\n");
1334 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1335 fprintf(stderr, "%.60s\n", graph_dotted_line);
1338 for (cpu = 0; cpu < cpus->nr; cpu++) {
1340 for (thread = 0; thread < nthreads; thread++) {
1343 if (!evsel->cgrp && !evsel->system_wide)
1344 pid = thread_map__pid(threads, thread);
1346 group_fd = get_group_fd(evsel, cpu, thread);
1348 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1349 pid, cpus->map[cpu], group_fd, flags);
1351 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1355 if (FD(evsel, cpu, thread) < 0) {
1357 pr_debug2("sys_perf_event_open failed, error %d\n",
1362 if (evsel->bpf_fd >= 0) {
1363 int evt_fd = FD(evsel, cpu, thread);
1364 int bpf_fd = evsel->bpf_fd;
1367 PERF_EVENT_IOC_SET_BPF,
1369 if (err && errno != EEXIST) {
1370 pr_err("failed to attach bpf fd %d: %s\n",
1371 bpf_fd, strerror(errno));
1377 set_rlimit = NO_CHANGE;
1380 * If we succeeded but had to kill clockid, fail and
1381 * have perf_evsel__open_strerror() print us a nice
1384 if (perf_missing_features.clockid ||
1385 perf_missing_features.clockid_wrong) {
1396 * perf stat needs between 5 and 22 fds per CPU. When we run out
1397 * of them try to increase the limits.
1399 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1401 int old_errno = errno;
1403 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1404 if (set_rlimit == NO_CHANGE)
1405 l.rlim_cur = l.rlim_max;
1407 l.rlim_cur = l.rlim_max + 1000;
1408 l.rlim_max = l.rlim_cur;
1410 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1419 if (err != -EINVAL || cpu > 0 || thread > 0)
1423 * Must probe features in the order they were added to the
1424 * perf_event_attr interface.
1426 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1427 perf_missing_features.clockid_wrong = true;
1428 goto fallback_missing_features;
1429 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1430 perf_missing_features.clockid = true;
1431 goto fallback_missing_features;
1432 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1433 perf_missing_features.cloexec = true;
1434 goto fallback_missing_features;
1435 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1436 perf_missing_features.mmap2 = true;
1437 goto fallback_missing_features;
1438 } else if (!perf_missing_features.exclude_guest &&
1439 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1440 perf_missing_features.exclude_guest = true;
1441 goto fallback_missing_features;
1442 } else if (!perf_missing_features.sample_id_all) {
1443 perf_missing_features.sample_id_all = true;
1444 goto retry_sample_id;
1449 while (--thread >= 0) {
1450 close(FD(evsel, cpu, thread));
1451 FD(evsel, cpu, thread) = -1;
1454 } while (--cpu >= 0);
1458 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1460 if (evsel->fd == NULL)
1463 perf_evsel__close_fd(evsel, ncpus, nthreads);
1464 perf_evsel__free_fd(evsel);
1476 struct thread_map map;
1478 } empty_thread_map = {
1483 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1484 struct thread_map *threads)
1487 /* Work around old compiler warnings about strict aliasing */
1488 cpus = &empty_cpu_map.map;
1491 if (threads == NULL)
1492 threads = &empty_thread_map.map;
1494 return __perf_evsel__open(evsel, cpus, threads);
1497 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1498 struct cpu_map *cpus)
1500 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1503 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1504 struct thread_map *threads)
1506 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1509 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1510 const union perf_event *event,
1511 struct perf_sample *sample)
1513 u64 type = evsel->attr.sample_type;
1514 const u64 *array = event->sample.array;
1515 bool swapped = evsel->needs_swap;
1518 array += ((event->header.size -
1519 sizeof(event->header)) / sizeof(u64)) - 1;
1521 if (type & PERF_SAMPLE_IDENTIFIER) {
1522 sample->id = *array;
1526 if (type & PERF_SAMPLE_CPU) {
1529 /* undo swap of u64, then swap on individual u32s */
1530 u.val64 = bswap_64(u.val64);
1531 u.val32[0] = bswap_32(u.val32[0]);
1534 sample->cpu = u.val32[0];
1538 if (type & PERF_SAMPLE_STREAM_ID) {
1539 sample->stream_id = *array;
1543 if (type & PERF_SAMPLE_ID) {
1544 sample->id = *array;
1548 if (type & PERF_SAMPLE_TIME) {
1549 sample->time = *array;
1553 if (type & PERF_SAMPLE_TID) {
1556 /* undo swap of u64, then swap on individual u32s */
1557 u.val64 = bswap_64(u.val64);
1558 u.val32[0] = bswap_32(u.val32[0]);
1559 u.val32[1] = bswap_32(u.val32[1]);
1562 sample->pid = u.val32[0];
1563 sample->tid = u.val32[1];
1570 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1573 return size > max_size || offset + size > endp;
1576 #define OVERFLOW_CHECK(offset, size, max_size) \
1578 if (overflow(endp, (max_size), (offset), (size))) \
1582 #define OVERFLOW_CHECK_u64(offset) \
1583 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1585 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1586 struct perf_sample *data)
1588 u64 type = evsel->attr.sample_type;
1589 bool swapped = evsel->needs_swap;
1591 u16 max_size = event->header.size;
1592 const void *endp = (void *)event + max_size;
1596 * used for cross-endian analysis. See git commit 65014ab3
1597 * for why this goofiness is needed.
1601 memset(data, 0, sizeof(*data));
1602 data->cpu = data->pid = data->tid = -1;
1603 data->stream_id = data->id = data->time = -1ULL;
1604 data->period = evsel->attr.sample_period;
1607 if (event->header.type != PERF_RECORD_SAMPLE) {
1608 if (!evsel->attr.sample_id_all)
1610 return perf_evsel__parse_id_sample(evsel, event, data);
1613 array = event->sample.array;
1616 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1617 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1618 * check the format does not go past the end of the event.
1620 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1624 if (type & PERF_SAMPLE_IDENTIFIER) {
1629 if (type & PERF_SAMPLE_IP) {
1634 if (type & PERF_SAMPLE_TID) {
1637 /* undo swap of u64, then swap on individual u32s */
1638 u.val64 = bswap_64(u.val64);
1639 u.val32[0] = bswap_32(u.val32[0]);
1640 u.val32[1] = bswap_32(u.val32[1]);
1643 data->pid = u.val32[0];
1644 data->tid = u.val32[1];
1648 if (type & PERF_SAMPLE_TIME) {
1649 data->time = *array;
1654 if (type & PERF_SAMPLE_ADDR) {
1655 data->addr = *array;
1659 if (type & PERF_SAMPLE_ID) {
1664 if (type & PERF_SAMPLE_STREAM_ID) {
1665 data->stream_id = *array;
1669 if (type & PERF_SAMPLE_CPU) {
1673 /* undo swap of u64, then swap on individual u32s */
1674 u.val64 = bswap_64(u.val64);
1675 u.val32[0] = bswap_32(u.val32[0]);
1678 data->cpu = u.val32[0];
1682 if (type & PERF_SAMPLE_PERIOD) {
1683 data->period = *array;
1687 if (type & PERF_SAMPLE_READ) {
1688 u64 read_format = evsel->attr.read_format;
1690 OVERFLOW_CHECK_u64(array);
1691 if (read_format & PERF_FORMAT_GROUP)
1692 data->read.group.nr = *array;
1694 data->read.one.value = *array;
1698 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1699 OVERFLOW_CHECK_u64(array);
1700 data->read.time_enabled = *array;
1704 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1705 OVERFLOW_CHECK_u64(array);
1706 data->read.time_running = *array;
1710 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1711 if (read_format & PERF_FORMAT_GROUP) {
1712 const u64 max_group_nr = UINT64_MAX /
1713 sizeof(struct sample_read_value);
1715 if (data->read.group.nr > max_group_nr)
1717 sz = data->read.group.nr *
1718 sizeof(struct sample_read_value);
1719 OVERFLOW_CHECK(array, sz, max_size);
1720 data->read.group.values =
1721 (struct sample_read_value *)array;
1722 array = (void *)array + sz;
1724 OVERFLOW_CHECK_u64(array);
1725 data->read.one.id = *array;
1730 if (type & PERF_SAMPLE_CALLCHAIN) {
1731 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1733 OVERFLOW_CHECK_u64(array);
1734 data->callchain = (struct ip_callchain *)array++;
1735 if (data->callchain->nr > max_callchain_nr)
1737 sz = data->callchain->nr * sizeof(u64);
1738 OVERFLOW_CHECK(array, sz, max_size);
1739 array = (void *)array + sz;
1742 if (type & PERF_SAMPLE_RAW) {
1743 OVERFLOW_CHECK_u64(array);
1745 if (WARN_ONCE(swapped,
1746 "Endianness of raw data not corrected!\n")) {
1747 /* undo swap of u64, then swap on individual u32s */
1748 u.val64 = bswap_64(u.val64);
1749 u.val32[0] = bswap_32(u.val32[0]);
1750 u.val32[1] = bswap_32(u.val32[1]);
1752 data->raw_size = u.val32[0];
1753 array = (void *)array + sizeof(u32);
1755 OVERFLOW_CHECK(array, data->raw_size, max_size);
1756 data->raw_data = (void *)array;
1757 array = (void *)array + data->raw_size;
1760 if (type & PERF_SAMPLE_BRANCH_STACK) {
1761 const u64 max_branch_nr = UINT64_MAX /
1762 sizeof(struct branch_entry);
1764 OVERFLOW_CHECK_u64(array);
1765 data->branch_stack = (struct branch_stack *)array++;
1767 if (data->branch_stack->nr > max_branch_nr)
1769 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1770 OVERFLOW_CHECK(array, sz, max_size);
1771 array = (void *)array + sz;
1774 if (type & PERF_SAMPLE_REGS_USER) {
1775 OVERFLOW_CHECK_u64(array);
1776 data->user_regs.abi = *array;
1779 if (data->user_regs.abi) {
1780 u64 mask = evsel->attr.sample_regs_user;
1782 sz = hweight_long(mask) * sizeof(u64);
1783 OVERFLOW_CHECK(array, sz, max_size);
1784 data->user_regs.mask = mask;
1785 data->user_regs.regs = (u64 *)array;
1786 array = (void *)array + sz;
1790 if (type & PERF_SAMPLE_STACK_USER) {
1791 OVERFLOW_CHECK_u64(array);
1794 data->user_stack.offset = ((char *)(array - 1)
1798 data->user_stack.size = 0;
1800 OVERFLOW_CHECK(array, sz, max_size);
1801 data->user_stack.data = (char *)array;
1802 array = (void *)array + sz;
1803 OVERFLOW_CHECK_u64(array);
1804 data->user_stack.size = *array++;
1805 if (WARN_ONCE(data->user_stack.size > sz,
1806 "user stack dump failure\n"))
1812 if (type & PERF_SAMPLE_WEIGHT) {
1813 OVERFLOW_CHECK_u64(array);
1814 data->weight = *array;
1818 data->data_src = PERF_MEM_DATA_SRC_NONE;
1819 if (type & PERF_SAMPLE_DATA_SRC) {
1820 OVERFLOW_CHECK_u64(array);
1821 data->data_src = *array;
1825 data->transaction = 0;
1826 if (type & PERF_SAMPLE_TRANSACTION) {
1827 OVERFLOW_CHECK_u64(array);
1828 data->transaction = *array;
1832 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1833 if (type & PERF_SAMPLE_REGS_INTR) {
1834 OVERFLOW_CHECK_u64(array);
1835 data->intr_regs.abi = *array;
1838 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1839 u64 mask = evsel->attr.sample_regs_intr;
1841 sz = hweight_long(mask) * sizeof(u64);
1842 OVERFLOW_CHECK(array, sz, max_size);
1843 data->intr_regs.mask = mask;
1844 data->intr_regs.regs = (u64 *)array;
1845 array = (void *)array + sz;
1852 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1855 size_t sz, result = sizeof(struct sample_event);
1857 if (type & PERF_SAMPLE_IDENTIFIER)
1858 result += sizeof(u64);
1860 if (type & PERF_SAMPLE_IP)
1861 result += sizeof(u64);
1863 if (type & PERF_SAMPLE_TID)
1864 result += sizeof(u64);
1866 if (type & PERF_SAMPLE_TIME)
1867 result += sizeof(u64);
1869 if (type & PERF_SAMPLE_ADDR)
1870 result += sizeof(u64);
1872 if (type & PERF_SAMPLE_ID)
1873 result += sizeof(u64);
1875 if (type & PERF_SAMPLE_STREAM_ID)
1876 result += sizeof(u64);
1878 if (type & PERF_SAMPLE_CPU)
1879 result += sizeof(u64);
1881 if (type & PERF_SAMPLE_PERIOD)
1882 result += sizeof(u64);
1884 if (type & PERF_SAMPLE_READ) {
1885 result += sizeof(u64);
1886 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1887 result += sizeof(u64);
1888 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1889 result += sizeof(u64);
1890 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1891 if (read_format & PERF_FORMAT_GROUP) {
1892 sz = sample->read.group.nr *
1893 sizeof(struct sample_read_value);
1896 result += sizeof(u64);
1900 if (type & PERF_SAMPLE_CALLCHAIN) {
1901 sz = (sample->callchain->nr + 1) * sizeof(u64);
1905 if (type & PERF_SAMPLE_RAW) {
1906 result += sizeof(u32);
1907 result += sample->raw_size;
1910 if (type & PERF_SAMPLE_BRANCH_STACK) {
1911 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1916 if (type & PERF_SAMPLE_REGS_USER) {
1917 if (sample->user_regs.abi) {
1918 result += sizeof(u64);
1919 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1922 result += sizeof(u64);
1926 if (type & PERF_SAMPLE_STACK_USER) {
1927 sz = sample->user_stack.size;
1928 result += sizeof(u64);
1931 result += sizeof(u64);
1935 if (type & PERF_SAMPLE_WEIGHT)
1936 result += sizeof(u64);
1938 if (type & PERF_SAMPLE_DATA_SRC)
1939 result += sizeof(u64);
1941 if (type & PERF_SAMPLE_TRANSACTION)
1942 result += sizeof(u64);
1944 if (type & PERF_SAMPLE_REGS_INTR) {
1945 if (sample->intr_regs.abi) {
1946 result += sizeof(u64);
1947 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1950 result += sizeof(u64);
1957 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1959 const struct perf_sample *sample,
1965 * used for cross-endian analysis. See git commit 65014ab3
1966 * for why this goofiness is needed.
1970 array = event->sample.array;
1972 if (type & PERF_SAMPLE_IDENTIFIER) {
1973 *array = sample->id;
1977 if (type & PERF_SAMPLE_IP) {
1978 *array = sample->ip;
1982 if (type & PERF_SAMPLE_TID) {
1983 u.val32[0] = sample->pid;
1984 u.val32[1] = sample->tid;
1987 * Inverse of what is done in perf_evsel__parse_sample
1989 u.val32[0] = bswap_32(u.val32[0]);
1990 u.val32[1] = bswap_32(u.val32[1]);
1991 u.val64 = bswap_64(u.val64);
1998 if (type & PERF_SAMPLE_TIME) {
1999 *array = sample->time;
2003 if (type & PERF_SAMPLE_ADDR) {
2004 *array = sample->addr;
2008 if (type & PERF_SAMPLE_ID) {
2009 *array = sample->id;
2013 if (type & PERF_SAMPLE_STREAM_ID) {
2014 *array = sample->stream_id;
2018 if (type & PERF_SAMPLE_CPU) {
2019 u.val32[0] = sample->cpu;
2022 * Inverse of what is done in perf_evsel__parse_sample
2024 u.val32[0] = bswap_32(u.val32[0]);
2025 u.val64 = bswap_64(u.val64);
2031 if (type & PERF_SAMPLE_PERIOD) {
2032 *array = sample->period;
2036 if (type & PERF_SAMPLE_READ) {
2037 if (read_format & PERF_FORMAT_GROUP)
2038 *array = sample->read.group.nr;
2040 *array = sample->read.one.value;
2043 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2044 *array = sample->read.time_enabled;
2048 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2049 *array = sample->read.time_running;
2053 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2054 if (read_format & PERF_FORMAT_GROUP) {
2055 sz = sample->read.group.nr *
2056 sizeof(struct sample_read_value);
2057 memcpy(array, sample->read.group.values, sz);
2058 array = (void *)array + sz;
2060 *array = sample->read.one.id;
2065 if (type & PERF_SAMPLE_CALLCHAIN) {
2066 sz = (sample->callchain->nr + 1) * sizeof(u64);
2067 memcpy(array, sample->callchain, sz);
2068 array = (void *)array + sz;
2071 if (type & PERF_SAMPLE_RAW) {
2072 u.val32[0] = sample->raw_size;
2073 if (WARN_ONCE(swapped,
2074 "Endianness of raw data not corrected!\n")) {
2076 * Inverse of what is done in perf_evsel__parse_sample
2078 u.val32[0] = bswap_32(u.val32[0]);
2079 u.val32[1] = bswap_32(u.val32[1]);
2080 u.val64 = bswap_64(u.val64);
2083 array = (void *)array + sizeof(u32);
2085 memcpy(array, sample->raw_data, sample->raw_size);
2086 array = (void *)array + sample->raw_size;
2089 if (type & PERF_SAMPLE_BRANCH_STACK) {
2090 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2092 memcpy(array, sample->branch_stack, sz);
2093 array = (void *)array + sz;
2096 if (type & PERF_SAMPLE_REGS_USER) {
2097 if (sample->user_regs.abi) {
2098 *array++ = sample->user_regs.abi;
2099 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2100 memcpy(array, sample->user_regs.regs, sz);
2101 array = (void *)array + sz;
2107 if (type & PERF_SAMPLE_STACK_USER) {
2108 sz = sample->user_stack.size;
2111 memcpy(array, sample->user_stack.data, sz);
2112 array = (void *)array + sz;
2117 if (type & PERF_SAMPLE_WEIGHT) {
2118 *array = sample->weight;
2122 if (type & PERF_SAMPLE_DATA_SRC) {
2123 *array = sample->data_src;
2127 if (type & PERF_SAMPLE_TRANSACTION) {
2128 *array = sample->transaction;
2132 if (type & PERF_SAMPLE_REGS_INTR) {
2133 if (sample->intr_regs.abi) {
2134 *array++ = sample->intr_regs.abi;
2135 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2136 memcpy(array, sample->intr_regs.regs, sz);
2137 array = (void *)array + sz;
2146 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2148 return pevent_find_field(evsel->tp_format, name);
2151 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2154 struct format_field *field = perf_evsel__field(evsel, name);
2160 offset = field->offset;
2162 if (field->flags & FIELD_IS_DYNAMIC) {
2163 offset = *(int *)(sample->raw_data + field->offset);
2167 return sample->raw_data + offset;
2170 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2173 struct format_field *field = perf_evsel__field(evsel, name);
2180 ptr = sample->raw_data + field->offset;
2182 switch (field->size) {
2186 value = *(u16 *)ptr;
2189 value = *(u32 *)ptr;
2192 memcpy(&value, ptr, sizeof(u64));
2198 if (!evsel->needs_swap)
2201 switch (field->size) {
2203 return bswap_16(value);
2205 return bswap_32(value);
2207 return bswap_64(value);
2215 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2221 ret += fprintf(fp, ",");
2223 ret += fprintf(fp, ":");
2227 va_start(args, fmt);
2228 ret += vfprintf(fp, fmt, args);
2233 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2235 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2238 int perf_evsel__fprintf(struct perf_evsel *evsel,
2239 struct perf_attr_details *details, FILE *fp)
2244 if (details->event_group) {
2245 struct perf_evsel *pos;
2247 if (!perf_evsel__is_group_leader(evsel))
2250 if (evsel->nr_members > 1)
2251 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2253 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2254 for_each_group_member(pos, evsel)
2255 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2257 if (evsel->nr_members > 1)
2258 printed += fprintf(fp, "}");
2262 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2264 if (details->verbose) {
2265 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2266 __print_attr__fprintf, &first);
2267 } else if (details->freq) {
2268 const char *term = "sample_freq";
2270 if (!evsel->attr.freq)
2271 term = "sample_period";
2273 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2274 term, (u64)evsel->attr.sample_freq);
2281 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2282 char *msg, size_t msgsize)
2284 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2285 evsel->attr.type == PERF_TYPE_HARDWARE &&
2286 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2288 * If it's cycles then fall back to hrtimer based
2289 * cpu-clock-tick sw counter, which is always available even if
2292 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2295 scnprintf(msg, msgsize, "%s",
2296 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2298 evsel->attr.type = PERF_TYPE_SOFTWARE;
2299 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2301 zfree(&evsel->name);
2308 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2309 int err, char *msg, size_t size)
2311 char sbuf[STRERR_BUFSIZE];
2316 return scnprintf(msg, size,
2317 "You may not have permission to collect %sstats.\n"
2318 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2319 " -1 - Not paranoid at all\n"
2320 " 0 - Disallow raw tracepoint access for unpriv\n"
2321 " 1 - Disallow cpu events for unpriv\n"
2322 " 2 - Disallow kernel profiling for unpriv",
2323 target->system_wide ? "system-wide " : "");
2325 return scnprintf(msg, size, "The %s event is not supported.",
2326 perf_evsel__name(evsel));
2328 return scnprintf(msg, size, "%s",
2329 "Too many events are opened.\n"
2330 "Probably the maximum number of open file descriptors has been reached.\n"
2331 "Hint: Try again after reducing the number of events.\n"
2332 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2334 if (target->cpu_list)
2335 return scnprintf(msg, size, "%s",
2336 "No such device - did you specify an out-of-range profile CPU?\n");
2339 if (evsel->attr.precise_ip)
2340 return scnprintf(msg, size, "%s",
2341 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2342 #if defined(__i386__) || defined(__x86_64__)
2343 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2344 return scnprintf(msg, size, "%s",
2345 "No hardware sampling interrupt available.\n"
2346 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2350 if (find_process("oprofiled"))
2351 return scnprintf(msg, size,
2352 "The PMU counters are busy/taken by another profiler.\n"
2353 "We found oprofile daemon running, please stop it and try again.");
2356 if (perf_missing_features.clockid)
2357 return scnprintf(msg, size, "clockid feature not supported.");
2358 if (perf_missing_features.clockid_wrong)
2359 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2365 return scnprintf(msg, size,
2366 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2367 "/bin/dmesg may provide additional information.\n"
2368 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2369 err, strerror_r(err, sbuf, sizeof(sbuf)),
2370 perf_evsel__name(evsel));