ring_buffer.h 5.6 KB

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