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 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
19 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
21 int __perf_evsel__sample_size(u64 sample_type)
23 u64 mask = sample_type & PERF_SAMPLE_MASK;
27 for (i = 0; i < 64; i++) {
28 if (mask & (1ULL << i))
37 static void hists__init(struct hists *hists)
39 memset(hists, 0, sizeof(*hists));
40 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
41 hists->entries_in = &hists->entries_in_array[0];
42 hists->entries_collapsed = RB_ROOT;
43 hists->entries = RB_ROOT;
44 pthread_mutex_init(&hists->lock, NULL);
47 void perf_evsel__init(struct perf_evsel *evsel,
48 struct perf_event_attr *attr, int idx)
52 INIT_LIST_HEAD(&evsel->node);
53 hists__init(&evsel->hists);
56 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
58 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61 perf_evsel__init(evsel, attr, idx);
66 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts)
68 struct perf_event_attr *attr = &evsel->attr;
69 int track = !evsel->idx; /* only the first counter needs these */
71 attr->sample_id_all = opts->sample_id_all_avail ? 1 : 0;
72 attr->inherit = !opts->no_inherit;
73 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
74 PERF_FORMAT_TOTAL_TIME_RUNNING |
77 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
80 * We default some events to a 1 default interval. But keep
81 * it a weak assumption overridable by the user.
83 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
84 opts->user_interval != ULLONG_MAX)) {
86 attr->sample_type |= PERF_SAMPLE_PERIOD;
88 attr->sample_freq = opts->freq;
90 attr->sample_period = opts->default_interval;
95 attr->sample_freq = 0;
97 if (opts->inherit_stat)
98 attr->inherit_stat = 1;
100 if (opts->sample_address) {
101 attr->sample_type |= PERF_SAMPLE_ADDR;
102 attr->mmap_data = track;
105 if (opts->call_graph)
106 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
108 if (opts->system_wide)
109 attr->sample_type |= PERF_SAMPLE_CPU;
111 if (opts->sample_id_all_avail &&
112 (opts->sample_time || opts->system_wide ||
113 !opts->no_inherit || opts->cpu_list))
114 attr->sample_type |= PERF_SAMPLE_TIME;
116 if (opts->raw_samples) {
117 attr->sample_type |= PERF_SAMPLE_TIME;
118 attr->sample_type |= PERF_SAMPLE_RAW;
119 attr->sample_type |= PERF_SAMPLE_CPU;
122 if (opts->no_delay) {
124 attr->wakeup_events = 1;
130 if (opts->target_pid == -1 && opts->target_tid == -1 && !opts->system_wide) {
132 attr->enable_on_exec = 1;
136 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
139 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
142 for (cpu = 0; cpu < ncpus; cpu++) {
143 for (thread = 0; thread < nthreads; thread++) {
144 FD(evsel, cpu, thread) = -1;
149 return evsel->fd != NULL ? 0 : -ENOMEM;
152 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
154 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
155 if (evsel->sample_id == NULL)
158 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
159 if (evsel->id == NULL) {
160 xyarray__delete(evsel->sample_id);
161 evsel->sample_id = NULL;
168 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
170 evsel->counts = zalloc((sizeof(*evsel->counts) +
171 (ncpus * sizeof(struct perf_counts_values))));
172 return evsel->counts != NULL ? 0 : -ENOMEM;
175 void perf_evsel__free_fd(struct perf_evsel *evsel)
177 xyarray__delete(evsel->fd);
181 void perf_evsel__free_id(struct perf_evsel *evsel)
183 xyarray__delete(evsel->sample_id);
184 evsel->sample_id = NULL;
189 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
193 for (cpu = 0; cpu < ncpus; cpu++)
194 for (thread = 0; thread < nthreads; ++thread) {
195 close(FD(evsel, cpu, thread));
196 FD(evsel, cpu, thread) = -1;
200 void perf_evsel__exit(struct perf_evsel *evsel)
202 assert(list_empty(&evsel->node));
203 xyarray__delete(evsel->fd);
204 xyarray__delete(evsel->sample_id);
208 void perf_evsel__delete(struct perf_evsel *evsel)
210 perf_evsel__exit(evsel);
211 close_cgroup(evsel->cgrp);
216 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
217 int cpu, int thread, bool scale)
219 struct perf_counts_values count;
220 size_t nv = scale ? 3 : 1;
222 if (FD(evsel, cpu, thread) < 0)
225 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
228 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
234 else if (count.run < count.ena)
235 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
237 count.ena = count.run = 0;
239 evsel->counts->cpu[cpu] = count;
243 int __perf_evsel__read(struct perf_evsel *evsel,
244 int ncpus, int nthreads, bool scale)
246 size_t nv = scale ? 3 : 1;
248 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
250 aggr->val = aggr->ena = aggr->run = 0;
252 for (cpu = 0; cpu < ncpus; cpu++) {
253 for (thread = 0; thread < nthreads; thread++) {
254 if (FD(evsel, cpu, thread) < 0)
257 if (readn(FD(evsel, cpu, thread),
258 &count, nv * sizeof(u64)) < 0)
261 aggr->val += count.val;
263 aggr->ena += count.ena;
264 aggr->run += count.run;
269 evsel->counts->scaled = 0;
271 if (aggr->run == 0) {
272 evsel->counts->scaled = -1;
277 if (aggr->run < aggr->ena) {
278 evsel->counts->scaled = 1;
279 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
282 aggr->ena = aggr->run = 0;
287 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
288 struct thread_map *threads, bool group,
289 struct xyarray *group_fds)
292 unsigned long flags = 0;
295 if (evsel->fd == NULL &&
296 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
300 flags = PERF_FLAG_PID_CGROUP;
301 pid = evsel->cgrp->fd;
304 for (cpu = 0; cpu < cpus->nr; cpu++) {
305 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
307 for (thread = 0; thread < threads->nr; thread++) {
310 pid = threads->map[thread];
312 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
316 if (FD(evsel, cpu, thread) < 0) {
321 if (group && group_fd == -1)
322 group_fd = FD(evsel, cpu, thread);
330 while (--thread >= 0) {
331 close(FD(evsel, cpu, thread));
332 FD(evsel, cpu, thread) = -1;
334 thread = threads->nr;
335 } while (--cpu >= 0);
339 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
341 if (evsel->fd == NULL)
344 perf_evsel__close_fd(evsel, ncpus, nthreads);
345 perf_evsel__free_fd(evsel);
358 struct thread_map map;
360 } empty_thread_map = {
365 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
366 struct thread_map *threads, bool group,
367 struct xyarray *group_fd)
370 /* Work around old compiler warnings about strict aliasing */
371 cpus = &empty_cpu_map.map;
375 threads = &empty_thread_map.map;
377 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
380 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
381 struct cpu_map *cpus, bool group,
382 struct xyarray *group_fd)
384 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
388 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
389 struct thread_map *threads, bool group,
390 struct xyarray *group_fd)
392 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
396 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
397 struct perf_sample *sample)
399 const u64 *array = event->sample.array;
401 array += ((event->header.size -
402 sizeof(event->header)) / sizeof(u64)) - 1;
404 if (type & PERF_SAMPLE_CPU) {
405 u32 *p = (u32 *)array;
410 if (type & PERF_SAMPLE_STREAM_ID) {
411 sample->stream_id = *array;
415 if (type & PERF_SAMPLE_ID) {
420 if (type & PERF_SAMPLE_TIME) {
421 sample->time = *array;
425 if (type & PERF_SAMPLE_TID) {
426 u32 *p = (u32 *)array;
434 static bool sample_overlap(const union perf_event *event,
435 const void *offset, u64 size)
437 const void *base = event;
439 if (offset + size > base + event->header.size)
445 int perf_event__parse_sample(const union perf_event *event, u64 type,
446 int sample_size, bool sample_id_all,
447 struct perf_sample *data, bool swapped)
452 * used for cross-endian analysis. See git commit 65014ab3
453 * for why this goofiness is needed.
461 data->cpu = data->pid = data->tid = -1;
462 data->stream_id = data->id = data->time = -1ULL;
464 if (event->header.type != PERF_RECORD_SAMPLE) {
467 return perf_event__parse_id_sample(event, type, data);
470 array = event->sample.array;
472 if (sample_size + sizeof(event->header) > event->header.size)
475 if (type & PERF_SAMPLE_IP) {
476 data->ip = event->ip.ip;
480 if (type & PERF_SAMPLE_TID) {
483 /* undo swap of u64, then swap on individual u32s */
484 u.val64 = bswap_64(u.val64);
485 u.val32[0] = bswap_32(u.val32[0]);
486 u.val32[1] = bswap_32(u.val32[1]);
489 data->pid = u.val32[0];
490 data->tid = u.val32[1];
494 if (type & PERF_SAMPLE_TIME) {
500 if (type & PERF_SAMPLE_ADDR) {
506 if (type & PERF_SAMPLE_ID) {
511 if (type & PERF_SAMPLE_STREAM_ID) {
512 data->stream_id = *array;
516 if (type & PERF_SAMPLE_CPU) {
520 /* undo swap of u64, then swap on individual u32s */
521 u.val64 = bswap_64(u.val64);
522 u.val32[0] = bswap_32(u.val32[0]);
525 data->cpu = u.val32[0];
529 if (type & PERF_SAMPLE_PERIOD) {
530 data->period = *array;
534 if (type & PERF_SAMPLE_READ) {
535 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
539 if (type & PERF_SAMPLE_CALLCHAIN) {
540 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
543 data->callchain = (struct ip_callchain *)array;
545 if (sample_overlap(event, array, data->callchain->nr))
548 array += 1 + data->callchain->nr;
551 if (type & PERF_SAMPLE_RAW) {
555 if (WARN_ONCE(swapped,
556 "Endianness of raw data not corrected!\n")) {
557 /* undo swap of u64, then swap on individual u32s */
558 u.val64 = bswap_64(u.val64);
559 u.val32[0] = bswap_32(u.val32[0]);
560 u.val32[1] = bswap_32(u.val32[1]);
563 if (sample_overlap(event, array, sizeof(u32)))
566 data->raw_size = u.val32[0];
567 pdata = (void *) array + sizeof(u32);
569 if (sample_overlap(event, pdata, data->raw_size))
572 data->raw_data = (void *) pdata;