perf tools: Add mem access sampling core support
[linux-3.10.git] / tools / perf / util / evsel.c
1 /*
2  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3  *
4  * Parts came from builtin-{top,stat,record}.c, see those files for further
5  * copyright notes.
6  *
7  * Released under the GPL v2. (and only v2, not any later version)
8  */
9
10 #include <byteswap.h>
11 #include <linux/bitops.h>
12 #include "asm/bug.h"
13 #include <lk/debugfs.h>
14 #include "event-parse.h"
15 #include "evsel.h"
16 #include "evlist.h"
17 #include "util.h"
18 #include "cpumap.h"
19 #include "thread_map.h"
20 #include "target.h"
21 #include <linux/hw_breakpoint.h>
22 #include <linux/perf_event.h>
23 #include "perf_regs.h"
24
25 static struct {
26         bool sample_id_all;
27         bool exclude_guest;
28 } perf_missing_features;
29
30 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
31
32 static int __perf_evsel__sample_size(u64 sample_type)
33 {
34         u64 mask = sample_type & PERF_SAMPLE_MASK;
35         int size = 0;
36         int i;
37
38         for (i = 0; i < 64; i++) {
39                 if (mask & (1ULL << i))
40                         size++;
41         }
42
43         size *= sizeof(u64);
44
45         return size;
46 }
47
48 void hists__init(struct hists *hists)
49 {
50         memset(hists, 0, sizeof(*hists));
51         hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
52         hists->entries_in = &hists->entries_in_array[0];
53         hists->entries_collapsed = RB_ROOT;
54         hists->entries = RB_ROOT;
55         pthread_mutex_init(&hists->lock, NULL);
56 }
57
58 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
59                                   enum perf_event_sample_format bit)
60 {
61         if (!(evsel->attr.sample_type & bit)) {
62                 evsel->attr.sample_type |= bit;
63                 evsel->sample_size += sizeof(u64);
64         }
65 }
66
67 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
68                                     enum perf_event_sample_format bit)
69 {
70         if (evsel->attr.sample_type & bit) {
71                 evsel->attr.sample_type &= ~bit;
72                 evsel->sample_size -= sizeof(u64);
73         }
74 }
75
76 void perf_evsel__set_sample_id(struct perf_evsel *evsel)
77 {
78         perf_evsel__set_sample_bit(evsel, ID);
79         evsel->attr.read_format |= PERF_FORMAT_ID;
80 }
81
82 void perf_evsel__init(struct perf_evsel *evsel,
83                       struct perf_event_attr *attr, int idx)
84 {
85         evsel->idx         = idx;
86         evsel->attr        = *attr;
87         evsel->leader      = evsel;
88         INIT_LIST_HEAD(&evsel->node);
89         hists__init(&evsel->hists);
90         evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
91 }
92
93 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
94 {
95         struct perf_evsel *evsel = zalloc(sizeof(*evsel));
96
97         if (evsel != NULL)
98                 perf_evsel__init(evsel, attr, idx);
99
100         return evsel;
101 }
102
103 struct event_format *event_format__new(const char *sys, const char *name)
104 {
105         int fd, n;
106         char *filename;
107         void *bf = NULL, *nbf;
108         size_t size = 0, alloc_size = 0;
109         struct event_format *format = NULL;
110
111         if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
112                 goto out;
113
114         fd = open(filename, O_RDONLY);
115         if (fd < 0)
116                 goto out_free_filename;
117
118         do {
119                 if (size == alloc_size) {
120                         alloc_size += BUFSIZ;
121                         nbf = realloc(bf, alloc_size);
122                         if (nbf == NULL)
123                                 goto out_free_bf;
124                         bf = nbf;
125                 }
126
127                 n = read(fd, bf + size, BUFSIZ);
128                 if (n < 0)
129                         goto out_free_bf;
130                 size += n;
131         } while (n > 0);
132
133         pevent_parse_format(&format, bf, size, sys);
134
135 out_free_bf:
136         free(bf);
137         close(fd);
138 out_free_filename:
139         free(filename);
140 out:
141         return format;
142 }
143
144 struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
145 {
146         struct perf_evsel *evsel = zalloc(sizeof(*evsel));
147
148         if (evsel != NULL) {
149                 struct perf_event_attr attr = {
150                         .type          = PERF_TYPE_TRACEPOINT,
151                         .sample_type   = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
152                                           PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
153                 };
154
155                 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
156                         goto out_free;
157
158                 evsel->tp_format = event_format__new(sys, name);
159                 if (evsel->tp_format == NULL)
160                         goto out_free;
161
162                 event_attr_init(&attr);
163                 attr.config = evsel->tp_format->id;
164                 attr.sample_period = 1;
165                 perf_evsel__init(evsel, &attr, idx);
166         }
167
168         return evsel;
169
170 out_free:
171         free(evsel->name);
172         free(evsel);
173         return NULL;
174 }
175
176 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
177         "cycles",
178         "instructions",
179         "cache-references",
180         "cache-misses",
181         "branches",
182         "branch-misses",
183         "bus-cycles",
184         "stalled-cycles-frontend",
185         "stalled-cycles-backend",
186         "ref-cycles",
187 };
188
189 static const char *__perf_evsel__hw_name(u64 config)
190 {
191         if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
192                 return perf_evsel__hw_names[config];
193
194         return "unknown-hardware";
195 }
196
197 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
198 {
199         int colon = 0, r = 0;
200         struct perf_event_attr *attr = &evsel->attr;
201         bool exclude_guest_default = false;
202
203 #define MOD_PRINT(context, mod) do {                                    \
204                 if (!attr->exclude_##context) {                         \
205                         if (!colon) colon = ++r;                        \
206                         r += scnprintf(bf + r, size - r, "%c", mod);    \
207                 } } while(0)
208
209         if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
210                 MOD_PRINT(kernel, 'k');
211                 MOD_PRINT(user, 'u');
212                 MOD_PRINT(hv, 'h');
213                 exclude_guest_default = true;
214         }
215
216         if (attr->precise_ip) {
217                 if (!colon)
218                         colon = ++r;
219                 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
220                 exclude_guest_default = true;
221         }
222
223         if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
224                 MOD_PRINT(host, 'H');
225                 MOD_PRINT(guest, 'G');
226         }
227 #undef MOD_PRINT
228         if (colon)
229                 bf[colon - 1] = ':';
230         return r;
231 }
232
233 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
234 {
235         int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
236         return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
237 }
238
239 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
240         "cpu-clock",
241         "task-clock",
242         "page-faults",
243         "context-switches",
244         "cpu-migrations",
245         "minor-faults",
246         "major-faults",
247         "alignment-faults",
248         "emulation-faults",
249 };
250
251 static const char *__perf_evsel__sw_name(u64 config)
252 {
253         if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
254                 return perf_evsel__sw_names[config];
255         return "unknown-software";
256 }
257
258 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
259 {
260         int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
261         return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
262 }
263
264 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
265 {
266         int r;
267
268         r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
269
270         if (type & HW_BREAKPOINT_R)
271                 r += scnprintf(bf + r, size - r, "r");
272
273         if (type & HW_BREAKPOINT_W)
274                 r += scnprintf(bf + r, size - r, "w");
275
276         if (type & HW_BREAKPOINT_X)
277                 r += scnprintf(bf + r, size - r, "x");
278
279         return r;
280 }
281
282 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
283 {
284         struct perf_event_attr *attr = &evsel->attr;
285         int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
286         return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
287 }
288
289 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
290                                 [PERF_EVSEL__MAX_ALIASES] = {
291  { "L1-dcache", "l1-d",         "l1d",          "L1-data",              },
292  { "L1-icache", "l1-i",         "l1i",          "L1-instruction",       },
293  { "LLC",       "L2",                                                   },
294  { "dTLB",      "d-tlb",        "Data-TLB",                             },
295  { "iTLB",      "i-tlb",        "Instruction-TLB",                      },
296  { "branch",    "branches",     "bpu",          "btb",          "bpc",  },
297  { "node",                                                              },
298 };
299
300 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
301                                    [PERF_EVSEL__MAX_ALIASES] = {
302  { "load",      "loads",        "read",                                 },
303  { "store",     "stores",       "write",                                },
304  { "prefetch",  "prefetches",   "speculative-read", "speculative-load", },
305 };
306
307 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
308                                        [PERF_EVSEL__MAX_ALIASES] = {
309  { "refs",      "Reference",    "ops",          "access",               },
310  { "misses",    "miss",                                                 },
311 };
312
313 #define C(x)            PERF_COUNT_HW_CACHE_##x
314 #define CACHE_READ      (1 << C(OP_READ))
315 #define CACHE_WRITE     (1 << C(OP_WRITE))
316 #define CACHE_PREFETCH  (1 << C(OP_PREFETCH))
317 #define COP(x)          (1 << x)
318
319 /*
320  * cache operartion stat
321  * L1I : Read and prefetch only
322  * ITLB and BPU : Read-only
323  */
324 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
325  [C(L1D)]       = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
326  [C(L1I)]       = (CACHE_READ | CACHE_PREFETCH),
327  [C(LL)]        = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
328  [C(DTLB)]      = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
329  [C(ITLB)]      = (CACHE_READ),
330  [C(BPU)]       = (CACHE_READ),
331  [C(NODE)]      = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
332 };
333
334 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
335 {
336         if (perf_evsel__hw_cache_stat[type] & COP(op))
337                 return true;    /* valid */
338         else
339                 return false;   /* invalid */
340 }
341
342 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
343                                             char *bf, size_t size)
344 {
345         if (result) {
346                 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
347                                  perf_evsel__hw_cache_op[op][0],
348                                  perf_evsel__hw_cache_result[result][0]);
349         }
350
351         return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
352                          perf_evsel__hw_cache_op[op][1]);
353 }
354
355 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
356 {
357         u8 op, result, type = (config >>  0) & 0xff;
358         const char *err = "unknown-ext-hardware-cache-type";
359
360         if (type > PERF_COUNT_HW_CACHE_MAX)
361                 goto out_err;
362
363         op = (config >>  8) & 0xff;
364         err = "unknown-ext-hardware-cache-op";
365         if (op > PERF_COUNT_HW_CACHE_OP_MAX)
366                 goto out_err;
367
368         result = (config >> 16) & 0xff;
369         err = "unknown-ext-hardware-cache-result";
370         if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
371                 goto out_err;
372
373         err = "invalid-cache";
374         if (!perf_evsel__is_cache_op_valid(type, op))
375                 goto out_err;
376
377         return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
378 out_err:
379         return scnprintf(bf, size, "%s", err);
380 }
381
382 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
383 {
384         int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
385         return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
386 }
387
388 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
389 {
390         int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
391         return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
392 }
393
394 const char *perf_evsel__name(struct perf_evsel *evsel)
395 {
396         char bf[128];
397
398         if (evsel->name)
399                 return evsel->name;
400
401         switch (evsel->attr.type) {
402         case PERF_TYPE_RAW:
403                 perf_evsel__raw_name(evsel, bf, sizeof(bf));
404                 break;
405
406         case PERF_TYPE_HARDWARE:
407                 perf_evsel__hw_name(evsel, bf, sizeof(bf));
408                 break;
409
410         case PERF_TYPE_HW_CACHE:
411                 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
412                 break;
413
414         case PERF_TYPE_SOFTWARE:
415                 perf_evsel__sw_name(evsel, bf, sizeof(bf));
416                 break;
417
418         case PERF_TYPE_TRACEPOINT:
419                 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
420                 break;
421
422         case PERF_TYPE_BREAKPOINT:
423                 perf_evsel__bp_name(evsel, bf, sizeof(bf));
424                 break;
425
426         default:
427                 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
428                           evsel->attr.type);
429                 break;
430         }
431
432         evsel->name = strdup(bf);
433
434         return evsel->name ?: "unknown";
435 }
436
437 const char *perf_evsel__group_name(struct perf_evsel *evsel)
438 {
439         return evsel->group_name ?: "anon group";
440 }
441
442 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
443 {
444         int ret;
445         struct perf_evsel *pos;
446         const char *group_name = perf_evsel__group_name(evsel);
447
448         ret = scnprintf(buf, size, "%s", group_name);
449
450         ret += scnprintf(buf + ret, size - ret, " { %s",
451                          perf_evsel__name(evsel));
452
453         for_each_group_member(pos, evsel)
454                 ret += scnprintf(buf + ret, size - ret, ", %s",
455                                  perf_evsel__name(pos));
456
457         ret += scnprintf(buf + ret, size - ret, " }");
458
459         return ret;
460 }
461
462 /*
463  * The enable_on_exec/disabled value strategy:
464  *
465  *  1) For any type of traced program:
466  *    - all independent events and group leaders are disabled
467  *    - all group members are enabled
468  *
469  *     Group members are ruled by group leaders. They need to
470  *     be enabled, because the group scheduling relies on that.
471  *
472  *  2) For traced programs executed by perf:
473  *     - all independent events and group leaders have
474  *       enable_on_exec set
475  *     - we don't specifically enable or disable any event during
476  *       the record command
477  *
478  *     Independent events and group leaders are initially disabled
479  *     and get enabled by exec. Group members are ruled by group
480  *     leaders as stated in 1).
481  *
482  *  3) For traced programs attached by perf (pid/tid):
483  *     - we specifically enable or disable all events during
484  *       the record command
485  *
486  *     When attaching events to already running traced we
487  *     enable/disable events specifically, as there's no
488  *     initial traced exec call.
489  */
490 void perf_evsel__config(struct perf_evsel *evsel,
491                         struct perf_record_opts *opts)
492 {
493         struct perf_event_attr *attr = &evsel->attr;
494         int track = !evsel->idx; /* only the first counter needs these */
495
496         attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
497         attr->inherit       = !opts->no_inherit;
498
499         perf_evsel__set_sample_bit(evsel, IP);
500         perf_evsel__set_sample_bit(evsel, TID);
501
502         /*
503          * We default some events to a 1 default interval. But keep
504          * it a weak assumption overridable by the user.
505          */
506         if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
507                                      opts->user_interval != ULLONG_MAX)) {
508                 if (opts->freq) {
509                         perf_evsel__set_sample_bit(evsel, PERIOD);
510                         attr->freq              = 1;
511                         attr->sample_freq       = opts->freq;
512                 } else {
513                         attr->sample_period = opts->default_interval;
514                 }
515         }
516
517         if (opts->no_samples)
518                 attr->sample_freq = 0;
519
520         if (opts->inherit_stat)
521                 attr->inherit_stat = 1;
522
523         if (opts->sample_address) {
524                 perf_evsel__set_sample_bit(evsel, ADDR);
525                 attr->mmap_data = track;
526         }
527
528         if (opts->call_graph) {
529                 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
530
531                 if (opts->call_graph == CALLCHAIN_DWARF) {
532                         perf_evsel__set_sample_bit(evsel, REGS_USER);
533                         perf_evsel__set_sample_bit(evsel, STACK_USER);
534                         attr->sample_regs_user = PERF_REGS_MASK;
535                         attr->sample_stack_user = opts->stack_dump_size;
536                         attr->exclude_callchain_user = 1;
537                 }
538         }
539
540         if (perf_target__has_cpu(&opts->target))
541                 perf_evsel__set_sample_bit(evsel, CPU);
542
543         if (opts->period)
544                 perf_evsel__set_sample_bit(evsel, PERIOD);
545
546         if (!perf_missing_features.sample_id_all &&
547             (opts->sample_time || !opts->no_inherit ||
548              perf_target__has_cpu(&opts->target)))
549                 perf_evsel__set_sample_bit(evsel, TIME);
550
551         if (opts->raw_samples) {
552                 perf_evsel__set_sample_bit(evsel, TIME);
553                 perf_evsel__set_sample_bit(evsel, RAW);
554                 perf_evsel__set_sample_bit(evsel, CPU);
555         }
556
557         if (opts->no_delay) {
558                 attr->watermark = 0;
559                 attr->wakeup_events = 1;
560         }
561         if (opts->branch_stack) {
562                 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
563                 attr->branch_sample_type = opts->branch_stack;
564         }
565
566         if (opts->sample_weight)
567                 attr->sample_type       |= PERF_SAMPLE_WEIGHT;
568
569         attr->mmap = track;
570         attr->comm = track;
571
572         /*
573          * XXX see the function comment above
574          *
575          * Disabling only independent events or group leaders,
576          * keeping group members enabled.
577          */
578         if (perf_evsel__is_group_leader(evsel))
579                 attr->disabled = 1;
580
581         /*
582          * Setting enable_on_exec for independent events and
583          * group leaders for traced executed by perf.
584          */
585         if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
586                 attr->enable_on_exec = 1;
587 }
588
589 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
590 {
591         int cpu, thread;
592         evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
593
594         if (evsel->fd) {
595                 for (cpu = 0; cpu < ncpus; cpu++) {
596                         for (thread = 0; thread < nthreads; thread++) {
597                                 FD(evsel, cpu, thread) = -1;
598                         }
599                 }
600         }
601
602         return evsel->fd != NULL ? 0 : -ENOMEM;
603 }
604
605 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
606                            const char *filter)
607 {
608         int cpu, thread;
609
610         for (cpu = 0; cpu < ncpus; cpu++) {
611                 for (thread = 0; thread < nthreads; thread++) {
612                         int fd = FD(evsel, cpu, thread),
613                             err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
614
615                         if (err)
616                                 return err;
617                 }
618         }
619
620         return 0;
621 }
622
623 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
624 {
625         evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
626         if (evsel->sample_id == NULL)
627                 return -ENOMEM;
628
629         evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
630         if (evsel->id == NULL) {
631                 xyarray__delete(evsel->sample_id);
632                 evsel->sample_id = NULL;
633                 return -ENOMEM;
634         }
635
636         return 0;
637 }
638
639 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
640 {
641         memset(evsel->counts, 0, (sizeof(*evsel->counts) +
642                                  (ncpus * sizeof(struct perf_counts_values))));
643 }
644
645 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
646 {
647         evsel->counts = zalloc((sizeof(*evsel->counts) +
648                                 (ncpus * sizeof(struct perf_counts_values))));
649         return evsel->counts != NULL ? 0 : -ENOMEM;
650 }
651
652 void perf_evsel__free_fd(struct perf_evsel *evsel)
653 {
654         xyarray__delete(evsel->fd);
655         evsel->fd = NULL;
656 }
657
658 void perf_evsel__free_id(struct perf_evsel *evsel)
659 {
660         xyarray__delete(evsel->sample_id);
661         evsel->sample_id = NULL;
662         free(evsel->id);
663         evsel->id = NULL;
664 }
665
666 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
667 {
668         int cpu, thread;
669
670         for (cpu = 0; cpu < ncpus; cpu++)
671                 for (thread = 0; thread < nthreads; ++thread) {
672                         close(FD(evsel, cpu, thread));
673                         FD(evsel, cpu, thread) = -1;
674                 }
675 }
676
677 void perf_evsel__free_counts(struct perf_evsel *evsel)
678 {
679         free(evsel->counts);
680 }
681
682 void perf_evsel__exit(struct perf_evsel *evsel)
683 {
684         assert(list_empty(&evsel->node));
685         perf_evsel__free_fd(evsel);
686         perf_evsel__free_id(evsel);
687 }
688
689 void perf_evsel__delete(struct perf_evsel *evsel)
690 {
691         perf_evsel__exit(evsel);
692         close_cgroup(evsel->cgrp);
693         free(evsel->group_name);
694         if (evsel->tp_format)
695                 pevent_free_format(evsel->tp_format);
696         free(evsel->name);
697         free(evsel);
698 }
699
700 static inline void compute_deltas(struct perf_evsel *evsel,
701                                   int cpu,
702                                   struct perf_counts_values *count)
703 {
704         struct perf_counts_values tmp;
705
706         if (!evsel->prev_raw_counts)
707                 return;
708
709         if (cpu == -1) {
710                 tmp = evsel->prev_raw_counts->aggr;
711                 evsel->prev_raw_counts->aggr = *count;
712         } else {
713                 tmp = evsel->prev_raw_counts->cpu[cpu];
714                 evsel->prev_raw_counts->cpu[cpu] = *count;
715         }
716
717         count->val = count->val - tmp.val;
718         count->ena = count->ena - tmp.ena;
719         count->run = count->run - tmp.run;
720 }
721
722 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
723                               int cpu, int thread, bool scale)
724 {
725         struct perf_counts_values count;
726         size_t nv = scale ? 3 : 1;
727
728         if (FD(evsel, cpu, thread) < 0)
729                 return -EINVAL;
730
731         if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
732                 return -ENOMEM;
733
734         if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
735                 return -errno;
736
737         compute_deltas(evsel, cpu, &count);
738
739         if (scale) {
740                 if (count.run == 0)
741                         count.val = 0;
742                 else if (count.run < count.ena)
743                         count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
744         } else
745                 count.ena = count.run = 0;
746
747         evsel->counts->cpu[cpu] = count;
748         return 0;
749 }
750
751 int __perf_evsel__read(struct perf_evsel *evsel,
752                        int ncpus, int nthreads, bool scale)
753 {
754         size_t nv = scale ? 3 : 1;
755         int cpu, thread;
756         struct perf_counts_values *aggr = &evsel->counts->aggr, count;
757
758         aggr->val = aggr->ena = aggr->run = 0;
759
760         for (cpu = 0; cpu < ncpus; cpu++) {
761                 for (thread = 0; thread < nthreads; thread++) {
762                         if (FD(evsel, cpu, thread) < 0)
763                                 continue;
764
765                         if (readn(FD(evsel, cpu, thread),
766                                   &count, nv * sizeof(u64)) < 0)
767                                 return -errno;
768
769                         aggr->val += count.val;
770                         if (scale) {
771                                 aggr->ena += count.ena;
772                                 aggr->run += count.run;
773                         }
774                 }
775         }
776
777         compute_deltas(evsel, -1, aggr);
778
779         evsel->counts->scaled = 0;
780         if (scale) {
781                 if (aggr->run == 0) {
782                         evsel->counts->scaled = -1;
783                         aggr->val = 0;
784                         return 0;
785                 }
786
787                 if (aggr->run < aggr->ena) {
788                         evsel->counts->scaled = 1;
789                         aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
790                 }
791         } else
792                 aggr->ena = aggr->run = 0;
793
794         return 0;
795 }
796
797 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
798 {
799         struct perf_evsel *leader = evsel->leader;
800         int fd;
801
802         if (perf_evsel__is_group_leader(evsel))
803                 return -1;
804
805         /*
806          * Leader must be already processed/open,
807          * if not it's a bug.
808          */
809         BUG_ON(!leader->fd);
810
811         fd = FD(leader, cpu, thread);
812         BUG_ON(fd == -1);
813
814         return fd;
815 }
816
817 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
818                               struct thread_map *threads)
819 {
820         int cpu, thread;
821         unsigned long flags = 0;
822         int pid = -1, err;
823
824         if (evsel->fd == NULL &&
825             perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
826                 return -ENOMEM;
827
828         if (evsel->cgrp) {
829                 flags = PERF_FLAG_PID_CGROUP;
830                 pid = evsel->cgrp->fd;
831         }
832
833 fallback_missing_features:
834         if (perf_missing_features.exclude_guest)
835                 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
836 retry_sample_id:
837         if (perf_missing_features.sample_id_all)
838                 evsel->attr.sample_id_all = 0;
839
840         for (cpu = 0; cpu < cpus->nr; cpu++) {
841
842                 for (thread = 0; thread < threads->nr; thread++) {
843                         int group_fd;
844
845                         if (!evsel->cgrp)
846                                 pid = threads->map[thread];
847
848                         group_fd = get_group_fd(evsel, cpu, thread);
849
850                         FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
851                                                                      pid,
852                                                                      cpus->map[cpu],
853                                                                      group_fd, flags);
854                         if (FD(evsel, cpu, thread) < 0) {
855                                 err = -errno;
856                                 goto try_fallback;
857                         }
858                 }
859         }
860
861         return 0;
862
863 try_fallback:
864         if (err != -EINVAL || cpu > 0 || thread > 0)
865                 goto out_close;
866
867         if (!perf_missing_features.exclude_guest &&
868             (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
869                 perf_missing_features.exclude_guest = true;
870                 goto fallback_missing_features;
871         } else if (!perf_missing_features.sample_id_all) {
872                 perf_missing_features.sample_id_all = true;
873                 goto retry_sample_id;
874         }
875
876 out_close:
877         do {
878                 while (--thread >= 0) {
879                         close(FD(evsel, cpu, thread));
880                         FD(evsel, cpu, thread) = -1;
881                 }
882                 thread = threads->nr;
883         } while (--cpu >= 0);
884         return err;
885 }
886
887 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
888 {
889         if (evsel->fd == NULL)
890                 return;
891
892         perf_evsel__close_fd(evsel, ncpus, nthreads);
893         perf_evsel__free_fd(evsel);
894         evsel->fd = NULL;
895 }
896
897 static struct {
898         struct cpu_map map;
899         int cpus[1];
900 } empty_cpu_map = {
901         .map.nr = 1,
902         .cpus   = { -1, },
903 };
904
905 static struct {
906         struct thread_map map;
907         int threads[1];
908 } empty_thread_map = {
909         .map.nr  = 1,
910         .threads = { -1, },
911 };
912
913 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
914                      struct thread_map *threads)
915 {
916         if (cpus == NULL) {
917                 /* Work around old compiler warnings about strict aliasing */
918                 cpus = &empty_cpu_map.map;
919         }
920
921         if (threads == NULL)
922                 threads = &empty_thread_map.map;
923
924         return __perf_evsel__open(evsel, cpus, threads);
925 }
926
927 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
928                              struct cpu_map *cpus)
929 {
930         return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
931 }
932
933 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
934                                 struct thread_map *threads)
935 {
936         return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
937 }
938
939 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
940                                        const union perf_event *event,
941                                        struct perf_sample *sample)
942 {
943         u64 type = evsel->attr.sample_type;
944         const u64 *array = event->sample.array;
945         bool swapped = evsel->needs_swap;
946         union u64_swap u;
947
948         array += ((event->header.size -
949                    sizeof(event->header)) / sizeof(u64)) - 1;
950
951         if (type & PERF_SAMPLE_CPU) {
952                 u.val64 = *array;
953                 if (swapped) {
954                         /* undo swap of u64, then swap on individual u32s */
955                         u.val64 = bswap_64(u.val64);
956                         u.val32[0] = bswap_32(u.val32[0]);
957                 }
958
959                 sample->cpu = u.val32[0];
960                 array--;
961         }
962
963         if (type & PERF_SAMPLE_STREAM_ID) {
964                 sample->stream_id = *array;
965                 array--;
966         }
967
968         if (type & PERF_SAMPLE_ID) {
969                 sample->id = *array;
970                 array--;
971         }
972
973         if (type & PERF_SAMPLE_TIME) {
974                 sample->time = *array;
975                 array--;
976         }
977
978         if (type & PERF_SAMPLE_TID) {
979                 u.val64 = *array;
980                 if (swapped) {
981                         /* undo swap of u64, then swap on individual u32s */
982                         u.val64 = bswap_64(u.val64);
983                         u.val32[0] = bswap_32(u.val32[0]);
984                         u.val32[1] = bswap_32(u.val32[1]);
985                 }
986
987                 sample->pid = u.val32[0];
988                 sample->tid = u.val32[1];
989         }
990
991         return 0;
992 }
993
994 static bool sample_overlap(const union perf_event *event,
995                            const void *offset, u64 size)
996 {
997         const void *base = event;
998
999         if (offset + size > base + event->header.size)
1000                 return true;
1001
1002         return false;
1003 }
1004
1005 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1006                              struct perf_sample *data)
1007 {
1008         u64 type = evsel->attr.sample_type;
1009         u64 regs_user = evsel->attr.sample_regs_user;
1010         bool swapped = evsel->needs_swap;
1011         const u64 *array;
1012
1013         /*
1014          * used for cross-endian analysis. See git commit 65014ab3
1015          * for why this goofiness is needed.
1016          */
1017         union u64_swap u;
1018
1019         memset(data, 0, sizeof(*data));
1020         data->cpu = data->pid = data->tid = -1;
1021         data->stream_id = data->id = data->time = -1ULL;
1022         data->period = 1;
1023         data->weight = 0;
1024
1025         if (event->header.type != PERF_RECORD_SAMPLE) {
1026                 if (!evsel->attr.sample_id_all)
1027                         return 0;
1028                 return perf_evsel__parse_id_sample(evsel, event, data);
1029         }
1030
1031         array = event->sample.array;
1032
1033         if (evsel->sample_size + sizeof(event->header) > event->header.size)
1034                 return -EFAULT;
1035
1036         if (type & PERF_SAMPLE_IP) {
1037                 data->ip = event->ip.ip;
1038                 array++;
1039         }
1040
1041         if (type & PERF_SAMPLE_TID) {
1042                 u.val64 = *array;
1043                 if (swapped) {
1044                         /* undo swap of u64, then swap on individual u32s */
1045                         u.val64 = bswap_64(u.val64);
1046                         u.val32[0] = bswap_32(u.val32[0]);
1047                         u.val32[1] = bswap_32(u.val32[1]);
1048                 }
1049
1050                 data->pid = u.val32[0];
1051                 data->tid = u.val32[1];
1052                 array++;
1053         }
1054
1055         if (type & PERF_SAMPLE_TIME) {
1056                 data->time = *array;
1057                 array++;
1058         }
1059
1060         data->addr = 0;
1061         if (type & PERF_SAMPLE_ADDR) {
1062                 data->addr = *array;
1063                 array++;
1064         }
1065
1066         data->id = -1ULL;
1067         if (type & PERF_SAMPLE_ID) {
1068                 data->id = *array;
1069                 array++;
1070         }
1071
1072         if (type & PERF_SAMPLE_STREAM_ID) {
1073                 data->stream_id = *array;
1074                 array++;
1075         }
1076
1077         if (type & PERF_SAMPLE_CPU) {
1078
1079                 u.val64 = *array;
1080                 if (swapped) {
1081                         /* undo swap of u64, then swap on individual u32s */
1082                         u.val64 = bswap_64(u.val64);
1083                         u.val32[0] = bswap_32(u.val32[0]);
1084                 }
1085
1086                 data->cpu = u.val32[0];
1087                 array++;
1088         }
1089
1090         if (type & PERF_SAMPLE_PERIOD) {
1091                 data->period = *array;
1092                 array++;
1093         }
1094
1095         if (type & PERF_SAMPLE_READ) {
1096                 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
1097                 return -1;
1098         }
1099
1100         if (type & PERF_SAMPLE_CALLCHAIN) {
1101                 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1102                         return -EFAULT;
1103
1104                 data->callchain = (struct ip_callchain *)array;
1105
1106                 if (sample_overlap(event, array, data->callchain->nr))
1107                         return -EFAULT;
1108
1109                 array += 1 + data->callchain->nr;
1110         }
1111
1112         if (type & PERF_SAMPLE_RAW) {
1113                 const u64 *pdata;
1114
1115                 u.val64 = *array;
1116                 if (WARN_ONCE(swapped,
1117                               "Endianness of raw data not corrected!\n")) {
1118                         /* undo swap of u64, then swap on individual u32s */
1119                         u.val64 = bswap_64(u.val64);
1120                         u.val32[0] = bswap_32(u.val32[0]);
1121                         u.val32[1] = bswap_32(u.val32[1]);
1122                 }
1123
1124                 if (sample_overlap(event, array, sizeof(u32)))
1125                         return -EFAULT;
1126
1127                 data->raw_size = u.val32[0];
1128                 pdata = (void *) array + sizeof(u32);
1129
1130                 if (sample_overlap(event, pdata, data->raw_size))
1131                         return -EFAULT;
1132
1133                 data->raw_data = (void *) pdata;
1134
1135                 array = (void *)array + data->raw_size + sizeof(u32);
1136         }
1137
1138         if (type & PERF_SAMPLE_BRANCH_STACK) {
1139                 u64 sz;
1140
1141                 data->branch_stack = (struct branch_stack *)array;
1142                 array++; /* nr */
1143
1144                 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1145                 sz /= sizeof(u64);
1146                 array += sz;
1147         }
1148
1149         if (type & PERF_SAMPLE_REGS_USER) {
1150                 /* First u64 tells us if we have any regs in sample. */
1151                 u64 avail = *array++;
1152
1153                 if (avail) {
1154                         data->user_regs.regs = (u64 *)array;
1155                         array += hweight_long(regs_user);
1156                 }
1157         }
1158
1159         if (type & PERF_SAMPLE_STACK_USER) {
1160                 u64 size = *array++;
1161
1162                 data->user_stack.offset = ((char *)(array - 1)
1163                                           - (char *) event);
1164
1165                 if (!size) {
1166                         data->user_stack.size = 0;
1167                 } else {
1168                         data->user_stack.data = (char *)array;
1169                         array += size / sizeof(*array);
1170                         data->user_stack.size = *array;
1171                 }
1172         }
1173
1174         data->weight = 0;
1175         if (type & PERF_SAMPLE_WEIGHT) {
1176                 data->weight = *array;
1177                 array++;
1178         }
1179
1180         data->data_src = PERF_MEM_DATA_SRC_NONE;
1181         if (type & PERF_SAMPLE_DATA_SRC) {
1182                 data->data_src = *array;
1183                 array++;
1184         }
1185
1186         return 0;
1187 }
1188
1189 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1190                                   const struct perf_sample *sample,
1191                                   bool swapped)
1192 {
1193         u64 *array;
1194
1195         /*
1196          * used for cross-endian analysis. See git commit 65014ab3
1197          * for why this goofiness is needed.
1198          */
1199         union u64_swap u;
1200
1201         array = event->sample.array;
1202
1203         if (type & PERF_SAMPLE_IP) {
1204                 event->ip.ip = sample->ip;
1205                 array++;
1206         }
1207
1208         if (type & PERF_SAMPLE_TID) {
1209                 u.val32[0] = sample->pid;
1210                 u.val32[1] = sample->tid;
1211                 if (swapped) {
1212                         /*
1213                          * Inverse of what is done in perf_evsel__parse_sample
1214                          */
1215                         u.val32[0] = bswap_32(u.val32[0]);
1216                         u.val32[1] = bswap_32(u.val32[1]);
1217                         u.val64 = bswap_64(u.val64);
1218                 }
1219
1220                 *array = u.val64;
1221                 array++;
1222         }
1223
1224         if (type & PERF_SAMPLE_TIME) {
1225                 *array = sample->time;
1226                 array++;
1227         }
1228
1229         if (type & PERF_SAMPLE_ADDR) {
1230                 *array = sample->addr;
1231                 array++;
1232         }
1233
1234         if (type & PERF_SAMPLE_ID) {
1235                 *array = sample->id;
1236                 array++;
1237         }
1238
1239         if (type & PERF_SAMPLE_STREAM_ID) {
1240                 *array = sample->stream_id;
1241                 array++;
1242         }
1243
1244         if (type & PERF_SAMPLE_CPU) {
1245                 u.val32[0] = sample->cpu;
1246                 if (swapped) {
1247                         /*
1248                          * Inverse of what is done in perf_evsel__parse_sample
1249                          */
1250                         u.val32[0] = bswap_32(u.val32[0]);
1251                         u.val64 = bswap_64(u.val64);
1252                 }
1253                 *array = u.val64;
1254                 array++;
1255         }
1256
1257         if (type & PERF_SAMPLE_PERIOD) {
1258                 *array = sample->period;
1259                 array++;
1260         }
1261
1262         return 0;
1263 }
1264
1265 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1266 {
1267         return pevent_find_field(evsel->tp_format, name);
1268 }
1269
1270 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1271                          const char *name)
1272 {
1273         struct format_field *field = perf_evsel__field(evsel, name);
1274         int offset;
1275
1276         if (!field)
1277                 return NULL;
1278
1279         offset = field->offset;
1280
1281         if (field->flags & FIELD_IS_DYNAMIC) {
1282                 offset = *(int *)(sample->raw_data + field->offset);
1283                 offset &= 0xffff;
1284         }
1285
1286         return sample->raw_data + offset;
1287 }
1288
1289 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1290                        const char *name)
1291 {
1292         struct format_field *field = perf_evsel__field(evsel, name);
1293         void *ptr;
1294         u64 value;
1295
1296         if (!field)
1297                 return 0;
1298
1299         ptr = sample->raw_data + field->offset;
1300
1301         switch (field->size) {
1302         case 1:
1303                 return *(u8 *)ptr;
1304         case 2:
1305                 value = *(u16 *)ptr;
1306                 break;
1307         case 4:
1308                 value = *(u32 *)ptr;
1309                 break;
1310         case 8:
1311                 value = *(u64 *)ptr;
1312                 break;
1313         default:
1314                 return 0;
1315         }
1316
1317         if (!evsel->needs_swap)
1318                 return value;
1319
1320         switch (field->size) {
1321         case 2:
1322                 return bswap_16(value);
1323         case 4:
1324                 return bswap_32(value);
1325         case 8:
1326                 return bswap_64(value);
1327         default:
1328                 return 0;
1329         }
1330
1331         return 0;
1332 }
1333
1334 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1335 {
1336         va_list args;
1337         int ret = 0;
1338
1339         if (!*first) {
1340                 ret += fprintf(fp, ",");
1341         } else {
1342                 ret += fprintf(fp, ":");
1343                 *first = false;
1344         }
1345
1346         va_start(args, fmt);
1347         ret += vfprintf(fp, fmt, args);
1348         va_end(args);
1349         return ret;
1350 }
1351
1352 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1353 {
1354         if (value == 0)
1355                 return 0;
1356
1357         return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1358 }
1359
1360 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1361
1362 struct bit_names {
1363         int bit;
1364         const char *name;
1365 };
1366
1367 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1368                          struct bit_names *bits, bool *first)
1369 {
1370         int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1371         bool first_bit = true;
1372
1373         do {
1374                 if (value & bits[i].bit) {
1375                         printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1376                         first_bit = false;
1377                 }
1378         } while (bits[++i].name != NULL);
1379
1380         return printed;
1381 }
1382
1383 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1384 {
1385 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1386         struct bit_names bits[] = {
1387                 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1388                 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1389                 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1390                 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1391                 { .name = NULL, }
1392         };
1393 #undef bit_name
1394         return bits__fprintf(fp, "sample_type", value, bits, first);
1395 }
1396
1397 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1398 {
1399 #define bit_name(n) { PERF_FORMAT_##n, #n }
1400         struct bit_names bits[] = {
1401                 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1402                 bit_name(ID), bit_name(GROUP),
1403                 { .name = NULL, }
1404         };
1405 #undef bit_name
1406         return bits__fprintf(fp, "read_format", value, bits, first);
1407 }
1408
1409 int perf_evsel__fprintf(struct perf_evsel *evsel,
1410                         struct perf_attr_details *details, FILE *fp)
1411 {
1412         bool first = true;
1413         int printed = 0;
1414
1415         if (details->event_group) {
1416                 struct perf_evsel *pos;
1417
1418                 if (!perf_evsel__is_group_leader(evsel))
1419                         return 0;
1420
1421                 if (evsel->nr_members > 1)
1422                         printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1423
1424                 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1425                 for_each_group_member(pos, evsel)
1426                         printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1427
1428                 if (evsel->nr_members > 1)
1429                         printed += fprintf(fp, "}");
1430                 goto out;
1431         }
1432
1433         printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1434
1435         if (details->verbose || details->freq) {
1436                 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1437                                          (u64)evsel->attr.sample_freq);
1438         }
1439
1440         if (details->verbose) {
1441                 if_print(type);
1442                 if_print(config);
1443                 if_print(config1);
1444                 if_print(config2);
1445                 if_print(size);
1446                 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1447                 if (evsel->attr.read_format)
1448                         printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
1449                 if_print(disabled);
1450                 if_print(inherit);
1451                 if_print(pinned);
1452                 if_print(exclusive);
1453                 if_print(exclude_user);
1454                 if_print(exclude_kernel);
1455                 if_print(exclude_hv);
1456                 if_print(exclude_idle);
1457                 if_print(mmap);
1458                 if_print(comm);
1459                 if_print(freq);
1460                 if_print(inherit_stat);
1461                 if_print(enable_on_exec);
1462                 if_print(task);
1463                 if_print(watermark);
1464                 if_print(precise_ip);
1465                 if_print(mmap_data);
1466                 if_print(sample_id_all);
1467                 if_print(exclude_host);
1468                 if_print(exclude_guest);
1469                 if_print(__reserved_1);
1470                 if_print(wakeup_events);
1471                 if_print(bp_type);
1472                 if_print(branch_sample_type);
1473         }
1474 out:
1475         fputc('\n', fp);
1476         return ++printed;
1477 }
1478
1479 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1480                           char *msg, size_t msgsize)
1481 {
1482         if ((err == ENOENT || err == ENXIO) &&
1483             evsel->attr.type   == PERF_TYPE_HARDWARE &&
1484             evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1485                 /*
1486                  * If it's cycles then fall back to hrtimer based
1487                  * cpu-clock-tick sw counter, which is always available even if
1488                  * no PMU support.
1489                  *
1490                  * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1491                  * b0a873e).
1492                  */
1493                 scnprintf(msg, msgsize, "%s",
1494 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1495
1496                 evsel->attr.type   = PERF_TYPE_SOFTWARE;
1497                 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1498
1499                 free(evsel->name);
1500                 evsel->name = NULL;
1501                 return true;
1502         }
1503
1504         return false;
1505 }
1506
1507 int perf_evsel__open_strerror(struct perf_evsel *evsel,
1508                               struct perf_target *target,
1509                               int err, char *msg, size_t size)
1510 {
1511         switch (err) {
1512         case EPERM:
1513         case EACCES:
1514                 return scnprintf(msg, size, "%s",
1515                  "You may not have permission to collect %sstats.\n"
1516                  "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1517                  " -1 - Not paranoid at all\n"
1518                  "  0 - Disallow raw tracepoint access for unpriv\n"
1519                  "  1 - Disallow cpu events for unpriv\n"
1520                  "  2 - Disallow kernel profiling for unpriv",
1521                                  target->system_wide ? "system-wide " : "");
1522         case ENOENT:
1523                 return scnprintf(msg, size, "The %s event is not supported.",
1524                                  perf_evsel__name(evsel));
1525         case EMFILE:
1526                 return scnprintf(msg, size, "%s",
1527                          "Too many events are opened.\n"
1528                          "Try again after reducing the number of events.");
1529         case ENODEV:
1530                 if (target->cpu_list)
1531                         return scnprintf(msg, size, "%s",
1532          "No such device - did you specify an out-of-range profile CPU?\n");
1533                 break;
1534         case EOPNOTSUPP:
1535                 if (evsel->attr.precise_ip)
1536                         return scnprintf(msg, size, "%s",
1537         "\'precise\' request may not be supported. Try removing 'p' modifier.");
1538 #if defined(__i386__) || defined(__x86_64__)
1539                 if (evsel->attr.type == PERF_TYPE_HARDWARE)
1540                         return scnprintf(msg, size, "%s",
1541         "No hardware sampling interrupt available.\n"
1542         "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
1543 #endif
1544                 break;
1545         default:
1546                 break;
1547         }
1548
1549         return scnprintf(msg, size,
1550         "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).  \n"
1551         "/bin/dmesg may provide additional information.\n"
1552         "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
1553                          err, strerror(err), perf_evsel__name(evsel));
1554 }