tracepoints.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Using the Linux Kernel Tracepoints
  2. Mathieu Desnoyers
  3. This document introduces Linux Kernel Tracepoints and their use. It
  4. provides examples of how to insert tracepoints in the kernel and
  5. connect probe functions to them and provides some examples of probe
  6. functions.
  7. * Purpose of tracepoints
  8. A tracepoint placed in code provides a hook to call a function (probe)
  9. that you can provide at runtime. A tracepoint can be "on" (a probe is
  10. connected to it) or "off" (no probe is attached). When a tracepoint is
  11. "off" it has no effect, except for adding a tiny time penalty
  12. (checking a condition for a branch) and space penalty (adding a few
  13. bytes for the function call at the end of the instrumented function
  14. and adds a data structure in a separate section). When a tracepoint
  15. is "on", the function you provide is called each time the tracepoint
  16. is executed, in the execution context of the caller. When the function
  17. provided ends its execution, it returns to the caller (continuing from
  18. the tracepoint site).
  19. You can put tracepoints at important locations in the code. They are
  20. lightweight hooks that can pass an arbitrary number of parameters,
  21. which prototypes are described in a tracepoint declaration placed in a
  22. header file.
  23. They can be used for tracing and performance accounting.
  24. * Usage
  25. Two elements are required for tracepoints :
  26. - A tracepoint definition, placed in a header file.
  27. - The tracepoint statement, in C code.
  28. In order to use tracepoints, you should include linux/tracepoint.h.
  29. In include/trace/events/subsys.h :
  30. #undef TRACE_SYSTEM
  31. #define TRACE_SYSTEM subsys
  32. #if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
  33. #define _TRACE_SUBSYS_H
  34. #include <linux/tracepoint.h>
  35. DECLARE_TRACE(subsys_eventname,
  36. TP_PROTO(int firstarg, struct task_struct *p),
  37. TP_ARGS(firstarg, p));
  38. #endif /* _TRACE_SUBSYS_H */
  39. /* This part must be outside protection */
  40. #include <trace/define_trace.h>
  41. In subsys/file.c (where the tracing statement must be added) :
  42. #include <trace/events/subsys.h>
  43. #define CREATE_TRACE_POINTS
  44. DEFINE_TRACE(subsys_eventname);
  45. void somefct(void)
  46. {
  47. ...
  48. trace_subsys_eventname(arg, task);
  49. ...
  50. }
  51. Where :
  52. - subsys_eventname is an identifier unique to your event
  53. - subsys is the name of your subsystem.
  54. - eventname is the name of the event to trace.
  55. - TP_PROTO(int firstarg, struct task_struct *p) is the prototype of the
  56. function called by this tracepoint.
  57. - TP_ARGS(firstarg, p) are the parameters names, same as found in the
  58. prototype.
  59. - if you use the header in multiple source files, #define CREATE_TRACE_POINTS
  60. should appear only in one source file.
  61. Connecting a function (probe) to a tracepoint is done by providing a
  62. probe (function to call) for the specific tracepoint through
  63. register_trace_subsys_eventname(). Removing a probe is done through
  64. unregister_trace_subsys_eventname(); it will remove the probe.
  65. tracepoint_synchronize_unregister() must be called before the end of
  66. the module exit function to make sure there is no caller left using
  67. the probe. This, and the fact that preemption is disabled around the
  68. probe call, make sure that probe removal and module unload are safe.
  69. The tracepoint mechanism supports inserting multiple instances of the
  70. same tracepoint, but a single definition must be made of a given
  71. tracepoint name over all the kernel to make sure no type conflict will
  72. occur. Name mangling of the tracepoints is done using the prototypes
  73. to make sure typing is correct. Verification of probe type correctness
  74. is done at the registration site by the compiler. Tracepoints can be
  75. put in inline functions, inlined static functions, and unrolled loops
  76. as well as regular functions.
  77. The naming scheme "subsys_event" is suggested here as a convention
  78. intended to limit collisions. Tracepoint names are global to the
  79. kernel: they are considered as being the same whether they are in the
  80. core kernel image or in modules.
  81. If the tracepoint has to be used in kernel modules, an
  82. EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
  83. used to export the defined tracepoints.
  84. If you need to do a bit of work for a tracepoint parameter, and
  85. that work is only used for the tracepoint, that work can be encapsulated
  86. within an if statement with the following:
  87. if (trace_foo_bar_enabled()) {
  88. int i;
  89. int tot = 0;
  90. for (i = 0; i < count; i++)
  91. tot += calculate_nuggets();
  92. trace_foo_bar(tot);
  93. }
  94. All trace_<tracepoint>() calls have a matching trace_<tracepoint>_enabled()
  95. function defined that returns true if the tracepoint is enabled and
  96. false otherwise. The trace_<tracepoint>() should always be within the
  97. block of the if (trace_<tracepoint>_enabled()) to prevent races between
  98. the tracepoint being enabled and the check being seen.
  99. The advantage of using the trace_<tracepoint>_enabled() is that it uses
  100. the static_key of the tracepoint to allow the if statement to be implemented
  101. with jump labels and avoid conditional branches.
  102. Note: The convenience macro TRACE_EVENT provides an alternative way to
  103. define tracepoints. Check http://lwn.net/Articles/379903,
  104. http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362
  105. for a series of articles with more details.