xref: /openbmc/linux/tools/perf/util/mmap.h (revision 6d99a79c)
1 #ifndef __PERF_MMAP_H
2 #define __PERF_MMAP_H 1
3 
4 #include <linux/compiler.h>
5 #include <linux/refcount.h>
6 #include <linux/types.h>
7 #include <linux/ring_buffer.h>
8 #include <stdbool.h>
9 #ifdef HAVE_AIO_SUPPORT
10 #include <aio.h>
11 #endif
12 #include "auxtrace.h"
13 #include "event.h"
14 
15 struct aiocb;
16 /**
17  * struct perf_mmap - perf's ring buffer mmap details
18  *
19  * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
20  */
21 struct perf_mmap {
22 	void		 *base;
23 	int		 mask;
24 	int		 fd;
25 	int		 cpu;
26 	refcount_t	 refcnt;
27 	u64		 prev;
28 	u64		 start;
29 	u64		 end;
30 	bool		 overwrite;
31 	struct auxtrace_mmap auxtrace_mmap;
32 	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
33 #ifdef HAVE_AIO_SUPPORT
34 	struct {
35 		void		 **data;
36 		struct aiocb	 *cblocks;
37 		struct aiocb	 **aiocb;
38 		int		 nr_cblocks;
39 	} aio;
40 #endif
41 };
42 
43 /*
44  * State machine of bkw_mmap_state:
45  *
46  *                     .________________(forbid)_____________.
47  *                     |                                     V
48  * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
49  *                     ^  ^              |   ^               |
50  *                     |  |__(forbid)____/   |___(forbid)___/|
51  *                     |                                     |
52  *                      \_________________(3)_______________/
53  *
54  * NOTREADY     : Backward ring buffers are not ready
55  * RUNNING      : Backward ring buffers are recording
56  * DATA_PENDING : We are required to collect data from backward ring buffers
57  * EMPTY        : We have collected data from backward ring buffers.
58  *
59  * (0): Setup backward ring buffer
60  * (1): Pause ring buffers for reading
61  * (2): Read from ring buffers
62  * (3): Resume ring buffers for recording
63  */
64 enum bkw_mmap_state {
65 	BKW_MMAP_NOTREADY,
66 	BKW_MMAP_RUNNING,
67 	BKW_MMAP_DATA_PENDING,
68 	BKW_MMAP_EMPTY,
69 };
70 
71 struct mmap_params {
72 	int			    prot, mask, nr_cblocks;
73 	struct auxtrace_mmap_params auxtrace_mp;
74 };
75 
76 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu);
77 void perf_mmap__munmap(struct perf_mmap *map);
78 
79 void perf_mmap__get(struct perf_mmap *map);
80 void perf_mmap__put(struct perf_mmap *map);
81 
82 void perf_mmap__consume(struct perf_mmap *map);
83 
84 static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
85 {
86 	return ring_buffer_read_head(mm->base);
87 }
88 
89 static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
90 {
91 	ring_buffer_write_tail(md->base, tail);
92 }
93 
94 union perf_event *perf_mmap__read_forward(struct perf_mmap *map);
95 
96 union perf_event *perf_mmap__read_event(struct perf_mmap *map);
97 
98 int perf_mmap__push(struct perf_mmap *md, void *to,
99 		    int push(struct perf_mmap *map, void *to, void *buf, size_t size));
100 #ifdef HAVE_AIO_SUPPORT
101 int perf_mmap__aio_push(struct perf_mmap *md, void *to, int idx,
102 			int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off),
103 			off_t *off);
104 #else
105 static inline int perf_mmap__aio_push(struct perf_mmap *md __maybe_unused, void *to __maybe_unused, int idx __maybe_unused,
106 	int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off) __maybe_unused,
107 	off_t *off __maybe_unused)
108 {
109 	return 0;
110 }
111 #endif
112 
113 size_t perf_mmap__mmap_len(struct perf_mmap *map);
114 
115 int perf_mmap__read_init(struct perf_mmap *md);
116 void perf_mmap__read_done(struct perf_mmap *map);
117 #endif /*__PERF_MMAP_H */
118