bcma: claim only 14e4:4365 PCI Dell card with SoftMAC BCM43142
[linux/fpc-iii.git] / tools / perf / util / evsel.c
blobcdbaf9b51e428ad4537a38ded24ee07bec54e90c
1 /*
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
5 * copyright notes.
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
10 #include <byteswap.h>
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>
18 #include "asm/bug.h"
19 #include "callchain.h"
20 #include "cgroup.h"
21 #include "evsel.h"
22 #include "evlist.h"
23 #include "util.h"
24 #include "cpumap.h"
25 #include "thread_map.h"
26 #include "target.h"
27 #include "perf_regs.h"
28 #include "debug.h"
29 #include "trace-event.h"
30 #include "stat.h"
32 static struct {
33 bool sample_id_all;
34 bool exclude_guest;
35 bool mmap2;
36 bool cloexec;
37 bool clockid;
38 bool clockid_wrong;
39 bool lbr_flags;
40 } perf_missing_features;
42 static clockid_t clockid;
44 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
46 return 0;
49 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
53 static struct {
54 size_t size;
55 int (*init)(struct perf_evsel *evsel);
56 void (*fini)(struct perf_evsel *evsel);
57 } perf_evsel__object = {
58 .size = sizeof(struct perf_evsel),
59 .init = perf_evsel__no_extra_init,
60 .fini = perf_evsel__no_extra_fini,
63 int perf_evsel__object_config(size_t object_size,
64 int (*init)(struct perf_evsel *evsel),
65 void (*fini)(struct perf_evsel *evsel))
68 if (object_size == 0)
69 goto set_methods;
71 if (perf_evsel__object.size > object_size)
72 return -EINVAL;
74 perf_evsel__object.size = object_size;
76 set_methods:
77 if (init != NULL)
78 perf_evsel__object.init = init;
80 if (fini != NULL)
81 perf_evsel__object.fini = fini;
83 return 0;
86 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
88 int __perf_evsel__sample_size(u64 sample_type)
90 u64 mask = sample_type & PERF_SAMPLE_MASK;
91 int size = 0;
92 int i;
94 for (i = 0; i < 64; i++) {
95 if (mask & (1ULL << i))
96 size++;
99 size *= sizeof(u64);
101 return size;
105 * __perf_evsel__calc_id_pos - calculate id_pos.
106 * @sample_type: sample type
108 * This function returns the position of the event id (PERF_SAMPLE_ID or
109 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
110 * sample_event.
112 static int __perf_evsel__calc_id_pos(u64 sample_type)
114 int idx = 0;
116 if (sample_type & PERF_SAMPLE_IDENTIFIER)
117 return 0;
119 if (!(sample_type & PERF_SAMPLE_ID))
120 return -1;
122 if (sample_type & PERF_SAMPLE_IP)
123 idx += 1;
125 if (sample_type & PERF_SAMPLE_TID)
126 idx += 1;
128 if (sample_type & PERF_SAMPLE_TIME)
129 idx += 1;
131 if (sample_type & PERF_SAMPLE_ADDR)
132 idx += 1;
134 return idx;
138 * __perf_evsel__calc_is_pos - calculate is_pos.
139 * @sample_type: sample type
141 * This function returns the position (counting backwards) of the event id
142 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
143 * sample_id_all is used there is an id sample appended to non-sample events.
145 static int __perf_evsel__calc_is_pos(u64 sample_type)
147 int idx = 1;
149 if (sample_type & PERF_SAMPLE_IDENTIFIER)
150 return 1;
152 if (!(sample_type & PERF_SAMPLE_ID))
153 return -1;
155 if (sample_type & PERF_SAMPLE_CPU)
156 idx += 1;
158 if (sample_type & PERF_SAMPLE_STREAM_ID)
159 idx += 1;
161 return idx;
164 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
166 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
167 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
170 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
171 enum perf_event_sample_format bit)
173 if (!(evsel->attr.sample_type & bit)) {
174 evsel->attr.sample_type |= bit;
175 evsel->sample_size += sizeof(u64);
176 perf_evsel__calc_id_pos(evsel);
180 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
181 enum perf_event_sample_format bit)
183 if (evsel->attr.sample_type & bit) {
184 evsel->attr.sample_type &= ~bit;
185 evsel->sample_size -= sizeof(u64);
186 perf_evsel__calc_id_pos(evsel);
190 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
191 bool can_sample_identifier)
193 if (can_sample_identifier) {
194 perf_evsel__reset_sample_bit(evsel, ID);
195 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
196 } else {
197 perf_evsel__set_sample_bit(evsel, ID);
199 evsel->attr.read_format |= PERF_FORMAT_ID;
202 void perf_evsel__init(struct perf_evsel *evsel,
203 struct perf_event_attr *attr, int idx)
205 evsel->idx = idx;
206 evsel->tracking = !idx;
207 evsel->attr = *attr;
208 evsel->leader = evsel;
209 evsel->unit = "";
210 evsel->scale = 1.0;
211 evsel->evlist = NULL;
212 evsel->bpf_fd = -1;
213 INIT_LIST_HEAD(&evsel->node);
214 INIT_LIST_HEAD(&evsel->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);
225 if (evsel != NULL)
226 perf_evsel__init(evsel, attr, idx);
228 return evsel;
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);
237 int err = -ENOMEM;
239 if (evsel == NULL) {
240 goto out_err;
241 } else {
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)
249 goto out_free;
251 evsel->tp_format = trace_event__tp_format(sys, name);
252 if (IS_ERR(evsel->tp_format)) {
253 err = PTR_ERR(evsel->tp_format);
254 goto out_free;
257 event_attr_init(&attr);
258 attr.config = evsel->tp_format->id;
259 attr.sample_period = 1;
260 perf_evsel__init(evsel, &attr, idx);
263 return evsel;
265 out_free:
266 zfree(&evsel->name);
267 free(evsel);
268 out_err:
269 return ERR_PTR(err);
272 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
273 "cycles",
274 "instructions",
275 "cache-references",
276 "cache-misses",
277 "branches",
278 "branch-misses",
279 "bus-cycles",
280 "stalled-cycles-frontend",
281 "stalled-cycles-backend",
282 "ref-cycles",
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); \
303 } } while(0)
305 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
306 MOD_PRINT(kernel, 'k');
307 MOD_PRINT(user, 'u');
308 MOD_PRINT(hv, 'h');
309 exclude_guest_default = true;
312 if (attr->precise_ip) {
313 if (!colon)
314 colon = ++r;
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');
323 #undef MOD_PRINT
324 if (colon)
325 bf[colon - 1] = ':';
326 return r;
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] = {
336 "cpu-clock",
337 "task-clock",
338 "page-faults",
339 "context-switches",
340 "cpu-migrations",
341 "minor-faults",
342 "major-faults",
343 "alignment-faults",
344 "emulation-faults",
345 "dummy",
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)
363 int r;
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");
376 return r;
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", },
390 { "LLC", "L2", },
391 { "dTLB", "d-tlb", "Data-TLB", },
392 { "iTLB", "i-tlb", "Instruction-TLB", },
393 { "branch", "branches", "bpu", "btb", "bpc", },
394 { "node", },
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 */
435 else
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)
442 if (result) {
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)
458 goto out_err;
460 op = (config >> 8) & 0xff;
461 err = "unknown-ext-hardware-cache-op";
462 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
463 goto out_err;
465 result = (config >> 16) & 0xff;
466 err = "unknown-ext-hardware-cache-result";
467 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
468 goto out_err;
470 err = "invalid-cache";
471 if (!perf_evsel__is_cache_op_valid(type, op))
472 goto out_err;
474 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
475 out_err:
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)
493 char bf[128];
495 if (evsel->name)
496 return evsel->name;
498 switch (evsel->attr.type) {
499 case PERF_TYPE_RAW:
500 perf_evsel__raw_name(evsel, bf, sizeof(bf));
501 break;
503 case PERF_TYPE_HARDWARE:
504 perf_evsel__hw_name(evsel, bf, sizeof(bf));
505 break;
507 case PERF_TYPE_HW_CACHE:
508 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
509 break;
511 case PERF_TYPE_SOFTWARE:
512 perf_evsel__sw_name(evsel, bf, sizeof(bf));
513 break;
515 case PERF_TYPE_TRACEPOINT:
516 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
517 break;
519 case PERF_TYPE_BREAKPOINT:
520 perf_evsel__bp_name(evsel, bf, sizeof(bf));
521 break;
523 default:
524 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
525 evsel->attr.type);
526 break;
529 evsel->name = strdup(bf);
531 return evsel->name ?: "unknown";
534 const char *perf_evsel__group_name(struct perf_evsel *evsel)
536 return evsel->group_name ?: "anon group";
539 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
541 int ret;
542 struct perf_evsel *pos;
543 const char *group_name = perf_evsel__group_name(evsel);
545 ret = scnprintf(buf, size, "%s", group_name);
547 ret += scnprintf(buf + ret, size - ret, " { %s",
548 perf_evsel__name(evsel));
550 for_each_group_member(pos, evsel)
551 ret += scnprintf(buf + ret, size - ret, ", %s",
552 perf_evsel__name(pos));
554 ret += scnprintf(buf + ret, size - ret, " }");
556 return ret;
559 static void
560 perf_evsel__config_callgraph(struct perf_evsel *evsel,
561 struct record_opts *opts,
562 struct callchain_param *param)
564 bool function = perf_evsel__is_function_event(evsel);
565 struct perf_event_attr *attr = &evsel->attr;
567 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
569 if (param->record_mode == CALLCHAIN_LBR) {
570 if (!opts->branch_stack) {
571 if (attr->exclude_user) {
572 pr_warning("LBR callstack option is only available "
573 "to get user callchain information. "
574 "Falling back to framepointers.\n");
575 } else {
576 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
577 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
578 PERF_SAMPLE_BRANCH_CALL_STACK |
579 PERF_SAMPLE_BRANCH_NO_CYCLES |
580 PERF_SAMPLE_BRANCH_NO_FLAGS;
582 } else
583 pr_warning("Cannot use LBR callstack with branch stack. "
584 "Falling back to framepointers.\n");
587 if (param->record_mode == CALLCHAIN_DWARF) {
588 if (!function) {
589 perf_evsel__set_sample_bit(evsel, REGS_USER);
590 perf_evsel__set_sample_bit(evsel, STACK_USER);
591 attr->sample_regs_user = PERF_REGS_MASK;
592 attr->sample_stack_user = param->dump_size;
593 attr->exclude_callchain_user = 1;
594 } else {
595 pr_info("Cannot use DWARF unwind for function trace event,"
596 " falling back to framepointers.\n");
600 if (function) {
601 pr_info("Disabling user space callchains for function trace event.\n");
602 attr->exclude_callchain_user = 1;
606 static void
607 perf_evsel__reset_callgraph(struct perf_evsel *evsel,
608 struct callchain_param *param)
610 struct perf_event_attr *attr = &evsel->attr;
612 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
613 if (param->record_mode == CALLCHAIN_LBR) {
614 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
615 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
616 PERF_SAMPLE_BRANCH_CALL_STACK);
618 if (param->record_mode == CALLCHAIN_DWARF) {
619 perf_evsel__reset_sample_bit(evsel, REGS_USER);
620 perf_evsel__reset_sample_bit(evsel, STACK_USER);
624 static void apply_config_terms(struct perf_evsel *evsel,
625 struct record_opts *opts)
627 struct perf_evsel_config_term *term;
628 struct list_head *config_terms = &evsel->config_terms;
629 struct perf_event_attr *attr = &evsel->attr;
630 struct callchain_param param;
631 u32 dump_size = 0;
632 char *callgraph_buf = NULL;
634 /* callgraph default */
635 param.record_mode = callchain_param.record_mode;
637 list_for_each_entry(term, config_terms, list) {
638 switch (term->type) {
639 case PERF_EVSEL__CONFIG_TERM_PERIOD:
640 attr->sample_period = term->val.period;
641 attr->freq = 0;
642 break;
643 case PERF_EVSEL__CONFIG_TERM_FREQ:
644 attr->sample_freq = term->val.freq;
645 attr->freq = 1;
646 break;
647 case PERF_EVSEL__CONFIG_TERM_TIME:
648 if (term->val.time)
649 perf_evsel__set_sample_bit(evsel, TIME);
650 else
651 perf_evsel__reset_sample_bit(evsel, TIME);
652 break;
653 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
654 callgraph_buf = term->val.callgraph;
655 break;
656 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
657 dump_size = term->val.stack_user;
658 break;
659 case PERF_EVSEL__CONFIG_TERM_INHERIT:
661 * attr->inherit should has already been set by
662 * perf_evsel__config. If user explicitly set
663 * inherit using config terms, override global
664 * opt->no_inherit setting.
666 attr->inherit = term->val.inherit ? 1 : 0;
667 break;
668 default:
669 break;
673 /* User explicitly set per-event callgraph, clear the old setting and reset. */
674 if ((callgraph_buf != NULL) || (dump_size > 0)) {
676 /* parse callgraph parameters */
677 if (callgraph_buf != NULL) {
678 if (!strcmp(callgraph_buf, "no")) {
679 param.enabled = false;
680 param.record_mode = CALLCHAIN_NONE;
681 } else {
682 param.enabled = true;
683 if (parse_callchain_record(callgraph_buf, &param)) {
684 pr_err("per-event callgraph setting for %s failed. "
685 "Apply callgraph global setting for it\n",
686 evsel->name);
687 return;
691 if (dump_size > 0) {
692 dump_size = round_up(dump_size, sizeof(u64));
693 param.dump_size = dump_size;
696 /* If global callgraph set, clear it */
697 if (callchain_param.enabled)
698 perf_evsel__reset_callgraph(evsel, &callchain_param);
700 /* set perf-event callgraph */
701 if (param.enabled)
702 perf_evsel__config_callgraph(evsel, opts, &param);
707 * The enable_on_exec/disabled value strategy:
709 * 1) For any type of traced program:
710 * - all independent events and group leaders are disabled
711 * - all group members are enabled
713 * Group members are ruled by group leaders. They need to
714 * be enabled, because the group scheduling relies on that.
716 * 2) For traced programs executed by perf:
717 * - all independent events and group leaders have
718 * enable_on_exec set
719 * - we don't specifically enable or disable any event during
720 * the record command
722 * Independent events and group leaders are initially disabled
723 * and get enabled by exec. Group members are ruled by group
724 * leaders as stated in 1).
726 * 3) For traced programs attached by perf (pid/tid):
727 * - we specifically enable or disable all events during
728 * the record command
730 * When attaching events to already running traced we
731 * enable/disable events specifically, as there's no
732 * initial traced exec call.
734 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
736 struct perf_evsel *leader = evsel->leader;
737 struct perf_event_attr *attr = &evsel->attr;
738 int track = evsel->tracking;
739 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
741 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
742 attr->inherit = !opts->no_inherit;
744 perf_evsel__set_sample_bit(evsel, IP);
745 perf_evsel__set_sample_bit(evsel, TID);
747 if (evsel->sample_read) {
748 perf_evsel__set_sample_bit(evsel, READ);
751 * We need ID even in case of single event, because
752 * PERF_SAMPLE_READ process ID specific data.
754 perf_evsel__set_sample_id(evsel, false);
757 * Apply group format only if we belong to group
758 * with more than one members.
760 if (leader->nr_members > 1) {
761 attr->read_format |= PERF_FORMAT_GROUP;
762 attr->inherit = 0;
767 * We default some events to have a default interval. But keep
768 * it a weak assumption overridable by the user.
770 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
771 opts->user_interval != ULLONG_MAX)) {
772 if (opts->freq) {
773 perf_evsel__set_sample_bit(evsel, PERIOD);
774 attr->freq = 1;
775 attr->sample_freq = opts->freq;
776 } else {
777 attr->sample_period = opts->default_interval;
782 * Disable sampling for all group members other
783 * than leader in case leader 'leads' the sampling.
785 if ((leader != evsel) && leader->sample_read) {
786 attr->sample_freq = 0;
787 attr->sample_period = 0;
790 if (opts->no_samples)
791 attr->sample_freq = 0;
793 if (opts->inherit_stat)
794 attr->inherit_stat = 1;
796 if (opts->sample_address) {
797 perf_evsel__set_sample_bit(evsel, ADDR);
798 attr->mmap_data = track;
802 * We don't allow user space callchains for function trace
803 * event, due to issues with page faults while tracing page
804 * fault handler and its overall trickiness nature.
806 if (perf_evsel__is_function_event(evsel))
807 evsel->attr.exclude_callchain_user = 1;
809 if (callchain_param.enabled && !evsel->no_aux_samples)
810 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
812 if (opts->sample_intr_regs) {
813 attr->sample_regs_intr = opts->sample_intr_regs;
814 perf_evsel__set_sample_bit(evsel, REGS_INTR);
817 if (target__has_cpu(&opts->target))
818 perf_evsel__set_sample_bit(evsel, CPU);
820 if (opts->period)
821 perf_evsel__set_sample_bit(evsel, PERIOD);
824 * When the user explicitely disabled time don't force it here.
826 if (opts->sample_time &&
827 (!perf_missing_features.sample_id_all &&
828 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
829 opts->sample_time_set)))
830 perf_evsel__set_sample_bit(evsel, TIME);
832 if (opts->raw_samples && !evsel->no_aux_samples) {
833 perf_evsel__set_sample_bit(evsel, TIME);
834 perf_evsel__set_sample_bit(evsel, RAW);
835 perf_evsel__set_sample_bit(evsel, CPU);
838 if (opts->sample_address)
839 perf_evsel__set_sample_bit(evsel, DATA_SRC);
841 if (opts->no_buffering) {
842 attr->watermark = 0;
843 attr->wakeup_events = 1;
845 if (opts->branch_stack && !evsel->no_aux_samples) {
846 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
847 attr->branch_sample_type = opts->branch_stack;
850 if (opts->sample_weight)
851 perf_evsel__set_sample_bit(evsel, WEIGHT);
853 attr->task = track;
854 attr->mmap = track;
855 attr->mmap2 = track && !perf_missing_features.mmap2;
856 attr->comm = track;
858 if (opts->record_switch_events)
859 attr->context_switch = track;
861 if (opts->sample_transaction)
862 perf_evsel__set_sample_bit(evsel, TRANSACTION);
864 if (opts->running_time) {
865 evsel->attr.read_format |=
866 PERF_FORMAT_TOTAL_TIME_ENABLED |
867 PERF_FORMAT_TOTAL_TIME_RUNNING;
871 * XXX see the function comment above
873 * Disabling only independent events or group leaders,
874 * keeping group members enabled.
876 if (perf_evsel__is_group_leader(evsel))
877 attr->disabled = 1;
880 * Setting enable_on_exec for independent events and
881 * group leaders for traced executed by perf.
883 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
884 !opts->initial_delay)
885 attr->enable_on_exec = 1;
887 if (evsel->immediate) {
888 attr->disabled = 0;
889 attr->enable_on_exec = 0;
892 clockid = opts->clockid;
893 if (opts->use_clockid) {
894 attr->use_clockid = 1;
895 attr->clockid = opts->clockid;
898 if (evsel->precise_max)
899 perf_event_attr__set_max_precise_ip(attr);
902 * Apply event specific term settings,
903 * it overloads any global configuration.
905 apply_config_terms(evsel, opts);
908 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
910 int cpu, thread;
912 if (evsel->system_wide)
913 nthreads = 1;
915 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
917 if (evsel->fd) {
918 for (cpu = 0; cpu < ncpus; cpu++) {
919 for (thread = 0; thread < nthreads; thread++) {
920 FD(evsel, cpu, thread) = -1;
925 return evsel->fd != NULL ? 0 : -ENOMEM;
928 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
929 int ioc, void *arg)
931 int cpu, thread;
933 if (evsel->system_wide)
934 nthreads = 1;
936 for (cpu = 0; cpu < ncpus; cpu++) {
937 for (thread = 0; thread < nthreads; thread++) {
938 int fd = FD(evsel, cpu, thread),
939 err = ioctl(fd, ioc, arg);
941 if (err)
942 return err;
946 return 0;
949 int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
950 const char *filter)
952 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
953 PERF_EVENT_IOC_SET_FILTER,
954 (void *)filter);
957 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
959 char *new_filter = strdup(filter);
961 if (new_filter != NULL) {
962 free(evsel->filter);
963 evsel->filter = new_filter;
964 return 0;
967 return -1;
970 int perf_evsel__append_filter(struct perf_evsel *evsel,
971 const char *op, const char *filter)
973 char *new_filter;
975 if (evsel->filter == NULL)
976 return perf_evsel__set_filter(evsel, filter);
978 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
979 free(evsel->filter);
980 evsel->filter = new_filter;
981 return 0;
984 return -1;
987 int perf_evsel__enable(struct perf_evsel *evsel)
989 int nthreads = thread_map__nr(evsel->threads);
990 int ncpus = cpu_map__nr(evsel->cpus);
992 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
993 PERF_EVENT_IOC_ENABLE,
997 int perf_evsel__disable(struct perf_evsel *evsel)
999 int nthreads = thread_map__nr(evsel->threads);
1000 int ncpus = cpu_map__nr(evsel->cpus);
1002 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1003 PERF_EVENT_IOC_DISABLE,
1007 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1009 if (ncpus == 0 || nthreads == 0)
1010 return 0;
1012 if (evsel->system_wide)
1013 nthreads = 1;
1015 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1016 if (evsel->sample_id == NULL)
1017 return -ENOMEM;
1019 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1020 if (evsel->id == NULL) {
1021 xyarray__delete(evsel->sample_id);
1022 evsel->sample_id = NULL;
1023 return -ENOMEM;
1026 return 0;
1029 static void perf_evsel__free_fd(struct perf_evsel *evsel)
1031 xyarray__delete(evsel->fd);
1032 evsel->fd = NULL;
1035 static void perf_evsel__free_id(struct perf_evsel *evsel)
1037 xyarray__delete(evsel->sample_id);
1038 evsel->sample_id = NULL;
1039 zfree(&evsel->id);
1042 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1044 struct perf_evsel_config_term *term, *h;
1046 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1047 list_del(&term->list);
1048 free(term);
1052 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1054 int cpu, thread;
1056 if (evsel->system_wide)
1057 nthreads = 1;
1059 for (cpu = 0; cpu < ncpus; cpu++)
1060 for (thread = 0; thread < nthreads; ++thread) {
1061 close(FD(evsel, cpu, thread));
1062 FD(evsel, cpu, thread) = -1;
1066 void perf_evsel__exit(struct perf_evsel *evsel)
1068 assert(list_empty(&evsel->node));
1069 assert(evsel->evlist == NULL);
1070 perf_evsel__free_fd(evsel);
1071 perf_evsel__free_id(evsel);
1072 perf_evsel__free_config_terms(evsel);
1073 close_cgroup(evsel->cgrp);
1074 cpu_map__put(evsel->cpus);
1075 cpu_map__put(evsel->own_cpus);
1076 thread_map__put(evsel->threads);
1077 zfree(&evsel->group_name);
1078 zfree(&evsel->name);
1079 perf_evsel__object.fini(evsel);
1082 void perf_evsel__delete(struct perf_evsel *evsel)
1084 perf_evsel__exit(evsel);
1085 free(evsel);
1088 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1089 struct perf_counts_values *count)
1091 struct perf_counts_values tmp;
1093 if (!evsel->prev_raw_counts)
1094 return;
1096 if (cpu == -1) {
1097 tmp = evsel->prev_raw_counts->aggr;
1098 evsel->prev_raw_counts->aggr = *count;
1099 } else {
1100 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1101 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1104 count->val = count->val - tmp.val;
1105 count->ena = count->ena - tmp.ena;
1106 count->run = count->run - tmp.run;
1109 void perf_counts_values__scale(struct perf_counts_values *count,
1110 bool scale, s8 *pscaled)
1112 s8 scaled = 0;
1114 if (scale) {
1115 if (count->run == 0) {
1116 scaled = -1;
1117 count->val = 0;
1118 } else if (count->run < count->ena) {
1119 scaled = 1;
1120 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1122 } else
1123 count->ena = count->run = 0;
1125 if (pscaled)
1126 *pscaled = scaled;
1129 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1130 struct perf_counts_values *count)
1132 memset(count, 0, sizeof(*count));
1134 if (FD(evsel, cpu, thread) < 0)
1135 return -EINVAL;
1137 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1138 return -errno;
1140 return 0;
1143 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1144 int cpu, int thread, bool scale)
1146 struct perf_counts_values count;
1147 size_t nv = scale ? 3 : 1;
1149 if (FD(evsel, cpu, thread) < 0)
1150 return -EINVAL;
1152 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1153 return -ENOMEM;
1155 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1156 return -errno;
1158 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1159 perf_counts_values__scale(&count, scale, NULL);
1160 *perf_counts(evsel->counts, cpu, thread) = count;
1161 return 0;
1164 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1166 struct perf_evsel *leader = evsel->leader;
1167 int fd;
1169 if (perf_evsel__is_group_leader(evsel))
1170 return -1;
1173 * Leader must be already processed/open,
1174 * if not it's a bug.
1176 BUG_ON(!leader->fd);
1178 fd = FD(leader, cpu, thread);
1179 BUG_ON(fd == -1);
1181 return fd;
1184 struct bit_names {
1185 int bit;
1186 const char *name;
1189 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1191 bool first_bit = true;
1192 int i = 0;
1194 do {
1195 if (value & bits[i].bit) {
1196 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1197 first_bit = false;
1199 } while (bits[++i].name != NULL);
1202 static void __p_sample_type(char *buf, size_t size, u64 value)
1204 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1205 struct bit_names bits[] = {
1206 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1207 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1208 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1209 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1210 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1211 bit_name(WEIGHT),
1212 { .name = NULL, }
1214 #undef bit_name
1215 __p_bits(buf, size, value, bits);
1218 static void __p_read_format(char *buf, size_t size, u64 value)
1220 #define bit_name(n) { PERF_FORMAT_##n, #n }
1221 struct bit_names bits[] = {
1222 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1223 bit_name(ID), bit_name(GROUP),
1224 { .name = NULL, }
1226 #undef bit_name
1227 __p_bits(buf, size, value, bits);
1230 #define BUF_SIZE 1024
1232 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1233 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1234 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1235 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1236 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1238 #define PRINT_ATTRn(_n, _f, _p) \
1239 do { \
1240 if (attr->_f) { \
1241 _p(attr->_f); \
1242 ret += attr__fprintf(fp, _n, buf, priv);\
1244 } while (0)
1246 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1248 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1249 attr__fprintf_f attr__fprintf, void *priv)
1251 char buf[BUF_SIZE];
1252 int ret = 0;
1254 PRINT_ATTRf(type, p_unsigned);
1255 PRINT_ATTRf(size, p_unsigned);
1256 PRINT_ATTRf(config, p_hex);
1257 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1258 PRINT_ATTRf(sample_type, p_sample_type);
1259 PRINT_ATTRf(read_format, p_read_format);
1261 PRINT_ATTRf(disabled, p_unsigned);
1262 PRINT_ATTRf(inherit, p_unsigned);
1263 PRINT_ATTRf(pinned, p_unsigned);
1264 PRINT_ATTRf(exclusive, p_unsigned);
1265 PRINT_ATTRf(exclude_user, p_unsigned);
1266 PRINT_ATTRf(exclude_kernel, p_unsigned);
1267 PRINT_ATTRf(exclude_hv, p_unsigned);
1268 PRINT_ATTRf(exclude_idle, p_unsigned);
1269 PRINT_ATTRf(mmap, p_unsigned);
1270 PRINT_ATTRf(comm, p_unsigned);
1271 PRINT_ATTRf(freq, p_unsigned);
1272 PRINT_ATTRf(inherit_stat, p_unsigned);
1273 PRINT_ATTRf(enable_on_exec, p_unsigned);
1274 PRINT_ATTRf(task, p_unsigned);
1275 PRINT_ATTRf(watermark, p_unsigned);
1276 PRINT_ATTRf(precise_ip, p_unsigned);
1277 PRINT_ATTRf(mmap_data, p_unsigned);
1278 PRINT_ATTRf(sample_id_all, p_unsigned);
1279 PRINT_ATTRf(exclude_host, p_unsigned);
1280 PRINT_ATTRf(exclude_guest, p_unsigned);
1281 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1282 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1283 PRINT_ATTRf(mmap2, p_unsigned);
1284 PRINT_ATTRf(comm_exec, p_unsigned);
1285 PRINT_ATTRf(use_clockid, p_unsigned);
1286 PRINT_ATTRf(context_switch, p_unsigned);
1288 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1289 PRINT_ATTRf(bp_type, p_unsigned);
1290 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1291 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1292 PRINT_ATTRf(branch_sample_type, p_unsigned);
1293 PRINT_ATTRf(sample_regs_user, p_hex);
1294 PRINT_ATTRf(sample_stack_user, p_unsigned);
1295 PRINT_ATTRf(clockid, p_signed);
1296 PRINT_ATTRf(sample_regs_intr, p_hex);
1297 PRINT_ATTRf(aux_watermark, p_unsigned);
1299 return ret;
1302 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1303 void *priv __attribute__((unused)))
1305 return fprintf(fp, " %-32s %s\n", name, val);
1308 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1309 struct thread_map *threads)
1311 int cpu, thread, nthreads;
1312 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1313 int pid = -1, err;
1314 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1316 if (evsel->system_wide)
1317 nthreads = 1;
1318 else
1319 nthreads = threads->nr;
1321 if (evsel->fd == NULL &&
1322 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1323 return -ENOMEM;
1325 if (evsel->cgrp) {
1326 flags |= PERF_FLAG_PID_CGROUP;
1327 pid = evsel->cgrp->fd;
1330 fallback_missing_features:
1331 if (perf_missing_features.clockid_wrong)
1332 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1333 if (perf_missing_features.clockid) {
1334 evsel->attr.use_clockid = 0;
1335 evsel->attr.clockid = 0;
1337 if (perf_missing_features.cloexec)
1338 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1339 if (perf_missing_features.mmap2)
1340 evsel->attr.mmap2 = 0;
1341 if (perf_missing_features.exclude_guest)
1342 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1343 if (perf_missing_features.lbr_flags)
1344 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1345 PERF_SAMPLE_BRANCH_NO_CYCLES);
1346 retry_sample_id:
1347 if (perf_missing_features.sample_id_all)
1348 evsel->attr.sample_id_all = 0;
1350 if (verbose >= 2) {
1351 fprintf(stderr, "%.60s\n", graph_dotted_line);
1352 fprintf(stderr, "perf_event_attr:\n");
1353 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1354 fprintf(stderr, "%.60s\n", graph_dotted_line);
1357 for (cpu = 0; cpu < cpus->nr; cpu++) {
1359 for (thread = 0; thread < nthreads; thread++) {
1360 int group_fd;
1362 if (!evsel->cgrp && !evsel->system_wide)
1363 pid = thread_map__pid(threads, thread);
1365 group_fd = get_group_fd(evsel, cpu, thread);
1366 retry_open:
1367 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1368 pid, cpus->map[cpu], group_fd, flags);
1370 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1371 pid,
1372 cpus->map[cpu],
1373 group_fd, flags);
1374 if (FD(evsel, cpu, thread) < 0) {
1375 err = -errno;
1376 pr_debug2("sys_perf_event_open failed, error %d\n",
1377 err);
1378 goto try_fallback;
1381 if (evsel->bpf_fd >= 0) {
1382 int evt_fd = FD(evsel, cpu, thread);
1383 int bpf_fd = evsel->bpf_fd;
1385 err = ioctl(evt_fd,
1386 PERF_EVENT_IOC_SET_BPF,
1387 bpf_fd);
1388 if (err && errno != EEXIST) {
1389 pr_err("failed to attach bpf fd %d: %s\n",
1390 bpf_fd, strerror(errno));
1391 err = -EINVAL;
1392 goto out_close;
1396 set_rlimit = NO_CHANGE;
1399 * If we succeeded but had to kill clockid, fail and
1400 * have perf_evsel__open_strerror() print us a nice
1401 * error.
1403 if (perf_missing_features.clockid ||
1404 perf_missing_features.clockid_wrong) {
1405 err = -EINVAL;
1406 goto out_close;
1411 return 0;
1413 try_fallback:
1415 * perf stat needs between 5 and 22 fds per CPU. When we run out
1416 * of them try to increase the limits.
1418 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1419 struct rlimit l;
1420 int old_errno = errno;
1422 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1423 if (set_rlimit == NO_CHANGE)
1424 l.rlim_cur = l.rlim_max;
1425 else {
1426 l.rlim_cur = l.rlim_max + 1000;
1427 l.rlim_max = l.rlim_cur;
1429 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1430 set_rlimit++;
1431 errno = old_errno;
1432 goto retry_open;
1435 errno = old_errno;
1438 if (err != -EINVAL || cpu > 0 || thread > 0)
1439 goto out_close;
1442 * Must probe features in the order they were added to the
1443 * perf_event_attr interface.
1445 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1446 perf_missing_features.clockid_wrong = true;
1447 goto fallback_missing_features;
1448 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1449 perf_missing_features.clockid = true;
1450 goto fallback_missing_features;
1451 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1452 perf_missing_features.cloexec = true;
1453 goto fallback_missing_features;
1454 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1455 perf_missing_features.mmap2 = true;
1456 goto fallback_missing_features;
1457 } else if (!perf_missing_features.exclude_guest &&
1458 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1459 perf_missing_features.exclude_guest = true;
1460 goto fallback_missing_features;
1461 } else if (!perf_missing_features.sample_id_all) {
1462 perf_missing_features.sample_id_all = true;
1463 goto retry_sample_id;
1464 } else if (!perf_missing_features.lbr_flags &&
1465 (evsel->attr.branch_sample_type &
1466 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1467 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1468 perf_missing_features.lbr_flags = true;
1469 goto fallback_missing_features;
1472 out_close:
1473 do {
1474 while (--thread >= 0) {
1475 close(FD(evsel, cpu, thread));
1476 FD(evsel, cpu, thread) = -1;
1478 thread = nthreads;
1479 } while (--cpu >= 0);
1480 return err;
1483 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1485 if (evsel->fd == NULL)
1486 return;
1488 perf_evsel__close_fd(evsel, ncpus, nthreads);
1489 perf_evsel__free_fd(evsel);
1492 static struct {
1493 struct cpu_map map;
1494 int cpus[1];
1495 } empty_cpu_map = {
1496 .map.nr = 1,
1497 .cpus = { -1, },
1500 static struct {
1501 struct thread_map map;
1502 int threads[1];
1503 } empty_thread_map = {
1504 .map.nr = 1,
1505 .threads = { -1, },
1508 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1509 struct thread_map *threads)
1511 if (cpus == NULL) {
1512 /* Work around old compiler warnings about strict aliasing */
1513 cpus = &empty_cpu_map.map;
1516 if (threads == NULL)
1517 threads = &empty_thread_map.map;
1519 return __perf_evsel__open(evsel, cpus, threads);
1522 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1523 struct cpu_map *cpus)
1525 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1528 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1529 struct thread_map *threads)
1531 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1534 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1535 const union perf_event *event,
1536 struct perf_sample *sample)
1538 u64 type = evsel->attr.sample_type;
1539 const u64 *array = event->sample.array;
1540 bool swapped = evsel->needs_swap;
1541 union u64_swap u;
1543 array += ((event->header.size -
1544 sizeof(event->header)) / sizeof(u64)) - 1;
1546 if (type & PERF_SAMPLE_IDENTIFIER) {
1547 sample->id = *array;
1548 array--;
1551 if (type & PERF_SAMPLE_CPU) {
1552 u.val64 = *array;
1553 if (swapped) {
1554 /* undo swap of u64, then swap on individual u32s */
1555 u.val64 = bswap_64(u.val64);
1556 u.val32[0] = bswap_32(u.val32[0]);
1559 sample->cpu = u.val32[0];
1560 array--;
1563 if (type & PERF_SAMPLE_STREAM_ID) {
1564 sample->stream_id = *array;
1565 array--;
1568 if (type & PERF_SAMPLE_ID) {
1569 sample->id = *array;
1570 array--;
1573 if (type & PERF_SAMPLE_TIME) {
1574 sample->time = *array;
1575 array--;
1578 if (type & PERF_SAMPLE_TID) {
1579 u.val64 = *array;
1580 if (swapped) {
1581 /* undo swap of u64, then swap on individual u32s */
1582 u.val64 = bswap_64(u.val64);
1583 u.val32[0] = bswap_32(u.val32[0]);
1584 u.val32[1] = bswap_32(u.val32[1]);
1587 sample->pid = u.val32[0];
1588 sample->tid = u.val32[1];
1589 array--;
1592 return 0;
1595 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1596 u64 size)
1598 return size > max_size || offset + size > endp;
1601 #define OVERFLOW_CHECK(offset, size, max_size) \
1602 do { \
1603 if (overflow(endp, (max_size), (offset), (size))) \
1604 return -EFAULT; \
1605 } while (0)
1607 #define OVERFLOW_CHECK_u64(offset) \
1608 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1610 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1611 struct perf_sample *data)
1613 u64 type = evsel->attr.sample_type;
1614 bool swapped = evsel->needs_swap;
1615 const u64 *array;
1616 u16 max_size = event->header.size;
1617 const void *endp = (void *)event + max_size;
1618 u64 sz;
1621 * used for cross-endian analysis. See git commit 65014ab3
1622 * for why this goofiness is needed.
1624 union u64_swap u;
1626 memset(data, 0, sizeof(*data));
1627 data->cpu = data->pid = data->tid = -1;
1628 data->stream_id = data->id = data->time = -1ULL;
1629 data->period = evsel->attr.sample_period;
1630 data->weight = 0;
1632 if (event->header.type != PERF_RECORD_SAMPLE) {
1633 if (!evsel->attr.sample_id_all)
1634 return 0;
1635 return perf_evsel__parse_id_sample(evsel, event, data);
1638 array = event->sample.array;
1641 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1642 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1643 * check the format does not go past the end of the event.
1645 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1646 return -EFAULT;
1648 data->id = -1ULL;
1649 if (type & PERF_SAMPLE_IDENTIFIER) {
1650 data->id = *array;
1651 array++;
1654 if (type & PERF_SAMPLE_IP) {
1655 data->ip = *array;
1656 array++;
1659 if (type & PERF_SAMPLE_TID) {
1660 u.val64 = *array;
1661 if (swapped) {
1662 /* undo swap of u64, then swap on individual u32s */
1663 u.val64 = bswap_64(u.val64);
1664 u.val32[0] = bswap_32(u.val32[0]);
1665 u.val32[1] = bswap_32(u.val32[1]);
1668 data->pid = u.val32[0];
1669 data->tid = u.val32[1];
1670 array++;
1673 if (type & PERF_SAMPLE_TIME) {
1674 data->time = *array;
1675 array++;
1678 data->addr = 0;
1679 if (type & PERF_SAMPLE_ADDR) {
1680 data->addr = *array;
1681 array++;
1684 if (type & PERF_SAMPLE_ID) {
1685 data->id = *array;
1686 array++;
1689 if (type & PERF_SAMPLE_STREAM_ID) {
1690 data->stream_id = *array;
1691 array++;
1694 if (type & PERF_SAMPLE_CPU) {
1696 u.val64 = *array;
1697 if (swapped) {
1698 /* undo swap of u64, then swap on individual u32s */
1699 u.val64 = bswap_64(u.val64);
1700 u.val32[0] = bswap_32(u.val32[0]);
1703 data->cpu = u.val32[0];
1704 array++;
1707 if (type & PERF_SAMPLE_PERIOD) {
1708 data->period = *array;
1709 array++;
1712 if (type & PERF_SAMPLE_READ) {
1713 u64 read_format = evsel->attr.read_format;
1715 OVERFLOW_CHECK_u64(array);
1716 if (read_format & PERF_FORMAT_GROUP)
1717 data->read.group.nr = *array;
1718 else
1719 data->read.one.value = *array;
1721 array++;
1723 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1724 OVERFLOW_CHECK_u64(array);
1725 data->read.time_enabled = *array;
1726 array++;
1729 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1730 OVERFLOW_CHECK_u64(array);
1731 data->read.time_running = *array;
1732 array++;
1735 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1736 if (read_format & PERF_FORMAT_GROUP) {
1737 const u64 max_group_nr = UINT64_MAX /
1738 sizeof(struct sample_read_value);
1740 if (data->read.group.nr > max_group_nr)
1741 return -EFAULT;
1742 sz = data->read.group.nr *
1743 sizeof(struct sample_read_value);
1744 OVERFLOW_CHECK(array, sz, max_size);
1745 data->read.group.values =
1746 (struct sample_read_value *)array;
1747 array = (void *)array + sz;
1748 } else {
1749 OVERFLOW_CHECK_u64(array);
1750 data->read.one.id = *array;
1751 array++;
1755 if (type & PERF_SAMPLE_CALLCHAIN) {
1756 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1758 OVERFLOW_CHECK_u64(array);
1759 data->callchain = (struct ip_callchain *)array++;
1760 if (data->callchain->nr > max_callchain_nr)
1761 return -EFAULT;
1762 sz = data->callchain->nr * sizeof(u64);
1763 OVERFLOW_CHECK(array, sz, max_size);
1764 array = (void *)array + sz;
1767 if (type & PERF_SAMPLE_RAW) {
1768 OVERFLOW_CHECK_u64(array);
1769 u.val64 = *array;
1770 if (WARN_ONCE(swapped,
1771 "Endianness of raw data not corrected!\n")) {
1772 /* undo swap of u64, then swap on individual u32s */
1773 u.val64 = bswap_64(u.val64);
1774 u.val32[0] = bswap_32(u.val32[0]);
1775 u.val32[1] = bswap_32(u.val32[1]);
1777 data->raw_size = u.val32[0];
1778 array = (void *)array + sizeof(u32);
1780 OVERFLOW_CHECK(array, data->raw_size, max_size);
1781 data->raw_data = (void *)array;
1782 array = (void *)array + data->raw_size;
1785 if (type & PERF_SAMPLE_BRANCH_STACK) {
1786 const u64 max_branch_nr = UINT64_MAX /
1787 sizeof(struct branch_entry);
1789 OVERFLOW_CHECK_u64(array);
1790 data->branch_stack = (struct branch_stack *)array++;
1792 if (data->branch_stack->nr > max_branch_nr)
1793 return -EFAULT;
1794 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1795 OVERFLOW_CHECK(array, sz, max_size);
1796 array = (void *)array + sz;
1799 if (type & PERF_SAMPLE_REGS_USER) {
1800 OVERFLOW_CHECK_u64(array);
1801 data->user_regs.abi = *array;
1802 array++;
1804 if (data->user_regs.abi) {
1805 u64 mask = evsel->attr.sample_regs_user;
1807 sz = hweight_long(mask) * sizeof(u64);
1808 OVERFLOW_CHECK(array, sz, max_size);
1809 data->user_regs.mask = mask;
1810 data->user_regs.regs = (u64 *)array;
1811 array = (void *)array + sz;
1815 if (type & PERF_SAMPLE_STACK_USER) {
1816 OVERFLOW_CHECK_u64(array);
1817 sz = *array++;
1819 data->user_stack.offset = ((char *)(array - 1)
1820 - (char *) event);
1822 if (!sz) {
1823 data->user_stack.size = 0;
1824 } else {
1825 OVERFLOW_CHECK(array, sz, max_size);
1826 data->user_stack.data = (char *)array;
1827 array = (void *)array + sz;
1828 OVERFLOW_CHECK_u64(array);
1829 data->user_stack.size = *array++;
1830 if (WARN_ONCE(data->user_stack.size > sz,
1831 "user stack dump failure\n"))
1832 return -EFAULT;
1836 data->weight = 0;
1837 if (type & PERF_SAMPLE_WEIGHT) {
1838 OVERFLOW_CHECK_u64(array);
1839 data->weight = *array;
1840 array++;
1843 data->data_src = PERF_MEM_DATA_SRC_NONE;
1844 if (type & PERF_SAMPLE_DATA_SRC) {
1845 OVERFLOW_CHECK_u64(array);
1846 data->data_src = *array;
1847 array++;
1850 data->transaction = 0;
1851 if (type & PERF_SAMPLE_TRANSACTION) {
1852 OVERFLOW_CHECK_u64(array);
1853 data->transaction = *array;
1854 array++;
1857 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1858 if (type & PERF_SAMPLE_REGS_INTR) {
1859 OVERFLOW_CHECK_u64(array);
1860 data->intr_regs.abi = *array;
1861 array++;
1863 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1864 u64 mask = evsel->attr.sample_regs_intr;
1866 sz = hweight_long(mask) * sizeof(u64);
1867 OVERFLOW_CHECK(array, sz, max_size);
1868 data->intr_regs.mask = mask;
1869 data->intr_regs.regs = (u64 *)array;
1870 array = (void *)array + sz;
1874 return 0;
1877 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1878 u64 read_format)
1880 size_t sz, result = sizeof(struct sample_event);
1882 if (type & PERF_SAMPLE_IDENTIFIER)
1883 result += sizeof(u64);
1885 if (type & PERF_SAMPLE_IP)
1886 result += sizeof(u64);
1888 if (type & PERF_SAMPLE_TID)
1889 result += sizeof(u64);
1891 if (type & PERF_SAMPLE_TIME)
1892 result += sizeof(u64);
1894 if (type & PERF_SAMPLE_ADDR)
1895 result += sizeof(u64);
1897 if (type & PERF_SAMPLE_ID)
1898 result += sizeof(u64);
1900 if (type & PERF_SAMPLE_STREAM_ID)
1901 result += sizeof(u64);
1903 if (type & PERF_SAMPLE_CPU)
1904 result += sizeof(u64);
1906 if (type & PERF_SAMPLE_PERIOD)
1907 result += sizeof(u64);
1909 if (type & PERF_SAMPLE_READ) {
1910 result += sizeof(u64);
1911 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1912 result += sizeof(u64);
1913 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1914 result += sizeof(u64);
1915 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1916 if (read_format & PERF_FORMAT_GROUP) {
1917 sz = sample->read.group.nr *
1918 sizeof(struct sample_read_value);
1919 result += sz;
1920 } else {
1921 result += sizeof(u64);
1925 if (type & PERF_SAMPLE_CALLCHAIN) {
1926 sz = (sample->callchain->nr + 1) * sizeof(u64);
1927 result += sz;
1930 if (type & PERF_SAMPLE_RAW) {
1931 result += sizeof(u32);
1932 result += sample->raw_size;
1935 if (type & PERF_SAMPLE_BRANCH_STACK) {
1936 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1937 sz += sizeof(u64);
1938 result += sz;
1941 if (type & PERF_SAMPLE_REGS_USER) {
1942 if (sample->user_regs.abi) {
1943 result += sizeof(u64);
1944 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1945 result += sz;
1946 } else {
1947 result += sizeof(u64);
1951 if (type & PERF_SAMPLE_STACK_USER) {
1952 sz = sample->user_stack.size;
1953 result += sizeof(u64);
1954 if (sz) {
1955 result += sz;
1956 result += sizeof(u64);
1960 if (type & PERF_SAMPLE_WEIGHT)
1961 result += sizeof(u64);
1963 if (type & PERF_SAMPLE_DATA_SRC)
1964 result += sizeof(u64);
1966 if (type & PERF_SAMPLE_TRANSACTION)
1967 result += sizeof(u64);
1969 if (type & PERF_SAMPLE_REGS_INTR) {
1970 if (sample->intr_regs.abi) {
1971 result += sizeof(u64);
1972 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1973 result += sz;
1974 } else {
1975 result += sizeof(u64);
1979 return result;
1982 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1983 u64 read_format,
1984 const struct perf_sample *sample,
1985 bool swapped)
1987 u64 *array;
1988 size_t sz;
1990 * used for cross-endian analysis. See git commit 65014ab3
1991 * for why this goofiness is needed.
1993 union u64_swap u;
1995 array = event->sample.array;
1997 if (type & PERF_SAMPLE_IDENTIFIER) {
1998 *array = sample->id;
1999 array++;
2002 if (type & PERF_SAMPLE_IP) {
2003 *array = sample->ip;
2004 array++;
2007 if (type & PERF_SAMPLE_TID) {
2008 u.val32[0] = sample->pid;
2009 u.val32[1] = sample->tid;
2010 if (swapped) {
2012 * Inverse of what is done in perf_evsel__parse_sample
2014 u.val32[0] = bswap_32(u.val32[0]);
2015 u.val32[1] = bswap_32(u.val32[1]);
2016 u.val64 = bswap_64(u.val64);
2019 *array = u.val64;
2020 array++;
2023 if (type & PERF_SAMPLE_TIME) {
2024 *array = sample->time;
2025 array++;
2028 if (type & PERF_SAMPLE_ADDR) {
2029 *array = sample->addr;
2030 array++;
2033 if (type & PERF_SAMPLE_ID) {
2034 *array = sample->id;
2035 array++;
2038 if (type & PERF_SAMPLE_STREAM_ID) {
2039 *array = sample->stream_id;
2040 array++;
2043 if (type & PERF_SAMPLE_CPU) {
2044 u.val32[0] = sample->cpu;
2045 if (swapped) {
2047 * Inverse of what is done in perf_evsel__parse_sample
2049 u.val32[0] = bswap_32(u.val32[0]);
2050 u.val64 = bswap_64(u.val64);
2052 *array = u.val64;
2053 array++;
2056 if (type & PERF_SAMPLE_PERIOD) {
2057 *array = sample->period;
2058 array++;
2061 if (type & PERF_SAMPLE_READ) {
2062 if (read_format & PERF_FORMAT_GROUP)
2063 *array = sample->read.group.nr;
2064 else
2065 *array = sample->read.one.value;
2066 array++;
2068 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2069 *array = sample->read.time_enabled;
2070 array++;
2073 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2074 *array = sample->read.time_running;
2075 array++;
2078 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2079 if (read_format & PERF_FORMAT_GROUP) {
2080 sz = sample->read.group.nr *
2081 sizeof(struct sample_read_value);
2082 memcpy(array, sample->read.group.values, sz);
2083 array = (void *)array + sz;
2084 } else {
2085 *array = sample->read.one.id;
2086 array++;
2090 if (type & PERF_SAMPLE_CALLCHAIN) {
2091 sz = (sample->callchain->nr + 1) * sizeof(u64);
2092 memcpy(array, sample->callchain, sz);
2093 array = (void *)array + sz;
2096 if (type & PERF_SAMPLE_RAW) {
2097 u.val32[0] = sample->raw_size;
2098 if (WARN_ONCE(swapped,
2099 "Endianness of raw data not corrected!\n")) {
2101 * Inverse of what is done in perf_evsel__parse_sample
2103 u.val32[0] = bswap_32(u.val32[0]);
2104 u.val32[1] = bswap_32(u.val32[1]);
2105 u.val64 = bswap_64(u.val64);
2107 *array = u.val64;
2108 array = (void *)array + sizeof(u32);
2110 memcpy(array, sample->raw_data, sample->raw_size);
2111 array = (void *)array + sample->raw_size;
2114 if (type & PERF_SAMPLE_BRANCH_STACK) {
2115 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2116 sz += sizeof(u64);
2117 memcpy(array, sample->branch_stack, sz);
2118 array = (void *)array + sz;
2121 if (type & PERF_SAMPLE_REGS_USER) {
2122 if (sample->user_regs.abi) {
2123 *array++ = sample->user_regs.abi;
2124 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
2125 memcpy(array, sample->user_regs.regs, sz);
2126 array = (void *)array + sz;
2127 } else {
2128 *array++ = 0;
2132 if (type & PERF_SAMPLE_STACK_USER) {
2133 sz = sample->user_stack.size;
2134 *array++ = sz;
2135 if (sz) {
2136 memcpy(array, sample->user_stack.data, sz);
2137 array = (void *)array + sz;
2138 *array++ = sz;
2142 if (type & PERF_SAMPLE_WEIGHT) {
2143 *array = sample->weight;
2144 array++;
2147 if (type & PERF_SAMPLE_DATA_SRC) {
2148 *array = sample->data_src;
2149 array++;
2152 if (type & PERF_SAMPLE_TRANSACTION) {
2153 *array = sample->transaction;
2154 array++;
2157 if (type & PERF_SAMPLE_REGS_INTR) {
2158 if (sample->intr_regs.abi) {
2159 *array++ = sample->intr_regs.abi;
2160 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2161 memcpy(array, sample->intr_regs.regs, sz);
2162 array = (void *)array + sz;
2163 } else {
2164 *array++ = 0;
2168 return 0;
2171 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2173 return pevent_find_field(evsel->tp_format, name);
2176 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2177 const char *name)
2179 struct format_field *field = perf_evsel__field(evsel, name);
2180 int offset;
2182 if (!field)
2183 return NULL;
2185 offset = field->offset;
2187 if (field->flags & FIELD_IS_DYNAMIC) {
2188 offset = *(int *)(sample->raw_data + field->offset);
2189 offset &= 0xffff;
2192 return sample->raw_data + offset;
2195 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2196 const char *name)
2198 struct format_field *field = perf_evsel__field(evsel, name);
2199 void *ptr;
2200 u64 value;
2202 if (!field)
2203 return 0;
2205 ptr = sample->raw_data + field->offset;
2207 switch (field->size) {
2208 case 1:
2209 return *(u8 *)ptr;
2210 case 2:
2211 value = *(u16 *)ptr;
2212 break;
2213 case 4:
2214 value = *(u32 *)ptr;
2215 break;
2216 case 8:
2217 memcpy(&value, ptr, sizeof(u64));
2218 break;
2219 default:
2220 return 0;
2223 if (!evsel->needs_swap)
2224 return value;
2226 switch (field->size) {
2227 case 2:
2228 return bswap_16(value);
2229 case 4:
2230 return bswap_32(value);
2231 case 8:
2232 return bswap_64(value);
2233 default:
2234 return 0;
2237 return 0;
2240 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2242 va_list args;
2243 int ret = 0;
2245 if (!*first) {
2246 ret += fprintf(fp, ",");
2247 } else {
2248 ret += fprintf(fp, ":");
2249 *first = false;
2252 va_start(args, fmt);
2253 ret += vfprintf(fp, fmt, args);
2254 va_end(args);
2255 return ret;
2258 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2260 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2263 int perf_evsel__fprintf(struct perf_evsel *evsel,
2264 struct perf_attr_details *details, FILE *fp)
2266 bool first = true;
2267 int printed = 0;
2269 if (details->event_group) {
2270 struct perf_evsel *pos;
2272 if (!perf_evsel__is_group_leader(evsel))
2273 return 0;
2275 if (evsel->nr_members > 1)
2276 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2278 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2279 for_each_group_member(pos, evsel)
2280 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2282 if (evsel->nr_members > 1)
2283 printed += fprintf(fp, "}");
2284 goto out;
2287 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2289 if (details->verbose) {
2290 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2291 __print_attr__fprintf, &first);
2292 } else if (details->freq) {
2293 const char *term = "sample_freq";
2295 if (!evsel->attr.freq)
2296 term = "sample_period";
2298 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2299 term, (u64)evsel->attr.sample_freq);
2302 if (details->trace_fields) {
2303 struct format_field *field;
2305 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2306 printed += comma_fprintf(fp, &first, " (not a tracepoint)");
2307 goto out;
2310 field = evsel->tp_format->format.fields;
2311 if (field == NULL) {
2312 printed += comma_fprintf(fp, &first, " (no trace field)");
2313 goto out;
2316 printed += comma_fprintf(fp, &first, " trace_fields: %s", field->name);
2318 field = field->next;
2319 while (field) {
2320 printed += comma_fprintf(fp, &first, "%s", field->name);
2321 field = field->next;
2324 out:
2325 fputc('\n', fp);
2326 return ++printed;
2329 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2330 char *msg, size_t msgsize)
2332 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2333 evsel->attr.type == PERF_TYPE_HARDWARE &&
2334 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2336 * If it's cycles then fall back to hrtimer based
2337 * cpu-clock-tick sw counter, which is always available even if
2338 * no PMU support.
2340 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2341 * b0a873e).
2343 scnprintf(msg, msgsize, "%s",
2344 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2346 evsel->attr.type = PERF_TYPE_SOFTWARE;
2347 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2349 zfree(&evsel->name);
2350 return true;
2353 return false;
2356 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2357 int err, char *msg, size_t size)
2359 char sbuf[STRERR_BUFSIZE];
2361 switch (err) {
2362 case EPERM:
2363 case EACCES:
2364 return scnprintf(msg, size,
2365 "You may not have permission to collect %sstats.\n"
2366 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2367 " -1 - Not paranoid at all\n"
2368 " 0 - Disallow raw tracepoint access for unpriv\n"
2369 " 1 - Disallow cpu events for unpriv\n"
2370 " 2 - Disallow kernel profiling for unpriv",
2371 target->system_wide ? "system-wide " : "");
2372 case ENOENT:
2373 return scnprintf(msg, size, "The %s event is not supported.",
2374 perf_evsel__name(evsel));
2375 case EMFILE:
2376 return scnprintf(msg, size, "%s",
2377 "Too many events are opened.\n"
2378 "Probably the maximum number of open file descriptors has been reached.\n"
2379 "Hint: Try again after reducing the number of events.\n"
2380 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2381 case ENODEV:
2382 if (target->cpu_list)
2383 return scnprintf(msg, size, "%s",
2384 "No such device - did you specify an out-of-range profile CPU?\n");
2385 break;
2386 case EOPNOTSUPP:
2387 if (evsel->attr.precise_ip)
2388 return scnprintf(msg, size, "%s",
2389 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2390 #if defined(__i386__) || defined(__x86_64__)
2391 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2392 return scnprintf(msg, size, "%s",
2393 "No hardware sampling interrupt available.\n"
2394 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2395 #endif
2396 break;
2397 case EBUSY:
2398 if (find_process("oprofiled"))
2399 return scnprintf(msg, size,
2400 "The PMU counters are busy/taken by another profiler.\n"
2401 "We found oprofile daemon running, please stop it and try again.");
2402 break;
2403 case EINVAL:
2404 if (perf_missing_features.clockid)
2405 return scnprintf(msg, size, "clockid feature not supported.");
2406 if (perf_missing_features.clockid_wrong)
2407 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2408 break;
2409 default:
2410 break;
2413 return scnprintf(msg, size,
2414 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2415 "/bin/dmesg may provide additional information.\n"
2416 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2417 err, strerror_r(err, sbuf, sizeof(sbuf)),
2418 perf_evsel__name(evsel));