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>
13 #include <lk/debugfs.h>
14 #include "event-parse.h"
19 #include "thread_map.h"
21 #include <linux/hw_breakpoint.h>
22 #include <linux/perf_event.h>
23 #include "perf_regs.h"
28 } perf_missing_features;
30 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
32 static int __perf_evsel__sample_size(u64 sample_type)
34 u64 mask = sample_type & PERF_SAMPLE_MASK;
38 for (i = 0; i < 64; i++) {
39 if (mask & (1ULL << i))
48 void hists__init(struct hists *hists)
50 memset(hists, 0, sizeof(*hists));
51 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
52 hists->entries_in = &hists->entries_in_array[0];
53 hists->entries_collapsed = RB_ROOT;
54 hists->entries = RB_ROOT;
55 pthread_mutex_init(&hists->lock, NULL);
58 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
59 enum perf_event_sample_format bit)
61 if (!(evsel->attr.sample_type & bit)) {
62 evsel->attr.sample_type |= bit;
63 evsel->sample_size += sizeof(u64);
67 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
68 enum perf_event_sample_format bit)
70 if (evsel->attr.sample_type & bit) {
71 evsel->attr.sample_type &= ~bit;
72 evsel->sample_size -= sizeof(u64);
76 void perf_evsel__set_sample_id(struct perf_evsel *evsel)
78 perf_evsel__set_sample_bit(evsel, ID);
79 evsel->attr.read_format |= PERF_FORMAT_ID;
82 void perf_evsel__init(struct perf_evsel *evsel,
83 struct perf_event_attr *attr, int idx)
87 evsel->leader = evsel;
88 INIT_LIST_HEAD(&evsel->node);
89 hists__init(&evsel->hists);
90 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
93 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
95 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
98 perf_evsel__init(evsel, attr, idx);
103 struct event_format *event_format__new(const char *sys, const char *name)
107 void *bf = NULL, *nbf;
108 size_t size = 0, alloc_size = 0;
109 struct event_format *format = NULL;
111 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
114 fd = open(filename, O_RDONLY);
116 goto out_free_filename;
119 if (size == alloc_size) {
120 alloc_size += BUFSIZ;
121 nbf = realloc(bf, alloc_size);
127 n = read(fd, bf + size, BUFSIZ);
133 pevent_parse_format(&format, bf, size, sys);
144 struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
146 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
149 struct perf_event_attr attr = {
150 .type = PERF_TYPE_TRACEPOINT,
151 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
152 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
155 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
158 evsel->tp_format = event_format__new(sys, name);
159 if (evsel->tp_format == NULL)
162 event_attr_init(&attr);
163 attr.config = evsel->tp_format->id;
164 attr.sample_period = 1;
165 perf_evsel__init(evsel, &attr, idx);
176 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
184 "stalled-cycles-frontend",
185 "stalled-cycles-backend",
189 static const char *__perf_evsel__hw_name(u64 config)
191 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
192 return perf_evsel__hw_names[config];
194 return "unknown-hardware";
197 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
199 int colon = 0, r = 0;
200 struct perf_event_attr *attr = &evsel->attr;
201 bool exclude_guest_default = false;
203 #define MOD_PRINT(context, mod) do { \
204 if (!attr->exclude_##context) { \
205 if (!colon) colon = ++r; \
206 r += scnprintf(bf + r, size - r, "%c", mod); \
209 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
210 MOD_PRINT(kernel, 'k');
211 MOD_PRINT(user, 'u');
213 exclude_guest_default = true;
216 if (attr->precise_ip) {
219 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
220 exclude_guest_default = true;
223 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
224 MOD_PRINT(host, 'H');
225 MOD_PRINT(guest, 'G');
233 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
235 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
236 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
239 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
251 static const char *__perf_evsel__sw_name(u64 config)
253 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
254 return perf_evsel__sw_names[config];
255 return "unknown-software";
258 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
260 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
261 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
264 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
268 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
270 if (type & HW_BREAKPOINT_R)
271 r += scnprintf(bf + r, size - r, "r");
273 if (type & HW_BREAKPOINT_W)
274 r += scnprintf(bf + r, size - r, "w");
276 if (type & HW_BREAKPOINT_X)
277 r += scnprintf(bf + r, size - r, "x");
282 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
284 struct perf_event_attr *attr = &evsel->attr;
285 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
286 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
289 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
290 [PERF_EVSEL__MAX_ALIASES] = {
291 { "L1-dcache", "l1-d", "l1d", "L1-data", },
292 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
294 { "dTLB", "d-tlb", "Data-TLB", },
295 { "iTLB", "i-tlb", "Instruction-TLB", },
296 { "branch", "branches", "bpu", "btb", "bpc", },
300 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
301 [PERF_EVSEL__MAX_ALIASES] = {
302 { "load", "loads", "read", },
303 { "store", "stores", "write", },
304 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
307 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
308 [PERF_EVSEL__MAX_ALIASES] = {
309 { "refs", "Reference", "ops", "access", },
310 { "misses", "miss", },
313 #define C(x) PERF_COUNT_HW_CACHE_##x
314 #define CACHE_READ (1 << C(OP_READ))
315 #define CACHE_WRITE (1 << C(OP_WRITE))
316 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
317 #define COP(x) (1 << x)
320 * cache operartion stat
321 * L1I : Read and prefetch only
322 * ITLB and BPU : Read-only
324 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
325 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
326 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
327 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
328 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
329 [C(ITLB)] = (CACHE_READ),
330 [C(BPU)] = (CACHE_READ),
331 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
334 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
336 if (perf_evsel__hw_cache_stat[type] & COP(op))
337 return true; /* valid */
339 return false; /* invalid */
342 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
343 char *bf, size_t size)
346 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
347 perf_evsel__hw_cache_op[op][0],
348 perf_evsel__hw_cache_result[result][0]);
351 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
352 perf_evsel__hw_cache_op[op][1]);
355 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
357 u8 op, result, type = (config >> 0) & 0xff;
358 const char *err = "unknown-ext-hardware-cache-type";
360 if (type > PERF_COUNT_HW_CACHE_MAX)
363 op = (config >> 8) & 0xff;
364 err = "unknown-ext-hardware-cache-op";
365 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
368 result = (config >> 16) & 0xff;
369 err = "unknown-ext-hardware-cache-result";
370 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
373 err = "invalid-cache";
374 if (!perf_evsel__is_cache_op_valid(type, op))
377 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
379 return scnprintf(bf, size, "%s", err);
382 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
384 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
385 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
388 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
390 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
391 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
394 const char *perf_evsel__name(struct perf_evsel *evsel)
401 switch (evsel->attr.type) {
403 perf_evsel__raw_name(evsel, bf, sizeof(bf));
406 case PERF_TYPE_HARDWARE:
407 perf_evsel__hw_name(evsel, bf, sizeof(bf));
410 case PERF_TYPE_HW_CACHE:
411 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
414 case PERF_TYPE_SOFTWARE:
415 perf_evsel__sw_name(evsel, bf, sizeof(bf));
418 case PERF_TYPE_TRACEPOINT:
419 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
422 case PERF_TYPE_BREAKPOINT:
423 perf_evsel__bp_name(evsel, bf, sizeof(bf));
427 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
432 evsel->name = strdup(bf);
434 return evsel->name ?: "unknown";
437 const char *perf_evsel__group_name(struct perf_evsel *evsel)
439 return evsel->group_name ?: "anon group";
442 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
445 struct perf_evsel *pos;
446 const char *group_name = perf_evsel__group_name(evsel);
448 ret = scnprintf(buf, size, "%s", group_name);
450 ret += scnprintf(buf + ret, size - ret, " { %s",
451 perf_evsel__name(evsel));
453 for_each_group_member(pos, evsel)
454 ret += scnprintf(buf + ret, size - ret, ", %s",
455 perf_evsel__name(pos));
457 ret += scnprintf(buf + ret, size - ret, " }");
463 * The enable_on_exec/disabled value strategy:
465 * 1) For any type of traced program:
466 * - all independent events and group leaders are disabled
467 * - all group members are enabled
469 * Group members are ruled by group leaders. They need to
470 * be enabled, because the group scheduling relies on that.
472 * 2) For traced programs executed by perf:
473 * - all independent events and group leaders have
475 * - we don't specifically enable or disable any event during
478 * Independent events and group leaders are initially disabled
479 * and get enabled by exec. Group members are ruled by group
480 * leaders as stated in 1).
482 * 3) For traced programs attached by perf (pid/tid):
483 * - we specifically enable or disable all events during
486 * When attaching events to already running traced we
487 * enable/disable events specifically, as there's no
488 * initial traced exec call.
490 void perf_evsel__config(struct perf_evsel *evsel,
491 struct perf_record_opts *opts)
493 struct perf_event_attr *attr = &evsel->attr;
494 int track = !evsel->idx; /* only the first counter needs these */
496 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
497 attr->inherit = !opts->no_inherit;
499 perf_evsel__set_sample_bit(evsel, IP);
500 perf_evsel__set_sample_bit(evsel, TID);
503 * We default some events to a 1 default interval. But keep
504 * it a weak assumption overridable by the user.
506 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
507 opts->user_interval != ULLONG_MAX)) {
509 perf_evsel__set_sample_bit(evsel, PERIOD);
511 attr->sample_freq = opts->freq;
513 attr->sample_period = opts->default_interval;
517 if (opts->no_samples)
518 attr->sample_freq = 0;
520 if (opts->inherit_stat)
521 attr->inherit_stat = 1;
523 if (opts->sample_address) {
524 perf_evsel__set_sample_bit(evsel, ADDR);
525 attr->mmap_data = track;
528 if (opts->call_graph) {
529 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
531 if (opts->call_graph == CALLCHAIN_DWARF) {
532 perf_evsel__set_sample_bit(evsel, REGS_USER);
533 perf_evsel__set_sample_bit(evsel, STACK_USER);
534 attr->sample_regs_user = PERF_REGS_MASK;
535 attr->sample_stack_user = opts->stack_dump_size;
536 attr->exclude_callchain_user = 1;
540 if (perf_target__has_cpu(&opts->target))
541 perf_evsel__set_sample_bit(evsel, CPU);
544 perf_evsel__set_sample_bit(evsel, PERIOD);
546 if (!perf_missing_features.sample_id_all &&
547 (opts->sample_time || !opts->no_inherit ||
548 perf_target__has_cpu(&opts->target)))
549 perf_evsel__set_sample_bit(evsel, TIME);
551 if (opts->raw_samples) {
552 perf_evsel__set_sample_bit(evsel, TIME);
553 perf_evsel__set_sample_bit(evsel, RAW);
554 perf_evsel__set_sample_bit(evsel, CPU);
557 if (opts->sample_address)
558 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
560 if (opts->no_delay) {
562 attr->wakeup_events = 1;
564 if (opts->branch_stack) {
565 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
566 attr->branch_sample_type = opts->branch_stack;
569 if (opts->sample_weight)
570 attr->sample_type |= PERF_SAMPLE_WEIGHT;
576 * XXX see the function comment above
578 * Disabling only independent events or group leaders,
579 * keeping group members enabled.
581 if (perf_evsel__is_group_leader(evsel))
585 * Setting enable_on_exec for independent events and
586 * group leaders for traced executed by perf.
588 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
589 attr->enable_on_exec = 1;
592 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
595 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
598 for (cpu = 0; cpu < ncpus; cpu++) {
599 for (thread = 0; thread < nthreads; thread++) {
600 FD(evsel, cpu, thread) = -1;
605 return evsel->fd != NULL ? 0 : -ENOMEM;
608 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
613 for (cpu = 0; cpu < ncpus; cpu++) {
614 for (thread = 0; thread < nthreads; thread++) {
615 int fd = FD(evsel, cpu, thread),
616 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
626 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
628 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
629 if (evsel->sample_id == NULL)
632 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
633 if (evsel->id == NULL) {
634 xyarray__delete(evsel->sample_id);
635 evsel->sample_id = NULL;
642 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
644 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
645 (ncpus * sizeof(struct perf_counts_values))));
648 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
650 evsel->counts = zalloc((sizeof(*evsel->counts) +
651 (ncpus * sizeof(struct perf_counts_values))));
652 return evsel->counts != NULL ? 0 : -ENOMEM;
655 void perf_evsel__free_fd(struct perf_evsel *evsel)
657 xyarray__delete(evsel->fd);
661 void perf_evsel__free_id(struct perf_evsel *evsel)
663 xyarray__delete(evsel->sample_id);
664 evsel->sample_id = NULL;
669 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
673 for (cpu = 0; cpu < ncpus; cpu++)
674 for (thread = 0; thread < nthreads; ++thread) {
675 close(FD(evsel, cpu, thread));
676 FD(evsel, cpu, thread) = -1;
680 void perf_evsel__free_counts(struct perf_evsel *evsel)
685 void perf_evsel__exit(struct perf_evsel *evsel)
687 assert(list_empty(&evsel->node));
688 perf_evsel__free_fd(evsel);
689 perf_evsel__free_id(evsel);
692 void perf_evsel__delete(struct perf_evsel *evsel)
694 perf_evsel__exit(evsel);
695 close_cgroup(evsel->cgrp);
696 free(evsel->group_name);
697 if (evsel->tp_format)
698 pevent_free_format(evsel->tp_format);
703 static inline void compute_deltas(struct perf_evsel *evsel,
705 struct perf_counts_values *count)
707 struct perf_counts_values tmp;
709 if (!evsel->prev_raw_counts)
713 tmp = evsel->prev_raw_counts->aggr;
714 evsel->prev_raw_counts->aggr = *count;
716 tmp = evsel->prev_raw_counts->cpu[cpu];
717 evsel->prev_raw_counts->cpu[cpu] = *count;
720 count->val = count->val - tmp.val;
721 count->ena = count->ena - tmp.ena;
722 count->run = count->run - tmp.run;
725 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
726 int cpu, int thread, bool scale)
728 struct perf_counts_values count;
729 size_t nv = scale ? 3 : 1;
731 if (FD(evsel, cpu, thread) < 0)
734 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
737 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
740 compute_deltas(evsel, cpu, &count);
745 else if (count.run < count.ena)
746 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
748 count.ena = count.run = 0;
750 evsel->counts->cpu[cpu] = count;
754 int __perf_evsel__read(struct perf_evsel *evsel,
755 int ncpus, int nthreads, bool scale)
757 size_t nv = scale ? 3 : 1;
759 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
761 aggr->val = aggr->ena = aggr->run = 0;
763 for (cpu = 0; cpu < ncpus; cpu++) {
764 for (thread = 0; thread < nthreads; thread++) {
765 if (FD(evsel, cpu, thread) < 0)
768 if (readn(FD(evsel, cpu, thread),
769 &count, nv * sizeof(u64)) < 0)
772 aggr->val += count.val;
774 aggr->ena += count.ena;
775 aggr->run += count.run;
780 compute_deltas(evsel, -1, aggr);
782 evsel->counts->scaled = 0;
784 if (aggr->run == 0) {
785 evsel->counts->scaled = -1;
790 if (aggr->run < aggr->ena) {
791 evsel->counts->scaled = 1;
792 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
795 aggr->ena = aggr->run = 0;
800 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
802 struct perf_evsel *leader = evsel->leader;
805 if (perf_evsel__is_group_leader(evsel))
809 * Leader must be already processed/open,
814 fd = FD(leader, cpu, thread);
820 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
821 struct thread_map *threads)
824 unsigned long flags = 0;
827 if (evsel->fd == NULL &&
828 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
832 flags = PERF_FLAG_PID_CGROUP;
833 pid = evsel->cgrp->fd;
836 fallback_missing_features:
837 if (perf_missing_features.exclude_guest)
838 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
840 if (perf_missing_features.sample_id_all)
841 evsel->attr.sample_id_all = 0;
843 for (cpu = 0; cpu < cpus->nr; cpu++) {
845 for (thread = 0; thread < threads->nr; thread++) {
849 pid = threads->map[thread];
851 group_fd = get_group_fd(evsel, cpu, thread);
853 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
857 if (FD(evsel, cpu, thread) < 0) {
867 if (err != -EINVAL || cpu > 0 || thread > 0)
870 if (!perf_missing_features.exclude_guest &&
871 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
872 perf_missing_features.exclude_guest = true;
873 goto fallback_missing_features;
874 } else if (!perf_missing_features.sample_id_all) {
875 perf_missing_features.sample_id_all = true;
876 goto retry_sample_id;
881 while (--thread >= 0) {
882 close(FD(evsel, cpu, thread));
883 FD(evsel, cpu, thread) = -1;
885 thread = threads->nr;
886 } while (--cpu >= 0);
890 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
892 if (evsel->fd == NULL)
895 perf_evsel__close_fd(evsel, ncpus, nthreads);
896 perf_evsel__free_fd(evsel);
909 struct thread_map map;
911 } empty_thread_map = {
916 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
917 struct thread_map *threads)
920 /* Work around old compiler warnings about strict aliasing */
921 cpus = &empty_cpu_map.map;
925 threads = &empty_thread_map.map;
927 return __perf_evsel__open(evsel, cpus, threads);
930 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
931 struct cpu_map *cpus)
933 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
936 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
937 struct thread_map *threads)
939 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
942 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
943 const union perf_event *event,
944 struct perf_sample *sample)
946 u64 type = evsel->attr.sample_type;
947 const u64 *array = event->sample.array;
948 bool swapped = evsel->needs_swap;
951 array += ((event->header.size -
952 sizeof(event->header)) / sizeof(u64)) - 1;
954 if (type & PERF_SAMPLE_CPU) {
957 /* undo swap of u64, then swap on individual u32s */
958 u.val64 = bswap_64(u.val64);
959 u.val32[0] = bswap_32(u.val32[0]);
962 sample->cpu = u.val32[0];
966 if (type & PERF_SAMPLE_STREAM_ID) {
967 sample->stream_id = *array;
971 if (type & PERF_SAMPLE_ID) {
976 if (type & PERF_SAMPLE_TIME) {
977 sample->time = *array;
981 if (type & PERF_SAMPLE_TID) {
984 /* undo swap of u64, then swap on individual u32s */
985 u.val64 = bswap_64(u.val64);
986 u.val32[0] = bswap_32(u.val32[0]);
987 u.val32[1] = bswap_32(u.val32[1]);
990 sample->pid = u.val32[0];
991 sample->tid = u.val32[1];
997 static bool sample_overlap(const union perf_event *event,
998 const void *offset, u64 size)
1000 const void *base = event;
1002 if (offset + size > base + event->header.size)
1008 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1009 struct perf_sample *data)
1011 u64 type = evsel->attr.sample_type;
1012 u64 regs_user = evsel->attr.sample_regs_user;
1013 bool swapped = evsel->needs_swap;
1017 * used for cross-endian analysis. See git commit 65014ab3
1018 * for why this goofiness is needed.
1022 memset(data, 0, sizeof(*data));
1023 data->cpu = data->pid = data->tid = -1;
1024 data->stream_id = data->id = data->time = -1ULL;
1028 if (event->header.type != PERF_RECORD_SAMPLE) {
1029 if (!evsel->attr.sample_id_all)
1031 return perf_evsel__parse_id_sample(evsel, event, data);
1034 array = event->sample.array;
1036 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1039 if (type & PERF_SAMPLE_IP) {
1040 data->ip = event->ip.ip;
1044 if (type & PERF_SAMPLE_TID) {
1047 /* undo swap of u64, then swap on individual u32s */
1048 u.val64 = bswap_64(u.val64);
1049 u.val32[0] = bswap_32(u.val32[0]);
1050 u.val32[1] = bswap_32(u.val32[1]);
1053 data->pid = u.val32[0];
1054 data->tid = u.val32[1];
1058 if (type & PERF_SAMPLE_TIME) {
1059 data->time = *array;
1064 if (type & PERF_SAMPLE_ADDR) {
1065 data->addr = *array;
1070 if (type & PERF_SAMPLE_ID) {
1075 if (type & PERF_SAMPLE_STREAM_ID) {
1076 data->stream_id = *array;
1080 if (type & PERF_SAMPLE_CPU) {
1084 /* undo swap of u64, then swap on individual u32s */
1085 u.val64 = bswap_64(u.val64);
1086 u.val32[0] = bswap_32(u.val32[0]);
1089 data->cpu = u.val32[0];
1093 if (type & PERF_SAMPLE_PERIOD) {
1094 data->period = *array;
1098 if (type & PERF_SAMPLE_READ) {
1099 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
1103 if (type & PERF_SAMPLE_CALLCHAIN) {
1104 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1107 data->callchain = (struct ip_callchain *)array;
1109 if (sample_overlap(event, array, data->callchain->nr))
1112 array += 1 + data->callchain->nr;
1115 if (type & PERF_SAMPLE_RAW) {
1119 if (WARN_ONCE(swapped,
1120 "Endianness of raw data not corrected!\n")) {
1121 /* undo swap of u64, then swap on individual u32s */
1122 u.val64 = bswap_64(u.val64);
1123 u.val32[0] = bswap_32(u.val32[0]);
1124 u.val32[1] = bswap_32(u.val32[1]);
1127 if (sample_overlap(event, array, sizeof(u32)))
1130 data->raw_size = u.val32[0];
1131 pdata = (void *) array + sizeof(u32);
1133 if (sample_overlap(event, pdata, data->raw_size))
1136 data->raw_data = (void *) pdata;
1138 array = (void *)array + data->raw_size + sizeof(u32);
1141 if (type & PERF_SAMPLE_BRANCH_STACK) {
1144 data->branch_stack = (struct branch_stack *)array;
1147 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1152 if (type & PERF_SAMPLE_REGS_USER) {
1153 /* First u64 tells us if we have any regs in sample. */
1154 u64 avail = *array++;
1157 data->user_regs.regs = (u64 *)array;
1158 array += hweight_long(regs_user);
1162 if (type & PERF_SAMPLE_STACK_USER) {
1163 u64 size = *array++;
1165 data->user_stack.offset = ((char *)(array - 1)
1169 data->user_stack.size = 0;
1171 data->user_stack.data = (char *)array;
1172 array += size / sizeof(*array);
1173 data->user_stack.size = *array;
1178 if (type & PERF_SAMPLE_WEIGHT) {
1179 data->weight = *array;
1183 data->data_src = PERF_MEM_DATA_SRC_NONE;
1184 if (type & PERF_SAMPLE_DATA_SRC) {
1185 data->data_src = *array;
1192 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1193 const struct perf_sample *sample,
1199 * used for cross-endian analysis. See git commit 65014ab3
1200 * for why this goofiness is needed.
1204 array = event->sample.array;
1206 if (type & PERF_SAMPLE_IP) {
1207 event->ip.ip = sample->ip;
1211 if (type & PERF_SAMPLE_TID) {
1212 u.val32[0] = sample->pid;
1213 u.val32[1] = sample->tid;
1216 * Inverse of what is done in perf_evsel__parse_sample
1218 u.val32[0] = bswap_32(u.val32[0]);
1219 u.val32[1] = bswap_32(u.val32[1]);
1220 u.val64 = bswap_64(u.val64);
1227 if (type & PERF_SAMPLE_TIME) {
1228 *array = sample->time;
1232 if (type & PERF_SAMPLE_ADDR) {
1233 *array = sample->addr;
1237 if (type & PERF_SAMPLE_ID) {
1238 *array = sample->id;
1242 if (type & PERF_SAMPLE_STREAM_ID) {
1243 *array = sample->stream_id;
1247 if (type & PERF_SAMPLE_CPU) {
1248 u.val32[0] = sample->cpu;
1251 * Inverse of what is done in perf_evsel__parse_sample
1253 u.val32[0] = bswap_32(u.val32[0]);
1254 u.val64 = bswap_64(u.val64);
1260 if (type & PERF_SAMPLE_PERIOD) {
1261 *array = sample->period;
1268 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1270 return pevent_find_field(evsel->tp_format, name);
1273 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1276 struct format_field *field = perf_evsel__field(evsel, name);
1282 offset = field->offset;
1284 if (field->flags & FIELD_IS_DYNAMIC) {
1285 offset = *(int *)(sample->raw_data + field->offset);
1289 return sample->raw_data + offset;
1292 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1295 struct format_field *field = perf_evsel__field(evsel, name);
1302 ptr = sample->raw_data + field->offset;
1304 switch (field->size) {
1308 value = *(u16 *)ptr;
1311 value = *(u32 *)ptr;
1314 value = *(u64 *)ptr;
1320 if (!evsel->needs_swap)
1323 switch (field->size) {
1325 return bswap_16(value);
1327 return bswap_32(value);
1329 return bswap_64(value);
1337 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1343 ret += fprintf(fp, ",");
1345 ret += fprintf(fp, ":");
1349 va_start(args, fmt);
1350 ret += vfprintf(fp, fmt, args);
1355 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1360 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1363 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1370 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1371 struct bit_names *bits, bool *first)
1373 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1374 bool first_bit = true;
1377 if (value & bits[i].bit) {
1378 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1381 } while (bits[++i].name != NULL);
1386 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1388 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1389 struct bit_names bits[] = {
1390 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1391 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1392 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1393 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1397 return bits__fprintf(fp, "sample_type", value, bits, first);
1400 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1402 #define bit_name(n) { PERF_FORMAT_##n, #n }
1403 struct bit_names bits[] = {
1404 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1405 bit_name(ID), bit_name(GROUP),
1409 return bits__fprintf(fp, "read_format", value, bits, first);
1412 int perf_evsel__fprintf(struct perf_evsel *evsel,
1413 struct perf_attr_details *details, FILE *fp)
1418 if (details->event_group) {
1419 struct perf_evsel *pos;
1421 if (!perf_evsel__is_group_leader(evsel))
1424 if (evsel->nr_members > 1)
1425 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1427 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1428 for_each_group_member(pos, evsel)
1429 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1431 if (evsel->nr_members > 1)
1432 printed += fprintf(fp, "}");
1436 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1438 if (details->verbose || details->freq) {
1439 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1440 (u64)evsel->attr.sample_freq);
1443 if (details->verbose) {
1449 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1450 if (evsel->attr.read_format)
1451 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
1455 if_print(exclusive);
1456 if_print(exclude_user);
1457 if_print(exclude_kernel);
1458 if_print(exclude_hv);
1459 if_print(exclude_idle);
1463 if_print(inherit_stat);
1464 if_print(enable_on_exec);
1466 if_print(watermark);
1467 if_print(precise_ip);
1468 if_print(mmap_data);
1469 if_print(sample_id_all);
1470 if_print(exclude_host);
1471 if_print(exclude_guest);
1472 if_print(__reserved_1);
1473 if_print(wakeup_events);
1475 if_print(branch_sample_type);
1482 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1483 char *msg, size_t msgsize)
1485 if ((err == ENOENT || err == ENXIO) &&
1486 evsel->attr.type == PERF_TYPE_HARDWARE &&
1487 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1489 * If it's cycles then fall back to hrtimer based
1490 * cpu-clock-tick sw counter, which is always available even if
1493 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1496 scnprintf(msg, msgsize, "%s",
1497 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1499 evsel->attr.type = PERF_TYPE_SOFTWARE;
1500 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1510 int perf_evsel__open_strerror(struct perf_evsel *evsel,
1511 struct perf_target *target,
1512 int err, char *msg, size_t size)
1517 return scnprintf(msg, size, "%s",
1518 "You may not have permission to collect %sstats.\n"
1519 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1520 " -1 - Not paranoid at all\n"
1521 " 0 - Disallow raw tracepoint access for unpriv\n"
1522 " 1 - Disallow cpu events for unpriv\n"
1523 " 2 - Disallow kernel profiling for unpriv",
1524 target->system_wide ? "system-wide " : "");
1526 return scnprintf(msg, size, "The %s event is not supported.",
1527 perf_evsel__name(evsel));
1529 return scnprintf(msg, size, "%s",
1530 "Too many events are opened.\n"
1531 "Try again after reducing the number of events.");
1533 if (target->cpu_list)
1534 return scnprintf(msg, size, "%s",
1535 "No such device - did you specify an out-of-range profile CPU?\n");
1538 if (evsel->attr.precise_ip)
1539 return scnprintf(msg, size, "%s",
1540 "\'precise\' request may not be supported. Try removing 'p' modifier.");
1541 #if defined(__i386__) || defined(__x86_64__)
1542 if (evsel->attr.type == PERF_TYPE_HARDWARE)
1543 return scnprintf(msg, size, "%s",
1544 "No hardware sampling interrupt available.\n"
1545 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
1552 return scnprintf(msg, size,
1553 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
1554 "/bin/dmesg may provide additional information.\n"
1555 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
1556 err, strerror(err), perf_evsel__name(evsel));