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/hw_breakpoint.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 static 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 static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
143 static const char *__perf_evsel__sw_name(u64 config)
145 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
146 return perf_evsel__sw_names[config];
147 return "unknown-software";
150 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
152 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
153 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
156 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
160 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
162 if (type & HW_BREAKPOINT_R)
163 r += scnprintf(bf + r, size - r, "r");
165 if (type & HW_BREAKPOINT_W)
166 r += scnprintf(bf + r, size - r, "w");
168 if (type & HW_BREAKPOINT_X)
169 r += scnprintf(bf + r, size - r, "x");
174 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
176 struct perf_event_attr *attr = &evsel->attr;
177 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
178 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
181 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
182 [PERF_EVSEL__MAX_ALIASES] = {
183 { "L1-dcache", "l1-d", "l1d", "L1-data", },
184 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
186 { "dTLB", "d-tlb", "Data-TLB", },
187 { "iTLB", "i-tlb", "Instruction-TLB", },
188 { "branch", "branches", "bpu", "btb", "bpc", },
192 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
193 [PERF_EVSEL__MAX_ALIASES] = {
194 { "load", "loads", "read", },
195 { "store", "stores", "write", },
196 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
199 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
200 [PERF_EVSEL__MAX_ALIASES] = {
201 { "refs", "Reference", "ops", "access", },
202 { "misses", "miss", },
205 #define C(x) PERF_COUNT_HW_CACHE_##x
206 #define CACHE_READ (1 << C(OP_READ))
207 #define CACHE_WRITE (1 << C(OP_WRITE))
208 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
209 #define COP(x) (1 << x)
212 * cache operartion stat
213 * L1I : Read and prefetch only
214 * ITLB and BPU : Read-only
216 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
217 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
218 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
219 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
220 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
221 [C(ITLB)] = (CACHE_READ),
222 [C(BPU)] = (CACHE_READ),
223 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
226 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
228 if (perf_evsel__hw_cache_stat[type] & COP(op))
229 return true; /* valid */
231 return false; /* invalid */
234 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
235 char *bf, size_t size)
238 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
239 perf_evsel__hw_cache_op[op][0],
240 perf_evsel__hw_cache_result[result][0]);
243 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
244 perf_evsel__hw_cache_op[op][1]);
247 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
249 u8 op, result, type = (config >> 0) & 0xff;
250 const char *err = "unknown-ext-hardware-cache-type";
252 if (type > PERF_COUNT_HW_CACHE_MAX)
255 op = (config >> 8) & 0xff;
256 err = "unknown-ext-hardware-cache-op";
257 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
260 result = (config >> 16) & 0xff;
261 err = "unknown-ext-hardware-cache-result";
262 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
265 err = "invalid-cache";
266 if (!perf_evsel__is_cache_op_valid(type, op))
269 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
271 return scnprintf(bf, size, "%s", err);
274 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
276 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
277 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
280 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
282 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
283 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
286 const char *perf_evsel__name(struct perf_evsel *evsel)
293 switch (evsel->attr.type) {
295 perf_evsel__raw_name(evsel, bf, sizeof(bf));
298 case PERF_TYPE_HARDWARE:
299 perf_evsel__hw_name(evsel, bf, sizeof(bf));
302 case PERF_TYPE_HW_CACHE:
303 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
306 case PERF_TYPE_SOFTWARE:
307 perf_evsel__sw_name(evsel, bf, sizeof(bf));
310 case PERF_TYPE_TRACEPOINT:
311 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
314 case PERF_TYPE_BREAKPOINT:
315 perf_evsel__bp_name(evsel, bf, sizeof(bf));
319 scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
323 evsel->name = strdup(bf);
325 return evsel->name ?: "unknown";
328 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
329 struct perf_evsel *first)
331 struct perf_event_attr *attr = &evsel->attr;
332 int track = !evsel->idx; /* only the first counter needs these */
335 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
336 attr->inherit = !opts->no_inherit;
337 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
338 PERF_FORMAT_TOTAL_TIME_RUNNING |
341 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
344 * We default some events to a 1 default interval. But keep
345 * it a weak assumption overridable by the user.
347 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
348 opts->user_interval != ULLONG_MAX)) {
350 attr->sample_type |= PERF_SAMPLE_PERIOD;
352 attr->sample_freq = opts->freq;
354 attr->sample_period = opts->default_interval;
358 if (opts->no_samples)
359 attr->sample_freq = 0;
361 if (opts->inherit_stat)
362 attr->inherit_stat = 1;
364 if (opts->sample_address) {
365 attr->sample_type |= PERF_SAMPLE_ADDR;
366 attr->mmap_data = track;
369 if (opts->call_graph)
370 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
372 if (perf_target__has_cpu(&opts->target))
373 attr->sample_type |= PERF_SAMPLE_CPU;
376 attr->sample_type |= PERF_SAMPLE_PERIOD;
378 if (!opts->sample_id_all_missing &&
379 (opts->sample_time || !opts->no_inherit ||
380 perf_target__has_cpu(&opts->target)))
381 attr->sample_type |= PERF_SAMPLE_TIME;
383 if (opts->raw_samples) {
384 attr->sample_type |= PERF_SAMPLE_TIME;
385 attr->sample_type |= PERF_SAMPLE_RAW;
386 attr->sample_type |= PERF_SAMPLE_CPU;
389 if (opts->no_delay) {
391 attr->wakeup_events = 1;
393 if (opts->branch_stack) {
394 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
395 attr->branch_sample_type = opts->branch_stack;
401 if (perf_target__none(&opts->target) &&
402 (!opts->group || evsel == first)) {
403 attr->enable_on_exec = 1;
407 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
410 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
413 for (cpu = 0; cpu < ncpus; cpu++) {
414 for (thread = 0; thread < nthreads; thread++) {
415 FD(evsel, cpu, thread) = -1;
420 return evsel->fd != NULL ? 0 : -ENOMEM;
423 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
425 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
426 if (evsel->sample_id == NULL)
429 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
430 if (evsel->id == NULL) {
431 xyarray__delete(evsel->sample_id);
432 evsel->sample_id = NULL;
439 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
441 evsel->counts = zalloc((sizeof(*evsel->counts) +
442 (ncpus * sizeof(struct perf_counts_values))));
443 return evsel->counts != NULL ? 0 : -ENOMEM;
446 void perf_evsel__free_fd(struct perf_evsel *evsel)
448 xyarray__delete(evsel->fd);
452 void perf_evsel__free_id(struct perf_evsel *evsel)
454 xyarray__delete(evsel->sample_id);
455 evsel->sample_id = NULL;
460 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
464 for (cpu = 0; cpu < ncpus; cpu++)
465 for (thread = 0; thread < nthreads; ++thread) {
466 close(FD(evsel, cpu, thread));
467 FD(evsel, cpu, thread) = -1;
471 void perf_evsel__exit(struct perf_evsel *evsel)
473 assert(list_empty(&evsel->node));
474 xyarray__delete(evsel->fd);
475 xyarray__delete(evsel->sample_id);
479 void perf_evsel__delete(struct perf_evsel *evsel)
481 perf_evsel__exit(evsel);
482 close_cgroup(evsel->cgrp);
487 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
488 int cpu, int thread, bool scale)
490 struct perf_counts_values count;
491 size_t nv = scale ? 3 : 1;
493 if (FD(evsel, cpu, thread) < 0)
496 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
499 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
505 else if (count.run < count.ena)
506 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
508 count.ena = count.run = 0;
510 evsel->counts->cpu[cpu] = count;
514 int __perf_evsel__read(struct perf_evsel *evsel,
515 int ncpus, int nthreads, bool scale)
517 size_t nv = scale ? 3 : 1;
519 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
521 aggr->val = aggr->ena = aggr->run = 0;
523 for (cpu = 0; cpu < ncpus; cpu++) {
524 for (thread = 0; thread < nthreads; thread++) {
525 if (FD(evsel, cpu, thread) < 0)
528 if (readn(FD(evsel, cpu, thread),
529 &count, nv * sizeof(u64)) < 0)
532 aggr->val += count.val;
534 aggr->ena += count.ena;
535 aggr->run += count.run;
540 evsel->counts->scaled = 0;
542 if (aggr->run == 0) {
543 evsel->counts->scaled = -1;
548 if (aggr->run < aggr->ena) {
549 evsel->counts->scaled = 1;
550 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
553 aggr->ena = aggr->run = 0;
558 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
559 struct thread_map *threads, bool group,
560 struct xyarray *group_fds)
563 unsigned long flags = 0;
566 if (evsel->fd == NULL &&
567 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
571 flags = PERF_FLAG_PID_CGROUP;
572 pid = evsel->cgrp->fd;
575 for (cpu = 0; cpu < cpus->nr; cpu++) {
576 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
578 for (thread = 0; thread < threads->nr; thread++) {
581 pid = threads->map[thread];
583 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
587 if (FD(evsel, cpu, thread) < 0) {
592 if (group && group_fd == -1)
593 group_fd = FD(evsel, cpu, thread);
601 while (--thread >= 0) {
602 close(FD(evsel, cpu, thread));
603 FD(evsel, cpu, thread) = -1;
605 thread = threads->nr;
606 } while (--cpu >= 0);
610 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
612 if (evsel->fd == NULL)
615 perf_evsel__close_fd(evsel, ncpus, nthreads);
616 perf_evsel__free_fd(evsel);
629 struct thread_map map;
631 } empty_thread_map = {
636 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
637 struct thread_map *threads, bool group,
638 struct xyarray *group_fd)
641 /* Work around old compiler warnings about strict aliasing */
642 cpus = &empty_cpu_map.map;
646 threads = &empty_thread_map.map;
648 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
651 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
652 struct cpu_map *cpus, bool group,
653 struct xyarray *group_fd)
655 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
659 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
660 struct thread_map *threads, bool group,
661 struct xyarray *group_fd)
663 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
667 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
668 struct perf_sample *sample,
671 const u64 *array = event->sample.array;
674 array += ((event->header.size -
675 sizeof(event->header)) / sizeof(u64)) - 1;
677 if (type & PERF_SAMPLE_CPU) {
680 /* undo swap of u64, then swap on individual u32s */
681 u.val64 = bswap_64(u.val64);
682 u.val32[0] = bswap_32(u.val32[0]);
685 sample->cpu = u.val32[0];
689 if (type & PERF_SAMPLE_STREAM_ID) {
690 sample->stream_id = *array;
694 if (type & PERF_SAMPLE_ID) {
699 if (type & PERF_SAMPLE_TIME) {
700 sample->time = *array;
704 if (type & PERF_SAMPLE_TID) {
707 /* undo swap of u64, then swap on individual u32s */
708 u.val64 = bswap_64(u.val64);
709 u.val32[0] = bswap_32(u.val32[0]);
710 u.val32[1] = bswap_32(u.val32[1]);
713 sample->pid = u.val32[0];
714 sample->tid = u.val32[1];
720 static bool sample_overlap(const union perf_event *event,
721 const void *offset, u64 size)
723 const void *base = event;
725 if (offset + size > base + event->header.size)
731 int perf_event__parse_sample(const union perf_event *event, u64 type,
732 int sample_size, bool sample_id_all,
733 struct perf_sample *data, bool swapped)
738 * used for cross-endian analysis. See git commit 65014ab3
739 * for why this goofiness is needed.
743 memset(data, 0, sizeof(*data));
744 data->cpu = data->pid = data->tid = -1;
745 data->stream_id = data->id = data->time = -1ULL;
748 if (event->header.type != PERF_RECORD_SAMPLE) {
751 return perf_event__parse_id_sample(event, type, data, swapped);
754 array = event->sample.array;
756 if (sample_size + sizeof(event->header) > event->header.size)
759 if (type & PERF_SAMPLE_IP) {
760 data->ip = event->ip.ip;
764 if (type & PERF_SAMPLE_TID) {
767 /* undo swap of u64, then swap on individual u32s */
768 u.val64 = bswap_64(u.val64);
769 u.val32[0] = bswap_32(u.val32[0]);
770 u.val32[1] = bswap_32(u.val32[1]);
773 data->pid = u.val32[0];
774 data->tid = u.val32[1];
778 if (type & PERF_SAMPLE_TIME) {
784 if (type & PERF_SAMPLE_ADDR) {
790 if (type & PERF_SAMPLE_ID) {
795 if (type & PERF_SAMPLE_STREAM_ID) {
796 data->stream_id = *array;
800 if (type & PERF_SAMPLE_CPU) {
804 /* undo swap of u64, then swap on individual u32s */
805 u.val64 = bswap_64(u.val64);
806 u.val32[0] = bswap_32(u.val32[0]);
809 data->cpu = u.val32[0];
813 if (type & PERF_SAMPLE_PERIOD) {
814 data->period = *array;
818 if (type & PERF_SAMPLE_READ) {
819 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
823 if (type & PERF_SAMPLE_CALLCHAIN) {
824 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
827 data->callchain = (struct ip_callchain *)array;
829 if (sample_overlap(event, array, data->callchain->nr))
832 array += 1 + data->callchain->nr;
835 if (type & PERF_SAMPLE_RAW) {
839 if (WARN_ONCE(swapped,
840 "Endianness of raw data not corrected!\n")) {
841 /* undo swap of u64, then swap on individual u32s */
842 u.val64 = bswap_64(u.val64);
843 u.val32[0] = bswap_32(u.val32[0]);
844 u.val32[1] = bswap_32(u.val32[1]);
847 if (sample_overlap(event, array, sizeof(u32)))
850 data->raw_size = u.val32[0];
851 pdata = (void *) array + sizeof(u32);
853 if (sample_overlap(event, pdata, data->raw_size))
856 data->raw_data = (void *) pdata;
858 array = (void *)array + data->raw_size + sizeof(u32);
861 if (type & PERF_SAMPLE_BRANCH_STACK) {
864 data->branch_stack = (struct branch_stack *)array;
867 sz = data->branch_stack->nr * sizeof(struct branch_entry);
874 int perf_event__synthesize_sample(union perf_event *event, u64 type,
875 const struct perf_sample *sample,
881 * used for cross-endian analysis. See git commit 65014ab3
882 * for why this goofiness is needed.
886 array = event->sample.array;
888 if (type & PERF_SAMPLE_IP) {
889 event->ip.ip = sample->ip;
893 if (type & PERF_SAMPLE_TID) {
894 u.val32[0] = sample->pid;
895 u.val32[1] = sample->tid;
898 * Inverse of what is done in perf_event__parse_sample
900 u.val32[0] = bswap_32(u.val32[0]);
901 u.val32[1] = bswap_32(u.val32[1]);
902 u.val64 = bswap_64(u.val64);
909 if (type & PERF_SAMPLE_TIME) {
910 *array = sample->time;
914 if (type & PERF_SAMPLE_ADDR) {
915 *array = sample->addr;
919 if (type & PERF_SAMPLE_ID) {
924 if (type & PERF_SAMPLE_STREAM_ID) {
925 *array = sample->stream_id;
929 if (type & PERF_SAMPLE_CPU) {
930 u.val32[0] = sample->cpu;
933 * Inverse of what is done in perf_event__parse_sample
935 u.val32[0] = bswap_32(u.val32[0]);
936 u.val64 = bswap_64(u.val64);
942 if (type & PERF_SAMPLE_PERIOD) {
943 *array = sample->period;