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