sort.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. u16 callchain_size;
  101. union {
  102. /*
  103. * Since perf diff only supports the stdio output, TUI
  104. * fields are only accessed from perf report (or perf
  105. * top). So make it a union to reduce memory usage.
  106. */
  107. struct hist_entry_diff diff;
  108. struct /* for TUI */ {
  109. u16 row_offset;
  110. u16 nr_rows;
  111. bool init_have_children;
  112. bool unfolded;
  113. bool has_children;
  114. bool has_no_entry;
  115. };
  116. };
  117. char *srcline;
  118. char *srcfile;
  119. struct symbol *parent;
  120. struct branch_info *branch_info;
  121. struct hists *hists;
  122. struct mem_info *mem_info;
  123. void *raw_data;
  124. u32 raw_size;
  125. void *trace_output;
  126. struct perf_hpp_list *hpp_list;
  127. struct hist_entry *parent_he;
  128. struct hist_entry_ops *ops;
  129. union {
  130. /* this is for hierarchical entry structure */
  131. struct {
  132. struct rb_root hroot_in;
  133. struct rb_root hroot_out;
  134. }; /* non-leaf entries */
  135. struct rb_root sorted_chain; /* leaf entry has callchains */
  136. };
  137. struct callchain_root callchain[0]; /* must be last member */
  138. };
  139. static __pure inline bool hist_entry__has_callchains(struct hist_entry *he)
  140. {
  141. return he->callchain_size != 0;
  142. }
  143. static inline bool hist_entry__has_pairs(struct hist_entry *he)
  144. {
  145. return !list_empty(&he->pairs.node);
  146. }
  147. static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
  148. {
  149. if (hist_entry__has_pairs(he))
  150. return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
  151. return NULL;
  152. }
  153. static inline void hist_entry__add_pair(struct hist_entry *pair,
  154. struct hist_entry *he)
  155. {
  156. list_add_tail(&pair->pairs.node, &he->pairs.head);
  157. }
  158. static inline float hist_entry__get_percent_limit(struct hist_entry *he)
  159. {
  160. u64 period = he->stat.period;
  161. u64 total_period = hists__total_period(he->hists);
  162. if (unlikely(total_period == 0))
  163. return 0;
  164. if (symbol_conf.cumulate_callchain)
  165. period = he->stat_acc->period;
  166. return period * 100.0 / total_period;
  167. }
  168. static inline u64 cl_address(u64 address)
  169. {
  170. /* return the cacheline of the address */
  171. return (address & ~(cacheline_size() - 1));
  172. }
  173. static inline u64 cl_offset(u64 address)
  174. {
  175. /* return the cacheline of the address */
  176. return (address & (cacheline_size() - 1));
  177. }
  178. enum sort_mode {
  179. SORT_MODE__NORMAL,
  180. SORT_MODE__BRANCH,
  181. SORT_MODE__MEMORY,
  182. SORT_MODE__TOP,
  183. SORT_MODE__DIFF,
  184. SORT_MODE__TRACEPOINT,
  185. };
  186. enum sort_type {
  187. /* common sort keys */
  188. SORT_PID,
  189. SORT_COMM,
  190. SORT_DSO,
  191. SORT_SYM,
  192. SORT_PARENT,
  193. SORT_CPU,
  194. SORT_SOCKET,
  195. SORT_SRCLINE,
  196. SORT_SRCFILE,
  197. SORT_LOCAL_WEIGHT,
  198. SORT_GLOBAL_WEIGHT,
  199. SORT_TRANSACTION,
  200. SORT_TRACE,
  201. SORT_SYM_SIZE,
  202. SORT_DSO_SIZE,
  203. SORT_CGROUP_ID,
  204. /* branch stack specific sort keys */
  205. __SORT_BRANCH_STACK,
  206. SORT_DSO_FROM = __SORT_BRANCH_STACK,
  207. SORT_DSO_TO,
  208. SORT_SYM_FROM,
  209. SORT_SYM_TO,
  210. SORT_MISPREDICT,
  211. SORT_ABORT,
  212. SORT_IN_TX,
  213. SORT_CYCLES,
  214. SORT_SRCLINE_FROM,
  215. SORT_SRCLINE_TO,
  216. /* memory mode specific sort keys */
  217. __SORT_MEMORY_MODE,
  218. SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
  219. SORT_MEM_DADDR_DSO,
  220. SORT_MEM_LOCKED,
  221. SORT_MEM_TLB,
  222. SORT_MEM_LVL,
  223. SORT_MEM_SNOOP,
  224. SORT_MEM_DCACHELINE,
  225. SORT_MEM_IADDR_SYMBOL,
  226. SORT_MEM_PHYS_DADDR,
  227. };
  228. /*
  229. * configurable sorting bits
  230. */
  231. struct sort_entry {
  232. const char *se_header;
  233. int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
  234. int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
  235. int64_t (*se_sort)(struct hist_entry *, struct hist_entry *);
  236. int (*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
  237. unsigned int width);
  238. int (*se_filter)(struct hist_entry *he, int type, const void *arg);
  239. u8 se_width_idx;
  240. };
  241. extern struct sort_entry sort_thread;
  242. extern struct list_head hist_entry__sort_list;
  243. struct perf_evlist;
  244. struct tep_handle;
  245. int setup_sorting(struct perf_evlist *evlist);
  246. int setup_output_field(void);
  247. void reset_output_field(void);
  248. void sort__setup_elide(FILE *fp);
  249. void perf_hpp__set_elide(int idx, bool elide);
  250. int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
  251. bool is_strict_order(const char *order);
  252. int hpp_dimension__add_output(unsigned col);
  253. void reset_dimensions(void);
  254. int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
  255. struct perf_evlist *evlist,
  256. int level);
  257. int output_field_add(struct perf_hpp_list *list, char *tok);
  258. int64_t
  259. sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right);
  260. int64_t
  261. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right);
  262. int64_t
  263. sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right);
  264. char *hist_entry__srcline(struct hist_entry *he);
  265. #endif /* __PERF_SORT_H */