markers.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Using the Linux Kernel Markers
  2. Mathieu Desnoyers
  3. This document introduces Linux Kernel Markers and their use. It provides
  4. examples of how to insert markers in the kernel and connect probe functions to
  5. them and provides some examples of probe functions.
  6. * Purpose of markers
  7. A marker placed in code provides a hook to call a function (probe) that you can
  8. provide at runtime. A marker can be "on" (a probe is connected to it) or "off"
  9. (no probe is attached). When a marker is "off" it has no effect, except for
  10. adding a tiny time penalty (checking a condition for a branch) and space
  11. penalty (adding a few bytes for the function call at the end of the
  12. instrumented function and adds a data structure in a separate section). When a
  13. marker is "on", the function you provide is called each time the marker is
  14. executed, in the execution context of the caller. When the function provided
  15. ends its execution, it returns to the caller (continuing from the marker site).
  16. You can put markers at important locations in the code. Markers are
  17. lightweight hooks that can pass an arbitrary number of parameters,
  18. described in a printk-like format string, to the attached probe function.
  19. They can be used for tracing and performance accounting.
  20. * Usage
  21. In order to use the macro trace_mark, you should include linux/marker.h.
  22. #include <linux/marker.h>
  23. And,
  24. trace_mark(subsystem_event, "myint %d mystring %s", someint, somestring);
  25. Where :
  26. - subsystem_event is an identifier unique to your event
  27. - subsystem is the name of your subsystem.
  28. - event is the name of the event to mark.
  29. - "myint %d mystring %s" is the formatted string for the serializer. "myint" and
  30. "mystring" are repectively the field names associated with the first and
  31. second parameter.
  32. - someint is an integer.
  33. - somestring is a char pointer.
  34. Connecting a function (probe) to a marker is done by providing a probe (function
  35. to call) for the specific marker through marker_probe_register() and can be
  36. activated by calling marker_arm(). Marker deactivation can be done by calling
  37. marker_disarm() as many times as marker_arm() has been called. Removing a probe
  38. is done through marker_probe_unregister(); it will disarm the probe.
  39. marker_synchronize_unregister() must be called between probe unregistration and
  40. the first occurrence of
  41. - the end of module exit function,
  42. to make sure there is no caller left using the probe;
  43. - the free of any resource used by the probes,
  44. to make sure the probes wont be accessing invalid data.
  45. This, and the fact that preemption is disabled around the probe call, make sure
  46. that probe removal and module unload are safe. See the "Probe example" section
  47. below for a sample probe module.
  48. The marker mechanism supports inserting multiple instances of the same marker.
  49. Markers can be put in inline functions, inlined static functions, and
  50. unrolled loops as well as regular functions.
  51. The naming scheme "subsystem_event" is suggested here as a convention intended
  52. to limit collisions. Marker names are global to the kernel: they are considered
  53. as being the same whether they are in the core kernel image or in modules.
  54. Conflicting format strings for markers with the same name will cause the markers
  55. to be detected to have a different format string not to be armed and will output
  56. a printk warning which identifies the inconsistency:
  57. "Format mismatch for probe probe_name (format), marker (format)"
  58. Another way to use markers is to simply define the marker without generating any
  59. function call to actually call into the marker. This is useful in combination
  60. with tracepoint probes in a scheme like this :
  61. void probe_tracepoint_name(unsigned int arg1, struct task_struct *tsk);
  62. DEFINE_MARKER_TP(marker_eventname, tracepoint_name, probe_tracepoint_name,
  63. "arg1 %u pid %d");
  64. notrace void probe_tracepoint_name(unsigned int arg1, struct task_struct *tsk)
  65. {
  66. struct marker *marker = &GET_MARKER(kernel_irq_entry);
  67. /* write data to trace buffers ... */
  68. }
  69. * Probe / marker example
  70. See the example provided in samples/markers/src
  71. Compile them with your kernel.
  72. Run, as root :
  73. modprobe marker-example (insmod order is not important)
  74. modprobe probe-example
  75. cat /proc/marker-example (returns an expected error)
  76. rmmod marker-example probe-example
  77. dmesg