kwork.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef PERF_UTIL_KWORK_H
  2. #define PERF_UTIL_KWORK_H
  3. #include "perf.h"
  4. #include "util/tool.h"
  5. #include "util/time-utils.h"
  6. #include <linux/bitmap.h>
  7. #include <linux/list.h>
  8. #include <linux/rbtree.h>
  9. #include <linux/types.h>
  10. struct perf_sample;
  11. struct perf_session;
  12. enum kwork_class_type {
  13. KWORK_CLASS_IRQ,
  14. KWORK_CLASS_SOFTIRQ,
  15. KWORK_CLASS_WORKQUEUE,
  16. KWORK_CLASS_SCHED,
  17. KWORK_CLASS_MAX,
  18. };
  19. enum kwork_report_type {
  20. KWORK_REPORT_RUNTIME,
  21. KWORK_REPORT_LATENCY,
  22. KWORK_REPORT_TIMEHIST,
  23. KWORK_REPORT_TOP,
  24. };
  25. enum kwork_trace_type {
  26. KWORK_TRACE_RAISE,
  27. KWORK_TRACE_ENTRY,
  28. KWORK_TRACE_EXIT,
  29. KWORK_TRACE_MAX,
  30. };
  31. /*
  32. * data structure:
  33. *
  34. * +==================+ +============+ +======================+
  35. * | class | | work | | atom |
  36. * +==================+ +============+ +======================+
  37. * +------------+ | +-----+ | | +------+ | | +-------+ +-----+ |
  38. * | perf_kwork | +-> | irq | --------|+-> | eth0 | --+-> | raise | - | ... | --+ +-----------+
  39. * +-----+------+ || +-----+ ||| +------+ ||| +-------+ +-----+ | | | |
  40. * | || ||| ||| | +-> | atom_page |
  41. * | || ||| ||| +-------+ +-----+ | | |
  42. * | class_list ||| |+-> | entry | - | ... | ----> | |
  43. * | || ||| ||| +-------+ +-----+ | | |
  44. * | || ||| ||| | +-> | |
  45. * | || ||| ||| +-------+ +-----+ | | | |
  46. * | || ||| |+-> | exit | - | ... | --+ +-----+-----+
  47. * | || ||| | | +-------+ +-----+ | |
  48. * | || ||| | | | |
  49. * | || ||| +-----+ | | | |
  50. * | || |+-> | ... | | | | |
  51. * | || | | +-----+ | | | |
  52. * | || | | | | | |
  53. * | || +---------+ | | +-----+ | | +-------+ +-----+ | |
  54. * | +-> | softirq | -------> | RCU | ---+-> | raise | - | ... | --+ +-----+-----+
  55. * | || +---------+ | | +-----+ ||| +-------+ +-----+ | | | |
  56. * | || | | ||| | +-> | atom_page |
  57. * | || | | ||| +-------+ +-----+ | | |
  58. * | || | | |+-> | entry | - | ... | ----> | |
  59. * | || | | ||| +-------+ +-----+ | | |
  60. * | || | | ||| | +-> | |
  61. * | || | | ||| +-------+ +-----+ | | | |
  62. * | || | | |+-> | exit | - | ... | --+ +-----+-----+
  63. * | || | | | | +-------+ +-----+ | |
  64. * | || | | | | | |
  65. * | || +-----------+ | | +-----+ | | | |
  66. * | +-> | workqueue | -----> | ... | | | | |
  67. * | | +-----------+ | | +-----+ | | | |
  68. * | +==================+ +============+ +======================+ |
  69. * | |
  70. * +----> atom_page_list ---------------------------------------------------------+
  71. *
  72. */
  73. struct kwork_atom {
  74. struct list_head list;
  75. u64 time;
  76. struct kwork_atom *prev;
  77. void *page_addr;
  78. unsigned long bit_inpage;
  79. };
  80. #define NR_ATOM_PER_PAGE 128
  81. struct kwork_atom_page {
  82. struct list_head list;
  83. struct kwork_atom atoms[NR_ATOM_PER_PAGE];
  84. DECLARE_BITMAP(bitmap, NR_ATOM_PER_PAGE);
  85. };
  86. struct perf_kwork;
  87. struct kwork_class;
  88. struct kwork_work {
  89. /*
  90. * class field
  91. */
  92. struct rb_node node;
  93. struct kwork_class *class;
  94. /*
  95. * work field
  96. */
  97. u64 id;
  98. int cpu;
  99. char *name;
  100. /*
  101. * atom field
  102. */
  103. u64 nr_atoms;
  104. struct list_head atom_list[KWORK_TRACE_MAX];
  105. /*
  106. * runtime report
  107. */
  108. u64 max_runtime;
  109. u64 max_runtime_start;
  110. u64 max_runtime_end;
  111. u64 total_runtime;
  112. /*
  113. * latency report
  114. */
  115. u64 max_latency;
  116. u64 max_latency_start;
  117. u64 max_latency_end;
  118. u64 total_latency;
  119. /*
  120. * top report
  121. */
  122. u32 cpu_usage;
  123. u32 tgid;
  124. bool is_kthread;
  125. };
  126. struct kwork_class {
  127. struct list_head list;
  128. const char *name;
  129. enum kwork_class_type type;
  130. unsigned int nr_tracepoints;
  131. const struct evsel_str_handler *tp_handlers;
  132. struct rb_root_cached work_root;
  133. int (*class_init)(struct kwork_class *class,
  134. struct perf_session *session);
  135. void (*work_init)(struct perf_kwork *kwork,
  136. struct kwork_class *class,
  137. struct kwork_work *work,
  138. enum kwork_trace_type src_type,
  139. struct evsel *evsel,
  140. struct perf_sample *sample,
  141. struct machine *machine);
  142. void (*work_name)(struct kwork_work *work,
  143. char *buf, int len);
  144. };
  145. struct trace_kwork_handler {
  146. int (*raise_event)(struct perf_kwork *kwork,
  147. struct kwork_class *class, struct evsel *evsel,
  148. struct perf_sample *sample, struct machine *machine);
  149. int (*entry_event)(struct perf_kwork *kwork,
  150. struct kwork_class *class, struct evsel *evsel,
  151. struct perf_sample *sample, struct machine *machine);
  152. int (*exit_event)(struct perf_kwork *kwork,
  153. struct kwork_class *class, struct evsel *evsel,
  154. struct perf_sample *sample, struct machine *machine);
  155. int (*sched_switch_event)(struct perf_kwork *kwork,
  156. struct kwork_class *class, struct evsel *evsel,
  157. struct perf_sample *sample, struct machine *machine);
  158. };
  159. struct __top_cpus_runtime {
  160. u64 load;
  161. u64 idle;
  162. u64 irq;
  163. u64 softirq;
  164. u64 total;
  165. };
  166. struct kwork_top_stat {
  167. DECLARE_BITMAP(all_cpus_bitmap, MAX_NR_CPUS);
  168. struct __top_cpus_runtime *cpus_runtime;
  169. };
  170. struct perf_kwork {
  171. /*
  172. * metadata
  173. */
  174. struct perf_tool tool;
  175. struct list_head class_list;
  176. struct list_head atom_page_list;
  177. struct list_head sort_list, cmp_id;
  178. struct rb_root_cached sorted_work_root;
  179. const struct trace_kwork_handler *tp_handler;
  180. /*
  181. * profile filters
  182. */
  183. const char *profile_name;
  184. const char *cpu_list;
  185. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  186. const char *time_str;
  187. struct perf_time_interval ptime;
  188. /*
  189. * options for command
  190. */
  191. bool force;
  192. const char *event_list_str;
  193. enum kwork_report_type report;
  194. /*
  195. * options for subcommand
  196. */
  197. bool summary;
  198. const char *sort_order;
  199. bool show_callchain;
  200. unsigned int max_stack;
  201. bool use_bpf;
  202. /*
  203. * statistics
  204. */
  205. u64 timestart;
  206. u64 timeend;
  207. unsigned long nr_events;
  208. unsigned long nr_lost_chunks;
  209. unsigned long nr_lost_events;
  210. u64 all_runtime;
  211. u64 all_count;
  212. u64 nr_skipped_events[KWORK_TRACE_MAX + 1];
  213. /*
  214. * perf kwork top data
  215. */
  216. struct kwork_top_stat top_stat;
  217. /* Add work callback. */
  218. struct kwork_work *(*add_work)(struct perf_kwork *kwork,
  219. struct kwork_class *class,
  220. struct kwork_work *key);
  221. };
  222. #ifdef HAVE_BPF_SKEL
  223. int perf_kwork__trace_prepare_bpf(struct perf_kwork *kwork);
  224. int perf_kwork__report_read_bpf(struct perf_kwork *kwork);
  225. void perf_kwork__report_cleanup_bpf(void);
  226. void perf_kwork__trace_start(void);
  227. void perf_kwork__trace_finish(void);
  228. int perf_kwork__top_prepare_bpf(struct perf_kwork *kwork);
  229. int perf_kwork__top_read_bpf(struct perf_kwork *kwork);
  230. void perf_kwork__top_cleanup_bpf(void);
  231. void perf_kwork__top_start(void);
  232. void perf_kwork__top_finish(void);
  233. #else /* !HAVE_BPF_SKEL */
  234. static inline int
  235. perf_kwork__trace_prepare_bpf(struct perf_kwork *kwork __maybe_unused)
  236. {
  237. return -1;
  238. }
  239. static inline int
  240. perf_kwork__report_read_bpf(struct perf_kwork *kwork __maybe_unused)
  241. {
  242. return -1;
  243. }
  244. static inline void perf_kwork__report_cleanup_bpf(void) {}
  245. static inline void perf_kwork__trace_start(void) {}
  246. static inline void perf_kwork__trace_finish(void) {}
  247. static inline int
  248. perf_kwork__top_prepare_bpf(struct perf_kwork *kwork __maybe_unused)
  249. {
  250. return -1;
  251. }
  252. static inline int
  253. perf_kwork__top_read_bpf(struct perf_kwork *kwork __maybe_unused)
  254. {
  255. return -1;
  256. }
  257. static inline void perf_kwork__top_cleanup_bpf(void) {}
  258. static inline void perf_kwork__top_start(void) {}
  259. static inline void perf_kwork__top_finish(void) {}
  260. #endif /* HAVE_BPF_SKEL */
  261. #endif /* PERF_UTIL_KWORK_H */