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>
22 #include <linux/list.h>
23 #include <linux/perf_event.h>
24 #include <linux/types.h>
37 struct auxtrace_info_event;
40 /* Auxtrace records must have the same alignment as perf event records */
41 #define PERF_AUXTRACE_RECORD_ALIGNMENT 8
44 PERF_AUXTRACE_UNKNOWN,
45 PERF_AUXTRACE_INTEL_PT,
46 PERF_AUXTRACE_INTEL_BTS,
50 enum itrace_period_type {
51 PERF_ITRACE_PERIOD_INSTRUCTIONS,
52 PERF_ITRACE_PERIOD_TICKS,
53 PERF_ITRACE_PERIOD_NANOSECS,
57 * struct itrace_synth_opts - AUX area tracing synthesis options.
58 * @set: indicates whether or not options have been set
59 * @inject: indicates the event (not just the sample) must be fully synthesized
60 * because 'perf inject' will write it out
61 * @instructions: whether to synthesize 'instructions' events
62 * @branches: whether to synthesize 'branches' events
63 * @transactions: whether to synthesize events for transactions
64 * @errors: whether to synthesize decoder error events
65 * @dont_decode: whether to skip decoding entirely
66 * @log: write a decoding log
67 * @calls: limit branch samples to calls (can be combined with @returns)
68 * @returns: limit branch samples to returns (can be combined with @calls)
69 * @callchain: add callchain to 'instructions' events
70 * @thread_stack: feed branches to the thread_stack
71 * @last_branch: add branch context to 'instruction' events
72 * @callchain_sz: maximum callchain size
73 * @last_branch_sz: branch context size
74 * @period: 'instructions' events period
75 * @period_type: 'instructions' events period type
76 * @initial_skip: skip N events at the beginning.
78 struct itrace_synth_opts {
92 unsigned int callchain_sz;
93 unsigned int last_branch_sz;
94 unsigned long long period;
95 enum itrace_period_type period_type;
96 unsigned long initial_skip;
100 * struct auxtrace_index_entry - indexes a AUX area tracing event within a
102 * @file_offset: offset within the perf.data file
103 * @sz: size of the event
105 struct auxtrace_index_entry {
110 #define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
113 * struct auxtrace_index - index of AUX area tracing events within a perf.data
115 * @list: linking a number of arrays of entries
116 * @nr: number of entries
117 * @entries: array of entries
119 struct auxtrace_index {
120 struct list_head list;
122 struct auxtrace_index_entry entries[PERF_AUXTRACE_INDEX_ENTRY_COUNT];
126 * struct auxtrace - session callbacks to allow AUX area data decoding.
127 * @process_event: lets the decoder see all session events
128 * @flush_events: process any remaining data
129 * @free_events: free resources associated with event processing
130 * @free: free resources associated with the session
133 int (*process_event)(struct perf_session *session,
134 union perf_event *event,
135 struct perf_sample *sample,
136 struct perf_tool *tool);
137 int (*process_auxtrace_event)(struct perf_session *session,
138 union perf_event *event,
139 struct perf_tool *tool);
140 int (*flush_events)(struct perf_session *session,
141 struct perf_tool *tool);
142 void (*free_events)(struct perf_session *session);
143 void (*free)(struct perf_session *session);
147 * struct auxtrace_buffer - a buffer containing AUX area tracing data.
148 * @list: buffers are queued in a list held by struct auxtrace_queue
149 * @size: size of the buffer in bytes
150 * @pid: in per-thread mode, the pid this buffer is associated with
151 * @tid: in per-thread mode, the tid this buffer is associated with
152 * @cpu: in per-cpu mode, the cpu this buffer is associated with
153 * @data: actual buffer data (can be null if the data has not been loaded)
154 * @data_offset: file offset at which the buffer can be read
155 * @mmap_addr: mmap address at which the buffer can be read
156 * @mmap_size: size of the mmap at @mmap_addr
157 * @data_needs_freeing: @data was malloc'd so free it when it is no longer
159 * @consecutive: the original data was split up and this buffer is consecutive
160 * to the previous buffer
161 * @offset: offset as determined by aux_head / aux_tail members of struct
162 * perf_event_mmap_page
163 * @reference: an implementation-specific reference determined when the data is
165 * @buffer_nr: used to number each buffer
166 * @use_size: implementation actually only uses this number of bytes
167 * @use_data: implementation actually only uses data starting at this address
169 struct auxtrace_buffer {
170 struct list_head list;
179 bool data_needs_freeing;
189 * struct auxtrace_queue - a queue of AUX area tracing data buffers.
190 * @head: head of buffer list
191 * @tid: in per-thread mode, the tid this queue is associated with
192 * @cpu: in per-cpu mode, the cpu this queue is associated with
193 * @set: %true once this queue has been dedicated to a specific thread or cpu
194 * @priv: implementation-specific data
196 struct auxtrace_queue {
197 struct list_head head;
205 * struct auxtrace_queues - an array of AUX area tracing queues.
206 * @queue_array: array of queues
207 * @nr_queues: number of queues
208 * @new_data: set whenever new data is queued
209 * @populated: queues have been fully populated using the auxtrace_index
210 * @next_buffer_nr: used to number each buffer
212 struct auxtrace_queues {
213 struct auxtrace_queue *queue_array;
214 unsigned int nr_queues;
221 * struct auxtrace_heap_item - element of struct auxtrace_heap.
222 * @queue_nr: queue number
223 * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
226 struct auxtrace_heap_item {
227 unsigned int queue_nr;
232 * struct auxtrace_heap - a heap suitable for sorting AUX area tracing queues.
233 * @heap_array: the heap
234 * @heap_cnt: the number of elements in the heap
235 * @heap_sz: maximum number of elements (grows as needed)
237 struct auxtrace_heap {
238 struct auxtrace_heap_item *heap_array;
239 unsigned int heap_cnt;
240 unsigned int heap_sz;
244 * struct auxtrace_mmap - records an mmap of the auxtrace buffer.
245 * @base: address of mapped area
246 * @userpg: pointer to buffer's perf_event_mmap_page
247 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
248 * @len: size of mapped area
249 * @prev: previous aux_head
250 * @idx: index of this mmap
251 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
253 * @cpu: cpu number for a per-cpu mmap otherwise %-1
255 struct auxtrace_mmap {
267 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap.
268 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
269 * @offset: file offset of mapped area
270 * @len: size of mapped area
271 * @prot: mmap memory protection
272 * @idx: index of this mmap
273 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
275 * @cpu: cpu number for a per-cpu mmap otherwise %-1
277 struct auxtrace_mmap_params {
288 * struct auxtrace_record - callbacks for recording AUX area data.
289 * @recording_options: validate and process recording options
290 * @info_priv_size: return the size of the private data in auxtrace_info_event
291 * @info_fill: fill-in the private data in auxtrace_info_event
292 * @free: free this auxtrace record structure
293 * @snapshot_start: starting a snapshot
294 * @snapshot_finish: finishing a snapshot
295 * @find_snapshot: find data to snapshot within auxtrace mmap
296 * @parse_snapshot_options: parse snapshot options
297 * @reference: provide a 64-bit reference number for auxtrace_event
298 * @read_finish: called after reading from an auxtrace mmap
300 struct auxtrace_record {
301 int (*recording_options)(struct auxtrace_record *itr,
302 struct perf_evlist *evlist,
303 struct record_opts *opts);
304 size_t (*info_priv_size)(struct auxtrace_record *itr,
305 struct perf_evlist *evlist);
306 int (*info_fill)(struct auxtrace_record *itr,
307 struct perf_session *session,
308 struct auxtrace_info_event *auxtrace_info,
310 void (*free)(struct auxtrace_record *itr);
311 int (*snapshot_start)(struct auxtrace_record *itr);
312 int (*snapshot_finish)(struct auxtrace_record *itr);
313 int (*find_snapshot)(struct auxtrace_record *itr, int idx,
314 struct auxtrace_mmap *mm, unsigned char *data,
315 u64 *head, u64 *old);
316 int (*parse_snapshot_options)(struct auxtrace_record *itr,
317 struct record_opts *opts,
319 u64 (*reference)(struct auxtrace_record *itr);
320 int (*read_finish)(struct auxtrace_record *itr, int idx);
321 unsigned int alignment;
325 * struct addr_filter - address filter.
327 * @range: true if it is a range filter
328 * @start: true if action is 'filter' or 'start'
329 * @action: 'filter', 'start' or 'stop' ('tracestop' is accepted but converted
331 * @sym_from: symbol name for the filter address
332 * @sym_to: symbol name that determines the filter size
333 * @sym_from_idx: selects n'th from symbols with the same name (0 means global
334 * and less than 0 means symbol must be unique)
335 * @sym_to_idx: same as @sym_from_idx but for @sym_to
336 * @addr: filter address
337 * @size: filter region size (for range filters)
338 * @filename: DSO file name or NULL for the kernel
339 * @str: allocated string that contains the other string members
342 struct list_head list;
346 const char *sym_from;
352 const char *filename;
357 * struct addr_filters - list of address filters.
358 * @head: list of address filters
359 * @cnt: number of address filters
361 struct addr_filters {
362 struct list_head head;
366 #ifdef HAVE_AUXTRACE_SUPPORT
369 * In snapshot mode the mmapped page is read-only which makes using
370 * __sync_val_compare_and_swap() problematic. However, snapshot mode expects
371 * the buffer is not updated while the snapshot is made (e.g. Intel PT disables
372 * the event) so there is not a race anyway.
374 static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)
376 struct perf_event_mmap_page *pc = mm->userpg;
377 u64 head = ACCESS_ONCE(pc->aux_head);
379 /* Ensure all reads are done after we read the head */
384 static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
386 struct perf_event_mmap_page *pc = mm->userpg;
387 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
388 u64 head = ACCESS_ONCE(pc->aux_head);
390 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
393 /* Ensure all reads are done after we read the head */
398 static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
400 struct perf_event_mmap_page *pc = mm->userpg;
401 #if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
405 /* Ensure all reads are done before we write the tail out */
407 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
411 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0);
412 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail));
416 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
417 struct auxtrace_mmap_params *mp,
418 void *userpg, int fd);
419 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
420 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
421 off_t auxtrace_offset,
422 unsigned int auxtrace_pages,
423 bool auxtrace_overwrite);
424 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
425 struct perf_evlist *evlist, int idx,
428 typedef int (*process_auxtrace_t)(struct perf_tool *tool,
429 union perf_event *event, void *data1,
430 size_t len1, void *data2, size_t len2);
432 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
433 struct perf_tool *tool, process_auxtrace_t fn);
435 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
436 struct auxtrace_record *itr,
437 struct perf_tool *tool, process_auxtrace_t fn,
438 size_t snapshot_size);
440 int auxtrace_queues__init(struct auxtrace_queues *queues);
441 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
442 struct perf_session *session,
443 union perf_event *event, off_t data_offset,
444 struct auxtrace_buffer **buffer_ptr);
445 void auxtrace_queues__free(struct auxtrace_queues *queues);
446 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
447 struct perf_session *session);
448 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
449 struct auxtrace_buffer *buffer);
450 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd);
451 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer);
452 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer);
453 void auxtrace_buffer__free(struct auxtrace_buffer *buffer);
455 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
457 void auxtrace_heap__pop(struct auxtrace_heap *heap);
458 void auxtrace_heap__free(struct auxtrace_heap *heap);
460 struct auxtrace_cache_entry {
461 struct hlist_node hash;
465 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
466 unsigned int limit_percent);
467 void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache);
468 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c);
469 void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry);
470 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
471 struct auxtrace_cache_entry *entry);
472 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key);
474 struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist,
477 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
478 struct record_opts *opts,
480 int auxtrace_record__options(struct auxtrace_record *itr,
481 struct perf_evlist *evlist,
482 struct record_opts *opts);
483 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
484 struct perf_evlist *evlist);
485 int auxtrace_record__info_fill(struct auxtrace_record *itr,
486 struct perf_session *session,
487 struct auxtrace_info_event *auxtrace_info,
489 void auxtrace_record__free(struct auxtrace_record *itr);
490 int auxtrace_record__snapshot_start(struct auxtrace_record *itr);
491 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr);
492 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
493 struct auxtrace_mmap *mm,
494 unsigned char *data, u64 *head, u64 *old);
495 u64 auxtrace_record__reference(struct auxtrace_record *itr);
497 int auxtrace_index__auxtrace_event(struct list_head *head, union perf_event *event,
499 int auxtrace_index__write(int fd, struct list_head *head);
500 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
502 void auxtrace_index__free(struct list_head *head);
504 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
505 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
508 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
509 struct perf_tool *tool,
510 struct perf_session *session,
511 perf_event__handler_t process);
512 int perf_event__process_auxtrace_info(struct perf_tool *tool,
513 union perf_event *event,
514 struct perf_session *session);
515 s64 perf_event__process_auxtrace(struct perf_tool *tool,
516 union perf_event *event,
517 struct perf_session *session);
518 int perf_event__process_auxtrace_error(struct perf_tool *tool,
519 union perf_event *event,
520 struct perf_session *session);
521 int itrace_parse_synth_opts(const struct option *opt, const char *str,
523 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
525 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp);
526 void perf_session__auxtrace_error_inc(struct perf_session *session,
527 union perf_event *event);
528 void events_stats__auxtrace_error_warn(const struct events_stats *stats);
530 void addr_filters__init(struct addr_filters *filts);
531 void addr_filters__exit(struct addr_filters *filts);
532 int addr_filters__parse_bare_filter(struct addr_filters *filts,
534 int auxtrace_parse_filters(struct perf_evlist *evlist);
536 static inline int auxtrace__process_event(struct perf_session *session,
537 union perf_event *event,
538 struct perf_sample *sample,
539 struct perf_tool *tool)
541 if (!session->auxtrace)
544 return session->auxtrace->process_event(session, event, sample, tool);
547 static inline int auxtrace__flush_events(struct perf_session *session,
548 struct perf_tool *tool)
550 if (!session->auxtrace)
553 return session->auxtrace->flush_events(session, tool);
556 static inline void auxtrace__free_events(struct perf_session *session)
558 if (!session->auxtrace)
561 return session->auxtrace->free_events(session);
564 static inline void auxtrace__free(struct perf_session *session)
566 if (!session->auxtrace)
569 return session->auxtrace->free(session);
574 static inline struct auxtrace_record *
575 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused,
583 void auxtrace_record__free(struct auxtrace_record *itr __maybe_unused)
588 perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr __maybe_unused,
589 struct perf_tool *tool __maybe_unused,
590 struct perf_session *session __maybe_unused,
591 perf_event__handler_t process __maybe_unused)
597 int auxtrace_record__options(struct auxtrace_record *itr __maybe_unused,
598 struct perf_evlist *evlist __maybe_unused,
599 struct record_opts *opts __maybe_unused)
604 #define perf_event__process_auxtrace_info 0
605 #define perf_event__process_auxtrace 0
606 #define perf_event__process_auxtrace_error 0
609 void perf_session__auxtrace_error_inc(struct perf_session *session
611 union perf_event *event
617 void events_stats__auxtrace_error_warn(const struct events_stats *stats
623 int itrace_parse_synth_opts(const struct option *opt __maybe_unused,
624 const char *str __maybe_unused,
625 int unset __maybe_unused)
627 pr_err("AUX area tracing not supported\n");
632 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
633 struct record_opts *opts __maybe_unused,
638 pr_err("AUX area tracing not supported\n");
643 int auxtrace__process_event(struct perf_session *session __maybe_unused,
644 union perf_event *event __maybe_unused,
645 struct perf_sample *sample __maybe_unused,
646 struct perf_tool *tool __maybe_unused)
652 int auxtrace__flush_events(struct perf_session *session __maybe_unused,
653 struct perf_tool *tool __maybe_unused)
659 void auxtrace__free_events(struct perf_session *session __maybe_unused)
664 void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache __maybe_unused)
669 void auxtrace__free(struct perf_session *session __maybe_unused)
674 int auxtrace_index__write(int fd __maybe_unused,
675 struct list_head *head __maybe_unused)
681 int auxtrace_index__process(int fd __maybe_unused,
682 u64 size __maybe_unused,
683 struct perf_session *session __maybe_unused,
684 bool needs_swap __maybe_unused)
690 void auxtrace_index__free(struct list_head *head __maybe_unused)
695 int auxtrace_parse_filters(struct perf_evlist *evlist __maybe_unused)
700 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
701 struct auxtrace_mmap_params *mp,
702 void *userpg, int fd);
703 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
704 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
705 off_t auxtrace_offset,
706 unsigned int auxtrace_pages,
707 bool auxtrace_overwrite);
708 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
709 struct perf_evlist *evlist, int idx,