Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / evsel.c
blob966360844fffbd10b4d97c318b22e0fc8446c1ee
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 <errno.h>
12 #include <inttypes.h>
13 #include <linux/bitops.h>
14 #include <api/fs/fs.h>
15 #include <api/fs/tracing_path.h>
16 #include <traceevent/event-parse.h>
17 #include <linux/hw_breakpoint.h>
18 #include <linux/perf_event.h>
19 #include <linux/compiler.h>
20 #include <linux/err.h>
21 #include <sys/ioctl.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include "asm/bug.h"
26 #include "callchain.h"
27 #include "cgroup.h"
28 #include "event.h"
29 #include "evsel.h"
30 #include "evlist.h"
31 #include "util.h"
32 #include "cpumap.h"
33 #include "thread_map.h"
34 #include "target.h"
35 #include "perf_regs.h"
36 #include "debug.h"
37 #include "trace-event.h"
38 #include "stat.h"
39 #include "memswap.h"
40 #include "util/parse-branch-options.h"
42 #include "sane_ctype.h"
44 struct perf_missing_features perf_missing_features;
46 static clockid_t clockid;
48 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
50 return 0;
53 void __weak test_attr__ready(void) { }
55 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
59 static struct {
60 size_t size;
61 int (*init)(struct perf_evsel *evsel);
62 void (*fini)(struct perf_evsel *evsel);
63 } perf_evsel__object = {
64 .size = sizeof(struct perf_evsel),
65 .init = perf_evsel__no_extra_init,
66 .fini = perf_evsel__no_extra_fini,
69 int perf_evsel__object_config(size_t object_size,
70 int (*init)(struct perf_evsel *evsel),
71 void (*fini)(struct perf_evsel *evsel))
74 if (object_size == 0)
75 goto set_methods;
77 if (perf_evsel__object.size > object_size)
78 return -EINVAL;
80 perf_evsel__object.size = object_size;
82 set_methods:
83 if (init != NULL)
84 perf_evsel__object.init = init;
86 if (fini != NULL)
87 perf_evsel__object.fini = fini;
89 return 0;
92 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
94 int __perf_evsel__sample_size(u64 sample_type)
96 u64 mask = sample_type & PERF_SAMPLE_MASK;
97 int size = 0;
98 int i;
100 for (i = 0; i < 64; i++) {
101 if (mask & (1ULL << i))
102 size++;
105 size *= sizeof(u64);
107 return size;
111 * __perf_evsel__calc_id_pos - calculate id_pos.
112 * @sample_type: sample type
114 * This function returns the position of the event id (PERF_SAMPLE_ID or
115 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
116 * sample_event.
118 static int __perf_evsel__calc_id_pos(u64 sample_type)
120 int idx = 0;
122 if (sample_type & PERF_SAMPLE_IDENTIFIER)
123 return 0;
125 if (!(sample_type & PERF_SAMPLE_ID))
126 return -1;
128 if (sample_type & PERF_SAMPLE_IP)
129 idx += 1;
131 if (sample_type & PERF_SAMPLE_TID)
132 idx += 1;
134 if (sample_type & PERF_SAMPLE_TIME)
135 idx += 1;
137 if (sample_type & PERF_SAMPLE_ADDR)
138 idx += 1;
140 return idx;
144 * __perf_evsel__calc_is_pos - calculate is_pos.
145 * @sample_type: sample type
147 * This function returns the position (counting backwards) of the event id
148 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
149 * sample_id_all is used there is an id sample appended to non-sample events.
151 static int __perf_evsel__calc_is_pos(u64 sample_type)
153 int idx = 1;
155 if (sample_type & PERF_SAMPLE_IDENTIFIER)
156 return 1;
158 if (!(sample_type & PERF_SAMPLE_ID))
159 return -1;
161 if (sample_type & PERF_SAMPLE_CPU)
162 idx += 1;
164 if (sample_type & PERF_SAMPLE_STREAM_ID)
165 idx += 1;
167 return idx;
170 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
172 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
173 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
176 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
177 enum perf_event_sample_format bit)
179 if (!(evsel->attr.sample_type & bit)) {
180 evsel->attr.sample_type |= bit;
181 evsel->sample_size += sizeof(u64);
182 perf_evsel__calc_id_pos(evsel);
186 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
187 enum perf_event_sample_format bit)
189 if (evsel->attr.sample_type & bit) {
190 evsel->attr.sample_type &= ~bit;
191 evsel->sample_size -= sizeof(u64);
192 perf_evsel__calc_id_pos(evsel);
196 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
197 bool can_sample_identifier)
199 if (can_sample_identifier) {
200 perf_evsel__reset_sample_bit(evsel, ID);
201 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
202 } else {
203 perf_evsel__set_sample_bit(evsel, ID);
205 evsel->attr.read_format |= PERF_FORMAT_ID;
209 * perf_evsel__is_function_event - Return whether given evsel is a function
210 * trace event
212 * @evsel - evsel selector to be tested
214 * Return %true if event is function trace event
216 bool perf_evsel__is_function_event(struct perf_evsel *evsel)
218 #define FUNCTION_EVENT "ftrace:function"
220 return evsel->name &&
221 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
223 #undef FUNCTION_EVENT
226 void perf_evsel__init(struct perf_evsel *evsel,
227 struct perf_event_attr *attr, int idx)
229 evsel->idx = idx;
230 evsel->tracking = !idx;
231 evsel->attr = *attr;
232 evsel->leader = evsel;
233 evsel->unit = "";
234 evsel->scale = 1.0;
235 evsel->max_events = ULONG_MAX;
236 evsel->evlist = NULL;
237 evsel->bpf_fd = -1;
238 INIT_LIST_HEAD(&evsel->node);
239 INIT_LIST_HEAD(&evsel->config_terms);
240 perf_evsel__object.init(evsel);
241 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
242 perf_evsel__calc_id_pos(evsel);
243 evsel->cmdline_group_boundary = false;
244 evsel->metric_expr = NULL;
245 evsel->metric_name = NULL;
246 evsel->metric_events = NULL;
247 evsel->collect_stat = false;
248 evsel->pmu_name = NULL;
251 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
253 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
255 if (!evsel)
256 return NULL;
257 perf_evsel__init(evsel, attr, idx);
259 if (perf_evsel__is_bpf_output(evsel)) {
260 evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
261 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
262 evsel->attr.sample_period = 1;
265 if (perf_evsel__is_clock(evsel)) {
267 * The evsel->unit points to static alias->unit
268 * so it's ok to use static string in here.
270 static const char *unit = "msec";
272 evsel->unit = unit;
273 evsel->scale = 1e-6;
276 return evsel;
279 static bool perf_event_can_profile_kernel(void)
281 return geteuid() == 0 || perf_event_paranoid() == -1;
284 struct perf_evsel *perf_evsel__new_cycles(bool precise)
286 struct perf_event_attr attr = {
287 .type = PERF_TYPE_HARDWARE,
288 .config = PERF_COUNT_HW_CPU_CYCLES,
289 .exclude_kernel = !perf_event_can_profile_kernel(),
291 struct perf_evsel *evsel;
293 event_attr_init(&attr);
295 if (!precise)
296 goto new_event;
299 * Now let the usual logic to set up the perf_event_attr defaults
300 * to kick in when we return and before perf_evsel__open() is called.
302 new_event:
303 evsel = perf_evsel__new(&attr);
304 if (evsel == NULL)
305 goto out;
307 evsel->precise_max = true;
309 /* use asprintf() because free(evsel) assumes name is allocated */
310 if (asprintf(&evsel->name, "cycles%s%s%.*s",
311 (attr.precise_ip || attr.exclude_kernel) ? ":" : "",
312 attr.exclude_kernel ? "u" : "",
313 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0)
314 goto error_free;
315 out:
316 return evsel;
317 error_free:
318 perf_evsel__delete(evsel);
319 evsel = NULL;
320 goto out;
324 * Returns pointer with encoded error via <linux/err.h> interface.
326 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
328 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
329 int err = -ENOMEM;
331 if (evsel == NULL) {
332 goto out_err;
333 } else {
334 struct perf_event_attr attr = {
335 .type = PERF_TYPE_TRACEPOINT,
336 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
337 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
340 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
341 goto out_free;
343 evsel->tp_format = trace_event__tp_format(sys, name);
344 if (IS_ERR(evsel->tp_format)) {
345 err = PTR_ERR(evsel->tp_format);
346 goto out_free;
349 event_attr_init(&attr);
350 attr.config = evsel->tp_format->id;
351 attr.sample_period = 1;
352 perf_evsel__init(evsel, &attr, idx);
355 return evsel;
357 out_free:
358 zfree(&evsel->name);
359 free(evsel);
360 out_err:
361 return ERR_PTR(err);
364 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
365 "cycles",
366 "instructions",
367 "cache-references",
368 "cache-misses",
369 "branches",
370 "branch-misses",
371 "bus-cycles",
372 "stalled-cycles-frontend",
373 "stalled-cycles-backend",
374 "ref-cycles",
377 static const char *__perf_evsel__hw_name(u64 config)
379 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
380 return perf_evsel__hw_names[config];
382 return "unknown-hardware";
385 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
387 int colon = 0, r = 0;
388 struct perf_event_attr *attr = &evsel->attr;
389 bool exclude_guest_default = false;
391 #define MOD_PRINT(context, mod) do { \
392 if (!attr->exclude_##context) { \
393 if (!colon) colon = ++r; \
394 r += scnprintf(bf + r, size - r, "%c", mod); \
395 } } while(0)
397 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
398 MOD_PRINT(kernel, 'k');
399 MOD_PRINT(user, 'u');
400 MOD_PRINT(hv, 'h');
401 exclude_guest_default = true;
404 if (attr->precise_ip) {
405 if (!colon)
406 colon = ++r;
407 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
408 exclude_guest_default = true;
411 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
412 MOD_PRINT(host, 'H');
413 MOD_PRINT(guest, 'G');
415 #undef MOD_PRINT
416 if (colon)
417 bf[colon - 1] = ':';
418 return r;
421 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
423 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
424 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
427 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
428 "cpu-clock",
429 "task-clock",
430 "page-faults",
431 "context-switches",
432 "cpu-migrations",
433 "minor-faults",
434 "major-faults",
435 "alignment-faults",
436 "emulation-faults",
437 "dummy",
440 static const char *__perf_evsel__sw_name(u64 config)
442 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
443 return perf_evsel__sw_names[config];
444 return "unknown-software";
447 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
449 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
450 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
453 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
455 int r;
457 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
459 if (type & HW_BREAKPOINT_R)
460 r += scnprintf(bf + r, size - r, "r");
462 if (type & HW_BREAKPOINT_W)
463 r += scnprintf(bf + r, size - r, "w");
465 if (type & HW_BREAKPOINT_X)
466 r += scnprintf(bf + r, size - r, "x");
468 return r;
471 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
473 struct perf_event_attr *attr = &evsel->attr;
474 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
475 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
478 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
479 [PERF_EVSEL__MAX_ALIASES] = {
480 { "L1-dcache", "l1-d", "l1d", "L1-data", },
481 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
482 { "LLC", "L2", },
483 { "dTLB", "d-tlb", "Data-TLB", },
484 { "iTLB", "i-tlb", "Instruction-TLB", },
485 { "branch", "branches", "bpu", "btb", "bpc", },
486 { "node", },
489 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
490 [PERF_EVSEL__MAX_ALIASES] = {
491 { "load", "loads", "read", },
492 { "store", "stores", "write", },
493 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
496 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
497 [PERF_EVSEL__MAX_ALIASES] = {
498 { "refs", "Reference", "ops", "access", },
499 { "misses", "miss", },
502 #define C(x) PERF_COUNT_HW_CACHE_##x
503 #define CACHE_READ (1 << C(OP_READ))
504 #define CACHE_WRITE (1 << C(OP_WRITE))
505 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
506 #define COP(x) (1 << x)
509 * cache operartion stat
510 * L1I : Read and prefetch only
511 * ITLB and BPU : Read-only
513 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
514 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
515 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
516 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
517 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
518 [C(ITLB)] = (CACHE_READ),
519 [C(BPU)] = (CACHE_READ),
520 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
523 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
525 if (perf_evsel__hw_cache_stat[type] & COP(op))
526 return true; /* valid */
527 else
528 return false; /* invalid */
531 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
532 char *bf, size_t size)
534 if (result) {
535 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
536 perf_evsel__hw_cache_op[op][0],
537 perf_evsel__hw_cache_result[result][0]);
540 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
541 perf_evsel__hw_cache_op[op][1]);
544 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
546 u8 op, result, type = (config >> 0) & 0xff;
547 const char *err = "unknown-ext-hardware-cache-type";
549 if (type >= PERF_COUNT_HW_CACHE_MAX)
550 goto out_err;
552 op = (config >> 8) & 0xff;
553 err = "unknown-ext-hardware-cache-op";
554 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
555 goto out_err;
557 result = (config >> 16) & 0xff;
558 err = "unknown-ext-hardware-cache-result";
559 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
560 goto out_err;
562 err = "invalid-cache";
563 if (!perf_evsel__is_cache_op_valid(type, op))
564 goto out_err;
566 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
567 out_err:
568 return scnprintf(bf, size, "%s", err);
571 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
573 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
574 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
577 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
579 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
580 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
583 const char *perf_evsel__name(struct perf_evsel *evsel)
585 char bf[128];
587 if (evsel->name)
588 return evsel->name;
590 switch (evsel->attr.type) {
591 case PERF_TYPE_RAW:
592 perf_evsel__raw_name(evsel, bf, sizeof(bf));
593 break;
595 case PERF_TYPE_HARDWARE:
596 perf_evsel__hw_name(evsel, bf, sizeof(bf));
597 break;
599 case PERF_TYPE_HW_CACHE:
600 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
601 break;
603 case PERF_TYPE_SOFTWARE:
604 perf_evsel__sw_name(evsel, bf, sizeof(bf));
605 break;
607 case PERF_TYPE_TRACEPOINT:
608 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
609 break;
611 case PERF_TYPE_BREAKPOINT:
612 perf_evsel__bp_name(evsel, bf, sizeof(bf));
613 break;
615 default:
616 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
617 evsel->attr.type);
618 break;
621 evsel->name = strdup(bf);
623 return evsel->name ?: "unknown";
626 const char *perf_evsel__group_name(struct perf_evsel *evsel)
628 return evsel->group_name ?: "anon group";
632 * Returns the group details for the specified leader,
633 * with following rules.
635 * For record -e '{cycles,instructions}'
636 * 'anon group { cycles:u, instructions:u }'
638 * For record -e 'cycles,instructions' and report --group
639 * 'cycles:u, instructions:u'
641 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
643 int ret = 0;
644 struct perf_evsel *pos;
645 const char *group_name = perf_evsel__group_name(evsel);
647 if (!evsel->forced_leader)
648 ret = scnprintf(buf, size, "%s { ", group_name);
650 ret += scnprintf(buf + ret, size - ret, "%s",
651 perf_evsel__name(evsel));
653 for_each_group_member(pos, evsel)
654 ret += scnprintf(buf + ret, size - ret, ", %s",
655 perf_evsel__name(pos));
657 if (!evsel->forced_leader)
658 ret += scnprintf(buf + ret, size - ret, " }");
660 return ret;
663 static void __perf_evsel__config_callchain(struct perf_evsel *evsel,
664 struct record_opts *opts,
665 struct callchain_param *param)
667 bool function = perf_evsel__is_function_event(evsel);
668 struct perf_event_attr *attr = &evsel->attr;
670 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
672 attr->sample_max_stack = param->max_stack;
674 if (param->record_mode == CALLCHAIN_LBR) {
675 if (!opts->branch_stack) {
676 if (attr->exclude_user) {
677 pr_warning("LBR callstack option is only available "
678 "to get user callchain information. "
679 "Falling back to framepointers.\n");
680 } else {
681 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
682 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
683 PERF_SAMPLE_BRANCH_CALL_STACK |
684 PERF_SAMPLE_BRANCH_NO_CYCLES |
685 PERF_SAMPLE_BRANCH_NO_FLAGS;
687 } else
688 pr_warning("Cannot use LBR callstack with branch stack. "
689 "Falling back to framepointers.\n");
692 if (param->record_mode == CALLCHAIN_DWARF) {
693 if (!function) {
694 perf_evsel__set_sample_bit(evsel, REGS_USER);
695 perf_evsel__set_sample_bit(evsel, STACK_USER);
696 attr->sample_regs_user |= PERF_REGS_MASK;
697 attr->sample_stack_user = param->dump_size;
698 attr->exclude_callchain_user = 1;
699 } else {
700 pr_info("Cannot use DWARF unwind for function trace event,"
701 " falling back to framepointers.\n");
705 if (function) {
706 pr_info("Disabling user space callchains for function trace event.\n");
707 attr->exclude_callchain_user = 1;
711 void perf_evsel__config_callchain(struct perf_evsel *evsel,
712 struct record_opts *opts,
713 struct callchain_param *param)
715 if (param->enabled)
716 return __perf_evsel__config_callchain(evsel, opts, param);
719 static void
720 perf_evsel__reset_callgraph(struct perf_evsel *evsel,
721 struct callchain_param *param)
723 struct perf_event_attr *attr = &evsel->attr;
725 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
726 if (param->record_mode == CALLCHAIN_LBR) {
727 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
728 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
729 PERF_SAMPLE_BRANCH_CALL_STACK);
731 if (param->record_mode == CALLCHAIN_DWARF) {
732 perf_evsel__reset_sample_bit(evsel, REGS_USER);
733 perf_evsel__reset_sample_bit(evsel, STACK_USER);
737 static void apply_config_terms(struct perf_evsel *evsel,
738 struct record_opts *opts, bool track)
740 struct perf_evsel_config_term *term;
741 struct list_head *config_terms = &evsel->config_terms;
742 struct perf_event_attr *attr = &evsel->attr;
743 /* callgraph default */
744 struct callchain_param param = {
745 .record_mode = callchain_param.record_mode,
747 u32 dump_size = 0;
748 int max_stack = 0;
749 const char *callgraph_buf = NULL;
751 list_for_each_entry(term, config_terms, list) {
752 switch (term->type) {
753 case PERF_EVSEL__CONFIG_TERM_PERIOD:
754 if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
755 attr->sample_period = term->val.period;
756 attr->freq = 0;
757 perf_evsel__reset_sample_bit(evsel, PERIOD);
759 break;
760 case PERF_EVSEL__CONFIG_TERM_FREQ:
761 if (!(term->weak && opts->user_freq != UINT_MAX)) {
762 attr->sample_freq = term->val.freq;
763 attr->freq = 1;
764 perf_evsel__set_sample_bit(evsel, PERIOD);
766 break;
767 case PERF_EVSEL__CONFIG_TERM_TIME:
768 if (term->val.time)
769 perf_evsel__set_sample_bit(evsel, TIME);
770 else
771 perf_evsel__reset_sample_bit(evsel, TIME);
772 break;
773 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
774 callgraph_buf = term->val.callgraph;
775 break;
776 case PERF_EVSEL__CONFIG_TERM_BRANCH:
777 if (term->val.branch && strcmp(term->val.branch, "no")) {
778 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
779 parse_branch_str(term->val.branch,
780 &attr->branch_sample_type);
781 } else
782 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
783 break;
784 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
785 dump_size = term->val.stack_user;
786 break;
787 case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
788 max_stack = term->val.max_stack;
789 break;
790 case PERF_EVSEL__CONFIG_TERM_MAX_EVENTS:
791 evsel->max_events = term->val.max_events;
792 break;
793 case PERF_EVSEL__CONFIG_TERM_INHERIT:
795 * attr->inherit should has already been set by
796 * perf_evsel__config. If user explicitly set
797 * inherit using config terms, override global
798 * opt->no_inherit setting.
800 attr->inherit = term->val.inherit ? 1 : 0;
801 break;
802 case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
803 attr->write_backward = term->val.overwrite ? 1 : 0;
804 break;
805 case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
806 break;
807 default:
808 break;
812 /* User explicitly set per-event callgraph, clear the old setting and reset. */
813 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
814 bool sample_address = false;
816 if (max_stack) {
817 param.max_stack = max_stack;
818 if (callgraph_buf == NULL)
819 callgraph_buf = "fp";
822 /* parse callgraph parameters */
823 if (callgraph_buf != NULL) {
824 if (!strcmp(callgraph_buf, "no")) {
825 param.enabled = false;
826 param.record_mode = CALLCHAIN_NONE;
827 } else {
828 param.enabled = true;
829 if (parse_callchain_record(callgraph_buf, &param)) {
830 pr_err("per-event callgraph setting for %s failed. "
831 "Apply callgraph global setting for it\n",
832 evsel->name);
833 return;
835 if (param.record_mode == CALLCHAIN_DWARF)
836 sample_address = true;
839 if (dump_size > 0) {
840 dump_size = round_up(dump_size, sizeof(u64));
841 param.dump_size = dump_size;
844 /* If global callgraph set, clear it */
845 if (callchain_param.enabled)
846 perf_evsel__reset_callgraph(evsel, &callchain_param);
848 /* set perf-event callgraph */
849 if (param.enabled) {
850 if (sample_address) {
851 perf_evsel__set_sample_bit(evsel, ADDR);
852 perf_evsel__set_sample_bit(evsel, DATA_SRC);
853 evsel->attr.mmap_data = track;
855 perf_evsel__config_callchain(evsel, opts, &param);
860 static bool is_dummy_event(struct perf_evsel *evsel)
862 return (evsel->attr.type == PERF_TYPE_SOFTWARE) &&
863 (evsel->attr.config == PERF_COUNT_SW_DUMMY);
867 * The enable_on_exec/disabled value strategy:
869 * 1) For any type of traced program:
870 * - all independent events and group leaders are disabled
871 * - all group members are enabled
873 * Group members are ruled by group leaders. They need to
874 * be enabled, because the group scheduling relies on that.
876 * 2) For traced programs executed by perf:
877 * - all independent events and group leaders have
878 * enable_on_exec set
879 * - we don't specifically enable or disable any event during
880 * the record command
882 * Independent events and group leaders are initially disabled
883 * and get enabled by exec. Group members are ruled by group
884 * leaders as stated in 1).
886 * 3) For traced programs attached by perf (pid/tid):
887 * - we specifically enable or disable all events during
888 * the record command
890 * When attaching events to already running traced we
891 * enable/disable events specifically, as there's no
892 * initial traced exec call.
894 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
895 struct callchain_param *callchain)
897 struct perf_evsel *leader = evsel->leader;
898 struct perf_event_attr *attr = &evsel->attr;
899 int track = evsel->tracking;
900 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
902 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
903 attr->inherit = !opts->no_inherit;
904 attr->write_backward = opts->overwrite ? 1 : 0;
906 perf_evsel__set_sample_bit(evsel, IP);
907 perf_evsel__set_sample_bit(evsel, TID);
909 if (evsel->sample_read) {
910 perf_evsel__set_sample_bit(evsel, READ);
913 * We need ID even in case of single event, because
914 * PERF_SAMPLE_READ process ID specific data.
916 perf_evsel__set_sample_id(evsel, false);
919 * Apply group format only if we belong to group
920 * with more than one members.
922 if (leader->nr_members > 1) {
923 attr->read_format |= PERF_FORMAT_GROUP;
924 attr->inherit = 0;
929 * We default some events to have a default interval. But keep
930 * it a weak assumption overridable by the user.
932 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
933 opts->user_interval != ULLONG_MAX)) {
934 if (opts->freq) {
935 perf_evsel__set_sample_bit(evsel, PERIOD);
936 attr->freq = 1;
937 attr->sample_freq = opts->freq;
938 } else {
939 attr->sample_period = opts->default_interval;
944 * Disable sampling for all group members other
945 * than leader in case leader 'leads' the sampling.
947 if ((leader != evsel) && leader->sample_read) {
948 attr->freq = 0;
949 attr->sample_freq = 0;
950 attr->sample_period = 0;
951 attr->write_backward = 0;
954 * We don't get sample for slave events, we make them
955 * when delivering group leader sample. Set the slave
956 * event to follow the master sample_type to ease up
957 * report.
959 attr->sample_type = leader->attr.sample_type;
962 if (opts->no_samples)
963 attr->sample_freq = 0;
965 if (opts->inherit_stat) {
966 evsel->attr.read_format |=
967 PERF_FORMAT_TOTAL_TIME_ENABLED |
968 PERF_FORMAT_TOTAL_TIME_RUNNING |
969 PERF_FORMAT_ID;
970 attr->inherit_stat = 1;
973 if (opts->sample_address) {
974 perf_evsel__set_sample_bit(evsel, ADDR);
975 attr->mmap_data = track;
979 * We don't allow user space callchains for function trace
980 * event, due to issues with page faults while tracing page
981 * fault handler and its overall trickiness nature.
983 if (perf_evsel__is_function_event(evsel))
984 evsel->attr.exclude_callchain_user = 1;
986 if (callchain && callchain->enabled && !evsel->no_aux_samples)
987 perf_evsel__config_callchain(evsel, opts, callchain);
989 if (opts->sample_intr_regs) {
990 attr->sample_regs_intr = opts->sample_intr_regs;
991 perf_evsel__set_sample_bit(evsel, REGS_INTR);
994 if (opts->sample_user_regs) {
995 attr->sample_regs_user |= opts->sample_user_regs;
996 perf_evsel__set_sample_bit(evsel, REGS_USER);
999 if (target__has_cpu(&opts->target) || opts->sample_cpu)
1000 perf_evsel__set_sample_bit(evsel, CPU);
1003 * When the user explicitly disabled time don't force it here.
1005 if (opts->sample_time &&
1006 (!perf_missing_features.sample_id_all &&
1007 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
1008 opts->sample_time_set)))
1009 perf_evsel__set_sample_bit(evsel, TIME);
1011 if (opts->raw_samples && !evsel->no_aux_samples) {
1012 perf_evsel__set_sample_bit(evsel, TIME);
1013 perf_evsel__set_sample_bit(evsel, RAW);
1014 perf_evsel__set_sample_bit(evsel, CPU);
1017 if (opts->sample_address)
1018 perf_evsel__set_sample_bit(evsel, DATA_SRC);
1020 if (opts->sample_phys_addr)
1021 perf_evsel__set_sample_bit(evsel, PHYS_ADDR);
1023 if (opts->no_buffering) {
1024 attr->watermark = 0;
1025 attr->wakeup_events = 1;
1027 if (opts->branch_stack && !evsel->no_aux_samples) {
1028 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
1029 attr->branch_sample_type = opts->branch_stack;
1032 if (opts->sample_weight)
1033 perf_evsel__set_sample_bit(evsel, WEIGHT);
1035 attr->task = track;
1036 attr->mmap = track;
1037 attr->mmap2 = track && !perf_missing_features.mmap2;
1038 attr->comm = track;
1039 attr->ksymbol = track && !perf_missing_features.ksymbol;
1040 attr->bpf_event = track && !opts->no_bpf_event &&
1041 !perf_missing_features.bpf_event;
1043 if (opts->record_namespaces)
1044 attr->namespaces = track;
1046 if (opts->record_switch_events)
1047 attr->context_switch = track;
1049 if (opts->sample_transaction)
1050 perf_evsel__set_sample_bit(evsel, TRANSACTION);
1052 if (opts->running_time) {
1053 evsel->attr.read_format |=
1054 PERF_FORMAT_TOTAL_TIME_ENABLED |
1055 PERF_FORMAT_TOTAL_TIME_RUNNING;
1059 * XXX see the function comment above
1061 * Disabling only independent events or group leaders,
1062 * keeping group members enabled.
1064 if (perf_evsel__is_group_leader(evsel))
1065 attr->disabled = 1;
1068 * Setting enable_on_exec for independent events and
1069 * group leaders for traced executed by perf.
1071 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
1072 !opts->initial_delay)
1073 attr->enable_on_exec = 1;
1075 if (evsel->immediate) {
1076 attr->disabled = 0;
1077 attr->enable_on_exec = 0;
1080 clockid = opts->clockid;
1081 if (opts->use_clockid) {
1082 attr->use_clockid = 1;
1083 attr->clockid = opts->clockid;
1086 if (evsel->precise_max)
1087 attr->precise_ip = 3;
1089 if (opts->all_user) {
1090 attr->exclude_kernel = 1;
1091 attr->exclude_user = 0;
1094 if (opts->all_kernel) {
1095 attr->exclude_kernel = 0;
1096 attr->exclude_user = 1;
1099 if (evsel->own_cpus || evsel->unit)
1100 evsel->attr.read_format |= PERF_FORMAT_ID;
1103 * Apply event specific term settings,
1104 * it overloads any global configuration.
1106 apply_config_terms(evsel, opts, track);
1108 evsel->ignore_missing_thread = opts->ignore_missing_thread;
1110 /* The --period option takes the precedence. */
1111 if (opts->period_set) {
1112 if (opts->period)
1113 perf_evsel__set_sample_bit(evsel, PERIOD);
1114 else
1115 perf_evsel__reset_sample_bit(evsel, PERIOD);
1119 * For initial_delay, a dummy event is added implicitly.
1120 * The software event will trigger -EOPNOTSUPP error out,
1121 * if BRANCH_STACK bit is set.
1123 if (opts->initial_delay && is_dummy_event(evsel))
1124 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
1127 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1129 if (evsel->system_wide)
1130 nthreads = 1;
1132 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
1134 if (evsel->fd) {
1135 int cpu, thread;
1136 for (cpu = 0; cpu < ncpus; cpu++) {
1137 for (thread = 0; thread < nthreads; thread++) {
1138 FD(evsel, cpu, thread) = -1;
1143 return evsel->fd != NULL ? 0 : -ENOMEM;
1146 static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
1147 int ioc, void *arg)
1149 int cpu, thread;
1151 for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) {
1152 for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
1153 int fd = FD(evsel, cpu, thread),
1154 err = ioctl(fd, ioc, arg);
1156 if (err)
1157 return err;
1161 return 0;
1164 int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
1166 return perf_evsel__run_ioctl(evsel,
1167 PERF_EVENT_IOC_SET_FILTER,
1168 (void *)filter);
1171 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
1173 char *new_filter = strdup(filter);
1175 if (new_filter != NULL) {
1176 free(evsel->filter);
1177 evsel->filter = new_filter;
1178 return 0;
1181 return -1;
1184 static int perf_evsel__append_filter(struct perf_evsel *evsel,
1185 const char *fmt, const char *filter)
1187 char *new_filter;
1189 if (evsel->filter == NULL)
1190 return perf_evsel__set_filter(evsel, filter);
1192 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
1193 free(evsel->filter);
1194 evsel->filter = new_filter;
1195 return 0;
1198 return -1;
1201 int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter)
1203 return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
1206 int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter)
1208 return perf_evsel__append_filter(evsel, "%s,%s", filter);
1211 int perf_evsel__enable(struct perf_evsel *evsel)
1213 int err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0);
1215 if (!err)
1216 evsel->disabled = false;
1218 return err;
1221 int perf_evsel__disable(struct perf_evsel *evsel)
1223 int err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0);
1225 * We mark it disabled here so that tools that disable a event can
1226 * ignore events after they disable it. I.e. the ring buffer may have
1227 * already a few more events queued up before the kernel got the stop
1228 * request.
1230 if (!err)
1231 evsel->disabled = true;
1233 return err;
1236 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1238 if (ncpus == 0 || nthreads == 0)
1239 return 0;
1241 if (evsel->system_wide)
1242 nthreads = 1;
1244 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1245 if (evsel->sample_id == NULL)
1246 return -ENOMEM;
1248 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1249 if (evsel->id == NULL) {
1250 xyarray__delete(evsel->sample_id);
1251 evsel->sample_id = NULL;
1252 return -ENOMEM;
1255 return 0;
1258 static void perf_evsel__free_fd(struct perf_evsel *evsel)
1260 xyarray__delete(evsel->fd);
1261 evsel->fd = NULL;
1264 static void perf_evsel__free_id(struct perf_evsel *evsel)
1266 xyarray__delete(evsel->sample_id);
1267 evsel->sample_id = NULL;
1268 zfree(&evsel->id);
1271 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1273 struct perf_evsel_config_term *term, *h;
1275 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1276 list_del(&term->list);
1277 free(term);
1281 void perf_evsel__close_fd(struct perf_evsel *evsel)
1283 int cpu, thread;
1285 for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++)
1286 for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
1287 close(FD(evsel, cpu, thread));
1288 FD(evsel, cpu, thread) = -1;
1292 void perf_evsel__exit(struct perf_evsel *evsel)
1294 assert(list_empty(&evsel->node));
1295 assert(evsel->evlist == NULL);
1296 perf_evsel__free_counts(evsel);
1297 perf_evsel__free_fd(evsel);
1298 perf_evsel__free_id(evsel);
1299 perf_evsel__free_config_terms(evsel);
1300 cgroup__put(evsel->cgrp);
1301 cpu_map__put(evsel->cpus);
1302 cpu_map__put(evsel->own_cpus);
1303 thread_map__put(evsel->threads);
1304 zfree(&evsel->group_name);
1305 zfree(&evsel->name);
1306 perf_evsel__object.fini(evsel);
1309 void perf_evsel__delete(struct perf_evsel *evsel)
1311 perf_evsel__exit(evsel);
1312 free(evsel);
1315 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
1316 struct perf_counts_values *count)
1318 struct perf_counts_values tmp;
1320 if (!evsel->prev_raw_counts)
1321 return;
1323 if (cpu == -1) {
1324 tmp = evsel->prev_raw_counts->aggr;
1325 evsel->prev_raw_counts->aggr = *count;
1326 } else {
1327 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1328 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
1331 count->val = count->val - tmp.val;
1332 count->ena = count->ena - tmp.ena;
1333 count->run = count->run - tmp.run;
1336 void perf_counts_values__scale(struct perf_counts_values *count,
1337 bool scale, s8 *pscaled)
1339 s8 scaled = 0;
1341 if (scale) {
1342 if (count->run == 0) {
1343 scaled = -1;
1344 count->val = 0;
1345 } else if (count->run < count->ena) {
1346 scaled = 1;
1347 count->val = (u64)((double) count->val * count->ena / count->run);
1351 if (pscaled)
1352 *pscaled = scaled;
1355 static int perf_evsel__read_size(struct perf_evsel *evsel)
1357 u64 read_format = evsel->attr.read_format;
1358 int entry = sizeof(u64); /* value */
1359 int size = 0;
1360 int nr = 1;
1362 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1363 size += sizeof(u64);
1365 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1366 size += sizeof(u64);
1368 if (read_format & PERF_FORMAT_ID)
1369 entry += sizeof(u64);
1371 if (read_format & PERF_FORMAT_GROUP) {
1372 nr = evsel->nr_members;
1373 size += sizeof(u64);
1376 size += entry * nr;
1377 return size;
1380 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1381 struct perf_counts_values *count)
1383 size_t size = perf_evsel__read_size(evsel);
1385 memset(count, 0, sizeof(*count));
1387 if (FD(evsel, cpu, thread) < 0)
1388 return -EINVAL;
1390 if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
1391 return -errno;
1393 return 0;
1396 static int
1397 perf_evsel__read_one(struct perf_evsel *evsel, int cpu, int thread)
1399 struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread);
1401 return perf_evsel__read(evsel, cpu, thread, count);
1404 static void
1405 perf_evsel__set_count(struct perf_evsel *counter, int cpu, int thread,
1406 u64 val, u64 ena, u64 run)
1408 struct perf_counts_values *count;
1410 count = perf_counts(counter->counts, cpu, thread);
1412 count->val = val;
1413 count->ena = ena;
1414 count->run = run;
1415 count->loaded = true;
1418 static int
1419 perf_evsel__process_group_data(struct perf_evsel *leader,
1420 int cpu, int thread, u64 *data)
1422 u64 read_format = leader->attr.read_format;
1423 struct sample_read_value *v;
1424 u64 nr, ena = 0, run = 0, i;
1426 nr = *data++;
1428 if (nr != (u64) leader->nr_members)
1429 return -EINVAL;
1431 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1432 ena = *data++;
1434 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1435 run = *data++;
1437 v = (struct sample_read_value *) data;
1439 perf_evsel__set_count(leader, cpu, thread,
1440 v[0].value, ena, run);
1442 for (i = 1; i < nr; i++) {
1443 struct perf_evsel *counter;
1445 counter = perf_evlist__id2evsel(leader->evlist, v[i].id);
1446 if (!counter)
1447 return -EINVAL;
1449 perf_evsel__set_count(counter, cpu, thread,
1450 v[i].value, ena, run);
1453 return 0;
1456 static int
1457 perf_evsel__read_group(struct perf_evsel *leader, int cpu, int thread)
1459 struct perf_stat_evsel *ps = leader->stats;
1460 u64 read_format = leader->attr.read_format;
1461 int size = perf_evsel__read_size(leader);
1462 u64 *data = ps->group_data;
1464 if (!(read_format & PERF_FORMAT_ID))
1465 return -EINVAL;
1467 if (!perf_evsel__is_group_leader(leader))
1468 return -EINVAL;
1470 if (!data) {
1471 data = zalloc(size);
1472 if (!data)
1473 return -ENOMEM;
1475 ps->group_data = data;
1478 if (FD(leader, cpu, thread) < 0)
1479 return -EINVAL;
1481 if (readn(FD(leader, cpu, thread), data, size) <= 0)
1482 return -errno;
1484 return perf_evsel__process_group_data(leader, cpu, thread, data);
1487 int perf_evsel__read_counter(struct perf_evsel *evsel, int cpu, int thread)
1489 u64 read_format = evsel->attr.read_format;
1491 if (read_format & PERF_FORMAT_GROUP)
1492 return perf_evsel__read_group(evsel, cpu, thread);
1493 else
1494 return perf_evsel__read_one(evsel, cpu, thread);
1497 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1498 int cpu, int thread, bool scale)
1500 struct perf_counts_values count;
1501 size_t nv = scale ? 3 : 1;
1503 if (FD(evsel, cpu, thread) < 0)
1504 return -EINVAL;
1506 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1507 return -ENOMEM;
1509 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
1510 return -errno;
1512 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1513 perf_counts_values__scale(&count, scale, NULL);
1514 *perf_counts(evsel->counts, cpu, thread) = count;
1515 return 0;
1518 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1520 struct perf_evsel *leader = evsel->leader;
1521 int fd;
1523 if (perf_evsel__is_group_leader(evsel))
1524 return -1;
1527 * Leader must be already processed/open,
1528 * if not it's a bug.
1530 BUG_ON(!leader->fd);
1532 fd = FD(leader, cpu, thread);
1533 BUG_ON(fd == -1);
1535 return fd;
1538 struct bit_names {
1539 int bit;
1540 const char *name;
1543 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1545 bool first_bit = true;
1546 int i = 0;
1548 do {
1549 if (value & bits[i].bit) {
1550 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1551 first_bit = false;
1553 } while (bits[++i].name != NULL);
1556 static void __p_sample_type(char *buf, size_t size, u64 value)
1558 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1559 struct bit_names bits[] = {
1560 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1561 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1562 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1563 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1564 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
1565 bit_name(WEIGHT), bit_name(PHYS_ADDR),
1566 { .name = NULL, }
1568 #undef bit_name
1569 __p_bits(buf, size, value, bits);
1572 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1574 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1575 struct bit_names bits[] = {
1576 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1577 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1578 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1579 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1580 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1581 { .name = NULL, }
1583 #undef bit_name
1584 __p_bits(buf, size, value, bits);
1587 static void __p_read_format(char *buf, size_t size, u64 value)
1589 #define bit_name(n) { PERF_FORMAT_##n, #n }
1590 struct bit_names bits[] = {
1591 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1592 bit_name(ID), bit_name(GROUP),
1593 { .name = NULL, }
1595 #undef bit_name
1596 __p_bits(buf, size, value, bits);
1599 #define BUF_SIZE 1024
1601 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1602 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1603 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1604 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1605 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1606 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1608 #define PRINT_ATTRn(_n, _f, _p) \
1609 do { \
1610 if (attr->_f) { \
1611 _p(attr->_f); \
1612 ret += attr__fprintf(fp, _n, buf, priv);\
1614 } while (0)
1616 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1618 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1619 attr__fprintf_f attr__fprintf, void *priv)
1621 char buf[BUF_SIZE];
1622 int ret = 0;
1624 PRINT_ATTRf(type, p_unsigned);
1625 PRINT_ATTRf(size, p_unsigned);
1626 PRINT_ATTRf(config, p_hex);
1627 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1628 PRINT_ATTRf(sample_type, p_sample_type);
1629 PRINT_ATTRf(read_format, p_read_format);
1631 PRINT_ATTRf(disabled, p_unsigned);
1632 PRINT_ATTRf(inherit, p_unsigned);
1633 PRINT_ATTRf(pinned, p_unsigned);
1634 PRINT_ATTRf(exclusive, p_unsigned);
1635 PRINT_ATTRf(exclude_user, p_unsigned);
1636 PRINT_ATTRf(exclude_kernel, p_unsigned);
1637 PRINT_ATTRf(exclude_hv, p_unsigned);
1638 PRINT_ATTRf(exclude_idle, p_unsigned);
1639 PRINT_ATTRf(mmap, p_unsigned);
1640 PRINT_ATTRf(comm, p_unsigned);
1641 PRINT_ATTRf(freq, p_unsigned);
1642 PRINT_ATTRf(inherit_stat, p_unsigned);
1643 PRINT_ATTRf(enable_on_exec, p_unsigned);
1644 PRINT_ATTRf(task, p_unsigned);
1645 PRINT_ATTRf(watermark, p_unsigned);
1646 PRINT_ATTRf(precise_ip, p_unsigned);
1647 PRINT_ATTRf(mmap_data, p_unsigned);
1648 PRINT_ATTRf(sample_id_all, p_unsigned);
1649 PRINT_ATTRf(exclude_host, p_unsigned);
1650 PRINT_ATTRf(exclude_guest, p_unsigned);
1651 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1652 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1653 PRINT_ATTRf(mmap2, p_unsigned);
1654 PRINT_ATTRf(comm_exec, p_unsigned);
1655 PRINT_ATTRf(use_clockid, p_unsigned);
1656 PRINT_ATTRf(context_switch, p_unsigned);
1657 PRINT_ATTRf(write_backward, p_unsigned);
1658 PRINT_ATTRf(namespaces, p_unsigned);
1659 PRINT_ATTRf(ksymbol, p_unsigned);
1660 PRINT_ATTRf(bpf_event, p_unsigned);
1662 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1663 PRINT_ATTRf(bp_type, p_unsigned);
1664 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1665 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1666 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
1667 PRINT_ATTRf(sample_regs_user, p_hex);
1668 PRINT_ATTRf(sample_stack_user, p_unsigned);
1669 PRINT_ATTRf(clockid, p_signed);
1670 PRINT_ATTRf(sample_regs_intr, p_hex);
1671 PRINT_ATTRf(aux_watermark, p_unsigned);
1672 PRINT_ATTRf(sample_max_stack, p_unsigned);
1674 return ret;
1677 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1678 void *priv __maybe_unused)
1680 return fprintf(fp, " %-32s %s\n", name, val);
1683 static void perf_evsel__remove_fd(struct perf_evsel *pos,
1684 int nr_cpus, int nr_threads,
1685 int thread_idx)
1687 for (int cpu = 0; cpu < nr_cpus; cpu++)
1688 for (int thread = thread_idx; thread < nr_threads - 1; thread++)
1689 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1);
1692 static int update_fds(struct perf_evsel *evsel,
1693 int nr_cpus, int cpu_idx,
1694 int nr_threads, int thread_idx)
1696 struct perf_evsel *pos;
1698 if (cpu_idx >= nr_cpus || thread_idx >= nr_threads)
1699 return -EINVAL;
1701 evlist__for_each_entry(evsel->evlist, pos) {
1702 nr_cpus = pos != evsel ? nr_cpus : cpu_idx;
1704 perf_evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx);
1707 * Since fds for next evsel has not been created,
1708 * there is no need to iterate whole event list.
1710 if (pos == evsel)
1711 break;
1713 return 0;
1716 static bool ignore_missing_thread(struct perf_evsel *evsel,
1717 int nr_cpus, int cpu,
1718 struct thread_map *threads,
1719 int thread, int err)
1721 pid_t ignore_pid = thread_map__pid(threads, thread);
1723 if (!evsel->ignore_missing_thread)
1724 return false;
1726 /* The system wide setup does not work with threads. */
1727 if (evsel->system_wide)
1728 return false;
1730 /* The -ESRCH is perf event syscall errno for pid's not found. */
1731 if (err != -ESRCH)
1732 return false;
1734 /* If there's only one thread, let it fail. */
1735 if (threads->nr == 1)
1736 return false;
1739 * We should remove fd for missing_thread first
1740 * because thread_map__remove() will decrease threads->nr.
1742 if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread))
1743 return false;
1745 if (thread_map__remove(threads, thread))
1746 return false;
1748 pr_warning("WARNING: Ignored open failure for pid %d\n",
1749 ignore_pid);
1750 return true;
1753 static void display_attr(struct perf_event_attr *attr)
1755 if (verbose >= 2) {
1756 fprintf(stderr, "%.60s\n", graph_dotted_line);
1757 fprintf(stderr, "perf_event_attr:\n");
1758 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL);
1759 fprintf(stderr, "%.60s\n", graph_dotted_line);
1763 static int perf_event_open(struct perf_evsel *evsel,
1764 pid_t pid, int cpu, int group_fd,
1765 unsigned long flags)
1767 int precise_ip = evsel->attr.precise_ip;
1768 int fd;
1770 while (1) {
1771 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
1772 pid, cpu, group_fd, flags);
1774 fd = sys_perf_event_open(&evsel->attr, pid, cpu, group_fd, flags);
1775 if (fd >= 0)
1776 break;
1779 * Do quick precise_ip fallback if:
1780 * - there is precise_ip set in perf_event_attr
1781 * - maximum precise is requested
1782 * - sys_perf_event_open failed with ENOTSUP error,
1783 * which is associated with wrong precise_ip
1785 if (!precise_ip || !evsel->precise_max || (errno != ENOTSUP))
1786 break;
1789 * We tried all the precise_ip values, and it's
1790 * still failing, so leave it to standard fallback.
1792 if (!evsel->attr.precise_ip) {
1793 evsel->attr.precise_ip = precise_ip;
1794 break;
1797 pr_debug2("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
1798 evsel->attr.precise_ip--;
1799 pr_debug2("decreasing precise_ip by one (%d)\n", evsel->attr.precise_ip);
1800 display_attr(&evsel->attr);
1803 return fd;
1806 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1807 struct thread_map *threads)
1809 int cpu, thread, nthreads;
1810 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1811 int pid = -1, err;
1812 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1814 if (perf_missing_features.write_backward && evsel->attr.write_backward)
1815 return -EINVAL;
1817 if (cpus == NULL) {
1818 static struct cpu_map *empty_cpu_map;
1820 if (empty_cpu_map == NULL) {
1821 empty_cpu_map = cpu_map__dummy_new();
1822 if (empty_cpu_map == NULL)
1823 return -ENOMEM;
1826 cpus = empty_cpu_map;
1829 if (threads == NULL) {
1830 static struct thread_map *empty_thread_map;
1832 if (empty_thread_map == NULL) {
1833 empty_thread_map = thread_map__new_by_tid(-1);
1834 if (empty_thread_map == NULL)
1835 return -ENOMEM;
1838 threads = empty_thread_map;
1841 if (evsel->system_wide)
1842 nthreads = 1;
1843 else
1844 nthreads = threads->nr;
1846 if (evsel->fd == NULL &&
1847 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1848 return -ENOMEM;
1850 if (evsel->cgrp) {
1851 flags |= PERF_FLAG_PID_CGROUP;
1852 pid = evsel->cgrp->fd;
1855 fallback_missing_features:
1856 if (perf_missing_features.clockid_wrong)
1857 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1858 if (perf_missing_features.clockid) {
1859 evsel->attr.use_clockid = 0;
1860 evsel->attr.clockid = 0;
1862 if (perf_missing_features.cloexec)
1863 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1864 if (perf_missing_features.mmap2)
1865 evsel->attr.mmap2 = 0;
1866 if (perf_missing_features.exclude_guest)
1867 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1868 if (perf_missing_features.lbr_flags)
1869 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1870 PERF_SAMPLE_BRANCH_NO_CYCLES);
1871 if (perf_missing_features.group_read && evsel->attr.inherit)
1872 evsel->attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID);
1873 if (perf_missing_features.ksymbol)
1874 evsel->attr.ksymbol = 0;
1875 if (perf_missing_features.bpf_event)
1876 evsel->attr.bpf_event = 0;
1877 retry_sample_id:
1878 if (perf_missing_features.sample_id_all)
1879 evsel->attr.sample_id_all = 0;
1881 display_attr(&evsel->attr);
1883 for (cpu = 0; cpu < cpus->nr; cpu++) {
1885 for (thread = 0; thread < nthreads; thread++) {
1886 int fd, group_fd;
1888 if (!evsel->cgrp && !evsel->system_wide)
1889 pid = thread_map__pid(threads, thread);
1891 group_fd = get_group_fd(evsel, cpu, thread);
1892 retry_open:
1893 test_attr__ready();
1895 fd = perf_event_open(evsel, pid, cpus->map[cpu],
1896 group_fd, flags);
1898 FD(evsel, cpu, thread) = fd;
1900 if (fd < 0) {
1901 err = -errno;
1903 if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
1905 * We just removed 1 thread, so take a step
1906 * back on thread index and lower the upper
1907 * nthreads limit.
1909 nthreads--;
1910 thread--;
1912 /* ... and pretend like nothing have happened. */
1913 err = 0;
1914 continue;
1917 pr_debug2("\nsys_perf_event_open failed, error %d\n",
1918 err);
1919 goto try_fallback;
1922 pr_debug2(" = %d\n", fd);
1924 if (evsel->bpf_fd >= 0) {
1925 int evt_fd = fd;
1926 int bpf_fd = evsel->bpf_fd;
1928 err = ioctl(evt_fd,
1929 PERF_EVENT_IOC_SET_BPF,
1930 bpf_fd);
1931 if (err && errno != EEXIST) {
1932 pr_err("failed to attach bpf fd %d: %s\n",
1933 bpf_fd, strerror(errno));
1934 err = -EINVAL;
1935 goto out_close;
1939 set_rlimit = NO_CHANGE;
1942 * If we succeeded but had to kill clockid, fail and
1943 * have perf_evsel__open_strerror() print us a nice
1944 * error.
1946 if (perf_missing_features.clockid ||
1947 perf_missing_features.clockid_wrong) {
1948 err = -EINVAL;
1949 goto out_close;
1954 return 0;
1956 try_fallback:
1958 * perf stat needs between 5 and 22 fds per CPU. When we run out
1959 * of them try to increase the limits.
1961 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1962 struct rlimit l;
1963 int old_errno = errno;
1965 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1966 if (set_rlimit == NO_CHANGE)
1967 l.rlim_cur = l.rlim_max;
1968 else {
1969 l.rlim_cur = l.rlim_max + 1000;
1970 l.rlim_max = l.rlim_cur;
1972 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1973 set_rlimit++;
1974 errno = old_errno;
1975 goto retry_open;
1978 errno = old_errno;
1981 if (err != -EINVAL || cpu > 0 || thread > 0)
1982 goto out_close;
1985 * Must probe features in the order they were added to the
1986 * perf_event_attr interface.
1988 if (!perf_missing_features.bpf_event && evsel->attr.bpf_event) {
1989 perf_missing_features.bpf_event = true;
1990 pr_debug2("switching off bpf_event\n");
1991 goto fallback_missing_features;
1992 } else if (!perf_missing_features.ksymbol && evsel->attr.ksymbol) {
1993 perf_missing_features.ksymbol = true;
1994 pr_debug2("switching off ksymbol\n");
1995 goto fallback_missing_features;
1996 } else if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
1997 perf_missing_features.write_backward = true;
1998 pr_debug2("switching off write_backward\n");
1999 goto out_close;
2000 } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
2001 perf_missing_features.clockid_wrong = true;
2002 pr_debug2("switching off clockid\n");
2003 goto fallback_missing_features;
2004 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
2005 perf_missing_features.clockid = true;
2006 pr_debug2("switching off use_clockid\n");
2007 goto fallback_missing_features;
2008 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
2009 perf_missing_features.cloexec = true;
2010 pr_debug2("switching off cloexec flag\n");
2011 goto fallback_missing_features;
2012 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
2013 perf_missing_features.mmap2 = true;
2014 pr_debug2("switching off mmap2\n");
2015 goto fallback_missing_features;
2016 } else if (!perf_missing_features.exclude_guest &&
2017 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
2018 perf_missing_features.exclude_guest = true;
2019 pr_debug2("switching off exclude_guest, exclude_host\n");
2020 goto fallback_missing_features;
2021 } else if (!perf_missing_features.sample_id_all) {
2022 perf_missing_features.sample_id_all = true;
2023 pr_debug2("switching off sample_id_all\n");
2024 goto retry_sample_id;
2025 } else if (!perf_missing_features.lbr_flags &&
2026 (evsel->attr.branch_sample_type &
2027 (PERF_SAMPLE_BRANCH_NO_CYCLES |
2028 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
2029 perf_missing_features.lbr_flags = true;
2030 pr_debug2("switching off branch sample type no (cycles/flags)\n");
2031 goto fallback_missing_features;
2032 } else if (!perf_missing_features.group_read &&
2033 evsel->attr.inherit &&
2034 (evsel->attr.read_format & PERF_FORMAT_GROUP) &&
2035 perf_evsel__is_group_leader(evsel)) {
2036 perf_missing_features.group_read = true;
2037 pr_debug2("switching off group read\n");
2038 goto fallback_missing_features;
2040 out_close:
2041 if (err)
2042 threads->err_thread = thread;
2044 do {
2045 while (--thread >= 0) {
2046 close(FD(evsel, cpu, thread));
2047 FD(evsel, cpu, thread) = -1;
2049 thread = nthreads;
2050 } while (--cpu >= 0);
2051 return err;
2054 void perf_evsel__close(struct perf_evsel *evsel)
2056 if (evsel->fd == NULL)
2057 return;
2059 perf_evsel__close_fd(evsel);
2060 perf_evsel__free_fd(evsel);
2063 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
2064 struct cpu_map *cpus)
2066 return perf_evsel__open(evsel, cpus, NULL);
2069 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
2070 struct thread_map *threads)
2072 return perf_evsel__open(evsel, NULL, threads);
2075 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
2076 const union perf_event *event,
2077 struct perf_sample *sample)
2079 u64 type = evsel->attr.sample_type;
2080 const u64 *array = event->sample.array;
2081 bool swapped = evsel->needs_swap;
2082 union u64_swap u;
2084 array += ((event->header.size -
2085 sizeof(event->header)) / sizeof(u64)) - 1;
2087 if (type & PERF_SAMPLE_IDENTIFIER) {
2088 sample->id = *array;
2089 array--;
2092 if (type & PERF_SAMPLE_CPU) {
2093 u.val64 = *array;
2094 if (swapped) {
2095 /* undo swap of u64, then swap on individual u32s */
2096 u.val64 = bswap_64(u.val64);
2097 u.val32[0] = bswap_32(u.val32[0]);
2100 sample->cpu = u.val32[0];
2101 array--;
2104 if (type & PERF_SAMPLE_STREAM_ID) {
2105 sample->stream_id = *array;
2106 array--;
2109 if (type & PERF_SAMPLE_ID) {
2110 sample->id = *array;
2111 array--;
2114 if (type & PERF_SAMPLE_TIME) {
2115 sample->time = *array;
2116 array--;
2119 if (type & PERF_SAMPLE_TID) {
2120 u.val64 = *array;
2121 if (swapped) {
2122 /* undo swap of u64, then swap on individual u32s */
2123 u.val64 = bswap_64(u.val64);
2124 u.val32[0] = bswap_32(u.val32[0]);
2125 u.val32[1] = bswap_32(u.val32[1]);
2128 sample->pid = u.val32[0];
2129 sample->tid = u.val32[1];
2130 array--;
2133 return 0;
2136 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
2137 u64 size)
2139 return size > max_size || offset + size > endp;
2142 #define OVERFLOW_CHECK(offset, size, max_size) \
2143 do { \
2144 if (overflow(endp, (max_size), (offset), (size))) \
2145 return -EFAULT; \
2146 } while (0)
2148 #define OVERFLOW_CHECK_u64(offset) \
2149 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2151 static int
2152 perf_event__check_size(union perf_event *event, unsigned int sample_size)
2155 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2156 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2157 * check the format does not go past the end of the event.
2159 if (sample_size + sizeof(event->header) > event->header.size)
2160 return -EFAULT;
2162 return 0;
2165 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
2166 struct perf_sample *data)
2168 u64 type = evsel->attr.sample_type;
2169 bool swapped = evsel->needs_swap;
2170 const u64 *array;
2171 u16 max_size = event->header.size;
2172 const void *endp = (void *)event + max_size;
2173 u64 sz;
2176 * used for cross-endian analysis. See git commit 65014ab3
2177 * for why this goofiness is needed.
2179 union u64_swap u;
2181 memset(data, 0, sizeof(*data));
2182 data->cpu = data->pid = data->tid = -1;
2183 data->stream_id = data->id = data->time = -1ULL;
2184 data->period = evsel->attr.sample_period;
2185 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2186 data->misc = event->header.misc;
2187 data->id = -1ULL;
2188 data->data_src = PERF_MEM_DATA_SRC_NONE;
2190 if (event->header.type != PERF_RECORD_SAMPLE) {
2191 if (!evsel->attr.sample_id_all)
2192 return 0;
2193 return perf_evsel__parse_id_sample(evsel, event, data);
2196 array = event->sample.array;
2198 if (perf_event__check_size(event, evsel->sample_size))
2199 return -EFAULT;
2201 if (type & PERF_SAMPLE_IDENTIFIER) {
2202 data->id = *array;
2203 array++;
2206 if (type & PERF_SAMPLE_IP) {
2207 data->ip = *array;
2208 array++;
2211 if (type & PERF_SAMPLE_TID) {
2212 u.val64 = *array;
2213 if (swapped) {
2214 /* undo swap of u64, then swap on individual u32s */
2215 u.val64 = bswap_64(u.val64);
2216 u.val32[0] = bswap_32(u.val32[0]);
2217 u.val32[1] = bswap_32(u.val32[1]);
2220 data->pid = u.val32[0];
2221 data->tid = u.val32[1];
2222 array++;
2225 if (type & PERF_SAMPLE_TIME) {
2226 data->time = *array;
2227 array++;
2230 if (type & PERF_SAMPLE_ADDR) {
2231 data->addr = *array;
2232 array++;
2235 if (type & PERF_SAMPLE_ID) {
2236 data->id = *array;
2237 array++;
2240 if (type & PERF_SAMPLE_STREAM_ID) {
2241 data->stream_id = *array;
2242 array++;
2245 if (type & PERF_SAMPLE_CPU) {
2247 u.val64 = *array;
2248 if (swapped) {
2249 /* undo swap of u64, then swap on individual u32s */
2250 u.val64 = bswap_64(u.val64);
2251 u.val32[0] = bswap_32(u.val32[0]);
2254 data->cpu = u.val32[0];
2255 array++;
2258 if (type & PERF_SAMPLE_PERIOD) {
2259 data->period = *array;
2260 array++;
2263 if (type & PERF_SAMPLE_READ) {
2264 u64 read_format = evsel->attr.read_format;
2266 OVERFLOW_CHECK_u64(array);
2267 if (read_format & PERF_FORMAT_GROUP)
2268 data->read.group.nr = *array;
2269 else
2270 data->read.one.value = *array;
2272 array++;
2274 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2275 OVERFLOW_CHECK_u64(array);
2276 data->read.time_enabled = *array;
2277 array++;
2280 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2281 OVERFLOW_CHECK_u64(array);
2282 data->read.time_running = *array;
2283 array++;
2286 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2287 if (read_format & PERF_FORMAT_GROUP) {
2288 const u64 max_group_nr = UINT64_MAX /
2289 sizeof(struct sample_read_value);
2291 if (data->read.group.nr > max_group_nr)
2292 return -EFAULT;
2293 sz = data->read.group.nr *
2294 sizeof(struct sample_read_value);
2295 OVERFLOW_CHECK(array, sz, max_size);
2296 data->read.group.values =
2297 (struct sample_read_value *)array;
2298 array = (void *)array + sz;
2299 } else {
2300 OVERFLOW_CHECK_u64(array);
2301 data->read.one.id = *array;
2302 array++;
2306 if (evsel__has_callchain(evsel)) {
2307 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
2309 OVERFLOW_CHECK_u64(array);
2310 data->callchain = (struct ip_callchain *)array++;
2311 if (data->callchain->nr > max_callchain_nr)
2312 return -EFAULT;
2313 sz = data->callchain->nr * sizeof(u64);
2314 OVERFLOW_CHECK(array, sz, max_size);
2315 array = (void *)array + sz;
2318 if (type & PERF_SAMPLE_RAW) {
2319 OVERFLOW_CHECK_u64(array);
2320 u.val64 = *array;
2323 * Undo swap of u64, then swap on individual u32s,
2324 * get the size of the raw area and undo all of the
2325 * swap. The pevent interface handles endianity by
2326 * itself.
2328 if (swapped) {
2329 u.val64 = bswap_64(u.val64);
2330 u.val32[0] = bswap_32(u.val32[0]);
2331 u.val32[1] = bswap_32(u.val32[1]);
2333 data->raw_size = u.val32[0];
2336 * The raw data is aligned on 64bits including the
2337 * u32 size, so it's safe to use mem_bswap_64.
2339 if (swapped)
2340 mem_bswap_64((void *) array, data->raw_size);
2342 array = (void *)array + sizeof(u32);
2344 OVERFLOW_CHECK(array, data->raw_size, max_size);
2345 data->raw_data = (void *)array;
2346 array = (void *)array + data->raw_size;
2349 if (type & PERF_SAMPLE_BRANCH_STACK) {
2350 const u64 max_branch_nr = UINT64_MAX /
2351 sizeof(struct branch_entry);
2353 OVERFLOW_CHECK_u64(array);
2354 data->branch_stack = (struct branch_stack *)array++;
2356 if (data->branch_stack->nr > max_branch_nr)
2357 return -EFAULT;
2358 sz = data->branch_stack->nr * sizeof(struct branch_entry);
2359 OVERFLOW_CHECK(array, sz, max_size);
2360 array = (void *)array + sz;
2363 if (type & PERF_SAMPLE_REGS_USER) {
2364 OVERFLOW_CHECK_u64(array);
2365 data->user_regs.abi = *array;
2366 array++;
2368 if (data->user_regs.abi) {
2369 u64 mask = evsel->attr.sample_regs_user;
2371 sz = hweight64(mask) * sizeof(u64);
2372 OVERFLOW_CHECK(array, sz, max_size);
2373 data->user_regs.mask = mask;
2374 data->user_regs.regs = (u64 *)array;
2375 array = (void *)array + sz;
2379 if (type & PERF_SAMPLE_STACK_USER) {
2380 OVERFLOW_CHECK_u64(array);
2381 sz = *array++;
2383 data->user_stack.offset = ((char *)(array - 1)
2384 - (char *) event);
2386 if (!sz) {
2387 data->user_stack.size = 0;
2388 } else {
2389 OVERFLOW_CHECK(array, sz, max_size);
2390 data->user_stack.data = (char *)array;
2391 array = (void *)array + sz;
2392 OVERFLOW_CHECK_u64(array);
2393 data->user_stack.size = *array++;
2394 if (WARN_ONCE(data->user_stack.size > sz,
2395 "user stack dump failure\n"))
2396 return -EFAULT;
2400 if (type & PERF_SAMPLE_WEIGHT) {
2401 OVERFLOW_CHECK_u64(array);
2402 data->weight = *array;
2403 array++;
2406 if (type & PERF_SAMPLE_DATA_SRC) {
2407 OVERFLOW_CHECK_u64(array);
2408 data->data_src = *array;
2409 array++;
2412 if (type & PERF_SAMPLE_TRANSACTION) {
2413 OVERFLOW_CHECK_u64(array);
2414 data->transaction = *array;
2415 array++;
2418 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2419 if (type & PERF_SAMPLE_REGS_INTR) {
2420 OVERFLOW_CHECK_u64(array);
2421 data->intr_regs.abi = *array;
2422 array++;
2424 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2425 u64 mask = evsel->attr.sample_regs_intr;
2427 sz = hweight64(mask) * sizeof(u64);
2428 OVERFLOW_CHECK(array, sz, max_size);
2429 data->intr_regs.mask = mask;
2430 data->intr_regs.regs = (u64 *)array;
2431 array = (void *)array + sz;
2435 data->phys_addr = 0;
2436 if (type & PERF_SAMPLE_PHYS_ADDR) {
2437 data->phys_addr = *array;
2438 array++;
2441 return 0;
2444 int perf_evsel__parse_sample_timestamp(struct perf_evsel *evsel,
2445 union perf_event *event,
2446 u64 *timestamp)
2448 u64 type = evsel->attr.sample_type;
2449 const u64 *array;
2451 if (!(type & PERF_SAMPLE_TIME))
2452 return -1;
2454 if (event->header.type != PERF_RECORD_SAMPLE) {
2455 struct perf_sample data = {
2456 .time = -1ULL,
2459 if (!evsel->attr.sample_id_all)
2460 return -1;
2461 if (perf_evsel__parse_id_sample(evsel, event, &data))
2462 return -1;
2464 *timestamp = data.time;
2465 return 0;
2468 array = event->sample.array;
2470 if (perf_event__check_size(event, evsel->sample_size))
2471 return -EFAULT;
2473 if (type & PERF_SAMPLE_IDENTIFIER)
2474 array++;
2476 if (type & PERF_SAMPLE_IP)
2477 array++;
2479 if (type & PERF_SAMPLE_TID)
2480 array++;
2482 if (type & PERF_SAMPLE_TIME)
2483 *timestamp = *array;
2485 return 0;
2488 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
2489 u64 read_format)
2491 size_t sz, result = sizeof(struct sample_event);
2493 if (type & PERF_SAMPLE_IDENTIFIER)
2494 result += sizeof(u64);
2496 if (type & PERF_SAMPLE_IP)
2497 result += sizeof(u64);
2499 if (type & PERF_SAMPLE_TID)
2500 result += sizeof(u64);
2502 if (type & PERF_SAMPLE_TIME)
2503 result += sizeof(u64);
2505 if (type & PERF_SAMPLE_ADDR)
2506 result += sizeof(u64);
2508 if (type & PERF_SAMPLE_ID)
2509 result += sizeof(u64);
2511 if (type & PERF_SAMPLE_STREAM_ID)
2512 result += sizeof(u64);
2514 if (type & PERF_SAMPLE_CPU)
2515 result += sizeof(u64);
2517 if (type & PERF_SAMPLE_PERIOD)
2518 result += sizeof(u64);
2520 if (type & PERF_SAMPLE_READ) {
2521 result += sizeof(u64);
2522 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2523 result += sizeof(u64);
2524 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2525 result += sizeof(u64);
2526 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2527 if (read_format & PERF_FORMAT_GROUP) {
2528 sz = sample->read.group.nr *
2529 sizeof(struct sample_read_value);
2530 result += sz;
2531 } else {
2532 result += sizeof(u64);
2536 if (type & PERF_SAMPLE_CALLCHAIN) {
2537 sz = (sample->callchain->nr + 1) * sizeof(u64);
2538 result += sz;
2541 if (type & PERF_SAMPLE_RAW) {
2542 result += sizeof(u32);
2543 result += sample->raw_size;
2546 if (type & PERF_SAMPLE_BRANCH_STACK) {
2547 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2548 sz += sizeof(u64);
2549 result += sz;
2552 if (type & PERF_SAMPLE_REGS_USER) {
2553 if (sample->user_regs.abi) {
2554 result += sizeof(u64);
2555 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
2556 result += sz;
2557 } else {
2558 result += sizeof(u64);
2562 if (type & PERF_SAMPLE_STACK_USER) {
2563 sz = sample->user_stack.size;
2564 result += sizeof(u64);
2565 if (sz) {
2566 result += sz;
2567 result += sizeof(u64);
2571 if (type & PERF_SAMPLE_WEIGHT)
2572 result += sizeof(u64);
2574 if (type & PERF_SAMPLE_DATA_SRC)
2575 result += sizeof(u64);
2577 if (type & PERF_SAMPLE_TRANSACTION)
2578 result += sizeof(u64);
2580 if (type & PERF_SAMPLE_REGS_INTR) {
2581 if (sample->intr_regs.abi) {
2582 result += sizeof(u64);
2583 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
2584 result += sz;
2585 } else {
2586 result += sizeof(u64);
2590 if (type & PERF_SAMPLE_PHYS_ADDR)
2591 result += sizeof(u64);
2593 return result;
2596 int perf_event__synthesize_sample(union perf_event *event, u64 type,
2597 u64 read_format,
2598 const struct perf_sample *sample)
2600 u64 *array;
2601 size_t sz;
2603 * used for cross-endian analysis. See git commit 65014ab3
2604 * for why this goofiness is needed.
2606 union u64_swap u;
2608 array = event->sample.array;
2610 if (type & PERF_SAMPLE_IDENTIFIER) {
2611 *array = sample->id;
2612 array++;
2615 if (type & PERF_SAMPLE_IP) {
2616 *array = sample->ip;
2617 array++;
2620 if (type & PERF_SAMPLE_TID) {
2621 u.val32[0] = sample->pid;
2622 u.val32[1] = sample->tid;
2623 *array = u.val64;
2624 array++;
2627 if (type & PERF_SAMPLE_TIME) {
2628 *array = sample->time;
2629 array++;
2632 if (type & PERF_SAMPLE_ADDR) {
2633 *array = sample->addr;
2634 array++;
2637 if (type & PERF_SAMPLE_ID) {
2638 *array = sample->id;
2639 array++;
2642 if (type & PERF_SAMPLE_STREAM_ID) {
2643 *array = sample->stream_id;
2644 array++;
2647 if (type & PERF_SAMPLE_CPU) {
2648 u.val32[0] = sample->cpu;
2649 u.val32[1] = 0;
2650 *array = u.val64;
2651 array++;
2654 if (type & PERF_SAMPLE_PERIOD) {
2655 *array = sample->period;
2656 array++;
2659 if (type & PERF_SAMPLE_READ) {
2660 if (read_format & PERF_FORMAT_GROUP)
2661 *array = sample->read.group.nr;
2662 else
2663 *array = sample->read.one.value;
2664 array++;
2666 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2667 *array = sample->read.time_enabled;
2668 array++;
2671 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2672 *array = sample->read.time_running;
2673 array++;
2676 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2677 if (read_format & PERF_FORMAT_GROUP) {
2678 sz = sample->read.group.nr *
2679 sizeof(struct sample_read_value);
2680 memcpy(array, sample->read.group.values, sz);
2681 array = (void *)array + sz;
2682 } else {
2683 *array = sample->read.one.id;
2684 array++;
2688 if (type & PERF_SAMPLE_CALLCHAIN) {
2689 sz = (sample->callchain->nr + 1) * sizeof(u64);
2690 memcpy(array, sample->callchain, sz);
2691 array = (void *)array + sz;
2694 if (type & PERF_SAMPLE_RAW) {
2695 u.val32[0] = sample->raw_size;
2696 *array = u.val64;
2697 array = (void *)array + sizeof(u32);
2699 memcpy(array, sample->raw_data, sample->raw_size);
2700 array = (void *)array + sample->raw_size;
2703 if (type & PERF_SAMPLE_BRANCH_STACK) {
2704 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2705 sz += sizeof(u64);
2706 memcpy(array, sample->branch_stack, sz);
2707 array = (void *)array + sz;
2710 if (type & PERF_SAMPLE_REGS_USER) {
2711 if (sample->user_regs.abi) {
2712 *array++ = sample->user_regs.abi;
2713 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
2714 memcpy(array, sample->user_regs.regs, sz);
2715 array = (void *)array + sz;
2716 } else {
2717 *array++ = 0;
2721 if (type & PERF_SAMPLE_STACK_USER) {
2722 sz = sample->user_stack.size;
2723 *array++ = sz;
2724 if (sz) {
2725 memcpy(array, sample->user_stack.data, sz);
2726 array = (void *)array + sz;
2727 *array++ = sz;
2731 if (type & PERF_SAMPLE_WEIGHT) {
2732 *array = sample->weight;
2733 array++;
2736 if (type & PERF_SAMPLE_DATA_SRC) {
2737 *array = sample->data_src;
2738 array++;
2741 if (type & PERF_SAMPLE_TRANSACTION) {
2742 *array = sample->transaction;
2743 array++;
2746 if (type & PERF_SAMPLE_REGS_INTR) {
2747 if (sample->intr_regs.abi) {
2748 *array++ = sample->intr_regs.abi;
2749 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
2750 memcpy(array, sample->intr_regs.regs, sz);
2751 array = (void *)array + sz;
2752 } else {
2753 *array++ = 0;
2757 if (type & PERF_SAMPLE_PHYS_ADDR) {
2758 *array = sample->phys_addr;
2759 array++;
2762 return 0;
2765 struct tep_format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2767 return tep_find_field(evsel->tp_format, name);
2770 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2771 const char *name)
2773 struct tep_format_field *field = perf_evsel__field(evsel, name);
2774 int offset;
2776 if (!field)
2777 return NULL;
2779 offset = field->offset;
2781 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2782 offset = *(int *)(sample->raw_data + field->offset);
2783 offset &= 0xffff;
2786 return sample->raw_data + offset;
2789 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample,
2790 bool needs_swap)
2792 u64 value;
2793 void *ptr = sample->raw_data + field->offset;
2795 switch (field->size) {
2796 case 1:
2797 return *(u8 *)ptr;
2798 case 2:
2799 value = *(u16 *)ptr;
2800 break;
2801 case 4:
2802 value = *(u32 *)ptr;
2803 break;
2804 case 8:
2805 memcpy(&value, ptr, sizeof(u64));
2806 break;
2807 default:
2808 return 0;
2811 if (!needs_swap)
2812 return value;
2814 switch (field->size) {
2815 case 2:
2816 return bswap_16(value);
2817 case 4:
2818 return bswap_32(value);
2819 case 8:
2820 return bswap_64(value);
2821 default:
2822 return 0;
2825 return 0;
2828 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2829 const char *name)
2831 struct tep_format_field *field = perf_evsel__field(evsel, name);
2833 if (!field)
2834 return 0;
2836 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2839 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2840 char *msg, size_t msgsize)
2842 int paranoid;
2844 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2845 evsel->attr.type == PERF_TYPE_HARDWARE &&
2846 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2848 * If it's cycles then fall back to hrtimer based
2849 * cpu-clock-tick sw counter, which is always available even if
2850 * no PMU support.
2852 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2853 * b0a873e).
2855 scnprintf(msg, msgsize, "%s",
2856 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2858 evsel->attr.type = PERF_TYPE_SOFTWARE;
2859 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2861 zfree(&evsel->name);
2862 return true;
2863 } else if (err == EACCES && !evsel->attr.exclude_kernel &&
2864 (paranoid = perf_event_paranoid()) > 1) {
2865 const char *name = perf_evsel__name(evsel);
2866 char *new_name;
2867 const char *sep = ":";
2869 /* Is there already the separator in the name. */
2870 if (strchr(name, '/') ||
2871 strchr(name, ':'))
2872 sep = "";
2874 if (asprintf(&new_name, "%s%su", name, sep) < 0)
2875 return false;
2877 if (evsel->name)
2878 free(evsel->name);
2879 evsel->name = new_name;
2880 scnprintf(msg, msgsize,
2881 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2882 evsel->attr.exclude_kernel = 1;
2884 return true;
2887 return false;
2890 static bool find_process(const char *name)
2892 size_t len = strlen(name);
2893 DIR *dir;
2894 struct dirent *d;
2895 int ret = -1;
2897 dir = opendir(procfs__mountpoint());
2898 if (!dir)
2899 return false;
2901 /* Walk through the directory. */
2902 while (ret && (d = readdir(dir)) != NULL) {
2903 char path[PATH_MAX];
2904 char *data;
2905 size_t size;
2907 if ((d->d_type != DT_DIR) ||
2908 !strcmp(".", d->d_name) ||
2909 !strcmp("..", d->d_name))
2910 continue;
2912 scnprintf(path, sizeof(path), "%s/%s/comm",
2913 procfs__mountpoint(), d->d_name);
2915 if (filename__read_str(path, &data, &size))
2916 continue;
2918 ret = strncmp(name, data, len);
2919 free(data);
2922 closedir(dir);
2923 return ret ? false : true;
2926 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2927 int err, char *msg, size_t size)
2929 char sbuf[STRERR_BUFSIZE];
2930 int printed = 0;
2932 switch (err) {
2933 case EPERM:
2934 case EACCES:
2935 if (err == EPERM)
2936 printed = scnprintf(msg, size,
2937 "No permission to enable %s event.\n\n",
2938 perf_evsel__name(evsel));
2940 return scnprintf(msg + printed, size - printed,
2941 "You may not have permission to collect %sstats.\n\n"
2942 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2943 "which controls use of the performance events system by\n"
2944 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2945 "The current value is %d:\n\n"
2946 " -1: Allow use of (almost) all events by all users\n"
2947 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
2948 ">= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN\n"
2949 " Disallow raw tracepoint access by users without CAP_SYS_ADMIN\n"
2950 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2951 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2952 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2953 " kernel.perf_event_paranoid = -1\n" ,
2954 target->system_wide ? "system-wide " : "",
2955 perf_event_paranoid());
2956 case ENOENT:
2957 return scnprintf(msg, size, "The %s event is not supported.",
2958 perf_evsel__name(evsel));
2959 case EMFILE:
2960 return scnprintf(msg, size, "%s",
2961 "Too many events are opened.\n"
2962 "Probably the maximum number of open file descriptors has been reached.\n"
2963 "Hint: Try again after reducing the number of events.\n"
2964 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2965 case ENOMEM:
2966 if (evsel__has_callchain(evsel) &&
2967 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2968 return scnprintf(msg, size,
2969 "Not enough memory to setup event with callchain.\n"
2970 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2971 "Hint: Current value: %d", sysctl__max_stack());
2972 break;
2973 case ENODEV:
2974 if (target->cpu_list)
2975 return scnprintf(msg, size, "%s",
2976 "No such device - did you specify an out-of-range profile CPU?");
2977 break;
2978 case EOPNOTSUPP:
2979 if (evsel->attr.sample_period != 0)
2980 return scnprintf(msg, size,
2981 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
2982 perf_evsel__name(evsel));
2983 if (evsel->attr.precise_ip)
2984 return scnprintf(msg, size, "%s",
2985 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2986 #if defined(__i386__) || defined(__x86_64__)
2987 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2988 return scnprintf(msg, size, "%s",
2989 "No hardware sampling interrupt available.\n");
2990 #endif
2991 break;
2992 case EBUSY:
2993 if (find_process("oprofiled"))
2994 return scnprintf(msg, size,
2995 "The PMU counters are busy/taken by another profiler.\n"
2996 "We found oprofile daemon running, please stop it and try again.");
2997 break;
2998 case EINVAL:
2999 if (evsel->attr.write_backward && perf_missing_features.write_backward)
3000 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
3001 if (perf_missing_features.clockid)
3002 return scnprintf(msg, size, "clockid feature not supported.");
3003 if (perf_missing_features.clockid_wrong)
3004 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
3005 break;
3006 default:
3007 break;
3010 return scnprintf(msg, size,
3011 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
3012 "/bin/dmesg | grep -i perf may provide additional information.\n",
3013 err, str_error_r(err, sbuf, sizeof(sbuf)),
3014 perf_evsel__name(evsel));
3017 struct perf_env *perf_evsel__env(struct perf_evsel *evsel)
3019 if (evsel && evsel->evlist)
3020 return evsel->evlist->env;
3021 return NULL;
3024 static int store_evsel_ids(struct perf_evsel *evsel, struct perf_evlist *evlist)
3026 int cpu, thread;
3028 for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) {
3029 for (thread = 0; thread < xyarray__max_y(evsel->fd);
3030 thread++) {
3031 int fd = FD(evsel, cpu, thread);
3033 if (perf_evlist__id_add_fd(evlist, evsel,
3034 cpu, thread, fd) < 0)
3035 return -1;
3039 return 0;
3042 int perf_evsel__store_ids(struct perf_evsel *evsel, struct perf_evlist *evlist)
3044 struct cpu_map *cpus = evsel->cpus;
3045 struct thread_map *threads = evsel->threads;
3047 if (perf_evsel__alloc_id(evsel, cpus->nr, threads->nr))
3048 return -ENOMEM;
3050 return store_evsel_ids(evsel, evlist);