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> |
| 7 | #include <asm/barrier.h> |
| 8 | #include <stdbool.h> |
| 9 | #include "auxtrace.h" |
| 10 | #include "event.h" |
| 11 | |
| 12 | /** |
| 13 | * struct perf_mmap - perf's ring buffer mmap details |
| 14 | * |
| 15 | * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this |
| 16 | */ |
| 17 | struct perf_mmap { |
| 18 | void *base; |
| 19 | int mask; |
| 20 | int fd; |
| 21 | refcount_t refcnt; |
| 22 | u64 prev; |
Kan Liang | 4fda345 | 2018-03-06 10:36:01 -0500 | [diff] [blame] | 23 | u64 start; |
| 24 | u64 end; |
Kan Liang | 2c5f6d8 | 2018-03-06 10:36:00 -0500 | [diff] [blame] | 25 | bool overwrite; |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 26 | struct auxtrace_mmap auxtrace_mmap; |
| 27 | char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * State machine of bkw_mmap_state: |
| 32 | * |
| 33 | * .________________(forbid)_____________. |
| 34 | * | V |
| 35 | * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY |
| 36 | * ^ ^ | ^ | |
| 37 | * | |__(forbid)____/ |___(forbid)___/| |
| 38 | * | | |
| 39 | * \_________________(3)_______________/ |
| 40 | * |
| 41 | * NOTREADY : Backward ring buffers are not ready |
| 42 | * RUNNING : Backward ring buffers are recording |
| 43 | * DATA_PENDING : We are required to collect data from backward ring buffers |
| 44 | * EMPTY : We have collected data from backward ring buffers. |
| 45 | * |
| 46 | * (0): Setup backward ring buffer |
| 47 | * (1): Pause ring buffers for reading |
| 48 | * (2): Read from ring buffers |
| 49 | * (3): Resume ring buffers for recording |
| 50 | */ |
| 51 | enum bkw_mmap_state { |
| 52 | BKW_MMAP_NOTREADY, |
| 53 | BKW_MMAP_RUNNING, |
| 54 | BKW_MMAP_DATA_PENDING, |
| 55 | BKW_MMAP_EMPTY, |
| 56 | }; |
| 57 | |
| 58 | struct mmap_params { |
| 59 | int prot, mask; |
| 60 | struct auxtrace_mmap_params auxtrace_mp; |
| 61 | }; |
| 62 | |
| 63 | int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd); |
| 64 | void perf_mmap__munmap(struct perf_mmap *map); |
| 65 | |
| 66 | void perf_mmap__get(struct perf_mmap *map); |
| 67 | void perf_mmap__put(struct perf_mmap *map); |
| 68 | |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 69 | void perf_mmap__consume(struct perf_mmap *map); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 70 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 71 | static inline u64 perf_mmap__read_head(struct perf_mmap *mm) |
| 72 | { |
| 73 | struct perf_event_mmap_page *pc = mm->base; |
Mark Rutland | f971e51 | 2017-11-27 10:38:21 +0000 | [diff] [blame] | 74 | u64 head = READ_ONCE(pc->data_head); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 75 | rmb(); |
| 76 | return head; |
| 77 | } |
| 78 | |
| 79 | static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail) |
| 80 | { |
| 81 | struct perf_event_mmap_page *pc = md->base; |
| 82 | |
| 83 | /* |
| 84 | * ensure all reads are done before we write the tail out. |
| 85 | */ |
| 86 | mb(); |
| 87 | pc->data_tail = tail; |
| 88 | } |
| 89 | |
Wang Nan | 8eb7a1f | 2017-12-03 02:00:41 +0000 | [diff] [blame] | 90 | 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] | 91 | |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 92 | union perf_event *perf_mmap__read_event(struct perf_mmap *map); |
Kan Liang | 7bb4597 | 2018-01-18 13:26:23 -0800 | [diff] [blame] | 93 | |
Kan Liang | 07a9461 | 2018-03-06 10:36:02 -0500 | [diff] [blame] | 94 | int perf_mmap__push(struct perf_mmap *md, void *to, |
| 95 | int push(void *to, void *buf, size_t size)); |
Arnaldo Carvalho de Melo | 73c17d8 | 2017-10-06 10:46:01 -0300 | [diff] [blame] | 96 | |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 97 | size_t perf_mmap__mmap_len(struct perf_mmap *map); |
| 98 | |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame^] | 99 | int perf_mmap__read_init(struct perf_mmap *md); |
Kan Liang | ee023de | 2018-01-18 13:26:22 -0800 | [diff] [blame] | 100 | void perf_mmap__read_done(struct perf_mmap *map); |
Arnaldo Carvalho de Melo | 1695849 | 2017-10-06 10:31:47 -0300 | [diff] [blame] | 101 | #endif /*__PERF_MMAP_H */ |