perf evsel: Carve out event modifier formatting
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / evsel.c
blobdab893804a147bb778b222ee0bf1550fa2346b12
1 /*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
10 #include <byteswap.h>
11 #include "asm/bug.h"
12 #include "evsel.h"
13 #include "evlist.h"
14 #include "util.h"
15 #include "cpumap.h"
16 #include "thread_map.h"
17 #include "target.h"
18 #include "../../include/linux/perf_event.h"
20 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
21 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
23 int __perf_evsel__sample_size(u64 sample_type)
25 u64 mask = sample_type & PERF_SAMPLE_MASK;
26 int size = 0;
27 int i;
29 for (i = 0; i < 64; i++) {
30 if (mask & (1ULL << i))
31 size++;
34 size *= sizeof(u64);
36 return size;
39 void hists__init(struct hists *hists)
41 memset(hists, 0, sizeof(*hists));
42 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
43 hists->entries_in = &hists->entries_in_array[0];
44 hists->entries_collapsed = RB_ROOT;
45 hists->entries = RB_ROOT;
46 pthread_mutex_init(&hists->lock, NULL);
49 void perf_evsel__init(struct perf_evsel *evsel,
50 struct perf_event_attr *attr, int idx)
52 evsel->idx = idx;
53 evsel->attr = *attr;
54 INIT_LIST_HEAD(&evsel->node);
55 hists__init(&evsel->hists);
58 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
60 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
62 if (evsel != NULL)
63 perf_evsel__init(evsel, attr, idx);
65 return evsel;
68 static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
69 "cycles",
70 "instructions",
71 "cache-references",
72 "cache-misses",
73 "branches",
74 "branch-misses",
75 "bus-cycles",
76 "stalled-cycles-frontend",
77 "stalled-cycles-backend",
78 "ref-cycles",
81 const char *__perf_evsel__hw_name(u64 config)
83 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84 return perf_evsel__hw_names[config];
86 return "unknown-hardware";
89 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
91 int colon = 0, r = 0;
92 struct perf_event_attr *attr = &evsel->attr;
93 bool exclude_guest_default = false;
95 #define MOD_PRINT(context, mod) do { \
96 if (!attr->exclude_##context) { \
97 if (!colon) colon = ++r; \
98 r += scnprintf(bf + r, size - r, "%c", mod); \
99 } } while(0)
101 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
102 MOD_PRINT(kernel, 'k');
103 MOD_PRINT(user, 'u');
104 MOD_PRINT(hv, 'h');
105 exclude_guest_default = true;
108 if (attr->precise_ip) {
109 if (!colon)
110 colon = ++r;
111 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
112 exclude_guest_default = true;
115 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
116 MOD_PRINT(host, 'H');
117 MOD_PRINT(guest, 'G');
119 #undef MOD_PRINT
120 if (colon)
121 bf[colon - 1] = ':';
122 return r;
125 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
127 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
128 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
131 int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
133 int ret;
135 switch (evsel->attr.type) {
136 case PERF_TYPE_RAW:
137 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
138 break;
140 case PERF_TYPE_HARDWARE:
141 ret = perf_evsel__hw_name(evsel, bf, size);
142 break;
143 default:
145 * FIXME
147 * This is the minimal perf_evsel__name so that we can
148 * reconstruct event names taking into account event modifiers.
150 * The old event_name uses it now for raw anr hw events, so that
151 * we don't drag all the parsing stuff into the python binding.
153 * On the next devel cycle the rest of the event naming will be
154 * brought here.
156 return 0;
159 return ret;
162 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
163 struct perf_evsel *first)
165 struct perf_event_attr *attr = &evsel->attr;
166 int track = !evsel->idx; /* only the first counter needs these */
168 attr->disabled = 1;
169 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
170 attr->inherit = !opts->no_inherit;
171 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
172 PERF_FORMAT_TOTAL_TIME_RUNNING |
173 PERF_FORMAT_ID;
175 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
178 * We default some events to a 1 default interval. But keep
179 * it a weak assumption overridable by the user.
181 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
182 opts->user_interval != ULLONG_MAX)) {
183 if (opts->freq) {
184 attr->sample_type |= PERF_SAMPLE_PERIOD;
185 attr->freq = 1;
186 attr->sample_freq = opts->freq;
187 } else {
188 attr->sample_period = opts->default_interval;
192 if (opts->no_samples)
193 attr->sample_freq = 0;
195 if (opts->inherit_stat)
196 attr->inherit_stat = 1;
198 if (opts->sample_address) {
199 attr->sample_type |= PERF_SAMPLE_ADDR;
200 attr->mmap_data = track;
203 if (opts->call_graph)
204 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
206 if (perf_target__has_cpu(&opts->target))
207 attr->sample_type |= PERF_SAMPLE_CPU;
209 if (opts->period)
210 attr->sample_type |= PERF_SAMPLE_PERIOD;
212 if (!opts->sample_id_all_missing &&
213 (opts->sample_time || !opts->no_inherit ||
214 perf_target__has_cpu(&opts->target)))
215 attr->sample_type |= PERF_SAMPLE_TIME;
217 if (opts->raw_samples) {
218 attr->sample_type |= PERF_SAMPLE_TIME;
219 attr->sample_type |= PERF_SAMPLE_RAW;
220 attr->sample_type |= PERF_SAMPLE_CPU;
223 if (opts->no_delay) {
224 attr->watermark = 0;
225 attr->wakeup_events = 1;
227 if (opts->branch_stack) {
228 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
229 attr->branch_sample_type = opts->branch_stack;
232 attr->mmap = track;
233 attr->comm = track;
235 if (perf_target__none(&opts->target) &&
236 (!opts->group || evsel == first)) {
237 attr->enable_on_exec = 1;
241 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
243 int cpu, thread;
244 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
246 if (evsel->fd) {
247 for (cpu = 0; cpu < ncpus; cpu++) {
248 for (thread = 0; thread < nthreads; thread++) {
249 FD(evsel, cpu, thread) = -1;
254 return evsel->fd != NULL ? 0 : -ENOMEM;
257 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
259 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
260 if (evsel->sample_id == NULL)
261 return -ENOMEM;
263 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
264 if (evsel->id == NULL) {
265 xyarray__delete(evsel->sample_id);
266 evsel->sample_id = NULL;
267 return -ENOMEM;
270 return 0;
273 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
275 evsel->counts = zalloc((sizeof(*evsel->counts) +
276 (ncpus * sizeof(struct perf_counts_values))));
277 return evsel->counts != NULL ? 0 : -ENOMEM;
280 void perf_evsel__free_fd(struct perf_evsel *evsel)
282 xyarray__delete(evsel->fd);
283 evsel->fd = NULL;
286 void perf_evsel__free_id(struct perf_evsel *evsel)
288 xyarray__delete(evsel->sample_id);
289 evsel->sample_id = NULL;
290 free(evsel->id);
291 evsel->id = NULL;
294 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
296 int cpu, thread;
298 for (cpu = 0; cpu < ncpus; cpu++)
299 for (thread = 0; thread < nthreads; ++thread) {
300 close(FD(evsel, cpu, thread));
301 FD(evsel, cpu, thread) = -1;
305 void perf_evsel__exit(struct perf_evsel *evsel)
307 assert(list_empty(&evsel->node));
308 xyarray__delete(evsel->fd);
309 xyarray__delete(evsel->sample_id);
310 free(evsel->id);
313 void perf_evsel__delete(struct perf_evsel *evsel)
315 perf_evsel__exit(evsel);
316 close_cgroup(evsel->cgrp);
317 free(evsel->name);
318 free(evsel);
321 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
322 int cpu, int thread, bool scale)
324 struct perf_counts_values count;
325 size_t nv = scale ? 3 : 1;
327 if (FD(evsel, cpu, thread) < 0)
328 return -EINVAL;
330 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
331 return -ENOMEM;
333 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
334 return -errno;
336 if (scale) {
337 if (count.run == 0)
338 count.val = 0;
339 else if (count.run < count.ena)
340 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
341 } else
342 count.ena = count.run = 0;
344 evsel->counts->cpu[cpu] = count;
345 return 0;
348 int __perf_evsel__read(struct perf_evsel *evsel,
349 int ncpus, int nthreads, bool scale)
351 size_t nv = scale ? 3 : 1;
352 int cpu, thread;
353 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
355 aggr->val = aggr->ena = aggr->run = 0;
357 for (cpu = 0; cpu < ncpus; cpu++) {
358 for (thread = 0; thread < nthreads; thread++) {
359 if (FD(evsel, cpu, thread) < 0)
360 continue;
362 if (readn(FD(evsel, cpu, thread),
363 &count, nv * sizeof(u64)) < 0)
364 return -errno;
366 aggr->val += count.val;
367 if (scale) {
368 aggr->ena += count.ena;
369 aggr->run += count.run;
374 evsel->counts->scaled = 0;
375 if (scale) {
376 if (aggr->run == 0) {
377 evsel->counts->scaled = -1;
378 aggr->val = 0;
379 return 0;
382 if (aggr->run < aggr->ena) {
383 evsel->counts->scaled = 1;
384 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
386 } else
387 aggr->ena = aggr->run = 0;
389 return 0;
392 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
393 struct thread_map *threads, bool group,
394 struct xyarray *group_fds)
396 int cpu, thread;
397 unsigned long flags = 0;
398 int pid = -1, err;
400 if (evsel->fd == NULL &&
401 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
402 return -ENOMEM;
404 if (evsel->cgrp) {
405 flags = PERF_FLAG_PID_CGROUP;
406 pid = evsel->cgrp->fd;
409 for (cpu = 0; cpu < cpus->nr; cpu++) {
410 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
412 for (thread = 0; thread < threads->nr; thread++) {
414 if (!evsel->cgrp)
415 pid = threads->map[thread];
417 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
418 pid,
419 cpus->map[cpu],
420 group_fd, flags);
421 if (FD(evsel, cpu, thread) < 0) {
422 err = -errno;
423 goto out_close;
426 if (group && group_fd == -1)
427 group_fd = FD(evsel, cpu, thread);
431 return 0;
433 out_close:
434 do {
435 while (--thread >= 0) {
436 close(FD(evsel, cpu, thread));
437 FD(evsel, cpu, thread) = -1;
439 thread = threads->nr;
440 } while (--cpu >= 0);
441 return err;
444 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
446 if (evsel->fd == NULL)
447 return;
449 perf_evsel__close_fd(evsel, ncpus, nthreads);
450 perf_evsel__free_fd(evsel);
451 evsel->fd = NULL;
454 static struct {
455 struct cpu_map map;
456 int cpus[1];
457 } empty_cpu_map = {
458 .map.nr = 1,
459 .cpus = { -1, },
462 static struct {
463 struct thread_map map;
464 int threads[1];
465 } empty_thread_map = {
466 .map.nr = 1,
467 .threads = { -1, },
470 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
471 struct thread_map *threads, bool group,
472 struct xyarray *group_fd)
474 if (cpus == NULL) {
475 /* Work around old compiler warnings about strict aliasing */
476 cpus = &empty_cpu_map.map;
479 if (threads == NULL)
480 threads = &empty_thread_map.map;
482 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
485 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
486 struct cpu_map *cpus, bool group,
487 struct xyarray *group_fd)
489 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
490 group_fd);
493 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
494 struct thread_map *threads, bool group,
495 struct xyarray *group_fd)
497 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
498 group_fd);
501 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
502 struct perf_sample *sample,
503 bool swapped)
505 const u64 *array = event->sample.array;
506 union u64_swap u;
508 array += ((event->header.size -
509 sizeof(event->header)) / sizeof(u64)) - 1;
511 if (type & PERF_SAMPLE_CPU) {
512 u.val64 = *array;
513 if (swapped) {
514 /* undo swap of u64, then swap on individual u32s */
515 u.val64 = bswap_64(u.val64);
516 u.val32[0] = bswap_32(u.val32[0]);
519 sample->cpu = u.val32[0];
520 array--;
523 if (type & PERF_SAMPLE_STREAM_ID) {
524 sample->stream_id = *array;
525 array--;
528 if (type & PERF_SAMPLE_ID) {
529 sample->id = *array;
530 array--;
533 if (type & PERF_SAMPLE_TIME) {
534 sample->time = *array;
535 array--;
538 if (type & PERF_SAMPLE_TID) {
539 u.val64 = *array;
540 if (swapped) {
541 /* undo swap of u64, then swap on individual u32s */
542 u.val64 = bswap_64(u.val64);
543 u.val32[0] = bswap_32(u.val32[0]);
544 u.val32[1] = bswap_32(u.val32[1]);
547 sample->pid = u.val32[0];
548 sample->tid = u.val32[1];
551 return 0;
554 static bool sample_overlap(const union perf_event *event,
555 const void *offset, u64 size)
557 const void *base = event;
559 if (offset + size > base + event->header.size)
560 return true;
562 return false;
565 int perf_event__parse_sample(const union perf_event *event, u64 type,
566 int sample_size, bool sample_id_all,
567 struct perf_sample *data, bool swapped)
569 const u64 *array;
572 * used for cross-endian analysis. See git commit 65014ab3
573 * for why this goofiness is needed.
575 union u64_swap u;
577 memset(data, 0, sizeof(*data));
578 data->cpu = data->pid = data->tid = -1;
579 data->stream_id = data->id = data->time = -1ULL;
580 data->period = 1;
582 if (event->header.type != PERF_RECORD_SAMPLE) {
583 if (!sample_id_all)
584 return 0;
585 return perf_event__parse_id_sample(event, type, data, swapped);
588 array = event->sample.array;
590 if (sample_size + sizeof(event->header) > event->header.size)
591 return -EFAULT;
593 if (type & PERF_SAMPLE_IP) {
594 data->ip = event->ip.ip;
595 array++;
598 if (type & PERF_SAMPLE_TID) {
599 u.val64 = *array;
600 if (swapped) {
601 /* undo swap of u64, then swap on individual u32s */
602 u.val64 = bswap_64(u.val64);
603 u.val32[0] = bswap_32(u.val32[0]);
604 u.val32[1] = bswap_32(u.val32[1]);
607 data->pid = u.val32[0];
608 data->tid = u.val32[1];
609 array++;
612 if (type & PERF_SAMPLE_TIME) {
613 data->time = *array;
614 array++;
617 data->addr = 0;
618 if (type & PERF_SAMPLE_ADDR) {
619 data->addr = *array;
620 array++;
623 data->id = -1ULL;
624 if (type & PERF_SAMPLE_ID) {
625 data->id = *array;
626 array++;
629 if (type & PERF_SAMPLE_STREAM_ID) {
630 data->stream_id = *array;
631 array++;
634 if (type & PERF_SAMPLE_CPU) {
636 u.val64 = *array;
637 if (swapped) {
638 /* undo swap of u64, then swap on individual u32s */
639 u.val64 = bswap_64(u.val64);
640 u.val32[0] = bswap_32(u.val32[0]);
643 data->cpu = u.val32[0];
644 array++;
647 if (type & PERF_SAMPLE_PERIOD) {
648 data->period = *array;
649 array++;
652 if (type & PERF_SAMPLE_READ) {
653 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
654 return -1;
657 if (type & PERF_SAMPLE_CALLCHAIN) {
658 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
659 return -EFAULT;
661 data->callchain = (struct ip_callchain *)array;
663 if (sample_overlap(event, array, data->callchain->nr))
664 return -EFAULT;
666 array += 1 + data->callchain->nr;
669 if (type & PERF_SAMPLE_RAW) {
670 const u64 *pdata;
672 u.val64 = *array;
673 if (WARN_ONCE(swapped,
674 "Endianness of raw data not corrected!\n")) {
675 /* undo swap of u64, then swap on individual u32s */
676 u.val64 = bswap_64(u.val64);
677 u.val32[0] = bswap_32(u.val32[0]);
678 u.val32[1] = bswap_32(u.val32[1]);
681 if (sample_overlap(event, array, sizeof(u32)))
682 return -EFAULT;
684 data->raw_size = u.val32[0];
685 pdata = (void *) array + sizeof(u32);
687 if (sample_overlap(event, pdata, data->raw_size))
688 return -EFAULT;
690 data->raw_data = (void *) pdata;
692 array = (void *)array + data->raw_size + sizeof(u32);
695 if (type & PERF_SAMPLE_BRANCH_STACK) {
696 u64 sz;
698 data->branch_stack = (struct branch_stack *)array;
699 array++; /* nr */
701 sz = data->branch_stack->nr * sizeof(struct branch_entry);
702 sz /= sizeof(u64);
703 array += sz;
705 return 0;
708 int perf_event__synthesize_sample(union perf_event *event, u64 type,
709 const struct perf_sample *sample,
710 bool swapped)
712 u64 *array;
715 * used for cross-endian analysis. See git commit 65014ab3
716 * for why this goofiness is needed.
718 union u64_swap u;
720 array = event->sample.array;
722 if (type & PERF_SAMPLE_IP) {
723 event->ip.ip = sample->ip;
724 array++;
727 if (type & PERF_SAMPLE_TID) {
728 u.val32[0] = sample->pid;
729 u.val32[1] = sample->tid;
730 if (swapped) {
732 * Inverse of what is done in perf_event__parse_sample
734 u.val32[0] = bswap_32(u.val32[0]);
735 u.val32[1] = bswap_32(u.val32[1]);
736 u.val64 = bswap_64(u.val64);
739 *array = u.val64;
740 array++;
743 if (type & PERF_SAMPLE_TIME) {
744 *array = sample->time;
745 array++;
748 if (type & PERF_SAMPLE_ADDR) {
749 *array = sample->addr;
750 array++;
753 if (type & PERF_SAMPLE_ID) {
754 *array = sample->id;
755 array++;
758 if (type & PERF_SAMPLE_STREAM_ID) {
759 *array = sample->stream_id;
760 array++;
763 if (type & PERF_SAMPLE_CPU) {
764 u.val32[0] = sample->cpu;
765 if (swapped) {
767 * Inverse of what is done in perf_event__parse_sample
769 u.val32[0] = bswap_32(u.val32[0]);
770 u.val64 = bswap_64(u.val64);
772 *array = u.val64;
773 array++;
776 if (type & PERF_SAMPLE_PERIOD) {
777 *array = sample->period;
778 array++;
781 return 0;