ring_buffer.h 5.8 KB

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