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