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