2 * auxtrace.c: AUX area trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
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.
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
16 #include <sys/types.h>
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>
25 #include <linux/string.h>
27 #include <sys/param.h>
33 #include <linux/list.h>
39 #include "thread_map.h"
43 #include <linux/hash.h>
48 #include "parse-options.h"
50 int auxtrace_mmap__mmap(struct auxtrace_mmap
*mm
,
51 struct auxtrace_mmap_params
*mp
,
54 struct perf_event_mmap_page
*pc
= userpg
;
56 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
57 pr_err("Cannot use AUX area tracing mmaps\n");
61 WARN_ONCE(mm
->base
, "Uninitialized auxtrace_mmap\n");
76 pc
->aux_offset
= mp
->offset
;
77 pc
->aux_size
= mp
->len
;
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");
89 void auxtrace_mmap__munmap(struct auxtrace_mmap
*mm
)
92 munmap(mm
->base
, mm
->len
);
97 void auxtrace_mmap_params__init(struct auxtrace_mmap_params
*mp
,
98 off_t auxtrace_offset
,
99 unsigned int auxtrace_pages
,
100 bool auxtrace_overwrite
)
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
);
113 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params
*mp
,
114 struct perf_evlist
*evlist
, int idx
,
120 mp
->cpu
= evlist
->cpus
->map
[idx
];
122 mp
->tid
= thread_map__pid(evlist
->threads
, 0);
127 mp
->tid
= thread_map__pid(evlist
->threads
, idx
);
131 #define AUXTRACE_INIT_NR_QUEUES 32
133 static struct auxtrace_queue
*auxtrace_alloc_queue_array(unsigned int nr_queues
)
135 struct auxtrace_queue
*queue_array
;
136 unsigned int max_nr_queues
, i
;
138 max_nr_queues
= UINT_MAX
/ sizeof(struct auxtrace_queue
);
139 if (nr_queues
> max_nr_queues
)
142 queue_array
= calloc(nr_queues
, sizeof(struct auxtrace_queue
));
146 for (i
= 0; i
< nr_queues
; i
++) {
147 INIT_LIST_HEAD(&queue_array
[i
].head
);
148 queue_array
[i
].priv
= NULL
;
154 int auxtrace_queues__init(struct auxtrace_queues
*queues
)
156 queues
->nr_queues
= AUXTRACE_INIT_NR_QUEUES
;
157 queues
->queue_array
= auxtrace_alloc_queue_array(queues
->nr_queues
);
158 if (!queues
->queue_array
)
163 static int auxtrace_queues__grow(struct auxtrace_queues
*queues
,
164 unsigned int new_nr_queues
)
166 unsigned int nr_queues
= queues
->nr_queues
;
167 struct auxtrace_queue
*queue_array
;
171 nr_queues
= AUXTRACE_INIT_NR_QUEUES
;
173 while (nr_queues
&& nr_queues
< new_nr_queues
)
176 if (nr_queues
< queues
->nr_queues
|| nr_queues
< new_nr_queues
)
179 queue_array
= auxtrace_alloc_queue_array(nr_queues
);
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
;
189 queues
->nr_queues
= nr_queues
;
190 queues
->queue_array
= queue_array
;
195 static void *auxtrace_copy_data(u64 size
, struct perf_session
*session
)
197 int fd
= perf_data_file__fd(session
->file
);
201 if (size
> SSIZE_MAX
)
208 ret
= readn(fd
, p
, size
);
209 if (ret
!= (ssize_t
)size
) {
217 static int auxtrace_queues__add_buffer(struct auxtrace_queues
*queues
,
219 struct auxtrace_buffer
*buffer
)
221 struct auxtrace_queue
*queue
;
224 if (idx
>= queues
->nr_queues
) {
225 err
= auxtrace_queues__grow(queues
, idx
+ 1);
230 queue
= &queues
->queue_array
[idx
];
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
);
242 buffer
->buffer_nr
= queues
->next_buffer_nr
++;
244 list_add_tail(&buffer
->list
, &queue
->head
);
246 queues
->new_data
= true;
247 queues
->populated
= true;
252 /* Limit buffers to 32MiB on 32-bit */
253 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
255 static int auxtrace_queues__split_buffer(struct auxtrace_queues
*queues
,
257 struct auxtrace_buffer
*buffer
)
259 u64 sz
= buffer
->size
;
260 bool consecutive
= false;
261 struct auxtrace_buffer
*b
;
264 while (sz
> BUFFER_LIMIT_FOR_32_BIT
) {
265 b
= memdup(buffer
, sizeof(struct auxtrace_buffer
));
268 b
->size
= BUFFER_LIMIT_FOR_32_BIT
;
269 b
->consecutive
= consecutive
;
270 err
= auxtrace_queues__add_buffer(queues
, idx
, b
);
272 auxtrace_buffer__free(b
);
275 buffer
->data_offset
+= BUFFER_LIMIT_FOR_32_BIT
;
276 sz
-= BUFFER_LIMIT_FOR_32_BIT
;
281 buffer
->consecutive
= consecutive
;
286 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues
*queues
,
287 struct perf_session
*session
,
289 struct auxtrace_buffer
*buffer
)
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
);
298 buffer
->data_needs_freeing
= true;
299 } else if (BITS_PER_LONG
== 32 &&
300 buffer
->size
> BUFFER_LIMIT_FOR_32_BIT
) {
303 err
= auxtrace_queues__split_buffer(queues
, idx
, buffer
);
308 return auxtrace_queues__add_buffer(queues
, idx
, buffer
);
311 int 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
)
316 struct auxtrace_buffer
*buffer
;
320 buffer
= zalloc(sizeof(struct auxtrace_buffer
));
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
;
333 err
= auxtrace_queues__add_event_buffer(queues
, session
, idx
, buffer
);
338 *buffer_ptr
= buffer
;
343 auxtrace_buffer__free(buffer
);
347 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues
*queues
,
348 struct perf_session
*session
,
349 off_t file_offset
, size_t sz
)
351 union perf_event
*event
;
353 char buf
[PERF_SAMPLE_MAX_SIZE
];
355 err
= perf_session__peek_event(session
, file_offset
, buf
,
356 PERF_SAMPLE_MAX_SIZE
, &event
, NULL
);
360 if (event
->header
.type
== PERF_RECORD_AUXTRACE
) {
361 if (event
->header
.size
< sizeof(struct auxtrace_event
) ||
362 event
->header
.size
!= sz
) {
366 file_offset
+= event
->header
.size
;
367 err
= auxtrace_queues__add_event(queues
, session
, event
,
374 void auxtrace_queues__free(struct auxtrace_queues
*queues
)
378 for (i
= 0; i
< queues
->nr_queues
; i
++) {
379 while (!list_empty(&queues
->queue_array
[i
].head
)) {
380 struct auxtrace_buffer
*buffer
;
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
);
389 zfree(&queues
->queue_array
);
390 queues
->nr_queues
= 0;
393 static void auxtrace_heapify(struct auxtrace_heap_item
*heap_array
,
394 unsigned int pos
, unsigned int queue_nr
,
400 parent
= (pos
- 1) >> 1;
401 if (heap_array
[parent
].ordinal
<= ordinal
)
403 heap_array
[pos
] = heap_array
[parent
];
406 heap_array
[pos
].queue_nr
= queue_nr
;
407 heap_array
[pos
].ordinal
= ordinal
;
410 int auxtrace_heap__add(struct auxtrace_heap
*heap
, unsigned int queue_nr
,
413 struct auxtrace_heap_item
*heap_array
;
415 if (queue_nr
>= heap
->heap_sz
) {
416 unsigned int heap_sz
= AUXTRACE_INIT_NR_QUEUES
;
418 while (heap_sz
<= queue_nr
)
420 heap_array
= realloc(heap
->heap_array
,
421 heap_sz
* sizeof(struct auxtrace_heap_item
));
424 heap
->heap_array
= heap_array
;
425 heap
->heap_sz
= heap_sz
;
428 auxtrace_heapify(heap
->heap_array
, heap
->heap_cnt
++, queue_nr
, ordinal
);
433 void auxtrace_heap__free(struct auxtrace_heap
*heap
)
435 zfree(&heap
->heap_array
);
440 void auxtrace_heap__pop(struct auxtrace_heap
*heap
)
442 unsigned int pos
, last
, heap_cnt
= heap
->heap_cnt
;
443 struct auxtrace_heap_item
*heap_array
;
450 heap_array
= heap
->heap_array
;
454 unsigned int left
, right
;
456 left
= (pos
<< 1) + 1;
457 if (left
>= heap_cnt
)
460 if (right
>= heap_cnt
) {
461 heap_array
[pos
] = heap_array
[left
];
464 if (heap_array
[left
].ordinal
< heap_array
[right
].ordinal
) {
465 heap_array
[pos
] = heap_array
[left
];
468 heap_array
[pos
] = heap_array
[right
];
474 auxtrace_heapify(heap_array
, pos
, heap_array
[last
].queue_nr
,
475 heap_array
[last
].ordinal
);
478 size_t auxtrace_record__info_priv_size(struct auxtrace_record
*itr
)
481 return itr
->info_priv_size(itr
);
485 static int auxtrace_not_supported(void)
487 pr_err("AUX area tracing is not supported on this architecture\n");
491 int auxtrace_record__info_fill(struct auxtrace_record
*itr
,
492 struct perf_session
*session
,
493 struct auxtrace_info_event
*auxtrace_info
,
497 return itr
->info_fill(itr
, session
, auxtrace_info
, priv_size
);
498 return auxtrace_not_supported();
501 void auxtrace_record__free(struct auxtrace_record
*itr
)
507 int auxtrace_record__snapshot_start(struct auxtrace_record
*itr
)
509 if (itr
&& itr
->snapshot_start
)
510 return itr
->snapshot_start(itr
);
514 int auxtrace_record__snapshot_finish(struct auxtrace_record
*itr
)
516 if (itr
&& itr
->snapshot_finish
)
517 return itr
->snapshot_finish(itr
);
521 int auxtrace_record__find_snapshot(struct auxtrace_record
*itr
, int idx
,
522 struct auxtrace_mmap
*mm
,
523 unsigned char *data
, u64
*head
, u64
*old
)
525 if (itr
&& itr
->find_snapshot
)
526 return itr
->find_snapshot(itr
, idx
, mm
, data
, head
, old
);
530 int auxtrace_record__options(struct auxtrace_record
*itr
,
531 struct perf_evlist
*evlist
,
532 struct record_opts
*opts
)
535 return itr
->recording_options(itr
, evlist
, opts
);
539 u64
auxtrace_record__reference(struct auxtrace_record
*itr
)
542 return itr
->reference(itr
);
546 int auxtrace_parse_snapshot_options(struct auxtrace_record
*itr
,
547 struct record_opts
*opts
, const char *str
)
553 return itr
->parse_snapshot_options(itr
, opts
, str
);
555 pr_err("No AUX area tracing to snapshot\n");
559 struct auxtrace_record
*__weak
560 auxtrace_record__init(struct perf_evlist
*evlist __maybe_unused
, int *err
)
566 static int auxtrace_index__alloc(struct list_head
*head
)
568 struct auxtrace_index
*auxtrace_index
;
570 auxtrace_index
= malloc(sizeof(struct auxtrace_index
));
574 auxtrace_index
->nr
= 0;
575 INIT_LIST_HEAD(&auxtrace_index
->list
);
577 list_add_tail(&auxtrace_index
->list
, head
);
582 void auxtrace_index__free(struct list_head
*head
)
584 struct auxtrace_index
*auxtrace_index
, *n
;
586 list_for_each_entry_safe(auxtrace_index
, n
, head
, list
) {
587 list_del(&auxtrace_index
->list
);
588 free(auxtrace_index
);
592 static struct auxtrace_index
*auxtrace_index__last(struct list_head
*head
)
594 struct auxtrace_index
*auxtrace_index
;
597 if (list_empty(head
)) {
598 err
= auxtrace_index__alloc(head
);
603 auxtrace_index
= list_entry(head
->prev
, struct auxtrace_index
, list
);
605 if (auxtrace_index
->nr
>= PERF_AUXTRACE_INDEX_ENTRY_COUNT
) {
606 err
= auxtrace_index__alloc(head
);
609 auxtrace_index
= list_entry(head
->prev
, struct auxtrace_index
,
613 return auxtrace_index
;
616 int auxtrace_index__auxtrace_event(struct list_head
*head
,
617 union perf_event
*event
, off_t file_offset
)
619 struct auxtrace_index
*auxtrace_index
;
622 auxtrace_index
= auxtrace_index__last(head
);
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;
634 static int auxtrace_index__do_write(int fd
,
635 struct auxtrace_index
*auxtrace_index
)
637 struct auxtrace_index_entry ent
;
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
))
649 int auxtrace_index__write(int fd
, struct list_head
*head
)
651 struct auxtrace_index
*auxtrace_index
;
655 list_for_each_entry(auxtrace_index
, head
, list
)
656 total
+= auxtrace_index
->nr
;
658 if (writen(fd
, &total
, sizeof(total
)) != sizeof(total
))
661 list_for_each_entry(auxtrace_index
, head
, list
) {
662 err
= auxtrace_index__do_write(fd
, auxtrace_index
);
670 static int auxtrace_index__process_entry(int fd
, struct list_head
*head
,
673 struct auxtrace_index
*auxtrace_index
;
674 struct auxtrace_index_entry ent
;
677 if (readn(fd
, &ent
, sizeof(ent
)) != sizeof(ent
))
680 auxtrace_index
= auxtrace_index__last(head
);
684 nr
= auxtrace_index
->nr
;
686 auxtrace_index
->entries
[nr
].file_offset
=
687 bswap_64(ent
.file_offset
);
688 auxtrace_index
->entries
[nr
].sz
= bswap_64(ent
.sz
);
690 auxtrace_index
->entries
[nr
].file_offset
= ent
.file_offset
;
691 auxtrace_index
->entries
[nr
].sz
= ent
.sz
;
694 auxtrace_index
->nr
= nr
+ 1;
699 int auxtrace_index__process(int fd
, u64 size
, struct perf_session
*session
,
702 struct list_head
*head
= &session
->auxtrace_index
;
705 if (readn(fd
, &nr
, sizeof(u64
)) != sizeof(u64
))
711 if (sizeof(u64
) + nr
* sizeof(struct auxtrace_index_entry
) > size
)
717 err
= auxtrace_index__process_entry(fd
, head
, needs_swap
);
725 static int auxtrace_queues__process_index_entry(struct auxtrace_queues
*queues
,
726 struct perf_session
*session
,
727 struct auxtrace_index_entry
*ent
)
729 return auxtrace_queues__add_indexed_event(queues
, session
,
730 ent
->file_offset
, ent
->sz
);
733 int auxtrace_queues__process_index(struct auxtrace_queues
*queues
,
734 struct perf_session
*session
)
736 struct auxtrace_index
*auxtrace_index
;
737 struct auxtrace_index_entry
*ent
;
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
,
754 struct auxtrace_buffer
*auxtrace_buffer__next(struct auxtrace_queue
*queue
,
755 struct auxtrace_buffer
*buffer
)
758 if (list_is_last(&buffer
->list
, &queue
->head
))
760 return list_entry(buffer
->list
.next
, struct auxtrace_buffer
,
763 if (list_empty(&queue
->head
))
765 return list_entry(queue
->head
.next
, struct auxtrace_buffer
,
770 void *auxtrace_buffer__get_data(struct auxtrace_buffer
*buffer
, int fd
)
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
;
780 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, file_offset
);
781 if (addr
== MAP_FAILED
)
784 buffer
->mmap_addr
= addr
;
785 buffer
->mmap_size
= size
;
787 buffer
->data
= addr
+ adj
;
792 void auxtrace_buffer__put_data(struct auxtrace_buffer
*buffer
)
794 if (!buffer
->data
|| !buffer
->mmap_addr
)
796 munmap(buffer
->mmap_addr
, buffer
->mmap_size
);
797 buffer
->mmap_addr
= NULL
;
798 buffer
->mmap_size
= 0;
800 buffer
->use_data
= NULL
;
803 void auxtrace_buffer__drop_data(struct auxtrace_buffer
*buffer
)
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
;
814 void auxtrace_buffer__free(struct auxtrace_buffer
*buffer
)
816 auxtrace_buffer__drop_data(buffer
);
820 void auxtrace_synth_error(struct auxtrace_error_event
*auxtrace_error
, int type
,
821 int code
, int cpu
, pid_t pid
, pid_t tid
, u64 ip
,
826 memset(auxtrace_error
, 0, sizeof(struct auxtrace_error_event
));
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
);
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
));
842 int 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
)
847 union perf_event
*ev
;
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
);
857 ev
->auxtrace_info
.header
.type
= PERF_RECORD_AUXTRACE_INFO
;
858 ev
->auxtrace_info
.header
.size
= sizeof(struct auxtrace_info_event
) +
860 err
= auxtrace_record__info_fill(itr
, session
, &ev
->auxtrace_info
,
865 err
= process(tool
, ev
, NULL
, NULL
);
871 static bool auxtrace__dont_decode(struct perf_session
*session
)
873 return !session
->itrace_synth_opts
||
874 session
->itrace_synth_opts
->dont_decode
;
877 int perf_event__process_auxtrace_info(struct perf_tool
*tool __maybe_unused
,
878 union perf_event
*event
,
879 struct perf_session
*session __maybe_unused
)
881 enum auxtrace_type type
= event
->auxtrace_info
.type
;
884 fprintf(stdout
, " type: %u\n", type
);
887 case PERF_AUXTRACE_UNKNOWN
:
893 s64
perf_event__process_auxtrace(struct perf_tool
*tool
,
894 union perf_event
*event
,
895 struct perf_session
*session
)
900 fprintf(stdout
, " size: %#"PRIx64
" offset: %#"PRIx64
" ref: %#"PRIx64
" idx: %u tid: %d cpu: %d\n",
901 event
->auxtrace
.size
, event
->auxtrace
.offset
,
902 event
->auxtrace
.reference
, event
->auxtrace
.idx
,
903 event
->auxtrace
.tid
, event
->auxtrace
.cpu
);
905 if (auxtrace__dont_decode(session
))
906 return event
->auxtrace
.size
;
908 if (!session
->auxtrace
|| event
->header
.type
!= PERF_RECORD_AUXTRACE
)
911 err
= session
->auxtrace
->process_auxtrace_event(session
, event
, tool
);
915 return event
->auxtrace
.size
;
918 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
919 #define PERF_ITRACE_DEFAULT_PERIOD 100000
920 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
921 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
923 void itrace_synth_opts__set_default(struct itrace_synth_opts
*synth_opts
)
925 synth_opts
->instructions
= true;
926 synth_opts
->branches
= true;
927 synth_opts
->transactions
= true;
928 synth_opts
->errors
= true;
929 synth_opts
->period_type
= PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
930 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
931 synth_opts
->callchain_sz
= PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
935 * Please check tools/perf/Documentation/perf-script.txt for information
936 * about the options parsed here, which is introduced after this cset,
937 * when support in 'perf script' for these options is introduced.
939 int itrace_parse_synth_opts(const struct option
*opt
, const char *str
,
942 struct itrace_synth_opts
*synth_opts
= opt
->value
;
946 synth_opts
->set
= true;
949 synth_opts
->dont_decode
= true;
954 itrace_synth_opts__set_default(synth_opts
);
961 synth_opts
->instructions
= true;
962 while (*p
== ' ' || *p
== ',')
965 synth_opts
->period
= strtoull(p
, &endptr
, 10);
967 while (*p
== ' ' || *p
== ',')
971 synth_opts
->period_type
=
972 PERF_ITRACE_PERIOD_INSTRUCTIONS
;
975 synth_opts
->period_type
=
976 PERF_ITRACE_PERIOD_TICKS
;
979 synth_opts
->period
*= 1000;
982 synth_opts
->period
*= 1000;
987 synth_opts
->period_type
=
988 PERF_ITRACE_PERIOD_NANOSECS
;
998 synth_opts
->branches
= true;
1001 synth_opts
->transactions
= true;
1004 synth_opts
->errors
= true;
1007 synth_opts
->log
= true;
1010 synth_opts
->branches
= true;
1011 synth_opts
->calls
= true;
1014 synth_opts
->branches
= true;
1015 synth_opts
->returns
= true;
1018 synth_opts
->callchain
= true;
1019 synth_opts
->callchain_sz
=
1020 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
1021 while (*p
== ' ' || *p
== ',')
1026 val
= strtoul(p
, &endptr
, 10);
1028 if (!val
|| val
> PERF_ITRACE_MAX_CALLCHAIN_SZ
)
1030 synth_opts
->callchain_sz
= val
;
1041 if (synth_opts
->instructions
) {
1042 if (!synth_opts
->period_type
)
1043 synth_opts
->period_type
=
1044 PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
1045 if (!synth_opts
->period
)
1046 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
1052 pr_err("Bad Instruction Tracing options '%s'\n", str
);
1056 static const char * const auxtrace_error_type_name
[] = {
1057 [PERF_AUXTRACE_ERROR_ITRACE
] = "instruction trace",
1060 static const char *auxtrace_error_name(int type
)
1062 const char *error_type_name
= NULL
;
1064 if (type
< PERF_AUXTRACE_ERROR_MAX
)
1065 error_type_name
= auxtrace_error_type_name
[type
];
1066 if (!error_type_name
)
1067 error_type_name
= "unknown AUX";
1068 return error_type_name
;
1071 size_t perf_event__fprintf_auxtrace_error(union perf_event
*event
, FILE *fp
)
1073 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
1076 ret
= fprintf(fp
, " %s error type %u",
1077 auxtrace_error_name(e
->type
), e
->type
);
1078 ret
+= fprintf(fp
, " cpu %d pid %d tid %d ip %#"PRIx64
" code %u: %s\n",
1079 e
->cpu
, e
->pid
, e
->tid
, e
->ip
, e
->code
, e
->msg
);
1083 void perf_session__auxtrace_error_inc(struct perf_session
*session
,
1084 union perf_event
*event
)
1086 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
1088 if (e
->type
< PERF_AUXTRACE_ERROR_MAX
)
1089 session
->evlist
->stats
.nr_auxtrace_errors
[e
->type
] += 1;
1092 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
)
1096 for (i
= 0; i
< PERF_AUXTRACE_ERROR_MAX
; i
++) {
1097 if (!stats
->nr_auxtrace_errors
[i
])
1099 ui__warning("%u %s errors\n",
1100 stats
->nr_auxtrace_errors
[i
],
1101 auxtrace_error_name(i
));
1105 int perf_event__process_auxtrace_error(struct perf_tool
*tool __maybe_unused
,
1106 union perf_event
*event
,
1107 struct perf_session
*session
)
1109 if (auxtrace__dont_decode(session
))
1112 perf_event__fprintf_auxtrace_error(event
, stdout
);
1116 static int __auxtrace_mmap__read(struct auxtrace_mmap
*mm
,
1117 struct auxtrace_record
*itr
,
1118 struct perf_tool
*tool
, process_auxtrace_t fn
,
1119 bool snapshot
, size_t snapshot_size
)
1121 u64 head
, old
= mm
->prev
, offset
, ref
;
1122 unsigned char *data
= mm
->base
;
1123 size_t size
, head_off
, old_off
, len1
, len2
, padding
;
1124 union perf_event ev
;
1125 void *data1
, *data2
;
1128 head
= auxtrace_mmap__read_snapshot_head(mm
);
1129 if (auxtrace_record__find_snapshot(itr
, mm
->idx
, mm
, data
,
1133 head
= auxtrace_mmap__read_head(mm
);
1139 pr_debug3("auxtrace idx %d old %#"PRIx64
" head %#"PRIx64
" diff %#"PRIx64
"\n",
1140 mm
->idx
, old
, head
, head
- old
);
1143 head_off
= head
& mm
->mask
;
1144 old_off
= old
& mm
->mask
;
1146 head_off
= head
% mm
->len
;
1147 old_off
= old
% mm
->len
;
1150 if (head_off
> old_off
)
1151 size
= head_off
- old_off
;
1153 size
= mm
->len
- (old_off
- head_off
);
1155 if (snapshot
&& size
> snapshot_size
)
1156 size
= snapshot_size
;
1158 ref
= auxtrace_record__reference(itr
);
1160 if (head
> old
|| size
<= head
|| mm
->mask
) {
1161 offset
= head
- size
;
1164 * When the buffer size is not a power of 2, 'head' wraps at the
1165 * highest multiple of the buffer size, so we have to subtract
1166 * the remainder here.
1168 u64 rem
= (0ULL - mm
->len
) % mm
->len
;
1170 offset
= head
- size
- rem
;
1173 if (size
> head_off
) {
1174 len1
= size
- head_off
;
1175 data1
= &data
[mm
->len
- len1
];
1180 data1
= &data
[head_off
- len1
];
1185 if (itr
->alignment
) {
1186 unsigned int unwanted
= len1
% itr
->alignment
;
1192 /* padding must be written by fn() e.g. record__process_auxtrace() */
1195 padding
= 8 - padding
;
1197 memset(&ev
, 0, sizeof(ev
));
1198 ev
.auxtrace
.header
.type
= PERF_RECORD_AUXTRACE
;
1199 ev
.auxtrace
.header
.size
= sizeof(ev
.auxtrace
);
1200 ev
.auxtrace
.size
= size
+ padding
;
1201 ev
.auxtrace
.offset
= offset
;
1202 ev
.auxtrace
.reference
= ref
;
1203 ev
.auxtrace
.idx
= mm
->idx
;
1204 ev
.auxtrace
.tid
= mm
->tid
;
1205 ev
.auxtrace
.cpu
= mm
->cpu
;
1207 if (fn(tool
, &ev
, data1
, len1
, data2
, len2
))
1213 auxtrace_mmap__write_tail(mm
, head
);
1214 if (itr
->read_finish
) {
1217 err
= itr
->read_finish(itr
, mm
->idx
);
1226 int auxtrace_mmap__read(struct auxtrace_mmap
*mm
, struct auxtrace_record
*itr
,
1227 struct perf_tool
*tool
, process_auxtrace_t fn
)
1229 return __auxtrace_mmap__read(mm
, itr
, tool
, fn
, false, 0);
1232 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap
*mm
,
1233 struct auxtrace_record
*itr
,
1234 struct perf_tool
*tool
, process_auxtrace_t fn
,
1235 size_t snapshot_size
)
1237 return __auxtrace_mmap__read(mm
, itr
, tool
, fn
, true, snapshot_size
);
1241 * struct auxtrace_cache - hash table to implement a cache
1242 * @hashtable: the hashtable
1243 * @sz: hashtable size (number of hlists)
1244 * @entry_size: size of an entry
1245 * @limit: limit the number of entries to this maximum, when reached the cache
1246 * is dropped and caching begins again with an empty cache
1247 * @cnt: current number of entries
1248 * @bits: hashtable size (@sz = 2^@bits)
1250 struct auxtrace_cache
{
1251 struct hlist_head
*hashtable
;
1259 struct auxtrace_cache
*auxtrace_cache__new(unsigned int bits
, size_t entry_size
,
1260 unsigned int limit_percent
)
1262 struct auxtrace_cache
*c
;
1263 struct hlist_head
*ht
;
1266 c
= zalloc(sizeof(struct auxtrace_cache
));
1272 ht
= calloc(sz
, sizeof(struct hlist_head
));
1276 for (i
= 0; i
< sz
; i
++)
1277 INIT_HLIST_HEAD(&ht
[i
]);
1281 c
->entry_size
= entry_size
;
1282 c
->limit
= (c
->sz
* limit_percent
) / 100;
1292 static void auxtrace_cache__drop(struct auxtrace_cache
*c
)
1294 struct auxtrace_cache_entry
*entry
;
1295 struct hlist_node
*tmp
;
1301 for (i
= 0; i
< c
->sz
; i
++) {
1302 hlist_for_each_entry_safe(entry
, tmp
, &c
->hashtable
[i
], hash
) {
1303 hlist_del(&entry
->hash
);
1304 auxtrace_cache__free_entry(c
, entry
);
1311 void auxtrace_cache__free(struct auxtrace_cache
*c
)
1316 auxtrace_cache__drop(c
);
1321 void *auxtrace_cache__alloc_entry(struct auxtrace_cache
*c
)
1323 return malloc(c
->entry_size
);
1326 void auxtrace_cache__free_entry(struct auxtrace_cache
*c __maybe_unused
,
1332 int auxtrace_cache__add(struct auxtrace_cache
*c
, u32 key
,
1333 struct auxtrace_cache_entry
*entry
)
1335 if (c
->limit
&& ++c
->cnt
> c
->limit
)
1336 auxtrace_cache__drop(c
);
1339 hlist_add_head(&entry
->hash
, &c
->hashtable
[hash_32(key
, c
->bits
)]);
1344 void *auxtrace_cache__lookup(struct auxtrace_cache
*c
, u32 key
)
1346 struct auxtrace_cache_entry
*entry
;
1347 struct hlist_head
*hlist
;
1352 hlist
= &c
->hashtable
[hash_32(key
, c
->bits
)];
1353 hlist_for_each_entry(entry
, hlist
, hash
) {
1354 if (entry
->key
== key
)