1 // SPDX-License-Identifier: GPL-2.0-only
3 * auxtrace.c: AUX area trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
15 #include <linux/kernel.h>
16 #include <linux/perf_event.h>
17 #include <linux/types.h>
18 #include <linux/bitops.h>
19 #include <linux/log2.h>
20 #include <linux/string.h>
21 #include <linux/time64.h>
23 #include <sys/param.h>
26 #include <linux/list.h>
27 #include <linux/zalloc.h>
35 #include "evsel_config.h"
37 #include "util/perf_api_probe.h"
38 #include "util/synthetic-events.h"
39 #include "thread_map.h"
43 #include <linux/hash.h>
49 #include <subcmd/parse-options.h>
53 #include "intel-bts.h"
56 #include "s390-cpumsf.h"
57 #include "util/mmap.h"
59 #include <linux/ctype.h>
60 #include "symbol/kallsyms.h"
61 #include <internal/lib.h>
62 #include "util/sample.h"
65 * Make a group from 'leader' to 'last', requiring that the events were not
66 * already grouped to a different leader.
68 static int evlist__regroup(struct evlist *evlist, struct evsel *leader, struct evsel *last)
73 if (!evsel__is_group_leader(leader))
77 evlist__for_each_entry(evlist, evsel) {
79 if (!(evsel__leader(evsel) == leader ||
80 (evsel__leader(evsel) == evsel &&
81 evsel->core.nr_members <= 1)))
83 } else if (evsel == leader) {
91 evlist__for_each_entry(evlist, evsel) {
93 if (!evsel__has_leader(evsel, leader)) {
94 evsel__set_leader(evsel, leader);
95 if (leader->core.nr_members < 1)
96 leader->core.nr_members = 1;
97 leader->core.nr_members += 1;
99 } else if (evsel == leader) {
109 static bool auxtrace__dont_decode(struct perf_session *session)
111 return !session->itrace_synth_opts ||
112 session->itrace_synth_opts->dont_decode;
115 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
116 struct auxtrace_mmap_params *mp,
117 void *userpg, int fd)
119 struct perf_event_mmap_page *pc = userpg;
121 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
129 mm->cpu = mp->cpu.cpu;
131 if (!mp->len || !mp->mmap_needed) {
136 pc->aux_offset = mp->offset;
137 pc->aux_size = mp->len;
139 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
140 if (mm->base == MAP_FAILED) {
141 pr_debug2("failed to mmap AUX area\n");
149 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
152 munmap(mm->base, mm->len);
157 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
158 off_t auxtrace_offset,
159 unsigned int auxtrace_pages,
160 bool auxtrace_overwrite)
162 if (auxtrace_pages) {
163 mp->offset = auxtrace_offset;
164 mp->len = auxtrace_pages * (size_t)page_size;
165 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
166 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
167 pr_debug2("AUX area mmap length %zu\n", mp->len);
173 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
174 struct evlist *evlist,
175 struct evsel *evsel, int idx)
177 bool per_cpu = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
179 mp->mmap_needed = evsel->needs_auxtrace_mmap;
181 if (!mp->mmap_needed)
187 mp->cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
188 if (evlist->core.threads)
189 mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
194 mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
198 #define AUXTRACE_INIT_NR_QUEUES 32
200 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
202 struct auxtrace_queue *queue_array;
203 unsigned int max_nr_queues, i;
205 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
206 if (nr_queues > max_nr_queues)
209 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
213 for (i = 0; i < nr_queues; i++) {
214 INIT_LIST_HEAD(&queue_array[i].head);
215 queue_array[i].priv = NULL;
221 int auxtrace_queues__init(struct auxtrace_queues *queues)
223 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
224 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
225 if (!queues->queue_array)
230 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
231 unsigned int new_nr_queues)
233 unsigned int nr_queues = queues->nr_queues;
234 struct auxtrace_queue *queue_array;
238 nr_queues = AUXTRACE_INIT_NR_QUEUES;
240 while (nr_queues && nr_queues < new_nr_queues)
243 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
246 queue_array = auxtrace_alloc_queue_array(nr_queues);
250 for (i = 0; i < queues->nr_queues; i++) {
251 list_splice_tail(&queues->queue_array[i].head,
252 &queue_array[i].head);
253 queue_array[i].tid = queues->queue_array[i].tid;
254 queue_array[i].cpu = queues->queue_array[i].cpu;
255 queue_array[i].set = queues->queue_array[i].set;
256 queue_array[i].priv = queues->queue_array[i].priv;
259 queues->nr_queues = nr_queues;
260 queues->queue_array = queue_array;
265 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
267 int fd = perf_data__fd(session->data);
271 if (size > SSIZE_MAX)
278 ret = readn(fd, p, size);
279 if (ret != (ssize_t)size) {
287 static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
289 struct auxtrace_buffer *buffer)
291 struct auxtrace_queue *queue;
294 if (idx >= queues->nr_queues) {
295 err = auxtrace_queues__grow(queues, idx + 1);
300 queue = &queues->queue_array[idx];
304 queue->tid = buffer->tid;
305 queue->cpu = buffer->cpu.cpu;
308 buffer->buffer_nr = queues->next_buffer_nr++;
310 list_add_tail(&buffer->list, &queue->head);
312 queues->new_data = true;
313 queues->populated = true;
318 /* Limit buffers to 32MiB on 32-bit */
319 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
321 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
323 struct auxtrace_buffer *buffer)
325 u64 sz = buffer->size;
326 bool consecutive = false;
327 struct auxtrace_buffer *b;
330 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
331 b = memdup(buffer, sizeof(struct auxtrace_buffer));
334 b->size = BUFFER_LIMIT_FOR_32_BIT;
335 b->consecutive = consecutive;
336 err = auxtrace_queues__queue_buffer(queues, idx, b);
338 auxtrace_buffer__free(b);
341 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
342 sz -= BUFFER_LIMIT_FOR_32_BIT;
347 buffer->consecutive = consecutive;
352 static bool filter_cpu(struct perf_session *session, struct perf_cpu cpu)
354 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
356 return cpu_bitmap && cpu.cpu != -1 && !test_bit(cpu.cpu, cpu_bitmap);
359 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
360 struct perf_session *session,
362 struct auxtrace_buffer *buffer,
363 struct auxtrace_buffer **buffer_ptr)
367 if (filter_cpu(session, buffer->cpu))
370 buffer = memdup(buffer, sizeof(*buffer));
374 if (session->one_mmap) {
375 buffer->data = buffer->data_offset - session->one_mmap_offset +
376 session->one_mmap_addr;
377 } else if (perf_data__is_pipe(session->data)) {
378 buffer->data = auxtrace_copy_data(buffer->size, session);
381 buffer->data_needs_freeing = true;
382 } else if (BITS_PER_LONG == 32 &&
383 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
384 err = auxtrace_queues__split_buffer(queues, idx, buffer);
389 err = auxtrace_queues__queue_buffer(queues, idx, buffer);
393 /* FIXME: Doesn't work for split buffer */
395 *buffer_ptr = buffer;
400 auxtrace_buffer__free(buffer);
404 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
405 struct perf_session *session,
406 union perf_event *event, off_t data_offset,
407 struct auxtrace_buffer **buffer_ptr)
409 struct auxtrace_buffer buffer = {
411 .tid = event->auxtrace.tid,
412 .cpu = { event->auxtrace.cpu },
413 .data_offset = data_offset,
414 .offset = event->auxtrace.offset,
415 .reference = event->auxtrace.reference,
416 .size = event->auxtrace.size,
418 unsigned int idx = event->auxtrace.idx;
420 return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
424 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
425 struct perf_session *session,
426 off_t file_offset, size_t sz)
428 union perf_event *event;
430 char buf[PERF_SAMPLE_MAX_SIZE];
432 err = perf_session__peek_event(session, file_offset, buf,
433 PERF_SAMPLE_MAX_SIZE, &event, NULL);
437 if (event->header.type == PERF_RECORD_AUXTRACE) {
438 if (event->header.size < sizeof(struct perf_record_auxtrace) ||
439 event->header.size != sz) {
443 file_offset += event->header.size;
444 err = auxtrace_queues__add_event(queues, session, event,
451 void auxtrace_queues__free(struct auxtrace_queues *queues)
455 for (i = 0; i < queues->nr_queues; i++) {
456 while (!list_empty(&queues->queue_array[i].head)) {
457 struct auxtrace_buffer *buffer;
459 buffer = list_entry(queues->queue_array[i].head.next,
460 struct auxtrace_buffer, list);
461 list_del_init(&buffer->list);
462 auxtrace_buffer__free(buffer);
466 zfree(&queues->queue_array);
467 queues->nr_queues = 0;
470 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
471 unsigned int pos, unsigned int queue_nr,
477 parent = (pos - 1) >> 1;
478 if (heap_array[parent].ordinal <= ordinal)
480 heap_array[pos] = heap_array[parent];
483 heap_array[pos].queue_nr = queue_nr;
484 heap_array[pos].ordinal = ordinal;
487 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
490 struct auxtrace_heap_item *heap_array;
492 if (queue_nr >= heap->heap_sz) {
493 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
495 while (heap_sz <= queue_nr)
497 heap_array = realloc(heap->heap_array,
498 heap_sz * sizeof(struct auxtrace_heap_item));
501 heap->heap_array = heap_array;
502 heap->heap_sz = heap_sz;
505 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
510 void auxtrace_heap__free(struct auxtrace_heap *heap)
512 zfree(&heap->heap_array);
517 void auxtrace_heap__pop(struct auxtrace_heap *heap)
519 unsigned int pos, last, heap_cnt = heap->heap_cnt;
520 struct auxtrace_heap_item *heap_array;
527 heap_array = heap->heap_array;
531 unsigned int left, right;
533 left = (pos << 1) + 1;
534 if (left >= heap_cnt)
537 if (right >= heap_cnt) {
538 heap_array[pos] = heap_array[left];
541 if (heap_array[left].ordinal < heap_array[right].ordinal) {
542 heap_array[pos] = heap_array[left];
545 heap_array[pos] = heap_array[right];
551 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
552 heap_array[last].ordinal);
555 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
556 struct evlist *evlist)
559 return itr->info_priv_size(itr, evlist);
563 static int auxtrace_not_supported(void)
565 pr_err("AUX area tracing is not supported on this architecture\n");
569 int auxtrace_record__info_fill(struct auxtrace_record *itr,
570 struct perf_session *session,
571 struct perf_record_auxtrace_info *auxtrace_info,
575 return itr->info_fill(itr, session, auxtrace_info, priv_size);
576 return auxtrace_not_supported();
579 void auxtrace_record__free(struct auxtrace_record *itr)
585 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
587 if (itr && itr->snapshot_start)
588 return itr->snapshot_start(itr);
592 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
594 if (!on_exit && itr && itr->snapshot_finish)
595 return itr->snapshot_finish(itr);
599 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
600 struct auxtrace_mmap *mm,
601 unsigned char *data, u64 *head, u64 *old)
603 if (itr && itr->find_snapshot)
604 return itr->find_snapshot(itr, idx, mm, data, head, old);
608 int auxtrace_record__options(struct auxtrace_record *itr,
609 struct evlist *evlist,
610 struct record_opts *opts)
613 itr->evlist = evlist;
614 return itr->recording_options(itr, evlist, opts);
619 u64 auxtrace_record__reference(struct auxtrace_record *itr)
622 return itr->reference(itr);
626 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
627 struct record_opts *opts, const char *str)
632 /* PMU-agnostic options */
635 opts->auxtrace_snapshot_on_exit = true;
642 if (itr && itr->parse_snapshot_options)
643 return itr->parse_snapshot_options(itr, opts, str);
645 pr_err("No AUX area tracing to snapshot\n");
649 static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
651 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
654 struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
655 int cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
657 if (cpu_map_idx == -1)
659 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
662 return perf_evsel__enable_thread(&evsel->core, idx);
665 int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
669 if (!itr->evlist || !itr->pmu)
672 evlist__for_each_entry(itr->evlist, evsel) {
673 if (evsel->core.attr.type == itr->pmu->type) {
676 return evlist__enable_event_idx(itr->evlist, evsel, idx);
683 * Event record size is 16-bit which results in a maximum size of about 64KiB.
684 * Allow about 4KiB for the rest of the sample record, to give a maximum
685 * AUX area sample size of 60KiB.
687 #define MAX_AUX_SAMPLE_SIZE (60 * 1024)
689 /* Arbitrary default size if no other default provided */
690 #define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024)
692 static int auxtrace_validate_aux_sample_size(struct evlist *evlist,
693 struct record_opts *opts)
696 bool has_aux_leader = false;
699 evlist__for_each_entry(evlist, evsel) {
700 sz = evsel->core.attr.aux_sample_size;
701 if (evsel__is_group_leader(evsel)) {
702 has_aux_leader = evsel__is_aux_event(evsel);
705 pr_err("Cannot add AUX area sampling to an AUX area event\n");
707 pr_err("Cannot add AUX area sampling to a group leader\n");
711 if (sz > MAX_AUX_SAMPLE_SIZE) {
712 pr_err("AUX area sample size %u too big, max. %d\n",
713 sz, MAX_AUX_SAMPLE_SIZE);
717 if (!has_aux_leader) {
718 pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n");
721 evsel__set_sample_bit(evsel, AUX);
722 opts->auxtrace_sample_mode = true;
724 evsel__reset_sample_bit(evsel, AUX);
728 if (!opts->auxtrace_sample_mode) {
729 pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n");
733 if (!perf_can_aux_sample()) {
734 pr_err("AUX area sampling is not supported by kernel\n");
741 int auxtrace_parse_sample_options(struct auxtrace_record *itr,
742 struct evlist *evlist,
743 struct record_opts *opts, const char *str)
745 struct evsel_config_term *term;
746 struct evsel *aux_evsel;
747 bool has_aux_sample_size = false;
748 bool has_aux_leader = false;
757 pr_err("No AUX area event to sample\n");
761 sz = strtoul(str, &endptr, 0);
762 if (*endptr || sz > UINT_MAX) {
763 pr_err("Bad AUX area sampling option: '%s'\n", str);
768 sz = itr->default_aux_sample_size;
771 sz = DEFAULT_AUX_SAMPLE_SIZE;
773 /* Set aux_sample_size based on --aux-sample option */
774 evlist__for_each_entry(evlist, evsel) {
775 if (evsel__is_group_leader(evsel)) {
776 has_aux_leader = evsel__is_aux_event(evsel);
777 } else if (has_aux_leader) {
778 evsel->core.attr.aux_sample_size = sz;
783 /* Override with aux_sample_size from config term */
784 evlist__for_each_entry(evlist, evsel) {
785 if (evsel__is_aux_event(evsel))
787 term = evsel__get_config_term(evsel, AUX_SAMPLE_SIZE);
789 has_aux_sample_size = true;
790 evsel->core.attr.aux_sample_size = term->val.aux_sample_size;
791 /* If possible, group with the AUX event */
792 if (aux_evsel && evsel->core.attr.aux_sample_size)
793 evlist__regroup(evlist, aux_evsel, evsel);
797 if (!str && !has_aux_sample_size)
801 pr_err("No AUX area event to sample\n");
805 return auxtrace_validate_aux_sample_size(evlist, opts);
808 void auxtrace_regroup_aux_output(struct evlist *evlist)
810 struct evsel *evsel, *aux_evsel = NULL;
811 struct evsel_config_term *term;
813 evlist__for_each_entry(evlist, evsel) {
814 if (evsel__is_aux_event(evsel))
816 term = evsel__get_config_term(evsel, AUX_OUTPUT);
817 /* If possible, group with the AUX event */
818 if (term && aux_evsel)
819 evlist__regroup(evlist, aux_evsel, evsel);
823 struct auxtrace_record *__weak
824 auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
830 static int auxtrace_index__alloc(struct list_head *head)
832 struct auxtrace_index *auxtrace_index;
834 auxtrace_index = malloc(sizeof(struct auxtrace_index));
838 auxtrace_index->nr = 0;
839 INIT_LIST_HEAD(&auxtrace_index->list);
841 list_add_tail(&auxtrace_index->list, head);
846 void auxtrace_index__free(struct list_head *head)
848 struct auxtrace_index *auxtrace_index, *n;
850 list_for_each_entry_safe(auxtrace_index, n, head, list) {
851 list_del_init(&auxtrace_index->list);
852 free(auxtrace_index);
856 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
858 struct auxtrace_index *auxtrace_index;
861 if (list_empty(head)) {
862 err = auxtrace_index__alloc(head);
867 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
869 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
870 err = auxtrace_index__alloc(head);
873 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
877 return auxtrace_index;
880 int auxtrace_index__auxtrace_event(struct list_head *head,
881 union perf_event *event, off_t file_offset)
883 struct auxtrace_index *auxtrace_index;
886 auxtrace_index = auxtrace_index__last(head);
890 nr = auxtrace_index->nr;
891 auxtrace_index->entries[nr].file_offset = file_offset;
892 auxtrace_index->entries[nr].sz = event->header.size;
893 auxtrace_index->nr += 1;
898 static int auxtrace_index__do_write(int fd,
899 struct auxtrace_index *auxtrace_index)
901 struct auxtrace_index_entry ent;
904 for (i = 0; i < auxtrace_index->nr; i++) {
905 ent.file_offset = auxtrace_index->entries[i].file_offset;
906 ent.sz = auxtrace_index->entries[i].sz;
907 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
913 int auxtrace_index__write(int fd, struct list_head *head)
915 struct auxtrace_index *auxtrace_index;
919 list_for_each_entry(auxtrace_index, head, list)
920 total += auxtrace_index->nr;
922 if (writen(fd, &total, sizeof(total)) != sizeof(total))
925 list_for_each_entry(auxtrace_index, head, list) {
926 err = auxtrace_index__do_write(fd, auxtrace_index);
934 static int auxtrace_index__process_entry(int fd, struct list_head *head,
937 struct auxtrace_index *auxtrace_index;
938 struct auxtrace_index_entry ent;
941 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
944 auxtrace_index = auxtrace_index__last(head);
948 nr = auxtrace_index->nr;
950 auxtrace_index->entries[nr].file_offset =
951 bswap_64(ent.file_offset);
952 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
954 auxtrace_index->entries[nr].file_offset = ent.file_offset;
955 auxtrace_index->entries[nr].sz = ent.sz;
958 auxtrace_index->nr = nr + 1;
963 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
966 struct list_head *head = &session->auxtrace_index;
969 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
975 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
981 err = auxtrace_index__process_entry(fd, head, needs_swap);
989 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
990 struct perf_session *session,
991 struct auxtrace_index_entry *ent)
993 return auxtrace_queues__add_indexed_event(queues, session,
994 ent->file_offset, ent->sz);
997 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
998 struct perf_session *session)
1000 struct auxtrace_index *auxtrace_index;
1001 struct auxtrace_index_entry *ent;
1005 if (auxtrace__dont_decode(session))
1008 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
1009 for (i = 0; i < auxtrace_index->nr; i++) {
1010 ent = &auxtrace_index->entries[i];
1011 err = auxtrace_queues__process_index_entry(queues,
1021 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
1022 struct auxtrace_buffer *buffer)
1025 if (list_is_last(&buffer->list, &queue->head))
1027 return list_entry(buffer->list.next, struct auxtrace_buffer,
1030 if (list_empty(&queue->head))
1032 return list_entry(queue->head.next, struct auxtrace_buffer,
1037 struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
1038 struct perf_sample *sample,
1039 struct perf_session *session)
1041 struct perf_sample_id *sid;
1049 sid = evlist__id2sid(session->evlist, id);
1055 if (idx >= queues->nr_queues)
1058 return &queues->queue_array[idx];
1061 int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
1062 struct perf_session *session,
1063 struct perf_sample *sample, u64 data_offset,
1066 struct auxtrace_buffer buffer = {
1068 .data_offset = data_offset,
1069 .reference = reference,
1070 .size = sample->aux_sample.size,
1072 struct perf_sample_id *sid;
1073 u64 id = sample->id;
1079 sid = evlist__id2sid(session->evlist, id);
1084 buffer.tid = sid->tid;
1085 buffer.cpu = sid->cpu;
1087 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL);
1095 static int auxtrace_queue_data_cb(struct perf_session *session,
1096 union perf_event *event, u64 offset,
1099 struct queue_data *qd = data;
1100 struct perf_sample sample;
1103 if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) {
1104 if (event->header.size < sizeof(struct perf_record_auxtrace))
1106 offset += event->header.size;
1107 return session->auxtrace->queue_data(session, NULL, event,
1111 if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE)
1114 err = evlist__parse_sample(session->evlist, event, &sample);
1118 if (!sample.aux_sample.size)
1121 offset += sample.aux_sample.data - (void *)event;
1123 return session->auxtrace->queue_data(session, &sample, NULL, offset);
1126 int auxtrace_queue_data(struct perf_session *session, bool samples, bool events)
1128 struct queue_data qd = {
1133 if (auxtrace__dont_decode(session))
1136 if (perf_data__is_pipe(session->data))
1139 if (!session->auxtrace || !session->auxtrace->queue_data)
1142 return perf_session__peek_events(session, session->header.data_offset,
1143 session->header.data_size,
1144 auxtrace_queue_data_cb, &qd);
1147 void *auxtrace_buffer__get_data_rw(struct auxtrace_buffer *buffer, int fd, bool rw)
1149 int prot = rw ? PROT_READ | PROT_WRITE : PROT_READ;
1150 size_t adj = buffer->data_offset & (page_size - 1);
1151 size_t size = buffer->size + adj;
1152 off_t file_offset = buffer->data_offset - adj;
1156 return buffer->data;
1158 addr = mmap(NULL, size, prot, MAP_SHARED, fd, file_offset);
1159 if (addr == MAP_FAILED)
1162 buffer->mmap_addr = addr;
1163 buffer->mmap_size = size;
1165 buffer->data = addr + adj;
1167 return buffer->data;
1170 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
1172 if (!buffer->data || !buffer->mmap_addr)
1174 munmap(buffer->mmap_addr, buffer->mmap_size);
1175 buffer->mmap_addr = NULL;
1176 buffer->mmap_size = 0;
1177 buffer->data = NULL;
1178 buffer->use_data = NULL;
1181 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
1183 auxtrace_buffer__put_data(buffer);
1184 if (buffer->data_needs_freeing) {
1185 buffer->data_needs_freeing = false;
1186 zfree(&buffer->data);
1187 buffer->use_data = NULL;
1192 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
1194 auxtrace_buffer__drop_data(buffer);
1198 void auxtrace_synth_guest_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1199 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1200 const char *msg, u64 timestamp,
1201 pid_t machine_pid, int vcpu)
1205 memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));
1207 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
1208 auxtrace_error->type = type;
1209 auxtrace_error->code = code;
1210 auxtrace_error->cpu = cpu;
1211 auxtrace_error->pid = pid;
1212 auxtrace_error->tid = tid;
1213 auxtrace_error->fmt = 1;
1214 auxtrace_error->ip = ip;
1215 auxtrace_error->time = timestamp;
1216 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
1218 auxtrace_error->fmt = 2;
1219 auxtrace_error->machine_pid = machine_pid;
1220 auxtrace_error->vcpu = vcpu;
1221 size = sizeof(*auxtrace_error);
1223 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
1224 strlen(auxtrace_error->msg) + 1;
1226 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
1229 void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1230 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1231 const char *msg, u64 timestamp)
1233 auxtrace_synth_guest_error(auxtrace_error, type, code, cpu, pid, tid,
1234 ip, msg, timestamp, 0, -1);
1237 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
1238 struct perf_tool *tool,
1239 struct perf_session *session,
1240 perf_event__handler_t process)
1242 union perf_event *ev;
1246 pr_debug2("Synthesizing auxtrace information\n");
1247 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
1248 ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
1252 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
1253 ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
1255 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
1260 err = process(tool, ev, NULL, NULL);
1266 static void unleader_evsel(struct evlist *evlist, struct evsel *leader)
1268 struct evsel *new_leader = NULL;
1269 struct evsel *evsel;
1271 /* Find new leader for the group */
1272 evlist__for_each_entry(evlist, evsel) {
1273 if (!evsel__has_leader(evsel, leader) || evsel == leader)
1277 evsel__set_leader(evsel, new_leader);
1280 /* Update group information */
1282 zfree(&new_leader->group_name);
1283 new_leader->group_name = leader->group_name;
1284 leader->group_name = NULL;
1286 new_leader->core.nr_members = leader->core.nr_members - 1;
1287 leader->core.nr_members = 1;
1291 static void unleader_auxtrace(struct perf_session *session)
1293 struct evsel *evsel;
1295 evlist__for_each_entry(session->evlist, evsel) {
1296 if (auxtrace__evsel_is_auxtrace(session, evsel) &&
1297 evsel__is_group_leader(evsel)) {
1298 unleader_evsel(session->evlist, evsel);
1303 int perf_event__process_auxtrace_info(struct perf_session *session,
1304 union perf_event *event)
1306 enum auxtrace_type type = event->auxtrace_info.type;
1310 fprintf(stdout, " type: %u\n", type);
1313 case PERF_AUXTRACE_INTEL_PT:
1314 err = intel_pt_process_auxtrace_info(event, session);
1316 case PERF_AUXTRACE_INTEL_BTS:
1317 err = intel_bts_process_auxtrace_info(event, session);
1319 case PERF_AUXTRACE_ARM_SPE:
1320 err = arm_spe_process_auxtrace_info(event, session);
1322 case PERF_AUXTRACE_CS_ETM:
1323 err = cs_etm__process_auxtrace_info(event, session);
1325 case PERF_AUXTRACE_S390_CPUMSF:
1326 err = s390_cpumsf_process_auxtrace_info(event, session);
1328 case PERF_AUXTRACE_HISI_PTT:
1329 err = hisi_ptt_process_auxtrace_info(event, session);
1331 case PERF_AUXTRACE_UNKNOWN:
1339 unleader_auxtrace(session);
1344 s64 perf_event__process_auxtrace(struct perf_session *session,
1345 union perf_event *event)
1350 fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n",
1351 event->auxtrace.size, event->auxtrace.offset,
1352 event->auxtrace.reference, event->auxtrace.idx,
1353 event->auxtrace.tid, event->auxtrace.cpu);
1355 if (auxtrace__dont_decode(session))
1356 return event->auxtrace.size;
1358 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
1361 err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
1365 return event->auxtrace.size;
1368 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
1369 #define PERF_ITRACE_DEFAULT_PERIOD 100000
1370 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
1371 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
1372 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
1373 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
1375 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
1378 synth_opts->branches = true;
1379 synth_opts->transactions = true;
1380 synth_opts->ptwrites = true;
1381 synth_opts->pwr_events = true;
1382 synth_opts->other_events = true;
1383 synth_opts->intr_events = true;
1384 synth_opts->errors = true;
1385 synth_opts->flc = true;
1386 synth_opts->llc = true;
1387 synth_opts->tlb = true;
1388 synth_opts->mem = true;
1389 synth_opts->remote_access = true;
1392 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
1393 synth_opts->period = 1;
1394 synth_opts->calls = true;
1396 synth_opts->instructions = true;
1397 synth_opts->cycles = true;
1398 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1399 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1401 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1402 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1403 synth_opts->initial_skip = 0;
1406 static int get_flag(const char **ptr, unsigned int *flags)
1411 if (c >= 'a' && c <= 'z') {
1412 *flags |= 1 << (c - 'a');
1415 } else if (c == ' ') {
1424 static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *minus_flags)
1430 if (get_flag(ptr, plus_flags))
1435 if (get_flag(ptr, minus_flags))
1447 #define ITRACE_DFLT_LOG_ON_ERROR_SZ 16384
1449 static unsigned int itrace_log_on_error_size(void)
1451 unsigned int sz = 0;
1453 perf_config_scan("itrace.debug-log-buffer-size", "%u", &sz);
1454 return sz ?: ITRACE_DFLT_LOG_ON_ERROR_SZ;
1458 * Please check tools/perf/Documentation/perf-script.txt for information
1459 * about the options parsed here, which is introduced after this cset,
1460 * when support in 'perf script' for these options is introduced.
1462 int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,
1463 const char *str, int unset)
1467 bool period_type_set = false;
1468 bool period_set = false;
1470 synth_opts->set = true;
1473 synth_opts->dont_decode = true;
1478 itrace_synth_opts__set_default(synth_opts,
1479 synth_opts->default_no_sample);
1483 for (p = str; *p;) {
1488 synth_opts->cycles = true;
1490 synth_opts->instructions = true;
1491 while (*p == ' ' || *p == ',')
1494 synth_opts->period = strtoull(p, &endptr, 10);
1497 while (*p == ' ' || *p == ',')
1501 synth_opts->period_type =
1502 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1503 period_type_set = true;
1506 synth_opts->period_type =
1507 PERF_ITRACE_PERIOD_TICKS;
1508 period_type_set = true;
1511 synth_opts->period *= 1000;
1514 synth_opts->period *= 1000;
1519 synth_opts->period_type =
1520 PERF_ITRACE_PERIOD_NANOSECS;
1521 period_type_set = true;
1531 synth_opts->branches = true;
1534 synth_opts->transactions = true;
1537 synth_opts->ptwrites = true;
1540 synth_opts->pwr_events = true;
1543 synth_opts->other_events = true;
1546 synth_opts->intr_events = true;
1549 synth_opts->errors = true;
1550 if (get_flags(&p, &synth_opts->error_plus_flags,
1551 &synth_opts->error_minus_flags))
1555 synth_opts->log = true;
1556 if (get_flags(&p, &synth_opts->log_plus_flags,
1557 &synth_opts->log_minus_flags))
1559 if (synth_opts->log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR)
1560 synth_opts->log_on_error_size = itrace_log_on_error_size();
1563 synth_opts->branches = true;
1564 synth_opts->calls = true;
1567 synth_opts->branches = true;
1568 synth_opts->returns = true;
1573 synth_opts->add_callchain = true;
1575 synth_opts->callchain = true;
1576 synth_opts->callchain_sz =
1577 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1578 while (*p == ' ' || *p == ',')
1583 val = strtoul(p, &endptr, 10);
1585 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1587 synth_opts->callchain_sz = val;
1593 synth_opts->add_last_branch = true;
1595 synth_opts->last_branch = true;
1596 synth_opts->last_branch_sz =
1597 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1598 while (*p == ' ' || *p == ',')
1603 val = strtoul(p, &endptr, 10);
1606 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1608 synth_opts->last_branch_sz = val;
1612 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1618 synth_opts->flc = true;
1621 synth_opts->llc = true;
1624 synth_opts->tlb = true;
1627 synth_opts->remote_access = true;
1630 synth_opts->mem = true;
1633 synth_opts->quick += 1;
1636 synth_opts->approx_ipc = true;
1639 synth_opts->timeless_decoding = true;
1649 if (synth_opts->instructions || synth_opts->cycles) {
1650 if (!period_type_set)
1651 synth_opts->period_type =
1652 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1654 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1660 pr_err("Bad Instruction Tracing options '%s'\n", str);
1664 int itrace_parse_synth_opts(const struct option *opt, const char *str, int unset)
1666 return itrace_do_parse_synth_opts(opt->value, str, unset);
1669 static const char * const auxtrace_error_type_name[] = {
1670 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1673 static const char *auxtrace_error_name(int type)
1675 const char *error_type_name = NULL;
1677 if (type < PERF_AUXTRACE_ERROR_MAX)
1678 error_type_name = auxtrace_error_type_name[type];
1679 if (!error_type_name)
1680 error_type_name = "unknown AUX";
1681 return error_type_name;
1684 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1686 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1687 unsigned long long nsecs = e->time;
1688 const char *msg = e->msg;
1691 ret = fprintf(fp, " %s error type %u",
1692 auxtrace_error_name(e->type), e->type);
1694 if (e->fmt && nsecs) {
1695 unsigned long secs = nsecs / NSEC_PER_SEC;
1697 nsecs -= secs * NSEC_PER_SEC;
1698 ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
1700 ret += fprintf(fp, " time 0");
1704 msg = (const char *)&e->time;
1706 if (e->fmt >= 2 && e->machine_pid)
1707 ret += fprintf(fp, " machine_pid %d vcpu %d", e->machine_pid, e->vcpu);
1709 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
1710 e->cpu, e->pid, e->tid, e->ip, e->code, msg);
1714 void perf_session__auxtrace_error_inc(struct perf_session *session,
1715 union perf_event *event)
1717 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1719 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1720 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1723 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1727 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1728 if (!stats->nr_auxtrace_errors[i])
1730 ui__warning("%u %s errors\n",
1731 stats->nr_auxtrace_errors[i],
1732 auxtrace_error_name(i));
1736 int perf_event__process_auxtrace_error(struct perf_session *session,
1737 union perf_event *event)
1739 if (auxtrace__dont_decode(session))
1742 perf_event__fprintf_auxtrace_error(event, stdout);
1747 * In the compat mode kernel runs in 64-bit and perf tool runs in 32-bit mode,
1748 * 32-bit perf tool cannot access 64-bit value atomically, which might lead to
1749 * the issues caused by the below sequence on multiple CPUs: when perf tool
1750 * accesses either the load operation or the store operation for 64-bit value,
1751 * on some architectures the operation is divided into two instructions, one
1752 * is for accessing the low 32-bit value and another is for the high 32-bit;
1753 * thus these two user operations can give the kernel chances to access the
1754 * 64-bit value, and thus leads to the unexpected load values.
1756 * kernel (64-bit) user (32-bit)
1758 * if (LOAD ->aux_tail) { --, LOAD ->aux_head_lo
1759 * STORE $aux_data | ,--->
1760 * FLUSH $aux_data | | LOAD ->aux_head_hi
1761 * STORE ->aux_head --|-------` smp_rmb()
1764 * | STORE ->aux_tail_lo
1766 * STORE ->aux_tail_hi
1768 * For this reason, it's impossible for the perf tool to work correctly when
1769 * the AUX head or tail is bigger than 4GB (more than 32 bits length); and we
1770 * can not simply limit the AUX ring buffer to less than 4GB, the reason is
1771 * the pointers can be increased monotonically, whatever the buffer size it is,
1772 * at the end the head and tail can be bigger than 4GB and carry out to the
1775 * To mitigate the issues and improve the user experience, we can allow the
1776 * perf tool working in certain conditions and bail out with error if detect
1777 * any overflow cannot be handled.
1779 * For reading the AUX head, it reads out the values for three times, and
1780 * compares the high 4 bytes of the values between the first time and the last
1781 * time, if there has no change for high 4 bytes injected by the kernel during
1782 * the user reading sequence, it's safe for use the second value.
1784 * When compat_auxtrace_mmap__write_tail() detects any carrying in the high
1785 * 32 bits, it means there have two store operations in user space and it cannot
1786 * promise the atomicity for 64-bit write, so return '-1' in this case to tell
1787 * the caller an overflow error has happened.
1789 u64 __weak compat_auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
1791 struct perf_event_mmap_page *pc = mm->userpg;
1792 u64 first, second, last;
1793 u64 mask = (u64)(UINT32_MAX) << 32;
1796 first = READ_ONCE(pc->aux_head);
1797 /* Ensure all reads are done after we read the head */
1799 second = READ_ONCE(pc->aux_head);
1800 /* Ensure all reads are done after we read the head */
1802 last = READ_ONCE(pc->aux_head);
1803 } while ((first & mask) != (last & mask));
1808 int __weak compat_auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
1810 struct perf_event_mmap_page *pc = mm->userpg;
1811 u64 mask = (u64)(UINT32_MAX) << 32;
1816 /* Ensure all reads are done before we write the tail out */
1818 WRITE_ONCE(pc->aux_tail, tail);
1822 static int __auxtrace_mmap__read(struct mmap *map,
1823 struct auxtrace_record *itr,
1824 struct perf_tool *tool, process_auxtrace_t fn,
1825 bool snapshot, size_t snapshot_size)
1827 struct auxtrace_mmap *mm = &map->auxtrace_mmap;
1828 u64 head, old = mm->prev, offset, ref;
1829 unsigned char *data = mm->base;
1830 size_t size, head_off, old_off, len1, len2, padding;
1831 union perf_event ev;
1832 void *data1, *data2;
1833 int kernel_is_64_bit = perf_env__kernel_is_64_bit(evsel__env(NULL));
1835 head = auxtrace_mmap__read_head(mm, kernel_is_64_bit);
1838 auxtrace_record__find_snapshot(itr, mm->idx, mm, data, &head, &old))
1844 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1845 mm->idx, old, head, head - old);
1848 head_off = head & mm->mask;
1849 old_off = old & mm->mask;
1851 head_off = head % mm->len;
1852 old_off = old % mm->len;
1855 if (head_off > old_off)
1856 size = head_off - old_off;
1858 size = mm->len - (old_off - head_off);
1860 if (snapshot && size > snapshot_size)
1861 size = snapshot_size;
1863 ref = auxtrace_record__reference(itr);
1865 if (head > old || size <= head || mm->mask) {
1866 offset = head - size;
1869 * When the buffer size is not a power of 2, 'head' wraps at the
1870 * highest multiple of the buffer size, so we have to subtract
1871 * the remainder here.
1873 u64 rem = (0ULL - mm->len) % mm->len;
1875 offset = head - size - rem;
1878 if (size > head_off) {
1879 len1 = size - head_off;
1880 data1 = &data[mm->len - len1];
1885 data1 = &data[head_off - len1];
1890 if (itr->alignment) {
1891 unsigned int unwanted = len1 % itr->alignment;
1897 /* padding must be written by fn() e.g. record__process_auxtrace() */
1898 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1900 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1902 memset(&ev, 0, sizeof(ev));
1903 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1904 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1905 ev.auxtrace.size = size + padding;
1906 ev.auxtrace.offset = offset;
1907 ev.auxtrace.reference = ref;
1908 ev.auxtrace.idx = mm->idx;
1909 ev.auxtrace.tid = mm->tid;
1910 ev.auxtrace.cpu = mm->cpu;
1912 if (fn(tool, map, &ev, data1, len1, data2, len2))
1920 err = auxtrace_mmap__write_tail(mm, head, kernel_is_64_bit);
1924 if (itr->read_finish) {
1925 err = itr->read_finish(itr, mm->idx);
1934 int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
1935 struct perf_tool *tool, process_auxtrace_t fn)
1937 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
1940 int auxtrace_mmap__read_snapshot(struct mmap *map,
1941 struct auxtrace_record *itr,
1942 struct perf_tool *tool, process_auxtrace_t fn,
1943 size_t snapshot_size)
1945 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
1949 * struct auxtrace_cache - hash table to implement a cache
1950 * @hashtable: the hashtable
1951 * @sz: hashtable size (number of hlists)
1952 * @entry_size: size of an entry
1953 * @limit: limit the number of entries to this maximum, when reached the cache
1954 * is dropped and caching begins again with an empty cache
1955 * @cnt: current number of entries
1956 * @bits: hashtable size (@sz = 2^@bits)
1958 struct auxtrace_cache {
1959 struct hlist_head *hashtable;
1967 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1968 unsigned int limit_percent)
1970 struct auxtrace_cache *c;
1971 struct hlist_head *ht;
1974 c = zalloc(sizeof(struct auxtrace_cache));
1980 ht = calloc(sz, sizeof(struct hlist_head));
1984 for (i = 0; i < sz; i++)
1985 INIT_HLIST_HEAD(&ht[i]);
1989 c->entry_size = entry_size;
1990 c->limit = (c->sz * limit_percent) / 100;
2000 static void auxtrace_cache__drop(struct auxtrace_cache *c)
2002 struct auxtrace_cache_entry *entry;
2003 struct hlist_node *tmp;
2009 for (i = 0; i < c->sz; i++) {
2010 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
2011 hlist_del(&entry->hash);
2012 auxtrace_cache__free_entry(c, entry);
2019 void auxtrace_cache__free(struct auxtrace_cache *c)
2024 auxtrace_cache__drop(c);
2025 zfree(&c->hashtable);
2029 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
2031 return malloc(c->entry_size);
2034 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
2040 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
2041 struct auxtrace_cache_entry *entry)
2043 if (c->limit && ++c->cnt > c->limit)
2044 auxtrace_cache__drop(c);
2047 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
2052 static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c,
2055 struct auxtrace_cache_entry *entry;
2056 struct hlist_head *hlist;
2057 struct hlist_node *n;
2062 hlist = &c->hashtable[hash_32(key, c->bits)];
2063 hlist_for_each_entry_safe(entry, n, hlist, hash) {
2064 if (entry->key == key) {
2065 hlist_del(&entry->hash);
2073 void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key)
2075 struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key);
2077 auxtrace_cache__free_entry(c, entry);
2080 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
2082 struct auxtrace_cache_entry *entry;
2083 struct hlist_head *hlist;
2088 hlist = &c->hashtable[hash_32(key, c->bits)];
2089 hlist_for_each_entry(entry, hlist, hash) {
2090 if (entry->key == key)
2097 static void addr_filter__free_str(struct addr_filter *filt)
2100 filt->action = NULL;
2101 filt->sym_from = NULL;
2102 filt->sym_to = NULL;
2103 filt->filename = NULL;
2106 static struct addr_filter *addr_filter__new(void)
2108 struct addr_filter *filt = zalloc(sizeof(*filt));
2111 INIT_LIST_HEAD(&filt->list);
2116 static void addr_filter__free(struct addr_filter *filt)
2119 addr_filter__free_str(filt);
2123 static void addr_filters__add(struct addr_filters *filts,
2124 struct addr_filter *filt)
2126 list_add_tail(&filt->list, &filts->head);
2130 static void addr_filters__del(struct addr_filters *filts,
2131 struct addr_filter *filt)
2133 list_del_init(&filt->list);
2137 void addr_filters__init(struct addr_filters *filts)
2139 INIT_LIST_HEAD(&filts->head);
2143 void addr_filters__exit(struct addr_filters *filts)
2145 struct addr_filter *filt, *n;
2147 list_for_each_entry_safe(filt, n, &filts->head, list) {
2148 addr_filters__del(filts, filt);
2149 addr_filter__free(filt);
2153 static int parse_num_or_str(char **inp, u64 *num, const char **str,
2154 const char *str_delim)
2156 *inp += strspn(*inp, " ");
2158 if (isdigit(**inp)) {
2164 *num = strtoull(*inp, &endptr, 0);
2175 *inp += strspn(*inp, " ");
2177 n = strcspn(*inp, str_delim);
2189 static int parse_action(struct addr_filter *filt)
2191 if (!strcmp(filt->action, "filter")) {
2194 } else if (!strcmp(filt->action, "start")) {
2196 } else if (!strcmp(filt->action, "stop")) {
2197 filt->start = false;
2198 } else if (!strcmp(filt->action, "tracestop")) {
2199 filt->start = false;
2201 filt->action += 5; /* Change 'tracestop' to 'stop' */
2208 static int parse_sym_idx(char **inp, int *idx)
2212 *inp += strspn(*inp, " ");
2219 if (**inp == 'g' || **inp == 'G') {
2227 num = strtoul(*inp, &endptr, 0);
2230 if (endptr == *inp || num > INT_MAX)
2239 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
2241 int err = parse_num_or_str(inp, num, str, " ");
2244 err = parse_sym_idx(inp, idx);
2249 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
2254 filt->str = fstr = strdup(*filter_inp);
2258 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
2262 err = parse_action(filt);
2266 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
2267 &filt->sym_from_idx);
2271 fstr += strspn(fstr, " ");
2275 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
2282 fstr += strspn(fstr, " ");
2286 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
2291 fstr += strspn(fstr, " ,");
2293 *filter_inp += fstr - filt->str;
2298 addr_filter__free_str(filt);
2303 int addr_filters__parse_bare_filter(struct addr_filters *filts,
2306 struct addr_filter *filt;
2307 const char *fstr = filter;
2311 filt = addr_filter__new();
2312 err = parse_one_filter(filt, &fstr);
2314 addr_filter__free(filt);
2315 addr_filters__exit(filts);
2318 addr_filters__add(filts, filt);
2337 static bool kern_sym_name_match(const char *kname, const char *name)
2339 size_t n = strlen(name);
2341 return !strcmp(kname, name) ||
2342 (!strncmp(kname, name, n) && kname[n] == '\t');
2345 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
2347 /* A function with the same name, and global or the n'th found or any */
2348 return kallsyms__is_function(type) &&
2349 kern_sym_name_match(name, args->name) &&
2350 ((args->global && isupper(type)) ||
2351 (args->selected && ++(args->cnt) == args->idx) ||
2352 (!args->global && !args->selected));
2355 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2357 struct sym_args *args = arg;
2359 if (args->started) {
2361 args->size = start - args->start;
2362 if (args->selected) {
2365 } else if (kern_sym_match(args, name, type)) {
2366 args->duplicate = true;
2369 } else if (kern_sym_match(args, name, type)) {
2370 args->started = true;
2371 args->start = start;
2377 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2379 struct sym_args *args = arg;
2381 if (kern_sym_match(args, name, type)) {
2382 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2383 ++args->cnt, start, type, name);
2385 } else if (args->near) {
2387 pr_err("\t\twhich is near\t\t%s\n", name);
2393 static int sym_not_found_error(const char *sym_name, int idx)
2396 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
2399 pr_err("Global symbol '%s' not found.\n", sym_name);
2401 pr_err("Symbol '%s' not found.\n", sym_name);
2403 pr_err("Note that symbols must be functions.\n");
2408 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
2410 struct sym_args args = {
2414 .selected = idx > 0,
2421 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
2423 pr_err("Failed to parse /proc/kallsyms\n");
2427 if (args.duplicate) {
2428 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
2430 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
2431 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2433 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2437 if (!args.started) {
2438 pr_err("Kernel symbol lookup: ");
2439 return sym_not_found_error(sym_name, idx);
2442 *start = args.start;
2448 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
2449 char type, u64 start)
2451 struct sym_args *args = arg;
2454 if (!kallsyms__is_function(type))
2457 if (!args->started) {
2458 args->started = true;
2459 args->start = start;
2461 /* Don't know exactly where the kernel ends, so we add a page */
2462 size = round_up(start, page_size) + page_size - args->start;
2463 if (size > args->size)
2469 static int addr_filter__entire_kernel(struct addr_filter *filt)
2471 struct sym_args args = { .started = false };
2474 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
2475 if (err < 0 || !args.started) {
2476 pr_err("Failed to parse /proc/kallsyms\n");
2480 filt->addr = args.start;
2481 filt->size = args.size;
2486 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
2488 if (start + size >= filt->addr)
2491 if (filt->sym_from) {
2492 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
2493 filt->sym_to, start, filt->sym_from, filt->addr);
2495 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
2496 filt->sym_to, start, filt->addr);
2502 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
2504 bool no_size = false;
2508 if (symbol_conf.kptr_restrict) {
2509 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
2513 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
2514 return addr_filter__entire_kernel(filt);
2516 if (filt->sym_from) {
2517 err = find_kern_sym(filt->sym_from, &start, &size,
2518 filt->sym_from_idx);
2522 if (filt->range && !filt->size && !filt->sym_to) {
2529 err = find_kern_sym(filt->sym_to, &start, &size,
2534 err = check_end_after_start(filt, start, size);
2537 filt->size = start + size - filt->addr;
2541 /* The very last symbol in kallsyms does not imply a particular size */
2543 pr_err("Cannot determine size of symbol '%s'\n",
2544 filt->sym_to ? filt->sym_to : filt->sym_from);
2551 static struct dso *load_dso(const char *name)
2556 map = dso__new_map(name);
2560 if (map__load(map) < 0)
2561 pr_err("File '%s' not found or has no symbols.\n", name);
2563 dso = dso__get(map__dso(map));
2570 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
2573 /* Same name, and global or the n'th found or any */
2574 return !arch__compare_symbol_names(name, sym->name) &&
2575 ((!idx && sym->binding == STB_GLOBAL) ||
2576 (idx > 0 && ++*cnt == idx) ||
2580 static void print_duplicate_syms(struct dso *dso, const char *sym_name)
2586 pr_err("Multiple symbols with name '%s'\n", sym_name);
2588 sym = dso__first_symbol(dso);
2590 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
2591 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2593 sym->binding == STB_GLOBAL ? 'g' :
2594 sym->binding == STB_LOCAL ? 'l' : 'w',
2599 pr_err("\t\twhich is near\t\t%s\n", sym->name);
2601 sym = dso__next_symbol(sym);
2604 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2606 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2609 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
2618 sym = dso__first_symbol(dso);
2622 *size = sym->start - *start;
2626 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2627 print_duplicate_syms(dso, sym_name);
2630 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2631 *start = sym->start;
2632 *size = sym->end - sym->start;
2634 sym = dso__next_symbol(sym);
2638 return sym_not_found_error(sym_name, idx);
2643 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
2645 if (dso__data_file_size(dso, NULL)) {
2646 pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
2652 filt->size = dso->data.file_size;
2657 static int addr_filter__resolve_syms(struct addr_filter *filt)
2663 if (!filt->sym_from && !filt->sym_to)
2666 if (!filt->filename)
2667 return addr_filter__resolve_kernel_syms(filt);
2669 dso = load_dso(filt->filename);
2671 pr_err("Failed to load symbols from: %s\n", filt->filename);
2675 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2676 err = addr_filter__entire_dso(filt, dso);
2680 if (filt->sym_from) {
2681 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2682 filt->sym_from_idx);
2686 if (filt->range && !filt->size && !filt->sym_to)
2691 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2696 err = check_end_after_start(filt, start, size);
2700 filt->size = start + size - filt->addr;
2709 static char *addr_filter__to_str(struct addr_filter *filt)
2711 char filename_buf[PATH_MAX];
2712 const char *at = "";
2713 const char *fn = "";
2717 if (filt->filename) {
2719 fn = realpath(filt->filename, filename_buf);
2725 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2726 filt->action, filt->addr, filt->size, at, fn);
2728 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2729 filt->action, filt->addr, at, fn);
2732 return err < 0 ? NULL : filter;
2735 static int parse_addr_filter(struct evsel *evsel, const char *filter,
2738 struct addr_filters filts;
2739 struct addr_filter *filt;
2742 addr_filters__init(&filts);
2744 err = addr_filters__parse_bare_filter(&filts, filter);
2748 if (filts.cnt > max_nr) {
2749 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2755 list_for_each_entry(filt, &filts.head, list) {
2758 err = addr_filter__resolve_syms(filt);
2762 new_filter = addr_filter__to_str(filt);
2768 if (evsel__append_addr_filter(evsel, new_filter)) {
2775 addr_filters__exit(&filts);
2778 pr_err("Failed to parse address filter: '%s'\n", filter);
2779 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2780 pr_err("Where multiple filters are separated by space or comma.\n");
2786 static int evsel__nr_addr_filter(struct evsel *evsel)
2788 struct perf_pmu *pmu = evsel__find_pmu(evsel);
2789 int nr_addr_filters = 0;
2794 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2796 return nr_addr_filters;
2799 int auxtrace_parse_filters(struct evlist *evlist)
2801 struct evsel *evsel;
2805 evlist__for_each_entry(evlist, evsel) {
2806 filter = evsel->filter;
2807 max_nr = evsel__nr_addr_filter(evsel);
2808 if (!filter || !max_nr)
2810 evsel->filter = NULL;
2811 err = parse_addr_filter(evsel, filter, max_nr);
2815 pr_debug("Address filter: %s\n", evsel->filter);
2821 int auxtrace__process_event(struct perf_session *session, union perf_event *event,
2822 struct perf_sample *sample, struct perf_tool *tool)
2824 if (!session->auxtrace)
2827 return session->auxtrace->process_event(session, event, sample, tool);
2830 void auxtrace__dump_auxtrace_sample(struct perf_session *session,
2831 struct perf_sample *sample)
2833 if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample ||
2834 auxtrace__dont_decode(session))
2837 session->auxtrace->dump_auxtrace_sample(session, sample);
2840 int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
2842 if (!session->auxtrace)
2845 return session->auxtrace->flush_events(session, tool);
2848 void auxtrace__free_events(struct perf_session *session)
2850 if (!session->auxtrace)
2853 return session->auxtrace->free_events(session);
2856 void auxtrace__free(struct perf_session *session)
2858 if (!session->auxtrace)
2861 return session->auxtrace->free(session);
2864 bool auxtrace__evsel_is_auxtrace(struct perf_session *session,
2865 struct evsel *evsel)
2867 if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace)
2870 return session->auxtrace->evsel_is_auxtrace(session, evsel);