]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/evsel.c
perf evlist: Remove obsolete dummy execve
[mirror_ubuntu-artful-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 10#include <byteswap.h>
0f6a3015 11#include <linux/bitops.h>
85c66be1 12#include <lk/debugfs.h>
4e319027
RR
13#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
16#include "asm/bug.h"
69aad6f1 17#include "evsel.h"
70082dd9 18#include "evlist.h"
69aad6f1 19#include "util.h"
86bd5e86 20#include "cpumap.h"
fd78260b 21#include "thread_map.h"
12864b31 22#include "target.h"
26d33022 23#include "perf_regs.h"
69aad6f1 24
594ac61a
ACM
25static struct {
26 bool sample_id_all;
27 bool exclude_guest;
28} perf_missing_features;
29
c52b12ed
ACM
30#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
31
bde09467 32static int __perf_evsel__sample_size(u64 sample_type)
c2a70653
ACM
33{
34 u64 mask = sample_type & PERF_SAMPLE_MASK;
35 int size = 0;
36 int i;
37
38 for (i = 0; i < 64; i++) {
39 if (mask & (1ULL << i))
40 size++;
41 }
42
43 size *= sizeof(u64);
44
45 return size;
46}
47
4bf9ce1b 48void hists__init(struct hists *hists)
0e2a5f10
ACM
49{
50 memset(hists, 0, sizeof(*hists));
51 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
52 hists->entries_in = &hists->entries_in_array[0];
53 hists->entries_collapsed = RB_ROOT;
54 hists->entries = RB_ROOT;
55 pthread_mutex_init(&hists->lock, NULL);
56}
57
7be5ebe8
ACM
58void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
59 enum perf_event_sample_format bit)
60{
61 if (!(evsel->attr.sample_type & bit)) {
62 evsel->attr.sample_type |= bit;
63 evsel->sample_size += sizeof(u64);
64 }
65}
66
67void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
68 enum perf_event_sample_format bit)
69{
70 if (evsel->attr.sample_type & bit) {
71 evsel->attr.sample_type &= ~bit;
72 evsel->sample_size -= sizeof(u64);
73 }
74}
75
7a5a5ca5
ACM
76void perf_evsel__set_sample_id(struct perf_evsel *evsel)
77{
78 perf_evsel__set_sample_bit(evsel, ID);
79 evsel->attr.read_format |= PERF_FORMAT_ID;
80}
81
ef1d1af2
ACM
82void perf_evsel__init(struct perf_evsel *evsel,
83 struct perf_event_attr *attr, int idx)
84{
85 evsel->idx = idx;
86 evsel->attr = *attr;
2cfda562 87 evsel->leader = evsel;
ef1d1af2 88 INIT_LIST_HEAD(&evsel->node);
1980c2eb 89 hists__init(&evsel->hists);
bde09467 90 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
ef1d1af2
ACM
91}
92
23a2f3ab 93struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
69aad6f1
ACM
94{
95 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
96
ef1d1af2
ACM
97 if (evsel != NULL)
98 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
99
100 return evsel;
101}
102
201b7334 103struct event_format *event_format__new(const char *sys, const char *name)
efd2b924
ACM
104{
105 int fd, n;
106 char *filename;
107 void *bf = NULL, *nbf;
108 size_t size = 0, alloc_size = 0;
109 struct event_format *format = NULL;
110
111 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
112 goto out;
113
114 fd = open(filename, O_RDONLY);
115 if (fd < 0)
116 goto out_free_filename;
117
118 do {
119 if (size == alloc_size) {
120 alloc_size += BUFSIZ;
121 nbf = realloc(bf, alloc_size);
122 if (nbf == NULL)
123 goto out_free_bf;
124 bf = nbf;
125 }
126
7cab84e8 127 n = read(fd, bf + size, alloc_size - size);
efd2b924
ACM
128 if (n < 0)
129 goto out_free_bf;
130 size += n;
131 } while (n > 0);
132
133 pevent_parse_format(&format, bf, size, sys);
134
135out_free_bf:
136 free(bf);
137 close(fd);
138out_free_filename:
139 free(filename);
140out:
141 return format;
142}
143
144struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
145{
146 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
147
148 if (evsel != NULL) {
149 struct perf_event_attr attr = {
0b80f8b3
ACM
150 .type = PERF_TYPE_TRACEPOINT,
151 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
152 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
efd2b924
ACM
153 };
154
e48ffe2b
ACM
155 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
156 goto out_free;
157
efd2b924
ACM
158 evsel->tp_format = event_format__new(sys, name);
159 if (evsel->tp_format == NULL)
160 goto out_free;
161
0b80f8b3 162 event_attr_init(&attr);
efd2b924 163 attr.config = evsel->tp_format->id;
0b80f8b3 164 attr.sample_period = 1;
efd2b924 165 perf_evsel__init(evsel, &attr, idx);
efd2b924
ACM
166 }
167
168 return evsel;
169
170out_free:
e48ffe2b 171 free(evsel->name);
efd2b924
ACM
172 free(evsel);
173 return NULL;
174}
175
8ad7013b 176const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
c410431c
ACM
177 "cycles",
178 "instructions",
179 "cache-references",
180 "cache-misses",
181 "branches",
182 "branch-misses",
183 "bus-cycles",
184 "stalled-cycles-frontend",
185 "stalled-cycles-backend",
186 "ref-cycles",
187};
188
dd4f5223 189static const char *__perf_evsel__hw_name(u64 config)
c410431c
ACM
190{
191 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
192 return perf_evsel__hw_names[config];
193
194 return "unknown-hardware";
195}
196
27f18617 197static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
c410431c 198{
27f18617 199 int colon = 0, r = 0;
c410431c 200 struct perf_event_attr *attr = &evsel->attr;
c410431c
ACM
201 bool exclude_guest_default = false;
202
203#define MOD_PRINT(context, mod) do { \
204 if (!attr->exclude_##context) { \
27f18617 205 if (!colon) colon = ++r; \
c410431c
ACM
206 r += scnprintf(bf + r, size - r, "%c", mod); \
207 } } while(0)
208
209 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
210 MOD_PRINT(kernel, 'k');
211 MOD_PRINT(user, 'u');
212 MOD_PRINT(hv, 'h');
213 exclude_guest_default = true;
214 }
215
216 if (attr->precise_ip) {
217 if (!colon)
27f18617 218 colon = ++r;
c410431c
ACM
219 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
220 exclude_guest_default = true;
221 }
222
223 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
224 MOD_PRINT(host, 'H');
225 MOD_PRINT(guest, 'G');
226 }
227#undef MOD_PRINT
228 if (colon)
27f18617 229 bf[colon - 1] = ':';
c410431c
ACM
230 return r;
231}
232
27f18617
ACM
233static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
234{
235 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
236 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
237}
238
8ad7013b 239const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
335c2f5d
ACM
240 "cpu-clock",
241 "task-clock",
242 "page-faults",
243 "context-switches",
8ad7013b 244 "cpu-migrations",
335c2f5d
ACM
245 "minor-faults",
246 "major-faults",
247 "alignment-faults",
248 "emulation-faults",
249};
250
dd4f5223 251static const char *__perf_evsel__sw_name(u64 config)
335c2f5d
ACM
252{
253 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
254 return perf_evsel__sw_names[config];
255 return "unknown-software";
256}
257
258static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
259{
260 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
261 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
262}
263
287e74aa
JO
264static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
265{
266 int r;
267
268 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
269
270 if (type & HW_BREAKPOINT_R)
271 r += scnprintf(bf + r, size - r, "r");
272
273 if (type & HW_BREAKPOINT_W)
274 r += scnprintf(bf + r, size - r, "w");
275
276 if (type & HW_BREAKPOINT_X)
277 r += scnprintf(bf + r, size - r, "x");
278
279 return r;
280}
281
282static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
283{
284 struct perf_event_attr *attr = &evsel->attr;
285 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
286 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
287}
288
0b668bc9
ACM
289const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
290 [PERF_EVSEL__MAX_ALIASES] = {
291 { "L1-dcache", "l1-d", "l1d", "L1-data", },
292 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
293 { "LLC", "L2", },
294 { "dTLB", "d-tlb", "Data-TLB", },
295 { "iTLB", "i-tlb", "Instruction-TLB", },
296 { "branch", "branches", "bpu", "btb", "bpc", },
297 { "node", },
298};
299
300const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
301 [PERF_EVSEL__MAX_ALIASES] = {
302 { "load", "loads", "read", },
303 { "store", "stores", "write", },
304 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
305};
306
307const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
308 [PERF_EVSEL__MAX_ALIASES] = {
309 { "refs", "Reference", "ops", "access", },
310 { "misses", "miss", },
311};
312
313#define C(x) PERF_COUNT_HW_CACHE_##x
314#define CACHE_READ (1 << C(OP_READ))
315#define CACHE_WRITE (1 << C(OP_WRITE))
316#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
317#define COP(x) (1 << x)
318
319/*
320 * cache operartion stat
321 * L1I : Read and prefetch only
322 * ITLB and BPU : Read-only
323 */
324static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
325 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
326 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
327 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
328 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
329 [C(ITLB)] = (CACHE_READ),
330 [C(BPU)] = (CACHE_READ),
331 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
332};
333
334bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
335{
336 if (perf_evsel__hw_cache_stat[type] & COP(op))
337 return true; /* valid */
338 else
339 return false; /* invalid */
340}
341
342int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
343 char *bf, size_t size)
344{
345 if (result) {
346 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
347 perf_evsel__hw_cache_op[op][0],
348 perf_evsel__hw_cache_result[result][0]);
349 }
350
351 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
352 perf_evsel__hw_cache_op[op][1]);
353}
354
dd4f5223 355static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
0b668bc9
ACM
356{
357 u8 op, result, type = (config >> 0) & 0xff;
358 const char *err = "unknown-ext-hardware-cache-type";
359
360 if (type > PERF_COUNT_HW_CACHE_MAX)
361 goto out_err;
362
363 op = (config >> 8) & 0xff;
364 err = "unknown-ext-hardware-cache-op";
365 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
366 goto out_err;
367
368 result = (config >> 16) & 0xff;
369 err = "unknown-ext-hardware-cache-result";
370 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
371 goto out_err;
372
373 err = "invalid-cache";
374 if (!perf_evsel__is_cache_op_valid(type, op))
375 goto out_err;
376
377 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
378out_err:
379 return scnprintf(bf, size, "%s", err);
380}
381
382static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
383{
384 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
385 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
386}
387
6eef3d9c
ACM
388static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
389{
390 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
391 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
392}
393
7289f83c 394const char *perf_evsel__name(struct perf_evsel *evsel)
a4460836 395{
7289f83c 396 char bf[128];
a4460836 397
7289f83c
ACM
398 if (evsel->name)
399 return evsel->name;
c410431c
ACM
400
401 switch (evsel->attr.type) {
402 case PERF_TYPE_RAW:
6eef3d9c 403 perf_evsel__raw_name(evsel, bf, sizeof(bf));
c410431c
ACM
404 break;
405
406 case PERF_TYPE_HARDWARE:
7289f83c 407 perf_evsel__hw_name(evsel, bf, sizeof(bf));
c410431c 408 break;
0b668bc9
ACM
409
410 case PERF_TYPE_HW_CACHE:
7289f83c 411 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
0b668bc9
ACM
412 break;
413
335c2f5d 414 case PERF_TYPE_SOFTWARE:
7289f83c 415 perf_evsel__sw_name(evsel, bf, sizeof(bf));
335c2f5d
ACM
416 break;
417
a4460836 418 case PERF_TYPE_TRACEPOINT:
7289f83c 419 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
a4460836
ACM
420 break;
421
287e74aa
JO
422 case PERF_TYPE_BREAKPOINT:
423 perf_evsel__bp_name(evsel, bf, sizeof(bf));
424 break;
425
c410431c 426 default:
ca1b1457
RR
427 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
428 evsel->attr.type);
a4460836 429 break;
c410431c
ACM
430 }
431
7289f83c
ACM
432 evsel->name = strdup(bf);
433
434 return evsel->name ?: "unknown";
c410431c
ACM
435}
436
717e263f
NK
437const char *perf_evsel__group_name(struct perf_evsel *evsel)
438{
439 return evsel->group_name ?: "anon group";
440}
441
442int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
443{
444 int ret;
445 struct perf_evsel *pos;
446 const char *group_name = perf_evsel__group_name(evsel);
447
448 ret = scnprintf(buf, size, "%s", group_name);
449
450 ret += scnprintf(buf + ret, size - ret, " { %s",
451 perf_evsel__name(evsel));
452
453 for_each_group_member(pos, evsel)
454 ret += scnprintf(buf + ret, size - ret, ", %s",
455 perf_evsel__name(pos));
456
457 ret += scnprintf(buf + ret, size - ret, " }");
458
459 return ret;
460}
461
774cb499
JO
462/*
463 * The enable_on_exec/disabled value strategy:
464 *
465 * 1) For any type of traced program:
466 * - all independent events and group leaders are disabled
467 * - all group members are enabled
468 *
469 * Group members are ruled by group leaders. They need to
470 * be enabled, because the group scheduling relies on that.
471 *
472 * 2) For traced programs executed by perf:
473 * - all independent events and group leaders have
474 * enable_on_exec set
475 * - we don't specifically enable or disable any event during
476 * the record command
477 *
478 * Independent events and group leaders are initially disabled
479 * and get enabled by exec. Group members are ruled by group
480 * leaders as stated in 1).
481 *
482 * 3) For traced programs attached by perf (pid/tid):
483 * - we specifically enable or disable all events during
484 * the record command
485 *
486 * When attaching events to already running traced we
487 * enable/disable events specifically, as there's no
488 * initial traced exec call.
489 */
cac21425
JO
490void perf_evsel__config(struct perf_evsel *evsel,
491 struct perf_record_opts *opts)
0f82ebc4 492{
3c176311 493 struct perf_evsel *leader = evsel->leader;
0f82ebc4
ACM
494 struct perf_event_attr *attr = &evsel->attr;
495 int track = !evsel->idx; /* only the first counter needs these */
496
594ac61a 497 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
0f82ebc4 498 attr->inherit = !opts->no_inherit;
0f82ebc4 499
7be5ebe8
ACM
500 perf_evsel__set_sample_bit(evsel, IP);
501 perf_evsel__set_sample_bit(evsel, TID);
0f82ebc4 502
3c176311
JO
503 if (evsel->sample_read) {
504 perf_evsel__set_sample_bit(evsel, READ);
505
506 /*
507 * We need ID even in case of single event, because
508 * PERF_SAMPLE_READ process ID specific data.
509 */
510 perf_evsel__set_sample_id(evsel);
511
512 /*
513 * Apply group format only if we belong to group
514 * with more than one members.
515 */
516 if (leader->nr_members > 1) {
517 attr->read_format |= PERF_FORMAT_GROUP;
518 attr->inherit = 0;
519 }
520 }
521
0f82ebc4
ACM
522 /*
523 * We default some events to a 1 default interval. But keep
524 * it a weak assumption overridable by the user.
525 */
526 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
527 opts->user_interval != ULLONG_MAX)) {
528 if (opts->freq) {
7be5ebe8 529 perf_evsel__set_sample_bit(evsel, PERIOD);
0f82ebc4
ACM
530 attr->freq = 1;
531 attr->sample_freq = opts->freq;
532 } else {
533 attr->sample_period = opts->default_interval;
534 }
535 }
536
3c176311
JO
537 /*
538 * Disable sampling for all group members other
539 * than leader in case leader 'leads' the sampling.
540 */
541 if ((leader != evsel) && leader->sample_read) {
542 attr->sample_freq = 0;
543 attr->sample_period = 0;
544 }
545
0f82ebc4
ACM
546 if (opts->no_samples)
547 attr->sample_freq = 0;
548
549 if (opts->inherit_stat)
550 attr->inherit_stat = 1;
551
552 if (opts->sample_address) {
7be5ebe8 553 perf_evsel__set_sample_bit(evsel, ADDR);
0f82ebc4
ACM
554 attr->mmap_data = track;
555 }
556
26d33022 557 if (opts->call_graph) {
7be5ebe8 558 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
0f82ebc4 559
26d33022 560 if (opts->call_graph == CALLCHAIN_DWARF) {
7be5ebe8
ACM
561 perf_evsel__set_sample_bit(evsel, REGS_USER);
562 perf_evsel__set_sample_bit(evsel, STACK_USER);
26d33022
JO
563 attr->sample_regs_user = PERF_REGS_MASK;
564 attr->sample_stack_user = opts->stack_dump_size;
565 attr->exclude_callchain_user = 1;
566 }
567 }
568
e40ee742 569 if (perf_target__has_cpu(&opts->target))
7be5ebe8 570 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4 571
3e76ac78 572 if (opts->period)
7be5ebe8 573 perf_evsel__set_sample_bit(evsel, PERIOD);
3e76ac78 574
594ac61a 575 if (!perf_missing_features.sample_id_all &&
d67356e7 576 (opts->sample_time || !opts->no_inherit ||
aa22dd49 577 perf_target__has_cpu(&opts->target)))
7be5ebe8 578 perf_evsel__set_sample_bit(evsel, TIME);
0f82ebc4
ACM
579
580 if (opts->raw_samples) {
7be5ebe8
ACM
581 perf_evsel__set_sample_bit(evsel, TIME);
582 perf_evsel__set_sample_bit(evsel, RAW);
583 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4
ACM
584 }
585
ccf49bfc
SE
586 if (opts->sample_address)
587 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
588
0f82ebc4
ACM
589 if (opts->no_delay) {
590 attr->watermark = 0;
591 attr->wakeup_events = 1;
592 }
bdfebd84 593 if (opts->branch_stack) {
7be5ebe8 594 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
bdfebd84
RAV
595 attr->branch_sample_type = opts->branch_stack;
596 }
0f82ebc4 597
05484298
AK
598 if (opts->sample_weight)
599 attr->sample_type |= PERF_SAMPLE_WEIGHT;
600
0f82ebc4
ACM
601 attr->mmap = track;
602 attr->comm = track;
603
774cb499
JO
604 /*
605 * XXX see the function comment above
606 *
607 * Disabling only independent events or group leaders,
608 * keeping group members enabled.
609 */
823254ed 610 if (perf_evsel__is_group_leader(evsel))
774cb499
JO
611 attr->disabled = 1;
612
613 /*
614 * Setting enable_on_exec for independent events and
615 * group leaders for traced executed by perf.
616 */
823254ed 617 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
0f82ebc4 618 attr->enable_on_exec = 1;
0f82ebc4
ACM
619}
620
69aad6f1
ACM
621int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
622{
4af4c955 623 int cpu, thread;
69aad6f1 624 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
625
626 if (evsel->fd) {
627 for (cpu = 0; cpu < ncpus; cpu++) {
628 for (thread = 0; thread < nthreads; thread++) {
629 FD(evsel, cpu, thread) = -1;
630 }
631 }
632 }
633
69aad6f1
ACM
634 return evsel->fd != NULL ? 0 : -ENOMEM;
635}
636
745cefc5
ACM
637int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
638 const char *filter)
639{
640 int cpu, thread;
641
642 for (cpu = 0; cpu < ncpus; cpu++) {
643 for (thread = 0; thread < nthreads; thread++) {
644 int fd = FD(evsel, cpu, thread),
645 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
646
647 if (err)
648 return err;
649 }
650 }
651
652 return 0;
653}
654
70db7533
ACM
655int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
656{
a91e5431
ACM
657 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
658 if (evsel->sample_id == NULL)
659 return -ENOMEM;
660
661 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
662 if (evsel->id == NULL) {
663 xyarray__delete(evsel->sample_id);
664 evsel->sample_id = NULL;
665 return -ENOMEM;
666 }
667
668 return 0;
70db7533
ACM
669}
670
a7e191c3
FD
671void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
672{
673 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
674 (ncpus * sizeof(struct perf_counts_values))));
675}
676
c52b12ed
ACM
677int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
678{
679 evsel->counts = zalloc((sizeof(*evsel->counts) +
680 (ncpus * sizeof(struct perf_counts_values))));
681 return evsel->counts != NULL ? 0 : -ENOMEM;
682}
683
69aad6f1
ACM
684void perf_evsel__free_fd(struct perf_evsel *evsel)
685{
686 xyarray__delete(evsel->fd);
687 evsel->fd = NULL;
688}
689
70db7533
ACM
690void perf_evsel__free_id(struct perf_evsel *evsel)
691{
a91e5431
ACM
692 xyarray__delete(evsel->sample_id);
693 evsel->sample_id = NULL;
694 free(evsel->id);
70db7533
ACM
695 evsel->id = NULL;
696}
697
c52b12ed
ACM
698void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
699{
700 int cpu, thread;
701
702 for (cpu = 0; cpu < ncpus; cpu++)
703 for (thread = 0; thread < nthreads; ++thread) {
704 close(FD(evsel, cpu, thread));
705 FD(evsel, cpu, thread) = -1;
706 }
707}
708
43f8e76e
NK
709void perf_evsel__free_counts(struct perf_evsel *evsel)
710{
711 free(evsel->counts);
712}
713
ef1d1af2 714void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
715{
716 assert(list_empty(&evsel->node));
736b05a0
NK
717 perf_evsel__free_fd(evsel);
718 perf_evsel__free_id(evsel);
ef1d1af2
ACM
719}
720
721void perf_evsel__delete(struct perf_evsel *evsel)
722{
723 perf_evsel__exit(evsel);
023695d9 724 close_cgroup(evsel->cgrp);
6a4bb04c 725 free(evsel->group_name);
e48ffe2b 726 if (evsel->tp_format)
efd2b924 727 pevent_free_format(evsel->tp_format);
f0c55bcf 728 free(evsel->name);
69aad6f1
ACM
729 free(evsel);
730}
c52b12ed 731
c7a79c47
SE
732static inline void compute_deltas(struct perf_evsel *evsel,
733 int cpu,
734 struct perf_counts_values *count)
735{
736 struct perf_counts_values tmp;
737
738 if (!evsel->prev_raw_counts)
739 return;
740
741 if (cpu == -1) {
742 tmp = evsel->prev_raw_counts->aggr;
743 evsel->prev_raw_counts->aggr = *count;
744 } else {
745 tmp = evsel->prev_raw_counts->cpu[cpu];
746 evsel->prev_raw_counts->cpu[cpu] = *count;
747 }
748
749 count->val = count->val - tmp.val;
750 count->ena = count->ena - tmp.ena;
751 count->run = count->run - tmp.run;
752}
753
c52b12ed
ACM
754int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
755 int cpu, int thread, bool scale)
756{
757 struct perf_counts_values count;
758 size_t nv = scale ? 3 : 1;
759
760 if (FD(evsel, cpu, thread) < 0)
761 return -EINVAL;
762
4eed11d5
ACM
763 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
764 return -ENOMEM;
765
c52b12ed
ACM
766 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
767 return -errno;
768
c7a79c47
SE
769 compute_deltas(evsel, cpu, &count);
770
c52b12ed
ACM
771 if (scale) {
772 if (count.run == 0)
773 count.val = 0;
774 else if (count.run < count.ena)
775 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
776 } else
777 count.ena = count.run = 0;
778
779 evsel->counts->cpu[cpu] = count;
780 return 0;
781}
782
783int __perf_evsel__read(struct perf_evsel *evsel,
784 int ncpus, int nthreads, bool scale)
785{
786 size_t nv = scale ? 3 : 1;
787 int cpu, thread;
788 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
789
52bcd994 790 aggr->val = aggr->ena = aggr->run = 0;
c52b12ed
ACM
791
792 for (cpu = 0; cpu < ncpus; cpu++) {
793 for (thread = 0; thread < nthreads; thread++) {
794 if (FD(evsel, cpu, thread) < 0)
795 continue;
796
797 if (readn(FD(evsel, cpu, thread),
798 &count, nv * sizeof(u64)) < 0)
799 return -errno;
800
801 aggr->val += count.val;
802 if (scale) {
803 aggr->ena += count.ena;
804 aggr->run += count.run;
805 }
806 }
807 }
808
c7a79c47
SE
809 compute_deltas(evsel, -1, aggr);
810
c52b12ed
ACM
811 evsel->counts->scaled = 0;
812 if (scale) {
813 if (aggr->run == 0) {
814 evsel->counts->scaled = -1;
815 aggr->val = 0;
816 return 0;
817 }
818
819 if (aggr->run < aggr->ena) {
820 evsel->counts->scaled = 1;
821 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
822 }
823 } else
824 aggr->ena = aggr->run = 0;
825
826 return 0;
827}
48290609 828
6a4bb04c
JO
829static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
830{
831 struct perf_evsel *leader = evsel->leader;
832 int fd;
833
823254ed 834 if (perf_evsel__is_group_leader(evsel))
6a4bb04c
JO
835 return -1;
836
837 /*
838 * Leader must be already processed/open,
839 * if not it's a bug.
840 */
841 BUG_ON(!leader->fd);
842
843 fd = FD(leader, cpu, thread);
844 BUG_ON(fd == -1);
845
846 return fd;
847}
848
0252208e 849static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 850 struct thread_map *threads)
48290609 851{
0252208e 852 int cpu, thread;
023695d9 853 unsigned long flags = 0;
727ab04e 854 int pid = -1, err;
48290609 855
0252208e
ACM
856 if (evsel->fd == NULL &&
857 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
727ab04e 858 return -ENOMEM;
4eed11d5 859
023695d9
SE
860 if (evsel->cgrp) {
861 flags = PERF_FLAG_PID_CGROUP;
862 pid = evsel->cgrp->fd;
863 }
864
594ac61a
ACM
865fallback_missing_features:
866 if (perf_missing_features.exclude_guest)
867 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
868retry_sample_id:
869 if (perf_missing_features.sample_id_all)
870 evsel->attr.sample_id_all = 0;
871
86bd5e86 872 for (cpu = 0; cpu < cpus->nr; cpu++) {
9d04f178 873
0252208e 874 for (thread = 0; thread < threads->nr; thread++) {
6a4bb04c 875 int group_fd;
023695d9
SE
876
877 if (!evsel->cgrp)
878 pid = threads->map[thread];
879
6a4bb04c
JO
880 group_fd = get_group_fd(evsel, cpu, thread);
881
0252208e 882 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 883 pid,
f08199d3 884 cpus->map[cpu],
023695d9 885 group_fd, flags);
727ab04e
ACM
886 if (FD(evsel, cpu, thread) < 0) {
887 err = -errno;
594ac61a 888 goto try_fallback;
727ab04e 889 }
0252208e 890 }
48290609
ACM
891 }
892
893 return 0;
894
594ac61a
ACM
895try_fallback:
896 if (err != -EINVAL || cpu > 0 || thread > 0)
897 goto out_close;
898
899 if (!perf_missing_features.exclude_guest &&
900 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
901 perf_missing_features.exclude_guest = true;
902 goto fallback_missing_features;
903 } else if (!perf_missing_features.sample_id_all) {
904 perf_missing_features.sample_id_all = true;
905 goto retry_sample_id;
906 }
907
48290609 908out_close:
0252208e
ACM
909 do {
910 while (--thread >= 0) {
911 close(FD(evsel, cpu, thread));
912 FD(evsel, cpu, thread) = -1;
913 }
914 thread = threads->nr;
915 } while (--cpu >= 0);
727ab04e
ACM
916 return err;
917}
918
919void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
920{
921 if (evsel->fd == NULL)
922 return;
923
924 perf_evsel__close_fd(evsel, ncpus, nthreads);
925 perf_evsel__free_fd(evsel);
926 evsel->fd = NULL;
48290609
ACM
927}
928
0252208e
ACM
929static struct {
930 struct cpu_map map;
931 int cpus[1];
932} empty_cpu_map = {
933 .map.nr = 1,
934 .cpus = { -1, },
935};
936
937static struct {
938 struct thread_map map;
939 int threads[1];
940} empty_thread_map = {
941 .map.nr = 1,
942 .threads = { -1, },
943};
944
f08199d3 945int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 946 struct thread_map *threads)
48290609 947{
0252208e
ACM
948 if (cpus == NULL) {
949 /* Work around old compiler warnings about strict aliasing */
950 cpus = &empty_cpu_map.map;
48290609
ACM
951 }
952
0252208e
ACM
953 if (threads == NULL)
954 threads = &empty_thread_map.map;
48290609 955
6a4bb04c 956 return __perf_evsel__open(evsel, cpus, threads);
48290609
ACM
957}
958
f08199d3 959int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
6a4bb04c 960 struct cpu_map *cpus)
48290609 961{
6a4bb04c 962 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
0252208e 963}
48290609 964
f08199d3 965int perf_evsel__open_per_thread(struct perf_evsel *evsel,
6a4bb04c 966 struct thread_map *threads)
0252208e 967{
6a4bb04c 968 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
48290609 969}
70082dd9 970
0807d2d8
ACM
971static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
972 const union perf_event *event,
973 struct perf_sample *sample)
d0dd74e8 974{
0807d2d8 975 u64 type = evsel->attr.sample_type;
d0dd74e8 976 const u64 *array = event->sample.array;
0807d2d8 977 bool swapped = evsel->needs_swap;
37073f9e 978 union u64_swap u;
d0dd74e8
ACM
979
980 array += ((event->header.size -
981 sizeof(event->header)) / sizeof(u64)) - 1;
982
983 if (type & PERF_SAMPLE_CPU) {
37073f9e
JO
984 u.val64 = *array;
985 if (swapped) {
986 /* undo swap of u64, then swap on individual u32s */
987 u.val64 = bswap_64(u.val64);
988 u.val32[0] = bswap_32(u.val32[0]);
989 }
990
991 sample->cpu = u.val32[0];
d0dd74e8
ACM
992 array--;
993 }
994
995 if (type & PERF_SAMPLE_STREAM_ID) {
996 sample->stream_id = *array;
997 array--;
998 }
999
1000 if (type & PERF_SAMPLE_ID) {
1001 sample->id = *array;
1002 array--;
1003 }
1004
1005 if (type & PERF_SAMPLE_TIME) {
1006 sample->time = *array;
1007 array--;
1008 }
1009
1010 if (type & PERF_SAMPLE_TID) {
37073f9e
JO
1011 u.val64 = *array;
1012 if (swapped) {
1013 /* undo swap of u64, then swap on individual u32s */
1014 u.val64 = bswap_64(u.val64);
1015 u.val32[0] = bswap_32(u.val32[0]);
1016 u.val32[1] = bswap_32(u.val32[1]);
1017 }
1018
1019 sample->pid = u.val32[0];
1020 sample->tid = u.val32[1];
d0dd74e8
ACM
1021 }
1022
1023 return 0;
1024}
1025
98e1da90
FW
1026static bool sample_overlap(const union perf_event *event,
1027 const void *offset, u64 size)
1028{
1029 const void *base = event;
1030
1031 if (offset + size > base + event->header.size)
1032 return true;
1033
1034 return false;
1035}
1036
a3f698fe 1037int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
0807d2d8 1038 struct perf_sample *data)
d0dd74e8 1039{
a3f698fe 1040 u64 type = evsel->attr.sample_type;
0f6a3015 1041 u64 regs_user = evsel->attr.sample_regs_user;
0807d2d8 1042 bool swapped = evsel->needs_swap;
d0dd74e8
ACM
1043 const u64 *array;
1044
936be503
DA
1045 /*
1046 * used for cross-endian analysis. See git commit 65014ab3
1047 * for why this goofiness is needed.
1048 */
6a11f92e 1049 union u64_swap u;
936be503 1050
f3bda2c9 1051 memset(data, 0, sizeof(*data));
d0dd74e8
ACM
1052 data->cpu = data->pid = data->tid = -1;
1053 data->stream_id = data->id = data->time = -1ULL;
a4a03fc7 1054 data->period = 1;
05484298 1055 data->weight = 0;
d0dd74e8
ACM
1056
1057 if (event->header.type != PERF_RECORD_SAMPLE) {
a3f698fe 1058 if (!evsel->attr.sample_id_all)
d0dd74e8 1059 return 0;
0807d2d8 1060 return perf_evsel__parse_id_sample(evsel, event, data);
d0dd74e8
ACM
1061 }
1062
1063 array = event->sample.array;
1064
a3f698fe 1065 if (evsel->sample_size + sizeof(event->header) > event->header.size)
a2854124
FW
1066 return -EFAULT;
1067
d0dd74e8
ACM
1068 if (type & PERF_SAMPLE_IP) {
1069 data->ip = event->ip.ip;
1070 array++;
1071 }
1072
1073 if (type & PERF_SAMPLE_TID) {
936be503
DA
1074 u.val64 = *array;
1075 if (swapped) {
1076 /* undo swap of u64, then swap on individual u32s */
1077 u.val64 = bswap_64(u.val64);
1078 u.val32[0] = bswap_32(u.val32[0]);
1079 u.val32[1] = bswap_32(u.val32[1]);
1080 }
1081
1082 data->pid = u.val32[0];
1083 data->tid = u.val32[1];
d0dd74e8
ACM
1084 array++;
1085 }
1086
1087 if (type & PERF_SAMPLE_TIME) {
1088 data->time = *array;
1089 array++;
1090 }
1091
7cec0922 1092 data->addr = 0;
d0dd74e8
ACM
1093 if (type & PERF_SAMPLE_ADDR) {
1094 data->addr = *array;
1095 array++;
1096 }
1097
1098 data->id = -1ULL;
1099 if (type & PERF_SAMPLE_ID) {
1100 data->id = *array;
1101 array++;
1102 }
1103
1104 if (type & PERF_SAMPLE_STREAM_ID) {
1105 data->stream_id = *array;
1106 array++;
1107 }
1108
1109 if (type & PERF_SAMPLE_CPU) {
936be503
DA
1110
1111 u.val64 = *array;
1112 if (swapped) {
1113 /* undo swap of u64, then swap on individual u32s */
1114 u.val64 = bswap_64(u.val64);
1115 u.val32[0] = bswap_32(u.val32[0]);
1116 }
1117
1118 data->cpu = u.val32[0];
d0dd74e8
ACM
1119 array++;
1120 }
1121
1122 if (type & PERF_SAMPLE_PERIOD) {
1123 data->period = *array;
1124 array++;
1125 }
1126
1127 if (type & PERF_SAMPLE_READ) {
9ede473c
JO
1128 u64 read_format = evsel->attr.read_format;
1129
1130 if (read_format & PERF_FORMAT_GROUP)
1131 data->read.group.nr = *array;
1132 else
1133 data->read.one.value = *array;
1134
1135 array++;
1136
1137 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1138 data->read.time_enabled = *array;
1139 array++;
1140 }
1141
1142 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1143 data->read.time_running = *array;
1144 array++;
1145 }
1146
1147 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1148 if (read_format & PERF_FORMAT_GROUP) {
1149 data->read.group.values = (struct sample_read_value *) array;
1150 array = (void *) array + data->read.group.nr *
1151 sizeof(struct sample_read_value);
1152 } else {
1153 data->read.one.id = *array;
1154 array++;
1155 }
d0dd74e8
ACM
1156 }
1157
1158 if (type & PERF_SAMPLE_CALLCHAIN) {
98e1da90
FW
1159 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1160 return -EFAULT;
1161
d0dd74e8 1162 data->callchain = (struct ip_callchain *)array;
98e1da90
FW
1163
1164 if (sample_overlap(event, array, data->callchain->nr))
1165 return -EFAULT;
1166
d0dd74e8
ACM
1167 array += 1 + data->callchain->nr;
1168 }
1169
1170 if (type & PERF_SAMPLE_RAW) {
8e303f20
JO
1171 const u64 *pdata;
1172
936be503
DA
1173 u.val64 = *array;
1174 if (WARN_ONCE(swapped,
1175 "Endianness of raw data not corrected!\n")) {
1176 /* undo swap of u64, then swap on individual u32s */
1177 u.val64 = bswap_64(u.val64);
1178 u.val32[0] = bswap_32(u.val32[0]);
1179 u.val32[1] = bswap_32(u.val32[1]);
1180 }
98e1da90
FW
1181
1182 if (sample_overlap(event, array, sizeof(u32)))
1183 return -EFAULT;
1184
936be503 1185 data->raw_size = u.val32[0];
8e303f20 1186 pdata = (void *) array + sizeof(u32);
98e1da90 1187
8e303f20 1188 if (sample_overlap(event, pdata, data->raw_size))
98e1da90
FW
1189 return -EFAULT;
1190
8e303f20 1191 data->raw_data = (void *) pdata;
fa30c964
SE
1192
1193 array = (void *)array + data->raw_size + sizeof(u32);
d0dd74e8
ACM
1194 }
1195
b5387528
RAV
1196 if (type & PERF_SAMPLE_BRANCH_STACK) {
1197 u64 sz;
1198
1199 data->branch_stack = (struct branch_stack *)array;
1200 array++; /* nr */
1201
1202 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1203 sz /= sizeof(u64);
1204 array += sz;
1205 }
0f6a3015
JO
1206
1207 if (type & PERF_SAMPLE_REGS_USER) {
1208 /* First u64 tells us if we have any regs in sample. */
1209 u64 avail = *array++;
1210
1211 if (avail) {
1212 data->user_regs.regs = (u64 *)array;
1213 array += hweight_long(regs_user);
1214 }
1215 }
1216
1217 if (type & PERF_SAMPLE_STACK_USER) {
1218 u64 size = *array++;
1219
1220 data->user_stack.offset = ((char *)(array - 1)
1221 - (char *) event);
1222
1223 if (!size) {
1224 data->user_stack.size = 0;
1225 } else {
1226 data->user_stack.data = (char *)array;
1227 array += size / sizeof(*array);
54bd2692 1228 data->user_stack.size = *array++;
0f6a3015
JO
1229 }
1230 }
1231
05484298
AK
1232 data->weight = 0;
1233 if (type & PERF_SAMPLE_WEIGHT) {
1234 data->weight = *array;
1235 array++;
1236 }
1237
98a3b32c
SE
1238 data->data_src = PERF_MEM_DATA_SRC_NONE;
1239 if (type & PERF_SAMPLE_DATA_SRC) {
1240 data->data_src = *array;
1241 array++;
1242 }
1243
d0dd74e8
ACM
1244 return 0;
1245}
74eec26f
AV
1246
1247int perf_event__synthesize_sample(union perf_event *event, u64 type,
1248 const struct perf_sample *sample,
1249 bool swapped)
1250{
1251 u64 *array;
1252
1253 /*
1254 * used for cross-endian analysis. See git commit 65014ab3
1255 * for why this goofiness is needed.
1256 */
6a11f92e 1257 union u64_swap u;
74eec26f
AV
1258
1259 array = event->sample.array;
1260
1261 if (type & PERF_SAMPLE_IP) {
1262 event->ip.ip = sample->ip;
1263 array++;
1264 }
1265
1266 if (type & PERF_SAMPLE_TID) {
1267 u.val32[0] = sample->pid;
1268 u.val32[1] = sample->tid;
1269 if (swapped) {
1270 /*
a3f698fe 1271 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1272 */
1273 u.val32[0] = bswap_32(u.val32[0]);
1274 u.val32[1] = bswap_32(u.val32[1]);
1275 u.val64 = bswap_64(u.val64);
1276 }
1277
1278 *array = u.val64;
1279 array++;
1280 }
1281
1282 if (type & PERF_SAMPLE_TIME) {
1283 *array = sample->time;
1284 array++;
1285 }
1286
1287 if (type & PERF_SAMPLE_ADDR) {
1288 *array = sample->addr;
1289 array++;
1290 }
1291
1292 if (type & PERF_SAMPLE_ID) {
1293 *array = sample->id;
1294 array++;
1295 }
1296
1297 if (type & PERF_SAMPLE_STREAM_ID) {
1298 *array = sample->stream_id;
1299 array++;
1300 }
1301
1302 if (type & PERF_SAMPLE_CPU) {
1303 u.val32[0] = sample->cpu;
1304 if (swapped) {
1305 /*
a3f698fe 1306 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1307 */
1308 u.val32[0] = bswap_32(u.val32[0]);
1309 u.val64 = bswap_64(u.val64);
1310 }
1311 *array = u.val64;
1312 array++;
1313 }
1314
1315 if (type & PERF_SAMPLE_PERIOD) {
1316 *array = sample->period;
1317 array++;
1318 }
1319
1320 return 0;
1321}
5555ded4 1322
efd2b924
ACM
1323struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1324{
1325 return pevent_find_field(evsel->tp_format, name);
1326}
1327
5d2074ea 1328void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
5555ded4
ACM
1329 const char *name)
1330{
efd2b924 1331 struct format_field *field = perf_evsel__field(evsel, name);
5555ded4
ACM
1332 int offset;
1333
efd2b924
ACM
1334 if (!field)
1335 return NULL;
5555ded4
ACM
1336
1337 offset = field->offset;
1338
1339 if (field->flags & FIELD_IS_DYNAMIC) {
1340 offset = *(int *)(sample->raw_data + field->offset);
1341 offset &= 0xffff;
1342 }
1343
1344 return sample->raw_data + offset;
1345}
1346
1347u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1348 const char *name)
1349{
efd2b924 1350 struct format_field *field = perf_evsel__field(evsel, name);
e6b6f679
ACM
1351 void *ptr;
1352 u64 value;
5555ded4 1353
efd2b924
ACM
1354 if (!field)
1355 return 0;
5555ded4 1356
e6b6f679 1357 ptr = sample->raw_data + field->offset;
5555ded4 1358
e6b6f679
ACM
1359 switch (field->size) {
1360 case 1:
1361 return *(u8 *)ptr;
1362 case 2:
1363 value = *(u16 *)ptr;
1364 break;
1365 case 4:
1366 value = *(u32 *)ptr;
1367 break;
1368 case 8:
1369 value = *(u64 *)ptr;
1370 break;
1371 default:
1372 return 0;
1373 }
1374
1375 if (!evsel->needs_swap)
1376 return value;
1377
1378 switch (field->size) {
1379 case 2:
1380 return bswap_16(value);
1381 case 4:
1382 return bswap_32(value);
1383 case 8:
1384 return bswap_64(value);
1385 default:
1386 return 0;
1387 }
1388
1389 return 0;
5555ded4 1390}
0698aedd
ACM
1391
1392static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1393{
1394 va_list args;
1395 int ret = 0;
1396
1397 if (!*first) {
1398 ret += fprintf(fp, ",");
1399 } else {
1400 ret += fprintf(fp, ":");
1401 *first = false;
1402 }
1403
1404 va_start(args, fmt);
1405 ret += vfprintf(fp, fmt, args);
1406 va_end(args);
1407 return ret;
1408}
1409
1410static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1411{
1412 if (value == 0)
1413 return 0;
1414
1415 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1416}
1417
1418#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1419
c79a4393
ACM
1420struct bit_names {
1421 int bit;
1422 const char *name;
1423};
1424
1425static int bits__fprintf(FILE *fp, const char *field, u64 value,
1426 struct bit_names *bits, bool *first)
1427{
1428 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1429 bool first_bit = true;
1430
1431 do {
1432 if (value & bits[i].bit) {
1433 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1434 first_bit = false;
1435 }
1436 } while (bits[++i].name != NULL);
1437
1438 return printed;
1439}
1440
1441static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1442{
1443#define bit_name(n) { PERF_SAMPLE_##n, #n }
1444 struct bit_names bits[] = {
1445 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1446 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1447 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1448 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1449 { .name = NULL, }
1450 };
1451#undef bit_name
1452 return bits__fprintf(fp, "sample_type", value, bits, first);
1453}
1454
1455static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1456{
1457#define bit_name(n) { PERF_FORMAT_##n, #n }
1458 struct bit_names bits[] = {
1459 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1460 bit_name(ID), bit_name(GROUP),
1461 { .name = NULL, }
1462 };
1463#undef bit_name
1464 return bits__fprintf(fp, "read_format", value, bits, first);
1465}
1466
0698aedd
ACM
1467int perf_evsel__fprintf(struct perf_evsel *evsel,
1468 struct perf_attr_details *details, FILE *fp)
1469{
1470 bool first = true;
e6ab07d0
NK
1471 int printed = 0;
1472
e35ef355 1473 if (details->event_group) {
e6ab07d0
NK
1474 struct perf_evsel *pos;
1475
1476 if (!perf_evsel__is_group_leader(evsel))
1477 return 0;
1478
1479 if (evsel->nr_members > 1)
1480 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1481
1482 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1483 for_each_group_member(pos, evsel)
1484 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1485
1486 if (evsel->nr_members > 1)
1487 printed += fprintf(fp, "}");
1488 goto out;
1489 }
1490
1491 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
0698aedd
ACM
1492
1493 if (details->verbose || details->freq) {
1494 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1495 (u64)evsel->attr.sample_freq);
1496 }
1497
1498 if (details->verbose) {
1499 if_print(type);
1500 if_print(config);
1501 if_print(config1);
1502 if_print(config2);
1503 if_print(size);
c79a4393
ACM
1504 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1505 if (evsel->attr.read_format)
1506 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
0698aedd
ACM
1507 if_print(disabled);
1508 if_print(inherit);
1509 if_print(pinned);
1510 if_print(exclusive);
1511 if_print(exclude_user);
1512 if_print(exclude_kernel);
1513 if_print(exclude_hv);
1514 if_print(exclude_idle);
1515 if_print(mmap);
1516 if_print(comm);
1517 if_print(freq);
1518 if_print(inherit_stat);
1519 if_print(enable_on_exec);
1520 if_print(task);
1521 if_print(watermark);
1522 if_print(precise_ip);
1523 if_print(mmap_data);
1524 if_print(sample_id_all);
1525 if_print(exclude_host);
1526 if_print(exclude_guest);
1527 if_print(__reserved_1);
1528 if_print(wakeup_events);
1529 if_print(bp_type);
1530 if_print(branch_sample_type);
1531 }
e6ab07d0 1532out:
0698aedd
ACM
1533 fputc('\n', fp);
1534 return ++printed;
1535}
c0a54341
ACM
1536
1537bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1538 char *msg, size_t msgsize)
1539{
2b821cce 1540 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
c0a54341
ACM
1541 evsel->attr.type == PERF_TYPE_HARDWARE &&
1542 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1543 /*
1544 * If it's cycles then fall back to hrtimer based
1545 * cpu-clock-tick sw counter, which is always available even if
1546 * no PMU support.
1547 *
1548 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1549 * b0a873e).
1550 */
1551 scnprintf(msg, msgsize, "%s",
1552"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1553
1554 evsel->attr.type = PERF_TYPE_SOFTWARE;
1555 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1556
1557 free(evsel->name);
1558 evsel->name = NULL;
1559 return true;
1560 }
1561
1562 return false;
1563}
56e52e85
ACM
1564
1565int perf_evsel__open_strerror(struct perf_evsel *evsel,
1566 struct perf_target *target,
1567 int err, char *msg, size_t size)
1568{
1569 switch (err) {
1570 case EPERM:
1571 case EACCES:
b69e63a4 1572 return scnprintf(msg, size,
56e52e85
ACM
1573 "You may not have permission to collect %sstats.\n"
1574 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1575 " -1 - Not paranoid at all\n"
1576 " 0 - Disallow raw tracepoint access for unpriv\n"
1577 " 1 - Disallow cpu events for unpriv\n"
1578 " 2 - Disallow kernel profiling for unpriv",
1579 target->system_wide ? "system-wide " : "");
1580 case ENOENT:
1581 return scnprintf(msg, size, "The %s event is not supported.",
1582 perf_evsel__name(evsel));
1583 case EMFILE:
1584 return scnprintf(msg, size, "%s",
1585 "Too many events are opened.\n"
1586 "Try again after reducing the number of events.");
1587 case ENODEV:
1588 if (target->cpu_list)
1589 return scnprintf(msg, size, "%s",
1590 "No such device - did you specify an out-of-range profile CPU?\n");
1591 break;
1592 case EOPNOTSUPP:
1593 if (evsel->attr.precise_ip)
1594 return scnprintf(msg, size, "%s",
1595 "\'precise\' request may not be supported. Try removing 'p' modifier.");
1596#if defined(__i386__) || defined(__x86_64__)
1597 if (evsel->attr.type == PERF_TYPE_HARDWARE)
1598 return scnprintf(msg, size, "%s",
1599 "No hardware sampling interrupt available.\n"
1600 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
1601#endif
1602 break;
1603 default:
1604 break;
1605 }
1606
1607 return scnprintf(msg, size,
1608 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
1609 "/bin/dmesg may provide additional information.\n"
1610 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
1611 err, strerror(err), perf_evsel__name(evsel));
1612}