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>
36 struct auxtrace_info_event
;
40 PERF_AUXTRACE_UNKNOWN
,
43 enum itrace_period_type
{
44 PERF_ITRACE_PERIOD_INSTRUCTIONS
,
45 PERF_ITRACE_PERIOD_TICKS
,
46 PERF_ITRACE_PERIOD_NANOSECS
,
50 * struct itrace_synth_opts - AUX area tracing synthesis options.
51 * @set: indicates whether or not options have been set
52 * @inject: indicates the event (not just the sample) must be fully synthesized
53 * because 'perf inject' will write it out
54 * @instructions: whether to synthesize 'instructions' events
55 * @branches: whether to synthesize 'branches' events
56 * @errors: whether to synthesize decoder error events
57 * @dont_decode: whether to skip decoding entirely
58 * @log: write a decoding log
59 * @calls: limit branch samples to calls (can be combined with @returns)
60 * @returns: limit branch samples to returns (can be combined with @calls)
61 * @callchain: add callchain to 'instructions' events
62 * @callchain_sz: maximum callchain size
63 * @period: 'instructions' events period
64 * @period_type: 'instructions' events period type
66 struct itrace_synth_opts
{
77 unsigned int callchain_sz
;
78 unsigned long long period
;
79 enum itrace_period_type period_type
;
83 * struct auxtrace_index_entry - indexes a AUX area tracing event within a
85 * @file_offset: offset within the perf.data file
86 * @sz: size of the event
88 struct auxtrace_index_entry
{
93 #define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
96 * struct auxtrace_index - index of AUX area tracing events within a perf.data
98 * @list: linking a number of arrays of entries
99 * @nr: number of entries
100 * @entries: array of entries
102 struct auxtrace_index
{
103 struct list_head list
;
105 struct auxtrace_index_entry entries
[PERF_AUXTRACE_INDEX_ENTRY_COUNT
];
109 * struct auxtrace - session callbacks to allow AUX area data decoding.
110 * @process_event: lets the decoder see all session events
111 * @flush_events: process any remaining data
112 * @free_events: free resources associated with event processing
113 * @free: free resources associated with the session
116 int (*process_event
)(struct perf_session
*session
,
117 union perf_event
*event
,
118 struct perf_sample
*sample
,
119 struct perf_tool
*tool
);
120 int (*process_auxtrace_event
)(struct perf_session
*session
,
121 union perf_event
*event
,
122 struct perf_tool
*tool
);
123 int (*flush_events
)(struct perf_session
*session
,
124 struct perf_tool
*tool
);
125 void (*free_events
)(struct perf_session
*session
);
126 void (*free
)(struct perf_session
*session
);
130 * struct auxtrace_buffer - a buffer containing AUX area tracing data.
131 * @list: buffers are queued in a list held by struct auxtrace_queue
132 * @size: size of the buffer in bytes
133 * @pid: in per-thread mode, the pid this buffer is associated with
134 * @tid: in per-thread mode, the tid this buffer is associated with
135 * @cpu: in per-cpu mode, the cpu this buffer is associated with
136 * @data: actual buffer data (can be null if the data has not been loaded)
137 * @data_offset: file offset at which the buffer can be read
138 * @mmap_addr: mmap address at which the buffer can be read
139 * @mmap_size: size of the mmap at @mmap_addr
140 * @data_needs_freeing: @data was malloc'd so free it when it is no longer
142 * @consecutive: the original data was split up and this buffer is consecutive
143 * to the previous buffer
144 * @offset: offset as determined by aux_head / aux_tail members of struct
145 * perf_event_mmap_page
146 * @reference: an implementation-specific reference determined when the data is
148 * @buffer_nr: used to number each buffer
149 * @use_size: implementation actually only uses this number of bytes
150 * @use_data: implementation actually only uses data starting at this address
152 struct auxtrace_buffer
{
153 struct list_head list
;
162 bool data_needs_freeing
;
172 * struct auxtrace_queue - a queue of AUX area tracing data buffers.
173 * @head: head of buffer list
174 * @tid: in per-thread mode, the tid this queue is associated with
175 * @cpu: in per-cpu mode, the cpu this queue is associated with
176 * @set: %true once this queue has been dedicated to a specific thread or cpu
177 * @priv: implementation-specific data
179 struct auxtrace_queue
{
180 struct list_head head
;
188 * struct auxtrace_queues - an array of AUX area tracing queues.
189 * @queue_array: array of queues
190 * @nr_queues: number of queues
191 * @new_data: set whenever new data is queued
192 * @populated: queues have been fully populated using the auxtrace_index
193 * @next_buffer_nr: used to number each buffer
195 struct auxtrace_queues
{
196 struct auxtrace_queue
*queue_array
;
197 unsigned int nr_queues
;
204 * struct auxtrace_heap_item - element of struct auxtrace_heap.
205 * @queue_nr: queue number
206 * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
209 struct auxtrace_heap_item
{
210 unsigned int queue_nr
;
215 * struct auxtrace_heap - a heap suitable for sorting AUX area tracing queues.
216 * @heap_array: the heap
217 * @heap_cnt: the number of elements in the heap
218 * @heap_sz: maximum number of elements (grows as needed)
220 struct auxtrace_heap
{
221 struct auxtrace_heap_item
*heap_array
;
222 unsigned int heap_cnt
;
223 unsigned int heap_sz
;
227 * struct auxtrace_mmap - records an mmap of the auxtrace buffer.
228 * @base: address of mapped area
229 * @userpg: pointer to buffer's perf_event_mmap_page
230 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
231 * @len: size of mapped area
232 * @prev: previous aux_head
233 * @idx: index of this mmap
234 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
236 * @cpu: cpu number for a per-cpu mmap otherwise %-1
238 struct auxtrace_mmap
{
250 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap.
251 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
252 * @offset: file offset of mapped area
253 * @len: size of mapped area
254 * @prot: mmap memory protection
255 * @idx: index of this mmap
256 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
258 * @cpu: cpu number for a per-cpu mmap otherwise %-1
260 struct auxtrace_mmap_params
{
271 * struct auxtrace_record - callbacks for recording AUX area data.
272 * @recording_options: validate and process recording options
273 * @info_priv_size: return the size of the private data in auxtrace_info_event
274 * @info_fill: fill-in the private data in auxtrace_info_event
275 * @free: free this auxtrace record structure
276 * @reference: provide a 64-bit reference number for auxtrace_event
277 * @read_finish: called after reading from an auxtrace mmap
279 struct auxtrace_record
{
280 int (*recording_options
)(struct auxtrace_record
*itr
,
281 struct perf_evlist
*evlist
,
282 struct record_opts
*opts
);
283 size_t (*info_priv_size
)(struct auxtrace_record
*itr
);
284 int (*info_fill
)(struct auxtrace_record
*itr
,
285 struct perf_session
*session
,
286 struct auxtrace_info_event
*auxtrace_info
,
288 void (*free
)(struct auxtrace_record
*itr
);
289 u64 (*reference
)(struct auxtrace_record
*itr
);
290 int (*read_finish
)(struct auxtrace_record
*itr
, int idx
);
293 static inline u64
auxtrace_mmap__read_head(struct auxtrace_mmap
*mm
)
295 struct perf_event_mmap_page
*pc
= mm
->userpg
;
296 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
297 u64 head
= ACCESS_ONCE(pc
->aux_head
);
299 u64 head
= __sync_val_compare_and_swap(&pc
->aux_head
, 0, 0);
302 /* Ensure all reads are done after we read the head */
307 static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap
*mm
, u64 tail
)
309 struct perf_event_mmap_page
*pc
= mm
->userpg
;
310 #if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
314 /* Ensure all reads are done before we write the tail out */
316 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
320 old_tail
= __sync_val_compare_and_swap(&pc
->aux_tail
, 0, 0);
321 } while (!__sync_bool_compare_and_swap(&pc
->aux_tail
, old_tail
, tail
));
325 int auxtrace_mmap__mmap(struct auxtrace_mmap
*mm
,
326 struct auxtrace_mmap_params
*mp
,
327 void *userpg
, int fd
);
328 void auxtrace_mmap__munmap(struct auxtrace_mmap
*mm
);
329 void auxtrace_mmap_params__init(struct auxtrace_mmap_params
*mp
,
330 off_t auxtrace_offset
,
331 unsigned int auxtrace_pages
,
332 bool auxtrace_overwrite
);
333 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params
*mp
,
334 struct perf_evlist
*evlist
, int idx
,
337 typedef int (*process_auxtrace_t
)(struct perf_tool
*tool
,
338 union perf_event
*event
, void *data1
,
339 size_t len1
, void *data2
, size_t len2
);
341 int auxtrace_mmap__read(struct auxtrace_mmap
*mm
, struct auxtrace_record
*itr
,
342 struct perf_tool
*tool
, process_auxtrace_t fn
);
344 int auxtrace_queues__init(struct auxtrace_queues
*queues
);
345 int auxtrace_queues__add_event(struct auxtrace_queues
*queues
,
346 struct perf_session
*session
,
347 union perf_event
*event
, off_t data_offset
,
348 struct auxtrace_buffer
**buffer_ptr
);
349 void auxtrace_queues__free(struct auxtrace_queues
*queues
);
350 int auxtrace_queues__process_index(struct auxtrace_queues
*queues
,
351 struct perf_session
*session
);
352 struct auxtrace_buffer
*auxtrace_buffer__next(struct auxtrace_queue
*queue
,
353 struct auxtrace_buffer
*buffer
);
354 void *auxtrace_buffer__get_data(struct auxtrace_buffer
*buffer
, int fd
);
355 void auxtrace_buffer__put_data(struct auxtrace_buffer
*buffer
);
356 void auxtrace_buffer__drop_data(struct auxtrace_buffer
*buffer
);
357 void auxtrace_buffer__free(struct auxtrace_buffer
*buffer
);
359 int auxtrace_heap__add(struct auxtrace_heap
*heap
, unsigned int queue_nr
,
361 void auxtrace_heap__pop(struct auxtrace_heap
*heap
);
362 void auxtrace_heap__free(struct auxtrace_heap
*heap
);
364 struct auxtrace_cache_entry
{
365 struct hlist_node hash
;
369 struct auxtrace_cache
*auxtrace_cache__new(unsigned int bits
, size_t entry_size
,
370 unsigned int limit_percent
);
371 void auxtrace_cache__free(struct auxtrace_cache
*auxtrace_cache
);
372 void *auxtrace_cache__alloc_entry(struct auxtrace_cache
*c
);
373 void auxtrace_cache__free_entry(struct auxtrace_cache
*c
, void *entry
);
374 int auxtrace_cache__add(struct auxtrace_cache
*c
, u32 key
,
375 struct auxtrace_cache_entry
*entry
);
376 void *auxtrace_cache__lookup(struct auxtrace_cache
*c
, u32 key
);
378 struct auxtrace_record
*auxtrace_record__init(struct perf_evlist
*evlist
,
381 int auxtrace_record__options(struct auxtrace_record
*itr
,
382 struct perf_evlist
*evlist
,
383 struct record_opts
*opts
);
384 size_t auxtrace_record__info_priv_size(struct auxtrace_record
*itr
);
385 int auxtrace_record__info_fill(struct auxtrace_record
*itr
,
386 struct perf_session
*session
,
387 struct auxtrace_info_event
*auxtrace_info
,
389 void auxtrace_record__free(struct auxtrace_record
*itr
);
390 u64
auxtrace_record__reference(struct auxtrace_record
*itr
);
392 int auxtrace_index__auxtrace_event(struct list_head
*head
, union perf_event
*event
,
394 int auxtrace_index__write(int fd
, struct list_head
*head
);
395 int auxtrace_index__process(int fd
, u64 size
, struct perf_session
*session
,
397 void auxtrace_index__free(struct list_head
*head
);
399 void auxtrace_synth_error(struct auxtrace_error_event
*auxtrace_error
, int type
,
400 int code
, int cpu
, pid_t pid
, pid_t tid
, u64 ip
,
403 int perf_event__synthesize_auxtrace_info(struct auxtrace_record
*itr
,
404 struct perf_tool
*tool
,
405 struct perf_session
*session
,
406 perf_event__handler_t process
);
407 int perf_event__process_auxtrace_info(struct perf_tool
*tool
,
408 union perf_event
*event
,
409 struct perf_session
*session
);
410 s64
perf_event__process_auxtrace(struct perf_tool
*tool
,
411 union perf_event
*event
,
412 struct perf_session
*session
);
413 int perf_event__process_auxtrace_error(struct perf_tool
*tool
,
414 union perf_event
*event
,
415 struct perf_session
*session
);
416 int itrace_parse_synth_opts(const struct option
*opt
, const char *str
,
418 void itrace_synth_opts__set_default(struct itrace_synth_opts
*synth_opts
);
420 size_t perf_event__fprintf_auxtrace_error(union perf_event
*event
, FILE *fp
);
421 void perf_session__auxtrace_error_inc(struct perf_session
*session
,
422 union perf_event
*event
);
423 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
);
425 static inline int auxtrace__process_event(struct perf_session
*session
,
426 union perf_event
*event
,
427 struct perf_sample
*sample
,
428 struct perf_tool
*tool
)
430 if (!session
->auxtrace
)
433 return session
->auxtrace
->process_event(session
, event
, sample
, tool
);
436 static inline int auxtrace__flush_events(struct perf_session
*session
,
437 struct perf_tool
*tool
)
439 if (!session
->auxtrace
)
442 return session
->auxtrace
->flush_events(session
, tool
);
445 static inline void auxtrace__free_events(struct perf_session
*session
)
447 if (!session
->auxtrace
)
450 return session
->auxtrace
->free_events(session
);
453 static inline void auxtrace__free(struct perf_session
*session
)
455 if (!session
->auxtrace
)
458 return session
->auxtrace
->free(session
);