blob: b9e4b9d66f5e1c13830f394175a1f49cc8419bb7 [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 Hunter99fa2982015-04-30 17:37:25 +030083 * struct auxtrace_index_entry - indexes a AUX area tracing event within a
84 * perf.data file.
85 * @file_offset: offset within the perf.data file
86 * @sz: size of the event
87 */
88struct auxtrace_index_entry {
89 u64 file_offset;
90 u64 sz;
91};
92
93#define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
94
95/**
96 * struct auxtrace_index - index of AUX area tracing events within a perf.data
97 * file.
98 * @list: linking a number of arrays of entries
99 * @nr: number of entries
100 * @entries: array of entries
101 */
102struct auxtrace_index {
103 struct list_head list;
104 size_t nr;
105 struct auxtrace_index_entry entries[PERF_AUXTRACE_INDEX_ENTRY_COUNT];
106};
107
108/**
Adrian Hunterc4468702015-04-09 18:53:48 +0300109 * 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
114 */
115struct auxtrace {
116 int (*process_event)(struct perf_session *session,
117 union perf_event *event,
118 struct perf_sample *sample,
119 struct perf_tool *tool);
Adrian Hunter73f75fb2015-04-09 18:53:53 +0300120 int (*process_auxtrace_event)(struct perf_session *session,
121 union perf_event *event,
122 struct perf_tool *tool);
Adrian Hunterc4468702015-04-09 18:53:48 +0300123 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);
127};
128
129/**
Adrian Huntere5027892015-04-21 12:21:51 +0300130 * 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
141 * needed
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
147 * recorded
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
151 */
152struct auxtrace_buffer {
153 struct list_head list;
154 size_t size;
155 pid_t pid;
156 pid_t tid;
157 int cpu;
158 void *data;
159 off_t data_offset;
160 void *mmap_addr;
161 size_t mmap_size;
162 bool data_needs_freeing;
163 bool consecutive;
164 u64 offset;
165 u64 reference;
166 u64 buffer_nr;
167 size_t use_size;
168 void *use_data;
169};
170
171/**
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
178 */
179struct auxtrace_queue {
180 struct list_head head;
181 pid_t tid;
182 int cpu;
183 bool set;
184 void *priv;
185};
186
187/**
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
194 */
195struct auxtrace_queues {
196 struct auxtrace_queue *queue_array;
197 unsigned int nr_queues;
198 bool new_data;
199 bool populated;
200 u64 next_buffer_nr;
201};
202
203/**
Adrian Hunterf9397152015-04-09 18:53:52 +0300204 * 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
207 * to be a timestamp
208 */
209struct auxtrace_heap_item {
210 unsigned int queue_nr;
211 u64 ordinal;
212};
213
214/**
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)
219 */
220struct auxtrace_heap {
221 struct auxtrace_heap_item *heap_array;
222 unsigned int heap_cnt;
223 unsigned int heap_sz;
224};
225
226/**
Adrian Hunter718c6022015-04-09 18:53:42 +0300227 * 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
235 * mmap) otherwise %0
236 * @cpu: cpu number for a per-cpu mmap otherwise %-1
237 */
238struct auxtrace_mmap {
239 void *base;
240 void *userpg;
241 size_t mask;
242 size_t len;
243 u64 prev;
244 int idx;
245 pid_t tid;
246 int cpu;
247};
248
249/**
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
257 * mmap) otherwise %0
258 * @cpu: cpu number for a per-cpu mmap otherwise %-1
259 */
260struct auxtrace_mmap_params {
261 size_t mask;
262 off_t offset;
263 size_t len;
264 int prot;
265 int idx;
266 pid_t tid;
267 int cpu;
268};
269
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300270/**
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
278 */
279struct 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,
287 size_t priv_size);
288 void (*free)(struct auxtrace_record *itr);
289 u64 (*reference)(struct auxtrace_record *itr);
290 int (*read_finish)(struct auxtrace_record *itr, int idx);
291};
292
Adrian Hunter718c6022015-04-09 18:53:42 +0300293static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
294{
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);
298#else
299 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
300#endif
301
302 /* Ensure all reads are done after we read the head */
303 rmb();
304 return head;
305}
306
307static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
308{
309 struct perf_event_mmap_page *pc = mm->userpg;
310#if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
311 u64 old_tail;
312#endif
313
314 /* Ensure all reads are done before we write the tail out */
315 mb();
316#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
317 pc->aux_tail = tail;
318#else
319 do {
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));
322#endif
323}
324
325int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
326 struct auxtrace_mmap_params *mp,
327 void *userpg, int fd);
328void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
329void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
330 off_t auxtrace_offset,
331 unsigned int auxtrace_pages,
332 bool auxtrace_overwrite);
333void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
334 struct perf_evlist *evlist, int idx,
335 bool per_cpu);
336
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300337typedef int (*process_auxtrace_t)(struct perf_tool *tool,
338 union perf_event *event, void *data1,
339 size_t len1, void *data2, size_t len2);
340
341int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
342 struct perf_tool *tool, process_auxtrace_t fn);
343
Adrian Huntere5027892015-04-21 12:21:51 +0300344int auxtrace_queues__init(struct auxtrace_queues *queues);
345int 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);
349void auxtrace_queues__free(struct auxtrace_queues *queues);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300350int auxtrace_queues__process_index(struct auxtrace_queues *queues,
351 struct perf_session *session);
Adrian Huntere5027892015-04-21 12:21:51 +0300352struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
353 struct auxtrace_buffer *buffer);
354void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd);
355void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer);
356void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer);
357void auxtrace_buffer__free(struct auxtrace_buffer *buffer);
Adrian Hunterf9397152015-04-09 18:53:52 +0300358
359int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
360 u64 ordinal);
361void auxtrace_heap__pop(struct auxtrace_heap *heap);
362void auxtrace_heap__free(struct auxtrace_heap *heap);
363
Adrian Hunterc3278f02015-04-09 18:53:54 +0300364struct auxtrace_cache_entry {
365 struct hlist_node hash;
366 u32 key;
367};
368
369struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
370 unsigned int limit_percent);
371void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache);
372void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c);
373void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry);
374int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
375 struct auxtrace_cache_entry *entry);
376void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key);
377
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300378struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist,
379 int *err);
380
381int auxtrace_record__options(struct auxtrace_record *itr,
382 struct perf_evlist *evlist,
383 struct record_opts *opts);
384size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr);
385int auxtrace_record__info_fill(struct auxtrace_record *itr,
386 struct perf_session *session,
387 struct auxtrace_info_event *auxtrace_info,
388 size_t priv_size);
389void auxtrace_record__free(struct auxtrace_record *itr);
390u64 auxtrace_record__reference(struct auxtrace_record *itr);
391
Adrian Hunter99fa2982015-04-30 17:37:25 +0300392int auxtrace_index__auxtrace_event(struct list_head *head, union perf_event *event,
393 off_t file_offset);
394int auxtrace_index__write(int fd, struct list_head *head);
395int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
396 bool needs_swap);
397void auxtrace_index__free(struct list_head *head);
398
Adrian Hunter85ed4722015-04-09 18:53:50 +0300399void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
400 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
401 const char *msg);
402
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300403int 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);
Adrian Hunter73f75fb2015-04-09 18:53:53 +0300407int perf_event__process_auxtrace_info(struct perf_tool *tool,
408 union perf_event *event,
409 struct perf_session *session);
410s64 perf_event__process_auxtrace(struct perf_tool *tool,
411 union perf_event *event,
412 struct perf_session *session);
Adrian Hunter85ed4722015-04-09 18:53:50 +0300413int perf_event__process_auxtrace_error(struct perf_tool *tool,
414 union perf_event *event,
415 struct perf_session *session);
Adrian Hunterf6986c952015-04-09 18:53:49 +0300416int itrace_parse_synth_opts(const struct option *opt, const char *str,
417 int unset);
418void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300419
Adrian Hunter85ed4722015-04-09 18:53:50 +0300420size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp);
421void perf_session__auxtrace_error_inc(struct perf_session *session,
422 union perf_event *event);
423void events_stats__auxtrace_error_warn(const struct events_stats *stats);
424
Adrian Hunterc4468702015-04-09 18:53:48 +0300425static inline int auxtrace__process_event(struct perf_session *session,
426 union perf_event *event,
427 struct perf_sample *sample,
428 struct perf_tool *tool)
429{
430 if (!session->auxtrace)
431 return 0;
432
433 return session->auxtrace->process_event(session, event, sample, tool);
434}
435
436static inline int auxtrace__flush_events(struct perf_session *session,
437 struct perf_tool *tool)
438{
439 if (!session->auxtrace)
440 return 0;
441
442 return session->auxtrace->flush_events(session, tool);
443}
444
445static inline void auxtrace__free_events(struct perf_session *session)
446{
447 if (!session->auxtrace)
448 return;
449
450 return session->auxtrace->free_events(session);
451}
452
453static inline void auxtrace__free(struct perf_session *session)
454{
455 if (!session->auxtrace)
456 return;
457
458 return session->auxtrace->free(session);
459}
460
Adrian Hunter718c6022015-04-09 18:53:42 +0300461#endif