user_events.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. =========================================
  2. user_events: User-based Event Tracing
  3. =========================================
  4. :Author: Beau Belgrave
  5. Overview
  6. --------
  7. User based trace events allow user processes to create events and trace data
  8. that can be viewed via existing tools, such as ftrace and perf.
  9. To enable this feature, build your kernel with CONFIG_USER_EVENTS=y.
  10. Programs can view status of the events via
  11. /sys/kernel/debug/tracing/user_events_status and can both register and write
  12. data out via /sys/kernel/debug/tracing/user_events_data.
  13. Programs can also use /sys/kernel/debug/tracing/dynamic_events to register and
  14. delete user based events via the u: prefix. The format of the command to
  15. dynamic_events is the same as the ioctl with the u: prefix applied.
  16. Typically programs will register a set of events that they wish to expose to
  17. tools that can read trace_events (such as ftrace and perf). The registration
  18. process gives back two ints to the program for each event. The first int is
  19. the status bit. This describes which bit in little-endian format in the
  20. /sys/kernel/debug/tracing/user_events_status file represents this event. The
  21. second int is the write index which describes the data when a write() or
  22. writev() is called on the /sys/kernel/debug/tracing/user_events_data file.
  23. The structures referenced in this document are contained within the
  24. /include/uapi/linux/user_events.h file in the source tree.
  25. **NOTE:** *Both user_events_status and user_events_data are under the tracefs
  26. filesystem and may be mounted at different paths than above.*
  27. Registering
  28. -----------
  29. Registering within a user process is done via ioctl() out to the
  30. /sys/kernel/debug/tracing/user_events_data file. The command to issue is
  31. DIAG_IOCSREG.
  32. This command takes a packed struct user_reg as an argument::
  33. struct user_reg {
  34. u32 size;
  35. u64 name_args;
  36. u32 status_bit;
  37. u32 write_index;
  38. };
  39. The struct user_reg requires two inputs, the first is the size of the structure
  40. to ensure forward and backward compatibility. The second is the command string
  41. to issue for registering. Upon success two outputs are set, the status bit
  42. and the write index.
  43. User based events show up under tracefs like any other event under the
  44. subsystem named "user_events". This means tools that wish to attach to the
  45. events need to use /sys/kernel/debug/tracing/events/user_events/[name]/enable
  46. or perf record -e user_events:[name] when attaching/recording.
  47. **NOTE:** *The write_index returned is only valid for the FD that was used*
  48. Command Format
  49. ^^^^^^^^^^^^^^
  50. The command string format is as follows::
  51. name[:FLAG1[,FLAG2...]] [Field1[;Field2...]]
  52. Supported Flags
  53. ^^^^^^^^^^^^^^^
  54. None yet
  55. Field Format
  56. ^^^^^^^^^^^^
  57. ::
  58. type name [size]
  59. Basic types are supported (__data_loc, u32, u64, int, char, char[20], etc).
  60. User programs are encouraged to use clearly sized types like u32.
  61. **NOTE:** *Long is not supported since size can vary between user and kernel.*
  62. The size is only valid for types that start with a struct prefix.
  63. This allows user programs to describe custom structs out to tools, if required.
  64. For example, a struct in C that looks like this::
  65. struct mytype {
  66. char data[20];
  67. };
  68. Would be represented by the following field::
  69. struct mytype myname 20
  70. Deleting
  71. -----------
  72. Deleting an event from within a user process is done via ioctl() out to the
  73. /sys/kernel/debug/tracing/user_events_data file. The command to issue is
  74. DIAG_IOCSDEL.
  75. This command only requires a single string specifying the event to delete by
  76. its name. Delete will only succeed if there are no references left to the
  77. event (in both user and kernel space). User programs should use a separate file
  78. to request deletes than the one used for registration due to this.
  79. Status
  80. ------
  81. When tools attach/record user based events the status of the event is updated
  82. in realtime. This allows user programs to only incur the cost of the write() or
  83. writev() calls when something is actively attached to the event.
  84. User programs call mmap() on /sys/kernel/debug/tracing/user_events_status to
  85. check the status for each event that is registered. The bit to check in the
  86. file is given back after the register ioctl() via user_reg.status_bit. The bit
  87. is always in little-endian format. Programs can check if the bit is set either
  88. using a byte-wise index with a mask or a long-wise index with a little-endian
  89. mask.
  90. Currently the size of user_events_status is a single page, however, custom
  91. kernel configurations can change this size to allow more user based events. In
  92. all cases the size of the file is a multiple of a page size.
  93. For example, if the register ioctl() gives back a status_bit of 3 you would
  94. check byte 0 (3 / 8) of the returned mmap data and then AND the result with 8
  95. (1 << (3 % 8)) to see if anything is attached to that event.
  96. A byte-wise index check is performed as follows::
  97. int index, mask;
  98. char *status_page;
  99. index = status_bit / 8;
  100. mask = 1 << (status_bit % 8);
  101. ...
  102. if (status_page[index] & mask) {
  103. /* Enabled */
  104. }
  105. A long-wise index check is performed as follows::
  106. #include <asm/bitsperlong.h>
  107. #include <endian.h>
  108. #if __BITS_PER_LONG == 64
  109. #define endian_swap(x) htole64(x)
  110. #else
  111. #define endian_swap(x) htole32(x)
  112. #endif
  113. long index, mask, *status_page;
  114. index = status_bit / __BITS_PER_LONG;
  115. mask = 1L << (status_bit % __BITS_PER_LONG);
  116. mask = endian_swap(mask);
  117. ...
  118. if (status_page[index] & mask) {
  119. /* Enabled */
  120. }
  121. Administrators can easily check the status of all registered events by reading
  122. the user_events_status file directly via a terminal. The output is as follows::
  123. Byte:Name [# Comments]
  124. ...
  125. Active: ActiveCount
  126. Busy: BusyCount
  127. Max: MaxCount
  128. For example, on a system that has a single event the output looks like this::
  129. 1:test
  130. Active: 1
  131. Busy: 0
  132. Max: 32768
  133. If a user enables the user event via ftrace, the output would change to this::
  134. 1:test # Used by ftrace
  135. Active: 1
  136. Busy: 1
  137. Max: 32768
  138. **NOTE:** *A status bit of 0 will never be returned. This allows user programs
  139. to have a bit that can be used on error cases.*
  140. Writing Data
  141. ------------
  142. After registering an event the same fd that was used to register can be used
  143. to write an entry for that event. The write_index returned must be at the start
  144. of the data, then the remaining data is treated as the payload of the event.
  145. For example, if write_index returned was 1 and I wanted to write out an int
  146. payload of the event. Then the data would have to be 8 bytes (2 ints) in size,
  147. with the first 4 bytes being equal to 1 and the last 4 bytes being equal to the
  148. value I want as the payload.
  149. In memory this would look like this::
  150. int index;
  151. int payload;
  152. User programs might have well known structs that they wish to use to emit out
  153. as payloads. In those cases writev() can be used, with the first vector being
  154. the index and the following vector(s) being the actual event payload.
  155. For example, if I have a struct like this::
  156. struct payload {
  157. int src;
  158. int dst;
  159. int flags;
  160. };
  161. It's advised for user programs to do the following::
  162. struct iovec io[2];
  163. struct payload e;
  164. io[0].iov_base = &write_index;
  165. io[0].iov_len = sizeof(write_index);
  166. io[1].iov_base = &e;
  167. io[1].iov_len = sizeof(e);
  168. writev(fd, (const struct iovec*)io, 2);
  169. **NOTE:** *The write_index is not emitted out into the trace being recorded.*
  170. Example Code
  171. ------------
  172. See sample code in samples/user_events.