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
= evlist
->threads
->map
[0];
127 mp
->tid
= evlist
->threads
->map
[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 void auxtrace_queues__free(struct auxtrace_queues
*queues
)
351 for (i
= 0; i
< queues
->nr_queues
; i
++) {
352 while (!list_empty(&queues
->queue_array
[i
].head
)) {
353 struct auxtrace_buffer
*buffer
;
355 buffer
= list_entry(queues
->queue_array
[i
].head
.next
,
356 struct auxtrace_buffer
, list
);
357 list_del(&buffer
->list
);
358 auxtrace_buffer__free(buffer
);
362 zfree(&queues
->queue_array
);
363 queues
->nr_queues
= 0;
366 static void auxtrace_heapify(struct auxtrace_heap_item
*heap_array
,
367 unsigned int pos
, unsigned int queue_nr
,
373 parent
= (pos
- 1) >> 1;
374 if (heap_array
[parent
].ordinal
<= ordinal
)
376 heap_array
[pos
] = heap_array
[parent
];
379 heap_array
[pos
].queue_nr
= queue_nr
;
380 heap_array
[pos
].ordinal
= ordinal
;
383 int auxtrace_heap__add(struct auxtrace_heap
*heap
, unsigned int queue_nr
,
386 struct auxtrace_heap_item
*heap_array
;
388 if (queue_nr
>= heap
->heap_sz
) {
389 unsigned int heap_sz
= AUXTRACE_INIT_NR_QUEUES
;
391 while (heap_sz
<= queue_nr
)
393 heap_array
= realloc(heap
->heap_array
,
394 heap_sz
* sizeof(struct auxtrace_heap_item
));
397 heap
->heap_array
= heap_array
;
398 heap
->heap_sz
= heap_sz
;
401 auxtrace_heapify(heap
->heap_array
, heap
->heap_cnt
++, queue_nr
, ordinal
);
406 void auxtrace_heap__free(struct auxtrace_heap
*heap
)
408 zfree(&heap
->heap_array
);
413 void auxtrace_heap__pop(struct auxtrace_heap
*heap
)
415 unsigned int pos
, last
, heap_cnt
= heap
->heap_cnt
;
416 struct auxtrace_heap_item
*heap_array
;
423 heap_array
= heap
->heap_array
;
427 unsigned int left
, right
;
429 left
= (pos
<< 1) + 1;
430 if (left
>= heap_cnt
)
433 if (right
>= heap_cnt
) {
434 heap_array
[pos
] = heap_array
[left
];
437 if (heap_array
[left
].ordinal
< heap_array
[right
].ordinal
) {
438 heap_array
[pos
] = heap_array
[left
];
441 heap_array
[pos
] = heap_array
[right
];
447 auxtrace_heapify(heap_array
, pos
, heap_array
[last
].queue_nr
,
448 heap_array
[last
].ordinal
);
451 size_t auxtrace_record__info_priv_size(struct auxtrace_record
*itr
)
454 return itr
->info_priv_size(itr
);
458 static int auxtrace_not_supported(void)
460 pr_err("AUX area tracing is not supported on this architecture\n");
464 int auxtrace_record__info_fill(struct auxtrace_record
*itr
,
465 struct perf_session
*session
,
466 struct auxtrace_info_event
*auxtrace_info
,
470 return itr
->info_fill(itr
, session
, auxtrace_info
, priv_size
);
471 return auxtrace_not_supported();
474 void auxtrace_record__free(struct auxtrace_record
*itr
)
480 int auxtrace_record__options(struct auxtrace_record
*itr
,
481 struct perf_evlist
*evlist
,
482 struct record_opts
*opts
)
485 return itr
->recording_options(itr
, evlist
, opts
);
489 u64
auxtrace_record__reference(struct auxtrace_record
*itr
)
492 return itr
->reference(itr
);
496 struct auxtrace_record
*__weak
497 auxtrace_record__init(struct perf_evlist
*evlist __maybe_unused
, int *err
)
503 struct auxtrace_buffer
*auxtrace_buffer__next(struct auxtrace_queue
*queue
,
504 struct auxtrace_buffer
*buffer
)
507 if (list_is_last(&buffer
->list
, &queue
->head
))
509 return list_entry(buffer
->list
.next
, struct auxtrace_buffer
,
512 if (list_empty(&queue
->head
))
514 return list_entry(queue
->head
.next
, struct auxtrace_buffer
,
519 void *auxtrace_buffer__get_data(struct auxtrace_buffer
*buffer
, int fd
)
521 size_t adj
= buffer
->data_offset
& (page_size
- 1);
522 size_t size
= buffer
->size
+ adj
;
523 off_t file_offset
= buffer
->data_offset
- adj
;
529 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, file_offset
);
530 if (addr
== MAP_FAILED
)
533 buffer
->mmap_addr
= addr
;
534 buffer
->mmap_size
= size
;
536 buffer
->data
= addr
+ adj
;
541 void auxtrace_buffer__put_data(struct auxtrace_buffer
*buffer
)
543 if (!buffer
->data
|| !buffer
->mmap_addr
)
545 munmap(buffer
->mmap_addr
, buffer
->mmap_size
);
546 buffer
->mmap_addr
= NULL
;
547 buffer
->mmap_size
= 0;
549 buffer
->use_data
= NULL
;
552 void auxtrace_buffer__drop_data(struct auxtrace_buffer
*buffer
)
554 auxtrace_buffer__put_data(buffer
);
555 if (buffer
->data_needs_freeing
) {
556 buffer
->data_needs_freeing
= false;
557 zfree(&buffer
->data
);
558 buffer
->use_data
= NULL
;
563 void auxtrace_buffer__free(struct auxtrace_buffer
*buffer
)
565 auxtrace_buffer__drop_data(buffer
);
569 void auxtrace_synth_error(struct auxtrace_error_event
*auxtrace_error
, int type
,
570 int code
, int cpu
, pid_t pid
, pid_t tid
, u64 ip
,
575 memset(auxtrace_error
, 0, sizeof(struct auxtrace_error_event
));
577 auxtrace_error
->header
.type
= PERF_RECORD_AUXTRACE_ERROR
;
578 auxtrace_error
->type
= type
;
579 auxtrace_error
->code
= code
;
580 auxtrace_error
->cpu
= cpu
;
581 auxtrace_error
->pid
= pid
;
582 auxtrace_error
->tid
= tid
;
583 auxtrace_error
->ip
= ip
;
584 strlcpy(auxtrace_error
->msg
, msg
, MAX_AUXTRACE_ERROR_MSG
);
586 size
= (void *)auxtrace_error
->msg
- (void *)auxtrace_error
+
587 strlen(auxtrace_error
->msg
) + 1;
588 auxtrace_error
->header
.size
= PERF_ALIGN(size
, sizeof(u64
));
591 int perf_event__synthesize_auxtrace_info(struct auxtrace_record
*itr
,
592 struct perf_tool
*tool
,
593 struct perf_session
*session
,
594 perf_event__handler_t process
)
596 union perf_event
*ev
;
600 pr_debug2("Synthesizing auxtrace information\n");
601 priv_size
= auxtrace_record__info_priv_size(itr
);
602 ev
= zalloc(sizeof(struct auxtrace_info_event
) + priv_size
);
606 ev
->auxtrace_info
.header
.type
= PERF_RECORD_AUXTRACE_INFO
;
607 ev
->auxtrace_info
.header
.size
= sizeof(struct auxtrace_info_event
) +
609 err
= auxtrace_record__info_fill(itr
, session
, &ev
->auxtrace_info
,
614 err
= process(tool
, ev
, NULL
, NULL
);
620 static bool auxtrace__dont_decode(struct perf_session
*session
)
622 return !session
->itrace_synth_opts
||
623 session
->itrace_synth_opts
->dont_decode
;
626 int perf_event__process_auxtrace_info(struct perf_tool
*tool __maybe_unused
,
627 union perf_event
*event
,
628 struct perf_session
*session __maybe_unused
)
630 enum auxtrace_type type
= event
->auxtrace_info
.type
;
633 fprintf(stdout
, " type: %u\n", type
);
636 case PERF_AUXTRACE_UNKNOWN
:
642 s64
perf_event__process_auxtrace(struct perf_tool
*tool
,
643 union perf_event
*event
,
644 struct perf_session
*session
)
649 fprintf(stdout
, " size: %#"PRIx64
" offset: %#"PRIx64
" ref: %#"PRIx64
" idx: %u tid: %d cpu: %d\n",
650 event
->auxtrace
.size
, event
->auxtrace
.offset
,
651 event
->auxtrace
.reference
, event
->auxtrace
.idx
,
652 event
->auxtrace
.tid
, event
->auxtrace
.cpu
);
654 if (auxtrace__dont_decode(session
))
655 return event
->auxtrace
.size
;
657 if (!session
->auxtrace
|| event
->header
.type
!= PERF_RECORD_AUXTRACE
)
660 err
= session
->auxtrace
->process_auxtrace_event(session
, event
, tool
);
664 return event
->auxtrace
.size
;
667 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
668 #define PERF_ITRACE_DEFAULT_PERIOD 100000
669 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
670 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
672 void itrace_synth_opts__set_default(struct itrace_synth_opts
*synth_opts
)
674 synth_opts
->instructions
= true;
675 synth_opts
->branches
= true;
676 synth_opts
->errors
= true;
677 synth_opts
->period_type
= PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
678 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
679 synth_opts
->callchain_sz
= PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
683 * Please check tools/perf/Documentation/perf-script.txt for information
684 * about the options parsed here, which is introduced after this cset,
685 * when support in 'perf script' for these options is introduced.
687 int itrace_parse_synth_opts(const struct option
*opt
, const char *str
,
690 struct itrace_synth_opts
*synth_opts
= opt
->value
;
694 synth_opts
->set
= true;
697 synth_opts
->dont_decode
= true;
702 itrace_synth_opts__set_default(synth_opts
);
709 synth_opts
->instructions
= true;
710 while (*p
== ' ' || *p
== ',')
713 synth_opts
->period
= strtoull(p
, &endptr
, 10);
715 while (*p
== ' ' || *p
== ',')
719 synth_opts
->period_type
=
720 PERF_ITRACE_PERIOD_INSTRUCTIONS
;
723 synth_opts
->period_type
=
724 PERF_ITRACE_PERIOD_TICKS
;
727 synth_opts
->period
*= 1000;
730 synth_opts
->period
*= 1000;
735 synth_opts
->period_type
=
736 PERF_ITRACE_PERIOD_NANOSECS
;
746 synth_opts
->branches
= true;
749 synth_opts
->errors
= true;
752 synth_opts
->log
= true;
755 synth_opts
->branches
= true;
756 synth_opts
->calls
= true;
759 synth_opts
->branches
= true;
760 synth_opts
->returns
= true;
763 synth_opts
->instructions
= true;
764 synth_opts
->callchain
= true;
765 synth_opts
->callchain_sz
=
766 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
767 while (*p
== ' ' || *p
== ',')
772 val
= strtoul(p
, &endptr
, 10);
774 if (!val
|| val
> PERF_ITRACE_MAX_CALLCHAIN_SZ
)
776 synth_opts
->callchain_sz
= val
;
787 if (synth_opts
->instructions
) {
788 if (!synth_opts
->period_type
)
789 synth_opts
->period_type
=
790 PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
791 if (!synth_opts
->period
)
792 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
798 pr_err("Bad Instruction Tracing options '%s'\n", str
);
802 static const char * const auxtrace_error_type_name
[] = {
803 [PERF_AUXTRACE_ERROR_ITRACE
] = "instruction trace",
806 static const char *auxtrace_error_name(int type
)
808 const char *error_type_name
= NULL
;
810 if (type
< PERF_AUXTRACE_ERROR_MAX
)
811 error_type_name
= auxtrace_error_type_name
[type
];
812 if (!error_type_name
)
813 error_type_name
= "unknown AUX";
814 return error_type_name
;
817 size_t perf_event__fprintf_auxtrace_error(union perf_event
*event
, FILE *fp
)
819 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
822 ret
= fprintf(fp
, " %s error type %u",
823 auxtrace_error_name(e
->type
), e
->type
);
824 ret
+= fprintf(fp
, " cpu %d pid %d tid %d ip %#"PRIx64
" code %u: %s\n",
825 e
->cpu
, e
->pid
, e
->tid
, e
->ip
, e
->code
, e
->msg
);
829 void perf_session__auxtrace_error_inc(struct perf_session
*session
,
830 union perf_event
*event
)
832 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
834 if (e
->type
< PERF_AUXTRACE_ERROR_MAX
)
835 session
->evlist
->stats
.nr_auxtrace_errors
[e
->type
] += 1;
838 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
)
842 for (i
= 0; i
< PERF_AUXTRACE_ERROR_MAX
; i
++) {
843 if (!stats
->nr_auxtrace_errors
[i
])
845 ui__warning("%u %s errors\n",
846 stats
->nr_auxtrace_errors
[i
],
847 auxtrace_error_name(i
));
851 int perf_event__process_auxtrace_error(struct perf_tool
*tool __maybe_unused
,
852 union perf_event
*event
,
853 struct perf_session
*session
)
855 if (auxtrace__dont_decode(session
))
858 perf_event__fprintf_auxtrace_error(event
, stdout
);
862 int auxtrace_mmap__read(struct auxtrace_mmap
*mm
, struct auxtrace_record
*itr
,
863 struct perf_tool
*tool
, process_auxtrace_t fn
)
865 u64 head
= auxtrace_mmap__read_head(mm
);
866 u64 old
= mm
->prev
, offset
, ref
;
867 unsigned char *data
= mm
->base
;
868 size_t size
, head_off
, old_off
, len1
, len2
, padding
;
875 pr_debug3("auxtrace idx %d old %#"PRIx64
" head %#"PRIx64
" diff %#"PRIx64
"\n",
876 mm
->idx
, old
, head
, head
- old
);
879 head_off
= head
& mm
->mask
;
880 old_off
= old
& mm
->mask
;
882 head_off
= head
% mm
->len
;
883 old_off
= old
% mm
->len
;
886 if (head_off
> old_off
)
887 size
= head_off
- old_off
;
889 size
= mm
->len
- (old_off
- head_off
);
891 ref
= auxtrace_record__reference(itr
);
893 if (head
> old
|| size
<= head
|| mm
->mask
) {
894 offset
= head
- size
;
897 * When the buffer size is not a power of 2, 'head' wraps at the
898 * highest multiple of the buffer size, so we have to subtract
899 * the remainder here.
901 u64 rem
= (0ULL - mm
->len
) % mm
->len
;
903 offset
= head
- size
- rem
;
906 if (size
> head_off
) {
907 len1
= size
- head_off
;
908 data1
= &data
[mm
->len
- len1
];
913 data1
= &data
[head_off
- len1
];
918 /* padding must be written by fn() e.g. record__process_auxtrace() */
921 padding
= 8 - padding
;
923 memset(&ev
, 0, sizeof(ev
));
924 ev
.auxtrace
.header
.type
= PERF_RECORD_AUXTRACE
;
925 ev
.auxtrace
.header
.size
= sizeof(ev
.auxtrace
);
926 ev
.auxtrace
.size
= size
+ padding
;
927 ev
.auxtrace
.offset
= offset
;
928 ev
.auxtrace
.reference
= ref
;
929 ev
.auxtrace
.idx
= mm
->idx
;
930 ev
.auxtrace
.tid
= mm
->tid
;
931 ev
.auxtrace
.cpu
= mm
->cpu
;
933 if (fn(tool
, &ev
, data1
, len1
, data2
, len2
))
938 auxtrace_mmap__write_tail(mm
, head
);
939 if (itr
->read_finish
) {
942 err
= itr
->read_finish(itr
, mm
->idx
);
951 * struct auxtrace_cache - hash table to implement a cache
952 * @hashtable: the hashtable
953 * @sz: hashtable size (number of hlists)
954 * @entry_size: size of an entry
955 * @limit: limit the number of entries to this maximum, when reached the cache
956 * is dropped and caching begins again with an empty cache
957 * @cnt: current number of entries
958 * @bits: hashtable size (@sz = 2^@bits)
960 struct auxtrace_cache
{
961 struct hlist_head
*hashtable
;
969 struct auxtrace_cache
*auxtrace_cache__new(unsigned int bits
, size_t entry_size
,
970 unsigned int limit_percent
)
972 struct auxtrace_cache
*c
;
973 struct hlist_head
*ht
;
976 c
= zalloc(sizeof(struct auxtrace_cache
));
982 ht
= calloc(sz
, sizeof(struct hlist_head
));
986 for (i
= 0; i
< sz
; i
++)
987 INIT_HLIST_HEAD(&ht
[i
]);
991 c
->entry_size
= entry_size
;
992 c
->limit
= (c
->sz
* limit_percent
) / 100;
1002 static void auxtrace_cache__drop(struct auxtrace_cache
*c
)
1004 struct auxtrace_cache_entry
*entry
;
1005 struct hlist_node
*tmp
;
1011 for (i
= 0; i
< c
->sz
; i
++) {
1012 hlist_for_each_entry_safe(entry
, tmp
, &c
->hashtable
[i
], hash
) {
1013 hlist_del(&entry
->hash
);
1014 auxtrace_cache__free_entry(c
, entry
);
1021 void auxtrace_cache__free(struct auxtrace_cache
*c
)
1026 auxtrace_cache__drop(c
);
1031 void *auxtrace_cache__alloc_entry(struct auxtrace_cache
*c
)
1033 return malloc(c
->entry_size
);
1036 void auxtrace_cache__free_entry(struct auxtrace_cache
*c __maybe_unused
,
1042 int auxtrace_cache__add(struct auxtrace_cache
*c
, u32 key
,
1043 struct auxtrace_cache_entry
*entry
)
1045 if (c
->limit
&& ++c
->cnt
> c
->limit
)
1046 auxtrace_cache__drop(c
);
1049 hlist_add_head(&entry
->hash
, &c
->hashtable
[hash_32(key
, c
->bits
)]);
1054 void *auxtrace_cache__lookup(struct auxtrace_cache
*c
, u32 key
)
1056 struct auxtrace_cache_entry
*entry
;
1057 struct hlist_head
*hlist
;
1062 hlist
= &c
->hashtable
[hash_32(key
, c
->bits
)];
1063 hlist_for_each_entry(entry
, hlist
, hash
) {
1064 if (entry
->key
== key
)