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