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 static 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);
56 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
59 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
61 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
64 perf_evsel__init(evsel, attr, idx);
69 static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
77 "stalled-cycles-frontend",
78 "stalled-cycles-backend",
82 static const char *__perf_evsel__hw_name(u64 config)
84 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
85 return perf_evsel__hw_names[config];
87 return "unknown-hardware";
90 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
93 struct perf_event_attr *attr = &evsel->attr;
94 bool exclude_guest_default = false;
96 #define MOD_PRINT(context, mod) do { \
97 if (!attr->exclude_##context) { \
98 if (!colon) colon = ++r; \
99 r += scnprintf(bf + r, size - r, "%c", mod); \
102 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
103 MOD_PRINT(kernel, 'k');
104 MOD_PRINT(user, 'u');
106 exclude_guest_default = true;
109 if (attr->precise_ip) {
112 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
113 exclude_guest_default = true;
116 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
117 MOD_PRINT(host, 'H');
118 MOD_PRINT(guest, 'G');
126 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
128 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
129 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
132 static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
144 static const char *__perf_evsel__sw_name(u64 config)
146 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
147 return perf_evsel__sw_names[config];
148 return "unknown-software";
151 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
153 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
154 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
157 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
161 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
163 if (type & HW_BREAKPOINT_R)
164 r += scnprintf(bf + r, size - r, "r");
166 if (type & HW_BREAKPOINT_W)
167 r += scnprintf(bf + r, size - r, "w");
169 if (type & HW_BREAKPOINT_X)
170 r += scnprintf(bf + r, size - r, "x");
175 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
177 struct perf_event_attr *attr = &evsel->attr;
178 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
179 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
182 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
183 [PERF_EVSEL__MAX_ALIASES] = {
184 { "L1-dcache", "l1-d", "l1d", "L1-data", },
185 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
187 { "dTLB", "d-tlb", "Data-TLB", },
188 { "iTLB", "i-tlb", "Instruction-TLB", },
189 { "branch", "branches", "bpu", "btb", "bpc", },
193 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
194 [PERF_EVSEL__MAX_ALIASES] = {
195 { "load", "loads", "read", },
196 { "store", "stores", "write", },
197 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
200 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
201 [PERF_EVSEL__MAX_ALIASES] = {
202 { "refs", "Reference", "ops", "access", },
203 { "misses", "miss", },
206 #define C(x) PERF_COUNT_HW_CACHE_##x
207 #define CACHE_READ (1 << C(OP_READ))
208 #define CACHE_WRITE (1 << C(OP_WRITE))
209 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
210 #define COP(x) (1 << x)
213 * cache operartion stat
214 * L1I : Read and prefetch only
215 * ITLB and BPU : Read-only
217 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
218 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
219 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
220 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
221 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
222 [C(ITLB)] = (CACHE_READ),
223 [C(BPU)] = (CACHE_READ),
224 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
227 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
229 if (perf_evsel__hw_cache_stat[type] & COP(op))
230 return true; /* valid */
232 return false; /* invalid */
235 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
236 char *bf, size_t size)
239 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
240 perf_evsel__hw_cache_op[op][0],
241 perf_evsel__hw_cache_result[result][0]);
244 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
245 perf_evsel__hw_cache_op[op][1]);
248 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
250 u8 op, result, type = (config >> 0) & 0xff;
251 const char *err = "unknown-ext-hardware-cache-type";
253 if (type > PERF_COUNT_HW_CACHE_MAX)
256 op = (config >> 8) & 0xff;
257 err = "unknown-ext-hardware-cache-op";
258 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
261 result = (config >> 16) & 0xff;
262 err = "unknown-ext-hardware-cache-result";
263 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
266 err = "invalid-cache";
267 if (!perf_evsel__is_cache_op_valid(type, op))
270 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
272 return scnprintf(bf, size, "%s", err);
275 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
277 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
278 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
281 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
283 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
284 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
287 const char *perf_evsel__name(struct perf_evsel *evsel)
294 switch (evsel->attr.type) {
296 perf_evsel__raw_name(evsel, bf, sizeof(bf));
299 case PERF_TYPE_HARDWARE:
300 perf_evsel__hw_name(evsel, bf, sizeof(bf));
303 case PERF_TYPE_HW_CACHE:
304 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
307 case PERF_TYPE_SOFTWARE:
308 perf_evsel__sw_name(evsel, bf, sizeof(bf));
311 case PERF_TYPE_TRACEPOINT:
312 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
315 case PERF_TYPE_BREAKPOINT:
316 perf_evsel__bp_name(evsel, bf, sizeof(bf));
320 scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
324 evsel->name = strdup(bf);
326 return evsel->name ?: "unknown";
329 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
330 struct perf_evsel *first)
332 struct perf_event_attr *attr = &evsel->attr;
333 int track = !evsel->idx; /* only the first counter needs these */
336 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
337 attr->inherit = !opts->no_inherit;
338 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
339 PERF_FORMAT_TOTAL_TIME_RUNNING |
342 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
345 * We default some events to a 1 default interval. But keep
346 * it a weak assumption overridable by the user.
348 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
349 opts->user_interval != ULLONG_MAX)) {
351 attr->sample_type |= PERF_SAMPLE_PERIOD;
353 attr->sample_freq = opts->freq;
355 attr->sample_period = opts->default_interval;
359 if (opts->no_samples)
360 attr->sample_freq = 0;
362 if (opts->inherit_stat)
363 attr->inherit_stat = 1;
365 if (opts->sample_address) {
366 attr->sample_type |= PERF_SAMPLE_ADDR;
367 attr->mmap_data = track;
370 if (opts->call_graph)
371 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
373 if (perf_target__has_cpu(&opts->target))
374 attr->sample_type |= PERF_SAMPLE_CPU;
377 attr->sample_type |= PERF_SAMPLE_PERIOD;
379 if (!opts->sample_id_all_missing &&
380 (opts->sample_time || !opts->no_inherit ||
381 perf_target__has_cpu(&opts->target)))
382 attr->sample_type |= PERF_SAMPLE_TIME;
384 if (opts->raw_samples) {
385 attr->sample_type |= PERF_SAMPLE_TIME;
386 attr->sample_type |= PERF_SAMPLE_RAW;
387 attr->sample_type |= PERF_SAMPLE_CPU;
390 if (opts->no_delay) {
392 attr->wakeup_events = 1;
394 if (opts->branch_stack) {
395 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
396 attr->branch_sample_type = opts->branch_stack;
402 if (perf_target__none(&opts->target) &&
403 (!opts->group || evsel == first)) {
404 attr->enable_on_exec = 1;
408 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
411 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
414 for (cpu = 0; cpu < ncpus; cpu++) {
415 for (thread = 0; thread < nthreads; thread++) {
416 FD(evsel, cpu, thread) = -1;
421 return evsel->fd != NULL ? 0 : -ENOMEM;
424 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
426 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
427 if (evsel->sample_id == NULL)
430 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
431 if (evsel->id == NULL) {
432 xyarray__delete(evsel->sample_id);
433 evsel->sample_id = NULL;
440 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
442 evsel->counts = zalloc((sizeof(*evsel->counts) +
443 (ncpus * sizeof(struct perf_counts_values))));
444 return evsel->counts != NULL ? 0 : -ENOMEM;
447 void perf_evsel__free_fd(struct perf_evsel *evsel)
449 xyarray__delete(evsel->fd);
453 void perf_evsel__free_id(struct perf_evsel *evsel)
455 xyarray__delete(evsel->sample_id);
456 evsel->sample_id = NULL;
461 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
465 for (cpu = 0; cpu < ncpus; cpu++)
466 for (thread = 0; thread < nthreads; ++thread) {
467 close(FD(evsel, cpu, thread));
468 FD(evsel, cpu, thread) = -1;
472 void perf_evsel__exit(struct perf_evsel *evsel)
474 assert(list_empty(&evsel->node));
475 xyarray__delete(evsel->fd);
476 xyarray__delete(evsel->sample_id);
480 void perf_evsel__delete(struct perf_evsel *evsel)
482 perf_evsel__exit(evsel);
483 close_cgroup(evsel->cgrp);
488 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
489 int cpu, int thread, bool scale)
491 struct perf_counts_values count;
492 size_t nv = scale ? 3 : 1;
494 if (FD(evsel, cpu, thread) < 0)
497 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
500 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
506 else if (count.run < count.ena)
507 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
509 count.ena = count.run = 0;
511 evsel->counts->cpu[cpu] = count;
515 int __perf_evsel__read(struct perf_evsel *evsel,
516 int ncpus, int nthreads, bool scale)
518 size_t nv = scale ? 3 : 1;
520 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
522 aggr->val = aggr->ena = aggr->run = 0;
524 for (cpu = 0; cpu < ncpus; cpu++) {
525 for (thread = 0; thread < nthreads; thread++) {
526 if (FD(evsel, cpu, thread) < 0)
529 if (readn(FD(evsel, cpu, thread),
530 &count, nv * sizeof(u64)) < 0)
533 aggr->val += count.val;
535 aggr->ena += count.ena;
536 aggr->run += count.run;
541 evsel->counts->scaled = 0;
543 if (aggr->run == 0) {
544 evsel->counts->scaled = -1;
549 if (aggr->run < aggr->ena) {
550 evsel->counts->scaled = 1;
551 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
554 aggr->ena = aggr->run = 0;
559 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
560 struct thread_map *threads, bool group,
561 struct xyarray *group_fds)
564 unsigned long flags = 0;
567 if (evsel->fd == NULL &&
568 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
572 flags = PERF_FLAG_PID_CGROUP;
573 pid = evsel->cgrp->fd;
576 for (cpu = 0; cpu < cpus->nr; cpu++) {
577 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
579 for (thread = 0; thread < threads->nr; thread++) {
582 pid = threads->map[thread];
584 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
588 if (FD(evsel, cpu, thread) < 0) {
593 if (group && group_fd == -1)
594 group_fd = FD(evsel, cpu, thread);
602 while (--thread >= 0) {
603 close(FD(evsel, cpu, thread));
604 FD(evsel, cpu, thread) = -1;
606 thread = threads->nr;
607 } while (--cpu >= 0);
611 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
613 if (evsel->fd == NULL)
616 perf_evsel__close_fd(evsel, ncpus, nthreads);
617 perf_evsel__free_fd(evsel);
630 struct thread_map map;
632 } empty_thread_map = {
637 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
638 struct thread_map *threads, bool group,
639 struct xyarray *group_fd)
642 /* Work around old compiler warnings about strict aliasing */
643 cpus = &empty_cpu_map.map;
647 threads = &empty_thread_map.map;
649 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
652 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
653 struct cpu_map *cpus, bool group,
654 struct xyarray *group_fd)
656 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
660 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
661 struct thread_map *threads, bool group,
662 struct xyarray *group_fd)
664 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
668 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
669 struct perf_sample *sample,
672 const u64 *array = event->sample.array;
675 array += ((event->header.size -
676 sizeof(event->header)) / sizeof(u64)) - 1;
678 if (type & PERF_SAMPLE_CPU) {
681 /* undo swap of u64, then swap on individual u32s */
682 u.val64 = bswap_64(u.val64);
683 u.val32[0] = bswap_32(u.val32[0]);
686 sample->cpu = u.val32[0];
690 if (type & PERF_SAMPLE_STREAM_ID) {
691 sample->stream_id = *array;
695 if (type & PERF_SAMPLE_ID) {
700 if (type & PERF_SAMPLE_TIME) {
701 sample->time = *array;
705 if (type & PERF_SAMPLE_TID) {
708 /* undo swap of u64, then swap on individual u32s */
709 u.val64 = bswap_64(u.val64);
710 u.val32[0] = bswap_32(u.val32[0]);
711 u.val32[1] = bswap_32(u.val32[1]);
714 sample->pid = u.val32[0];
715 sample->tid = u.val32[1];
721 static bool sample_overlap(const union perf_event *event,
722 const void *offset, u64 size)
724 const void *base = event;
726 if (offset + size > base + event->header.size)
732 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
733 struct perf_sample *data, bool swapped)
735 u64 type = evsel->attr.sample_type;
739 * used for cross-endian analysis. See git commit 65014ab3
740 * for why this goofiness is needed.
744 memset(data, 0, sizeof(*data));
745 data->cpu = data->pid = data->tid = -1;
746 data->stream_id = data->id = data->time = -1ULL;
749 if (event->header.type != PERF_RECORD_SAMPLE) {
750 if (!evsel->attr.sample_id_all)
752 return perf_event__parse_id_sample(event, type, data, swapped);
755 array = event->sample.array;
757 if (evsel->sample_size + sizeof(event->header) > event->header.size)
760 if (type & PERF_SAMPLE_IP) {
761 data->ip = event->ip.ip;
765 if (type & PERF_SAMPLE_TID) {
768 /* undo swap of u64, then swap on individual u32s */
769 u.val64 = bswap_64(u.val64);
770 u.val32[0] = bswap_32(u.val32[0]);
771 u.val32[1] = bswap_32(u.val32[1]);
774 data->pid = u.val32[0];
775 data->tid = u.val32[1];
779 if (type & PERF_SAMPLE_TIME) {
785 if (type & PERF_SAMPLE_ADDR) {
791 if (type & PERF_SAMPLE_ID) {
796 if (type & PERF_SAMPLE_STREAM_ID) {
797 data->stream_id = *array;
801 if (type & PERF_SAMPLE_CPU) {
805 /* undo swap of u64, then swap on individual u32s */
806 u.val64 = bswap_64(u.val64);
807 u.val32[0] = bswap_32(u.val32[0]);
810 data->cpu = u.val32[0];
814 if (type & PERF_SAMPLE_PERIOD) {
815 data->period = *array;
819 if (type & PERF_SAMPLE_READ) {
820 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
824 if (type & PERF_SAMPLE_CALLCHAIN) {
825 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
828 data->callchain = (struct ip_callchain *)array;
830 if (sample_overlap(event, array, data->callchain->nr))
833 array += 1 + data->callchain->nr;
836 if (type & PERF_SAMPLE_RAW) {
840 if (WARN_ONCE(swapped,
841 "Endianness of raw data not corrected!\n")) {
842 /* undo swap of u64, then swap on individual u32s */
843 u.val64 = bswap_64(u.val64);
844 u.val32[0] = bswap_32(u.val32[0]);
845 u.val32[1] = bswap_32(u.val32[1]);
848 if (sample_overlap(event, array, sizeof(u32)))
851 data->raw_size = u.val32[0];
852 pdata = (void *) array + sizeof(u32);
854 if (sample_overlap(event, pdata, data->raw_size))
857 data->raw_data = (void *) pdata;
859 array = (void *)array + data->raw_size + sizeof(u32);
862 if (type & PERF_SAMPLE_BRANCH_STACK) {
865 data->branch_stack = (struct branch_stack *)array;
868 sz = data->branch_stack->nr * sizeof(struct branch_entry);
875 int perf_event__synthesize_sample(union perf_event *event, u64 type,
876 const struct perf_sample *sample,
882 * used for cross-endian analysis. See git commit 65014ab3
883 * for why this goofiness is needed.
887 array = event->sample.array;
889 if (type & PERF_SAMPLE_IP) {
890 event->ip.ip = sample->ip;
894 if (type & PERF_SAMPLE_TID) {
895 u.val32[0] = sample->pid;
896 u.val32[1] = sample->tid;
899 * Inverse of what is done in perf_evsel__parse_sample
901 u.val32[0] = bswap_32(u.val32[0]);
902 u.val32[1] = bswap_32(u.val32[1]);
903 u.val64 = bswap_64(u.val64);
910 if (type & PERF_SAMPLE_TIME) {
911 *array = sample->time;
915 if (type & PERF_SAMPLE_ADDR) {
916 *array = sample->addr;
920 if (type & PERF_SAMPLE_ID) {
925 if (type & PERF_SAMPLE_STREAM_ID) {
926 *array = sample->stream_id;
930 if (type & PERF_SAMPLE_CPU) {
931 u.val32[0] = sample->cpu;
934 * Inverse of what is done in perf_evsel__parse_sample
936 u.val32[0] = bswap_32(u.val32[0]);
937 u.val64 = bswap_64(u.val64);
943 if (type & PERF_SAMPLE_PERIOD) {
944 *array = sample->period;