1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2dd68ada2SJohn Kacur #ifndef __PERF_SORT_H 3dd68ada2SJohn Kacur #define __PERF_SORT_H 41eae20c1SArnaldo Carvalho de Melo #include <regex.h> 5185bcb92SArnaldo Carvalho de Melo #include <stdbool.h> 6dd68ada2SJohn Kacur #include <linux/list.h> 7dd68ada2SJohn Kacur #include <linux/rbtree.h> 89f4e8ff2SArnaldo Carvalho de Melo #include "map_symbol.h" 99f4e8ff2SArnaldo Carvalho de Melo #include "symbol_conf.h" 10dd68ada2SJohn Kacur #include "callchain.h" 11dd68ada2SJohn Kacur #include "values.h" 1214135663SNamhyung Kim #include "hist.h" 13cebf7d51SJin Yao #include "stat.h" 14cebf7d51SJin Yao #include "spark.h" 15e7ff8920SArnaldo Carvalho de Melo 16185bcb92SArnaldo Carvalho de Melo struct option; 17e7ff8920SArnaldo Carvalho de Melo struct thread; 18dd68ada2SJohn Kacur 19dd68ada2SJohn Kacur extern regex_t parent_regex; 20edb7c60eSArnaldo Carvalho de Melo extern const char *sort_order; 21a7d945bcSNamhyung Kim extern const char *field_order; 22edb7c60eSArnaldo Carvalho de Melo extern const char default_parent_pattern[]; 23edb7c60eSArnaldo Carvalho de Melo extern const char *parent_pattern; 24fa1f4565SArnaldo Carvalho de Melo extern const char *default_sort_order; 25b21484f1SGreg Price extern regex_t ignore_callees_regex; 26b21484f1SGreg Price extern int have_ignore_callees; 2755369fc1SNamhyung Kim extern enum sort_mode sort__mode; 28dd68ada2SJohn Kacur extern struct sort_entry sort_comm; 29dd68ada2SJohn Kacur extern struct sort_entry sort_dso; 30dd68ada2SJohn Kacur extern struct sort_entry sort_sym; 31dd68ada2SJohn Kacur extern struct sort_entry sort_parent; 32a68c2c58SStephane Eranian extern struct sort_entry sort_dso_from; 33a68c2c58SStephane Eranian extern struct sort_entry sort_dso_to; 34a68c2c58SStephane Eranian extern struct sort_entry sort_sym_from; 35a68c2c58SStephane Eranian extern struct sort_entry sort_sym_to; 36f666ac0dSJiri Olsa extern struct sort_entry sort_srcline; 37a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension; 38228f14f2SYunlong Song extern const char default_mem_sort_order[]; 39dd68ada2SJohn Kacur 404968ac8fSAndi Kleen struct res_sample { 414968ac8fSAndi Kleen u64 time; 424968ac8fSAndi Kleen int cpu; 434968ac8fSAndi Kleen int tid; 444968ac8fSAndi Kleen }; 454968ac8fSAndi Kleen 46b24c28f7SNamhyung Kim struct he_stat { 47b24c28f7SNamhyung Kim u64 period; 48b24c28f7SNamhyung Kim u64 period_sys; 49b24c28f7SNamhyung Kim u64 period_us; 50b24c28f7SNamhyung Kim u64 period_guest_sys; 51b24c28f7SNamhyung Kim u64 period_guest_us; 5205484298SAndi Kleen u64 weight; 53b24c28f7SNamhyung Kim u32 nr_events; 54b24c28f7SNamhyung Kim }; 55b24c28f7SNamhyung Kim 56d890a98cSHari Bathini struct namespace_id { 57d890a98cSHari Bathini u64 dev; 58d890a98cSHari Bathini u64 ino; 59d890a98cSHari Bathini }; 60d890a98cSHari Bathini 6196c47f19SJiri Olsa struct hist_entry_diff { 6296c47f19SJiri Olsa bool computed; 63a0b404f4SNamhyung Kim union { 6496c47f19SJiri Olsa /* PERF_HPP__DELTA */ 6596c47f19SJiri Olsa double period_ratio_delta; 6696c47f19SJiri Olsa 6796c47f19SJiri Olsa /* PERF_HPP__RATIO */ 6896c47f19SJiri Olsa double period_ratio; 6981d5f958SJiri Olsa 7081d5f958SJiri Olsa /* HISTC_WEIGHTED_DIFF */ 7181d5f958SJiri Olsa s64 wdiff; 7299150a1fSJin Yao 7399150a1fSJin Yao /* PERF_HPP_DIFF__CYCLES */ 7499150a1fSJin Yao s64 cycles; 7596c47f19SJiri Olsa }; 76cebf7d51SJin Yao struct stats stats; 77cebf7d51SJin Yao unsigned long svals[NUM_SPARKS]; 78a0b404f4SNamhyung Kim }; 7996c47f19SJiri Olsa 80f542e767SJiri Olsa struct hist_entry_ops { 81f542e767SJiri Olsa void *(*new)(size_t size); 82f542e767SJiri Olsa void (*free)(void *ptr); 83f542e767SJiri Olsa }; 84f542e767SJiri Olsa 850f0cbf7aSArnaldo Carvalho de Melo /** 860f0cbf7aSArnaldo Carvalho de Melo * struct hist_entry - histogram entry 870f0cbf7aSArnaldo Carvalho de Melo * 880f0cbf7aSArnaldo Carvalho de Melo * @row_offset - offset from the first callchain expanded to appear on screen 890f0cbf7aSArnaldo Carvalho de Melo * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding 900f0cbf7aSArnaldo Carvalho de Melo */ 91dd68ada2SJohn Kacur struct hist_entry { 921980c2ebSArnaldo Carvalho de Melo struct rb_node rb_node_in; 93dd68ada2SJohn Kacur struct rb_node rb_node; 94b821c732SArnaldo Carvalho de Melo union { 95b821c732SArnaldo Carvalho de Melo struct list_head node; 96b821c732SArnaldo Carvalho de Melo struct list_head head; 97b821c732SArnaldo Carvalho de Melo } pairs; 98b24c28f7SNamhyung Kim struct he_stat stat; 99f8be1c8cSNamhyung Kim struct he_stat *stat_acc; 10059fd5306SArnaldo Carvalho de Melo struct map_symbol ms; 101a5e29acaSArnaldo Carvalho de Melo struct thread *thread; 1024dfced35SNamhyung Kim struct comm *comm; 103d890a98cSHari Bathini struct namespace_id cgroup_id; 104b629f3e9SNamhyung Kim u64 cgroup; 105dd68ada2SJohn Kacur u64 ip; 106475eeab9SAndi Kleen u64 transaction; 1070c4c4debSKan Liang s32 socket; 108f60f3593SArun Sharma s32 cpu; 1097365be55SDon Zickus u8 cpumode; 110aef810ecSNamhyung Kim u8 depth; 1110f0cbf7aSArnaldo Carvalho de Melo 112e0af43d2SJiri Olsa /* We are added by hists__add_dummy_entry. */ 113e0af43d2SJiri Olsa bool dummy; 114aef810ecSNamhyung Kim bool leaf; 115e0af43d2SJiri Olsa 116dd68ada2SJohn Kacur char level; 117a5e29acaSArnaldo Carvalho de Melo u8 filtered; 11841477acfSArnaldo Carvalho de Melo 11941477acfSArnaldo Carvalho de Melo u16 callchain_size; 12029750821SNamhyung Kim union { 12129750821SNamhyung Kim /* 12229750821SNamhyung Kim * Since perf diff only supports the stdio output, TUI 12329750821SNamhyung Kim * fields are only accessed from perf report (or perf 12403440c4eSMasahiro Yamada * top). So make it a union to reduce memory usage. 12529750821SNamhyung Kim */ 12629750821SNamhyung Kim struct hist_entry_diff diff; 12729750821SNamhyung Kim struct /* for TUI */ { 12829750821SNamhyung Kim u16 row_offset; 12929750821SNamhyung Kim u16 nr_rows; 130d8a0f800SNamhyung Kim bool init_have_children; 1313698dab1SNamhyung Kim bool unfolded; 1323698dab1SNamhyung Kim bool has_children; 13379dded87SNamhyung Kim bool has_no_entry; 13429750821SNamhyung Kim }; 13529750821SNamhyung Kim }; 136409a8be6SArnaldo Carvalho de Melo char *srcline; 13731191a85SAndi Kleen char *srcfile; 138439d473bSArnaldo Carvalho de Melo struct symbol *parent; 139b5387528SRoberto Agostino Vitillo struct branch_info *branch_info; 1403723908dSAndi Kleen long time; 141ae359f19SJiri Olsa struct hists *hists; 14298a3b32cSStephane Eranian struct mem_info *mem_info; 143fe96245cSJin Yao struct block_info *block_info; 14472392834SNamhyung Kim void *raw_data; 14572392834SNamhyung Kim u32 raw_size; 1464968ac8fSAndi Kleen int num_res; 1474968ac8fSAndi Kleen struct res_sample *res_samples; 14860517d28SNamhyung Kim void *trace_output; 1491b2dbbf4SNamhyung Kim struct perf_hpp_list *hpp_list; 150aef810ecSNamhyung Kim struct hist_entry *parent_he; 151f542e767SJiri Olsa struct hist_entry_ops *ops; 152aef810ecSNamhyung Kim union { 153aef810ecSNamhyung Kim /* this is for hierarchical entry structure */ 154aef810ecSNamhyung Kim struct { 1552eb3d689SDavidlohr Bueso struct rb_root_cached hroot_in; 1562eb3d689SDavidlohr Bueso struct rb_root_cached hroot_out; 157aef810ecSNamhyung Kim }; /* non-leaf entries */ 158aef810ecSNamhyung Kim struct rb_root sorted_chain; /* leaf entry has callchains */ 159aef810ecSNamhyung Kim }; 16098a3b32cSStephane Eranian struct callchain_root callchain[0]; /* must be last member */ 16186a9eee0SArnaldo Carvalho de Melo }; 162dd68ada2SJohn Kacur 1630b5d6eceSArnaldo Carvalho de Melo static __pure inline bool hist_entry__has_callchains(struct hist_entry *he) 1640b5d6eceSArnaldo Carvalho de Melo { 165e5654455SArnaldo Carvalho de Melo return he->callchain_size != 0; 1660b5d6eceSArnaldo Carvalho de Melo } 1670b5d6eceSArnaldo Carvalho de Melo 168bdc633feSArnaldo Carvalho de Melo int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, size_t size, unsigned int width); 169bdc633feSArnaldo Carvalho de Melo 170b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he) 171b821c732SArnaldo Carvalho de Melo { 172b821c732SArnaldo Carvalho de Melo return !list_empty(&he->pairs.node); 173b821c732SArnaldo Carvalho de Melo } 174b821c732SArnaldo Carvalho de Melo 175b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he) 176b821c732SArnaldo Carvalho de Melo { 177b821c732SArnaldo Carvalho de Melo if (hist_entry__has_pairs(he)) 178b821c732SArnaldo Carvalho de Melo return list_entry(he->pairs.node.next, struct hist_entry, pairs.node); 179b821c732SArnaldo Carvalho de Melo return NULL; 180b821c732SArnaldo Carvalho de Melo } 181b821c732SArnaldo Carvalho de Melo 1824d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair, 1834d23322aSJiri Olsa struct hist_entry *he) 184b821c732SArnaldo Carvalho de Melo { 1854d23322aSJiri Olsa list_add_tail(&pair->pairs.node, &he->pairs.head); 186b821c732SArnaldo Carvalho de Melo } 187b821c732SArnaldo Carvalho de Melo 18814135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he) 18914135663SNamhyung Kim { 19014135663SNamhyung Kim u64 period = he->stat.period; 19114135663SNamhyung Kim u64 total_period = hists__total_period(he->hists); 19214135663SNamhyung Kim 19314135663SNamhyung Kim if (unlikely(total_period == 0)) 19414135663SNamhyung Kim return 0; 19514135663SNamhyung Kim 19614135663SNamhyung Kim if (symbol_conf.cumulate_callchain) 19714135663SNamhyung Kim period = he->stat_acc->period; 19814135663SNamhyung Kim 19914135663SNamhyung Kim return period * 100.0 / total_period; 20014135663SNamhyung Kim } 20114135663SNamhyung Kim 20255369fc1SNamhyung Kim enum sort_mode { 20355369fc1SNamhyung Kim SORT_MODE__NORMAL, 20455369fc1SNamhyung Kim SORT_MODE__BRANCH, 20555369fc1SNamhyung Kim SORT_MODE__MEMORY, 206512ae1bdSNamhyung Kim SORT_MODE__TOP, 207512ae1bdSNamhyung Kim SORT_MODE__DIFF, 208d49dadeaSNamhyung Kim SORT_MODE__TRACEPOINT, 20955369fc1SNamhyung Kim }; 21055369fc1SNamhyung Kim 211a4fb581bSFrederic Weisbecker enum sort_type { 212fc5871edSNamhyung Kim /* common sort keys */ 213a4fb581bSFrederic Weisbecker SORT_PID, 214a4fb581bSFrederic Weisbecker SORT_COMM, 215a4fb581bSFrederic Weisbecker SORT_DSO, 216a4fb581bSFrederic Weisbecker SORT_SYM, 217f60f3593SArun Sharma SORT_PARENT, 218f60f3593SArun Sharma SORT_CPU, 2192e7ea3abSKan Liang SORT_SOCKET, 220fc5871edSNamhyung Kim SORT_SRCLINE, 22131191a85SAndi Kleen SORT_SRCFILE, 222f9ea55d0SAndi Kleen SORT_LOCAL_WEIGHT, 223f9ea55d0SAndi Kleen SORT_GLOBAL_WEIGHT, 224475eeab9SAndi Kleen SORT_TRANSACTION, 225a34bb6a0SNamhyung Kim SORT_TRACE, 2267768f8daSCharles Baylis SORT_SYM_SIZE, 227b74d12d5SKim Phillips SORT_DSO_SIZE, 228b629f3e9SNamhyung Kim SORT_CGROUP, 229d890a98cSHari Bathini SORT_CGROUP_ID, 230ec6ae74fSJin Yao SORT_SYM_IPC_NULL, 2313723908dSAndi Kleen SORT_TIME, 232fc5871edSNamhyung Kim 233fc5871edSNamhyung Kim /* branch stack specific sort keys */ 234fc5871edSNamhyung Kim __SORT_BRANCH_STACK, 235fc5871edSNamhyung Kim SORT_DSO_FROM = __SORT_BRANCH_STACK, 236b5387528SRoberto Agostino Vitillo SORT_DSO_TO, 237b5387528SRoberto Agostino Vitillo SORT_SYM_FROM, 238b5387528SRoberto Agostino Vitillo SORT_SYM_TO, 239b5387528SRoberto Agostino Vitillo SORT_MISPREDICT, 240f5d05bceSAndi Kleen SORT_ABORT, 241f5d05bceSAndi Kleen SORT_IN_TX, 2420e332f03SAndi Kleen SORT_CYCLES, 243508be0dfSAndi Kleen SORT_SRCLINE_FROM, 244508be0dfSAndi Kleen SORT_SRCLINE_TO, 245ec6ae74fSJin Yao SORT_SYM_IPC, 246afab87b9SNamhyung Kim 247afab87b9SNamhyung Kim /* memory mode specific sort keys */ 248afab87b9SNamhyung Kim __SORT_MEMORY_MODE, 249f9ea55d0SAndi Kleen SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE, 250afab87b9SNamhyung Kim SORT_MEM_DADDR_DSO, 251afab87b9SNamhyung Kim SORT_MEM_LOCKED, 252afab87b9SNamhyung Kim SORT_MEM_TLB, 253afab87b9SNamhyung Kim SORT_MEM_LVL, 254afab87b9SNamhyung Kim SORT_MEM_SNOOP, 2559b32ba71SDon Zickus SORT_MEM_DCACHELINE, 25628e6db20SDon Zickus SORT_MEM_IADDR_SYMBOL, 2578780fb25SKan Liang SORT_MEM_PHYS_DADDR, 258a4fb581bSFrederic Weisbecker }; 259a4fb581bSFrederic Weisbecker 260dd68ada2SJohn Kacur /* 261dd68ada2SJohn Kacur * configurable sorting bits 262dd68ada2SJohn Kacur */ 263dd68ada2SJohn Kacur 264dd68ada2SJohn Kacur struct sort_entry { 265fcd14984SFrederic Weisbecker const char *se_header; 266dd68ada2SJohn Kacur 267fcd14984SFrederic Weisbecker int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *); 268fcd14984SFrederic Weisbecker int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *); 269202e7a6dSNamhyung Kim int64_t (*se_sort)(struct hist_entry *, struct hist_entry *); 270316c7136SArnaldo Carvalho de Melo int (*se_snprintf)(struct hist_entry *he, char *bf, size_t size, 271a4e3b956SArnaldo Carvalho de Melo unsigned int width); 27254430101SNamhyung Kim int (*se_filter)(struct hist_entry *he, int type, const void *arg); 2738a6c5b26SArnaldo Carvalho de Melo u8 se_width_idx; 274dd68ada2SJohn Kacur }; 275dd68ada2SJohn Kacur 27699150a1fSJin Yao struct block_hist { 27799150a1fSJin Yao struct hists block_hists; 27899150a1fSJin Yao struct perf_hpp_list block_list; 27999150a1fSJin Yao struct perf_hpp_fmt block_fmt; 28099150a1fSJin Yao int block_idx; 28199150a1fSJin Yao bool valid; 28299150a1fSJin Yao struct hist_entry he; 28399150a1fSJin Yao }; 28499150a1fSJin Yao 285dd68ada2SJohn Kacur extern struct sort_entry sort_thread; 286dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list; 287dd68ada2SJohn Kacur 28863503dbaSJiri Olsa struct evlist; 289096177a8STzvetomir Stoyanov (VMware) struct tep_handle; 29063503dbaSJiri Olsa int setup_sorting(struct evlist *evlist); 291a7d945bcSNamhyung Kim int setup_output_field(void); 2921c89fe9bSNamhyung Kim void reset_output_field(void); 29308e71542SNamhyung Kim void sort__setup_elide(FILE *fp); 294f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide); 295dd68ada2SJohn Kacur 296702fb9b4SAndi Kleen const char *sort_help(const char *prefix); 297702fb9b4SAndi Kleen 298b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset); 299b21484f1SGreg Price 3002f3f9bcfSJiri Olsa bool is_strict_order(const char *order); 301beeaaeb3SJiri Olsa 302beeaaeb3SJiri Olsa int hpp_dimension__add_output(unsigned col); 303bcf98740SJiri Olsa void reset_dimensions(void); 304a76490e4SJiri Olsa int sort_dimension__add(struct perf_hpp_list *list, const char *tok, 30563503dbaSJiri Olsa struct evlist *evlist, 306a76490e4SJiri Olsa int level); 307a76490e4SJiri Olsa int output_field_add(struct perf_hpp_list *list, char *tok); 3085fe7b9b4SJiri Olsa int64_t 3095fe7b9b4SJiri Olsa sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right); 3105fe7b9b4SJiri Olsa int64_t 3115fe7b9b4SJiri Olsa sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right); 3125fe7b9b4SJiri Olsa int64_t 3135fe7b9b4SJiri Olsa sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right); 3146a53da05SArnaldo Carvalho de Melo char *hist_entry__srcline(struct hist_entry *he); 315dd68ada2SJohn Kacur #endif /* __PERF_SORT_H */ 316