1
2
3
4
5
6
7#ifndef __PERF_AUXTRACE_H
8#define __PERF_AUXTRACE_H
9
10#include <sys/types.h>
11#include <errno.h>
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdio.h>
15#include <linux/list.h>
16#include <linux/perf_event.h>
17#include <linux/types.h>
18#include <asm/bitsperlong.h>
19#include <asm/barrier.h>
20
21union perf_event;
22struct perf_session;
23struct evlist;
24struct perf_tool;
25struct mmap;
26struct perf_sample;
27struct option;
28struct record_opts;
29struct perf_record_auxtrace_error;
30struct perf_record_auxtrace_info;
31struct events_stats;
32struct perf_pmu;
33
34enum auxtrace_error_type {
35 PERF_AUXTRACE_ERROR_ITRACE = 1,
36 PERF_AUXTRACE_ERROR_MAX
37};
38
39
40#define PERF_AUXTRACE_RECORD_ALIGNMENT 8
41
42enum auxtrace_type {
43 PERF_AUXTRACE_UNKNOWN,
44 PERF_AUXTRACE_INTEL_PT,
45 PERF_AUXTRACE_INTEL_BTS,
46 PERF_AUXTRACE_CS_ETM,
47 PERF_AUXTRACE_ARM_SPE,
48 PERF_AUXTRACE_S390_CPUMSF,
49};
50
51enum itrace_period_type {
52 PERF_ITRACE_PERIOD_INSTRUCTIONS,
53 PERF_ITRACE_PERIOD_TICKS,
54 PERF_ITRACE_PERIOD_NANOSECS,
55};
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87struct itrace_synth_opts {
88 bool set;
89 bool default_no_sample;
90 bool inject;
91 bool instructions;
92 bool branches;
93 bool transactions;
94 bool ptwrites;
95 bool pwr_events;
96 bool other_events;
97 bool errors;
98 bool dont_decode;
99 bool log;
100 bool calls;
101 bool returns;
102 bool callchain;
103 bool thread_stack;
104 bool last_branch;
105 unsigned int callchain_sz;
106 unsigned int last_branch_sz;
107 unsigned long long period;
108 enum itrace_period_type period_type;
109 unsigned long initial_skip;
110 unsigned long *cpu_bitmap;
111 struct perf_time_interval *ptime_range;
112 int range_num;
113};
114
115
116
117
118
119
120
121struct auxtrace_index_entry {
122 u64 file_offset;
123 u64 sz;
124};
125
126#define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
127
128
129
130
131
132
133
134
135struct auxtrace_index {
136 struct list_head list;
137 size_t nr;
138 struct auxtrace_index_entry entries[PERF_AUXTRACE_INDEX_ENTRY_COUNT];
139};
140
141
142
143
144
145
146
147
148
149
150
151
152struct auxtrace {
153 int (*process_event)(struct perf_session *session,
154 union perf_event *event,
155 struct perf_sample *sample,
156 struct perf_tool *tool);
157 int (*process_auxtrace_event)(struct perf_session *session,
158 union perf_event *event,
159 struct perf_tool *tool);
160 int (*queue_data)(struct perf_session *session,
161 struct perf_sample *sample, union perf_event *event,
162 u64 data_offset);
163 void (*dump_auxtrace_sample)(struct perf_session *session,
164 struct perf_sample *sample);
165 int (*flush_events)(struct perf_session *session,
166 struct perf_tool *tool);
167 void (*free_events)(struct perf_session *session);
168 void (*free)(struct perf_session *session);
169};
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194struct auxtrace_buffer {
195 struct list_head list;
196 size_t size;
197 pid_t pid;
198 pid_t tid;
199 int cpu;
200 void *data;
201 off_t data_offset;
202 void *mmap_addr;
203 size_t mmap_size;
204 bool data_needs_freeing;
205 bool consecutive;
206 u64 offset;
207 u64 reference;
208 u64 buffer_nr;
209 size_t use_size;
210 void *use_data;
211};
212
213
214
215
216
217
218
219
220
221struct auxtrace_queue {
222 struct list_head head;
223 pid_t tid;
224 int cpu;
225 bool set;
226 void *priv;
227};
228
229
230
231
232
233
234
235
236
237struct auxtrace_queues {
238 struct auxtrace_queue *queue_array;
239 unsigned int nr_queues;
240 bool new_data;
241 bool populated;
242 u64 next_buffer_nr;
243};
244
245
246
247
248
249
250
251struct auxtrace_heap_item {
252 unsigned int queue_nr;
253 u64 ordinal;
254};
255
256
257
258
259
260
261
262struct auxtrace_heap {
263 struct auxtrace_heap_item *heap_array;
264 unsigned int heap_cnt;
265 unsigned int heap_sz;
266};
267
268
269
270
271
272
273
274
275
276
277
278
279
280struct auxtrace_mmap {
281 void *base;
282 void *userpg;
283 size_t mask;
284 size_t len;
285 u64 prev;
286 int idx;
287 pid_t tid;
288 int cpu;
289};
290
291
292
293
294
295
296
297
298
299
300
301
302struct auxtrace_mmap_params {
303 size_t mask;
304 off_t offset;
305 size_t len;
306 int prot;
307 int idx;
308 pid_t tid;
309 int cpu;
310};
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329struct auxtrace_record {
330 int (*recording_options)(struct auxtrace_record *itr,
331 struct evlist *evlist,
332 struct record_opts *opts);
333 size_t (*info_priv_size)(struct auxtrace_record *itr,
334 struct evlist *evlist);
335 int (*info_fill)(struct auxtrace_record *itr,
336 struct perf_session *session,
337 struct perf_record_auxtrace_info *auxtrace_info,
338 size_t priv_size);
339 void (*free)(struct auxtrace_record *itr);
340 int (*snapshot_start)(struct auxtrace_record *itr);
341 int (*snapshot_finish)(struct auxtrace_record *itr);
342 int (*find_snapshot)(struct auxtrace_record *itr, int idx,
343 struct auxtrace_mmap *mm, unsigned char *data,
344 u64 *head, u64 *old);
345 int (*parse_snapshot_options)(struct auxtrace_record *itr,
346 struct record_opts *opts,
347 const char *str);
348 u64 (*reference)(struct auxtrace_record *itr);
349 int (*read_finish)(struct auxtrace_record *itr, int idx);
350 unsigned int alignment;
351 unsigned int default_aux_sample_size;
352 struct perf_pmu *pmu;
353 struct evlist *evlist;
354};
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373struct addr_filter {
374 struct list_head list;
375 bool range;
376 bool start;
377 const char *action;
378 const char *sym_from;
379 const char *sym_to;
380 int sym_from_idx;
381 int sym_to_idx;
382 u64 addr;
383 u64 size;
384 const char *filename;
385 char *str;
386};
387
388
389
390
391
392
393struct addr_filters {
394 struct list_head head;
395 int cnt;
396};
397
398struct auxtrace_cache;
399
400#ifdef HAVE_AUXTRACE_SUPPORT
401
402
403
404
405
406
407
408static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)
409{
410 struct perf_event_mmap_page *pc = mm->userpg;
411 u64 head = READ_ONCE(pc->aux_head);
412
413
414 rmb();
415 return head;
416}
417
418static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
419{
420 struct perf_event_mmap_page *pc = mm->userpg;
421#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
422 u64 head = READ_ONCE(pc->aux_head);
423#else
424 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
425#endif
426
427
428 rmb();
429 return head;
430}
431
432static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
433{
434 struct perf_event_mmap_page *pc = mm->userpg;
435#if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
436 u64 old_tail;
437#endif
438
439
440 mb();
441#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
442 pc->aux_tail = tail;
443#else
444 do {
445 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0);
446 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail));
447#endif
448}
449
450int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
451 struct auxtrace_mmap_params *mp,
452 void *userpg, int fd);
453void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
454void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
455 off_t auxtrace_offset,
456 unsigned int auxtrace_pages,
457 bool auxtrace_overwrite);
458void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
459 struct evlist *evlist, int idx,
460 bool per_cpu);
461
462typedef int (*process_auxtrace_t)(struct perf_tool *tool,
463 struct mmap *map,
464 union perf_event *event, void *data1,
465 size_t len1, void *data2, size_t len2);
466
467int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
468 struct perf_tool *tool, process_auxtrace_t fn);
469
470int auxtrace_mmap__read_snapshot(struct mmap *map,
471 struct auxtrace_record *itr,
472 struct perf_tool *tool, process_auxtrace_t fn,
473 size_t snapshot_size);
474
475int auxtrace_queues__init(struct auxtrace_queues *queues);
476int auxtrace_queues__add_event(struct auxtrace_queues *queues,
477 struct perf_session *session,
478 union perf_event *event, off_t data_offset,
479 struct auxtrace_buffer **buffer_ptr);
480struct auxtrace_queue *
481auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
482 struct perf_sample *sample,
483 struct perf_session *session);
484int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
485 struct perf_session *session,
486 struct perf_sample *sample, u64 data_offset,
487 u64 reference);
488void auxtrace_queues__free(struct auxtrace_queues *queues);
489int auxtrace_queues__process_index(struct auxtrace_queues *queues,
490 struct perf_session *session);
491int auxtrace_queue_data(struct perf_session *session, bool samples,
492 bool events);
493struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
494 struct auxtrace_buffer *buffer);
495void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd);
496void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer);
497void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer);
498void auxtrace_buffer__free(struct auxtrace_buffer *buffer);
499
500int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
501 u64 ordinal);
502void auxtrace_heap__pop(struct auxtrace_heap *heap);
503void auxtrace_heap__free(struct auxtrace_heap *heap);
504
505struct auxtrace_cache_entry {
506 struct hlist_node hash;
507 u32 key;
508};
509
510struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
511 unsigned int limit_percent);
512void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache);
513void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c);
514void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry);
515int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
516 struct auxtrace_cache_entry *entry);
517void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key);
518void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key);
519
520struct auxtrace_record *auxtrace_record__init(struct evlist *evlist,
521 int *err);
522
523int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
524 struct record_opts *opts,
525 const char *str);
526int auxtrace_parse_sample_options(struct auxtrace_record *itr,
527 struct evlist *evlist,
528 struct record_opts *opts, const char *str);
529int auxtrace_record__options(struct auxtrace_record *itr,
530 struct evlist *evlist,
531 struct record_opts *opts);
532size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
533 struct evlist *evlist);
534int auxtrace_record__info_fill(struct auxtrace_record *itr,
535 struct perf_session *session,
536 struct perf_record_auxtrace_info *auxtrace_info,
537 size_t priv_size);
538void auxtrace_record__free(struct auxtrace_record *itr);
539int auxtrace_record__snapshot_start(struct auxtrace_record *itr);
540int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit);
541int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
542 struct auxtrace_mmap *mm,
543 unsigned char *data, u64 *head, u64 *old);
544u64 auxtrace_record__reference(struct auxtrace_record *itr);
545int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx);
546
547int auxtrace_index__auxtrace_event(struct list_head *head, union perf_event *event,
548 off_t file_offset);
549int auxtrace_index__write(int fd, struct list_head *head);
550int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
551 bool needs_swap);
552void auxtrace_index__free(struct list_head *head);
553
554void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
555 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
556 const char *msg, u64 timestamp);
557
558int perf_event__process_auxtrace_info(struct perf_session *session,
559 union perf_event *event);
560s64 perf_event__process_auxtrace(struct perf_session *session,
561 union perf_event *event);
562int perf_event__process_auxtrace_error(struct perf_session *session,
563 union perf_event *event);
564int itrace_parse_synth_opts(const struct option *opt, const char *str,
565 int unset);
566void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
567 bool no_sample);
568
569size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp);
570void perf_session__auxtrace_error_inc(struct perf_session *session,
571 union perf_event *event);
572void events_stats__auxtrace_error_warn(const struct events_stats *stats);
573
574void addr_filters__init(struct addr_filters *filts);
575void addr_filters__exit(struct addr_filters *filts);
576int addr_filters__parse_bare_filter(struct addr_filters *filts,
577 const char *filter);
578int auxtrace_parse_filters(struct evlist *evlist);
579
580int auxtrace__process_event(struct perf_session *session, union perf_event *event,
581 struct perf_sample *sample, struct perf_tool *tool);
582void auxtrace__dump_auxtrace_sample(struct perf_session *session,
583 struct perf_sample *sample);
584int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool);
585void auxtrace__free_events(struct perf_session *session);
586void auxtrace__free(struct perf_session *session);
587
588#define ITRACE_HELP \
589" i: synthesize instructions events\n" \
590" b: synthesize branches events\n" \
591" c: synthesize branches events (calls only)\n" \
592" r: synthesize branches events (returns only)\n" \
593" x: synthesize transactions events\n" \
594" w: synthesize ptwrite events\n" \
595" p: synthesize power events\n" \
596" e: synthesize error events\n" \
597" d: create a debug log\n" \
598" g[len]: synthesize a call chain (use with i or x)\n" \
599" l[len]: synthesize last branch entries (use with i or x)\n" \
600" sNUMBER: skip initial number of events\n" \
601" PERIOD[ns|us|ms|i|t]: specify period to sample stream\n" \
602" concatenate multiple options. Default is ibxwpe or cewp\n"
603
604static inline
605void itrace_synth_opts__set_time_range(struct itrace_synth_opts *opts,
606 struct perf_time_interval *ptime_range,
607 int range_num)
608{
609 opts->ptime_range = ptime_range;
610 opts->range_num = range_num;
611}
612
613static inline
614void itrace_synth_opts__clear_time_range(struct itrace_synth_opts *opts)
615{
616 opts->ptime_range = NULL;
617 opts->range_num = 0;
618}
619
620#else
621#include "debug.h"
622
623static inline struct auxtrace_record *
624auxtrace_record__init(struct evlist *evlist __maybe_unused,
625 int *err)
626{
627 *err = 0;
628 return NULL;
629}
630
631static inline
632void auxtrace_record__free(struct auxtrace_record *itr __maybe_unused)
633{
634}
635
636static inline
637int auxtrace_record__options(struct auxtrace_record *itr __maybe_unused,
638 struct evlist *evlist __maybe_unused,
639 struct record_opts *opts __maybe_unused)
640{
641 return 0;
642}
643
644#define perf_event__process_auxtrace_info 0
645#define perf_event__process_auxtrace 0
646#define perf_event__process_auxtrace_error 0
647
648static inline
649void perf_session__auxtrace_error_inc(struct perf_session *session
650 __maybe_unused,
651 union perf_event *event
652 __maybe_unused)
653{
654}
655
656static inline
657void events_stats__auxtrace_error_warn(const struct events_stats *stats
658 __maybe_unused)
659{
660}
661
662static inline
663int itrace_parse_synth_opts(const struct option *opt __maybe_unused,
664 const char *str __maybe_unused,
665 int unset __maybe_unused)
666{
667 pr_err("AUX area tracing not supported\n");
668 return -EINVAL;
669}
670
671static inline
672int auxtrace_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
673 struct record_opts *opts __maybe_unused,
674 const char *str)
675{
676 if (!str)
677 return 0;
678 pr_err("AUX area tracing not supported\n");
679 return -EINVAL;
680}
681
682static inline
683int auxtrace_parse_sample_options(struct auxtrace_record *itr __maybe_unused,
684 struct evlist *evlist __maybe_unused,
685 struct record_opts *opts __maybe_unused,
686 const char *str)
687{
688 if (!str)
689 return 0;
690 pr_err("AUX area tracing not supported\n");
691 return -EINVAL;
692}
693
694static inline
695int auxtrace__process_event(struct perf_session *session __maybe_unused,
696 union perf_event *event __maybe_unused,
697 struct perf_sample *sample __maybe_unused,
698 struct perf_tool *tool __maybe_unused)
699{
700 return 0;
701}
702
703static inline
704void auxtrace__dump_auxtrace_sample(struct perf_session *session __maybe_unused,
705 struct perf_sample *sample __maybe_unused)
706{
707}
708
709static inline
710int auxtrace__flush_events(struct perf_session *session __maybe_unused,
711 struct perf_tool *tool __maybe_unused)
712{
713 return 0;
714}
715
716static inline
717void auxtrace__free_events(struct perf_session *session __maybe_unused)
718{
719}
720
721static inline
722void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache __maybe_unused)
723{
724}
725
726static inline
727void auxtrace__free(struct perf_session *session __maybe_unused)
728{
729}
730
731static inline
732int auxtrace_index__write(int fd __maybe_unused,
733 struct list_head *head __maybe_unused)
734{
735 return -EINVAL;
736}
737
738static inline
739int auxtrace_index__process(int fd __maybe_unused,
740 u64 size __maybe_unused,
741 struct perf_session *session __maybe_unused,
742 bool needs_swap __maybe_unused)
743{
744 return -EINVAL;
745}
746
747static inline
748void auxtrace_index__free(struct list_head *head __maybe_unused)
749{
750}
751
752static inline
753int auxtrace_parse_filters(struct evlist *evlist __maybe_unused)
754{
755 return 0;
756}
757
758int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
759 struct auxtrace_mmap_params *mp,
760 void *userpg, int fd);
761void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
762void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
763 off_t auxtrace_offset,
764 unsigned int auxtrace_pages,
765 bool auxtrace_overwrite);
766void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
767 struct evlist *evlist, int idx,
768 bool per_cpu);
769
770#define ITRACE_HELP ""
771
772static inline
773void itrace_synth_opts__set_time_range(struct itrace_synth_opts *opts
774 __maybe_unused,
775 struct perf_time_interval *ptime_range
776 __maybe_unused,
777 int range_num __maybe_unused)
778{
779}
780
781static inline
782void itrace_synth_opts__clear_time_range(struct itrace_synth_opts *opts
783 __maybe_unused)
784{
785}
786
787#endif
788
789#endif
790