markers.txt 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 and make
  39. sure there is no caller left using the probe when it returns. Probe removal is
  40. preempt-safe because preemption is disabled around the probe call. See the
  41. "Probe example" section below for a sample probe module.
  42. The marker mechanism supports inserting multiple instances of the same marker.
  43. Markers can be put in inline functions, inlined static functions, and
  44. unrolled loops as well as regular functions.
  45. The naming scheme "subsystem_event" is suggested here as a convention intended
  46. to limit collisions. Marker names are global to the kernel: they are considered
  47. as being the same whether they are in the core kernel image or in modules.
  48. Conflicting format strings for markers with the same name will cause the markers
  49. to be detected to have a different format string not to be armed and will output
  50. a printk warning which identifies the inconsistency:
  51. "Format mismatch for probe probe_name (format), marker (format)"
  52. * Probe / marker example
  53. See the example provided in samples/markers/src
  54. Compile them with your kernel.
  55. Run, as root :
  56. modprobe marker-example (insmod order is not important)
  57. modprobe probe-example
  58. cat /proc/marker-example (returns an expected error)
  59. rmmod marker-example probe-example
  60. dmesg