1dd68ada2SJohn Kacur #ifndef __PERF_SORT_H 2dd68ada2SJohn Kacur #define __PERF_SORT_H 3dd68ada2SJohn Kacur #include "../builtin.h" 4dd68ada2SJohn Kacur 5dd68ada2SJohn Kacur #include "util.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 "strlist.h" 15dd68ada2SJohn Kacur #include "values.h" 16dd68ada2SJohn Kacur 17dd68ada2SJohn Kacur #include "../perf.h" 18dd68ada2SJohn Kacur #include "debug.h" 19dd68ada2SJohn Kacur #include "header.h" 20dd68ada2SJohn Kacur 214b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 22dd68ada2SJohn Kacur #include "parse-events.h" 2314135663SNamhyung Kim #include "hist.h" 24dd68ada2SJohn Kacur #include "thread.h" 25dd68ada2SJohn Kacur 26dd68ada2SJohn Kacur extern regex_t parent_regex; 27edb7c60eSArnaldo Carvalho de Melo extern const char *sort_order; 28a7d945bcSNamhyung Kim extern const char *field_order; 29edb7c60eSArnaldo Carvalho de Melo extern const char default_parent_pattern[]; 30edb7c60eSArnaldo Carvalho de Melo extern const char *parent_pattern; 31fa1f4565SArnaldo Carvalho de Melo extern const char *default_sort_order; 32b21484f1SGreg Price extern regex_t ignore_callees_regex; 33b21484f1SGreg Price extern int have_ignore_callees; 3455369fc1SNamhyung Kim extern enum sort_mode sort__mode; 35dd68ada2SJohn Kacur extern struct sort_entry sort_comm; 36dd68ada2SJohn Kacur extern struct sort_entry sort_dso; 37dd68ada2SJohn Kacur extern struct sort_entry sort_sym; 38dd68ada2SJohn Kacur extern struct sort_entry sort_parent; 39a68c2c58SStephane Eranian extern struct sort_entry sort_dso_from; 40a68c2c58SStephane Eranian extern struct sort_entry sort_dso_to; 41a68c2c58SStephane Eranian extern struct sort_entry sort_sym_from; 42a68c2c58SStephane Eranian extern struct sort_entry sort_sym_to; 43f666ac0dSJiri Olsa extern struct sort_entry sort_srcline; 44a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension; 45228f14f2SYunlong Song extern const char default_mem_sort_order[]; 46dd68ada2SJohn Kacur 47b24c28f7SNamhyung Kim struct he_stat { 48b24c28f7SNamhyung Kim u64 period; 49b24c28f7SNamhyung Kim u64 period_sys; 50b24c28f7SNamhyung Kim u64 period_us; 51b24c28f7SNamhyung Kim u64 period_guest_sys; 52b24c28f7SNamhyung Kim u64 period_guest_us; 5305484298SAndi Kleen u64 weight; 54b24c28f7SNamhyung Kim u32 nr_events; 55b24c28f7SNamhyung Kim }; 56b24c28f7SNamhyung Kim 57d890a98cSHari Bathini struct namespace_id { 58d890a98cSHari Bathini u64 dev; 59d890a98cSHari Bathini u64 ino; 60d890a98cSHari Bathini }; 61d890a98cSHari Bathini 6296c47f19SJiri Olsa struct hist_entry_diff { 6396c47f19SJiri Olsa bool computed; 64a0b404f4SNamhyung Kim union { 6596c47f19SJiri Olsa /* PERF_HPP__DELTA */ 6696c47f19SJiri Olsa double period_ratio_delta; 6796c47f19SJiri Olsa 6896c47f19SJiri Olsa /* PERF_HPP__RATIO */ 6996c47f19SJiri Olsa double period_ratio; 7081d5f958SJiri Olsa 7181d5f958SJiri Olsa /* HISTC_WEIGHTED_DIFF */ 7281d5f958SJiri Olsa s64 wdiff; 7396c47f19SJiri Olsa }; 74a0b404f4SNamhyung Kim }; 7596c47f19SJiri Olsa 76f542e767SJiri Olsa struct hist_entry_ops { 77f542e767SJiri Olsa void *(*new)(size_t size); 78f542e767SJiri Olsa void (*free)(void *ptr); 79f542e767SJiri Olsa }; 80f542e767SJiri Olsa 810f0cbf7aSArnaldo Carvalho de Melo /** 820f0cbf7aSArnaldo Carvalho de Melo * struct hist_entry - histogram entry 830f0cbf7aSArnaldo Carvalho de Melo * 840f0cbf7aSArnaldo Carvalho de Melo * @row_offset - offset from the first callchain expanded to appear on screen 850f0cbf7aSArnaldo Carvalho de Melo * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding 860f0cbf7aSArnaldo Carvalho de Melo */ 87dd68ada2SJohn Kacur struct hist_entry { 881980c2ebSArnaldo Carvalho de Melo struct rb_node rb_node_in; 89dd68ada2SJohn Kacur struct rb_node rb_node; 90b821c732SArnaldo Carvalho de Melo union { 91b821c732SArnaldo Carvalho de Melo struct list_head node; 92b821c732SArnaldo Carvalho de Melo struct list_head head; 93b821c732SArnaldo Carvalho de Melo } pairs; 94b24c28f7SNamhyung Kim struct he_stat stat; 95f8be1c8cSNamhyung Kim struct he_stat *stat_acc; 9659fd5306SArnaldo Carvalho de Melo struct map_symbol ms; 97a5e29acaSArnaldo Carvalho de Melo struct thread *thread; 984dfced35SNamhyung Kim struct comm *comm; 99d890a98cSHari Bathini struct namespace_id cgroup_id; 100dd68ada2SJohn Kacur u64 ip; 101475eeab9SAndi Kleen u64 transaction; 1020c4c4debSKan Liang s32 socket; 103f60f3593SArun Sharma s32 cpu; 1047365be55SDon Zickus u8 cpumode; 105aef810ecSNamhyung Kim u8 depth; 1060f0cbf7aSArnaldo Carvalho de Melo 107e0af43d2SJiri Olsa /* We are added by hists__add_dummy_entry. */ 108e0af43d2SJiri Olsa bool dummy; 109aef810ecSNamhyung Kim bool leaf; 110e0af43d2SJiri Olsa 111dd68ada2SJohn Kacur char level; 112a5e29acaSArnaldo Carvalho de Melo u8 filtered; 11329750821SNamhyung Kim union { 11429750821SNamhyung Kim /* 11529750821SNamhyung Kim * Since perf diff only supports the stdio output, TUI 11629750821SNamhyung Kim * fields are only accessed from perf report (or perf 11703440c4eSMasahiro Yamada * top). So make it a union to reduce memory usage. 11829750821SNamhyung Kim */ 11929750821SNamhyung Kim struct hist_entry_diff diff; 12029750821SNamhyung Kim struct /* for TUI */ { 12129750821SNamhyung Kim u16 row_offset; 12229750821SNamhyung Kim u16 nr_rows; 123d8a0f800SNamhyung Kim bool init_have_children; 1243698dab1SNamhyung Kim bool unfolded; 1253698dab1SNamhyung Kim bool has_children; 12679dded87SNamhyung Kim bool has_no_entry; 12729750821SNamhyung Kim }; 12829750821SNamhyung Kim }; 129409a8be6SArnaldo Carvalho de Melo char *srcline; 13031191a85SAndi Kleen char *srcfile; 1310d3eb0b7SJin Yao struct inline_node *inline_node; 132439d473bSArnaldo Carvalho de Melo struct symbol *parent; 133b5387528SRoberto Agostino Vitillo struct branch_info *branch_info; 134ae359f19SJiri Olsa struct hists *hists; 13598a3b32cSStephane Eranian struct mem_info *mem_info; 13672392834SNamhyung Kim void *raw_data; 13772392834SNamhyung Kim u32 raw_size; 13860517d28SNamhyung Kim void *trace_output; 1391b2dbbf4SNamhyung Kim struct perf_hpp_list *hpp_list; 140aef810ecSNamhyung Kim struct hist_entry *parent_he; 141f542e767SJiri Olsa struct hist_entry_ops *ops; 142aef810ecSNamhyung Kim union { 143aef810ecSNamhyung Kim /* this is for hierarchical entry structure */ 144aef810ecSNamhyung Kim struct { 145aef810ecSNamhyung Kim struct rb_root hroot_in; 146aef810ecSNamhyung Kim struct rb_root hroot_out; 147aef810ecSNamhyung Kim }; /* non-leaf entries */ 148aef810ecSNamhyung Kim struct rb_root sorted_chain; /* leaf entry has callchains */ 149aef810ecSNamhyung Kim }; 15098a3b32cSStephane Eranian struct callchain_root callchain[0]; /* must be last member */ 15186a9eee0SArnaldo Carvalho de Melo }; 152dd68ada2SJohn Kacur 153b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he) 154b821c732SArnaldo Carvalho de Melo { 155b821c732SArnaldo Carvalho de Melo return !list_empty(&he->pairs.node); 156b821c732SArnaldo Carvalho de Melo } 157b821c732SArnaldo Carvalho de Melo 158b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he) 159b821c732SArnaldo Carvalho de Melo { 160b821c732SArnaldo Carvalho de Melo if (hist_entry__has_pairs(he)) 161b821c732SArnaldo Carvalho de Melo return list_entry(he->pairs.node.next, struct hist_entry, pairs.node); 162b821c732SArnaldo Carvalho de Melo return NULL; 163b821c732SArnaldo Carvalho de Melo } 164b821c732SArnaldo Carvalho de Melo 1654d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair, 1664d23322aSJiri Olsa struct hist_entry *he) 167b821c732SArnaldo Carvalho de Melo { 1684d23322aSJiri Olsa list_add_tail(&pair->pairs.node, &he->pairs.head); 169b821c732SArnaldo Carvalho de Melo } 170b821c732SArnaldo Carvalho de Melo 17114135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he) 17214135663SNamhyung Kim { 17314135663SNamhyung Kim u64 period = he->stat.period; 17414135663SNamhyung Kim u64 total_period = hists__total_period(he->hists); 17514135663SNamhyung Kim 17614135663SNamhyung Kim if (unlikely(total_period == 0)) 17714135663SNamhyung Kim return 0; 17814135663SNamhyung Kim 17914135663SNamhyung Kim if (symbol_conf.cumulate_callchain) 18014135663SNamhyung Kim period = he->stat_acc->period; 18114135663SNamhyung Kim 18214135663SNamhyung Kim return period * 100.0 / total_period; 18314135663SNamhyung Kim } 18414135663SNamhyung Kim 185e95cf700SJiri Olsa static inline u64 cl_address(u64 address) 186e95cf700SJiri Olsa { 187e95cf700SJiri Olsa /* return the cacheline of the address */ 188e95cf700SJiri Olsa return (address & ~(cacheline_size - 1)); 189e95cf700SJiri Olsa } 19014135663SNamhyung Kim 191d3927110SJiri Olsa static inline u64 cl_offset(u64 address) 192d3927110SJiri Olsa { 193d3927110SJiri Olsa /* return the cacheline of the address */ 194d3927110SJiri Olsa return (address & (cacheline_size - 1)); 195d3927110SJiri Olsa } 196d3927110SJiri Olsa 19755369fc1SNamhyung Kim enum sort_mode { 19855369fc1SNamhyung Kim SORT_MODE__NORMAL, 19955369fc1SNamhyung Kim SORT_MODE__BRANCH, 20055369fc1SNamhyung Kim SORT_MODE__MEMORY, 201512ae1bdSNamhyung Kim SORT_MODE__TOP, 202512ae1bdSNamhyung Kim SORT_MODE__DIFF, 203d49dadeaSNamhyung Kim SORT_MODE__TRACEPOINT, 20455369fc1SNamhyung Kim }; 20555369fc1SNamhyung Kim 206a4fb581bSFrederic Weisbecker enum sort_type { 207fc5871edSNamhyung Kim /* common sort keys */ 208a4fb581bSFrederic Weisbecker SORT_PID, 209a4fb581bSFrederic Weisbecker SORT_COMM, 210a4fb581bSFrederic Weisbecker SORT_DSO, 211a4fb581bSFrederic Weisbecker SORT_SYM, 212f60f3593SArun Sharma SORT_PARENT, 213f60f3593SArun Sharma SORT_CPU, 2142e7ea3abSKan Liang SORT_SOCKET, 215fc5871edSNamhyung Kim SORT_SRCLINE, 21631191a85SAndi Kleen SORT_SRCFILE, 217f9ea55d0SAndi Kleen SORT_LOCAL_WEIGHT, 218f9ea55d0SAndi Kleen SORT_GLOBAL_WEIGHT, 219475eeab9SAndi Kleen SORT_TRANSACTION, 220a34bb6a0SNamhyung Kim SORT_TRACE, 2217768f8daSCharles Baylis SORT_SYM_SIZE, 222d890a98cSHari Bathini SORT_CGROUP_ID, 223fc5871edSNamhyung Kim 224fc5871edSNamhyung Kim /* branch stack specific sort keys */ 225fc5871edSNamhyung Kim __SORT_BRANCH_STACK, 226fc5871edSNamhyung Kim SORT_DSO_FROM = __SORT_BRANCH_STACK, 227b5387528SRoberto Agostino Vitillo SORT_DSO_TO, 228b5387528SRoberto Agostino Vitillo SORT_SYM_FROM, 229b5387528SRoberto Agostino Vitillo SORT_SYM_TO, 230b5387528SRoberto Agostino Vitillo SORT_MISPREDICT, 231f5d05bceSAndi Kleen SORT_ABORT, 232f5d05bceSAndi Kleen SORT_IN_TX, 2330e332f03SAndi Kleen SORT_CYCLES, 234508be0dfSAndi Kleen SORT_SRCLINE_FROM, 235508be0dfSAndi Kleen SORT_SRCLINE_TO, 236afab87b9SNamhyung Kim 237afab87b9SNamhyung Kim /* memory mode specific sort keys */ 238afab87b9SNamhyung Kim __SORT_MEMORY_MODE, 239f9ea55d0SAndi Kleen SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE, 240afab87b9SNamhyung Kim SORT_MEM_DADDR_DSO, 241afab87b9SNamhyung Kim SORT_MEM_LOCKED, 242afab87b9SNamhyung Kim SORT_MEM_TLB, 243afab87b9SNamhyung Kim SORT_MEM_LVL, 244afab87b9SNamhyung Kim SORT_MEM_SNOOP, 2459b32ba71SDon Zickus SORT_MEM_DCACHELINE, 24628e6db20SDon Zickus SORT_MEM_IADDR_SYMBOL, 247a4fb581bSFrederic Weisbecker }; 248a4fb581bSFrederic Weisbecker 249dd68ada2SJohn Kacur /* 250dd68ada2SJohn Kacur * configurable sorting bits 251dd68ada2SJohn Kacur */ 252dd68ada2SJohn Kacur 253dd68ada2SJohn Kacur struct sort_entry { 254fcd14984SFrederic Weisbecker const char *se_header; 255dd68ada2SJohn Kacur 256fcd14984SFrederic Weisbecker int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *); 257fcd14984SFrederic Weisbecker int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *); 258202e7a6dSNamhyung Kim int64_t (*se_sort)(struct hist_entry *, struct hist_entry *); 259316c7136SArnaldo Carvalho de Melo int (*se_snprintf)(struct hist_entry *he, char *bf, size_t size, 260a4e3b956SArnaldo Carvalho de Melo unsigned int width); 26154430101SNamhyung Kim int (*se_filter)(struct hist_entry *he, int type, const void *arg); 2628a6c5b26SArnaldo Carvalho de Melo u8 se_width_idx; 263dd68ada2SJohn Kacur }; 264dd68ada2SJohn Kacur 265dd68ada2SJohn Kacur extern struct sort_entry sort_thread; 266dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list; 267dd68ada2SJohn Kacur 26840184c46SNamhyung Kim struct perf_evlist; 26940184c46SNamhyung Kim struct pevent; 27040184c46SNamhyung Kim int setup_sorting(struct perf_evlist *evlist); 271a7d945bcSNamhyung Kim int setup_output_field(void); 2721c89fe9bSNamhyung Kim void reset_output_field(void); 27308e71542SNamhyung Kim void sort__setup_elide(FILE *fp); 274f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide); 275dd68ada2SJohn Kacur 276b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset); 277b21484f1SGreg Price 2782f3f9bcfSJiri Olsa bool is_strict_order(const char *order); 279beeaaeb3SJiri Olsa 280beeaaeb3SJiri Olsa int hpp_dimension__add_output(unsigned col); 281bcf98740SJiri Olsa void reset_dimensions(void); 282a76490e4SJiri Olsa int sort_dimension__add(struct perf_hpp_list *list, const char *tok, 283a76490e4SJiri Olsa struct perf_evlist *evlist, 284a76490e4SJiri Olsa int level); 285a76490e4SJiri Olsa int output_field_add(struct perf_hpp_list *list, char *tok); 2865fe7b9b4SJiri Olsa int64_t 2875fe7b9b4SJiri Olsa sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right); 2885fe7b9b4SJiri Olsa int64_t 2895fe7b9b4SJiri Olsa sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right); 2905fe7b9b4SJiri Olsa int64_t 2915fe7b9b4SJiri Olsa sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right); 29289d9ba8fSJiri Olsa char *hist_entry__get_srcline(struct hist_entry *he); 293dd68ada2SJohn Kacur #endif /* __PERF_SORT_H */ 294