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