1dd68ada2SJohn Kacur #ifndef __PERF_SORT_H 2dd68ada2SJohn Kacur #define __PERF_SORT_H 3dd68ada2SJohn Kacur #include "../builtin.h" 4dd68ada2SJohn Kacur 51eae20c1SArnaldo Carvalho de Melo #include <regex.h> 6dd68ada2SJohn Kacur 7dd68ada2SJohn Kacur #include "color.h" 8dd68ada2SJohn Kacur #include <linux/list.h> 9dd68ada2SJohn Kacur #include "cache.h" 10dd68ada2SJohn Kacur #include <linux/rbtree.h> 11dd68ada2SJohn Kacur #include "symbol.h" 12dd68ada2SJohn Kacur #include "string.h" 13dd68ada2SJohn Kacur #include "callchain.h" 14dd68ada2SJohn Kacur #include "values.h" 15dd68ada2SJohn Kacur 16dd68ada2SJohn Kacur #include "../perf.h" 17dd68ada2SJohn Kacur #include "debug.h" 18dd68ada2SJohn Kacur #include "header.h" 19dd68ada2SJohn Kacur 204b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 21dd68ada2SJohn Kacur #include "parse-events.h" 2214135663SNamhyung Kim #include "hist.h" 23632a5cabSArnaldo Carvalho de Melo #include "srcline.h" 24e7ff8920SArnaldo Carvalho de Melo 25e7ff8920SArnaldo Carvalho de Melo struct thread; 26dd68ada2SJohn Kacur 27dd68ada2SJohn Kacur extern regex_t parent_regex; 28edb7c60eSArnaldo Carvalho de Melo extern const char *sort_order; 29a7d945bcSNamhyung Kim extern const char *field_order; 30edb7c60eSArnaldo Carvalho de Melo extern const char default_parent_pattern[]; 31edb7c60eSArnaldo Carvalho de Melo extern const char *parent_pattern; 32fa1f4565SArnaldo Carvalho de Melo extern const char *default_sort_order; 33b21484f1SGreg Price extern regex_t ignore_callees_regex; 34b21484f1SGreg Price extern int have_ignore_callees; 3555369fc1SNamhyung Kim extern enum sort_mode sort__mode; 36dd68ada2SJohn Kacur extern struct sort_entry sort_comm; 37dd68ada2SJohn Kacur extern struct sort_entry sort_dso; 38dd68ada2SJohn Kacur extern struct sort_entry sort_sym; 39dd68ada2SJohn Kacur extern struct sort_entry sort_parent; 40a68c2c58SStephane Eranian extern struct sort_entry sort_dso_from; 41a68c2c58SStephane Eranian extern struct sort_entry sort_dso_to; 42a68c2c58SStephane Eranian extern struct sort_entry sort_sym_from; 43a68c2c58SStephane Eranian extern struct sort_entry sort_sym_to; 44f666ac0dSJiri Olsa extern struct sort_entry sort_srcline; 45a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension; 46228f14f2SYunlong Song extern const char default_mem_sort_order[]; 47dd68ada2SJohn Kacur 48b24c28f7SNamhyung Kim struct he_stat { 49b24c28f7SNamhyung Kim u64 period; 50b24c28f7SNamhyung Kim u64 period_sys; 51b24c28f7SNamhyung Kim u64 period_us; 52b24c28f7SNamhyung Kim u64 period_guest_sys; 53b24c28f7SNamhyung Kim u64 period_guest_us; 5405484298SAndi Kleen u64 weight; 55b24c28f7SNamhyung Kim u32 nr_events; 56b24c28f7SNamhyung Kim }; 57b24c28f7SNamhyung Kim 58d890a98cSHari Bathini struct namespace_id { 59d890a98cSHari Bathini u64 dev; 60d890a98cSHari Bathini u64 ino; 61d890a98cSHari Bathini }; 62d890a98cSHari Bathini 6396c47f19SJiri Olsa struct hist_entry_diff { 6496c47f19SJiri Olsa bool computed; 65a0b404f4SNamhyung Kim union { 6696c47f19SJiri Olsa /* PERF_HPP__DELTA */ 6796c47f19SJiri Olsa double period_ratio_delta; 6896c47f19SJiri Olsa 6996c47f19SJiri Olsa /* PERF_HPP__RATIO */ 7096c47f19SJiri Olsa double period_ratio; 7181d5f958SJiri Olsa 7281d5f958SJiri Olsa /* HISTC_WEIGHTED_DIFF */ 7381d5f958SJiri Olsa s64 wdiff; 7496c47f19SJiri Olsa }; 75a0b404f4SNamhyung Kim }; 7696c47f19SJiri Olsa 77f542e767SJiri Olsa struct hist_entry_ops { 78f542e767SJiri Olsa void *(*new)(size_t size); 79f542e767SJiri Olsa void (*free)(void *ptr); 80f542e767SJiri Olsa }; 81f542e767SJiri Olsa 820f0cbf7aSArnaldo Carvalho de Melo /** 830f0cbf7aSArnaldo Carvalho de Melo * struct hist_entry - histogram entry 840f0cbf7aSArnaldo Carvalho de Melo * 850f0cbf7aSArnaldo Carvalho de Melo * @row_offset - offset from the first callchain expanded to appear on screen 860f0cbf7aSArnaldo Carvalho de Melo * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding 870f0cbf7aSArnaldo Carvalho de Melo */ 88dd68ada2SJohn Kacur struct hist_entry { 891980c2ebSArnaldo Carvalho de Melo struct rb_node rb_node_in; 90dd68ada2SJohn Kacur struct rb_node rb_node; 91b821c732SArnaldo Carvalho de Melo union { 92b821c732SArnaldo Carvalho de Melo struct list_head node; 93b821c732SArnaldo Carvalho de Melo struct list_head head; 94b821c732SArnaldo Carvalho de Melo } pairs; 95b24c28f7SNamhyung Kim struct he_stat stat; 96f8be1c8cSNamhyung Kim struct he_stat *stat_acc; 9759fd5306SArnaldo Carvalho de Melo struct map_symbol ms; 98a5e29acaSArnaldo Carvalho de Melo struct thread *thread; 994dfced35SNamhyung Kim struct comm *comm; 100d890a98cSHari Bathini struct namespace_id cgroup_id; 101dd68ada2SJohn Kacur u64 ip; 102475eeab9SAndi Kleen u64 transaction; 1030c4c4debSKan Liang s32 socket; 104f60f3593SArun Sharma s32 cpu; 1057365be55SDon Zickus u8 cpumode; 106aef810ecSNamhyung Kim u8 depth; 1070f0cbf7aSArnaldo Carvalho de Melo 108e0af43d2SJiri Olsa /* We are added by hists__add_dummy_entry. */ 109e0af43d2SJiri Olsa bool dummy; 110aef810ecSNamhyung Kim bool leaf; 111e0af43d2SJiri Olsa 112dd68ada2SJohn Kacur char level; 113a5e29acaSArnaldo Carvalho de Melo u8 filtered; 11429750821SNamhyung Kim union { 11529750821SNamhyung Kim /* 11629750821SNamhyung Kim * Since perf diff only supports the stdio output, TUI 11729750821SNamhyung Kim * fields are only accessed from perf report (or perf 11803440c4eSMasahiro Yamada * top). So make it a union to reduce memory usage. 11929750821SNamhyung Kim */ 12029750821SNamhyung Kim struct hist_entry_diff diff; 12129750821SNamhyung Kim struct /* for TUI */ { 12229750821SNamhyung Kim u16 row_offset; 12329750821SNamhyung Kim u16 nr_rows; 124d8a0f800SNamhyung Kim bool init_have_children; 1253698dab1SNamhyung Kim bool unfolded; 1263698dab1SNamhyung Kim bool has_children; 12779dded87SNamhyung Kim bool has_no_entry; 12829750821SNamhyung Kim }; 12929750821SNamhyung Kim }; 130409a8be6SArnaldo Carvalho de Melo char *srcline; 13131191a85SAndi Kleen char *srcfile; 1320d3eb0b7SJin Yao struct inline_node *inline_node; 133439d473bSArnaldo Carvalho de Melo struct symbol *parent; 134b5387528SRoberto Agostino Vitillo struct branch_info *branch_info; 135ae359f19SJiri Olsa struct hists *hists; 13698a3b32cSStephane Eranian struct mem_info *mem_info; 13772392834SNamhyung Kim void *raw_data; 13872392834SNamhyung Kim u32 raw_size; 13960517d28SNamhyung Kim void *trace_output; 1401b2dbbf4SNamhyung Kim struct perf_hpp_list *hpp_list; 141aef810ecSNamhyung Kim struct hist_entry *parent_he; 142f542e767SJiri Olsa struct hist_entry_ops *ops; 143aef810ecSNamhyung Kim union { 144aef810ecSNamhyung Kim /* this is for hierarchical entry structure */ 145aef810ecSNamhyung Kim struct { 146aef810ecSNamhyung Kim struct rb_root hroot_in; 147aef810ecSNamhyung Kim struct rb_root hroot_out; 148aef810ecSNamhyung Kim }; /* non-leaf entries */ 149aef810ecSNamhyung Kim struct rb_root sorted_chain; /* leaf entry has callchains */ 150aef810ecSNamhyung Kim }; 15198a3b32cSStephane Eranian struct callchain_root callchain[0]; /* must be last member */ 15286a9eee0SArnaldo Carvalho de Melo }; 153dd68ada2SJohn Kacur 154b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he) 155b821c732SArnaldo Carvalho de Melo { 156b821c732SArnaldo Carvalho de Melo return !list_empty(&he->pairs.node); 157b821c732SArnaldo Carvalho de Melo } 158b821c732SArnaldo Carvalho de Melo 159b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he) 160b821c732SArnaldo Carvalho de Melo { 161b821c732SArnaldo Carvalho de Melo if (hist_entry__has_pairs(he)) 162b821c732SArnaldo Carvalho de Melo return list_entry(he->pairs.node.next, struct hist_entry, pairs.node); 163b821c732SArnaldo Carvalho de Melo return NULL; 164b821c732SArnaldo Carvalho de Melo } 165b821c732SArnaldo Carvalho de Melo 1664d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair, 1674d23322aSJiri Olsa struct hist_entry *he) 168b821c732SArnaldo Carvalho de Melo { 1694d23322aSJiri Olsa list_add_tail(&pair->pairs.node, &he->pairs.head); 170b821c732SArnaldo Carvalho de Melo } 171b821c732SArnaldo Carvalho de Melo 17214135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he) 17314135663SNamhyung Kim { 17414135663SNamhyung Kim u64 period = he->stat.period; 17514135663SNamhyung Kim u64 total_period = hists__total_period(he->hists); 17614135663SNamhyung Kim 17714135663SNamhyung Kim if (unlikely(total_period == 0)) 17814135663SNamhyung Kim return 0; 17914135663SNamhyung Kim 18014135663SNamhyung Kim if (symbol_conf.cumulate_callchain) 18114135663SNamhyung Kim period = he->stat_acc->period; 18214135663SNamhyung Kim 18314135663SNamhyung Kim return period * 100.0 / total_period; 18414135663SNamhyung Kim } 18514135663SNamhyung Kim 186e95cf700SJiri Olsa static inline u64 cl_address(u64 address) 187e95cf700SJiri Olsa { 188e95cf700SJiri Olsa /* return the cacheline of the address */ 189e95cf700SJiri Olsa return (address & ~(cacheline_size - 1)); 190e95cf700SJiri Olsa } 19114135663SNamhyung Kim 192d3927110SJiri Olsa static inline u64 cl_offset(u64 address) 193d3927110SJiri Olsa { 194d3927110SJiri Olsa /* return the cacheline of the address */ 195d3927110SJiri Olsa return (address & (cacheline_size - 1)); 196d3927110SJiri Olsa } 197d3927110SJiri Olsa 19855369fc1SNamhyung Kim enum sort_mode { 19955369fc1SNamhyung Kim SORT_MODE__NORMAL, 20055369fc1SNamhyung Kim SORT_MODE__BRANCH, 20155369fc1SNamhyung Kim SORT_MODE__MEMORY, 202512ae1bdSNamhyung Kim SORT_MODE__TOP, 203512ae1bdSNamhyung Kim SORT_MODE__DIFF, 204d49dadeaSNamhyung Kim SORT_MODE__TRACEPOINT, 20555369fc1SNamhyung Kim }; 20655369fc1SNamhyung Kim 207a4fb581bSFrederic Weisbecker enum sort_type { 208fc5871edSNamhyung Kim /* common sort keys */ 209a4fb581bSFrederic Weisbecker SORT_PID, 210a4fb581bSFrederic Weisbecker SORT_COMM, 211a4fb581bSFrederic Weisbecker SORT_DSO, 212a4fb581bSFrederic Weisbecker SORT_SYM, 213f60f3593SArun Sharma SORT_PARENT, 214f60f3593SArun Sharma SORT_CPU, 2152e7ea3abSKan Liang SORT_SOCKET, 216fc5871edSNamhyung Kim SORT_SRCLINE, 21731191a85SAndi Kleen SORT_SRCFILE, 218f9ea55d0SAndi Kleen SORT_LOCAL_WEIGHT, 219f9ea55d0SAndi Kleen SORT_GLOBAL_WEIGHT, 220475eeab9SAndi Kleen SORT_TRANSACTION, 221a34bb6a0SNamhyung Kim SORT_TRACE, 2227768f8daSCharles Baylis SORT_SYM_SIZE, 223d890a98cSHari Bathini SORT_CGROUP_ID, 224fc5871edSNamhyung Kim 225fc5871edSNamhyung Kim /* branch stack specific sort keys */ 226fc5871edSNamhyung Kim __SORT_BRANCH_STACK, 227fc5871edSNamhyung Kim SORT_DSO_FROM = __SORT_BRANCH_STACK, 228b5387528SRoberto Agostino Vitillo SORT_DSO_TO, 229b5387528SRoberto Agostino Vitillo SORT_SYM_FROM, 230b5387528SRoberto Agostino Vitillo SORT_SYM_TO, 231b5387528SRoberto Agostino Vitillo SORT_MISPREDICT, 232f5d05bceSAndi Kleen SORT_ABORT, 233f5d05bceSAndi Kleen SORT_IN_TX, 2340e332f03SAndi Kleen SORT_CYCLES, 235508be0dfSAndi Kleen SORT_SRCLINE_FROM, 236508be0dfSAndi Kleen SORT_SRCLINE_TO, 237afab87b9SNamhyung Kim 238afab87b9SNamhyung Kim /* memory mode specific sort keys */ 239afab87b9SNamhyung Kim __SORT_MEMORY_MODE, 240f9ea55d0SAndi Kleen SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE, 241afab87b9SNamhyung Kim SORT_MEM_DADDR_DSO, 242afab87b9SNamhyung Kim SORT_MEM_LOCKED, 243afab87b9SNamhyung Kim SORT_MEM_TLB, 244afab87b9SNamhyung Kim SORT_MEM_LVL, 245afab87b9SNamhyung Kim SORT_MEM_SNOOP, 2469b32ba71SDon Zickus SORT_MEM_DCACHELINE, 24728e6db20SDon Zickus SORT_MEM_IADDR_SYMBOL, 248a4fb581bSFrederic Weisbecker }; 249a4fb581bSFrederic Weisbecker 250dd68ada2SJohn Kacur /* 251dd68ada2SJohn Kacur * configurable sorting bits 252dd68ada2SJohn Kacur */ 253dd68ada2SJohn Kacur 254dd68ada2SJohn Kacur struct sort_entry { 255fcd14984SFrederic Weisbecker const char *se_header; 256dd68ada2SJohn Kacur 257fcd14984SFrederic Weisbecker int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *); 258fcd14984SFrederic Weisbecker int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *); 259202e7a6dSNamhyung Kim int64_t (*se_sort)(struct hist_entry *, struct hist_entry *); 260316c7136SArnaldo Carvalho de Melo int (*se_snprintf)(struct hist_entry *he, char *bf, size_t size, 261a4e3b956SArnaldo Carvalho de Melo unsigned int width); 26254430101SNamhyung Kim int (*se_filter)(struct hist_entry *he, int type, const void *arg); 2638a6c5b26SArnaldo Carvalho de Melo u8 se_width_idx; 264dd68ada2SJohn Kacur }; 265dd68ada2SJohn Kacur 266dd68ada2SJohn Kacur extern struct sort_entry sort_thread; 267dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list; 268dd68ada2SJohn Kacur 26940184c46SNamhyung Kim struct perf_evlist; 27040184c46SNamhyung Kim struct pevent; 27140184c46SNamhyung Kim int setup_sorting(struct perf_evlist *evlist); 272a7d945bcSNamhyung Kim int setup_output_field(void); 2731c89fe9bSNamhyung Kim void reset_output_field(void); 27408e71542SNamhyung Kim void sort__setup_elide(FILE *fp); 275f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide); 276dd68ada2SJohn Kacur 277b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset); 278b21484f1SGreg Price 2792f3f9bcfSJiri Olsa bool is_strict_order(const char *order); 280beeaaeb3SJiri Olsa 281beeaaeb3SJiri Olsa int hpp_dimension__add_output(unsigned col); 282bcf98740SJiri Olsa void reset_dimensions(void); 283a76490e4SJiri Olsa int sort_dimension__add(struct perf_hpp_list *list, const char *tok, 284a76490e4SJiri Olsa struct perf_evlist *evlist, 285a76490e4SJiri Olsa int level); 286a76490e4SJiri Olsa int output_field_add(struct perf_hpp_list *list, char *tok); 2875fe7b9b4SJiri Olsa int64_t 2885fe7b9b4SJiri Olsa sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right); 2895fe7b9b4SJiri Olsa int64_t 2905fe7b9b4SJiri Olsa sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right); 2915fe7b9b4SJiri Olsa int64_t 2925fe7b9b4SJiri Olsa sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right); 29389d9ba8fSJiri Olsa char *hist_entry__get_srcline(struct hist_entry *he); 294dd68ada2SJohn Kacur #endif /* __PERF_SORT_H */ 295