Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 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> |
Daniel Borkmann | 09d62154 | 2018-10-19 15:51:02 +0200 | [diff] [blame] | 7 | #include <linux/ring_buffer.h> |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 8 | #include <stdbool.h> |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 9 | #ifdef HAVE_AIO_SUPPORT |
| 10 | #include <aio.h> |
| 11 | #endif |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 12 | #include "auxtrace.h" |
| 13 | #include "event.h" |
| 14 | |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame^] | 15 | struct aiocb; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 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; |
Jiri Olsa | 31fb4c0 | 2018-08-17 13:45:55 +0200 | [diff] [blame] | 25 | int cpu; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 26 | refcount_t refcnt; |
| 27 | u64 prev; |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 28 | u64 start; |
| 29 | u64 end; |
Kan Liang | 2c5f6d8 | 2018-03-06 10:36:00 -0500 | [diff] [blame] | 30 | bool overwrite; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 31 | struct auxtrace_mmap auxtrace_mmap; |
| 32 | char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 33 | #ifdef HAVE_AIO_SUPPORT |
| 34 | struct { |
| 35 | void *data; |
| 36 | struct aiocb cblock; |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame^] | 37 | int nr_cblocks; |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 38 | } aio; |
| 39 | #endif |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * State machine of bkw_mmap_state: |
| 44 | * |
| 45 | * .________________(forbid)_____________. |
| 46 | * | V |
| 47 | * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY |
| 48 | * ^ ^ | ^ | |
| 49 | * | |__(forbid)____/ |___(forbid)___/| |
| 50 | * | | |
| 51 | * \_________________(3)_______________/ |
| 52 | * |
| 53 | * NOTREADY : Backward ring buffers are not ready |
| 54 | * RUNNING : Backward ring buffers are recording |
| 55 | * DATA_PENDING : We are required to collect data from backward ring buffers |
| 56 | * EMPTY : We have collected data from backward ring buffers. |
| 57 | * |
| 58 | * (0): Setup backward ring buffer |
| 59 | * (1): Pause ring buffers for reading |
| 60 | * (2): Read from ring buffers |
| 61 | * (3): Resume ring buffers for recording |
| 62 | */ |
| 63 | enum bkw_mmap_state { |
| 64 | BKW_MMAP_NOTREADY, |
| 65 | BKW_MMAP_RUNNING, |
| 66 | BKW_MMAP_DATA_PENDING, |
| 67 | BKW_MMAP_EMPTY, |
| 68 | }; |
| 69 | |
| 70 | struct mmap_params { |
Alexey Budankov | 0b77383 | 2018-11-06 12:03:35 +0300 | [diff] [blame] | 71 | int prot, mask, nr_cblocks; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 72 | struct auxtrace_mmap_params auxtrace_mp; |
| 73 | }; |
| 74 | |
Jiri Olsa | 31fb4c0 | 2018-08-17 13:45:55 +0200 | [diff] [blame] | 75 | int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 76 | void perf_mmap__munmap(struct perf_mmap *map); |
| 77 | |
| 78 | void perf_mmap__get(struct perf_mmap *map); |
| 79 | void perf_mmap__put(struct perf_mmap *map); |
| 80 | |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 81 | void perf_mmap__consume(struct perf_mmap *map); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 82 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 83 | static inline u64 perf_mmap__read_head(struct perf_mmap *mm) |
| 84 | { |
Daniel Borkmann | 09d62154 | 2018-10-19 15:51:02 +0200 | [diff] [blame] | 85 | return ring_buffer_read_head(mm->base); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail) |
| 89 | { |
Daniel Borkmann | 09d62154 | 2018-10-19 15:51:02 +0200 | [diff] [blame] | 90 | ring_buffer_write_tail(md->base, tail); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 91 | } |
| 92 | |
Wang Nan | 8eb7a1f | 2017-12-03 02:00:41 +0000 | [diff] [blame] | 93 | union perf_event *perf_mmap__read_forward(struct perf_mmap *map); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 94 | |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 95 | union perf_event *perf_mmap__read_event(struct perf_mmap *map); |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 96 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 97 | int perf_mmap__push(struct perf_mmap *md, void *to, |
Jiri Olsa | ded2b8f | 2018-09-13 14:54:06 +0200 | [diff] [blame] | 98 | int push(struct perf_mmap *map, void *to, void *buf, size_t size)); |
Alexey Budankov | d3d1af6 | 2018-11-06 12:04:58 +0300 | [diff] [blame^] | 99 | #ifdef HAVE_AIO_SUPPORT |
| 100 | int perf_mmap__aio_push(struct perf_mmap *md, void *to, |
| 101 | int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off), |
| 102 | off_t *off); |
| 103 | #else |
| 104 | static inline int perf_mmap__aio_push(struct perf_mmap *md __maybe_unused, void *to __maybe_unused, |
| 105 | int push(void *to, struct aiocb *cblock, void *buf, size_t size, off_t off) __maybe_unused, |
| 106 | off_t *off __maybe_unused) |
| 107 | { |
| 108 | return 0; |
| 109 | } |
| 110 | #endif |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 111 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 112 | size_t perf_mmap__mmap_len(struct perf_mmap *map); |
| 113 | |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame] | 114 | int perf_mmap__read_init(struct perf_mmap *md); |
Kan Liang | ee023de | 2018-01-18 13:26:22 -0800 | [diff] [blame] | 115 | void perf_mmap__read_done(struct perf_mmap *map); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 116 | #endif /*__PERF_MMAP_H */ |