blob: 0f0b7e11e2d90e879b3dafa632b74e1db2243c4f [file] [log] [blame]
Adrian Hunter718c6022015-04-09 18:53:42 +03001/*
2 * auxtrace.c: AUX area trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#include <sys/types.h>
17#include <sys/mman.h>
18#include <stdbool.h>
19
20#include <linux/kernel.h>
21#include <linux/perf_event.h>
22#include <linux/types.h>
23#include <linux/bitops.h>
24#include <linux/log2.h>
Adrian Huntere5027892015-04-21 12:21:51 +030025#include <linux/string.h>
Adrian Hunter718c6022015-04-09 18:53:42 +030026
Adrian Huntere5027892015-04-21 12:21:51 +030027#include <sys/param.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030028#include <stdlib.h>
Adrian Hunter85ed4722015-04-09 18:53:50 +030029#include <stdio.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030030#include <string.h>
Adrian Huntere5027892015-04-21 12:21:51 +030031#include <limits.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030032#include <errno.h>
Adrian Huntere5027892015-04-21 12:21:51 +030033#include <linux/list.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030034
Adrian Hunter718c6022015-04-09 18:53:42 +030035#include "../perf.h"
36#include "util.h"
37#include "evlist.h"
38#include "cpumap.h"
39#include "thread_map.h"
40#include "asm/bug.h"
41#include "auxtrace.h"
42
Adrian Hunterc3278f02015-04-09 18:53:54 +030043#include <linux/hash.h>
44
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030045#include "event.h"
Adrian Hunter85ed4722015-04-09 18:53:50 +030046#include "session.h"
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030047#include "debug.h"
Adrian Hunterf6986c952015-04-09 18:53:49 +030048#include "parse-options.h"
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030049
Adrian Hunter5efb1d52015-07-17 19:33:42 +030050#include "intel-pt.h"
51
Adrian Hunter718c6022015-04-09 18:53:42 +030052int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
53 struct auxtrace_mmap_params *mp,
54 void *userpg, int fd)
55{
56 struct perf_event_mmap_page *pc = userpg;
57
Adrian Hunter718c6022015-04-09 18:53:42 +030058 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
59
60 mm->userpg = userpg;
61 mm->mask = mp->mask;
62 mm->len = mp->len;
63 mm->prev = 0;
64 mm->idx = mp->idx;
65 mm->tid = mp->tid;
66 mm->cpu = mp->cpu;
67
68 if (!mp->len) {
69 mm->base = NULL;
70 return 0;
71 }
72
Adrian Huntera7fde092015-07-14 15:32:41 +030073#if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
74 pr_err("Cannot use AUX area tracing mmaps\n");
75 return -1;
76#endif
77
Adrian Hunter718c6022015-04-09 18:53:42 +030078 pc->aux_offset = mp->offset;
79 pc->aux_size = mp->len;
80
81 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
82 if (mm->base == MAP_FAILED) {
83 pr_debug2("failed to mmap AUX area\n");
84 mm->base = NULL;
85 return -1;
86 }
87
88 return 0;
89}
90
91void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
92{
93 if (mm->base) {
94 munmap(mm->base, mm->len);
95 mm->base = NULL;
96 }
97}
98
99void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
100 off_t auxtrace_offset,
101 unsigned int auxtrace_pages,
102 bool auxtrace_overwrite)
103{
104 if (auxtrace_pages) {
105 mp->offset = auxtrace_offset;
106 mp->len = auxtrace_pages * (size_t)page_size;
107 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
108 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
109 pr_debug2("AUX area mmap length %zu\n", mp->len);
110 } else {
111 mp->len = 0;
112 }
113}
114
115void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
116 struct perf_evlist *evlist, int idx,
117 bool per_cpu)
118{
119 mp->idx = idx;
120
121 if (per_cpu) {
122 mp->cpu = evlist->cpus->map[idx];
123 if (evlist->threads)
Jiri Olsae13798c2015-06-23 00:36:02 +0200124 mp->tid = thread_map__pid(evlist->threads, 0);
Adrian Hunter718c6022015-04-09 18:53:42 +0300125 else
126 mp->tid = -1;
127 } else {
128 mp->cpu = -1;
Jiri Olsae13798c2015-06-23 00:36:02 +0200129 mp->tid = thread_map__pid(evlist->threads, idx);
Adrian Hunter718c6022015-04-09 18:53:42 +0300130 }
131}
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300132
Adrian Huntere5027892015-04-21 12:21:51 +0300133#define AUXTRACE_INIT_NR_QUEUES 32
134
135static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
136{
137 struct auxtrace_queue *queue_array;
138 unsigned int max_nr_queues, i;
139
140 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
141 if (nr_queues > max_nr_queues)
142 return NULL;
143
144 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
145 if (!queue_array)
146 return NULL;
147
148 for (i = 0; i < nr_queues; i++) {
149 INIT_LIST_HEAD(&queue_array[i].head);
150 queue_array[i].priv = NULL;
151 }
152
153 return queue_array;
154}
155
156int auxtrace_queues__init(struct auxtrace_queues *queues)
157{
158 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
159 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
160 if (!queues->queue_array)
161 return -ENOMEM;
162 return 0;
163}
164
165static int auxtrace_queues__grow(struct auxtrace_queues *queues,
166 unsigned int new_nr_queues)
167{
168 unsigned int nr_queues = queues->nr_queues;
169 struct auxtrace_queue *queue_array;
170 unsigned int i;
171
172 if (!nr_queues)
173 nr_queues = AUXTRACE_INIT_NR_QUEUES;
174
175 while (nr_queues && nr_queues < new_nr_queues)
176 nr_queues <<= 1;
177
178 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
179 return -EINVAL;
180
181 queue_array = auxtrace_alloc_queue_array(nr_queues);
182 if (!queue_array)
183 return -ENOMEM;
184
185 for (i = 0; i < queues->nr_queues; i++) {
186 list_splice_tail(&queues->queue_array[i].head,
187 &queue_array[i].head);
188 queue_array[i].priv = queues->queue_array[i].priv;
189 }
190
191 queues->nr_queues = nr_queues;
192 queues->queue_array = queue_array;
193
194 return 0;
195}
196
197static void *auxtrace_copy_data(u64 size, struct perf_session *session)
198{
199 int fd = perf_data_file__fd(session->file);
200 void *p;
201 ssize_t ret;
202
203 if (size > SSIZE_MAX)
204 return NULL;
205
206 p = malloc(size);
207 if (!p)
208 return NULL;
209
210 ret = readn(fd, p, size);
211 if (ret != (ssize_t)size) {
212 free(p);
213 return NULL;
214 }
215
216 return p;
217}
218
219static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
220 unsigned int idx,
221 struct auxtrace_buffer *buffer)
222{
223 struct auxtrace_queue *queue;
224 int err;
225
226 if (idx >= queues->nr_queues) {
227 err = auxtrace_queues__grow(queues, idx + 1);
228 if (err)
229 return err;
230 }
231
232 queue = &queues->queue_array[idx];
233
234 if (!queue->set) {
235 queue->set = true;
236 queue->tid = buffer->tid;
237 queue->cpu = buffer->cpu;
238 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
239 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
240 queue->cpu, queue->tid, buffer->cpu, buffer->tid);
241 return -EINVAL;
242 }
243
244 buffer->buffer_nr = queues->next_buffer_nr++;
245
246 list_add_tail(&buffer->list, &queue->head);
247
248 queues->new_data = true;
249 queues->populated = true;
250
251 return 0;
252}
253
254/* Limit buffers to 32MiB on 32-bit */
255#define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
256
257static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
258 unsigned int idx,
259 struct auxtrace_buffer *buffer)
260{
261 u64 sz = buffer->size;
262 bool consecutive = false;
263 struct auxtrace_buffer *b;
264 int err;
265
266 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
267 b = memdup(buffer, sizeof(struct auxtrace_buffer));
268 if (!b)
269 return -ENOMEM;
270 b->size = BUFFER_LIMIT_FOR_32_BIT;
271 b->consecutive = consecutive;
272 err = auxtrace_queues__add_buffer(queues, idx, b);
273 if (err) {
274 auxtrace_buffer__free(b);
275 return err;
276 }
277 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
278 sz -= BUFFER_LIMIT_FOR_32_BIT;
279 consecutive = true;
280 }
281
282 buffer->size = sz;
283 buffer->consecutive = consecutive;
284
285 return 0;
286}
287
288static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
289 struct perf_session *session,
290 unsigned int idx,
291 struct auxtrace_buffer *buffer)
292{
293 if (session->one_mmap) {
294 buffer->data = buffer->data_offset - session->one_mmap_offset +
295 session->one_mmap_addr;
296 } else if (perf_data_file__is_pipe(session->file)) {
297 buffer->data = auxtrace_copy_data(buffer->size, session);
298 if (!buffer->data)
299 return -ENOMEM;
300 buffer->data_needs_freeing = true;
301 } else if (BITS_PER_LONG == 32 &&
302 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
303 int err;
304
305 err = auxtrace_queues__split_buffer(queues, idx, buffer);
306 if (err)
307 return err;
308 }
309
310 return auxtrace_queues__add_buffer(queues, idx, buffer);
311}
312
313int auxtrace_queues__add_event(struct auxtrace_queues *queues,
314 struct perf_session *session,
315 union perf_event *event, off_t data_offset,
316 struct auxtrace_buffer **buffer_ptr)
317{
318 struct auxtrace_buffer *buffer;
319 unsigned int idx;
320 int err;
321
322 buffer = zalloc(sizeof(struct auxtrace_buffer));
323 if (!buffer)
324 return -ENOMEM;
325
326 buffer->pid = -1;
327 buffer->tid = event->auxtrace.tid;
328 buffer->cpu = event->auxtrace.cpu;
329 buffer->data_offset = data_offset;
330 buffer->offset = event->auxtrace.offset;
331 buffer->reference = event->auxtrace.reference;
332 buffer->size = event->auxtrace.size;
333 idx = event->auxtrace.idx;
334
335 err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
336 if (err)
337 goto out_err;
338
339 if (buffer_ptr)
340 *buffer_ptr = buffer;
341
342 return 0;
343
344out_err:
345 auxtrace_buffer__free(buffer);
346 return err;
347}
348
Adrian Hunter99fa2982015-04-30 17:37:25 +0300349static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
350 struct perf_session *session,
351 off_t file_offset, size_t sz)
352{
353 union perf_event *event;
354 int err;
355 char buf[PERF_SAMPLE_MAX_SIZE];
356
357 err = perf_session__peek_event(session, file_offset, buf,
358 PERF_SAMPLE_MAX_SIZE, &event, NULL);
359 if (err)
360 return err;
361
362 if (event->header.type == PERF_RECORD_AUXTRACE) {
363 if (event->header.size < sizeof(struct auxtrace_event) ||
364 event->header.size != sz) {
365 err = -EINVAL;
366 goto out;
367 }
368 file_offset += event->header.size;
369 err = auxtrace_queues__add_event(queues, session, event,
370 file_offset, NULL);
371 }
372out:
373 return err;
374}
375
Adrian Huntere5027892015-04-21 12:21:51 +0300376void auxtrace_queues__free(struct auxtrace_queues *queues)
377{
378 unsigned int i;
379
380 for (i = 0; i < queues->nr_queues; i++) {
381 while (!list_empty(&queues->queue_array[i].head)) {
382 struct auxtrace_buffer *buffer;
383
384 buffer = list_entry(queues->queue_array[i].head.next,
385 struct auxtrace_buffer, list);
386 list_del(&buffer->list);
387 auxtrace_buffer__free(buffer);
388 }
389 }
390
391 zfree(&queues->queue_array);
392 queues->nr_queues = 0;
393}
394
Adrian Hunterf9397152015-04-09 18:53:52 +0300395static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
396 unsigned int pos, unsigned int queue_nr,
397 u64 ordinal)
398{
399 unsigned int parent;
400
401 while (pos) {
402 parent = (pos - 1) >> 1;
403 if (heap_array[parent].ordinal <= ordinal)
404 break;
405 heap_array[pos] = heap_array[parent];
406 pos = parent;
407 }
408 heap_array[pos].queue_nr = queue_nr;
409 heap_array[pos].ordinal = ordinal;
410}
411
412int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
413 u64 ordinal)
414{
415 struct auxtrace_heap_item *heap_array;
416
417 if (queue_nr >= heap->heap_sz) {
418 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
419
420 while (heap_sz <= queue_nr)
421 heap_sz <<= 1;
422 heap_array = realloc(heap->heap_array,
423 heap_sz * sizeof(struct auxtrace_heap_item));
424 if (!heap_array)
425 return -ENOMEM;
426 heap->heap_array = heap_array;
427 heap->heap_sz = heap_sz;
428 }
429
430 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
431
432 return 0;
433}
434
435void auxtrace_heap__free(struct auxtrace_heap *heap)
436{
437 zfree(&heap->heap_array);
438 heap->heap_cnt = 0;
439 heap->heap_sz = 0;
440}
441
442void auxtrace_heap__pop(struct auxtrace_heap *heap)
443{
444 unsigned int pos, last, heap_cnt = heap->heap_cnt;
445 struct auxtrace_heap_item *heap_array;
446
447 if (!heap_cnt)
448 return;
449
450 heap->heap_cnt -= 1;
451
452 heap_array = heap->heap_array;
453
454 pos = 0;
455 while (1) {
456 unsigned int left, right;
457
458 left = (pos << 1) + 1;
459 if (left >= heap_cnt)
460 break;
461 right = left + 1;
462 if (right >= heap_cnt) {
463 heap_array[pos] = heap_array[left];
464 return;
465 }
466 if (heap_array[left].ordinal < heap_array[right].ordinal) {
467 heap_array[pos] = heap_array[left];
468 pos = left;
469 } else {
470 heap_array[pos] = heap_array[right];
471 pos = right;
472 }
473 }
474
475 last = heap_cnt - 1;
476 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
477 heap_array[last].ordinal);
478}
479
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300480size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr)
481{
482 if (itr)
483 return itr->info_priv_size(itr);
484 return 0;
485}
486
487static int auxtrace_not_supported(void)
488{
489 pr_err("AUX area tracing is not supported on this architecture\n");
490 return -EINVAL;
491}
492
493int auxtrace_record__info_fill(struct auxtrace_record *itr,
494 struct perf_session *session,
495 struct auxtrace_info_event *auxtrace_info,
496 size_t priv_size)
497{
498 if (itr)
499 return itr->info_fill(itr, session, auxtrace_info, priv_size);
500 return auxtrace_not_supported();
501}
502
503void auxtrace_record__free(struct auxtrace_record *itr)
504{
505 if (itr)
506 itr->free(itr);
507}
508
Adrian Hunterd20031b2015-04-30 17:37:31 +0300509int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
510{
511 if (itr && itr->snapshot_start)
512 return itr->snapshot_start(itr);
513 return 0;
514}
515
516int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
517{
518 if (itr && itr->snapshot_finish)
519 return itr->snapshot_finish(itr);
520 return 0;
521}
522
523int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
524 struct auxtrace_mmap *mm,
525 unsigned char *data, u64 *head, u64 *old)
526{
527 if (itr && itr->find_snapshot)
528 return itr->find_snapshot(itr, idx, mm, data, head, old);
529 return 0;
530}
531
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300532int auxtrace_record__options(struct auxtrace_record *itr,
533 struct perf_evlist *evlist,
534 struct record_opts *opts)
535{
536 if (itr)
537 return itr->recording_options(itr, evlist, opts);
538 return 0;
539}
540
541u64 auxtrace_record__reference(struct auxtrace_record *itr)
542{
543 if (itr)
544 return itr->reference(itr);
545 return 0;
546}
547
Adrian Hunterd20031b2015-04-30 17:37:31 +0300548int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
549 struct record_opts *opts, const char *str)
550{
551 if (!str)
552 return 0;
553
554 if (itr)
555 return itr->parse_snapshot_options(itr, opts, str);
556
557 pr_err("No AUX area tracing to snapshot\n");
558 return -EINVAL;
559}
560
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300561struct auxtrace_record *__weak
562auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
563{
564 *err = 0;
565 return NULL;
566}
567
Adrian Hunter99fa2982015-04-30 17:37:25 +0300568static int auxtrace_index__alloc(struct list_head *head)
569{
570 struct auxtrace_index *auxtrace_index;
571
572 auxtrace_index = malloc(sizeof(struct auxtrace_index));
573 if (!auxtrace_index)
574 return -ENOMEM;
575
576 auxtrace_index->nr = 0;
577 INIT_LIST_HEAD(&auxtrace_index->list);
578
579 list_add_tail(&auxtrace_index->list, head);
580
581 return 0;
582}
583
584void auxtrace_index__free(struct list_head *head)
585{
586 struct auxtrace_index *auxtrace_index, *n;
587
588 list_for_each_entry_safe(auxtrace_index, n, head, list) {
589 list_del(&auxtrace_index->list);
590 free(auxtrace_index);
591 }
592}
593
594static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
595{
596 struct auxtrace_index *auxtrace_index;
597 int err;
598
599 if (list_empty(head)) {
600 err = auxtrace_index__alloc(head);
601 if (err)
602 return NULL;
603 }
604
605 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
606
607 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
608 err = auxtrace_index__alloc(head);
609 if (err)
610 return NULL;
611 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
612 list);
613 }
614
615 return auxtrace_index;
616}
617
618int auxtrace_index__auxtrace_event(struct list_head *head,
619 union perf_event *event, off_t file_offset)
620{
621 struct auxtrace_index *auxtrace_index;
622 size_t nr;
623
624 auxtrace_index = auxtrace_index__last(head);
625 if (!auxtrace_index)
626 return -ENOMEM;
627
628 nr = auxtrace_index->nr;
629 auxtrace_index->entries[nr].file_offset = file_offset;
630 auxtrace_index->entries[nr].sz = event->header.size;
631 auxtrace_index->nr += 1;
632
633 return 0;
634}
635
636static int auxtrace_index__do_write(int fd,
637 struct auxtrace_index *auxtrace_index)
638{
639 struct auxtrace_index_entry ent;
640 size_t i;
641
642 for (i = 0; i < auxtrace_index->nr; i++) {
643 ent.file_offset = auxtrace_index->entries[i].file_offset;
644 ent.sz = auxtrace_index->entries[i].sz;
645 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
646 return -errno;
647 }
648 return 0;
649}
650
651int auxtrace_index__write(int fd, struct list_head *head)
652{
653 struct auxtrace_index *auxtrace_index;
654 u64 total = 0;
655 int err;
656
657 list_for_each_entry(auxtrace_index, head, list)
658 total += auxtrace_index->nr;
659
660 if (writen(fd, &total, sizeof(total)) != sizeof(total))
661 return -errno;
662
663 list_for_each_entry(auxtrace_index, head, list) {
664 err = auxtrace_index__do_write(fd, auxtrace_index);
665 if (err)
666 return err;
667 }
668
669 return 0;
670}
671
672static int auxtrace_index__process_entry(int fd, struct list_head *head,
673 bool needs_swap)
674{
675 struct auxtrace_index *auxtrace_index;
676 struct auxtrace_index_entry ent;
677 size_t nr;
678
679 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
680 return -1;
681
682 auxtrace_index = auxtrace_index__last(head);
683 if (!auxtrace_index)
684 return -1;
685
686 nr = auxtrace_index->nr;
687 if (needs_swap) {
688 auxtrace_index->entries[nr].file_offset =
689 bswap_64(ent.file_offset);
690 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
691 } else {
692 auxtrace_index->entries[nr].file_offset = ent.file_offset;
693 auxtrace_index->entries[nr].sz = ent.sz;
694 }
695
696 auxtrace_index->nr = nr + 1;
697
698 return 0;
699}
700
701int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
702 bool needs_swap)
703{
704 struct list_head *head = &session->auxtrace_index;
705 u64 nr;
706
707 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
708 return -1;
709
710 if (needs_swap)
711 nr = bswap_64(nr);
712
713 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
714 return -1;
715
716 while (nr--) {
717 int err;
718
719 err = auxtrace_index__process_entry(fd, head, needs_swap);
720 if (err)
721 return -1;
722 }
723
724 return 0;
725}
726
727static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
728 struct perf_session *session,
729 struct auxtrace_index_entry *ent)
730{
731 return auxtrace_queues__add_indexed_event(queues, session,
732 ent->file_offset, ent->sz);
733}
734
735int auxtrace_queues__process_index(struct auxtrace_queues *queues,
736 struct perf_session *session)
737{
738 struct auxtrace_index *auxtrace_index;
739 struct auxtrace_index_entry *ent;
740 size_t i;
741 int err;
742
743 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
744 for (i = 0; i < auxtrace_index->nr; i++) {
745 ent = &auxtrace_index->entries[i];
746 err = auxtrace_queues__process_index_entry(queues,
747 session,
748 ent);
749 if (err)
750 return err;
751 }
752 }
753 return 0;
754}
755
Adrian Huntere5027892015-04-21 12:21:51 +0300756struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
757 struct auxtrace_buffer *buffer)
758{
759 if (buffer) {
760 if (list_is_last(&buffer->list, &queue->head))
761 return NULL;
762 return list_entry(buffer->list.next, struct auxtrace_buffer,
763 list);
764 } else {
765 if (list_empty(&queue->head))
766 return NULL;
767 return list_entry(queue->head.next, struct auxtrace_buffer,
768 list);
769 }
770}
771
772void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
773{
774 size_t adj = buffer->data_offset & (page_size - 1);
775 size_t size = buffer->size + adj;
776 off_t file_offset = buffer->data_offset - adj;
777 void *addr;
778
779 if (buffer->data)
780 return buffer->data;
781
782 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
783 if (addr == MAP_FAILED)
784 return NULL;
785
786 buffer->mmap_addr = addr;
787 buffer->mmap_size = size;
788
789 buffer->data = addr + adj;
790
791 return buffer->data;
792}
793
794void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
795{
796 if (!buffer->data || !buffer->mmap_addr)
797 return;
798 munmap(buffer->mmap_addr, buffer->mmap_size);
799 buffer->mmap_addr = NULL;
800 buffer->mmap_size = 0;
801 buffer->data = NULL;
802 buffer->use_data = NULL;
803}
804
805void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
806{
807 auxtrace_buffer__put_data(buffer);
808 if (buffer->data_needs_freeing) {
809 buffer->data_needs_freeing = false;
810 zfree(&buffer->data);
811 buffer->use_data = NULL;
812 buffer->size = 0;
813 }
814}
815
816void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
817{
818 auxtrace_buffer__drop_data(buffer);
819 free(buffer);
820}
821
Adrian Hunter85ed4722015-04-09 18:53:50 +0300822void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
823 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
824 const char *msg)
825{
826 size_t size;
827
828 memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
829
830 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
831 auxtrace_error->type = type;
832 auxtrace_error->code = code;
833 auxtrace_error->cpu = cpu;
834 auxtrace_error->pid = pid;
835 auxtrace_error->tid = tid;
836 auxtrace_error->ip = ip;
837 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
838
839 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
840 strlen(auxtrace_error->msg) + 1;
841 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
842}
843
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300844int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
845 struct perf_tool *tool,
846 struct perf_session *session,
847 perf_event__handler_t process)
848{
849 union perf_event *ev;
850 size_t priv_size;
851 int err;
852
853 pr_debug2("Synthesizing auxtrace information\n");
854 priv_size = auxtrace_record__info_priv_size(itr);
855 ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
856 if (!ev)
857 return -ENOMEM;
858
859 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
860 ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
861 priv_size;
862 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
863 priv_size);
864 if (err)
865 goto out_free;
866
867 err = process(tool, ev, NULL, NULL);
868out_free:
869 free(ev);
870 return err;
871}
872
Adrian Hunter73f75fb2015-04-09 18:53:53 +0300873static bool auxtrace__dont_decode(struct perf_session *session)
874{
875 return !session->itrace_synth_opts ||
876 session->itrace_synth_opts->dont_decode;
877}
878
879int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
880 union perf_event *event,
Adrian Hunter5efb1d52015-07-17 19:33:42 +0300881 struct perf_session *session)
Adrian Hunter73f75fb2015-04-09 18:53:53 +0300882{
883 enum auxtrace_type type = event->auxtrace_info.type;
884
885 if (dump_trace)
886 fprintf(stdout, " type: %u\n", type);
887
888 switch (type) {
Adrian Hunter55ea4ab2015-07-17 19:33:36 +0300889 case PERF_AUXTRACE_INTEL_PT:
Adrian Hunter5efb1d52015-07-17 19:33:42 +0300890 return intel_pt_process_auxtrace_info(event, session);
Adrian Hunter73f75fb2015-04-09 18:53:53 +0300891 case PERF_AUXTRACE_UNKNOWN:
892 default:
893 return -EINVAL;
894 }
895}
896
897s64 perf_event__process_auxtrace(struct perf_tool *tool,
898 union perf_event *event,
899 struct perf_session *session)
900{
901 s64 err;
902
903 if (dump_trace)
904 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
905 event->auxtrace.size, event->auxtrace.offset,
906 event->auxtrace.reference, event->auxtrace.idx,
907 event->auxtrace.tid, event->auxtrace.cpu);
908
909 if (auxtrace__dont_decode(session))
910 return event->auxtrace.size;
911
912 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
913 return -EINVAL;
914
915 err = session->auxtrace->process_auxtrace_event(session, event, tool);
916 if (err < 0)
917 return err;
918
919 return event->auxtrace.size;
920}
921
Adrian Hunterf6986c952015-04-09 18:53:49 +0300922#define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
923#define PERF_ITRACE_DEFAULT_PERIOD 100000
924#define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
925#define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
926
927void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
928{
929 synth_opts->instructions = true;
930 synth_opts->branches = true;
Adrian Hunter53c76b02015-04-30 17:37:28 +0300931 synth_opts->transactions = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +0300932 synth_opts->errors = true;
933 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
934 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
935 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
936}
937
938/*
939 * Please check tools/perf/Documentation/perf-script.txt for information
940 * about the options parsed here, which is introduced after this cset,
941 * when support in 'perf script' for these options is introduced.
942 */
943int itrace_parse_synth_opts(const struct option *opt, const char *str,
944 int unset)
945{
946 struct itrace_synth_opts *synth_opts = opt->value;
947 const char *p;
948 char *endptr;
Adrian Hunterf70cfa02015-07-17 19:33:46 +0300949 bool period_type_set = false;
Adrian Hunterf6986c952015-04-09 18:53:49 +0300950
951 synth_opts->set = true;
952
953 if (unset) {
954 synth_opts->dont_decode = true;
955 return 0;
956 }
957
958 if (!str) {
959 itrace_synth_opts__set_default(synth_opts);
960 return 0;
961 }
962
963 for (p = str; *p;) {
964 switch (*p++) {
965 case 'i':
966 synth_opts->instructions = true;
967 while (*p == ' ' || *p == ',')
968 p += 1;
969 if (isdigit(*p)) {
970 synth_opts->period = strtoull(p, &endptr, 10);
971 p = endptr;
972 while (*p == ' ' || *p == ',')
973 p += 1;
974 switch (*p++) {
975 case 'i':
976 synth_opts->period_type =
977 PERF_ITRACE_PERIOD_INSTRUCTIONS;
Adrian Hunterf70cfa02015-07-17 19:33:46 +0300978 period_type_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +0300979 break;
980 case 't':
981 synth_opts->period_type =
982 PERF_ITRACE_PERIOD_TICKS;
Adrian Hunterf70cfa02015-07-17 19:33:46 +0300983 period_type_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +0300984 break;
985 case 'm':
986 synth_opts->period *= 1000;
987 /* Fall through */
988 case 'u':
989 synth_opts->period *= 1000;
990 /* Fall through */
991 case 'n':
992 if (*p++ != 's')
993 goto out_err;
994 synth_opts->period_type =
995 PERF_ITRACE_PERIOD_NANOSECS;
Adrian Hunterf70cfa02015-07-17 19:33:46 +0300996 period_type_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +0300997 break;
998 case '\0':
999 goto out;
1000 default:
1001 goto out_err;
1002 }
1003 }
1004 break;
1005 case 'b':
1006 synth_opts->branches = true;
1007 break;
Adrian Hunter53c76b02015-04-30 17:37:28 +03001008 case 'x':
1009 synth_opts->transactions = true;
1010 break;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001011 case 'e':
1012 synth_opts->errors = true;
1013 break;
1014 case 'd':
1015 synth_opts->log = true;
1016 break;
1017 case 'c':
1018 synth_opts->branches = true;
1019 synth_opts->calls = true;
1020 break;
1021 case 'r':
1022 synth_opts->branches = true;
1023 synth_opts->returns = true;
1024 break;
1025 case 'g':
Adrian Hunterf6986c952015-04-09 18:53:49 +03001026 synth_opts->callchain = true;
1027 synth_opts->callchain_sz =
1028 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1029 while (*p == ' ' || *p == ',')
1030 p += 1;
1031 if (isdigit(*p)) {
1032 unsigned int val;
1033
1034 val = strtoul(p, &endptr, 10);
1035 p = endptr;
1036 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1037 goto out_err;
1038 synth_opts->callchain_sz = val;
1039 }
1040 break;
1041 case ' ':
1042 case ',':
1043 break;
1044 default:
1045 goto out_err;
1046 }
1047 }
1048out:
1049 if (synth_opts->instructions) {
Adrian Hunterf70cfa02015-07-17 19:33:46 +03001050 if (!period_type_set)
Adrian Hunterf6986c952015-04-09 18:53:49 +03001051 synth_opts->period_type =
1052 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1053 if (!synth_opts->period)
1054 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1055 }
1056
1057 return 0;
1058
1059out_err:
1060 pr_err("Bad Instruction Tracing options '%s'\n", str);
1061 return -EINVAL;
1062}
1063
Adrian Hunter85ed4722015-04-09 18:53:50 +03001064static const char * const auxtrace_error_type_name[] = {
1065 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1066};
1067
1068static const char *auxtrace_error_name(int type)
1069{
1070 const char *error_type_name = NULL;
1071
1072 if (type < PERF_AUXTRACE_ERROR_MAX)
1073 error_type_name = auxtrace_error_type_name[type];
1074 if (!error_type_name)
1075 error_type_name = "unknown AUX";
1076 return error_type_name;
1077}
1078
1079size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1080{
1081 struct auxtrace_error_event *e = &event->auxtrace_error;
1082 int ret;
1083
1084 ret = fprintf(fp, " %s error type %u",
1085 auxtrace_error_name(e->type), e->type);
1086 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1087 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1088 return ret;
1089}
1090
1091void perf_session__auxtrace_error_inc(struct perf_session *session,
1092 union perf_event *event)
1093{
1094 struct auxtrace_error_event *e = &event->auxtrace_error;
1095
1096 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1097 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1098}
1099
1100void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1101{
1102 int i;
1103
1104 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1105 if (!stats->nr_auxtrace_errors[i])
1106 continue;
1107 ui__warning("%u %s errors\n",
1108 stats->nr_auxtrace_errors[i],
1109 auxtrace_error_name(i));
1110 }
1111}
1112
1113int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1114 union perf_event *event,
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001115 struct perf_session *session)
Adrian Hunter85ed4722015-04-09 18:53:50 +03001116{
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001117 if (auxtrace__dont_decode(session))
1118 return 0;
1119
Adrian Hunter85ed4722015-04-09 18:53:50 +03001120 perf_event__fprintf_auxtrace_error(event, stdout);
1121 return 0;
1122}
1123
Adrian Hunterd20031b2015-04-30 17:37:31 +03001124static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1125 struct auxtrace_record *itr,
1126 struct perf_tool *tool, process_auxtrace_t fn,
1127 bool snapshot, size_t snapshot_size)
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001128{
Adrian Hunterd20031b2015-04-30 17:37:31 +03001129 u64 head, old = mm->prev, offset, ref;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001130 unsigned char *data = mm->base;
1131 size_t size, head_off, old_off, len1, len2, padding;
1132 union perf_event ev;
1133 void *data1, *data2;
1134
Adrian Hunterd20031b2015-04-30 17:37:31 +03001135 if (snapshot) {
1136 head = auxtrace_mmap__read_snapshot_head(mm);
1137 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1138 &head, &old))
1139 return -1;
1140 } else {
1141 head = auxtrace_mmap__read_head(mm);
1142 }
1143
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001144 if (old == head)
1145 return 0;
1146
1147 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1148 mm->idx, old, head, head - old);
1149
1150 if (mm->mask) {
1151 head_off = head & mm->mask;
1152 old_off = old & mm->mask;
1153 } else {
1154 head_off = head % mm->len;
1155 old_off = old % mm->len;
1156 }
1157
1158 if (head_off > old_off)
1159 size = head_off - old_off;
1160 else
1161 size = mm->len - (old_off - head_off);
1162
Adrian Hunterd20031b2015-04-30 17:37:31 +03001163 if (snapshot && size > snapshot_size)
1164 size = snapshot_size;
1165
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001166 ref = auxtrace_record__reference(itr);
1167
1168 if (head > old || size <= head || mm->mask) {
1169 offset = head - size;
1170 } else {
1171 /*
1172 * When the buffer size is not a power of 2, 'head' wraps at the
1173 * highest multiple of the buffer size, so we have to subtract
1174 * the remainder here.
1175 */
1176 u64 rem = (0ULL - mm->len) % mm->len;
1177
1178 offset = head - size - rem;
1179 }
1180
1181 if (size > head_off) {
1182 len1 = size - head_off;
1183 data1 = &data[mm->len - len1];
1184 len2 = head_off;
1185 data2 = &data[0];
1186 } else {
1187 len1 = size;
1188 data1 = &data[head_off - len1];
1189 len2 = 0;
1190 data2 = NULL;
1191 }
1192
Adrian Hunter83b2ea22015-05-29 16:33:38 +03001193 if (itr->alignment) {
1194 unsigned int unwanted = len1 % itr->alignment;
1195
1196 len1 -= unwanted;
1197 size -= unwanted;
1198 }
1199
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001200 /* padding must be written by fn() e.g. record__process_auxtrace() */
1201 padding = size & 7;
1202 if (padding)
1203 padding = 8 - padding;
1204
1205 memset(&ev, 0, sizeof(ev));
1206 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1207 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1208 ev.auxtrace.size = size + padding;
1209 ev.auxtrace.offset = offset;
1210 ev.auxtrace.reference = ref;
1211 ev.auxtrace.idx = mm->idx;
1212 ev.auxtrace.tid = mm->tid;
1213 ev.auxtrace.cpu = mm->cpu;
1214
1215 if (fn(tool, &ev, data1, len1, data2, len2))
1216 return -1;
1217
1218 mm->prev = head;
1219
Adrian Hunterd20031b2015-04-30 17:37:31 +03001220 if (!snapshot) {
1221 auxtrace_mmap__write_tail(mm, head);
1222 if (itr->read_finish) {
1223 int err;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001224
Adrian Hunterd20031b2015-04-30 17:37:31 +03001225 err = itr->read_finish(itr, mm->idx);
1226 if (err < 0)
1227 return err;
1228 }
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001229 }
1230
1231 return 1;
1232}
Adrian Hunterc3278f02015-04-09 18:53:54 +03001233
Adrian Hunterd20031b2015-04-30 17:37:31 +03001234int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1235 struct perf_tool *tool, process_auxtrace_t fn)
1236{
1237 return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1238}
1239
1240int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1241 struct auxtrace_record *itr,
1242 struct perf_tool *tool, process_auxtrace_t fn,
1243 size_t snapshot_size)
1244{
1245 return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1246}
1247
Adrian Hunterc3278f02015-04-09 18:53:54 +03001248/**
1249 * struct auxtrace_cache - hash table to implement a cache
1250 * @hashtable: the hashtable
1251 * @sz: hashtable size (number of hlists)
1252 * @entry_size: size of an entry
1253 * @limit: limit the number of entries to this maximum, when reached the cache
1254 * is dropped and caching begins again with an empty cache
1255 * @cnt: current number of entries
1256 * @bits: hashtable size (@sz = 2^@bits)
1257 */
1258struct auxtrace_cache {
1259 struct hlist_head *hashtable;
1260 size_t sz;
1261 size_t entry_size;
1262 size_t limit;
1263 size_t cnt;
1264 unsigned int bits;
1265};
1266
1267struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1268 unsigned int limit_percent)
1269{
1270 struct auxtrace_cache *c;
1271 struct hlist_head *ht;
1272 size_t sz, i;
1273
1274 c = zalloc(sizeof(struct auxtrace_cache));
1275 if (!c)
1276 return NULL;
1277
1278 sz = 1UL << bits;
1279
1280 ht = calloc(sz, sizeof(struct hlist_head));
1281 if (!ht)
1282 goto out_free;
1283
1284 for (i = 0; i < sz; i++)
1285 INIT_HLIST_HEAD(&ht[i]);
1286
1287 c->hashtable = ht;
1288 c->sz = sz;
1289 c->entry_size = entry_size;
1290 c->limit = (c->sz * limit_percent) / 100;
1291 c->bits = bits;
1292
1293 return c;
1294
1295out_free:
1296 free(c);
1297 return NULL;
1298}
1299
1300static void auxtrace_cache__drop(struct auxtrace_cache *c)
1301{
1302 struct auxtrace_cache_entry *entry;
1303 struct hlist_node *tmp;
1304 size_t i;
1305
1306 if (!c)
1307 return;
1308
1309 for (i = 0; i < c->sz; i++) {
1310 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1311 hlist_del(&entry->hash);
1312 auxtrace_cache__free_entry(c, entry);
1313 }
1314 }
1315
1316 c->cnt = 0;
1317}
1318
1319void auxtrace_cache__free(struct auxtrace_cache *c)
1320{
1321 if (!c)
1322 return;
1323
1324 auxtrace_cache__drop(c);
1325 free(c->hashtable);
1326 free(c);
1327}
1328
1329void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1330{
1331 return malloc(c->entry_size);
1332}
1333
1334void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1335 void *entry)
1336{
1337 free(entry);
1338}
1339
1340int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1341 struct auxtrace_cache_entry *entry)
1342{
1343 if (c->limit && ++c->cnt > c->limit)
1344 auxtrace_cache__drop(c);
1345
1346 entry->key = key;
1347 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1348
1349 return 0;
1350}
1351
1352void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1353{
1354 struct auxtrace_cache_entry *entry;
1355 struct hlist_head *hlist;
1356
1357 if (!c)
1358 return NULL;
1359
1360 hlist = &c->hashtable[hash_32(key, c->bits)];
1361 hlist_for_each_entry(entry, hlist, hash) {
1362 if (entry->key == key)
1363 return entry;
1364 }
1365
1366 return NULL;
1367}