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__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
92 struct perf_event_attr *attr = &evsel->attr;
93 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(attr->config));
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 int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
130 switch (evsel->attr.type) {
132 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
135 case PERF_TYPE_HARDWARE:
136 ret = perf_evsel__hw_name(evsel, bf, size);
142 * This is the minimal perf_evsel__name so that we can
143 * reconstruct event names taking into account event modifiers.
145 * The old event_name uses it now for raw anr hw events, so that
146 * we don't drag all the parsing stuff into the python binding.
148 * On the next devel cycle the rest of the event naming will be
157 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
158 struct perf_evsel *first)
160 struct perf_event_attr *attr = &evsel->attr;
161 int track = !evsel->idx; /* only the first counter needs these */
164 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
165 attr->inherit = !opts->no_inherit;
166 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
167 PERF_FORMAT_TOTAL_TIME_RUNNING |
170 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
173 * We default some events to a 1 default interval. But keep
174 * it a weak assumption overridable by the user.
176 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
177 opts->user_interval != ULLONG_MAX)) {
179 attr->sample_type |= PERF_SAMPLE_PERIOD;
181 attr->sample_freq = opts->freq;
183 attr->sample_period = opts->default_interval;
187 if (opts->no_samples)
188 attr->sample_freq = 0;
190 if (opts->inherit_stat)
191 attr->inherit_stat = 1;
193 if (opts->sample_address) {
194 attr->sample_type |= PERF_SAMPLE_ADDR;
195 attr->mmap_data = track;
198 if (opts->call_graph)
199 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
201 if (perf_target__has_cpu(&opts->target))
202 attr->sample_type |= PERF_SAMPLE_CPU;
205 attr->sample_type |= PERF_SAMPLE_PERIOD;
207 if (!opts->sample_id_all_missing &&
208 (opts->sample_time || !opts->no_inherit ||
209 perf_target__has_cpu(&opts->target)))
210 attr->sample_type |= PERF_SAMPLE_TIME;
212 if (opts->raw_samples) {
213 attr->sample_type |= PERF_SAMPLE_TIME;
214 attr->sample_type |= PERF_SAMPLE_RAW;
215 attr->sample_type |= PERF_SAMPLE_CPU;
218 if (opts->no_delay) {
220 attr->wakeup_events = 1;
222 if (opts->branch_stack) {
223 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
224 attr->branch_sample_type = opts->branch_stack;
230 if (perf_target__none(&opts->target) &&
231 (!opts->group || evsel == first)) {
232 attr->enable_on_exec = 1;
236 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
239 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
242 for (cpu = 0; cpu < ncpus; cpu++) {
243 for (thread = 0; thread < nthreads; thread++) {
244 FD(evsel, cpu, thread) = -1;
249 return evsel->fd != NULL ? 0 : -ENOMEM;
252 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
254 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
255 if (evsel->sample_id == NULL)
258 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
259 if (evsel->id == NULL) {
260 xyarray__delete(evsel->sample_id);
261 evsel->sample_id = NULL;
268 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
270 evsel->counts = zalloc((sizeof(*evsel->counts) +
271 (ncpus * sizeof(struct perf_counts_values))));
272 return evsel->counts != NULL ? 0 : -ENOMEM;
275 void perf_evsel__free_fd(struct perf_evsel *evsel)
277 xyarray__delete(evsel->fd);
281 void perf_evsel__free_id(struct perf_evsel *evsel)
283 xyarray__delete(evsel->sample_id);
284 evsel->sample_id = NULL;
289 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
293 for (cpu = 0; cpu < ncpus; cpu++)
294 for (thread = 0; thread < nthreads; ++thread) {
295 close(FD(evsel, cpu, thread));
296 FD(evsel, cpu, thread) = -1;
300 void perf_evsel__exit(struct perf_evsel *evsel)
302 assert(list_empty(&evsel->node));
303 xyarray__delete(evsel->fd);
304 xyarray__delete(evsel->sample_id);
308 void perf_evsel__delete(struct perf_evsel *evsel)
310 perf_evsel__exit(evsel);
311 close_cgroup(evsel->cgrp);
316 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
317 int cpu, int thread, bool scale)
319 struct perf_counts_values count;
320 size_t nv = scale ? 3 : 1;
322 if (FD(evsel, cpu, thread) < 0)
325 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
328 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
334 else if (count.run < count.ena)
335 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
337 count.ena = count.run = 0;
339 evsel->counts->cpu[cpu] = count;
343 int __perf_evsel__read(struct perf_evsel *evsel,
344 int ncpus, int nthreads, bool scale)
346 size_t nv = scale ? 3 : 1;
348 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
350 aggr->val = aggr->ena = aggr->run = 0;
352 for (cpu = 0; cpu < ncpus; cpu++) {
353 for (thread = 0; thread < nthreads; thread++) {
354 if (FD(evsel, cpu, thread) < 0)
357 if (readn(FD(evsel, cpu, thread),
358 &count, nv * sizeof(u64)) < 0)
361 aggr->val += count.val;
363 aggr->ena += count.ena;
364 aggr->run += count.run;
369 evsel->counts->scaled = 0;
371 if (aggr->run == 0) {
372 evsel->counts->scaled = -1;
377 if (aggr->run < aggr->ena) {
378 evsel->counts->scaled = 1;
379 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
382 aggr->ena = aggr->run = 0;
387 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
388 struct thread_map *threads, bool group,
389 struct xyarray *group_fds)
392 unsigned long flags = 0;
395 if (evsel->fd == NULL &&
396 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
400 flags = PERF_FLAG_PID_CGROUP;
401 pid = evsel->cgrp->fd;
404 for (cpu = 0; cpu < cpus->nr; cpu++) {
405 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
407 for (thread = 0; thread < threads->nr; thread++) {
410 pid = threads->map[thread];
412 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
416 if (FD(evsel, cpu, thread) < 0) {
421 if (group && group_fd == -1)
422 group_fd = FD(evsel, cpu, thread);
430 while (--thread >= 0) {
431 close(FD(evsel, cpu, thread));
432 FD(evsel, cpu, thread) = -1;
434 thread = threads->nr;
435 } while (--cpu >= 0);
439 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
441 if (evsel->fd == NULL)
444 perf_evsel__close_fd(evsel, ncpus, nthreads);
445 perf_evsel__free_fd(evsel);
458 struct thread_map map;
460 } empty_thread_map = {
465 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
466 struct thread_map *threads, bool group,
467 struct xyarray *group_fd)
470 /* Work around old compiler warnings about strict aliasing */
471 cpus = &empty_cpu_map.map;
475 threads = &empty_thread_map.map;
477 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
480 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
481 struct cpu_map *cpus, bool group,
482 struct xyarray *group_fd)
484 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
488 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
489 struct thread_map *threads, bool group,
490 struct xyarray *group_fd)
492 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
496 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
497 struct perf_sample *sample,
500 const u64 *array = event->sample.array;
503 array += ((event->header.size -
504 sizeof(event->header)) / sizeof(u64)) - 1;
506 if (type & PERF_SAMPLE_CPU) {
509 /* undo swap of u64, then swap on individual u32s */
510 u.val64 = bswap_64(u.val64);
511 u.val32[0] = bswap_32(u.val32[0]);
514 sample->cpu = u.val32[0];
518 if (type & PERF_SAMPLE_STREAM_ID) {
519 sample->stream_id = *array;
523 if (type & PERF_SAMPLE_ID) {
528 if (type & PERF_SAMPLE_TIME) {
529 sample->time = *array;
533 if (type & PERF_SAMPLE_TID) {
536 /* undo swap of u64, then swap on individual u32s */
537 u.val64 = bswap_64(u.val64);
538 u.val32[0] = bswap_32(u.val32[0]);
539 u.val32[1] = bswap_32(u.val32[1]);
542 sample->pid = u.val32[0];
543 sample->tid = u.val32[1];
549 static bool sample_overlap(const union perf_event *event,
550 const void *offset, u64 size)
552 const void *base = event;
554 if (offset + size > base + event->header.size)
560 int perf_event__parse_sample(const union perf_event *event, u64 type,
561 int sample_size, bool sample_id_all,
562 struct perf_sample *data, bool swapped)
567 * used for cross-endian analysis. See git commit 65014ab3
568 * for why this goofiness is needed.
572 memset(data, 0, sizeof(*data));
573 data->cpu = data->pid = data->tid = -1;
574 data->stream_id = data->id = data->time = -1ULL;
577 if (event->header.type != PERF_RECORD_SAMPLE) {
580 return perf_event__parse_id_sample(event, type, data, swapped);
583 array = event->sample.array;
585 if (sample_size + sizeof(event->header) > event->header.size)
588 if (type & PERF_SAMPLE_IP) {
589 data->ip = event->ip.ip;
593 if (type & PERF_SAMPLE_TID) {
596 /* undo swap of u64, then swap on individual u32s */
597 u.val64 = bswap_64(u.val64);
598 u.val32[0] = bswap_32(u.val32[0]);
599 u.val32[1] = bswap_32(u.val32[1]);
602 data->pid = u.val32[0];
603 data->tid = u.val32[1];
607 if (type & PERF_SAMPLE_TIME) {
613 if (type & PERF_SAMPLE_ADDR) {
619 if (type & PERF_SAMPLE_ID) {
624 if (type & PERF_SAMPLE_STREAM_ID) {
625 data->stream_id = *array;
629 if (type & PERF_SAMPLE_CPU) {
633 /* undo swap of u64, then swap on individual u32s */
634 u.val64 = bswap_64(u.val64);
635 u.val32[0] = bswap_32(u.val32[0]);
638 data->cpu = u.val32[0];
642 if (type & PERF_SAMPLE_PERIOD) {
643 data->period = *array;
647 if (type & PERF_SAMPLE_READ) {
648 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
652 if (type & PERF_SAMPLE_CALLCHAIN) {
653 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
656 data->callchain = (struct ip_callchain *)array;
658 if (sample_overlap(event, array, data->callchain->nr))
661 array += 1 + data->callchain->nr;
664 if (type & PERF_SAMPLE_RAW) {
668 if (WARN_ONCE(swapped,
669 "Endianness of raw data not corrected!\n")) {
670 /* undo swap of u64, then swap on individual u32s */
671 u.val64 = bswap_64(u.val64);
672 u.val32[0] = bswap_32(u.val32[0]);
673 u.val32[1] = bswap_32(u.val32[1]);
676 if (sample_overlap(event, array, sizeof(u32)))
679 data->raw_size = u.val32[0];
680 pdata = (void *) array + sizeof(u32);
682 if (sample_overlap(event, pdata, data->raw_size))
685 data->raw_data = (void *) pdata;
687 array = (void *)array + data->raw_size + sizeof(u32);
690 if (type & PERF_SAMPLE_BRANCH_STACK) {
693 data->branch_stack = (struct branch_stack *)array;
696 sz = data->branch_stack->nr * sizeof(struct branch_entry);
703 int perf_event__synthesize_sample(union perf_event *event, u64 type,
704 const struct perf_sample *sample,
710 * used for cross-endian analysis. See git commit 65014ab3
711 * for why this goofiness is needed.
715 array = event->sample.array;
717 if (type & PERF_SAMPLE_IP) {
718 event->ip.ip = sample->ip;
722 if (type & PERF_SAMPLE_TID) {
723 u.val32[0] = sample->pid;
724 u.val32[1] = sample->tid;
727 * Inverse of what is done in perf_event__parse_sample
729 u.val32[0] = bswap_32(u.val32[0]);
730 u.val32[1] = bswap_32(u.val32[1]);
731 u.val64 = bswap_64(u.val64);
738 if (type & PERF_SAMPLE_TIME) {
739 *array = sample->time;
743 if (type & PERF_SAMPLE_ADDR) {
744 *array = sample->addr;
748 if (type & PERF_SAMPLE_ID) {
753 if (type & PERF_SAMPLE_STREAM_ID) {
754 *array = sample->stream_id;
758 if (type & PERF_SAMPLE_CPU) {
759 u.val32[0] = sample->cpu;
762 * Inverse of what is done in perf_event__parse_sample
764 u.val32[0] = bswap_32(u.val32[0]);
765 u.val64 = bswap_64(u.val64);
771 if (type & PERF_SAMPLE_PERIOD) {
772 *array = sample->period;