2 * auxtrace.h: 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 #ifndef __PERF_AUXTRACE_H
17 #define __PERF_AUXTRACE_H
19 #include <sys/types.h>
23 #include <linux/list.h>
24 #include <linux/perf_event.h>
25 #include <linux/types.h>
26 #include <asm/bitsperlong.h>
40 struct auxtrace_info_event
;
43 /* Auxtrace records must have the same alignment as perf event records */
44 #define PERF_AUXTRACE_RECORD_ALIGNMENT 8
47 PERF_AUXTRACE_UNKNOWN
,
48 PERF_AUXTRACE_INTEL_PT
,
49 PERF_AUXTRACE_INTEL_BTS
,
51 PERF_AUXTRACE_ARM_SPE
,
52 PERF_AUXTRACE_S390_CPUMSF
,
55 enum itrace_period_type
{
56 PERF_ITRACE_PERIOD_INSTRUCTIONS
,
57 PERF_ITRACE_PERIOD_TICKS
,
58 PERF_ITRACE_PERIOD_NANOSECS
,
62 * struct itrace_synth_opts - AUX area tracing synthesis options.
63 * @set: indicates whether or not options have been set
64 * @default_no_sample: Default to no sampling.
65 * @inject: indicates the event (not just the sample) must be fully synthesized
66 * because 'perf inject' will write it out
67 * @instructions: whether to synthesize 'instructions' events
68 * @branches: whether to synthesize 'branches' events
69 * @transactions: whether to synthesize events for transactions
70 * @ptwrites: whether to synthesize events for ptwrites
71 * @pwr_events: whether to synthesize power events
72 * @errors: whether to synthesize decoder error events
73 * @dont_decode: whether to skip decoding entirely
74 * @log: write a decoding log
75 * @calls: limit branch samples to calls (can be combined with @returns)
76 * @returns: limit branch samples to returns (can be combined with @calls)
77 * @callchain: add callchain to 'instructions' events
78 * @thread_stack: feed branches to the thread_stack
79 * @last_branch: add branch context to 'instruction' events
80 * @callchain_sz: maximum callchain size
81 * @last_branch_sz: branch context size
82 * @period: 'instructions' events period
83 * @period_type: 'instructions' events period type
84 * @initial_skip: skip N events at the beginning.
85 * @cpu_bitmap: CPUs for which to synthesize events, or NULL for all
87 struct itrace_synth_opts
{
89 bool default_no_sample
;
104 unsigned int callchain_sz
;
105 unsigned int last_branch_sz
;
106 unsigned long long period
;
107 enum itrace_period_type period_type
;
108 unsigned long initial_skip
;
109 unsigned long *cpu_bitmap
;
113 * struct auxtrace_index_entry - indexes a AUX area tracing event within a
115 * @file_offset: offset within the perf.data file
116 * @sz: size of the event
118 struct auxtrace_index_entry
{
123 #define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
126 * struct auxtrace_index - index of AUX area tracing events within a perf.data
128 * @list: linking a number of arrays of entries
129 * @nr: number of entries
130 * @entries: array of entries
132 struct auxtrace_index
{
133 struct list_head list
;
135 struct auxtrace_index_entry entries
[PERF_AUXTRACE_INDEX_ENTRY_COUNT
];
139 * struct auxtrace - session callbacks to allow AUX area data decoding.
140 * @process_event: lets the decoder see all session events
141 * @process_auxtrace_event: process a PERF_RECORD_AUXTRACE event
142 * @flush_events: process any remaining data
143 * @free_events: free resources associated with event processing
144 * @free: free resources associated with the session
147 int (*process_event
)(struct perf_session
*session
,
148 union perf_event
*event
,
149 struct perf_sample
*sample
,
150 struct perf_tool
*tool
);
151 int (*process_auxtrace_event
)(struct perf_session
*session
,
152 union perf_event
*event
,
153 struct perf_tool
*tool
);
154 int (*flush_events
)(struct perf_session
*session
,
155 struct perf_tool
*tool
);
156 void (*free_events
)(struct perf_session
*session
);
157 void (*free
)(struct perf_session
*session
);
161 * struct auxtrace_buffer - a buffer containing AUX area tracing data.
162 * @list: buffers are queued in a list held by struct auxtrace_queue
163 * @size: size of the buffer in bytes
164 * @pid: in per-thread mode, the pid this buffer is associated with
165 * @tid: in per-thread mode, the tid this buffer is associated with
166 * @cpu: in per-cpu mode, the cpu this buffer is associated with
167 * @data: actual buffer data (can be null if the data has not been loaded)
168 * @data_offset: file offset at which the buffer can be read
169 * @mmap_addr: mmap address at which the buffer can be read
170 * @mmap_size: size of the mmap at @mmap_addr
171 * @data_needs_freeing: @data was malloc'd so free it when it is no longer
173 * @consecutive: the original data was split up and this buffer is consecutive
174 * to the previous buffer
175 * @offset: offset as determined by aux_head / aux_tail members of struct
176 * perf_event_mmap_page
177 * @reference: an implementation-specific reference determined when the data is
179 * @buffer_nr: used to number each buffer
180 * @use_size: implementation actually only uses this number of bytes
181 * @use_data: implementation actually only uses data starting at this address
183 struct auxtrace_buffer
{
184 struct list_head list
;
193 bool data_needs_freeing
;
203 * struct auxtrace_queue - a queue of AUX area tracing data buffers.
204 * @head: head of buffer list
205 * @tid: in per-thread mode, the tid this queue is associated with
206 * @cpu: in per-cpu mode, the cpu this queue is associated with
207 * @set: %true once this queue has been dedicated to a specific thread or cpu
208 * @priv: implementation-specific data
210 struct auxtrace_queue
{
211 struct list_head head
;
219 * struct auxtrace_queues - an array of AUX area tracing queues.
220 * @queue_array: array of queues
221 * @nr_queues: number of queues
222 * @new_data: set whenever new data is queued
223 * @populated: queues have been fully populated using the auxtrace_index
224 * @next_buffer_nr: used to number each buffer
226 struct auxtrace_queues
{
227 struct auxtrace_queue
*queue_array
;
228 unsigned int nr_queues
;
235 * struct auxtrace_heap_item - element of struct auxtrace_heap.
236 * @queue_nr: queue number
237 * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
240 struct auxtrace_heap_item
{
241 unsigned int queue_nr
;
246 * struct auxtrace_heap - a heap suitable for sorting AUX area tracing queues.
247 * @heap_array: the heap
248 * @heap_cnt: the number of elements in the heap
249 * @heap_sz: maximum number of elements (grows as needed)
251 struct auxtrace_heap
{
252 struct auxtrace_heap_item
*heap_array
;
253 unsigned int heap_cnt
;
254 unsigned int heap_sz
;
258 * struct auxtrace_mmap - records an mmap of the auxtrace buffer.
259 * @base: address of mapped area
260 * @userpg: pointer to buffer's perf_event_mmap_page
261 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
262 * @len: size of mapped area
263 * @prev: previous aux_head
264 * @idx: index of this mmap
265 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
267 * @cpu: cpu number for a per-cpu mmap otherwise %-1
269 struct auxtrace_mmap
{
281 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap.
282 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
283 * @offset: file offset of mapped area
284 * @len: size of mapped area
285 * @prot: mmap memory protection
286 * @idx: index of this mmap
287 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
289 * @cpu: cpu number for a per-cpu mmap otherwise %-1
291 struct auxtrace_mmap_params
{
302 * struct auxtrace_record - callbacks for recording AUX area data.
303 * @recording_options: validate and process recording options
304 * @info_priv_size: return the size of the private data in auxtrace_info_event
305 * @info_fill: fill-in the private data in auxtrace_info_event
306 * @free: free this auxtrace record structure
307 * @snapshot_start: starting a snapshot
308 * @snapshot_finish: finishing a snapshot
309 * @find_snapshot: find data to snapshot within auxtrace mmap
310 * @parse_snapshot_options: parse snapshot options
311 * @reference: provide a 64-bit reference number for auxtrace_event
312 * @read_finish: called after reading from an auxtrace mmap
313 * @alignment: alignment (if any) for AUX area data
315 struct auxtrace_record
{
316 int (*recording_options
)(struct auxtrace_record
*itr
,
317 struct perf_evlist
*evlist
,
318 struct record_opts
*opts
);
319 size_t (*info_priv_size
)(struct auxtrace_record
*itr
,
320 struct perf_evlist
*evlist
);
321 int (*info_fill
)(struct auxtrace_record
*itr
,
322 struct perf_session
*session
,
323 struct auxtrace_info_event
*auxtrace_info
,
325 void (*free
)(struct auxtrace_record
*itr
);
326 int (*snapshot_start
)(struct auxtrace_record
*itr
);
327 int (*snapshot_finish
)(struct auxtrace_record
*itr
);
328 int (*find_snapshot
)(struct auxtrace_record
*itr
, int idx
,
329 struct auxtrace_mmap
*mm
, unsigned char *data
,
330 u64
*head
, u64
*old
);
331 int (*parse_snapshot_options
)(struct auxtrace_record
*itr
,
332 struct record_opts
*opts
,
334 u64 (*reference
)(struct auxtrace_record
*itr
);
335 int (*read_finish
)(struct auxtrace_record
*itr
, int idx
);
336 unsigned int alignment
;
340 * struct addr_filter - address filter.
342 * @range: true if it is a range filter
343 * @start: true if action is 'filter' or 'start'
344 * @action: 'filter', 'start' or 'stop' ('tracestop' is accepted but converted
346 * @sym_from: symbol name for the filter address
347 * @sym_to: symbol name that determines the filter size
348 * @sym_from_idx: selects n'th from symbols with the same name (0 means global
349 * and less than 0 means symbol must be unique)
350 * @sym_to_idx: same as @sym_from_idx but for @sym_to
351 * @addr: filter address
352 * @size: filter region size (for range filters)
353 * @filename: DSO file name or NULL for the kernel
354 * @str: allocated string that contains the other string members
357 struct list_head list
;
361 const char *sym_from
;
367 const char *filename
;
372 * struct addr_filters - list of address filters.
373 * @head: list of address filters
374 * @cnt: number of address filters
376 struct addr_filters
{
377 struct list_head head
;
381 #ifdef HAVE_AUXTRACE_SUPPORT
384 * In snapshot mode the mmapped page is read-only which makes using
385 * __sync_val_compare_and_swap() problematic. However, snapshot mode expects
386 * the buffer is not updated while the snapshot is made (e.g. Intel PT disables
387 * the event) so there is not a race anyway.
389 static inline u64
auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap
*mm
)
391 struct perf_event_mmap_page
*pc
= mm
->userpg
;
392 u64 head
= READ_ONCE(pc
->aux_head
);
394 /* Ensure all reads are done after we read the head */
399 static inline u64
auxtrace_mmap__read_head(struct auxtrace_mmap
*mm
)
401 struct perf_event_mmap_page
*pc
= mm
->userpg
;
402 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
403 u64 head
= READ_ONCE(pc
->aux_head
);
405 u64 head
= __sync_val_compare_and_swap(&pc
->aux_head
, 0, 0);
408 /* Ensure all reads are done after we read the head */
413 static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap
*mm
, u64 tail
)
415 struct perf_event_mmap_page
*pc
= mm
->userpg
;
416 #if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
420 /* Ensure all reads are done before we write the tail out */
422 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
426 old_tail
= __sync_val_compare_and_swap(&pc
->aux_tail
, 0, 0);
427 } while (!__sync_bool_compare_and_swap(&pc
->aux_tail
, old_tail
, tail
));
431 int auxtrace_mmap__mmap(struct auxtrace_mmap
*mm
,
432 struct auxtrace_mmap_params
*mp
,
433 void *userpg
, int fd
);
434 void auxtrace_mmap__munmap(struct auxtrace_mmap
*mm
);
435 void auxtrace_mmap_params__init(struct auxtrace_mmap_params
*mp
,
436 off_t auxtrace_offset
,
437 unsigned int auxtrace_pages
,
438 bool auxtrace_overwrite
);
439 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params
*mp
,
440 struct perf_evlist
*evlist
, int idx
,
443 typedef int (*process_auxtrace_t
)(struct perf_tool
*tool
,
444 struct perf_mmap
*map
,
445 union perf_event
*event
, void *data1
,
446 size_t len1
, void *data2
, size_t len2
);
448 int auxtrace_mmap__read(struct perf_mmap
*map
, struct auxtrace_record
*itr
,
449 struct perf_tool
*tool
, process_auxtrace_t fn
);
451 int auxtrace_mmap__read_snapshot(struct perf_mmap
*map
,
452 struct auxtrace_record
*itr
,
453 struct perf_tool
*tool
, process_auxtrace_t fn
,
454 size_t snapshot_size
);
456 int auxtrace_queues__init(struct auxtrace_queues
*queues
);
457 int auxtrace_queues__add_event(struct auxtrace_queues
*queues
,
458 struct perf_session
*session
,
459 union perf_event
*event
, off_t data_offset
,
460 struct auxtrace_buffer
**buffer_ptr
);
461 void auxtrace_queues__free(struct auxtrace_queues
*queues
);
462 int auxtrace_queues__process_index(struct auxtrace_queues
*queues
,
463 struct perf_session
*session
);
464 struct auxtrace_buffer
*auxtrace_buffer__next(struct auxtrace_queue
*queue
,
465 struct auxtrace_buffer
*buffer
);
466 void *auxtrace_buffer__get_data(struct auxtrace_buffer
*buffer
, int fd
);
467 void auxtrace_buffer__put_data(struct auxtrace_buffer
*buffer
);
468 void auxtrace_buffer__drop_data(struct auxtrace_buffer
*buffer
);
469 void auxtrace_buffer__free(struct auxtrace_buffer
*buffer
);
471 int auxtrace_heap__add(struct auxtrace_heap
*heap
, unsigned int queue_nr
,
473 void auxtrace_heap__pop(struct auxtrace_heap
*heap
);
474 void auxtrace_heap__free(struct auxtrace_heap
*heap
);
476 struct auxtrace_cache_entry
{
477 struct hlist_node hash
;
481 struct auxtrace_cache
*auxtrace_cache__new(unsigned int bits
, size_t entry_size
,
482 unsigned int limit_percent
);
483 void auxtrace_cache__free(struct auxtrace_cache
*auxtrace_cache
);
484 void *auxtrace_cache__alloc_entry(struct auxtrace_cache
*c
);
485 void auxtrace_cache__free_entry(struct auxtrace_cache
*c
, void *entry
);
486 int auxtrace_cache__add(struct auxtrace_cache
*c
, u32 key
,
487 struct auxtrace_cache_entry
*entry
);
488 void *auxtrace_cache__lookup(struct auxtrace_cache
*c
, u32 key
);
490 struct auxtrace_record
*auxtrace_record__init(struct perf_evlist
*evlist
,
493 int auxtrace_parse_snapshot_options(struct auxtrace_record
*itr
,
494 struct record_opts
*opts
,
496 int auxtrace_record__options(struct auxtrace_record
*itr
,
497 struct perf_evlist
*evlist
,
498 struct record_opts
*opts
);
499 size_t auxtrace_record__info_priv_size(struct auxtrace_record
*itr
,
500 struct perf_evlist
*evlist
);
501 int auxtrace_record__info_fill(struct auxtrace_record
*itr
,
502 struct perf_session
*session
,
503 struct auxtrace_info_event
*auxtrace_info
,
505 void auxtrace_record__free(struct auxtrace_record
*itr
);
506 int auxtrace_record__snapshot_start(struct auxtrace_record
*itr
);
507 int auxtrace_record__snapshot_finish(struct auxtrace_record
*itr
);
508 int auxtrace_record__find_snapshot(struct auxtrace_record
*itr
, int idx
,
509 struct auxtrace_mmap
*mm
,
510 unsigned char *data
, u64
*head
, u64
*old
);
511 u64
auxtrace_record__reference(struct auxtrace_record
*itr
);
513 int auxtrace_index__auxtrace_event(struct list_head
*head
, union perf_event
*event
,
515 int auxtrace_index__write(int fd
, struct list_head
*head
);
516 int auxtrace_index__process(int fd
, u64 size
, struct perf_session
*session
,
518 void auxtrace_index__free(struct list_head
*head
);
520 void auxtrace_synth_error(struct auxtrace_error_event
*auxtrace_error
, int type
,
521 int code
, int cpu
, pid_t pid
, pid_t tid
, u64 ip
,
522 const char *msg
, u64 timestamp
);
524 int perf_event__synthesize_auxtrace_info(struct auxtrace_record
*itr
,
525 struct perf_tool
*tool
,
526 struct perf_session
*session
,
527 perf_event__handler_t process
);
528 int perf_event__process_auxtrace_info(struct perf_session
*session
,
529 union perf_event
*event
);
530 s64
perf_event__process_auxtrace(struct perf_session
*session
,
531 union perf_event
*event
);
532 int perf_event__process_auxtrace_error(struct perf_session
*session
,
533 union perf_event
*event
);
534 int itrace_parse_synth_opts(const struct option
*opt
, const char *str
,
536 void itrace_synth_opts__set_default(struct itrace_synth_opts
*synth_opts
,
539 size_t perf_event__fprintf_auxtrace_error(union perf_event
*event
, FILE *fp
);
540 void perf_session__auxtrace_error_inc(struct perf_session
*session
,
541 union perf_event
*event
);
542 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
);
544 void addr_filters__init(struct addr_filters
*filts
);
545 void addr_filters__exit(struct addr_filters
*filts
);
546 int addr_filters__parse_bare_filter(struct addr_filters
*filts
,
548 int auxtrace_parse_filters(struct perf_evlist
*evlist
);
550 static inline int auxtrace__process_event(struct perf_session
*session
,
551 union perf_event
*event
,
552 struct perf_sample
*sample
,
553 struct perf_tool
*tool
)
555 if (!session
->auxtrace
)
558 return session
->auxtrace
->process_event(session
, event
, sample
, tool
);
561 static inline int auxtrace__flush_events(struct perf_session
*session
,
562 struct perf_tool
*tool
)
564 if (!session
->auxtrace
)
567 return session
->auxtrace
->flush_events(session
, tool
);
570 static inline void auxtrace__free_events(struct perf_session
*session
)
572 if (!session
->auxtrace
)
575 return session
->auxtrace
->free_events(session
);
578 static inline void auxtrace__free(struct perf_session
*session
)
580 if (!session
->auxtrace
)
583 return session
->auxtrace
->free(session
);
586 #define ITRACE_HELP \
587 " i: synthesize instructions events\n" \
588 " b: synthesize branches events\n" \
589 " c: synthesize branches events (calls only)\n" \
590 " r: synthesize branches events (returns only)\n" \
591 " x: synthesize transactions events\n" \
592 " w: synthesize ptwrite events\n" \
593 " p: synthesize power events\n" \
594 " e: synthesize error events\n" \
595 " d: create a debug log\n" \
596 " g[len]: synthesize a call chain (use with i or x)\n" \
597 " l[len]: synthesize last branch entries (use with i or x)\n" \
598 " sNUMBER: skip initial number of events\n" \
599 " PERIOD[ns|us|ms|i|t]: specify period to sample stream\n" \
600 " concatenate multiple options. Default is ibxwpe or cewp\n"
605 static inline struct auxtrace_record
*
606 auxtrace_record__init(struct perf_evlist
*evlist __maybe_unused
,
614 void auxtrace_record__free(struct auxtrace_record
*itr __maybe_unused
)
619 perf_event__synthesize_auxtrace_info(struct auxtrace_record
*itr __maybe_unused
,
620 struct perf_tool
*tool __maybe_unused
,
621 struct perf_session
*session __maybe_unused
,
622 perf_event__handler_t process __maybe_unused
)
628 int auxtrace_record__options(struct auxtrace_record
*itr __maybe_unused
,
629 struct perf_evlist
*evlist __maybe_unused
,
630 struct record_opts
*opts __maybe_unused
)
635 #define perf_event__process_auxtrace_info 0
636 #define perf_event__process_auxtrace 0
637 #define perf_event__process_auxtrace_error 0
640 void perf_session__auxtrace_error_inc(struct perf_session
*session
642 union perf_event
*event
648 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
654 int itrace_parse_synth_opts(const struct option
*opt __maybe_unused
,
655 const char *str __maybe_unused
,
656 int unset __maybe_unused
)
658 pr_err("AUX area tracing not supported\n");
663 int auxtrace_parse_snapshot_options(struct auxtrace_record
*itr __maybe_unused
,
664 struct record_opts
*opts __maybe_unused
,
669 pr_err("AUX area tracing not supported\n");
674 int auxtrace__process_event(struct perf_session
*session __maybe_unused
,
675 union perf_event
*event __maybe_unused
,
676 struct perf_sample
*sample __maybe_unused
,
677 struct perf_tool
*tool __maybe_unused
)
683 int auxtrace__flush_events(struct perf_session
*session __maybe_unused
,
684 struct perf_tool
*tool __maybe_unused
)
690 void auxtrace__free_events(struct perf_session
*session __maybe_unused
)
695 void auxtrace_cache__free(struct auxtrace_cache
*auxtrace_cache __maybe_unused
)
700 void auxtrace__free(struct perf_session
*session __maybe_unused
)
705 int auxtrace_index__write(int fd __maybe_unused
,
706 struct list_head
*head __maybe_unused
)
712 int auxtrace_index__process(int fd __maybe_unused
,
713 u64 size __maybe_unused
,
714 struct perf_session
*session __maybe_unused
,
715 bool needs_swap __maybe_unused
)
721 void auxtrace_index__free(struct list_head
*head __maybe_unused
)
726 int auxtrace_parse_filters(struct perf_evlist
*evlist __maybe_unused
)
731 int auxtrace_mmap__mmap(struct auxtrace_mmap
*mm
,
732 struct auxtrace_mmap_params
*mp
,
733 void *userpg
, int fd
);
734 void auxtrace_mmap__munmap(struct auxtrace_mmap
*mm
);
735 void auxtrace_mmap_params__init(struct auxtrace_mmap_params
*mp
,
736 off_t auxtrace_offset
,
737 unsigned int auxtrace_pages
,
738 bool auxtrace_overwrite
);
739 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params
*mp
,
740 struct perf_evlist
*evlist
, int idx
,
743 #define ITRACE_HELP ""