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"
30 } perf_missing_features;
32 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34 int __perf_evsel__sample_size(u64 sample_type)
36 u64 mask = sample_type & PERF_SAMPLE_MASK;
40 for (i = 0; i < 64; i++) {
41 if (mask & (1ULL << i))
51 * __perf_evsel__calc_id_pos - calculate id_pos.
52 * @sample_type: sample type
54 * This function returns the position of the event id (PERF_SAMPLE_ID or
55 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
58 static int __perf_evsel__calc_id_pos(u64 sample_type)
62 if (sample_type & PERF_SAMPLE_IDENTIFIER)
65 if (!(sample_type & PERF_SAMPLE_ID))
68 if (sample_type & PERF_SAMPLE_IP)
71 if (sample_type & PERF_SAMPLE_TID)
74 if (sample_type & PERF_SAMPLE_TIME)
77 if (sample_type & PERF_SAMPLE_ADDR)
84 * __perf_evsel__calc_is_pos - calculate is_pos.
85 * @sample_type: sample type
87 * This function returns the position (counting backwards) of the event id
88 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
89 * sample_id_all is used there is an id sample appended to non-sample events.
91 static int __perf_evsel__calc_is_pos(u64 sample_type)
95 if (sample_type & PERF_SAMPLE_IDENTIFIER)
98 if (!(sample_type & PERF_SAMPLE_ID))
101 if (sample_type & PERF_SAMPLE_CPU)
104 if (sample_type & PERF_SAMPLE_STREAM_ID)
110 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
112 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
113 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
116 void hists__init(struct hists *hists)
118 memset(hists, 0, sizeof(*hists));
119 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
120 hists->entries_in = &hists->entries_in_array[0];
121 hists->entries_collapsed = RB_ROOT;
122 hists->entries = RB_ROOT;
123 pthread_mutex_init(&hists->lock, NULL);
126 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
127 enum perf_event_sample_format bit)
129 if (!(evsel->attr.sample_type & bit)) {
130 evsel->attr.sample_type |= bit;
131 evsel->sample_size += sizeof(u64);
132 perf_evsel__calc_id_pos(evsel);
136 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
137 enum perf_event_sample_format bit)
139 if (evsel->attr.sample_type & bit) {
140 evsel->attr.sample_type &= ~bit;
141 evsel->sample_size -= sizeof(u64);
142 perf_evsel__calc_id_pos(evsel);
146 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
147 bool can_sample_identifier)
149 if (can_sample_identifier) {
150 perf_evsel__reset_sample_bit(evsel, ID);
151 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
153 perf_evsel__set_sample_bit(evsel, ID);
155 evsel->attr.read_format |= PERF_FORMAT_ID;
158 void perf_evsel__init(struct perf_evsel *evsel,
159 struct perf_event_attr *attr, int idx)
163 evsel->leader = evsel;
164 INIT_LIST_HEAD(&evsel->node);
165 hists__init(&evsel->hists);
166 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
167 perf_evsel__calc_id_pos(evsel);
170 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
172 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
175 perf_evsel__init(evsel, attr, idx);
180 struct event_format *event_format__new(const char *sys, const char *name)
184 void *bf = NULL, *nbf;
185 size_t size = 0, alloc_size = 0;
186 struct event_format *format = NULL;
188 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
191 fd = open(filename, O_RDONLY);
193 goto out_free_filename;
196 if (size == alloc_size) {
197 alloc_size += BUFSIZ;
198 nbf = realloc(bf, alloc_size);
204 n = read(fd, bf + size, alloc_size - size);
210 pevent_parse_format(&format, bf, size, sys);
221 struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
223 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
226 struct perf_event_attr attr = {
227 .type = PERF_TYPE_TRACEPOINT,
228 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
229 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
232 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
235 evsel->tp_format = event_format__new(sys, name);
236 if (evsel->tp_format == NULL)
239 event_attr_init(&attr);
240 attr.config = evsel->tp_format->id;
241 attr.sample_period = 1;
242 perf_evsel__init(evsel, &attr, idx);
253 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
261 "stalled-cycles-frontend",
262 "stalled-cycles-backend",
266 static const char *__perf_evsel__hw_name(u64 config)
268 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
269 return perf_evsel__hw_names[config];
271 return "unknown-hardware";
274 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
276 int colon = 0, r = 0;
277 struct perf_event_attr *attr = &evsel->attr;
278 bool exclude_guest_default = false;
280 #define MOD_PRINT(context, mod) do { \
281 if (!attr->exclude_##context) { \
282 if (!colon) colon = ++r; \
283 r += scnprintf(bf + r, size - r, "%c", mod); \
286 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
287 MOD_PRINT(kernel, 'k');
288 MOD_PRINT(user, 'u');
290 exclude_guest_default = true;
293 if (attr->precise_ip) {
296 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
297 exclude_guest_default = true;
300 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
301 MOD_PRINT(host, 'H');
302 MOD_PRINT(guest, 'G');
310 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
312 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
313 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
316 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
328 static const char *__perf_evsel__sw_name(u64 config)
330 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
331 return perf_evsel__sw_names[config];
332 return "unknown-software";
335 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
337 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
338 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
341 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
345 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
347 if (type & HW_BREAKPOINT_R)
348 r += scnprintf(bf + r, size - r, "r");
350 if (type & HW_BREAKPOINT_W)
351 r += scnprintf(bf + r, size - r, "w");
353 if (type & HW_BREAKPOINT_X)
354 r += scnprintf(bf + r, size - r, "x");
359 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
361 struct perf_event_attr *attr = &evsel->attr;
362 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
363 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
366 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
367 [PERF_EVSEL__MAX_ALIASES] = {
368 { "L1-dcache", "l1-d", "l1d", "L1-data", },
369 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
371 { "dTLB", "d-tlb", "Data-TLB", },
372 { "iTLB", "i-tlb", "Instruction-TLB", },
373 { "branch", "branches", "bpu", "btb", "bpc", },
377 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
378 [PERF_EVSEL__MAX_ALIASES] = {
379 { "load", "loads", "read", },
380 { "store", "stores", "write", },
381 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
384 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
385 [PERF_EVSEL__MAX_ALIASES] = {
386 { "refs", "Reference", "ops", "access", },
387 { "misses", "miss", },
390 #define C(x) PERF_COUNT_HW_CACHE_##x
391 #define CACHE_READ (1 << C(OP_READ))
392 #define CACHE_WRITE (1 << C(OP_WRITE))
393 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
394 #define COP(x) (1 << x)
397 * cache operartion stat
398 * L1I : Read and prefetch only
399 * ITLB and BPU : Read-only
401 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
402 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
403 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
404 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
406 [C(ITLB)] = (CACHE_READ),
407 [C(BPU)] = (CACHE_READ),
408 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
411 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
413 if (perf_evsel__hw_cache_stat[type] & COP(op))
414 return true; /* valid */
416 return false; /* invalid */
419 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
420 char *bf, size_t size)
423 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
424 perf_evsel__hw_cache_op[op][0],
425 perf_evsel__hw_cache_result[result][0]);
428 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
429 perf_evsel__hw_cache_op[op][1]);
432 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
434 u8 op, result, type = (config >> 0) & 0xff;
435 const char *err = "unknown-ext-hardware-cache-type";
437 if (type > PERF_COUNT_HW_CACHE_MAX)
440 op = (config >> 8) & 0xff;
441 err = "unknown-ext-hardware-cache-op";
442 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
445 result = (config >> 16) & 0xff;
446 err = "unknown-ext-hardware-cache-result";
447 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
450 err = "invalid-cache";
451 if (!perf_evsel__is_cache_op_valid(type, op))
454 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
456 return scnprintf(bf, size, "%s", err);
459 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
461 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
462 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
465 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
467 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
468 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
471 const char *perf_evsel__name(struct perf_evsel *evsel)
478 switch (evsel->attr.type) {
480 perf_evsel__raw_name(evsel, bf, sizeof(bf));
483 case PERF_TYPE_HARDWARE:
484 perf_evsel__hw_name(evsel, bf, sizeof(bf));
487 case PERF_TYPE_HW_CACHE:
488 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
491 case PERF_TYPE_SOFTWARE:
492 perf_evsel__sw_name(evsel, bf, sizeof(bf));
495 case PERF_TYPE_TRACEPOINT:
496 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
499 case PERF_TYPE_BREAKPOINT:
500 perf_evsel__bp_name(evsel, bf, sizeof(bf));
504 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
509 evsel->name = strdup(bf);
511 return evsel->name ?: "unknown";
514 const char *perf_evsel__group_name(struct perf_evsel *evsel)
516 return evsel->group_name ?: "anon group";
519 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
522 struct perf_evsel *pos;
523 const char *group_name = perf_evsel__group_name(evsel);
525 ret = scnprintf(buf, size, "%s", group_name);
527 ret += scnprintf(buf + ret, size - ret, " { %s",
528 perf_evsel__name(evsel));
530 for_each_group_member(pos, evsel)
531 ret += scnprintf(buf + ret, size - ret, ", %s",
532 perf_evsel__name(pos));
534 ret += scnprintf(buf + ret, size - ret, " }");
540 * The enable_on_exec/disabled value strategy:
542 * 1) For any type of traced program:
543 * - all independent events and group leaders are disabled
544 * - all group members are enabled
546 * Group members are ruled by group leaders. They need to
547 * be enabled, because the group scheduling relies on that.
549 * 2) For traced programs executed by perf:
550 * - all independent events and group leaders have
552 * - we don't specifically enable or disable any event during
555 * Independent events and group leaders are initially disabled
556 * and get enabled by exec. Group members are ruled by group
557 * leaders as stated in 1).
559 * 3) For traced programs attached by perf (pid/tid):
560 * - we specifically enable or disable all events during
563 * When attaching events to already running traced we
564 * enable/disable events specifically, as there's no
565 * initial traced exec call.
567 void perf_evsel__config(struct perf_evsel *evsel,
568 struct perf_record_opts *opts)
570 struct perf_evsel *leader = evsel->leader;
571 struct perf_event_attr *attr = &evsel->attr;
572 int track = !evsel->idx; /* only the first counter needs these */
574 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
575 attr->inherit = !opts->no_inherit;
577 perf_evsel__set_sample_bit(evsel, IP);
578 perf_evsel__set_sample_bit(evsel, TID);
580 if (evsel->sample_read) {
581 perf_evsel__set_sample_bit(evsel, READ);
584 * We need ID even in case of single event, because
585 * PERF_SAMPLE_READ process ID specific data.
587 perf_evsel__set_sample_id(evsel, false);
590 * Apply group format only if we belong to group
591 * with more than one members.
593 if (leader->nr_members > 1) {
594 attr->read_format |= PERF_FORMAT_GROUP;
600 * We default some events to a 1 default interval. But keep
601 * it a weak assumption overridable by the user.
603 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
604 opts->user_interval != ULLONG_MAX)) {
606 perf_evsel__set_sample_bit(evsel, PERIOD);
608 attr->sample_freq = opts->freq;
610 attr->sample_period = opts->default_interval;
615 * Disable sampling for all group members other
616 * than leader in case leader 'leads' the sampling.
618 if ((leader != evsel) && leader->sample_read) {
619 attr->sample_freq = 0;
620 attr->sample_period = 0;
623 if (opts->no_samples)
624 attr->sample_freq = 0;
626 if (opts->inherit_stat)
627 attr->inherit_stat = 1;
629 if (opts->sample_address) {
630 perf_evsel__set_sample_bit(evsel, ADDR);
631 attr->mmap_data = track;
634 if (opts->call_graph) {
635 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
637 if (opts->call_graph == CALLCHAIN_DWARF) {
638 perf_evsel__set_sample_bit(evsel, REGS_USER);
639 perf_evsel__set_sample_bit(evsel, STACK_USER);
640 attr->sample_regs_user = PERF_REGS_MASK;
641 attr->sample_stack_user = opts->stack_dump_size;
642 attr->exclude_callchain_user = 1;
646 if (perf_target__has_cpu(&opts->target))
647 perf_evsel__set_sample_bit(evsel, CPU);
650 perf_evsel__set_sample_bit(evsel, PERIOD);
652 if (!perf_missing_features.sample_id_all &&
653 (opts->sample_time || !opts->no_inherit ||
654 perf_target__has_cpu(&opts->target)))
655 perf_evsel__set_sample_bit(evsel, TIME);
657 if (opts->raw_samples) {
658 perf_evsel__set_sample_bit(evsel, TIME);
659 perf_evsel__set_sample_bit(evsel, RAW);
660 perf_evsel__set_sample_bit(evsel, CPU);
663 if (opts->sample_address)
664 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
666 if (opts->no_delay) {
668 attr->wakeup_events = 1;
670 if (opts->branch_stack) {
671 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
672 attr->branch_sample_type = opts->branch_stack;
675 if (opts->sample_weight)
676 attr->sample_type |= PERF_SAMPLE_WEIGHT;
682 * XXX see the function comment above
684 * Disabling only independent events or group leaders,
685 * keeping group members enabled.
687 if (perf_evsel__is_group_leader(evsel))
691 * Setting enable_on_exec for independent events and
692 * group leaders for traced executed by perf.
694 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
695 attr->enable_on_exec = 1;
698 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
701 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
704 for (cpu = 0; cpu < ncpus; cpu++) {
705 for (thread = 0; thread < nthreads; thread++) {
706 FD(evsel, cpu, thread) = -1;
711 return evsel->fd != NULL ? 0 : -ENOMEM;
714 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
719 for (cpu = 0; cpu < ncpus; cpu++) {
720 for (thread = 0; thread < nthreads; thread++) {
721 int fd = FD(evsel, cpu, thread),
722 err = ioctl(fd, ioc, arg);
732 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
735 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
736 PERF_EVENT_IOC_SET_FILTER,
740 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
742 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
743 PERF_EVENT_IOC_ENABLE,
747 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
749 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
750 if (evsel->sample_id == NULL)
753 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
754 if (evsel->id == NULL) {
755 xyarray__delete(evsel->sample_id);
756 evsel->sample_id = NULL;
763 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
765 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
766 (ncpus * sizeof(struct perf_counts_values))));
769 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
771 evsel->counts = zalloc((sizeof(*evsel->counts) +
772 (ncpus * sizeof(struct perf_counts_values))));
773 return evsel->counts != NULL ? 0 : -ENOMEM;
776 void perf_evsel__free_fd(struct perf_evsel *evsel)
778 xyarray__delete(evsel->fd);
782 void perf_evsel__free_id(struct perf_evsel *evsel)
784 xyarray__delete(evsel->sample_id);
785 evsel->sample_id = NULL;
790 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
794 for (cpu = 0; cpu < ncpus; cpu++)
795 for (thread = 0; thread < nthreads; ++thread) {
796 close(FD(evsel, cpu, thread));
797 FD(evsel, cpu, thread) = -1;
801 void perf_evsel__free_counts(struct perf_evsel *evsel)
806 void perf_evsel__exit(struct perf_evsel *evsel)
808 assert(list_empty(&evsel->node));
809 perf_evsel__free_fd(evsel);
810 perf_evsel__free_id(evsel);
813 void perf_evsel__delete(struct perf_evsel *evsel)
815 perf_evsel__exit(evsel);
816 close_cgroup(evsel->cgrp);
817 free(evsel->group_name);
818 if (evsel->tp_format)
819 pevent_free_format(evsel->tp_format);
824 static inline void compute_deltas(struct perf_evsel *evsel,
826 struct perf_counts_values *count)
828 struct perf_counts_values tmp;
830 if (!evsel->prev_raw_counts)
834 tmp = evsel->prev_raw_counts->aggr;
835 evsel->prev_raw_counts->aggr = *count;
837 tmp = evsel->prev_raw_counts->cpu[cpu];
838 evsel->prev_raw_counts->cpu[cpu] = *count;
841 count->val = count->val - tmp.val;
842 count->ena = count->ena - tmp.ena;
843 count->run = count->run - tmp.run;
846 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
847 int cpu, int thread, bool scale)
849 struct perf_counts_values count;
850 size_t nv = scale ? 3 : 1;
852 if (FD(evsel, cpu, thread) < 0)
855 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
858 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
861 compute_deltas(evsel, cpu, &count);
866 else if (count.run < count.ena)
867 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
869 count.ena = count.run = 0;
871 evsel->counts->cpu[cpu] = count;
875 int __perf_evsel__read(struct perf_evsel *evsel,
876 int ncpus, int nthreads, bool scale)
878 size_t nv = scale ? 3 : 1;
880 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
882 aggr->val = aggr->ena = aggr->run = 0;
884 for (cpu = 0; cpu < ncpus; cpu++) {
885 for (thread = 0; thread < nthreads; thread++) {
886 if (FD(evsel, cpu, thread) < 0)
889 if (readn(FD(evsel, cpu, thread),
890 &count, nv * sizeof(u64)) < 0)
893 aggr->val += count.val;
895 aggr->ena += count.ena;
896 aggr->run += count.run;
901 compute_deltas(evsel, -1, aggr);
903 evsel->counts->scaled = 0;
905 if (aggr->run == 0) {
906 evsel->counts->scaled = -1;
911 if (aggr->run < aggr->ena) {
912 evsel->counts->scaled = 1;
913 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
916 aggr->ena = aggr->run = 0;
921 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
923 struct perf_evsel *leader = evsel->leader;
926 if (perf_evsel__is_group_leader(evsel))
930 * Leader must be already processed/open,
935 fd = FD(leader, cpu, thread);
941 #define __PRINT_ATTR(fmt, cast, field) \
942 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
944 #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
945 #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
946 #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
947 #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
949 #define PRINT_ATTR2N(name1, field1, name2, field2) \
950 fprintf(fp, " %-19s %u %-19s %u\n", \
951 name1, attr->field1, name2, attr->field2)
953 #define PRINT_ATTR2(field1, field2) \
954 PRINT_ATTR2N(#field1, field1, #field2, field2)
956 static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
960 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
961 ret += fprintf(fp, "perf_event_attr:\n");
963 ret += PRINT_ATTR_U32(type);
964 ret += PRINT_ATTR_U32(size);
965 ret += PRINT_ATTR_X64(config);
966 ret += PRINT_ATTR_U64(sample_period);
967 ret += PRINT_ATTR_U64(sample_freq);
968 ret += PRINT_ATTR_X64(sample_type);
969 ret += PRINT_ATTR_X64(read_format);
971 ret += PRINT_ATTR2(disabled, inherit);
972 ret += PRINT_ATTR2(pinned, exclusive);
973 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
974 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
975 ret += PRINT_ATTR2(mmap, comm);
976 ret += PRINT_ATTR2(freq, inherit_stat);
977 ret += PRINT_ATTR2(enable_on_exec, task);
978 ret += PRINT_ATTR2(watermark, precise_ip);
979 ret += PRINT_ATTR2(mmap_data, sample_id_all);
980 ret += PRINT_ATTR2(exclude_host, exclude_guest);
981 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
982 "excl.callchain_user", exclude_callchain_user);
984 ret += PRINT_ATTR_U32(wakeup_events);
985 ret += PRINT_ATTR_U32(wakeup_watermark);
986 ret += PRINT_ATTR_X32(bp_type);
987 ret += PRINT_ATTR_X64(bp_addr);
988 ret += PRINT_ATTR_X64(config1);
989 ret += PRINT_ATTR_U64(bp_len);
990 ret += PRINT_ATTR_X64(config2);
991 ret += PRINT_ATTR_X64(branch_sample_type);
992 ret += PRINT_ATTR_X64(sample_regs_user);
993 ret += PRINT_ATTR_U32(sample_stack_user);
995 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1000 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1001 struct thread_map *threads)
1004 unsigned long flags = 0;
1006 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1008 if (evsel->fd == NULL &&
1009 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
1013 flags = PERF_FLAG_PID_CGROUP;
1014 pid = evsel->cgrp->fd;
1017 fallback_missing_features:
1018 if (perf_missing_features.exclude_guest)
1019 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1021 if (perf_missing_features.sample_id_all)
1022 evsel->attr.sample_id_all = 0;
1025 perf_event_attr__fprintf(&evsel->attr, stderr);
1027 for (cpu = 0; cpu < cpus->nr; cpu++) {
1029 for (thread = 0; thread < threads->nr; thread++) {
1033 pid = threads->map[thread];
1035 group_fd = get_group_fd(evsel, cpu, thread);
1037 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1038 pid, cpus->map[cpu], group_fd, flags);
1040 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1044 if (FD(evsel, cpu, thread) < 0) {
1048 set_rlimit = NO_CHANGE;
1056 * perf stat needs between 5 and 22 fds per CPU. When we run out
1057 * of them try to increase the limits.
1059 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1061 int old_errno = errno;
1063 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1064 if (set_rlimit == NO_CHANGE)
1065 l.rlim_cur = l.rlim_max;
1067 l.rlim_cur = l.rlim_max + 1000;
1068 l.rlim_max = l.rlim_cur;
1070 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1079 if (err != -EINVAL || cpu > 0 || thread > 0)
1082 if (!perf_missing_features.exclude_guest &&
1083 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1084 perf_missing_features.exclude_guest = true;
1085 goto fallback_missing_features;
1086 } else if (!perf_missing_features.sample_id_all) {
1087 perf_missing_features.sample_id_all = true;
1088 goto retry_sample_id;
1093 while (--thread >= 0) {
1094 close(FD(evsel, cpu, thread));
1095 FD(evsel, cpu, thread) = -1;
1097 thread = threads->nr;
1098 } while (--cpu >= 0);
1102 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1104 if (evsel->fd == NULL)
1107 perf_evsel__close_fd(evsel, ncpus, nthreads);
1108 perf_evsel__free_fd(evsel);
1121 struct thread_map map;
1123 } empty_thread_map = {
1128 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1129 struct thread_map *threads)
1132 /* Work around old compiler warnings about strict aliasing */
1133 cpus = &empty_cpu_map.map;
1136 if (threads == NULL)
1137 threads = &empty_thread_map.map;
1139 return __perf_evsel__open(evsel, cpus, threads);
1142 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1143 struct cpu_map *cpus)
1145 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1148 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1149 struct thread_map *threads)
1151 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1154 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1155 const union perf_event *event,
1156 struct perf_sample *sample)
1158 u64 type = evsel->attr.sample_type;
1159 const u64 *array = event->sample.array;
1160 bool swapped = evsel->needs_swap;
1163 array += ((event->header.size -
1164 sizeof(event->header)) / sizeof(u64)) - 1;
1166 if (type & PERF_SAMPLE_IDENTIFIER) {
1167 sample->id = *array;
1171 if (type & PERF_SAMPLE_CPU) {
1174 /* undo swap of u64, then swap on individual u32s */
1175 u.val64 = bswap_64(u.val64);
1176 u.val32[0] = bswap_32(u.val32[0]);
1179 sample->cpu = u.val32[0];
1183 if (type & PERF_SAMPLE_STREAM_ID) {
1184 sample->stream_id = *array;
1188 if (type & PERF_SAMPLE_ID) {
1189 sample->id = *array;
1193 if (type & PERF_SAMPLE_TIME) {
1194 sample->time = *array;
1198 if (type & PERF_SAMPLE_TID) {
1201 /* undo swap of u64, then swap on individual u32s */
1202 u.val64 = bswap_64(u.val64);
1203 u.val32[0] = bswap_32(u.val32[0]);
1204 u.val32[1] = bswap_32(u.val32[1]);
1207 sample->pid = u.val32[0];
1208 sample->tid = u.val32[1];
1214 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1217 return size > max_size || offset + size > endp;
1220 #define OVERFLOW_CHECK(offset, size, max_size) \
1222 if (overflow(endp, (max_size), (offset), (size))) \
1226 #define OVERFLOW_CHECK_u64(offset) \
1227 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1229 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1230 struct perf_sample *data)
1232 u64 type = evsel->attr.sample_type;
1233 bool swapped = evsel->needs_swap;
1235 u16 max_size = event->header.size;
1236 const void *endp = (void *)event + max_size;
1240 * used for cross-endian analysis. See git commit 65014ab3
1241 * for why this goofiness is needed.
1245 memset(data, 0, sizeof(*data));
1246 data->cpu = data->pid = data->tid = -1;
1247 data->stream_id = data->id = data->time = -1ULL;
1251 if (event->header.type != PERF_RECORD_SAMPLE) {
1252 if (!evsel->attr.sample_id_all)
1254 return perf_evsel__parse_id_sample(evsel, event, data);
1257 array = event->sample.array;
1260 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1261 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1262 * check the format does not go past the end of the event.
1264 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1268 if (type & PERF_SAMPLE_IDENTIFIER) {
1273 if (type & PERF_SAMPLE_IP) {
1278 if (type & PERF_SAMPLE_TID) {
1281 /* undo swap of u64, then swap on individual u32s */
1282 u.val64 = bswap_64(u.val64);
1283 u.val32[0] = bswap_32(u.val32[0]);
1284 u.val32[1] = bswap_32(u.val32[1]);
1287 data->pid = u.val32[0];
1288 data->tid = u.val32[1];
1292 if (type & PERF_SAMPLE_TIME) {
1293 data->time = *array;
1298 if (type & PERF_SAMPLE_ADDR) {
1299 data->addr = *array;
1303 if (type & PERF_SAMPLE_ID) {
1308 if (type & PERF_SAMPLE_STREAM_ID) {
1309 data->stream_id = *array;
1313 if (type & PERF_SAMPLE_CPU) {
1317 /* undo swap of u64, then swap on individual u32s */
1318 u.val64 = bswap_64(u.val64);
1319 u.val32[0] = bswap_32(u.val32[0]);
1322 data->cpu = u.val32[0];
1326 if (type & PERF_SAMPLE_PERIOD) {
1327 data->period = *array;
1331 if (type & PERF_SAMPLE_READ) {
1332 u64 read_format = evsel->attr.read_format;
1334 OVERFLOW_CHECK_u64(array);
1335 if (read_format & PERF_FORMAT_GROUP)
1336 data->read.group.nr = *array;
1338 data->read.one.value = *array;
1342 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1343 OVERFLOW_CHECK_u64(array);
1344 data->read.time_enabled = *array;
1348 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1349 OVERFLOW_CHECK_u64(array);
1350 data->read.time_running = *array;
1354 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1355 if (read_format & PERF_FORMAT_GROUP) {
1356 const u64 max_group_nr = UINT64_MAX /
1357 sizeof(struct sample_read_value);
1359 if (data->read.group.nr > max_group_nr)
1361 sz = data->read.group.nr *
1362 sizeof(struct sample_read_value);
1363 OVERFLOW_CHECK(array, sz, max_size);
1364 data->read.group.values =
1365 (struct sample_read_value *)array;
1366 array = (void *)array + sz;
1368 OVERFLOW_CHECK_u64(array);
1369 data->read.one.id = *array;
1374 if (type & PERF_SAMPLE_CALLCHAIN) {
1375 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1377 OVERFLOW_CHECK_u64(array);
1378 data->callchain = (struct ip_callchain *)array++;
1379 if (data->callchain->nr > max_callchain_nr)
1381 sz = data->callchain->nr * sizeof(u64);
1382 OVERFLOW_CHECK(array, sz, max_size);
1383 array = (void *)array + sz;
1386 if (type & PERF_SAMPLE_RAW) {
1387 OVERFLOW_CHECK_u64(array);
1389 if (WARN_ONCE(swapped,
1390 "Endianness of raw data not corrected!\n")) {
1391 /* undo swap of u64, then swap on individual u32s */
1392 u.val64 = bswap_64(u.val64);
1393 u.val32[0] = bswap_32(u.val32[0]);
1394 u.val32[1] = bswap_32(u.val32[1]);
1396 data->raw_size = u.val32[0];
1397 array = (void *)array + sizeof(u32);
1399 OVERFLOW_CHECK(array, data->raw_size, max_size);
1400 data->raw_data = (void *)array;
1401 array = (void *)array + data->raw_size;
1404 if (type & PERF_SAMPLE_BRANCH_STACK) {
1405 const u64 max_branch_nr = UINT64_MAX /
1406 sizeof(struct branch_entry);
1408 OVERFLOW_CHECK_u64(array);
1409 data->branch_stack = (struct branch_stack *)array++;
1411 if (data->branch_stack->nr > max_branch_nr)
1413 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1414 OVERFLOW_CHECK(array, sz, max_size);
1415 array = (void *)array + sz;
1418 if (type & PERF_SAMPLE_REGS_USER) {
1419 OVERFLOW_CHECK_u64(array);
1420 data->user_regs.abi = *array;
1423 if (data->user_regs.abi) {
1424 u64 regs_user = evsel->attr.sample_regs_user;
1426 sz = hweight_long(regs_user) * sizeof(u64);
1427 OVERFLOW_CHECK(array, sz, max_size);
1428 data->user_regs.regs = (u64 *)array;
1429 array = (void *)array + sz;
1433 if (type & PERF_SAMPLE_STACK_USER) {
1434 OVERFLOW_CHECK_u64(array);
1437 data->user_stack.offset = ((char *)(array - 1)
1441 data->user_stack.size = 0;
1443 OVERFLOW_CHECK(array, sz, max_size);
1444 data->user_stack.data = (char *)array;
1445 array = (void *)array + sz;
1446 OVERFLOW_CHECK_u64(array);
1447 data->user_stack.size = *array++;
1452 if (type & PERF_SAMPLE_WEIGHT) {
1453 OVERFLOW_CHECK_u64(array);
1454 data->weight = *array;
1458 data->data_src = PERF_MEM_DATA_SRC_NONE;
1459 if (type & PERF_SAMPLE_DATA_SRC) {
1460 OVERFLOW_CHECK_u64(array);
1461 data->data_src = *array;
1468 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1469 u64 sample_regs_user, u64 read_format)
1471 size_t sz, result = sizeof(struct sample_event);
1473 if (type & PERF_SAMPLE_IDENTIFIER)
1474 result += sizeof(u64);
1476 if (type & PERF_SAMPLE_IP)
1477 result += sizeof(u64);
1479 if (type & PERF_SAMPLE_TID)
1480 result += sizeof(u64);
1482 if (type & PERF_SAMPLE_TIME)
1483 result += sizeof(u64);
1485 if (type & PERF_SAMPLE_ADDR)
1486 result += sizeof(u64);
1488 if (type & PERF_SAMPLE_ID)
1489 result += sizeof(u64);
1491 if (type & PERF_SAMPLE_STREAM_ID)
1492 result += sizeof(u64);
1494 if (type & PERF_SAMPLE_CPU)
1495 result += sizeof(u64);
1497 if (type & PERF_SAMPLE_PERIOD)
1498 result += sizeof(u64);
1500 if (type & PERF_SAMPLE_READ) {
1501 result += sizeof(u64);
1502 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1503 result += sizeof(u64);
1504 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1505 result += sizeof(u64);
1506 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1507 if (read_format & PERF_FORMAT_GROUP) {
1508 sz = sample->read.group.nr *
1509 sizeof(struct sample_read_value);
1512 result += sizeof(u64);
1516 if (type & PERF_SAMPLE_CALLCHAIN) {
1517 sz = (sample->callchain->nr + 1) * sizeof(u64);
1521 if (type & PERF_SAMPLE_RAW) {
1522 result += sizeof(u32);
1523 result += sample->raw_size;
1526 if (type & PERF_SAMPLE_BRANCH_STACK) {
1527 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1532 if (type & PERF_SAMPLE_REGS_USER) {
1533 if (sample->user_regs.abi) {
1534 result += sizeof(u64);
1535 sz = hweight_long(sample_regs_user) * sizeof(u64);
1538 result += sizeof(u64);
1542 if (type & PERF_SAMPLE_STACK_USER) {
1543 sz = sample->user_stack.size;
1544 result += sizeof(u64);
1547 result += sizeof(u64);
1551 if (type & PERF_SAMPLE_WEIGHT)
1552 result += sizeof(u64);
1554 if (type & PERF_SAMPLE_DATA_SRC)
1555 result += sizeof(u64);
1560 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1561 u64 sample_regs_user, u64 read_format,
1562 const struct perf_sample *sample,
1568 * used for cross-endian analysis. See git commit 65014ab3
1569 * for why this goofiness is needed.
1573 array = event->sample.array;
1575 if (type & PERF_SAMPLE_IDENTIFIER) {
1576 *array = sample->id;
1580 if (type & PERF_SAMPLE_IP) {
1581 *array = sample->ip;
1585 if (type & PERF_SAMPLE_TID) {
1586 u.val32[0] = sample->pid;
1587 u.val32[1] = sample->tid;
1590 * Inverse of what is done in perf_evsel__parse_sample
1592 u.val32[0] = bswap_32(u.val32[0]);
1593 u.val32[1] = bswap_32(u.val32[1]);
1594 u.val64 = bswap_64(u.val64);
1601 if (type & PERF_SAMPLE_TIME) {
1602 *array = sample->time;
1606 if (type & PERF_SAMPLE_ADDR) {
1607 *array = sample->addr;
1611 if (type & PERF_SAMPLE_ID) {
1612 *array = sample->id;
1616 if (type & PERF_SAMPLE_STREAM_ID) {
1617 *array = sample->stream_id;
1621 if (type & PERF_SAMPLE_CPU) {
1622 u.val32[0] = sample->cpu;
1625 * Inverse of what is done in perf_evsel__parse_sample
1627 u.val32[0] = bswap_32(u.val32[0]);
1628 u.val64 = bswap_64(u.val64);
1634 if (type & PERF_SAMPLE_PERIOD) {
1635 *array = sample->period;
1639 if (type & PERF_SAMPLE_READ) {
1640 if (read_format & PERF_FORMAT_GROUP)
1641 *array = sample->read.group.nr;
1643 *array = sample->read.one.value;
1646 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1647 *array = sample->read.time_enabled;
1651 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1652 *array = sample->read.time_running;
1656 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1657 if (read_format & PERF_FORMAT_GROUP) {
1658 sz = sample->read.group.nr *
1659 sizeof(struct sample_read_value);
1660 memcpy(array, sample->read.group.values, sz);
1661 array = (void *)array + sz;
1663 *array = sample->read.one.id;
1668 if (type & PERF_SAMPLE_CALLCHAIN) {
1669 sz = (sample->callchain->nr + 1) * sizeof(u64);
1670 memcpy(array, sample->callchain, sz);
1671 array = (void *)array + sz;
1674 if (type & PERF_SAMPLE_RAW) {
1675 u.val32[0] = sample->raw_size;
1676 if (WARN_ONCE(swapped,
1677 "Endianness of raw data not corrected!\n")) {
1679 * Inverse of what is done in perf_evsel__parse_sample
1681 u.val32[0] = bswap_32(u.val32[0]);
1682 u.val32[1] = bswap_32(u.val32[1]);
1683 u.val64 = bswap_64(u.val64);
1686 array = (void *)array + sizeof(u32);
1688 memcpy(array, sample->raw_data, sample->raw_size);
1689 array = (void *)array + sample->raw_size;
1692 if (type & PERF_SAMPLE_BRANCH_STACK) {
1693 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1695 memcpy(array, sample->branch_stack, sz);
1696 array = (void *)array + sz;
1699 if (type & PERF_SAMPLE_REGS_USER) {
1700 if (sample->user_regs.abi) {
1701 *array++ = sample->user_regs.abi;
1702 sz = hweight_long(sample_regs_user) * sizeof(u64);
1703 memcpy(array, sample->user_regs.regs, sz);
1704 array = (void *)array + sz;
1710 if (type & PERF_SAMPLE_STACK_USER) {
1711 sz = sample->user_stack.size;
1714 memcpy(array, sample->user_stack.data, sz);
1715 array = (void *)array + sz;
1720 if (type & PERF_SAMPLE_WEIGHT) {
1721 *array = sample->weight;
1725 if (type & PERF_SAMPLE_DATA_SRC) {
1726 *array = sample->data_src;
1733 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1735 return pevent_find_field(evsel->tp_format, name);
1738 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1741 struct format_field *field = perf_evsel__field(evsel, name);
1747 offset = field->offset;
1749 if (field->flags & FIELD_IS_DYNAMIC) {
1750 offset = *(int *)(sample->raw_data + field->offset);
1754 return sample->raw_data + offset;
1757 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1760 struct format_field *field = perf_evsel__field(evsel, name);
1767 ptr = sample->raw_data + field->offset;
1769 switch (field->size) {
1773 value = *(u16 *)ptr;
1776 value = *(u32 *)ptr;
1779 value = *(u64 *)ptr;
1785 if (!evsel->needs_swap)
1788 switch (field->size) {
1790 return bswap_16(value);
1792 return bswap_32(value);
1794 return bswap_64(value);
1802 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1808 ret += fprintf(fp, ",");
1810 ret += fprintf(fp, ":");
1814 va_start(args, fmt);
1815 ret += vfprintf(fp, fmt, args);
1820 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1825 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1828 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1835 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1836 struct bit_names *bits, bool *first)
1838 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1839 bool first_bit = true;
1842 if (value & bits[i].bit) {
1843 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1846 } while (bits[++i].name != NULL);
1851 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1853 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1854 struct bit_names bits[] = {
1855 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1856 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1857 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1858 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1859 bit_name(IDENTIFIER),
1863 return bits__fprintf(fp, "sample_type", value, bits, first);
1866 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1868 #define bit_name(n) { PERF_FORMAT_##n, #n }
1869 struct bit_names bits[] = {
1870 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1871 bit_name(ID), bit_name(GROUP),
1875 return bits__fprintf(fp, "read_format", value, bits, first);
1878 int perf_evsel__fprintf(struct perf_evsel *evsel,
1879 struct perf_attr_details *details, FILE *fp)
1884 if (details->event_group) {
1885 struct perf_evsel *pos;
1887 if (!perf_evsel__is_group_leader(evsel))
1890 if (evsel->nr_members > 1)
1891 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1893 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1894 for_each_group_member(pos, evsel)
1895 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1897 if (evsel->nr_members > 1)
1898 printed += fprintf(fp, "}");
1902 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1904 if (details->verbose || details->freq) {
1905 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1906 (u64)evsel->attr.sample_freq);
1909 if (details->verbose) {
1915 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1916 if (evsel->attr.read_format)
1917 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
1921 if_print(exclusive);
1922 if_print(exclude_user);
1923 if_print(exclude_kernel);
1924 if_print(exclude_hv);
1925 if_print(exclude_idle);
1929 if_print(inherit_stat);
1930 if_print(enable_on_exec);
1932 if_print(watermark);
1933 if_print(precise_ip);
1934 if_print(mmap_data);
1935 if_print(sample_id_all);
1936 if_print(exclude_host);
1937 if_print(exclude_guest);
1938 if_print(__reserved_1);
1939 if_print(wakeup_events);
1941 if_print(branch_sample_type);
1948 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1949 char *msg, size_t msgsize)
1951 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
1952 evsel->attr.type == PERF_TYPE_HARDWARE &&
1953 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1955 * If it's cycles then fall back to hrtimer based
1956 * cpu-clock-tick sw counter, which is always available even if
1959 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1962 scnprintf(msg, msgsize, "%s",
1963 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1965 evsel->attr.type = PERF_TYPE_SOFTWARE;
1966 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1976 int perf_evsel__open_strerror(struct perf_evsel *evsel,
1977 struct perf_target *target,
1978 int err, char *msg, size_t size)
1983 return scnprintf(msg, size,
1984 "You may not have permission to collect %sstats.\n"
1985 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1986 " -1 - Not paranoid at all\n"
1987 " 0 - Disallow raw tracepoint access for unpriv\n"
1988 " 1 - Disallow cpu events for unpriv\n"
1989 " 2 - Disallow kernel profiling for unpriv",
1990 target->system_wide ? "system-wide " : "");
1992 return scnprintf(msg, size, "The %s event is not supported.",
1993 perf_evsel__name(evsel));
1995 return scnprintf(msg, size, "%s",
1996 "Too many events are opened.\n"
1997 "Try again after reducing the number of events.");
1999 if (target->cpu_list)
2000 return scnprintf(msg, size, "%s",
2001 "No such device - did you specify an out-of-range profile CPU?\n");
2004 if (evsel->attr.precise_ip)
2005 return scnprintf(msg, size, "%s",
2006 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2007 #if defined(__i386__) || defined(__x86_64__)
2008 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2009 return scnprintf(msg, size, "%s",
2010 "No hardware sampling interrupt available.\n"
2011 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2018 return scnprintf(msg, size,
2019 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
2020 "/bin/dmesg may provide additional information.\n"
2021 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2022 err, strerror(err), perf_evsel__name(evsel));