]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/util/evsel.c
perf tools: Fix hw breakpoint's type modifier parsing
[mirror_ubuntu-zesty-kernel.git] / tools / perf / util / evsel.c
CommitLineData
f8a95309
ACM
1/*
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
936be503
DA
10#include <byteswap.h>
11#include "asm/bug.h"
69aad6f1 12#include "evsel.h"
70082dd9 13#include "evlist.h"
69aad6f1 14#include "util.h"
86bd5e86 15#include "cpumap.h"
fd78260b 16#include "thread_map.h"
12864b31 17#include "target.h"
69aad6f1 18
c52b12ed 19#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
727ab04e 20#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
c52b12ed 21
c2a70653
ACM
22int __perf_evsel__sample_size(u64 sample_type)
23{
24 u64 mask = sample_type & PERF_SAMPLE_MASK;
25 int size = 0;
26 int i;
27
28 for (i = 0; i < 64; i++) {
29 if (mask & (1ULL << i))
30 size++;
31 }
32
33 size *= sizeof(u64);
34
35 return size;
36}
37
4bf9ce1b 38void hists__init(struct hists *hists)
0e2a5f10
ACM
39{
40 memset(hists, 0, sizeof(*hists));
41 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
42 hists->entries_in = &hists->entries_in_array[0];
43 hists->entries_collapsed = RB_ROOT;
44 hists->entries = RB_ROOT;
45 pthread_mutex_init(&hists->lock, NULL);
46}
47
ef1d1af2
ACM
48void perf_evsel__init(struct perf_evsel *evsel,
49 struct perf_event_attr *attr, int idx)
50{
51 evsel->idx = idx;
52 evsel->attr = *attr;
53 INIT_LIST_HEAD(&evsel->node);
1980c2eb 54 hists__init(&evsel->hists);
ef1d1af2
ACM
55}
56
23a2f3ab 57struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
69aad6f1
ACM
58{
59 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
60
ef1d1af2
ACM
61 if (evsel != NULL)
62 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
63
64 return evsel;
65}
66
c410431c
ACM
67static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
68 "cycles",
69 "instructions",
70 "cache-references",
71 "cache-misses",
72 "branches",
73 "branch-misses",
74 "bus-cycles",
75 "stalled-cycles-frontend",
76 "stalled-cycles-backend",
77 "ref-cycles",
78};
79
dd4f5223 80static const char *__perf_evsel__hw_name(u64 config)
c410431c
ACM
81{
82 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
83 return perf_evsel__hw_names[config];
84
85 return "unknown-hardware";
86}
87
27f18617 88static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
c410431c 89{
27f18617 90 int colon = 0, r = 0;
c410431c 91 struct perf_event_attr *attr = &evsel->attr;
c410431c
ACM
92 bool exclude_guest_default = false;
93
94#define MOD_PRINT(context, mod) do { \
95 if (!attr->exclude_##context) { \
27f18617 96 if (!colon) colon = ++r; \
c410431c
ACM
97 r += scnprintf(bf + r, size - r, "%c", mod); \
98 } } while(0)
99
100 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
101 MOD_PRINT(kernel, 'k');
102 MOD_PRINT(user, 'u');
103 MOD_PRINT(hv, 'h');
104 exclude_guest_default = true;
105 }
106
107 if (attr->precise_ip) {
108 if (!colon)
27f18617 109 colon = ++r;
c410431c
ACM
110 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
111 exclude_guest_default = true;
112 }
113
114 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
115 MOD_PRINT(host, 'H');
116 MOD_PRINT(guest, 'G');
117 }
118#undef MOD_PRINT
119 if (colon)
27f18617 120 bf[colon - 1] = ':';
c410431c
ACM
121 return r;
122}
123
27f18617
ACM
124static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
125{
126 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
127 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
128}
129
335c2f5d
ACM
130static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
131 "cpu-clock",
132 "task-clock",
133 "page-faults",
134 "context-switches",
135 "CPU-migrations",
136 "minor-faults",
137 "major-faults",
138 "alignment-faults",
139 "emulation-faults",
140};
141
dd4f5223 142static const char *__perf_evsel__sw_name(u64 config)
335c2f5d
ACM
143{
144 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
145 return perf_evsel__sw_names[config];
146 return "unknown-software";
147}
148
149static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
150{
151 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
152 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
153}
154
0b668bc9
ACM
155const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
156 [PERF_EVSEL__MAX_ALIASES] = {
157 { "L1-dcache", "l1-d", "l1d", "L1-data", },
158 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
159 { "LLC", "L2", },
160 { "dTLB", "d-tlb", "Data-TLB", },
161 { "iTLB", "i-tlb", "Instruction-TLB", },
162 { "branch", "branches", "bpu", "btb", "bpc", },
163 { "node", },
164};
165
166const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
167 [PERF_EVSEL__MAX_ALIASES] = {
168 { "load", "loads", "read", },
169 { "store", "stores", "write", },
170 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
171};
172
173const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
174 [PERF_EVSEL__MAX_ALIASES] = {
175 { "refs", "Reference", "ops", "access", },
176 { "misses", "miss", },
177};
178
179#define C(x) PERF_COUNT_HW_CACHE_##x
180#define CACHE_READ (1 << C(OP_READ))
181#define CACHE_WRITE (1 << C(OP_WRITE))
182#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
183#define COP(x) (1 << x)
184
185/*
186 * cache operartion stat
187 * L1I : Read and prefetch only
188 * ITLB and BPU : Read-only
189 */
190static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
191 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
192 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
193 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
194 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
195 [C(ITLB)] = (CACHE_READ),
196 [C(BPU)] = (CACHE_READ),
197 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
198};
199
200bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
201{
202 if (perf_evsel__hw_cache_stat[type] & COP(op))
203 return true; /* valid */
204 else
205 return false; /* invalid */
206}
207
208int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
209 char *bf, size_t size)
210{
211 if (result) {
212 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
213 perf_evsel__hw_cache_op[op][0],
214 perf_evsel__hw_cache_result[result][0]);
215 }
216
217 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
218 perf_evsel__hw_cache_op[op][1]);
219}
220
dd4f5223 221static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
0b668bc9
ACM
222{
223 u8 op, result, type = (config >> 0) & 0xff;
224 const char *err = "unknown-ext-hardware-cache-type";
225
226 if (type > PERF_COUNT_HW_CACHE_MAX)
227 goto out_err;
228
229 op = (config >> 8) & 0xff;
230 err = "unknown-ext-hardware-cache-op";
231 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
232 goto out_err;
233
234 result = (config >> 16) & 0xff;
235 err = "unknown-ext-hardware-cache-result";
236 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
237 goto out_err;
238
239 err = "invalid-cache";
240 if (!perf_evsel__is_cache_op_valid(type, op))
241 goto out_err;
242
243 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
244out_err:
245 return scnprintf(bf, size, "%s", err);
246}
247
248static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
249{
250 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
251 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
252}
253
6eef3d9c
ACM
254static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
255{
256 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
257 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
258}
259
7289f83c 260const char *perf_evsel__name(struct perf_evsel *evsel)
a4460836 261{
7289f83c 262 char bf[128];
a4460836 263
7289f83c
ACM
264 if (evsel->name)
265 return evsel->name;
c410431c
ACM
266
267 switch (evsel->attr.type) {
268 case PERF_TYPE_RAW:
6eef3d9c 269 perf_evsel__raw_name(evsel, bf, sizeof(bf));
c410431c
ACM
270 break;
271
272 case PERF_TYPE_HARDWARE:
7289f83c 273 perf_evsel__hw_name(evsel, bf, sizeof(bf));
c410431c 274 break;
0b668bc9
ACM
275
276 case PERF_TYPE_HW_CACHE:
7289f83c 277 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
0b668bc9
ACM
278 break;
279
335c2f5d 280 case PERF_TYPE_SOFTWARE:
7289f83c 281 perf_evsel__sw_name(evsel, bf, sizeof(bf));
335c2f5d
ACM
282 break;
283
a4460836 284 case PERF_TYPE_TRACEPOINT:
7289f83c 285 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
a4460836
ACM
286 break;
287
c410431c 288 default:
7289f83c 289 scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
a4460836 290 break;
c410431c
ACM
291 }
292
7289f83c
ACM
293 evsel->name = strdup(bf);
294
295 return evsel->name ?: "unknown";
c410431c
ACM
296}
297
5090c6ae
NK
298void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
299 struct perf_evsel *first)
0f82ebc4
ACM
300{
301 struct perf_event_attr *attr = &evsel->attr;
302 int track = !evsel->idx; /* only the first counter needs these */
303
5e1c81d9 304 attr->disabled = 1;
808e1226 305 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
0f82ebc4
ACM
306 attr->inherit = !opts->no_inherit;
307 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
308 PERF_FORMAT_TOTAL_TIME_RUNNING |
309 PERF_FORMAT_ID;
310
311 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
312
313 /*
314 * We default some events to a 1 default interval. But keep
315 * it a weak assumption overridable by the user.
316 */
317 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
318 opts->user_interval != ULLONG_MAX)) {
319 if (opts->freq) {
320 attr->sample_type |= PERF_SAMPLE_PERIOD;
321 attr->freq = 1;
322 attr->sample_freq = opts->freq;
323 } else {
324 attr->sample_period = opts->default_interval;
325 }
326 }
327
328 if (opts->no_samples)
329 attr->sample_freq = 0;
330
331 if (opts->inherit_stat)
332 attr->inherit_stat = 1;
333
334 if (opts->sample_address) {
335 attr->sample_type |= PERF_SAMPLE_ADDR;
336 attr->mmap_data = track;
337 }
338
339 if (opts->call_graph)
340 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
341
e40ee742 342 if (perf_target__has_cpu(&opts->target))
0f82ebc4
ACM
343 attr->sample_type |= PERF_SAMPLE_CPU;
344
3e76ac78
AV
345 if (opts->period)
346 attr->sample_type |= PERF_SAMPLE_PERIOD;
347
808e1226 348 if (!opts->sample_id_all_missing &&
d67356e7 349 (opts->sample_time || !opts->no_inherit ||
aa22dd49 350 perf_target__has_cpu(&opts->target)))
0f82ebc4
ACM
351 attr->sample_type |= PERF_SAMPLE_TIME;
352
353 if (opts->raw_samples) {
354 attr->sample_type |= PERF_SAMPLE_TIME;
355 attr->sample_type |= PERF_SAMPLE_RAW;
356 attr->sample_type |= PERF_SAMPLE_CPU;
357 }
358
359 if (opts->no_delay) {
360 attr->watermark = 0;
361 attr->wakeup_events = 1;
362 }
bdfebd84
RAV
363 if (opts->branch_stack) {
364 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
365 attr->branch_sample_type = opts->branch_stack;
366 }
0f82ebc4
ACM
367
368 attr->mmap = track;
369 attr->comm = track;
370
d67356e7
NK
371 if (perf_target__none(&opts->target) &&
372 (!opts->group || evsel == first)) {
0f82ebc4
ACM
373 attr->enable_on_exec = 1;
374 }
375}
376
69aad6f1
ACM
377int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
378{
4af4c955 379 int cpu, thread;
69aad6f1 380 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
381
382 if (evsel->fd) {
383 for (cpu = 0; cpu < ncpus; cpu++) {
384 for (thread = 0; thread < nthreads; thread++) {
385 FD(evsel, cpu, thread) = -1;
386 }
387 }
388 }
389
69aad6f1
ACM
390 return evsel->fd != NULL ? 0 : -ENOMEM;
391}
392
70db7533
ACM
393int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
394{
a91e5431
ACM
395 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
396 if (evsel->sample_id == NULL)
397 return -ENOMEM;
398
399 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
400 if (evsel->id == NULL) {
401 xyarray__delete(evsel->sample_id);
402 evsel->sample_id = NULL;
403 return -ENOMEM;
404 }
405
406 return 0;
70db7533
ACM
407}
408
c52b12ed
ACM
409int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
410{
411 evsel->counts = zalloc((sizeof(*evsel->counts) +
412 (ncpus * sizeof(struct perf_counts_values))));
413 return evsel->counts != NULL ? 0 : -ENOMEM;
414}
415
69aad6f1
ACM
416void perf_evsel__free_fd(struct perf_evsel *evsel)
417{
418 xyarray__delete(evsel->fd);
419 evsel->fd = NULL;
420}
421
70db7533
ACM
422void perf_evsel__free_id(struct perf_evsel *evsel)
423{
a91e5431
ACM
424 xyarray__delete(evsel->sample_id);
425 evsel->sample_id = NULL;
426 free(evsel->id);
70db7533
ACM
427 evsel->id = NULL;
428}
429
c52b12ed
ACM
430void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
431{
432 int cpu, thread;
433
434 for (cpu = 0; cpu < ncpus; cpu++)
435 for (thread = 0; thread < nthreads; ++thread) {
436 close(FD(evsel, cpu, thread));
437 FD(evsel, cpu, thread) = -1;
438 }
439}
440
ef1d1af2 441void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
442{
443 assert(list_empty(&evsel->node));
444 xyarray__delete(evsel->fd);
a91e5431
ACM
445 xyarray__delete(evsel->sample_id);
446 free(evsel->id);
ef1d1af2
ACM
447}
448
449void perf_evsel__delete(struct perf_evsel *evsel)
450{
451 perf_evsel__exit(evsel);
023695d9 452 close_cgroup(evsel->cgrp);
f0c55bcf 453 free(evsel->name);
69aad6f1
ACM
454 free(evsel);
455}
c52b12ed
ACM
456
457int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
458 int cpu, int thread, bool scale)
459{
460 struct perf_counts_values count;
461 size_t nv = scale ? 3 : 1;
462
463 if (FD(evsel, cpu, thread) < 0)
464 return -EINVAL;
465
4eed11d5
ACM
466 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
467 return -ENOMEM;
468
c52b12ed
ACM
469 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
470 return -errno;
471
472 if (scale) {
473 if (count.run == 0)
474 count.val = 0;
475 else if (count.run < count.ena)
476 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
477 } else
478 count.ena = count.run = 0;
479
480 evsel->counts->cpu[cpu] = count;
481 return 0;
482}
483
484int __perf_evsel__read(struct perf_evsel *evsel,
485 int ncpus, int nthreads, bool scale)
486{
487 size_t nv = scale ? 3 : 1;
488 int cpu, thread;
489 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
490
52bcd994 491 aggr->val = aggr->ena = aggr->run = 0;
c52b12ed
ACM
492
493 for (cpu = 0; cpu < ncpus; cpu++) {
494 for (thread = 0; thread < nthreads; thread++) {
495 if (FD(evsel, cpu, thread) < 0)
496 continue;
497
498 if (readn(FD(evsel, cpu, thread),
499 &count, nv * sizeof(u64)) < 0)
500 return -errno;
501
502 aggr->val += count.val;
503 if (scale) {
504 aggr->ena += count.ena;
505 aggr->run += count.run;
506 }
507 }
508 }
509
510 evsel->counts->scaled = 0;
511 if (scale) {
512 if (aggr->run == 0) {
513 evsel->counts->scaled = -1;
514 aggr->val = 0;
515 return 0;
516 }
517
518 if (aggr->run < aggr->ena) {
519 evsel->counts->scaled = 1;
520 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
521 }
522 } else
523 aggr->ena = aggr->run = 0;
524
525 return 0;
526}
48290609 527
0252208e 528static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
727ab04e
ACM
529 struct thread_map *threads, bool group,
530 struct xyarray *group_fds)
48290609 531{
0252208e 532 int cpu, thread;
023695d9 533 unsigned long flags = 0;
727ab04e 534 int pid = -1, err;
48290609 535
0252208e
ACM
536 if (evsel->fd == NULL &&
537 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
727ab04e 538 return -ENOMEM;
4eed11d5 539
023695d9
SE
540 if (evsel->cgrp) {
541 flags = PERF_FLAG_PID_CGROUP;
542 pid = evsel->cgrp->fd;
543 }
544
86bd5e86 545 for (cpu = 0; cpu < cpus->nr; cpu++) {
727ab04e 546 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
9d04f178 547
0252208e 548 for (thread = 0; thread < threads->nr; thread++) {
023695d9
SE
549
550 if (!evsel->cgrp)
551 pid = threads->map[thread];
552
0252208e 553 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 554 pid,
f08199d3 555 cpus->map[cpu],
023695d9 556 group_fd, flags);
727ab04e
ACM
557 if (FD(evsel, cpu, thread) < 0) {
558 err = -errno;
0252208e 559 goto out_close;
727ab04e 560 }
f08199d3
ACM
561
562 if (group && group_fd == -1)
563 group_fd = FD(evsel, cpu, thread);
0252208e 564 }
48290609
ACM
565 }
566
567 return 0;
568
569out_close:
0252208e
ACM
570 do {
571 while (--thread >= 0) {
572 close(FD(evsel, cpu, thread));
573 FD(evsel, cpu, thread) = -1;
574 }
575 thread = threads->nr;
576 } while (--cpu >= 0);
727ab04e
ACM
577 return err;
578}
579
580void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
581{
582 if (evsel->fd == NULL)
583 return;
584
585 perf_evsel__close_fd(evsel, ncpus, nthreads);
586 perf_evsel__free_fd(evsel);
587 evsel->fd = NULL;
48290609
ACM
588}
589
0252208e
ACM
590static struct {
591 struct cpu_map map;
592 int cpus[1];
593} empty_cpu_map = {
594 .map.nr = 1,
595 .cpus = { -1, },
596};
597
598static struct {
599 struct thread_map map;
600 int threads[1];
601} empty_thread_map = {
602 .map.nr = 1,
603 .threads = { -1, },
604};
605
f08199d3 606int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
727ab04e
ACM
607 struct thread_map *threads, bool group,
608 struct xyarray *group_fd)
48290609 609{
0252208e
ACM
610 if (cpus == NULL) {
611 /* Work around old compiler warnings about strict aliasing */
612 cpus = &empty_cpu_map.map;
48290609
ACM
613 }
614
0252208e
ACM
615 if (threads == NULL)
616 threads = &empty_thread_map.map;
48290609 617
727ab04e 618 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
48290609
ACM
619}
620
f08199d3 621int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
727ab04e
ACM
622 struct cpu_map *cpus, bool group,
623 struct xyarray *group_fd)
48290609 624{
727ab04e
ACM
625 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
626 group_fd);
0252208e 627}
48290609 628
f08199d3 629int perf_evsel__open_per_thread(struct perf_evsel *evsel,
727ab04e
ACM
630 struct thread_map *threads, bool group,
631 struct xyarray *group_fd)
0252208e 632{
727ab04e
ACM
633 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
634 group_fd);
48290609 635}
70082dd9 636
8115d60c 637static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
37073f9e
JO
638 struct perf_sample *sample,
639 bool swapped)
d0dd74e8
ACM
640{
641 const u64 *array = event->sample.array;
37073f9e 642 union u64_swap u;
d0dd74e8
ACM
643
644 array += ((event->header.size -
645 sizeof(event->header)) / sizeof(u64)) - 1;
646
647 if (type & PERF_SAMPLE_CPU) {
37073f9e
JO
648 u.val64 = *array;
649 if (swapped) {
650 /* undo swap of u64, then swap on individual u32s */
651 u.val64 = bswap_64(u.val64);
652 u.val32[0] = bswap_32(u.val32[0]);
653 }
654
655 sample->cpu = u.val32[0];
d0dd74e8
ACM
656 array--;
657 }
658
659 if (type & PERF_SAMPLE_STREAM_ID) {
660 sample->stream_id = *array;
661 array--;
662 }
663
664 if (type & PERF_SAMPLE_ID) {
665 sample->id = *array;
666 array--;
667 }
668
669 if (type & PERF_SAMPLE_TIME) {
670 sample->time = *array;
671 array--;
672 }
673
674 if (type & PERF_SAMPLE_TID) {
37073f9e
JO
675 u.val64 = *array;
676 if (swapped) {
677 /* undo swap of u64, then swap on individual u32s */
678 u.val64 = bswap_64(u.val64);
679 u.val32[0] = bswap_32(u.val32[0]);
680 u.val32[1] = bswap_32(u.val32[1]);
681 }
682
683 sample->pid = u.val32[0];
684 sample->tid = u.val32[1];
d0dd74e8
ACM
685 }
686
687 return 0;
688}
689
98e1da90
FW
690static bool sample_overlap(const union perf_event *event,
691 const void *offset, u64 size)
692{
693 const void *base = event;
694
695 if (offset + size > base + event->header.size)
696 return true;
697
698 return false;
699}
700
8115d60c 701int perf_event__parse_sample(const union perf_event *event, u64 type,
a2854124 702 int sample_size, bool sample_id_all,
936be503 703 struct perf_sample *data, bool swapped)
d0dd74e8
ACM
704{
705 const u64 *array;
706
936be503
DA
707 /*
708 * used for cross-endian analysis. See git commit 65014ab3
709 * for why this goofiness is needed.
710 */
6a11f92e 711 union u64_swap u;
936be503 712
f3bda2c9 713 memset(data, 0, sizeof(*data));
d0dd74e8
ACM
714 data->cpu = data->pid = data->tid = -1;
715 data->stream_id = data->id = data->time = -1ULL;
a4a03fc7 716 data->period = 1;
d0dd74e8
ACM
717
718 if (event->header.type != PERF_RECORD_SAMPLE) {
719 if (!sample_id_all)
720 return 0;
37073f9e 721 return perf_event__parse_id_sample(event, type, data, swapped);
d0dd74e8
ACM
722 }
723
724 array = event->sample.array;
725
a2854124
FW
726 if (sample_size + sizeof(event->header) > event->header.size)
727 return -EFAULT;
728
d0dd74e8
ACM
729 if (type & PERF_SAMPLE_IP) {
730 data->ip = event->ip.ip;
731 array++;
732 }
733
734 if (type & PERF_SAMPLE_TID) {
936be503
DA
735 u.val64 = *array;
736 if (swapped) {
737 /* undo swap of u64, then swap on individual u32s */
738 u.val64 = bswap_64(u.val64);
739 u.val32[0] = bswap_32(u.val32[0]);
740 u.val32[1] = bswap_32(u.val32[1]);
741 }
742
743 data->pid = u.val32[0];
744 data->tid = u.val32[1];
d0dd74e8
ACM
745 array++;
746 }
747
748 if (type & PERF_SAMPLE_TIME) {
749 data->time = *array;
750 array++;
751 }
752
7cec0922 753 data->addr = 0;
d0dd74e8
ACM
754 if (type & PERF_SAMPLE_ADDR) {
755 data->addr = *array;
756 array++;
757 }
758
759 data->id = -1ULL;
760 if (type & PERF_SAMPLE_ID) {
761 data->id = *array;
762 array++;
763 }
764
765 if (type & PERF_SAMPLE_STREAM_ID) {
766 data->stream_id = *array;
767 array++;
768 }
769
770 if (type & PERF_SAMPLE_CPU) {
936be503
DA
771
772 u.val64 = *array;
773 if (swapped) {
774 /* undo swap of u64, then swap on individual u32s */
775 u.val64 = bswap_64(u.val64);
776 u.val32[0] = bswap_32(u.val32[0]);
777 }
778
779 data->cpu = u.val32[0];
d0dd74e8
ACM
780 array++;
781 }
782
783 if (type & PERF_SAMPLE_PERIOD) {
784 data->period = *array;
785 array++;
786 }
787
788 if (type & PERF_SAMPLE_READ) {
f9d36996 789 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
d0dd74e8
ACM
790 return -1;
791 }
792
793 if (type & PERF_SAMPLE_CALLCHAIN) {
98e1da90
FW
794 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
795 return -EFAULT;
796
d0dd74e8 797 data->callchain = (struct ip_callchain *)array;
98e1da90
FW
798
799 if (sample_overlap(event, array, data->callchain->nr))
800 return -EFAULT;
801
d0dd74e8
ACM
802 array += 1 + data->callchain->nr;
803 }
804
805 if (type & PERF_SAMPLE_RAW) {
8e303f20
JO
806 const u64 *pdata;
807
936be503
DA
808 u.val64 = *array;
809 if (WARN_ONCE(swapped,
810 "Endianness of raw data not corrected!\n")) {
811 /* undo swap of u64, then swap on individual u32s */
812 u.val64 = bswap_64(u.val64);
813 u.val32[0] = bswap_32(u.val32[0]);
814 u.val32[1] = bswap_32(u.val32[1]);
815 }
98e1da90
FW
816
817 if (sample_overlap(event, array, sizeof(u32)))
818 return -EFAULT;
819
936be503 820 data->raw_size = u.val32[0];
8e303f20 821 pdata = (void *) array + sizeof(u32);
98e1da90 822
8e303f20 823 if (sample_overlap(event, pdata, data->raw_size))
98e1da90
FW
824 return -EFAULT;
825
8e303f20 826 data->raw_data = (void *) pdata;
fa30c964
SE
827
828 array = (void *)array + data->raw_size + sizeof(u32);
d0dd74e8
ACM
829 }
830
b5387528
RAV
831 if (type & PERF_SAMPLE_BRANCH_STACK) {
832 u64 sz;
833
834 data->branch_stack = (struct branch_stack *)array;
835 array++; /* nr */
836
837 sz = data->branch_stack->nr * sizeof(struct branch_entry);
838 sz /= sizeof(u64);
839 array += sz;
840 }
d0dd74e8
ACM
841 return 0;
842}
74eec26f
AV
843
844int perf_event__synthesize_sample(union perf_event *event, u64 type,
845 const struct perf_sample *sample,
846 bool swapped)
847{
848 u64 *array;
849
850 /*
851 * used for cross-endian analysis. See git commit 65014ab3
852 * for why this goofiness is needed.
853 */
6a11f92e 854 union u64_swap u;
74eec26f
AV
855
856 array = event->sample.array;
857
858 if (type & PERF_SAMPLE_IP) {
859 event->ip.ip = sample->ip;
860 array++;
861 }
862
863 if (type & PERF_SAMPLE_TID) {
864 u.val32[0] = sample->pid;
865 u.val32[1] = sample->tid;
866 if (swapped) {
867 /*
868 * Inverse of what is done in perf_event__parse_sample
869 */
870 u.val32[0] = bswap_32(u.val32[0]);
871 u.val32[1] = bswap_32(u.val32[1]);
872 u.val64 = bswap_64(u.val64);
873 }
874
875 *array = u.val64;
876 array++;
877 }
878
879 if (type & PERF_SAMPLE_TIME) {
880 *array = sample->time;
881 array++;
882 }
883
884 if (type & PERF_SAMPLE_ADDR) {
885 *array = sample->addr;
886 array++;
887 }
888
889 if (type & PERF_SAMPLE_ID) {
890 *array = sample->id;
891 array++;
892 }
893
894 if (type & PERF_SAMPLE_STREAM_ID) {
895 *array = sample->stream_id;
896 array++;
897 }
898
899 if (type & PERF_SAMPLE_CPU) {
900 u.val32[0] = sample->cpu;
901 if (swapped) {
902 /*
903 * Inverse of what is done in perf_event__parse_sample
904 */
905 u.val32[0] = bswap_32(u.val32[0]);
906 u.val64 = bswap_64(u.val64);
907 }
908 *array = u.val64;
909 array++;
910 }
911
912 if (type & PERF_SAMPLE_PERIOD) {
913 *array = sample->period;
914 array++;
915 }
916
917 return 0;
918}