perf evsel: Introduce perf_evsel__compute_deltas function
[cascardo/linux.git] / tools / perf / util / evsel.c
1 /*
2  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3  *
4  * Parts came from builtin-{top,stat,record}.c, see those files for further
5  * copyright notes.
6  *
7  * Released under the GPL v2. (and only v2, not any later version)
8  */
9
10 #include <byteswap.h>
11 #include <linux/bitops.h>
12 #include <api/fs/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>
17 #include "asm/bug.h"
18 #include "callchain.h"
19 #include "cgroup.h"
20 #include "evsel.h"
21 #include "evlist.h"
22 #include "util.h"
23 #include "cpumap.h"
24 #include "thread_map.h"
25 #include "target.h"
26 #include "perf_regs.h"
27 #include "debug.h"
28 #include "trace-event.h"
29
30 static struct {
31         bool sample_id_all;
32         bool exclude_guest;
33         bool mmap2;
34         bool cloexec;
35 } perf_missing_features;
36
37 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
38 {
39         return 0;
40 }
41
42 static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
43 {
44 }
45
46 static struct {
47         size_t  size;
48         int     (*init)(struct perf_evsel *evsel);
49         void    (*fini)(struct perf_evsel *evsel);
50 } perf_evsel__object = {
51         .size = sizeof(struct perf_evsel),
52         .init = perf_evsel__no_extra_init,
53         .fini = perf_evsel__no_extra_fini,
54 };
55
56 int perf_evsel__object_config(size_t object_size,
57                               int (*init)(struct perf_evsel *evsel),
58                               void (*fini)(struct perf_evsel *evsel))
59 {
60
61         if (object_size == 0)
62                 goto set_methods;
63
64         if (perf_evsel__object.size > object_size)
65                 return -EINVAL;
66
67         perf_evsel__object.size = object_size;
68
69 set_methods:
70         if (init != NULL)
71                 perf_evsel__object.init = init;
72
73         if (fini != NULL)
74                 perf_evsel__object.fini = fini;
75
76         return 0;
77 }
78
79 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
80
81 int __perf_evsel__sample_size(u64 sample_type)
82 {
83         u64 mask = sample_type & PERF_SAMPLE_MASK;
84         int size = 0;
85         int i;
86
87         for (i = 0; i < 64; i++) {
88                 if (mask & (1ULL << i))
89                         size++;
90         }
91
92         size *= sizeof(u64);
93
94         return size;
95 }
96
97 /**
98  * __perf_evsel__calc_id_pos - calculate id_pos.
99  * @sample_type: sample type
100  *
101  * This function returns the position of the event id (PERF_SAMPLE_ID or
102  * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
103  * sample_event.
104  */
105 static int __perf_evsel__calc_id_pos(u64 sample_type)
106 {
107         int idx = 0;
108
109         if (sample_type & PERF_SAMPLE_IDENTIFIER)
110                 return 0;
111
112         if (!(sample_type & PERF_SAMPLE_ID))
113                 return -1;
114
115         if (sample_type & PERF_SAMPLE_IP)
116                 idx += 1;
117
118         if (sample_type & PERF_SAMPLE_TID)
119                 idx += 1;
120
121         if (sample_type & PERF_SAMPLE_TIME)
122                 idx += 1;
123
124         if (sample_type & PERF_SAMPLE_ADDR)
125                 idx += 1;
126
127         return idx;
128 }
129
130 /**
131  * __perf_evsel__calc_is_pos - calculate is_pos.
132  * @sample_type: sample type
133  *
134  * This function returns the position (counting backwards) of the event id
135  * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
136  * sample_id_all is used there is an id sample appended to non-sample events.
137  */
138 static int __perf_evsel__calc_is_pos(u64 sample_type)
139 {
140         int idx = 1;
141
142         if (sample_type & PERF_SAMPLE_IDENTIFIER)
143                 return 1;
144
145         if (!(sample_type & PERF_SAMPLE_ID))
146                 return -1;
147
148         if (sample_type & PERF_SAMPLE_CPU)
149                 idx += 1;
150
151         if (sample_type & PERF_SAMPLE_STREAM_ID)
152                 idx += 1;
153
154         return idx;
155 }
156
157 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
158 {
159         evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
160         evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
161 }
162
163 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
164                                   enum perf_event_sample_format bit)
165 {
166         if (!(evsel->attr.sample_type & bit)) {
167                 evsel->attr.sample_type |= bit;
168                 evsel->sample_size += sizeof(u64);
169                 perf_evsel__calc_id_pos(evsel);
170         }
171 }
172
173 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
174                                     enum perf_event_sample_format bit)
175 {
176         if (evsel->attr.sample_type & bit) {
177                 evsel->attr.sample_type &= ~bit;
178                 evsel->sample_size -= sizeof(u64);
179                 perf_evsel__calc_id_pos(evsel);
180         }
181 }
182
183 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
184                                bool can_sample_identifier)
185 {
186         if (can_sample_identifier) {
187                 perf_evsel__reset_sample_bit(evsel, ID);
188                 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
189         } else {
190                 perf_evsel__set_sample_bit(evsel, ID);
191         }
192         evsel->attr.read_format |= PERF_FORMAT_ID;
193 }
194
195 void perf_evsel__init(struct perf_evsel *evsel,
196                       struct perf_event_attr *attr, int idx)
197 {
198         evsel->idx         = idx;
199         evsel->tracking    = !idx;
200         evsel->attr        = *attr;
201         evsel->leader      = evsel;
202         evsel->unit        = "";
203         evsel->scale       = 1.0;
204         INIT_LIST_HEAD(&evsel->node);
205         perf_evsel__object.init(evsel);
206         evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
207         perf_evsel__calc_id_pos(evsel);
208 }
209
210 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
211 {
212         struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
213
214         if (evsel != NULL)
215                 perf_evsel__init(evsel, attr, idx);
216
217         return evsel;
218 }
219
220 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
221 {
222         struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
223
224         if (evsel != NULL) {
225                 struct perf_event_attr attr = {
226                         .type          = PERF_TYPE_TRACEPOINT,
227                         .sample_type   = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
228                                           PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
229                 };
230
231                 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
232                         goto out_free;
233
234                 evsel->tp_format = trace_event__tp_format(sys, name);
235                 if (evsel->tp_format == NULL)
236                         goto out_free;
237
238                 event_attr_init(&attr);
239                 attr.config = evsel->tp_format->id;
240                 attr.sample_period = 1;
241                 perf_evsel__init(evsel, &attr, idx);
242         }
243
244         return evsel;
245
246 out_free:
247         zfree(&evsel->name);
248         free(evsel);
249         return NULL;
250 }
251
252 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
253         "cycles",
254         "instructions",
255         "cache-references",
256         "cache-misses",
257         "branches",
258         "branch-misses",
259         "bus-cycles",
260         "stalled-cycles-frontend",
261         "stalled-cycles-backend",
262         "ref-cycles",
263 };
264
265 static const char *__perf_evsel__hw_name(u64 config)
266 {
267         if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
268                 return perf_evsel__hw_names[config];
269
270         return "unknown-hardware";
271 }
272
273 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
274 {
275         int colon = 0, r = 0;
276         struct perf_event_attr *attr = &evsel->attr;
277         bool exclude_guest_default = false;
278
279 #define MOD_PRINT(context, mod) do {                                    \
280                 if (!attr->exclude_##context) {                         \
281                         if (!colon) colon = ++r;                        \
282                         r += scnprintf(bf + r, size - r, "%c", mod);    \
283                 } } while(0)
284
285         if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
286                 MOD_PRINT(kernel, 'k');
287                 MOD_PRINT(user, 'u');
288                 MOD_PRINT(hv, 'h');
289                 exclude_guest_default = true;
290         }
291
292         if (attr->precise_ip) {
293                 if (!colon)
294                         colon = ++r;
295                 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
296                 exclude_guest_default = true;
297         }
298
299         if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
300                 MOD_PRINT(host, 'H');
301                 MOD_PRINT(guest, 'G');
302         }
303 #undef MOD_PRINT
304         if (colon)
305                 bf[colon - 1] = ':';
306         return r;
307 }
308
309 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
310 {
311         int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
312         return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
313 }
314
315 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
316         "cpu-clock",
317         "task-clock",
318         "page-faults",
319         "context-switches",
320         "cpu-migrations",
321         "minor-faults",
322         "major-faults",
323         "alignment-faults",
324         "emulation-faults",
325         "dummy",
326 };
327
328 static const char *__perf_evsel__sw_name(u64 config)
329 {
330         if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
331                 return perf_evsel__sw_names[config];
332         return "unknown-software";
333 }
334
335 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
336 {
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);
339 }
340
341 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
342 {
343         int r;
344
345         r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
346
347         if (type & HW_BREAKPOINT_R)
348                 r += scnprintf(bf + r, size - r, "r");
349
350         if (type & HW_BREAKPOINT_W)
351                 r += scnprintf(bf + r, size - r, "w");
352
353         if (type & HW_BREAKPOINT_X)
354                 r += scnprintf(bf + r, size - r, "x");
355
356         return r;
357 }
358
359 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
360 {
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);
364 }
365
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",       },
370  { "LLC",       "L2",                                                   },
371  { "dTLB",      "d-tlb",        "Data-TLB",                             },
372  { "iTLB",      "i-tlb",        "Instruction-TLB",                      },
373  { "branch",    "branches",     "bpu",          "btb",          "bpc",  },
374  { "node",                                                              },
375 };
376
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", },
382 };
383
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",                                                 },
388 };
389
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)
395
396 /*
397  * cache operartion stat
398  * L1I : Read and prefetch only
399  * ITLB and BPU : Read-only
400  */
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),
409 };
410
411 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
412 {
413         if (perf_evsel__hw_cache_stat[type] & COP(op))
414                 return true;    /* valid */
415         else
416                 return false;   /* invalid */
417 }
418
419 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
420                                             char *bf, size_t size)
421 {
422         if (result) {
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]);
426         }
427
428         return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
429                          perf_evsel__hw_cache_op[op][1]);
430 }
431
432 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
433 {
434         u8 op, result, type = (config >>  0) & 0xff;
435         const char *err = "unknown-ext-hardware-cache-type";
436
437         if (type > PERF_COUNT_HW_CACHE_MAX)
438                 goto out_err;
439
440         op = (config >>  8) & 0xff;
441         err = "unknown-ext-hardware-cache-op";
442         if (op > PERF_COUNT_HW_CACHE_OP_MAX)
443                 goto out_err;
444
445         result = (config >> 16) & 0xff;
446         err = "unknown-ext-hardware-cache-result";
447         if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
448                 goto out_err;
449
450         err = "invalid-cache";
451         if (!perf_evsel__is_cache_op_valid(type, op))
452                 goto out_err;
453
454         return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
455 out_err:
456         return scnprintf(bf, size, "%s", err);
457 }
458
459 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
460 {
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);
463 }
464
465 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
466 {
467         int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
468         return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
469 }
470
471 const char *perf_evsel__name(struct perf_evsel *evsel)
472 {
473         char bf[128];
474
475         if (evsel->name)
476                 return evsel->name;
477
478         switch (evsel->attr.type) {
479         case PERF_TYPE_RAW:
480                 perf_evsel__raw_name(evsel, bf, sizeof(bf));
481                 break;
482
483         case PERF_TYPE_HARDWARE:
484                 perf_evsel__hw_name(evsel, bf, sizeof(bf));
485                 break;
486
487         case PERF_TYPE_HW_CACHE:
488                 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
489                 break;
490
491         case PERF_TYPE_SOFTWARE:
492                 perf_evsel__sw_name(evsel, bf, sizeof(bf));
493                 break;
494
495         case PERF_TYPE_TRACEPOINT:
496                 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
497                 break;
498
499         case PERF_TYPE_BREAKPOINT:
500                 perf_evsel__bp_name(evsel, bf, sizeof(bf));
501                 break;
502
503         default:
504                 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
505                           evsel->attr.type);
506                 break;
507         }
508
509         evsel->name = strdup(bf);
510
511         return evsel->name ?: "unknown";
512 }
513
514 const char *perf_evsel__group_name(struct perf_evsel *evsel)
515 {
516         return evsel->group_name ?: "anon group";
517 }
518
519 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
520 {
521         int ret;
522         struct perf_evsel *pos;
523         const char *group_name = perf_evsel__group_name(evsel);
524
525         ret = scnprintf(buf, size, "%s", group_name);
526
527         ret += scnprintf(buf + ret, size - ret, " { %s",
528                          perf_evsel__name(evsel));
529
530         for_each_group_member(pos, evsel)
531                 ret += scnprintf(buf + ret, size - ret, ", %s",
532                                  perf_evsel__name(pos));
533
534         ret += scnprintf(buf + ret, size - ret, " }");
535
536         return ret;
537 }
538
539 static void
540 perf_evsel__config_callgraph(struct perf_evsel *evsel)
541 {
542         bool function = perf_evsel__is_function_event(evsel);
543         struct perf_event_attr *attr = &evsel->attr;
544
545         perf_evsel__set_sample_bit(evsel, CALLCHAIN);
546
547         if (callchain_param.record_mode == CALLCHAIN_DWARF) {
548                 if (!function) {
549                         perf_evsel__set_sample_bit(evsel, REGS_USER);
550                         perf_evsel__set_sample_bit(evsel, STACK_USER);
551                         attr->sample_regs_user = PERF_REGS_MASK;
552                         attr->sample_stack_user = callchain_param.dump_size;
553                         attr->exclude_callchain_user = 1;
554                 } else {
555                         pr_info("Cannot use DWARF unwind for function trace event,"
556                                 " falling back to framepointers.\n");
557                 }
558         }
559
560         if (function) {
561                 pr_info("Disabling user space callchains for function trace event.\n");
562                 attr->exclude_callchain_user = 1;
563         }
564 }
565
566 /*
567  * The enable_on_exec/disabled value strategy:
568  *
569  *  1) For any type of traced program:
570  *    - all independent events and group leaders are disabled
571  *    - all group members are enabled
572  *
573  *     Group members are ruled by group leaders. They need to
574  *     be enabled, because the group scheduling relies on that.
575  *
576  *  2) For traced programs executed by perf:
577  *     - all independent events and group leaders have
578  *       enable_on_exec set
579  *     - we don't specifically enable or disable any event during
580  *       the record command
581  *
582  *     Independent events and group leaders are initially disabled
583  *     and get enabled by exec. Group members are ruled by group
584  *     leaders as stated in 1).
585  *
586  *  3) For traced programs attached by perf (pid/tid):
587  *     - we specifically enable or disable all events during
588  *       the record command
589  *
590  *     When attaching events to already running traced we
591  *     enable/disable events specifically, as there's no
592  *     initial traced exec call.
593  */
594 void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
595 {
596         struct perf_evsel *leader = evsel->leader;
597         struct perf_event_attr *attr = &evsel->attr;
598         int track = evsel->tracking;
599         bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
600
601         attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
602         attr->inherit       = !opts->no_inherit;
603
604         perf_evsel__set_sample_bit(evsel, IP);
605         perf_evsel__set_sample_bit(evsel, TID);
606
607         if (evsel->sample_read) {
608                 perf_evsel__set_sample_bit(evsel, READ);
609
610                 /*
611                  * We need ID even in case of single event, because
612                  * PERF_SAMPLE_READ process ID specific data.
613                  */
614                 perf_evsel__set_sample_id(evsel, false);
615
616                 /*
617                  * Apply group format only if we belong to group
618                  * with more than one members.
619                  */
620                 if (leader->nr_members > 1) {
621                         attr->read_format |= PERF_FORMAT_GROUP;
622                         attr->inherit = 0;
623                 }
624         }
625
626         /*
627          * We default some events to have a default interval. But keep
628          * it a weak assumption overridable by the user.
629          */
630         if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
631                                      opts->user_interval != ULLONG_MAX)) {
632                 if (opts->freq) {
633                         perf_evsel__set_sample_bit(evsel, PERIOD);
634                         attr->freq              = 1;
635                         attr->sample_freq       = opts->freq;
636                 } else {
637                         attr->sample_period = opts->default_interval;
638                 }
639         }
640
641         /*
642          * Disable sampling for all group members other
643          * than leader in case leader 'leads' the sampling.
644          */
645         if ((leader != evsel) && leader->sample_read) {
646                 attr->sample_freq   = 0;
647                 attr->sample_period = 0;
648         }
649
650         if (opts->no_samples)
651                 attr->sample_freq = 0;
652
653         if (opts->inherit_stat)
654                 attr->inherit_stat = 1;
655
656         if (opts->sample_address) {
657                 perf_evsel__set_sample_bit(evsel, ADDR);
658                 attr->mmap_data = track;
659         }
660
661         /*
662          * We don't allow user space callchains for  function trace
663          * event, due to issues with page faults while tracing page
664          * fault handler and its overall trickiness nature.
665          */
666         if (perf_evsel__is_function_event(evsel))
667                 evsel->attr.exclude_callchain_user = 1;
668
669         if (callchain_param.enabled && !evsel->no_aux_samples)
670                 perf_evsel__config_callgraph(evsel);
671
672         if (opts->sample_intr_regs) {
673                 attr->sample_regs_intr = PERF_REGS_MASK;
674                 perf_evsel__set_sample_bit(evsel, REGS_INTR);
675         }
676
677         if (target__has_cpu(&opts->target))
678                 perf_evsel__set_sample_bit(evsel, CPU);
679
680         if (opts->period)
681                 perf_evsel__set_sample_bit(evsel, PERIOD);
682
683         /*
684          * When the user explicitely disabled time don't force it here.
685          */
686         if (opts->sample_time &&
687             (!perf_missing_features.sample_id_all &&
688             (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
689                 perf_evsel__set_sample_bit(evsel, TIME);
690
691         if (opts->raw_samples && !evsel->no_aux_samples) {
692                 perf_evsel__set_sample_bit(evsel, TIME);
693                 perf_evsel__set_sample_bit(evsel, RAW);
694                 perf_evsel__set_sample_bit(evsel, CPU);
695         }
696
697         if (opts->sample_address)
698                 perf_evsel__set_sample_bit(evsel, DATA_SRC);
699
700         if (opts->no_buffering) {
701                 attr->watermark = 0;
702                 attr->wakeup_events = 1;
703         }
704         if (opts->branch_stack && !evsel->no_aux_samples) {
705                 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
706                 attr->branch_sample_type = opts->branch_stack;
707         }
708
709         if (opts->sample_weight)
710                 perf_evsel__set_sample_bit(evsel, WEIGHT);
711
712         attr->mmap  = track;
713         attr->mmap2 = track && !perf_missing_features.mmap2;
714         attr->comm  = track;
715
716         if (opts->sample_transaction)
717                 perf_evsel__set_sample_bit(evsel, TRANSACTION);
718
719         /*
720          * XXX see the function comment above
721          *
722          * Disabling only independent events or group leaders,
723          * keeping group members enabled.
724          */
725         if (perf_evsel__is_group_leader(evsel))
726                 attr->disabled = 1;
727
728         /*
729          * Setting enable_on_exec for independent events and
730          * group leaders for traced executed by perf.
731          */
732         if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
733                 !opts->initial_delay)
734                 attr->enable_on_exec = 1;
735
736         if (evsel->immediate) {
737                 attr->disabled = 0;
738                 attr->enable_on_exec = 0;
739         }
740 }
741
742 static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
743 {
744         int cpu, thread;
745
746         if (evsel->system_wide)
747                 nthreads = 1;
748
749         evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
750
751         if (evsel->fd) {
752                 for (cpu = 0; cpu < ncpus; cpu++) {
753                         for (thread = 0; thread < nthreads; thread++) {
754                                 FD(evsel, cpu, thread) = -1;
755                         }
756                 }
757         }
758
759         return evsel->fd != NULL ? 0 : -ENOMEM;
760 }
761
762 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
763                           int ioc,  void *arg)
764 {
765         int cpu, thread;
766
767         if (evsel->system_wide)
768                 nthreads = 1;
769
770         for (cpu = 0; cpu < ncpus; cpu++) {
771                 for (thread = 0; thread < nthreads; thread++) {
772                         int fd = FD(evsel, cpu, thread),
773                             err = ioctl(fd, ioc, arg);
774
775                         if (err)
776                                 return err;
777                 }
778         }
779
780         return 0;
781 }
782
783 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
784                            const char *filter)
785 {
786         return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
787                                      PERF_EVENT_IOC_SET_FILTER,
788                                      (void *)filter);
789 }
790
791 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
792 {
793         return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
794                                      PERF_EVENT_IOC_ENABLE,
795                                      0);
796 }
797
798 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
799 {
800         if (evsel->system_wide)
801                 nthreads = 1;
802
803         evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
804         if (evsel->sample_id == NULL)
805                 return -ENOMEM;
806
807         evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
808         if (evsel->id == NULL) {
809                 xyarray__delete(evsel->sample_id);
810                 evsel->sample_id = NULL;
811                 return -ENOMEM;
812         }
813
814         return 0;
815 }
816
817 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
818 {
819         memset(evsel->counts, 0, (sizeof(*evsel->counts) +
820                                  (ncpus * sizeof(struct perf_counts_values))));
821 }
822
823 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
824 {
825         evsel->counts = zalloc((sizeof(*evsel->counts) +
826                                 (ncpus * sizeof(struct perf_counts_values))));
827         return evsel->counts != NULL ? 0 : -ENOMEM;
828 }
829
830 static void perf_evsel__free_fd(struct perf_evsel *evsel)
831 {
832         xyarray__delete(evsel->fd);
833         evsel->fd = NULL;
834 }
835
836 static void perf_evsel__free_id(struct perf_evsel *evsel)
837 {
838         xyarray__delete(evsel->sample_id);
839         evsel->sample_id = NULL;
840         zfree(&evsel->id);
841 }
842
843 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
844 {
845         int cpu, thread;
846
847         if (evsel->system_wide)
848                 nthreads = 1;
849
850         for (cpu = 0; cpu < ncpus; cpu++)
851                 for (thread = 0; thread < nthreads; ++thread) {
852                         close(FD(evsel, cpu, thread));
853                         FD(evsel, cpu, thread) = -1;
854                 }
855 }
856
857 void perf_evsel__free_counts(struct perf_evsel *evsel)
858 {
859         zfree(&evsel->counts);
860 }
861
862 void perf_evsel__exit(struct perf_evsel *evsel)
863 {
864         assert(list_empty(&evsel->node));
865         perf_evsel__free_fd(evsel);
866         perf_evsel__free_id(evsel);
867         close_cgroup(evsel->cgrp);
868         zfree(&evsel->group_name);
869         zfree(&evsel->name);
870         perf_evsel__object.fini(evsel);
871 }
872
873 void perf_evsel__delete(struct perf_evsel *evsel)
874 {
875         perf_evsel__exit(evsel);
876         free(evsel);
877 }
878
879 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu,
880                                 struct perf_counts_values *count)
881 {
882         struct perf_counts_values tmp;
883
884         if (!evsel->prev_raw_counts)
885                 return;
886
887         if (cpu == -1) {
888                 tmp = evsel->prev_raw_counts->aggr;
889                 evsel->prev_raw_counts->aggr = *count;
890         } else {
891                 tmp = evsel->prev_raw_counts->cpu[cpu];
892                 evsel->prev_raw_counts->cpu[cpu] = *count;
893         }
894
895         count->val = count->val - tmp.val;
896         count->ena = count->ena - tmp.ena;
897         count->run = count->run - tmp.run;
898 }
899
900 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
901                               int cpu, int thread, bool scale)
902 {
903         struct perf_counts_values count;
904         size_t nv = scale ? 3 : 1;
905
906         if (FD(evsel, cpu, thread) < 0)
907                 return -EINVAL;
908
909         if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
910                 return -ENOMEM;
911
912         if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
913                 return -errno;
914
915         perf_evsel__compute_deltas(evsel, cpu, &count);
916
917         if (scale) {
918                 if (count.run == 0)
919                         count.val = 0;
920                 else if (count.run < count.ena)
921                         count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
922         } else
923                 count.ena = count.run = 0;
924
925         evsel->counts->cpu[cpu] = count;
926         return 0;
927 }
928
929 int __perf_evsel__read(struct perf_evsel *evsel,
930                        int ncpus, int nthreads, bool scale)
931 {
932         size_t nv = scale ? 3 : 1;
933         int cpu, thread;
934         struct perf_counts_values *aggr = &evsel->counts->aggr, count;
935
936         if (evsel->system_wide)
937                 nthreads = 1;
938
939         aggr->val = aggr->ena = aggr->run = 0;
940
941         for (cpu = 0; cpu < ncpus; cpu++) {
942                 for (thread = 0; thread < nthreads; thread++) {
943                         if (FD(evsel, cpu, thread) < 0)
944                                 continue;
945
946                         if (readn(FD(evsel, cpu, thread),
947                                   &count, nv * sizeof(u64)) < 0)
948                                 return -errno;
949
950                         aggr->val += count.val;
951                         if (scale) {
952                                 aggr->ena += count.ena;
953                                 aggr->run += count.run;
954                         }
955                 }
956         }
957
958         perf_evsel__compute_deltas(evsel, -1, aggr);
959
960         evsel->counts->scaled = 0;
961         if (scale) {
962                 if (aggr->run == 0) {
963                         evsel->counts->scaled = -1;
964                         aggr->val = 0;
965                         return 0;
966                 }
967
968                 if (aggr->run < aggr->ena) {
969                         evsel->counts->scaled = 1;
970                         aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
971                 }
972         } else
973                 aggr->ena = aggr->run = 0;
974
975         return 0;
976 }
977
978 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
979 {
980         struct perf_evsel *leader = evsel->leader;
981         int fd;
982
983         if (perf_evsel__is_group_leader(evsel))
984                 return -1;
985
986         /*
987          * Leader must be already processed/open,
988          * if not it's a bug.
989          */
990         BUG_ON(!leader->fd);
991
992         fd = FD(leader, cpu, thread);
993         BUG_ON(fd == -1);
994
995         return fd;
996 }
997
998 #define __PRINT_ATTR(fmt, cast, field)  \
999         fprintf(fp, "  %-19s "fmt"\n", #field, cast attr->field)
1000
1001 #define PRINT_ATTR_U32(field)  __PRINT_ATTR("%u" , , field)
1002 #define PRINT_ATTR_X32(field)  __PRINT_ATTR("%#x", , field)
1003 #define PRINT_ATTR_U64(field)  __PRINT_ATTR("%" PRIu64, (uint64_t), field)
1004 #define PRINT_ATTR_X64(field)  __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
1005
1006 #define PRINT_ATTR2N(name1, field1, name2, field2)      \
1007         fprintf(fp, "  %-19s %u    %-19s %u\n",         \
1008         name1, attr->field1, name2, attr->field2)
1009
1010 #define PRINT_ATTR2(field1, field2) \
1011         PRINT_ATTR2N(#field1, field1, #field2, field2)
1012
1013 static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
1014 {
1015         size_t ret = 0;
1016
1017         ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1018         ret += fprintf(fp, "perf_event_attr:\n");
1019
1020         ret += PRINT_ATTR_U32(type);
1021         ret += PRINT_ATTR_U32(size);
1022         ret += PRINT_ATTR_X64(config);
1023         ret += PRINT_ATTR_U64(sample_period);
1024         ret += PRINT_ATTR_U64(sample_freq);
1025         ret += PRINT_ATTR_X64(sample_type);
1026         ret += PRINT_ATTR_X64(read_format);
1027
1028         ret += PRINT_ATTR2(disabled, inherit);
1029         ret += PRINT_ATTR2(pinned, exclusive);
1030         ret += PRINT_ATTR2(exclude_user, exclude_kernel);
1031         ret += PRINT_ATTR2(exclude_hv, exclude_idle);
1032         ret += PRINT_ATTR2(mmap, comm);
1033         ret += PRINT_ATTR2(mmap2, comm_exec);
1034         ret += PRINT_ATTR2(freq, inherit_stat);
1035         ret += PRINT_ATTR2(enable_on_exec, task);
1036         ret += PRINT_ATTR2(watermark, precise_ip);
1037         ret += PRINT_ATTR2(mmap_data, sample_id_all);
1038         ret += PRINT_ATTR2(exclude_host, exclude_guest);
1039         ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
1040                             "excl.callchain_user", exclude_callchain_user);
1041
1042         ret += PRINT_ATTR_U32(wakeup_events);
1043         ret += PRINT_ATTR_U32(wakeup_watermark);
1044         ret += PRINT_ATTR_X32(bp_type);
1045         ret += PRINT_ATTR_X64(bp_addr);
1046         ret += PRINT_ATTR_X64(config1);
1047         ret += PRINT_ATTR_U64(bp_len);
1048         ret += PRINT_ATTR_X64(config2);
1049         ret += PRINT_ATTR_X64(branch_sample_type);
1050         ret += PRINT_ATTR_X64(sample_regs_user);
1051         ret += PRINT_ATTR_U32(sample_stack_user);
1052         ret += PRINT_ATTR_X64(sample_regs_intr);
1053
1054         ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1055
1056         return ret;
1057 }
1058
1059 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1060                               struct thread_map *threads)
1061 {
1062         int cpu, thread, nthreads;
1063         unsigned long flags = PERF_FLAG_FD_CLOEXEC;
1064         int pid = -1, err;
1065         enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1066
1067         if (evsel->system_wide)
1068                 nthreads = 1;
1069         else
1070                 nthreads = threads->nr;
1071
1072         if (evsel->fd == NULL &&
1073             perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
1074                 return -ENOMEM;
1075
1076         if (evsel->cgrp) {
1077                 flags |= PERF_FLAG_PID_CGROUP;
1078                 pid = evsel->cgrp->fd;
1079         }
1080
1081 fallback_missing_features:
1082         if (perf_missing_features.cloexec)
1083                 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1084         if (perf_missing_features.mmap2)
1085                 evsel->attr.mmap2 = 0;
1086         if (perf_missing_features.exclude_guest)
1087                 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1088 retry_sample_id:
1089         if (perf_missing_features.sample_id_all)
1090                 evsel->attr.sample_id_all = 0;
1091
1092         if (verbose >= 2)
1093                 perf_event_attr__fprintf(&evsel->attr, stderr);
1094
1095         for (cpu = 0; cpu < cpus->nr; cpu++) {
1096
1097                 for (thread = 0; thread < nthreads; thread++) {
1098                         int group_fd;
1099
1100                         if (!evsel->cgrp && !evsel->system_wide)
1101                                 pid = threads->map[thread];
1102
1103                         group_fd = get_group_fd(evsel, cpu, thread);
1104 retry_open:
1105                         pr_debug2("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx\n",
1106                                   pid, cpus->map[cpu], group_fd, flags);
1107
1108                         FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1109                                                                      pid,
1110                                                                      cpus->map[cpu],
1111                                                                      group_fd, flags);
1112                         if (FD(evsel, cpu, thread) < 0) {
1113                                 err = -errno;
1114                                 pr_debug2("sys_perf_event_open failed, error %d\n",
1115                                           err);
1116                                 goto try_fallback;
1117                         }
1118                         set_rlimit = NO_CHANGE;
1119                 }
1120         }
1121
1122         return 0;
1123
1124 try_fallback:
1125         /*
1126          * perf stat needs between 5 and 22 fds per CPU. When we run out
1127          * of them try to increase the limits.
1128          */
1129         if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1130                 struct rlimit l;
1131                 int old_errno = errno;
1132
1133                 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1134                         if (set_rlimit == NO_CHANGE)
1135                                 l.rlim_cur = l.rlim_max;
1136                         else {
1137                                 l.rlim_cur = l.rlim_max + 1000;
1138                                 l.rlim_max = l.rlim_cur;
1139                         }
1140                         if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1141                                 set_rlimit++;
1142                                 errno = old_errno;
1143                                 goto retry_open;
1144                         }
1145                 }
1146                 errno = old_errno;
1147         }
1148
1149         if (err != -EINVAL || cpu > 0 || thread > 0)
1150                 goto out_close;
1151
1152         if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1153                 perf_missing_features.cloexec = true;
1154                 goto fallback_missing_features;
1155         } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1156                 perf_missing_features.mmap2 = true;
1157                 goto fallback_missing_features;
1158         } else if (!perf_missing_features.exclude_guest &&
1159                    (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1160                 perf_missing_features.exclude_guest = true;
1161                 goto fallback_missing_features;
1162         } else if (!perf_missing_features.sample_id_all) {
1163                 perf_missing_features.sample_id_all = true;
1164                 goto retry_sample_id;
1165         }
1166
1167 out_close:
1168         do {
1169                 while (--thread >= 0) {
1170                         close(FD(evsel, cpu, thread));
1171                         FD(evsel, cpu, thread) = -1;
1172                 }
1173                 thread = nthreads;
1174         } while (--cpu >= 0);
1175         return err;
1176 }
1177
1178 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1179 {
1180         if (evsel->fd == NULL)
1181                 return;
1182
1183         perf_evsel__close_fd(evsel, ncpus, nthreads);
1184         perf_evsel__free_fd(evsel);
1185 }
1186
1187 static struct {
1188         struct cpu_map map;
1189         int cpus[1];
1190 } empty_cpu_map = {
1191         .map.nr = 1,
1192         .cpus   = { -1, },
1193 };
1194
1195 static struct {
1196         struct thread_map map;
1197         int threads[1];
1198 } empty_thread_map = {
1199         .map.nr  = 1,
1200         .threads = { -1, },
1201 };
1202
1203 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1204                      struct thread_map *threads)
1205 {
1206         if (cpus == NULL) {
1207                 /* Work around old compiler warnings about strict aliasing */
1208                 cpus = &empty_cpu_map.map;
1209         }
1210
1211         if (threads == NULL)
1212                 threads = &empty_thread_map.map;
1213
1214         return __perf_evsel__open(evsel, cpus, threads);
1215 }
1216
1217 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1218                              struct cpu_map *cpus)
1219 {
1220         return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1221 }
1222
1223 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1224                                 struct thread_map *threads)
1225 {
1226         return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1227 }
1228
1229 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1230                                        const union perf_event *event,
1231                                        struct perf_sample *sample)
1232 {
1233         u64 type = evsel->attr.sample_type;
1234         const u64 *array = event->sample.array;
1235         bool swapped = evsel->needs_swap;
1236         union u64_swap u;
1237
1238         array += ((event->header.size -
1239                    sizeof(event->header)) / sizeof(u64)) - 1;
1240
1241         if (type & PERF_SAMPLE_IDENTIFIER) {
1242                 sample->id = *array;
1243                 array--;
1244         }
1245
1246         if (type & PERF_SAMPLE_CPU) {
1247                 u.val64 = *array;
1248                 if (swapped) {
1249                         /* undo swap of u64, then swap on individual u32s */
1250                         u.val64 = bswap_64(u.val64);
1251                         u.val32[0] = bswap_32(u.val32[0]);
1252                 }
1253
1254                 sample->cpu = u.val32[0];
1255                 array--;
1256         }
1257
1258         if (type & PERF_SAMPLE_STREAM_ID) {
1259                 sample->stream_id = *array;
1260                 array--;
1261         }
1262
1263         if (type & PERF_SAMPLE_ID) {
1264                 sample->id = *array;
1265                 array--;
1266         }
1267
1268         if (type & PERF_SAMPLE_TIME) {
1269                 sample->time = *array;
1270                 array--;
1271         }
1272
1273         if (type & PERF_SAMPLE_TID) {
1274                 u.val64 = *array;
1275                 if (swapped) {
1276                         /* undo swap of u64, then swap on individual u32s */
1277                         u.val64 = bswap_64(u.val64);
1278                         u.val32[0] = bswap_32(u.val32[0]);
1279                         u.val32[1] = bswap_32(u.val32[1]);
1280                 }
1281
1282                 sample->pid = u.val32[0];
1283                 sample->tid = u.val32[1];
1284                 array--;
1285         }
1286
1287         return 0;
1288 }
1289
1290 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1291                             u64 size)
1292 {
1293         return size > max_size || offset + size > endp;
1294 }
1295
1296 #define OVERFLOW_CHECK(offset, size, max_size)                          \
1297         do {                                                            \
1298                 if (overflow(endp, (max_size), (offset), (size)))       \
1299                         return -EFAULT;                                 \
1300         } while (0)
1301
1302 #define OVERFLOW_CHECK_u64(offset) \
1303         OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1304
1305 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1306                              struct perf_sample *data)
1307 {
1308         u64 type = evsel->attr.sample_type;
1309         bool swapped = evsel->needs_swap;
1310         const u64 *array;
1311         u16 max_size = event->header.size;
1312         const void *endp = (void *)event + max_size;
1313         u64 sz;
1314
1315         /*
1316          * used for cross-endian analysis. See git commit 65014ab3
1317          * for why this goofiness is needed.
1318          */
1319         union u64_swap u;
1320
1321         memset(data, 0, sizeof(*data));
1322         data->cpu = data->pid = data->tid = -1;
1323         data->stream_id = data->id = data->time = -1ULL;
1324         data->period = evsel->attr.sample_period;
1325         data->weight = 0;
1326
1327         if (event->header.type != PERF_RECORD_SAMPLE) {
1328                 if (!evsel->attr.sample_id_all)
1329                         return 0;
1330                 return perf_evsel__parse_id_sample(evsel, event, data);
1331         }
1332
1333         array = event->sample.array;
1334
1335         /*
1336          * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1337          * up to PERF_SAMPLE_PERIOD.  After that overflow() must be used to
1338          * check the format does not go past the end of the event.
1339          */
1340         if (evsel->sample_size + sizeof(event->header) > event->header.size)
1341                 return -EFAULT;
1342
1343         data->id = -1ULL;
1344         if (type & PERF_SAMPLE_IDENTIFIER) {
1345                 data->id = *array;
1346                 array++;
1347         }
1348
1349         if (type & PERF_SAMPLE_IP) {
1350                 data->ip = *array;
1351                 array++;
1352         }
1353
1354         if (type & PERF_SAMPLE_TID) {
1355                 u.val64 = *array;
1356                 if (swapped) {
1357                         /* undo swap of u64, then swap on individual u32s */
1358                         u.val64 = bswap_64(u.val64);
1359                         u.val32[0] = bswap_32(u.val32[0]);
1360                         u.val32[1] = bswap_32(u.val32[1]);
1361                 }
1362
1363                 data->pid = u.val32[0];
1364                 data->tid = u.val32[1];
1365                 array++;
1366         }
1367
1368         if (type & PERF_SAMPLE_TIME) {
1369                 data->time = *array;
1370                 array++;
1371         }
1372
1373         data->addr = 0;
1374         if (type & PERF_SAMPLE_ADDR) {
1375                 data->addr = *array;
1376                 array++;
1377         }
1378
1379         if (type & PERF_SAMPLE_ID) {
1380                 data->id = *array;
1381                 array++;
1382         }
1383
1384         if (type & PERF_SAMPLE_STREAM_ID) {
1385                 data->stream_id = *array;
1386                 array++;
1387         }
1388
1389         if (type & PERF_SAMPLE_CPU) {
1390
1391                 u.val64 = *array;
1392                 if (swapped) {
1393                         /* undo swap of u64, then swap on individual u32s */
1394                         u.val64 = bswap_64(u.val64);
1395                         u.val32[0] = bswap_32(u.val32[0]);
1396                 }
1397
1398                 data->cpu = u.val32[0];
1399                 array++;
1400         }
1401
1402         if (type & PERF_SAMPLE_PERIOD) {
1403                 data->period = *array;
1404                 array++;
1405         }
1406
1407         if (type & PERF_SAMPLE_READ) {
1408                 u64 read_format = evsel->attr.read_format;
1409
1410                 OVERFLOW_CHECK_u64(array);
1411                 if (read_format & PERF_FORMAT_GROUP)
1412                         data->read.group.nr = *array;
1413                 else
1414                         data->read.one.value = *array;
1415
1416                 array++;
1417
1418                 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1419                         OVERFLOW_CHECK_u64(array);
1420                         data->read.time_enabled = *array;
1421                         array++;
1422                 }
1423
1424                 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1425                         OVERFLOW_CHECK_u64(array);
1426                         data->read.time_running = *array;
1427                         array++;
1428                 }
1429
1430                 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1431                 if (read_format & PERF_FORMAT_GROUP) {
1432                         const u64 max_group_nr = UINT64_MAX /
1433                                         sizeof(struct sample_read_value);
1434
1435                         if (data->read.group.nr > max_group_nr)
1436                                 return -EFAULT;
1437                         sz = data->read.group.nr *
1438                              sizeof(struct sample_read_value);
1439                         OVERFLOW_CHECK(array, sz, max_size);
1440                         data->read.group.values =
1441                                         (struct sample_read_value *)array;
1442                         array = (void *)array + sz;
1443                 } else {
1444                         OVERFLOW_CHECK_u64(array);
1445                         data->read.one.id = *array;
1446                         array++;
1447                 }
1448         }
1449
1450         if (type & PERF_SAMPLE_CALLCHAIN) {
1451                 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1452
1453                 OVERFLOW_CHECK_u64(array);
1454                 data->callchain = (struct ip_callchain *)array++;
1455                 if (data->callchain->nr > max_callchain_nr)
1456                         return -EFAULT;
1457                 sz = data->callchain->nr * sizeof(u64);
1458                 OVERFLOW_CHECK(array, sz, max_size);
1459                 array = (void *)array + sz;
1460         }
1461
1462         if (type & PERF_SAMPLE_RAW) {
1463                 OVERFLOW_CHECK_u64(array);
1464                 u.val64 = *array;
1465                 if (WARN_ONCE(swapped,
1466                               "Endianness of raw data not corrected!\n")) {
1467                         /* undo swap of u64, then swap on individual u32s */
1468                         u.val64 = bswap_64(u.val64);
1469                         u.val32[0] = bswap_32(u.val32[0]);
1470                         u.val32[1] = bswap_32(u.val32[1]);
1471                 }
1472                 data->raw_size = u.val32[0];
1473                 array = (void *)array + sizeof(u32);
1474
1475                 OVERFLOW_CHECK(array, data->raw_size, max_size);
1476                 data->raw_data = (void *)array;
1477                 array = (void *)array + data->raw_size;
1478         }
1479
1480         if (type & PERF_SAMPLE_BRANCH_STACK) {
1481                 const u64 max_branch_nr = UINT64_MAX /
1482                                           sizeof(struct branch_entry);
1483
1484                 OVERFLOW_CHECK_u64(array);
1485                 data->branch_stack = (struct branch_stack *)array++;
1486
1487                 if (data->branch_stack->nr > max_branch_nr)
1488                         return -EFAULT;
1489                 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1490                 OVERFLOW_CHECK(array, sz, max_size);
1491                 array = (void *)array + sz;
1492         }
1493
1494         if (type & PERF_SAMPLE_REGS_USER) {
1495                 OVERFLOW_CHECK_u64(array);
1496                 data->user_regs.abi = *array;
1497                 array++;
1498
1499                 if (data->user_regs.abi) {
1500                         u64 mask = evsel->attr.sample_regs_user;
1501
1502                         sz = hweight_long(mask) * sizeof(u64);
1503                         OVERFLOW_CHECK(array, sz, max_size);
1504                         data->user_regs.mask = mask;
1505                         data->user_regs.regs = (u64 *)array;
1506                         array = (void *)array + sz;
1507                 }
1508         }
1509
1510         if (type & PERF_SAMPLE_STACK_USER) {
1511                 OVERFLOW_CHECK_u64(array);
1512                 sz = *array++;
1513
1514                 data->user_stack.offset = ((char *)(array - 1)
1515                                           - (char *) event);
1516
1517                 if (!sz) {
1518                         data->user_stack.size = 0;
1519                 } else {
1520                         OVERFLOW_CHECK(array, sz, max_size);
1521                         data->user_stack.data = (char *)array;
1522                         array = (void *)array + sz;
1523                         OVERFLOW_CHECK_u64(array);
1524                         data->user_stack.size = *array++;
1525                         if (WARN_ONCE(data->user_stack.size > sz,
1526                                       "user stack dump failure\n"))
1527                                 return -EFAULT;
1528                 }
1529         }
1530
1531         data->weight = 0;
1532         if (type & PERF_SAMPLE_WEIGHT) {
1533                 OVERFLOW_CHECK_u64(array);
1534                 data->weight = *array;
1535                 array++;
1536         }
1537
1538         data->data_src = PERF_MEM_DATA_SRC_NONE;
1539         if (type & PERF_SAMPLE_DATA_SRC) {
1540                 OVERFLOW_CHECK_u64(array);
1541                 data->data_src = *array;
1542                 array++;
1543         }
1544
1545         data->transaction = 0;
1546         if (type & PERF_SAMPLE_TRANSACTION) {
1547                 OVERFLOW_CHECK_u64(array);
1548                 data->transaction = *array;
1549                 array++;
1550         }
1551
1552         data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1553         if (type & PERF_SAMPLE_REGS_INTR) {
1554                 OVERFLOW_CHECK_u64(array);
1555                 data->intr_regs.abi = *array;
1556                 array++;
1557
1558                 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1559                         u64 mask = evsel->attr.sample_regs_intr;
1560
1561                         sz = hweight_long(mask) * sizeof(u64);
1562                         OVERFLOW_CHECK(array, sz, max_size);
1563                         data->intr_regs.mask = mask;
1564                         data->intr_regs.regs = (u64 *)array;
1565                         array = (void *)array + sz;
1566                 }
1567         }
1568
1569         return 0;
1570 }
1571
1572 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1573                                      u64 read_format)
1574 {
1575         size_t sz, result = sizeof(struct sample_event);
1576
1577         if (type & PERF_SAMPLE_IDENTIFIER)
1578                 result += sizeof(u64);
1579
1580         if (type & PERF_SAMPLE_IP)
1581                 result += sizeof(u64);
1582
1583         if (type & PERF_SAMPLE_TID)
1584                 result += sizeof(u64);
1585
1586         if (type & PERF_SAMPLE_TIME)
1587                 result += sizeof(u64);
1588
1589         if (type & PERF_SAMPLE_ADDR)
1590                 result += sizeof(u64);
1591
1592         if (type & PERF_SAMPLE_ID)
1593                 result += sizeof(u64);
1594
1595         if (type & PERF_SAMPLE_STREAM_ID)
1596                 result += sizeof(u64);
1597
1598         if (type & PERF_SAMPLE_CPU)
1599                 result += sizeof(u64);
1600
1601         if (type & PERF_SAMPLE_PERIOD)
1602                 result += sizeof(u64);
1603
1604         if (type & PERF_SAMPLE_READ) {
1605                 result += sizeof(u64);
1606                 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1607                         result += sizeof(u64);
1608                 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1609                         result += sizeof(u64);
1610                 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1611                 if (read_format & PERF_FORMAT_GROUP) {
1612                         sz = sample->read.group.nr *
1613                              sizeof(struct sample_read_value);
1614                         result += sz;
1615                 } else {
1616                         result += sizeof(u64);
1617                 }
1618         }
1619
1620         if (type & PERF_SAMPLE_CALLCHAIN) {
1621                 sz = (sample->callchain->nr + 1) * sizeof(u64);
1622                 result += sz;
1623         }
1624
1625         if (type & PERF_SAMPLE_RAW) {
1626                 result += sizeof(u32);
1627                 result += sample->raw_size;
1628         }
1629
1630         if (type & PERF_SAMPLE_BRANCH_STACK) {
1631                 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1632                 sz += sizeof(u64);
1633                 result += sz;
1634         }
1635
1636         if (type & PERF_SAMPLE_REGS_USER) {
1637                 if (sample->user_regs.abi) {
1638                         result += sizeof(u64);
1639                         sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1640                         result += sz;
1641                 } else {
1642                         result += sizeof(u64);
1643                 }
1644         }
1645
1646         if (type & PERF_SAMPLE_STACK_USER) {
1647                 sz = sample->user_stack.size;
1648                 result += sizeof(u64);
1649                 if (sz) {
1650                         result += sz;
1651                         result += sizeof(u64);
1652                 }
1653         }
1654
1655         if (type & PERF_SAMPLE_WEIGHT)
1656                 result += sizeof(u64);
1657
1658         if (type & PERF_SAMPLE_DATA_SRC)
1659                 result += sizeof(u64);
1660
1661         if (type & PERF_SAMPLE_TRANSACTION)
1662                 result += sizeof(u64);
1663
1664         if (type & PERF_SAMPLE_REGS_INTR) {
1665                 if (sample->intr_regs.abi) {
1666                         result += sizeof(u64);
1667                         sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1668                         result += sz;
1669                 } else {
1670                         result += sizeof(u64);
1671                 }
1672         }
1673
1674         return result;
1675 }
1676
1677 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1678                                   u64 read_format,
1679                                   const struct perf_sample *sample,
1680                                   bool swapped)
1681 {
1682         u64 *array;
1683         size_t sz;
1684         /*
1685          * used for cross-endian analysis. See git commit 65014ab3
1686          * for why this goofiness is needed.
1687          */
1688         union u64_swap u;
1689
1690         array = event->sample.array;
1691
1692         if (type & PERF_SAMPLE_IDENTIFIER) {
1693                 *array = sample->id;
1694                 array++;
1695         }
1696
1697         if (type & PERF_SAMPLE_IP) {
1698                 *array = sample->ip;
1699                 array++;
1700         }
1701
1702         if (type & PERF_SAMPLE_TID) {
1703                 u.val32[0] = sample->pid;
1704                 u.val32[1] = sample->tid;
1705                 if (swapped) {
1706                         /*
1707                          * Inverse of what is done in perf_evsel__parse_sample
1708                          */
1709                         u.val32[0] = bswap_32(u.val32[0]);
1710                         u.val32[1] = bswap_32(u.val32[1]);
1711                         u.val64 = bswap_64(u.val64);
1712                 }
1713
1714                 *array = u.val64;
1715                 array++;
1716         }
1717
1718         if (type & PERF_SAMPLE_TIME) {
1719                 *array = sample->time;
1720                 array++;
1721         }
1722
1723         if (type & PERF_SAMPLE_ADDR) {
1724                 *array = sample->addr;
1725                 array++;
1726         }
1727
1728         if (type & PERF_SAMPLE_ID) {
1729                 *array = sample->id;
1730                 array++;
1731         }
1732
1733         if (type & PERF_SAMPLE_STREAM_ID) {
1734                 *array = sample->stream_id;
1735                 array++;
1736         }
1737
1738         if (type & PERF_SAMPLE_CPU) {
1739                 u.val32[0] = sample->cpu;
1740                 if (swapped) {
1741                         /*
1742                          * Inverse of what is done in perf_evsel__parse_sample
1743                          */
1744                         u.val32[0] = bswap_32(u.val32[0]);
1745                         u.val64 = bswap_64(u.val64);
1746                 }
1747                 *array = u.val64;
1748                 array++;
1749         }
1750
1751         if (type & PERF_SAMPLE_PERIOD) {
1752                 *array = sample->period;
1753                 array++;
1754         }
1755
1756         if (type & PERF_SAMPLE_READ) {
1757                 if (read_format & PERF_FORMAT_GROUP)
1758                         *array = sample->read.group.nr;
1759                 else
1760                         *array = sample->read.one.value;
1761                 array++;
1762
1763                 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1764                         *array = sample->read.time_enabled;
1765                         array++;
1766                 }
1767
1768                 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1769                         *array = sample->read.time_running;
1770                         array++;
1771                 }
1772
1773                 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1774                 if (read_format & PERF_FORMAT_GROUP) {
1775                         sz = sample->read.group.nr *
1776                              sizeof(struct sample_read_value);
1777                         memcpy(array, sample->read.group.values, sz);
1778                         array = (void *)array + sz;
1779                 } else {
1780                         *array = sample->read.one.id;
1781                         array++;
1782                 }
1783         }
1784
1785         if (type & PERF_SAMPLE_CALLCHAIN) {
1786                 sz = (sample->callchain->nr + 1) * sizeof(u64);
1787                 memcpy(array, sample->callchain, sz);
1788                 array = (void *)array + sz;
1789         }
1790
1791         if (type & PERF_SAMPLE_RAW) {
1792                 u.val32[0] = sample->raw_size;
1793                 if (WARN_ONCE(swapped,
1794                               "Endianness of raw data not corrected!\n")) {
1795                         /*
1796                          * Inverse of what is done in perf_evsel__parse_sample
1797                          */
1798                         u.val32[0] = bswap_32(u.val32[0]);
1799                         u.val32[1] = bswap_32(u.val32[1]);
1800                         u.val64 = bswap_64(u.val64);
1801                 }
1802                 *array = u.val64;
1803                 array = (void *)array + sizeof(u32);
1804
1805                 memcpy(array, sample->raw_data, sample->raw_size);
1806                 array = (void *)array + sample->raw_size;
1807         }
1808
1809         if (type & PERF_SAMPLE_BRANCH_STACK) {
1810                 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1811                 sz += sizeof(u64);
1812                 memcpy(array, sample->branch_stack, sz);
1813                 array = (void *)array + sz;
1814         }
1815
1816         if (type & PERF_SAMPLE_REGS_USER) {
1817                 if (sample->user_regs.abi) {
1818                         *array++ = sample->user_regs.abi;
1819                         sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
1820                         memcpy(array, sample->user_regs.regs, sz);
1821                         array = (void *)array + sz;
1822                 } else {
1823                         *array++ = 0;
1824                 }
1825         }
1826
1827         if (type & PERF_SAMPLE_STACK_USER) {
1828                 sz = sample->user_stack.size;
1829                 *array++ = sz;
1830                 if (sz) {
1831                         memcpy(array, sample->user_stack.data, sz);
1832                         array = (void *)array + sz;
1833                         *array++ = sz;
1834                 }
1835         }
1836
1837         if (type & PERF_SAMPLE_WEIGHT) {
1838                 *array = sample->weight;
1839                 array++;
1840         }
1841
1842         if (type & PERF_SAMPLE_DATA_SRC) {
1843                 *array = sample->data_src;
1844                 array++;
1845         }
1846
1847         if (type & PERF_SAMPLE_TRANSACTION) {
1848                 *array = sample->transaction;
1849                 array++;
1850         }
1851
1852         if (type & PERF_SAMPLE_REGS_INTR) {
1853                 if (sample->intr_regs.abi) {
1854                         *array++ = sample->intr_regs.abi;
1855                         sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1856                         memcpy(array, sample->intr_regs.regs, sz);
1857                         array = (void *)array + sz;
1858                 } else {
1859                         *array++ = 0;
1860                 }
1861         }
1862
1863         return 0;
1864 }
1865
1866 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1867 {
1868         return pevent_find_field(evsel->tp_format, name);
1869 }
1870
1871 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1872                          const char *name)
1873 {
1874         struct format_field *field = perf_evsel__field(evsel, name);
1875         int offset;
1876
1877         if (!field)
1878                 return NULL;
1879
1880         offset = field->offset;
1881
1882         if (field->flags & FIELD_IS_DYNAMIC) {
1883                 offset = *(int *)(sample->raw_data + field->offset);
1884                 offset &= 0xffff;
1885         }
1886
1887         return sample->raw_data + offset;
1888 }
1889
1890 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1891                        const char *name)
1892 {
1893         struct format_field *field = perf_evsel__field(evsel, name);
1894         void *ptr;
1895         u64 value;
1896
1897         if (!field)
1898                 return 0;
1899
1900         ptr = sample->raw_data + field->offset;
1901
1902         switch (field->size) {
1903         case 1:
1904                 return *(u8 *)ptr;
1905         case 2:
1906                 value = *(u16 *)ptr;
1907                 break;
1908         case 4:
1909                 value = *(u32 *)ptr;
1910                 break;
1911         case 8:
1912                 value = *(u64 *)ptr;
1913                 break;
1914         default:
1915                 return 0;
1916         }
1917
1918         if (!evsel->needs_swap)
1919                 return value;
1920
1921         switch (field->size) {
1922         case 2:
1923                 return bswap_16(value);
1924         case 4:
1925                 return bswap_32(value);
1926         case 8:
1927                 return bswap_64(value);
1928         default:
1929                 return 0;
1930         }
1931
1932         return 0;
1933 }
1934
1935 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1936 {
1937         va_list args;
1938         int ret = 0;
1939
1940         if (!*first) {
1941                 ret += fprintf(fp, ",");
1942         } else {
1943                 ret += fprintf(fp, ":");
1944                 *first = false;
1945         }
1946
1947         va_start(args, fmt);
1948         ret += vfprintf(fp, fmt, args);
1949         va_end(args);
1950         return ret;
1951 }
1952
1953 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1954 {
1955         if (value == 0)
1956                 return 0;
1957
1958         return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1959 }
1960
1961 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1962
1963 struct bit_names {
1964         int bit;
1965         const char *name;
1966 };
1967
1968 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1969                          struct bit_names *bits, bool *first)
1970 {
1971         int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1972         bool first_bit = true;
1973
1974         do {
1975                 if (value & bits[i].bit) {
1976                         printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1977                         first_bit = false;
1978                 }
1979         } while (bits[++i].name != NULL);
1980
1981         return printed;
1982 }
1983
1984 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1985 {
1986 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1987         struct bit_names bits[] = {
1988                 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1989                 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1990                 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1991                 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1992                 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1993                 { .name = NULL, }
1994         };
1995 #undef bit_name
1996         return bits__fprintf(fp, "sample_type", value, bits, first);
1997 }
1998
1999 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
2000 {
2001 #define bit_name(n) { PERF_FORMAT_##n, #n }
2002         struct bit_names bits[] = {
2003                 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
2004                 bit_name(ID), bit_name(GROUP),
2005                 { .name = NULL, }
2006         };
2007 #undef bit_name
2008         return bits__fprintf(fp, "read_format", value, bits, first);
2009 }
2010
2011 int perf_evsel__fprintf(struct perf_evsel *evsel,
2012                         struct perf_attr_details *details, FILE *fp)
2013 {
2014         bool first = true;
2015         int printed = 0;
2016
2017         if (details->event_group) {
2018                 struct perf_evsel *pos;
2019
2020                 if (!perf_evsel__is_group_leader(evsel))
2021                         return 0;
2022
2023                 if (evsel->nr_members > 1)
2024                         printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2025
2026                 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2027                 for_each_group_member(pos, evsel)
2028                         printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2029
2030                 if (evsel->nr_members > 1)
2031                         printed += fprintf(fp, "}");
2032                 goto out;
2033         }
2034
2035         printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2036
2037         if (details->verbose || details->freq) {
2038                 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2039                                          (u64)evsel->attr.sample_freq);
2040         }
2041
2042         if (details->verbose) {
2043                 if_print(type);
2044                 if_print(config);
2045                 if_print(config1);
2046                 if_print(config2);
2047                 if_print(size);
2048                 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
2049                 if (evsel->attr.read_format)
2050                         printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
2051                 if_print(disabled);
2052                 if_print(inherit);
2053                 if_print(pinned);
2054                 if_print(exclusive);
2055                 if_print(exclude_user);
2056                 if_print(exclude_kernel);
2057                 if_print(exclude_hv);
2058                 if_print(exclude_idle);
2059                 if_print(mmap);
2060                 if_print(mmap2);
2061                 if_print(comm);
2062                 if_print(comm_exec);
2063                 if_print(freq);
2064                 if_print(inherit_stat);
2065                 if_print(enable_on_exec);
2066                 if_print(task);
2067                 if_print(watermark);
2068                 if_print(precise_ip);
2069                 if_print(mmap_data);
2070                 if_print(sample_id_all);
2071                 if_print(exclude_host);
2072                 if_print(exclude_guest);
2073                 if_print(__reserved_1);
2074                 if_print(wakeup_events);
2075                 if_print(bp_type);
2076                 if_print(branch_sample_type);
2077         }
2078 out:
2079         fputc('\n', fp);
2080         return ++printed;
2081 }
2082
2083 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2084                           char *msg, size_t msgsize)
2085 {
2086         if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
2087             evsel->attr.type   == PERF_TYPE_HARDWARE &&
2088             evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2089                 /*
2090                  * If it's cycles then fall back to hrtimer based
2091                  * cpu-clock-tick sw counter, which is always available even if
2092                  * no PMU support.
2093                  *
2094                  * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2095                  * b0a873e).
2096                  */
2097                 scnprintf(msg, msgsize, "%s",
2098 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2099
2100                 evsel->attr.type   = PERF_TYPE_SOFTWARE;
2101                 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2102
2103                 zfree(&evsel->name);
2104                 return true;
2105         }
2106
2107         return false;
2108 }
2109
2110 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2111                               int err, char *msg, size_t size)
2112 {
2113         char sbuf[STRERR_BUFSIZE];
2114
2115         switch (err) {
2116         case EPERM:
2117         case EACCES:
2118                 return scnprintf(msg, size,
2119                  "You may not have permission to collect %sstats.\n"
2120                  "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2121                  " -1 - Not paranoid at all\n"
2122                  "  0 - Disallow raw tracepoint access for unpriv\n"
2123                  "  1 - Disallow cpu events for unpriv\n"
2124                  "  2 - Disallow kernel profiling for unpriv",
2125                                  target->system_wide ? "system-wide " : "");
2126         case ENOENT:
2127                 return scnprintf(msg, size, "The %s event is not supported.",
2128                                  perf_evsel__name(evsel));
2129         case EMFILE:
2130                 return scnprintf(msg, size, "%s",
2131                          "Too many events are opened.\n"
2132                          "Try again after reducing the number of events.");
2133         case ENODEV:
2134                 if (target->cpu_list)
2135                         return scnprintf(msg, size, "%s",
2136          "No such device - did you specify an out-of-range profile CPU?\n");
2137                 break;
2138         case EOPNOTSUPP:
2139                 if (evsel->attr.precise_ip)
2140                         return scnprintf(msg, size, "%s",
2141         "\'precise\' request may not be supported. Try removing 'p' modifier.");
2142 #if defined(__i386__) || defined(__x86_64__)
2143                 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2144                         return scnprintf(msg, size, "%s",
2145         "No hardware sampling interrupt available.\n"
2146         "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2147 #endif
2148                 break;
2149         case EBUSY:
2150                 if (find_process("oprofiled"))
2151                         return scnprintf(msg, size,
2152         "The PMU counters are busy/taken by another profiler.\n"
2153         "We found oprofile daemon running, please stop it and try again.");
2154                 break;
2155         default:
2156                 break;
2157         }
2158
2159         return scnprintf(msg, size,
2160         "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2161         "/bin/dmesg may provide additional information.\n"
2162         "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2163                          err, strerror_r(err, sbuf, sizeof(sbuf)),
2164                          perf_evsel__name(evsel));
2165 }