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 void perf_evsel__init(struct perf_evsel *evsel,
38 struct perf_event_attr *attr, int idx)
42 INIT_LIST_HEAD(&evsel->node);
43 hists__init(&evsel->hists);
46 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
48 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
51 perf_evsel__init(evsel, attr, idx);
56 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts)
58 struct perf_event_attr *attr = &evsel->attr;
59 int track = !evsel->idx; /* only the first counter needs these */
61 attr->sample_id_all = opts->sample_id_all_avail ? 1 : 0;
62 attr->inherit = !opts->no_inherit;
63 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
64 PERF_FORMAT_TOTAL_TIME_RUNNING |
67 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
70 * We default some events to a 1 default interval. But keep
71 * it a weak assumption overridable by the user.
73 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
74 opts->user_interval != ULLONG_MAX)) {
76 attr->sample_type |= PERF_SAMPLE_PERIOD;
78 attr->sample_freq = opts->freq;
80 attr->sample_period = opts->default_interval;
85 attr->sample_freq = 0;
87 if (opts->inherit_stat)
88 attr->inherit_stat = 1;
90 if (opts->sample_address) {
91 attr->sample_type |= PERF_SAMPLE_ADDR;
92 attr->mmap_data = track;
96 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
98 if (opts->system_wide)
99 attr->sample_type |= PERF_SAMPLE_CPU;
101 if (opts->sample_id_all_avail &&
102 (opts->sample_time || opts->system_wide ||
103 !opts->no_inherit || opts->cpu_list))
104 attr->sample_type |= PERF_SAMPLE_TIME;
106 if (opts->raw_samples) {
107 attr->sample_type |= PERF_SAMPLE_TIME;
108 attr->sample_type |= PERF_SAMPLE_RAW;
109 attr->sample_type |= PERF_SAMPLE_CPU;
112 if (opts->no_delay) {
114 attr->wakeup_events = 1;
120 if (opts->target_pid == -1 && opts->target_tid == -1 && !opts->system_wide) {
122 attr->enable_on_exec = 1;
126 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
129 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
132 for (cpu = 0; cpu < ncpus; cpu++) {
133 for (thread = 0; thread < nthreads; thread++) {
134 FD(evsel, cpu, thread) = -1;
139 return evsel->fd != NULL ? 0 : -ENOMEM;
142 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
144 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
145 if (evsel->sample_id == NULL)
148 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
149 if (evsel->id == NULL) {
150 xyarray__delete(evsel->sample_id);
151 evsel->sample_id = NULL;
158 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
160 evsel->counts = zalloc((sizeof(*evsel->counts) +
161 (ncpus * sizeof(struct perf_counts_values))));
162 return evsel->counts != NULL ? 0 : -ENOMEM;
165 void perf_evsel__free_fd(struct perf_evsel *evsel)
167 xyarray__delete(evsel->fd);
171 void perf_evsel__free_id(struct perf_evsel *evsel)
173 xyarray__delete(evsel->sample_id);
174 evsel->sample_id = NULL;
179 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
183 for (cpu = 0; cpu < ncpus; cpu++)
184 for (thread = 0; thread < nthreads; ++thread) {
185 close(FD(evsel, cpu, thread));
186 FD(evsel, cpu, thread) = -1;
190 void perf_evsel__exit(struct perf_evsel *evsel)
192 assert(list_empty(&evsel->node));
193 xyarray__delete(evsel->fd);
194 xyarray__delete(evsel->sample_id);
198 void perf_evsel__delete(struct perf_evsel *evsel)
200 perf_evsel__exit(evsel);
201 close_cgroup(evsel->cgrp);
206 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
207 int cpu, int thread, bool scale)
209 struct perf_counts_values count;
210 size_t nv = scale ? 3 : 1;
212 if (FD(evsel, cpu, thread) < 0)
215 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
218 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
224 else if (count.run < count.ena)
225 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
227 count.ena = count.run = 0;
229 evsel->counts->cpu[cpu] = count;
233 int __perf_evsel__read(struct perf_evsel *evsel,
234 int ncpus, int nthreads, bool scale)
236 size_t nv = scale ? 3 : 1;
238 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
240 aggr->val = aggr->ena = aggr->run = 0;
242 for (cpu = 0; cpu < ncpus; cpu++) {
243 for (thread = 0; thread < nthreads; thread++) {
244 if (FD(evsel, cpu, thread) < 0)
247 if (readn(FD(evsel, cpu, thread),
248 &count, nv * sizeof(u64)) < 0)
251 aggr->val += count.val;
253 aggr->ena += count.ena;
254 aggr->run += count.run;
259 evsel->counts->scaled = 0;
261 if (aggr->run == 0) {
262 evsel->counts->scaled = -1;
267 if (aggr->run < aggr->ena) {
268 evsel->counts->scaled = 1;
269 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
272 aggr->ena = aggr->run = 0;
277 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
278 struct thread_map *threads, bool group,
279 struct xyarray *group_fds)
282 unsigned long flags = 0;
285 if (evsel->fd == NULL &&
286 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
290 flags = PERF_FLAG_PID_CGROUP;
291 pid = evsel->cgrp->fd;
294 for (cpu = 0; cpu < cpus->nr; cpu++) {
295 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
297 for (thread = 0; thread < threads->nr; thread++) {
300 pid = threads->map[thread];
302 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
306 if (FD(evsel, cpu, thread) < 0) {
311 if (group && group_fd == -1)
312 group_fd = FD(evsel, cpu, thread);
320 while (--thread >= 0) {
321 close(FD(evsel, cpu, thread));
322 FD(evsel, cpu, thread) = -1;
324 thread = threads->nr;
325 } while (--cpu >= 0);
329 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
331 if (evsel->fd == NULL)
334 perf_evsel__close_fd(evsel, ncpus, nthreads);
335 perf_evsel__free_fd(evsel);
348 struct thread_map map;
350 } empty_thread_map = {
355 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
356 struct thread_map *threads, bool group,
357 struct xyarray *group_fd)
360 /* Work around old compiler warnings about strict aliasing */
361 cpus = &empty_cpu_map.map;
365 threads = &empty_thread_map.map;
367 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
370 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
371 struct cpu_map *cpus, bool group,
372 struct xyarray *group_fd)
374 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
378 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
379 struct thread_map *threads, bool group,
380 struct xyarray *group_fd)
382 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
386 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
387 struct perf_sample *sample)
389 const u64 *array = event->sample.array;
391 array += ((event->header.size -
392 sizeof(event->header)) / sizeof(u64)) - 1;
394 if (type & PERF_SAMPLE_CPU) {
395 u32 *p = (u32 *)array;
400 if (type & PERF_SAMPLE_STREAM_ID) {
401 sample->stream_id = *array;
405 if (type & PERF_SAMPLE_ID) {
410 if (type & PERF_SAMPLE_TIME) {
411 sample->time = *array;
415 if (type & PERF_SAMPLE_TID) {
416 u32 *p = (u32 *)array;
424 static bool sample_overlap(const union perf_event *event,
425 const void *offset, u64 size)
427 const void *base = event;
429 if (offset + size > base + event->header.size)
435 int perf_event__parse_sample(const union perf_event *event, u64 type,
436 int sample_size, bool sample_id_all,
437 struct perf_sample *data, bool swapped)
442 * used for cross-endian analysis. See git commit 65014ab3
443 * for why this goofiness is needed.
451 data->cpu = data->pid = data->tid = -1;
452 data->stream_id = data->id = data->time = -1ULL;
454 if (event->header.type != PERF_RECORD_SAMPLE) {
457 return perf_event__parse_id_sample(event, type, data);
460 array = event->sample.array;
462 if (sample_size + sizeof(event->header) > event->header.size)
465 if (type & PERF_SAMPLE_IP) {
466 data->ip = event->ip.ip;
470 if (type & PERF_SAMPLE_TID) {
473 /* undo swap of u64, then swap on individual u32s */
474 u.val64 = bswap_64(u.val64);
475 u.val32[0] = bswap_32(u.val32[0]);
476 u.val32[1] = bswap_32(u.val32[1]);
479 data->pid = u.val32[0];
480 data->tid = u.val32[1];
484 if (type & PERF_SAMPLE_TIME) {
490 if (type & PERF_SAMPLE_ADDR) {
496 if (type & PERF_SAMPLE_ID) {
501 if (type & PERF_SAMPLE_STREAM_ID) {
502 data->stream_id = *array;
506 if (type & PERF_SAMPLE_CPU) {
510 /* undo swap of u64, then swap on individual u32s */
511 u.val64 = bswap_64(u.val64);
512 u.val32[0] = bswap_32(u.val32[0]);
515 data->cpu = u.val32[0];
519 if (type & PERF_SAMPLE_PERIOD) {
520 data->period = *array;
524 if (type & PERF_SAMPLE_READ) {
525 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
529 if (type & PERF_SAMPLE_CALLCHAIN) {
530 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
533 data->callchain = (struct ip_callchain *)array;
535 if (sample_overlap(event, array, data->callchain->nr))
538 array += 1 + data->callchain->nr;
541 if (type & PERF_SAMPLE_RAW) {
545 if (WARN_ONCE(swapped,
546 "Endianness of raw data not corrected!\n")) {
547 /* undo swap of u64, then swap on individual u32s */
548 u.val64 = bswap_64(u.val64);
549 u.val32[0] = bswap_32(u.val32[0]);
550 u.val32[1] = bswap_32(u.val32[1]);
553 if (sample_overlap(event, array, sizeof(u32)))
556 data->raw_size = u.val32[0];
557 pdata = (void *) array + sizeof(u32);
559 if (sample_overlap(event, pdata, data->raw_size))
562 data->raw_data = (void *) pdata;