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