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)
16 #include "thread_map.h"
18 #include "../../include/linux/perf_event.h"
20 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
21 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
23 int __perf_evsel__sample_size(u64 sample_type)
25 u64 mask = sample_type & PERF_SAMPLE_MASK;
29 for (i = 0; i < 64; i++) {
30 if (mask & (1ULL << i))
39 void hists__init(struct hists *hists)
41 memset(hists, 0, sizeof(*hists));
42 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
43 hists->entries_in = &hists->entries_in_array[0];
44 hists->entries_collapsed = RB_ROOT;
45 hists->entries = RB_ROOT;
46 pthread_mutex_init(&hists->lock, NULL);
49 void perf_evsel__init(struct perf_evsel *evsel,
50 struct perf_event_attr *attr, int idx)
54 INIT_LIST_HEAD(&evsel->node);
55 hists__init(&evsel->hists);
58 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
60 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
63 perf_evsel__init(evsel, attr, idx);
68 static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
76 "stalled-cycles-frontend",
77 "stalled-cycles-backend",
81 const char *__perf_evsel__hw_name(u64 config)
83 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84 return perf_evsel__hw_names[config];
86 return "unknown-hardware";
89 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
92 struct perf_event_attr *attr = &evsel->attr;
93 bool exclude_guest_default = false;
95 #define MOD_PRINT(context, mod) do { \
96 if (!attr->exclude_##context) { \
97 if (!colon) colon = ++r; \
98 r += scnprintf(bf + r, size - r, "%c", mod); \
101 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
102 MOD_PRINT(kernel, 'k');
103 MOD_PRINT(user, 'u');
105 exclude_guest_default = true;
108 if (attr->precise_ip) {
111 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
112 exclude_guest_default = true;
115 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
116 MOD_PRINT(host, 'H');
117 MOD_PRINT(guest, 'G');
125 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
127 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
128 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
131 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
132 [PERF_EVSEL__MAX_ALIASES] = {
133 { "L1-dcache", "l1-d", "l1d", "L1-data", },
134 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
136 { "dTLB", "d-tlb", "Data-TLB", },
137 { "iTLB", "i-tlb", "Instruction-TLB", },
138 { "branch", "branches", "bpu", "btb", "bpc", },
142 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
143 [PERF_EVSEL__MAX_ALIASES] = {
144 { "load", "loads", "read", },
145 { "store", "stores", "write", },
146 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
149 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
150 [PERF_EVSEL__MAX_ALIASES] = {
151 { "refs", "Reference", "ops", "access", },
152 { "misses", "miss", },
155 #define C(x) PERF_COUNT_HW_CACHE_##x
156 #define CACHE_READ (1 << C(OP_READ))
157 #define CACHE_WRITE (1 << C(OP_WRITE))
158 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
159 #define COP(x) (1 << x)
162 * cache operartion stat
163 * L1I : Read and prefetch only
164 * ITLB and BPU : Read-only
166 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
167 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
168 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
169 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
170 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
171 [C(ITLB)] = (CACHE_READ),
172 [C(BPU)] = (CACHE_READ),
173 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
176 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
178 if (perf_evsel__hw_cache_stat[type] & COP(op))
179 return true; /* valid */
181 return false; /* invalid */
184 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
185 char *bf, size_t size)
188 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
189 perf_evsel__hw_cache_op[op][0],
190 perf_evsel__hw_cache_result[result][0]);
193 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
194 perf_evsel__hw_cache_op[op][1]);
197 int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
199 u8 op, result, type = (config >> 0) & 0xff;
200 const char *err = "unknown-ext-hardware-cache-type";
202 if (type > PERF_COUNT_HW_CACHE_MAX)
205 op = (config >> 8) & 0xff;
206 err = "unknown-ext-hardware-cache-op";
207 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
210 result = (config >> 16) & 0xff;
211 err = "unknown-ext-hardware-cache-result";
212 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
215 err = "invalid-cache";
216 if (!perf_evsel__is_cache_op_valid(type, op))
219 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
221 return scnprintf(bf, size, "%s", err);
224 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
226 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
227 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
230 int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
234 switch (evsel->attr.type) {
236 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
239 case PERF_TYPE_HARDWARE:
240 ret = perf_evsel__hw_name(evsel, bf, size);
243 case PERF_TYPE_HW_CACHE:
244 ret = perf_evsel__hw_cache_name(evsel, bf, size);
251 * This is the minimal perf_evsel__name so that we can
252 * reconstruct event names taking into account event modifiers.
254 * The old event_name uses it now for raw anr hw events, so that
255 * we don't drag all the parsing stuff into the python binding.
257 * On the next devel cycle the rest of the event naming will be
266 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
267 struct perf_evsel *first)
269 struct perf_event_attr *attr = &evsel->attr;
270 int track = !evsel->idx; /* only the first counter needs these */
273 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
274 attr->inherit = !opts->no_inherit;
275 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
276 PERF_FORMAT_TOTAL_TIME_RUNNING |
279 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
282 * We default some events to a 1 default interval. But keep
283 * it a weak assumption overridable by the user.
285 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
286 opts->user_interval != ULLONG_MAX)) {
288 attr->sample_type |= PERF_SAMPLE_PERIOD;
290 attr->sample_freq = opts->freq;
292 attr->sample_period = opts->default_interval;
296 if (opts->no_samples)
297 attr->sample_freq = 0;
299 if (opts->inherit_stat)
300 attr->inherit_stat = 1;
302 if (opts->sample_address) {
303 attr->sample_type |= PERF_SAMPLE_ADDR;
304 attr->mmap_data = track;
307 if (opts->call_graph)
308 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
310 if (perf_target__has_cpu(&opts->target))
311 attr->sample_type |= PERF_SAMPLE_CPU;
314 attr->sample_type |= PERF_SAMPLE_PERIOD;
316 if (!opts->sample_id_all_missing &&
317 (opts->sample_time || !opts->no_inherit ||
318 perf_target__has_cpu(&opts->target)))
319 attr->sample_type |= PERF_SAMPLE_TIME;
321 if (opts->raw_samples) {
322 attr->sample_type |= PERF_SAMPLE_TIME;
323 attr->sample_type |= PERF_SAMPLE_RAW;
324 attr->sample_type |= PERF_SAMPLE_CPU;
327 if (opts->no_delay) {
329 attr->wakeup_events = 1;
331 if (opts->branch_stack) {
332 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
333 attr->branch_sample_type = opts->branch_stack;
339 if (perf_target__none(&opts->target) &&
340 (!opts->group || evsel == first)) {
341 attr->enable_on_exec = 1;
345 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
348 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
351 for (cpu = 0; cpu < ncpus; cpu++) {
352 for (thread = 0; thread < nthreads; thread++) {
353 FD(evsel, cpu, thread) = -1;
358 return evsel->fd != NULL ? 0 : -ENOMEM;
361 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
363 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
364 if (evsel->sample_id == NULL)
367 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
368 if (evsel->id == NULL) {
369 xyarray__delete(evsel->sample_id);
370 evsel->sample_id = NULL;
377 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
379 evsel->counts = zalloc((sizeof(*evsel->counts) +
380 (ncpus * sizeof(struct perf_counts_values))));
381 return evsel->counts != NULL ? 0 : -ENOMEM;
384 void perf_evsel__free_fd(struct perf_evsel *evsel)
386 xyarray__delete(evsel->fd);
390 void perf_evsel__free_id(struct perf_evsel *evsel)
392 xyarray__delete(evsel->sample_id);
393 evsel->sample_id = NULL;
398 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
402 for (cpu = 0; cpu < ncpus; cpu++)
403 for (thread = 0; thread < nthreads; ++thread) {
404 close(FD(evsel, cpu, thread));
405 FD(evsel, cpu, thread) = -1;
409 void perf_evsel__exit(struct perf_evsel *evsel)
411 assert(list_empty(&evsel->node));
412 xyarray__delete(evsel->fd);
413 xyarray__delete(evsel->sample_id);
417 void perf_evsel__delete(struct perf_evsel *evsel)
419 perf_evsel__exit(evsel);
420 close_cgroup(evsel->cgrp);
425 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
426 int cpu, int thread, bool scale)
428 struct perf_counts_values count;
429 size_t nv = scale ? 3 : 1;
431 if (FD(evsel, cpu, thread) < 0)
434 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
437 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
443 else if (count.run < count.ena)
444 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
446 count.ena = count.run = 0;
448 evsel->counts->cpu[cpu] = count;
452 int __perf_evsel__read(struct perf_evsel *evsel,
453 int ncpus, int nthreads, bool scale)
455 size_t nv = scale ? 3 : 1;
457 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
459 aggr->val = aggr->ena = aggr->run = 0;
461 for (cpu = 0; cpu < ncpus; cpu++) {
462 for (thread = 0; thread < nthreads; thread++) {
463 if (FD(evsel, cpu, thread) < 0)
466 if (readn(FD(evsel, cpu, thread),
467 &count, nv * sizeof(u64)) < 0)
470 aggr->val += count.val;
472 aggr->ena += count.ena;
473 aggr->run += count.run;
478 evsel->counts->scaled = 0;
480 if (aggr->run == 0) {
481 evsel->counts->scaled = -1;
486 if (aggr->run < aggr->ena) {
487 evsel->counts->scaled = 1;
488 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
491 aggr->ena = aggr->run = 0;
496 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
497 struct thread_map *threads, bool group,
498 struct xyarray *group_fds)
501 unsigned long flags = 0;
504 if (evsel->fd == NULL &&
505 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
509 flags = PERF_FLAG_PID_CGROUP;
510 pid = evsel->cgrp->fd;
513 for (cpu = 0; cpu < cpus->nr; cpu++) {
514 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
516 for (thread = 0; thread < threads->nr; thread++) {
519 pid = threads->map[thread];
521 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
525 if (FD(evsel, cpu, thread) < 0) {
530 if (group && group_fd == -1)
531 group_fd = FD(evsel, cpu, thread);
539 while (--thread >= 0) {
540 close(FD(evsel, cpu, thread));
541 FD(evsel, cpu, thread) = -1;
543 thread = threads->nr;
544 } while (--cpu >= 0);
548 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
550 if (evsel->fd == NULL)
553 perf_evsel__close_fd(evsel, ncpus, nthreads);
554 perf_evsel__free_fd(evsel);
567 struct thread_map map;
569 } empty_thread_map = {
574 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
575 struct thread_map *threads, bool group,
576 struct xyarray *group_fd)
579 /* Work around old compiler warnings about strict aliasing */
580 cpus = &empty_cpu_map.map;
584 threads = &empty_thread_map.map;
586 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
589 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
590 struct cpu_map *cpus, bool group,
591 struct xyarray *group_fd)
593 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
597 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
598 struct thread_map *threads, bool group,
599 struct xyarray *group_fd)
601 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
605 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
606 struct perf_sample *sample,
609 const u64 *array = event->sample.array;
612 array += ((event->header.size -
613 sizeof(event->header)) / sizeof(u64)) - 1;
615 if (type & PERF_SAMPLE_CPU) {
618 /* undo swap of u64, then swap on individual u32s */
619 u.val64 = bswap_64(u.val64);
620 u.val32[0] = bswap_32(u.val32[0]);
623 sample->cpu = u.val32[0];
627 if (type & PERF_SAMPLE_STREAM_ID) {
628 sample->stream_id = *array;
632 if (type & PERF_SAMPLE_ID) {
637 if (type & PERF_SAMPLE_TIME) {
638 sample->time = *array;
642 if (type & PERF_SAMPLE_TID) {
645 /* undo swap of u64, then swap on individual u32s */
646 u.val64 = bswap_64(u.val64);
647 u.val32[0] = bswap_32(u.val32[0]);
648 u.val32[1] = bswap_32(u.val32[1]);
651 sample->pid = u.val32[0];
652 sample->tid = u.val32[1];
658 static bool sample_overlap(const union perf_event *event,
659 const void *offset, u64 size)
661 const void *base = event;
663 if (offset + size > base + event->header.size)
669 int perf_event__parse_sample(const union perf_event *event, u64 type,
670 int sample_size, bool sample_id_all,
671 struct perf_sample *data, bool swapped)
676 * used for cross-endian analysis. See git commit 65014ab3
677 * for why this goofiness is needed.
681 memset(data, 0, sizeof(*data));
682 data->cpu = data->pid = data->tid = -1;
683 data->stream_id = data->id = data->time = -1ULL;
686 if (event->header.type != PERF_RECORD_SAMPLE) {
689 return perf_event__parse_id_sample(event, type, data, swapped);
692 array = event->sample.array;
694 if (sample_size + sizeof(event->header) > event->header.size)
697 if (type & PERF_SAMPLE_IP) {
698 data->ip = event->ip.ip;
702 if (type & PERF_SAMPLE_TID) {
705 /* undo swap of u64, then swap on individual u32s */
706 u.val64 = bswap_64(u.val64);
707 u.val32[0] = bswap_32(u.val32[0]);
708 u.val32[1] = bswap_32(u.val32[1]);
711 data->pid = u.val32[0];
712 data->tid = u.val32[1];
716 if (type & PERF_SAMPLE_TIME) {
722 if (type & PERF_SAMPLE_ADDR) {
728 if (type & PERF_SAMPLE_ID) {
733 if (type & PERF_SAMPLE_STREAM_ID) {
734 data->stream_id = *array;
738 if (type & PERF_SAMPLE_CPU) {
742 /* undo swap of u64, then swap on individual u32s */
743 u.val64 = bswap_64(u.val64);
744 u.val32[0] = bswap_32(u.val32[0]);
747 data->cpu = u.val32[0];
751 if (type & PERF_SAMPLE_PERIOD) {
752 data->period = *array;
756 if (type & PERF_SAMPLE_READ) {
757 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
761 if (type & PERF_SAMPLE_CALLCHAIN) {
762 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
765 data->callchain = (struct ip_callchain *)array;
767 if (sample_overlap(event, array, data->callchain->nr))
770 array += 1 + data->callchain->nr;
773 if (type & PERF_SAMPLE_RAW) {
777 if (WARN_ONCE(swapped,
778 "Endianness of raw data not corrected!\n")) {
779 /* undo swap of u64, then swap on individual u32s */
780 u.val64 = bswap_64(u.val64);
781 u.val32[0] = bswap_32(u.val32[0]);
782 u.val32[1] = bswap_32(u.val32[1]);
785 if (sample_overlap(event, array, sizeof(u32)))
788 data->raw_size = u.val32[0];
789 pdata = (void *) array + sizeof(u32);
791 if (sample_overlap(event, pdata, data->raw_size))
794 data->raw_data = (void *) pdata;
796 array = (void *)array + data->raw_size + sizeof(u32);
799 if (type & PERF_SAMPLE_BRANCH_STACK) {
802 data->branch_stack = (struct branch_stack *)array;
805 sz = data->branch_stack->nr * sizeof(struct branch_entry);
812 int perf_event__synthesize_sample(union perf_event *event, u64 type,
813 const struct perf_sample *sample,
819 * used for cross-endian analysis. See git commit 65014ab3
820 * for why this goofiness is needed.
824 array = event->sample.array;
826 if (type & PERF_SAMPLE_IP) {
827 event->ip.ip = sample->ip;
831 if (type & PERF_SAMPLE_TID) {
832 u.val32[0] = sample->pid;
833 u.val32[1] = sample->tid;
836 * Inverse of what is done in perf_event__parse_sample
838 u.val32[0] = bswap_32(u.val32[0]);
839 u.val32[1] = bswap_32(u.val32[1]);
840 u.val64 = bswap_64(u.val64);
847 if (type & PERF_SAMPLE_TIME) {
848 *array = sample->time;
852 if (type & PERF_SAMPLE_ADDR) {
853 *array = sample->addr;
857 if (type & PERF_SAMPLE_ID) {
862 if (type & PERF_SAMPLE_STREAM_ID) {
863 *array = sample->stream_id;
867 if (type & PERF_SAMPLE_CPU) {
868 u.val32[0] = sample->cpu;
871 * Inverse of what is done in perf_event__parse_sample
873 u.val32[0] = bswap_32(u.val32[0]);
874 u.val64 = bswap_64(u.val64);
880 if (type & PERF_SAMPLE_PERIOD) {
881 *array = sample->period;