perfmon.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2014-2018 Remy Noel.
  3. * Copyright (c) 2014-2018 Richard Braun.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. *
  19. * Performance monitoring based on hardware performance counters.
  20. *
  21. * The hardware layer is represented by a performance monitoring unit (PMU),
  22. * which provides performance monitoring counters (PMCs).
  23. */
  24. #ifndef KERN_PERFMON_H
  25. #define KERN_PERFMON_H
  26. #include <stdint.h>
  27. #include <kern/init.h>
  28. #include <kern/perfmon_types.h>
  29. #include <kern/thread.h>
  30. // IDs of generic performance monitoring events.
  31. #define PERFMON_EV_CYCLE 0
  32. #define PERFMON_EV_REF_CYCLE 1
  33. #define PERFMON_EV_INSTRUCTION 2
  34. #define PERFMON_EV_CACHE_REF 3
  35. #define PERFMON_EV_CACHE_MISS 4
  36. #define PERFMON_EV_BRANCH 5
  37. #define PERFMON_EV_BRANCH_MISS 6
  38. #define PERFMON_NR_GENERIC_EVENTS 7
  39. // Event flags.
  40. #define PERFMON_EF_KERN 0x1 // Monitor events in kernel mode.
  41. #define PERFMON_EF_USER 0x2 // Monitor events in user mode.
  42. #define PERFMON_EF_RAW 0x4 // Raw event ID, generic if unset.
  43. /*
  44. * Performance monitoring operations.
  45. *
  46. * This is a public structure.
  47. *
  48. * All operations are either global but serialized by the caller, or
  49. * processor-local and called with interrupts and preemption disabled.
  50. *
  51. * If the hardware doesn't efficiently support overflow interrupts, the
  52. * handler must be set to NULL, making the perfmon module perdiocally
  53. * check the raw value of the hardware counters.
  54. */
  55. struct perfmon_dev_ops
  56. {
  57. /*
  58. * Convert a generic event ID into a raw event ID.
  59. *
  60. * Global operation.
  61. */
  62. int (*translate) (unsigned int *, unsigned int);
  63. /*
  64. * Allocate a performance monitoring counter globally for the given
  65. * raw event ID, and return the counter ID through the given pointer.
  66. * The driver may return any PMC ID, as long as it uniquely identifies
  67. * the underlying counter. The PMC index is passed when reporting
  68. * overflows, if using a custom overflow interrupt handler.
  69. *
  70. * Global operation.
  71. */
  72. int (*alloc) (unsigned int *, unsigned int, unsigned int);
  73. /*
  74. * Free an allocated performance monitoring counter.
  75. *
  76. * Global operation.
  77. */
  78. void (*free) (unsigned int);
  79. /*
  80. * Start a performance monitoring counter for the given raw event ID.
  81. *
  82. * Processor-local operation.
  83. */
  84. void (*start) (unsigned int, unsigned int);
  85. /*
  86. * Stop a performance monitoring counter.
  87. *
  88. * Processor-local operation.
  89. */
  90. void (*stop) (unsigned int);
  91. /*
  92. * Read the value of a performance monitoring counter.
  93. *
  94. * Processor-local operation.
  95. */
  96. uint64_t (*read) (unsigned int);
  97. /*
  98. * Custom overflow interrupt handler.
  99. *
  100. * Processor-local operation.
  101. */
  102. void (*handle_overflow_intr) (void);
  103. };
  104. /*
  105. * Performance monitoring device.
  106. *
  107. * This is a public structure.
  108. *
  109. * The PMC width is expressed in bits.
  110. *
  111. * If the driver doesn't provide an overflow interrupt handler, it may set
  112. * the poll interval, in ticks, to a duration that safely allows the detection
  113. * of a single overflow. A value of 0 lets the perfmon module compute a poll
  114. * interval itself.
  115. */
  116. struct perfmon_dev
  117. {
  118. const struct perfmon_dev_ops *ops;
  119. unsigned int pmc_width;
  120. uint64_t poll_interval;
  121. };
  122. // Performance monitoring thread data.
  123. struct perfmon_td;
  124. /*
  125. * Performance monitoring event.
  126. *
  127. * An event describes a single, well-defined hardware condition and tracks
  128. * its occurrences over a period of time.
  129. */
  130. struct perfmon_event;
  131. // Initialize thread-specific data.
  132. void perfmon_td_init (struct perfmon_td *td);
  133. /*
  134. * Load/unload events attached to a thread on the current processor.
  135. *
  136. * These functions should only be used by the scheduler on a context switch.
  137. * Interrupts and preemption must be disabled when calling these functions.
  138. */
  139. void perfmon_td_load (struct perfmon_td *td);
  140. void perfmon_td_unload (struct perfmon_td *td);
  141. // Initialize an event.
  142. int perfmon_event_init (struct perfmon_event *event, unsigned int id,
  143. unsigned int flags);
  144. /*
  145. * Attach/detach an event to/from a thread or a processor.
  146. *
  147. * Attaching an event allocates hardware resources and enables monitoring.
  148. * The number of occurrences for the given event is reset.
  149. *
  150. * An event can only be attached to one thread or processor at a time.
  151. */
  152. int perfmon_event_attach (struct perfmon_event *event, struct thread *thread);
  153. int perfmon_event_attach_cpu (struct perfmon_event *event, unsigned int cpu);
  154. int perfmon_event_detach (struct perfmon_event *event);
  155. // Obtain the number of occurrences of an event.
  156. uint64_t perfmon_event_read (struct perfmon_event *event);
  157. /*
  158. * Register a PMU device.
  159. *
  160. * Currently, there can only be a single system-wide PMU device, which
  161. * assumes the driver is the same for all processors.
  162. */
  163. void perfmon_register (struct perfmon_dev *dev);
  164. /*
  165. * Handle an overflow interrupt.
  166. *
  167. * This function must be called in interrupt context.
  168. */
  169. void perfmon_overflow_intr (void);
  170. /*
  171. * Report a PMC overflow.
  172. *
  173. * This function is intended to be used by PMU drivers using a custom
  174. * overflow interrupt handler.
  175. *
  176. * This function must be called in interrupt context.
  177. */
  178. void perfmon_report_overflow (unsigned int pmc_index);
  179. /*
  180. * This init operation provides :
  181. * - PMU device registration
  182. */
  183. INIT_OP_DECLARE (perfmon_bootstrap);
  184. #endif