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"
23 #include "thread_map.h"
25 #include "perf_regs.h"
27 #include "trace-event.h"
34 } perf_missing_features;
36 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
41 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
47 int (*init)(struct perf_evsel *evsel);
48 void (*fini)(struct perf_evsel *evsel);
49 } perf_evsel__object = {
50 .size = sizeof(struct perf_evsel),
51 .init = perf_evsel__no_extra_init,
52 .fini = perf_evsel__no_extra_fini,
55 int perf_evsel__object_config(size_t object_size,
56 int (*init)(struct perf_evsel *evsel),
57 void (*fini)(struct perf_evsel *evsel))
63 if (perf_evsel__object.size > object_size)
66 perf_evsel__object.size = object_size;
70 perf_evsel__object.init = init;
73 perf_evsel__object.fini = fini;
78 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
80 int __perf_evsel__sample_size(u64 sample_type)
82 u64 mask = sample_type & PERF_SAMPLE_MASK;
86 for (i = 0; i < 64; i++) {
87 if (mask & (1ULL << i))
97 * __perf_evsel__calc_id_pos - calculate id_pos.
98 * @sample_type: sample type
100 * This function returns the position of the event id (PERF_SAMPLE_ID or
101 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
104 static int __perf_evsel__calc_id_pos(u64 sample_type)
108 if (sample_type & PERF_SAMPLE_IDENTIFIER)
111 if (!(sample_type & PERF_SAMPLE_ID))
114 if (sample_type & PERF_SAMPLE_IP)
117 if (sample_type & PERF_SAMPLE_TID)
120 if (sample_type & PERF_SAMPLE_TIME)
123 if (sample_type & PERF_SAMPLE_ADDR)
130 * __perf_evsel__calc_is_pos - calculate is_pos.
131 * @sample_type: sample type
133 * This function returns the position (counting backwards) of the event id
134 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
135 * sample_id_all is used there is an id sample appended to non-sample events.
137 static int __perf_evsel__calc_is_pos(u64 sample_type)
141 if (sample_type & PERF_SAMPLE_IDENTIFIER)
144 if (!(sample_type & PERF_SAMPLE_ID))
147 if (sample_type & PERF_SAMPLE_CPU)
150 if (sample_type & PERF_SAMPLE_STREAM_ID)
156 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
158 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
159 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
162 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
163 enum perf_event_sample_format bit)
165 if (!(evsel->attr.sample_type & bit)) {
166 evsel->attr.sample_type |= bit;
167 evsel->sample_size += sizeof(u64);
168 perf_evsel__calc_id_pos(evsel);
172 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
173 enum perf_event_sample_format bit)
175 if (evsel->attr.sample_type & bit) {
176 evsel->attr.sample_type &= ~bit;
177 evsel->sample_size -= sizeof(u64);
178 perf_evsel__calc_id_pos(evsel);
182 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
183 bool can_sample_identifier)
185 if (can_sample_identifier) {
186 perf_evsel__reset_sample_bit(evsel, ID);
187 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
189 perf_evsel__set_sample_bit(evsel, ID);
191 evsel->attr.read_format |= PERF_FORMAT_ID;
194 void perf_evsel__init(struct perf_evsel *evsel,
195 struct perf_event_attr *attr, int idx)
198 evsel->tracking = !idx;
200 evsel->leader = evsel;
203 INIT_LIST_HEAD(&evsel->node);
204 perf_evsel__object.init(evsel);
205 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
206 perf_evsel__calc_id_pos(evsel);
209 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
211 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
214 perf_evsel__init(evsel, attr, idx);
219 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
221 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
224 struct perf_event_attr attr = {
225 .type = PERF_TYPE_TRACEPOINT,
226 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
227 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
230 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
233 evsel->tp_format = trace_event__tp_format(sys, name);
234 if (evsel->tp_format == NULL)
237 event_attr_init(&attr);
238 attr.config = evsel->tp_format->id;
239 attr.sample_period = 1;
240 perf_evsel__init(evsel, &attr, idx);
251 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
259 "stalled-cycles-frontend",
260 "stalled-cycles-backend",
264 static const char *__perf_evsel__hw_name(u64 config)
266 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
267 return perf_evsel__hw_names[config];
269 return "unknown-hardware";
272 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
274 int colon = 0, r = 0;
275 struct perf_event_attr *attr = &evsel->attr;
276 bool exclude_guest_default = false;
278 #define MOD_PRINT(context, mod) do { \
279 if (!attr->exclude_##context) { \
280 if (!colon) colon = ++r; \
281 r += scnprintf(bf + r, size - r, "%c", mod); \
284 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
285 MOD_PRINT(kernel, 'k');
286 MOD_PRINT(user, 'u');
288 exclude_guest_default = true;
291 if (attr->precise_ip) {
294 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
295 exclude_guest_default = true;
298 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
299 MOD_PRINT(host, 'H');
300 MOD_PRINT(guest, 'G');
308 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
310 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
311 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
314 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
327 static const char *__perf_evsel__sw_name(u64 config)
329 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
330 return perf_evsel__sw_names[config];
331 return "unknown-software";
334 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
336 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
337 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
340 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
344 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
346 if (type & HW_BREAKPOINT_R)
347 r += scnprintf(bf + r, size - r, "r");
349 if (type & HW_BREAKPOINT_W)
350 r += scnprintf(bf + r, size - r, "w");
352 if (type & HW_BREAKPOINT_X)
353 r += scnprintf(bf + r, size - r, "x");
358 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
360 struct perf_event_attr *attr = &evsel->attr;
361 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
362 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
365 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
366 [PERF_EVSEL__MAX_ALIASES] = {
367 { "L1-dcache", "l1-d", "l1d", "L1-data", },
368 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
370 { "dTLB", "d-tlb", "Data-TLB", },
371 { "iTLB", "i-tlb", "Instruction-TLB", },
372 { "branch", "branches", "bpu", "btb", "bpc", },
376 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
377 [PERF_EVSEL__MAX_ALIASES] = {
378 { "load", "loads", "read", },
379 { "store", "stores", "write", },
380 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
383 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
384 [PERF_EVSEL__MAX_ALIASES] = {
385 { "refs", "Reference", "ops", "access", },
386 { "misses", "miss", },
389 #define C(x) PERF_COUNT_HW_CACHE_##x
390 #define CACHE_READ (1 << C(OP_READ))
391 #define CACHE_WRITE (1 << C(OP_WRITE))
392 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
393 #define COP(x) (1 << x)
396 * cache operartion stat
397 * L1I : Read and prefetch only
398 * ITLB and BPU : Read-only
400 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
401 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
402 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
403 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
404 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405 [C(ITLB)] = (CACHE_READ),
406 [C(BPU)] = (CACHE_READ),
407 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
410 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
412 if (perf_evsel__hw_cache_stat[type] & COP(op))
413 return true; /* valid */
415 return false; /* invalid */
418 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
419 char *bf, size_t size)
422 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
423 perf_evsel__hw_cache_op[op][0],
424 perf_evsel__hw_cache_result[result][0]);
427 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
428 perf_evsel__hw_cache_op[op][1]);
431 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
433 u8 op, result, type = (config >> 0) & 0xff;
434 const char *err = "unknown-ext-hardware-cache-type";
436 if (type > PERF_COUNT_HW_CACHE_MAX)
439 op = (config >> 8) & 0xff;
440 err = "unknown-ext-hardware-cache-op";
441 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
444 result = (config >> 16) & 0xff;
445 err = "unknown-ext-hardware-cache-result";
446 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
449 err = "invalid-cache";
450 if (!perf_evsel__is_cache_op_valid(type, op))
453 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
455 return scnprintf(bf, size, "%s", err);
458 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
460 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
461 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
464 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
466 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
467 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
470 const char *perf_evsel__name(struct perf_evsel *evsel)
477 switch (evsel->attr.type) {
479 perf_evsel__raw_name(evsel, bf, sizeof(bf));
482 case PERF_TYPE_HARDWARE:
483 perf_evsel__hw_name(evsel, bf, sizeof(bf));
486 case PERF_TYPE_HW_CACHE:
487 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
490 case PERF_TYPE_SOFTWARE:
491 perf_evsel__sw_name(evsel, bf, sizeof(bf));
494 case PERF_TYPE_TRACEPOINT:
495 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
498 case PERF_TYPE_BREAKPOINT:
499 perf_evsel__bp_name(evsel, bf, sizeof(bf));
503 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
508 evsel->name = strdup(bf);
510 return evsel->name ?: "unknown";
513 const char *perf_evsel__group_name(struct perf_evsel *evsel)
515 return evsel->group_name ?: "anon group";
518 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
521 struct perf_evsel *pos;
522 const char *group_name = perf_evsel__group_name(evsel);
524 ret = scnprintf(buf, size, "%s", group_name);
526 ret += scnprintf(buf + ret, size - ret, " { %s",
527 perf_evsel__name(evsel));
529 for_each_group_member(pos, evsel)
530 ret += scnprintf(buf + ret, size - ret, ", %s",
531 perf_evsel__name(pos));
533 ret += scnprintf(buf + ret, size - ret, " }");
539 perf_evsel__config_callgraph(struct perf_evsel *evsel)
541 bool function = perf_evsel__is_function_event(evsel);
542 struct perf_event_attr *attr = &evsel->attr;
544 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
546 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
548 perf_evsel__set_sample_bit(evsel, REGS_USER);
549 perf_evsel__set_sample_bit(evsel, STACK_USER);
550 attr->sample_regs_user = PERF_REGS_MASK;
551 attr->sample_stack_user = callchain_param.dump_size;
552 attr->exclude_callchain_user = 1;
554 pr_info("Cannot use DWARF unwind for function trace event,"
555 " falling back to framepointers.\n");
560 pr_info("Disabling user space callchains for function trace event.\n");
561 attr->exclude_callchain_user = 1;
566 * The enable_on_exec/disabled value strategy:
568 * 1) For any type of traced program:
569 * - all independent events and group leaders are disabled
570 * - all group members are enabled
572 * Group members are ruled by group leaders. They need to
573 * be enabled, because the group scheduling relies on that.
575 * 2) For traced programs executed by perf:
576 * - all independent events and group leaders have
578 * - we don't specifically enable or disable any event during
581 * Independent events and group leaders are initially disabled
582 * and get enabled by exec. Group members are ruled by group
583 * leaders as stated in 1).
585 * 3) For traced programs attached by perf (pid/tid):
586 * - we specifically enable or disable all events during
589 * When attaching events to already running traced we
590 * enable/disable events specifically, as there's no
591 * initial traced exec call.
593 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
595 struct perf_evsel *leader = evsel->leader;
596 struct perf_event_attr *attr = &evsel->attr;
597 int track = evsel->tracking;
598 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
600 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
601 attr->inherit = !opts->no_inherit;
603 perf_evsel__set_sample_bit(evsel, IP);
604 perf_evsel__set_sample_bit(evsel, TID);
606 if (evsel->sample_read) {
607 perf_evsel__set_sample_bit(evsel, READ);
610 * We need ID even in case of single event, because
611 * PERF_SAMPLE_READ process ID specific data.
613 perf_evsel__set_sample_id(evsel, false);
616 * Apply group format only if we belong to group
617 * with more than one members.
619 if (leader->nr_members > 1) {
620 attr->read_format |= PERF_FORMAT_GROUP;
626 * We default some events to have a default interval. But keep
627 * it a weak assumption overridable by the user.
629 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
630 opts->user_interval != ULLONG_MAX)) {
632 perf_evsel__set_sample_bit(evsel, PERIOD);
634 attr->sample_freq = opts->freq;
636 attr->sample_period = opts->default_interval;
641 * Disable sampling for all group members other
642 * than leader in case leader 'leads' the sampling.
644 if ((leader != evsel) && leader->sample_read) {
645 attr->sample_freq = 0;
646 attr->sample_period = 0;
649 if (opts->no_samples)
650 attr->sample_freq = 0;
652 if (opts->inherit_stat)
653 attr->inherit_stat = 1;
655 if (opts->sample_address) {
656 perf_evsel__set_sample_bit(evsel, ADDR);
657 attr->mmap_data = track;
660 if (callchain_param.enabled && !evsel->no_aux_samples)
661 perf_evsel__config_callgraph(evsel);
663 if (target__has_cpu(&opts->target))
664 perf_evsel__set_sample_bit(evsel, CPU);
667 perf_evsel__set_sample_bit(evsel, PERIOD);
670 * When the user explicitely disabled time don't force it here.
672 if (opts->sample_time &&
673 (!perf_missing_features.sample_id_all &&
674 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
675 perf_evsel__set_sample_bit(evsel, TIME);
677 if (opts->raw_samples && !evsel->no_aux_samples) {
678 perf_evsel__set_sample_bit(evsel, TIME);
679 perf_evsel__set_sample_bit(evsel, RAW);
680 perf_evsel__set_sample_bit(evsel, CPU);
683 if (opts->sample_address)
684 perf_evsel__set_sample_bit(evsel, DATA_SRC);
686 if (opts->no_buffering) {
688 attr->wakeup_events = 1;
690 if (opts->branch_stack && !evsel->no_aux_samples) {
691 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
692 attr->branch_sample_type = opts->branch_stack;
695 if (opts->sample_weight)
696 perf_evsel__set_sample_bit(evsel, WEIGHT);
699 attr->mmap2 = track && !perf_missing_features.mmap2;
702 if (opts->sample_transaction)
703 perf_evsel__set_sample_bit(evsel, TRANSACTION);
706 * XXX see the function comment above
708 * Disabling only independent events or group leaders,
709 * keeping group members enabled.
711 if (perf_evsel__is_group_leader(evsel))
715 * Setting enable_on_exec for independent events and
716 * group leaders for traced executed by perf.
718 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
719 !opts->initial_delay)
720 attr->enable_on_exec = 1;
722 if (evsel->immediate) {
724 attr->enable_on_exec = 0;
728 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
732 if (evsel->system_wide)
735 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
738 for (cpu = 0; cpu < ncpus; cpu++) {
739 for (thread = 0; thread < nthreads; thread++) {
740 FD(evsel, cpu, thread) = -1;
745 return evsel->fd != NULL ? 0 : -ENOMEM;
748 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
753 if (evsel->system_wide)
756 for (cpu = 0; cpu < ncpus; cpu++) {
757 for (thread = 0; thread < nthreads; thread++) {
758 int fd = FD(evsel, cpu, thread),
759 err = ioctl(fd, ioc, arg);
769 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
772 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
773 PERF_EVENT_IOC_SET_FILTER,
777 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
779 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
780 PERF_EVENT_IOC_ENABLE,
784 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
786 if (evsel->system_wide)
789 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
790 if (evsel->sample_id == NULL)
793 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
794 if (evsel->id == NULL) {
795 xyarray__delete(evsel->sample_id);
796 evsel->sample_id = NULL;
803 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
805 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
806 (ncpus * sizeof(struct perf_counts_values))));
809 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
811 evsel->counts = zalloc((sizeof(*evsel->counts) +
812 (ncpus * sizeof(struct perf_counts_values))));
813 return evsel->counts != NULL ? 0 : -ENOMEM;
816 void perf_evsel__free_fd(struct perf_evsel *evsel)
818 xyarray__delete(evsel->fd);
822 void perf_evsel__free_id(struct perf_evsel *evsel)
824 xyarray__delete(evsel->sample_id);
825 evsel->sample_id = NULL;
829 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
833 if (evsel->system_wide)
836 for (cpu = 0; cpu < ncpus; cpu++)
837 for (thread = 0; thread < nthreads; ++thread) {
838 close(FD(evsel, cpu, thread));
839 FD(evsel, cpu, thread) = -1;
843 void perf_evsel__free_counts(struct perf_evsel *evsel)
845 zfree(&evsel->counts);
848 void perf_evsel__exit(struct perf_evsel *evsel)
850 assert(list_empty(&evsel->node));
851 perf_evsel__free_fd(evsel);
852 perf_evsel__free_id(evsel);
853 perf_evsel__object.fini(evsel);
856 void perf_evsel__delete(struct perf_evsel *evsel)
858 perf_evsel__exit(evsel);
859 close_cgroup(evsel->cgrp);
860 zfree(&evsel->group_name);
861 if (evsel->tp_format)
862 pevent_free_format(evsel->tp_format);
867 static inline void compute_deltas(struct perf_evsel *evsel,
869 struct perf_counts_values *count)
871 struct perf_counts_values tmp;
873 if (!evsel->prev_raw_counts)
877 tmp = evsel->prev_raw_counts->aggr;
878 evsel->prev_raw_counts->aggr = *count;
880 tmp = evsel->prev_raw_counts->cpu[cpu];
881 evsel->prev_raw_counts->cpu[cpu] = *count;
884 count->val = count->val - tmp.val;
885 count->ena = count->ena - tmp.ena;
886 count->run = count->run - tmp.run;
889 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
890 int cpu, int thread, bool scale)
892 struct perf_counts_values count;
893 size_t nv = scale ? 3 : 1;
895 if (FD(evsel, cpu, thread) < 0)
898 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
901 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
904 compute_deltas(evsel, cpu, &count);
909 else if (count.run < count.ena)
910 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
912 count.ena = count.run = 0;
914 evsel->counts->cpu[cpu] = count;
918 int __perf_evsel__read(struct perf_evsel *evsel,
919 int ncpus, int nthreads, bool scale)
921 size_t nv = scale ? 3 : 1;
923 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
925 if (evsel->system_wide)
928 aggr->val = aggr->ena = aggr->run = 0;
930 for (cpu = 0; cpu < ncpus; cpu++) {
931 for (thread = 0; thread < nthreads; thread++) {
932 if (FD(evsel, cpu, thread) < 0)
935 if (readn(FD(evsel, cpu, thread),
936 &count, nv * sizeof(u64)) < 0)
939 aggr->val += count.val;
941 aggr->ena += count.ena;
942 aggr->run += count.run;
947 compute_deltas(evsel, -1, aggr);
949 evsel->counts->scaled = 0;
951 if (aggr->run == 0) {
952 evsel->counts->scaled = -1;
957 if (aggr->run < aggr->ena) {
958 evsel->counts->scaled = 1;
959 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
962 aggr->ena = aggr->run = 0;
967 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
969 struct perf_evsel *leader = evsel->leader;
972 if (perf_evsel__is_group_leader(evsel))
976 * Leader must be already processed/open,
981 fd = FD(leader, cpu, thread);
987 #define __PRINT_ATTR(fmt, cast, field) \
988 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
990 #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
991 #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
992 #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
993 #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
995 #define PRINT_ATTR2N(name1, field1, name2, field2) \
996 fprintf(fp, " %-19s %u %-19s %u\n", \
997 name1, attr->field1, name2, attr->field2)
999 #define PRINT_ATTR2(field1, field2) \
1000 PRINT_ATTR2N(#field1, field1, #field2, field2)
1002 static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
1006 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1007 ret += fprintf(fp, "perf_event_attr:\n");
1009 ret += PRINT_ATTR_U32(type);
1010 ret += PRINT_ATTR_U32(size);
1011 ret += PRINT_ATTR_X64(config);
1012 ret += PRINT_ATTR_U64(sample_period);
1013 ret += PRINT_ATTR_U64(sample_freq);
1014 ret += PRINT_ATTR_X64(sample_type);
1015 ret += PRINT_ATTR_X64(read_format);
1017 ret += PRINT_ATTR2(disabled, inherit);
1018 ret += PRINT_ATTR2(pinned, exclusive);
1019 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
1020 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
1021 ret += PRINT_ATTR2(mmap, comm);
1022 ret += PRINT_ATTR2(mmap2, comm_exec);
1023 ret += PRINT_ATTR2(freq, inherit_stat);
1024 ret += PRINT_ATTR2(enable_on_exec, task);
1025 ret += PRINT_ATTR2(watermark, precise_ip);
1026 ret += PRINT_ATTR2(mmap_data, sample_id_all);
1027 ret += PRINT_ATTR2(exclude_host, exclude_guest);
1028 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
1029 "excl.callchain_user", exclude_callchain_user);
1031 ret += PRINT_ATTR_U32(wakeup_events);
1032 ret += PRINT_ATTR_U32(wakeup_watermark);
1033 ret += PRINT_ATTR_X32(bp_type);
1034 ret += PRINT_ATTR_X64(bp_addr);
1035 ret += PRINT_ATTR_X64(config1);
1036 ret += PRINT_ATTR_U64(bp_len);
1037 ret += PRINT_ATTR_X64(config2);
1038 ret += PRINT_ATTR_X64(branch_sample_type);
1039 ret += PRINT_ATTR_X64(sample_regs_user);
1040 ret += PRINT_ATTR_U32(sample_stack_user);
1042 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1047 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1048 struct thread_map *threads)
1050 int cpu, thread, nthreads;
1051 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1053 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1055 if (evsel->system_wide)
1058 nthreads = threads->nr;
1060 if (evsel->fd == NULL &&
1061 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1065 flags |= PERF_FLAG_PID_CGROUP;
1066 pid = evsel->cgrp->fd;
1069 fallback_missing_features:
1070 if (perf_missing_features.cloexec)
1071 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1072 if (perf_missing_features.mmap2)
1073 evsel->attr.mmap2 = 0;
1074 if (perf_missing_features.exclude_guest)
1075 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1077 if (perf_missing_features.sample_id_all)
1078 evsel->attr.sample_id_all = 0;
1081 perf_event_attr__fprintf(&evsel->attr, stderr);
1083 for (cpu = 0; cpu < cpus->nr; cpu++) {
1085 for (thread = 0; thread < nthreads; thread++) {
1088 if (!evsel->cgrp && !evsel->system_wide)
1089 pid = threads->map[thread];
1091 group_fd = get_group_fd(evsel, cpu, thread);
1093 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1094 pid, cpus->map[cpu], group_fd, flags);
1096 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1100 if (FD(evsel, cpu, thread) < 0) {
1102 pr_debug2("sys_perf_event_open failed, error %d\n",
1106 set_rlimit = NO_CHANGE;
1114 * perf stat needs between 5 and 22 fds per CPU. When we run out
1115 * of them try to increase the limits.
1117 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1119 int old_errno = errno;
1121 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1122 if (set_rlimit == NO_CHANGE)
1123 l.rlim_cur = l.rlim_max;
1125 l.rlim_cur = l.rlim_max + 1000;
1126 l.rlim_max = l.rlim_cur;
1128 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1137 if (err != -EINVAL || cpu > 0 || thread > 0)
1140 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1141 perf_missing_features.cloexec = true;
1142 goto fallback_missing_features;
1143 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1144 perf_missing_features.mmap2 = true;
1145 goto fallback_missing_features;
1146 } else if (!perf_missing_features.exclude_guest &&
1147 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1148 perf_missing_features.exclude_guest = true;
1149 goto fallback_missing_features;
1150 } else if (!perf_missing_features.sample_id_all) {
1151 perf_missing_features.sample_id_all = true;
1152 goto retry_sample_id;
1157 while (--thread >= 0) {
1158 close(FD(evsel, cpu, thread));
1159 FD(evsel, cpu, thread) = -1;
1162 } while (--cpu >= 0);
1166 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1168 if (evsel->fd == NULL)
1171 perf_evsel__close_fd(evsel, ncpus, nthreads);
1172 perf_evsel__free_fd(evsel);
1184 struct thread_map map;
1186 } empty_thread_map = {
1191 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1192 struct thread_map *threads)
1195 /* Work around old compiler warnings about strict aliasing */
1196 cpus = &empty_cpu_map.map;
1199 if (threads == NULL)
1200 threads = &empty_thread_map.map;
1202 return __perf_evsel__open(evsel, cpus, threads);
1205 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1206 struct cpu_map *cpus)
1208 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1211 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1212 struct thread_map *threads)
1214 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1217 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1218 const union perf_event *event,
1219 struct perf_sample *sample)
1221 u64 type = evsel->attr.sample_type;
1222 const u64 *array = event->sample.array;
1223 bool swapped = evsel->needs_swap;
1226 array += ((event->header.size -
1227 sizeof(event->header)) / sizeof(u64)) - 1;
1229 if (type & PERF_SAMPLE_IDENTIFIER) {
1230 sample->id = *array;
1234 if (type & PERF_SAMPLE_CPU) {
1237 /* undo swap of u64, then swap on individual u32s */
1238 u.val64 = bswap_64(u.val64);
1239 u.val32[0] = bswap_32(u.val32[0]);
1242 sample->cpu = u.val32[0];
1246 if (type & PERF_SAMPLE_STREAM_ID) {
1247 sample->stream_id = *array;
1251 if (type & PERF_SAMPLE_ID) {
1252 sample->id = *array;
1256 if (type & PERF_SAMPLE_TIME) {
1257 sample->time = *array;
1261 if (type & PERF_SAMPLE_TID) {
1264 /* undo swap of u64, then swap on individual u32s */
1265 u.val64 = bswap_64(u.val64);
1266 u.val32[0] = bswap_32(u.val32[0]);
1267 u.val32[1] = bswap_32(u.val32[1]);
1270 sample->pid = u.val32[0];
1271 sample->tid = u.val32[1];
1278 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1281 return size > max_size || offset + size > endp;
1284 #define OVERFLOW_CHECK(offset, size, max_size) \
1286 if (overflow(endp, (max_size), (offset), (size))) \
1290 #define OVERFLOW_CHECK_u64(offset) \
1291 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1293 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1294 struct perf_sample *data)
1296 u64 type = evsel->attr.sample_type;
1297 bool swapped = evsel->needs_swap;
1299 u16 max_size = event->header.size;
1300 const void *endp = (void *)event + max_size;
1304 * used for cross-endian analysis. See git commit 65014ab3
1305 * for why this goofiness is needed.
1309 memset(data, 0, sizeof(*data));
1310 data->cpu = data->pid = data->tid = -1;
1311 data->stream_id = data->id = data->time = -1ULL;
1312 data->period = evsel->attr.sample_period;
1315 if (event->header.type != PERF_RECORD_SAMPLE) {
1316 if (!evsel->attr.sample_id_all)
1318 return perf_evsel__parse_id_sample(evsel, event, data);
1321 array = event->sample.array;
1324 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1325 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1326 * check the format does not go past the end of the event.
1328 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1332 if (type & PERF_SAMPLE_IDENTIFIER) {
1337 if (type & PERF_SAMPLE_IP) {
1342 if (type & PERF_SAMPLE_TID) {
1345 /* undo swap of u64, then swap on individual u32s */
1346 u.val64 = bswap_64(u.val64);
1347 u.val32[0] = bswap_32(u.val32[0]);
1348 u.val32[1] = bswap_32(u.val32[1]);
1351 data->pid = u.val32[0];
1352 data->tid = u.val32[1];
1356 if (type & PERF_SAMPLE_TIME) {
1357 data->time = *array;
1362 if (type & PERF_SAMPLE_ADDR) {
1363 data->addr = *array;
1367 if (type & PERF_SAMPLE_ID) {
1372 if (type & PERF_SAMPLE_STREAM_ID) {
1373 data->stream_id = *array;
1377 if (type & PERF_SAMPLE_CPU) {
1381 /* undo swap of u64, then swap on individual u32s */
1382 u.val64 = bswap_64(u.val64);
1383 u.val32[0] = bswap_32(u.val32[0]);
1386 data->cpu = u.val32[0];
1390 if (type & PERF_SAMPLE_PERIOD) {
1391 data->period = *array;
1395 if (type & PERF_SAMPLE_READ) {
1396 u64 read_format = evsel->attr.read_format;
1398 OVERFLOW_CHECK_u64(array);
1399 if (read_format & PERF_FORMAT_GROUP)
1400 data->read.group.nr = *array;
1402 data->read.one.value = *array;
1406 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1407 OVERFLOW_CHECK_u64(array);
1408 data->read.time_enabled = *array;
1412 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1413 OVERFLOW_CHECK_u64(array);
1414 data->read.time_running = *array;
1418 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1419 if (read_format & PERF_FORMAT_GROUP) {
1420 const u64 max_group_nr = UINT64_MAX /
1421 sizeof(struct sample_read_value);
1423 if (data->read.group.nr > max_group_nr)
1425 sz = data->read.group.nr *
1426 sizeof(struct sample_read_value);
1427 OVERFLOW_CHECK(array, sz, max_size);
1428 data->read.group.values =
1429 (struct sample_read_value *)array;
1430 array = (void *)array + sz;
1432 OVERFLOW_CHECK_u64(array);
1433 data->read.one.id = *array;
1438 if (type & PERF_SAMPLE_CALLCHAIN) {
1439 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1441 OVERFLOW_CHECK_u64(array);
1442 data->callchain = (struct ip_callchain *)array++;
1443 if (data->callchain->nr > max_callchain_nr)
1445 sz = data->callchain->nr * sizeof(u64);
1446 OVERFLOW_CHECK(array, sz, max_size);
1447 array = (void *)array + sz;
1450 if (type & PERF_SAMPLE_RAW) {
1451 OVERFLOW_CHECK_u64(array);
1453 if (WARN_ONCE(swapped,
1454 "Endianness of raw data not corrected!\n")) {
1455 /* undo swap of u64, then swap on individual u32s */
1456 u.val64 = bswap_64(u.val64);
1457 u.val32[0] = bswap_32(u.val32[0]);
1458 u.val32[1] = bswap_32(u.val32[1]);
1460 data->raw_size = u.val32[0];
1461 array = (void *)array + sizeof(u32);
1463 OVERFLOW_CHECK(array, data->raw_size, max_size);
1464 data->raw_data = (void *)array;
1465 array = (void *)array + data->raw_size;
1468 if (type & PERF_SAMPLE_BRANCH_STACK) {
1469 const u64 max_branch_nr = UINT64_MAX /
1470 sizeof(struct branch_entry);
1472 OVERFLOW_CHECK_u64(array);
1473 data->branch_stack = (struct branch_stack *)array++;
1475 if (data->branch_stack->nr > max_branch_nr)
1477 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1478 OVERFLOW_CHECK(array, sz, max_size);
1479 array = (void *)array + sz;
1482 if (type & PERF_SAMPLE_REGS_USER) {
1483 OVERFLOW_CHECK_u64(array);
1484 data->user_regs.abi = *array;
1487 if (data->user_regs.abi) {
1488 u64 mask = evsel->attr.sample_regs_user;
1490 sz = hweight_long(mask) * sizeof(u64);
1491 OVERFLOW_CHECK(array, sz, max_size);
1492 data->user_regs.mask = mask;
1493 data->user_regs.regs = (u64 *)array;
1494 array = (void *)array + sz;
1498 if (type & PERF_SAMPLE_STACK_USER) {
1499 OVERFLOW_CHECK_u64(array);
1502 data->user_stack.offset = ((char *)(array - 1)
1506 data->user_stack.size = 0;
1508 OVERFLOW_CHECK(array, sz, max_size);
1509 data->user_stack.data = (char *)array;
1510 array = (void *)array + sz;
1511 OVERFLOW_CHECK_u64(array);
1512 data->user_stack.size = *array++;
1513 if (WARN_ONCE(data->user_stack.size > sz,
1514 "user stack dump failure\n"))
1520 if (type & PERF_SAMPLE_WEIGHT) {
1521 OVERFLOW_CHECK_u64(array);
1522 data->weight = *array;
1526 data->data_src = PERF_MEM_DATA_SRC_NONE;
1527 if (type & PERF_SAMPLE_DATA_SRC) {
1528 OVERFLOW_CHECK_u64(array);
1529 data->data_src = *array;
1533 data->transaction = 0;
1534 if (type & PERF_SAMPLE_TRANSACTION) {
1535 OVERFLOW_CHECK_u64(array);
1536 data->transaction = *array;
1543 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1546 size_t sz, result = sizeof(struct sample_event);
1548 if (type & PERF_SAMPLE_IDENTIFIER)
1549 result += sizeof(u64);
1551 if (type & PERF_SAMPLE_IP)
1552 result += sizeof(u64);
1554 if (type & PERF_SAMPLE_TID)
1555 result += sizeof(u64);
1557 if (type & PERF_SAMPLE_TIME)
1558 result += sizeof(u64);
1560 if (type & PERF_SAMPLE_ADDR)
1561 result += sizeof(u64);
1563 if (type & PERF_SAMPLE_ID)
1564 result += sizeof(u64);
1566 if (type & PERF_SAMPLE_STREAM_ID)
1567 result += sizeof(u64);
1569 if (type & PERF_SAMPLE_CPU)
1570 result += sizeof(u64);
1572 if (type & PERF_SAMPLE_PERIOD)
1573 result += sizeof(u64);
1575 if (type & PERF_SAMPLE_READ) {
1576 result += sizeof(u64);
1577 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1578 result += sizeof(u64);
1579 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1580 result += sizeof(u64);
1581 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1582 if (read_format & PERF_FORMAT_GROUP) {
1583 sz = sample->read.group.nr *
1584 sizeof(struct sample_read_value);
1587 result += sizeof(u64);
1591 if (type & PERF_SAMPLE_CALLCHAIN) {
1592 sz = (sample->callchain->nr + 1) * sizeof(u64);
1596 if (type & PERF_SAMPLE_RAW) {
1597 result += sizeof(u32);
1598 result += sample->raw_size;
1601 if (type & PERF_SAMPLE_BRANCH_STACK) {
1602 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1607 if (type & PERF_SAMPLE_REGS_USER) {
1608 if (sample->user_regs.abi) {
1609 result += sizeof(u64);
1610 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1613 result += sizeof(u64);
1617 if (type & PERF_SAMPLE_STACK_USER) {
1618 sz = sample->user_stack.size;
1619 result += sizeof(u64);
1622 result += sizeof(u64);
1626 if (type & PERF_SAMPLE_WEIGHT)
1627 result += sizeof(u64);
1629 if (type & PERF_SAMPLE_DATA_SRC)
1630 result += sizeof(u64);
1632 if (type & PERF_SAMPLE_TRANSACTION)
1633 result += sizeof(u64);
1638 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1640 const struct perf_sample *sample,
1646 * used for cross-endian analysis. See git commit 65014ab3
1647 * for why this goofiness is needed.
1651 array = event->sample.array;
1653 if (type & PERF_SAMPLE_IDENTIFIER) {
1654 *array = sample->id;
1658 if (type & PERF_SAMPLE_IP) {
1659 *array = sample->ip;
1663 if (type & PERF_SAMPLE_TID) {
1664 u.val32[0] = sample->pid;
1665 u.val32[1] = sample->tid;
1668 * Inverse of what is done in perf_evsel__parse_sample
1670 u.val32[0] = bswap_32(u.val32[0]);
1671 u.val32[1] = bswap_32(u.val32[1]);
1672 u.val64 = bswap_64(u.val64);
1679 if (type & PERF_SAMPLE_TIME) {
1680 *array = sample->time;
1684 if (type & PERF_SAMPLE_ADDR) {
1685 *array = sample->addr;
1689 if (type & PERF_SAMPLE_ID) {
1690 *array = sample->id;
1694 if (type & PERF_SAMPLE_STREAM_ID) {
1695 *array = sample->stream_id;
1699 if (type & PERF_SAMPLE_CPU) {
1700 u.val32[0] = sample->cpu;
1703 * Inverse of what is done in perf_evsel__parse_sample
1705 u.val32[0] = bswap_32(u.val32[0]);
1706 u.val64 = bswap_64(u.val64);
1712 if (type & PERF_SAMPLE_PERIOD) {
1713 *array = sample->period;
1717 if (type & PERF_SAMPLE_READ) {
1718 if (read_format & PERF_FORMAT_GROUP)
1719 *array = sample->read.group.nr;
1721 *array = sample->read.one.value;
1724 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1725 *array = sample->read.time_enabled;
1729 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1730 *array = sample->read.time_running;
1734 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1735 if (read_format & PERF_FORMAT_GROUP) {
1736 sz = sample->read.group.nr *
1737 sizeof(struct sample_read_value);
1738 memcpy(array, sample->read.group.values, sz);
1739 array = (void *)array + sz;
1741 *array = sample->read.one.id;
1746 if (type & PERF_SAMPLE_CALLCHAIN) {
1747 sz = (sample->callchain->nr + 1) * sizeof(u64);
1748 memcpy(array, sample->callchain, sz);
1749 array = (void *)array + sz;
1752 if (type & PERF_SAMPLE_RAW) {
1753 u.val32[0] = sample->raw_size;
1754 if (WARN_ONCE(swapped,
1755 "Endianness of raw data not corrected!\n")) {
1757 * Inverse of what is done in perf_evsel__parse_sample
1759 u.val32[0] = bswap_32(u.val32[0]);
1760 u.val32[1] = bswap_32(u.val32[1]);
1761 u.val64 = bswap_64(u.val64);
1764 array = (void *)array + sizeof(u32);
1766 memcpy(array, sample->raw_data, sample->raw_size);
1767 array = (void *)array + sample->raw_size;
1770 if (type & PERF_SAMPLE_BRANCH_STACK) {
1771 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1773 memcpy(array, sample->branch_stack, sz);
1774 array = (void *)array + sz;
1777 if (type & PERF_SAMPLE_REGS_USER) {
1778 if (sample->user_regs.abi) {
1779 *array++ = sample->user_regs.abi;
1780 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1781 memcpy(array, sample->user_regs.regs, sz);
1782 array = (void *)array + sz;
1788 if (type & PERF_SAMPLE_STACK_USER) {
1789 sz = sample->user_stack.size;
1792 memcpy(array, sample->user_stack.data, sz);
1793 array = (void *)array + sz;
1798 if (type & PERF_SAMPLE_WEIGHT) {
1799 *array = sample->weight;
1803 if (type & PERF_SAMPLE_DATA_SRC) {
1804 *array = sample->data_src;
1808 if (type & PERF_SAMPLE_TRANSACTION) {
1809 *array = sample->transaction;
1816 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1818 return pevent_find_field(evsel->tp_format, name);
1821 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1824 struct format_field *field = perf_evsel__field(evsel, name);
1830 offset = field->offset;
1832 if (field->flags & FIELD_IS_DYNAMIC) {
1833 offset = *(int *)(sample->raw_data + field->offset);
1837 return sample->raw_data + offset;
1840 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1843 struct format_field *field = perf_evsel__field(evsel, name);
1850 ptr = sample->raw_data + field->offset;
1852 switch (field->size) {
1856 value = *(u16 *)ptr;
1859 value = *(u32 *)ptr;
1862 value = *(u64 *)ptr;
1868 if (!evsel->needs_swap)
1871 switch (field->size) {
1873 return bswap_16(value);
1875 return bswap_32(value);
1877 return bswap_64(value);
1885 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1891 ret += fprintf(fp, ",");
1893 ret += fprintf(fp, ":");
1897 va_start(args, fmt);
1898 ret += vfprintf(fp, fmt, args);
1903 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1908 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1911 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1918 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1919 struct bit_names *bits, bool *first)
1921 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1922 bool first_bit = true;
1925 if (value & bits[i].bit) {
1926 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1929 } while (bits[++i].name != NULL);
1934 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1936 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1937 struct bit_names bits[] = {
1938 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1939 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1940 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1941 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1942 bit_name(IDENTIFIER),
1946 return bits__fprintf(fp, "sample_type", value, bits, first);
1949 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1951 #define bit_name(n) { PERF_FORMAT_##n, #n }
1952 struct bit_names bits[] = {
1953 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1954 bit_name(ID), bit_name(GROUP),
1958 return bits__fprintf(fp, "read_format", value, bits, first);
1961 int perf_evsel__fprintf(struct perf_evsel *evsel,
1962 struct perf_attr_details *details, FILE *fp)
1967 if (details->event_group) {
1968 struct perf_evsel *pos;
1970 if (!perf_evsel__is_group_leader(evsel))
1973 if (evsel->nr_members > 1)
1974 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1976 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1977 for_each_group_member(pos, evsel)
1978 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1980 if (evsel->nr_members > 1)
1981 printed += fprintf(fp, "}");
1985 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1987 if (details->verbose || details->freq) {
1988 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1989 (u64)evsel->attr.sample_freq);
1992 if (details->verbose) {
1998 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1999 if (evsel->attr.read_format)
2000 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
2004 if_print(exclusive);
2005 if_print(exclude_user);
2006 if_print(exclude_kernel);
2007 if_print(exclude_hv);
2008 if_print(exclude_idle);
2012 if_print(comm_exec);
2014 if_print(inherit_stat);
2015 if_print(enable_on_exec);
2017 if_print(watermark);
2018 if_print(precise_ip);
2019 if_print(mmap_data);
2020 if_print(sample_id_all);
2021 if_print(exclude_host);
2022 if_print(exclude_guest);
2023 if_print(__reserved_1);
2024 if_print(wakeup_events);
2026 if_print(branch_sample_type);
2033 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2034 char *msg, size_t msgsize)
2036 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2037 evsel->attr.type == PERF_TYPE_HARDWARE &&
2038 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2040 * If it's cycles then fall back to hrtimer based
2041 * cpu-clock-tick sw counter, which is always available even if
2044 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2047 scnprintf(msg, msgsize, "%s",
2048 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2050 evsel->attr.type = PERF_TYPE_SOFTWARE;
2051 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2053 zfree(&evsel->name);
2060 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2061 int err, char *msg, size_t size)
2063 char sbuf[STRERR_BUFSIZE];
2068 return scnprintf(msg, size,
2069 "You may not have permission to collect %sstats.\n"
2070 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2071 " -1 - Not paranoid at all\n"
2072 " 0 - Disallow raw tracepoint access for unpriv\n"
2073 " 1 - Disallow cpu events for unpriv\n"
2074 " 2 - Disallow kernel profiling for unpriv",
2075 target->system_wide ? "system-wide " : "");
2077 return scnprintf(msg, size, "The %s event is not supported.",
2078 perf_evsel__name(evsel));
2080 return scnprintf(msg, size, "%s",
2081 "Too many events are opened.\n"
2082 "Try again after reducing the number of events.");
2084 if (target->cpu_list)
2085 return scnprintf(msg, size, "%s",
2086 "No such device - did you specify an out-of-range profile CPU?\n");
2089 if (evsel->attr.precise_ip)
2090 return scnprintf(msg, size, "%s",
2091 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2092 #if defined(__i386__) || defined(__x86_64__)
2093 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2094 return scnprintf(msg, size, "%s",
2095 "No hardware sampling interrupt available.\n"
2096 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2100 if (find_process("oprofiled"))
2101 return scnprintf(msg, size,
2102 "The PMU counters are busy/taken by another profiler.\n"
2103 "We found oprofile daemon running, please stop it and try again.");
2109 return scnprintf(msg, size,
2110 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2111 "/bin/dmesg may provide additional information.\n"
2112 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2113 err, strerror_r(err, sbuf, sizeof(sbuf)),
2114 perf_evsel__name(evsel));