xref: /openbmc/linux/tools/perf/util/mmap.h (revision 8eb7a1fe)
116958497SArnaldo Carvalho de Melo #ifndef __PERF_MMAP_H
216958497SArnaldo Carvalho de Melo #define __PERF_MMAP_H 1
316958497SArnaldo Carvalho de Melo 
416958497SArnaldo Carvalho de Melo #include <linux/compiler.h>
516958497SArnaldo Carvalho de Melo #include <linux/refcount.h>
616958497SArnaldo Carvalho de Melo #include <linux/types.h>
716958497SArnaldo Carvalho de Melo #include <asm/barrier.h>
816958497SArnaldo Carvalho de Melo #include <stdbool.h>
916958497SArnaldo Carvalho de Melo #include "auxtrace.h"
1016958497SArnaldo Carvalho de Melo #include "event.h"
1116958497SArnaldo Carvalho de Melo 
1216958497SArnaldo Carvalho de Melo /**
1316958497SArnaldo Carvalho de Melo  * struct perf_mmap - perf's ring buffer mmap details
1416958497SArnaldo Carvalho de Melo  *
1516958497SArnaldo Carvalho de Melo  * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
1616958497SArnaldo Carvalho de Melo  */
1716958497SArnaldo Carvalho de Melo struct perf_mmap {
1816958497SArnaldo Carvalho de Melo 	void		 *base;
1916958497SArnaldo Carvalho de Melo 	int		 mask;
2016958497SArnaldo Carvalho de Melo 	int		 fd;
2116958497SArnaldo Carvalho de Melo 	refcount_t	 refcnt;
2216958497SArnaldo Carvalho de Melo 	u64		 prev;
2316958497SArnaldo Carvalho de Melo 	struct auxtrace_mmap auxtrace_mmap;
2416958497SArnaldo Carvalho de Melo 	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
2516958497SArnaldo Carvalho de Melo };
2616958497SArnaldo Carvalho de Melo 
2716958497SArnaldo Carvalho de Melo /*
2816958497SArnaldo Carvalho de Melo  * State machine of bkw_mmap_state:
2916958497SArnaldo Carvalho de Melo  *
3016958497SArnaldo Carvalho de Melo  *                     .________________(forbid)_____________.
3116958497SArnaldo Carvalho de Melo  *                     |                                     V
3216958497SArnaldo Carvalho de Melo  * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
3316958497SArnaldo Carvalho de Melo  *                     ^  ^              |   ^               |
3416958497SArnaldo Carvalho de Melo  *                     |  |__(forbid)____/   |___(forbid)___/|
3516958497SArnaldo Carvalho de Melo  *                     |                                     |
3616958497SArnaldo Carvalho de Melo  *                      \_________________(3)_______________/
3716958497SArnaldo Carvalho de Melo  *
3816958497SArnaldo Carvalho de Melo  * NOTREADY     : Backward ring buffers are not ready
3916958497SArnaldo Carvalho de Melo  * RUNNING      : Backward ring buffers are recording
4016958497SArnaldo Carvalho de Melo  * DATA_PENDING : We are required to collect data from backward ring buffers
4116958497SArnaldo Carvalho de Melo  * EMPTY        : We have collected data from backward ring buffers.
4216958497SArnaldo Carvalho de Melo  *
4316958497SArnaldo Carvalho de Melo  * (0): Setup backward ring buffer
4416958497SArnaldo Carvalho de Melo  * (1): Pause ring buffers for reading
4516958497SArnaldo Carvalho de Melo  * (2): Read from ring buffers
4616958497SArnaldo Carvalho de Melo  * (3): Resume ring buffers for recording
4716958497SArnaldo Carvalho de Melo  */
4816958497SArnaldo Carvalho de Melo enum bkw_mmap_state {
4916958497SArnaldo Carvalho de Melo 	BKW_MMAP_NOTREADY,
5016958497SArnaldo Carvalho de Melo 	BKW_MMAP_RUNNING,
5116958497SArnaldo Carvalho de Melo 	BKW_MMAP_DATA_PENDING,
5216958497SArnaldo Carvalho de Melo 	BKW_MMAP_EMPTY,
5316958497SArnaldo Carvalho de Melo };
5416958497SArnaldo Carvalho de Melo 
5516958497SArnaldo Carvalho de Melo struct mmap_params {
5616958497SArnaldo Carvalho de Melo 	int			    prot, mask;
5716958497SArnaldo Carvalho de Melo 	struct auxtrace_mmap_params auxtrace_mp;
5816958497SArnaldo Carvalho de Melo };
5916958497SArnaldo Carvalho de Melo 
6016958497SArnaldo Carvalho de Melo int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd);
6116958497SArnaldo Carvalho de Melo void perf_mmap__munmap(struct perf_mmap *map);
6216958497SArnaldo Carvalho de Melo 
6316958497SArnaldo Carvalho de Melo void perf_mmap__get(struct perf_mmap *map);
6416958497SArnaldo Carvalho de Melo void perf_mmap__put(struct perf_mmap *map);
6516958497SArnaldo Carvalho de Melo 
6616958497SArnaldo Carvalho de Melo void perf_mmap__consume(struct perf_mmap *map, bool overwrite);
6716958497SArnaldo Carvalho de Melo 
6816958497SArnaldo Carvalho de Melo void perf_mmap__read_catchup(struct perf_mmap *md);
6916958497SArnaldo Carvalho de Melo 
7016958497SArnaldo Carvalho de Melo static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
7116958497SArnaldo Carvalho de Melo {
7216958497SArnaldo Carvalho de Melo 	struct perf_event_mmap_page *pc = mm->base;
7316958497SArnaldo Carvalho de Melo 	u64 head = ACCESS_ONCE(pc->data_head);
7416958497SArnaldo Carvalho de Melo 	rmb();
7516958497SArnaldo Carvalho de Melo 	return head;
7616958497SArnaldo Carvalho de Melo }
7716958497SArnaldo Carvalho de Melo 
7816958497SArnaldo Carvalho de Melo static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
7916958497SArnaldo Carvalho de Melo {
8016958497SArnaldo Carvalho de Melo 	struct perf_event_mmap_page *pc = md->base;
8116958497SArnaldo Carvalho de Melo 
8216958497SArnaldo Carvalho de Melo 	/*
8316958497SArnaldo Carvalho de Melo 	 * ensure all reads are done before we write the tail out.
8416958497SArnaldo Carvalho de Melo 	 */
8516958497SArnaldo Carvalho de Melo 	mb();
8616958497SArnaldo Carvalho de Melo 	pc->data_tail = tail;
8716958497SArnaldo Carvalho de Melo }
8816958497SArnaldo Carvalho de Melo 
898eb7a1feSWang Nan union perf_event *perf_mmap__read_forward(struct perf_mmap *map);
9016958497SArnaldo Carvalho de Melo union perf_event *perf_mmap__read_backward(struct perf_mmap *map);
9116958497SArnaldo Carvalho de Melo 
92ca6a9a05SWang Nan int perf_mmap__push(struct perf_mmap *md, bool backward,
9373c17d81SArnaldo Carvalho de Melo 		    void *to, int push(void *to, void *buf, size_t size));
9473c17d81SArnaldo Carvalho de Melo 
9516958497SArnaldo Carvalho de Melo size_t perf_mmap__mmap_len(struct perf_mmap *map);
9616958497SArnaldo Carvalho de Melo 
9716958497SArnaldo Carvalho de Melo #endif /*__PERF_MMAP_H */
98