ring_buffer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef _LINUX_RING_BUFFER_H
  2. #define _LINUX_RING_BUFFER_H
  3. #include <linux/kmemcheck.h>
  4. #include <linux/mm.h>
  5. #include <linux/seq_file.h>
  6. #include <linux/poll.h>
  7. struct ring_buffer;
  8. struct ring_buffer_iter;
  9. /*
  10. * Don't refer to this struct directly, use functions below.
  11. */
  12. struct ring_buffer_event {
  13. kmemcheck_bitfield_begin(bitfield);
  14. u32 type_len:5, time_delta:27;
  15. kmemcheck_bitfield_end(bitfield);
  16. u32 array[];
  17. };
  18. /**
  19. * enum ring_buffer_type - internal ring buffer types
  20. *
  21. * @RINGBUF_TYPE_PADDING: Left over page padding or discarded event
  22. * If time_delta is 0:
  23. * array is ignored
  24. * size is variable depending on how much
  25. * padding is needed
  26. * If time_delta is non zero:
  27. * array[0] holds the actual length
  28. * size = 4 + length (bytes)
  29. *
  30. * @RINGBUF_TYPE_TIME_EXTEND: Extend the time delta
  31. * array[0] = time delta (28 .. 59)
  32. * size = 8 bytes
  33. *
  34. * @RINGBUF_TYPE_TIME_STAMP: Sync time stamp with external clock
  35. * array[0] = tv_nsec
  36. * array[1..2] = tv_sec
  37. * size = 16 bytes
  38. *
  39. * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX:
  40. * Data record
  41. * If type_len is zero:
  42. * array[0] holds the actual length
  43. * array[1..(length+3)/4] holds data
  44. * size = 4 + length (bytes)
  45. * else
  46. * length = type_len << 2
  47. * array[0..(length+3)/4-1] holds data
  48. * size = 4 + length (bytes)
  49. */
  50. enum ring_buffer_type {
  51. RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28,
  52. RINGBUF_TYPE_PADDING,
  53. RINGBUF_TYPE_TIME_EXTEND,
  54. /* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */
  55. RINGBUF_TYPE_TIME_STAMP,
  56. };
  57. unsigned ring_buffer_event_length(struct ring_buffer_event *event);
  58. void *ring_buffer_event_data(struct ring_buffer_event *event);
  59. /*
  60. * ring_buffer_discard_commit will remove an event that has not
  61. * ben committed yet. If this is used, then ring_buffer_unlock_commit
  62. * must not be called on the discarded event. This function
  63. * will try to remove the event from the ring buffer completely
  64. * if another event has not been written after it.
  65. *
  66. * Example use:
  67. *
  68. * if (some_condition)
  69. * ring_buffer_discard_commit(buffer, event);
  70. * else
  71. * ring_buffer_unlock_commit(buffer, event);
  72. */
  73. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  74. struct ring_buffer_event *event);
  75. /*
  76. * size is in bytes for each per CPU buffer.
  77. */
  78. struct ring_buffer *
  79. __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key);
  80. /*
  81. * Because the ring buffer is generic, if other users of the ring buffer get
  82. * traced by ftrace, it can produce lockdep warnings. We need to keep each
  83. * ring buffer's lock class separate.
  84. */
  85. #define ring_buffer_alloc(size, flags) \
  86. ({ \
  87. static struct lock_class_key __key; \
  88. __ring_buffer_alloc((size), (flags), &__key); \
  89. })
  90. int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full);
  91. int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
  92. struct file *filp, poll_table *poll_table);
  93. #define RING_BUFFER_ALL_CPUS -1
  94. void ring_buffer_free(struct ring_buffer *buffer);
  95. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size, int cpu);
  96. void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val);
  97. struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer,
  98. unsigned long length);
  99. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  100. struct ring_buffer_event *event);
  101. int ring_buffer_write(struct ring_buffer *buffer,
  102. unsigned long length, void *data);
  103. struct ring_buffer_event *
  104. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
  105. unsigned long *lost_events);
  106. struct ring_buffer_event *
  107. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
  108. unsigned long *lost_events);
  109. struct ring_buffer_iter *
  110. ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu, gfp_t flags);
  111. void ring_buffer_read_prepare_sync(void);
  112. void ring_buffer_read_start(struct ring_buffer_iter *iter);
  113. void ring_buffer_read_finish(struct ring_buffer_iter *iter);
  114. struct ring_buffer_event *
  115. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
  116. struct ring_buffer_event *
  117. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts);
  118. void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
  119. int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
  120. unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu);
  121. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu);
  122. void ring_buffer_reset(struct ring_buffer *buffer);
  123. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  124. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  125. struct ring_buffer *buffer_b, int cpu);
  126. #else
  127. static inline int
  128. ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  129. struct ring_buffer *buffer_b, int cpu)
  130. {
  131. return -ENODEV;
  132. }
  133. #endif
  134. bool ring_buffer_empty(struct ring_buffer *buffer);
  135. bool ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu);
  136. void ring_buffer_record_disable(struct ring_buffer *buffer);
  137. void ring_buffer_record_enable(struct ring_buffer *buffer);
  138. void ring_buffer_record_off(struct ring_buffer *buffer);
  139. void ring_buffer_record_on(struct ring_buffer *buffer);
  140. int ring_buffer_record_is_on(struct ring_buffer *buffer);
  141. int ring_buffer_record_is_set_on(struct ring_buffer *buffer);
  142. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu);
  143. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu);
  144. u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu);
  145. unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu);
  146. unsigned long ring_buffer_entries(struct ring_buffer *buffer);
  147. unsigned long ring_buffer_overruns(struct ring_buffer *buffer);
  148. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu);
  149. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu);
  150. unsigned long ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu);
  151. unsigned long ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu);
  152. unsigned long ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu);
  153. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu);
  154. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  155. int cpu, u64 *ts);
  156. void ring_buffer_set_clock(struct ring_buffer *buffer,
  157. u64 (*clock)(void));
  158. size_t ring_buffer_page_len(void *page);
  159. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu);
  160. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data);
  161. int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page,
  162. size_t len, int cpu, int full);
  163. struct trace_seq;
  164. int ring_buffer_print_entry_header(struct trace_seq *s);
  165. int ring_buffer_print_page_header(struct trace_seq *s);
  166. enum ring_buffer_flags {
  167. RB_FL_OVERWRITE = 1 << 0,
  168. };
  169. #endif /* _LINUX_RING_BUFFER_H */