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