blob: dab893804a147bb778b222ee0bf1550fa2346b12 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
11#include "asm/bug.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020012#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020013#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020014#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020015#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020016#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090017#include "target.h"
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030018#include "../../include/linux/perf_event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020019
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020020#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -020021#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020022
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030023int __perf_evsel__sample_size(u64 sample_type)
24{
25 u64 mask = sample_type & PERF_SAMPLE_MASK;
26 int size = 0;
27 int i;
28
29 for (i = 0; i < 64; i++) {
30 if (mask & (1ULL << i))
31 size++;
32 }
33
34 size *= sizeof(u64);
35
36 return size;
37}
38
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010039void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020040{
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);
47}
48
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020049void perf_evsel__init(struct perf_evsel *evsel,
50 struct perf_event_attr *attr, int idx)
51{
52 evsel->idx = idx;
53 evsel->attr = *attr;
54 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030055 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020056}
57
Lin Ming23a2f3a2011-01-07 11:11:09 +080058struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020059{
60 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020062 if (evsel != NULL)
63 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020064
65 return evsel;
66}
67
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030068static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
69 "cycles",
70 "instructions",
71 "cache-references",
72 "cache-misses",
73 "branches",
74 "branch-misses",
75 "bus-cycles",
76 "stalled-cycles-frontend",
77 "stalled-cycles-backend",
78 "ref-cycles",
79};
80
81const char *__perf_evsel__hw_name(u64 config)
82{
83 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84 return perf_evsel__hw_names[config];
85
86 return "unknown-hardware";
87}
88
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030089static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030090{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030091 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030092 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030093 bool exclude_guest_default = false;
94
95#define MOD_PRINT(context, mod) do { \
96 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030097 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030098 r += scnprintf(bf + r, size - r, "%c", mod); \
99 } } while(0)
100
101 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
102 MOD_PRINT(kernel, 'k');
103 MOD_PRINT(user, 'u');
104 MOD_PRINT(hv, 'h');
105 exclude_guest_default = true;
106 }
107
108 if (attr->precise_ip) {
109 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300110 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300111 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
112 exclude_guest_default = true;
113 }
114
115 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
116 MOD_PRINT(host, 'H');
117 MOD_PRINT(guest, 'G');
118 }
119#undef MOD_PRINT
120 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300121 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300122 return r;
123}
124
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300125static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
126{
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);
129}
130
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300131int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
132{
133 int ret;
134
135 switch (evsel->attr.type) {
136 case PERF_TYPE_RAW:
137 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
138 break;
139
140 case PERF_TYPE_HARDWARE:
141 ret = perf_evsel__hw_name(evsel, bf, size);
142 break;
143 default:
144 /*
145 * FIXME
146 *
147 * This is the minimal perf_evsel__name so that we can
148 * reconstruct event names taking into account event modifiers.
149 *
150 * The old event_name uses it now for raw anr hw events, so that
151 * we don't drag all the parsing stuff into the python binding.
152 *
153 * On the next devel cycle the rest of the event naming will be
154 * brought here.
155 */
156 return 0;
157 }
158
159 return ret;
160}
161
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900162void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
163 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200164{
165 struct perf_event_attr *attr = &evsel->attr;
166 int track = !evsel->idx; /* only the first counter needs these */
167
David Ahern5e1c81d2012-05-13 22:01:28 -0600168 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200169 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200170 attr->inherit = !opts->no_inherit;
171 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
172 PERF_FORMAT_TOTAL_TIME_RUNNING |
173 PERF_FORMAT_ID;
174
175 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
176
177 /*
178 * We default some events to a 1 default interval. But keep
179 * it a weak assumption overridable by the user.
180 */
181 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
182 opts->user_interval != ULLONG_MAX)) {
183 if (opts->freq) {
184 attr->sample_type |= PERF_SAMPLE_PERIOD;
185 attr->freq = 1;
186 attr->sample_freq = opts->freq;
187 } else {
188 attr->sample_period = opts->default_interval;
189 }
190 }
191
192 if (opts->no_samples)
193 attr->sample_freq = 0;
194
195 if (opts->inherit_stat)
196 attr->inherit_stat = 1;
197
198 if (opts->sample_address) {
199 attr->sample_type |= PERF_SAMPLE_ADDR;
200 attr->mmap_data = track;
201 }
202
203 if (opts->call_graph)
204 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
205
Namhyung Kime40ee742012-05-21 10:42:07 +0900206 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200207 attr->sample_type |= PERF_SAMPLE_CPU;
208
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300209 if (opts->period)
210 attr->sample_type |= PERF_SAMPLE_PERIOD;
211
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200212 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900213 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900214 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200215 attr->sample_type |= PERF_SAMPLE_TIME;
216
217 if (opts->raw_samples) {
218 attr->sample_type |= PERF_SAMPLE_TIME;
219 attr->sample_type |= PERF_SAMPLE_RAW;
220 attr->sample_type |= PERF_SAMPLE_CPU;
221 }
222
223 if (opts->no_delay) {
224 attr->watermark = 0;
225 attr->wakeup_events = 1;
226 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100227 if (opts->branch_stack) {
228 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
229 attr->branch_sample_type = opts->branch_stack;
230 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200231
232 attr->mmap = track;
233 attr->comm = track;
234
Namhyung Kimd67356e2012-05-07 14:09:03 +0900235 if (perf_target__none(&opts->target) &&
236 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200237 attr->enable_on_exec = 1;
238 }
239}
240
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200241int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
242{
David Ahern4af4c952011-05-27 09:58:34 -0600243 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200244 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600245
246 if (evsel->fd) {
247 for (cpu = 0; cpu < ncpus; cpu++) {
248 for (thread = 0; thread < nthreads; thread++) {
249 FD(evsel, cpu, thread) = -1;
250 }
251 }
252 }
253
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200254 return evsel->fd != NULL ? 0 : -ENOMEM;
255}
256
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200257int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
258{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300259 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
260 if (evsel->sample_id == NULL)
261 return -ENOMEM;
262
263 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
264 if (evsel->id == NULL) {
265 xyarray__delete(evsel->sample_id);
266 evsel->sample_id = NULL;
267 return -ENOMEM;
268 }
269
270 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200271}
272
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200273int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
274{
275 evsel->counts = zalloc((sizeof(*evsel->counts) +
276 (ncpus * sizeof(struct perf_counts_values))));
277 return evsel->counts != NULL ? 0 : -ENOMEM;
278}
279
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200280void perf_evsel__free_fd(struct perf_evsel *evsel)
281{
282 xyarray__delete(evsel->fd);
283 evsel->fd = NULL;
284}
285
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200286void perf_evsel__free_id(struct perf_evsel *evsel)
287{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300288 xyarray__delete(evsel->sample_id);
289 evsel->sample_id = NULL;
290 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200291 evsel->id = NULL;
292}
293
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200294void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
295{
296 int cpu, thread;
297
298 for (cpu = 0; cpu < ncpus; cpu++)
299 for (thread = 0; thread < nthreads; ++thread) {
300 close(FD(evsel, cpu, thread));
301 FD(evsel, cpu, thread) = -1;
302 }
303}
304
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200305void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200306{
307 assert(list_empty(&evsel->node));
308 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300309 xyarray__delete(evsel->sample_id);
310 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200311}
312
313void perf_evsel__delete(struct perf_evsel *evsel)
314{
315 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200316 close_cgroup(evsel->cgrp);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200317 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200318 free(evsel);
319}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200320
321int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
322 int cpu, int thread, bool scale)
323{
324 struct perf_counts_values count;
325 size_t nv = scale ? 3 : 1;
326
327 if (FD(evsel, cpu, thread) < 0)
328 return -EINVAL;
329
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200330 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
331 return -ENOMEM;
332
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200333 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
334 return -errno;
335
336 if (scale) {
337 if (count.run == 0)
338 count.val = 0;
339 else if (count.run < count.ena)
340 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
341 } else
342 count.ena = count.run = 0;
343
344 evsel->counts->cpu[cpu] = count;
345 return 0;
346}
347
348int __perf_evsel__read(struct perf_evsel *evsel,
349 int ncpus, int nthreads, bool scale)
350{
351 size_t nv = scale ? 3 : 1;
352 int cpu, thread;
353 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
354
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200355 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200356
357 for (cpu = 0; cpu < ncpus; cpu++) {
358 for (thread = 0; thread < nthreads; thread++) {
359 if (FD(evsel, cpu, thread) < 0)
360 continue;
361
362 if (readn(FD(evsel, cpu, thread),
363 &count, nv * sizeof(u64)) < 0)
364 return -errno;
365
366 aggr->val += count.val;
367 if (scale) {
368 aggr->ena += count.ena;
369 aggr->run += count.run;
370 }
371 }
372 }
373
374 evsel->counts->scaled = 0;
375 if (scale) {
376 if (aggr->run == 0) {
377 evsel->counts->scaled = -1;
378 aggr->val = 0;
379 return 0;
380 }
381
382 if (aggr->run < aggr->ena) {
383 evsel->counts->scaled = 1;
384 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
385 }
386 } else
387 aggr->ena = aggr->run = 0;
388
389 return 0;
390}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200391
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200392static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200393 struct thread_map *threads, bool group,
394 struct xyarray *group_fds)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200395{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200396 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200397 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200398 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200399
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200400 if (evsel->fd == NULL &&
401 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200402 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200403
Stephane Eranian023695d2011-02-14 11:20:01 +0200404 if (evsel->cgrp) {
405 flags = PERF_FLAG_PID_CGROUP;
406 pid = evsel->cgrp->fd;
407 }
408
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200409 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200410 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200411
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200412 for (thread = 0; thread < threads->nr; thread++) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200413
414 if (!evsel->cgrp)
415 pid = threads->map[thread];
416
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200417 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200418 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200419 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200420 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200421 if (FD(evsel, cpu, thread) < 0) {
422 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200423 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200424 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200425
426 if (group && group_fd == -1)
427 group_fd = FD(evsel, cpu, thread);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200428 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200429 }
430
431 return 0;
432
433out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200434 do {
435 while (--thread >= 0) {
436 close(FD(evsel, cpu, thread));
437 FD(evsel, cpu, thread) = -1;
438 }
439 thread = threads->nr;
440 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200441 return err;
442}
443
444void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
445{
446 if (evsel->fd == NULL)
447 return;
448
449 perf_evsel__close_fd(evsel, ncpus, nthreads);
450 perf_evsel__free_fd(evsel);
451 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200452}
453
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200454static struct {
455 struct cpu_map map;
456 int cpus[1];
457} empty_cpu_map = {
458 .map.nr = 1,
459 .cpus = { -1, },
460};
461
462static struct {
463 struct thread_map map;
464 int threads[1];
465} empty_thread_map = {
466 .map.nr = 1,
467 .threads = { -1, },
468};
469
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200470int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200471 struct thread_map *threads, bool group,
472 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200473{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200474 if (cpus == NULL) {
475 /* Work around old compiler warnings about strict aliasing */
476 cpus = &empty_cpu_map.map;
477 }
478
479 if (threads == NULL)
480 threads = &empty_thread_map.map;
481
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200482 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200483}
484
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200485int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200486 struct cpu_map *cpus, bool group,
487 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200488{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200489 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
490 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200491}
492
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200493int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200494 struct thread_map *threads, bool group,
495 struct xyarray *group_fd)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200496{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200497 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
498 group_fd);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200499}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200500
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200501static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200502 struct perf_sample *sample,
503 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200504{
505 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200506 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200507
508 array += ((event->header.size -
509 sizeof(event->header)) / sizeof(u64)) - 1;
510
511 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200512 u.val64 = *array;
513 if (swapped) {
514 /* undo swap of u64, then swap on individual u32s */
515 u.val64 = bswap_64(u.val64);
516 u.val32[0] = bswap_32(u.val32[0]);
517 }
518
519 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200520 array--;
521 }
522
523 if (type & PERF_SAMPLE_STREAM_ID) {
524 sample->stream_id = *array;
525 array--;
526 }
527
528 if (type & PERF_SAMPLE_ID) {
529 sample->id = *array;
530 array--;
531 }
532
533 if (type & PERF_SAMPLE_TIME) {
534 sample->time = *array;
535 array--;
536 }
537
538 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200539 u.val64 = *array;
540 if (swapped) {
541 /* undo swap of u64, then swap on individual u32s */
542 u.val64 = bswap_64(u.val64);
543 u.val32[0] = bswap_32(u.val32[0]);
544 u.val32[1] = bswap_32(u.val32[1]);
545 }
546
547 sample->pid = u.val32[0];
548 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200549 }
550
551 return 0;
552}
553
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200554static bool sample_overlap(const union perf_event *event,
555 const void *offset, u64 size)
556{
557 const void *base = event;
558
559 if (offset + size > base + event->header.size)
560 return true;
561
562 return false;
563}
564
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200565int perf_event__parse_sample(const union perf_event *event, u64 type,
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200566 int sample_size, bool sample_id_all,
David Ahern936be502011-09-06 09:12:26 -0600567 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200568{
569 const u64 *array;
570
David Ahern936be502011-09-06 09:12:26 -0600571 /*
572 * used for cross-endian analysis. See git commit 65014ab3
573 * for why this goofiness is needed.
574 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200575 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600576
Robert Richterf3bda2c2011-12-15 17:32:39 +0100577 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200578 data->cpu = data->pid = data->tid = -1;
579 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530580 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200581
582 if (event->header.type != PERF_RECORD_SAMPLE) {
583 if (!sample_id_all)
584 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200585 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200586 }
587
588 array = event->sample.array;
589
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200590 if (sample_size + sizeof(event->header) > event->header.size)
591 return -EFAULT;
592
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200593 if (type & PERF_SAMPLE_IP) {
594 data->ip = event->ip.ip;
595 array++;
596 }
597
598 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600599 u.val64 = *array;
600 if (swapped) {
601 /* undo swap of u64, then swap on individual u32s */
602 u.val64 = bswap_64(u.val64);
603 u.val32[0] = bswap_32(u.val32[0]);
604 u.val32[1] = bswap_32(u.val32[1]);
605 }
606
607 data->pid = u.val32[0];
608 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200609 array++;
610 }
611
612 if (type & PERF_SAMPLE_TIME) {
613 data->time = *array;
614 array++;
615 }
616
David Ahern7cec0922011-05-30 13:08:23 -0600617 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200618 if (type & PERF_SAMPLE_ADDR) {
619 data->addr = *array;
620 array++;
621 }
622
623 data->id = -1ULL;
624 if (type & PERF_SAMPLE_ID) {
625 data->id = *array;
626 array++;
627 }
628
629 if (type & PERF_SAMPLE_STREAM_ID) {
630 data->stream_id = *array;
631 array++;
632 }
633
634 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600635
636 u.val64 = *array;
637 if (swapped) {
638 /* undo swap of u64, then swap on individual u32s */
639 u.val64 = bswap_64(u.val64);
640 u.val32[0] = bswap_32(u.val32[0]);
641 }
642
643 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200644 array++;
645 }
646
647 if (type & PERF_SAMPLE_PERIOD) {
648 data->period = *array;
649 array++;
650 }
651
652 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100653 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200654 return -1;
655 }
656
657 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200658 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
659 return -EFAULT;
660
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200661 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200662
663 if (sample_overlap(event, array, data->callchain->nr))
664 return -EFAULT;
665
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200666 array += 1 + data->callchain->nr;
667 }
668
669 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200670 const u64 *pdata;
671
David Ahern936be502011-09-06 09:12:26 -0600672 u.val64 = *array;
673 if (WARN_ONCE(swapped,
674 "Endianness of raw data not corrected!\n")) {
675 /* undo swap of u64, then swap on individual u32s */
676 u.val64 = bswap_64(u.val64);
677 u.val32[0] = bswap_32(u.val32[0]);
678 u.val32[1] = bswap_32(u.val32[1]);
679 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200680
681 if (sample_overlap(event, array, sizeof(u32)))
682 return -EFAULT;
683
David Ahern936be502011-09-06 09:12:26 -0600684 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200685 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200686
Jiri Olsa8e303f22011-09-29 17:05:08 +0200687 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200688 return -EFAULT;
689
Jiri Olsa8e303f22011-09-29 17:05:08 +0200690 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100691
692 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200693 }
694
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100695 if (type & PERF_SAMPLE_BRANCH_STACK) {
696 u64 sz;
697
698 data->branch_stack = (struct branch_stack *)array;
699 array++; /* nr */
700
701 sz = data->branch_stack->nr * sizeof(struct branch_entry);
702 sz /= sizeof(u64);
703 array += sz;
704 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200705 return 0;
706}
Andrew Vagin74eec262011-11-28 12:03:31 +0300707
708int perf_event__synthesize_sample(union perf_event *event, u64 type,
709 const struct perf_sample *sample,
710 bool swapped)
711{
712 u64 *array;
713
714 /*
715 * used for cross-endian analysis. See git commit 65014ab3
716 * for why this goofiness is needed.
717 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200718 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300719
720 array = event->sample.array;
721
722 if (type & PERF_SAMPLE_IP) {
723 event->ip.ip = sample->ip;
724 array++;
725 }
726
727 if (type & PERF_SAMPLE_TID) {
728 u.val32[0] = sample->pid;
729 u.val32[1] = sample->tid;
730 if (swapped) {
731 /*
732 * Inverse of what is done in perf_event__parse_sample
733 */
734 u.val32[0] = bswap_32(u.val32[0]);
735 u.val32[1] = bswap_32(u.val32[1]);
736 u.val64 = bswap_64(u.val64);
737 }
738
739 *array = u.val64;
740 array++;
741 }
742
743 if (type & PERF_SAMPLE_TIME) {
744 *array = sample->time;
745 array++;
746 }
747
748 if (type & PERF_SAMPLE_ADDR) {
749 *array = sample->addr;
750 array++;
751 }
752
753 if (type & PERF_SAMPLE_ID) {
754 *array = sample->id;
755 array++;
756 }
757
758 if (type & PERF_SAMPLE_STREAM_ID) {
759 *array = sample->stream_id;
760 array++;
761 }
762
763 if (type & PERF_SAMPLE_CPU) {
764 u.val32[0] = sample->cpu;
765 if (swapped) {
766 /*
767 * Inverse of what is done in perf_event__parse_sample
768 */
769 u.val32[0] = bswap_32(u.val32[0]);
770 u.val64 = bswap_64(u.val64);
771 }
772 *array = u.val64;
773 array++;
774 }
775
776 if (type & PERF_SAMPLE_PERIOD) {
777 *array = sample->period;
778 array++;
779 }
780
781 return 0;
782}