events.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. Event Tracing
  2. Documentation written by Theodore Ts'o
  3. Introduction
  4. ============
  5. Tracepoints (see Documentation/trace/tracepoints.txt) can be used
  6. without creating custom kernel modules to register probe functions
  7. using the event tracing infrastructure.
  8. Not all tracepoints can be traced using the event tracing system;
  9. the kernel developer must provide code snippets which define how the
  10. tracing information is saved into the tracing buffer, and how the
  11. the tracing information should be printed.
  12. Using Event Tracing
  13. ===================
  14. The events which are available for tracing can be found in the file
  15. /sys/kernel/debug/tracing/available_events.
  16. To enable a particular event, such as 'sched_wakeup', simply echo it
  17. to /sys/debug/tracing/set_event. For example:
  18. # echo sched_wakeup > /sys/kernel/debug/tracing/set_event
  19. [ Note: events can also be enabled/disabled via the 'enabled' toggle
  20. found in the /sys/kernel/tracing/events/ hierarchy of directories. ]
  21. To disable an event, echo the event name to the set_event file prefixed
  22. with an exclamation point:
  23. # echo '!sched_wakeup' >> /sys/kernel/debug/tracing/set_event
  24. To disable events, echo an empty line to the set_event file:
  25. # echo > /sys/kernel/debug/tracing/set_event
  26. The events are organized into subsystems, such as ext4, irq, sched,
  27. etc., and a full event name looks like this: <subsystem>:<event>. The
  28. subsystem name is optional, but it is displayed in the available_events
  29. file. All of the events in a subsystem can be specified via the syntax
  30. "<subsystem>:*"; for example, to enable all irq events, you can use the
  31. command:
  32. # echo 'irq:*' > /sys/kernel/debug/tracing/set_event
  33. Defining an event-enabled tracepoint
  34. ------------------------------------
  35. A kernel developer which wishes to define an event-enabled tracepoint
  36. must declare the tracepoint using TRACE_EVENT instead of DECLARE_TRACE.
  37. This is done via two header files in include/trace. For example, to
  38. event-enable the jbd2 subsystem, we must create two files,
  39. include/trace/jbd2.h and include/trace/jbd2_event_types.h. The
  40. include/trace/jbd2.h file should be included by kernel source files that
  41. will have a tracepoint inserted, and might look like this:
  42. #ifndef _TRACE_JBD2_H
  43. #define _TRACE_JBD2_H
  44. #include <linux/jbd2.h>
  45. #include <linux/tracepoint.h>
  46. #include <trace/jbd2_event_types.h>
  47. #endif
  48. In a file that utilizes a jbd2 tracepoint, this header file would be
  49. included. Note that you still have to use DEFINE_TRACE(). So for
  50. example, if fs/jbd2/commit.c planned to use the jbd2_start_commit
  51. tracepoint, it would have the following near the beginning of the file:
  52. #include <trace/jbd2.h>
  53. DEFINE_TRACE(jbd2_start_commit);
  54. Then in the function that would call the tracepoint, it would call the
  55. tracepoint function. (For more information, please see the tracepoint
  56. documentation in Documentation/trace/tracepoints.txt):
  57. trace_jbd2_start_commit(journal, commit_transaction);
  58. The code snippets which allow jbd2_start_commit to be an event-enabled
  59. tracepoint are placed in the file include/trace/jbd2_event_types.h:
  60. /* use <trace/jbd2.h> instead */
  61. #ifndef TRACE_EVENT
  62. # error Do not include this file directly.
  63. # error Unless you know what you are doing.
  64. #endif
  65. #undef TRACE_SYSTEM
  66. #define TRACE_SYSTEM jbd2
  67. #include <linux/jbd2.h>
  68. TRACE_EVENT(jbd2_start_commit,
  69. TP_PROTO(journal_t *journal, transaction_t *commit_transaction),
  70. TP_ARGS(journal, commit_transaction),
  71. TP_STRUCT__entry(
  72. __array( char, devname, BDEVNAME_SIZE+24 )
  73. __field( int, transaction )
  74. ),
  75. TP_fast_assign(
  76. memcpy(__entry->devname, journal->j_devname, BDEVNAME_SIZE+24);
  77. __entry->transaction = commit_transaction->t_tid;
  78. ),
  79. TP_printk("dev %s transaction %d",
  80. __entry->devname, __entry->transaction)
  81. );
  82. The TP_PROTO and TP_ARGS are unchanged from DECLARE_TRACE. The new
  83. arguments to TRACE_EVENT are TP_STRUCT__entry, TP_fast_assign, and
  84. TP_printk.
  85. TP_STRUCT__entry defines the data structure which will be stored in the
  86. trace buffer. Normally, fields in __entry will be arrays or simple
  87. types. It is possible to place data structures in __entry --- however,
  88. pointers in the data structure can not be trusted, since they will be
  89. accessed sometime later by TP_printk, and if the data structure contains
  90. fields that will not or cannot be used by TP_printk, this will waste
  91. space in the trace buffer. In general, data structures should be
  92. avoided, unless they do only contain non-pointer types and all of the
  93. fields will be used by TP_printk.
  94. TP_fast_assign defines the code snippet which saves information into the
  95. __entry data structure, using the passed-in arguments defined in
  96. TP_PROTO and TP_ARGS.
  97. Finally, TP_printk will print the __entry data structure. At the time
  98. when the code snippet defined by TP_printk is executed, it will not have
  99. access to the TP_ARGS arguments; it can only use the information saved
  100. in the __entry data structure.