mmap.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef __PERF_MMAP_H
  2. #define __PERF_MMAP_H 1
  3. #include <linux/compiler.h>
  4. #include <linux/refcount.h>
  5. #include <linux/types.h>
  6. #include <asm/barrier.h>
  7. #include <stdbool.h>
  8. #include "auxtrace.h"
  9. #include "event.h"
  10. /**
  11. * struct perf_mmap - perf's ring buffer mmap details
  12. *
  13. * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
  14. */
  15. struct perf_mmap {
  16. void *base;
  17. int mask;
  18. int fd;
  19. int cpu;
  20. refcount_t refcnt;
  21. u64 prev;
  22. u64 start;
  23. u64 end;
  24. bool overwrite;
  25. struct auxtrace_mmap auxtrace_mmap;
  26. char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
  27. };
  28. /*
  29. * State machine of bkw_mmap_state:
  30. *
  31. * .________________(forbid)_____________.
  32. * | V
  33. * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
  34. * ^ ^ | ^ |
  35. * | |__(forbid)____/ |___(forbid)___/|
  36. * | |
  37. * \_________________(3)_______________/
  38. *
  39. * NOTREADY : Backward ring buffers are not ready
  40. * RUNNING : Backward ring buffers are recording
  41. * DATA_PENDING : We are required to collect data from backward ring buffers
  42. * EMPTY : We have collected data from backward ring buffers.
  43. *
  44. * (0): Setup backward ring buffer
  45. * (1): Pause ring buffers for reading
  46. * (2): Read from ring buffers
  47. * (3): Resume ring buffers for recording
  48. */
  49. enum bkw_mmap_state {
  50. BKW_MMAP_NOTREADY,
  51. BKW_MMAP_RUNNING,
  52. BKW_MMAP_DATA_PENDING,
  53. BKW_MMAP_EMPTY,
  54. };
  55. struct mmap_params {
  56. int prot, mask;
  57. struct auxtrace_mmap_params auxtrace_mp;
  58. };
  59. int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu);
  60. void perf_mmap__munmap(struct perf_mmap *map);
  61. void perf_mmap__get(struct perf_mmap *map);
  62. void perf_mmap__put(struct perf_mmap *map);
  63. void perf_mmap__consume(struct perf_mmap *map);
  64. static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
  65. {
  66. struct perf_event_mmap_page *pc = mm->base;
  67. u64 head = READ_ONCE(pc->data_head);
  68. rmb();
  69. return head;
  70. }
  71. static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
  72. {
  73. struct perf_event_mmap_page *pc = md->base;
  74. /*
  75. * ensure all reads are done before we write the tail out.
  76. */
  77. mb();
  78. pc->data_tail = tail;
  79. }
  80. union perf_event *perf_mmap__read_forward(struct perf_mmap *map);
  81. union perf_event *perf_mmap__read_event(struct perf_mmap *map);
  82. int perf_mmap__push(struct perf_mmap *md, void *to,
  83. int push(void *to, void *buf, size_t size));
  84. size_t perf_mmap__mmap_len(struct perf_mmap *map);
  85. int perf_mmap__read_init(struct perf_mmap *md);
  86. void perf_mmap__read_done(struct perf_mmap *map);
  87. #endif /*__PERF_MMAP_H */