ring_buffer.h 6.4 KB

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