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"
51 #include "intel-bts.h"
54 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
55 struct auxtrace_mmap_params *mp,
58 struct perf_event_mmap_page *pc = userpg;
60 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
75 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
76 pr_err("Cannot use AUX area tracing mmaps\n");
80 pc->aux_offset = mp->offset;
81 pc->aux_size = mp->len;
83 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
84 if (mm->base == MAP_FAILED) {
85 pr_debug2("failed to mmap AUX area\n");
93 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
96 munmap(mm->base, mm->len);
101 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
102 off_t auxtrace_offset,
103 unsigned int auxtrace_pages,
104 bool auxtrace_overwrite)
106 if (auxtrace_pages) {
107 mp->offset = auxtrace_offset;
108 mp->len = auxtrace_pages * (size_t)page_size;
109 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
110 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
111 pr_debug2("AUX area mmap length %zu\n", mp->len);
117 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
118 struct perf_evlist *evlist, int idx,
124 mp->cpu = evlist->cpus->map[idx];
126 mp->tid = thread_map__pid(evlist->threads, 0);
131 mp->tid = thread_map__pid(evlist->threads, idx);
135 #define AUXTRACE_INIT_NR_QUEUES 32
137 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
139 struct auxtrace_queue *queue_array;
140 unsigned int max_nr_queues, i;
142 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
143 if (nr_queues > max_nr_queues)
146 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
150 for (i = 0; i < nr_queues; i++) {
151 INIT_LIST_HEAD(&queue_array[i].head);
152 queue_array[i].priv = NULL;
158 int auxtrace_queues__init(struct auxtrace_queues *queues)
160 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
161 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
162 if (!queues->queue_array)
167 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
168 unsigned int new_nr_queues)
170 unsigned int nr_queues = queues->nr_queues;
171 struct auxtrace_queue *queue_array;
175 nr_queues = AUXTRACE_INIT_NR_QUEUES;
177 while (nr_queues && nr_queues < new_nr_queues)
180 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
183 queue_array = auxtrace_alloc_queue_array(nr_queues);
187 for (i = 0; i < queues->nr_queues; i++) {
188 list_splice_tail(&queues->queue_array[i].head,
189 &queue_array[i].head);
190 queue_array[i].priv = queues->queue_array[i].priv;
193 queues->nr_queues = nr_queues;
194 queues->queue_array = queue_array;
199 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
201 int fd = perf_data_file__fd(session->file);
205 if (size > SSIZE_MAX)
212 ret = readn(fd, p, size);
213 if (ret != (ssize_t)size) {
221 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
223 struct auxtrace_buffer *buffer)
225 struct auxtrace_queue *queue;
228 if (idx >= queues->nr_queues) {
229 err = auxtrace_queues__grow(queues, idx + 1);
234 queue = &queues->queue_array[idx];
238 queue->tid = buffer->tid;
239 queue->cpu = buffer->cpu;
240 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
241 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
242 queue->cpu, queue->tid, buffer->cpu, buffer->tid);
246 buffer->buffer_nr = queues->next_buffer_nr++;
248 list_add_tail(&buffer->list, &queue->head);
250 queues->new_data = true;
251 queues->populated = true;
256 /* Limit buffers to 32MiB on 32-bit */
257 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
259 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
261 struct auxtrace_buffer *buffer)
263 u64 sz = buffer->size;
264 bool consecutive = false;
265 struct auxtrace_buffer *b;
268 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
269 b = memdup(buffer, sizeof(struct auxtrace_buffer));
272 b->size = BUFFER_LIMIT_FOR_32_BIT;
273 b->consecutive = consecutive;
274 err = auxtrace_queues__add_buffer(queues, idx, b);
276 auxtrace_buffer__free(b);
279 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
280 sz -= BUFFER_LIMIT_FOR_32_BIT;
285 buffer->consecutive = consecutive;
290 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
291 struct perf_session *session,
293 struct auxtrace_buffer *buffer)
295 if (session->one_mmap) {
296 buffer->data = buffer->data_offset - session->one_mmap_offset +
297 session->one_mmap_addr;
298 } else if (perf_data_file__is_pipe(session->file)) {
299 buffer->data = auxtrace_copy_data(buffer->size, session);
302 buffer->data_needs_freeing = true;
303 } else if (BITS_PER_LONG == 32 &&
304 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
307 err = auxtrace_queues__split_buffer(queues, idx, buffer);
312 return auxtrace_queues__add_buffer(queues, idx, buffer);
315 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
316 struct perf_session *session,
317 union perf_event *event, off_t data_offset,
318 struct auxtrace_buffer **buffer_ptr)
320 struct auxtrace_buffer *buffer;
324 buffer = zalloc(sizeof(struct auxtrace_buffer));
329 buffer->tid = event->auxtrace.tid;
330 buffer->cpu = event->auxtrace.cpu;
331 buffer->data_offset = data_offset;
332 buffer->offset = event->auxtrace.offset;
333 buffer->reference = event->auxtrace.reference;
334 buffer->size = event->auxtrace.size;
335 idx = event->auxtrace.idx;
337 err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
342 *buffer_ptr = buffer;
347 auxtrace_buffer__free(buffer);
351 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
352 struct perf_session *session,
353 off_t file_offset, size_t sz)
355 union perf_event *event;
357 char buf[PERF_SAMPLE_MAX_SIZE];
359 err = perf_session__peek_event(session, file_offset, buf,
360 PERF_SAMPLE_MAX_SIZE, &event, NULL);
364 if (event->header.type == PERF_RECORD_AUXTRACE) {
365 if (event->header.size < sizeof(struct auxtrace_event) ||
366 event->header.size != sz) {
370 file_offset += event->header.size;
371 err = auxtrace_queues__add_event(queues, session, event,
378 void auxtrace_queues__free(struct auxtrace_queues *queues)
382 for (i = 0; i < queues->nr_queues; i++) {
383 while (!list_empty(&queues->queue_array[i].head)) {
384 struct auxtrace_buffer *buffer;
386 buffer = list_entry(queues->queue_array[i].head.next,
387 struct auxtrace_buffer, list);
388 list_del(&buffer->list);
389 auxtrace_buffer__free(buffer);
393 zfree(&queues->queue_array);
394 queues->nr_queues = 0;
397 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
398 unsigned int pos, unsigned int queue_nr,
404 parent = (pos - 1) >> 1;
405 if (heap_array[parent].ordinal <= ordinal)
407 heap_array[pos] = heap_array[parent];
410 heap_array[pos].queue_nr = queue_nr;
411 heap_array[pos].ordinal = ordinal;
414 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
417 struct auxtrace_heap_item *heap_array;
419 if (queue_nr >= heap->heap_sz) {
420 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
422 while (heap_sz <= queue_nr)
424 heap_array = realloc(heap->heap_array,
425 heap_sz * sizeof(struct auxtrace_heap_item));
428 heap->heap_array = heap_array;
429 heap->heap_sz = heap_sz;
432 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
437 void auxtrace_heap__free(struct auxtrace_heap *heap)
439 zfree(&heap->heap_array);
444 void auxtrace_heap__pop(struct auxtrace_heap *heap)
446 unsigned int pos, last, heap_cnt = heap->heap_cnt;
447 struct auxtrace_heap_item *heap_array;
454 heap_array = heap->heap_array;
458 unsigned int left, right;
460 left = (pos << 1) + 1;
461 if (left >= heap_cnt)
464 if (right >= heap_cnt) {
465 heap_array[pos] = heap_array[left];
468 if (heap_array[left].ordinal < heap_array[right].ordinal) {
469 heap_array[pos] = heap_array[left];
472 heap_array[pos] = heap_array[right];
478 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
479 heap_array[last].ordinal);
482 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
483 struct perf_evlist *evlist)
486 return itr->info_priv_size(itr, evlist);
490 static int auxtrace_not_supported(void)
492 pr_err("AUX area tracing is not supported on this architecture\n");
496 int auxtrace_record__info_fill(struct auxtrace_record *itr,
497 struct perf_session *session,
498 struct auxtrace_info_event *auxtrace_info,
502 return itr->info_fill(itr, session, auxtrace_info, priv_size);
503 return auxtrace_not_supported();
506 void auxtrace_record__free(struct auxtrace_record *itr)
512 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
514 if (itr && itr->snapshot_start)
515 return itr->snapshot_start(itr);
519 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
521 if (itr && itr->snapshot_finish)
522 return itr->snapshot_finish(itr);
526 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
527 struct auxtrace_mmap *mm,
528 unsigned char *data, u64 *head, u64 *old)
530 if (itr && itr->find_snapshot)
531 return itr->find_snapshot(itr, idx, mm, data, head, old);
535 int auxtrace_record__options(struct auxtrace_record *itr,
536 struct perf_evlist *evlist,
537 struct record_opts *opts)
540 return itr->recording_options(itr, evlist, opts);
544 u64 auxtrace_record__reference(struct auxtrace_record *itr)
547 return itr->reference(itr);
551 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
552 struct record_opts *opts, const char *str)
558 return itr->parse_snapshot_options(itr, opts, str);
560 pr_err("No AUX area tracing to snapshot\n");
564 struct auxtrace_record *__weak
565 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
571 static int auxtrace_index__alloc(struct list_head *head)
573 struct auxtrace_index *auxtrace_index;
575 auxtrace_index = malloc(sizeof(struct auxtrace_index));
579 auxtrace_index->nr = 0;
580 INIT_LIST_HEAD(&auxtrace_index->list);
582 list_add_tail(&auxtrace_index->list, head);
587 void auxtrace_index__free(struct list_head *head)
589 struct auxtrace_index *auxtrace_index, *n;
591 list_for_each_entry_safe(auxtrace_index, n, head, list) {
592 list_del(&auxtrace_index->list);
593 free(auxtrace_index);
597 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
599 struct auxtrace_index *auxtrace_index;
602 if (list_empty(head)) {
603 err = auxtrace_index__alloc(head);
608 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
610 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
611 err = auxtrace_index__alloc(head);
614 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
618 return auxtrace_index;
621 int auxtrace_index__auxtrace_event(struct list_head *head,
622 union perf_event *event, off_t file_offset)
624 struct auxtrace_index *auxtrace_index;
627 auxtrace_index = auxtrace_index__last(head);
631 nr = auxtrace_index->nr;
632 auxtrace_index->entries[nr].file_offset = file_offset;
633 auxtrace_index->entries[nr].sz = event->header.size;
634 auxtrace_index->nr += 1;
639 static int auxtrace_index__do_write(int fd,
640 struct auxtrace_index *auxtrace_index)
642 struct auxtrace_index_entry ent;
645 for (i = 0; i < auxtrace_index->nr; i++) {
646 ent.file_offset = auxtrace_index->entries[i].file_offset;
647 ent.sz = auxtrace_index->entries[i].sz;
648 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
654 int auxtrace_index__write(int fd, struct list_head *head)
656 struct auxtrace_index *auxtrace_index;
660 list_for_each_entry(auxtrace_index, head, list)
661 total += auxtrace_index->nr;
663 if (writen(fd, &total, sizeof(total)) != sizeof(total))
666 list_for_each_entry(auxtrace_index, head, list) {
667 err = auxtrace_index__do_write(fd, auxtrace_index);
675 static int auxtrace_index__process_entry(int fd, struct list_head *head,
678 struct auxtrace_index *auxtrace_index;
679 struct auxtrace_index_entry ent;
682 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
685 auxtrace_index = auxtrace_index__last(head);
689 nr = auxtrace_index->nr;
691 auxtrace_index->entries[nr].file_offset =
692 bswap_64(ent.file_offset);
693 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
695 auxtrace_index->entries[nr].file_offset = ent.file_offset;
696 auxtrace_index->entries[nr].sz = ent.sz;
699 auxtrace_index->nr = nr + 1;
704 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
707 struct list_head *head = &session->auxtrace_index;
710 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
716 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
722 err = auxtrace_index__process_entry(fd, head, needs_swap);
730 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
731 struct perf_session *session,
732 struct auxtrace_index_entry *ent)
734 return auxtrace_queues__add_indexed_event(queues, session,
735 ent->file_offset, ent->sz);
738 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
739 struct perf_session *session)
741 struct auxtrace_index *auxtrace_index;
742 struct auxtrace_index_entry *ent;
746 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
747 for (i = 0; i < auxtrace_index->nr; i++) {
748 ent = &auxtrace_index->entries[i];
749 err = auxtrace_queues__process_index_entry(queues,
759 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
760 struct auxtrace_buffer *buffer)
763 if (list_is_last(&buffer->list, &queue->head))
765 return list_entry(buffer->list.next, struct auxtrace_buffer,
768 if (list_empty(&queue->head))
770 return list_entry(queue->head.next, struct auxtrace_buffer,
775 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
777 size_t adj = buffer->data_offset & (page_size - 1);
778 size_t size = buffer->size + adj;
779 off_t file_offset = buffer->data_offset - adj;
785 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
786 if (addr == MAP_FAILED)
789 buffer->mmap_addr = addr;
790 buffer->mmap_size = size;
792 buffer->data = addr + adj;
797 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
799 if (!buffer->data || !buffer->mmap_addr)
801 munmap(buffer->mmap_addr, buffer->mmap_size);
802 buffer->mmap_addr = NULL;
803 buffer->mmap_size = 0;
805 buffer->use_data = NULL;
808 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
810 auxtrace_buffer__put_data(buffer);
811 if (buffer->data_needs_freeing) {
812 buffer->data_needs_freeing = false;
813 zfree(&buffer->data);
814 buffer->use_data = NULL;
819 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
821 auxtrace_buffer__drop_data(buffer);
825 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
826 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
831 memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
833 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
834 auxtrace_error->type = type;
835 auxtrace_error->code = code;
836 auxtrace_error->cpu = cpu;
837 auxtrace_error->pid = pid;
838 auxtrace_error->tid = tid;
839 auxtrace_error->ip = ip;
840 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
842 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
843 strlen(auxtrace_error->msg) + 1;
844 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
847 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
848 struct perf_tool *tool,
849 struct perf_session *session,
850 perf_event__handler_t process)
852 union perf_event *ev;
856 pr_debug2("Synthesizing auxtrace information\n");
857 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
858 ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
862 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
863 ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
865 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
870 err = process(tool, ev, NULL, NULL);
876 static bool auxtrace__dont_decode(struct perf_session *session)
878 return !session->itrace_synth_opts ||
879 session->itrace_synth_opts->dont_decode;
882 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
883 union perf_event *event,
884 struct perf_session *session)
886 enum auxtrace_type type = event->auxtrace_info.type;
889 fprintf(stdout, " type: %u\n", type);
892 case PERF_AUXTRACE_INTEL_PT:
893 return intel_pt_process_auxtrace_info(event, session);
894 case PERF_AUXTRACE_INTEL_BTS:
895 return intel_bts_process_auxtrace_info(event, session);
896 case PERF_AUXTRACE_CS_ETM:
897 return cs_etm__process_auxtrace_info(event, session);
898 case PERF_AUXTRACE_UNKNOWN:
904 s64 perf_event__process_auxtrace(struct perf_tool *tool,
905 union perf_event *event,
906 struct perf_session *session)
911 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
912 event->auxtrace.size, event->auxtrace.offset,
913 event->auxtrace.reference, event->auxtrace.idx,
914 event->auxtrace.tid, event->auxtrace.cpu);
916 if (auxtrace__dont_decode(session))
917 return event->auxtrace.size;
919 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
922 err = session->auxtrace->process_auxtrace_event(session, event, tool);
926 return event->auxtrace.size;
929 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
930 #define PERF_ITRACE_DEFAULT_PERIOD 100000
931 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
932 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
933 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
934 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
936 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
938 synth_opts->instructions = true;
939 synth_opts->branches = true;
940 synth_opts->transactions = true;
941 synth_opts->errors = true;
942 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
943 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
944 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
945 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
949 * Please check tools/perf/Documentation/perf-script.txt for information
950 * about the options parsed here, which is introduced after this cset,
951 * when support in 'perf script' for these options is introduced.
953 int itrace_parse_synth_opts(const struct option *opt, const char *str,
956 struct itrace_synth_opts *synth_opts = opt->value;
959 bool period_type_set = false;
960 bool period_set = false;
962 synth_opts->set = true;
965 synth_opts->dont_decode = true;
970 itrace_synth_opts__set_default(synth_opts);
977 synth_opts->instructions = true;
978 while (*p == ' ' || *p == ',')
981 synth_opts->period = strtoull(p, &endptr, 10);
984 while (*p == ' ' || *p == ',')
988 synth_opts->period_type =
989 PERF_ITRACE_PERIOD_INSTRUCTIONS;
990 period_type_set = true;
993 synth_opts->period_type =
994 PERF_ITRACE_PERIOD_TICKS;
995 period_type_set = true;
998 synth_opts->period *= 1000;
1001 synth_opts->period *= 1000;
1006 synth_opts->period_type =
1007 PERF_ITRACE_PERIOD_NANOSECS;
1008 period_type_set = true;
1018 synth_opts->branches = true;
1021 synth_opts->transactions = true;
1024 synth_opts->errors = true;
1027 synth_opts->log = true;
1030 synth_opts->branches = true;
1031 synth_opts->calls = true;
1034 synth_opts->branches = true;
1035 synth_opts->returns = true;
1038 synth_opts->callchain = true;
1039 synth_opts->callchain_sz =
1040 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1041 while (*p == ' ' || *p == ',')
1046 val = strtoul(p, &endptr, 10);
1048 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1050 synth_opts->callchain_sz = val;
1054 synth_opts->last_branch = true;
1055 synth_opts->last_branch_sz =
1056 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1057 while (*p == ' ' || *p == ',')
1062 val = strtoul(p, &endptr, 10);
1065 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1067 synth_opts->last_branch_sz = val;
1078 if (synth_opts->instructions) {
1079 if (!period_type_set)
1080 synth_opts->period_type =
1081 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1083 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1089 pr_err("Bad Instruction Tracing options '%s'\n", str);
1093 static const char * const auxtrace_error_type_name[] = {
1094 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1097 static const char *auxtrace_error_name(int type)
1099 const char *error_type_name = NULL;
1101 if (type < PERF_AUXTRACE_ERROR_MAX)
1102 error_type_name = auxtrace_error_type_name[type];
1103 if (!error_type_name)
1104 error_type_name = "unknown AUX";
1105 return error_type_name;
1108 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1110 struct auxtrace_error_event *e = &event->auxtrace_error;
1113 ret = fprintf(fp, " %s error type %u",
1114 auxtrace_error_name(e->type), e->type);
1115 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1116 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1120 void perf_session__auxtrace_error_inc(struct perf_session *session,
1121 union perf_event *event)
1123 struct auxtrace_error_event *e = &event->auxtrace_error;
1125 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1126 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1129 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1133 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1134 if (!stats->nr_auxtrace_errors[i])
1136 ui__warning("%u %s errors\n",
1137 stats->nr_auxtrace_errors[i],
1138 auxtrace_error_name(i));
1142 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1143 union perf_event *event,
1144 struct perf_session *session)
1146 if (auxtrace__dont_decode(session))
1149 perf_event__fprintf_auxtrace_error(event, stdout);
1153 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1154 struct auxtrace_record *itr,
1155 struct perf_tool *tool, process_auxtrace_t fn,
1156 bool snapshot, size_t snapshot_size)
1158 u64 head, old = mm->prev, offset, ref;
1159 unsigned char *data = mm->base;
1160 size_t size, head_off, old_off, len1, len2, padding;
1161 union perf_event ev;
1162 void *data1, *data2;
1165 head = auxtrace_mmap__read_snapshot_head(mm);
1166 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1170 head = auxtrace_mmap__read_head(mm);
1176 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1177 mm->idx, old, head, head - old);
1180 head_off = head & mm->mask;
1181 old_off = old & mm->mask;
1183 head_off = head % mm->len;
1184 old_off = old % mm->len;
1187 if (head_off > old_off)
1188 size = head_off - old_off;
1190 size = mm->len - (old_off - head_off);
1192 if (snapshot && size > snapshot_size)
1193 size = snapshot_size;
1195 ref = auxtrace_record__reference(itr);
1197 if (head > old || size <= head || mm->mask) {
1198 offset = head - size;
1201 * When the buffer size is not a power of 2, 'head' wraps at the
1202 * highest multiple of the buffer size, so we have to subtract
1203 * the remainder here.
1205 u64 rem = (0ULL - mm->len) % mm->len;
1207 offset = head - size - rem;
1210 if (size > head_off) {
1211 len1 = size - head_off;
1212 data1 = &data[mm->len - len1];
1217 data1 = &data[head_off - len1];
1222 if (itr->alignment) {
1223 unsigned int unwanted = len1 % itr->alignment;
1229 /* padding must be written by fn() e.g. record__process_auxtrace() */
1232 padding = 8 - padding;
1234 memset(&ev, 0, sizeof(ev));
1235 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1236 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1237 ev.auxtrace.size = size + padding;
1238 ev.auxtrace.offset = offset;
1239 ev.auxtrace.reference = ref;
1240 ev.auxtrace.idx = mm->idx;
1241 ev.auxtrace.tid = mm->tid;
1242 ev.auxtrace.cpu = mm->cpu;
1244 if (fn(tool, &ev, data1, len1, data2, len2))
1250 auxtrace_mmap__write_tail(mm, head);
1251 if (itr->read_finish) {
1254 err = itr->read_finish(itr, mm->idx);
1263 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1264 struct perf_tool *tool, process_auxtrace_t fn)
1266 return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1269 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1270 struct auxtrace_record *itr,
1271 struct perf_tool *tool, process_auxtrace_t fn,
1272 size_t snapshot_size)
1274 return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1278 * struct auxtrace_cache - hash table to implement a cache
1279 * @hashtable: the hashtable
1280 * @sz: hashtable size (number of hlists)
1281 * @entry_size: size of an entry
1282 * @limit: limit the number of entries to this maximum, when reached the cache
1283 * is dropped and caching begins again with an empty cache
1284 * @cnt: current number of entries
1285 * @bits: hashtable size (@sz = 2^@bits)
1287 struct auxtrace_cache {
1288 struct hlist_head *hashtable;
1296 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1297 unsigned int limit_percent)
1299 struct auxtrace_cache *c;
1300 struct hlist_head *ht;
1303 c = zalloc(sizeof(struct auxtrace_cache));
1309 ht = calloc(sz, sizeof(struct hlist_head));
1313 for (i = 0; i < sz; i++)
1314 INIT_HLIST_HEAD(&ht[i]);
1318 c->entry_size = entry_size;
1319 c->limit = (c->sz * limit_percent) / 100;
1329 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1331 struct auxtrace_cache_entry *entry;
1332 struct hlist_node *tmp;
1338 for (i = 0; i < c->sz; i++) {
1339 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1340 hlist_del(&entry->hash);
1341 auxtrace_cache__free_entry(c, entry);
1348 void auxtrace_cache__free(struct auxtrace_cache *c)
1353 auxtrace_cache__drop(c);
1358 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1360 return malloc(c->entry_size);
1363 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1369 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1370 struct auxtrace_cache_entry *entry)
1372 if (c->limit && ++c->cnt > c->limit)
1373 auxtrace_cache__drop(c);
1376 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1381 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1383 struct auxtrace_cache_entry *entry;
1384 struct hlist_head *hlist;
1389 hlist = &c->hashtable[hash_32(key, c->bits)];
1390 hlist_for_each_entry(entry, hlist, hash) {
1391 if (entry->key == key)