pmu.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. Supporting PMUs on RISC-V platforms
  2. ==========================================
  3. Alan Kao <alankao@andestech.com>, Mar 2018
  4. Introduction
  5. ------------
  6. As of this writing, perf_event-related features mentioned in The RISC-V ISA
  7. Privileged Version 1.10 are as follows:
  8. (please check the manual for more details)
  9. * [m|s]counteren
  10. * mcycle[h], cycle[h]
  11. * minstret[h], instret[h]
  12. * mhpeventx, mhpcounterx[h]
  13. With such function set only, porting perf would require a lot of work, due to
  14. the lack of the following general architectural performance monitoring features:
  15. * Enabling/Disabling counters
  16. Counters are just free-running all the time in our case.
  17. * Interrupt caused by counter overflow
  18. No such feature in the spec.
  19. * Interrupt indicator
  20. It is not possible to have many interrupt ports for all counters, so an
  21. interrupt indicator is required for software to tell which counter has
  22. just overflowed.
  23. * Writing to counters
  24. There will be an SBI to support this since the kernel cannot modify the
  25. counters [1]. Alternatively, some vendor considers to implement
  26. hardware-extension for M-S-U model machines to write counters directly.
  27. This document aims to provide developers a quick guide on supporting their
  28. PMUs in the kernel. The following sections briefly explain perf' mechanism
  29. and todos.
  30. You may check previous discussions here [1][2]. Also, it might be helpful
  31. to check the appendix for related kernel structures.
  32. 1. Initialization
  33. -----------------
  34. *riscv_pmu* is a global pointer of type *struct riscv_pmu*, which contains
  35. various methods according to perf's internal convention and PMU-specific
  36. parameters. One should declare such instance to represent the PMU. By default,
  37. *riscv_pmu* points to a constant structure *riscv_base_pmu*, which has very
  38. basic support to a baseline QEMU model.
  39. Then he/she can either assign the instance's pointer to *riscv_pmu* so that
  40. the minimal and already-implemented logic can be leveraged, or invent his/her
  41. own *riscv_init_platform_pmu* implementation.
  42. In other words, existing sources of *riscv_base_pmu* merely provide a
  43. reference implementation. Developers can flexibly decide how many parts they
  44. can leverage, and in the most extreme case, they can customize every function
  45. according to their needs.
  46. 2. Event Initialization
  47. -----------------------
  48. When a user launches a perf command to monitor some events, it is first
  49. interpreted by the userspace perf tool into multiple *perf_event_open*
  50. system calls, and then each of them calls to the body of *event_init*
  51. member function that was assigned in the previous step. In *riscv_base_pmu*'s
  52. case, it is *riscv_event_init*.
  53. The main purpose of this function is to translate the event provided by user
  54. into bitmap, so that HW-related control registers or counters can directly be
  55. manipulated. The translation is based on the mappings and methods provided in
  56. *riscv_pmu*.
  57. Note that some features can be done in this stage as well:
  58. (1) interrupt setting, which is stated in the next section;
  59. (2) privilege level setting (user space only, kernel space only, both);
  60. (3) destructor setting. Normally it is sufficient to apply *riscv_destroy_event*;
  61. (4) tweaks for non-sampling events, which will be utilized by functions such as
  62. *perf_adjust_period*, usually something like the follows:
  63. if (!is_sampling_event(event)) {
  64. hwc->sample_period = x86_pmu.max_period;
  65. hwc->last_period = hwc->sample_period;
  66. local64_set(&hwc->period_left, hwc->sample_period);
  67. }
  68. In the case of *riscv_base_pmu*, only (3) is provided for now.
  69. 3. Interrupt
  70. ------------
  71. 3.1. Interrupt Initialization
  72. This often occurs at the beginning of the *event_init* method. In common
  73. practice, this should be a code segment like
  74. int x86_reserve_hardware(void)
  75. {
  76. int err = 0;
  77. if (!atomic_inc_not_zero(&pmc_refcount)) {
  78. mutex_lock(&pmc_reserve_mutex);
  79. if (atomic_read(&pmc_refcount) == 0) {
  80. if (!reserve_pmc_hardware())
  81. err = -EBUSY;
  82. else
  83. reserve_ds_buffers();
  84. }
  85. if (!err)
  86. atomic_inc(&pmc_refcount);
  87. mutex_unlock(&pmc_reserve_mutex);
  88. }
  89. return err;
  90. }
  91. And the magic is in *reserve_pmc_hardware*, which usually does atomic
  92. operations to make implemented IRQ accessible from some global function pointer.
  93. *release_pmc_hardware* serves the opposite purpose, and it is used in event
  94. destructors mentioned in previous section.
  95. (Note: From the implementations in all the architectures, the *reserve/release*
  96. pair are always IRQ settings, so the *pmc_hardware* seems somehow misleading.
  97. It does NOT deal with the binding between an event and a physical counter,
  98. which will be introduced in the next section.)
  99. 3.2. IRQ Structure
  100. Basically, a IRQ runs the following pseudo code:
  101. for each hardware counter that triggered this overflow
  102. get the event of this counter
  103. // following two steps are defined as *read()*,
  104. // check the section Reading/Writing Counters for details.
  105. count the delta value since previous interrupt
  106. update the event->count (# event occurs) by adding delta, and
  107. event->hw.period_left by subtracting delta
  108. if the event overflows
  109. sample data
  110. set the counter appropriately for the next overflow
  111. if the event overflows again
  112. too frequently, throttle this event
  113. fi
  114. fi
  115. end for
  116. However as of this writing, none of the RISC-V implementations have designed an
  117. interrupt for perf, so the details are to be completed in the future.
  118. 4. Reading/Writing Counters
  119. ---------------------------
  120. They seem symmetric but perf treats them quite differently. For reading, there
  121. is a *read* interface in *struct pmu*, but it serves more than just reading.
  122. According to the context, the *read* function not only reads the content of the
  123. counter (event->count), but also updates the left period to the next interrupt
  124. (event->hw.period_left).
  125. But the core of perf does not need direct write to counters. Writing counters
  126. is hidden behind the abstraction of 1) *pmu->start*, literally start counting so one
  127. has to set the counter to a good value for the next interrupt; 2) inside the IRQ
  128. it should set the counter to the same resonable value.
  129. Reading is not a problem in RISC-V but writing would need some effort, since
  130. counters are not allowed to be written by S-mode.
  131. 5. add()/del()/start()/stop()
  132. -----------------------------
  133. Basic idea: add()/del() adds/deletes events to/from a PMU, and start()/stop()
  134. starts/stop the counter of some event in the PMU. All of them take the same
  135. arguments: *struct perf_event *event* and *int flag*.
  136. Consider perf as a state machine, then you will find that these functions serve
  137. as the state transition process between those states.
  138. Three states (event->hw.state) are defined:
  139. * PERF_HES_STOPPED: the counter is stopped
  140. * PERF_HES_UPTODATE: the event->count is up-to-date
  141. * PERF_HES_ARCH: arch-dependent usage ... we don't need this for now
  142. A normal flow of these state transitions are as follows:
  143. * A user launches a perf event, resulting in calling to *event_init*.
  144. * When being context-switched in, *add* is called by the perf core, with a flag
  145. PERF_EF_START, which means that the event should be started after it is added.
  146. At this stage, a general event is bound to a physical counter, if any.
  147. The state changes to PERF_HES_STOPPED and PERF_HES_UPTODATE, because it is now
  148. stopped, and the (software) event count does not need updating.
  149. ** *start* is then called, and the counter is enabled.
  150. With flag PERF_EF_RELOAD, it writes an appropriate value to the counter (check
  151. previous section for detail).
  152. Nothing is written if the flag does not contain PERF_EF_RELOAD.
  153. The state now is reset to none, because it is neither stopped nor updated
  154. (the counting already started)
  155. * When being context-switched out, *del* is called. It then checks out all the
  156. events in the PMU and calls *stop* to update their counts.
  157. ** *stop* is called by *del*
  158. and the perf core with flag PERF_EF_UPDATE, and it often shares the same
  159. subroutine as *read* with the same logic.
  160. The state changes to PERF_HES_STOPPED and PERF_HES_UPTODATE, again.
  161. ** Life cycle of these two pairs: *add* and *del* are called repeatedly as
  162. tasks switch in-and-out; *start* and *stop* is also called when the perf core
  163. needs a quick stop-and-start, for instance, when the interrupt period is being
  164. adjusted.
  165. Current implementation is sufficient for now and can be easily extended to
  166. features in the future.
  167. A. Related Structures
  168. ---------------------
  169. * struct pmu: include/linux/perf_event.h
  170. * struct riscv_pmu: arch/riscv/include/asm/perf_event.h
  171. Both structures are designed to be read-only.
  172. *struct pmu* defines some function pointer interfaces, and most of them take
  173. *struct perf_event* as a main argument, dealing with perf events according to
  174. perf's internal state machine (check kernel/events/core.c for details).
  175. *struct riscv_pmu* defines PMU-specific parameters. The naming follows the
  176. convention of all other architectures.
  177. * struct perf_event: include/linux/perf_event.h
  178. * struct hw_perf_event
  179. The generic structure that represents perf events, and the hardware-related
  180. details.
  181. * struct riscv_hw_events: arch/riscv/include/asm/perf_event.h
  182. The structure that holds the status of events, has two fixed members:
  183. the number of events and the array of the events.
  184. References
  185. ----------
  186. [1] https://github.com/riscv/riscv-linux/pull/124
  187. [2] https://groups.google.com/a/groups.riscv.org/forum/#!topic/sw-dev/f19TmCNP6yA