perf tools: Add ability to synthesize event according to a sample
[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
18 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
19 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
20
21 int __perf_evsel__sample_size(u64 sample_type)
22 {
23         u64 mask = sample_type & PERF_SAMPLE_MASK;
24         int size = 0;
25         int i;
26
27         for (i = 0; i < 64; i++) {
28                 if (mask & (1ULL << i))
29                         size++;
30         }
31
32         size *= sizeof(u64);
33
34         return size;
35 }
36
37 static void hists__init(struct hists *hists)
38 {
39         memset(hists, 0, sizeof(*hists));
40         hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
41         hists->entries_in = &hists->entries_in_array[0];
42         hists->entries_collapsed = RB_ROOT;
43         hists->entries = RB_ROOT;
44         pthread_mutex_init(&hists->lock, NULL);
45 }
46
47 void perf_evsel__init(struct perf_evsel *evsel,
48                       struct perf_event_attr *attr, int idx)
49 {
50         evsel->idx         = idx;
51         evsel->attr        = *attr;
52         INIT_LIST_HEAD(&evsel->node);
53         hists__init(&evsel->hists);
54 }
55
56 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
57 {
58         struct perf_evsel *evsel = zalloc(sizeof(*evsel));
59
60         if (evsel != NULL)
61                 perf_evsel__init(evsel, attr, idx);
62
63         return evsel;
64 }
65
66 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts)
67 {
68         struct perf_event_attr *attr = &evsel->attr;
69         int track = !evsel->idx; /* only the first counter needs these */
70
71         attr->sample_id_all = opts->sample_id_all_avail ? 1 : 0;
72         attr->inherit       = !opts->no_inherit;
73         attr->read_format   = PERF_FORMAT_TOTAL_TIME_ENABLED |
74                               PERF_FORMAT_TOTAL_TIME_RUNNING |
75                               PERF_FORMAT_ID;
76
77         attr->sample_type  |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
78
79         /*
80          * We default some events to a 1 default interval. But keep
81          * it a weak assumption overridable by the user.
82          */
83         if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
84                                      opts->user_interval != ULLONG_MAX)) {
85                 if (opts->freq) {
86                         attr->sample_type       |= PERF_SAMPLE_PERIOD;
87                         attr->freq              = 1;
88                         attr->sample_freq       = opts->freq;
89                 } else {
90                         attr->sample_period = opts->default_interval;
91                 }
92         }
93
94         if (opts->no_samples)
95                 attr->sample_freq = 0;
96
97         if (opts->inherit_stat)
98                 attr->inherit_stat = 1;
99
100         if (opts->sample_address) {
101                 attr->sample_type       |= PERF_SAMPLE_ADDR;
102                 attr->mmap_data = track;
103         }
104
105         if (opts->call_graph)
106                 attr->sample_type       |= PERF_SAMPLE_CALLCHAIN;
107
108         if (opts->system_wide)
109                 attr->sample_type       |= PERF_SAMPLE_CPU;
110
111         if (opts->sample_id_all_avail &&
112             (opts->sample_time || opts->system_wide ||
113              !opts->no_inherit || opts->cpu_list))
114                 attr->sample_type       |= PERF_SAMPLE_TIME;
115
116         if (opts->raw_samples) {
117                 attr->sample_type       |= PERF_SAMPLE_TIME;
118                 attr->sample_type       |= PERF_SAMPLE_RAW;
119                 attr->sample_type       |= PERF_SAMPLE_CPU;
120         }
121
122         if (opts->no_delay) {
123                 attr->watermark = 0;
124                 attr->wakeup_events = 1;
125         }
126
127         attr->mmap = track;
128         attr->comm = track;
129
130         if (opts->target_pid == -1 && opts->target_tid == -1 && !opts->system_wide) {
131                 attr->disabled = 1;
132                 attr->enable_on_exec = 1;
133         }
134 }
135
136 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
137 {
138         int cpu, thread;
139         evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
140
141         if (evsel->fd) {
142                 for (cpu = 0; cpu < ncpus; cpu++) {
143                         for (thread = 0; thread < nthreads; thread++) {
144                                 FD(evsel, cpu, thread) = -1;
145                         }
146                 }
147         }
148
149         return evsel->fd != NULL ? 0 : -ENOMEM;
150 }
151
152 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
153 {
154         evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
155         if (evsel->sample_id == NULL)
156                 return -ENOMEM;
157
158         evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
159         if (evsel->id == NULL) {
160                 xyarray__delete(evsel->sample_id);
161                 evsel->sample_id = NULL;
162                 return -ENOMEM;
163         }
164
165         return 0;
166 }
167
168 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
169 {
170         evsel->counts = zalloc((sizeof(*evsel->counts) +
171                                 (ncpus * sizeof(struct perf_counts_values))));
172         return evsel->counts != NULL ? 0 : -ENOMEM;
173 }
174
175 void perf_evsel__free_fd(struct perf_evsel *evsel)
176 {
177         xyarray__delete(evsel->fd);
178         evsel->fd = NULL;
179 }
180
181 void perf_evsel__free_id(struct perf_evsel *evsel)
182 {
183         xyarray__delete(evsel->sample_id);
184         evsel->sample_id = NULL;
185         free(evsel->id);
186         evsel->id = NULL;
187 }
188
189 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
190 {
191         int cpu, thread;
192
193         for (cpu = 0; cpu < ncpus; cpu++)
194                 for (thread = 0; thread < nthreads; ++thread) {
195                         close(FD(evsel, cpu, thread));
196                         FD(evsel, cpu, thread) = -1;
197                 }
198 }
199
200 void perf_evsel__exit(struct perf_evsel *evsel)
201 {
202         assert(list_empty(&evsel->node));
203         xyarray__delete(evsel->fd);
204         xyarray__delete(evsel->sample_id);
205         free(evsel->id);
206 }
207
208 void perf_evsel__delete(struct perf_evsel *evsel)
209 {
210         perf_evsel__exit(evsel);
211         close_cgroup(evsel->cgrp);
212         free(evsel->name);
213         free(evsel);
214 }
215
216 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
217                               int cpu, int thread, bool scale)
218 {
219         struct perf_counts_values count;
220         size_t nv = scale ? 3 : 1;
221
222         if (FD(evsel, cpu, thread) < 0)
223                 return -EINVAL;
224
225         if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
226                 return -ENOMEM;
227
228         if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
229                 return -errno;
230
231         if (scale) {
232                 if (count.run == 0)
233                         count.val = 0;
234                 else if (count.run < count.ena)
235                         count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
236         } else
237                 count.ena = count.run = 0;
238
239         evsel->counts->cpu[cpu] = count;
240         return 0;
241 }
242
243 int __perf_evsel__read(struct perf_evsel *evsel,
244                        int ncpus, int nthreads, bool scale)
245 {
246         size_t nv = scale ? 3 : 1;
247         int cpu, thread;
248         struct perf_counts_values *aggr = &evsel->counts->aggr, count;
249
250         aggr->val = aggr->ena = aggr->run = 0;
251
252         for (cpu = 0; cpu < ncpus; cpu++) {
253                 for (thread = 0; thread < nthreads; thread++) {
254                         if (FD(evsel, cpu, thread) < 0)
255                                 continue;
256
257                         if (readn(FD(evsel, cpu, thread),
258                                   &count, nv * sizeof(u64)) < 0)
259                                 return -errno;
260
261                         aggr->val += count.val;
262                         if (scale) {
263                                 aggr->ena += count.ena;
264                                 aggr->run += count.run;
265                         }
266                 }
267         }
268
269         evsel->counts->scaled = 0;
270         if (scale) {
271                 if (aggr->run == 0) {
272                         evsel->counts->scaled = -1;
273                         aggr->val = 0;
274                         return 0;
275                 }
276
277                 if (aggr->run < aggr->ena) {
278                         evsel->counts->scaled = 1;
279                         aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
280                 }
281         } else
282                 aggr->ena = aggr->run = 0;
283
284         return 0;
285 }
286
287 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
288                               struct thread_map *threads, bool group,
289                               struct xyarray *group_fds)
290 {
291         int cpu, thread;
292         unsigned long flags = 0;
293         int pid = -1, err;
294
295         if (evsel->fd == NULL &&
296             perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
297                 return -ENOMEM;
298
299         if (evsel->cgrp) {
300                 flags = PERF_FLAG_PID_CGROUP;
301                 pid = evsel->cgrp->fd;
302         }
303
304         for (cpu = 0; cpu < cpus->nr; cpu++) {
305                 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
306
307                 for (thread = 0; thread < threads->nr; thread++) {
308
309                         if (!evsel->cgrp)
310                                 pid = threads->map[thread];
311
312                         FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
313                                                                      pid,
314                                                                      cpus->map[cpu],
315                                                                      group_fd, flags);
316                         if (FD(evsel, cpu, thread) < 0) {
317                                 err = -errno;
318                                 goto out_close;
319                         }
320
321                         if (group && group_fd == -1)
322                                 group_fd = FD(evsel, cpu, thread);
323                 }
324         }
325
326         return 0;
327
328 out_close:
329         do {
330                 while (--thread >= 0) {
331                         close(FD(evsel, cpu, thread));
332                         FD(evsel, cpu, thread) = -1;
333                 }
334                 thread = threads->nr;
335         } while (--cpu >= 0);
336         return err;
337 }
338
339 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
340 {
341         if (evsel->fd == NULL)
342                 return;
343
344         perf_evsel__close_fd(evsel, ncpus, nthreads);
345         perf_evsel__free_fd(evsel);
346         evsel->fd = NULL;
347 }
348
349 static struct {
350         struct cpu_map map;
351         int cpus[1];
352 } empty_cpu_map = {
353         .map.nr = 1,
354         .cpus   = { -1, },
355 };
356
357 static struct {
358         struct thread_map map;
359         int threads[1];
360 } empty_thread_map = {
361         .map.nr  = 1,
362         .threads = { -1, },
363 };
364
365 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
366                      struct thread_map *threads, bool group,
367                      struct xyarray *group_fd)
368 {
369         if (cpus == NULL) {
370                 /* Work around old compiler warnings about strict aliasing */
371                 cpus = &empty_cpu_map.map;
372         }
373
374         if (threads == NULL)
375                 threads = &empty_thread_map.map;
376
377         return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
378 }
379
380 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
381                              struct cpu_map *cpus, bool group,
382                              struct xyarray *group_fd)
383 {
384         return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
385                                   group_fd);
386 }
387
388 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
389                                 struct thread_map *threads, bool group,
390                                 struct xyarray *group_fd)
391 {
392         return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
393                                   group_fd);
394 }
395
396 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
397                                        struct perf_sample *sample)
398 {
399         const u64 *array = event->sample.array;
400
401         array += ((event->header.size -
402                    sizeof(event->header)) / sizeof(u64)) - 1;
403
404         if (type & PERF_SAMPLE_CPU) {
405                 u32 *p = (u32 *)array;
406                 sample->cpu = *p;
407                 array--;
408         }
409
410         if (type & PERF_SAMPLE_STREAM_ID) {
411                 sample->stream_id = *array;
412                 array--;
413         }
414
415         if (type & PERF_SAMPLE_ID) {
416                 sample->id = *array;
417                 array--;
418         }
419
420         if (type & PERF_SAMPLE_TIME) {
421                 sample->time = *array;
422                 array--;
423         }
424
425         if (type & PERF_SAMPLE_TID) {
426                 u32 *p = (u32 *)array;
427                 sample->pid = p[0];
428                 sample->tid = p[1];
429         }
430
431         return 0;
432 }
433
434 static bool sample_overlap(const union perf_event *event,
435                            const void *offset, u64 size)
436 {
437         const void *base = event;
438
439         if (offset + size > base + event->header.size)
440                 return true;
441
442         return false;
443 }
444
445 int perf_event__parse_sample(const union perf_event *event, u64 type,
446                              int sample_size, bool sample_id_all,
447                              struct perf_sample *data, bool swapped)
448 {
449         const u64 *array;
450
451         /*
452          * used for cross-endian analysis. See git commit 65014ab3
453          * for why this goofiness is needed.
454          */
455         union {
456                 u64 val64;
457                 u32 val32[2];
458         } u;
459
460
461         data->cpu = data->pid = data->tid = -1;
462         data->stream_id = data->id = data->time = -1ULL;
463
464         if (event->header.type != PERF_RECORD_SAMPLE) {
465                 if (!sample_id_all)
466                         return 0;
467                 return perf_event__parse_id_sample(event, type, data);
468         }
469
470         array = event->sample.array;
471
472         if (sample_size + sizeof(event->header) > event->header.size)
473                 return -EFAULT;
474
475         if (type & PERF_SAMPLE_IP) {
476                 data->ip = event->ip.ip;
477                 array++;
478         }
479
480         if (type & PERF_SAMPLE_TID) {
481                 u.val64 = *array;
482                 if (swapped) {
483                         /* undo swap of u64, then swap on individual u32s */
484                         u.val64 = bswap_64(u.val64);
485                         u.val32[0] = bswap_32(u.val32[0]);
486                         u.val32[1] = bswap_32(u.val32[1]);
487                 }
488
489                 data->pid = u.val32[0];
490                 data->tid = u.val32[1];
491                 array++;
492         }
493
494         if (type & PERF_SAMPLE_TIME) {
495                 data->time = *array;
496                 array++;
497         }
498
499         data->addr = 0;
500         if (type & PERF_SAMPLE_ADDR) {
501                 data->addr = *array;
502                 array++;
503         }
504
505         data->id = -1ULL;
506         if (type & PERF_SAMPLE_ID) {
507                 data->id = *array;
508                 array++;
509         }
510
511         if (type & PERF_SAMPLE_STREAM_ID) {
512                 data->stream_id = *array;
513                 array++;
514         }
515
516         if (type & PERF_SAMPLE_CPU) {
517
518                 u.val64 = *array;
519                 if (swapped) {
520                         /* undo swap of u64, then swap on individual u32s */
521                         u.val64 = bswap_64(u.val64);
522                         u.val32[0] = bswap_32(u.val32[0]);
523                 }
524
525                 data->cpu = u.val32[0];
526                 array++;
527         }
528
529         if (type & PERF_SAMPLE_PERIOD) {
530                 data->period = *array;
531                 array++;
532         }
533
534         if (type & PERF_SAMPLE_READ) {
535                 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
536                 return -1;
537         }
538
539         if (type & PERF_SAMPLE_CALLCHAIN) {
540                 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
541                         return -EFAULT;
542
543                 data->callchain = (struct ip_callchain *)array;
544
545                 if (sample_overlap(event, array, data->callchain->nr))
546                         return -EFAULT;
547
548                 array += 1 + data->callchain->nr;
549         }
550
551         if (type & PERF_SAMPLE_RAW) {
552                 const u64 *pdata;
553
554                 u.val64 = *array;
555                 if (WARN_ONCE(swapped,
556                               "Endianness of raw data not corrected!\n")) {
557                         /* undo swap of u64, then swap on individual u32s */
558                         u.val64 = bswap_64(u.val64);
559                         u.val32[0] = bswap_32(u.val32[0]);
560                         u.val32[1] = bswap_32(u.val32[1]);
561                 }
562
563                 if (sample_overlap(event, array, sizeof(u32)))
564                         return -EFAULT;
565
566                 data->raw_size = u.val32[0];
567                 pdata = (void *) array + sizeof(u32);
568
569                 if (sample_overlap(event, pdata, data->raw_size))
570                         return -EFAULT;
571
572                 data->raw_data = (void *) pdata;
573         }
574
575         return 0;
576 }
577
578 int perf_event__synthesize_sample(union perf_event *event, u64 type,
579                                   const struct perf_sample *sample,
580                                   bool swapped)
581 {
582         u64 *array;
583
584         /*
585          * used for cross-endian analysis. See git commit 65014ab3
586          * for why this goofiness is needed.
587          */
588         union {
589                 u64 val64;
590                 u32 val32[2];
591         } u;
592
593         array = event->sample.array;
594
595         if (type & PERF_SAMPLE_IP) {
596                 event->ip.ip = sample->ip;
597                 array++;
598         }
599
600         if (type & PERF_SAMPLE_TID) {
601                 u.val32[0] = sample->pid;
602                 u.val32[1] = sample->tid;
603                 if (swapped) {
604                         /*
605                          * Inverse of what is done in perf_event__parse_sample
606                          */
607                         u.val32[0] = bswap_32(u.val32[0]);
608                         u.val32[1] = bswap_32(u.val32[1]);
609                         u.val64 = bswap_64(u.val64);
610                 }
611
612                 *array = u.val64;
613                 array++;
614         }
615
616         if (type & PERF_SAMPLE_TIME) {
617                 *array = sample->time;
618                 array++;
619         }
620
621         if (type & PERF_SAMPLE_ADDR) {
622                 *array = sample->addr;
623                 array++;
624         }
625
626         if (type & PERF_SAMPLE_ID) {
627                 *array = sample->id;
628                 array++;
629         }
630
631         if (type & PERF_SAMPLE_STREAM_ID) {
632                 *array = sample->stream_id;
633                 array++;
634         }
635
636         if (type & PERF_SAMPLE_CPU) {
637                 u.val32[0] = sample->cpu;
638                 if (swapped) {
639                         /*
640                          * Inverse of what is done in perf_event__parse_sample
641                          */
642                         u.val32[0] = bswap_32(u.val32[0]);
643                         u.val64 = bswap_64(u.val64);
644                 }
645                 *array = u.val64;
646                 array++;
647         }
648
649         if (type & PERF_SAMPLE_PERIOD) {
650                 *array = sample->period;
651                 array++;
652         }
653
654         return 0;
655 }