sort.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_SORT_H
  3. #define __PERF_SORT_H
  4. #include "../builtin.h"
  5. #include <regex.h>
  6. #include "color.h"
  7. #include <linux/list.h>
  8. #include "cache.h"
  9. #include <linux/rbtree.h>
  10. #include "symbol.h"
  11. #include "string.h"
  12. #include "callchain.h"
  13. #include "values.h"
  14. #include "../perf.h"
  15. #include "debug.h"
  16. #include "header.h"
  17. #include <subcmd/parse-options.h>
  18. #include "parse-events.h"
  19. #include "hist.h"
  20. #include "srcline.h"
  21. struct thread;
  22. extern regex_t parent_regex;
  23. extern const char *sort_order;
  24. extern const char *field_order;
  25. extern const char default_parent_pattern[];
  26. extern const char *parent_pattern;
  27. extern const char *default_sort_order;
  28. extern regex_t ignore_callees_regex;
  29. extern int have_ignore_callees;
  30. extern enum sort_mode sort__mode;
  31. extern struct sort_entry sort_comm;
  32. extern struct sort_entry sort_dso;
  33. extern struct sort_entry sort_sym;
  34. extern struct sort_entry sort_parent;
  35. extern struct sort_entry sort_dso_from;
  36. extern struct sort_entry sort_dso_to;
  37. extern struct sort_entry sort_sym_from;
  38. extern struct sort_entry sort_sym_to;
  39. extern struct sort_entry sort_srcline;
  40. extern enum sort_type sort__first_dimension;
  41. extern const char default_mem_sort_order[];
  42. struct he_stat {
  43. u64 period;
  44. u64 period_sys;
  45. u64 period_us;
  46. u64 period_guest_sys;
  47. u64 period_guest_us;
  48. u64 weight;
  49. u32 nr_events;
  50. };
  51. struct namespace_id {
  52. u64 dev;
  53. u64 ino;
  54. };
  55. struct hist_entry_diff {
  56. bool computed;
  57. union {
  58. /* PERF_HPP__DELTA */
  59. double period_ratio_delta;
  60. /* PERF_HPP__RATIO */
  61. double period_ratio;
  62. /* HISTC_WEIGHTED_DIFF */
  63. s64 wdiff;
  64. };
  65. };
  66. struct hist_entry_ops {
  67. void *(*new)(size_t size);
  68. void (*free)(void *ptr);
  69. };
  70. /**
  71. * struct hist_entry - histogram entry
  72. *
  73. * @row_offset - offset from the first callchain expanded to appear on screen
  74. * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
  75. */
  76. struct hist_entry {
  77. struct rb_node rb_node_in;
  78. struct rb_node rb_node;
  79. union {
  80. struct list_head node;
  81. struct list_head head;
  82. } pairs;
  83. struct he_stat stat;
  84. struct he_stat *stat_acc;
  85. struct map_symbol ms;
  86. struct thread *thread;
  87. struct comm *comm;
  88. struct namespace_id cgroup_id;
  89. u64 ip;
  90. u64 transaction;
  91. s32 socket;
  92. s32 cpu;
  93. u8 cpumode;
  94. u8 depth;
  95. /* We are added by hists__add_dummy_entry. */
  96. bool dummy;
  97. bool leaf;
  98. char level;
  99. u8 filtered;
  100. union {
  101. /*
  102. * Since perf diff only supports the stdio output, TUI
  103. * fields are only accessed from perf report (or perf
  104. * top). So make it a union to reduce memory usage.
  105. */
  106. struct hist_entry_diff diff;
  107. struct /* for TUI */ {
  108. u16 row_offset;
  109. u16 nr_rows;
  110. bool init_have_children;
  111. bool unfolded;
  112. bool has_children;
  113. bool has_no_entry;
  114. };
  115. };
  116. char *srcline;
  117. char *srcfile;
  118. struct symbol *parent;
  119. struct branch_info *branch_info;
  120. struct hists *hists;
  121. struct mem_info *mem_info;
  122. void *raw_data;
  123. u32 raw_size;
  124. void *trace_output;
  125. struct perf_hpp_list *hpp_list;
  126. struct hist_entry *parent_he;
  127. struct hist_entry_ops *ops;
  128. union {
  129. /* this is for hierarchical entry structure */
  130. struct {
  131. struct rb_root hroot_in;
  132. struct rb_root hroot_out;
  133. }; /* non-leaf entries */
  134. struct rb_root sorted_chain; /* leaf entry has callchains */
  135. };
  136. struct callchain_root callchain[0]; /* must be last member */
  137. };
  138. static __pure inline bool hist_entry__has_callchains(struct hist_entry *he)
  139. {
  140. return hists__has_callchains(he->hists);
  141. }
  142. static inline bool hist_entry__has_pairs(struct hist_entry *he)
  143. {
  144. return !list_empty(&he->pairs.node);
  145. }
  146. static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
  147. {
  148. if (hist_entry__has_pairs(he))
  149. return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
  150. return NULL;
  151. }
  152. static inline void hist_entry__add_pair(struct hist_entry *pair,
  153. struct hist_entry *he)
  154. {
  155. list_add_tail(&pair->pairs.node, &he->pairs.head);
  156. }
  157. static inline float hist_entry__get_percent_limit(struct hist_entry *he)
  158. {
  159. u64 period = he->stat.period;
  160. u64 total_period = hists__total_period(he->hists);
  161. if (unlikely(total_period == 0))
  162. return 0;
  163. if (symbol_conf.cumulate_callchain)
  164. period = he->stat_acc->period;
  165. return period * 100.0 / total_period;
  166. }
  167. static inline u64 cl_address(u64 address)
  168. {
  169. /* return the cacheline of the address */
  170. return (address & ~(cacheline_size() - 1));
  171. }
  172. static inline u64 cl_offset(u64 address)
  173. {
  174. /* return the cacheline of the address */
  175. return (address & (cacheline_size() - 1));
  176. }
  177. enum sort_mode {
  178. SORT_MODE__NORMAL,
  179. SORT_MODE__BRANCH,
  180. SORT_MODE__MEMORY,
  181. SORT_MODE__TOP,
  182. SORT_MODE__DIFF,
  183. SORT_MODE__TRACEPOINT,
  184. };
  185. enum sort_type {
  186. /* common sort keys */
  187. SORT_PID,
  188. SORT_COMM,
  189. SORT_DSO,
  190. SORT_SYM,
  191. SORT_PARENT,
  192. SORT_CPU,
  193. SORT_SOCKET,
  194. SORT_SRCLINE,
  195. SORT_SRCFILE,
  196. SORT_LOCAL_WEIGHT,
  197. SORT_GLOBAL_WEIGHT,
  198. SORT_TRANSACTION,
  199. SORT_TRACE,
  200. SORT_SYM_SIZE,
  201. SORT_DSO_SIZE,
  202. SORT_CGROUP_ID,
  203. /* branch stack specific sort keys */
  204. __SORT_BRANCH_STACK,
  205. SORT_DSO_FROM = __SORT_BRANCH_STACK,
  206. SORT_DSO_TO,
  207. SORT_SYM_FROM,
  208. SORT_SYM_TO,
  209. SORT_MISPREDICT,
  210. SORT_ABORT,
  211. SORT_IN_TX,
  212. SORT_CYCLES,
  213. SORT_SRCLINE_FROM,
  214. SORT_SRCLINE_TO,
  215. /* memory mode specific sort keys */
  216. __SORT_MEMORY_MODE,
  217. SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
  218. SORT_MEM_DADDR_DSO,
  219. SORT_MEM_LOCKED,
  220. SORT_MEM_TLB,
  221. SORT_MEM_LVL,
  222. SORT_MEM_SNOOP,
  223. SORT_MEM_DCACHELINE,
  224. SORT_MEM_IADDR_SYMBOL,
  225. SORT_MEM_PHYS_DADDR,
  226. };
  227. /*
  228. * configurable sorting bits
  229. */
  230. struct sort_entry {
  231. const char *se_header;
  232. int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
  233. int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
  234. int64_t (*se_sort)(struct hist_entry *, struct hist_entry *);
  235. int (*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
  236. unsigned int width);
  237. int (*se_filter)(struct hist_entry *he, int type, const void *arg);
  238. u8 se_width_idx;
  239. };
  240. extern struct sort_entry sort_thread;
  241. extern struct list_head hist_entry__sort_list;
  242. struct perf_evlist;
  243. struct pevent;
  244. int setup_sorting(struct perf_evlist *evlist);
  245. int setup_output_field(void);
  246. void reset_output_field(void);
  247. void sort__setup_elide(FILE *fp);
  248. void perf_hpp__set_elide(int idx, bool elide);
  249. int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
  250. bool is_strict_order(const char *order);
  251. int hpp_dimension__add_output(unsigned col);
  252. void reset_dimensions(void);
  253. int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
  254. struct perf_evlist *evlist,
  255. int level);
  256. int output_field_add(struct perf_hpp_list *list, char *tok);
  257. int64_t
  258. sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right);
  259. int64_t
  260. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right);
  261. int64_t
  262. sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right);
  263. char *hist_entry__srcline(struct hist_entry *he);
  264. #endif /* __PERF_SORT_H */