perf tools: Add empty Build files for architectures lacking them
[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 apply_config_terms(struct perf_evsel *evsel)
592 {
593 struct perf_evsel_config_term *term;
594 struct list_head *config_terms = &evsel->config_terms;
595 struct perf_event_attr *attr = &evsel->attr;
596
597 list_for_each_entry(term, config_terms, list) {
598 switch (term->type) {
599 case PERF_EVSEL__CONFIG_TERM_PERIOD:
600 attr->sample_period = term->val.period;
601 break;
602 case PERF_EVSEL__CONFIG_TERM_TIME:
603 if (term->val.time)
604 perf_evsel__set_sample_bit(evsel, TIME);
605 else
606 perf_evsel__reset_sample_bit(evsel, TIME);
607 break;
608 default:
609 break;
610 }
611 }
612 }
613
614 /*
615 * The enable_on_exec/disabled value strategy:
616 *
617 * 1) For any type of traced program:
618 * - all independent events and group leaders are disabled
619 * - all group members are enabled
620 *
621 * Group members are ruled by group leaders. They need to
622 * be enabled, because the group scheduling relies on that.
623 *
624 * 2) For traced programs executed by perf:
625 * - all independent events and group leaders have
626 * enable_on_exec set
627 * - we don't specifically enable or disable any event during
628 * the record command
629 *
630 * Independent events and group leaders are initially disabled
631 * and get enabled by exec. Group members are ruled by group
632 * leaders as stated in 1).
633 *
634 * 3) For traced programs attached by perf (pid/tid):
635 * - we specifically enable or disable all events during
636 * the record command
637 *
638 * When attaching events to already running traced we
639 * enable/disable events specifically, as there's no
640 * initial traced exec call.
641 */
642 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
643 {
644 struct perf_evsel *leader = evsel->leader;
645 struct perf_event_attr *attr = &evsel->attr;
646 int track = evsel->tracking;
647 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
648
649 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
650 attr->inherit = !opts->no_inherit;
651
652 perf_evsel__set_sample_bit(evsel, IP);
653 perf_evsel__set_sample_bit(evsel, TID);
654
655 if (evsel->sample_read) {
656 perf_evsel__set_sample_bit(evsel, READ);
657
658 /*
659 * We need ID even in case of single event, because
660 * PERF_SAMPLE_READ process ID specific data.
661 */
662 perf_evsel__set_sample_id(evsel, false);
663
664 /*
665 * Apply group format only if we belong to group
666 * with more than one members.
667 */
668 if (leader->nr_members > 1) {
669 attr->read_format |= PERF_FORMAT_GROUP;
670 attr->inherit = 0;
671 }
672 }
673
674 /*
675 * We default some events to have a default interval. But keep
676 * it a weak assumption overridable by the user.
677 */
678 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
679 opts->user_interval != ULLONG_MAX)) {
680 if (opts->freq) {
681 perf_evsel__set_sample_bit(evsel, PERIOD);
682 attr->freq = 1;
683 attr->sample_freq = opts->freq;
684 } else {
685 attr->sample_period = opts->default_interval;
686 }
687 }
688
689 /*
690 * Disable sampling for all group members other
691 * than leader in case leader 'leads' the sampling.
692 */
693 if ((leader != evsel) && leader->sample_read) {
694 attr->sample_freq = 0;
695 attr->sample_period = 0;
696 }
697
698 if (opts->no_samples)
699 attr->sample_freq = 0;
700
701 if (opts->inherit_stat)
702 attr->inherit_stat = 1;
703
704 if (opts->sample_address) {
705 perf_evsel__set_sample_bit(evsel, ADDR);
706 attr->mmap_data = track;
707 }
708
709 /*
710 * We don't allow user space callchains for function trace
711 * event, due to issues with page faults while tracing page
712 * fault handler and its overall trickiness nature.
713 */
714 if (perf_evsel__is_function_event(evsel))
715 evsel->attr.exclude_callchain_user = 1;
716
717 if (callchain_param.enabled && !evsel->no_aux_samples)
718 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
719
720 if (opts->sample_intr_regs) {
721 attr->sample_regs_intr = PERF_REGS_MASK;
722 perf_evsel__set_sample_bit(evsel, REGS_INTR);
723 }
724
725 if (target__has_cpu(&opts->target))
726 perf_evsel__set_sample_bit(evsel, CPU);
727
728 if (opts->period)
729 perf_evsel__set_sample_bit(evsel, PERIOD);
730
731 /*
732 * When the user explicitely disabled time don't force it here.
733 */
734 if (opts->sample_time &&
735 (!perf_missing_features.sample_id_all &&
736 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
737 opts->sample_time_set)))
738 perf_evsel__set_sample_bit(evsel, TIME);
739
740 if (opts->raw_samples && !evsel->no_aux_samples) {
741 perf_evsel__set_sample_bit(evsel, TIME);
742 perf_evsel__set_sample_bit(evsel, RAW);
743 perf_evsel__set_sample_bit(evsel, CPU);
744 }
745
746 if (opts->sample_address)
747 perf_evsel__set_sample_bit(evsel, DATA_SRC);
748
749 if (opts->no_buffering) {
750 attr->watermark = 0;
751 attr->wakeup_events = 1;
752 }
753 if (opts->branch_stack && !evsel->no_aux_samples) {
754 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
755 attr->branch_sample_type = opts->branch_stack;
756 }
757
758 if (opts->sample_weight)
759 perf_evsel__set_sample_bit(evsel, WEIGHT);
760
761 attr->task = track;
762 attr->mmap = track;
763 attr->mmap2 = track && !perf_missing_features.mmap2;
764 attr->comm = track;
765
766 if (opts->record_switch_events)
767 attr->context_switch = track;
768
769 if (opts->sample_transaction)
770 perf_evsel__set_sample_bit(evsel, TRANSACTION);
771
772 if (opts->running_time) {
773 evsel->attr.read_format |=
774 PERF_FORMAT_TOTAL_TIME_ENABLED |
775 PERF_FORMAT_TOTAL_TIME_RUNNING;
776 }
777
778 /*
779 * XXX see the function comment above
780 *
781 * Disabling only independent events or group leaders,
782 * keeping group members enabled.
783 */
784 if (perf_evsel__is_group_leader(evsel))
785 attr->disabled = 1;
786
787 /*
788 * Setting enable_on_exec for independent events and
789 * group leaders for traced executed by perf.
790 */
791 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
792 !opts->initial_delay)
793 attr->enable_on_exec = 1;
794
795 if (evsel->immediate) {
796 attr->disabled = 0;
797 attr->enable_on_exec = 0;
798 }
799
800 clockid = opts->clockid;
801 if (opts->use_clockid) {
802 attr->use_clockid = 1;
803 attr->clockid = opts->clockid;
804 }
805
806 /*
807 * Apply event specific term settings,
808 * it overloads any global configuration.
809 */
810 apply_config_terms(evsel);
811 }
812
813 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
814 {
815 int cpu, thread;
816
817 if (evsel->system_wide)
818 nthreads = 1;
819
820 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
821
822 if (evsel->fd) {
823 for (cpu = 0; cpu < ncpus; cpu++) {
824 for (thread = 0; thread < nthreads; thread++) {
825 FD(evsel, cpu, thread) = -1;
826 }
827 }
828 }
829
830 return evsel->fd != NULL ? 0 : -ENOMEM;
831 }
832
833 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
834 int ioc, void *arg)
835 {
836 int cpu, thread;
837
838 if (evsel->system_wide)
839 nthreads = 1;
840
841 for (cpu = 0; cpu < ncpus; cpu++) {
842 for (thread = 0; thread < nthreads; thread++) {
843 int fd = FD(evsel, cpu, thread),
844 err = ioctl(fd, ioc, arg);
845
846 if (err)
847 return err;
848 }
849 }
850
851 return 0;
852 }
853
854 int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
855 const char *filter)
856 {
857 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
858 PERF_EVENT_IOC_SET_FILTER,
859 (void *)filter);
860 }
861
862 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
863 {
864 char *new_filter = strdup(filter);
865
866 if (new_filter != NULL) {
867 free(evsel->filter);
868 evsel->filter = new_filter;
869 return 0;
870 }
871
872 return -1;
873 }
874
875 int perf_evsel__append_filter(struct perf_evsel *evsel,
876 const char *op, const char *filter)
877 {
878 char *new_filter;
879
880 if (evsel->filter == NULL)
881 return perf_evsel__set_filter(evsel, filter);
882
883 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
884 free(evsel->filter);
885 evsel->filter = new_filter;
886 return 0;
887 }
888
889 return -1;
890 }
891
892 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
893 {
894 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
895 PERF_EVENT_IOC_ENABLE,
896 0);
897 }
898
899 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
900 {
901 if (ncpus == 0 || nthreads == 0)
902 return 0;
903
904 if (evsel->system_wide)
905 nthreads = 1;
906
907 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
908 if (evsel->sample_id == NULL)
909 return -ENOMEM;
910
911 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
912 if (evsel->id == NULL) {
913 xyarray__delete(evsel->sample_id);
914 evsel->sample_id = NULL;
915 return -ENOMEM;
916 }
917
918 return 0;
919 }
920
921 static void perf_evsel__free_fd(struct perf_evsel *evsel)
922 {
923 xyarray__delete(evsel->fd);
924 evsel->fd = NULL;
925 }
926
927 static void perf_evsel__free_id(struct perf_evsel *evsel)
928 {
929 xyarray__delete(evsel->sample_id);
930 evsel->sample_id = NULL;
931 zfree(&evsel->id);
932 }
933
934 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
935 {
936 struct perf_evsel_config_term *term, *h;
937
938 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
939 list_del(&term->list);
940 free(term);
941 }
942 }
943
944 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
945 {
946 int cpu, thread;
947
948 if (evsel->system_wide)
949 nthreads = 1;
950
951 for (cpu = 0; cpu < ncpus; cpu++)
952 for (thread = 0; thread < nthreads; ++thread) {
953 close(FD(evsel, cpu, thread));
954 FD(evsel, cpu, thread) = -1;
955 }
956 }
957
958 void perf_evsel__exit(struct perf_evsel *evsel)
959 {
960 assert(list_empty(&evsel->node));
961 perf_evsel__free_fd(evsel);
962 perf_evsel__free_id(evsel);
963 perf_evsel__free_config_terms(evsel);
964 close_cgroup(evsel->cgrp);
965 cpu_map__put(evsel->cpus);
966 thread_map__put(evsel->threads);
967 zfree(&evsel->group_name);
968 zfree(&evsel->name);
969 perf_evsel__object.fini(evsel);
970 }
971
972 void perf_evsel__delete(struct perf_evsel *evsel)
973 {
974 perf_evsel__exit(evsel);
975 free(evsel);
976 }
977
978 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
979 struct perf_counts_values *count)
980 {
981 struct perf_counts_values tmp;
982
983 if (!evsel->prev_raw_counts)
984 return;
985
986 if (cpu == -1) {
987 tmp = evsel->prev_raw_counts->aggr;
988 evsel->prev_raw_counts->aggr = *count;
989 } else {
990 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
991 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
992 }
993
994 count->val = count->val - tmp.val;
995 count->ena = count->ena - tmp.ena;
996 count->run = count->run - tmp.run;
997 }
998
999 void perf_counts_values__scale(struct perf_counts_values *count,
1000 bool scale, s8 *pscaled)
1001 {
1002 s8 scaled = 0;
1003
1004 if (scale) {
1005 if (count->run == 0) {
1006 scaled = -1;
1007 count->val = 0;
1008 } else if (count->run < count->ena) {
1009 scaled = 1;
1010 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1011 }
1012 } else
1013 count->ena = count->run = 0;
1014
1015 if (pscaled)
1016 *pscaled = scaled;
1017 }
1018
1019 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1020 struct perf_counts_values *count)
1021 {
1022 memset(count, 0, sizeof(*count));
1023
1024 if (FD(evsel, cpu, thread) < 0)
1025 return -EINVAL;
1026
1027 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1028 return -errno;
1029
1030 return 0;
1031 }
1032
1033 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1034 int cpu, int thread, bool scale)
1035 {
1036 struct perf_counts_values count;
1037 size_t nv = scale ? 3 : 1;
1038
1039 if (FD(evsel, cpu, thread) < 0)
1040 return -EINVAL;
1041
1042 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
1043 return -ENOMEM;
1044
1045 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1046 return -errno;
1047
1048 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
1049 perf_counts_values__scale(&count, scale, NULL);
1050 *perf_counts(evsel->counts, cpu, thread) = count;
1051 return 0;
1052 }
1053
1054 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1055 {
1056 struct perf_evsel *leader = evsel->leader;
1057 int fd;
1058
1059 if (perf_evsel__is_group_leader(evsel))
1060 return -1;
1061
1062 /*
1063 * Leader must be already processed/open,
1064 * if not it's a bug.
1065 */
1066 BUG_ON(!leader->fd);
1067
1068 fd = FD(leader, cpu, thread);
1069 BUG_ON(fd == -1);
1070
1071 return fd;
1072 }
1073
1074 struct bit_names {
1075 int bit;
1076 const char *name;
1077 };
1078
1079 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
1080 {
1081 bool first_bit = true;
1082 int i = 0;
1083
1084 do {
1085 if (value & bits[i].bit) {
1086 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1087 first_bit = false;
1088 }
1089 } while (bits[++i].name != NULL);
1090 }
1091
1092 static void __p_sample_type(char *buf, size_t size, u64 value)
1093 {
1094 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1095 struct bit_names bits[] = {
1096 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1097 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1098 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1099 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1100 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1101 { .name = NULL, }
1102 };
1103 #undef bit_name
1104 __p_bits(buf, size, value, bits);
1105 }
1106
1107 static void __p_read_format(char *buf, size_t size, u64 value)
1108 {
1109 #define bit_name(n) { PERF_FORMAT_##n, #n }
1110 struct bit_names bits[] = {
1111 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1112 bit_name(ID), bit_name(GROUP),
1113 { .name = NULL, }
1114 };
1115 #undef bit_name
1116 __p_bits(buf, size, value, bits);
1117 }
1118
1119 #define BUF_SIZE 1024
1120
1121 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1122 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1123 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1124 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1125 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1126
1127 #define PRINT_ATTRn(_n, _f, _p) \
1128 do { \
1129 if (attr->_f) { \
1130 _p(attr->_f); \
1131 ret += attr__fprintf(fp, _n, buf, priv);\
1132 } \
1133 } while (0)
1134
1135 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1136
1137 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1138 attr__fprintf_f attr__fprintf, void *priv)
1139 {
1140 char buf[BUF_SIZE];
1141 int ret = 0;
1142
1143 PRINT_ATTRf(type, p_unsigned);
1144 PRINT_ATTRf(size, p_unsigned);
1145 PRINT_ATTRf(config, p_hex);
1146 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1147 PRINT_ATTRf(sample_type, p_sample_type);
1148 PRINT_ATTRf(read_format, p_read_format);
1149
1150 PRINT_ATTRf(disabled, p_unsigned);
1151 PRINT_ATTRf(inherit, p_unsigned);
1152 PRINT_ATTRf(pinned, p_unsigned);
1153 PRINT_ATTRf(exclusive, p_unsigned);
1154 PRINT_ATTRf(exclude_user, p_unsigned);
1155 PRINT_ATTRf(exclude_kernel, p_unsigned);
1156 PRINT_ATTRf(exclude_hv, p_unsigned);
1157 PRINT_ATTRf(exclude_idle, p_unsigned);
1158 PRINT_ATTRf(mmap, p_unsigned);
1159 PRINT_ATTRf(comm, p_unsigned);
1160 PRINT_ATTRf(freq, p_unsigned);
1161 PRINT_ATTRf(inherit_stat, p_unsigned);
1162 PRINT_ATTRf(enable_on_exec, p_unsigned);
1163 PRINT_ATTRf(task, p_unsigned);
1164 PRINT_ATTRf(watermark, p_unsigned);
1165 PRINT_ATTRf(precise_ip, p_unsigned);
1166 PRINT_ATTRf(mmap_data, p_unsigned);
1167 PRINT_ATTRf(sample_id_all, p_unsigned);
1168 PRINT_ATTRf(exclude_host, p_unsigned);
1169 PRINT_ATTRf(exclude_guest, p_unsigned);
1170 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1171 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1172 PRINT_ATTRf(mmap2, p_unsigned);
1173 PRINT_ATTRf(comm_exec, p_unsigned);
1174 PRINT_ATTRf(use_clockid, p_unsigned);
1175 PRINT_ATTRf(context_switch, p_unsigned);
1176
1177 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1178 PRINT_ATTRf(bp_type, p_unsigned);
1179 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1180 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1181 PRINT_ATTRf(sample_regs_user, p_hex);
1182 PRINT_ATTRf(sample_stack_user, p_unsigned);
1183 PRINT_ATTRf(clockid, p_signed);
1184 PRINT_ATTRf(sample_regs_intr, p_hex);
1185 PRINT_ATTRf(aux_watermark, p_unsigned);
1186
1187 return ret;
1188 }
1189
1190 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1191 void *priv __attribute__((unused)))
1192 {
1193 return fprintf(fp, " %-32s %s\n", name, val);
1194 }
1195
1196 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1197 struct thread_map *threads)
1198 {
1199 int cpu, thread, nthreads;
1200 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1201 int pid = -1, err;
1202 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1203
1204 if (evsel->system_wide)
1205 nthreads = 1;
1206 else
1207 nthreads = threads->nr;
1208
1209 if (evsel->fd == NULL &&
1210 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1211 return -ENOMEM;
1212
1213 if (evsel->cgrp) {
1214 flags |= PERF_FLAG_PID_CGROUP;
1215 pid = evsel->cgrp->fd;
1216 }
1217
1218 fallback_missing_features:
1219 if (perf_missing_features.clockid_wrong)
1220 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1221 if (perf_missing_features.clockid) {
1222 evsel->attr.use_clockid = 0;
1223 evsel->attr.clockid = 0;
1224 }
1225 if (perf_missing_features.cloexec)
1226 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1227 if (perf_missing_features.mmap2)
1228 evsel->attr.mmap2 = 0;
1229 if (perf_missing_features.exclude_guest)
1230 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1231 retry_sample_id:
1232 if (perf_missing_features.sample_id_all)
1233 evsel->attr.sample_id_all = 0;
1234
1235 if (verbose >= 2) {
1236 fprintf(stderr, "%.60s\n", graph_dotted_line);
1237 fprintf(stderr, "perf_event_attr:\n");
1238 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1239 fprintf(stderr, "%.60s\n", graph_dotted_line);
1240 }
1241
1242 for (cpu = 0; cpu < cpus->nr; cpu++) {
1243
1244 for (thread = 0; thread < nthreads; thread++) {
1245 int group_fd;
1246
1247 if (!evsel->cgrp && !evsel->system_wide)
1248 pid = thread_map__pid(threads, thread);
1249
1250 group_fd = get_group_fd(evsel, cpu, thread);
1251 retry_open:
1252 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1253 pid, cpus->map[cpu], group_fd, flags);
1254
1255 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1256 pid,
1257 cpus->map[cpu],
1258 group_fd, flags);
1259 if (FD(evsel, cpu, thread) < 0) {
1260 err = -errno;
1261 pr_debug2("sys_perf_event_open failed, error %d\n",
1262 err);
1263 goto try_fallback;
1264 }
1265 set_rlimit = NO_CHANGE;
1266
1267 /*
1268 * If we succeeded but had to kill clockid, fail and
1269 * have perf_evsel__open_strerror() print us a nice
1270 * error.
1271 */
1272 if (perf_missing_features.clockid ||
1273 perf_missing_features.clockid_wrong) {
1274 err = -EINVAL;
1275 goto out_close;
1276 }
1277 }
1278 }
1279
1280 return 0;
1281
1282 try_fallback:
1283 /*
1284 * perf stat needs between 5 and 22 fds per CPU. When we run out
1285 * of them try to increase the limits.
1286 */
1287 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1288 struct rlimit l;
1289 int old_errno = errno;
1290
1291 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1292 if (set_rlimit == NO_CHANGE)
1293 l.rlim_cur = l.rlim_max;
1294 else {
1295 l.rlim_cur = l.rlim_max + 1000;
1296 l.rlim_max = l.rlim_cur;
1297 }
1298 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1299 set_rlimit++;
1300 errno = old_errno;
1301 goto retry_open;
1302 }
1303 }
1304 errno = old_errno;
1305 }
1306
1307 if (err != -EINVAL || cpu > 0 || thread > 0)
1308 goto out_close;
1309
1310 /*
1311 * Must probe features in the order they were added to the
1312 * perf_event_attr interface.
1313 */
1314 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1315 perf_missing_features.clockid_wrong = true;
1316 goto fallback_missing_features;
1317 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1318 perf_missing_features.clockid = true;
1319 goto fallback_missing_features;
1320 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1321 perf_missing_features.cloexec = true;
1322 goto fallback_missing_features;
1323 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1324 perf_missing_features.mmap2 = true;
1325 goto fallback_missing_features;
1326 } else if (!perf_missing_features.exclude_guest &&
1327 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1328 perf_missing_features.exclude_guest = true;
1329 goto fallback_missing_features;
1330 } else if (!perf_missing_features.sample_id_all) {
1331 perf_missing_features.sample_id_all = true;
1332 goto retry_sample_id;
1333 }
1334
1335 out_close:
1336 do {
1337 while (--thread >= 0) {
1338 close(FD(evsel, cpu, thread));
1339 FD(evsel, cpu, thread) = -1;
1340 }
1341 thread = nthreads;
1342 } while (--cpu >= 0);
1343 return err;
1344 }
1345
1346 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1347 {
1348 if (evsel->fd == NULL)
1349 return;
1350
1351 perf_evsel__close_fd(evsel, ncpus, nthreads);
1352 perf_evsel__free_fd(evsel);
1353 }
1354
1355 static struct {
1356 struct cpu_map map;
1357 int cpus[1];
1358 } empty_cpu_map = {
1359 .map.nr = 1,
1360 .cpus = { -1, },
1361 };
1362
1363 static struct {
1364 struct thread_map map;
1365 int threads[1];
1366 } empty_thread_map = {
1367 .map.nr = 1,
1368 .threads = { -1, },
1369 };
1370
1371 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1372 struct thread_map *threads)
1373 {
1374 if (cpus == NULL) {
1375 /* Work around old compiler warnings about strict aliasing */
1376 cpus = &empty_cpu_map.map;
1377 }
1378
1379 if (threads == NULL)
1380 threads = &empty_thread_map.map;
1381
1382 return __perf_evsel__open(evsel, cpus, threads);
1383 }
1384
1385 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1386 struct cpu_map *cpus)
1387 {
1388 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1389 }
1390
1391 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1392 struct thread_map *threads)
1393 {
1394 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1395 }
1396
1397 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1398 const union perf_event *event,
1399 struct perf_sample *sample)
1400 {
1401 u64 type = evsel->attr.sample_type;
1402 const u64 *array = event->sample.array;
1403 bool swapped = evsel->needs_swap;
1404 union u64_swap u;
1405
1406 array += ((event->header.size -
1407 sizeof(event->header)) / sizeof(u64)) - 1;
1408
1409 if (type & PERF_SAMPLE_IDENTIFIER) {
1410 sample->id = *array;
1411 array--;
1412 }
1413
1414 if (type & PERF_SAMPLE_CPU) {
1415 u.val64 = *array;
1416 if (swapped) {
1417 /* undo swap of u64, then swap on individual u32s */
1418 u.val64 = bswap_64(u.val64);
1419 u.val32[0] = bswap_32(u.val32[0]);
1420 }
1421
1422 sample->cpu = u.val32[0];
1423 array--;
1424 }
1425
1426 if (type & PERF_SAMPLE_STREAM_ID) {
1427 sample->stream_id = *array;
1428 array--;
1429 }
1430
1431 if (type & PERF_SAMPLE_ID) {
1432 sample->id = *array;
1433 array--;
1434 }
1435
1436 if (type & PERF_SAMPLE_TIME) {
1437 sample->time = *array;
1438 array--;
1439 }
1440
1441 if (type & PERF_SAMPLE_TID) {
1442 u.val64 = *array;
1443 if (swapped) {
1444 /* undo swap of u64, then swap on individual u32s */
1445 u.val64 = bswap_64(u.val64);
1446 u.val32[0] = bswap_32(u.val32[0]);
1447 u.val32[1] = bswap_32(u.val32[1]);
1448 }
1449
1450 sample->pid = u.val32[0];
1451 sample->tid = u.val32[1];
1452 array--;
1453 }
1454
1455 return 0;
1456 }
1457
1458 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1459 u64 size)
1460 {
1461 return size > max_size || offset + size > endp;
1462 }
1463
1464 #define OVERFLOW_CHECK(offset, size, max_size) \
1465 do { \
1466 if (overflow(endp, (max_size), (offset), (size))) \
1467 return -EFAULT; \
1468 } while (0)
1469
1470 #define OVERFLOW_CHECK_u64(offset) \
1471 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1472
1473 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1474 struct perf_sample *data)
1475 {
1476 u64 type = evsel->attr.sample_type;
1477 bool swapped = evsel->needs_swap;
1478 const u64 *array;
1479 u16 max_size = event->header.size;
1480 const void *endp = (void *)event + max_size;
1481 u64 sz;
1482
1483 /*
1484 * used for cross-endian analysis. See git commit 65014ab3
1485 * for why this goofiness is needed.
1486 */
1487 union u64_swap u;
1488
1489 memset(data, 0, sizeof(*data));
1490 data->cpu = data->pid = data->tid = -1;
1491 data->stream_id = data->id = data->time = -1ULL;
1492 data->period = evsel->attr.sample_period;
1493 data->weight = 0;
1494
1495 if (event->header.type != PERF_RECORD_SAMPLE) {
1496 if (!evsel->attr.sample_id_all)
1497 return 0;
1498 return perf_evsel__parse_id_sample(evsel, event, data);
1499 }
1500
1501 array = event->sample.array;
1502
1503 /*
1504 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1505 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1506 * check the format does not go past the end of the event.
1507 */
1508 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1509 return -EFAULT;
1510
1511 data->id = -1ULL;
1512 if (type & PERF_SAMPLE_IDENTIFIER) {
1513 data->id = *array;
1514 array++;
1515 }
1516
1517 if (type & PERF_SAMPLE_IP) {
1518 data->ip = *array;
1519 array++;
1520 }
1521
1522 if (type & PERF_SAMPLE_TID) {
1523 u.val64 = *array;
1524 if (swapped) {
1525 /* undo swap of u64, then swap on individual u32s */
1526 u.val64 = bswap_64(u.val64);
1527 u.val32[0] = bswap_32(u.val32[0]);
1528 u.val32[1] = bswap_32(u.val32[1]);
1529 }
1530
1531 data->pid = u.val32[0];
1532 data->tid = u.val32[1];
1533 array++;
1534 }
1535
1536 if (type & PERF_SAMPLE_TIME) {
1537 data->time = *array;
1538 array++;
1539 }
1540
1541 data->addr = 0;
1542 if (type & PERF_SAMPLE_ADDR) {
1543 data->addr = *array;
1544 array++;
1545 }
1546
1547 if (type & PERF_SAMPLE_ID) {
1548 data->id = *array;
1549 array++;
1550 }
1551
1552 if (type & PERF_SAMPLE_STREAM_ID) {
1553 data->stream_id = *array;
1554 array++;
1555 }
1556
1557 if (type & PERF_SAMPLE_CPU) {
1558
1559 u.val64 = *array;
1560 if (swapped) {
1561 /* undo swap of u64, then swap on individual u32s */
1562 u.val64 = bswap_64(u.val64);
1563 u.val32[0] = bswap_32(u.val32[0]);
1564 }
1565
1566 data->cpu = u.val32[0];
1567 array++;
1568 }
1569
1570 if (type & PERF_SAMPLE_PERIOD) {
1571 data->period = *array;
1572 array++;
1573 }
1574
1575 if (type & PERF_SAMPLE_READ) {
1576 u64 read_format = evsel->attr.read_format;
1577
1578 OVERFLOW_CHECK_u64(array);
1579 if (read_format & PERF_FORMAT_GROUP)
1580 data->read.group.nr = *array;
1581 else
1582 data->read.one.value = *array;
1583
1584 array++;
1585
1586 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1587 OVERFLOW_CHECK_u64(array);
1588 data->read.time_enabled = *array;
1589 array++;
1590 }
1591
1592 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1593 OVERFLOW_CHECK_u64(array);
1594 data->read.time_running = *array;
1595 array++;
1596 }
1597
1598 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1599 if (read_format & PERF_FORMAT_GROUP) {
1600 const u64 max_group_nr = UINT64_MAX /
1601 sizeof(struct sample_read_value);
1602
1603 if (data->read.group.nr > max_group_nr)
1604 return -EFAULT;
1605 sz = data->read.group.nr *
1606 sizeof(struct sample_read_value);
1607 OVERFLOW_CHECK(array, sz, max_size);
1608 data->read.group.values =
1609 (struct sample_read_value *)array;
1610 array = (void *)array + sz;
1611 } else {
1612 OVERFLOW_CHECK_u64(array);
1613 data->read.one.id = *array;
1614 array++;
1615 }
1616 }
1617
1618 if (type & PERF_SAMPLE_CALLCHAIN) {
1619 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1620
1621 OVERFLOW_CHECK_u64(array);
1622 data->callchain = (struct ip_callchain *)array++;
1623 if (data->callchain->nr > max_callchain_nr)
1624 return -EFAULT;
1625 sz = data->callchain->nr * sizeof(u64);
1626 OVERFLOW_CHECK(array, sz, max_size);
1627 array = (void *)array + sz;
1628 }
1629
1630 if (type & PERF_SAMPLE_RAW) {
1631 OVERFLOW_CHECK_u64(array);
1632 u.val64 = *array;
1633 if (WARN_ONCE(swapped,
1634 "Endianness of raw data not corrected!\n")) {
1635 /* undo swap of u64, then swap on individual u32s */
1636 u.val64 = bswap_64(u.val64);
1637 u.val32[0] = bswap_32(u.val32[0]);
1638 u.val32[1] = bswap_32(u.val32[1]);
1639 }
1640 data->raw_size = u.val32[0];
1641 array = (void *)array + sizeof(u32);
1642
1643 OVERFLOW_CHECK(array, data->raw_size, max_size);
1644 data->raw_data = (void *)array;
1645 array = (void *)array + data->raw_size;
1646 }
1647
1648 if (type & PERF_SAMPLE_BRANCH_STACK) {
1649 const u64 max_branch_nr = UINT64_MAX /
1650 sizeof(struct branch_entry);
1651
1652 OVERFLOW_CHECK_u64(array);
1653 data->branch_stack = (struct branch_stack *)array++;
1654
1655 if (data->branch_stack->nr > max_branch_nr)
1656 return -EFAULT;
1657 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1658 OVERFLOW_CHECK(array, sz, max_size);
1659 array = (void *)array + sz;
1660 }
1661
1662 if (type & PERF_SAMPLE_REGS_USER) {
1663 OVERFLOW_CHECK_u64(array);
1664 data->user_regs.abi = *array;
1665 array++;
1666
1667 if (data->user_regs.abi) {
1668 u64 mask = evsel->attr.sample_regs_user;
1669
1670 sz = hweight_long(mask) * sizeof(u64);
1671 OVERFLOW_CHECK(array, sz, max_size);
1672 data->user_regs.mask = mask;
1673 data->user_regs.regs = (u64 *)array;
1674 array = (void *)array + sz;
1675 }
1676 }
1677
1678 if (type & PERF_SAMPLE_STACK_USER) {
1679 OVERFLOW_CHECK_u64(array);
1680 sz = *array++;
1681
1682 data->user_stack.offset = ((char *)(array - 1)
1683 - (char *) event);
1684
1685 if (!sz) {
1686 data->user_stack.size = 0;
1687 } else {
1688 OVERFLOW_CHECK(array, sz, max_size);
1689 data->user_stack.data = (char *)array;
1690 array = (void *)array + sz;
1691 OVERFLOW_CHECK_u64(array);
1692 data->user_stack.size = *array++;
1693 if (WARN_ONCE(data->user_stack.size > sz,
1694 "user stack dump failure\n"))
1695 return -EFAULT;
1696 }
1697 }
1698
1699 data->weight = 0;
1700 if (type & PERF_SAMPLE_WEIGHT) {
1701 OVERFLOW_CHECK_u64(array);
1702 data->weight = *array;
1703 array++;
1704 }
1705
1706 data->data_src = PERF_MEM_DATA_SRC_NONE;
1707 if (type & PERF_SAMPLE_DATA_SRC) {
1708 OVERFLOW_CHECK_u64(array);
1709 data->data_src = *array;
1710 array++;
1711 }
1712
1713 data->transaction = 0;
1714 if (type & PERF_SAMPLE_TRANSACTION) {
1715 OVERFLOW_CHECK_u64(array);
1716 data->transaction = *array;
1717 array++;
1718 }
1719
1720 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1721 if (type & PERF_SAMPLE_REGS_INTR) {
1722 OVERFLOW_CHECK_u64(array);
1723 data->intr_regs.abi = *array;
1724 array++;
1725
1726 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1727 u64 mask = evsel->attr.sample_regs_intr;
1728
1729 sz = hweight_long(mask) * sizeof(u64);
1730 OVERFLOW_CHECK(array, sz, max_size);
1731 data->intr_regs.mask = mask;
1732 data->intr_regs.regs = (u64 *)array;
1733 array = (void *)array + sz;
1734 }
1735 }
1736
1737 return 0;
1738 }
1739
1740 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1741 u64 read_format)
1742 {
1743 size_t sz, result = sizeof(struct sample_event);
1744
1745 if (type & PERF_SAMPLE_IDENTIFIER)
1746 result += sizeof(u64);
1747
1748 if (type & PERF_SAMPLE_IP)
1749 result += sizeof(u64);
1750
1751 if (type & PERF_SAMPLE_TID)
1752 result += sizeof(u64);
1753
1754 if (type & PERF_SAMPLE_TIME)
1755 result += sizeof(u64);
1756
1757 if (type & PERF_SAMPLE_ADDR)
1758 result += sizeof(u64);
1759
1760 if (type & PERF_SAMPLE_ID)
1761 result += sizeof(u64);
1762
1763 if (type & PERF_SAMPLE_STREAM_ID)
1764 result += sizeof(u64);
1765
1766 if (type & PERF_SAMPLE_CPU)
1767 result += sizeof(u64);
1768
1769 if (type & PERF_SAMPLE_PERIOD)
1770 result += sizeof(u64);
1771
1772 if (type & PERF_SAMPLE_READ) {
1773 result += sizeof(u64);
1774 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1775 result += sizeof(u64);
1776 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1777 result += sizeof(u64);
1778 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1779 if (read_format & PERF_FORMAT_GROUP) {
1780 sz = sample->read.group.nr *
1781 sizeof(struct sample_read_value);
1782 result += sz;
1783 } else {
1784 result += sizeof(u64);
1785 }
1786 }
1787
1788 if (type & PERF_SAMPLE_CALLCHAIN) {
1789 sz = (sample->callchain->nr + 1) * sizeof(u64);
1790 result += sz;
1791 }
1792
1793 if (type & PERF_SAMPLE_RAW) {
1794 result += sizeof(u32);
1795 result += sample->raw_size;
1796 }
1797
1798 if (type & PERF_SAMPLE_BRANCH_STACK) {
1799 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1800 sz += sizeof(u64);
1801 result += sz;
1802 }
1803
1804 if (type & PERF_SAMPLE_REGS_USER) {
1805 if (sample->user_regs.abi) {
1806 result += sizeof(u64);
1807 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1808 result += sz;
1809 } else {
1810 result += sizeof(u64);
1811 }
1812 }
1813
1814 if (type & PERF_SAMPLE_STACK_USER) {
1815 sz = sample->user_stack.size;
1816 result += sizeof(u64);
1817 if (sz) {
1818 result += sz;
1819 result += sizeof(u64);
1820 }
1821 }
1822
1823 if (type & PERF_SAMPLE_WEIGHT)
1824 result += sizeof(u64);
1825
1826 if (type & PERF_SAMPLE_DATA_SRC)
1827 result += sizeof(u64);
1828
1829 if (type & PERF_SAMPLE_TRANSACTION)
1830 result += sizeof(u64);
1831
1832 if (type & PERF_SAMPLE_REGS_INTR) {
1833 if (sample->intr_regs.abi) {
1834 result += sizeof(u64);
1835 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1836 result += sz;
1837 } else {
1838 result += sizeof(u64);
1839 }
1840 }
1841
1842 return result;
1843 }
1844
1845 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1846 u64 read_format,
1847 const struct perf_sample *sample,
1848 bool swapped)
1849 {
1850 u64 *array;
1851 size_t sz;
1852 /*
1853 * used for cross-endian analysis. See git commit 65014ab3
1854 * for why this goofiness is needed.
1855 */
1856 union u64_swap u;
1857
1858 array = event->sample.array;
1859
1860 if (type & PERF_SAMPLE_IDENTIFIER) {
1861 *array = sample->id;
1862 array++;
1863 }
1864
1865 if (type & PERF_SAMPLE_IP) {
1866 *array = sample->ip;
1867 array++;
1868 }
1869
1870 if (type & PERF_SAMPLE_TID) {
1871 u.val32[0] = sample->pid;
1872 u.val32[1] = sample->tid;
1873 if (swapped) {
1874 /*
1875 * Inverse of what is done in perf_evsel__parse_sample
1876 */
1877 u.val32[0] = bswap_32(u.val32[0]);
1878 u.val32[1] = bswap_32(u.val32[1]);
1879 u.val64 = bswap_64(u.val64);
1880 }
1881
1882 *array = u.val64;
1883 array++;
1884 }
1885
1886 if (type & PERF_SAMPLE_TIME) {
1887 *array = sample->time;
1888 array++;
1889 }
1890
1891 if (type & PERF_SAMPLE_ADDR) {
1892 *array = sample->addr;
1893 array++;
1894 }
1895
1896 if (type & PERF_SAMPLE_ID) {
1897 *array = sample->id;
1898 array++;
1899 }
1900
1901 if (type & PERF_SAMPLE_STREAM_ID) {
1902 *array = sample->stream_id;
1903 array++;
1904 }
1905
1906 if (type & PERF_SAMPLE_CPU) {
1907 u.val32[0] = sample->cpu;
1908 if (swapped) {
1909 /*
1910 * Inverse of what is done in perf_evsel__parse_sample
1911 */
1912 u.val32[0] = bswap_32(u.val32[0]);
1913 u.val64 = bswap_64(u.val64);
1914 }
1915 *array = u.val64;
1916 array++;
1917 }
1918
1919 if (type & PERF_SAMPLE_PERIOD) {
1920 *array = sample->period;
1921 array++;
1922 }
1923
1924 if (type & PERF_SAMPLE_READ) {
1925 if (read_format & PERF_FORMAT_GROUP)
1926 *array = sample->read.group.nr;
1927 else
1928 *array = sample->read.one.value;
1929 array++;
1930
1931 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1932 *array = sample->read.time_enabled;
1933 array++;
1934 }
1935
1936 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1937 *array = sample->read.time_running;
1938 array++;
1939 }
1940
1941 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1942 if (read_format & PERF_FORMAT_GROUP) {
1943 sz = sample->read.group.nr *
1944 sizeof(struct sample_read_value);
1945 memcpy(array, sample->read.group.values, sz);
1946 array = (void *)array + sz;
1947 } else {
1948 *array = sample->read.one.id;
1949 array++;
1950 }
1951 }
1952
1953 if (type & PERF_SAMPLE_CALLCHAIN) {
1954 sz = (sample->callchain->nr + 1) * sizeof(u64);
1955 memcpy(array, sample->callchain, sz);
1956 array = (void *)array + sz;
1957 }
1958
1959 if (type & PERF_SAMPLE_RAW) {
1960 u.val32[0] = sample->raw_size;
1961 if (WARN_ONCE(swapped,
1962 "Endianness of raw data not corrected!\n")) {
1963 /*
1964 * Inverse of what is done in perf_evsel__parse_sample
1965 */
1966 u.val32[0] = bswap_32(u.val32[0]);
1967 u.val32[1] = bswap_32(u.val32[1]);
1968 u.val64 = bswap_64(u.val64);
1969 }
1970 *array = u.val64;
1971 array = (void *)array + sizeof(u32);
1972
1973 memcpy(array, sample->raw_data, sample->raw_size);
1974 array = (void *)array + sample->raw_size;
1975 }
1976
1977 if (type & PERF_SAMPLE_BRANCH_STACK) {
1978 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1979 sz += sizeof(u64);
1980 memcpy(array, sample->branch_stack, sz);
1981 array = (void *)array + sz;
1982 }
1983
1984 if (type & PERF_SAMPLE_REGS_USER) {
1985 if (sample->user_regs.abi) {
1986 *array++ = sample->user_regs.abi;
1987 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1988 memcpy(array, sample->user_regs.regs, sz);
1989 array = (void *)array + sz;
1990 } else {
1991 *array++ = 0;
1992 }
1993 }
1994
1995 if (type & PERF_SAMPLE_STACK_USER) {
1996 sz = sample->user_stack.size;
1997 *array++ = sz;
1998 if (sz) {
1999 memcpy(array, sample->user_stack.data, sz);
2000 array = (void *)array + sz;
2001 *array++ = sz;
2002 }
2003 }
2004
2005 if (type & PERF_SAMPLE_WEIGHT) {
2006 *array = sample->weight;
2007 array++;
2008 }
2009
2010 if (type & PERF_SAMPLE_DATA_SRC) {
2011 *array = sample->data_src;
2012 array++;
2013 }
2014
2015 if (type & PERF_SAMPLE_TRANSACTION) {
2016 *array = sample->transaction;
2017 array++;
2018 }
2019
2020 if (type & PERF_SAMPLE_REGS_INTR) {
2021 if (sample->intr_regs.abi) {
2022 *array++ = sample->intr_regs.abi;
2023 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2024 memcpy(array, sample->intr_regs.regs, sz);
2025 array = (void *)array + sz;
2026 } else {
2027 *array++ = 0;
2028 }
2029 }
2030
2031 return 0;
2032 }
2033
2034 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2035 {
2036 return pevent_find_field(evsel->tp_format, name);
2037 }
2038
2039 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
2040 const char *name)
2041 {
2042 struct format_field *field = perf_evsel__field(evsel, name);
2043 int offset;
2044
2045 if (!field)
2046 return NULL;
2047
2048 offset = field->offset;
2049
2050 if (field->flags & FIELD_IS_DYNAMIC) {
2051 offset = *(int *)(sample->raw_data + field->offset);
2052 offset &= 0xffff;
2053 }
2054
2055 return sample->raw_data + offset;
2056 }
2057
2058 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2059 const char *name)
2060 {
2061 struct format_field *field = perf_evsel__field(evsel, name);
2062 void *ptr;
2063 u64 value;
2064
2065 if (!field)
2066 return 0;
2067
2068 ptr = sample->raw_data + field->offset;
2069
2070 switch (field->size) {
2071 case 1:
2072 return *(u8 *)ptr;
2073 case 2:
2074 value = *(u16 *)ptr;
2075 break;
2076 case 4:
2077 value = *(u32 *)ptr;
2078 break;
2079 case 8:
2080 memcpy(&value, ptr, sizeof(u64));
2081 break;
2082 default:
2083 return 0;
2084 }
2085
2086 if (!evsel->needs_swap)
2087 return value;
2088
2089 switch (field->size) {
2090 case 2:
2091 return bswap_16(value);
2092 case 4:
2093 return bswap_32(value);
2094 case 8:
2095 return bswap_64(value);
2096 default:
2097 return 0;
2098 }
2099
2100 return 0;
2101 }
2102
2103 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2104 {
2105 va_list args;
2106 int ret = 0;
2107
2108 if (!*first) {
2109 ret += fprintf(fp, ",");
2110 } else {
2111 ret += fprintf(fp, ":");
2112 *first = false;
2113 }
2114
2115 va_start(args, fmt);
2116 ret += vfprintf(fp, fmt, args);
2117 va_end(args);
2118 return ret;
2119 }
2120
2121 static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
2122 {
2123 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
2124 }
2125
2126 int perf_evsel__fprintf(struct perf_evsel *evsel,
2127 struct perf_attr_details *details, FILE *fp)
2128 {
2129 bool first = true;
2130 int printed = 0;
2131
2132 if (details->event_group) {
2133 struct perf_evsel *pos;
2134
2135 if (!perf_evsel__is_group_leader(evsel))
2136 return 0;
2137
2138 if (evsel->nr_members > 1)
2139 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2140
2141 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2142 for_each_group_member(pos, evsel)
2143 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2144
2145 if (evsel->nr_members > 1)
2146 printed += fprintf(fp, "}");
2147 goto out;
2148 }
2149
2150 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2151
2152 if (details->verbose) {
2153 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2154 __print_attr__fprintf, &first);
2155 } else if (details->freq) {
2156 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2157 (u64)evsel->attr.sample_freq);
2158 }
2159 out:
2160 fputc('\n', fp);
2161 return ++printed;
2162 }
2163
2164 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2165 char *msg, size_t msgsize)
2166 {
2167 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2168 evsel->attr.type == PERF_TYPE_HARDWARE &&
2169 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2170 /*
2171 * If it's cycles then fall back to hrtimer based
2172 * cpu-clock-tick sw counter, which is always available even if
2173 * no PMU support.
2174 *
2175 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2176 * b0a873e).
2177 */
2178 scnprintf(msg, msgsize, "%s",
2179 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2180
2181 evsel->attr.type = PERF_TYPE_SOFTWARE;
2182 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2183
2184 zfree(&evsel->name);
2185 return true;
2186 }
2187
2188 return false;
2189 }
2190
2191 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2192 int err, char *msg, size_t size)
2193 {
2194 char sbuf[STRERR_BUFSIZE];
2195
2196 switch (err) {
2197 case EPERM:
2198 case EACCES:
2199 return scnprintf(msg, size,
2200 "You may not have permission to collect %sstats.\n"
2201 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2202 " -1 - Not paranoid at all\n"
2203 " 0 - Disallow raw tracepoint access for unpriv\n"
2204 " 1 - Disallow cpu events for unpriv\n"
2205 " 2 - Disallow kernel profiling for unpriv",
2206 target->system_wide ? "system-wide " : "");
2207 case ENOENT:
2208 return scnprintf(msg, size, "The %s event is not supported.",
2209 perf_evsel__name(evsel));
2210 case EMFILE:
2211 return scnprintf(msg, size, "%s",
2212 "Too many events are opened.\n"
2213 "Probably the maximum number of open file descriptors has been reached.\n"
2214 "Hint: Try again after reducing the number of events.\n"
2215 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2216 case ENODEV:
2217 if (target->cpu_list)
2218 return scnprintf(msg, size, "%s",
2219 "No such device - did you specify an out-of-range profile CPU?\n");
2220 break;
2221 case EOPNOTSUPP:
2222 if (evsel->attr.precise_ip)
2223 return scnprintf(msg, size, "%s",
2224 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2225 #if defined(__i386__) || defined(__x86_64__)
2226 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2227 return scnprintf(msg, size, "%s",
2228 "No hardware sampling interrupt available.\n"
2229 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2230 #endif
2231 break;
2232 case EBUSY:
2233 if (find_process("oprofiled"))
2234 return scnprintf(msg, size,
2235 "The PMU counters are busy/taken by another profiler.\n"
2236 "We found oprofile daemon running, please stop it and try again.");
2237 break;
2238 case EINVAL:
2239 if (perf_missing_features.clockid)
2240 return scnprintf(msg, size, "clockid feature not supported.");
2241 if (perf_missing_features.clockid_wrong)
2242 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2243 break;
2244 default:
2245 break;
2246 }
2247
2248 return scnprintf(msg, size,
2249 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2250 "/bin/dmesg may provide additional information.\n"
2251 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2252 err, strerror_r(err, sbuf, sizeof(sbuf)),
2253 perf_evsel__name(evsel));
2254 }
This page took 0.10787 seconds and 5 git commands to generate.