Don Zickus | 9b32ba7 | 2014-06-01 15:38:29 +0200 | [diff] [blame] | 1 | #include <sys/mman.h> |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2 | #include "sort.h" |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 3 | #include "hist.h" |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 4 | #include "comm.h" |
Namhyung Kim | 08e7154 | 2013-04-03 21:26:19 +0900 | [diff] [blame] | 5 | #include "symbol.h" |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 6 | #include "evsel.h" |
Namhyung Kim | 40184c4 | 2015-12-23 02:07:01 +0900 | [diff] [blame] | 7 | #include "evlist.h" |
| 8 | #include <traceevent/event-parse.h> |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 9 | #include "mem-events.h" |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 10 | |
| 11 | regex_t parent_regex; |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 12 | const char default_parent_pattern[] = "^sys_|^do_page_fault"; |
| 13 | const char *parent_pattern = default_parent_pattern; |
| 14 | const char default_sort_order[] = "comm,dso,symbol"; |
Andi Kleen | 40997d6 | 2015-07-18 08:24:53 -0700 | [diff] [blame] | 15 | const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles"; |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 16 | const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked"; |
| 17 | const char default_top_sort_order[] = "dso,symbol"; |
| 18 | const char default_diff_sort_order[] = "dso,symbol"; |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 19 | const char default_tracepoint_sort_order[] = "trace"; |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 20 | const char *sort_order; |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 21 | const char *field_order; |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 22 | regex_t ignore_callees_regex; |
| 23 | int have_ignore_callees = 0; |
Frederic Weisbecker | af0a6fa | 2009-10-22 23:23:22 +0200 | [diff] [blame] | 24 | int sort__need_collapse = 0; |
| 25 | int sort__has_parent = 0; |
Namhyung Kim | 1af55640 | 2012-09-14 17:35:27 +0900 | [diff] [blame] | 26 | int sort__has_sym = 0; |
Namhyung Kim | 68f6d02 | 2013-12-18 14:21:10 +0900 | [diff] [blame] | 27 | int sort__has_dso = 0; |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 28 | int sort__has_socket = 0; |
Namhyung Kim | cfd92da | 2016-01-21 19:13:24 -0300 | [diff] [blame] | 29 | int sort__has_thread = 0; |
Namhyung Kim | 55369fc | 2013-04-01 20:35:20 +0900 | [diff] [blame] | 30 | enum sort_mode sort__mode = SORT_MODE__NORMAL; |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 31 | |
Arnaldo Carvalho de Melo | 37d9bb5 | 2016-02-12 11:27:51 -0300 | [diff] [blame] | 32 | /* |
| 33 | * Replaces all occurrences of a char used with the: |
| 34 | * |
| 35 | * -t, --field-separator |
| 36 | * |
| 37 | * option, that uses a special separator character and don't pad with spaces, |
| 38 | * replacing all occurances of this separator in symbol names (and other |
| 39 | * output) with a '.' character, that thus it's the only non valid separator. |
| 40 | */ |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 41 | static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 42 | { |
| 43 | int n; |
| 44 | va_list ap; |
| 45 | |
| 46 | va_start(ap, fmt); |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 47 | n = vsnprintf(bf, size, fmt, ap); |
Jiri Olsa | 0ca0c13 | 2012-09-06 17:46:56 +0200 | [diff] [blame] | 48 | if (symbol_conf.field_sep && n > 0) { |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 49 | char *sep = bf; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 50 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 51 | while (1) { |
Jiri Olsa | 0ca0c13 | 2012-09-06 17:46:56 +0200 | [diff] [blame] | 52 | sep = strchr(sep, *symbol_conf.field_sep); |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 53 | if (sep == NULL) |
| 54 | break; |
| 55 | *sep = '.'; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 56 | } |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 57 | } |
| 58 | va_end(ap); |
Anton Blanchard | b832796 | 2012-03-07 11:42:49 +1100 | [diff] [blame] | 59 | |
| 60 | if (n >= (int)size) |
| 61 | return size - 1; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 62 | return n; |
| 63 | } |
| 64 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 65 | static int64_t cmp_null(const void *l, const void *r) |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 66 | { |
| 67 | if (!l && !r) |
| 68 | return 0; |
| 69 | else if (!l) |
| 70 | return -1; |
| 71 | else |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | /* --sort pid */ |
| 76 | |
| 77 | static int64_t |
| 78 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 79 | { |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 80 | return right->thread->tid - left->thread->tid; |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 83 | static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf, |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 84 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 85 | { |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 86 | const char *comm = thread__comm_str(he->thread); |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 87 | |
| 88 | width = max(7U, width) - 6; |
| 89 | return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid, |
| 90 | width, width, comm ?: ""); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 93 | static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg) |
| 94 | { |
| 95 | const struct thread *th = arg; |
| 96 | |
| 97 | if (type != HIST_FILTER__THREAD) |
| 98 | return -1; |
| 99 | |
| 100 | return th && he->thread != th; |
| 101 | } |
| 102 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 103 | struct sort_entry sort_thread = { |
Namhyung Kim | 8246de8 | 2014-07-31 14:47:35 +0900 | [diff] [blame] | 104 | .se_header = " Pid:Command", |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 105 | .se_cmp = sort__thread_cmp, |
| 106 | .se_snprintf = hist_entry__thread_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 107 | .se_filter = hist_entry__thread_filter, |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 108 | .se_width_idx = HISTC_THREAD, |
| 109 | }; |
| 110 | |
| 111 | /* --sort comm */ |
| 112 | |
| 113 | static int64_t |
| 114 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 115 | { |
Frederic Weisbecker | fedd63d | 2013-09-11 17:18:09 +0200 | [diff] [blame] | 116 | /* Compare the addr that should be unique among comm */ |
Jiri Olsa | 2f15bd8 | 2015-05-15 17:54:28 +0200 | [diff] [blame] | 117 | return strcmp(comm__str(right->comm), comm__str(left->comm)); |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int64_t |
| 121 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) |
| 122 | { |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 123 | /* Compare the addr that should be unique among comm */ |
Jiri Olsa | 2f15bd8 | 2015-05-15 17:54:28 +0200 | [diff] [blame] | 124 | return strcmp(comm__str(right->comm), comm__str(left->comm)); |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 127 | static int64_t |
| 128 | sort__comm_sort(struct hist_entry *left, struct hist_entry *right) |
| 129 | { |
| 130 | return strcmp(comm__str(right->comm), comm__str(left->comm)); |
| 131 | } |
| 132 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 133 | static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf, |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 134 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 135 | { |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 136 | return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm)); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 139 | struct sort_entry sort_comm = { |
| 140 | .se_header = "Command", |
| 141 | .se_cmp = sort__comm_cmp, |
| 142 | .se_collapse = sort__comm_collapse, |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 143 | .se_sort = sort__comm_sort, |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 144 | .se_snprintf = hist_entry__comm_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 145 | .se_filter = hist_entry__thread_filter, |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 146 | .se_width_idx = HISTC_COMM, |
| 147 | }; |
| 148 | |
| 149 | /* --sort dso */ |
| 150 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 151 | static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 152 | { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 153 | struct dso *dso_l = map_l ? map_l->dso : NULL; |
| 154 | struct dso *dso_r = map_r ? map_r->dso : NULL; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 155 | const char *dso_name_l, *dso_name_r; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 156 | |
| 157 | if (!dso_l || !dso_r) |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 158 | return cmp_null(dso_r, dso_l); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 159 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 160 | if (verbose) { |
| 161 | dso_name_l = dso_l->long_name; |
| 162 | dso_name_r = dso_r->long_name; |
| 163 | } else { |
| 164 | dso_name_l = dso_l->short_name; |
| 165 | dso_name_r = dso_r->short_name; |
| 166 | } |
| 167 | |
| 168 | return strcmp(dso_name_l, dso_name_r); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 171 | static int64_t |
| 172 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 173 | { |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 174 | return _sort__dso_cmp(right->ms.map, left->ms.map); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 177 | static int _hist_entry__dso_snprintf(struct map *map, char *bf, |
| 178 | size_t size, unsigned int width) |
| 179 | { |
| 180 | if (map && map->dso) { |
| 181 | const char *dso_name = !verbose ? map->dso->short_name : |
| 182 | map->dso->long_name; |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 183 | return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 184 | } |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 185 | |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 186 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]"); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 189 | static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 190 | size_t size, unsigned int width) |
| 191 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 192 | return _hist_entry__dso_snprintf(he->ms.map, bf, size, width); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 195 | static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg) |
| 196 | { |
| 197 | const struct dso *dso = arg; |
| 198 | |
| 199 | if (type != HIST_FILTER__DSO) |
| 200 | return -1; |
| 201 | |
| 202 | return dso && (!he->ms.map || he->ms.map->dso != dso); |
| 203 | } |
| 204 | |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 205 | struct sort_entry sort_dso = { |
| 206 | .se_header = "Shared Object", |
| 207 | .se_cmp = sort__dso_cmp, |
| 208 | .se_snprintf = hist_entry__dso_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 209 | .se_filter = hist_entry__dso_filter, |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 210 | .se_width_idx = HISTC_DSO, |
| 211 | }; |
| 212 | |
| 213 | /* --sort symbol */ |
| 214 | |
Namhyung Kim | 2037be5 | 2013-12-18 14:21:09 +0900 | [diff] [blame] | 215 | static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip) |
| 216 | { |
| 217 | return (int64_t)(right_ip - left_ip); |
| 218 | } |
| 219 | |
Namhyung Kim | 51f27d1 | 2013-02-06 14:57:15 +0900 | [diff] [blame] | 220 | static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r) |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 221 | { |
| 222 | if (!sym_l || !sym_r) |
| 223 | return cmp_null(sym_l, sym_r); |
| 224 | |
| 225 | if (sym_l == sym_r) |
| 226 | return 0; |
| 227 | |
Yannick Brosseau | c05676c | 2015-06-17 16:41:10 -0700 | [diff] [blame] | 228 | if (sym_l->start != sym_r->start) |
| 229 | return (int64_t)(sym_r->start - sym_l->start); |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 230 | |
Yannick Brosseau | c05676c | 2015-06-17 16:41:10 -0700 | [diff] [blame] | 231 | return (int64_t)(sym_r->end - sym_l->end); |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int64_t |
| 235 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) |
| 236 | { |
Namhyung Kim | 09600e0 | 2013-10-15 11:01:56 +0900 | [diff] [blame] | 237 | int64_t ret; |
| 238 | |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 239 | if (!left->ms.sym && !right->ms.sym) |
Namhyung Kim | 2037be5 | 2013-12-18 14:21:09 +0900 | [diff] [blame] | 240 | return _sort__addr_cmp(left->ip, right->ip); |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 241 | |
Namhyung Kim | 09600e0 | 2013-10-15 11:01:56 +0900 | [diff] [blame] | 242 | /* |
| 243 | * comparing symbol address alone is not enough since it's a |
| 244 | * relative address within a dso. |
| 245 | */ |
Namhyung Kim | 68f6d02 | 2013-12-18 14:21:10 +0900 | [diff] [blame] | 246 | if (!sort__has_dso) { |
| 247 | ret = sort__dso_cmp(left, right); |
| 248 | if (ret != 0) |
| 249 | return ret; |
| 250 | } |
Namhyung Kim | 09600e0 | 2013-10-15 11:01:56 +0900 | [diff] [blame] | 251 | |
Namhyung Kim | 51f27d1 | 2013-02-06 14:57:15 +0900 | [diff] [blame] | 252 | return _sort__sym_cmp(left->ms.sym, right->ms.sym); |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 255 | static int64_t |
| 256 | sort__sym_sort(struct hist_entry *left, struct hist_entry *right) |
| 257 | { |
| 258 | if (!left->ms.sym || !right->ms.sym) |
| 259 | return cmp_null(left->ms.sym, right->ms.sym); |
| 260 | |
| 261 | return strcmp(right->ms.sym->name, left->ms.sym->name); |
| 262 | } |
| 263 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 264 | static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym, |
| 265 | u64 ip, char level, char *bf, size_t size, |
Namhyung Kim | 4335552 | 2012-12-27 18:11:39 +0900 | [diff] [blame] | 266 | unsigned int width) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 267 | { |
| 268 | size_t ret = 0; |
| 269 | |
| 270 | if (verbose) { |
| 271 | char o = map ? dso__symtab_origin(map->dso) : '!'; |
| 272 | ret += repsep_snprintf(bf, size, "%-#*llx %c ", |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 273 | BITS_PER_LONG / 4 + 2, ip, o); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 277 | if (sym && map) { |
| 278 | if (map->type == MAP__VARIABLE) { |
| 279 | ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name); |
| 280 | ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx", |
Namhyung Kim | 6266774 | 2013-01-24 16:10:42 +0100 | [diff] [blame] | 281 | ip - map->unmap_ip(map, sym->start)); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 282 | } else { |
Arnaldo Carvalho de Melo | 89fee70 | 2016-02-11 17:14:13 -0300 | [diff] [blame] | 283 | ret += repsep_snprintf(bf + ret, size - ret, "%.*s", |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 284 | width - ret, |
| 285 | sym->name); |
| 286 | } |
| 287 | } else { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 288 | size_t len = BITS_PER_LONG / 4; |
| 289 | ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", |
| 290 | len, ip); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Arnaldo Carvalho de Melo | 89fee70 | 2016-02-11 17:14:13 -0300 | [diff] [blame] | 293 | return ret; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 296 | static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, |
Namhyung Kim | 4335552 | 2012-12-27 18:11:39 +0900 | [diff] [blame] | 297 | size_t size, unsigned int width) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 298 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 299 | return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip, |
| 300 | he->level, bf, size, width); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 301 | } |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 302 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 303 | static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg) |
| 304 | { |
| 305 | const char *sym = arg; |
| 306 | |
| 307 | if (type != HIST_FILTER__SYMBOL) |
| 308 | return -1; |
| 309 | |
| 310 | return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym)); |
| 311 | } |
| 312 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 313 | struct sort_entry sort_sym = { |
| 314 | .se_header = "Symbol", |
| 315 | .se_cmp = sort__sym_cmp, |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 316 | .se_sort = sort__sym_sort, |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 317 | .se_snprintf = hist_entry__sym_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 318 | .se_filter = hist_entry__sym_filter, |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 319 | .se_width_idx = HISTC_SYMBOL, |
| 320 | }; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 321 | |
Arnaldo Carvalho de Melo | 409a8be | 2012-05-30 10:33:24 -0300 | [diff] [blame] | 322 | /* --sort srcline */ |
| 323 | |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 324 | static char *hist_entry__get_srcline(struct hist_entry *he) |
| 325 | { |
| 326 | struct map *map = he->ms.map; |
| 327 | |
| 328 | if (!map) |
| 329 | return SRCLINE_UNKNOWN; |
| 330 | |
| 331 | return get_srcline(map->dso, map__rip_2objdump(map, he->ip), |
| 332 | he->ms.sym, true); |
| 333 | } |
| 334 | |
Arnaldo Carvalho de Melo | 409a8be | 2012-05-30 10:33:24 -0300 | [diff] [blame] | 335 | static int64_t |
| 336 | sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right) |
| 337 | { |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 338 | if (!left->srcline) |
| 339 | left->srcline = hist_entry__get_srcline(left); |
| 340 | if (!right->srcline) |
| 341 | right->srcline = hist_entry__get_srcline(right); |
| 342 | |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 343 | return strcmp(right->srcline, left->srcline); |
Arnaldo Carvalho de Melo | 409a8be | 2012-05-30 10:33:24 -0300 | [diff] [blame] | 344 | } |
| 345 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 346 | static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf, |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 347 | size_t size, unsigned int width) |
Arnaldo Carvalho de Melo | 409a8be | 2012-05-30 10:33:24 -0300 | [diff] [blame] | 348 | { |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 349 | if (!he->srcline) |
| 350 | he->srcline = hist_entry__get_srcline(he); |
| 351 | |
Namhyung Kim | 2960ed6 | 2016-02-22 09:32:33 +0900 | [diff] [blame] | 352 | return repsep_snprintf(bf, size, "%-.*s", width, he->srcline); |
Arnaldo Carvalho de Melo | 409a8be | 2012-05-30 10:33:24 -0300 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | struct sort_entry sort_srcline = { |
| 356 | .se_header = "Source:Line", |
| 357 | .se_cmp = sort__srcline_cmp, |
| 358 | .se_snprintf = hist_entry__srcline_snprintf, |
| 359 | .se_width_idx = HISTC_SRCLINE, |
| 360 | }; |
| 361 | |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 362 | /* --sort srcfile */ |
| 363 | |
| 364 | static char no_srcfile[1]; |
| 365 | |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 366 | static char *hist_entry__get_srcfile(struct hist_entry *e) |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 367 | { |
| 368 | char *sf, *p; |
| 369 | struct map *map = e->ms.map; |
| 370 | |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 371 | if (!map) |
| 372 | return no_srcfile; |
| 373 | |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 374 | sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip), |
| 375 | e->ms.sym, false, true); |
Andi Kleen | 76b1065 | 2015-08-11 06:36:55 -0700 | [diff] [blame] | 376 | if (!strcmp(sf, SRCLINE_UNKNOWN)) |
| 377 | return no_srcfile; |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 378 | p = strchr(sf, ':'); |
| 379 | if (p && *sf) { |
| 380 | *p = 0; |
| 381 | return sf; |
| 382 | } |
| 383 | free(sf); |
| 384 | return no_srcfile; |
| 385 | } |
| 386 | |
| 387 | static int64_t |
| 388 | sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right) |
| 389 | { |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 390 | if (!left->srcfile) |
| 391 | left->srcfile = hist_entry__get_srcfile(left); |
| 392 | if (!right->srcfile) |
| 393 | right->srcfile = hist_entry__get_srcfile(right); |
| 394 | |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 395 | return strcmp(right->srcfile, left->srcfile); |
| 396 | } |
| 397 | |
| 398 | static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf, |
| 399 | size_t size, unsigned int width) |
| 400 | { |
Namhyung Kim | cecaec6 | 2016-02-22 09:31:51 +0900 | [diff] [blame] | 401 | if (!he->srcfile) |
| 402 | he->srcfile = hist_entry__get_srcfile(he); |
| 403 | |
Namhyung Kim | 2960ed6 | 2016-02-22 09:32:33 +0900 | [diff] [blame] | 404 | return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile); |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | struct sort_entry sort_srcfile = { |
| 408 | .se_header = "Source File", |
| 409 | .se_cmp = sort__srcfile_cmp, |
| 410 | .se_snprintf = hist_entry__srcfile_snprintf, |
| 411 | .se_width_idx = HISTC_SRCFILE, |
| 412 | }; |
| 413 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 414 | /* --sort parent */ |
| 415 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 416 | static int64_t |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 417 | sort__parent_cmp(struct hist_entry *left, struct hist_entry *right) |
| 418 | { |
| 419 | struct symbol *sym_l = left->parent; |
| 420 | struct symbol *sym_r = right->parent; |
| 421 | |
| 422 | if (!sym_l || !sym_r) |
| 423 | return cmp_null(sym_l, sym_r); |
| 424 | |
Namhyung Kim | 202e7a6 | 2014-03-04 11:01:41 +0900 | [diff] [blame] | 425 | return strcmp(sym_r->name, sym_l->name); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 428 | static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf, |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 429 | size_t size, unsigned int width) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 430 | { |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 431 | return repsep_snprintf(bf, size, "%-*.*s", width, width, |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 432 | he->parent ? he->parent->name : "[other]"); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 435 | struct sort_entry sort_parent = { |
| 436 | .se_header = "Parent symbol", |
| 437 | .se_cmp = sort__parent_cmp, |
| 438 | .se_snprintf = hist_entry__parent_snprintf, |
| 439 | .se_width_idx = HISTC_PARENT, |
| 440 | }; |
| 441 | |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 442 | /* --sort cpu */ |
| 443 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 444 | static int64_t |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 445 | sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right) |
| 446 | { |
| 447 | return right->cpu - left->cpu; |
| 448 | } |
| 449 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 450 | static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf, |
| 451 | size_t size, unsigned int width) |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 452 | { |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 453 | return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu); |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 454 | } |
| 455 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 456 | struct sort_entry sort_cpu = { |
| 457 | .se_header = "CPU", |
| 458 | .se_cmp = sort__cpu_cmp, |
| 459 | .se_snprintf = hist_entry__cpu_snprintf, |
| 460 | .se_width_idx = HISTC_CPU, |
| 461 | }; |
| 462 | |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 463 | /* --sort socket */ |
| 464 | |
| 465 | static int64_t |
| 466 | sort__socket_cmp(struct hist_entry *left, struct hist_entry *right) |
| 467 | { |
| 468 | return right->socket - left->socket; |
| 469 | } |
| 470 | |
| 471 | static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf, |
| 472 | size_t size, unsigned int width) |
| 473 | { |
| 474 | return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket); |
| 475 | } |
| 476 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 477 | static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg) |
| 478 | { |
| 479 | int sk = *(const int *)arg; |
| 480 | |
| 481 | if (type != HIST_FILTER__SOCKET) |
| 482 | return -1; |
| 483 | |
| 484 | return sk >= 0 && he->socket != sk; |
| 485 | } |
| 486 | |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 487 | struct sort_entry sort_socket = { |
| 488 | .se_header = "Socket", |
| 489 | .se_cmp = sort__socket_cmp, |
| 490 | .se_snprintf = hist_entry__socket_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 491 | .se_filter = hist_entry__socket_filter, |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 492 | .se_width_idx = HISTC_SOCKET, |
| 493 | }; |
| 494 | |
Namhyung Kim | a34bb6a0 | 2015-12-23 02:07:04 +0900 | [diff] [blame] | 495 | /* --sort trace */ |
| 496 | |
| 497 | static char *get_trace_output(struct hist_entry *he) |
| 498 | { |
| 499 | struct trace_seq seq; |
| 500 | struct perf_evsel *evsel; |
| 501 | struct pevent_record rec = { |
| 502 | .data = he->raw_data, |
| 503 | .size = he->raw_size, |
| 504 | }; |
| 505 | |
| 506 | evsel = hists_to_evsel(he->hists); |
| 507 | |
| 508 | trace_seq_init(&seq); |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 509 | if (symbol_conf.raw_trace) { |
| 510 | pevent_print_fields(&seq, he->raw_data, he->raw_size, |
| 511 | evsel->tp_format); |
| 512 | } else { |
| 513 | pevent_event_info(&seq, evsel->tp_format, &rec); |
| 514 | } |
Namhyung Kim | a34bb6a0 | 2015-12-23 02:07:04 +0900 | [diff] [blame] | 515 | return seq.buffer; |
| 516 | } |
| 517 | |
| 518 | static int64_t |
| 519 | sort__trace_cmp(struct hist_entry *left, struct hist_entry *right) |
| 520 | { |
| 521 | struct perf_evsel *evsel; |
| 522 | |
| 523 | evsel = hists_to_evsel(left->hists); |
| 524 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) |
| 525 | return 0; |
| 526 | |
| 527 | if (left->trace_output == NULL) |
| 528 | left->trace_output = get_trace_output(left); |
| 529 | if (right->trace_output == NULL) |
| 530 | right->trace_output = get_trace_output(right); |
| 531 | |
Namhyung Kim | a34bb6a0 | 2015-12-23 02:07:04 +0900 | [diff] [blame] | 532 | return strcmp(right->trace_output, left->trace_output); |
| 533 | } |
| 534 | |
| 535 | static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf, |
| 536 | size_t size, unsigned int width) |
| 537 | { |
| 538 | struct perf_evsel *evsel; |
| 539 | |
| 540 | evsel = hists_to_evsel(he->hists); |
| 541 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) |
Namhyung Kim | 2960ed6 | 2016-02-22 09:32:33 +0900 | [diff] [blame] | 542 | return scnprintf(bf, size, "%-.*s", width, "N/A"); |
Namhyung Kim | a34bb6a0 | 2015-12-23 02:07:04 +0900 | [diff] [blame] | 543 | |
| 544 | if (he->trace_output == NULL) |
| 545 | he->trace_output = get_trace_output(he); |
Namhyung Kim | 2960ed6 | 2016-02-22 09:32:33 +0900 | [diff] [blame] | 546 | return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output); |
Namhyung Kim | a34bb6a0 | 2015-12-23 02:07:04 +0900 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | struct sort_entry sort_trace = { |
| 550 | .se_header = "Trace output", |
| 551 | .se_cmp = sort__trace_cmp, |
| 552 | .se_snprintf = hist_entry__trace_snprintf, |
| 553 | .se_width_idx = HISTC_TRACE, |
| 554 | }; |
| 555 | |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 556 | /* sort keys for branch stacks */ |
| 557 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 558 | static int64_t |
| 559 | sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right) |
| 560 | { |
Jiri Olsa | 288a4b9 | 2014-10-16 16:07:07 +0200 | [diff] [blame] | 561 | if (!left->branch_info || !right->branch_info) |
| 562 | return cmp_null(left->branch_info, right->branch_info); |
| 563 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 564 | return _sort__dso_cmp(left->branch_info->from.map, |
| 565 | right->branch_info->from.map); |
| 566 | } |
| 567 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 568 | static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 569 | size_t size, unsigned int width) |
| 570 | { |
Jiri Olsa | 288a4b9 | 2014-10-16 16:07:07 +0200 | [diff] [blame] | 571 | if (he->branch_info) |
| 572 | return _hist_entry__dso_snprintf(he->branch_info->from.map, |
| 573 | bf, size, width); |
| 574 | else |
| 575 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 578 | static int hist_entry__dso_from_filter(struct hist_entry *he, int type, |
| 579 | const void *arg) |
| 580 | { |
| 581 | const struct dso *dso = arg; |
| 582 | |
| 583 | if (type != HIST_FILTER__DSO) |
| 584 | return -1; |
| 585 | |
| 586 | return dso && (!he->branch_info || !he->branch_info->from.map || |
| 587 | he->branch_info->from.map->dso != dso); |
| 588 | } |
| 589 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 590 | static int64_t |
| 591 | sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right) |
| 592 | { |
Jiri Olsa | 8b62fa5 | 2014-10-16 16:07:06 +0200 | [diff] [blame] | 593 | if (!left->branch_info || !right->branch_info) |
| 594 | return cmp_null(left->branch_info, right->branch_info); |
| 595 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 596 | return _sort__dso_cmp(left->branch_info->to.map, |
| 597 | right->branch_info->to.map); |
| 598 | } |
| 599 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 600 | static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 601 | size_t size, unsigned int width) |
| 602 | { |
Jiri Olsa | 8b62fa5 | 2014-10-16 16:07:06 +0200 | [diff] [blame] | 603 | if (he->branch_info) |
| 604 | return _hist_entry__dso_snprintf(he->branch_info->to.map, |
| 605 | bf, size, width); |
| 606 | else |
| 607 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 610 | static int hist_entry__dso_to_filter(struct hist_entry *he, int type, |
| 611 | const void *arg) |
| 612 | { |
| 613 | const struct dso *dso = arg; |
| 614 | |
| 615 | if (type != HIST_FILTER__DSO) |
| 616 | return -1; |
| 617 | |
| 618 | return dso && (!he->branch_info || !he->branch_info->to.map || |
| 619 | he->branch_info->to.map->dso != dso); |
| 620 | } |
| 621 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 622 | static int64_t |
| 623 | sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right) |
| 624 | { |
| 625 | struct addr_map_symbol *from_l = &left->branch_info->from; |
| 626 | struct addr_map_symbol *from_r = &right->branch_info->from; |
| 627 | |
Jiri Olsa | 1b9e97a | 2014-10-16 16:07:05 +0200 | [diff] [blame] | 628 | if (!left->branch_info || !right->branch_info) |
| 629 | return cmp_null(left->branch_info, right->branch_info); |
| 630 | |
| 631 | from_l = &left->branch_info->from; |
| 632 | from_r = &right->branch_info->from; |
| 633 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 634 | if (!from_l->sym && !from_r->sym) |
Namhyung Kim | 2037be5 | 2013-12-18 14:21:09 +0900 | [diff] [blame] | 635 | return _sort__addr_cmp(from_l->addr, from_r->addr); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 636 | |
Namhyung Kim | 51f27d1 | 2013-02-06 14:57:15 +0900 | [diff] [blame] | 637 | return _sort__sym_cmp(from_l->sym, from_r->sym); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static int64_t |
| 641 | sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right) |
| 642 | { |
Jiri Olsa | 38cdbd3 | 2014-10-16 16:07:04 +0200 | [diff] [blame] | 643 | struct addr_map_symbol *to_l, *to_r; |
| 644 | |
| 645 | if (!left->branch_info || !right->branch_info) |
| 646 | return cmp_null(left->branch_info, right->branch_info); |
| 647 | |
| 648 | to_l = &left->branch_info->to; |
| 649 | to_r = &right->branch_info->to; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 650 | |
| 651 | if (!to_l->sym && !to_r->sym) |
Namhyung Kim | 2037be5 | 2013-12-18 14:21:09 +0900 | [diff] [blame] | 652 | return _sort__addr_cmp(to_l->addr, to_r->addr); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 653 | |
Namhyung Kim | 51f27d1 | 2013-02-06 14:57:15 +0900 | [diff] [blame] | 654 | return _sort__sym_cmp(to_l->sym, to_r->sym); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 655 | } |
| 656 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 657 | static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf, |
Namhyung Kim | 4335552 | 2012-12-27 18:11:39 +0900 | [diff] [blame] | 658 | size_t size, unsigned int width) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 659 | { |
Jiri Olsa | 1b9e97a | 2014-10-16 16:07:05 +0200 | [diff] [blame] | 660 | if (he->branch_info) { |
| 661 | struct addr_map_symbol *from = &he->branch_info->from; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 662 | |
Jiri Olsa | 1b9e97a | 2014-10-16 16:07:05 +0200 | [diff] [blame] | 663 | return _hist_entry__sym_snprintf(from->map, from->sym, from->addr, |
| 664 | he->level, bf, size, width); |
| 665 | } |
| 666 | |
| 667 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 668 | } |
| 669 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 670 | static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf, |
Namhyung Kim | 4335552 | 2012-12-27 18:11:39 +0900 | [diff] [blame] | 671 | size_t size, unsigned int width) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 672 | { |
Jiri Olsa | 38cdbd3 | 2014-10-16 16:07:04 +0200 | [diff] [blame] | 673 | if (he->branch_info) { |
| 674 | struct addr_map_symbol *to = &he->branch_info->to; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 675 | |
Jiri Olsa | 38cdbd3 | 2014-10-16 16:07:04 +0200 | [diff] [blame] | 676 | return _hist_entry__sym_snprintf(to->map, to->sym, to->addr, |
| 677 | he->level, bf, size, width); |
| 678 | } |
| 679 | |
| 680 | return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A"); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 683 | static int hist_entry__sym_from_filter(struct hist_entry *he, int type, |
| 684 | const void *arg) |
| 685 | { |
| 686 | const char *sym = arg; |
| 687 | |
| 688 | if (type != HIST_FILTER__SYMBOL) |
| 689 | return -1; |
| 690 | |
| 691 | return sym && !(he->branch_info && he->branch_info->from.sym && |
| 692 | strstr(he->branch_info->from.sym->name, sym)); |
| 693 | } |
| 694 | |
| 695 | static int hist_entry__sym_to_filter(struct hist_entry *he, int type, |
| 696 | const void *arg) |
| 697 | { |
| 698 | const char *sym = arg; |
| 699 | |
| 700 | if (type != HIST_FILTER__SYMBOL) |
| 701 | return -1; |
| 702 | |
| 703 | return sym && !(he->branch_info && he->branch_info->to.sym && |
| 704 | strstr(he->branch_info->to.sym->name, sym)); |
| 705 | } |
| 706 | |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 707 | struct sort_entry sort_dso_from = { |
| 708 | .se_header = "Source Shared Object", |
| 709 | .se_cmp = sort__dso_from_cmp, |
| 710 | .se_snprintf = hist_entry__dso_from_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 711 | .se_filter = hist_entry__dso_from_filter, |
Namhyung Kim | 14d1ac7 | 2012-12-27 18:11:38 +0900 | [diff] [blame] | 712 | .se_width_idx = HISTC_DSO_FROM, |
| 713 | }; |
| 714 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 715 | struct sort_entry sort_dso_to = { |
| 716 | .se_header = "Target Shared Object", |
| 717 | .se_cmp = sort__dso_to_cmp, |
| 718 | .se_snprintf = hist_entry__dso_to_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 719 | .se_filter = hist_entry__dso_to_filter, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 720 | .se_width_idx = HISTC_DSO_TO, |
| 721 | }; |
| 722 | |
| 723 | struct sort_entry sort_sym_from = { |
| 724 | .se_header = "Source Symbol", |
| 725 | .se_cmp = sort__sym_from_cmp, |
| 726 | .se_snprintf = hist_entry__sym_from_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 727 | .se_filter = hist_entry__sym_from_filter, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 728 | .se_width_idx = HISTC_SYMBOL_FROM, |
| 729 | }; |
| 730 | |
| 731 | struct sort_entry sort_sym_to = { |
| 732 | .se_header = "Target Symbol", |
| 733 | .se_cmp = sort__sym_to_cmp, |
| 734 | .se_snprintf = hist_entry__sym_to_snprintf, |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 735 | .se_filter = hist_entry__sym_to_filter, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 736 | .se_width_idx = HISTC_SYMBOL_TO, |
| 737 | }; |
| 738 | |
| 739 | static int64_t |
| 740 | sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right) |
| 741 | { |
Jiri Olsa | 428560e | 2014-10-16 16:07:03 +0200 | [diff] [blame] | 742 | unsigned char mp, p; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 743 | |
Jiri Olsa | 428560e | 2014-10-16 16:07:03 +0200 | [diff] [blame] | 744 | if (!left->branch_info || !right->branch_info) |
| 745 | return cmp_null(left->branch_info, right->branch_info); |
| 746 | |
| 747 | mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred; |
| 748 | p = left->branch_info->flags.predicted != right->branch_info->flags.predicted; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 749 | return mp || p; |
| 750 | } |
| 751 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 752 | static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 753 | size_t size, unsigned int width){ |
| 754 | static const char *out = "N/A"; |
| 755 | |
Jiri Olsa | 428560e | 2014-10-16 16:07:03 +0200 | [diff] [blame] | 756 | if (he->branch_info) { |
| 757 | if (he->branch_info->flags.predicted) |
| 758 | out = "N"; |
| 759 | else if (he->branch_info->flags.mispred) |
| 760 | out = "Y"; |
| 761 | } |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 762 | |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 763 | return repsep_snprintf(bf, size, "%-*.*s", width, width, out); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Andi Kleen | 0e332f0 | 2015-07-18 08:24:46 -0700 | [diff] [blame] | 766 | static int64_t |
| 767 | sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right) |
| 768 | { |
| 769 | return left->branch_info->flags.cycles - |
| 770 | right->branch_info->flags.cycles; |
| 771 | } |
| 772 | |
| 773 | static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf, |
| 774 | size_t size, unsigned int width) |
| 775 | { |
| 776 | if (he->branch_info->flags.cycles == 0) |
| 777 | return repsep_snprintf(bf, size, "%-*s", width, "-"); |
| 778 | return repsep_snprintf(bf, size, "%-*hd", width, |
| 779 | he->branch_info->flags.cycles); |
| 780 | } |
| 781 | |
| 782 | struct sort_entry sort_cycles = { |
| 783 | .se_header = "Basic Block Cycles", |
| 784 | .se_cmp = sort__cycles_cmp, |
| 785 | .se_snprintf = hist_entry__cycles_snprintf, |
| 786 | .se_width_idx = HISTC_CYCLES, |
| 787 | }; |
| 788 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 789 | /* --sort daddr_sym */ |
| 790 | static int64_t |
| 791 | sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right) |
| 792 | { |
| 793 | uint64_t l = 0, r = 0; |
| 794 | |
| 795 | if (left->mem_info) |
| 796 | l = left->mem_info->daddr.addr; |
| 797 | if (right->mem_info) |
| 798 | r = right->mem_info->daddr.addr; |
| 799 | |
| 800 | return (int64_t)(r - l); |
| 801 | } |
| 802 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 803 | static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 804 | size_t size, unsigned int width) |
| 805 | { |
| 806 | uint64_t addr = 0; |
| 807 | struct map *map = NULL; |
| 808 | struct symbol *sym = NULL; |
| 809 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 810 | if (he->mem_info) { |
| 811 | addr = he->mem_info->daddr.addr; |
| 812 | map = he->mem_info->daddr.map; |
| 813 | sym = he->mem_info->daddr.sym; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 814 | } |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 815 | return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 816 | width); |
| 817 | } |
| 818 | |
| 819 | static int64_t |
Don Zickus | 28e6db2 | 2015-10-05 20:06:07 +0200 | [diff] [blame] | 820 | sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right) |
| 821 | { |
| 822 | uint64_t l = 0, r = 0; |
| 823 | |
| 824 | if (left->mem_info) |
| 825 | l = left->mem_info->iaddr.addr; |
| 826 | if (right->mem_info) |
| 827 | r = right->mem_info->iaddr.addr; |
| 828 | |
| 829 | return (int64_t)(r - l); |
| 830 | } |
| 831 | |
| 832 | static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf, |
| 833 | size_t size, unsigned int width) |
| 834 | { |
| 835 | uint64_t addr = 0; |
| 836 | struct map *map = NULL; |
| 837 | struct symbol *sym = NULL; |
| 838 | |
| 839 | if (he->mem_info) { |
| 840 | addr = he->mem_info->iaddr.addr; |
| 841 | map = he->mem_info->iaddr.map; |
| 842 | sym = he->mem_info->iaddr.sym; |
| 843 | } |
| 844 | return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size, |
| 845 | width); |
| 846 | } |
| 847 | |
| 848 | static int64_t |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 849 | sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right) |
| 850 | { |
| 851 | struct map *map_l = NULL; |
| 852 | struct map *map_r = NULL; |
| 853 | |
| 854 | if (left->mem_info) |
| 855 | map_l = left->mem_info->daddr.map; |
| 856 | if (right->mem_info) |
| 857 | map_r = right->mem_info->daddr.map; |
| 858 | |
| 859 | return _sort__dso_cmp(map_l, map_r); |
| 860 | } |
| 861 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 862 | static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 863 | size_t size, unsigned int width) |
| 864 | { |
| 865 | struct map *map = NULL; |
| 866 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 867 | if (he->mem_info) |
| 868 | map = he->mem_info->daddr.map; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 869 | |
| 870 | return _hist_entry__dso_snprintf(map, bf, size, width); |
| 871 | } |
| 872 | |
| 873 | static int64_t |
| 874 | sort__locked_cmp(struct hist_entry *left, struct hist_entry *right) |
| 875 | { |
| 876 | union perf_mem_data_src data_src_l; |
| 877 | union perf_mem_data_src data_src_r; |
| 878 | |
| 879 | if (left->mem_info) |
| 880 | data_src_l = left->mem_info->data_src; |
| 881 | else |
| 882 | data_src_l.mem_lock = PERF_MEM_LOCK_NA; |
| 883 | |
| 884 | if (right->mem_info) |
| 885 | data_src_r = right->mem_info->data_src; |
| 886 | else |
| 887 | data_src_r.mem_lock = PERF_MEM_LOCK_NA; |
| 888 | |
| 889 | return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock); |
| 890 | } |
| 891 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 892 | static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 893 | size_t size, unsigned int width) |
| 894 | { |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 895 | char out[10]; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 896 | |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 897 | perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info); |
Arnaldo Carvalho de Melo | 89fee70 | 2016-02-11 17:14:13 -0300 | [diff] [blame] | 898 | return repsep_snprintf(bf, size, "%.*s", width, out); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static int64_t |
| 902 | sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right) |
| 903 | { |
| 904 | union perf_mem_data_src data_src_l; |
| 905 | union perf_mem_data_src data_src_r; |
| 906 | |
| 907 | if (left->mem_info) |
| 908 | data_src_l = left->mem_info->data_src; |
| 909 | else |
| 910 | data_src_l.mem_dtlb = PERF_MEM_TLB_NA; |
| 911 | |
| 912 | if (right->mem_info) |
| 913 | data_src_r = right->mem_info->data_src; |
| 914 | else |
| 915 | data_src_r.mem_dtlb = PERF_MEM_TLB_NA; |
| 916 | |
| 917 | return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb); |
| 918 | } |
| 919 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 920 | static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 921 | size_t size, unsigned int width) |
| 922 | { |
| 923 | char out[64]; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 924 | |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 925 | perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 926 | return repsep_snprintf(bf, size, "%-*s", width, out); |
| 927 | } |
| 928 | |
| 929 | static int64_t |
| 930 | sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right) |
| 931 | { |
| 932 | union perf_mem_data_src data_src_l; |
| 933 | union perf_mem_data_src data_src_r; |
| 934 | |
| 935 | if (left->mem_info) |
| 936 | data_src_l = left->mem_info->data_src; |
| 937 | else |
| 938 | data_src_l.mem_lvl = PERF_MEM_LVL_NA; |
| 939 | |
| 940 | if (right->mem_info) |
| 941 | data_src_r = right->mem_info->data_src; |
| 942 | else |
| 943 | data_src_r.mem_lvl = PERF_MEM_LVL_NA; |
| 944 | |
| 945 | return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl); |
| 946 | } |
| 947 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 948 | static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 949 | size_t size, unsigned int width) |
| 950 | { |
| 951 | char out[64]; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 952 | |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 953 | perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 954 | return repsep_snprintf(bf, size, "%-*s", width, out); |
| 955 | } |
| 956 | |
| 957 | static int64_t |
| 958 | sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right) |
| 959 | { |
| 960 | union perf_mem_data_src data_src_l; |
| 961 | union perf_mem_data_src data_src_r; |
| 962 | |
| 963 | if (left->mem_info) |
| 964 | data_src_l = left->mem_info->data_src; |
| 965 | else |
| 966 | data_src_l.mem_snoop = PERF_MEM_SNOOP_NA; |
| 967 | |
| 968 | if (right->mem_info) |
| 969 | data_src_r = right->mem_info->data_src; |
| 970 | else |
| 971 | data_src_r.mem_snoop = PERF_MEM_SNOOP_NA; |
| 972 | |
| 973 | return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop); |
| 974 | } |
| 975 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 976 | static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 977 | size_t size, unsigned int width) |
| 978 | { |
| 979 | char out[64]; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 980 | |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 981 | perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 982 | return repsep_snprintf(bf, size, "%-*s", width, out); |
| 983 | } |
| 984 | |
Don Zickus | 9b32ba7 | 2014-06-01 15:38:29 +0200 | [diff] [blame] | 985 | static int64_t |
| 986 | sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right) |
| 987 | { |
| 988 | u64 l, r; |
| 989 | struct map *l_map, *r_map; |
| 990 | |
| 991 | if (!left->mem_info) return -1; |
| 992 | if (!right->mem_info) return 1; |
| 993 | |
| 994 | /* group event types together */ |
| 995 | if (left->cpumode > right->cpumode) return -1; |
| 996 | if (left->cpumode < right->cpumode) return 1; |
| 997 | |
| 998 | l_map = left->mem_info->daddr.map; |
| 999 | r_map = right->mem_info->daddr.map; |
| 1000 | |
| 1001 | /* if both are NULL, jump to sort on al_addr instead */ |
| 1002 | if (!l_map && !r_map) |
| 1003 | goto addr; |
| 1004 | |
| 1005 | if (!l_map) return -1; |
| 1006 | if (!r_map) return 1; |
| 1007 | |
| 1008 | if (l_map->maj > r_map->maj) return -1; |
| 1009 | if (l_map->maj < r_map->maj) return 1; |
| 1010 | |
| 1011 | if (l_map->min > r_map->min) return -1; |
| 1012 | if (l_map->min < r_map->min) return 1; |
| 1013 | |
| 1014 | if (l_map->ino > r_map->ino) return -1; |
| 1015 | if (l_map->ino < r_map->ino) return 1; |
| 1016 | |
| 1017 | if (l_map->ino_generation > r_map->ino_generation) return -1; |
| 1018 | if (l_map->ino_generation < r_map->ino_generation) return 1; |
| 1019 | |
| 1020 | /* |
| 1021 | * Addresses with no major/minor numbers are assumed to be |
| 1022 | * anonymous in userspace. Sort those on pid then address. |
| 1023 | * |
| 1024 | * The kernel and non-zero major/minor mapped areas are |
| 1025 | * assumed to be unity mapped. Sort those on address. |
| 1026 | */ |
| 1027 | |
| 1028 | if ((left->cpumode != PERF_RECORD_MISC_KERNEL) && |
| 1029 | (!(l_map->flags & MAP_SHARED)) && |
| 1030 | !l_map->maj && !l_map->min && !l_map->ino && |
| 1031 | !l_map->ino_generation) { |
| 1032 | /* userspace anonymous */ |
| 1033 | |
| 1034 | if (left->thread->pid_ > right->thread->pid_) return -1; |
| 1035 | if (left->thread->pid_ < right->thread->pid_) return 1; |
| 1036 | } |
| 1037 | |
| 1038 | addr: |
| 1039 | /* al_addr does all the right addr - start + offset calculations */ |
| 1040 | l = cl_address(left->mem_info->daddr.al_addr); |
| 1041 | r = cl_address(right->mem_info->daddr.al_addr); |
| 1042 | |
| 1043 | if (l > r) return -1; |
| 1044 | if (l < r) return 1; |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf, |
| 1050 | size_t size, unsigned int width) |
| 1051 | { |
| 1052 | |
| 1053 | uint64_t addr = 0; |
| 1054 | struct map *map = NULL; |
| 1055 | struct symbol *sym = NULL; |
| 1056 | char level = he->level; |
| 1057 | |
| 1058 | if (he->mem_info) { |
| 1059 | addr = cl_address(he->mem_info->daddr.al_addr); |
| 1060 | map = he->mem_info->daddr.map; |
| 1061 | sym = he->mem_info->daddr.sym; |
| 1062 | |
| 1063 | /* print [s] for shared data mmaps */ |
| 1064 | if ((he->cpumode != PERF_RECORD_MISC_KERNEL) && |
| 1065 | map && (map->type == MAP__VARIABLE) && |
| 1066 | (map->flags & MAP_SHARED) && |
| 1067 | (map->maj || map->min || map->ino || |
| 1068 | map->ino_generation)) |
| 1069 | level = 's'; |
| 1070 | else if (!map) |
| 1071 | level = 'X'; |
| 1072 | } |
| 1073 | return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size, |
| 1074 | width); |
| 1075 | } |
| 1076 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1077 | struct sort_entry sort_mispredict = { |
| 1078 | .se_header = "Branch Mispredicted", |
| 1079 | .se_cmp = sort__mispredict_cmp, |
| 1080 | .se_snprintf = hist_entry__mispredict_snprintf, |
| 1081 | .se_width_idx = HISTC_MISPREDICT, |
| 1082 | }; |
| 1083 | |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 1084 | static u64 he_weight(struct hist_entry *he) |
| 1085 | { |
| 1086 | return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int64_t |
| 1090 | sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right) |
| 1091 | { |
| 1092 | return he_weight(left) - he_weight(right); |
| 1093 | } |
| 1094 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1095 | static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf, |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 1096 | size_t size, unsigned int width) |
| 1097 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1098 | return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he)); |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | struct sort_entry sort_local_weight = { |
| 1102 | .se_header = "Local Weight", |
| 1103 | .se_cmp = sort__local_weight_cmp, |
| 1104 | .se_snprintf = hist_entry__local_weight_snprintf, |
| 1105 | .se_width_idx = HISTC_LOCAL_WEIGHT, |
| 1106 | }; |
| 1107 | |
| 1108 | static int64_t |
| 1109 | sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right) |
| 1110 | { |
| 1111 | return left->stat.weight - right->stat.weight; |
| 1112 | } |
| 1113 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1114 | static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf, |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 1115 | size_t size, unsigned int width) |
| 1116 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1117 | return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight); |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | struct sort_entry sort_global_weight = { |
| 1121 | .se_header = "Weight", |
| 1122 | .se_cmp = sort__global_weight_cmp, |
| 1123 | .se_snprintf = hist_entry__global_weight_snprintf, |
| 1124 | .se_width_idx = HISTC_GLOBAL_WEIGHT, |
| 1125 | }; |
| 1126 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1127 | struct sort_entry sort_mem_daddr_sym = { |
| 1128 | .se_header = "Data Symbol", |
| 1129 | .se_cmp = sort__daddr_cmp, |
| 1130 | .se_snprintf = hist_entry__daddr_snprintf, |
| 1131 | .se_width_idx = HISTC_MEM_DADDR_SYMBOL, |
| 1132 | }; |
| 1133 | |
Don Zickus | 28e6db2 | 2015-10-05 20:06:07 +0200 | [diff] [blame] | 1134 | struct sort_entry sort_mem_iaddr_sym = { |
| 1135 | .se_header = "Code Symbol", |
| 1136 | .se_cmp = sort__iaddr_cmp, |
| 1137 | .se_snprintf = hist_entry__iaddr_snprintf, |
| 1138 | .se_width_idx = HISTC_MEM_IADDR_SYMBOL, |
| 1139 | }; |
| 1140 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1141 | struct sort_entry sort_mem_daddr_dso = { |
| 1142 | .se_header = "Data Object", |
| 1143 | .se_cmp = sort__dso_daddr_cmp, |
| 1144 | .se_snprintf = hist_entry__dso_daddr_snprintf, |
| 1145 | .se_width_idx = HISTC_MEM_DADDR_SYMBOL, |
| 1146 | }; |
| 1147 | |
| 1148 | struct sort_entry sort_mem_locked = { |
| 1149 | .se_header = "Locked", |
| 1150 | .se_cmp = sort__locked_cmp, |
| 1151 | .se_snprintf = hist_entry__locked_snprintf, |
| 1152 | .se_width_idx = HISTC_MEM_LOCKED, |
| 1153 | }; |
| 1154 | |
| 1155 | struct sort_entry sort_mem_tlb = { |
| 1156 | .se_header = "TLB access", |
| 1157 | .se_cmp = sort__tlb_cmp, |
| 1158 | .se_snprintf = hist_entry__tlb_snprintf, |
| 1159 | .se_width_idx = HISTC_MEM_TLB, |
| 1160 | }; |
| 1161 | |
| 1162 | struct sort_entry sort_mem_lvl = { |
| 1163 | .se_header = "Memory access", |
| 1164 | .se_cmp = sort__lvl_cmp, |
| 1165 | .se_snprintf = hist_entry__lvl_snprintf, |
| 1166 | .se_width_idx = HISTC_MEM_LVL, |
| 1167 | }; |
| 1168 | |
| 1169 | struct sort_entry sort_mem_snoop = { |
| 1170 | .se_header = "Snoop", |
| 1171 | .se_cmp = sort__snoop_cmp, |
| 1172 | .se_snprintf = hist_entry__snoop_snprintf, |
| 1173 | .se_width_idx = HISTC_MEM_SNOOP, |
| 1174 | }; |
| 1175 | |
Don Zickus | 9b32ba7 | 2014-06-01 15:38:29 +0200 | [diff] [blame] | 1176 | struct sort_entry sort_mem_dcacheline = { |
| 1177 | .se_header = "Data Cacheline", |
| 1178 | .se_cmp = sort__dcacheline_cmp, |
| 1179 | .se_snprintf = hist_entry__dcacheline_snprintf, |
| 1180 | .se_width_idx = HISTC_MEM_DCACHELINE, |
| 1181 | }; |
| 1182 | |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1183 | static int64_t |
| 1184 | sort__abort_cmp(struct hist_entry *left, struct hist_entry *right) |
| 1185 | { |
Jiri Olsa | 49f4744 | 2014-10-16 16:07:01 +0200 | [diff] [blame] | 1186 | if (!left->branch_info || !right->branch_info) |
| 1187 | return cmp_null(left->branch_info, right->branch_info); |
| 1188 | |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1189 | return left->branch_info->flags.abort != |
| 1190 | right->branch_info->flags.abort; |
| 1191 | } |
| 1192 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1193 | static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf, |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1194 | size_t size, unsigned int width) |
| 1195 | { |
Jiri Olsa | 49f4744 | 2014-10-16 16:07:01 +0200 | [diff] [blame] | 1196 | static const char *out = "N/A"; |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1197 | |
Jiri Olsa | 49f4744 | 2014-10-16 16:07:01 +0200 | [diff] [blame] | 1198 | if (he->branch_info) { |
| 1199 | if (he->branch_info->flags.abort) |
| 1200 | out = "A"; |
| 1201 | else |
| 1202 | out = "."; |
| 1203 | } |
| 1204 | |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1205 | return repsep_snprintf(bf, size, "%-*s", width, out); |
| 1206 | } |
| 1207 | |
| 1208 | struct sort_entry sort_abort = { |
| 1209 | .se_header = "Transaction abort", |
| 1210 | .se_cmp = sort__abort_cmp, |
| 1211 | .se_snprintf = hist_entry__abort_snprintf, |
| 1212 | .se_width_idx = HISTC_ABORT, |
| 1213 | }; |
| 1214 | |
| 1215 | static int64_t |
| 1216 | sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right) |
| 1217 | { |
Jiri Olsa | 0199d24 | 2014-10-16 16:07:02 +0200 | [diff] [blame] | 1218 | if (!left->branch_info || !right->branch_info) |
| 1219 | return cmp_null(left->branch_info, right->branch_info); |
| 1220 | |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1221 | return left->branch_info->flags.in_tx != |
| 1222 | right->branch_info->flags.in_tx; |
| 1223 | } |
| 1224 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1225 | static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf, |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1226 | size_t size, unsigned int width) |
| 1227 | { |
Jiri Olsa | 0199d24 | 2014-10-16 16:07:02 +0200 | [diff] [blame] | 1228 | static const char *out = "N/A"; |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1229 | |
Jiri Olsa | 0199d24 | 2014-10-16 16:07:02 +0200 | [diff] [blame] | 1230 | if (he->branch_info) { |
| 1231 | if (he->branch_info->flags.in_tx) |
| 1232 | out = "T"; |
| 1233 | else |
| 1234 | out = "."; |
| 1235 | } |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1236 | |
| 1237 | return repsep_snprintf(bf, size, "%-*s", width, out); |
| 1238 | } |
| 1239 | |
| 1240 | struct sort_entry sort_in_tx = { |
| 1241 | .se_header = "Branch in transaction", |
| 1242 | .se_cmp = sort__in_tx_cmp, |
| 1243 | .se_snprintf = hist_entry__in_tx_snprintf, |
| 1244 | .se_width_idx = HISTC_IN_TX, |
| 1245 | }; |
| 1246 | |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 1247 | static int64_t |
| 1248 | sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right) |
| 1249 | { |
| 1250 | return left->transaction - right->transaction; |
| 1251 | } |
| 1252 | |
| 1253 | static inline char *add_str(char *p, const char *str) |
| 1254 | { |
| 1255 | strcpy(p, str); |
| 1256 | return p + strlen(str); |
| 1257 | } |
| 1258 | |
| 1259 | static struct txbit { |
| 1260 | unsigned flag; |
| 1261 | const char *name; |
| 1262 | int skip_for_len; |
| 1263 | } txbits[] = { |
| 1264 | { PERF_TXN_ELISION, "EL ", 0 }, |
| 1265 | { PERF_TXN_TRANSACTION, "TX ", 1 }, |
| 1266 | { PERF_TXN_SYNC, "SYNC ", 1 }, |
| 1267 | { PERF_TXN_ASYNC, "ASYNC ", 0 }, |
| 1268 | { PERF_TXN_RETRY, "RETRY ", 0 }, |
| 1269 | { PERF_TXN_CONFLICT, "CON ", 0 }, |
| 1270 | { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 }, |
| 1271 | { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 }, |
| 1272 | { 0, NULL, 0 } |
| 1273 | }; |
| 1274 | |
| 1275 | int hist_entry__transaction_len(void) |
| 1276 | { |
| 1277 | int i; |
| 1278 | int len = 0; |
| 1279 | |
| 1280 | for (i = 0; txbits[i].name; i++) { |
| 1281 | if (!txbits[i].skip_for_len) |
| 1282 | len += strlen(txbits[i].name); |
| 1283 | } |
| 1284 | len += 4; /* :XX<space> */ |
| 1285 | return len; |
| 1286 | } |
| 1287 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1288 | static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf, |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 1289 | size_t size, unsigned int width) |
| 1290 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 1291 | u64 t = he->transaction; |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 1292 | char buf[128]; |
| 1293 | char *p = buf; |
| 1294 | int i; |
| 1295 | |
| 1296 | buf[0] = 0; |
| 1297 | for (i = 0; txbits[i].name; i++) |
| 1298 | if (txbits[i].flag & t) |
| 1299 | p = add_str(p, txbits[i].name); |
| 1300 | if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC))) |
| 1301 | p = add_str(p, "NEITHER "); |
| 1302 | if (t & PERF_TXN_ABORT_MASK) { |
| 1303 | sprintf(p, ":%" PRIx64, |
| 1304 | (t & PERF_TXN_ABORT_MASK) >> |
| 1305 | PERF_TXN_ABORT_SHIFT); |
| 1306 | p += strlen(p); |
| 1307 | } |
| 1308 | |
| 1309 | return repsep_snprintf(bf, size, "%-*s", width, buf); |
| 1310 | } |
| 1311 | |
| 1312 | struct sort_entry sort_transaction = { |
| 1313 | .se_header = "Transaction ", |
| 1314 | .se_cmp = sort__transaction_cmp, |
| 1315 | .se_snprintf = hist_entry__transaction_snprintf, |
| 1316 | .se_width_idx = HISTC_TRANSACTION, |
| 1317 | }; |
| 1318 | |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 1319 | struct sort_dimension { |
| 1320 | const char *name; |
| 1321 | struct sort_entry *entry; |
| 1322 | int taken; |
| 1323 | }; |
| 1324 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1325 | #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) } |
| 1326 | |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 1327 | static struct sort_dimension common_sort_dimensions[] = { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1328 | DIM(SORT_PID, "pid", sort_thread), |
| 1329 | DIM(SORT_COMM, "comm", sort_comm), |
| 1330 | DIM(SORT_DSO, "dso", sort_dso), |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1331 | DIM(SORT_SYM, "symbol", sort_sym), |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1332 | DIM(SORT_PARENT, "parent", sort_parent), |
| 1333 | DIM(SORT_CPU, "cpu", sort_cpu), |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 1334 | DIM(SORT_SOCKET, "socket", sort_socket), |
Arnaldo Carvalho de Melo | 409a8be | 2012-05-30 10:33:24 -0300 | [diff] [blame] | 1335 | DIM(SORT_SRCLINE, "srcline", sort_srcline), |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 1336 | DIM(SORT_SRCFILE, "srcfile", sort_srcfile), |
Andi Kleen | f9ea55d | 2013-07-18 15:58:53 -0700 | [diff] [blame] | 1337 | DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight), |
| 1338 | DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight), |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 1339 | DIM(SORT_TRANSACTION, "transaction", sort_transaction), |
Namhyung Kim | a34bb6a0 | 2015-12-23 02:07:04 +0900 | [diff] [blame] | 1340 | DIM(SORT_TRACE, "trace", sort_trace), |
Frederic Weisbecker | 872a878 | 2011-06-29 03:14:52 +0200 | [diff] [blame] | 1341 | }; |
| 1342 | |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 1343 | #undef DIM |
| 1344 | |
| 1345 | #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) } |
| 1346 | |
| 1347 | static struct sort_dimension bstack_sort_dimensions[] = { |
| 1348 | DIM(SORT_DSO_FROM, "dso_from", sort_dso_from), |
| 1349 | DIM(SORT_DSO_TO, "dso_to", sort_dso_to), |
| 1350 | DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from), |
| 1351 | DIM(SORT_SYM_TO, "symbol_to", sort_sym_to), |
| 1352 | DIM(SORT_MISPREDICT, "mispredict", sort_mispredict), |
Andi Kleen | f5d05bc | 2013-09-20 07:40:41 -0700 | [diff] [blame] | 1353 | DIM(SORT_IN_TX, "in_tx", sort_in_tx), |
| 1354 | DIM(SORT_ABORT, "abort", sort_abort), |
Andi Kleen | 0e332f0 | 2015-07-18 08:24:46 -0700 | [diff] [blame] | 1355 | DIM(SORT_CYCLES, "cycles", sort_cycles), |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 1356 | }; |
| 1357 | |
| 1358 | #undef DIM |
| 1359 | |
Namhyung Kim | afab87b | 2013-04-03 21:26:11 +0900 | [diff] [blame] | 1360 | #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) } |
| 1361 | |
| 1362 | static struct sort_dimension memory_sort_dimensions[] = { |
Namhyung Kim | afab87b | 2013-04-03 21:26:11 +0900 | [diff] [blame] | 1363 | DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym), |
Don Zickus | 28e6db2 | 2015-10-05 20:06:07 +0200 | [diff] [blame] | 1364 | DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym), |
Namhyung Kim | afab87b | 2013-04-03 21:26:11 +0900 | [diff] [blame] | 1365 | DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso), |
| 1366 | DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked), |
| 1367 | DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb), |
| 1368 | DIM(SORT_MEM_LVL, "mem", sort_mem_lvl), |
| 1369 | DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop), |
Don Zickus | 9b32ba7 | 2014-06-01 15:38:29 +0200 | [diff] [blame] | 1370 | DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline), |
Namhyung Kim | afab87b | 2013-04-03 21:26:11 +0900 | [diff] [blame] | 1371 | }; |
| 1372 | |
| 1373 | #undef DIM |
| 1374 | |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 1375 | struct hpp_dimension { |
| 1376 | const char *name; |
| 1377 | struct perf_hpp_fmt *fmt; |
| 1378 | int taken; |
| 1379 | }; |
| 1380 | |
| 1381 | #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], } |
| 1382 | |
| 1383 | static struct hpp_dimension hpp_sort_dimensions[] = { |
| 1384 | DIM(PERF_HPP__OVERHEAD, "overhead"), |
| 1385 | DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"), |
| 1386 | DIM(PERF_HPP__OVERHEAD_US, "overhead_us"), |
| 1387 | DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"), |
| 1388 | DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"), |
Namhyung Kim | 594dcbf | 2013-10-30 16:06:59 +0900 | [diff] [blame] | 1389 | DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"), |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 1390 | DIM(PERF_HPP__SAMPLES, "sample"), |
| 1391 | DIM(PERF_HPP__PERIOD, "period"), |
| 1392 | }; |
| 1393 | |
| 1394 | #undef DIM |
| 1395 | |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1396 | struct hpp_sort_entry { |
| 1397 | struct perf_hpp_fmt hpp; |
| 1398 | struct sort_entry *se; |
| 1399 | }; |
| 1400 | |
Namhyung Kim | e0d66c7 | 2014-07-31 14:47:37 +0900 | [diff] [blame] | 1401 | void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists) |
Namhyung Kim | 678a500 | 2014-03-20 11:18:54 +0900 | [diff] [blame] | 1402 | { |
| 1403 | struct hpp_sort_entry *hse; |
| 1404 | |
| 1405 | if (!perf_hpp__is_sort_entry(fmt)) |
| 1406 | return; |
| 1407 | |
| 1408 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
Namhyung Kim | 1ecd445 | 2014-07-31 14:47:40 +0900 | [diff] [blame] | 1409 | hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name)); |
Namhyung Kim | 678a500 | 2014-03-20 11:18:54 +0900 | [diff] [blame] | 1410 | } |
| 1411 | |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1412 | static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
| 1413 | struct perf_evsel *evsel) |
| 1414 | { |
| 1415 | struct hpp_sort_entry *hse; |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1416 | size_t len = fmt->user_len; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1417 | |
| 1418 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1419 | |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1420 | if (!len) |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 1421 | len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx); |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1422 | |
Namhyung Kim | 1ecd445 | 2014-07-31 14:47:40 +0900 | [diff] [blame] | 1423 | return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name); |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static int __sort__hpp_width(struct perf_hpp_fmt *fmt, |
| 1427 | struct perf_hpp *hpp __maybe_unused, |
| 1428 | struct perf_evsel *evsel) |
| 1429 | { |
| 1430 | struct hpp_sort_entry *hse; |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1431 | size_t len = fmt->user_len; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1432 | |
| 1433 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1434 | |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1435 | if (!len) |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 1436 | len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx); |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1437 | |
| 1438 | return len; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
| 1442 | struct hist_entry *he) |
| 1443 | { |
| 1444 | struct hpp_sort_entry *hse; |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1445 | size_t len = fmt->user_len; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1446 | |
| 1447 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1448 | |
| 1449 | if (!len) |
| 1450 | len = hists__col_len(he->hists, hse->se->se_width_idx); |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1451 | |
| 1452 | return hse->se->se_snprintf(he, hpp->buf, hpp->size, len); |
| 1453 | } |
| 1454 | |
Namhyung Kim | 87bbdf7 | 2015-01-08 09:45:46 +0900 | [diff] [blame] | 1455 | static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt, |
| 1456 | struct hist_entry *a, struct hist_entry *b) |
| 1457 | { |
| 1458 | struct hpp_sort_entry *hse; |
| 1459 | |
| 1460 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1461 | return hse->se->se_cmp(a, b); |
| 1462 | } |
| 1463 | |
| 1464 | static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt, |
| 1465 | struct hist_entry *a, struct hist_entry *b) |
| 1466 | { |
| 1467 | struct hpp_sort_entry *hse; |
| 1468 | int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *); |
| 1469 | |
| 1470 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1471 | collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp; |
| 1472 | return collapse_fn(a, b); |
| 1473 | } |
| 1474 | |
| 1475 | static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt, |
| 1476 | struct hist_entry *a, struct hist_entry *b) |
| 1477 | { |
| 1478 | struct hpp_sort_entry *hse; |
| 1479 | int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *); |
| 1480 | |
| 1481 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1482 | sort_fn = hse->se->se_sort ?: hse->se->se_cmp; |
| 1483 | return sort_fn(a, b); |
| 1484 | } |
| 1485 | |
Jiri Olsa | 9735808 | 2016-01-18 10:24:03 +0100 | [diff] [blame] | 1486 | bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format) |
| 1487 | { |
| 1488 | return format->header == __sort__hpp_header; |
| 1489 | } |
| 1490 | |
Namhyung Kim | a9c6e46 | 2016-02-25 00:13:33 +0900 | [diff] [blame] | 1491 | bool perf_hpp__is_trace_entry(struct perf_hpp_fmt *fmt) |
| 1492 | { |
| 1493 | struct hpp_sort_entry *hse; |
| 1494 | |
| 1495 | if (!perf_hpp__is_sort_entry(fmt)) |
| 1496 | return false; |
| 1497 | |
| 1498 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1499 | return hse->se == &sort_trace; |
| 1500 | } |
| 1501 | |
| 1502 | bool perf_hpp__is_srcline_entry(struct perf_hpp_fmt *fmt) |
| 1503 | { |
| 1504 | struct hpp_sort_entry *hse; |
| 1505 | |
| 1506 | if (!perf_hpp__is_sort_entry(fmt)) |
| 1507 | return false; |
| 1508 | |
| 1509 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1510 | return hse->se == &sort_srcline; |
| 1511 | } |
| 1512 | |
| 1513 | bool perf_hpp__is_srcfile_entry(struct perf_hpp_fmt *fmt) |
| 1514 | { |
| 1515 | struct hpp_sort_entry *hse; |
| 1516 | |
| 1517 | if (!perf_hpp__is_sort_entry(fmt)) |
| 1518 | return false; |
| 1519 | |
| 1520 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1521 | return hse->se == &sort_srcfile; |
| 1522 | } |
| 1523 | |
Jiri Olsa | 9735808 | 2016-01-18 10:24:03 +0100 | [diff] [blame] | 1524 | static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b) |
| 1525 | { |
| 1526 | struct hpp_sort_entry *hse_a; |
| 1527 | struct hpp_sort_entry *hse_b; |
| 1528 | |
| 1529 | if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b)) |
| 1530 | return false; |
| 1531 | |
| 1532 | hse_a = container_of(a, struct hpp_sort_entry, hpp); |
| 1533 | hse_b = container_of(b, struct hpp_sort_entry, hpp); |
| 1534 | |
| 1535 | return hse_a->se == hse_b->se; |
| 1536 | } |
| 1537 | |
Jiri Olsa | 564132f | 2016-01-18 10:24:09 +0100 | [diff] [blame] | 1538 | static void hse_free(struct perf_hpp_fmt *fmt) |
| 1539 | { |
| 1540 | struct hpp_sort_entry *hse; |
| 1541 | |
| 1542 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1543 | free(hse); |
| 1544 | } |
| 1545 | |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1546 | static struct hpp_sort_entry * |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1547 | __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level) |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1548 | { |
| 1549 | struct hpp_sort_entry *hse; |
| 1550 | |
| 1551 | hse = malloc(sizeof(*hse)); |
| 1552 | if (hse == NULL) { |
| 1553 | pr_err("Memory allocation failed\n"); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1554 | return NULL; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | hse->se = sd->entry; |
Namhyung Kim | 1ecd445 | 2014-07-31 14:47:40 +0900 | [diff] [blame] | 1558 | hse->hpp.name = sd->entry->se_header; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1559 | hse->hpp.header = __sort__hpp_header; |
| 1560 | hse->hpp.width = __sort__hpp_width; |
| 1561 | hse->hpp.entry = __sort__hpp_entry; |
| 1562 | hse->hpp.color = NULL; |
| 1563 | |
Namhyung Kim | 87bbdf7 | 2015-01-08 09:45:46 +0900 | [diff] [blame] | 1564 | hse->hpp.cmp = __sort__hpp_cmp; |
| 1565 | hse->hpp.collapse = __sort__hpp_collapse; |
| 1566 | hse->hpp.sort = __sort__hpp_sort; |
Jiri Olsa | 9735808 | 2016-01-18 10:24:03 +0100 | [diff] [blame] | 1567 | hse->hpp.equal = __sort__hpp_equal; |
Jiri Olsa | 564132f | 2016-01-18 10:24:09 +0100 | [diff] [blame] | 1568 | hse->hpp.free = hse_free; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1569 | |
| 1570 | INIT_LIST_HEAD(&hse->hpp.list); |
| 1571 | INIT_LIST_HEAD(&hse->hpp.sort_list); |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 1572 | hse->hpp.elide = false; |
Namhyung Kim | e0d66c7 | 2014-07-31 14:47:37 +0900 | [diff] [blame] | 1573 | hse->hpp.len = 0; |
Namhyung Kim | 5b59166 | 2014-07-31 14:47:38 +0900 | [diff] [blame] | 1574 | hse->hpp.user_len = 0; |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1575 | hse->hpp.level = level; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1576 | |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1577 | return hse; |
| 1578 | } |
| 1579 | |
Jiri Olsa | 564132f | 2016-01-18 10:24:09 +0100 | [diff] [blame] | 1580 | static void hpp_free(struct perf_hpp_fmt *fmt) |
| 1581 | { |
| 1582 | free(fmt); |
| 1583 | } |
| 1584 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1585 | static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd, |
| 1586 | int level) |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 1587 | { |
| 1588 | struct perf_hpp_fmt *fmt; |
| 1589 | |
| 1590 | fmt = memdup(hd->fmt, sizeof(*fmt)); |
| 1591 | if (fmt) { |
| 1592 | INIT_LIST_HEAD(&fmt->list); |
| 1593 | INIT_LIST_HEAD(&fmt->sort_list); |
Jiri Olsa | 564132f | 2016-01-18 10:24:09 +0100 | [diff] [blame] | 1594 | fmt->free = hpp_free; |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1595 | fmt->level = level; |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | return fmt; |
| 1599 | } |
| 1600 | |
Namhyung Kim | 5443010 | 2016-02-25 00:13:37 +0900 | [diff] [blame] | 1601 | int hist_entry__filter(struct hist_entry *he, int type, const void *arg) |
| 1602 | { |
| 1603 | struct perf_hpp_fmt *fmt; |
| 1604 | struct hpp_sort_entry *hse; |
| 1605 | |
| 1606 | fmt = he->fmt; |
| 1607 | if (fmt == NULL || !perf_hpp__is_sort_entry(fmt)) |
| 1608 | return -1; |
| 1609 | |
| 1610 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 1611 | if (hse->se->se_filter == NULL) |
| 1612 | return -1; |
| 1613 | |
| 1614 | return hse->se->se_filter(he, type, arg); |
| 1615 | } |
| 1616 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1617 | static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd, int level) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1618 | { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1619 | struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1620 | |
| 1621 | if (hse == NULL) |
| 1622 | return -1; |
| 1623 | |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 1624 | perf_hpp__register_sort_field(&hse->hpp); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 1628 | static int __sort_dimension__add_hpp_output(struct perf_hpp_list *list, |
| 1629 | struct sort_dimension *sd) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1630 | { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1631 | struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1632 | |
| 1633 | if (hse == NULL) |
| 1634 | return -1; |
| 1635 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 1636 | perf_hpp_list__column_register(list, &hse->hpp); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |
| 1639 | |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1640 | struct hpp_dynamic_entry { |
| 1641 | struct perf_hpp_fmt hpp; |
| 1642 | struct perf_evsel *evsel; |
| 1643 | struct format_field *field; |
| 1644 | unsigned dynamic_len; |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 1645 | bool raw_trace; |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1646 | }; |
| 1647 | |
| 1648 | static int hde_width(struct hpp_dynamic_entry *hde) |
| 1649 | { |
| 1650 | if (!hde->hpp.len) { |
| 1651 | int len = hde->dynamic_len; |
| 1652 | int namelen = strlen(hde->field->name); |
| 1653 | int fieldlen = hde->field->size; |
| 1654 | |
| 1655 | if (namelen > len) |
| 1656 | len = namelen; |
| 1657 | |
| 1658 | if (!(hde->field->flags & FIELD_IS_STRING)) { |
| 1659 | /* length for print hex numbers */ |
| 1660 | fieldlen = hde->field->size * 2 + 2; |
| 1661 | } |
| 1662 | if (fieldlen > len) |
| 1663 | len = fieldlen; |
| 1664 | |
| 1665 | hde->hpp.len = len; |
| 1666 | } |
| 1667 | return hde->hpp.len; |
| 1668 | } |
| 1669 | |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1670 | static void update_dynamic_len(struct hpp_dynamic_entry *hde, |
| 1671 | struct hist_entry *he) |
| 1672 | { |
| 1673 | char *str, *pos; |
| 1674 | struct format_field *field = hde->field; |
| 1675 | size_t namelen; |
| 1676 | bool last = false; |
| 1677 | |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 1678 | if (hde->raw_trace) |
| 1679 | return; |
| 1680 | |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1681 | /* parse pretty print result and update max length */ |
| 1682 | if (!he->trace_output) |
| 1683 | he->trace_output = get_trace_output(he); |
| 1684 | |
| 1685 | namelen = strlen(field->name); |
| 1686 | str = he->trace_output; |
| 1687 | |
| 1688 | while (str) { |
| 1689 | pos = strchr(str, ' '); |
| 1690 | if (pos == NULL) { |
| 1691 | last = true; |
| 1692 | pos = str + strlen(str); |
| 1693 | } |
| 1694 | |
| 1695 | if (!strncmp(str, field->name, namelen)) { |
| 1696 | size_t len; |
| 1697 | |
| 1698 | str += namelen + 1; |
| 1699 | len = pos - str; |
| 1700 | |
| 1701 | if (len > hde->dynamic_len) |
| 1702 | hde->dynamic_len = len; |
| 1703 | break; |
| 1704 | } |
| 1705 | |
| 1706 | if (last) |
| 1707 | str = NULL; |
| 1708 | else |
| 1709 | str = pos + 1; |
| 1710 | } |
| 1711 | } |
| 1712 | |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1713 | static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
| 1714 | struct perf_evsel *evsel __maybe_unused) |
| 1715 | { |
| 1716 | struct hpp_dynamic_entry *hde; |
| 1717 | size_t len = fmt->user_len; |
| 1718 | |
| 1719 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); |
| 1720 | |
| 1721 | if (!len) |
| 1722 | len = hde_width(hde); |
| 1723 | |
| 1724 | return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name); |
| 1725 | } |
| 1726 | |
| 1727 | static int __sort__hde_width(struct perf_hpp_fmt *fmt, |
| 1728 | struct perf_hpp *hpp __maybe_unused, |
| 1729 | struct perf_evsel *evsel __maybe_unused) |
| 1730 | { |
| 1731 | struct hpp_dynamic_entry *hde; |
| 1732 | size_t len = fmt->user_len; |
| 1733 | |
| 1734 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); |
| 1735 | |
| 1736 | if (!len) |
| 1737 | len = hde_width(hde); |
| 1738 | |
| 1739 | return len; |
| 1740 | } |
| 1741 | |
Namhyung Kim | 361459f | 2015-12-23 02:07:08 +0900 | [diff] [blame] | 1742 | bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists) |
| 1743 | { |
| 1744 | struct hpp_dynamic_entry *hde; |
| 1745 | |
| 1746 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); |
| 1747 | |
| 1748 | return hists_to_evsel(hists) == hde->evsel; |
| 1749 | } |
| 1750 | |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1751 | static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
| 1752 | struct hist_entry *he) |
| 1753 | { |
| 1754 | struct hpp_dynamic_entry *hde; |
| 1755 | size_t len = fmt->user_len; |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1756 | char *str, *pos; |
| 1757 | struct format_field *field; |
| 1758 | size_t namelen; |
| 1759 | bool last = false; |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1760 | int ret; |
| 1761 | |
| 1762 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); |
| 1763 | |
| 1764 | if (!len) |
| 1765 | len = hde_width(hde); |
| 1766 | |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 1767 | if (hde->raw_trace) |
| 1768 | goto raw_field; |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1769 | |
Namhyung Kim | e049d4a | 2016-02-27 03:52:46 +0900 | [diff] [blame] | 1770 | if (!he->trace_output) |
| 1771 | he->trace_output = get_trace_output(he); |
| 1772 | |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 1773 | field = hde->field; |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1774 | namelen = strlen(field->name); |
| 1775 | str = he->trace_output; |
| 1776 | |
| 1777 | while (str) { |
| 1778 | pos = strchr(str, ' '); |
| 1779 | if (pos == NULL) { |
| 1780 | last = true; |
| 1781 | pos = str + strlen(str); |
| 1782 | } |
| 1783 | |
| 1784 | if (!strncmp(str, field->name, namelen)) { |
| 1785 | str += namelen + 1; |
| 1786 | str = strndup(str, pos - str); |
| 1787 | |
| 1788 | if (str == NULL) |
| 1789 | return scnprintf(hpp->buf, hpp->size, |
| 1790 | "%*.*s", len, len, "ERROR"); |
| 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | if (last) |
| 1795 | str = NULL; |
| 1796 | else |
| 1797 | str = pos + 1; |
| 1798 | } |
| 1799 | |
| 1800 | if (str == NULL) { |
| 1801 | struct trace_seq seq; |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 1802 | raw_field: |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1803 | trace_seq_init(&seq); |
| 1804 | pevent_print_field(&seq, he->raw_data, hde->field); |
| 1805 | str = seq.buffer; |
| 1806 | } |
| 1807 | |
| 1808 | ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str); |
| 1809 | free(str); |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1810 | return ret; |
| 1811 | } |
| 1812 | |
| 1813 | static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt, |
| 1814 | struct hist_entry *a, struct hist_entry *b) |
| 1815 | { |
| 1816 | struct hpp_dynamic_entry *hde; |
| 1817 | struct format_field *field; |
| 1818 | unsigned offset, size; |
| 1819 | |
| 1820 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); |
| 1821 | |
Namhyung Kim | abab5e7 | 2016-02-27 03:52:47 +0900 | [diff] [blame] | 1822 | if (b == NULL) { |
| 1823 | update_dynamic_len(hde, a); |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1827 | field = hde->field; |
| 1828 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 1829 | unsigned long long dyn; |
| 1830 | |
| 1831 | pevent_read_number_field(field, a->raw_data, &dyn); |
| 1832 | offset = dyn & 0xffff; |
| 1833 | size = (dyn >> 16) & 0xffff; |
| 1834 | |
| 1835 | /* record max width for output */ |
| 1836 | if (size > hde->dynamic_len) |
| 1837 | hde->dynamic_len = size; |
| 1838 | } else { |
| 1839 | offset = field->offset; |
| 1840 | size = field->size; |
| 1841 | } |
| 1842 | |
| 1843 | return memcmp(a->raw_data + offset, b->raw_data + offset, size); |
| 1844 | } |
| 1845 | |
Namhyung Kim | 361459f | 2015-12-23 02:07:08 +0900 | [diff] [blame] | 1846 | bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt) |
| 1847 | { |
| 1848 | return fmt->cmp == __sort__hde_cmp; |
| 1849 | } |
| 1850 | |
Namhyung Kim | 665aa75 | 2016-02-21 23:22:35 +0900 | [diff] [blame] | 1851 | static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b) |
| 1852 | { |
| 1853 | struct hpp_dynamic_entry *hde_a; |
| 1854 | struct hpp_dynamic_entry *hde_b; |
| 1855 | |
| 1856 | if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b)) |
| 1857 | return false; |
| 1858 | |
| 1859 | hde_a = container_of(a, struct hpp_dynamic_entry, hpp); |
| 1860 | hde_b = container_of(b, struct hpp_dynamic_entry, hpp); |
| 1861 | |
| 1862 | return hde_a->field == hde_b->field; |
| 1863 | } |
| 1864 | |
Jiri Olsa | 564132f | 2016-01-18 10:24:09 +0100 | [diff] [blame] | 1865 | static void hde_free(struct perf_hpp_fmt *fmt) |
| 1866 | { |
| 1867 | struct hpp_dynamic_entry *hde; |
| 1868 | |
| 1869 | hde = container_of(fmt, struct hpp_dynamic_entry, hpp); |
| 1870 | free(hde); |
| 1871 | } |
| 1872 | |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1873 | static struct hpp_dynamic_entry * |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1874 | __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field, |
| 1875 | int level) |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1876 | { |
| 1877 | struct hpp_dynamic_entry *hde; |
| 1878 | |
| 1879 | hde = malloc(sizeof(*hde)); |
| 1880 | if (hde == NULL) { |
| 1881 | pr_debug("Memory allocation failed\n"); |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | |
| 1885 | hde->evsel = evsel; |
| 1886 | hde->field = field; |
| 1887 | hde->dynamic_len = 0; |
| 1888 | |
| 1889 | hde->hpp.name = field->name; |
| 1890 | hde->hpp.header = __sort__hde_header; |
| 1891 | hde->hpp.width = __sort__hde_width; |
| 1892 | hde->hpp.entry = __sort__hde_entry; |
| 1893 | hde->hpp.color = NULL; |
| 1894 | |
| 1895 | hde->hpp.cmp = __sort__hde_cmp; |
| 1896 | hde->hpp.collapse = __sort__hde_cmp; |
| 1897 | hde->hpp.sort = __sort__hde_cmp; |
Namhyung Kim | 665aa75 | 2016-02-21 23:22:35 +0900 | [diff] [blame] | 1898 | hde->hpp.equal = __sort__hde_equal; |
Jiri Olsa | 564132f | 2016-01-18 10:24:09 +0100 | [diff] [blame] | 1899 | hde->hpp.free = hde_free; |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1900 | |
| 1901 | INIT_LIST_HEAD(&hde->hpp.list); |
| 1902 | INIT_LIST_HEAD(&hde->hpp.sort_list); |
| 1903 | hde->hpp.elide = false; |
| 1904 | hde->hpp.len = 0; |
| 1905 | hde->hpp.user_len = 0; |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1906 | hde->hpp.level = level; |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 1907 | |
| 1908 | return hde; |
| 1909 | } |
| 1910 | |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 1911 | static int parse_field_name(char *str, char **event, char **field, char **opt) |
| 1912 | { |
| 1913 | char *event_name, *field_name, *opt_name; |
| 1914 | |
| 1915 | event_name = str; |
| 1916 | field_name = strchr(str, '.'); |
| 1917 | |
| 1918 | if (field_name) { |
| 1919 | *field_name++ = '\0'; |
| 1920 | } else { |
| 1921 | event_name = NULL; |
| 1922 | field_name = str; |
| 1923 | } |
| 1924 | |
| 1925 | opt_name = strchr(field_name, '/'); |
| 1926 | if (opt_name) |
| 1927 | *opt_name++ = '\0'; |
| 1928 | |
| 1929 | *event = event_name; |
| 1930 | *field = field_name; |
| 1931 | *opt = opt_name; |
| 1932 | |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | /* find match evsel using a given event name. The event name can be: |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 1937 | * 1. '%' + event index (e.g. '%1' for first event) |
| 1938 | * 2. full event name (e.g. sched:sched_switch) |
| 1939 | * 3. partial event name (should not contain ':') |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 1940 | */ |
| 1941 | static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name) |
| 1942 | { |
| 1943 | struct perf_evsel *evsel = NULL; |
| 1944 | struct perf_evsel *pos; |
| 1945 | bool full_name; |
| 1946 | |
| 1947 | /* case 1 */ |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 1948 | if (event_name[0] == '%') { |
| 1949 | int nr = strtol(event_name+1, NULL, 0); |
| 1950 | |
| 1951 | if (nr > evlist->nr_entries) |
| 1952 | return NULL; |
| 1953 | |
| 1954 | evsel = perf_evlist__first(evlist); |
| 1955 | while (--nr > 0) |
| 1956 | evsel = perf_evsel__next(evsel); |
| 1957 | |
| 1958 | return evsel; |
| 1959 | } |
| 1960 | |
| 1961 | full_name = !!strchr(event_name, ':'); |
| 1962 | evlist__for_each(evlist, pos) { |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 1963 | /* case 2 */ |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 1964 | if (full_name && !strcmp(pos->name, event_name)) |
| 1965 | return pos; |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 1966 | /* case 3 */ |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 1967 | if (!full_name && strstr(pos->name, event_name)) { |
| 1968 | if (evsel) { |
| 1969 | pr_debug("'%s' event is ambiguous: it can be %s or %s\n", |
| 1970 | event_name, evsel->name, pos->name); |
| 1971 | return NULL; |
| 1972 | } |
| 1973 | evsel = pos; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | return evsel; |
| 1978 | } |
| 1979 | |
Namhyung Kim | 3b099bf5 | 2015-12-23 02:07:07 +0900 | [diff] [blame] | 1980 | static int __dynamic_dimension__add(struct perf_evsel *evsel, |
| 1981 | struct format_field *field, |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1982 | bool raw_trace, int level) |
Namhyung Kim | 3b099bf5 | 2015-12-23 02:07:07 +0900 | [diff] [blame] | 1983 | { |
| 1984 | struct hpp_dynamic_entry *hde; |
| 1985 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1986 | hde = __alloc_dynamic_entry(evsel, field, level); |
Namhyung Kim | 3b099bf5 | 2015-12-23 02:07:07 +0900 | [diff] [blame] | 1987 | if (hde == NULL) |
| 1988 | return -ENOMEM; |
| 1989 | |
| 1990 | hde->raw_trace = raw_trace; |
| 1991 | |
| 1992 | perf_hpp__register_sort_field(&hde->hpp); |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 1996 | static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level) |
Namhyung Kim | 2e422fd | 2015-12-23 02:07:09 +0900 | [diff] [blame] | 1997 | { |
| 1998 | int ret; |
| 1999 | struct format_field *field; |
| 2000 | |
| 2001 | field = evsel->tp_format->format.fields; |
| 2002 | while (field) { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2003 | ret = __dynamic_dimension__add(evsel, field, raw_trace, level); |
Namhyung Kim | 2e422fd | 2015-12-23 02:07:09 +0900 | [diff] [blame] | 2004 | if (ret < 0) |
| 2005 | return ret; |
| 2006 | |
| 2007 | field = field->next; |
| 2008 | } |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2012 | static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace, |
| 2013 | int level) |
Namhyung Kim | 2e422fd | 2015-12-23 02:07:09 +0900 | [diff] [blame] | 2014 | { |
| 2015 | int ret; |
| 2016 | struct perf_evsel *evsel; |
| 2017 | |
| 2018 | evlist__for_each(evlist, evsel) { |
| 2019 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) |
| 2020 | continue; |
| 2021 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2022 | ret = add_evsel_fields(evsel, raw_trace, level); |
Namhyung Kim | 2e422fd | 2015-12-23 02:07:09 +0900 | [diff] [blame] | 2023 | if (ret < 0) |
| 2024 | return ret; |
| 2025 | } |
| 2026 | return 0; |
| 2027 | } |
| 2028 | |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 2029 | static int add_all_matching_fields(struct perf_evlist *evlist, |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2030 | char *field_name, bool raw_trace, int level) |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 2031 | { |
| 2032 | int ret = -ESRCH; |
| 2033 | struct perf_evsel *evsel; |
| 2034 | struct format_field *field; |
| 2035 | |
| 2036 | evlist__for_each(evlist, evsel) { |
| 2037 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) |
| 2038 | continue; |
| 2039 | |
| 2040 | field = pevent_find_any_field(evsel->tp_format, field_name); |
| 2041 | if (field == NULL) |
| 2042 | continue; |
| 2043 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2044 | ret = __dynamic_dimension__add(evsel, field, raw_trace, level); |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 2045 | if (ret < 0) |
| 2046 | break; |
| 2047 | } |
| 2048 | return ret; |
| 2049 | } |
| 2050 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2051 | static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok, |
| 2052 | int level) |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2053 | { |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 2054 | char *str, *event_name, *field_name, *opt_name; |
| 2055 | struct perf_evsel *evsel; |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2056 | struct format_field *field; |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 2057 | bool raw_trace = symbol_conf.raw_trace; |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2058 | int ret = 0; |
| 2059 | |
| 2060 | if (evlist == NULL) |
| 2061 | return -ENOENT; |
| 2062 | |
| 2063 | str = strdup(tok); |
| 2064 | if (str == NULL) |
| 2065 | return -ENOMEM; |
| 2066 | |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 2067 | if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) { |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2068 | ret = -EINVAL; |
| 2069 | goto out; |
| 2070 | } |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2071 | |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 2072 | if (opt_name) { |
| 2073 | if (strcmp(opt_name, "raw")) { |
| 2074 | pr_debug("unsupported field option %s\n", opt_name); |
Namhyung Kim | 053a398 | 2015-12-23 02:07:05 +0900 | [diff] [blame] | 2075 | ret = -EINVAL; |
| 2076 | goto out; |
| 2077 | } |
| 2078 | raw_trace = true; |
| 2079 | } |
| 2080 | |
Namhyung Kim | 2e422fd | 2015-12-23 02:07:09 +0900 | [diff] [blame] | 2081 | if (!strcmp(field_name, "trace_fields")) { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2082 | ret = add_all_dynamic_fields(evlist, raw_trace, level); |
Namhyung Kim | 2e422fd | 2015-12-23 02:07:09 +0900 | [diff] [blame] | 2083 | goto out; |
| 2084 | } |
| 2085 | |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 2086 | if (event_name == NULL) { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2087 | ret = add_all_matching_fields(evlist, field_name, raw_trace, level); |
Namhyung Kim | 9735be2 | 2016-01-05 19:58:35 +0900 | [diff] [blame] | 2088 | goto out; |
| 2089 | } |
| 2090 | |
Namhyung Kim | 5d0cff9 | 2015-12-23 02:07:06 +0900 | [diff] [blame] | 2091 | evsel = find_evsel(evlist, event_name); |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2092 | if (evsel == NULL) { |
| 2093 | pr_debug("Cannot find event: %s\n", event_name); |
| 2094 | ret = -ENOENT; |
| 2095 | goto out; |
| 2096 | } |
| 2097 | |
| 2098 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) { |
| 2099 | pr_debug("%s is not a tracepoint event\n", event_name); |
| 2100 | ret = -EINVAL; |
| 2101 | goto out; |
| 2102 | } |
| 2103 | |
Namhyung Kim | 3b099bf5 | 2015-12-23 02:07:07 +0900 | [diff] [blame] | 2104 | if (!strcmp(field_name, "*")) { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2105 | ret = add_evsel_fields(evsel, raw_trace, level); |
Namhyung Kim | 3b099bf5 | 2015-12-23 02:07:07 +0900 | [diff] [blame] | 2106 | } else { |
| 2107 | field = pevent_find_any_field(evsel->tp_format, field_name); |
| 2108 | if (field == NULL) { |
| 2109 | pr_debug("Cannot find event field for %s.%s\n", |
| 2110 | event_name, field_name); |
| 2111 | return -ENOENT; |
| 2112 | } |
| 2113 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2114 | ret = __dynamic_dimension__add(evsel, field, raw_trace, level); |
Namhyung Kim | 3b099bf5 | 2015-12-23 02:07:07 +0900 | [diff] [blame] | 2115 | } |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2116 | |
| 2117 | out: |
| 2118 | free(str); |
| 2119 | return ret; |
| 2120 | } |
| 2121 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2122 | static int __sort_dimension__add(struct sort_dimension *sd, int level) |
Namhyung Kim | 2f532d0 | 2013-04-03 21:26:10 +0900 | [diff] [blame] | 2123 | { |
| 2124 | if (sd->taken) |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 2125 | return 0; |
| 2126 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2127 | if (__sort_dimension__add_hpp_sort(sd, level) < 0) |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 2128 | return -1; |
Namhyung Kim | 2f532d0 | 2013-04-03 21:26:10 +0900 | [diff] [blame] | 2129 | |
| 2130 | if (sd->entry->se_collapse) |
| 2131 | sort__need_collapse = 1; |
| 2132 | |
Namhyung Kim | 2f532d0 | 2013-04-03 21:26:10 +0900 | [diff] [blame] | 2133 | sd->taken = 1; |
Namhyung Kim | 8b53699 | 2014-03-03 11:46:55 +0900 | [diff] [blame] | 2134 | |
| 2135 | return 0; |
Namhyung Kim | 2f532d0 | 2013-04-03 21:26:10 +0900 | [diff] [blame] | 2136 | } |
| 2137 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2138 | static int __hpp_dimension__add(struct hpp_dimension *hd, int level) |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 2139 | { |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 2140 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 2141 | |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 2142 | if (hd->taken) |
| 2143 | return 0; |
| 2144 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2145 | fmt = __hpp_dimension__alloc_hpp(hd, level); |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 2146 | if (!fmt) |
| 2147 | return -1; |
| 2148 | |
| 2149 | hd->taken = 1; |
| 2150 | perf_hpp__register_sort_field(fmt); |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 2151 | return 0; |
| 2152 | } |
| 2153 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2154 | static int __sort_dimension__add_output(struct perf_hpp_list *list, |
| 2155 | struct sort_dimension *sd) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2156 | { |
| 2157 | if (sd->taken) |
| 2158 | return 0; |
| 2159 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2160 | if (__sort_dimension__add_hpp_output(list, sd) < 0) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2161 | return -1; |
| 2162 | |
| 2163 | sd->taken = 1; |
| 2164 | return 0; |
| 2165 | } |
| 2166 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2167 | static int __hpp_dimension__add_output(struct perf_hpp_list *list, |
| 2168 | struct hpp_dimension *hd) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2169 | { |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 2170 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2171 | |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 2172 | if (hd->taken) |
| 2173 | return 0; |
| 2174 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2175 | fmt = __hpp_dimension__alloc_hpp(hd, 0); |
Jiri Olsa | 1945c3e | 2016-01-18 10:24:07 +0100 | [diff] [blame] | 2176 | if (!fmt) |
| 2177 | return -1; |
| 2178 | |
| 2179 | hd->taken = 1; |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2180 | perf_hpp_list__column_register(list, fmt); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2181 | return 0; |
| 2182 | } |
| 2183 | |
Jiri Olsa | beeaaeb | 2015-10-06 14:25:11 +0200 | [diff] [blame] | 2184 | int hpp_dimension__add_output(unsigned col) |
| 2185 | { |
| 2186 | BUG_ON(col >= PERF_HPP__MAX_INDEX); |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2187 | return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]); |
Jiri Olsa | beeaaeb | 2015-10-06 14:25:11 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2190 | static int sort_dimension__add(const char *tok, struct perf_evlist *evlist, |
| 2191 | int level) |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2192 | { |
| 2193 | unsigned int i; |
| 2194 | |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 2195 | for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) { |
| 2196 | struct sort_dimension *sd = &common_sort_dimensions[i]; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2197 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2198 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 2199 | continue; |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 2200 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2201 | if (sd->entry == &sort_parent) { |
| 2202 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); |
| 2203 | if (ret) { |
| 2204 | char err[BUFSIZ]; |
| 2205 | |
| 2206 | regerror(ret, &parent_regex, err, sizeof(err)); |
Arnaldo Carvalho de Melo | 2aefa4f | 2010-04-02 12:30:57 -0300 | [diff] [blame] | 2207 | pr_err("Invalid regex: %s\n%s", parent_pattern, err); |
| 2208 | return -EINVAL; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2209 | } |
| 2210 | sort__has_parent = 1; |
Namhyung Kim | 930477b | 2013-04-05 10:26:36 +0900 | [diff] [blame] | 2211 | } else if (sd->entry == &sort_sym) { |
Namhyung Kim | 1af55640 | 2012-09-14 17:35:27 +0900 | [diff] [blame] | 2212 | sort__has_sym = 1; |
Kan Liang | 94ba462 | 2015-02-09 05:39:44 +0000 | [diff] [blame] | 2213 | /* |
| 2214 | * perf diff displays the performance difference amongst |
| 2215 | * two or more perf.data files. Those files could come |
| 2216 | * from different binaries. So we should not compare |
| 2217 | * their ips, but the name of symbol. |
| 2218 | */ |
| 2219 | if (sort__mode == SORT_MODE__DIFF) |
| 2220 | sd->entry->se_collapse = sort__sym_sort; |
| 2221 | |
Namhyung Kim | 68f6d02 | 2013-12-18 14:21:10 +0900 | [diff] [blame] | 2222 | } else if (sd->entry == &sort_dso) { |
| 2223 | sort__has_dso = 1; |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 2224 | } else if (sd->entry == &sort_socket) { |
| 2225 | sort__has_socket = 1; |
Namhyung Kim | cfd92da | 2016-01-21 19:13:24 -0300 | [diff] [blame] | 2226 | } else if (sd->entry == &sort_thread) { |
| 2227 | sort__has_thread = 1; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2230 | return __sort_dimension__add(sd, level); |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2231 | } |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 2232 | |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 2233 | for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) { |
| 2234 | struct hpp_dimension *hd = &hpp_sort_dimensions[i]; |
| 2235 | |
| 2236 | if (strncasecmp(tok, hd->name, strlen(tok))) |
| 2237 | continue; |
| 2238 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2239 | return __hpp_dimension__add(hd, level); |
Namhyung Kim | a2ce067 | 2014-03-04 09:06:42 +0900 | [diff] [blame] | 2240 | } |
| 2241 | |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 2242 | for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) { |
| 2243 | struct sort_dimension *sd = &bstack_sort_dimensions[i]; |
| 2244 | |
| 2245 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 2246 | continue; |
| 2247 | |
Namhyung Kim | 55369fc | 2013-04-01 20:35:20 +0900 | [diff] [blame] | 2248 | if (sort__mode != SORT_MODE__BRANCH) |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 2249 | return -EINVAL; |
| 2250 | |
| 2251 | if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to) |
| 2252 | sort__has_sym = 1; |
| 2253 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2254 | __sort_dimension__add(sd, level); |
Namhyung Kim | fc5871e | 2012-12-27 18:11:46 +0900 | [diff] [blame] | 2255 | return 0; |
| 2256 | } |
| 2257 | |
Namhyung Kim | afab87b | 2013-04-03 21:26:11 +0900 | [diff] [blame] | 2258 | for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) { |
| 2259 | struct sort_dimension *sd = &memory_sort_dimensions[i]; |
| 2260 | |
| 2261 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 2262 | continue; |
| 2263 | |
| 2264 | if (sort__mode != SORT_MODE__MEMORY) |
| 2265 | return -EINVAL; |
| 2266 | |
| 2267 | if (sd->entry == &sort_mem_daddr_sym) |
| 2268 | sort__has_sym = 1; |
| 2269 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2270 | __sort_dimension__add(sd, level); |
Namhyung Kim | afab87b | 2013-04-03 21:26:11 +0900 | [diff] [blame] | 2271 | return 0; |
| 2272 | } |
| 2273 | |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2274 | if (!add_dynamic_entry(evlist, tok, level)) |
Namhyung Kim | c7c2a5e | 2015-12-23 02:07:02 +0900 | [diff] [blame] | 2275 | return 0; |
| 2276 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 2277 | return -ESRCH; |
| 2278 | } |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 2279 | |
Jiri Olsa | 2fbaa39 | 2016-01-18 10:24:10 +0100 | [diff] [blame] | 2280 | static int setup_sort_list(char *str, struct perf_evlist *evlist) |
| 2281 | { |
| 2282 | char *tmp, *tok; |
| 2283 | int ret = 0; |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2284 | int level = 0; |
Jiri Olsa | 2fbaa39 | 2016-01-18 10:24:10 +0100 | [diff] [blame] | 2285 | |
| 2286 | for (tok = strtok_r(str, ", ", &tmp); |
| 2287 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2288 | ret = sort_dimension__add(tok, evlist, level++); |
Jiri Olsa | 2fbaa39 | 2016-01-18 10:24:10 +0100 | [diff] [blame] | 2289 | if (ret == -EINVAL) { |
| 2290 | error("Invalid --sort key: `%s'", tok); |
| 2291 | break; |
| 2292 | } else if (ret == -ESRCH) { |
| 2293 | error("Unknown --sort key: `%s'", tok); |
| 2294 | break; |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | return ret; |
| 2299 | } |
| 2300 | |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2301 | static const char *get_default_sort_order(struct perf_evlist *evlist) |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 2302 | { |
| 2303 | const char *default_sort_orders[] = { |
| 2304 | default_sort_order, |
| 2305 | default_branch_sort_order, |
| 2306 | default_mem_sort_order, |
| 2307 | default_top_sort_order, |
| 2308 | default_diff_sort_order, |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2309 | default_tracepoint_sort_order, |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 2310 | }; |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2311 | bool use_trace = true; |
| 2312 | struct perf_evsel *evsel; |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 2313 | |
| 2314 | BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders)); |
| 2315 | |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2316 | if (evlist == NULL) |
| 2317 | goto out_no_evlist; |
| 2318 | |
| 2319 | evlist__for_each(evlist, evsel) { |
| 2320 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) { |
| 2321 | use_trace = false; |
| 2322 | break; |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | if (use_trace) { |
| 2327 | sort__mode = SORT_MODE__TRACEPOINT; |
| 2328 | if (symbol_conf.raw_trace) |
| 2329 | return "trace_fields"; |
| 2330 | } |
| 2331 | out_no_evlist: |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 2332 | return default_sort_orders[sort__mode]; |
| 2333 | } |
| 2334 | |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2335 | static int setup_sort_order(struct perf_evlist *evlist) |
Jiri Olsa | 1a1c0ff | 2014-08-23 14:59:48 +0200 | [diff] [blame] | 2336 | { |
| 2337 | char *new_sort_order; |
| 2338 | |
| 2339 | /* |
| 2340 | * Append '+'-prefixed sort order to the default sort |
| 2341 | * order string. |
| 2342 | */ |
| 2343 | if (!sort_order || is_strict_order(sort_order)) |
| 2344 | return 0; |
| 2345 | |
| 2346 | if (sort_order[1] == '\0') { |
| 2347 | error("Invalid --sort key: `+'"); |
| 2348 | return -EINVAL; |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * We allocate new sort_order string, but we never free it, |
| 2353 | * because it's checked over the rest of the code. |
| 2354 | */ |
| 2355 | if (asprintf(&new_sort_order, "%s,%s", |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2356 | get_default_sort_order(evlist), sort_order + 1) < 0) { |
Jiri Olsa | 1a1c0ff | 2014-08-23 14:59:48 +0200 | [diff] [blame] | 2357 | error("Not enough memory to set up --sort"); |
| 2358 | return -ENOMEM; |
| 2359 | } |
| 2360 | |
| 2361 | sort_order = new_sort_order; |
| 2362 | return 0; |
| 2363 | } |
| 2364 | |
Jiri Olsa | b97511c | 2016-01-07 10:14:08 +0100 | [diff] [blame] | 2365 | /* |
| 2366 | * Adds 'pre,' prefix into 'str' is 'pre' is |
| 2367 | * not already part of 'str'. |
| 2368 | */ |
| 2369 | static char *prefix_if_not_in(const char *pre, char *str) |
| 2370 | { |
| 2371 | char *n; |
| 2372 | |
| 2373 | if (!str || strstr(str, pre)) |
| 2374 | return str; |
| 2375 | |
| 2376 | if (asprintf(&n, "%s,%s", pre, str) < 0) |
| 2377 | return NULL; |
| 2378 | |
| 2379 | free(str); |
| 2380 | return n; |
| 2381 | } |
| 2382 | |
| 2383 | static char *setup_overhead(char *keys) |
| 2384 | { |
| 2385 | keys = prefix_if_not_in("overhead", keys); |
| 2386 | |
| 2387 | if (symbol_conf.cumulate_callchain) |
| 2388 | keys = prefix_if_not_in("overhead_children", keys); |
| 2389 | |
| 2390 | return keys; |
| 2391 | } |
| 2392 | |
Namhyung Kim | 40184c4 | 2015-12-23 02:07:01 +0900 | [diff] [blame] | 2393 | static int __setup_sorting(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 2394 | { |
Jiri Olsa | 2fbaa39 | 2016-01-18 10:24:10 +0100 | [diff] [blame] | 2395 | char *str; |
Jiri Olsa | 1a1c0ff | 2014-08-23 14:59:48 +0200 | [diff] [blame] | 2396 | const char *sort_keys; |
Namhyung Kim | 5530998 | 2013-02-06 14:57:16 +0900 | [diff] [blame] | 2397 | int ret = 0; |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 2398 | |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2399 | ret = setup_sort_order(evlist); |
Jiri Olsa | 1a1c0ff | 2014-08-23 14:59:48 +0200 | [diff] [blame] | 2400 | if (ret) |
| 2401 | return ret; |
| 2402 | |
| 2403 | sort_keys = sort_order; |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2404 | if (sort_keys == NULL) { |
Jiri Olsa | 2f3f9bc | 2014-08-22 15:58:38 +0200 | [diff] [blame] | 2405 | if (is_strict_order(field_order)) { |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2406 | /* |
| 2407 | * If user specified field order but no sort order, |
| 2408 | * we'll honor it and not add default sort orders. |
| 2409 | */ |
| 2410 | return 0; |
| 2411 | } |
| 2412 | |
Namhyung Kim | d49dade | 2015-12-23 02:07:10 +0900 | [diff] [blame] | 2413 | sort_keys = get_default_sort_order(evlist); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2414 | } |
Namhyung Kim | 512ae1b | 2014-03-18 11:31:39 +0900 | [diff] [blame] | 2415 | |
| 2416 | str = strdup(sort_keys); |
Namhyung Kim | 5936f54 | 2013-02-06 14:57:17 +0900 | [diff] [blame] | 2417 | if (str == NULL) { |
| 2418 | error("Not enough memory to setup sort keys"); |
| 2419 | return -ENOMEM; |
| 2420 | } |
| 2421 | |
Jiri Olsa | b97511c | 2016-01-07 10:14:08 +0100 | [diff] [blame] | 2422 | /* |
| 2423 | * Prepend overhead fields for backward compatibility. |
| 2424 | */ |
| 2425 | if (!is_strict_order(field_order)) { |
| 2426 | str = setup_overhead(str); |
| 2427 | if (str == NULL) { |
| 2428 | error("Not enough memory to setup overhead keys"); |
| 2429 | return -ENOMEM; |
| 2430 | } |
| 2431 | } |
| 2432 | |
Jiri Olsa | 2fbaa39 | 2016-01-18 10:24:10 +0100 | [diff] [blame] | 2433 | ret = setup_sort_list(str, evlist); |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 2434 | |
| 2435 | free(str); |
Namhyung Kim | 5530998 | 2013-02-06 14:57:16 +0900 | [diff] [blame] | 2436 | return ret; |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 2437 | } |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 2438 | |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2439 | void perf_hpp__set_elide(int idx, bool elide) |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 2440 | { |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2441 | struct perf_hpp_fmt *fmt; |
| 2442 | struct hpp_sort_entry *hse; |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 2443 | |
Jiri Olsa | cf09404 | 2016-01-18 10:24:17 +0100 | [diff] [blame] | 2444 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2445 | if (!perf_hpp__is_sort_entry(fmt)) |
| 2446 | continue; |
| 2447 | |
| 2448 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 2449 | if (hse->se->se_width_idx == idx) { |
| 2450 | fmt->elide = elide; |
| 2451 | break; |
| 2452 | } |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 2453 | } |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 2454 | } |
| 2455 | |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2456 | static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp) |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 2457 | { |
| 2458 | if (list && strlist__nr_entries(list) == 1) { |
| 2459 | if (fp != NULL) |
| 2460 | fprintf(fp, "# %s: %s\n", list_name, |
| 2461 | strlist__entry(list, 0)->s); |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2462 | return true; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 2463 | } |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2464 | return false; |
| 2465 | } |
| 2466 | |
| 2467 | static bool get_elide(int idx, FILE *output) |
| 2468 | { |
| 2469 | switch (idx) { |
| 2470 | case HISTC_SYMBOL: |
| 2471 | return __get_elide(symbol_conf.sym_list, "symbol", output); |
| 2472 | case HISTC_DSO: |
| 2473 | return __get_elide(symbol_conf.dso_list, "dso", output); |
| 2474 | case HISTC_COMM: |
| 2475 | return __get_elide(symbol_conf.comm_list, "comm", output); |
| 2476 | default: |
| 2477 | break; |
| 2478 | } |
| 2479 | |
| 2480 | if (sort__mode != SORT_MODE__BRANCH) |
| 2481 | return false; |
| 2482 | |
| 2483 | switch (idx) { |
| 2484 | case HISTC_SYMBOL_FROM: |
| 2485 | return __get_elide(symbol_conf.sym_from_list, "sym_from", output); |
| 2486 | case HISTC_SYMBOL_TO: |
| 2487 | return __get_elide(symbol_conf.sym_to_list, "sym_to", output); |
| 2488 | case HISTC_DSO_FROM: |
| 2489 | return __get_elide(symbol_conf.dso_from_list, "dso_from", output); |
| 2490 | case HISTC_DSO_TO: |
| 2491 | return __get_elide(symbol_conf.dso_to_list, "dso_to", output); |
| 2492 | default: |
| 2493 | break; |
| 2494 | } |
| 2495 | |
| 2496 | return false; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 2497 | } |
Namhyung Kim | 08e7154 | 2013-04-03 21:26:19 +0900 | [diff] [blame] | 2498 | |
| 2499 | void sort__setup_elide(FILE *output) |
| 2500 | { |
Namhyung Kim | cfaa154 | 2014-05-19 14:19:30 +0900 | [diff] [blame] | 2501 | struct perf_hpp_fmt *fmt; |
| 2502 | struct hpp_sort_entry *hse; |
Namhyung Kim | 7524f63 | 2013-11-08 17:53:42 +0900 | [diff] [blame] | 2503 | |
Jiri Olsa | cf09404 | 2016-01-18 10:24:17 +0100 | [diff] [blame] | 2504 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2505 | if (!perf_hpp__is_sort_entry(fmt)) |
| 2506 | continue; |
Namhyung Kim | 08e7154 | 2013-04-03 21:26:19 +0900 | [diff] [blame] | 2507 | |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2508 | hse = container_of(fmt, struct hpp_sort_entry, hpp); |
| 2509 | fmt->elide = get_elide(hse->se->se_width_idx, output); |
Namhyung Kim | 08e7154 | 2013-04-03 21:26:19 +0900 | [diff] [blame] | 2510 | } |
| 2511 | |
Namhyung Kim | 7524f63 | 2013-11-08 17:53:42 +0900 | [diff] [blame] | 2512 | /* |
| 2513 | * It makes no sense to elide all of sort entries. |
| 2514 | * Just revert them to show up again. |
| 2515 | */ |
Jiri Olsa | cf09404 | 2016-01-18 10:24:17 +0100 | [diff] [blame] | 2516 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
Namhyung Kim | cfaa154 | 2014-05-19 14:19:30 +0900 | [diff] [blame] | 2517 | if (!perf_hpp__is_sort_entry(fmt)) |
| 2518 | continue; |
| 2519 | |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2520 | if (!fmt->elide) |
Namhyung Kim | 7524f63 | 2013-11-08 17:53:42 +0900 | [diff] [blame] | 2521 | return; |
| 2522 | } |
| 2523 | |
Jiri Olsa | cf09404 | 2016-01-18 10:24:17 +0100 | [diff] [blame] | 2524 | perf_hpp_list__for_each_format(&perf_hpp_list, fmt) { |
Namhyung Kim | cfaa154 | 2014-05-19 14:19:30 +0900 | [diff] [blame] | 2525 | if (!perf_hpp__is_sort_entry(fmt)) |
| 2526 | continue; |
| 2527 | |
Jiri Olsa | f299842 | 2014-05-23 17:15:47 +0200 | [diff] [blame] | 2528 | fmt->elide = false; |
Namhyung Kim | cfaa154 | 2014-05-19 14:19:30 +0900 | [diff] [blame] | 2529 | } |
Namhyung Kim | 08e7154 | 2013-04-03 21:26:19 +0900 | [diff] [blame] | 2530 | } |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2531 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2532 | static int output_field_add(struct perf_hpp_list *list, char *tok) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2533 | { |
| 2534 | unsigned int i; |
| 2535 | |
| 2536 | for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) { |
| 2537 | struct sort_dimension *sd = &common_sort_dimensions[i]; |
| 2538 | |
| 2539 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 2540 | continue; |
| 2541 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2542 | return __sort_dimension__add_output(list, sd); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) { |
| 2546 | struct hpp_dimension *hd = &hpp_sort_dimensions[i]; |
| 2547 | |
| 2548 | if (strncasecmp(tok, hd->name, strlen(tok))) |
| 2549 | continue; |
| 2550 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2551 | return __hpp_dimension__add_output(list, hd); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) { |
| 2555 | struct sort_dimension *sd = &bstack_sort_dimensions[i]; |
| 2556 | |
| 2557 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 2558 | continue; |
| 2559 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2560 | return __sort_dimension__add_output(list, sd); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) { |
| 2564 | struct sort_dimension *sd = &memory_sort_dimensions[i]; |
| 2565 | |
| 2566 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 2567 | continue; |
| 2568 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2569 | return __sort_dimension__add_output(list, sd); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | return -ESRCH; |
| 2573 | } |
| 2574 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2575 | static int setup_output_list(struct perf_hpp_list *list, char *str) |
Jiri Olsa | 6d3375e | 2016-01-18 10:24:11 +0100 | [diff] [blame] | 2576 | { |
| 2577 | char *tmp, *tok; |
| 2578 | int ret = 0; |
| 2579 | |
| 2580 | for (tok = strtok_r(str, ", ", &tmp); |
| 2581 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2582 | ret = output_field_add(list, tok); |
Jiri Olsa | 6d3375e | 2016-01-18 10:24:11 +0100 | [diff] [blame] | 2583 | if (ret == -EINVAL) { |
| 2584 | error("Invalid --fields key: `%s'", tok); |
| 2585 | break; |
| 2586 | } else if (ret == -ESRCH) { |
| 2587 | error("Unknown --fields key: `%s'", tok); |
| 2588 | break; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | return ret; |
| 2593 | } |
| 2594 | |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2595 | static void reset_dimensions(void) |
| 2596 | { |
| 2597 | unsigned int i; |
| 2598 | |
| 2599 | for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) |
| 2600 | common_sort_dimensions[i].taken = 0; |
| 2601 | |
| 2602 | for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) |
| 2603 | hpp_sort_dimensions[i].taken = 0; |
| 2604 | |
| 2605 | for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) |
| 2606 | bstack_sort_dimensions[i].taken = 0; |
| 2607 | |
| 2608 | for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) |
| 2609 | memory_sort_dimensions[i].taken = 0; |
| 2610 | } |
| 2611 | |
Jiri Olsa | 2f3f9bc | 2014-08-22 15:58:38 +0200 | [diff] [blame] | 2612 | bool is_strict_order(const char *order) |
| 2613 | { |
| 2614 | return order && (*order != '+'); |
| 2615 | } |
| 2616 | |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2617 | static int __setup_output_field(void) |
| 2618 | { |
Jiri Olsa | 6d3375e | 2016-01-18 10:24:11 +0100 | [diff] [blame] | 2619 | char *str, *strp; |
Jiri Olsa | 2f3f9bc | 2014-08-22 15:58:38 +0200 | [diff] [blame] | 2620 | int ret = -EINVAL; |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2621 | |
| 2622 | if (field_order == NULL) |
| 2623 | return 0; |
| 2624 | |
Jiri Olsa | 2f3f9bc | 2014-08-22 15:58:38 +0200 | [diff] [blame] | 2625 | strp = str = strdup(field_order); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2626 | if (str == NULL) { |
| 2627 | error("Not enough memory to setup output fields"); |
| 2628 | return -ENOMEM; |
| 2629 | } |
| 2630 | |
Jiri Olsa | 2f3f9bc | 2014-08-22 15:58:38 +0200 | [diff] [blame] | 2631 | if (!is_strict_order(field_order)) |
| 2632 | strp++; |
| 2633 | |
| 2634 | if (!strlen(strp)) { |
| 2635 | error("Invalid --fields key: `+'"); |
| 2636 | goto out; |
| 2637 | } |
| 2638 | |
Jiri Olsa | 0760002 | 2016-01-18 10:24:16 +0100 | [diff] [blame] | 2639 | ret = setup_output_list(&perf_hpp_list, strp); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2640 | |
Jiri Olsa | 2f3f9bc | 2014-08-22 15:58:38 +0200 | [diff] [blame] | 2641 | out: |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2642 | free(str); |
| 2643 | return ret; |
| 2644 | } |
| 2645 | |
Arnaldo Carvalho de Melo | 9b24063 | 2016-03-02 09:58:00 -0300 | [diff] [blame] | 2646 | static void evlist__set_hists_nr_sort_keys(struct perf_evlist *evlist) |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2647 | { |
Namhyung Kim | d3a72fd | 2016-02-27 03:52:44 +0900 | [diff] [blame] | 2648 | struct perf_evsel *evsel; |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2649 | |
Namhyung Kim | d3a72fd | 2016-02-27 03:52:44 +0900 | [diff] [blame] | 2650 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 9b24063 | 2016-03-02 09:58:00 -0300 | [diff] [blame] | 2651 | struct perf_hpp_fmt *fmt; |
| 2652 | struct hists *hists = evsel__hists(evsel); |
| 2653 | |
Namhyung Kim | d3a72fd | 2016-02-27 03:52:44 +0900 | [diff] [blame] | 2654 | hists->nr_sort_keys = perf_hpp_list.nr_sort_keys; |
| 2655 | |
| 2656 | /* |
| 2657 | * If dynamic entries were used, it might add multiple |
| 2658 | * entries to each evsel for a single field name. Set |
| 2659 | * actual number of sort keys for each hists. |
| 2660 | */ |
| 2661 | perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) { |
| 2662 | if (perf_hpp__is_dynamic_entry(fmt) && |
| 2663 | !perf_hpp__defined_dynamic_entry(fmt, hists)) |
| 2664 | hists->nr_sort_keys--; |
| 2665 | } |
| 2666 | } |
Arnaldo Carvalho de Melo | 9b24063 | 2016-03-02 09:58:00 -0300 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | int setup_sorting(struct perf_evlist *evlist) |
| 2670 | { |
| 2671 | int err; |
| 2672 | |
| 2673 | err = __setup_sorting(evlist); |
| 2674 | if (err < 0) |
| 2675 | return err; |
| 2676 | |
| 2677 | if (parent_pattern != default_parent_pattern) { |
Namhyung Kim | 4b633eb | 2016-03-07 16:44:43 -0300 | [diff] [blame^] | 2678 | err = sort_dimension__add("parent", evlist, -1); |
Arnaldo Carvalho de Melo | 9b24063 | 2016-03-02 09:58:00 -0300 | [diff] [blame] | 2679 | if (err < 0) |
| 2680 | return err; |
| 2681 | } |
| 2682 | |
| 2683 | if (evlist != NULL) |
| 2684 | evlist__set_hists_nr_sort_keys(evlist); |
Namhyung Kim | d3a72fd | 2016-02-27 03:52:44 +0900 | [diff] [blame] | 2685 | |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2686 | reset_dimensions(); |
| 2687 | |
| 2688 | /* |
| 2689 | * perf diff doesn't use default hpp output fields. |
| 2690 | */ |
| 2691 | if (sort__mode != SORT_MODE__DIFF) |
| 2692 | perf_hpp__init(); |
| 2693 | |
| 2694 | err = __setup_output_field(); |
| 2695 | if (err < 0) |
| 2696 | return err; |
| 2697 | |
| 2698 | /* copy sort keys to output fields */ |
Jiri Olsa | 43e0a68 | 2016-01-18 10:24:21 +0100 | [diff] [blame] | 2699 | perf_hpp__setup_output_field(&perf_hpp_list); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2700 | /* and then copy output fields to sort keys */ |
Jiri Olsa | 43e0a68 | 2016-01-18 10:24:21 +0100 | [diff] [blame] | 2701 | perf_hpp__append_sort_keys(&perf_hpp_list); |
Namhyung Kim | a7d945b | 2014-03-04 10:46:34 +0900 | [diff] [blame] | 2702 | |
| 2703 | return 0; |
| 2704 | } |
Namhyung Kim | 1c89fe9 | 2014-05-07 18:42:24 +0900 | [diff] [blame] | 2705 | |
| 2706 | void reset_output_field(void) |
| 2707 | { |
| 2708 | sort__need_collapse = 0; |
| 2709 | sort__has_parent = 0; |
| 2710 | sort__has_sym = 0; |
| 2711 | sort__has_dso = 0; |
| 2712 | |
Namhyung Kim | d69b296 | 2014-05-23 10:59:01 +0900 | [diff] [blame] | 2713 | field_order = NULL; |
| 2714 | sort_order = NULL; |
| 2715 | |
Namhyung Kim | 1c89fe9 | 2014-05-07 18:42:24 +0900 | [diff] [blame] | 2716 | reset_dimensions(); |
Jiri Olsa | 43e0a68 | 2016-01-18 10:24:21 +0100 | [diff] [blame] | 2717 | perf_hpp__reset_output_field(&perf_hpp_list); |
Namhyung Kim | 1c89fe9 | 2014-05-07 18:42:24 +0900 | [diff] [blame] | 2718 | } |