xref: /linux/tools/perf/util/mmap.h (revision 4e0ae876f77bc01a7e77724dea57b4b82bd53244)
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 	cpu_set_t	affinity_mask;
42 };
43 
44 /*
45  * State machine of bkw_mmap_state:
46  *
47  *                     .________________(forbid)_____________.
48  *                     |                                     V
49  * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
50  *                     ^  ^              |   ^               |
51  *                     |  |__(forbid)____/   |___(forbid)___/|
52  *                     |                                     |
53  *                      \_________________(3)_______________/
54  *
55  * NOTREADY     : Backward ring buffers are not ready
56  * RUNNING      : Backward ring buffers are recording
57  * DATA_PENDING : We are required to collect data from backward ring buffers
58  * EMPTY        : We have collected data from backward ring buffers.
59  *
60  * (0): Setup backward ring buffer
61  * (1): Pause ring buffers for reading
62  * (2): Read from ring buffers
63  * (3): Resume ring buffers for recording
64  */
65 enum bkw_mmap_state {
66 	BKW_MMAP_NOTREADY,
67 	BKW_MMAP_RUNNING,
68 	BKW_MMAP_DATA_PENDING,
69 	BKW_MMAP_EMPTY,
70 };
71 
72 struct mmap_params {
73 	int			    prot, mask, nr_cblocks, affinity;
74 	struct auxtrace_mmap_params auxtrace_mp;
75 };
76 
77 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu);
78 void perf_mmap__munmap(struct perf_mmap *map);
79 
80 void perf_mmap__get(struct perf_mmap *map);
81 void perf_mmap__put(struct perf_mmap *map);
82 
83 void perf_mmap__consume(struct perf_mmap *map);
84 
85 static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
86 {
87 	return ring_buffer_read_head(mm->base);
88 }
89 
90 static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
91 {
92 	ring_buffer_write_tail(md->base, tail);
93 }
94 
95 union perf_event *perf_mmap__read_forward(struct perf_mmap *map);
96 
97 union perf_event *perf_mmap__read_event(struct perf_mmap *map);
98 
99 int perf_mmap__push(struct perf_mmap *md, void *to,
100 		    int push(struct perf_mmap *map, void *to, void *buf, size_t size));
101 #ifdef HAVE_AIO_SUPPORT
102 int perf_mmap__aio_push(struct perf_mmap *md, void *to, int idx,
103 			int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off),
104 			off_t *off);
105 #else
106 static inline int perf_mmap__aio_push(struct perf_mmap *md __maybe_unused, void *to __maybe_unused, int idx __maybe_unused,
107 	int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off) __maybe_unused,
108 	off_t *off __maybe_unused)
109 {
110 	return 0;
111 }
112 #endif
113 
114 size_t perf_mmap__mmap_len(struct perf_mmap *map);
115 
116 int perf_mmap__read_init(struct perf_mmap *md);
117 void perf_mmap__read_done(struct perf_mmap *map);
118 #endif /*__PERF_MMAP_H */
119