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