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/perf_event.h>
23 #include <linux/types.h>
34 struct auxtrace_info_event;
36 enum itrace_period_type {
37 PERF_ITRACE_PERIOD_INSTRUCTIONS,
38 PERF_ITRACE_PERIOD_TICKS,
39 PERF_ITRACE_PERIOD_NANOSECS,
43 * struct itrace_synth_opts - AUX area tracing synthesis options.
44 * @set: indicates whether or not options have been set
45 * @inject: indicates the event (not just the sample) must be fully synthesized
46 * because 'perf inject' will write it out
47 * @instructions: whether to synthesize 'instructions' events
48 * @branches: whether to synthesize 'branches' events
49 * @errors: whether to synthesize decoder error events
50 * @dont_decode: whether to skip decoding entirely
51 * @log: write a decoding log
52 * @calls: limit branch samples to calls (can be combined with @returns)
53 * @returns: limit branch samples to returns (can be combined with @calls)
54 * @callchain: add callchain to 'instructions' events
55 * @callchain_sz: maximum callchain size
56 * @period: 'instructions' events period
57 * @period_type: 'instructions' events period type
59 struct itrace_synth_opts {
70 unsigned int callchain_sz;
71 unsigned long long period;
72 enum itrace_period_type period_type;
76 * struct auxtrace - session callbacks to allow AUX area data decoding.
77 * @process_event: lets the decoder see all session events
78 * @flush_events: process any remaining data
79 * @free_events: free resources associated with event processing
80 * @free: free resources associated with the session
83 int (*process_event)(struct perf_session *session,
84 union perf_event *event,
85 struct perf_sample *sample,
86 struct perf_tool *tool);
87 int (*flush_events)(struct perf_session *session,
88 struct perf_tool *tool);
89 void (*free_events)(struct perf_session *session);
90 void (*free)(struct perf_session *session);
94 * struct auxtrace_mmap - records an mmap of the auxtrace buffer.
95 * @base: address of mapped area
96 * @userpg: pointer to buffer's perf_event_mmap_page
97 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
98 * @len: size of mapped area
99 * @prev: previous aux_head
100 * @idx: index of this mmap
101 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
103 * @cpu: cpu number for a per-cpu mmap otherwise %-1
105 struct auxtrace_mmap {
117 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap.
118 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
119 * @offset: file offset of mapped area
120 * @len: size of mapped area
121 * @prot: mmap memory protection
122 * @idx: index of this mmap
123 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
125 * @cpu: cpu number for a per-cpu mmap otherwise %-1
127 struct auxtrace_mmap_params {
138 * struct auxtrace_record - callbacks for recording AUX area data.
139 * @recording_options: validate and process recording options
140 * @info_priv_size: return the size of the private data in auxtrace_info_event
141 * @info_fill: fill-in the private data in auxtrace_info_event
142 * @free: free this auxtrace record structure
143 * @reference: provide a 64-bit reference number for auxtrace_event
144 * @read_finish: called after reading from an auxtrace mmap
146 struct auxtrace_record {
147 int (*recording_options)(struct auxtrace_record *itr,
148 struct perf_evlist *evlist,
149 struct record_opts *opts);
150 size_t (*info_priv_size)(struct auxtrace_record *itr);
151 int (*info_fill)(struct auxtrace_record *itr,
152 struct perf_session *session,
153 struct auxtrace_info_event *auxtrace_info,
155 void (*free)(struct auxtrace_record *itr);
156 u64 (*reference)(struct auxtrace_record *itr);
157 int (*read_finish)(struct auxtrace_record *itr, int idx);
160 static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
162 struct perf_event_mmap_page *pc = mm->userpg;
163 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
164 u64 head = ACCESS_ONCE(pc->aux_head);
166 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
169 /* Ensure all reads are done after we read the head */
174 static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
176 struct perf_event_mmap_page *pc = mm->userpg;
177 #if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
181 /* Ensure all reads are done before we write the tail out */
183 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
187 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0);
188 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail));
192 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
193 struct auxtrace_mmap_params *mp,
194 void *userpg, int fd);
195 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
196 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
197 off_t auxtrace_offset,
198 unsigned int auxtrace_pages,
199 bool auxtrace_overwrite);
200 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
201 struct perf_evlist *evlist, int idx,
204 typedef int (*process_auxtrace_t)(struct perf_tool *tool,
205 union perf_event *event, void *data1,
206 size_t len1, void *data2, size_t len2);
208 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
209 struct perf_tool *tool, process_auxtrace_t fn);
211 struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist,
214 int auxtrace_record__options(struct auxtrace_record *itr,
215 struct perf_evlist *evlist,
216 struct record_opts *opts);
217 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr);
218 int auxtrace_record__info_fill(struct auxtrace_record *itr,
219 struct perf_session *session,
220 struct auxtrace_info_event *auxtrace_info,
222 void auxtrace_record__free(struct auxtrace_record *itr);
223 u64 auxtrace_record__reference(struct auxtrace_record *itr);
225 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
226 struct perf_tool *tool,
227 struct perf_session *session,
228 perf_event__handler_t process);
229 int itrace_parse_synth_opts(const struct option *opt, const char *str,
231 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
233 static inline int auxtrace__process_event(struct perf_session *session,
234 union perf_event *event,
235 struct perf_sample *sample,
236 struct perf_tool *tool)
238 if (!session->auxtrace)
241 return session->auxtrace->process_event(session, event, sample, tool);
244 static inline int auxtrace__flush_events(struct perf_session *session,
245 struct perf_tool *tool)
247 if (!session->auxtrace)
250 return session->auxtrace->flush_events(session, tool);
253 static inline void auxtrace__free_events(struct perf_session *session)
255 if (!session->auxtrace)
258 return session->auxtrace->free_events(session);
261 static inline void auxtrace__free(struct perf_session *session)
263 if (!session->auxtrace)
266 return session->auxtrace->free(session);