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 static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
143 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 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
157 [PERF_EVSEL__MAX_ALIASES] = {
158 { "L1-dcache", "l1-d", "l1d", "L1-data", },
159 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
161 { "dTLB", "d-tlb", "Data-TLB", },
162 { "iTLB", "i-tlb", "Instruction-TLB", },
163 { "branch", "branches", "bpu", "btb", "bpc", },
167 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
168 [PERF_EVSEL__MAX_ALIASES] = {
169 { "load", "loads", "read", },
170 { "store", "stores", "write", },
171 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
174 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
175 [PERF_EVSEL__MAX_ALIASES] = {
176 { "refs", "Reference", "ops", "access", },
177 { "misses", "miss", },
180 #define C(x) PERF_COUNT_HW_CACHE_##x
181 #define CACHE_READ (1 << C(OP_READ))
182 #define CACHE_WRITE (1 << C(OP_WRITE))
183 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
184 #define COP(x) (1 << x)
187 * cache operartion stat
188 * L1I : Read and prefetch only
189 * ITLB and BPU : Read-only
191 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
192 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
193 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
194 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
195 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
196 [C(ITLB)] = (CACHE_READ),
197 [C(BPU)] = (CACHE_READ),
198 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
201 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
203 if (perf_evsel__hw_cache_stat[type] & COP(op))
204 return true; /* valid */
206 return false; /* invalid */
209 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
210 char *bf, size_t size)
213 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
214 perf_evsel__hw_cache_op[op][0],
215 perf_evsel__hw_cache_result[result][0]);
218 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
219 perf_evsel__hw_cache_op[op][1]);
222 int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
224 u8 op, result, type = (config >> 0) & 0xff;
225 const char *err = "unknown-ext-hardware-cache-type";
227 if (type > PERF_COUNT_HW_CACHE_MAX)
230 op = (config >> 8) & 0xff;
231 err = "unknown-ext-hardware-cache-op";
232 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
235 result = (config >> 16) & 0xff;
236 err = "unknown-ext-hardware-cache-result";
237 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
240 err = "invalid-cache";
241 if (!perf_evsel__is_cache_op_valid(type, op))
244 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
246 return scnprintf(bf, size, "%s", err);
249 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
251 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
252 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
255 int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
259 switch (evsel->attr.type) {
261 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
264 case PERF_TYPE_HARDWARE:
265 ret = perf_evsel__hw_name(evsel, bf, size);
268 case PERF_TYPE_HW_CACHE:
269 ret = perf_evsel__hw_cache_name(evsel, bf, size);
272 case PERF_TYPE_SOFTWARE:
273 ret = perf_evsel__sw_name(evsel, bf, size);
280 * This is the minimal perf_evsel__name so that we can
281 * reconstruct event names taking into account event modifiers.
283 * The old event_name uses it now for raw anr hw events, so that
284 * we don't drag all the parsing stuff into the python binding.
286 * On the next devel cycle the rest of the event naming will be
295 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
296 struct perf_evsel *first)
298 struct perf_event_attr *attr = &evsel->attr;
299 int track = !evsel->idx; /* only the first counter needs these */
302 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
303 attr->inherit = !opts->no_inherit;
304 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
305 PERF_FORMAT_TOTAL_TIME_RUNNING |
308 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
311 * We default some events to a 1 default interval. But keep
312 * it a weak assumption overridable by the user.
314 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
315 opts->user_interval != ULLONG_MAX)) {
317 attr->sample_type |= PERF_SAMPLE_PERIOD;
319 attr->sample_freq = opts->freq;
321 attr->sample_period = opts->default_interval;
325 if (opts->no_samples)
326 attr->sample_freq = 0;
328 if (opts->inherit_stat)
329 attr->inherit_stat = 1;
331 if (opts->sample_address) {
332 attr->sample_type |= PERF_SAMPLE_ADDR;
333 attr->mmap_data = track;
336 if (opts->call_graph)
337 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
339 if (perf_target__has_cpu(&opts->target))
340 attr->sample_type |= PERF_SAMPLE_CPU;
343 attr->sample_type |= PERF_SAMPLE_PERIOD;
345 if (!opts->sample_id_all_missing &&
346 (opts->sample_time || !opts->no_inherit ||
347 perf_target__has_cpu(&opts->target)))
348 attr->sample_type |= PERF_SAMPLE_TIME;
350 if (opts->raw_samples) {
351 attr->sample_type |= PERF_SAMPLE_TIME;
352 attr->sample_type |= PERF_SAMPLE_RAW;
353 attr->sample_type |= PERF_SAMPLE_CPU;
356 if (opts->no_delay) {
358 attr->wakeup_events = 1;
360 if (opts->branch_stack) {
361 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
362 attr->branch_sample_type = opts->branch_stack;
368 if (perf_target__none(&opts->target) &&
369 (!opts->group || evsel == first)) {
370 attr->enable_on_exec = 1;
374 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
377 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
380 for (cpu = 0; cpu < ncpus; cpu++) {
381 for (thread = 0; thread < nthreads; thread++) {
382 FD(evsel, cpu, thread) = -1;
387 return evsel->fd != NULL ? 0 : -ENOMEM;
390 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
392 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
393 if (evsel->sample_id == NULL)
396 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
397 if (evsel->id == NULL) {
398 xyarray__delete(evsel->sample_id);
399 evsel->sample_id = NULL;
406 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
408 evsel->counts = zalloc((sizeof(*evsel->counts) +
409 (ncpus * sizeof(struct perf_counts_values))));
410 return evsel->counts != NULL ? 0 : -ENOMEM;
413 void perf_evsel__free_fd(struct perf_evsel *evsel)
415 xyarray__delete(evsel->fd);
419 void perf_evsel__free_id(struct perf_evsel *evsel)
421 xyarray__delete(evsel->sample_id);
422 evsel->sample_id = NULL;
427 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
431 for (cpu = 0; cpu < ncpus; cpu++)
432 for (thread = 0; thread < nthreads; ++thread) {
433 close(FD(evsel, cpu, thread));
434 FD(evsel, cpu, thread) = -1;
438 void perf_evsel__exit(struct perf_evsel *evsel)
440 assert(list_empty(&evsel->node));
441 xyarray__delete(evsel->fd);
442 xyarray__delete(evsel->sample_id);
446 void perf_evsel__delete(struct perf_evsel *evsel)
448 perf_evsel__exit(evsel);
449 close_cgroup(evsel->cgrp);
454 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
455 int cpu, int thread, bool scale)
457 struct perf_counts_values count;
458 size_t nv = scale ? 3 : 1;
460 if (FD(evsel, cpu, thread) < 0)
463 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
466 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
472 else if (count.run < count.ena)
473 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
475 count.ena = count.run = 0;
477 evsel->counts->cpu[cpu] = count;
481 int __perf_evsel__read(struct perf_evsel *evsel,
482 int ncpus, int nthreads, bool scale)
484 size_t nv = scale ? 3 : 1;
486 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
488 aggr->val = aggr->ena = aggr->run = 0;
490 for (cpu = 0; cpu < ncpus; cpu++) {
491 for (thread = 0; thread < nthreads; thread++) {
492 if (FD(evsel, cpu, thread) < 0)
495 if (readn(FD(evsel, cpu, thread),
496 &count, nv * sizeof(u64)) < 0)
499 aggr->val += count.val;
501 aggr->ena += count.ena;
502 aggr->run += count.run;
507 evsel->counts->scaled = 0;
509 if (aggr->run == 0) {
510 evsel->counts->scaled = -1;
515 if (aggr->run < aggr->ena) {
516 evsel->counts->scaled = 1;
517 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
520 aggr->ena = aggr->run = 0;
525 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
526 struct thread_map *threads, bool group,
527 struct xyarray *group_fds)
530 unsigned long flags = 0;
533 if (evsel->fd == NULL &&
534 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
538 flags = PERF_FLAG_PID_CGROUP;
539 pid = evsel->cgrp->fd;
542 for (cpu = 0; cpu < cpus->nr; cpu++) {
543 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
545 for (thread = 0; thread < threads->nr; thread++) {
548 pid = threads->map[thread];
550 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
554 if (FD(evsel, cpu, thread) < 0) {
559 if (group && group_fd == -1)
560 group_fd = FD(evsel, cpu, thread);
568 while (--thread >= 0) {
569 close(FD(evsel, cpu, thread));
570 FD(evsel, cpu, thread) = -1;
572 thread = threads->nr;
573 } while (--cpu >= 0);
577 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
579 if (evsel->fd == NULL)
582 perf_evsel__close_fd(evsel, ncpus, nthreads);
583 perf_evsel__free_fd(evsel);
596 struct thread_map map;
598 } empty_thread_map = {
603 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
604 struct thread_map *threads, bool group,
605 struct xyarray *group_fd)
608 /* Work around old compiler warnings about strict aliasing */
609 cpus = &empty_cpu_map.map;
613 threads = &empty_thread_map.map;
615 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
618 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
619 struct cpu_map *cpus, bool group,
620 struct xyarray *group_fd)
622 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
626 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
627 struct thread_map *threads, bool group,
628 struct xyarray *group_fd)
630 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
634 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
635 struct perf_sample *sample,
638 const u64 *array = event->sample.array;
641 array += ((event->header.size -
642 sizeof(event->header)) / sizeof(u64)) - 1;
644 if (type & PERF_SAMPLE_CPU) {
647 /* undo swap of u64, then swap on individual u32s */
648 u.val64 = bswap_64(u.val64);
649 u.val32[0] = bswap_32(u.val32[0]);
652 sample->cpu = u.val32[0];
656 if (type & PERF_SAMPLE_STREAM_ID) {
657 sample->stream_id = *array;
661 if (type & PERF_SAMPLE_ID) {
666 if (type & PERF_SAMPLE_TIME) {
667 sample->time = *array;
671 if (type & PERF_SAMPLE_TID) {
674 /* undo swap of u64, then swap on individual u32s */
675 u.val64 = bswap_64(u.val64);
676 u.val32[0] = bswap_32(u.val32[0]);
677 u.val32[1] = bswap_32(u.val32[1]);
680 sample->pid = u.val32[0];
681 sample->tid = u.val32[1];
687 static bool sample_overlap(const union perf_event *event,
688 const void *offset, u64 size)
690 const void *base = event;
692 if (offset + size > base + event->header.size)
698 int perf_event__parse_sample(const union perf_event *event, u64 type,
699 int sample_size, bool sample_id_all,
700 struct perf_sample *data, bool swapped)
705 * used for cross-endian analysis. See git commit 65014ab3
706 * for why this goofiness is needed.
710 memset(data, 0, sizeof(*data));
711 data->cpu = data->pid = data->tid = -1;
712 data->stream_id = data->id = data->time = -1ULL;
715 if (event->header.type != PERF_RECORD_SAMPLE) {
718 return perf_event__parse_id_sample(event, type, data, swapped);
721 array = event->sample.array;
723 if (sample_size + sizeof(event->header) > event->header.size)
726 if (type & PERF_SAMPLE_IP) {
727 data->ip = event->ip.ip;
731 if (type & PERF_SAMPLE_TID) {
734 /* undo swap of u64, then swap on individual u32s */
735 u.val64 = bswap_64(u.val64);
736 u.val32[0] = bswap_32(u.val32[0]);
737 u.val32[1] = bswap_32(u.val32[1]);
740 data->pid = u.val32[0];
741 data->tid = u.val32[1];
745 if (type & PERF_SAMPLE_TIME) {
751 if (type & PERF_SAMPLE_ADDR) {
757 if (type & PERF_SAMPLE_ID) {
762 if (type & PERF_SAMPLE_STREAM_ID) {
763 data->stream_id = *array;
767 if (type & PERF_SAMPLE_CPU) {
771 /* undo swap of u64, then swap on individual u32s */
772 u.val64 = bswap_64(u.val64);
773 u.val32[0] = bswap_32(u.val32[0]);
776 data->cpu = u.val32[0];
780 if (type & PERF_SAMPLE_PERIOD) {
781 data->period = *array;
785 if (type & PERF_SAMPLE_READ) {
786 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
790 if (type & PERF_SAMPLE_CALLCHAIN) {
791 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
794 data->callchain = (struct ip_callchain *)array;
796 if (sample_overlap(event, array, data->callchain->nr))
799 array += 1 + data->callchain->nr;
802 if (type & PERF_SAMPLE_RAW) {
806 if (WARN_ONCE(swapped,
807 "Endianness of raw data not corrected!\n")) {
808 /* undo swap of u64, then swap on individual u32s */
809 u.val64 = bswap_64(u.val64);
810 u.val32[0] = bswap_32(u.val32[0]);
811 u.val32[1] = bswap_32(u.val32[1]);
814 if (sample_overlap(event, array, sizeof(u32)))
817 data->raw_size = u.val32[0];
818 pdata = (void *) array + sizeof(u32);
820 if (sample_overlap(event, pdata, data->raw_size))
823 data->raw_data = (void *) pdata;
825 array = (void *)array + data->raw_size + sizeof(u32);
828 if (type & PERF_SAMPLE_BRANCH_STACK) {
831 data->branch_stack = (struct branch_stack *)array;
834 sz = data->branch_stack->nr * sizeof(struct branch_entry);
841 int perf_event__synthesize_sample(union perf_event *event, u64 type,
842 const struct perf_sample *sample,
848 * used for cross-endian analysis. See git commit 65014ab3
849 * for why this goofiness is needed.
853 array = event->sample.array;
855 if (type & PERF_SAMPLE_IP) {
856 event->ip.ip = sample->ip;
860 if (type & PERF_SAMPLE_TID) {
861 u.val32[0] = sample->pid;
862 u.val32[1] = sample->tid;
865 * Inverse of what is done in perf_event__parse_sample
867 u.val32[0] = bswap_32(u.val32[0]);
868 u.val32[1] = bswap_32(u.val32[1]);
869 u.val64 = bswap_64(u.val64);
876 if (type & PERF_SAMPLE_TIME) {
877 *array = sample->time;
881 if (type & PERF_SAMPLE_ADDR) {
882 *array = sample->addr;
886 if (type & PERF_SAMPLE_ID) {
891 if (type & PERF_SAMPLE_STREAM_ID) {
892 *array = sample->stream_id;
896 if (type & PERF_SAMPLE_CPU) {
897 u.val32[0] = sample->cpu;
900 * Inverse of what is done in perf_event__parse_sample
902 u.val32[0] = bswap_32(u.val32[0]);
903 u.val64 = bswap_64(u.val64);
909 if (type & PERF_SAMPLE_PERIOD) {
910 *array = sample->period;