]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/evsel.c
perf tools: Add new PERF_RECORD_SWITCH event
[mirror_ubuntu-bionic-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>
553873e1 12#include <api/fs/debugfs.h>
4e319027
RR
13#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
bec19672 16#include <sys/resource.h>
4e319027 17#include "asm/bug.h"
8f651eae 18#include "callchain.h"
f14d5707 19#include "cgroup.h"
69aad6f1 20#include "evsel.h"
70082dd9 21#include "evlist.h"
69aad6f1 22#include "util.h"
86bd5e86 23#include "cpumap.h"
fd78260b 24#include "thread_map.h"
12864b31 25#include "target.h"
26d33022 26#include "perf_regs.h"
e3e1a54f 27#include "debug.h"
97978b3e 28#include "trace-event.h"
a9a3a4d9 29#include "stat.h"
69aad6f1 30
594ac61a
ACM
31static struct {
32 bool sample_id_all;
33 bool exclude_guest;
5c5e854b 34 bool mmap2;
57480d2c 35 bool cloexec;
814c8c38
PZ
36 bool clockid;
37 bool clockid_wrong;
594ac61a
ACM
38} perf_missing_features;
39
814c8c38
PZ
40static clockid_t clockid;
41
ce8ccff5
ACM
42static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
43{
44 return 0;
45}
46
47static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
48{
49}
50
51static struct {
52 size_t size;
53 int (*init)(struct perf_evsel *evsel);
54 void (*fini)(struct perf_evsel *evsel);
55} perf_evsel__object = {
56 .size = sizeof(struct perf_evsel),
57 .init = perf_evsel__no_extra_init,
58 .fini = perf_evsel__no_extra_fini,
59};
60
61int perf_evsel__object_config(size_t object_size,
62 int (*init)(struct perf_evsel *evsel),
63 void (*fini)(struct perf_evsel *evsel))
64{
65
66 if (object_size == 0)
67 goto set_methods;
68
69 if (perf_evsel__object.size > object_size)
70 return -EINVAL;
71
72 perf_evsel__object.size = object_size;
73
74set_methods:
75 if (init != NULL)
76 perf_evsel__object.init = init;
77
78 if (fini != NULL)
79 perf_evsel__object.fini = fini;
80
81 return 0;
82}
83
c52b12ed
ACM
84#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
85
75562573 86int __perf_evsel__sample_size(u64 sample_type)
c2a70653
ACM
87{
88 u64 mask = sample_type & PERF_SAMPLE_MASK;
89 int size = 0;
90 int i;
91
92 for (i = 0; i < 64; i++) {
93 if (mask & (1ULL << i))
94 size++;
95 }
96
97 size *= sizeof(u64);
98
99 return size;
100}
101
75562573
AH
102/**
103 * __perf_evsel__calc_id_pos - calculate id_pos.
104 * @sample_type: sample type
105 *
106 * This function returns the position of the event id (PERF_SAMPLE_ID or
107 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
108 * sample_event.
109 */
110static int __perf_evsel__calc_id_pos(u64 sample_type)
111{
112 int idx = 0;
113
114 if (sample_type & PERF_SAMPLE_IDENTIFIER)
115 return 0;
116
117 if (!(sample_type & PERF_SAMPLE_ID))
118 return -1;
119
120 if (sample_type & PERF_SAMPLE_IP)
121 idx += 1;
122
123 if (sample_type & PERF_SAMPLE_TID)
124 idx += 1;
125
126 if (sample_type & PERF_SAMPLE_TIME)
127 idx += 1;
128
129 if (sample_type & PERF_SAMPLE_ADDR)
130 idx += 1;
131
132 return idx;
133}
134
135/**
136 * __perf_evsel__calc_is_pos - calculate is_pos.
137 * @sample_type: sample type
138 *
139 * This function returns the position (counting backwards) of the event id
140 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
141 * sample_id_all is used there is an id sample appended to non-sample events.
142 */
143static int __perf_evsel__calc_is_pos(u64 sample_type)
144{
145 int idx = 1;
146
147 if (sample_type & PERF_SAMPLE_IDENTIFIER)
148 return 1;
149
150 if (!(sample_type & PERF_SAMPLE_ID))
151 return -1;
152
153 if (sample_type & PERF_SAMPLE_CPU)
154 idx += 1;
155
156 if (sample_type & PERF_SAMPLE_STREAM_ID)
157 idx += 1;
158
159 return idx;
160}
161
162void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
163{
164 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
165 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
166}
167
7be5ebe8
ACM
168void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
169 enum perf_event_sample_format bit)
170{
171 if (!(evsel->attr.sample_type & bit)) {
172 evsel->attr.sample_type |= bit;
173 evsel->sample_size += sizeof(u64);
75562573 174 perf_evsel__calc_id_pos(evsel);
7be5ebe8
ACM
175 }
176}
177
178void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
179 enum perf_event_sample_format bit)
180{
181 if (evsel->attr.sample_type & bit) {
182 evsel->attr.sample_type &= ~bit;
183 evsel->sample_size -= sizeof(u64);
75562573 184 perf_evsel__calc_id_pos(evsel);
7be5ebe8
ACM
185 }
186}
187
75562573
AH
188void perf_evsel__set_sample_id(struct perf_evsel *evsel,
189 bool can_sample_identifier)
7a5a5ca5 190{
75562573
AH
191 if (can_sample_identifier) {
192 perf_evsel__reset_sample_bit(evsel, ID);
193 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
194 } else {
195 perf_evsel__set_sample_bit(evsel, ID);
196 }
7a5a5ca5
ACM
197 evsel->attr.read_format |= PERF_FORMAT_ID;
198}
199
ef1d1af2
ACM
200void perf_evsel__init(struct perf_evsel *evsel,
201 struct perf_event_attr *attr, int idx)
202{
203 evsel->idx = idx;
60b0896c 204 evsel->tracking = !idx;
ef1d1af2 205 evsel->attr = *attr;
2cfda562 206 evsel->leader = evsel;
410136f5
SE
207 evsel->unit = "";
208 evsel->scale = 1.0;
ef1d1af2 209 INIT_LIST_HEAD(&evsel->node);
ce8ccff5 210 perf_evsel__object.init(evsel);
bde09467 211 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
75562573 212 perf_evsel__calc_id_pos(evsel);
15bfd2cc 213 evsel->cmdline_group_boundary = false;
ef1d1af2
ACM
214}
215
ef503831 216struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
69aad6f1 217{
ce8ccff5 218 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
69aad6f1 219
ef1d1af2
ACM
220 if (evsel != NULL)
221 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
222
223 return evsel;
224}
225
ef503831 226struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
efd2b924 227{
ce8ccff5 228 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
efd2b924
ACM
229
230 if (evsel != NULL) {
231 struct perf_event_attr attr = {
0b80f8b3
ACM
232 .type = PERF_TYPE_TRACEPOINT,
233 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
234 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
efd2b924
ACM
235 };
236
e48ffe2b
ACM
237 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
238 goto out_free;
239
97978b3e 240 evsel->tp_format = trace_event__tp_format(sys, name);
efd2b924
ACM
241 if (evsel->tp_format == NULL)
242 goto out_free;
243
0b80f8b3 244 event_attr_init(&attr);
efd2b924 245 attr.config = evsel->tp_format->id;
0b80f8b3 246 attr.sample_period = 1;
efd2b924 247 perf_evsel__init(evsel, &attr, idx);
efd2b924
ACM
248 }
249
250 return evsel;
251
252out_free:
74cf249d 253 zfree(&evsel->name);
efd2b924
ACM
254 free(evsel);
255 return NULL;
256}
257
8ad7013b 258const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
c410431c
ACM
259 "cycles",
260 "instructions",
261 "cache-references",
262 "cache-misses",
263 "branches",
264 "branch-misses",
265 "bus-cycles",
266 "stalled-cycles-frontend",
267 "stalled-cycles-backend",
268 "ref-cycles",
269};
270
dd4f5223 271static const char *__perf_evsel__hw_name(u64 config)
c410431c
ACM
272{
273 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
274 return perf_evsel__hw_names[config];
275
276 return "unknown-hardware";
277}
278
27f18617 279static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
c410431c 280{
27f18617 281 int colon = 0, r = 0;
c410431c 282 struct perf_event_attr *attr = &evsel->attr;
c410431c
ACM
283 bool exclude_guest_default = false;
284
285#define MOD_PRINT(context, mod) do { \
286 if (!attr->exclude_##context) { \
27f18617 287 if (!colon) colon = ++r; \
c410431c
ACM
288 r += scnprintf(bf + r, size - r, "%c", mod); \
289 } } while(0)
290
291 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
292 MOD_PRINT(kernel, 'k');
293 MOD_PRINT(user, 'u');
294 MOD_PRINT(hv, 'h');
295 exclude_guest_default = true;
296 }
297
298 if (attr->precise_ip) {
299 if (!colon)
27f18617 300 colon = ++r;
c410431c
ACM
301 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
302 exclude_guest_default = true;
303 }
304
305 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
306 MOD_PRINT(host, 'H');
307 MOD_PRINT(guest, 'G');
308 }
309#undef MOD_PRINT
310 if (colon)
27f18617 311 bf[colon - 1] = ':';
c410431c
ACM
312 return r;
313}
314
27f18617
ACM
315static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
316{
317 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
318 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
319}
320
8ad7013b 321const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
335c2f5d
ACM
322 "cpu-clock",
323 "task-clock",
324 "page-faults",
325 "context-switches",
8ad7013b 326 "cpu-migrations",
335c2f5d
ACM
327 "minor-faults",
328 "major-faults",
329 "alignment-faults",
330 "emulation-faults",
d22d1a2a 331 "dummy",
335c2f5d
ACM
332};
333
dd4f5223 334static const char *__perf_evsel__sw_name(u64 config)
335c2f5d
ACM
335{
336 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
337 return perf_evsel__sw_names[config];
338 return "unknown-software";
339}
340
341static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
342{
343 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
344 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
345}
346
287e74aa
JO
347static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
348{
349 int r;
350
351 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
352
353 if (type & HW_BREAKPOINT_R)
354 r += scnprintf(bf + r, size - r, "r");
355
356 if (type & HW_BREAKPOINT_W)
357 r += scnprintf(bf + r, size - r, "w");
358
359 if (type & HW_BREAKPOINT_X)
360 r += scnprintf(bf + r, size - r, "x");
361
362 return r;
363}
364
365static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
366{
367 struct perf_event_attr *attr = &evsel->attr;
368 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
369 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
370}
371
0b668bc9
ACM
372const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
373 [PERF_EVSEL__MAX_ALIASES] = {
374 { "L1-dcache", "l1-d", "l1d", "L1-data", },
375 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
376 { "LLC", "L2", },
377 { "dTLB", "d-tlb", "Data-TLB", },
378 { "iTLB", "i-tlb", "Instruction-TLB", },
379 { "branch", "branches", "bpu", "btb", "bpc", },
380 { "node", },
381};
382
383const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
384 [PERF_EVSEL__MAX_ALIASES] = {
385 { "load", "loads", "read", },
386 { "store", "stores", "write", },
387 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
388};
389
390const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
391 [PERF_EVSEL__MAX_ALIASES] = {
392 { "refs", "Reference", "ops", "access", },
393 { "misses", "miss", },
394};
395
396#define C(x) PERF_COUNT_HW_CACHE_##x
397#define CACHE_READ (1 << C(OP_READ))
398#define CACHE_WRITE (1 << C(OP_WRITE))
399#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
400#define COP(x) (1 << x)
401
402/*
403 * cache operartion stat
404 * L1I : Read and prefetch only
405 * ITLB and BPU : Read-only
406 */
407static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
408 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
409 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
410 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
411 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
412 [C(ITLB)] = (CACHE_READ),
413 [C(BPU)] = (CACHE_READ),
414 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
415};
416
417bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
418{
419 if (perf_evsel__hw_cache_stat[type] & COP(op))
420 return true; /* valid */
421 else
422 return false; /* invalid */
423}
424
425int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
426 char *bf, size_t size)
427{
428 if (result) {
429 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
430 perf_evsel__hw_cache_op[op][0],
431 perf_evsel__hw_cache_result[result][0]);
432 }
433
434 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
435 perf_evsel__hw_cache_op[op][1]);
436}
437
dd4f5223 438static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
0b668bc9
ACM
439{
440 u8 op, result, type = (config >> 0) & 0xff;
441 const char *err = "unknown-ext-hardware-cache-type";
442
443 if (type > PERF_COUNT_HW_CACHE_MAX)
444 goto out_err;
445
446 op = (config >> 8) & 0xff;
447 err = "unknown-ext-hardware-cache-op";
448 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
449 goto out_err;
450
451 result = (config >> 16) & 0xff;
452 err = "unknown-ext-hardware-cache-result";
453 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
454 goto out_err;
455
456 err = "invalid-cache";
457 if (!perf_evsel__is_cache_op_valid(type, op))
458 goto out_err;
459
460 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
461out_err:
462 return scnprintf(bf, size, "%s", err);
463}
464
465static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
466{
467 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
468 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
469}
470
6eef3d9c
ACM
471static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
472{
473 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
474 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
475}
476
7289f83c 477const char *perf_evsel__name(struct perf_evsel *evsel)
a4460836 478{
7289f83c 479 char bf[128];
a4460836 480
7289f83c
ACM
481 if (evsel->name)
482 return evsel->name;
c410431c
ACM
483
484 switch (evsel->attr.type) {
485 case PERF_TYPE_RAW:
6eef3d9c 486 perf_evsel__raw_name(evsel, bf, sizeof(bf));
c410431c
ACM
487 break;
488
489 case PERF_TYPE_HARDWARE:
7289f83c 490 perf_evsel__hw_name(evsel, bf, sizeof(bf));
c410431c 491 break;
0b668bc9
ACM
492
493 case PERF_TYPE_HW_CACHE:
7289f83c 494 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
0b668bc9
ACM
495 break;
496
335c2f5d 497 case PERF_TYPE_SOFTWARE:
7289f83c 498 perf_evsel__sw_name(evsel, bf, sizeof(bf));
335c2f5d
ACM
499 break;
500
a4460836 501 case PERF_TYPE_TRACEPOINT:
7289f83c 502 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
a4460836
ACM
503 break;
504
287e74aa
JO
505 case PERF_TYPE_BREAKPOINT:
506 perf_evsel__bp_name(evsel, bf, sizeof(bf));
507 break;
508
c410431c 509 default:
ca1b1457
RR
510 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
511 evsel->attr.type);
a4460836 512 break;
c410431c
ACM
513 }
514
7289f83c
ACM
515 evsel->name = strdup(bf);
516
517 return evsel->name ?: "unknown";
c410431c
ACM
518}
519
717e263f
NK
520const char *perf_evsel__group_name(struct perf_evsel *evsel)
521{
522 return evsel->group_name ?: "anon group";
523}
524
525int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
526{
527 int ret;
528 struct perf_evsel *pos;
529 const char *group_name = perf_evsel__group_name(evsel);
530
531 ret = scnprintf(buf, size, "%s", group_name);
532
533 ret += scnprintf(buf + ret, size - ret, " { %s",
534 perf_evsel__name(evsel));
535
536 for_each_group_member(pos, evsel)
537 ret += scnprintf(buf + ret, size - ret, ", %s",
538 perf_evsel__name(pos));
539
540 ret += scnprintf(buf + ret, size - ret, " }");
541
542 return ret;
543}
544
6bedfab6 545static void
aad2b21c
KL
546perf_evsel__config_callgraph(struct perf_evsel *evsel,
547 struct record_opts *opts)
6bedfab6
JO
548{
549 bool function = perf_evsel__is_function_event(evsel);
550 struct perf_event_attr *attr = &evsel->attr;
551
552 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
553
aad2b21c
KL
554 if (callchain_param.record_mode == CALLCHAIN_LBR) {
555 if (!opts->branch_stack) {
556 if (attr->exclude_user) {
557 pr_warning("LBR callstack option is only available "
558 "to get user callchain information. "
559 "Falling back to framepointers.\n");
560 } else {
561 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
562 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
563 PERF_SAMPLE_BRANCH_CALL_STACK;
564 }
565 } else
566 pr_warning("Cannot use LBR callstack with branch stack. "
567 "Falling back to framepointers.\n");
568 }
569
72a128aa 570 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
6bedfab6
JO
571 if (!function) {
572 perf_evsel__set_sample_bit(evsel, REGS_USER);
573 perf_evsel__set_sample_bit(evsel, STACK_USER);
574 attr->sample_regs_user = PERF_REGS_MASK;
72a128aa 575 attr->sample_stack_user = callchain_param.dump_size;
6bedfab6
JO
576 attr->exclude_callchain_user = 1;
577 } else {
578 pr_info("Cannot use DWARF unwind for function trace event,"
579 " falling back to framepointers.\n");
580 }
581 }
582
583 if (function) {
584 pr_info("Disabling user space callchains for function trace event.\n");
585 attr->exclude_callchain_user = 1;
586 }
587}
588
774cb499
JO
589/*
590 * The enable_on_exec/disabled value strategy:
591 *
592 * 1) For any type of traced program:
593 * - all independent events and group leaders are disabled
594 * - all group members are enabled
595 *
596 * Group members are ruled by group leaders. They need to
597 * be enabled, because the group scheduling relies on that.
598 *
599 * 2) For traced programs executed by perf:
600 * - all independent events and group leaders have
601 * enable_on_exec set
602 * - we don't specifically enable or disable any event during
603 * the record command
604 *
605 * Independent events and group leaders are initially disabled
606 * and get enabled by exec. Group members are ruled by group
607 * leaders as stated in 1).
608 *
609 * 3) For traced programs attached by perf (pid/tid):
610 * - we specifically enable or disable all events during
611 * the record command
612 *
613 * When attaching events to already running traced we
614 * enable/disable events specifically, as there's no
615 * initial traced exec call.
616 */
b4006796 617void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
0f82ebc4 618{
3c176311 619 struct perf_evsel *leader = evsel->leader;
0f82ebc4 620 struct perf_event_attr *attr = &evsel->attr;
60b0896c 621 int track = evsel->tracking;
3aa5939d 622 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
0f82ebc4 623
594ac61a 624 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
0f82ebc4 625 attr->inherit = !opts->no_inherit;
0f82ebc4 626
7be5ebe8
ACM
627 perf_evsel__set_sample_bit(evsel, IP);
628 perf_evsel__set_sample_bit(evsel, TID);
0f82ebc4 629
3c176311
JO
630 if (evsel->sample_read) {
631 perf_evsel__set_sample_bit(evsel, READ);
632
633 /*
634 * We need ID even in case of single event, because
635 * PERF_SAMPLE_READ process ID specific data.
636 */
75562573 637 perf_evsel__set_sample_id(evsel, false);
3c176311
JO
638
639 /*
640 * Apply group format only if we belong to group
641 * with more than one members.
642 */
643 if (leader->nr_members > 1) {
644 attr->read_format |= PERF_FORMAT_GROUP;
645 attr->inherit = 0;
646 }
647 }
648
0f82ebc4 649 /*
17314e23 650 * We default some events to have a default interval. But keep
0f82ebc4
ACM
651 * it a weak assumption overridable by the user.
652 */
17314e23 653 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
0f82ebc4
ACM
654 opts->user_interval != ULLONG_MAX)) {
655 if (opts->freq) {
7be5ebe8 656 perf_evsel__set_sample_bit(evsel, PERIOD);
0f82ebc4
ACM
657 attr->freq = 1;
658 attr->sample_freq = opts->freq;
659 } else {
660 attr->sample_period = opts->default_interval;
661 }
662 }
663
3c176311
JO
664 /*
665 * Disable sampling for all group members other
666 * than leader in case leader 'leads' the sampling.
667 */
668 if ((leader != evsel) && leader->sample_read) {
669 attr->sample_freq = 0;
670 attr->sample_period = 0;
671 }
672
0f82ebc4
ACM
673 if (opts->no_samples)
674 attr->sample_freq = 0;
675
676 if (opts->inherit_stat)
677 attr->inherit_stat = 1;
678
679 if (opts->sample_address) {
7be5ebe8 680 perf_evsel__set_sample_bit(evsel, ADDR);
0f82ebc4
ACM
681 attr->mmap_data = track;
682 }
683
f140373b
JO
684 /*
685 * We don't allow user space callchains for function trace
686 * event, due to issues with page faults while tracing page
687 * fault handler and its overall trickiness nature.
688 */
689 if (perf_evsel__is_function_event(evsel))
690 evsel->attr.exclude_callchain_user = 1;
691
72a128aa 692 if (callchain_param.enabled && !evsel->no_aux_samples)
aad2b21c 693 perf_evsel__config_callgraph(evsel, opts);
26d33022 694
6a21c0b5
SE
695 if (opts->sample_intr_regs) {
696 attr->sample_regs_intr = PERF_REGS_MASK;
697 perf_evsel__set_sample_bit(evsel, REGS_INTR);
698 }
699
3aa5939d 700 if (target__has_cpu(&opts->target))
7be5ebe8 701 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4 702
3e76ac78 703 if (opts->period)
7be5ebe8 704 perf_evsel__set_sample_bit(evsel, PERIOD);
3e76ac78 705
8affc2b8
AK
706 /*
707 * When the user explicitely disabled time don't force it here.
708 */
709 if (opts->sample_time &&
710 (!perf_missing_features.sample_id_all &&
3abebc55
AH
711 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
712 opts->sample_time_set)))
7be5ebe8 713 perf_evsel__set_sample_bit(evsel, TIME);
0f82ebc4 714
6ff1ce76 715 if (opts->raw_samples && !evsel->no_aux_samples) {
7be5ebe8
ACM
716 perf_evsel__set_sample_bit(evsel, TIME);
717 perf_evsel__set_sample_bit(evsel, RAW);
718 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4
ACM
719 }
720
ccf49bfc 721 if (opts->sample_address)
1e7ed5ec 722 perf_evsel__set_sample_bit(evsel, DATA_SRC);
ccf49bfc 723
509051ea 724 if (opts->no_buffering) {
0f82ebc4
ACM
725 attr->watermark = 0;
726 attr->wakeup_events = 1;
727 }
6ff1ce76 728 if (opts->branch_stack && !evsel->no_aux_samples) {
7be5ebe8 729 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
bdfebd84
RAV
730 attr->branch_sample_type = opts->branch_stack;
731 }
0f82ebc4 732
05484298 733 if (opts->sample_weight)
1e7ed5ec 734 perf_evsel__set_sample_bit(evsel, WEIGHT);
05484298 735
62e503b7 736 attr->task = track;
5c5e854b 737 attr->mmap = track;
a5a5ba72 738 attr->mmap2 = track && !perf_missing_features.mmap2;
5c5e854b 739 attr->comm = track;
0f82ebc4 740
475eeab9 741 if (opts->sample_transaction)
1e7ed5ec 742 perf_evsel__set_sample_bit(evsel, TRANSACTION);
475eeab9 743
85c273d2
AK
744 if (opts->running_time) {
745 evsel->attr.read_format |=
746 PERF_FORMAT_TOTAL_TIME_ENABLED |
747 PERF_FORMAT_TOTAL_TIME_RUNNING;
748 }
749
774cb499
JO
750 /*
751 * XXX see the function comment above
752 *
753 * Disabling only independent events or group leaders,
754 * keeping group members enabled.
755 */
823254ed 756 if (perf_evsel__is_group_leader(evsel))
774cb499
JO
757 attr->disabled = 1;
758
759 /*
760 * Setting enable_on_exec for independent events and
761 * group leaders for traced executed by perf.
762 */
6619a53e
AK
763 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
764 !opts->initial_delay)
0f82ebc4 765 attr->enable_on_exec = 1;
2afd2bcf
AH
766
767 if (evsel->immediate) {
768 attr->disabled = 0;
769 attr->enable_on_exec = 0;
770 }
814c8c38
PZ
771
772 clockid = opts->clockid;
773 if (opts->use_clockid) {
774 attr->use_clockid = 1;
775 attr->clockid = opts->clockid;
776 }
0f82ebc4
ACM
777}
778
8885846f 779static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
69aad6f1 780{
4af4c955 781 int cpu, thread;
bf8e8f4b
AH
782
783 if (evsel->system_wide)
784 nthreads = 1;
785
69aad6f1 786 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
787
788 if (evsel->fd) {
789 for (cpu = 0; cpu < ncpus; cpu++) {
790 for (thread = 0; thread < nthreads; thread++) {
791 FD(evsel, cpu, thread) = -1;
792 }
793 }
794 }
795
69aad6f1
ACM
796 return evsel->fd != NULL ? 0 : -ENOMEM;
797}
798
e2407bef
AK
799static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
800 int ioc, void *arg)
745cefc5
ACM
801{
802 int cpu, thread;
803
bf8e8f4b
AH
804 if (evsel->system_wide)
805 nthreads = 1;
806
745cefc5
ACM
807 for (cpu = 0; cpu < ncpus; cpu++) {
808 for (thread = 0; thread < nthreads; thread++) {
809 int fd = FD(evsel, cpu, thread),
e2407bef 810 err = ioctl(fd, ioc, arg);
745cefc5
ACM
811
812 if (err)
813 return err;
814 }
815 }
816
817 return 0;
818}
819
f47805a2
ACM
820int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
821 const char *filter)
e2407bef
AK
822{
823 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
824 PERF_EVENT_IOC_SET_FILTER,
825 (void *)filter);
826}
827
12467ae4
ACM
828int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
829{
830 char *new_filter = strdup(filter);
831
832 if (new_filter != NULL) {
833 free(evsel->filter);
834 evsel->filter = new_filter;
835 return 0;
836 }
837
838 return -1;
839}
840
64ec84f5
ACM
841int perf_evsel__append_filter(struct perf_evsel *evsel,
842 const char *op, const char *filter)
843{
844 char *new_filter;
845
846 if (evsel->filter == NULL)
847 return perf_evsel__set_filter(evsel, filter);
848
849 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
850 free(evsel->filter);
851 evsel->filter = new_filter;
852 return 0;
853 }
854
855 return -1;
856}
857
e2407bef
AK
858int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
859{
860 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
861 PERF_EVENT_IOC_ENABLE,
862 0);
863}
864
70db7533
ACM
865int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
866{
8d9cbd8f
VG
867 if (ncpus == 0 || nthreads == 0)
868 return 0;
869
bf8e8f4b
AH
870 if (evsel->system_wide)
871 nthreads = 1;
872
a91e5431
ACM
873 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
874 if (evsel->sample_id == NULL)
875 return -ENOMEM;
876
877 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
878 if (evsel->id == NULL) {
879 xyarray__delete(evsel->sample_id);
880 evsel->sample_id = NULL;
881 return -ENOMEM;
882 }
883
884 return 0;
70db7533
ACM
885}
886
8885846f 887static void perf_evsel__free_fd(struct perf_evsel *evsel)
69aad6f1
ACM
888{
889 xyarray__delete(evsel->fd);
890 evsel->fd = NULL;
891}
892
8885846f 893static void perf_evsel__free_id(struct perf_evsel *evsel)
70db7533 894{
a91e5431
ACM
895 xyarray__delete(evsel->sample_id);
896 evsel->sample_id = NULL;
04662523 897 zfree(&evsel->id);
70db7533
ACM
898}
899
c52b12ed
ACM
900void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
901{
902 int cpu, thread;
903
bf8e8f4b
AH
904 if (evsel->system_wide)
905 nthreads = 1;
906
c52b12ed
ACM
907 for (cpu = 0; cpu < ncpus; cpu++)
908 for (thread = 0; thread < nthreads; ++thread) {
909 close(FD(evsel, cpu, thread));
910 FD(evsel, cpu, thread) = -1;
911 }
912}
913
ef1d1af2 914void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
915{
916 assert(list_empty(&evsel->node));
736b05a0
NK
917 perf_evsel__free_fd(evsel);
918 perf_evsel__free_id(evsel);
597e48c1 919 close_cgroup(evsel->cgrp);
f30a79b0 920 cpu_map__put(evsel->cpus);
578e91ec 921 thread_map__put(evsel->threads);
597e48c1 922 zfree(&evsel->group_name);
597e48c1 923 zfree(&evsel->name);
ce8ccff5 924 perf_evsel__object.fini(evsel);
ef1d1af2
ACM
925}
926
927void perf_evsel__delete(struct perf_evsel *evsel)
928{
929 perf_evsel__exit(evsel);
69aad6f1
ACM
930 free(evsel);
931}
c52b12ed 932
a6fa0038 933void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
857a94a2 934 struct perf_counts_values *count)
c7a79c47
SE
935{
936 struct perf_counts_values tmp;
937
938 if (!evsel->prev_raw_counts)
939 return;
940
941 if (cpu == -1) {
942 tmp = evsel->prev_raw_counts->aggr;
943 evsel->prev_raw_counts->aggr = *count;
944 } else {
a6fa0038
JO
945 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
946 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
c7a79c47
SE
947 }
948
949 count->val = count->val - tmp.val;
950 count->ena = count->ena - tmp.ena;
951 count->run = count->run - tmp.run;
952}
953
13112bbf
JO
954void perf_counts_values__scale(struct perf_counts_values *count,
955 bool scale, s8 *pscaled)
956{
957 s8 scaled = 0;
958
959 if (scale) {
960 if (count->run == 0) {
961 scaled = -1;
962 count->val = 0;
963 } else if (count->run < count->ena) {
964 scaled = 1;
965 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
966 }
967 } else
968 count->ena = count->run = 0;
969
970 if (pscaled)
971 *pscaled = scaled;
972}
973
f99f4719
JO
974int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
975 struct perf_counts_values *count)
976{
977 memset(count, 0, sizeof(*count));
978
979 if (FD(evsel, cpu, thread) < 0)
980 return -EINVAL;
981
982 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
983 return -errno;
984
985 return 0;
986}
987
c52b12ed
ACM
988int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
989 int cpu, int thread, bool scale)
990{
991 struct perf_counts_values count;
992 size_t nv = scale ? 3 : 1;
993
994 if (FD(evsel, cpu, thread) < 0)
995 return -EINVAL;
996
a6fa0038 997 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
4eed11d5
ACM
998 return -ENOMEM;
999
c52b12ed
ACM
1000 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1001 return -errno;
1002
a6fa0038 1003 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
13112bbf 1004 perf_counts_values__scale(&count, scale, NULL);
a6fa0038 1005 *perf_counts(evsel->counts, cpu, thread) = count;
c52b12ed
ACM
1006 return 0;
1007}
1008
6a4bb04c
JO
1009static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1010{
1011 struct perf_evsel *leader = evsel->leader;
1012 int fd;
1013
823254ed 1014 if (perf_evsel__is_group_leader(evsel))
6a4bb04c
JO
1015 return -1;
1016
1017 /*
1018 * Leader must be already processed/open,
1019 * if not it's a bug.
1020 */
1021 BUG_ON(!leader->fd);
1022
1023 fd = FD(leader, cpu, thread);
1024 BUG_ON(fd == -1);
1025
1026 return fd;
1027}
1028
2c5e8c52
PZ
1029struct bit_names {
1030 int bit;
1031 const char *name;
1032};
1033
1034static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1035{
1036 bool first_bit = true;
1037 int i = 0;
1038
1039 do {
1040 if (value & bits[i].bit) {
1041 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1042 first_bit = false;
1043 }
1044 } while (bits[++i].name != NULL);
1045}
1046
1047static void __p_sample_type(char *buf, size_t size, u64 value)
1048{
1049#define bit_name(n) { PERF_SAMPLE_##n, #n }
1050 struct bit_names bits[] = {
1051 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1052 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1053 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1054 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1055 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1056 { .name = NULL, }
1057 };
1058#undef bit_name
1059 __p_bits(buf, size, value, bits);
1060}
1061
1062static void __p_read_format(char *buf, size_t size, u64 value)
1063{
1064#define bit_name(n) { PERF_FORMAT_##n, #n }
1065 struct bit_names bits[] = {
1066 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1067 bit_name(ID), bit_name(GROUP),
1068 { .name = NULL, }
1069 };
1070#undef bit_name
1071 __p_bits(buf, size, value, bits);
1072}
1073
1074#define BUF_SIZE 1024
1075
7310aed7 1076#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
2c5e8c52
PZ
1077#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1078#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1079#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1080#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1081
1082#define PRINT_ATTRn(_n, _f, _p) \
1083do { \
1084 if (attr->_f) { \
1085 _p(attr->_f); \
1086 ret += attr__fprintf(fp, _n, buf, priv);\
1087 } \
1088} while (0)
1089
1090#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1091
1092int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1093 attr__fprintf_f attr__fprintf, void *priv)
1094{
1095 char buf[BUF_SIZE];
1096 int ret = 0;
1097
1098 PRINT_ATTRf(type, p_unsigned);
1099 PRINT_ATTRf(size, p_unsigned);
1100 PRINT_ATTRf(config, p_hex);
1101 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1102 PRINT_ATTRf(sample_type, p_sample_type);
1103 PRINT_ATTRf(read_format, p_read_format);
1104
1105 PRINT_ATTRf(disabled, p_unsigned);
1106 PRINT_ATTRf(inherit, p_unsigned);
1107 PRINT_ATTRf(pinned, p_unsigned);
1108 PRINT_ATTRf(exclusive, p_unsigned);
1109 PRINT_ATTRf(exclude_user, p_unsigned);
1110 PRINT_ATTRf(exclude_kernel, p_unsigned);
1111 PRINT_ATTRf(exclude_hv, p_unsigned);
1112 PRINT_ATTRf(exclude_idle, p_unsigned);
1113 PRINT_ATTRf(mmap, p_unsigned);
1114 PRINT_ATTRf(comm, p_unsigned);
1115 PRINT_ATTRf(freq, p_unsigned);
1116 PRINT_ATTRf(inherit_stat, p_unsigned);
1117 PRINT_ATTRf(enable_on_exec, p_unsigned);
1118 PRINT_ATTRf(task, p_unsigned);
1119 PRINT_ATTRf(watermark, p_unsigned);
1120 PRINT_ATTRf(precise_ip, p_unsigned);
1121 PRINT_ATTRf(mmap_data, p_unsigned);
1122 PRINT_ATTRf(sample_id_all, p_unsigned);
1123 PRINT_ATTRf(exclude_host, p_unsigned);
1124 PRINT_ATTRf(exclude_guest, p_unsigned);
1125 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1126 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1127 PRINT_ATTRf(mmap2, p_unsigned);
1128 PRINT_ATTRf(comm_exec, p_unsigned);
1129 PRINT_ATTRf(use_clockid, p_unsigned);
0286039f 1130 PRINT_ATTRf(context_switch, p_unsigned);
2c5e8c52
PZ
1131
1132 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1133 PRINT_ATTRf(bp_type, p_unsigned);
1134 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1135 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1136 PRINT_ATTRf(sample_regs_user, p_hex);
1137 PRINT_ATTRf(sample_stack_user, p_unsigned);
1138 PRINT_ATTRf(clockid, p_signed);
1139 PRINT_ATTRf(sample_regs_intr, p_hex);
70d73de4 1140 PRINT_ATTRf(aux_watermark, p_unsigned);
e3e1a54f
AH
1141
1142 return ret;
1143}
1144
2c5e8c52
PZ
1145static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1146 void *priv __attribute__((unused)))
1147{
1148 return fprintf(fp, " %-32s %s\n", name, val);
1149}
1150
0252208e 1151static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 1152 struct thread_map *threads)
48290609 1153{
bf8e8f4b 1154 int cpu, thread, nthreads;
57480d2c 1155 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
727ab04e 1156 int pid = -1, err;
bec19672 1157 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
48290609 1158
bf8e8f4b
AH
1159 if (evsel->system_wide)
1160 nthreads = 1;
1161 else
1162 nthreads = threads->nr;
1163
0252208e 1164 if (evsel->fd == NULL &&
bf8e8f4b 1165 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
727ab04e 1166 return -ENOMEM;
4eed11d5 1167
023695d9 1168 if (evsel->cgrp) {
57480d2c 1169 flags |= PERF_FLAG_PID_CGROUP;
023695d9
SE
1170 pid = evsel->cgrp->fd;
1171 }
1172
594ac61a 1173fallback_missing_features:
814c8c38
PZ
1174 if (perf_missing_features.clockid_wrong)
1175 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1176 if (perf_missing_features.clockid) {
1177 evsel->attr.use_clockid = 0;
1178 evsel->attr.clockid = 0;
1179 }
57480d2c
YD
1180 if (perf_missing_features.cloexec)
1181 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
5c5e854b
SE
1182 if (perf_missing_features.mmap2)
1183 evsel->attr.mmap2 = 0;
594ac61a
ACM
1184 if (perf_missing_features.exclude_guest)
1185 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1186retry_sample_id:
1187 if (perf_missing_features.sample_id_all)
1188 evsel->attr.sample_id_all = 0;
1189
2c5e8c52
PZ
1190 if (verbose >= 2) {
1191 fprintf(stderr, "%.60s\n", graph_dotted_line);
1192 fprintf(stderr, "perf_event_attr:\n");
1193 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1194 fprintf(stderr, "%.60s\n", graph_dotted_line);
1195 }
e3e1a54f 1196
86bd5e86 1197 for (cpu = 0; cpu < cpus->nr; cpu++) {
9d04f178 1198
bf8e8f4b 1199 for (thread = 0; thread < nthreads; thread++) {
6a4bb04c 1200 int group_fd;
023695d9 1201
bf8e8f4b 1202 if (!evsel->cgrp && !evsel->system_wide)
e13798c7 1203 pid = thread_map__pid(threads, thread);
023695d9 1204
6a4bb04c 1205 group_fd = get_group_fd(evsel, cpu, thread);
bec19672 1206retry_open:
a33f6efc 1207 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
e3e1a54f
AH
1208 pid, cpus->map[cpu], group_fd, flags);
1209
0252208e 1210 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 1211 pid,
f08199d3 1212 cpus->map[cpu],
023695d9 1213 group_fd, flags);
727ab04e
ACM
1214 if (FD(evsel, cpu, thread) < 0) {
1215 err = -errno;
a33f6efc 1216 pr_debug2("sys_perf_event_open failed, error %d\n",
f852fd62 1217 err);
594ac61a 1218 goto try_fallback;
727ab04e 1219 }
bec19672 1220 set_rlimit = NO_CHANGE;
814c8c38
PZ
1221
1222 /*
1223 * If we succeeded but had to kill clockid, fail and
1224 * have perf_evsel__open_strerror() print us a nice
1225 * error.
1226 */
1227 if (perf_missing_features.clockid ||
1228 perf_missing_features.clockid_wrong) {
1229 err = -EINVAL;
1230 goto out_close;
1231 }
0252208e 1232 }
48290609
ACM
1233 }
1234
1235 return 0;
1236
594ac61a 1237try_fallback:
bec19672
AK
1238 /*
1239 * perf stat needs between 5 and 22 fds per CPU. When we run out
1240 * of them try to increase the limits.
1241 */
1242 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1243 struct rlimit l;
1244 int old_errno = errno;
1245
1246 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1247 if (set_rlimit == NO_CHANGE)
1248 l.rlim_cur = l.rlim_max;
1249 else {
1250 l.rlim_cur = l.rlim_max + 1000;
1251 l.rlim_max = l.rlim_cur;
1252 }
1253 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1254 set_rlimit++;
1255 errno = old_errno;
1256 goto retry_open;
1257 }
1258 }
1259 errno = old_errno;
1260 }
1261
594ac61a
ACM
1262 if (err != -EINVAL || cpu > 0 || thread > 0)
1263 goto out_close;
1264
814c8c38
PZ
1265 /*
1266 * Must probe features in the order they were added to the
1267 * perf_event_attr interface.
1268 */
1269 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1270 perf_missing_features.clockid_wrong = true;
1271 goto fallback_missing_features;
1272 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1273 perf_missing_features.clockid = true;
1274 goto fallback_missing_features;
1275 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
57480d2c
YD
1276 perf_missing_features.cloexec = true;
1277 goto fallback_missing_features;
1278 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
5c5e854b
SE
1279 perf_missing_features.mmap2 = true;
1280 goto fallback_missing_features;
1281 } else if (!perf_missing_features.exclude_guest &&
1282 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
594ac61a
ACM
1283 perf_missing_features.exclude_guest = true;
1284 goto fallback_missing_features;
1285 } else if (!perf_missing_features.sample_id_all) {
1286 perf_missing_features.sample_id_all = true;
1287 goto retry_sample_id;
1288 }
1289
48290609 1290out_close:
0252208e
ACM
1291 do {
1292 while (--thread >= 0) {
1293 close(FD(evsel, cpu, thread));
1294 FD(evsel, cpu, thread) = -1;
1295 }
bf8e8f4b 1296 thread = nthreads;
0252208e 1297 } while (--cpu >= 0);
727ab04e
ACM
1298 return err;
1299}
1300
1301void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1302{
1303 if (evsel->fd == NULL)
1304 return;
1305
1306 perf_evsel__close_fd(evsel, ncpus, nthreads);
1307 perf_evsel__free_fd(evsel);
48290609
ACM
1308}
1309
0252208e
ACM
1310static struct {
1311 struct cpu_map map;
1312 int cpus[1];
1313} empty_cpu_map = {
1314 .map.nr = 1,
1315 .cpus = { -1, },
1316};
1317
1318static struct {
1319 struct thread_map map;
1320 int threads[1];
1321} empty_thread_map = {
1322 .map.nr = 1,
1323 .threads = { -1, },
1324};
1325
f08199d3 1326int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 1327 struct thread_map *threads)
48290609 1328{
0252208e
ACM
1329 if (cpus == NULL) {
1330 /* Work around old compiler warnings about strict aliasing */
1331 cpus = &empty_cpu_map.map;
48290609
ACM
1332 }
1333
0252208e
ACM
1334 if (threads == NULL)
1335 threads = &empty_thread_map.map;
48290609 1336
6a4bb04c 1337 return __perf_evsel__open(evsel, cpus, threads);
48290609
ACM
1338}
1339
f08199d3 1340int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
6a4bb04c 1341 struct cpu_map *cpus)
48290609 1342{
6a4bb04c 1343 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
0252208e 1344}
48290609 1345
f08199d3 1346int perf_evsel__open_per_thread(struct perf_evsel *evsel,
6a4bb04c 1347 struct thread_map *threads)
0252208e 1348{
6a4bb04c 1349 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
48290609 1350}
70082dd9 1351
0807d2d8
ACM
1352static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1353 const union perf_event *event,
1354 struct perf_sample *sample)
d0dd74e8 1355{
0807d2d8 1356 u64 type = evsel->attr.sample_type;
d0dd74e8 1357 const u64 *array = event->sample.array;
0807d2d8 1358 bool swapped = evsel->needs_swap;
37073f9e 1359 union u64_swap u;
d0dd74e8
ACM
1360
1361 array += ((event->header.size -
1362 sizeof(event->header)) / sizeof(u64)) - 1;
1363
75562573
AH
1364 if (type & PERF_SAMPLE_IDENTIFIER) {
1365 sample->id = *array;
1366 array--;
1367 }
1368
d0dd74e8 1369 if (type & PERF_SAMPLE_CPU) {
37073f9e
JO
1370 u.val64 = *array;
1371 if (swapped) {
1372 /* undo swap of u64, then swap on individual u32s */
1373 u.val64 = bswap_64(u.val64);
1374 u.val32[0] = bswap_32(u.val32[0]);
1375 }
1376
1377 sample->cpu = u.val32[0];
d0dd74e8
ACM
1378 array--;
1379 }
1380
1381 if (type & PERF_SAMPLE_STREAM_ID) {
1382 sample->stream_id = *array;
1383 array--;
1384 }
1385
1386 if (type & PERF_SAMPLE_ID) {
1387 sample->id = *array;
1388 array--;
1389 }
1390
1391 if (type & PERF_SAMPLE_TIME) {
1392 sample->time = *array;
1393 array--;
1394 }
1395
1396 if (type & PERF_SAMPLE_TID) {
37073f9e
JO
1397 u.val64 = *array;
1398 if (swapped) {
1399 /* undo swap of u64, then swap on individual u32s */
1400 u.val64 = bswap_64(u.val64);
1401 u.val32[0] = bswap_32(u.val32[0]);
1402 u.val32[1] = bswap_32(u.val32[1]);
1403 }
1404
1405 sample->pid = u.val32[0];
1406 sample->tid = u.val32[1];
dd44bc6b 1407 array--;
d0dd74e8
ACM
1408 }
1409
1410 return 0;
1411}
1412
03b6ea9b
AH
1413static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1414 u64 size)
98e1da90 1415{
03b6ea9b
AH
1416 return size > max_size || offset + size > endp;
1417}
98e1da90 1418
03b6ea9b
AH
1419#define OVERFLOW_CHECK(offset, size, max_size) \
1420 do { \
1421 if (overflow(endp, (max_size), (offset), (size))) \
1422 return -EFAULT; \
1423 } while (0)
98e1da90 1424
03b6ea9b
AH
1425#define OVERFLOW_CHECK_u64(offset) \
1426 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
98e1da90 1427
a3f698fe 1428int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
0807d2d8 1429 struct perf_sample *data)
d0dd74e8 1430{
a3f698fe 1431 u64 type = evsel->attr.sample_type;
0807d2d8 1432 bool swapped = evsel->needs_swap;
d0dd74e8 1433 const u64 *array;
03b6ea9b
AH
1434 u16 max_size = event->header.size;
1435 const void *endp = (void *)event + max_size;
1436 u64 sz;
d0dd74e8 1437
936be503
DA
1438 /*
1439 * used for cross-endian analysis. See git commit 65014ab3
1440 * for why this goofiness is needed.
1441 */
6a11f92e 1442 union u64_swap u;
936be503 1443
f3bda2c9 1444 memset(data, 0, sizeof(*data));
d0dd74e8
ACM
1445 data->cpu = data->pid = data->tid = -1;
1446 data->stream_id = data->id = data->time = -1ULL;
bc529086 1447 data->period = evsel->attr.sample_period;
05484298 1448 data->weight = 0;
d0dd74e8
ACM
1449
1450 if (event->header.type != PERF_RECORD_SAMPLE) {
a3f698fe 1451 if (!evsel->attr.sample_id_all)
d0dd74e8 1452 return 0;
0807d2d8 1453 return perf_evsel__parse_id_sample(evsel, event, data);
d0dd74e8
ACM
1454 }
1455
1456 array = event->sample.array;
1457
03b6ea9b
AH
1458 /*
1459 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1460 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1461 * check the format does not go past the end of the event.
1462 */
a3f698fe 1463 if (evsel->sample_size + sizeof(event->header) > event->header.size)
a2854124
FW
1464 return -EFAULT;
1465
75562573
AH
1466 data->id = -1ULL;
1467 if (type & PERF_SAMPLE_IDENTIFIER) {
1468 data->id = *array;
1469 array++;
1470 }
1471
d0dd74e8 1472 if (type & PERF_SAMPLE_IP) {
ef89325f 1473 data->ip = *array;
d0dd74e8
ACM
1474 array++;
1475 }
1476
1477 if (type & PERF_SAMPLE_TID) {
936be503
DA
1478 u.val64 = *array;
1479 if (swapped) {
1480 /* undo swap of u64, then swap on individual u32s */
1481 u.val64 = bswap_64(u.val64);
1482 u.val32[0] = bswap_32(u.val32[0]);
1483 u.val32[1] = bswap_32(u.val32[1]);
1484 }
1485
1486 data->pid = u.val32[0];
1487 data->tid = u.val32[1];
d0dd74e8
ACM
1488 array++;
1489 }
1490
1491 if (type & PERF_SAMPLE_TIME) {
1492 data->time = *array;
1493 array++;
1494 }
1495
7cec0922 1496 data->addr = 0;
d0dd74e8
ACM
1497 if (type & PERF_SAMPLE_ADDR) {
1498 data->addr = *array;
1499 array++;
1500 }
1501
d0dd74e8
ACM
1502 if (type & PERF_SAMPLE_ID) {
1503 data->id = *array;
1504 array++;
1505 }
1506
1507 if (type & PERF_SAMPLE_STREAM_ID) {
1508 data->stream_id = *array;
1509 array++;
1510 }
1511
1512 if (type & PERF_SAMPLE_CPU) {
936be503
DA
1513
1514 u.val64 = *array;
1515 if (swapped) {
1516 /* undo swap of u64, then swap on individual u32s */
1517 u.val64 = bswap_64(u.val64);
1518 u.val32[0] = bswap_32(u.val32[0]);
1519 }
1520
1521 data->cpu = u.val32[0];
d0dd74e8
ACM
1522 array++;
1523 }
1524
1525 if (type & PERF_SAMPLE_PERIOD) {
1526 data->period = *array;
1527 array++;
1528 }
1529
1530 if (type & PERF_SAMPLE_READ) {
9ede473c
JO
1531 u64 read_format = evsel->attr.read_format;
1532
03b6ea9b 1533 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1534 if (read_format & PERF_FORMAT_GROUP)
1535 data->read.group.nr = *array;
1536 else
1537 data->read.one.value = *array;
1538
1539 array++;
1540
1541 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
03b6ea9b 1542 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1543 data->read.time_enabled = *array;
1544 array++;
1545 }
1546
1547 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
03b6ea9b 1548 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1549 data->read.time_running = *array;
1550 array++;
1551 }
1552
1553 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1554 if (read_format & PERF_FORMAT_GROUP) {
03b6ea9b
AH
1555 const u64 max_group_nr = UINT64_MAX /
1556 sizeof(struct sample_read_value);
1557
1558 if (data->read.group.nr > max_group_nr)
1559 return -EFAULT;
1560 sz = data->read.group.nr *
1561 sizeof(struct sample_read_value);
1562 OVERFLOW_CHECK(array, sz, max_size);
1563 data->read.group.values =
1564 (struct sample_read_value *)array;
1565 array = (void *)array + sz;
9ede473c 1566 } else {
03b6ea9b 1567 OVERFLOW_CHECK_u64(array);
9ede473c
JO
1568 data->read.one.id = *array;
1569 array++;
1570 }
d0dd74e8
ACM
1571 }
1572
1573 if (type & PERF_SAMPLE_CALLCHAIN) {
03b6ea9b 1574 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
98e1da90 1575
03b6ea9b
AH
1576 OVERFLOW_CHECK_u64(array);
1577 data->callchain = (struct ip_callchain *)array++;
1578 if (data->callchain->nr > max_callchain_nr)
98e1da90 1579 return -EFAULT;
03b6ea9b
AH
1580 sz = data->callchain->nr * sizeof(u64);
1581 OVERFLOW_CHECK(array, sz, max_size);
1582 array = (void *)array + sz;
d0dd74e8
ACM
1583 }
1584
1585 if (type & PERF_SAMPLE_RAW) {
03b6ea9b 1586 OVERFLOW_CHECK_u64(array);
936be503
DA
1587 u.val64 = *array;
1588 if (WARN_ONCE(swapped,
1589 "Endianness of raw data not corrected!\n")) {
1590 /* undo swap of u64, then swap on individual u32s */
1591 u.val64 = bswap_64(u.val64);
1592 u.val32[0] = bswap_32(u.val32[0]);
1593 u.val32[1] = bswap_32(u.val32[1]);
1594 }
936be503 1595 data->raw_size = u.val32[0];
03b6ea9b 1596 array = (void *)array + sizeof(u32);
98e1da90 1597
03b6ea9b
AH
1598 OVERFLOW_CHECK(array, data->raw_size, max_size);
1599 data->raw_data = (void *)array;
1600 array = (void *)array + data->raw_size;
d0dd74e8
ACM
1601 }
1602
b5387528 1603 if (type & PERF_SAMPLE_BRANCH_STACK) {
03b6ea9b
AH
1604 const u64 max_branch_nr = UINT64_MAX /
1605 sizeof(struct branch_entry);
b5387528 1606
03b6ea9b
AH
1607 OVERFLOW_CHECK_u64(array);
1608 data->branch_stack = (struct branch_stack *)array++;
b5387528 1609
03b6ea9b
AH
1610 if (data->branch_stack->nr > max_branch_nr)
1611 return -EFAULT;
b5387528 1612 sz = data->branch_stack->nr * sizeof(struct branch_entry);
03b6ea9b
AH
1613 OVERFLOW_CHECK(array, sz, max_size);
1614 array = (void *)array + sz;
b5387528 1615 }
0f6a3015
JO
1616
1617 if (type & PERF_SAMPLE_REGS_USER) {
03b6ea9b 1618 OVERFLOW_CHECK_u64(array);
5b95a4a3
AH
1619 data->user_regs.abi = *array;
1620 array++;
0f6a3015 1621
5b95a4a3 1622 if (data->user_regs.abi) {
352ea45a 1623 u64 mask = evsel->attr.sample_regs_user;
03b6ea9b 1624
352ea45a 1625 sz = hweight_long(mask) * sizeof(u64);
03b6ea9b 1626 OVERFLOW_CHECK(array, sz, max_size);
352ea45a 1627 data->user_regs.mask = mask;
0f6a3015 1628 data->user_regs.regs = (u64 *)array;
03b6ea9b 1629 array = (void *)array + sz;
0f6a3015
JO
1630 }
1631 }
1632
1633 if (type & PERF_SAMPLE_STACK_USER) {
03b6ea9b
AH
1634 OVERFLOW_CHECK_u64(array);
1635 sz = *array++;
0f6a3015
JO
1636
1637 data->user_stack.offset = ((char *)(array - 1)
1638 - (char *) event);
1639
03b6ea9b 1640 if (!sz) {
0f6a3015
JO
1641 data->user_stack.size = 0;
1642 } else {
03b6ea9b 1643 OVERFLOW_CHECK(array, sz, max_size);
0f6a3015 1644 data->user_stack.data = (char *)array;
03b6ea9b
AH
1645 array = (void *)array + sz;
1646 OVERFLOW_CHECK_u64(array);
54bd2692 1647 data->user_stack.size = *array++;
a65cb4b9
JO
1648 if (WARN_ONCE(data->user_stack.size > sz,
1649 "user stack dump failure\n"))
1650 return -EFAULT;
0f6a3015
JO
1651 }
1652 }
1653
05484298
AK
1654 data->weight = 0;
1655 if (type & PERF_SAMPLE_WEIGHT) {
03b6ea9b 1656 OVERFLOW_CHECK_u64(array);
05484298
AK
1657 data->weight = *array;
1658 array++;
1659 }
1660
98a3b32c
SE
1661 data->data_src = PERF_MEM_DATA_SRC_NONE;
1662 if (type & PERF_SAMPLE_DATA_SRC) {
03b6ea9b 1663 OVERFLOW_CHECK_u64(array);
98a3b32c
SE
1664 data->data_src = *array;
1665 array++;
1666 }
1667
475eeab9
AK
1668 data->transaction = 0;
1669 if (type & PERF_SAMPLE_TRANSACTION) {
87b95524 1670 OVERFLOW_CHECK_u64(array);
475eeab9
AK
1671 data->transaction = *array;
1672 array++;
1673 }
1674
6a21c0b5
SE
1675 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1676 if (type & PERF_SAMPLE_REGS_INTR) {
1677 OVERFLOW_CHECK_u64(array);
1678 data->intr_regs.abi = *array;
1679 array++;
1680
1681 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1682 u64 mask = evsel->attr.sample_regs_intr;
1683
1684 sz = hweight_long(mask) * sizeof(u64);
1685 OVERFLOW_CHECK(array, sz, max_size);
1686 data->intr_regs.mask = mask;
1687 data->intr_regs.regs = (u64 *)array;
1688 array = (void *)array + sz;
1689 }
1690 }
1691
d0dd74e8
ACM
1692 return 0;
1693}
74eec26f 1694
b1cf6f65 1695size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
352ea45a 1696 u64 read_format)
b1cf6f65
AH
1697{
1698 size_t sz, result = sizeof(struct sample_event);
1699
1700 if (type & PERF_SAMPLE_IDENTIFIER)
1701 result += sizeof(u64);
1702
1703 if (type & PERF_SAMPLE_IP)
1704 result += sizeof(u64);
1705
1706 if (type & PERF_SAMPLE_TID)
1707 result += sizeof(u64);
1708
1709 if (type & PERF_SAMPLE_TIME)
1710 result += sizeof(u64);
1711
1712 if (type & PERF_SAMPLE_ADDR)
1713 result += sizeof(u64);
1714
1715 if (type & PERF_SAMPLE_ID)
1716 result += sizeof(u64);
1717
1718 if (type & PERF_SAMPLE_STREAM_ID)
1719 result += sizeof(u64);
1720
1721 if (type & PERF_SAMPLE_CPU)
1722 result += sizeof(u64);
1723
1724 if (type & PERF_SAMPLE_PERIOD)
1725 result += sizeof(u64);
1726
1727 if (type & PERF_SAMPLE_READ) {
1728 result += sizeof(u64);
1729 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1730 result += sizeof(u64);
1731 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1732 result += sizeof(u64);
1733 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1734 if (read_format & PERF_FORMAT_GROUP) {
1735 sz = sample->read.group.nr *
1736 sizeof(struct sample_read_value);
1737 result += sz;
1738 } else {
1739 result += sizeof(u64);
1740 }
1741 }
1742
1743 if (type & PERF_SAMPLE_CALLCHAIN) {
1744 sz = (sample->callchain->nr + 1) * sizeof(u64);
1745 result += sz;
1746 }
1747
1748 if (type & PERF_SAMPLE_RAW) {
1749 result += sizeof(u32);
1750 result += sample->raw_size;
1751 }
1752
1753 if (type & PERF_SAMPLE_BRANCH_STACK) {
1754 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1755 sz += sizeof(u64);
1756 result += sz;
1757 }
1758
1759 if (type & PERF_SAMPLE_REGS_USER) {
1760 if (sample->user_regs.abi) {
1761 result += sizeof(u64);
352ea45a 1762 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
b1cf6f65
AH
1763 result += sz;
1764 } else {
1765 result += sizeof(u64);
1766 }
1767 }
1768
1769 if (type & PERF_SAMPLE_STACK_USER) {
1770 sz = sample->user_stack.size;
1771 result += sizeof(u64);
1772 if (sz) {
1773 result += sz;
1774 result += sizeof(u64);
1775 }
1776 }
1777
1778 if (type & PERF_SAMPLE_WEIGHT)
1779 result += sizeof(u64);
1780
1781 if (type & PERF_SAMPLE_DATA_SRC)
1782 result += sizeof(u64);
1783
42d88910
AH
1784 if (type & PERF_SAMPLE_TRANSACTION)
1785 result += sizeof(u64);
1786
6a21c0b5
SE
1787 if (type & PERF_SAMPLE_REGS_INTR) {
1788 if (sample->intr_regs.abi) {
1789 result += sizeof(u64);
1790 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1791 result += sz;
1792 } else {
1793 result += sizeof(u64);
1794 }
1795 }
1796
b1cf6f65
AH
1797 return result;
1798}
1799
74eec26f 1800int perf_event__synthesize_sample(union perf_event *event, u64 type,
352ea45a 1801 u64 read_format,
74eec26f
AV
1802 const struct perf_sample *sample,
1803 bool swapped)
1804{
1805 u64 *array;
d03f2170 1806 size_t sz;
74eec26f
AV
1807 /*
1808 * used for cross-endian analysis. See git commit 65014ab3
1809 * for why this goofiness is needed.
1810 */
6a11f92e 1811 union u64_swap u;
74eec26f
AV
1812
1813 array = event->sample.array;
1814
75562573
AH
1815 if (type & PERF_SAMPLE_IDENTIFIER) {
1816 *array = sample->id;
1817 array++;
1818 }
1819
74eec26f 1820 if (type & PERF_SAMPLE_IP) {
ef89325f 1821 *array = sample->ip;
74eec26f
AV
1822 array++;
1823 }
1824
1825 if (type & PERF_SAMPLE_TID) {
1826 u.val32[0] = sample->pid;
1827 u.val32[1] = sample->tid;
1828 if (swapped) {
1829 /*
a3f698fe 1830 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1831 */
1832 u.val32[0] = bswap_32(u.val32[0]);
1833 u.val32[1] = bswap_32(u.val32[1]);
1834 u.val64 = bswap_64(u.val64);
1835 }
1836
1837 *array = u.val64;
1838 array++;
1839 }
1840
1841 if (type & PERF_SAMPLE_TIME) {
1842 *array = sample->time;
1843 array++;
1844 }
1845
1846 if (type & PERF_SAMPLE_ADDR) {
1847 *array = sample->addr;
1848 array++;
1849 }
1850
1851 if (type & PERF_SAMPLE_ID) {
1852 *array = sample->id;
1853 array++;
1854 }
1855
1856 if (type & PERF_SAMPLE_STREAM_ID) {
1857 *array = sample->stream_id;
1858 array++;
1859 }
1860
1861 if (type & PERF_SAMPLE_CPU) {
1862 u.val32[0] = sample->cpu;
1863 if (swapped) {
1864 /*
a3f698fe 1865 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1866 */
1867 u.val32[0] = bswap_32(u.val32[0]);
1868 u.val64 = bswap_64(u.val64);
1869 }
1870 *array = u.val64;
1871 array++;
1872 }
1873
1874 if (type & PERF_SAMPLE_PERIOD) {
1875 *array = sample->period;
1876 array++;
1877 }
1878
d03f2170
AH
1879 if (type & PERF_SAMPLE_READ) {
1880 if (read_format & PERF_FORMAT_GROUP)
1881 *array = sample->read.group.nr;
1882 else
1883 *array = sample->read.one.value;
1884 array++;
1885
1886 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1887 *array = sample->read.time_enabled;
1888 array++;
1889 }
1890
1891 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1892 *array = sample->read.time_running;
1893 array++;
1894 }
1895
1896 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1897 if (read_format & PERF_FORMAT_GROUP) {
1898 sz = sample->read.group.nr *
1899 sizeof(struct sample_read_value);
1900 memcpy(array, sample->read.group.values, sz);
1901 array = (void *)array + sz;
1902 } else {
1903 *array = sample->read.one.id;
1904 array++;
1905 }
1906 }
1907
1908 if (type & PERF_SAMPLE_CALLCHAIN) {
1909 sz = (sample->callchain->nr + 1) * sizeof(u64);
1910 memcpy(array, sample->callchain, sz);
1911 array = (void *)array + sz;
1912 }
1913
1914 if (type & PERF_SAMPLE_RAW) {
1915 u.val32[0] = sample->raw_size;
1916 if (WARN_ONCE(swapped,
1917 "Endianness of raw data not corrected!\n")) {
1918 /*
1919 * Inverse of what is done in perf_evsel__parse_sample
1920 */
1921 u.val32[0] = bswap_32(u.val32[0]);
1922 u.val32[1] = bswap_32(u.val32[1]);
1923 u.val64 = bswap_64(u.val64);
1924 }
1925 *array = u.val64;
1926 array = (void *)array + sizeof(u32);
1927
1928 memcpy(array, sample->raw_data, sample->raw_size);
1929 array = (void *)array + sample->raw_size;
1930 }
1931
1932 if (type & PERF_SAMPLE_BRANCH_STACK) {
1933 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1934 sz += sizeof(u64);
1935 memcpy(array, sample->branch_stack, sz);
1936 array = (void *)array + sz;
1937 }
1938
1939 if (type & PERF_SAMPLE_REGS_USER) {
1940 if (sample->user_regs.abi) {
1941 *array++ = sample->user_regs.abi;
352ea45a 1942 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
d03f2170
AH
1943 memcpy(array, sample->user_regs.regs, sz);
1944 array = (void *)array + sz;
1945 } else {
1946 *array++ = 0;
1947 }
1948 }
1949
1950 if (type & PERF_SAMPLE_STACK_USER) {
1951 sz = sample->user_stack.size;
1952 *array++ = sz;
1953 if (sz) {
1954 memcpy(array, sample->user_stack.data, sz);
1955 array = (void *)array + sz;
1956 *array++ = sz;
1957 }
1958 }
1959
1960 if (type & PERF_SAMPLE_WEIGHT) {
1961 *array = sample->weight;
1962 array++;
1963 }
1964
1965 if (type & PERF_SAMPLE_DATA_SRC) {
1966 *array = sample->data_src;
1967 array++;
1968 }
1969
42d88910
AH
1970 if (type & PERF_SAMPLE_TRANSACTION) {
1971 *array = sample->transaction;
1972 array++;
1973 }
1974
6a21c0b5
SE
1975 if (type & PERF_SAMPLE_REGS_INTR) {
1976 if (sample->intr_regs.abi) {
1977 *array++ = sample->intr_regs.abi;
1978 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1979 memcpy(array, sample->intr_regs.regs, sz);
1980 array = (void *)array + sz;
1981 } else {
1982 *array++ = 0;
1983 }
1984 }
1985
74eec26f
AV
1986 return 0;
1987}
5555ded4 1988
efd2b924
ACM
1989struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1990{
1991 return pevent_find_field(evsel->tp_format, name);
1992}
1993
5d2074ea 1994void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
5555ded4
ACM
1995 const char *name)
1996{
efd2b924 1997 struct format_field *field = perf_evsel__field(evsel, name);
5555ded4
ACM
1998 int offset;
1999
efd2b924
ACM
2000 if (!field)
2001 return NULL;
5555ded4
ACM
2002
2003 offset = field->offset;
2004
2005 if (field->flags & FIELD_IS_DYNAMIC) {
2006 offset = *(int *)(sample->raw_data + field->offset);
2007 offset &= 0xffff;
2008 }
2009
2010 return sample->raw_data + offset;
2011}
2012
2013u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2014 const char *name)
2015{
efd2b924 2016 struct format_field *field = perf_evsel__field(evsel, name);
e6b6f679
ACM
2017 void *ptr;
2018 u64 value;
5555ded4 2019
efd2b924
ACM
2020 if (!field)
2021 return 0;
5555ded4 2022
e6b6f679 2023 ptr = sample->raw_data + field->offset;
5555ded4 2024
e6b6f679
ACM
2025 switch (field->size) {
2026 case 1:
2027 return *(u8 *)ptr;
2028 case 2:
2029 value = *(u16 *)ptr;
2030 break;
2031 case 4:
2032 value = *(u32 *)ptr;
2033 break;
2034 case 8:
e94eedab 2035 memcpy(&value, ptr, sizeof(u64));
e6b6f679
ACM
2036 break;
2037 default:
2038 return 0;
2039 }
2040
2041 if (!evsel->needs_swap)
2042 return value;
2043
2044 switch (field->size) {
2045 case 2:
2046 return bswap_16(value);
2047 case 4:
2048 return bswap_32(value);
2049 case 8:
2050 return bswap_64(value);
2051 default:
2052 return 0;
2053 }
2054
2055 return 0;
5555ded4 2056}
0698aedd
ACM
2057
2058static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2059{
2060 va_list args;
2061 int ret = 0;
2062
2063 if (!*first) {
2064 ret += fprintf(fp, ",");
2065 } else {
2066 ret += fprintf(fp, ":");
2067 *first = false;
2068 }
2069
2070 va_start(args, fmt);
2071 ret += vfprintf(fp, fmt, args);
2072 va_end(args);
2073 return ret;
2074}
2075
2c5e8c52 2076static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
c79a4393 2077{
2c5e8c52 2078 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
c79a4393
ACM
2079}
2080
0698aedd
ACM
2081int perf_evsel__fprintf(struct perf_evsel *evsel,
2082 struct perf_attr_details *details, FILE *fp)
2083{
2084 bool first = true;
e6ab07d0
NK
2085 int printed = 0;
2086
e35ef355 2087 if (details->event_group) {
e6ab07d0
NK
2088 struct perf_evsel *pos;
2089
2090 if (!perf_evsel__is_group_leader(evsel))
2091 return 0;
2092
2093 if (evsel->nr_members > 1)
2094 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2095
2096 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2097 for_each_group_member(pos, evsel)
2098 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2099
2100 if (evsel->nr_members > 1)
2101 printed += fprintf(fp, "}");
2102 goto out;
2103 }
2104
2105 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
0698aedd 2106
2c5e8c52
PZ
2107 if (details->verbose) {
2108 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2109 __print_attr__fprintf, &first);
2110 } else if (details->freq) {
0698aedd
ACM
2111 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2112 (u64)evsel->attr.sample_freq);
2113 }
e6ab07d0 2114out:
0698aedd
ACM
2115 fputc('\n', fp);
2116 return ++printed;
2117}
c0a54341
ACM
2118
2119bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2120 char *msg, size_t msgsize)
2121{
2b821cce 2122 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
c0a54341
ACM
2123 evsel->attr.type == PERF_TYPE_HARDWARE &&
2124 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2125 /*
2126 * If it's cycles then fall back to hrtimer based
2127 * cpu-clock-tick sw counter, which is always available even if
2128 * no PMU support.
2129 *
2130 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2131 * b0a873e).
2132 */
2133 scnprintf(msg, msgsize, "%s",
2134"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2135
2136 evsel->attr.type = PERF_TYPE_SOFTWARE;
2137 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2138
04662523 2139 zfree(&evsel->name);
c0a54341
ACM
2140 return true;
2141 }
2142
2143 return false;
2144}
56e52e85 2145
602ad878 2146int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
56e52e85
ACM
2147 int err, char *msg, size_t size)
2148{
6e81c74c
MH
2149 char sbuf[STRERR_BUFSIZE];
2150
56e52e85
ACM
2151 switch (err) {
2152 case EPERM:
2153 case EACCES:
b69e63a4 2154 return scnprintf(msg, size,
56e52e85
ACM
2155 "You may not have permission to collect %sstats.\n"
2156 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2157 " -1 - Not paranoid at all\n"
2158 " 0 - Disallow raw tracepoint access for unpriv\n"
2159 " 1 - Disallow cpu events for unpriv\n"
2160 " 2 - Disallow kernel profiling for unpriv",
2161 target->system_wide ? "system-wide " : "");
2162 case ENOENT:
2163 return scnprintf(msg, size, "The %s event is not supported.",
2164 perf_evsel__name(evsel));
2165 case EMFILE:
2166 return scnprintf(msg, size, "%s",
2167 "Too many events are opened.\n"
18ffdfe8
JO
2168 "Probably the maximum number of open file descriptors has been reached.\n"
2169 "Hint: Try again after reducing the number of events.\n"
2170 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
56e52e85
ACM
2171 case ENODEV:
2172 if (target->cpu_list)
2173 return scnprintf(msg, size, "%s",
2174 "No such device - did you specify an out-of-range profile CPU?\n");
2175 break;
2176 case EOPNOTSUPP:
2177 if (evsel->attr.precise_ip)
2178 return scnprintf(msg, size, "%s",
2179 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2180#if defined(__i386__) || defined(__x86_64__)
2181 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2182 return scnprintf(msg, size, "%s",
2183 "No hardware sampling interrupt available.\n"
2184 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2185#endif
2186 break;
63914aca
JO
2187 case EBUSY:
2188 if (find_process("oprofiled"))
2189 return scnprintf(msg, size,
2190 "The PMU counters are busy/taken by another profiler.\n"
2191 "We found oprofile daemon running, please stop it and try again.");
2192 break;
814c8c38
PZ
2193 case EINVAL:
2194 if (perf_missing_features.clockid)
2195 return scnprintf(msg, size, "clockid feature not supported.");
2196 if (perf_missing_features.clockid_wrong)
2197 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2198 break;
56e52e85
ACM
2199 default:
2200 break;
2201 }
2202
2203 return scnprintf(msg, size,
6e81c74c 2204 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
56e52e85
ACM
2205 "/bin/dmesg may provide additional information.\n"
2206 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
6e81c74c
MH
2207 err, strerror_r(err, sbuf, sizeof(sbuf)),
2208 perf_evsel__name(evsel));
56e52e85 2209}