perf evsel: Introduce method to request IDs be used
[deliverable/linux.git] / tools / perf / util / evsel.c
1 /*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
10 #include <byteswap.h>
11 #include <linux/bitops.h>
12 #include "asm/bug.h"
13 #include "debugfs.h"
14 #include "event-parse.h"
15 #include "evsel.h"
16 #include "evlist.h"
17 #include "util.h"
18 #include "cpumap.h"
19 #include "thread_map.h"
20 #include "target.h"
21 #include <linux/hw_breakpoint.h>
22 #include <linux/perf_event.h>
23 #include "perf_regs.h"
24
25 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
26
27 static int __perf_evsel__sample_size(u64 sample_type)
28 {
29 u64 mask = sample_type & PERF_SAMPLE_MASK;
30 int size = 0;
31 int i;
32
33 for (i = 0; i < 64; i++) {
34 if (mask & (1ULL << i))
35 size++;
36 }
37
38 size *= sizeof(u64);
39
40 return size;
41 }
42
43 void hists__init(struct hists *hists)
44 {
45 memset(hists, 0, sizeof(*hists));
46 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
47 hists->entries_in = &hists->entries_in_array[0];
48 hists->entries_collapsed = RB_ROOT;
49 hists->entries = RB_ROOT;
50 pthread_mutex_init(&hists->lock, NULL);
51 }
52
53 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
54 enum perf_event_sample_format bit)
55 {
56 if (!(evsel->attr.sample_type & bit)) {
57 evsel->attr.sample_type |= bit;
58 evsel->sample_size += sizeof(u64);
59 }
60 }
61
62 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
63 enum perf_event_sample_format bit)
64 {
65 if (evsel->attr.sample_type & bit) {
66 evsel->attr.sample_type &= ~bit;
67 evsel->sample_size -= sizeof(u64);
68 }
69 }
70
71 void perf_evsel__set_sample_id(struct perf_evsel *evsel)
72 {
73 perf_evsel__set_sample_bit(evsel, ID);
74 evsel->attr.read_format |= PERF_FORMAT_ID;
75 }
76
77 void perf_evsel__init(struct perf_evsel *evsel,
78 struct perf_event_attr *attr, int idx)
79 {
80 evsel->idx = idx;
81 evsel->attr = *attr;
82 evsel->leader = evsel;
83 INIT_LIST_HEAD(&evsel->node);
84 hists__init(&evsel->hists);
85 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
86 }
87
88 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
89 {
90 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
91
92 if (evsel != NULL)
93 perf_evsel__init(evsel, attr, idx);
94
95 return evsel;
96 }
97
98 struct event_format *event_format__new(const char *sys, const char *name)
99 {
100 int fd, n;
101 char *filename;
102 void *bf = NULL, *nbf;
103 size_t size = 0, alloc_size = 0;
104 struct event_format *format = NULL;
105
106 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
107 goto out;
108
109 fd = open(filename, O_RDONLY);
110 if (fd < 0)
111 goto out_free_filename;
112
113 do {
114 if (size == alloc_size) {
115 alloc_size += BUFSIZ;
116 nbf = realloc(bf, alloc_size);
117 if (nbf == NULL)
118 goto out_free_bf;
119 bf = nbf;
120 }
121
122 n = read(fd, bf + size, BUFSIZ);
123 if (n < 0)
124 goto out_free_bf;
125 size += n;
126 } while (n > 0);
127
128 pevent_parse_format(&format, bf, size, sys);
129
130 out_free_bf:
131 free(bf);
132 close(fd);
133 out_free_filename:
134 free(filename);
135 out:
136 return format;
137 }
138
139 struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
140 {
141 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
142
143 if (evsel != NULL) {
144 struct perf_event_attr attr = {
145 .type = PERF_TYPE_TRACEPOINT,
146 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
147 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
148 };
149
150 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
151 goto out_free;
152
153 evsel->tp_format = event_format__new(sys, name);
154 if (evsel->tp_format == NULL)
155 goto out_free;
156
157 event_attr_init(&attr);
158 attr.config = evsel->tp_format->id;
159 attr.sample_period = 1;
160 perf_evsel__init(evsel, &attr, idx);
161 }
162
163 return evsel;
164
165 out_free:
166 free(evsel->name);
167 free(evsel);
168 return NULL;
169 }
170
171 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
172 "cycles",
173 "instructions",
174 "cache-references",
175 "cache-misses",
176 "branches",
177 "branch-misses",
178 "bus-cycles",
179 "stalled-cycles-frontend",
180 "stalled-cycles-backend",
181 "ref-cycles",
182 };
183
184 static const char *__perf_evsel__hw_name(u64 config)
185 {
186 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
187 return perf_evsel__hw_names[config];
188
189 return "unknown-hardware";
190 }
191
192 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
193 {
194 int colon = 0, r = 0;
195 struct perf_event_attr *attr = &evsel->attr;
196 bool exclude_guest_default = false;
197
198 #define MOD_PRINT(context, mod) do { \
199 if (!attr->exclude_##context) { \
200 if (!colon) colon = ++r; \
201 r += scnprintf(bf + r, size - r, "%c", mod); \
202 } } while(0)
203
204 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
205 MOD_PRINT(kernel, 'k');
206 MOD_PRINT(user, 'u');
207 MOD_PRINT(hv, 'h');
208 exclude_guest_default = true;
209 }
210
211 if (attr->precise_ip) {
212 if (!colon)
213 colon = ++r;
214 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
215 exclude_guest_default = true;
216 }
217
218 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
219 MOD_PRINT(host, 'H');
220 MOD_PRINT(guest, 'G');
221 }
222 #undef MOD_PRINT
223 if (colon)
224 bf[colon - 1] = ':';
225 return r;
226 }
227
228 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
229 {
230 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
231 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
232 }
233
234 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
235 "cpu-clock",
236 "task-clock",
237 "page-faults",
238 "context-switches",
239 "cpu-migrations",
240 "minor-faults",
241 "major-faults",
242 "alignment-faults",
243 "emulation-faults",
244 };
245
246 static const char *__perf_evsel__sw_name(u64 config)
247 {
248 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
249 return perf_evsel__sw_names[config];
250 return "unknown-software";
251 }
252
253 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
254 {
255 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
256 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
257 }
258
259 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
260 {
261 int r;
262
263 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
264
265 if (type & HW_BREAKPOINT_R)
266 r += scnprintf(bf + r, size - r, "r");
267
268 if (type & HW_BREAKPOINT_W)
269 r += scnprintf(bf + r, size - r, "w");
270
271 if (type & HW_BREAKPOINT_X)
272 r += scnprintf(bf + r, size - r, "x");
273
274 return r;
275 }
276
277 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
278 {
279 struct perf_event_attr *attr = &evsel->attr;
280 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
281 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
282 }
283
284 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
285 [PERF_EVSEL__MAX_ALIASES] = {
286 { "L1-dcache", "l1-d", "l1d", "L1-data", },
287 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
288 { "LLC", "L2", },
289 { "dTLB", "d-tlb", "Data-TLB", },
290 { "iTLB", "i-tlb", "Instruction-TLB", },
291 { "branch", "branches", "bpu", "btb", "bpc", },
292 { "node", },
293 };
294
295 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
296 [PERF_EVSEL__MAX_ALIASES] = {
297 { "load", "loads", "read", },
298 { "store", "stores", "write", },
299 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
300 };
301
302 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
303 [PERF_EVSEL__MAX_ALIASES] = {
304 { "refs", "Reference", "ops", "access", },
305 { "misses", "miss", },
306 };
307
308 #define C(x) PERF_COUNT_HW_CACHE_##x
309 #define CACHE_READ (1 << C(OP_READ))
310 #define CACHE_WRITE (1 << C(OP_WRITE))
311 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
312 #define COP(x) (1 << x)
313
314 /*
315 * cache operartion stat
316 * L1I : Read and prefetch only
317 * ITLB and BPU : Read-only
318 */
319 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
320 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
321 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
322 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
323 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
324 [C(ITLB)] = (CACHE_READ),
325 [C(BPU)] = (CACHE_READ),
326 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
327 };
328
329 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
330 {
331 if (perf_evsel__hw_cache_stat[type] & COP(op))
332 return true; /* valid */
333 else
334 return false; /* invalid */
335 }
336
337 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
338 char *bf, size_t size)
339 {
340 if (result) {
341 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
342 perf_evsel__hw_cache_op[op][0],
343 perf_evsel__hw_cache_result[result][0]);
344 }
345
346 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
347 perf_evsel__hw_cache_op[op][1]);
348 }
349
350 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
351 {
352 u8 op, result, type = (config >> 0) & 0xff;
353 const char *err = "unknown-ext-hardware-cache-type";
354
355 if (type > PERF_COUNT_HW_CACHE_MAX)
356 goto out_err;
357
358 op = (config >> 8) & 0xff;
359 err = "unknown-ext-hardware-cache-op";
360 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
361 goto out_err;
362
363 result = (config >> 16) & 0xff;
364 err = "unknown-ext-hardware-cache-result";
365 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
366 goto out_err;
367
368 err = "invalid-cache";
369 if (!perf_evsel__is_cache_op_valid(type, op))
370 goto out_err;
371
372 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
373 out_err:
374 return scnprintf(bf, size, "%s", err);
375 }
376
377 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
378 {
379 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
380 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
381 }
382
383 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
384 {
385 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
386 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
387 }
388
389 const char *perf_evsel__name(struct perf_evsel *evsel)
390 {
391 char bf[128];
392
393 if (evsel->name)
394 return evsel->name;
395
396 switch (evsel->attr.type) {
397 case PERF_TYPE_RAW:
398 perf_evsel__raw_name(evsel, bf, sizeof(bf));
399 break;
400
401 case PERF_TYPE_HARDWARE:
402 perf_evsel__hw_name(evsel, bf, sizeof(bf));
403 break;
404
405 case PERF_TYPE_HW_CACHE:
406 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
407 break;
408
409 case PERF_TYPE_SOFTWARE:
410 perf_evsel__sw_name(evsel, bf, sizeof(bf));
411 break;
412
413 case PERF_TYPE_TRACEPOINT:
414 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
415 break;
416
417 case PERF_TYPE_BREAKPOINT:
418 perf_evsel__bp_name(evsel, bf, sizeof(bf));
419 break;
420
421 default:
422 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
423 evsel->attr.type);
424 break;
425 }
426
427 evsel->name = strdup(bf);
428
429 return evsel->name ?: "unknown";
430 }
431
432 /*
433 * The enable_on_exec/disabled value strategy:
434 *
435 * 1) For any type of traced program:
436 * - all independent events and group leaders are disabled
437 * - all group members are enabled
438 *
439 * Group members are ruled by group leaders. They need to
440 * be enabled, because the group scheduling relies on that.
441 *
442 * 2) For traced programs executed by perf:
443 * - all independent events and group leaders have
444 * enable_on_exec set
445 * - we don't specifically enable or disable any event during
446 * the record command
447 *
448 * Independent events and group leaders are initially disabled
449 * and get enabled by exec. Group members are ruled by group
450 * leaders as stated in 1).
451 *
452 * 3) For traced programs attached by perf (pid/tid):
453 * - we specifically enable or disable all events during
454 * the record command
455 *
456 * When attaching events to already running traced we
457 * enable/disable events specifically, as there's no
458 * initial traced exec call.
459 */
460 void perf_evsel__config(struct perf_evsel *evsel,
461 struct perf_record_opts *opts)
462 {
463 struct perf_event_attr *attr = &evsel->attr;
464 int track = !evsel->idx; /* only the first counter needs these */
465
466 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
467 attr->inherit = !opts->no_inherit;
468 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
469 PERF_FORMAT_TOTAL_TIME_RUNNING |
470 PERF_FORMAT_ID;
471
472 perf_evsel__set_sample_bit(evsel, IP);
473 perf_evsel__set_sample_bit(evsel, TID);
474
475 /*
476 * We default some events to a 1 default interval. But keep
477 * it a weak assumption overridable by the user.
478 */
479 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
480 opts->user_interval != ULLONG_MAX)) {
481 if (opts->freq) {
482 perf_evsel__set_sample_bit(evsel, PERIOD);
483 attr->freq = 1;
484 attr->sample_freq = opts->freq;
485 } else {
486 attr->sample_period = opts->default_interval;
487 }
488 }
489
490 if (opts->no_samples)
491 attr->sample_freq = 0;
492
493 if (opts->inherit_stat)
494 attr->inherit_stat = 1;
495
496 if (opts->sample_address) {
497 perf_evsel__set_sample_bit(evsel, ADDR);
498 attr->mmap_data = track;
499 }
500
501 if (opts->call_graph) {
502 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
503
504 if (opts->call_graph == CALLCHAIN_DWARF) {
505 perf_evsel__set_sample_bit(evsel, REGS_USER);
506 perf_evsel__set_sample_bit(evsel, STACK_USER);
507 attr->sample_regs_user = PERF_REGS_MASK;
508 attr->sample_stack_user = opts->stack_dump_size;
509 attr->exclude_callchain_user = 1;
510 }
511 }
512
513 if (perf_target__has_cpu(&opts->target))
514 perf_evsel__set_sample_bit(evsel, CPU);
515
516 if (opts->period)
517 perf_evsel__set_sample_bit(evsel, PERIOD);
518
519 if (!opts->sample_id_all_missing &&
520 (opts->sample_time || !opts->no_inherit ||
521 perf_target__has_cpu(&opts->target)))
522 perf_evsel__set_sample_bit(evsel, TIME);
523
524 if (opts->raw_samples) {
525 perf_evsel__set_sample_bit(evsel, TIME);
526 perf_evsel__set_sample_bit(evsel, RAW);
527 perf_evsel__set_sample_bit(evsel, CPU);
528 }
529
530 if (opts->no_delay) {
531 attr->watermark = 0;
532 attr->wakeup_events = 1;
533 }
534 if (opts->branch_stack) {
535 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
536 attr->branch_sample_type = opts->branch_stack;
537 }
538
539 attr->mmap = track;
540 attr->comm = track;
541
542 /*
543 * XXX see the function comment above
544 *
545 * Disabling only independent events or group leaders,
546 * keeping group members enabled.
547 */
548 if (perf_evsel__is_group_leader(evsel))
549 attr->disabled = 1;
550
551 /*
552 * Setting enable_on_exec for independent events and
553 * group leaders for traced executed by perf.
554 */
555 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
556 attr->enable_on_exec = 1;
557 }
558
559 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
560 {
561 int cpu, thread;
562 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
563
564 if (evsel->fd) {
565 for (cpu = 0; cpu < ncpus; cpu++) {
566 for (thread = 0; thread < nthreads; thread++) {
567 FD(evsel, cpu, thread) = -1;
568 }
569 }
570 }
571
572 return evsel->fd != NULL ? 0 : -ENOMEM;
573 }
574
575 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
576 const char *filter)
577 {
578 int cpu, thread;
579
580 for (cpu = 0; cpu < ncpus; cpu++) {
581 for (thread = 0; thread < nthreads; thread++) {
582 int fd = FD(evsel, cpu, thread),
583 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
584
585 if (err)
586 return err;
587 }
588 }
589
590 return 0;
591 }
592
593 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
594 {
595 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
596 if (evsel->sample_id == NULL)
597 return -ENOMEM;
598
599 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
600 if (evsel->id == NULL) {
601 xyarray__delete(evsel->sample_id);
602 evsel->sample_id = NULL;
603 return -ENOMEM;
604 }
605
606 return 0;
607 }
608
609 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
610 {
611 evsel->counts = zalloc((sizeof(*evsel->counts) +
612 (ncpus * sizeof(struct perf_counts_values))));
613 return evsel->counts != NULL ? 0 : -ENOMEM;
614 }
615
616 void perf_evsel__free_fd(struct perf_evsel *evsel)
617 {
618 xyarray__delete(evsel->fd);
619 evsel->fd = NULL;
620 }
621
622 void perf_evsel__free_id(struct perf_evsel *evsel)
623 {
624 xyarray__delete(evsel->sample_id);
625 evsel->sample_id = NULL;
626 free(evsel->id);
627 evsel->id = NULL;
628 }
629
630 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
631 {
632 int cpu, thread;
633
634 for (cpu = 0; cpu < ncpus; cpu++)
635 for (thread = 0; thread < nthreads; ++thread) {
636 close(FD(evsel, cpu, thread));
637 FD(evsel, cpu, thread) = -1;
638 }
639 }
640
641 void perf_evsel__exit(struct perf_evsel *evsel)
642 {
643 assert(list_empty(&evsel->node));
644 xyarray__delete(evsel->fd);
645 xyarray__delete(evsel->sample_id);
646 free(evsel->id);
647 }
648
649 void perf_evsel__delete(struct perf_evsel *evsel)
650 {
651 perf_evsel__exit(evsel);
652 close_cgroup(evsel->cgrp);
653 free(evsel->group_name);
654 if (evsel->tp_format)
655 pevent_free_format(evsel->tp_format);
656 free(evsel->name);
657 free(evsel);
658 }
659
660 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
661 int cpu, int thread, bool scale)
662 {
663 struct perf_counts_values count;
664 size_t nv = scale ? 3 : 1;
665
666 if (FD(evsel, cpu, thread) < 0)
667 return -EINVAL;
668
669 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
670 return -ENOMEM;
671
672 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
673 return -errno;
674
675 if (scale) {
676 if (count.run == 0)
677 count.val = 0;
678 else if (count.run < count.ena)
679 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
680 } else
681 count.ena = count.run = 0;
682
683 evsel->counts->cpu[cpu] = count;
684 return 0;
685 }
686
687 int __perf_evsel__read(struct perf_evsel *evsel,
688 int ncpus, int nthreads, bool scale)
689 {
690 size_t nv = scale ? 3 : 1;
691 int cpu, thread;
692 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
693
694 aggr->val = aggr->ena = aggr->run = 0;
695
696 for (cpu = 0; cpu < ncpus; cpu++) {
697 for (thread = 0; thread < nthreads; thread++) {
698 if (FD(evsel, cpu, thread) < 0)
699 continue;
700
701 if (readn(FD(evsel, cpu, thread),
702 &count, nv * sizeof(u64)) < 0)
703 return -errno;
704
705 aggr->val += count.val;
706 if (scale) {
707 aggr->ena += count.ena;
708 aggr->run += count.run;
709 }
710 }
711 }
712
713 evsel->counts->scaled = 0;
714 if (scale) {
715 if (aggr->run == 0) {
716 evsel->counts->scaled = -1;
717 aggr->val = 0;
718 return 0;
719 }
720
721 if (aggr->run < aggr->ena) {
722 evsel->counts->scaled = 1;
723 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
724 }
725 } else
726 aggr->ena = aggr->run = 0;
727
728 return 0;
729 }
730
731 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
732 {
733 struct perf_evsel *leader = evsel->leader;
734 int fd;
735
736 if (perf_evsel__is_group_leader(evsel))
737 return -1;
738
739 /*
740 * Leader must be already processed/open,
741 * if not it's a bug.
742 */
743 BUG_ON(!leader->fd);
744
745 fd = FD(leader, cpu, thread);
746 BUG_ON(fd == -1);
747
748 return fd;
749 }
750
751 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
752 struct thread_map *threads)
753 {
754 int cpu, thread;
755 unsigned long flags = 0;
756 int pid = -1, err;
757
758 if (evsel->fd == NULL &&
759 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
760 return -ENOMEM;
761
762 if (evsel->cgrp) {
763 flags = PERF_FLAG_PID_CGROUP;
764 pid = evsel->cgrp->fd;
765 }
766
767 for (cpu = 0; cpu < cpus->nr; cpu++) {
768
769 for (thread = 0; thread < threads->nr; thread++) {
770 int group_fd;
771
772 if (!evsel->cgrp)
773 pid = threads->map[thread];
774
775 group_fd = get_group_fd(evsel, cpu, thread);
776
777 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
778 pid,
779 cpus->map[cpu],
780 group_fd, flags);
781 if (FD(evsel, cpu, thread) < 0) {
782 err = -errno;
783 goto out_close;
784 }
785 }
786 }
787
788 return 0;
789
790 out_close:
791 do {
792 while (--thread >= 0) {
793 close(FD(evsel, cpu, thread));
794 FD(evsel, cpu, thread) = -1;
795 }
796 thread = threads->nr;
797 } while (--cpu >= 0);
798 return err;
799 }
800
801 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
802 {
803 if (evsel->fd == NULL)
804 return;
805
806 perf_evsel__close_fd(evsel, ncpus, nthreads);
807 perf_evsel__free_fd(evsel);
808 evsel->fd = NULL;
809 }
810
811 static struct {
812 struct cpu_map map;
813 int cpus[1];
814 } empty_cpu_map = {
815 .map.nr = 1,
816 .cpus = { -1, },
817 };
818
819 static struct {
820 struct thread_map map;
821 int threads[1];
822 } empty_thread_map = {
823 .map.nr = 1,
824 .threads = { -1, },
825 };
826
827 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
828 struct thread_map *threads)
829 {
830 if (cpus == NULL) {
831 /* Work around old compiler warnings about strict aliasing */
832 cpus = &empty_cpu_map.map;
833 }
834
835 if (threads == NULL)
836 threads = &empty_thread_map.map;
837
838 return __perf_evsel__open(evsel, cpus, threads);
839 }
840
841 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
842 struct cpu_map *cpus)
843 {
844 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
845 }
846
847 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
848 struct thread_map *threads)
849 {
850 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
851 }
852
853 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
854 const union perf_event *event,
855 struct perf_sample *sample)
856 {
857 u64 type = evsel->attr.sample_type;
858 const u64 *array = event->sample.array;
859 bool swapped = evsel->needs_swap;
860 union u64_swap u;
861
862 array += ((event->header.size -
863 sizeof(event->header)) / sizeof(u64)) - 1;
864
865 if (type & PERF_SAMPLE_CPU) {
866 u.val64 = *array;
867 if (swapped) {
868 /* undo swap of u64, then swap on individual u32s */
869 u.val64 = bswap_64(u.val64);
870 u.val32[0] = bswap_32(u.val32[0]);
871 }
872
873 sample->cpu = u.val32[0];
874 array--;
875 }
876
877 if (type & PERF_SAMPLE_STREAM_ID) {
878 sample->stream_id = *array;
879 array--;
880 }
881
882 if (type & PERF_SAMPLE_ID) {
883 sample->id = *array;
884 array--;
885 }
886
887 if (type & PERF_SAMPLE_TIME) {
888 sample->time = *array;
889 array--;
890 }
891
892 if (type & PERF_SAMPLE_TID) {
893 u.val64 = *array;
894 if (swapped) {
895 /* undo swap of u64, then swap on individual u32s */
896 u.val64 = bswap_64(u.val64);
897 u.val32[0] = bswap_32(u.val32[0]);
898 u.val32[1] = bswap_32(u.val32[1]);
899 }
900
901 sample->pid = u.val32[0];
902 sample->tid = u.val32[1];
903 }
904
905 return 0;
906 }
907
908 static bool sample_overlap(const union perf_event *event,
909 const void *offset, u64 size)
910 {
911 const void *base = event;
912
913 if (offset + size > base + event->header.size)
914 return true;
915
916 return false;
917 }
918
919 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
920 struct perf_sample *data)
921 {
922 u64 type = evsel->attr.sample_type;
923 u64 regs_user = evsel->attr.sample_regs_user;
924 bool swapped = evsel->needs_swap;
925 const u64 *array;
926
927 /*
928 * used for cross-endian analysis. See git commit 65014ab3
929 * for why this goofiness is needed.
930 */
931 union u64_swap u;
932
933 memset(data, 0, sizeof(*data));
934 data->cpu = data->pid = data->tid = -1;
935 data->stream_id = data->id = data->time = -1ULL;
936 data->period = 1;
937
938 if (event->header.type != PERF_RECORD_SAMPLE) {
939 if (!evsel->attr.sample_id_all)
940 return 0;
941 return perf_evsel__parse_id_sample(evsel, event, data);
942 }
943
944 array = event->sample.array;
945
946 if (evsel->sample_size + sizeof(event->header) > event->header.size)
947 return -EFAULT;
948
949 if (type & PERF_SAMPLE_IP) {
950 data->ip = event->ip.ip;
951 array++;
952 }
953
954 if (type & PERF_SAMPLE_TID) {
955 u.val64 = *array;
956 if (swapped) {
957 /* undo swap of u64, then swap on individual u32s */
958 u.val64 = bswap_64(u.val64);
959 u.val32[0] = bswap_32(u.val32[0]);
960 u.val32[1] = bswap_32(u.val32[1]);
961 }
962
963 data->pid = u.val32[0];
964 data->tid = u.val32[1];
965 array++;
966 }
967
968 if (type & PERF_SAMPLE_TIME) {
969 data->time = *array;
970 array++;
971 }
972
973 data->addr = 0;
974 if (type & PERF_SAMPLE_ADDR) {
975 data->addr = *array;
976 array++;
977 }
978
979 data->id = -1ULL;
980 if (type & PERF_SAMPLE_ID) {
981 data->id = *array;
982 array++;
983 }
984
985 if (type & PERF_SAMPLE_STREAM_ID) {
986 data->stream_id = *array;
987 array++;
988 }
989
990 if (type & PERF_SAMPLE_CPU) {
991
992 u.val64 = *array;
993 if (swapped) {
994 /* undo swap of u64, then swap on individual u32s */
995 u.val64 = bswap_64(u.val64);
996 u.val32[0] = bswap_32(u.val32[0]);
997 }
998
999 data->cpu = u.val32[0];
1000 array++;
1001 }
1002
1003 if (type & PERF_SAMPLE_PERIOD) {
1004 data->period = *array;
1005 array++;
1006 }
1007
1008 if (type & PERF_SAMPLE_READ) {
1009 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
1010 return -1;
1011 }
1012
1013 if (type & PERF_SAMPLE_CALLCHAIN) {
1014 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1015 return -EFAULT;
1016
1017 data->callchain = (struct ip_callchain *)array;
1018
1019 if (sample_overlap(event, array, data->callchain->nr))
1020 return -EFAULT;
1021
1022 array += 1 + data->callchain->nr;
1023 }
1024
1025 if (type & PERF_SAMPLE_RAW) {
1026 const u64 *pdata;
1027
1028 u.val64 = *array;
1029 if (WARN_ONCE(swapped,
1030 "Endianness of raw data not corrected!\n")) {
1031 /* undo swap of u64, then swap on individual u32s */
1032 u.val64 = bswap_64(u.val64);
1033 u.val32[0] = bswap_32(u.val32[0]);
1034 u.val32[1] = bswap_32(u.val32[1]);
1035 }
1036
1037 if (sample_overlap(event, array, sizeof(u32)))
1038 return -EFAULT;
1039
1040 data->raw_size = u.val32[0];
1041 pdata = (void *) array + sizeof(u32);
1042
1043 if (sample_overlap(event, pdata, data->raw_size))
1044 return -EFAULT;
1045
1046 data->raw_data = (void *) pdata;
1047
1048 array = (void *)array + data->raw_size + sizeof(u32);
1049 }
1050
1051 if (type & PERF_SAMPLE_BRANCH_STACK) {
1052 u64 sz;
1053
1054 data->branch_stack = (struct branch_stack *)array;
1055 array++; /* nr */
1056
1057 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1058 sz /= sizeof(u64);
1059 array += sz;
1060 }
1061
1062 if (type & PERF_SAMPLE_REGS_USER) {
1063 /* First u64 tells us if we have any regs in sample. */
1064 u64 avail = *array++;
1065
1066 if (avail) {
1067 data->user_regs.regs = (u64 *)array;
1068 array += hweight_long(regs_user);
1069 }
1070 }
1071
1072 if (type & PERF_SAMPLE_STACK_USER) {
1073 u64 size = *array++;
1074
1075 data->user_stack.offset = ((char *)(array - 1)
1076 - (char *) event);
1077
1078 if (!size) {
1079 data->user_stack.size = 0;
1080 } else {
1081 data->user_stack.data = (char *)array;
1082 array += size / sizeof(*array);
1083 data->user_stack.size = *array;
1084 }
1085 }
1086
1087 return 0;
1088 }
1089
1090 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1091 const struct perf_sample *sample,
1092 bool swapped)
1093 {
1094 u64 *array;
1095
1096 /*
1097 * used for cross-endian analysis. See git commit 65014ab3
1098 * for why this goofiness is needed.
1099 */
1100 union u64_swap u;
1101
1102 array = event->sample.array;
1103
1104 if (type & PERF_SAMPLE_IP) {
1105 event->ip.ip = sample->ip;
1106 array++;
1107 }
1108
1109 if (type & PERF_SAMPLE_TID) {
1110 u.val32[0] = sample->pid;
1111 u.val32[1] = sample->tid;
1112 if (swapped) {
1113 /*
1114 * Inverse of what is done in perf_evsel__parse_sample
1115 */
1116 u.val32[0] = bswap_32(u.val32[0]);
1117 u.val32[1] = bswap_32(u.val32[1]);
1118 u.val64 = bswap_64(u.val64);
1119 }
1120
1121 *array = u.val64;
1122 array++;
1123 }
1124
1125 if (type & PERF_SAMPLE_TIME) {
1126 *array = sample->time;
1127 array++;
1128 }
1129
1130 if (type & PERF_SAMPLE_ADDR) {
1131 *array = sample->addr;
1132 array++;
1133 }
1134
1135 if (type & PERF_SAMPLE_ID) {
1136 *array = sample->id;
1137 array++;
1138 }
1139
1140 if (type & PERF_SAMPLE_STREAM_ID) {
1141 *array = sample->stream_id;
1142 array++;
1143 }
1144
1145 if (type & PERF_SAMPLE_CPU) {
1146 u.val32[0] = sample->cpu;
1147 if (swapped) {
1148 /*
1149 * Inverse of what is done in perf_evsel__parse_sample
1150 */
1151 u.val32[0] = bswap_32(u.val32[0]);
1152 u.val64 = bswap_64(u.val64);
1153 }
1154 *array = u.val64;
1155 array++;
1156 }
1157
1158 if (type & PERF_SAMPLE_PERIOD) {
1159 *array = sample->period;
1160 array++;
1161 }
1162
1163 return 0;
1164 }
1165
1166 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1167 {
1168 return pevent_find_field(evsel->tp_format, name);
1169 }
1170
1171 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1172 const char *name)
1173 {
1174 struct format_field *field = perf_evsel__field(evsel, name);
1175 int offset;
1176
1177 if (!field)
1178 return NULL;
1179
1180 offset = field->offset;
1181
1182 if (field->flags & FIELD_IS_DYNAMIC) {
1183 offset = *(int *)(sample->raw_data + field->offset);
1184 offset &= 0xffff;
1185 }
1186
1187 return sample->raw_data + offset;
1188 }
1189
1190 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1191 const char *name)
1192 {
1193 struct format_field *field = perf_evsel__field(evsel, name);
1194 void *ptr;
1195 u64 value;
1196
1197 if (!field)
1198 return 0;
1199
1200 ptr = sample->raw_data + field->offset;
1201
1202 switch (field->size) {
1203 case 1:
1204 return *(u8 *)ptr;
1205 case 2:
1206 value = *(u16 *)ptr;
1207 break;
1208 case 4:
1209 value = *(u32 *)ptr;
1210 break;
1211 case 8:
1212 value = *(u64 *)ptr;
1213 break;
1214 default:
1215 return 0;
1216 }
1217
1218 if (!evsel->needs_swap)
1219 return value;
1220
1221 switch (field->size) {
1222 case 2:
1223 return bswap_16(value);
1224 case 4:
1225 return bswap_32(value);
1226 case 8:
1227 return bswap_64(value);
1228 default:
1229 return 0;
1230 }
1231
1232 return 0;
1233 }
This page took 0.112391 seconds and 5 git commands to generate.