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
7 * Released under the GPL v2. (and only v2, not any later version)
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>
22 #include "thread_map.h"
24 #include "perf_regs.h"
31 } perf_missing_features;
33 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
35 int __perf_evsel__sample_size(u64 sample_type)
37 u64 mask = sample_type & PERF_SAMPLE_MASK;
41 for (i = 0; i < 64; i++) {
42 if (mask & (1ULL << i))
52 * __perf_evsel__calc_id_pos - calculate id_pos.
53 * @sample_type: sample type
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
59 static int __perf_evsel__calc_id_pos(u64 sample_type)
63 if (sample_type & PERF_SAMPLE_IDENTIFIER)
66 if (!(sample_type & PERF_SAMPLE_ID))
69 if (sample_type & PERF_SAMPLE_IP)
72 if (sample_type & PERF_SAMPLE_TID)
75 if (sample_type & PERF_SAMPLE_TIME)
78 if (sample_type & PERF_SAMPLE_ADDR)
85 * __perf_evsel__calc_is_pos - calculate is_pos.
86 * @sample_type: sample type
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.
92 static int __perf_evsel__calc_is_pos(u64 sample_type)
96 if (sample_type & PERF_SAMPLE_IDENTIFIER)
99 if (!(sample_type & PERF_SAMPLE_ID))
102 if (sample_type & PERF_SAMPLE_CPU)
105 if (sample_type & PERF_SAMPLE_STREAM_ID)
111 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
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);
117 void hists__init(struct hists *hists)
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);
127 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
128 enum perf_event_sample_format bit)
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);
137 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
138 enum perf_event_sample_format bit)
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);
147 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
148 bool can_sample_identifier)
150 if (can_sample_identifier) {
151 perf_evsel__reset_sample_bit(evsel, ID);
152 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
154 perf_evsel__set_sample_bit(evsel, ID);
156 evsel->attr.read_format |= PERF_FORMAT_ID;
159 void perf_evsel__init(struct perf_evsel *evsel,
160 struct perf_event_attr *attr, int idx)
164 evsel->leader = evsel;
165 INIT_LIST_HEAD(&evsel->node);
166 hists__init(&evsel->hists);
167 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
168 perf_evsel__calc_id_pos(evsel);
171 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
173 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
176 perf_evsel__init(evsel, attr, idx);
181 struct event_format *event_format__new(const char *sys, const char *name)
185 void *bf = NULL, *nbf;
186 size_t size = 0, alloc_size = 0;
187 struct event_format *format = NULL;
189 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
192 fd = open(filename, O_RDONLY);
194 goto out_free_filename;
197 if (size == alloc_size) {
198 alloc_size += BUFSIZ;
199 nbf = realloc(bf, alloc_size);
205 n = read(fd, bf + size, alloc_size - size);
211 pevent_parse_format(&format, bf, size, sys);
222 struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
224 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
227 struct perf_event_attr attr = {
228 .type = PERF_TYPE_TRACEPOINT,
229 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
230 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
233 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
236 evsel->tp_format = event_format__new(sys, name);
237 if (evsel->tp_format == NULL)
240 event_attr_init(&attr);
241 attr.config = evsel->tp_format->id;
242 attr.sample_period = 1;
243 perf_evsel__init(evsel, &attr, idx);
254 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
262 "stalled-cycles-frontend",
263 "stalled-cycles-backend",
267 static const char *__perf_evsel__hw_name(u64 config)
269 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
270 return perf_evsel__hw_names[config];
272 return "unknown-hardware";
275 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
277 int colon = 0, r = 0;
278 struct perf_event_attr *attr = &evsel->attr;
279 bool exclude_guest_default = false;
281 #define MOD_PRINT(context, mod) do { \
282 if (!attr->exclude_##context) { \
283 if (!colon) colon = ++r; \
284 r += scnprintf(bf + r, size - r, "%c", mod); \
287 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
288 MOD_PRINT(kernel, 'k');
289 MOD_PRINT(user, 'u');
291 exclude_guest_default = true;
294 if (attr->precise_ip) {
297 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
298 exclude_guest_default = true;
301 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
302 MOD_PRINT(host, 'H');
303 MOD_PRINT(guest, 'G');
311 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
313 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
314 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
317 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
330 static const char *__perf_evsel__sw_name(u64 config)
332 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
333 return perf_evsel__sw_names[config];
334 return "unknown-software";
337 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
339 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
340 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
343 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
347 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
349 if (type & HW_BREAKPOINT_R)
350 r += scnprintf(bf + r, size - r, "r");
352 if (type & HW_BREAKPOINT_W)
353 r += scnprintf(bf + r, size - r, "w");
355 if (type & HW_BREAKPOINT_X)
356 r += scnprintf(bf + r, size - r, "x");
361 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
363 struct perf_event_attr *attr = &evsel->attr;
364 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
365 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
368 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
369 [PERF_EVSEL__MAX_ALIASES] = {
370 { "L1-dcache", "l1-d", "l1d", "L1-data", },
371 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
373 { "dTLB", "d-tlb", "Data-TLB", },
374 { "iTLB", "i-tlb", "Instruction-TLB", },
375 { "branch", "branches", "bpu", "btb", "bpc", },
379 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
380 [PERF_EVSEL__MAX_ALIASES] = {
381 { "load", "loads", "read", },
382 { "store", "stores", "write", },
383 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
386 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
387 [PERF_EVSEL__MAX_ALIASES] = {
388 { "refs", "Reference", "ops", "access", },
389 { "misses", "miss", },
392 #define C(x) PERF_COUNT_HW_CACHE_##x
393 #define CACHE_READ (1 << C(OP_READ))
394 #define CACHE_WRITE (1 << C(OP_WRITE))
395 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
396 #define COP(x) (1 << x)
399 * cache operartion stat
400 * L1I : Read and prefetch only
401 * ITLB and BPU : Read-only
403 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
404 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
406 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
407 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
408 [C(ITLB)] = (CACHE_READ),
409 [C(BPU)] = (CACHE_READ),
410 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
413 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
415 if (perf_evsel__hw_cache_stat[type] & COP(op))
416 return true; /* valid */
418 return false; /* invalid */
421 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
422 char *bf, size_t size)
425 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
426 perf_evsel__hw_cache_op[op][0],
427 perf_evsel__hw_cache_result[result][0]);
430 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
431 perf_evsel__hw_cache_op[op][1]);
434 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
436 u8 op, result, type = (config >> 0) & 0xff;
437 const char *err = "unknown-ext-hardware-cache-type";
439 if (type > PERF_COUNT_HW_CACHE_MAX)
442 op = (config >> 8) & 0xff;
443 err = "unknown-ext-hardware-cache-op";
444 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
447 result = (config >> 16) & 0xff;
448 err = "unknown-ext-hardware-cache-result";
449 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
452 err = "invalid-cache";
453 if (!perf_evsel__is_cache_op_valid(type, op))
456 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
458 return scnprintf(bf, size, "%s", err);
461 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
463 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
464 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
467 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
469 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
470 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
473 const char *perf_evsel__name(struct perf_evsel *evsel)
480 switch (evsel->attr.type) {
482 perf_evsel__raw_name(evsel, bf, sizeof(bf));
485 case PERF_TYPE_HARDWARE:
486 perf_evsel__hw_name(evsel, bf, sizeof(bf));
489 case PERF_TYPE_HW_CACHE:
490 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
493 case PERF_TYPE_SOFTWARE:
494 perf_evsel__sw_name(evsel, bf, sizeof(bf));
497 case PERF_TYPE_TRACEPOINT:
498 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
501 case PERF_TYPE_BREAKPOINT:
502 perf_evsel__bp_name(evsel, bf, sizeof(bf));
506 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
511 evsel->name = strdup(bf);
513 return evsel->name ?: "unknown";
516 const char *perf_evsel__group_name(struct perf_evsel *evsel)
518 return evsel->group_name ?: "anon group";
521 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
524 struct perf_evsel *pos;
525 const char *group_name = perf_evsel__group_name(evsel);
527 ret = scnprintf(buf, size, "%s", group_name);
529 ret += scnprintf(buf + ret, size - ret, " { %s",
530 perf_evsel__name(evsel));
532 for_each_group_member(pos, evsel)
533 ret += scnprintf(buf + ret, size - ret, ", %s",
534 perf_evsel__name(pos));
536 ret += scnprintf(buf + ret, size - ret, " }");
542 * The enable_on_exec/disabled value strategy:
544 * 1) For any type of traced program:
545 * - all independent events and group leaders are disabled
546 * - all group members are enabled
548 * Group members are ruled by group leaders. They need to
549 * be enabled, because the group scheduling relies on that.
551 * 2) For traced programs executed by perf:
552 * - all independent events and group leaders have
554 * - we don't specifically enable or disable any event during
557 * Independent events and group leaders are initially disabled
558 * and get enabled by exec. Group members are ruled by group
559 * leaders as stated in 1).
561 * 3) For traced programs attached by perf (pid/tid):
562 * - we specifically enable or disable all events during
565 * When attaching events to already running traced we
566 * enable/disable events specifically, as there's no
567 * initial traced exec call.
569 void perf_evsel__config(struct perf_evsel *evsel,
570 struct perf_record_opts *opts)
572 struct perf_evsel *leader = evsel->leader;
573 struct perf_event_attr *attr = &evsel->attr;
574 int track = !evsel->idx; /* only the first counter needs these */
576 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
577 attr->inherit = !opts->no_inherit;
579 perf_evsel__set_sample_bit(evsel, IP);
580 perf_evsel__set_sample_bit(evsel, TID);
582 if (evsel->sample_read) {
583 perf_evsel__set_sample_bit(evsel, READ);
586 * We need ID even in case of single event, because
587 * PERF_SAMPLE_READ process ID specific data.
589 perf_evsel__set_sample_id(evsel, false);
592 * Apply group format only if we belong to group
593 * with more than one members.
595 if (leader->nr_members > 1) {
596 attr->read_format |= PERF_FORMAT_GROUP;
602 * We default some events to a 1 default interval. But keep
603 * it a weak assumption overridable by the user.
605 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
606 opts->user_interval != ULLONG_MAX)) {
608 perf_evsel__set_sample_bit(evsel, PERIOD);
610 attr->sample_freq = opts->freq;
612 attr->sample_period = opts->default_interval;
617 * Disable sampling for all group members other
618 * than leader in case leader 'leads' the sampling.
620 if ((leader != evsel) && leader->sample_read) {
621 attr->sample_freq = 0;
622 attr->sample_period = 0;
625 if (opts->no_samples)
626 attr->sample_freq = 0;
628 if (opts->inherit_stat)
629 attr->inherit_stat = 1;
631 if (opts->sample_address) {
632 perf_evsel__set_sample_bit(evsel, ADDR);
633 attr->mmap_data = track;
636 if (opts->call_graph) {
637 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
639 if (opts->call_graph == CALLCHAIN_DWARF) {
640 perf_evsel__set_sample_bit(evsel, REGS_USER);
641 perf_evsel__set_sample_bit(evsel, STACK_USER);
642 attr->sample_regs_user = PERF_REGS_MASK;
643 attr->sample_stack_user = opts->stack_dump_size;
644 attr->exclude_callchain_user = 1;
648 if (perf_target__has_cpu(&opts->target))
649 perf_evsel__set_sample_bit(evsel, CPU);
652 perf_evsel__set_sample_bit(evsel, PERIOD);
654 if (!perf_missing_features.sample_id_all &&
655 (opts->sample_time || !opts->no_inherit ||
656 perf_target__has_cpu(&opts->target)))
657 perf_evsel__set_sample_bit(evsel, TIME);
659 if (opts->raw_samples) {
660 perf_evsel__set_sample_bit(evsel, TIME);
661 perf_evsel__set_sample_bit(evsel, RAW);
662 perf_evsel__set_sample_bit(evsel, CPU);
665 if (opts->sample_address)
666 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
668 if (opts->no_delay) {
670 attr->wakeup_events = 1;
672 if (opts->branch_stack) {
673 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
674 attr->branch_sample_type = opts->branch_stack;
677 if (opts->sample_weight)
678 attr->sample_type |= PERF_SAMPLE_WEIGHT;
681 attr->mmap2 = track && !perf_missing_features.mmap2;
684 if (opts->sample_transaction)
685 attr->sample_type |= PERF_SAMPLE_TRANSACTION;
688 * XXX see the function comment above
690 * Disabling only independent events or group leaders,
691 * keeping group members enabled.
693 if (perf_evsel__is_group_leader(evsel))
697 * Setting enable_on_exec for independent events and
698 * group leaders for traced executed by perf.
700 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
701 attr->enable_on_exec = 1;
704 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
707 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
710 for (cpu = 0; cpu < ncpus; cpu++) {
711 for (thread = 0; thread < nthreads; thread++) {
712 FD(evsel, cpu, thread) = -1;
717 return evsel->fd != NULL ? 0 : -ENOMEM;
720 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
725 for (cpu = 0; cpu < ncpus; cpu++) {
726 for (thread = 0; thread < nthreads; thread++) {
727 int fd = FD(evsel, cpu, thread),
728 err = ioctl(fd, ioc, arg);
738 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
741 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
742 PERF_EVENT_IOC_SET_FILTER,
746 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
748 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
749 PERF_EVENT_IOC_ENABLE,
753 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
755 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
756 if (evsel->sample_id == NULL)
759 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
760 if (evsel->id == NULL) {
761 xyarray__delete(evsel->sample_id);
762 evsel->sample_id = NULL;
769 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
771 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
772 (ncpus * sizeof(struct perf_counts_values))));
775 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
777 evsel->counts = zalloc((sizeof(*evsel->counts) +
778 (ncpus * sizeof(struct perf_counts_values))));
779 return evsel->counts != NULL ? 0 : -ENOMEM;
782 void perf_evsel__free_fd(struct perf_evsel *evsel)
784 xyarray__delete(evsel->fd);
788 void perf_evsel__free_id(struct perf_evsel *evsel)
790 xyarray__delete(evsel->sample_id);
791 evsel->sample_id = NULL;
796 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
800 for (cpu = 0; cpu < ncpus; cpu++)
801 for (thread = 0; thread < nthreads; ++thread) {
802 close(FD(evsel, cpu, thread));
803 FD(evsel, cpu, thread) = -1;
807 void perf_evsel__free_counts(struct perf_evsel *evsel)
812 void perf_evsel__exit(struct perf_evsel *evsel)
814 assert(list_empty(&evsel->node));
815 perf_evsel__free_fd(evsel);
816 perf_evsel__free_id(evsel);
819 void perf_evsel__delete(struct perf_evsel *evsel)
821 perf_evsel__exit(evsel);
822 close_cgroup(evsel->cgrp);
823 free(evsel->group_name);
824 if (evsel->tp_format)
825 pevent_free_format(evsel->tp_format);
830 static inline void compute_deltas(struct perf_evsel *evsel,
832 struct perf_counts_values *count)
834 struct perf_counts_values tmp;
836 if (!evsel->prev_raw_counts)
840 tmp = evsel->prev_raw_counts->aggr;
841 evsel->prev_raw_counts->aggr = *count;
843 tmp = evsel->prev_raw_counts->cpu[cpu];
844 evsel->prev_raw_counts->cpu[cpu] = *count;
847 count->val = count->val - tmp.val;
848 count->ena = count->ena - tmp.ena;
849 count->run = count->run - tmp.run;
852 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
853 int cpu, int thread, bool scale)
855 struct perf_counts_values count;
856 size_t nv = scale ? 3 : 1;
858 if (FD(evsel, cpu, thread) < 0)
861 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
864 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
867 compute_deltas(evsel, cpu, &count);
872 else if (count.run < count.ena)
873 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
875 count.ena = count.run = 0;
877 evsel->counts->cpu[cpu] = count;
881 int __perf_evsel__read(struct perf_evsel *evsel,
882 int ncpus, int nthreads, bool scale)
884 size_t nv = scale ? 3 : 1;
886 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
888 aggr->val = aggr->ena = aggr->run = 0;
890 for (cpu = 0; cpu < ncpus; cpu++) {
891 for (thread = 0; thread < nthreads; thread++) {
892 if (FD(evsel, cpu, thread) < 0)
895 if (readn(FD(evsel, cpu, thread),
896 &count, nv * sizeof(u64)) < 0)
899 aggr->val += count.val;
901 aggr->ena += count.ena;
902 aggr->run += count.run;
907 compute_deltas(evsel, -1, aggr);
909 evsel->counts->scaled = 0;
911 if (aggr->run == 0) {
912 evsel->counts->scaled = -1;
917 if (aggr->run < aggr->ena) {
918 evsel->counts->scaled = 1;
919 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
922 aggr->ena = aggr->run = 0;
927 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
929 struct perf_evsel *leader = evsel->leader;
932 if (perf_evsel__is_group_leader(evsel))
936 * Leader must be already processed/open,
941 fd = FD(leader, cpu, thread);
947 #define __PRINT_ATTR(fmt, cast, field) \
948 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
950 #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
951 #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
952 #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
953 #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
955 #define PRINT_ATTR2N(name1, field1, name2, field2) \
956 fprintf(fp, " %-19s %u %-19s %u\n", \
957 name1, attr->field1, name2, attr->field2)
959 #define PRINT_ATTR2(field1, field2) \
960 PRINT_ATTR2N(#field1, field1, #field2, field2)
962 static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
966 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
967 ret += fprintf(fp, "perf_event_attr:\n");
969 ret += PRINT_ATTR_U32(type);
970 ret += PRINT_ATTR_U32(size);
971 ret += PRINT_ATTR_X64(config);
972 ret += PRINT_ATTR_U64(sample_period);
973 ret += PRINT_ATTR_U64(sample_freq);
974 ret += PRINT_ATTR_X64(sample_type);
975 ret += PRINT_ATTR_X64(read_format);
977 ret += PRINT_ATTR2(disabled, inherit);
978 ret += PRINT_ATTR2(pinned, exclusive);
979 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
980 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
981 ret += PRINT_ATTR2(mmap, comm);
982 ret += PRINT_ATTR2(freq, inherit_stat);
983 ret += PRINT_ATTR2(enable_on_exec, task);
984 ret += PRINT_ATTR2(watermark, precise_ip);
985 ret += PRINT_ATTR2(mmap_data, sample_id_all);
986 ret += PRINT_ATTR2(exclude_host, exclude_guest);
987 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
988 "excl.callchain_user", exclude_callchain_user);
990 ret += PRINT_ATTR_U32(wakeup_events);
991 ret += PRINT_ATTR_U32(wakeup_watermark);
992 ret += PRINT_ATTR_X32(bp_type);
993 ret += PRINT_ATTR_X64(bp_addr);
994 ret += PRINT_ATTR_X64(config1);
995 ret += PRINT_ATTR_U64(bp_len);
996 ret += PRINT_ATTR_X64(config2);
997 ret += PRINT_ATTR_X64(branch_sample_type);
998 ret += PRINT_ATTR_X64(sample_regs_user);
999 ret += PRINT_ATTR_U32(sample_stack_user);
1001 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1006 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1007 struct thread_map *threads)
1010 unsigned long flags = 0;
1012 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1014 if (evsel->fd == NULL &&
1015 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
1019 flags = PERF_FLAG_PID_CGROUP;
1020 pid = evsel->cgrp->fd;
1023 fallback_missing_features:
1024 if (perf_missing_features.mmap2)
1025 evsel->attr.mmap2 = 0;
1026 if (perf_missing_features.exclude_guest)
1027 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1029 if (perf_missing_features.sample_id_all)
1030 evsel->attr.sample_id_all = 0;
1033 perf_event_attr__fprintf(&evsel->attr, stderr);
1035 for (cpu = 0; cpu < cpus->nr; cpu++) {
1037 for (thread = 0; thread < threads->nr; thread++) {
1041 pid = threads->map[thread];
1043 group_fd = get_group_fd(evsel, cpu, thread);
1045 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1046 pid, cpus->map[cpu], group_fd, flags);
1048 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1052 if (FD(evsel, cpu, thread) < 0) {
1056 set_rlimit = NO_CHANGE;
1064 * perf stat needs between 5 and 22 fds per CPU. When we run out
1065 * of them try to increase the limits.
1067 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1069 int old_errno = errno;
1071 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1072 if (set_rlimit == NO_CHANGE)
1073 l.rlim_cur = l.rlim_max;
1075 l.rlim_cur = l.rlim_max + 1000;
1076 l.rlim_max = l.rlim_cur;
1078 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1087 if (err != -EINVAL || cpu > 0 || thread > 0)
1090 if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1091 perf_missing_features.mmap2 = true;
1092 goto fallback_missing_features;
1093 } else if (!perf_missing_features.exclude_guest &&
1094 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1095 perf_missing_features.exclude_guest = true;
1096 goto fallback_missing_features;
1097 } else if (!perf_missing_features.sample_id_all) {
1098 perf_missing_features.sample_id_all = true;
1099 goto retry_sample_id;
1104 while (--thread >= 0) {
1105 close(FD(evsel, cpu, thread));
1106 FD(evsel, cpu, thread) = -1;
1108 thread = threads->nr;
1109 } while (--cpu >= 0);
1113 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1115 if (evsel->fd == NULL)
1118 perf_evsel__close_fd(evsel, ncpus, nthreads);
1119 perf_evsel__free_fd(evsel);
1132 struct thread_map map;
1134 } empty_thread_map = {
1139 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1140 struct thread_map *threads)
1143 /* Work around old compiler warnings about strict aliasing */
1144 cpus = &empty_cpu_map.map;
1147 if (threads == NULL)
1148 threads = &empty_thread_map.map;
1150 return __perf_evsel__open(evsel, cpus, threads);
1153 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1154 struct cpu_map *cpus)
1156 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1159 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1160 struct thread_map *threads)
1162 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1165 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1166 const union perf_event *event,
1167 struct perf_sample *sample)
1169 u64 type = evsel->attr.sample_type;
1170 const u64 *array = event->sample.array;
1171 bool swapped = evsel->needs_swap;
1174 array += ((event->header.size -
1175 sizeof(event->header)) / sizeof(u64)) - 1;
1177 if (type & PERF_SAMPLE_IDENTIFIER) {
1178 sample->id = *array;
1182 if (type & PERF_SAMPLE_CPU) {
1185 /* undo swap of u64, then swap on individual u32s */
1186 u.val64 = bswap_64(u.val64);
1187 u.val32[0] = bswap_32(u.val32[0]);
1190 sample->cpu = u.val32[0];
1194 if (type & PERF_SAMPLE_STREAM_ID) {
1195 sample->stream_id = *array;
1199 if (type & PERF_SAMPLE_ID) {
1200 sample->id = *array;
1204 if (type & PERF_SAMPLE_TIME) {
1205 sample->time = *array;
1209 if (type & PERF_SAMPLE_TID) {
1212 /* undo swap of u64, then swap on individual u32s */
1213 u.val64 = bswap_64(u.val64);
1214 u.val32[0] = bswap_32(u.val32[0]);
1215 u.val32[1] = bswap_32(u.val32[1]);
1218 sample->pid = u.val32[0];
1219 sample->tid = u.val32[1];
1225 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1228 return size > max_size || offset + size > endp;
1231 #define OVERFLOW_CHECK(offset, size, max_size) \
1233 if (overflow(endp, (max_size), (offset), (size))) \
1237 #define OVERFLOW_CHECK_u64(offset) \
1238 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1240 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1241 struct perf_sample *data)
1243 u64 type = evsel->attr.sample_type;
1244 bool swapped = evsel->needs_swap;
1246 u16 max_size = event->header.size;
1247 const void *endp = (void *)event + max_size;
1251 * used for cross-endian analysis. See git commit 65014ab3
1252 * for why this goofiness is needed.
1256 memset(data, 0, sizeof(*data));
1257 data->cpu = data->pid = data->tid = -1;
1258 data->stream_id = data->id = data->time = -1ULL;
1262 if (event->header.type != PERF_RECORD_SAMPLE) {
1263 if (!evsel->attr.sample_id_all)
1265 return perf_evsel__parse_id_sample(evsel, event, data);
1268 array = event->sample.array;
1271 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1272 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1273 * check the format does not go past the end of the event.
1275 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1279 if (type & PERF_SAMPLE_IDENTIFIER) {
1284 if (type & PERF_SAMPLE_IP) {
1289 if (type & PERF_SAMPLE_TID) {
1292 /* undo swap of u64, then swap on individual u32s */
1293 u.val64 = bswap_64(u.val64);
1294 u.val32[0] = bswap_32(u.val32[0]);
1295 u.val32[1] = bswap_32(u.val32[1]);
1298 data->pid = u.val32[0];
1299 data->tid = u.val32[1];
1303 if (type & PERF_SAMPLE_TIME) {
1304 data->time = *array;
1309 if (type & PERF_SAMPLE_ADDR) {
1310 data->addr = *array;
1314 if (type & PERF_SAMPLE_ID) {
1319 if (type & PERF_SAMPLE_STREAM_ID) {
1320 data->stream_id = *array;
1324 if (type & PERF_SAMPLE_CPU) {
1328 /* undo swap of u64, then swap on individual u32s */
1329 u.val64 = bswap_64(u.val64);
1330 u.val32[0] = bswap_32(u.val32[0]);
1333 data->cpu = u.val32[0];
1337 if (type & PERF_SAMPLE_PERIOD) {
1338 data->period = *array;
1342 if (type & PERF_SAMPLE_READ) {
1343 u64 read_format = evsel->attr.read_format;
1345 OVERFLOW_CHECK_u64(array);
1346 if (read_format & PERF_FORMAT_GROUP)
1347 data->read.group.nr = *array;
1349 data->read.one.value = *array;
1353 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1354 OVERFLOW_CHECK_u64(array);
1355 data->read.time_enabled = *array;
1359 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1360 OVERFLOW_CHECK_u64(array);
1361 data->read.time_running = *array;
1365 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1366 if (read_format & PERF_FORMAT_GROUP) {
1367 const u64 max_group_nr = UINT64_MAX /
1368 sizeof(struct sample_read_value);
1370 if (data->read.group.nr > max_group_nr)
1372 sz = data->read.group.nr *
1373 sizeof(struct sample_read_value);
1374 OVERFLOW_CHECK(array, sz, max_size);
1375 data->read.group.values =
1376 (struct sample_read_value *)array;
1377 array = (void *)array + sz;
1379 OVERFLOW_CHECK_u64(array);
1380 data->read.one.id = *array;
1385 if (type & PERF_SAMPLE_CALLCHAIN) {
1386 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1388 OVERFLOW_CHECK_u64(array);
1389 data->callchain = (struct ip_callchain *)array++;
1390 if (data->callchain->nr > max_callchain_nr)
1392 sz = data->callchain->nr * sizeof(u64);
1393 OVERFLOW_CHECK(array, sz, max_size);
1394 array = (void *)array + sz;
1397 if (type & PERF_SAMPLE_RAW) {
1398 OVERFLOW_CHECK_u64(array);
1400 if (WARN_ONCE(swapped,
1401 "Endianness of raw data not corrected!\n")) {
1402 /* undo swap of u64, then swap on individual u32s */
1403 u.val64 = bswap_64(u.val64);
1404 u.val32[0] = bswap_32(u.val32[0]);
1405 u.val32[1] = bswap_32(u.val32[1]);
1407 data->raw_size = u.val32[0];
1408 array = (void *)array + sizeof(u32);
1410 OVERFLOW_CHECK(array, data->raw_size, max_size);
1411 data->raw_data = (void *)array;
1412 array = (void *)array + data->raw_size;
1415 if (type & PERF_SAMPLE_BRANCH_STACK) {
1416 const u64 max_branch_nr = UINT64_MAX /
1417 sizeof(struct branch_entry);
1419 OVERFLOW_CHECK_u64(array);
1420 data->branch_stack = (struct branch_stack *)array++;
1422 if (data->branch_stack->nr > max_branch_nr)
1424 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1425 OVERFLOW_CHECK(array, sz, max_size);
1426 array = (void *)array + sz;
1429 if (type & PERF_SAMPLE_REGS_USER) {
1430 OVERFLOW_CHECK_u64(array);
1431 data->user_regs.abi = *array;
1434 if (data->user_regs.abi) {
1435 u64 regs_user = evsel->attr.sample_regs_user;
1437 sz = hweight_long(regs_user) * sizeof(u64);
1438 OVERFLOW_CHECK(array, sz, max_size);
1439 data->user_regs.regs = (u64 *)array;
1440 array = (void *)array + sz;
1444 if (type & PERF_SAMPLE_STACK_USER) {
1445 OVERFLOW_CHECK_u64(array);
1448 data->user_stack.offset = ((char *)(array - 1)
1452 data->user_stack.size = 0;
1454 OVERFLOW_CHECK(array, sz, max_size);
1455 data->user_stack.data = (char *)array;
1456 array = (void *)array + sz;
1457 OVERFLOW_CHECK_u64(array);
1458 data->user_stack.size = *array++;
1463 if (type & PERF_SAMPLE_WEIGHT) {
1464 OVERFLOW_CHECK_u64(array);
1465 data->weight = *array;
1469 data->data_src = PERF_MEM_DATA_SRC_NONE;
1470 if (type & PERF_SAMPLE_DATA_SRC) {
1471 OVERFLOW_CHECK_u64(array);
1472 data->data_src = *array;
1476 data->transaction = 0;
1477 if (type & PERF_SAMPLE_TRANSACTION) {
1478 data->transaction = *array;
1485 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1486 u64 sample_regs_user, u64 read_format)
1488 size_t sz, result = sizeof(struct sample_event);
1490 if (type & PERF_SAMPLE_IDENTIFIER)
1491 result += sizeof(u64);
1493 if (type & PERF_SAMPLE_IP)
1494 result += sizeof(u64);
1496 if (type & PERF_SAMPLE_TID)
1497 result += sizeof(u64);
1499 if (type & PERF_SAMPLE_TIME)
1500 result += sizeof(u64);
1502 if (type & PERF_SAMPLE_ADDR)
1503 result += sizeof(u64);
1505 if (type & PERF_SAMPLE_ID)
1506 result += sizeof(u64);
1508 if (type & PERF_SAMPLE_STREAM_ID)
1509 result += sizeof(u64);
1511 if (type & PERF_SAMPLE_CPU)
1512 result += sizeof(u64);
1514 if (type & PERF_SAMPLE_PERIOD)
1515 result += sizeof(u64);
1517 if (type & PERF_SAMPLE_READ) {
1518 result += sizeof(u64);
1519 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1520 result += sizeof(u64);
1521 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1522 result += sizeof(u64);
1523 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1524 if (read_format & PERF_FORMAT_GROUP) {
1525 sz = sample->read.group.nr *
1526 sizeof(struct sample_read_value);
1529 result += sizeof(u64);
1533 if (type & PERF_SAMPLE_CALLCHAIN) {
1534 sz = (sample->callchain->nr + 1) * sizeof(u64);
1538 if (type & PERF_SAMPLE_RAW) {
1539 result += sizeof(u32);
1540 result += sample->raw_size;
1543 if (type & PERF_SAMPLE_BRANCH_STACK) {
1544 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1549 if (type & PERF_SAMPLE_REGS_USER) {
1550 if (sample->user_regs.abi) {
1551 result += sizeof(u64);
1552 sz = hweight_long(sample_regs_user) * sizeof(u64);
1555 result += sizeof(u64);
1559 if (type & PERF_SAMPLE_STACK_USER) {
1560 sz = sample->user_stack.size;
1561 result += sizeof(u64);
1564 result += sizeof(u64);
1568 if (type & PERF_SAMPLE_WEIGHT)
1569 result += sizeof(u64);
1571 if (type & PERF_SAMPLE_DATA_SRC)
1572 result += sizeof(u64);
1577 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1578 u64 sample_regs_user, u64 read_format,
1579 const struct perf_sample *sample,
1585 * used for cross-endian analysis. See git commit 65014ab3
1586 * for why this goofiness is needed.
1590 array = event->sample.array;
1592 if (type & PERF_SAMPLE_IDENTIFIER) {
1593 *array = sample->id;
1597 if (type & PERF_SAMPLE_IP) {
1598 *array = sample->ip;
1602 if (type & PERF_SAMPLE_TID) {
1603 u.val32[0] = sample->pid;
1604 u.val32[1] = sample->tid;
1607 * Inverse of what is done in perf_evsel__parse_sample
1609 u.val32[0] = bswap_32(u.val32[0]);
1610 u.val32[1] = bswap_32(u.val32[1]);
1611 u.val64 = bswap_64(u.val64);
1618 if (type & PERF_SAMPLE_TIME) {
1619 *array = sample->time;
1623 if (type & PERF_SAMPLE_ADDR) {
1624 *array = sample->addr;
1628 if (type & PERF_SAMPLE_ID) {
1629 *array = sample->id;
1633 if (type & PERF_SAMPLE_STREAM_ID) {
1634 *array = sample->stream_id;
1638 if (type & PERF_SAMPLE_CPU) {
1639 u.val32[0] = sample->cpu;
1642 * Inverse of what is done in perf_evsel__parse_sample
1644 u.val32[0] = bswap_32(u.val32[0]);
1645 u.val64 = bswap_64(u.val64);
1651 if (type & PERF_SAMPLE_PERIOD) {
1652 *array = sample->period;
1656 if (type & PERF_SAMPLE_READ) {
1657 if (read_format & PERF_FORMAT_GROUP)
1658 *array = sample->read.group.nr;
1660 *array = sample->read.one.value;
1663 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1664 *array = sample->read.time_enabled;
1668 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1669 *array = sample->read.time_running;
1673 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1674 if (read_format & PERF_FORMAT_GROUP) {
1675 sz = sample->read.group.nr *
1676 sizeof(struct sample_read_value);
1677 memcpy(array, sample->read.group.values, sz);
1678 array = (void *)array + sz;
1680 *array = sample->read.one.id;
1685 if (type & PERF_SAMPLE_CALLCHAIN) {
1686 sz = (sample->callchain->nr + 1) * sizeof(u64);
1687 memcpy(array, sample->callchain, sz);
1688 array = (void *)array + sz;
1691 if (type & PERF_SAMPLE_RAW) {
1692 u.val32[0] = sample->raw_size;
1693 if (WARN_ONCE(swapped,
1694 "Endianness of raw data not corrected!\n")) {
1696 * Inverse of what is done in perf_evsel__parse_sample
1698 u.val32[0] = bswap_32(u.val32[0]);
1699 u.val32[1] = bswap_32(u.val32[1]);
1700 u.val64 = bswap_64(u.val64);
1703 array = (void *)array + sizeof(u32);
1705 memcpy(array, sample->raw_data, sample->raw_size);
1706 array = (void *)array + sample->raw_size;
1709 if (type & PERF_SAMPLE_BRANCH_STACK) {
1710 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1712 memcpy(array, sample->branch_stack, sz);
1713 array = (void *)array + sz;
1716 if (type & PERF_SAMPLE_REGS_USER) {
1717 if (sample->user_regs.abi) {
1718 *array++ = sample->user_regs.abi;
1719 sz = hweight_long(sample_regs_user) * sizeof(u64);
1720 memcpy(array, sample->user_regs.regs, sz);
1721 array = (void *)array + sz;
1727 if (type & PERF_SAMPLE_STACK_USER) {
1728 sz = sample->user_stack.size;
1731 memcpy(array, sample->user_stack.data, sz);
1732 array = (void *)array + sz;
1737 if (type & PERF_SAMPLE_WEIGHT) {
1738 *array = sample->weight;
1742 if (type & PERF_SAMPLE_DATA_SRC) {
1743 *array = sample->data_src;
1750 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1752 return pevent_find_field(evsel->tp_format, name);
1755 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1758 struct format_field *field = perf_evsel__field(evsel, name);
1764 offset = field->offset;
1766 if (field->flags & FIELD_IS_DYNAMIC) {
1767 offset = *(int *)(sample->raw_data + field->offset);
1771 return sample->raw_data + offset;
1774 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1777 struct format_field *field = perf_evsel__field(evsel, name);
1784 ptr = sample->raw_data + field->offset;
1786 switch (field->size) {
1790 value = *(u16 *)ptr;
1793 value = *(u32 *)ptr;
1796 value = *(u64 *)ptr;
1802 if (!evsel->needs_swap)
1805 switch (field->size) {
1807 return bswap_16(value);
1809 return bswap_32(value);
1811 return bswap_64(value);
1819 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1825 ret += fprintf(fp, ",");
1827 ret += fprintf(fp, ":");
1831 va_start(args, fmt);
1832 ret += vfprintf(fp, fmt, args);
1837 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1842 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1845 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1852 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1853 struct bit_names *bits, bool *first)
1855 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1856 bool first_bit = true;
1859 if (value & bits[i].bit) {
1860 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1863 } while (bits[++i].name != NULL);
1868 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1870 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1871 struct bit_names bits[] = {
1872 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1873 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1874 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1875 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1876 bit_name(IDENTIFIER),
1880 return bits__fprintf(fp, "sample_type", value, bits, first);
1883 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1885 #define bit_name(n) { PERF_FORMAT_##n, #n }
1886 struct bit_names bits[] = {
1887 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1888 bit_name(ID), bit_name(GROUP),
1892 return bits__fprintf(fp, "read_format", value, bits, first);
1895 int perf_evsel__fprintf(struct perf_evsel *evsel,
1896 struct perf_attr_details *details, FILE *fp)
1901 if (details->event_group) {
1902 struct perf_evsel *pos;
1904 if (!perf_evsel__is_group_leader(evsel))
1907 if (evsel->nr_members > 1)
1908 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1910 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1911 for_each_group_member(pos, evsel)
1912 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1914 if (evsel->nr_members > 1)
1915 printed += fprintf(fp, "}");
1919 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1921 if (details->verbose || details->freq) {
1922 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1923 (u64)evsel->attr.sample_freq);
1926 if (details->verbose) {
1932 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1933 if (evsel->attr.read_format)
1934 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
1938 if_print(exclusive);
1939 if_print(exclude_user);
1940 if_print(exclude_kernel);
1941 if_print(exclude_hv);
1942 if_print(exclude_idle);
1947 if_print(inherit_stat);
1948 if_print(enable_on_exec);
1950 if_print(watermark);
1951 if_print(precise_ip);
1952 if_print(mmap_data);
1953 if_print(sample_id_all);
1954 if_print(exclude_host);
1955 if_print(exclude_guest);
1956 if_print(__reserved_1);
1957 if_print(wakeup_events);
1959 if_print(branch_sample_type);
1966 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1967 char *msg, size_t msgsize)
1969 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
1970 evsel->attr.type == PERF_TYPE_HARDWARE &&
1971 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1973 * If it's cycles then fall back to hrtimer based
1974 * cpu-clock-tick sw counter, which is always available even if
1977 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1980 scnprintf(msg, msgsize, "%s",
1981 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1983 evsel->attr.type = PERF_TYPE_SOFTWARE;
1984 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1994 int perf_evsel__open_strerror(struct perf_evsel *evsel,
1995 struct perf_target *target,
1996 int err, char *msg, size_t size)
2001 return scnprintf(msg, size,
2002 "You may not have permission to collect %sstats.\n"
2003 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2004 " -1 - Not paranoid at all\n"
2005 " 0 - Disallow raw tracepoint access for unpriv\n"
2006 " 1 - Disallow cpu events for unpriv\n"
2007 " 2 - Disallow kernel profiling for unpriv",
2008 target->system_wide ? "system-wide " : "");
2010 return scnprintf(msg, size, "The %s event is not supported.",
2011 perf_evsel__name(evsel));
2013 return scnprintf(msg, size, "%s",
2014 "Too many events are opened.\n"
2015 "Try again after reducing the number of events.");
2017 if (target->cpu_list)
2018 return scnprintf(msg, size, "%s",
2019 "No such device - did you specify an out-of-range profile CPU?\n");
2022 if (evsel->attr.precise_ip)
2023 return scnprintf(msg, size, "%s",
2024 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2025 #if defined(__i386__) || defined(__x86_64__)
2026 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2027 return scnprintf(msg, size, "%s",
2028 "No hardware sampling interrupt available.\n"
2029 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2036 return scnprintf(msg, size,
2037 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
2038 "/bin/dmesg may provide additional information.\n"
2039 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2040 err, strerror(err), perf_evsel__name(evsel));