ring-buffer-map.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==================================
  3. Tracefs ring-buffer memory mapping
  4. ==================================
  5. :Author: Vincent Donnefort <vdonnefort@google.com>
  6. Overview
  7. ========
  8. Tracefs ring-buffer memory map provides an efficient method to stream data
  9. as no memory copy is necessary. The application mapping the ring-buffer becomes
  10. then a consumer for that ring-buffer, in a similar fashion to trace_pipe.
  11. Memory mapping setup
  12. ====================
  13. The mapping works with a mmap() of the trace_pipe_raw interface.
  14. The first system page of the mapping contains ring-buffer statistics and
  15. description. It is referred to as the meta-page. One of the most important
  16. fields of the meta-page is the reader. It contains the sub-buffer ID which can
  17. be safely read by the mapper (see ring-buffer-design.rst).
  18. The meta-page is followed by all the sub-buffers, ordered by ascending ID. It is
  19. therefore effortless to know where the reader starts in the mapping:
  20. .. code-block:: c
  21. reader_id = meta->reader->id;
  22. reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size;
  23. When the application is done with the current reader, it can get a new one using
  24. the trace_pipe_raw ioctl() TRACE_MMAP_IOCTL_GET_READER. This ioctl also updates
  25. the meta-page fields.
  26. Limitations
  27. ===========
  28. When a mapping is in place on a Tracefs ring-buffer, it is not possible to
  29. either resize it (either by increasing the entire size of the ring-buffer or
  30. each subbuf). It is also not possible to use snapshot and causes splice to copy
  31. the ring buffer data instead of using the copyless swap from the ring buffer.
  32. Concurrent readers (either another application mapping that ring-buffer or the
  33. kernel with trace_pipe) are allowed but not recommended. They will compete for
  34. the ring-buffer and the output is unpredictable, just like concurrent readers on
  35. trace_pipe would be.
  36. Example
  37. =======
  38. .. code-block:: c
  39. #include <fcntl.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <linux/trace_mmap.h>
  44. #include <sys/mman.h>
  45. #include <sys/ioctl.h>
  46. #define TRACE_PIPE_RAW "/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw"
  47. int main(void)
  48. {
  49. int page_size = getpagesize(), fd, reader_id;
  50. unsigned long meta_len, data_len;
  51. struct trace_buffer_meta *meta;
  52. void *map, *reader, *data;
  53. fd = open(TRACE_PIPE_RAW, O_RDONLY | O_NONBLOCK);
  54. if (fd < 0)
  55. exit(EXIT_FAILURE);
  56. map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  57. if (map == MAP_FAILED)
  58. exit(EXIT_FAILURE);
  59. meta = (struct trace_buffer_meta *)map;
  60. meta_len = meta->meta_page_size;
  61. printf("entries: %llu\n", meta->entries);
  62. printf("overrun: %llu\n", meta->overrun);
  63. printf("read: %llu\n", meta->read);
  64. printf("nr_subbufs: %u\n", meta->nr_subbufs);
  65. data_len = meta->subbuf_size * meta->nr_subbufs;
  66. data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, meta_len);
  67. if (data == MAP_FAILED)
  68. exit(EXIT_FAILURE);
  69. if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER) < 0)
  70. exit(EXIT_FAILURE);
  71. reader_id = meta->reader.id;
  72. reader = data + meta->subbuf_size * reader_id;
  73. printf("Current reader address: %p\n", reader);
  74. munmap(data, data_len);
  75. munmap(meta, meta_len);
  76. close (fd);
  77. return 0;
  78. }