blob: 53b60a64a693b95d6afbd1bb33ca424707b829ba [file] [log] [blame]
Adrian Hunter718c6022015-04-09 18:53:42 +03001/*
2 * auxtrace.h: AUX area trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
4 *
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.
8 *
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
12 * more details.
13 *
14 */
15
16#ifndef __PERF_AUXTRACE_H
17#define __PERF_AUXTRACE_H
18
19#include <sys/types.h>
20#include <stdbool.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030021#include <stddef.h>
Adrian Huntere5027892015-04-21 12:21:51 +030022#include <linux/list.h>
Adrian Hunter718c6022015-04-09 18:53:42 +030023#include <linux/perf_event.h>
24#include <linux/types.h>
25
26#include "../perf.h"
Adrian Hunter85ed4722015-04-09 18:53:50 +030027#include "event.h"
Adrian Hunterc4468702015-04-09 18:53:48 +030028#include "session.h"
Adrian Hunter718c6022015-04-09 18:53:42 +030029
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030030union perf_event;
31struct perf_session;
Adrian Hunter718c6022015-04-09 18:53:42 +030032struct perf_evlist;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030033struct perf_tool;
Adrian Hunterf6986c952015-04-09 18:53:49 +030034struct option;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030035struct record_opts;
36struct auxtrace_info_event;
Adrian Hunter85ed4722015-04-09 18:53:50 +030037struct events_stats;
Adrian Hunter718c6022015-04-09 18:53:42 +030038
Adrian Hunter73f75fb2015-04-09 18:53:53 +030039enum auxtrace_type {
40 PERF_AUXTRACE_UNKNOWN,
41};
42
Adrian Hunterf6986c952015-04-09 18:53:49 +030043enum itrace_period_type {
44 PERF_ITRACE_PERIOD_INSTRUCTIONS,
45 PERF_ITRACE_PERIOD_TICKS,
46 PERF_ITRACE_PERIOD_NANOSECS,
47};
48
49/**
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
65 */
66struct itrace_synth_opts {
67 bool set;
68 bool inject;
69 bool instructions;
70 bool branches;
71 bool errors;
72 bool dont_decode;
73 bool log;
74 bool calls;
75 bool returns;
76 bool callchain;
77 unsigned int callchain_sz;
78 unsigned long long period;
79 enum itrace_period_type period_type;
80};
81
Adrian Hunter718c6022015-04-09 18:53:42 +030082/**
Adrian Hunterc4468702015-04-09 18:53:48 +030083 * struct auxtrace - session callbacks to allow AUX area data decoding.
84 * @process_event: lets the decoder see all session events
85 * @flush_events: process any remaining data
86 * @free_events: free resources associated with event processing
87 * @free: free resources associated with the session
88 */
89struct auxtrace {
90 int (*process_event)(struct perf_session *session,
91 union perf_event *event,
92 struct perf_sample *sample,
93 struct perf_tool *tool);
Adrian Hunter73f75fb2015-04-09 18:53:53 +030094 int (*process_auxtrace_event)(struct perf_session *session,
95 union perf_event *event,
96 struct perf_tool *tool);
Adrian Hunterc4468702015-04-09 18:53:48 +030097 int (*flush_events)(struct perf_session *session,
98 struct perf_tool *tool);
99 void (*free_events)(struct perf_session *session);
100 void (*free)(struct perf_session *session);
101};
102
103/**
Adrian Huntere5027892015-04-21 12:21:51 +0300104 * struct auxtrace_buffer - a buffer containing AUX area tracing data.
105 * @list: buffers are queued in a list held by struct auxtrace_queue
106 * @size: size of the buffer in bytes
107 * @pid: in per-thread mode, the pid this buffer is associated with
108 * @tid: in per-thread mode, the tid this buffer is associated with
109 * @cpu: in per-cpu mode, the cpu this buffer is associated with
110 * @data: actual buffer data (can be null if the data has not been loaded)
111 * @data_offset: file offset at which the buffer can be read
112 * @mmap_addr: mmap address at which the buffer can be read
113 * @mmap_size: size of the mmap at @mmap_addr
114 * @data_needs_freeing: @data was malloc'd so free it when it is no longer
115 * needed
116 * @consecutive: the original data was split up and this buffer is consecutive
117 * to the previous buffer
118 * @offset: offset as determined by aux_head / aux_tail members of struct
119 * perf_event_mmap_page
120 * @reference: an implementation-specific reference determined when the data is
121 * recorded
122 * @buffer_nr: used to number each buffer
123 * @use_size: implementation actually only uses this number of bytes
124 * @use_data: implementation actually only uses data starting at this address
125 */
126struct auxtrace_buffer {
127 struct list_head list;
128 size_t size;
129 pid_t pid;
130 pid_t tid;
131 int cpu;
132 void *data;
133 off_t data_offset;
134 void *mmap_addr;
135 size_t mmap_size;
136 bool data_needs_freeing;
137 bool consecutive;
138 u64 offset;
139 u64 reference;
140 u64 buffer_nr;
141 size_t use_size;
142 void *use_data;
143};
144
145/**
146 * struct auxtrace_queue - a queue of AUX area tracing data buffers.
147 * @head: head of buffer list
148 * @tid: in per-thread mode, the tid this queue is associated with
149 * @cpu: in per-cpu mode, the cpu this queue is associated with
150 * @set: %true once this queue has been dedicated to a specific thread or cpu
151 * @priv: implementation-specific data
152 */
153struct auxtrace_queue {
154 struct list_head head;
155 pid_t tid;
156 int cpu;
157 bool set;
158 void *priv;
159};
160
161/**
162 * struct auxtrace_queues - an array of AUX area tracing queues.
163 * @queue_array: array of queues
164 * @nr_queues: number of queues
165 * @new_data: set whenever new data is queued
166 * @populated: queues have been fully populated using the auxtrace_index
167 * @next_buffer_nr: used to number each buffer
168 */
169struct auxtrace_queues {
170 struct auxtrace_queue *queue_array;
171 unsigned int nr_queues;
172 bool new_data;
173 bool populated;
174 u64 next_buffer_nr;
175};
176
177/**
Adrian Hunterf9397152015-04-09 18:53:52 +0300178 * struct auxtrace_heap_item - element of struct auxtrace_heap.
179 * @queue_nr: queue number
180 * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
181 * to be a timestamp
182 */
183struct auxtrace_heap_item {
184 unsigned int queue_nr;
185 u64 ordinal;
186};
187
188/**
189 * struct auxtrace_heap - a heap suitable for sorting AUX area tracing queues.
190 * @heap_array: the heap
191 * @heap_cnt: the number of elements in the heap
192 * @heap_sz: maximum number of elements (grows as needed)
193 */
194struct auxtrace_heap {
195 struct auxtrace_heap_item *heap_array;
196 unsigned int heap_cnt;
197 unsigned int heap_sz;
198};
199
200/**
Adrian Hunter718c6022015-04-09 18:53:42 +0300201 * struct auxtrace_mmap - records an mmap of the auxtrace buffer.
202 * @base: address of mapped area
203 * @userpg: pointer to buffer's perf_event_mmap_page
204 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
205 * @len: size of mapped area
206 * @prev: previous aux_head
207 * @idx: index of this mmap
208 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
209 * mmap) otherwise %0
210 * @cpu: cpu number for a per-cpu mmap otherwise %-1
211 */
212struct auxtrace_mmap {
213 void *base;
214 void *userpg;
215 size_t mask;
216 size_t len;
217 u64 prev;
218 int idx;
219 pid_t tid;
220 int cpu;
221};
222
223/**
224 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap.
225 * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
226 * @offset: file offset of mapped area
227 * @len: size of mapped area
228 * @prot: mmap memory protection
229 * @idx: index of this mmap
230 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu
231 * mmap) otherwise %0
232 * @cpu: cpu number for a per-cpu mmap otherwise %-1
233 */
234struct auxtrace_mmap_params {
235 size_t mask;
236 off_t offset;
237 size_t len;
238 int prot;
239 int idx;
240 pid_t tid;
241 int cpu;
242};
243
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300244/**
245 * struct auxtrace_record - callbacks for recording AUX area data.
246 * @recording_options: validate and process recording options
247 * @info_priv_size: return the size of the private data in auxtrace_info_event
248 * @info_fill: fill-in the private data in auxtrace_info_event
249 * @free: free this auxtrace record structure
250 * @reference: provide a 64-bit reference number for auxtrace_event
251 * @read_finish: called after reading from an auxtrace mmap
252 */
253struct auxtrace_record {
254 int (*recording_options)(struct auxtrace_record *itr,
255 struct perf_evlist *evlist,
256 struct record_opts *opts);
257 size_t (*info_priv_size)(struct auxtrace_record *itr);
258 int (*info_fill)(struct auxtrace_record *itr,
259 struct perf_session *session,
260 struct auxtrace_info_event *auxtrace_info,
261 size_t priv_size);
262 void (*free)(struct auxtrace_record *itr);
263 u64 (*reference)(struct auxtrace_record *itr);
264 int (*read_finish)(struct auxtrace_record *itr, int idx);
265};
266
Adrian Hunter718c6022015-04-09 18:53:42 +0300267static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
268{
269 struct perf_event_mmap_page *pc = mm->userpg;
270#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
271 u64 head = ACCESS_ONCE(pc->aux_head);
272#else
273 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
274#endif
275
276 /* Ensure all reads are done after we read the head */
277 rmb();
278 return head;
279}
280
281static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
282{
283 struct perf_event_mmap_page *pc = mm->userpg;
284#if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
285 u64 old_tail;
286#endif
287
288 /* Ensure all reads are done before we write the tail out */
289 mb();
290#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
291 pc->aux_tail = tail;
292#else
293 do {
294 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0);
295 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail));
296#endif
297}
298
299int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
300 struct auxtrace_mmap_params *mp,
301 void *userpg, int fd);
302void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
303void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
304 off_t auxtrace_offset,
305 unsigned int auxtrace_pages,
306 bool auxtrace_overwrite);
307void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
308 struct perf_evlist *evlist, int idx,
309 bool per_cpu);
310
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300311typedef int (*process_auxtrace_t)(struct perf_tool *tool,
312 union perf_event *event, void *data1,
313 size_t len1, void *data2, size_t len2);
314
315int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
316 struct perf_tool *tool, process_auxtrace_t fn);
317
Adrian Huntere5027892015-04-21 12:21:51 +0300318int auxtrace_queues__init(struct auxtrace_queues *queues);
319int auxtrace_queues__add_event(struct auxtrace_queues *queues,
320 struct perf_session *session,
321 union perf_event *event, off_t data_offset,
322 struct auxtrace_buffer **buffer_ptr);
323void auxtrace_queues__free(struct auxtrace_queues *queues);
324struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
325 struct auxtrace_buffer *buffer);
326void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd);
327void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer);
328void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer);
329void auxtrace_buffer__free(struct auxtrace_buffer *buffer);
Adrian Hunterf9397152015-04-09 18:53:52 +0300330
331int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
332 u64 ordinal);
333void auxtrace_heap__pop(struct auxtrace_heap *heap);
334void auxtrace_heap__free(struct auxtrace_heap *heap);
335
Adrian Hunterc3278f02015-04-09 18:53:54 +0300336struct auxtrace_cache_entry {
337 struct hlist_node hash;
338 u32 key;
339};
340
341struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
342 unsigned int limit_percent);
343void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache);
344void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c);
345void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry);
346int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
347 struct auxtrace_cache_entry *entry);
348void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key);
349
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300350struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist,
351 int *err);
352
353int auxtrace_record__options(struct auxtrace_record *itr,
354 struct perf_evlist *evlist,
355 struct record_opts *opts);
356size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr);
357int auxtrace_record__info_fill(struct auxtrace_record *itr,
358 struct perf_session *session,
359 struct auxtrace_info_event *auxtrace_info,
360 size_t priv_size);
361void auxtrace_record__free(struct auxtrace_record *itr);
362u64 auxtrace_record__reference(struct auxtrace_record *itr);
363
Adrian Hunter85ed4722015-04-09 18:53:50 +0300364void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
365 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
366 const char *msg);
367
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300368int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
369 struct perf_tool *tool,
370 struct perf_session *session,
371 perf_event__handler_t process);
Adrian Hunter73f75fb2015-04-09 18:53:53 +0300372int perf_event__process_auxtrace_info(struct perf_tool *tool,
373 union perf_event *event,
374 struct perf_session *session);
375s64 perf_event__process_auxtrace(struct perf_tool *tool,
376 union perf_event *event,
377 struct perf_session *session);
Adrian Hunter85ed4722015-04-09 18:53:50 +0300378int perf_event__process_auxtrace_error(struct perf_tool *tool,
379 union perf_event *event,
380 struct perf_session *session);
Adrian Hunterf6986c952015-04-09 18:53:49 +0300381int itrace_parse_synth_opts(const struct option *opt, const char *str,
382 int unset);
383void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300384
Adrian Hunter85ed4722015-04-09 18:53:50 +0300385size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp);
386void perf_session__auxtrace_error_inc(struct perf_session *session,
387 union perf_event *event);
388void events_stats__auxtrace_error_warn(const struct events_stats *stats);
389
Adrian Hunterc4468702015-04-09 18:53:48 +0300390static inline int auxtrace__process_event(struct perf_session *session,
391 union perf_event *event,
392 struct perf_sample *sample,
393 struct perf_tool *tool)
394{
395 if (!session->auxtrace)
396 return 0;
397
398 return session->auxtrace->process_event(session, event, sample, tool);
399}
400
401static inline int auxtrace__flush_events(struct perf_session *session,
402 struct perf_tool *tool)
403{
404 if (!session->auxtrace)
405 return 0;
406
407 return session->auxtrace->flush_events(session, tool);
408}
409
410static inline void auxtrace__free_events(struct perf_session *session)
411{
412 if (!session->auxtrace)
413 return;
414
415 return session->auxtrace->free_events(session);
416}
417
418static inline void auxtrace__free(struct perf_session *session)
419{
420 if (!session->auxtrace)
421 return;
422
423 return session->auxtrace->free(session);
424}
425
Adrian Hunter718c6022015-04-09 18:53:42 +0300426#endif