blob: 2446c39b5fa6f4c28f5827a272a5dea8726294e1 [file] [log] [blame]
Don Zickus9b32ba72014-06-01 15:38:29 +02001#include <sys/mman.h>
John Kacurdd68ada2009-09-24 18:02:49 +02002#include "sort.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03003#include "hist.h"
Namhyung Kim4dfced32013-09-13 16:28:57 +09004#include "comm.h"
Namhyung Kim08e71542013-04-03 21:26:19 +09005#include "symbol.h"
Namhyung Kim8b536992014-03-03 11:46:55 +09006#include "evsel.h"
Namhyung Kim40184c42015-12-23 02:07:01 +09007#include "evlist.h"
8#include <traceevent/event-parse.h>
Jiri Olsa0c877d72016-02-24 09:46:46 +01009#include "mem-events.h"
John Kacurdd68ada2009-09-24 18:02:49 +020010
11regex_t parent_regex;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030012const char default_parent_pattern[] = "^sys_|^do_page_fault";
13const char *parent_pattern = default_parent_pattern;
14const char default_sort_order[] = "comm,dso,symbol";
Andi Kleen40997d62015-07-18 08:24:53 -070015const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
Namhyung Kim512ae1b2014-03-18 11:31:39 +090016const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
17const char default_top_sort_order[] = "dso,symbol";
18const char default_diff_sort_order[] = "dso,symbol";
Namhyung Kimd49dade2015-12-23 02:07:10 +090019const char default_tracepoint_sort_order[] = "trace";
Namhyung Kim512ae1b2014-03-18 11:31:39 +090020const char *sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +090021const char *field_order;
Greg Priceb21484f2012-12-06 21:48:05 -080022regex_t ignore_callees_regex;
23int have_ignore_callees = 0;
Kan Liang2e7ea3a2015-09-04 10:45:43 -040024int sort__has_socket = 0;
Namhyung Kimcfd92da2016-01-21 19:13:24 -030025int sort__has_thread = 0;
Namhyung Kim078b8d42016-03-09 23:20:51 +090026int sort__has_comm = 0;
Namhyung Kim55369fc2013-04-01 20:35:20 +090027enum sort_mode sort__mode = SORT_MODE__NORMAL;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020028
Arnaldo Carvalho de Melo37d9bb52016-02-12 11:27:51 -030029/*
30 * Replaces all occurrences of a char used with the:
31 *
32 * -t, --field-separator
33 *
34 * option, that uses a special separator character and don't pad with spaces,
35 * replacing all occurances of this separator in symbol names (and other
36 * output) with a '.' character, that thus it's the only non valid separator.
37*/
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030038static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020039{
40 int n;
41 va_list ap;
42
43 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030044 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020045 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030046 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020047
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030048 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020049 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030050 if (sep == NULL)
51 break;
52 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020053 }
John Kacurdd68ada2009-09-24 18:02:49 +020054 }
55 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110056
57 if (n >= (int)size)
58 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020059 return n;
60}
61
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020062static int64_t cmp_null(const void *l, const void *r)
Frederic Weisbecker872a8782011-06-29 03:14:52 +020063{
64 if (!l && !r)
65 return 0;
66 else if (!l)
67 return -1;
68 else
69 return 1;
70}
71
72/* --sort pid */
73
74static int64_t
75sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
76{
Adrian Hunter38051232013-07-04 16:20:31 +030077 return right->thread->tid - left->thread->tid;
Frederic Weisbecker872a8782011-06-29 03:14:52 +020078}
79
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030080static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030081 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020082{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020083 const char *comm = thread__comm_str(he->thread);
Namhyung Kim5b591662014-07-31 14:47:38 +090084
85 width = max(7U, width) - 6;
86 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
87 width, width, comm ?: "");
John Kacurdd68ada2009-09-24 18:02:49 +020088}
89
Namhyung Kim54430102016-02-25 00:13:37 +090090static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
91{
92 const struct thread *th = arg;
93
94 if (type != HIST_FILTER__THREAD)
95 return -1;
96
97 return th && he->thread != th;
98}
99
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200100struct sort_entry sort_thread = {
Namhyung Kim8246de82014-07-31 14:47:35 +0900101 .se_header = " Pid:Command",
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200102 .se_cmp = sort__thread_cmp,
103 .se_snprintf = hist_entry__thread_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900104 .se_filter = hist_entry__thread_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200105 .se_width_idx = HISTC_THREAD,
106};
107
108/* --sort comm */
109
110static int64_t
111sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
112{
Frederic Weisbeckerfedd63d2013-09-11 17:18:09 +0200113 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200114 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200115}
116
117static int64_t
118sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
119{
Namhyung Kim4dfced32013-09-13 16:28:57 +0900120 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200121 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200122}
123
Namhyung Kim202e7a62014-03-04 11:01:41 +0900124static int64_t
125sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
126{
127 return strcmp(comm__str(right->comm), comm__str(left->comm));
128}
129
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300130static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300131 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200132{
Namhyung Kim5b591662014-07-31 14:47:38 +0900133 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
John Kacurdd68ada2009-09-24 18:02:49 +0200134}
135
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900136struct sort_entry sort_comm = {
137 .se_header = "Command",
138 .se_cmp = sort__comm_cmp,
139 .se_collapse = sort__comm_collapse,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900140 .se_sort = sort__comm_sort,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900141 .se_snprintf = hist_entry__comm_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900142 .se_filter = hist_entry__thread_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900143 .se_width_idx = HISTC_COMM,
144};
145
146/* --sort dso */
147
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100148static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200149{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100150 struct dso *dso_l = map_l ? map_l->dso : NULL;
151 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300152 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200153
154 if (!dso_l || !dso_r)
Namhyung Kim202e7a62014-03-04 11:01:41 +0900155 return cmp_null(dso_r, dso_l);
John Kacurdd68ada2009-09-24 18:02:49 +0200156
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300157 if (verbose) {
158 dso_name_l = dso_l->long_name;
159 dso_name_r = dso_r->long_name;
160 } else {
161 dso_name_l = dso_l->short_name;
162 dso_name_r = dso_r->short_name;
163 }
164
165 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200166}
167
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100168static int64_t
169sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200170{
Namhyung Kim202e7a62014-03-04 11:01:41 +0900171 return _sort__dso_cmp(right->ms.map, left->ms.map);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100172}
173
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100174static int _hist_entry__dso_snprintf(struct map *map, char *bf,
175 size_t size, unsigned int width)
176{
177 if (map && map->dso) {
178 const char *dso_name = !verbose ? map->dso->short_name :
179 map->dso->long_name;
Namhyung Kim5b591662014-07-31 14:47:38 +0900180 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300181 }
John Kacurdd68ada2009-09-24 18:02:49 +0200182
Namhyung Kim5b591662014-07-31 14:47:38 +0900183 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200184}
185
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300186static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100187 size_t size, unsigned int width)
188{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300189 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100190}
191
Namhyung Kim54430102016-02-25 00:13:37 +0900192static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
193{
194 const struct dso *dso = arg;
195
196 if (type != HIST_FILTER__DSO)
197 return -1;
198
199 return dso && (!he->ms.map || he->ms.map->dso != dso);
200}
201
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900202struct sort_entry sort_dso = {
203 .se_header = "Shared Object",
204 .se_cmp = sort__dso_cmp,
205 .se_snprintf = hist_entry__dso_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900206 .se_filter = hist_entry__dso_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900207 .se_width_idx = HISTC_DSO,
208};
209
210/* --sort symbol */
211
Namhyung Kim2037be52013-12-18 14:21:09 +0900212static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
213{
214 return (int64_t)(right_ip - left_ip);
215}
216
Namhyung Kim51f27d12013-02-06 14:57:15 +0900217static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900218{
219 if (!sym_l || !sym_r)
220 return cmp_null(sym_l, sym_r);
221
222 if (sym_l == sym_r)
223 return 0;
224
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700225 if (sym_l->start != sym_r->start)
226 return (int64_t)(sym_r->start - sym_l->start);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900227
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700228 return (int64_t)(sym_r->end - sym_l->end);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900229}
230
231static int64_t
232sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
233{
Namhyung Kim09600e02013-10-15 11:01:56 +0900234 int64_t ret;
235
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900236 if (!left->ms.sym && !right->ms.sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900237 return _sort__addr_cmp(left->ip, right->ip);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900238
Namhyung Kim09600e02013-10-15 11:01:56 +0900239 /*
240 * comparing symbol address alone is not enough since it's a
241 * relative address within a dso.
242 */
Jiri Olsa69849fc2016-05-03 13:54:45 +0200243 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
Namhyung Kim68f6d022013-12-18 14:21:10 +0900244 ret = sort__dso_cmp(left, right);
245 if (ret != 0)
246 return ret;
247 }
Namhyung Kim09600e02013-10-15 11:01:56 +0900248
Namhyung Kim51f27d12013-02-06 14:57:15 +0900249 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900250}
251
Namhyung Kim202e7a62014-03-04 11:01:41 +0900252static int64_t
253sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
254{
255 if (!left->ms.sym || !right->ms.sym)
256 return cmp_null(left->ms.sym, right->ms.sym);
257
258 return strcmp(right->ms.sym->name, left->ms.sym->name);
259}
260
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100261static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
262 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900263 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100264{
265 size_t ret = 0;
266
267 if (verbose) {
268 char o = map ? dso__symtab_origin(map->dso) : '!';
269 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Namhyung Kimded19d52013-04-01 20:35:19 +0900270 BITS_PER_LONG / 4 + 2, ip, o);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100271 }
272
273 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100274 if (sym && map) {
275 if (map->type == MAP__VARIABLE) {
276 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
277 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
Namhyung Kim62667742013-01-24 16:10:42 +0100278 ip - map->unmap_ip(map, sym->start));
Stephane Eranian98a3b322013-01-24 16:10:35 +0100279 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300280 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
Stephane Eranian98a3b322013-01-24 16:10:35 +0100281 width - ret,
282 sym->name);
283 }
284 } else {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100285 size_t len = BITS_PER_LONG / 4;
286 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
287 len, ip);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100288 }
289
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300290 return ret;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100291}
292
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300293static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900294 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100295{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300296 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
297 he->level, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100298}
John Kacurdd68ada2009-09-24 18:02:49 +0200299
Namhyung Kim54430102016-02-25 00:13:37 +0900300static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
301{
302 const char *sym = arg;
303
304 if (type != HIST_FILTER__SYMBOL)
305 return -1;
306
307 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
308}
309
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200310struct sort_entry sort_sym = {
311 .se_header = "Symbol",
312 .se_cmp = sort__sym_cmp,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900313 .se_sort = sort__sym_sort,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200314 .se_snprintf = hist_entry__sym_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900315 .se_filter = hist_entry__sym_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200316 .se_width_idx = HISTC_SYMBOL,
317};
John Kacurdd68ada2009-09-24 18:02:49 +0200318
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300319/* --sort srcline */
320
Namhyung Kimcecaec62016-02-22 09:31:51 +0900321static char *hist_entry__get_srcline(struct hist_entry *he)
322{
323 struct map *map = he->ms.map;
324
325 if (!map)
326 return SRCLINE_UNKNOWN;
327
328 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
329 he->ms.sym, true);
330}
331
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300332static int64_t
333sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
334{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900335 if (!left->srcline)
336 left->srcline = hist_entry__get_srcline(left);
337 if (!right->srcline)
338 right->srcline = hist_entry__get_srcline(right);
339
Namhyung Kim202e7a62014-03-04 11:01:41 +0900340 return strcmp(right->srcline, left->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300341}
342
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300343static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim5b591662014-07-31 14:47:38 +0900344 size_t size, unsigned int width)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300345{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900346 if (!he->srcline)
347 he->srcline = hist_entry__get_srcline(he);
348
Namhyung Kim2960ed62016-02-22 09:32:33 +0900349 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300350}
351
352struct sort_entry sort_srcline = {
353 .se_header = "Source:Line",
354 .se_cmp = sort__srcline_cmp,
355 .se_snprintf = hist_entry__srcline_snprintf,
356 .se_width_idx = HISTC_SRCLINE,
357};
358
Andi Kleen31191a82015-08-07 15:54:24 -0700359/* --sort srcfile */
360
361static char no_srcfile[1];
362
Namhyung Kimcecaec62016-02-22 09:31:51 +0900363static char *hist_entry__get_srcfile(struct hist_entry *e)
Andi Kleen31191a82015-08-07 15:54:24 -0700364{
365 char *sf, *p;
366 struct map *map = e->ms.map;
367
Namhyung Kimcecaec62016-02-22 09:31:51 +0900368 if (!map)
369 return no_srcfile;
370
Andi Kleen2f84b422015-09-01 11:47:19 -0700371 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
372 e->ms.sym, false, true);
Andi Kleen76b10652015-08-11 06:36:55 -0700373 if (!strcmp(sf, SRCLINE_UNKNOWN))
374 return no_srcfile;
Andi Kleen31191a82015-08-07 15:54:24 -0700375 p = strchr(sf, ':');
376 if (p && *sf) {
377 *p = 0;
378 return sf;
379 }
380 free(sf);
381 return no_srcfile;
382}
383
384static int64_t
385sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
386{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900387 if (!left->srcfile)
388 left->srcfile = hist_entry__get_srcfile(left);
389 if (!right->srcfile)
390 right->srcfile = hist_entry__get_srcfile(right);
391
Andi Kleen31191a82015-08-07 15:54:24 -0700392 return strcmp(right->srcfile, left->srcfile);
393}
394
395static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
396 size_t size, unsigned int width)
397{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900398 if (!he->srcfile)
399 he->srcfile = hist_entry__get_srcfile(he);
400
Namhyung Kim2960ed62016-02-22 09:32:33 +0900401 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
Andi Kleen31191a82015-08-07 15:54:24 -0700402}
403
404struct sort_entry sort_srcfile = {
405 .se_header = "Source File",
406 .se_cmp = sort__srcfile_cmp,
407 .se_snprintf = hist_entry__srcfile_snprintf,
408 .se_width_idx = HISTC_SRCFILE,
409};
410
John Kacurdd68ada2009-09-24 18:02:49 +0200411/* --sort parent */
412
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200413static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200414sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
415{
416 struct symbol *sym_l = left->parent;
417 struct symbol *sym_r = right->parent;
418
419 if (!sym_l || !sym_r)
420 return cmp_null(sym_l, sym_r);
421
Namhyung Kim202e7a62014-03-04 11:01:41 +0900422 return strcmp(sym_r->name, sym_l->name);
John Kacurdd68ada2009-09-24 18:02:49 +0200423}
424
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300425static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300426 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200427{
Namhyung Kim5b591662014-07-31 14:47:38 +0900428 return repsep_snprintf(bf, size, "%-*.*s", width, width,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300429 he->parent ? he->parent->name : "[other]");
John Kacurdd68ada2009-09-24 18:02:49 +0200430}
431
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200432struct sort_entry sort_parent = {
433 .se_header = "Parent symbol",
434 .se_cmp = sort__parent_cmp,
435 .se_snprintf = hist_entry__parent_snprintf,
436 .se_width_idx = HISTC_PARENT,
437};
438
Arun Sharmaf60f3592010-06-04 11:27:10 -0300439/* --sort cpu */
440
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200441static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300442sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
443{
444 return right->cpu - left->cpu;
445}
446
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300447static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
448 size_t size, unsigned int width)
Arun Sharmaf60f3592010-06-04 11:27:10 -0300449{
Namhyung Kim5b591662014-07-31 14:47:38 +0900450 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300451}
452
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200453struct sort_entry sort_cpu = {
454 .se_header = "CPU",
455 .se_cmp = sort__cpu_cmp,
456 .se_snprintf = hist_entry__cpu_snprintf,
457 .se_width_idx = HISTC_CPU,
458};
459
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400460/* --sort socket */
461
462static int64_t
463sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
464{
465 return right->socket - left->socket;
466}
467
468static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
469 size_t size, unsigned int width)
470{
471 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
472}
473
Namhyung Kim54430102016-02-25 00:13:37 +0900474static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
475{
476 int sk = *(const int *)arg;
477
478 if (type != HIST_FILTER__SOCKET)
479 return -1;
480
481 return sk >= 0 && he->socket != sk;
482}
483
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400484struct sort_entry sort_socket = {
485 .se_header = "Socket",
486 .se_cmp = sort__socket_cmp,
487 .se_snprintf = hist_entry__socket_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900488 .se_filter = hist_entry__socket_filter,
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400489 .se_width_idx = HISTC_SOCKET,
490};
491
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900492/* --sort trace */
493
494static char *get_trace_output(struct hist_entry *he)
495{
496 struct trace_seq seq;
497 struct perf_evsel *evsel;
498 struct pevent_record rec = {
499 .data = he->raw_data,
500 .size = he->raw_size,
501 };
502
503 evsel = hists_to_evsel(he->hists);
504
505 trace_seq_init(&seq);
Namhyung Kim053a3982015-12-23 02:07:05 +0900506 if (symbol_conf.raw_trace) {
507 pevent_print_fields(&seq, he->raw_data, he->raw_size,
508 evsel->tp_format);
509 } else {
510 pevent_event_info(&seq, evsel->tp_format, &rec);
511 }
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900512 return seq.buffer;
513}
514
515static int64_t
516sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
517{
518 struct perf_evsel *evsel;
519
520 evsel = hists_to_evsel(left->hists);
521 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
522 return 0;
523
524 if (left->trace_output == NULL)
525 left->trace_output = get_trace_output(left);
526 if (right->trace_output == NULL)
527 right->trace_output = get_trace_output(right);
528
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900529 return strcmp(right->trace_output, left->trace_output);
530}
531
532static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
533 size_t size, unsigned int width)
534{
535 struct perf_evsel *evsel;
536
537 evsel = hists_to_evsel(he->hists);
538 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
Namhyung Kim2960ed62016-02-22 09:32:33 +0900539 return scnprintf(bf, size, "%-.*s", width, "N/A");
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900540
541 if (he->trace_output == NULL)
542 he->trace_output = get_trace_output(he);
Namhyung Kim2960ed62016-02-22 09:32:33 +0900543 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900544}
545
546struct sort_entry sort_trace = {
547 .se_header = "Trace output",
548 .se_cmp = sort__trace_cmp,
549 .se_snprintf = hist_entry__trace_snprintf,
550 .se_width_idx = HISTC_TRACE,
551};
552
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900553/* sort keys for branch stacks */
554
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100555static int64_t
556sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
557{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200558 if (!left->branch_info || !right->branch_info)
559 return cmp_null(left->branch_info, right->branch_info);
560
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100561 return _sort__dso_cmp(left->branch_info->from.map,
562 right->branch_info->from.map);
563}
564
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300565static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100566 size_t size, unsigned int width)
567{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200568 if (he->branch_info)
569 return _hist_entry__dso_snprintf(he->branch_info->from.map,
570 bf, size, width);
571 else
572 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100573}
574
Namhyung Kim54430102016-02-25 00:13:37 +0900575static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
576 const void *arg)
577{
578 const struct dso *dso = arg;
579
580 if (type != HIST_FILTER__DSO)
581 return -1;
582
583 return dso && (!he->branch_info || !he->branch_info->from.map ||
584 he->branch_info->from.map->dso != dso);
585}
586
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100587static int64_t
588sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
589{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200590 if (!left->branch_info || !right->branch_info)
591 return cmp_null(left->branch_info, right->branch_info);
592
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100593 return _sort__dso_cmp(left->branch_info->to.map,
594 right->branch_info->to.map);
595}
596
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300597static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100598 size_t size, unsigned int width)
599{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200600 if (he->branch_info)
601 return _hist_entry__dso_snprintf(he->branch_info->to.map,
602 bf, size, width);
603 else
604 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100605}
606
Namhyung Kim54430102016-02-25 00:13:37 +0900607static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
608 const void *arg)
609{
610 const struct dso *dso = arg;
611
612 if (type != HIST_FILTER__DSO)
613 return -1;
614
615 return dso && (!he->branch_info || !he->branch_info->to.map ||
616 he->branch_info->to.map->dso != dso);
617}
618
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100619static int64_t
620sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
621{
622 struct addr_map_symbol *from_l = &left->branch_info->from;
623 struct addr_map_symbol *from_r = &right->branch_info->from;
624
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200625 if (!left->branch_info || !right->branch_info)
626 return cmp_null(left->branch_info, right->branch_info);
627
628 from_l = &left->branch_info->from;
629 from_r = &right->branch_info->from;
630
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100631 if (!from_l->sym && !from_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900632 return _sort__addr_cmp(from_l->addr, from_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100633
Namhyung Kim51f27d12013-02-06 14:57:15 +0900634 return _sort__sym_cmp(from_l->sym, from_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100635}
636
637static int64_t
638sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
639{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200640 struct addr_map_symbol *to_l, *to_r;
641
642 if (!left->branch_info || !right->branch_info)
643 return cmp_null(left->branch_info, right->branch_info);
644
645 to_l = &left->branch_info->to;
646 to_r = &right->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100647
648 if (!to_l->sym && !to_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900649 return _sort__addr_cmp(to_l->addr, to_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100650
Namhyung Kim51f27d12013-02-06 14:57:15 +0900651 return _sort__sym_cmp(to_l->sym, to_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100652}
653
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300654static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900655 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100656{
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200657 if (he->branch_info) {
658 struct addr_map_symbol *from = &he->branch_info->from;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100659
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200660 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
661 he->level, bf, size, width);
662 }
663
664 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100665}
666
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300667static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900668 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100669{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200670 if (he->branch_info) {
671 struct addr_map_symbol *to = &he->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100672
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200673 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
674 he->level, bf, size, width);
675 }
676
677 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100678}
679
Namhyung Kim54430102016-02-25 00:13:37 +0900680static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
681 const void *arg)
682{
683 const char *sym = arg;
684
685 if (type != HIST_FILTER__SYMBOL)
686 return -1;
687
688 return sym && !(he->branch_info && he->branch_info->from.sym &&
689 strstr(he->branch_info->from.sym->name, sym));
690}
691
692static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
693 const void *arg)
694{
695 const char *sym = arg;
696
697 if (type != HIST_FILTER__SYMBOL)
698 return -1;
699
700 return sym && !(he->branch_info && he->branch_info->to.sym &&
701 strstr(he->branch_info->to.sym->name, sym));
702}
703
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900704struct sort_entry sort_dso_from = {
705 .se_header = "Source Shared Object",
706 .se_cmp = sort__dso_from_cmp,
707 .se_snprintf = hist_entry__dso_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900708 .se_filter = hist_entry__dso_from_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900709 .se_width_idx = HISTC_DSO_FROM,
710};
711
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100712struct sort_entry sort_dso_to = {
713 .se_header = "Target Shared Object",
714 .se_cmp = sort__dso_to_cmp,
715 .se_snprintf = hist_entry__dso_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900716 .se_filter = hist_entry__dso_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100717 .se_width_idx = HISTC_DSO_TO,
718};
719
720struct sort_entry sort_sym_from = {
721 .se_header = "Source Symbol",
722 .se_cmp = sort__sym_from_cmp,
723 .se_snprintf = hist_entry__sym_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900724 .se_filter = hist_entry__sym_from_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100725 .se_width_idx = HISTC_SYMBOL_FROM,
726};
727
728struct sort_entry sort_sym_to = {
729 .se_header = "Target Symbol",
730 .se_cmp = sort__sym_to_cmp,
731 .se_snprintf = hist_entry__sym_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900732 .se_filter = hist_entry__sym_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100733 .se_width_idx = HISTC_SYMBOL_TO,
734};
735
736static int64_t
737sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
738{
Jiri Olsa428560e2014-10-16 16:07:03 +0200739 unsigned char mp, p;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100740
Jiri Olsa428560e2014-10-16 16:07:03 +0200741 if (!left->branch_info || !right->branch_info)
742 return cmp_null(left->branch_info, right->branch_info);
743
744 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
745 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100746 return mp || p;
747}
748
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300749static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100750 size_t size, unsigned int width){
751 static const char *out = "N/A";
752
Jiri Olsa428560e2014-10-16 16:07:03 +0200753 if (he->branch_info) {
754 if (he->branch_info->flags.predicted)
755 out = "N";
756 else if (he->branch_info->flags.mispred)
757 out = "Y";
758 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100759
Namhyung Kim5b591662014-07-31 14:47:38 +0900760 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100761}
762
Andi Kleen0e332f02015-07-18 08:24:46 -0700763static int64_t
764sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
765{
766 return left->branch_info->flags.cycles -
767 right->branch_info->flags.cycles;
768}
769
770static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
771 size_t size, unsigned int width)
772{
773 if (he->branch_info->flags.cycles == 0)
774 return repsep_snprintf(bf, size, "%-*s", width, "-");
775 return repsep_snprintf(bf, size, "%-*hd", width,
776 he->branch_info->flags.cycles);
777}
778
779struct sort_entry sort_cycles = {
780 .se_header = "Basic Block Cycles",
781 .se_cmp = sort__cycles_cmp,
782 .se_snprintf = hist_entry__cycles_snprintf,
783 .se_width_idx = HISTC_CYCLES,
784};
785
Stephane Eranian98a3b322013-01-24 16:10:35 +0100786/* --sort daddr_sym */
787static int64_t
788sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
789{
790 uint64_t l = 0, r = 0;
791
792 if (left->mem_info)
793 l = left->mem_info->daddr.addr;
794 if (right->mem_info)
795 r = right->mem_info->daddr.addr;
796
797 return (int64_t)(r - l);
798}
799
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300800static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100801 size_t size, unsigned int width)
802{
803 uint64_t addr = 0;
804 struct map *map = NULL;
805 struct symbol *sym = NULL;
806
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300807 if (he->mem_info) {
808 addr = he->mem_info->daddr.addr;
809 map = he->mem_info->daddr.map;
810 sym = he->mem_info->daddr.sym;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100811 }
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300812 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100813 width);
814}
815
816static int64_t
Don Zickus28e6db22015-10-05 20:06:07 +0200817sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
818{
819 uint64_t l = 0, r = 0;
820
821 if (left->mem_info)
822 l = left->mem_info->iaddr.addr;
823 if (right->mem_info)
824 r = right->mem_info->iaddr.addr;
825
826 return (int64_t)(r - l);
827}
828
829static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
830 size_t size, unsigned int width)
831{
832 uint64_t addr = 0;
833 struct map *map = NULL;
834 struct symbol *sym = NULL;
835
836 if (he->mem_info) {
837 addr = he->mem_info->iaddr.addr;
838 map = he->mem_info->iaddr.map;
839 sym = he->mem_info->iaddr.sym;
840 }
841 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
842 width);
843}
844
845static int64_t
Stephane Eranian98a3b322013-01-24 16:10:35 +0100846sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
847{
848 struct map *map_l = NULL;
849 struct map *map_r = NULL;
850
851 if (left->mem_info)
852 map_l = left->mem_info->daddr.map;
853 if (right->mem_info)
854 map_r = right->mem_info->daddr.map;
855
856 return _sort__dso_cmp(map_l, map_r);
857}
858
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300859static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100860 size_t size, unsigned int width)
861{
862 struct map *map = NULL;
863
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300864 if (he->mem_info)
865 map = he->mem_info->daddr.map;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100866
867 return _hist_entry__dso_snprintf(map, bf, size, width);
868}
869
870static int64_t
871sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
872{
873 union perf_mem_data_src data_src_l;
874 union perf_mem_data_src data_src_r;
875
876 if (left->mem_info)
877 data_src_l = left->mem_info->data_src;
878 else
879 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
880
881 if (right->mem_info)
882 data_src_r = right->mem_info->data_src;
883 else
884 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
885
886 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
887}
888
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300889static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100890 size_t size, unsigned int width)
891{
Jiri Olsa69a77272016-02-24 09:46:49 +0100892 char out[10];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100893
Jiri Olsa69a77272016-02-24 09:46:49 +0100894 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300895 return repsep_snprintf(bf, size, "%.*s", width, out);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100896}
897
898static int64_t
899sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
900{
901 union perf_mem_data_src data_src_l;
902 union perf_mem_data_src data_src_r;
903
904 if (left->mem_info)
905 data_src_l = left->mem_info->data_src;
906 else
907 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
908
909 if (right->mem_info)
910 data_src_r = right->mem_info->data_src;
911 else
912 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
913
914 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
915}
916
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300917static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100918 size_t size, unsigned int width)
919{
920 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100921
Jiri Olsa0c877d72016-02-24 09:46:46 +0100922 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100923 return repsep_snprintf(bf, size, "%-*s", width, out);
924}
925
926static int64_t
927sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
928{
929 union perf_mem_data_src data_src_l;
930 union perf_mem_data_src data_src_r;
931
932 if (left->mem_info)
933 data_src_l = left->mem_info->data_src;
934 else
935 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
936
937 if (right->mem_info)
938 data_src_r = right->mem_info->data_src;
939 else
940 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
941
942 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
943}
944
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300945static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100946 size_t size, unsigned int width)
947{
948 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100949
Jiri Olsa071e9a12016-02-24 09:46:47 +0100950 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100951 return repsep_snprintf(bf, size, "%-*s", width, out);
952}
953
954static int64_t
955sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
956{
957 union perf_mem_data_src data_src_l;
958 union perf_mem_data_src data_src_r;
959
960 if (left->mem_info)
961 data_src_l = left->mem_info->data_src;
962 else
963 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
964
965 if (right->mem_info)
966 data_src_r = right->mem_info->data_src;
967 else
968 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
969
970 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
971}
972
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300973static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100974 size_t size, unsigned int width)
975{
976 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100977
Jiri Olsa2c07af12016-02-24 09:46:48 +0100978 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100979 return repsep_snprintf(bf, size, "%-*s", width, out);
980}
981
Don Zickus9b32ba72014-06-01 15:38:29 +0200982static int64_t
983sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
984{
985 u64 l, r;
986 struct map *l_map, *r_map;
987
988 if (!left->mem_info) return -1;
989 if (!right->mem_info) return 1;
990
991 /* group event types together */
992 if (left->cpumode > right->cpumode) return -1;
993 if (left->cpumode < right->cpumode) return 1;
994
995 l_map = left->mem_info->daddr.map;
996 r_map = right->mem_info->daddr.map;
997
998 /* if both are NULL, jump to sort on al_addr instead */
999 if (!l_map && !r_map)
1000 goto addr;
1001
1002 if (!l_map) return -1;
1003 if (!r_map) return 1;
1004
1005 if (l_map->maj > r_map->maj) return -1;
1006 if (l_map->maj < r_map->maj) return 1;
1007
1008 if (l_map->min > r_map->min) return -1;
1009 if (l_map->min < r_map->min) return 1;
1010
1011 if (l_map->ino > r_map->ino) return -1;
1012 if (l_map->ino < r_map->ino) return 1;
1013
1014 if (l_map->ino_generation > r_map->ino_generation) return -1;
1015 if (l_map->ino_generation < r_map->ino_generation) return 1;
1016
1017 /*
1018 * Addresses with no major/minor numbers are assumed to be
1019 * anonymous in userspace. Sort those on pid then address.
1020 *
1021 * The kernel and non-zero major/minor mapped areas are
1022 * assumed to be unity mapped. Sort those on address.
1023 */
1024
1025 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1026 (!(l_map->flags & MAP_SHARED)) &&
1027 !l_map->maj && !l_map->min && !l_map->ino &&
1028 !l_map->ino_generation) {
1029 /* userspace anonymous */
1030
1031 if (left->thread->pid_ > right->thread->pid_) return -1;
1032 if (left->thread->pid_ < right->thread->pid_) return 1;
1033 }
1034
1035addr:
1036 /* al_addr does all the right addr - start + offset calculations */
1037 l = cl_address(left->mem_info->daddr.al_addr);
1038 r = cl_address(right->mem_info->daddr.al_addr);
1039
1040 if (l > r) return -1;
1041 if (l < r) return 1;
1042
1043 return 0;
1044}
1045
1046static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1047 size_t size, unsigned int width)
1048{
1049
1050 uint64_t addr = 0;
1051 struct map *map = NULL;
1052 struct symbol *sym = NULL;
1053 char level = he->level;
1054
1055 if (he->mem_info) {
1056 addr = cl_address(he->mem_info->daddr.al_addr);
1057 map = he->mem_info->daddr.map;
1058 sym = he->mem_info->daddr.sym;
1059
1060 /* print [s] for shared data mmaps */
1061 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1062 map && (map->type == MAP__VARIABLE) &&
1063 (map->flags & MAP_SHARED) &&
1064 (map->maj || map->min || map->ino ||
1065 map->ino_generation))
1066 level = 's';
1067 else if (!map)
1068 level = 'X';
1069 }
1070 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1071 width);
1072}
1073
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001074struct sort_entry sort_mispredict = {
1075 .se_header = "Branch Mispredicted",
1076 .se_cmp = sort__mispredict_cmp,
1077 .se_snprintf = hist_entry__mispredict_snprintf,
1078 .se_width_idx = HISTC_MISPREDICT,
1079};
1080
Andi Kleen05484292013-01-24 16:10:29 +01001081static u64 he_weight(struct hist_entry *he)
1082{
1083 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1084}
1085
1086static int64_t
1087sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1088{
1089 return he_weight(left) - he_weight(right);
1090}
1091
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001092static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001093 size_t size, unsigned int width)
1094{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001095 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
Andi Kleen05484292013-01-24 16:10:29 +01001096}
1097
1098struct sort_entry sort_local_weight = {
1099 .se_header = "Local Weight",
1100 .se_cmp = sort__local_weight_cmp,
1101 .se_snprintf = hist_entry__local_weight_snprintf,
1102 .se_width_idx = HISTC_LOCAL_WEIGHT,
1103};
1104
1105static int64_t
1106sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1107{
1108 return left->stat.weight - right->stat.weight;
1109}
1110
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001111static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001112 size_t size, unsigned int width)
1113{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001114 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
Andi Kleen05484292013-01-24 16:10:29 +01001115}
1116
1117struct sort_entry sort_global_weight = {
1118 .se_header = "Weight",
1119 .se_cmp = sort__global_weight_cmp,
1120 .se_snprintf = hist_entry__global_weight_snprintf,
1121 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1122};
1123
Stephane Eranian98a3b322013-01-24 16:10:35 +01001124struct sort_entry sort_mem_daddr_sym = {
1125 .se_header = "Data Symbol",
1126 .se_cmp = sort__daddr_cmp,
1127 .se_snprintf = hist_entry__daddr_snprintf,
1128 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1129};
1130
Don Zickus28e6db22015-10-05 20:06:07 +02001131struct sort_entry sort_mem_iaddr_sym = {
1132 .se_header = "Code Symbol",
1133 .se_cmp = sort__iaddr_cmp,
1134 .se_snprintf = hist_entry__iaddr_snprintf,
1135 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1136};
1137
Stephane Eranian98a3b322013-01-24 16:10:35 +01001138struct sort_entry sort_mem_daddr_dso = {
1139 .se_header = "Data Object",
1140 .se_cmp = sort__dso_daddr_cmp,
1141 .se_snprintf = hist_entry__dso_daddr_snprintf,
1142 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1143};
1144
1145struct sort_entry sort_mem_locked = {
1146 .se_header = "Locked",
1147 .se_cmp = sort__locked_cmp,
1148 .se_snprintf = hist_entry__locked_snprintf,
1149 .se_width_idx = HISTC_MEM_LOCKED,
1150};
1151
1152struct sort_entry sort_mem_tlb = {
1153 .se_header = "TLB access",
1154 .se_cmp = sort__tlb_cmp,
1155 .se_snprintf = hist_entry__tlb_snprintf,
1156 .se_width_idx = HISTC_MEM_TLB,
1157};
1158
1159struct sort_entry sort_mem_lvl = {
1160 .se_header = "Memory access",
1161 .se_cmp = sort__lvl_cmp,
1162 .se_snprintf = hist_entry__lvl_snprintf,
1163 .se_width_idx = HISTC_MEM_LVL,
1164};
1165
1166struct sort_entry sort_mem_snoop = {
1167 .se_header = "Snoop",
1168 .se_cmp = sort__snoop_cmp,
1169 .se_snprintf = hist_entry__snoop_snprintf,
1170 .se_width_idx = HISTC_MEM_SNOOP,
1171};
1172
Don Zickus9b32ba72014-06-01 15:38:29 +02001173struct sort_entry sort_mem_dcacheline = {
1174 .se_header = "Data Cacheline",
1175 .se_cmp = sort__dcacheline_cmp,
1176 .se_snprintf = hist_entry__dcacheline_snprintf,
1177 .se_width_idx = HISTC_MEM_DCACHELINE,
1178};
1179
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001180static int64_t
1181sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1182{
Jiri Olsa49f47442014-10-16 16:07:01 +02001183 if (!left->branch_info || !right->branch_info)
1184 return cmp_null(left->branch_info, right->branch_info);
1185
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001186 return left->branch_info->flags.abort !=
1187 right->branch_info->flags.abort;
1188}
1189
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001190static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001191 size_t size, unsigned int width)
1192{
Jiri Olsa49f47442014-10-16 16:07:01 +02001193 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001194
Jiri Olsa49f47442014-10-16 16:07:01 +02001195 if (he->branch_info) {
1196 if (he->branch_info->flags.abort)
1197 out = "A";
1198 else
1199 out = ".";
1200 }
1201
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001202 return repsep_snprintf(bf, size, "%-*s", width, out);
1203}
1204
1205struct sort_entry sort_abort = {
1206 .se_header = "Transaction abort",
1207 .se_cmp = sort__abort_cmp,
1208 .se_snprintf = hist_entry__abort_snprintf,
1209 .se_width_idx = HISTC_ABORT,
1210};
1211
1212static int64_t
1213sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1214{
Jiri Olsa0199d242014-10-16 16:07:02 +02001215 if (!left->branch_info || !right->branch_info)
1216 return cmp_null(left->branch_info, right->branch_info);
1217
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001218 return left->branch_info->flags.in_tx !=
1219 right->branch_info->flags.in_tx;
1220}
1221
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001222static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001223 size_t size, unsigned int width)
1224{
Jiri Olsa0199d242014-10-16 16:07:02 +02001225 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001226
Jiri Olsa0199d242014-10-16 16:07:02 +02001227 if (he->branch_info) {
1228 if (he->branch_info->flags.in_tx)
1229 out = "T";
1230 else
1231 out = ".";
1232 }
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001233
1234 return repsep_snprintf(bf, size, "%-*s", width, out);
1235}
1236
1237struct sort_entry sort_in_tx = {
1238 .se_header = "Branch in transaction",
1239 .se_cmp = sort__in_tx_cmp,
1240 .se_snprintf = hist_entry__in_tx_snprintf,
1241 .se_width_idx = HISTC_IN_TX,
1242};
1243
Andi Kleen475eeab2013-09-20 07:40:43 -07001244static int64_t
1245sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1246{
1247 return left->transaction - right->transaction;
1248}
1249
1250static inline char *add_str(char *p, const char *str)
1251{
1252 strcpy(p, str);
1253 return p + strlen(str);
1254}
1255
1256static struct txbit {
1257 unsigned flag;
1258 const char *name;
1259 int skip_for_len;
1260} txbits[] = {
1261 { PERF_TXN_ELISION, "EL ", 0 },
1262 { PERF_TXN_TRANSACTION, "TX ", 1 },
1263 { PERF_TXN_SYNC, "SYNC ", 1 },
1264 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1265 { PERF_TXN_RETRY, "RETRY ", 0 },
1266 { PERF_TXN_CONFLICT, "CON ", 0 },
1267 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1268 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1269 { 0, NULL, 0 }
1270};
1271
1272int hist_entry__transaction_len(void)
1273{
1274 int i;
1275 int len = 0;
1276
1277 for (i = 0; txbits[i].name; i++) {
1278 if (!txbits[i].skip_for_len)
1279 len += strlen(txbits[i].name);
1280 }
1281 len += 4; /* :XX<space> */
1282 return len;
1283}
1284
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001285static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
Andi Kleen475eeab2013-09-20 07:40:43 -07001286 size_t size, unsigned int width)
1287{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001288 u64 t = he->transaction;
Andi Kleen475eeab2013-09-20 07:40:43 -07001289 char buf[128];
1290 char *p = buf;
1291 int i;
1292
1293 buf[0] = 0;
1294 for (i = 0; txbits[i].name; i++)
1295 if (txbits[i].flag & t)
1296 p = add_str(p, txbits[i].name);
1297 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1298 p = add_str(p, "NEITHER ");
1299 if (t & PERF_TXN_ABORT_MASK) {
1300 sprintf(p, ":%" PRIx64,
1301 (t & PERF_TXN_ABORT_MASK) >>
1302 PERF_TXN_ABORT_SHIFT);
1303 p += strlen(p);
1304 }
1305
1306 return repsep_snprintf(bf, size, "%-*s", width, buf);
1307}
1308
1309struct sort_entry sort_transaction = {
1310 .se_header = "Transaction ",
1311 .se_cmp = sort__transaction_cmp,
1312 .se_snprintf = hist_entry__transaction_snprintf,
1313 .se_width_idx = HISTC_TRANSACTION,
1314};
1315
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001316struct sort_dimension {
1317 const char *name;
1318 struct sort_entry *entry;
1319 int taken;
1320};
1321
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001322#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1323
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001324static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001325 DIM(SORT_PID, "pid", sort_thread),
1326 DIM(SORT_COMM, "comm", sort_comm),
1327 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001328 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001329 DIM(SORT_PARENT, "parent", sort_parent),
1330 DIM(SORT_CPU, "cpu", sort_cpu),
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001331 DIM(SORT_SOCKET, "socket", sort_socket),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -03001332 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Andi Kleen31191a82015-08-07 15:54:24 -07001333 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
Andi Kleenf9ea55d2013-07-18 15:58:53 -07001334 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1335 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
Andi Kleen475eeab2013-09-20 07:40:43 -07001336 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
Namhyung Kima34bb6a2015-12-23 02:07:04 +09001337 DIM(SORT_TRACE, "trace", sort_trace),
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001338};
1339
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001340#undef DIM
1341
1342#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1343
1344static struct sort_dimension bstack_sort_dimensions[] = {
1345 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1346 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1347 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1348 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1349 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001350 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1351 DIM(SORT_ABORT, "abort", sort_abort),
Andi Kleen0e332f02015-07-18 08:24:46 -07001352 DIM(SORT_CYCLES, "cycles", sort_cycles),
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001353};
1354
1355#undef DIM
1356
Namhyung Kimafab87b2013-04-03 21:26:11 +09001357#define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1358
1359static struct sort_dimension memory_sort_dimensions[] = {
Namhyung Kimafab87b2013-04-03 21:26:11 +09001360 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
Don Zickus28e6db22015-10-05 20:06:07 +02001361 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001362 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1363 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1364 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1365 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1366 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
Don Zickus9b32ba72014-06-01 15:38:29 +02001367 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001368};
1369
1370#undef DIM
1371
Namhyung Kima2ce0672014-03-04 09:06:42 +09001372struct hpp_dimension {
1373 const char *name;
1374 struct perf_hpp_fmt *fmt;
1375 int taken;
1376};
1377
1378#define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1379
1380static struct hpp_dimension hpp_sort_dimensions[] = {
1381 DIM(PERF_HPP__OVERHEAD, "overhead"),
1382 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1383 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1384 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1385 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
Namhyung Kim594dcbf2013-10-30 16:06:59 +09001386 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
Namhyung Kima2ce0672014-03-04 09:06:42 +09001387 DIM(PERF_HPP__SAMPLES, "sample"),
1388 DIM(PERF_HPP__PERIOD, "period"),
1389};
1390
1391#undef DIM
1392
Namhyung Kim8b536992014-03-03 11:46:55 +09001393struct hpp_sort_entry {
1394 struct perf_hpp_fmt hpp;
1395 struct sort_entry *se;
1396};
1397
Namhyung Kime0d66c72014-07-31 14:47:37 +09001398void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
Namhyung Kim678a5002014-03-20 11:18:54 +09001399{
1400 struct hpp_sort_entry *hse;
1401
1402 if (!perf_hpp__is_sort_entry(fmt))
1403 return;
1404
1405 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001406 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
Namhyung Kim678a5002014-03-20 11:18:54 +09001407}
1408
Namhyung Kim8b536992014-03-03 11:46:55 +09001409static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1410 struct perf_evsel *evsel)
1411{
1412 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001413 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001414
1415 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001416
Namhyung Kim5b591662014-07-31 14:47:38 +09001417 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001418 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001419
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001420 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
Namhyung Kim8b536992014-03-03 11:46:55 +09001421}
1422
1423static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1424 struct perf_hpp *hpp __maybe_unused,
1425 struct perf_evsel *evsel)
1426{
1427 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001428 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001429
1430 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1431
Namhyung Kim5b591662014-07-31 14:47:38 +09001432 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001433 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001434
1435 return len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001436}
1437
1438static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1439 struct hist_entry *he)
1440{
1441 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001442 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001443
1444 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim5b591662014-07-31 14:47:38 +09001445
1446 if (!len)
1447 len = hists__col_len(he->hists, hse->se->se_width_idx);
Namhyung Kim8b536992014-03-03 11:46:55 +09001448
1449 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1450}
1451
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001452static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1453 struct hist_entry *a, struct hist_entry *b)
1454{
1455 struct hpp_sort_entry *hse;
1456
1457 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1458 return hse->se->se_cmp(a, b);
1459}
1460
1461static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1462 struct hist_entry *a, struct hist_entry *b)
1463{
1464 struct hpp_sort_entry *hse;
1465 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1466
1467 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1468 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1469 return collapse_fn(a, b);
1470}
1471
1472static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1473 struct hist_entry *a, struct hist_entry *b)
1474{
1475 struct hpp_sort_entry *hse;
1476 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1477
1478 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1479 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1480 return sort_fn(a, b);
1481}
1482
Jiri Olsa97358082016-01-18 10:24:03 +01001483bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1484{
1485 return format->header == __sort__hpp_header;
1486}
1487
Namhyung Kim4945cf22016-03-09 22:46:57 +09001488#define MK_SORT_ENTRY_CHK(key) \
1489bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1490{ \
1491 struct hpp_sort_entry *hse; \
1492 \
1493 if (!perf_hpp__is_sort_entry(fmt)) \
1494 return false; \
1495 \
1496 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1497 return hse->se == &sort_ ## key ; \
Namhyung Kima9c6e462016-02-25 00:13:33 +09001498}
1499
Namhyung Kim4945cf22016-03-09 22:46:57 +09001500MK_SORT_ENTRY_CHK(trace)
1501MK_SORT_ENTRY_CHK(srcline)
1502MK_SORT_ENTRY_CHK(srcfile)
1503MK_SORT_ENTRY_CHK(thread)
1504MK_SORT_ENTRY_CHK(comm)
1505MK_SORT_ENTRY_CHK(dso)
1506MK_SORT_ENTRY_CHK(sym)
Namhyung Kima9c6e462016-02-25 00:13:33 +09001507
Namhyung Kima9c6e462016-02-25 00:13:33 +09001508
Jiri Olsa97358082016-01-18 10:24:03 +01001509static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1510{
1511 struct hpp_sort_entry *hse_a;
1512 struct hpp_sort_entry *hse_b;
1513
1514 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1515 return false;
1516
1517 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1518 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1519
1520 return hse_a->se == hse_b->se;
1521}
1522
Jiri Olsa564132f2016-01-18 10:24:09 +01001523static void hse_free(struct perf_hpp_fmt *fmt)
1524{
1525 struct hpp_sort_entry *hse;
1526
1527 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1528 free(hse);
1529}
1530
Namhyung Kima7d945b2014-03-04 10:46:34 +09001531static struct hpp_sort_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001532__sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
Namhyung Kim8b536992014-03-03 11:46:55 +09001533{
1534 struct hpp_sort_entry *hse;
1535
1536 hse = malloc(sizeof(*hse));
1537 if (hse == NULL) {
1538 pr_err("Memory allocation failed\n");
Namhyung Kima7d945b2014-03-04 10:46:34 +09001539 return NULL;
Namhyung Kim8b536992014-03-03 11:46:55 +09001540 }
1541
1542 hse->se = sd->entry;
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001543 hse->hpp.name = sd->entry->se_header;
Namhyung Kim8b536992014-03-03 11:46:55 +09001544 hse->hpp.header = __sort__hpp_header;
1545 hse->hpp.width = __sort__hpp_width;
1546 hse->hpp.entry = __sort__hpp_entry;
1547 hse->hpp.color = NULL;
1548
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001549 hse->hpp.cmp = __sort__hpp_cmp;
1550 hse->hpp.collapse = __sort__hpp_collapse;
1551 hse->hpp.sort = __sort__hpp_sort;
Jiri Olsa97358082016-01-18 10:24:03 +01001552 hse->hpp.equal = __sort__hpp_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001553 hse->hpp.free = hse_free;
Namhyung Kim8b536992014-03-03 11:46:55 +09001554
1555 INIT_LIST_HEAD(&hse->hpp.list);
1556 INIT_LIST_HEAD(&hse->hpp.sort_list);
Jiri Olsaf2998422014-05-23 17:15:47 +02001557 hse->hpp.elide = false;
Namhyung Kime0d66c72014-07-31 14:47:37 +09001558 hse->hpp.len = 0;
Namhyung Kim5b591662014-07-31 14:47:38 +09001559 hse->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001560 hse->hpp.level = level;
Namhyung Kim8b536992014-03-03 11:46:55 +09001561
Namhyung Kima7d945b2014-03-04 10:46:34 +09001562 return hse;
1563}
1564
Jiri Olsa564132f2016-01-18 10:24:09 +01001565static void hpp_free(struct perf_hpp_fmt *fmt)
1566{
1567 free(fmt);
1568}
1569
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001570static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1571 int level)
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001572{
1573 struct perf_hpp_fmt *fmt;
1574
1575 fmt = memdup(hd->fmt, sizeof(*fmt));
1576 if (fmt) {
1577 INIT_LIST_HEAD(&fmt->list);
1578 INIT_LIST_HEAD(&fmt->sort_list);
Jiri Olsa564132f2016-01-18 10:24:09 +01001579 fmt->free = hpp_free;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001580 fmt->level = level;
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001581 }
1582
1583 return fmt;
1584}
1585
Namhyung Kim54430102016-02-25 00:13:37 +09001586int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1587{
1588 struct perf_hpp_fmt *fmt;
1589 struct hpp_sort_entry *hse;
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001590 int ret = -1;
1591 int r;
Namhyung Kim54430102016-02-25 00:13:37 +09001592
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001593 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1594 if (!perf_hpp__is_sort_entry(fmt))
1595 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001596
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001597 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1598 if (hse->se->se_filter == NULL)
1599 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001600
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001601 /*
1602 * hist entry is filtered if any of sort key in the hpp list
1603 * is applied. But it should skip non-matched filter types.
1604 */
1605 r = hse->se->se_filter(he, type, arg);
1606 if (r >= 0) {
1607 if (ret < 0)
1608 ret = 0;
1609 ret |= r;
1610 }
1611 }
1612
1613 return ret;
Namhyung Kim54430102016-02-25 00:13:37 +09001614}
1615
Jiri Olsad7b617f2016-03-09 11:04:17 +01001616static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1617 struct perf_hpp_list *list,
1618 int level)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001619{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001620 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001621
1622 if (hse == NULL)
1623 return -1;
1624
Jiri Olsad7b617f2016-03-09 11:04:17 +01001625 perf_hpp_list__register_sort_field(list, &hse->hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001626 return 0;
1627}
1628
Jiri Olsad7b617f2016-03-09 11:04:17 +01001629static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1630 struct perf_hpp_list *list)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001631{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001632 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001633
1634 if (hse == NULL)
1635 return -1;
1636
Jiri Olsa07600022016-01-18 10:24:16 +01001637 perf_hpp_list__column_register(list, &hse->hpp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001638 return 0;
1639}
1640
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001641struct hpp_dynamic_entry {
1642 struct perf_hpp_fmt hpp;
1643 struct perf_evsel *evsel;
1644 struct format_field *field;
1645 unsigned dynamic_len;
Namhyung Kim053a3982015-12-23 02:07:05 +09001646 bool raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001647};
1648
1649static int hde_width(struct hpp_dynamic_entry *hde)
1650{
1651 if (!hde->hpp.len) {
1652 int len = hde->dynamic_len;
1653 int namelen = strlen(hde->field->name);
1654 int fieldlen = hde->field->size;
1655
1656 if (namelen > len)
1657 len = namelen;
1658
1659 if (!(hde->field->flags & FIELD_IS_STRING)) {
1660 /* length for print hex numbers */
1661 fieldlen = hde->field->size * 2 + 2;
1662 }
1663 if (fieldlen > len)
1664 len = fieldlen;
1665
1666 hde->hpp.len = len;
1667 }
1668 return hde->hpp.len;
1669}
1670
Namhyung Kim60517d22015-12-23 02:07:03 +09001671static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1672 struct hist_entry *he)
1673{
1674 char *str, *pos;
1675 struct format_field *field = hde->field;
1676 size_t namelen;
1677 bool last = false;
1678
Namhyung Kim053a3982015-12-23 02:07:05 +09001679 if (hde->raw_trace)
1680 return;
1681
Namhyung Kim60517d22015-12-23 02:07:03 +09001682 /* parse pretty print result and update max length */
1683 if (!he->trace_output)
1684 he->trace_output = get_trace_output(he);
1685
1686 namelen = strlen(field->name);
1687 str = he->trace_output;
1688
1689 while (str) {
1690 pos = strchr(str, ' ');
1691 if (pos == NULL) {
1692 last = true;
1693 pos = str + strlen(str);
1694 }
1695
1696 if (!strncmp(str, field->name, namelen)) {
1697 size_t len;
1698
1699 str += namelen + 1;
1700 len = pos - str;
1701
1702 if (len > hde->dynamic_len)
1703 hde->dynamic_len = len;
1704 break;
1705 }
1706
1707 if (last)
1708 str = NULL;
1709 else
1710 str = pos + 1;
1711 }
1712}
1713
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001714static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1715 struct perf_evsel *evsel __maybe_unused)
1716{
1717 struct hpp_dynamic_entry *hde;
1718 size_t len = fmt->user_len;
1719
1720 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1721
1722 if (!len)
1723 len = hde_width(hde);
1724
1725 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1726}
1727
1728static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1729 struct perf_hpp *hpp __maybe_unused,
1730 struct perf_evsel *evsel __maybe_unused)
1731{
1732 struct hpp_dynamic_entry *hde;
1733 size_t len = fmt->user_len;
1734
1735 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1736
1737 if (!len)
1738 len = hde_width(hde);
1739
1740 return len;
1741}
1742
Namhyung Kim361459f2015-12-23 02:07:08 +09001743bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1744{
1745 struct hpp_dynamic_entry *hde;
1746
1747 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1748
1749 return hists_to_evsel(hists) == hde->evsel;
1750}
1751
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001752static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1753 struct hist_entry *he)
1754{
1755 struct hpp_dynamic_entry *hde;
1756 size_t len = fmt->user_len;
Namhyung Kim60517d22015-12-23 02:07:03 +09001757 char *str, *pos;
1758 struct format_field *field;
1759 size_t namelen;
1760 bool last = false;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001761 int ret;
1762
1763 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1764
1765 if (!len)
1766 len = hde_width(hde);
1767
Namhyung Kim053a3982015-12-23 02:07:05 +09001768 if (hde->raw_trace)
1769 goto raw_field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001770
Namhyung Kime049d4a2016-02-27 03:52:46 +09001771 if (!he->trace_output)
1772 he->trace_output = get_trace_output(he);
1773
Namhyung Kim053a3982015-12-23 02:07:05 +09001774 field = hde->field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001775 namelen = strlen(field->name);
1776 str = he->trace_output;
1777
1778 while (str) {
1779 pos = strchr(str, ' ');
1780 if (pos == NULL) {
1781 last = true;
1782 pos = str + strlen(str);
1783 }
1784
1785 if (!strncmp(str, field->name, namelen)) {
1786 str += namelen + 1;
1787 str = strndup(str, pos - str);
1788
1789 if (str == NULL)
1790 return scnprintf(hpp->buf, hpp->size,
1791 "%*.*s", len, len, "ERROR");
1792 break;
1793 }
1794
1795 if (last)
1796 str = NULL;
1797 else
1798 str = pos + 1;
1799 }
1800
1801 if (str == NULL) {
1802 struct trace_seq seq;
Namhyung Kim053a3982015-12-23 02:07:05 +09001803raw_field:
Namhyung Kim60517d22015-12-23 02:07:03 +09001804 trace_seq_init(&seq);
1805 pevent_print_field(&seq, he->raw_data, hde->field);
1806 str = seq.buffer;
1807 }
1808
1809 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1810 free(str);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001811 return ret;
1812}
1813
1814static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1815 struct hist_entry *a, struct hist_entry *b)
1816{
1817 struct hpp_dynamic_entry *hde;
1818 struct format_field *field;
1819 unsigned offset, size;
1820
1821 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1822
Namhyung Kimabab5e72016-02-27 03:52:47 +09001823 if (b == NULL) {
1824 update_dynamic_len(hde, a);
1825 return 0;
1826 }
1827
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001828 field = hde->field;
1829 if (field->flags & FIELD_IS_DYNAMIC) {
1830 unsigned long long dyn;
1831
1832 pevent_read_number_field(field, a->raw_data, &dyn);
1833 offset = dyn & 0xffff;
1834 size = (dyn >> 16) & 0xffff;
1835
1836 /* record max width for output */
1837 if (size > hde->dynamic_len)
1838 hde->dynamic_len = size;
1839 } else {
1840 offset = field->offset;
1841 size = field->size;
1842 }
1843
1844 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1845}
1846
Namhyung Kim361459f2015-12-23 02:07:08 +09001847bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1848{
1849 return fmt->cmp == __sort__hde_cmp;
1850}
1851
Namhyung Kim665aa752016-02-21 23:22:35 +09001852static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1853{
1854 struct hpp_dynamic_entry *hde_a;
1855 struct hpp_dynamic_entry *hde_b;
1856
1857 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1858 return false;
1859
1860 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1861 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1862
1863 return hde_a->field == hde_b->field;
1864}
1865
Jiri Olsa564132f2016-01-18 10:24:09 +01001866static void hde_free(struct perf_hpp_fmt *fmt)
1867{
1868 struct hpp_dynamic_entry *hde;
1869
1870 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1871 free(hde);
1872}
1873
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001874static struct hpp_dynamic_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001875__alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1876 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001877{
1878 struct hpp_dynamic_entry *hde;
1879
1880 hde = malloc(sizeof(*hde));
1881 if (hde == NULL) {
1882 pr_debug("Memory allocation failed\n");
1883 return NULL;
1884 }
1885
1886 hde->evsel = evsel;
1887 hde->field = field;
1888 hde->dynamic_len = 0;
1889
1890 hde->hpp.name = field->name;
1891 hde->hpp.header = __sort__hde_header;
1892 hde->hpp.width = __sort__hde_width;
1893 hde->hpp.entry = __sort__hde_entry;
1894 hde->hpp.color = NULL;
1895
1896 hde->hpp.cmp = __sort__hde_cmp;
1897 hde->hpp.collapse = __sort__hde_cmp;
1898 hde->hpp.sort = __sort__hde_cmp;
Namhyung Kim665aa752016-02-21 23:22:35 +09001899 hde->hpp.equal = __sort__hde_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001900 hde->hpp.free = hde_free;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001901
1902 INIT_LIST_HEAD(&hde->hpp.list);
1903 INIT_LIST_HEAD(&hde->hpp.sort_list);
1904 hde->hpp.elide = false;
1905 hde->hpp.len = 0;
1906 hde->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001907 hde->hpp.level = level;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001908
1909 return hde;
1910}
1911
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03001912struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1913{
1914 struct perf_hpp_fmt *new_fmt = NULL;
1915
1916 if (perf_hpp__is_sort_entry(fmt)) {
1917 struct hpp_sort_entry *hse, *new_hse;
1918
1919 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1920 new_hse = memdup(hse, sizeof(*hse));
1921 if (new_hse)
1922 new_fmt = &new_hse->hpp;
1923 } else if (perf_hpp__is_dynamic_entry(fmt)) {
1924 struct hpp_dynamic_entry *hde, *new_hde;
1925
1926 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1927 new_hde = memdup(hde, sizeof(*hde));
1928 if (new_hde)
1929 new_fmt = &new_hde->hpp;
1930 } else {
1931 new_fmt = memdup(fmt, sizeof(*fmt));
1932 }
1933
1934 INIT_LIST_HEAD(&new_fmt->list);
1935 INIT_LIST_HEAD(&new_fmt->sort_list);
1936
1937 return new_fmt;
1938}
1939
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001940static int parse_field_name(char *str, char **event, char **field, char **opt)
1941{
1942 char *event_name, *field_name, *opt_name;
1943
1944 event_name = str;
1945 field_name = strchr(str, '.');
1946
1947 if (field_name) {
1948 *field_name++ = '\0';
1949 } else {
1950 event_name = NULL;
1951 field_name = str;
1952 }
1953
1954 opt_name = strchr(field_name, '/');
1955 if (opt_name)
1956 *opt_name++ = '\0';
1957
1958 *event = event_name;
1959 *field = field_name;
1960 *opt = opt_name;
1961
1962 return 0;
1963}
1964
1965/* find match evsel using a given event name. The event name can be:
Namhyung Kim9735be22016-01-05 19:58:35 +09001966 * 1. '%' + event index (e.g. '%1' for first event)
1967 * 2. full event name (e.g. sched:sched_switch)
1968 * 3. partial event name (should not contain ':')
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001969 */
1970static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1971{
1972 struct perf_evsel *evsel = NULL;
1973 struct perf_evsel *pos;
1974 bool full_name;
1975
1976 /* case 1 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001977 if (event_name[0] == '%') {
1978 int nr = strtol(event_name+1, NULL, 0);
1979
1980 if (nr > evlist->nr_entries)
1981 return NULL;
1982
1983 evsel = perf_evlist__first(evlist);
1984 while (--nr > 0)
1985 evsel = perf_evsel__next(evsel);
1986
1987 return evsel;
1988 }
1989
1990 full_name = !!strchr(event_name, ':');
1991 evlist__for_each(evlist, pos) {
Namhyung Kim9735be22016-01-05 19:58:35 +09001992 /* case 2 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001993 if (full_name && !strcmp(pos->name, event_name))
1994 return pos;
Namhyung Kim9735be22016-01-05 19:58:35 +09001995 /* case 3 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001996 if (!full_name && strstr(pos->name, event_name)) {
1997 if (evsel) {
1998 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
1999 event_name, evsel->name, pos->name);
2000 return NULL;
2001 }
2002 evsel = pos;
2003 }
2004 }
2005
2006 return evsel;
2007}
2008
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002009static int __dynamic_dimension__add(struct perf_evsel *evsel,
2010 struct format_field *field,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002011 bool raw_trace, int level)
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002012{
2013 struct hpp_dynamic_entry *hde;
2014
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002015 hde = __alloc_dynamic_entry(evsel, field, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002016 if (hde == NULL)
2017 return -ENOMEM;
2018
2019 hde->raw_trace = raw_trace;
2020
2021 perf_hpp__register_sort_field(&hde->hpp);
2022 return 0;
2023}
2024
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002025static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002026{
2027 int ret;
2028 struct format_field *field;
2029
2030 field = evsel->tp_format->format.fields;
2031 while (field) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002032 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002033 if (ret < 0)
2034 return ret;
2035
2036 field = field->next;
2037 }
2038 return 0;
2039}
2040
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002041static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2042 int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002043{
2044 int ret;
2045 struct perf_evsel *evsel;
2046
2047 evlist__for_each(evlist, evsel) {
2048 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2049 continue;
2050
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002051 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002052 if (ret < 0)
2053 return ret;
2054 }
2055 return 0;
2056}
2057
Namhyung Kim9735be22016-01-05 19:58:35 +09002058static int add_all_matching_fields(struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002059 char *field_name, bool raw_trace, int level)
Namhyung Kim9735be22016-01-05 19:58:35 +09002060{
2061 int ret = -ESRCH;
2062 struct perf_evsel *evsel;
2063 struct format_field *field;
2064
2065 evlist__for_each(evlist, evsel) {
2066 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2067 continue;
2068
2069 field = pevent_find_any_field(evsel->tp_format, field_name);
2070 if (field == NULL)
2071 continue;
2072
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002073 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002074 if (ret < 0)
2075 break;
2076 }
2077 return ret;
2078}
2079
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002080static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2081 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002082{
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002083 char *str, *event_name, *field_name, *opt_name;
2084 struct perf_evsel *evsel;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002085 struct format_field *field;
Namhyung Kim053a3982015-12-23 02:07:05 +09002086 bool raw_trace = symbol_conf.raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002087 int ret = 0;
2088
2089 if (evlist == NULL)
2090 return -ENOENT;
2091
2092 str = strdup(tok);
2093 if (str == NULL)
2094 return -ENOMEM;
2095
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002096 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002097 ret = -EINVAL;
2098 goto out;
2099 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002100
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002101 if (opt_name) {
2102 if (strcmp(opt_name, "raw")) {
2103 pr_debug("unsupported field option %s\n", opt_name);
Namhyung Kim053a3982015-12-23 02:07:05 +09002104 ret = -EINVAL;
2105 goto out;
2106 }
2107 raw_trace = true;
2108 }
2109
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002110 if (!strcmp(field_name, "trace_fields")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002111 ret = add_all_dynamic_fields(evlist, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002112 goto out;
2113 }
2114
Namhyung Kim9735be22016-01-05 19:58:35 +09002115 if (event_name == NULL) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002116 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002117 goto out;
2118 }
2119
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002120 evsel = find_evsel(evlist, event_name);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002121 if (evsel == NULL) {
2122 pr_debug("Cannot find event: %s\n", event_name);
2123 ret = -ENOENT;
2124 goto out;
2125 }
2126
2127 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2128 pr_debug("%s is not a tracepoint event\n", event_name);
2129 ret = -EINVAL;
2130 goto out;
2131 }
2132
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002133 if (!strcmp(field_name, "*")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002134 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002135 } else {
2136 field = pevent_find_any_field(evsel->tp_format, field_name);
2137 if (field == NULL) {
2138 pr_debug("Cannot find event field for %s.%s\n",
2139 event_name, field_name);
2140 return -ENOENT;
2141 }
2142
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002143 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002144 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002145
2146out:
2147 free(str);
2148 return ret;
2149}
2150
Jiri Olsad7b617f2016-03-09 11:04:17 +01002151static int __sort_dimension__add(struct sort_dimension *sd,
2152 struct perf_hpp_list *list,
2153 int level)
Namhyung Kim2f532d092013-04-03 21:26:10 +09002154{
2155 if (sd->taken)
Namhyung Kim8b536992014-03-03 11:46:55 +09002156 return 0;
2157
Jiri Olsad7b617f2016-03-09 11:04:17 +01002158 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
Namhyung Kim8b536992014-03-03 11:46:55 +09002159 return -1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002160
2161 if (sd->entry->se_collapse)
Jiri Olsa52225032016-05-03 13:54:42 +02002162 list->need_collapse = 1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002163
Namhyung Kim2f532d092013-04-03 21:26:10 +09002164 sd->taken = 1;
Namhyung Kim8b536992014-03-03 11:46:55 +09002165
2166 return 0;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002167}
2168
Jiri Olsad7b617f2016-03-09 11:04:17 +01002169static int __hpp_dimension__add(struct hpp_dimension *hd,
2170 struct perf_hpp_list *list,
2171 int level)
Namhyung Kima2ce0672014-03-04 09:06:42 +09002172{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002173 struct perf_hpp_fmt *fmt;
Namhyung Kima2ce0672014-03-04 09:06:42 +09002174
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002175 if (hd->taken)
2176 return 0;
2177
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002178 fmt = __hpp_dimension__alloc_hpp(hd, level);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002179 if (!fmt)
2180 return -1;
2181
2182 hd->taken = 1;
Jiri Olsad7b617f2016-03-09 11:04:17 +01002183 perf_hpp_list__register_sort_field(list, fmt);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002184 return 0;
2185}
2186
Jiri Olsa07600022016-01-18 10:24:16 +01002187static int __sort_dimension__add_output(struct perf_hpp_list *list,
2188 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002189{
2190 if (sd->taken)
2191 return 0;
2192
Jiri Olsad7b617f2016-03-09 11:04:17 +01002193 if (__sort_dimension__add_hpp_output(sd, list) < 0)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002194 return -1;
2195
2196 sd->taken = 1;
2197 return 0;
2198}
2199
Jiri Olsa07600022016-01-18 10:24:16 +01002200static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2201 struct hpp_dimension *hd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002202{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002203 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002204
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002205 if (hd->taken)
2206 return 0;
2207
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002208 fmt = __hpp_dimension__alloc_hpp(hd, 0);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002209 if (!fmt)
2210 return -1;
2211
2212 hd->taken = 1;
Jiri Olsa07600022016-01-18 10:24:16 +01002213 perf_hpp_list__column_register(list, fmt);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002214 return 0;
2215}
2216
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002217int hpp_dimension__add_output(unsigned col)
2218{
2219 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa07600022016-01-18 10:24:16 +01002220 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002221}
2222
Jiri Olsad7b617f2016-03-09 11:04:17 +01002223static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -03002224 struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002225 int level)
John Kacurdd68ada2009-09-24 18:02:49 +02002226{
2227 unsigned int i;
2228
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002229 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2230 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +02002231
John Kacurdd68ada2009-09-24 18:02:49 +02002232 if (strncasecmp(tok, sd->name, strlen(tok)))
2233 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002234
John Kacurdd68ada2009-09-24 18:02:49 +02002235 if (sd->entry == &sort_parent) {
2236 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2237 if (ret) {
2238 char err[BUFSIZ];
2239
2240 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -03002241 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2242 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +02002243 }
Jiri Olsade7e6a72016-05-03 13:54:43 +02002244 list->parent = 1;
Namhyung Kim930477b2013-04-05 10:26:36 +09002245 } else if (sd->entry == &sort_sym) {
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002246 list->sym = 1;
Kan Liang94ba4622015-02-09 05:39:44 +00002247 /*
2248 * perf diff displays the performance difference amongst
2249 * two or more perf.data files. Those files could come
2250 * from different binaries. So we should not compare
2251 * their ips, but the name of symbol.
2252 */
2253 if (sort__mode == SORT_MODE__DIFF)
2254 sd->entry->se_collapse = sort__sym_sort;
2255
Namhyung Kim68f6d022013-12-18 14:21:10 +09002256 } else if (sd->entry == &sort_dso) {
Jiri Olsa69849fc2016-05-03 13:54:45 +02002257 list->dso = 1;
Kan Liang2e7ea3a2015-09-04 10:45:43 -04002258 } else if (sd->entry == &sort_socket) {
2259 sort__has_socket = 1;
Namhyung Kimcfd92da2016-01-21 19:13:24 -03002260 } else if (sd->entry == &sort_thread) {
2261 sort__has_thread = 1;
Namhyung Kim078b8d42016-03-09 23:20:51 +09002262 } else if (sd->entry == &sort_comm) {
2263 sort__has_comm = 1;
John Kacurdd68ada2009-09-24 18:02:49 +02002264 }
2265
Jiri Olsad7b617f2016-03-09 11:04:17 +01002266 return __sort_dimension__add(sd, list, level);
John Kacurdd68ada2009-09-24 18:02:49 +02002267 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002268
Namhyung Kima2ce0672014-03-04 09:06:42 +09002269 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2270 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2271
2272 if (strncasecmp(tok, hd->name, strlen(tok)))
2273 continue;
2274
Jiri Olsad7b617f2016-03-09 11:04:17 +01002275 return __hpp_dimension__add(hd, list, level);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002276 }
2277
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002278 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2279 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2280
2281 if (strncasecmp(tok, sd->name, strlen(tok)))
2282 continue;
2283
Namhyung Kim55369fc2013-04-01 20:35:20 +09002284 if (sort__mode != SORT_MODE__BRANCH)
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002285 return -EINVAL;
2286
2287 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002288 list->sym = 1;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002289
Jiri Olsad7b617f2016-03-09 11:04:17 +01002290 __sort_dimension__add(sd, list, level);
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002291 return 0;
2292 }
2293
Namhyung Kimafab87b2013-04-03 21:26:11 +09002294 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2295 struct sort_dimension *sd = &memory_sort_dimensions[i];
2296
2297 if (strncasecmp(tok, sd->name, strlen(tok)))
2298 continue;
2299
2300 if (sort__mode != SORT_MODE__MEMORY)
2301 return -EINVAL;
2302
2303 if (sd->entry == &sort_mem_daddr_sym)
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002304 list->sym = 1;
Namhyung Kimafab87b2013-04-03 21:26:11 +09002305
Jiri Olsad7b617f2016-03-09 11:04:17 +01002306 __sort_dimension__add(sd, list, level);
Namhyung Kimafab87b2013-04-03 21:26:11 +09002307 return 0;
2308 }
2309
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002310 if (!add_dynamic_entry(evlist, tok, level))
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002311 return 0;
2312
John Kacurdd68ada2009-09-24 18:02:49 +02002313 return -ESRCH;
2314}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002315
Jiri Olsad7b617f2016-03-09 11:04:17 +01002316static int setup_sort_list(struct perf_hpp_list *list, char *str,
2317 struct perf_evlist *evlist)
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002318{
2319 char *tmp, *tok;
2320 int ret = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002321 int level = 0;
Namhyung Kima23f37e2016-03-07 16:44:47 -03002322 int next_level = 1;
2323 bool in_group = false;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002324
Namhyung Kima23f37e2016-03-07 16:44:47 -03002325 do {
2326 tok = str;
2327 tmp = strpbrk(str, "{}, ");
2328 if (tmp) {
2329 if (in_group)
2330 next_level = level;
2331 else
2332 next_level = level + 1;
2333
2334 if (*tmp == '{')
2335 in_group = true;
2336 else if (*tmp == '}')
2337 in_group = false;
2338
2339 *tmp = '\0';
2340 str = tmp + 1;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002341 }
Namhyung Kima23f37e2016-03-07 16:44:47 -03002342
2343 if (*tok) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002344 ret = sort_dimension__add(list, tok, evlist, level);
Namhyung Kima23f37e2016-03-07 16:44:47 -03002345 if (ret == -EINVAL) {
2346 error("Invalid --sort key: `%s'", tok);
2347 break;
2348 } else if (ret == -ESRCH) {
2349 error("Unknown --sort key: `%s'", tok);
2350 break;
2351 }
2352 }
2353
2354 level = next_level;
2355 } while (tmp);
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002356
2357 return ret;
2358}
2359
Namhyung Kimd49dade2015-12-23 02:07:10 +09002360static const char *get_default_sort_order(struct perf_evlist *evlist)
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002361{
2362 const char *default_sort_orders[] = {
2363 default_sort_order,
2364 default_branch_sort_order,
2365 default_mem_sort_order,
2366 default_top_sort_order,
2367 default_diff_sort_order,
Namhyung Kimd49dade2015-12-23 02:07:10 +09002368 default_tracepoint_sort_order,
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002369 };
Namhyung Kimd49dade2015-12-23 02:07:10 +09002370 bool use_trace = true;
2371 struct perf_evsel *evsel;
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002372
2373 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2374
Namhyung Kimd49dade2015-12-23 02:07:10 +09002375 if (evlist == NULL)
2376 goto out_no_evlist;
2377
2378 evlist__for_each(evlist, evsel) {
2379 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2380 use_trace = false;
2381 break;
2382 }
2383 }
2384
2385 if (use_trace) {
2386 sort__mode = SORT_MODE__TRACEPOINT;
2387 if (symbol_conf.raw_trace)
2388 return "trace_fields";
2389 }
2390out_no_evlist:
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002391 return default_sort_orders[sort__mode];
2392}
2393
Namhyung Kimd49dade2015-12-23 02:07:10 +09002394static int setup_sort_order(struct perf_evlist *evlist)
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002395{
2396 char *new_sort_order;
2397
2398 /*
2399 * Append '+'-prefixed sort order to the default sort
2400 * order string.
2401 */
2402 if (!sort_order || is_strict_order(sort_order))
2403 return 0;
2404
2405 if (sort_order[1] == '\0') {
2406 error("Invalid --sort key: `+'");
2407 return -EINVAL;
2408 }
2409
2410 /*
2411 * We allocate new sort_order string, but we never free it,
2412 * because it's checked over the rest of the code.
2413 */
2414 if (asprintf(&new_sort_order, "%s,%s",
Namhyung Kimd49dade2015-12-23 02:07:10 +09002415 get_default_sort_order(evlist), sort_order + 1) < 0) {
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002416 error("Not enough memory to set up --sort");
2417 return -ENOMEM;
2418 }
2419
2420 sort_order = new_sort_order;
2421 return 0;
2422}
2423
Jiri Olsab97511c2016-01-07 10:14:08 +01002424/*
2425 * Adds 'pre,' prefix into 'str' is 'pre' is
2426 * not already part of 'str'.
2427 */
2428static char *prefix_if_not_in(const char *pre, char *str)
2429{
2430 char *n;
2431
2432 if (!str || strstr(str, pre))
2433 return str;
2434
2435 if (asprintf(&n, "%s,%s", pre, str) < 0)
2436 return NULL;
2437
2438 free(str);
2439 return n;
2440}
2441
2442static char *setup_overhead(char *keys)
2443{
2444 keys = prefix_if_not_in("overhead", keys);
2445
2446 if (symbol_conf.cumulate_callchain)
2447 keys = prefix_if_not_in("overhead_children", keys);
2448
2449 return keys;
2450}
2451
Namhyung Kim40184c42015-12-23 02:07:01 +09002452static int __setup_sorting(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002453{
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002454 char *str;
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002455 const char *sort_keys;
Namhyung Kim55309982013-02-06 14:57:16 +09002456 int ret = 0;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002457
Namhyung Kimd49dade2015-12-23 02:07:10 +09002458 ret = setup_sort_order(evlist);
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002459 if (ret)
2460 return ret;
2461
2462 sort_keys = sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002463 if (sort_keys == NULL) {
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002464 if (is_strict_order(field_order)) {
Namhyung Kima7d945b2014-03-04 10:46:34 +09002465 /*
2466 * If user specified field order but no sort order,
2467 * we'll honor it and not add default sort orders.
2468 */
2469 return 0;
2470 }
2471
Namhyung Kimd49dade2015-12-23 02:07:10 +09002472 sort_keys = get_default_sort_order(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002473 }
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002474
2475 str = strdup(sort_keys);
Namhyung Kim5936f542013-02-06 14:57:17 +09002476 if (str == NULL) {
2477 error("Not enough memory to setup sort keys");
2478 return -ENOMEM;
2479 }
2480
Jiri Olsab97511c2016-01-07 10:14:08 +01002481 /*
2482 * Prepend overhead fields for backward compatibility.
2483 */
2484 if (!is_strict_order(field_order)) {
2485 str = setup_overhead(str);
2486 if (str == NULL) {
2487 error("Not enough memory to setup overhead keys");
2488 return -ENOMEM;
2489 }
2490 }
2491
Jiri Olsad7b617f2016-03-09 11:04:17 +01002492 ret = setup_sort_list(&perf_hpp_list, str, evlist);
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002493
2494 free(str);
Namhyung Kim55309982013-02-06 14:57:16 +09002495 return ret;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002496}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002497
Jiri Olsaf2998422014-05-23 17:15:47 +02002498void perf_hpp__set_elide(int idx, bool elide)
Namhyung Kime67d49a2014-03-18 13:00:59 +09002499{
Jiri Olsaf2998422014-05-23 17:15:47 +02002500 struct perf_hpp_fmt *fmt;
2501 struct hpp_sort_entry *hse;
Namhyung Kime67d49a2014-03-18 13:00:59 +09002502
Jiri Olsacf094042016-01-18 10:24:17 +01002503 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002504 if (!perf_hpp__is_sort_entry(fmt))
2505 continue;
2506
2507 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2508 if (hse->se->se_width_idx == idx) {
2509 fmt->elide = elide;
2510 break;
2511 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002512 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002513}
2514
Jiri Olsaf2998422014-05-23 17:15:47 +02002515static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002516{
2517 if (list && strlist__nr_entries(list) == 1) {
2518 if (fp != NULL)
2519 fprintf(fp, "# %s: %s\n", list_name,
2520 strlist__entry(list, 0)->s);
Jiri Olsaf2998422014-05-23 17:15:47 +02002521 return true;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002522 }
Jiri Olsaf2998422014-05-23 17:15:47 +02002523 return false;
2524}
2525
2526static bool get_elide(int idx, FILE *output)
2527{
2528 switch (idx) {
2529 case HISTC_SYMBOL:
2530 return __get_elide(symbol_conf.sym_list, "symbol", output);
2531 case HISTC_DSO:
2532 return __get_elide(symbol_conf.dso_list, "dso", output);
2533 case HISTC_COMM:
2534 return __get_elide(symbol_conf.comm_list, "comm", output);
2535 default:
2536 break;
2537 }
2538
2539 if (sort__mode != SORT_MODE__BRANCH)
2540 return false;
2541
2542 switch (idx) {
2543 case HISTC_SYMBOL_FROM:
2544 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2545 case HISTC_SYMBOL_TO:
2546 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2547 case HISTC_DSO_FROM:
2548 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2549 case HISTC_DSO_TO:
2550 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2551 default:
2552 break;
2553 }
2554
2555 return false;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002556}
Namhyung Kim08e71542013-04-03 21:26:19 +09002557
2558void sort__setup_elide(FILE *output)
2559{
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002560 struct perf_hpp_fmt *fmt;
2561 struct hpp_sort_entry *hse;
Namhyung Kim7524f632013-11-08 17:53:42 +09002562
Jiri Olsacf094042016-01-18 10:24:17 +01002563 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002564 if (!perf_hpp__is_sort_entry(fmt))
2565 continue;
Namhyung Kim08e71542013-04-03 21:26:19 +09002566
Jiri Olsaf2998422014-05-23 17:15:47 +02002567 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2568 fmt->elide = get_elide(hse->se->se_width_idx, output);
Namhyung Kim08e71542013-04-03 21:26:19 +09002569 }
2570
Namhyung Kim7524f632013-11-08 17:53:42 +09002571 /*
2572 * It makes no sense to elide all of sort entries.
2573 * Just revert them to show up again.
2574 */
Jiri Olsacf094042016-01-18 10:24:17 +01002575 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002576 if (!perf_hpp__is_sort_entry(fmt))
2577 continue;
2578
Jiri Olsaf2998422014-05-23 17:15:47 +02002579 if (!fmt->elide)
Namhyung Kim7524f632013-11-08 17:53:42 +09002580 return;
2581 }
2582
Jiri Olsacf094042016-01-18 10:24:17 +01002583 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002584 if (!perf_hpp__is_sort_entry(fmt))
2585 continue;
2586
Jiri Olsaf2998422014-05-23 17:15:47 +02002587 fmt->elide = false;
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002588 }
Namhyung Kim08e71542013-04-03 21:26:19 +09002589}
Namhyung Kima7d945b2014-03-04 10:46:34 +09002590
Jiri Olsa07600022016-01-18 10:24:16 +01002591static int output_field_add(struct perf_hpp_list *list, char *tok)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002592{
2593 unsigned int i;
2594
2595 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2596 struct sort_dimension *sd = &common_sort_dimensions[i];
2597
2598 if (strncasecmp(tok, sd->name, strlen(tok)))
2599 continue;
2600
Jiri Olsa07600022016-01-18 10:24:16 +01002601 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002602 }
2603
2604 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2605 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2606
2607 if (strncasecmp(tok, hd->name, strlen(tok)))
2608 continue;
2609
Jiri Olsa07600022016-01-18 10:24:16 +01002610 return __hpp_dimension__add_output(list, hd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002611 }
2612
2613 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2614 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2615
2616 if (strncasecmp(tok, sd->name, strlen(tok)))
2617 continue;
2618
Jiri Olsa07600022016-01-18 10:24:16 +01002619 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002620 }
2621
2622 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2623 struct sort_dimension *sd = &memory_sort_dimensions[i];
2624
2625 if (strncasecmp(tok, sd->name, strlen(tok)))
2626 continue;
2627
Jiri Olsa07600022016-01-18 10:24:16 +01002628 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002629 }
2630
2631 return -ESRCH;
2632}
2633
Jiri Olsa07600022016-01-18 10:24:16 +01002634static int setup_output_list(struct perf_hpp_list *list, char *str)
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002635{
2636 char *tmp, *tok;
2637 int ret = 0;
2638
2639 for (tok = strtok_r(str, ", ", &tmp);
2640 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Jiri Olsa07600022016-01-18 10:24:16 +01002641 ret = output_field_add(list, tok);
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002642 if (ret == -EINVAL) {
2643 error("Invalid --fields key: `%s'", tok);
2644 break;
2645 } else if (ret == -ESRCH) {
2646 error("Unknown --fields key: `%s'", tok);
2647 break;
2648 }
2649 }
2650
2651 return ret;
2652}
2653
Namhyung Kima7d945b2014-03-04 10:46:34 +09002654static void reset_dimensions(void)
2655{
2656 unsigned int i;
2657
2658 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2659 common_sort_dimensions[i].taken = 0;
2660
2661 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2662 hpp_sort_dimensions[i].taken = 0;
2663
2664 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2665 bstack_sort_dimensions[i].taken = 0;
2666
2667 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2668 memory_sort_dimensions[i].taken = 0;
2669}
2670
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002671bool is_strict_order(const char *order)
2672{
2673 return order && (*order != '+');
2674}
2675
Namhyung Kima7d945b2014-03-04 10:46:34 +09002676static int __setup_output_field(void)
2677{
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002678 char *str, *strp;
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002679 int ret = -EINVAL;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002680
2681 if (field_order == NULL)
2682 return 0;
2683
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002684 strp = str = strdup(field_order);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002685 if (str == NULL) {
2686 error("Not enough memory to setup output fields");
2687 return -ENOMEM;
2688 }
2689
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002690 if (!is_strict_order(field_order))
2691 strp++;
2692
2693 if (!strlen(strp)) {
2694 error("Invalid --fields key: `+'");
2695 goto out;
2696 }
2697
Jiri Olsa07600022016-01-18 10:24:16 +01002698 ret = setup_output_list(&perf_hpp_list, strp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002699
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002700out:
Namhyung Kima7d945b2014-03-04 10:46:34 +09002701 free(str);
2702 return ret;
2703}
2704
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002705int setup_sorting(struct perf_evlist *evlist)
2706{
2707 int err;
2708
2709 err = __setup_sorting(evlist);
2710 if (err < 0)
2711 return err;
2712
2713 if (parent_pattern != default_parent_pattern) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002714 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002715 if (err < 0)
2716 return err;
2717 }
2718
Namhyung Kima7d945b2014-03-04 10:46:34 +09002719 reset_dimensions();
2720
2721 /*
2722 * perf diff doesn't use default hpp output fields.
2723 */
2724 if (sort__mode != SORT_MODE__DIFF)
2725 perf_hpp__init();
2726
2727 err = __setup_output_field();
2728 if (err < 0)
2729 return err;
2730
2731 /* copy sort keys to output fields */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002732 perf_hpp__setup_output_field(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002733 /* and then copy output fields to sort keys */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002734 perf_hpp__append_sort_keys(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002735
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002736 /* setup hists-specific output fields */
2737 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2738 return -1;
2739
Namhyung Kima7d945b2014-03-04 10:46:34 +09002740 return 0;
2741}
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002742
2743void reset_output_field(void)
2744{
Jiri Olsa52225032016-05-03 13:54:42 +02002745 perf_hpp_list.need_collapse = 0;
Jiri Olsade7e6a72016-05-03 13:54:43 +02002746 perf_hpp_list.parent = 0;
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002747 perf_hpp_list.sym = 0;
Jiri Olsa69849fc2016-05-03 13:54:45 +02002748 perf_hpp_list.dso = 0;
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002749
Namhyung Kimd69b2962014-05-23 10:59:01 +09002750 field_order = NULL;
2751 sort_order = NULL;
2752
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002753 reset_dimensions();
Jiri Olsa43e0a682016-01-18 10:24:21 +01002754 perf_hpp__reset_output_field(&perf_hpp_list);
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002755}