2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
11 #include <linux/bitops.h>
12 #include <api/fs/debugfs.h>
13 #include <traceevent/event-parse.h>
14 #include <linux/hw_breakpoint.h>
15 #include <linux/perf_event.h>
16 #include <sys/resource.h>
18 #include "callchain.h"
24 #include "thread_map.h"
26 #include "perf_regs.h"
28 #include "trace-event.h"
38 } perf_missing_features;
40 static clockid_t clockid;
42 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
47 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
53 int (*init)(struct perf_evsel *evsel);
54 void (*fini)(struct perf_evsel *evsel);
55 } perf_evsel__object = {
56 .size = sizeof(struct perf_evsel),
57 .init = perf_evsel__no_extra_init,
58 .fini = perf_evsel__no_extra_fini,
61 int perf_evsel__object_config(size_t object_size,
62 int (*init)(struct perf_evsel *evsel),
63 void (*fini)(struct perf_evsel *evsel))
69 if (perf_evsel__object.size > object_size)
72 perf_evsel__object.size = object_size;
76 perf_evsel__object.init = init;
79 perf_evsel__object.fini = fini;
84 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
86 int __perf_evsel__sample_size(u64 sample_type)
88 u64 mask = sample_type & PERF_SAMPLE_MASK;
92 for (i = 0; i < 64; i++) {
93 if (mask & (1ULL << i))
103 * __perf_evsel__calc_id_pos - calculate id_pos.
104 * @sample_type: sample type
106 * This function returns the position of the event id (PERF_SAMPLE_ID or
107 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
110 static int __perf_evsel__calc_id_pos(u64 sample_type)
114 if (sample_type & PERF_SAMPLE_IDENTIFIER)
117 if (!(sample_type & PERF_SAMPLE_ID))
120 if (sample_type & PERF_SAMPLE_IP)
123 if (sample_type & PERF_SAMPLE_TID)
126 if (sample_type & PERF_SAMPLE_TIME)
129 if (sample_type & PERF_SAMPLE_ADDR)
136 * __perf_evsel__calc_is_pos - calculate is_pos.
137 * @sample_type: sample type
139 * This function returns the position (counting backwards) of the event id
140 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
141 * sample_id_all is used there is an id sample appended to non-sample events.
143 static int __perf_evsel__calc_is_pos(u64 sample_type)
147 if (sample_type & PERF_SAMPLE_IDENTIFIER)
150 if (!(sample_type & PERF_SAMPLE_ID))
153 if (sample_type & PERF_SAMPLE_CPU)
156 if (sample_type & PERF_SAMPLE_STREAM_ID)
162 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
164 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
165 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
168 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
169 enum perf_event_sample_format bit)
171 if (!(evsel->attr.sample_type & bit)) {
172 evsel->attr.sample_type |= bit;
173 evsel->sample_size += sizeof(u64);
174 perf_evsel__calc_id_pos(evsel);
178 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
179 enum perf_event_sample_format bit)
181 if (evsel->attr.sample_type & bit) {
182 evsel->attr.sample_type &= ~bit;
183 evsel->sample_size -= sizeof(u64);
184 perf_evsel__calc_id_pos(evsel);
188 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
189 bool can_sample_identifier)
191 if (can_sample_identifier) {
192 perf_evsel__reset_sample_bit(evsel, ID);
193 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
195 perf_evsel__set_sample_bit(evsel, ID);
197 evsel->attr.read_format |= PERF_FORMAT_ID;
200 void perf_evsel__init(struct perf_evsel *evsel,
201 struct perf_event_attr *attr, int idx)
204 evsel->tracking = !idx;
206 evsel->leader = evsel;
209 INIT_LIST_HEAD(&evsel->node);
210 perf_evsel__object.init(evsel);
211 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
212 perf_evsel__calc_id_pos(evsel);
215 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
217 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
220 perf_evsel__init(evsel, attr, idx);
225 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
227 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
230 struct perf_event_attr attr = {
231 .type = PERF_TYPE_TRACEPOINT,
232 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
233 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
236 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
239 evsel->tp_format = trace_event__tp_format(sys, name);
240 if (evsel->tp_format == NULL)
243 event_attr_init(&attr);
244 attr.config = evsel->tp_format->id;
245 attr.sample_period = 1;
246 perf_evsel__init(evsel, &attr, idx);
257 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
265 "stalled-cycles-frontend",
266 "stalled-cycles-backend",
270 static const char *__perf_evsel__hw_name(u64 config)
272 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
273 return perf_evsel__hw_names[config];
275 return "unknown-hardware";
278 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
280 int colon = 0, r = 0;
281 struct perf_event_attr *attr = &evsel->attr;
282 bool exclude_guest_default = false;
284 #define MOD_PRINT(context, mod) do { \
285 if (!attr->exclude_##context) { \
286 if (!colon) colon = ++r; \
287 r += scnprintf(bf + r, size - r, "%c", mod); \
290 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
291 MOD_PRINT(kernel, 'k');
292 MOD_PRINT(user, 'u');
294 exclude_guest_default = true;
297 if (attr->precise_ip) {
300 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
301 exclude_guest_default = true;
304 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
305 MOD_PRINT(host, 'H');
306 MOD_PRINT(guest, 'G');
314 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
316 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
317 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
320 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
333 static const char *__perf_evsel__sw_name(u64 config)
335 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
336 return perf_evsel__sw_names[config];
337 return "unknown-software";
340 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
342 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
343 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
346 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
350 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
352 if (type & HW_BREAKPOINT_R)
353 r += scnprintf(bf + r, size - r, "r");
355 if (type & HW_BREAKPOINT_W)
356 r += scnprintf(bf + r, size - r, "w");
358 if (type & HW_BREAKPOINT_X)
359 r += scnprintf(bf + r, size - r, "x");
364 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
366 struct perf_event_attr *attr = &evsel->attr;
367 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
368 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
371 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
372 [PERF_EVSEL__MAX_ALIASES] = {
373 { "L1-dcache", "l1-d", "l1d", "L1-data", },
374 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
376 { "dTLB", "d-tlb", "Data-TLB", },
377 { "iTLB", "i-tlb", "Instruction-TLB", },
378 { "branch", "branches", "bpu", "btb", "bpc", },
382 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
383 [PERF_EVSEL__MAX_ALIASES] = {
384 { "load", "loads", "read", },
385 { "store", "stores", "write", },
386 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
389 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
390 [PERF_EVSEL__MAX_ALIASES] = {
391 { "refs", "Reference", "ops", "access", },
392 { "misses", "miss", },
395 #define C(x) PERF_COUNT_HW_CACHE_##x
396 #define CACHE_READ (1 << C(OP_READ))
397 #define CACHE_WRITE (1 << C(OP_WRITE))
398 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
399 #define COP(x) (1 << x)
402 * cache operartion stat
403 * L1I : Read and prefetch only
404 * ITLB and BPU : Read-only
406 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
407 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
408 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
409 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
410 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
411 [C(ITLB)] = (CACHE_READ),
412 [C(BPU)] = (CACHE_READ),
413 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
416 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
418 if (perf_evsel__hw_cache_stat[type] & COP(op))
419 return true; /* valid */
421 return false; /* invalid */
424 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
425 char *bf, size_t size)
428 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
429 perf_evsel__hw_cache_op[op][0],
430 perf_evsel__hw_cache_result[result][0]);
433 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
434 perf_evsel__hw_cache_op[op][1]);
437 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
439 u8 op, result, type = (config >> 0) & 0xff;
440 const char *err = "unknown-ext-hardware-cache-type";
442 if (type > PERF_COUNT_HW_CACHE_MAX)
445 op = (config >> 8) & 0xff;
446 err = "unknown-ext-hardware-cache-op";
447 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
450 result = (config >> 16) & 0xff;
451 err = "unknown-ext-hardware-cache-result";
452 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
455 err = "invalid-cache";
456 if (!perf_evsel__is_cache_op_valid(type, op))
459 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
461 return scnprintf(bf, size, "%s", err);
464 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
466 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
467 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
470 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
472 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
473 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
476 const char *perf_evsel__name(struct perf_evsel *evsel)
483 switch (evsel->attr.type) {
485 perf_evsel__raw_name(evsel, bf, sizeof(bf));
488 case PERF_TYPE_HARDWARE:
489 perf_evsel__hw_name(evsel, bf, sizeof(bf));
492 case PERF_TYPE_HW_CACHE:
493 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
496 case PERF_TYPE_SOFTWARE:
497 perf_evsel__sw_name(evsel, bf, sizeof(bf));
500 case PERF_TYPE_TRACEPOINT:
501 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
504 case PERF_TYPE_BREAKPOINT:
505 perf_evsel__bp_name(evsel, bf, sizeof(bf));
509 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
514 evsel->name = strdup(bf);
516 return evsel->name ?: "unknown";
519 const char *perf_evsel__group_name(struct perf_evsel *evsel)
521 return evsel->group_name ?: "anon group";
524 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
527 struct perf_evsel *pos;
528 const char *group_name = perf_evsel__group_name(evsel);
530 ret = scnprintf(buf, size, "%s", group_name);
532 ret += scnprintf(buf + ret, size - ret, " { %s",
533 perf_evsel__name(evsel));
535 for_each_group_member(pos, evsel)
536 ret += scnprintf(buf + ret, size - ret, ", %s",
537 perf_evsel__name(pos));
539 ret += scnprintf(buf + ret, size - ret, " }");
545 perf_evsel__config_callgraph(struct perf_evsel *evsel,
546 struct record_opts *opts)
548 bool function = perf_evsel__is_function_event(evsel);
549 struct perf_event_attr *attr = &evsel->attr;
551 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
553 if (callchain_param.record_mode == CALLCHAIN_LBR) {
554 if (!opts->branch_stack) {
555 if (attr->exclude_user) {
556 pr_warning("LBR callstack option is only available "
557 "to get user callchain information. "
558 "Falling back to framepointers.\n");
560 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
561 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
562 PERF_SAMPLE_BRANCH_CALL_STACK;
565 pr_warning("Cannot use LBR callstack with branch stack. "
566 "Falling back to framepointers.\n");
569 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
571 perf_evsel__set_sample_bit(evsel, REGS_USER);
572 perf_evsel__set_sample_bit(evsel, STACK_USER);
573 attr->sample_regs_user = PERF_REGS_MASK;
574 attr->sample_stack_user = callchain_param.dump_size;
575 attr->exclude_callchain_user = 1;
577 pr_info("Cannot use DWARF unwind for function trace event,"
578 " falling back to framepointers.\n");
583 pr_info("Disabling user space callchains for function trace event.\n");
584 attr->exclude_callchain_user = 1;
589 * The enable_on_exec/disabled value strategy:
591 * 1) For any type of traced program:
592 * - all independent events and group leaders are disabled
593 * - all group members are enabled
595 * Group members are ruled by group leaders. They need to
596 * be enabled, because the group scheduling relies on that.
598 * 2) For traced programs executed by perf:
599 * - all independent events and group leaders have
601 * - we don't specifically enable or disable any event during
604 * Independent events and group leaders are initially disabled
605 * and get enabled by exec. Group members are ruled by group
606 * leaders as stated in 1).
608 * 3) For traced programs attached by perf (pid/tid):
609 * - we specifically enable or disable all events during
612 * When attaching events to already running traced we
613 * enable/disable events specifically, as there's no
614 * initial traced exec call.
616 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
618 struct perf_evsel *leader = evsel->leader;
619 struct perf_event_attr *attr = &evsel->attr;
620 int track = evsel->tracking;
621 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
623 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
624 attr->inherit = !opts->no_inherit;
626 perf_evsel__set_sample_bit(evsel, IP);
627 perf_evsel__set_sample_bit(evsel, TID);
629 if (evsel->sample_read) {
630 perf_evsel__set_sample_bit(evsel, READ);
633 * We need ID even in case of single event, because
634 * PERF_SAMPLE_READ process ID specific data.
636 perf_evsel__set_sample_id(evsel, false);
639 * Apply group format only if we belong to group
640 * with more than one members.
642 if (leader->nr_members > 1) {
643 attr->read_format |= PERF_FORMAT_GROUP;
649 * We default some events to have a default interval. But keep
650 * it a weak assumption overridable by the user.
652 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
653 opts->user_interval != ULLONG_MAX)) {
655 perf_evsel__set_sample_bit(evsel, PERIOD);
657 attr->sample_freq = opts->freq;
659 attr->sample_period = opts->default_interval;
664 * Disable sampling for all group members other
665 * than leader in case leader 'leads' the sampling.
667 if ((leader != evsel) && leader->sample_read) {
668 attr->sample_freq = 0;
669 attr->sample_period = 0;
672 if (opts->no_samples)
673 attr->sample_freq = 0;
675 if (opts->inherit_stat)
676 attr->inherit_stat = 1;
678 if (opts->sample_address) {
679 perf_evsel__set_sample_bit(evsel, ADDR);
680 attr->mmap_data = track;
684 * We don't allow user space callchains for function trace
685 * event, due to issues with page faults while tracing page
686 * fault handler and its overall trickiness nature.
688 if (perf_evsel__is_function_event(evsel))
689 evsel->attr.exclude_callchain_user = 1;
691 if (callchain_param.enabled && !evsel->no_aux_samples)
692 perf_evsel__config_callgraph(evsel, opts);
694 if (opts->sample_intr_regs) {
695 attr->sample_regs_intr = PERF_REGS_MASK;
696 perf_evsel__set_sample_bit(evsel, REGS_INTR);
699 if (target__has_cpu(&opts->target))
700 perf_evsel__set_sample_bit(evsel, CPU);
703 perf_evsel__set_sample_bit(evsel, PERIOD);
706 * When the user explicitely disabled time don't force it here.
708 if (opts->sample_time &&
709 (!perf_missing_features.sample_id_all &&
710 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
711 perf_evsel__set_sample_bit(evsel, TIME);
713 if (opts->raw_samples && !evsel->no_aux_samples) {
714 perf_evsel__set_sample_bit(evsel, TIME);
715 perf_evsel__set_sample_bit(evsel, RAW);
716 perf_evsel__set_sample_bit(evsel, CPU);
719 if (opts->sample_address)
720 perf_evsel__set_sample_bit(evsel, DATA_SRC);
722 if (opts->no_buffering) {
724 attr->wakeup_events = 1;
726 if (opts->branch_stack && !evsel->no_aux_samples) {
727 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
728 attr->branch_sample_type = opts->branch_stack;
731 if (opts->sample_weight)
732 perf_evsel__set_sample_bit(evsel, WEIGHT);
736 attr->mmap2 = track && !perf_missing_features.mmap2;
739 if (opts->sample_transaction)
740 perf_evsel__set_sample_bit(evsel, TRANSACTION);
742 if (opts->running_time) {
743 evsel->attr.read_format |=
744 PERF_FORMAT_TOTAL_TIME_ENABLED |
745 PERF_FORMAT_TOTAL_TIME_RUNNING;
749 * XXX see the function comment above
751 * Disabling only independent events or group leaders,
752 * keeping group members enabled.
754 if (perf_evsel__is_group_leader(evsel))
758 * Setting enable_on_exec for independent events and
759 * group leaders for traced executed by perf.
761 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
762 !opts->initial_delay)
763 attr->enable_on_exec = 1;
765 if (evsel->immediate) {
767 attr->enable_on_exec = 0;
770 clockid = opts->clockid;
771 if (opts->use_clockid) {
772 attr->use_clockid = 1;
773 attr->clockid = opts->clockid;
777 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
781 if (evsel->system_wide)
784 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
787 for (cpu = 0; cpu < ncpus; cpu++) {
788 for (thread = 0; thread < nthreads; thread++) {
789 FD(evsel, cpu, thread) = -1;
794 return evsel->fd != NULL ? 0 : -ENOMEM;
797 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
802 if (evsel->system_wide)
805 for (cpu = 0; cpu < ncpus; cpu++) {
806 for (thread = 0; thread < nthreads; thread++) {
807 int fd = FD(evsel, cpu, thread),
808 err = ioctl(fd, ioc, arg);
818 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
821 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
822 PERF_EVENT_IOC_SET_FILTER,
826 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
828 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
829 PERF_EVENT_IOC_ENABLE,
833 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
835 if (ncpus == 0 || nthreads == 0)
838 if (evsel->system_wide)
841 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
842 if (evsel->sample_id == NULL)
845 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
846 if (evsel->id == NULL) {
847 xyarray__delete(evsel->sample_id);
848 evsel->sample_id = NULL;
855 static void perf_evsel__free_fd(struct perf_evsel *evsel)
857 xyarray__delete(evsel->fd);
861 static void perf_evsel__free_id(struct perf_evsel *evsel)
863 xyarray__delete(evsel->sample_id);
864 evsel->sample_id = NULL;
868 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
872 if (evsel->system_wide)
875 for (cpu = 0; cpu < ncpus; cpu++)
876 for (thread = 0; thread < nthreads; ++thread) {
877 close(FD(evsel, cpu, thread));
878 FD(evsel, cpu, thread) = -1;
882 void perf_evsel__exit(struct perf_evsel *evsel)
884 assert(list_empty(&evsel->node));
885 perf_evsel__free_fd(evsel);
886 perf_evsel__free_id(evsel);
887 close_cgroup(evsel->cgrp);
888 cpu_map__put(evsel->cpus);
889 thread_map__put(evsel->threads);
890 zfree(&evsel->group_name);
892 perf_evsel__object.fini(evsel);
895 void perf_evsel__delete(struct perf_evsel *evsel)
897 perf_evsel__exit(evsel);
901 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu,
902 struct perf_counts_values *count)
904 struct perf_counts_values tmp;
906 if (!evsel->prev_raw_counts)
910 tmp = evsel->prev_raw_counts->aggr;
911 evsel->prev_raw_counts->aggr = *count;
913 tmp = evsel->prev_raw_counts->cpu[cpu];
914 evsel->prev_raw_counts->cpu[cpu] = *count;
917 count->val = count->val - tmp.val;
918 count->ena = count->ena - tmp.ena;
919 count->run = count->run - tmp.run;
922 void perf_counts_values__scale(struct perf_counts_values *count,
923 bool scale, s8 *pscaled)
928 if (count->run == 0) {
931 } else if (count->run < count->ena) {
933 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
936 count->ena = count->run = 0;
942 int perf_evsel__read_cb(struct perf_evsel *evsel, int cpu, int thread,
943 perf_evsel__read_cb_t cb)
945 struct perf_counts_values count;
947 memset(&count, 0, sizeof(count));
949 if (FD(evsel, cpu, thread) < 0)
952 if (readn(FD(evsel, cpu, thread), &count, sizeof(count)) < 0)
955 return cb(evsel, cpu, thread, &count);
958 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
959 int cpu, int thread, bool scale)
961 struct perf_counts_values count;
962 size_t nv = scale ? 3 : 1;
964 if (FD(evsel, cpu, thread) < 0)
967 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
970 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
973 perf_evsel__compute_deltas(evsel, cpu, &count);
974 perf_counts_values__scale(&count, scale, NULL);
975 evsel->counts->cpu[cpu] = count;
979 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
981 struct perf_evsel *leader = evsel->leader;
984 if (perf_evsel__is_group_leader(evsel))
988 * Leader must be already processed/open,
993 fd = FD(leader, cpu, thread);
1004 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1006 bool first_bit = true;
1010 if (value & bits[i].bit) {
1011 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1014 } while (bits[++i].name != NULL);
1017 static void __p_sample_type(char *buf, size_t size, u64 value)
1019 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1020 struct bit_names bits[] = {
1021 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1022 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1023 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1024 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1025 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1029 __p_bits(buf, size, value, bits);
1032 static void __p_read_format(char *buf, size_t size, u64 value)
1034 #define bit_name(n) { PERF_FORMAT_##n, #n }
1035 struct bit_names bits[] = {
1036 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1037 bit_name(ID), bit_name(GROUP),
1041 __p_bits(buf, size, value, bits);
1044 #define BUF_SIZE 1024
1046 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1047 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1048 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1049 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1050 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1052 #define PRINT_ATTRn(_n, _f, _p) \
1056 ret += attr__fprintf(fp, _n, buf, priv);\
1060 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1062 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1063 attr__fprintf_f attr__fprintf, void *priv)
1068 PRINT_ATTRf(type, p_unsigned);
1069 PRINT_ATTRf(size, p_unsigned);
1070 PRINT_ATTRf(config, p_hex);
1071 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1072 PRINT_ATTRf(sample_type, p_sample_type);
1073 PRINT_ATTRf(read_format, p_read_format);
1075 PRINT_ATTRf(disabled, p_unsigned);
1076 PRINT_ATTRf(inherit, p_unsigned);
1077 PRINT_ATTRf(pinned, p_unsigned);
1078 PRINT_ATTRf(exclusive, p_unsigned);
1079 PRINT_ATTRf(exclude_user, p_unsigned);
1080 PRINT_ATTRf(exclude_kernel, p_unsigned);
1081 PRINT_ATTRf(exclude_hv, p_unsigned);
1082 PRINT_ATTRf(exclude_idle, p_unsigned);
1083 PRINT_ATTRf(mmap, p_unsigned);
1084 PRINT_ATTRf(comm, p_unsigned);
1085 PRINT_ATTRf(freq, p_unsigned);
1086 PRINT_ATTRf(inherit_stat, p_unsigned);
1087 PRINT_ATTRf(enable_on_exec, p_unsigned);
1088 PRINT_ATTRf(task, p_unsigned);
1089 PRINT_ATTRf(watermark, p_unsigned);
1090 PRINT_ATTRf(precise_ip, p_unsigned);
1091 PRINT_ATTRf(mmap_data, p_unsigned);
1092 PRINT_ATTRf(sample_id_all, p_unsigned);
1093 PRINT_ATTRf(exclude_host, p_unsigned);
1094 PRINT_ATTRf(exclude_guest, p_unsigned);
1095 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1096 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1097 PRINT_ATTRf(mmap2, p_unsigned);
1098 PRINT_ATTRf(comm_exec, p_unsigned);
1099 PRINT_ATTRf(use_clockid, p_unsigned);
1101 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1102 PRINT_ATTRf(bp_type, p_unsigned);
1103 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1104 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1105 PRINT_ATTRf(sample_regs_user, p_hex);
1106 PRINT_ATTRf(sample_stack_user, p_unsigned);
1107 PRINT_ATTRf(clockid, p_signed);
1108 PRINT_ATTRf(sample_regs_intr, p_hex);
1109 PRINT_ATTRf(aux_watermark, p_unsigned);
1114 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1115 void *priv __attribute__((unused)))
1117 return fprintf(fp, " %-32s %s\n", name, val);
1120 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1121 struct thread_map *threads)
1123 int cpu, thread, nthreads;
1124 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1126 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1128 if (evsel->system_wide)
1131 nthreads = threads->nr;
1133 if (evsel->fd == NULL &&
1134 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1138 flags |= PERF_FLAG_PID_CGROUP;
1139 pid = evsel->cgrp->fd;
1142 fallback_missing_features:
1143 if (perf_missing_features.clockid_wrong)
1144 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1145 if (perf_missing_features.clockid) {
1146 evsel->attr.use_clockid = 0;
1147 evsel->attr.clockid = 0;
1149 if (perf_missing_features.cloexec)
1150 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1151 if (perf_missing_features.mmap2)
1152 evsel->attr.mmap2 = 0;
1153 if (perf_missing_features.exclude_guest)
1154 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1156 if (perf_missing_features.sample_id_all)
1157 evsel->attr.sample_id_all = 0;
1160 fprintf(stderr, "%.60s\n", graph_dotted_line);
1161 fprintf(stderr, "perf_event_attr:\n");
1162 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1163 fprintf(stderr, "%.60s\n", graph_dotted_line);
1166 for (cpu = 0; cpu < cpus->nr; cpu++) {
1168 for (thread = 0; thread < nthreads; thread++) {
1171 if (!evsel->cgrp && !evsel->system_wide)
1172 pid = thread_map__pid(threads, thread);
1174 group_fd = get_group_fd(evsel, cpu, thread);
1176 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1177 pid, cpus->map[cpu], group_fd, flags);
1179 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1183 if (FD(evsel, cpu, thread) < 0) {
1185 pr_debug2("sys_perf_event_open failed, error %d\n",
1189 set_rlimit = NO_CHANGE;
1192 * If we succeeded but had to kill clockid, fail and
1193 * have perf_evsel__open_strerror() print us a nice
1196 if (perf_missing_features.clockid ||
1197 perf_missing_features.clockid_wrong) {
1208 * perf stat needs between 5 and 22 fds per CPU. When we run out
1209 * of them try to increase the limits.
1211 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1213 int old_errno = errno;
1215 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1216 if (set_rlimit == NO_CHANGE)
1217 l.rlim_cur = l.rlim_max;
1219 l.rlim_cur = l.rlim_max + 1000;
1220 l.rlim_max = l.rlim_cur;
1222 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1231 if (err != -EINVAL || cpu > 0 || thread > 0)
1235 * Must probe features in the order they were added to the
1236 * perf_event_attr interface.
1238 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1239 perf_missing_features.clockid_wrong = true;
1240 goto fallback_missing_features;
1241 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1242 perf_missing_features.clockid = true;
1243 goto fallback_missing_features;
1244 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1245 perf_missing_features.cloexec = true;
1246 goto fallback_missing_features;
1247 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1248 perf_missing_features.mmap2 = true;
1249 goto fallback_missing_features;
1250 } else if (!perf_missing_features.exclude_guest &&
1251 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1252 perf_missing_features.exclude_guest = true;
1253 goto fallback_missing_features;
1254 } else if (!perf_missing_features.sample_id_all) {
1255 perf_missing_features.sample_id_all = true;
1256 goto retry_sample_id;
1261 while (--thread >= 0) {
1262 close(FD(evsel, cpu, thread));
1263 FD(evsel, cpu, thread) = -1;
1266 } while (--cpu >= 0);
1270 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1272 if (evsel->fd == NULL)
1275 perf_evsel__close_fd(evsel, ncpus, nthreads);
1276 perf_evsel__free_fd(evsel);
1288 struct thread_map map;
1290 } empty_thread_map = {
1295 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1296 struct thread_map *threads)
1299 /* Work around old compiler warnings about strict aliasing */
1300 cpus = &empty_cpu_map.map;
1303 if (threads == NULL)
1304 threads = &empty_thread_map.map;
1306 return __perf_evsel__open(evsel, cpus, threads);
1309 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1310 struct cpu_map *cpus)
1312 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1315 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1316 struct thread_map *threads)
1318 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1321 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1322 const union perf_event *event,
1323 struct perf_sample *sample)
1325 u64 type = evsel->attr.sample_type;
1326 const u64 *array = event->sample.array;
1327 bool swapped = evsel->needs_swap;
1330 array += ((event->header.size -
1331 sizeof(event->header)) / sizeof(u64)) - 1;
1333 if (type & PERF_SAMPLE_IDENTIFIER) {
1334 sample->id = *array;
1338 if (type & PERF_SAMPLE_CPU) {
1341 /* undo swap of u64, then swap on individual u32s */
1342 u.val64 = bswap_64(u.val64);
1343 u.val32[0] = bswap_32(u.val32[0]);
1346 sample->cpu = u.val32[0];
1350 if (type & PERF_SAMPLE_STREAM_ID) {
1351 sample->stream_id = *array;
1355 if (type & PERF_SAMPLE_ID) {
1356 sample->id = *array;
1360 if (type & PERF_SAMPLE_TIME) {
1361 sample->time = *array;
1365 if (type & PERF_SAMPLE_TID) {
1368 /* undo swap of u64, then swap on individual u32s */
1369 u.val64 = bswap_64(u.val64);
1370 u.val32[0] = bswap_32(u.val32[0]);
1371 u.val32[1] = bswap_32(u.val32[1]);
1374 sample->pid = u.val32[0];
1375 sample->tid = u.val32[1];
1382 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1385 return size > max_size || offset + size > endp;
1388 #define OVERFLOW_CHECK(offset, size, max_size) \
1390 if (overflow(endp, (max_size), (offset), (size))) \
1394 #define OVERFLOW_CHECK_u64(offset) \
1395 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1397 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1398 struct perf_sample *data)
1400 u64 type = evsel->attr.sample_type;
1401 bool swapped = evsel->needs_swap;
1403 u16 max_size = event->header.size;
1404 const void *endp = (void *)event + max_size;
1408 * used for cross-endian analysis. See git commit 65014ab3
1409 * for why this goofiness is needed.
1413 memset(data, 0, sizeof(*data));
1414 data->cpu = data->pid = data->tid = -1;
1415 data->stream_id = data->id = data->time = -1ULL;
1416 data->period = evsel->attr.sample_period;
1419 if (event->header.type != PERF_RECORD_SAMPLE) {
1420 if (!evsel->attr.sample_id_all)
1422 return perf_evsel__parse_id_sample(evsel, event, data);
1425 array = event->sample.array;
1428 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1429 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1430 * check the format does not go past the end of the event.
1432 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1436 if (type & PERF_SAMPLE_IDENTIFIER) {
1441 if (type & PERF_SAMPLE_IP) {
1446 if (type & PERF_SAMPLE_TID) {
1449 /* undo swap of u64, then swap on individual u32s */
1450 u.val64 = bswap_64(u.val64);
1451 u.val32[0] = bswap_32(u.val32[0]);
1452 u.val32[1] = bswap_32(u.val32[1]);
1455 data->pid = u.val32[0];
1456 data->tid = u.val32[1];
1460 if (type & PERF_SAMPLE_TIME) {
1461 data->time = *array;
1466 if (type & PERF_SAMPLE_ADDR) {
1467 data->addr = *array;
1471 if (type & PERF_SAMPLE_ID) {
1476 if (type & PERF_SAMPLE_STREAM_ID) {
1477 data->stream_id = *array;
1481 if (type & PERF_SAMPLE_CPU) {
1485 /* undo swap of u64, then swap on individual u32s */
1486 u.val64 = bswap_64(u.val64);
1487 u.val32[0] = bswap_32(u.val32[0]);
1490 data->cpu = u.val32[0];
1494 if (type & PERF_SAMPLE_PERIOD) {
1495 data->period = *array;
1499 if (type & PERF_SAMPLE_READ) {
1500 u64 read_format = evsel->attr.read_format;
1502 OVERFLOW_CHECK_u64(array);
1503 if (read_format & PERF_FORMAT_GROUP)
1504 data->read.group.nr = *array;
1506 data->read.one.value = *array;
1510 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1511 OVERFLOW_CHECK_u64(array);
1512 data->read.time_enabled = *array;
1516 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1517 OVERFLOW_CHECK_u64(array);
1518 data->read.time_running = *array;
1522 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1523 if (read_format & PERF_FORMAT_GROUP) {
1524 const u64 max_group_nr = UINT64_MAX /
1525 sizeof(struct sample_read_value);
1527 if (data->read.group.nr > max_group_nr)
1529 sz = data->read.group.nr *
1530 sizeof(struct sample_read_value);
1531 OVERFLOW_CHECK(array, sz, max_size);
1532 data->read.group.values =
1533 (struct sample_read_value *)array;
1534 array = (void *)array + sz;
1536 OVERFLOW_CHECK_u64(array);
1537 data->read.one.id = *array;
1542 if (type & PERF_SAMPLE_CALLCHAIN) {
1543 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1545 OVERFLOW_CHECK_u64(array);
1546 data->callchain = (struct ip_callchain *)array++;
1547 if (data->callchain->nr > max_callchain_nr)
1549 sz = data->callchain->nr * sizeof(u64);
1550 OVERFLOW_CHECK(array, sz, max_size);
1551 array = (void *)array + sz;
1554 if (type & PERF_SAMPLE_RAW) {
1555 OVERFLOW_CHECK_u64(array);
1557 if (WARN_ONCE(swapped,
1558 "Endianness of raw data not corrected!\n")) {
1559 /* undo swap of u64, then swap on individual u32s */
1560 u.val64 = bswap_64(u.val64);
1561 u.val32[0] = bswap_32(u.val32[0]);
1562 u.val32[1] = bswap_32(u.val32[1]);
1564 data->raw_size = u.val32[0];
1565 array = (void *)array + sizeof(u32);
1567 OVERFLOW_CHECK(array, data->raw_size, max_size);
1568 data->raw_data = (void *)array;
1569 array = (void *)array + data->raw_size;
1572 if (type & PERF_SAMPLE_BRANCH_STACK) {
1573 const u64 max_branch_nr = UINT64_MAX /
1574 sizeof(struct branch_entry);
1576 OVERFLOW_CHECK_u64(array);
1577 data->branch_stack = (struct branch_stack *)array++;
1579 if (data->branch_stack->nr > max_branch_nr)
1581 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1582 OVERFLOW_CHECK(array, sz, max_size);
1583 array = (void *)array + sz;
1586 if (type & PERF_SAMPLE_REGS_USER) {
1587 OVERFLOW_CHECK_u64(array);
1588 data->user_regs.abi = *array;
1591 if (data->user_regs.abi) {
1592 u64 mask = evsel->attr.sample_regs_user;
1594 sz = hweight_long(mask) * sizeof(u64);
1595 OVERFLOW_CHECK(array, sz, max_size);
1596 data->user_regs.mask = mask;
1597 data->user_regs.regs = (u64 *)array;
1598 array = (void *)array + sz;
1602 if (type & PERF_SAMPLE_STACK_USER) {
1603 OVERFLOW_CHECK_u64(array);
1606 data->user_stack.offset = ((char *)(array - 1)
1610 data->user_stack.size = 0;
1612 OVERFLOW_CHECK(array, sz, max_size);
1613 data->user_stack.data = (char *)array;
1614 array = (void *)array + sz;
1615 OVERFLOW_CHECK_u64(array);
1616 data->user_stack.size = *array++;
1617 if (WARN_ONCE(data->user_stack.size > sz,
1618 "user stack dump failure\n"))
1624 if (type & PERF_SAMPLE_WEIGHT) {
1625 OVERFLOW_CHECK_u64(array);
1626 data->weight = *array;
1630 data->data_src = PERF_MEM_DATA_SRC_NONE;
1631 if (type & PERF_SAMPLE_DATA_SRC) {
1632 OVERFLOW_CHECK_u64(array);
1633 data->data_src = *array;
1637 data->transaction = 0;
1638 if (type & PERF_SAMPLE_TRANSACTION) {
1639 OVERFLOW_CHECK_u64(array);
1640 data->transaction = *array;
1644 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1645 if (type & PERF_SAMPLE_REGS_INTR) {
1646 OVERFLOW_CHECK_u64(array);
1647 data->intr_regs.abi = *array;
1650 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1651 u64 mask = evsel->attr.sample_regs_intr;
1653 sz = hweight_long(mask) * sizeof(u64);
1654 OVERFLOW_CHECK(array, sz, max_size);
1655 data->intr_regs.mask = mask;
1656 data->intr_regs.regs = (u64 *)array;
1657 array = (void *)array + sz;
1664 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1667 size_t sz, result = sizeof(struct sample_event);
1669 if (type & PERF_SAMPLE_IDENTIFIER)
1670 result += sizeof(u64);
1672 if (type & PERF_SAMPLE_IP)
1673 result += sizeof(u64);
1675 if (type & PERF_SAMPLE_TID)
1676 result += sizeof(u64);
1678 if (type & PERF_SAMPLE_TIME)
1679 result += sizeof(u64);
1681 if (type & PERF_SAMPLE_ADDR)
1682 result += sizeof(u64);
1684 if (type & PERF_SAMPLE_ID)
1685 result += sizeof(u64);
1687 if (type & PERF_SAMPLE_STREAM_ID)
1688 result += sizeof(u64);
1690 if (type & PERF_SAMPLE_CPU)
1691 result += sizeof(u64);
1693 if (type & PERF_SAMPLE_PERIOD)
1694 result += sizeof(u64);
1696 if (type & PERF_SAMPLE_READ) {
1697 result += sizeof(u64);
1698 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1699 result += sizeof(u64);
1700 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1701 result += sizeof(u64);
1702 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1703 if (read_format & PERF_FORMAT_GROUP) {
1704 sz = sample->read.group.nr *
1705 sizeof(struct sample_read_value);
1708 result += sizeof(u64);
1712 if (type & PERF_SAMPLE_CALLCHAIN) {
1713 sz = (sample->callchain->nr + 1) * sizeof(u64);
1717 if (type & PERF_SAMPLE_RAW) {
1718 result += sizeof(u32);
1719 result += sample->raw_size;
1722 if (type & PERF_SAMPLE_BRANCH_STACK) {
1723 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1728 if (type & PERF_SAMPLE_REGS_USER) {
1729 if (sample->user_regs.abi) {
1730 result += sizeof(u64);
1731 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1734 result += sizeof(u64);
1738 if (type & PERF_SAMPLE_STACK_USER) {
1739 sz = sample->user_stack.size;
1740 result += sizeof(u64);
1743 result += sizeof(u64);
1747 if (type & PERF_SAMPLE_WEIGHT)
1748 result += sizeof(u64);
1750 if (type & PERF_SAMPLE_DATA_SRC)
1751 result += sizeof(u64);
1753 if (type & PERF_SAMPLE_TRANSACTION)
1754 result += sizeof(u64);
1756 if (type & PERF_SAMPLE_REGS_INTR) {
1757 if (sample->intr_regs.abi) {
1758 result += sizeof(u64);
1759 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1762 result += sizeof(u64);
1769 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1771 const struct perf_sample *sample,
1777 * used for cross-endian analysis. See git commit 65014ab3
1778 * for why this goofiness is needed.
1782 array = event->sample.array;
1784 if (type & PERF_SAMPLE_IDENTIFIER) {
1785 *array = sample->id;
1789 if (type & PERF_SAMPLE_IP) {
1790 *array = sample->ip;
1794 if (type & PERF_SAMPLE_TID) {
1795 u.val32[0] = sample->pid;
1796 u.val32[1] = sample->tid;
1799 * Inverse of what is done in perf_evsel__parse_sample
1801 u.val32[0] = bswap_32(u.val32[0]);
1802 u.val32[1] = bswap_32(u.val32[1]);
1803 u.val64 = bswap_64(u.val64);
1810 if (type & PERF_SAMPLE_TIME) {
1811 *array = sample->time;
1815 if (type & PERF_SAMPLE_ADDR) {
1816 *array = sample->addr;
1820 if (type & PERF_SAMPLE_ID) {
1821 *array = sample->id;
1825 if (type & PERF_SAMPLE_STREAM_ID) {
1826 *array = sample->stream_id;
1830 if (type & PERF_SAMPLE_CPU) {
1831 u.val32[0] = sample->cpu;
1834 * Inverse of what is done in perf_evsel__parse_sample
1836 u.val32[0] = bswap_32(u.val32[0]);
1837 u.val64 = bswap_64(u.val64);
1843 if (type & PERF_SAMPLE_PERIOD) {
1844 *array = sample->period;
1848 if (type & PERF_SAMPLE_READ) {
1849 if (read_format & PERF_FORMAT_GROUP)
1850 *array = sample->read.group.nr;
1852 *array = sample->read.one.value;
1855 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1856 *array = sample->read.time_enabled;
1860 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1861 *array = sample->read.time_running;
1865 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1866 if (read_format & PERF_FORMAT_GROUP) {
1867 sz = sample->read.group.nr *
1868 sizeof(struct sample_read_value);
1869 memcpy(array, sample->read.group.values, sz);
1870 array = (void *)array + sz;
1872 *array = sample->read.one.id;
1877 if (type & PERF_SAMPLE_CALLCHAIN) {
1878 sz = (sample->callchain->nr + 1) * sizeof(u64);
1879 memcpy(array, sample->callchain, sz);
1880 array = (void *)array + sz;
1883 if (type & PERF_SAMPLE_RAW) {
1884 u.val32[0] = sample->raw_size;
1885 if (WARN_ONCE(swapped,
1886 "Endianness of raw data not corrected!\n")) {
1888 * Inverse of what is done in perf_evsel__parse_sample
1890 u.val32[0] = bswap_32(u.val32[0]);
1891 u.val32[1] = bswap_32(u.val32[1]);
1892 u.val64 = bswap_64(u.val64);
1895 array = (void *)array + sizeof(u32);
1897 memcpy(array, sample->raw_data, sample->raw_size);
1898 array = (void *)array + sample->raw_size;
1901 if (type & PERF_SAMPLE_BRANCH_STACK) {
1902 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1904 memcpy(array, sample->branch_stack, sz);
1905 array = (void *)array + sz;
1908 if (type & PERF_SAMPLE_REGS_USER) {
1909 if (sample->user_regs.abi) {
1910 *array++ = sample->user_regs.abi;
1911 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1912 memcpy(array, sample->user_regs.regs, sz);
1913 array = (void *)array + sz;
1919 if (type & PERF_SAMPLE_STACK_USER) {
1920 sz = sample->user_stack.size;
1923 memcpy(array, sample->user_stack.data, sz);
1924 array = (void *)array + sz;
1929 if (type & PERF_SAMPLE_WEIGHT) {
1930 *array = sample->weight;
1934 if (type & PERF_SAMPLE_DATA_SRC) {
1935 *array = sample->data_src;
1939 if (type & PERF_SAMPLE_TRANSACTION) {
1940 *array = sample->transaction;
1944 if (type & PERF_SAMPLE_REGS_INTR) {
1945 if (sample->intr_regs.abi) {
1946 *array++ = sample->intr_regs.abi;
1947 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1948 memcpy(array, sample->intr_regs.regs, sz);
1949 array = (void *)array + sz;
1958 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1960 return pevent_find_field(evsel->tp_format, name);
1963 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1966 struct format_field *field = perf_evsel__field(evsel, name);
1972 offset = field->offset;
1974 if (field->flags & FIELD_IS_DYNAMIC) {
1975 offset = *(int *)(sample->raw_data + field->offset);
1979 return sample->raw_data + offset;
1982 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1985 struct format_field *field = perf_evsel__field(evsel, name);
1992 ptr = sample->raw_data + field->offset;
1994 switch (field->size) {
1998 value = *(u16 *)ptr;
2001 value = *(u32 *)ptr;
2004 memcpy(&value, ptr, sizeof(u64));
2010 if (!evsel->needs_swap)
2013 switch (field->size) {
2015 return bswap_16(value);
2017 return bswap_32(value);
2019 return bswap_64(value);
2027 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2033 ret += fprintf(fp, ",");
2035 ret += fprintf(fp, ":");
2039 va_start(args, fmt);
2040 ret += vfprintf(fp, fmt, args);
2045 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2047 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2050 int perf_evsel__fprintf(struct perf_evsel *evsel,
2051 struct perf_attr_details *details, FILE *fp)
2056 if (details->event_group) {
2057 struct perf_evsel *pos;
2059 if (!perf_evsel__is_group_leader(evsel))
2062 if (evsel->nr_members > 1)
2063 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2065 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2066 for_each_group_member(pos, evsel)
2067 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2069 if (evsel->nr_members > 1)
2070 printed += fprintf(fp, "}");
2074 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2076 if (details->verbose) {
2077 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2078 __print_attr__fprintf, &first);
2079 } else if (details->freq) {
2080 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2081 (u64)evsel->attr.sample_freq);
2088 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2089 char *msg, size_t msgsize)
2091 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2092 evsel->attr.type == PERF_TYPE_HARDWARE &&
2093 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2095 * If it's cycles then fall back to hrtimer based
2096 * cpu-clock-tick sw counter, which is always available even if
2099 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2102 scnprintf(msg, msgsize, "%s",
2103 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2105 evsel->attr.type = PERF_TYPE_SOFTWARE;
2106 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2108 zfree(&evsel->name);
2115 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2116 int err, char *msg, size_t size)
2118 char sbuf[STRERR_BUFSIZE];
2123 return scnprintf(msg, size,
2124 "You may not have permission to collect %sstats.\n"
2125 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2126 " -1 - Not paranoid at all\n"
2127 " 0 - Disallow raw tracepoint access for unpriv\n"
2128 " 1 - Disallow cpu events for unpriv\n"
2129 " 2 - Disallow kernel profiling for unpriv",
2130 target->system_wide ? "system-wide " : "");
2132 return scnprintf(msg, size, "The %s event is not supported.",
2133 perf_evsel__name(evsel));
2135 return scnprintf(msg, size, "%s",
2136 "Too many events are opened.\n"
2137 "Probably the maximum number of open file descriptors has been reached.\n"
2138 "Hint: Try again after reducing the number of events.\n"
2139 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2141 if (target->cpu_list)
2142 return scnprintf(msg, size, "%s",
2143 "No such device - did you specify an out-of-range profile CPU?\n");
2146 if (evsel->attr.precise_ip)
2147 return scnprintf(msg, size, "%s",
2148 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2149 #if defined(__i386__) || defined(__x86_64__)
2150 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2151 return scnprintf(msg, size, "%s",
2152 "No hardware sampling interrupt available.\n"
2153 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2157 if (find_process("oprofiled"))
2158 return scnprintf(msg, size,
2159 "The PMU counters are busy/taken by another profiler.\n"
2160 "We found oprofile daemon running, please stop it and try again.");
2163 if (perf_missing_features.clockid)
2164 return scnprintf(msg, size, "clockid feature not supported.");
2165 if (perf_missing_features.clockid_wrong)
2166 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2172 return scnprintf(msg, size,
2173 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2174 "/bin/dmesg may provide additional information.\n"
2175 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2176 err, strerror_r(err, sbuf, sizeof(sbuf)),
2177 perf_evsel__name(evsel));