Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.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 "asm/bug.h"
12 #include "evsel.h"
13 #include "evlist.h"
14 #include "util.h"
15 #include "cpumap.h"
16 #include "thread_map.h"
17 #include "target.h"
18 #include "../../include/linux/perf_event.h"
19
20 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
21 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
22
23 int __perf_evsel__sample_size(u64 sample_type)
24 {
25         u64 mask = sample_type & PERF_SAMPLE_MASK;
26         int size = 0;
27         int i;
28
29         for (i = 0; i < 64; i++) {
30                 if (mask & (1ULL << i))
31                         size++;
32         }
33
34         size *= sizeof(u64);
35
36         return size;
37 }
38
39 void hists__init(struct hists *hists)
40 {
41         memset(hists, 0, sizeof(*hists));
42         hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
43         hists->entries_in = &hists->entries_in_array[0];
44         hists->entries_collapsed = RB_ROOT;
45         hists->entries = RB_ROOT;
46         pthread_mutex_init(&hists->lock, NULL);
47 }
48
49 void perf_evsel__init(struct perf_evsel *evsel,
50                       struct perf_event_attr *attr, int idx)
51 {
52         evsel->idx         = idx;
53         evsel->attr        = *attr;
54         INIT_LIST_HEAD(&evsel->node);
55         hists__init(&evsel->hists);
56 }
57
58 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
59 {
60         struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61
62         if (evsel != NULL)
63                 perf_evsel__init(evsel, attr, idx);
64
65         return evsel;
66 }
67
68 static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
69         "cycles",
70         "instructions",
71         "cache-references",
72         "cache-misses",
73         "branches",
74         "branch-misses",
75         "bus-cycles",
76         "stalled-cycles-frontend",
77         "stalled-cycles-backend",
78         "ref-cycles",
79 };
80
81 const char *__perf_evsel__hw_name(u64 config)
82 {
83         if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84                 return perf_evsel__hw_names[config];
85
86         return "unknown-hardware";
87 }
88
89 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
90 {
91         int colon = 0;
92         struct perf_event_attr *attr = &evsel->attr;
93         int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(attr->config));
94         bool exclude_guest_default = false;
95
96 #define MOD_PRINT(context, mod) do {                                    \
97                 if (!attr->exclude_##context) {                         \
98                         if (!colon) colon = r++;                        \
99                         r += scnprintf(bf + r, size - r, "%c", mod);    \
100                 } } while(0)
101
102         if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
103                 MOD_PRINT(kernel, 'k');
104                 MOD_PRINT(user, 'u');
105                 MOD_PRINT(hv, 'h');
106                 exclude_guest_default = true;
107         }
108
109         if (attr->precise_ip) {
110                 if (!colon)
111                         colon = r++;
112                 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
113                 exclude_guest_default = true;
114         }
115
116         if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
117                 MOD_PRINT(host, 'H');
118                 MOD_PRINT(guest, 'G');
119         }
120 #undef MOD_PRINT
121         if (colon)
122                 bf[colon] = ':';
123         return r;
124 }
125
126 int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
127 {
128         int ret;
129
130         switch (evsel->attr.type) {
131         case PERF_TYPE_RAW:
132                 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
133                 break;
134
135         case PERF_TYPE_HARDWARE:
136                 ret = perf_evsel__hw_name(evsel, bf, size);
137                 break;
138         default:
139                 /*
140                  * FIXME
141                  *
142                  * This is the minimal perf_evsel__name so that we can
143                  * reconstruct event names taking into account event modifiers.
144                  *
145                  * The old event_name uses it now for raw anr hw events, so that
146                  * we don't drag all the parsing stuff into the python binding.
147                  *
148                  * On the next devel cycle the rest of the event naming will be
149                  * brought here.
150                  */
151                 return 0;
152         }
153
154         return ret;
155 }
156
157 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
158                         struct perf_evsel *first)
159 {
160         struct perf_event_attr *attr = &evsel->attr;
161         int track = !evsel->idx; /* only the first counter needs these */
162
163         attr->disabled = 1;
164         attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
165         attr->inherit       = !opts->no_inherit;
166         attr->read_format   = PERF_FORMAT_TOTAL_TIME_ENABLED |
167                               PERF_FORMAT_TOTAL_TIME_RUNNING |
168                               PERF_FORMAT_ID;
169
170         attr->sample_type  |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
171
172         /*
173          * We default some events to a 1 default interval. But keep
174          * it a weak assumption overridable by the user.
175          */
176         if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
177                                      opts->user_interval != ULLONG_MAX)) {
178                 if (opts->freq) {
179                         attr->sample_type       |= PERF_SAMPLE_PERIOD;
180                         attr->freq              = 1;
181                         attr->sample_freq       = opts->freq;
182                 } else {
183                         attr->sample_period = opts->default_interval;
184                 }
185         }
186
187         if (opts->no_samples)
188                 attr->sample_freq = 0;
189
190         if (opts->inherit_stat)
191                 attr->inherit_stat = 1;
192
193         if (opts->sample_address) {
194                 attr->sample_type       |= PERF_SAMPLE_ADDR;
195                 attr->mmap_data = track;
196         }
197
198         if (opts->call_graph)
199                 attr->sample_type       |= PERF_SAMPLE_CALLCHAIN;
200
201         if (perf_target__has_cpu(&opts->target))
202                 attr->sample_type       |= PERF_SAMPLE_CPU;
203
204         if (opts->period)
205                 attr->sample_type       |= PERF_SAMPLE_PERIOD;
206
207         if (!opts->sample_id_all_missing &&
208             (opts->sample_time || !opts->no_inherit ||
209              perf_target__has_cpu(&opts->target)))
210                 attr->sample_type       |= PERF_SAMPLE_TIME;
211
212         if (opts->raw_samples) {
213                 attr->sample_type       |= PERF_SAMPLE_TIME;
214                 attr->sample_type       |= PERF_SAMPLE_RAW;
215                 attr->sample_type       |= PERF_SAMPLE_CPU;
216         }
217
218         if (opts->no_delay) {
219                 attr->watermark = 0;
220                 attr->wakeup_events = 1;
221         }
222         if (opts->branch_stack) {
223                 attr->sample_type       |= PERF_SAMPLE_BRANCH_STACK;
224                 attr->branch_sample_type = opts->branch_stack;
225         }
226
227         attr->mmap = track;
228         attr->comm = track;
229
230         if (perf_target__none(&opts->target) &&
231             (!opts->group || evsel == first)) {
232                 attr->enable_on_exec = 1;
233         }
234 }
235
236 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
237 {
238         int cpu, thread;
239         evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
240
241         if (evsel->fd) {
242                 for (cpu = 0; cpu < ncpus; cpu++) {
243                         for (thread = 0; thread < nthreads; thread++) {
244                                 FD(evsel, cpu, thread) = -1;
245                         }
246                 }
247         }
248
249         return evsel->fd != NULL ? 0 : -ENOMEM;
250 }
251
252 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
253 {
254         evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
255         if (evsel->sample_id == NULL)
256                 return -ENOMEM;
257
258         evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
259         if (evsel->id == NULL) {
260                 xyarray__delete(evsel->sample_id);
261                 evsel->sample_id = NULL;
262                 return -ENOMEM;
263         }
264
265         return 0;
266 }
267
268 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
269 {
270         evsel->counts = zalloc((sizeof(*evsel->counts) +
271                                 (ncpus * sizeof(struct perf_counts_values))));
272         return evsel->counts != NULL ? 0 : -ENOMEM;
273 }
274
275 void perf_evsel__free_fd(struct perf_evsel *evsel)
276 {
277         xyarray__delete(evsel->fd);
278         evsel->fd = NULL;
279 }
280
281 void perf_evsel__free_id(struct perf_evsel *evsel)
282 {
283         xyarray__delete(evsel->sample_id);
284         evsel->sample_id = NULL;
285         free(evsel->id);
286         evsel->id = NULL;
287 }
288
289 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
290 {
291         int cpu, thread;
292
293         for (cpu = 0; cpu < ncpus; cpu++)
294                 for (thread = 0; thread < nthreads; ++thread) {
295                         close(FD(evsel, cpu, thread));
296                         FD(evsel, cpu, thread) = -1;
297                 }
298 }
299
300 void perf_evsel__exit(struct perf_evsel *evsel)
301 {
302         assert(list_empty(&evsel->node));
303         xyarray__delete(evsel->fd);
304         xyarray__delete(evsel->sample_id);
305         free(evsel->id);
306 }
307
308 void perf_evsel__delete(struct perf_evsel *evsel)
309 {
310         perf_evsel__exit(evsel);
311         close_cgroup(evsel->cgrp);
312         free(evsel->name);
313         free(evsel);
314 }
315
316 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
317                               int cpu, int thread, bool scale)
318 {
319         struct perf_counts_values count;
320         size_t nv = scale ? 3 : 1;
321
322         if (FD(evsel, cpu, thread) < 0)
323                 return -EINVAL;
324
325         if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
326                 return -ENOMEM;
327
328         if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
329                 return -errno;
330
331         if (scale) {
332                 if (count.run == 0)
333                         count.val = 0;
334                 else if (count.run < count.ena)
335                         count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
336         } else
337                 count.ena = count.run = 0;
338
339         evsel->counts->cpu[cpu] = count;
340         return 0;
341 }
342
343 int __perf_evsel__read(struct perf_evsel *evsel,
344                        int ncpus, int nthreads, bool scale)
345 {
346         size_t nv = scale ? 3 : 1;
347         int cpu, thread;
348         struct perf_counts_values *aggr = &evsel->counts->aggr, count;
349
350         aggr->val = aggr->ena = aggr->run = 0;
351
352         for (cpu = 0; cpu < ncpus; cpu++) {
353                 for (thread = 0; thread < nthreads; thread++) {
354                         if (FD(evsel, cpu, thread) < 0)
355                                 continue;
356
357                         if (readn(FD(evsel, cpu, thread),
358                                   &count, nv * sizeof(u64)) < 0)
359                                 return -errno;
360
361                         aggr->val += count.val;
362                         if (scale) {
363                                 aggr->ena += count.ena;
364                                 aggr->run += count.run;
365                         }
366                 }
367         }
368
369         evsel->counts->scaled = 0;
370         if (scale) {
371                 if (aggr->run == 0) {
372                         evsel->counts->scaled = -1;
373                         aggr->val = 0;
374                         return 0;
375                 }
376
377                 if (aggr->run < aggr->ena) {
378                         evsel->counts->scaled = 1;
379                         aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
380                 }
381         } else
382                 aggr->ena = aggr->run = 0;
383
384         return 0;
385 }
386
387 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
388                               struct thread_map *threads, bool group,
389                               struct xyarray *group_fds)
390 {
391         int cpu, thread;
392         unsigned long flags = 0;
393         int pid = -1, err;
394
395         if (evsel->fd == NULL &&
396             perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
397                 return -ENOMEM;
398
399         if (evsel->cgrp) {
400                 flags = PERF_FLAG_PID_CGROUP;
401                 pid = evsel->cgrp->fd;
402         }
403
404         for (cpu = 0; cpu < cpus->nr; cpu++) {
405                 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
406
407                 for (thread = 0; thread < threads->nr; thread++) {
408
409                         if (!evsel->cgrp)
410                                 pid = threads->map[thread];
411
412                         FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
413                                                                      pid,
414                                                                      cpus->map[cpu],
415                                                                      group_fd, flags);
416                         if (FD(evsel, cpu, thread) < 0) {
417                                 err = -errno;
418                                 goto out_close;
419                         }
420
421                         if (group && group_fd == -1)
422                                 group_fd = FD(evsel, cpu, thread);
423                 }
424         }
425
426         return 0;
427
428 out_close:
429         do {
430                 while (--thread >= 0) {
431                         close(FD(evsel, cpu, thread));
432                         FD(evsel, cpu, thread) = -1;
433                 }
434                 thread = threads->nr;
435         } while (--cpu >= 0);
436         return err;
437 }
438
439 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
440 {
441         if (evsel->fd == NULL)
442                 return;
443
444         perf_evsel__close_fd(evsel, ncpus, nthreads);
445         perf_evsel__free_fd(evsel);
446         evsel->fd = NULL;
447 }
448
449 static struct {
450         struct cpu_map map;
451         int cpus[1];
452 } empty_cpu_map = {
453         .map.nr = 1,
454         .cpus   = { -1, },
455 };
456
457 static struct {
458         struct thread_map map;
459         int threads[1];
460 } empty_thread_map = {
461         .map.nr  = 1,
462         .threads = { -1, },
463 };
464
465 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
466                      struct thread_map *threads, bool group,
467                      struct xyarray *group_fd)
468 {
469         if (cpus == NULL) {
470                 /* Work around old compiler warnings about strict aliasing */
471                 cpus = &empty_cpu_map.map;
472         }
473
474         if (threads == NULL)
475                 threads = &empty_thread_map.map;
476
477         return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
478 }
479
480 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
481                              struct cpu_map *cpus, bool group,
482                              struct xyarray *group_fd)
483 {
484         return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
485                                   group_fd);
486 }
487
488 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
489                                 struct thread_map *threads, bool group,
490                                 struct xyarray *group_fd)
491 {
492         return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
493                                   group_fd);
494 }
495
496 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
497                                        struct perf_sample *sample)
498 {
499         const u64 *array = event->sample.array;
500
501         array += ((event->header.size -
502                    sizeof(event->header)) / sizeof(u64)) - 1;
503
504         if (type & PERF_SAMPLE_CPU) {
505                 u32 *p = (u32 *)array;
506                 sample->cpu = *p;
507                 array--;
508         }
509
510         if (type & PERF_SAMPLE_STREAM_ID) {
511                 sample->stream_id = *array;
512                 array--;
513         }
514
515         if (type & PERF_SAMPLE_ID) {
516                 sample->id = *array;
517                 array--;
518         }
519
520         if (type & PERF_SAMPLE_TIME) {
521                 sample->time = *array;
522                 array--;
523         }
524
525         if (type & PERF_SAMPLE_TID) {
526                 u32 *p = (u32 *)array;
527                 sample->pid = p[0];
528                 sample->tid = p[1];
529         }
530
531         return 0;
532 }
533
534 static bool sample_overlap(const union perf_event *event,
535                            const void *offset, u64 size)
536 {
537         const void *base = event;
538
539         if (offset + size > base + event->header.size)
540                 return true;
541
542         return false;
543 }
544
545 int perf_event__parse_sample(const union perf_event *event, u64 type,
546                              int sample_size, bool sample_id_all,
547                              struct perf_sample *data, bool swapped)
548 {
549         const u64 *array;
550
551         /*
552          * used for cross-endian analysis. See git commit 65014ab3
553          * for why this goofiness is needed.
554          */
555         union u64_swap u;
556
557         memset(data, 0, sizeof(*data));
558         data->cpu = data->pid = data->tid = -1;
559         data->stream_id = data->id = data->time = -1ULL;
560         data->period = 1;
561
562         if (event->header.type != PERF_RECORD_SAMPLE) {
563                 if (!sample_id_all)
564                         return 0;
565                 return perf_event__parse_id_sample(event, type, data);
566         }
567
568         array = event->sample.array;
569
570         if (sample_size + sizeof(event->header) > event->header.size)
571                 return -EFAULT;
572
573         if (type & PERF_SAMPLE_IP) {
574                 data->ip = event->ip.ip;
575                 array++;
576         }
577
578         if (type & PERF_SAMPLE_TID) {
579                 u.val64 = *array;
580                 if (swapped) {
581                         /* undo swap of u64, then swap on individual u32s */
582                         u.val64 = bswap_64(u.val64);
583                         u.val32[0] = bswap_32(u.val32[0]);
584                         u.val32[1] = bswap_32(u.val32[1]);
585                 }
586
587                 data->pid = u.val32[0];
588                 data->tid = u.val32[1];
589                 array++;
590         }
591
592         if (type & PERF_SAMPLE_TIME) {
593                 data->time = *array;
594                 array++;
595         }
596
597         data->addr = 0;
598         if (type & PERF_SAMPLE_ADDR) {
599                 data->addr = *array;
600                 array++;
601         }
602
603         data->id = -1ULL;
604         if (type & PERF_SAMPLE_ID) {
605                 data->id = *array;
606                 array++;
607         }
608
609         if (type & PERF_SAMPLE_STREAM_ID) {
610                 data->stream_id = *array;
611                 array++;
612         }
613
614         if (type & PERF_SAMPLE_CPU) {
615
616                 u.val64 = *array;
617                 if (swapped) {
618                         /* undo swap of u64, then swap on individual u32s */
619                         u.val64 = bswap_64(u.val64);
620                         u.val32[0] = bswap_32(u.val32[0]);
621                 }
622
623                 data->cpu = u.val32[0];
624                 array++;
625         }
626
627         if (type & PERF_SAMPLE_PERIOD) {
628                 data->period = *array;
629                 array++;
630         }
631
632         if (type & PERF_SAMPLE_READ) {
633                 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
634                 return -1;
635         }
636
637         if (type & PERF_SAMPLE_CALLCHAIN) {
638                 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
639                         return -EFAULT;
640
641                 data->callchain = (struct ip_callchain *)array;
642
643                 if (sample_overlap(event, array, data->callchain->nr))
644                         return -EFAULT;
645
646                 array += 1 + data->callchain->nr;
647         }
648
649         if (type & PERF_SAMPLE_RAW) {
650                 const u64 *pdata;
651
652                 u.val64 = *array;
653                 if (WARN_ONCE(swapped,
654                               "Endianness of raw data not corrected!\n")) {
655                         /* undo swap of u64, then swap on individual u32s */
656                         u.val64 = bswap_64(u.val64);
657                         u.val32[0] = bswap_32(u.val32[0]);
658                         u.val32[1] = bswap_32(u.val32[1]);
659                 }
660
661                 if (sample_overlap(event, array, sizeof(u32)))
662                         return -EFAULT;
663
664                 data->raw_size = u.val32[0];
665                 pdata = (void *) array + sizeof(u32);
666
667                 if (sample_overlap(event, pdata, data->raw_size))
668                         return -EFAULT;
669
670                 data->raw_data = (void *) pdata;
671
672                 array = (void *)array + data->raw_size + sizeof(u32);
673         }
674
675         if (type & PERF_SAMPLE_BRANCH_STACK) {
676                 u64 sz;
677
678                 data->branch_stack = (struct branch_stack *)array;
679                 array++; /* nr */
680
681                 sz = data->branch_stack->nr * sizeof(struct branch_entry);
682                 sz /= sizeof(u64);
683                 array += sz;
684         }
685         return 0;
686 }
687
688 int perf_event__synthesize_sample(union perf_event *event, u64 type,
689                                   const struct perf_sample *sample,
690                                   bool swapped)
691 {
692         u64 *array;
693
694         /*
695          * used for cross-endian analysis. See git commit 65014ab3
696          * for why this goofiness is needed.
697          */
698         union u64_swap u;
699
700         array = event->sample.array;
701
702         if (type & PERF_SAMPLE_IP) {
703                 event->ip.ip = sample->ip;
704                 array++;
705         }
706
707         if (type & PERF_SAMPLE_TID) {
708                 u.val32[0] = sample->pid;
709                 u.val32[1] = sample->tid;
710                 if (swapped) {
711                         /*
712                          * Inverse of what is done in perf_event__parse_sample
713                          */
714                         u.val32[0] = bswap_32(u.val32[0]);
715                         u.val32[1] = bswap_32(u.val32[1]);
716                         u.val64 = bswap_64(u.val64);
717                 }
718
719                 *array = u.val64;
720                 array++;
721         }
722
723         if (type & PERF_SAMPLE_TIME) {
724                 *array = sample->time;
725                 array++;
726         }
727
728         if (type & PERF_SAMPLE_ADDR) {
729                 *array = sample->addr;
730                 array++;
731         }
732
733         if (type & PERF_SAMPLE_ID) {
734                 *array = sample->id;
735                 array++;
736         }
737
738         if (type & PERF_SAMPLE_STREAM_ID) {
739                 *array = sample->stream_id;
740                 array++;
741         }
742
743         if (type & PERF_SAMPLE_CPU) {
744                 u.val32[0] = sample->cpu;
745                 if (swapped) {
746                         /*
747                          * Inverse of what is done in perf_event__parse_sample
748                          */
749                         u.val32[0] = bswap_32(u.val32[0]);
750                         u.val64 = bswap_64(u.val64);
751                 }
752                 *array = u.val64;
753                 array++;
754         }
755
756         if (type & PERF_SAMPLE_PERIOD) {
757                 *array = sample->period;
758                 array++;
759         }
760
761         return 0;
762 }