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