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;
41 PERF_AUXTRACE_UNKNOWN,
44 enum itrace_period_type {
45 PERF_ITRACE_PERIOD_INSTRUCTIONS,
46 PERF_ITRACE_PERIOD_TICKS,
47 PERF_ITRACE_PERIOD_NANOSECS,
51 * struct itrace_synth_opts - AUX area tracing synthesis options.
52 * @set: indicates whether or not options have been set
53 * @inject: indicates the event (not just the sample) must be fully synthesized
54 * because 'perf inject' will write it out
55 * @instructions: whether to synthesize 'instructions' events
56 * @branches: whether to synthesize 'branches' events
57 * @transactions: whether to synthesize events for transactions
58 * @errors: whether to synthesize decoder error events
59 * @dont_decode: whether to skip decoding entirely
60 * @log: write a decoding log
61 * @calls: limit branch samples to calls (can be combined with @returns)
62 * @returns: limit branch samples to returns (can be combined with @calls)
63 * @callchain: add callchain to 'instructions' events
64 * @callchain_sz: maximum callchain size
65 * @period: 'instructions' events period
66 * @period_type: 'instructions' events period type
68 struct itrace_synth_opts {
80 unsigned int callchain_sz;
81 unsigned long long period;
82 enum itrace_period_type period_type;
86 * struct auxtrace_index_entry - indexes a AUX area tracing event within a
88 * @file_offset: offset within the perf.data file
89 * @sz: size of the event
91 struct auxtrace_index_entry {
96 #define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
99 * struct auxtrace_index - index of AUX area tracing events within a perf.data
101 * @list: linking a number of arrays of entries
102 * @nr: number of entries
103 * @entries: array of entries
105 struct auxtrace_index {
106 struct list_head list;
108 struct auxtrace_index_entry entries[PERF_AUXTRACE_INDEX_ENTRY_COUNT];
112 * struct auxtrace - session callbacks to allow AUX area data decoding.
113 * @process_event: lets the decoder see all session events
114 * @flush_events: process any remaining data
115 * @free_events: free resources associated with event processing
116 * @free: free resources associated with the session
119 int (*process_event)(struct perf_session *session,
120 union perf_event *event,
121 struct perf_sample *sample,
122 struct perf_tool *tool);
123 int (*process_auxtrace_event)(struct perf_session *session,
124 union perf_event *event,
125 struct perf_tool *tool);
126 int (*flush_events)(struct perf_session *session,
127 struct perf_tool *tool);
128 void (*free_events)(struct perf_session *session);
129 void (*free)(struct perf_session *session);
133 * struct auxtrace_buffer - a buffer containing AUX area tracing data.
134 * @list: buffers are queued in a list held by struct auxtrace_queue
135 * @size: size of the buffer in bytes
136 * @pid: in per-thread mode, the pid this buffer is associated with
137 * @tid: in per-thread mode, the tid this buffer is associated with
138 * @cpu: in per-cpu mode, the cpu this buffer is associated with
139 * @data: actual buffer data (can be null if the data has not been loaded)
140 * @data_offset: file offset at which the buffer can be read
141 * @mmap_addr: mmap address at which the buffer can be read
142 * @mmap_size: size of the mmap at @mmap_addr
143 * @data_needs_freeing: @data was malloc'd so free it when it is no longer
145 * @consecutive: the original data was split up and this buffer is consecutive
146 * to the previous buffer
147 * @offset: offset as determined by aux_head / aux_tail members of struct
148 * perf_event_mmap_page
149 * @reference: an implementation-specific reference determined when the data is
151 * @buffer_nr: used to number each buffer
152 * @use_size: implementation actually only uses this number of bytes
153 * @use_data: implementation actually only uses data starting at this address
155 struct auxtrace_buffer {
156 struct list_head list;
165 bool data_needs_freeing;
175 * struct auxtrace_queue - a queue of AUX area tracing data buffers.
176 * @head: head of buffer list
177 * @tid: in per-thread mode, the tid this queue is associated with
178 * @cpu: in per-cpu mode, the cpu this queue is associated with
179 * @set: %true once this queue has been dedicated to a specific thread or cpu
180 * @priv: implementation-specific data
182 struct auxtrace_queue {
183 struct list_head head;
191 * struct auxtrace_queues - an array of AUX area tracing queues.
192 * @queue_array: array of queues
193 * @nr_queues: number of queues
194 * @new_data: set whenever new data is queued
195 * @populated: queues have been fully populated using the auxtrace_index
196 * @next_buffer_nr: used to number each buffer
198 struct auxtrace_queues {
199 struct auxtrace_queue *queue_array;
200 unsigned int nr_queues;
207 * struct auxtrace_heap_item - element of struct auxtrace_heap.
208 * @queue_nr: queue number
209 * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
212 struct auxtrace_heap_item {
213 unsigned int queue_nr;
218 * struct auxtrace_heap - a heap suitable for sorting AUX area tracing queues.
219 * @heap_array: the heap
220 * @heap_cnt: the number of elements in the heap
221 * @heap_sz: maximum number of elements (grows as needed)
223 struct auxtrace_heap {
224 struct auxtrace_heap_item *heap_array;
225 unsigned int heap_cnt;
226 unsigned int heap_sz;
230 * struct auxtrace_mmap - records an mmap of the auxtrace buffer.
231 * @base: address of mapped area
232 * @userpg: pointer to buffer's perf_event_mmap_page
233 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
234 * @len: size of mapped area
235 * @prev: previous aux_head
236 * @idx: index of this mmap
237 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
239 * @cpu: cpu number for a per-cpu mmap otherwise %-1
241 struct auxtrace_mmap {
253 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap.
254 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
255 * @offset: file offset of mapped area
256 * @len: size of mapped area
257 * @prot: mmap memory protection
258 * @idx: index of this mmap
259 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
261 * @cpu: cpu number for a per-cpu mmap otherwise %-1
263 struct auxtrace_mmap_params {
274 * struct auxtrace_record - callbacks for recording AUX area data.
275 * @recording_options: validate and process recording options
276 * @info_priv_size: return the size of the private data in auxtrace_info_event
277 * @info_fill: fill-in the private data in auxtrace_info_event
278 * @free: free this auxtrace record structure
279 * @snapshot_start: starting a snapshot
280 * @snapshot_finish: finishing a snapshot
281 * @find_snapshot: find data to snapshot within auxtrace mmap
282 * @parse_snapshot_options: parse snapshot options
283 * @reference: provide a 64-bit reference number for auxtrace_event
284 * @read_finish: called after reading from an auxtrace mmap
286 struct auxtrace_record {
287 int (*recording_options)(struct auxtrace_record *itr,
288 struct perf_evlist *evlist,
289 struct record_opts *opts);
290 size_t (*info_priv_size)(struct auxtrace_record *itr);
291 int (*info_fill)(struct auxtrace_record *itr,
292 struct perf_session *session,
293 struct auxtrace_info_event *auxtrace_info,
295 void (*free)(struct auxtrace_record *itr);
296 int (*snapshot_start)(struct auxtrace_record *itr);
297 int (*snapshot_finish)(struct auxtrace_record *itr);
298 int (*find_snapshot)(struct auxtrace_record *itr, int idx,
299 struct auxtrace_mmap *mm, unsigned char *data,
300 u64 *head, u64 *old);
301 int (*parse_snapshot_options)(struct auxtrace_record *itr,
302 struct record_opts *opts,
304 u64 (*reference)(struct auxtrace_record *itr);
305 int (*read_finish)(struct auxtrace_record *itr, int idx);
308 #ifdef HAVE_AUXTRACE_SUPPORT
311 * In snapshot mode the mmapped page is read-only which makes using
312 * __sync_val_compare_and_swap() problematic. However, snapshot mode expects
313 * the buffer is not updated while the snapshot is made (e.g. Intel PT disables
314 * the event) so there is not a race anyway.
316 static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)
318 struct perf_event_mmap_page *pc = mm->userpg;
319 u64 head = ACCESS_ONCE(pc->aux_head);
321 /* Ensure all reads are done after we read the head */
326 static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
328 struct perf_event_mmap_page *pc = mm->userpg;
329 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
330 u64 head = ACCESS_ONCE(pc->aux_head);
332 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
335 /* Ensure all reads are done after we read the head */
340 static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
342 struct perf_event_mmap_page *pc = mm->userpg;
343 #if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
347 /* Ensure all reads are done before we write the tail out */
349 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
353 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0);
354 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail));
358 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
359 struct auxtrace_mmap_params *mp,
360 void *userpg, int fd);
361 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
362 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
363 off_t auxtrace_offset,
364 unsigned int auxtrace_pages,
365 bool auxtrace_overwrite);
366 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
367 struct perf_evlist *evlist, int idx,
370 typedef int (*process_auxtrace_t)(struct perf_tool *tool,
371 union perf_event *event, void *data1,
372 size_t len1, void *data2, size_t len2);
374 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
375 struct perf_tool *tool, process_auxtrace_t fn);
377 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
378 struct auxtrace_record *itr,
379 struct perf_tool *tool, process_auxtrace_t fn,
380 size_t snapshot_size);
382 int auxtrace_queues__init(struct auxtrace_queues *queues);
383 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
384 struct perf_session *session,
385 union perf_event *event, off_t data_offset,
386 struct auxtrace_buffer **buffer_ptr);
387 void auxtrace_queues__free(struct auxtrace_queues *queues);
388 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
389 struct perf_session *session);
390 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
391 struct auxtrace_buffer *buffer);
392 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd);
393 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer);
394 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer);
395 void auxtrace_buffer__free(struct auxtrace_buffer *buffer);
397 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
399 void auxtrace_heap__pop(struct auxtrace_heap *heap);
400 void auxtrace_heap__free(struct auxtrace_heap *heap);
402 struct auxtrace_cache_entry {
403 struct hlist_node hash;
407 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
408 unsigned int limit_percent);
409 void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache);
410 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c);
411 void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry);
412 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
413 struct auxtrace_cache_entry *entry);
414 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key);
416 struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist,
419 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
420 struct record_opts *opts,
422 int auxtrace_record__options(struct auxtrace_record *itr,
423 struct perf_evlist *evlist,
424 struct record_opts *opts);
425 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr);
426 int auxtrace_record__info_fill(struct auxtrace_record *itr,
427 struct perf_session *session,
428 struct auxtrace_info_event *auxtrace_info,
430 void auxtrace_record__free(struct auxtrace_record *itr);
431 int auxtrace_record__snapshot_start(struct auxtrace_record *itr);
432 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr);
433 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
434 struct auxtrace_mmap *mm,
435 unsigned char *data, u64 *head, u64 *old);
436 u64 auxtrace_record__reference(struct auxtrace_record *itr);
438 int auxtrace_index__auxtrace_event(struct list_head *head, union perf_event *event,
440 int auxtrace_index__write(int fd, struct list_head *head);
441 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
443 void auxtrace_index__free(struct list_head *head);
445 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
446 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
449 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
450 struct perf_tool *tool,
451 struct perf_session *session,
452 perf_event__handler_t process);
453 int perf_event__process_auxtrace_info(struct perf_tool *tool,
454 union perf_event *event,
455 struct perf_session *session);
456 s64 perf_event__process_auxtrace(struct perf_tool *tool,
457 union perf_event *event,
458 struct perf_session *session);
459 int perf_event__process_auxtrace_error(struct perf_tool *tool,
460 union perf_event *event,
461 struct perf_session *session);
462 int itrace_parse_synth_opts(const struct option *opt, const char *str,
464 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
466 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp);
467 void perf_session__auxtrace_error_inc(struct perf_session *session,
468 union perf_event *event);
469 void events_stats__auxtrace_error_warn(const struct events_stats *stats);
471 static inline int auxtrace__process_event(struct perf_session *session,
472 union perf_event *event,
473 struct perf_sample *sample,
474 struct perf_tool *tool)
476 if (!session->auxtrace)
479 return session->auxtrace->process_event(session, event, sample, tool);
482 static inline int auxtrace__flush_events(struct perf_session *session,
483 struct perf_tool *tool)
485 if (!session->auxtrace)
488 return session->auxtrace->flush_events(session, tool);
491 static inline void auxtrace__free_events(struct perf_session *session)
493 if (!session->auxtrace)
496 return session->auxtrace->free_events(session);
499 static inline void auxtrace__free(struct perf_session *session)
501 if (!session->auxtrace)
504 return session->auxtrace->free(session);
509 static inline struct auxtrace_record *
510 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused,
511 int *err __maybe_unused)
518 void auxtrace_record__free(struct auxtrace_record *itr __maybe_unused)
523 perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr __maybe_unused,
524 struct perf_tool *tool __maybe_unused,
525 struct perf_session *session __maybe_unused,
526 perf_event__handler_t process __maybe_unused)
532 int auxtrace_record__options(struct auxtrace_record *itr __maybe_unused,
533 struct perf_evlist *evlist __maybe_unused,
534 struct record_opts *opts __maybe_unused)
539 #define perf_event__process_auxtrace_info 0
540 #define perf_event__process_auxtrace 0
541 #define perf_event__process_auxtrace_error 0
544 void perf_session__auxtrace_error_inc(struct perf_session *session
546 union perf_event *event
552 void events_stats__auxtrace_error_warn(const struct events_stats *stats
558 int itrace_parse_synth_opts(const struct option *opt __maybe_unused,
559 const char *str __maybe_unused,
560 int unset __maybe_unused)
562 pr_err("AUX area tracing not supported\n");
567 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
568 struct record_opts *opts __maybe_unused,
573 pr_err("AUX area tracing not supported\n");
578 int auxtrace__process_event(struct perf_session *session __maybe_unused,
579 union perf_event *event __maybe_unused,
580 struct perf_sample *sample __maybe_unused,
581 struct perf_tool *tool __maybe_unused)
587 int auxtrace__flush_events(struct perf_session *session __maybe_unused,
588 struct perf_tool *tool __maybe_unused)
594 void auxtrace__free_events(struct perf_session *session __maybe_unused)
599 void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache __maybe_unused)
604 void auxtrace__free(struct perf_session *session __maybe_unused)
609 int auxtrace_index__write(int fd __maybe_unused,
610 struct list_head *head __maybe_unused)
616 int auxtrace_index__process(int fd __maybe_unused,
617 u64 size __maybe_unused,
618 struct perf_session *session __maybe_unused,
619 bool needs_swap __maybe_unused)
625 void auxtrace_index__free(struct list_head *head __maybe_unused)
629 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
630 struct auxtrace_mmap_params *mp,
631 void *userpg, int fd);
632 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
633 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
634 off_t auxtrace_offset,
635 unsigned int auxtrace_pages,
636 bool auxtrace_overwrite);
637 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
638 struct perf_evlist *evlist, int idx,