perfmon.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /*
  31. * IDs of generic performance monitoring events.
  32. */
  33. #define PERFMON_EV_CYCLE 0
  34. #define PERFMON_EV_REF_CYCLE 1
  35. #define PERFMON_EV_INSTRUCTION 2
  36. #define PERFMON_EV_CACHE_REF 3
  37. #define PERFMON_EV_CACHE_MISS 4
  38. #define PERFMON_EV_BRANCH 5
  39. #define PERFMON_EV_BRANCH_MISS 6
  40. #define PERFMON_NR_GENERIC_EVENTS 7
  41. /*
  42. * Event flags.
  43. */
  44. #define PERFMON_EF_KERN 0x1 /* Monitor events in kernel mode */
  45. #define PERFMON_EF_USER 0x2 /* Monitor events in user mode */
  46. #define PERFMON_EF_RAW 0x4 /* Raw event ID, generic if unset */
  47. /*
  48. * Performance monitoring operations.
  49. *
  50. * This is a public structure.
  51. *
  52. * All operations are either global but serialized by the caller, or
  53. * processor-local and called with interrupts and preemption disabled.
  54. *
  55. * If the hardware doesn't efficiently support overflow interrupts, the
  56. * handler must be set to NULL, making the perfmon module perdiocally
  57. * check the raw value of the hardware counters.
  58. */
  59. struct perfmon_dev_ops {
  60. /*
  61. * Convert a generic event ID into a raw event ID.
  62. *
  63. * Global operation.
  64. */
  65. int (*translate)(unsigned int *raw_event_idp, unsigned int event_id);
  66. /*
  67. * Allocate a performance monitoring counter globally for the given
  68. * raw event ID, and return the counter ID through the given pointer.
  69. * The driver may return any PMC ID, as long as it uniquely identifies
  70. * the underlying counter. The PMC index is passed when reporting
  71. * overflows, if using a custom overflow interrupt handler.
  72. *
  73. * Global operation.
  74. */
  75. int (*alloc)(unsigned int *pmc_idp, unsigned int pmc_index,
  76. unsigned int raw_event_id);
  77. /*
  78. * Free an allocated performance monitoring counter.
  79. *
  80. * Global operation.
  81. */
  82. void (*free)(unsigned int pmc_id);
  83. /*
  84. * Start a performance monitoring counter for the given raw event ID.
  85. *
  86. * Processor-local operation.
  87. */
  88. void (*start)(unsigned int pmc_id, unsigned int raw_event_id);
  89. /*
  90. * Stop a performance monitoring counter.
  91. *
  92. * Processor-local operation.
  93. */
  94. void (*stop)(unsigned int pmc_id);
  95. /*
  96. * Read the value of a performance monitoring counter.
  97. *
  98. * Processor-local operation.
  99. */
  100. uint64_t (*read)(unsigned int pmc_id);
  101. /*
  102. * Custom overflow interrupt handler.
  103. *
  104. * Processor-local operation.
  105. */
  106. void (*handle_overflow_intr)(void);
  107. };
  108. /*
  109. * Performance monitoring device.
  110. *
  111. * This is a public structure.
  112. *
  113. * The PMC width is expressed in bits.
  114. *
  115. * If the driver doesn't provide an overflow interrupt handler, it may set
  116. * the poll interval, in ticks, to a duration that safely allows the detection
  117. * of a single overflow. A value of 0 lets the perfmon module compute a poll
  118. * interval itself.
  119. */
  120. struct perfmon_dev {
  121. const struct perfmon_dev_ops *ops;
  122. unsigned int pmc_width;
  123. uint64_t poll_interval;
  124. };
  125. /*
  126. * Performance monitoring thread data.
  127. */
  128. struct perfmon_td;
  129. /*
  130. * Performance monitoring event.
  131. *
  132. * An event describes a single, well-defined hardware condition and tracks
  133. * its occurrences over a period of time.
  134. */
  135. struct perfmon_event;
  136. /*
  137. * Initialize thread-specific data.
  138. */
  139. void perfmon_td_init(struct perfmon_td *td);
  140. /*
  141. * Load/unload events attached to a thread on the current processor.
  142. *
  143. * These functions should only be used by the scheduler on a context switch.
  144. * Interrupts and preemption must be disabled when calling these functions.
  145. */
  146. void perfmon_td_load(struct perfmon_td *td);
  147. void perfmon_td_unload(struct perfmon_td *td);
  148. /*
  149. * Initialize an event.
  150. */
  151. int perfmon_event_init(struct perfmon_event *event, unsigned int id,
  152. unsigned int flags);
  153. /*
  154. * Attach/detach an event to/from a thread or a processor.
  155. *
  156. * Attaching an event allocates hardware resources and enables monitoring.
  157. * The number of occurrences for the given event is reset.
  158. *
  159. * An event can only be attached to one thread or processor at a time.
  160. */
  161. int perfmon_event_attach(struct perfmon_event *event, struct thread *thread);
  162. int perfmon_event_attach_cpu(struct perfmon_event *event, unsigned int cpu);
  163. int perfmon_event_detach(struct perfmon_event *event);
  164. /*
  165. * Obtain the number of occurrences of an event.
  166. */
  167. uint64_t perfmon_event_read(struct perfmon_event *event);
  168. /*
  169. * Register a PMU device.
  170. *
  171. * Currently, there can only be a single system-wide PMU device, which
  172. * assumes the driver is the same for all processors.
  173. */
  174. void perfmon_register(struct perfmon_dev *dev);
  175. /*
  176. * Handle an overflow interrupt.
  177. *
  178. * This function must be called in interrupt context.
  179. */
  180. void perfmon_overflow_intr(void);
  181. /*
  182. * Report a PMC overflow.
  183. *
  184. * This function is intended to be used by PMU drivers using a custom
  185. * overflow interrupt handler.
  186. *
  187. * This function must be called in interrupt context.
  188. */
  189. void perfmon_report_overflow(unsigned int pmc_index);
  190. /*
  191. * This init operation provides :
  192. * - PMU device registration
  193. */
  194. INIT_OP_DECLARE(perfmon_bootstrap);
  195. #endif /* KERN_PERFMON_H */