da_monitor_instrumentation.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. Deterministic Automata Instrumentation
  2. ======================================
  3. The RV monitor file created by dot2k, with the name "$MODEL_NAME.c"
  4. includes a section dedicated to instrumentation.
  5. In the example of the wip.dot monitor created on [1], it will look like::
  6. /*
  7. * This is the instrumentation part of the monitor.
  8. *
  9. * This is the section where manual work is required. Here the kernel events
  10. * are translated into model's event.
  11. *
  12. */
  13. static void handle_preempt_disable(void *data, /* XXX: fill header */)
  14. {
  15. da_handle_event_wip(preempt_disable_wip);
  16. }
  17. static void handle_preempt_enable(void *data, /* XXX: fill header */)
  18. {
  19. da_handle_event_wip(preempt_enable_wip);
  20. }
  21. static void handle_sched_waking(void *data, /* XXX: fill header */)
  22. {
  23. da_handle_event_wip(sched_waking_wip);
  24. }
  25. static int enable_wip(void)
  26. {
  27. int retval;
  28. retval = da_monitor_init_wip();
  29. if (retval)
  30. return retval;
  31. rv_attach_trace_probe("wip", /* XXX: tracepoint */, handle_preempt_disable);
  32. rv_attach_trace_probe("wip", /* XXX: tracepoint */, handle_preempt_enable);
  33. rv_attach_trace_probe("wip", /* XXX: tracepoint */, handle_sched_waking);
  34. return 0;
  35. }
  36. The comment at the top of the section explains the general idea: the
  37. instrumentation section translates *kernel events* into the *model's
  38. event*.
  39. Tracing callback functions
  40. --------------------------
  41. The first three functions are the starting point of the callback *handler
  42. functions* for each of the three events from the wip model. The developer
  43. does not necessarily need to use them: they are just starting points.
  44. Using the example of::
  45. void handle_preempt_disable(void *data, /* XXX: fill header */)
  46. {
  47. da_handle_event_wip(preempt_disable_wip);
  48. }
  49. The preempt_disable event from the model connects directly to the
  50. preemptirq:preempt_disable. The preemptirq:preempt_disable event
  51. has the following signature, from include/trace/events/preemptirq.h::
  52. TP_PROTO(unsigned long ip, unsigned long parent_ip)
  53. Hence, the handle_preempt_disable() function will look like::
  54. void handle_preempt_disable(void *data, unsigned long ip, unsigned long parent_ip)
  55. In this case, the kernel event translates one to one with the automata
  56. event, and indeed, no other change is required for this function.
  57. The next handler function, handle_preempt_enable() has the same argument
  58. list from the handle_preempt_disable(). The difference is that the
  59. preempt_enable event will be used to synchronize the system to the model.
  60. Initially, the *model* is placed in the initial state. However, the *system*
  61. might or might not be in the initial state. The monitor cannot start
  62. processing events until it knows that the system has reached the initial state.
  63. Otherwise, the monitor and the system could be out-of-sync.
  64. Looking at the automata definition, it is possible to see that the system
  65. and the model are expected to return to the initial state after the
  66. preempt_enable execution. Hence, it can be used to synchronize the
  67. system and the model at the initialization of the monitoring section.
  68. The start is informed via a special handle function, the
  69. "da_handle_start_event_$(MONITOR_NAME)(event)", in this case::
  70. da_handle_start_event_wip(preempt_enable_wip);
  71. So, the callback function will look like::
  72. void handle_preempt_enable(void *data, unsigned long ip, unsigned long parent_ip)
  73. {
  74. da_handle_start_event_wip(preempt_enable_wip);
  75. }
  76. Finally, the "handle_sched_waking()" will look like::
  77. void handle_sched_waking(void *data, struct task_struct *task)
  78. {
  79. da_handle_event_wip(sched_waking_wip);
  80. }
  81. And the explanation is left for the reader as an exercise.
  82. enable and disable functions
  83. ----------------------------
  84. dot2k automatically creates two special functions::
  85. enable_$(MONITOR_NAME)()
  86. disable_$(MONITOR_NAME)()
  87. These functions are called when the monitor is enabled and disabled,
  88. respectively.
  89. They should be used to *attach* and *detach* the instrumentation to the running
  90. system. The developer must add to the relative function all that is needed to
  91. *attach* and *detach* its monitor to the system.
  92. For the wip case, these functions were named::
  93. enable_wip()
  94. disable_wip()
  95. But no change was required because: by default, these functions *attach* and
  96. *detach* the tracepoints_to_attach, which was enough for this case.
  97. Instrumentation helpers
  98. -----------------------
  99. To complete the instrumentation, the *handler functions* need to be attached to a
  100. kernel event, at the monitoring enable phase.
  101. The RV interface also facilitates this step. For example, the macro "rv_attach_trace_probe()"
  102. is used to connect the wip model events to the relative kernel event. dot2k automatically
  103. adds "rv_attach_trace_probe()" function call for each model event in the enable phase, as
  104. a suggestion.
  105. For example, from the wip sample model::
  106. static int enable_wip(void)
  107. {
  108. int retval;
  109. retval = da_monitor_init_wip();
  110. if (retval)
  111. return retval;
  112. rv_attach_trace_probe("wip", /* XXX: tracepoint */, handle_preempt_enable);
  113. rv_attach_trace_probe("wip", /* XXX: tracepoint */, handle_sched_waking);
  114. rv_attach_trace_probe("wip", /* XXX: tracepoint */, handle_preempt_disable);
  115. return 0;
  116. }
  117. The probes then need to be detached at the disable phase.
  118. [1] The wip model is presented in::
  119. Documentation/trace/rv/deterministic_automata.rst
  120. The wip monitor is presented in::
  121. Documentation/trace/rv/da_monitor_synthesis.rst