blob: 75b33d387f50d088840e2ed1596e564906ec28e6 [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;
Namhyung Kim1af556402012-09-14 17:35:27 +090024int sort__has_sym = 0;
Namhyung Kim68f6d022013-12-18 14:21:10 +090025int sort__has_dso = 0;
Kan Liang2e7ea3a2015-09-04 10:45:43 -040026int sort__has_socket = 0;
Namhyung Kimcfd92da2016-01-21 19:13:24 -030027int sort__has_thread = 0;
Namhyung Kim078b8d42016-03-09 23:20:51 +090028int sort__has_comm = 0;
Namhyung Kim55369fc2013-04-01 20:35:20 +090029enum sort_mode sort__mode = SORT_MODE__NORMAL;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020030
Arnaldo Carvalho de Melo37d9bb52016-02-12 11:27:51 -030031/*
32 * Replaces all occurrences of a char used with the:
33 *
34 * -t, --field-separator
35 *
36 * option, that uses a special separator character and don't pad with spaces,
37 * replacing all occurances of this separator in symbol names (and other
38 * output) with a '.' character, that thus it's the only non valid separator.
39*/
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030040static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020041{
42 int n;
43 va_list ap;
44
45 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030046 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020047 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030048 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020049
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030050 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020051 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030052 if (sep == NULL)
53 break;
54 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020055 }
John Kacurdd68ada2009-09-24 18:02:49 +020056 }
57 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110058
59 if (n >= (int)size)
60 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020061 return n;
62}
63
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020064static int64_t cmp_null(const void *l, const void *r)
Frederic Weisbecker872a8782011-06-29 03:14:52 +020065{
66 if (!l && !r)
67 return 0;
68 else if (!l)
69 return -1;
70 else
71 return 1;
72}
73
74/* --sort pid */
75
76static int64_t
77sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
78{
Adrian Hunter38051232013-07-04 16:20:31 +030079 return right->thread->tid - left->thread->tid;
Frederic Weisbecker872a8782011-06-29 03:14:52 +020080}
81
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030082static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030083 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020084{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020085 const char *comm = thread__comm_str(he->thread);
Namhyung Kim5b591662014-07-31 14:47:38 +090086
87 width = max(7U, width) - 6;
88 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
89 width, width, comm ?: "");
John Kacurdd68ada2009-09-24 18:02:49 +020090}
91
Namhyung Kim54430102016-02-25 00:13:37 +090092static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
93{
94 const struct thread *th = arg;
95
96 if (type != HIST_FILTER__THREAD)
97 return -1;
98
99 return th && he->thread != th;
100}
101
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200102struct sort_entry sort_thread = {
Namhyung Kim8246de82014-07-31 14:47:35 +0900103 .se_header = " Pid:Command",
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200104 .se_cmp = sort__thread_cmp,
105 .se_snprintf = hist_entry__thread_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900106 .se_filter = hist_entry__thread_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200107 .se_width_idx = HISTC_THREAD,
108};
109
110/* --sort comm */
111
112static int64_t
113sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
114{
Frederic Weisbeckerfedd63d2013-09-11 17:18:09 +0200115 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200116 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200117}
118
119static int64_t
120sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
121{
Namhyung Kim4dfced32013-09-13 16:28:57 +0900122 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200123 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200124}
125
Namhyung Kim202e7a62014-03-04 11:01:41 +0900126static int64_t
127sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
128{
129 return strcmp(comm__str(right->comm), comm__str(left->comm));
130}
131
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300132static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300133 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200134{
Namhyung Kim5b591662014-07-31 14:47:38 +0900135 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
John Kacurdd68ada2009-09-24 18:02:49 +0200136}
137
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900138struct sort_entry sort_comm = {
139 .se_header = "Command",
140 .se_cmp = sort__comm_cmp,
141 .se_collapse = sort__comm_collapse,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900142 .se_sort = sort__comm_sort,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900143 .se_snprintf = hist_entry__comm_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900144 .se_filter = hist_entry__thread_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900145 .se_width_idx = HISTC_COMM,
146};
147
148/* --sort dso */
149
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100150static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200151{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100152 struct dso *dso_l = map_l ? map_l->dso : NULL;
153 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300154 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200155
156 if (!dso_l || !dso_r)
Namhyung Kim202e7a62014-03-04 11:01:41 +0900157 return cmp_null(dso_r, dso_l);
John Kacurdd68ada2009-09-24 18:02:49 +0200158
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300159 if (verbose) {
160 dso_name_l = dso_l->long_name;
161 dso_name_r = dso_r->long_name;
162 } else {
163 dso_name_l = dso_l->short_name;
164 dso_name_r = dso_r->short_name;
165 }
166
167 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200168}
169
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100170static int64_t
171sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200172{
Namhyung Kim202e7a62014-03-04 11:01:41 +0900173 return _sort__dso_cmp(right->ms.map, left->ms.map);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100174}
175
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100176static int _hist_entry__dso_snprintf(struct map *map, char *bf,
177 size_t size, unsigned int width)
178{
179 if (map && map->dso) {
180 const char *dso_name = !verbose ? map->dso->short_name :
181 map->dso->long_name;
Namhyung Kim5b591662014-07-31 14:47:38 +0900182 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300183 }
John Kacurdd68ada2009-09-24 18:02:49 +0200184
Namhyung Kim5b591662014-07-31 14:47:38 +0900185 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200186}
187
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300188static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100189 size_t size, unsigned int width)
190{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300191 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100192}
193
Namhyung Kim54430102016-02-25 00:13:37 +0900194static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
195{
196 const struct dso *dso = arg;
197
198 if (type != HIST_FILTER__DSO)
199 return -1;
200
201 return dso && (!he->ms.map || he->ms.map->dso != dso);
202}
203
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900204struct sort_entry sort_dso = {
205 .se_header = "Shared Object",
206 .se_cmp = sort__dso_cmp,
207 .se_snprintf = hist_entry__dso_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900208 .se_filter = hist_entry__dso_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900209 .se_width_idx = HISTC_DSO,
210};
211
212/* --sort symbol */
213
Namhyung Kim2037be52013-12-18 14:21:09 +0900214static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
215{
216 return (int64_t)(right_ip - left_ip);
217}
218
Namhyung Kim51f27d12013-02-06 14:57:15 +0900219static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900220{
221 if (!sym_l || !sym_r)
222 return cmp_null(sym_l, sym_r);
223
224 if (sym_l == sym_r)
225 return 0;
226
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700227 if (sym_l->start != sym_r->start)
228 return (int64_t)(sym_r->start - sym_l->start);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900229
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700230 return (int64_t)(sym_r->end - sym_l->end);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900231}
232
233static int64_t
234sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
235{
Namhyung Kim09600e02013-10-15 11:01:56 +0900236 int64_t ret;
237
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900238 if (!left->ms.sym && !right->ms.sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900239 return _sort__addr_cmp(left->ip, right->ip);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900240
Namhyung Kim09600e02013-10-15 11:01:56 +0900241 /*
242 * comparing symbol address alone is not enough since it's a
243 * relative address within a dso.
244 */
Namhyung Kim68f6d022013-12-18 14:21:10 +0900245 if (!sort__has_dso) {
246 ret = sort__dso_cmp(left, right);
247 if (ret != 0)
248 return ret;
249 }
Namhyung Kim09600e02013-10-15 11:01:56 +0900250
Namhyung Kim51f27d12013-02-06 14:57:15 +0900251 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900252}
253
Namhyung Kim202e7a62014-03-04 11:01:41 +0900254static int64_t
255sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
256{
257 if (!left->ms.sym || !right->ms.sym)
258 return cmp_null(left->ms.sym, right->ms.sym);
259
260 return strcmp(right->ms.sym->name, left->ms.sym->name);
261}
262
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100263static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
264 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900265 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100266{
267 size_t ret = 0;
268
269 if (verbose) {
270 char o = map ? dso__symtab_origin(map->dso) : '!';
271 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Namhyung Kimded19d52013-04-01 20:35:19 +0900272 BITS_PER_LONG / 4 + 2, ip, o);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100273 }
274
275 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100276 if (sym && map) {
277 if (map->type == MAP__VARIABLE) {
278 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
279 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
Namhyung Kim62667742013-01-24 16:10:42 +0100280 ip - map->unmap_ip(map, sym->start));
Stephane Eranian98a3b322013-01-24 16:10:35 +0100281 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300282 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
Stephane Eranian98a3b322013-01-24 16:10:35 +0100283 width - ret,
284 sym->name);
285 }
286 } else {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100287 size_t len = BITS_PER_LONG / 4;
288 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
289 len, ip);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100290 }
291
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300292 return ret;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100293}
294
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300295static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900296 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100297{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300298 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
299 he->level, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100300}
John Kacurdd68ada2009-09-24 18:02:49 +0200301
Namhyung Kim54430102016-02-25 00:13:37 +0900302static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
303{
304 const char *sym = arg;
305
306 if (type != HIST_FILTER__SYMBOL)
307 return -1;
308
309 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
310}
311
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200312struct sort_entry sort_sym = {
313 .se_header = "Symbol",
314 .se_cmp = sort__sym_cmp,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900315 .se_sort = sort__sym_sort,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200316 .se_snprintf = hist_entry__sym_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900317 .se_filter = hist_entry__sym_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200318 .se_width_idx = HISTC_SYMBOL,
319};
John Kacurdd68ada2009-09-24 18:02:49 +0200320
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300321/* --sort srcline */
322
Namhyung Kimcecaec62016-02-22 09:31:51 +0900323static char *hist_entry__get_srcline(struct hist_entry *he)
324{
325 struct map *map = he->ms.map;
326
327 if (!map)
328 return SRCLINE_UNKNOWN;
329
330 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
331 he->ms.sym, true);
332}
333
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300334static int64_t
335sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
336{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900337 if (!left->srcline)
338 left->srcline = hist_entry__get_srcline(left);
339 if (!right->srcline)
340 right->srcline = hist_entry__get_srcline(right);
341
Namhyung Kim202e7a62014-03-04 11:01:41 +0900342 return strcmp(right->srcline, left->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300343}
344
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300345static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim5b591662014-07-31 14:47:38 +0900346 size_t size, unsigned int width)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300347{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900348 if (!he->srcline)
349 he->srcline = hist_entry__get_srcline(he);
350
Namhyung Kim2960ed62016-02-22 09:32:33 +0900351 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300352}
353
354struct sort_entry sort_srcline = {
355 .se_header = "Source:Line",
356 .se_cmp = sort__srcline_cmp,
357 .se_snprintf = hist_entry__srcline_snprintf,
358 .se_width_idx = HISTC_SRCLINE,
359};
360
Andi Kleen31191a82015-08-07 15:54:24 -0700361/* --sort srcfile */
362
363static char no_srcfile[1];
364
Namhyung Kimcecaec62016-02-22 09:31:51 +0900365static char *hist_entry__get_srcfile(struct hist_entry *e)
Andi Kleen31191a82015-08-07 15:54:24 -0700366{
367 char *sf, *p;
368 struct map *map = e->ms.map;
369
Namhyung Kimcecaec62016-02-22 09:31:51 +0900370 if (!map)
371 return no_srcfile;
372
Andi Kleen2f84b422015-09-01 11:47:19 -0700373 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
374 e->ms.sym, false, true);
Andi Kleen76b10652015-08-11 06:36:55 -0700375 if (!strcmp(sf, SRCLINE_UNKNOWN))
376 return no_srcfile;
Andi Kleen31191a82015-08-07 15:54:24 -0700377 p = strchr(sf, ':');
378 if (p && *sf) {
379 *p = 0;
380 return sf;
381 }
382 free(sf);
383 return no_srcfile;
384}
385
386static int64_t
387sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
388{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900389 if (!left->srcfile)
390 left->srcfile = hist_entry__get_srcfile(left);
391 if (!right->srcfile)
392 right->srcfile = hist_entry__get_srcfile(right);
393
Andi Kleen31191a82015-08-07 15:54:24 -0700394 return strcmp(right->srcfile, left->srcfile);
395}
396
397static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
398 size_t size, unsigned int width)
399{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900400 if (!he->srcfile)
401 he->srcfile = hist_entry__get_srcfile(he);
402
Namhyung Kim2960ed62016-02-22 09:32:33 +0900403 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
Andi Kleen31191a82015-08-07 15:54:24 -0700404}
405
406struct sort_entry sort_srcfile = {
407 .se_header = "Source File",
408 .se_cmp = sort__srcfile_cmp,
409 .se_snprintf = hist_entry__srcfile_snprintf,
410 .se_width_idx = HISTC_SRCFILE,
411};
412
John Kacurdd68ada2009-09-24 18:02:49 +0200413/* --sort parent */
414
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200415static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200416sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
417{
418 struct symbol *sym_l = left->parent;
419 struct symbol *sym_r = right->parent;
420
421 if (!sym_l || !sym_r)
422 return cmp_null(sym_l, sym_r);
423
Namhyung Kim202e7a62014-03-04 11:01:41 +0900424 return strcmp(sym_r->name, sym_l->name);
John Kacurdd68ada2009-09-24 18:02:49 +0200425}
426
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300427static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300428 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200429{
Namhyung Kim5b591662014-07-31 14:47:38 +0900430 return repsep_snprintf(bf, size, "%-*.*s", width, width,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300431 he->parent ? he->parent->name : "[other]");
John Kacurdd68ada2009-09-24 18:02:49 +0200432}
433
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200434struct sort_entry sort_parent = {
435 .se_header = "Parent symbol",
436 .se_cmp = sort__parent_cmp,
437 .se_snprintf = hist_entry__parent_snprintf,
438 .se_width_idx = HISTC_PARENT,
439};
440
Arun Sharmaf60f3592010-06-04 11:27:10 -0300441/* --sort cpu */
442
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200443static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300444sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
445{
446 return right->cpu - left->cpu;
447}
448
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300449static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
450 size_t size, unsigned int width)
Arun Sharmaf60f3592010-06-04 11:27:10 -0300451{
Namhyung Kim5b591662014-07-31 14:47:38 +0900452 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300453}
454
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200455struct sort_entry sort_cpu = {
456 .se_header = "CPU",
457 .se_cmp = sort__cpu_cmp,
458 .se_snprintf = hist_entry__cpu_snprintf,
459 .se_width_idx = HISTC_CPU,
460};
461
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400462/* --sort socket */
463
464static int64_t
465sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
466{
467 return right->socket - left->socket;
468}
469
470static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
471 size_t size, unsigned int width)
472{
473 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
474}
475
Namhyung Kim54430102016-02-25 00:13:37 +0900476static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
477{
478 int sk = *(const int *)arg;
479
480 if (type != HIST_FILTER__SOCKET)
481 return -1;
482
483 return sk >= 0 && he->socket != sk;
484}
485
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400486struct sort_entry sort_socket = {
487 .se_header = "Socket",
488 .se_cmp = sort__socket_cmp,
489 .se_snprintf = hist_entry__socket_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900490 .se_filter = hist_entry__socket_filter,
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400491 .se_width_idx = HISTC_SOCKET,
492};
493
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900494/* --sort trace */
495
496static char *get_trace_output(struct hist_entry *he)
497{
498 struct trace_seq seq;
499 struct perf_evsel *evsel;
500 struct pevent_record rec = {
501 .data = he->raw_data,
502 .size = he->raw_size,
503 };
504
505 evsel = hists_to_evsel(he->hists);
506
507 trace_seq_init(&seq);
Namhyung Kim053a3982015-12-23 02:07:05 +0900508 if (symbol_conf.raw_trace) {
509 pevent_print_fields(&seq, he->raw_data, he->raw_size,
510 evsel->tp_format);
511 } else {
512 pevent_event_info(&seq, evsel->tp_format, &rec);
513 }
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900514 return seq.buffer;
515}
516
517static int64_t
518sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
519{
520 struct perf_evsel *evsel;
521
522 evsel = hists_to_evsel(left->hists);
523 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
524 return 0;
525
526 if (left->trace_output == NULL)
527 left->trace_output = get_trace_output(left);
528 if (right->trace_output == NULL)
529 right->trace_output = get_trace_output(right);
530
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900531 return strcmp(right->trace_output, left->trace_output);
532}
533
534static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
535 size_t size, unsigned int width)
536{
537 struct perf_evsel *evsel;
538
539 evsel = hists_to_evsel(he->hists);
540 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
Namhyung Kim2960ed62016-02-22 09:32:33 +0900541 return scnprintf(bf, size, "%-.*s", width, "N/A");
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900542
543 if (he->trace_output == NULL)
544 he->trace_output = get_trace_output(he);
Namhyung Kim2960ed62016-02-22 09:32:33 +0900545 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900546}
547
548struct sort_entry sort_trace = {
549 .se_header = "Trace output",
550 .se_cmp = sort__trace_cmp,
551 .se_snprintf = hist_entry__trace_snprintf,
552 .se_width_idx = HISTC_TRACE,
553};
554
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900555/* sort keys for branch stacks */
556
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100557static int64_t
558sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
559{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200560 if (!left->branch_info || !right->branch_info)
561 return cmp_null(left->branch_info, right->branch_info);
562
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100563 return _sort__dso_cmp(left->branch_info->from.map,
564 right->branch_info->from.map);
565}
566
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300567static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100568 size_t size, unsigned int width)
569{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200570 if (he->branch_info)
571 return _hist_entry__dso_snprintf(he->branch_info->from.map,
572 bf, size, width);
573 else
574 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100575}
576
Namhyung Kim54430102016-02-25 00:13:37 +0900577static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
578 const void *arg)
579{
580 const struct dso *dso = arg;
581
582 if (type != HIST_FILTER__DSO)
583 return -1;
584
585 return dso && (!he->branch_info || !he->branch_info->from.map ||
586 he->branch_info->from.map->dso != dso);
587}
588
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100589static int64_t
590sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
591{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200592 if (!left->branch_info || !right->branch_info)
593 return cmp_null(left->branch_info, right->branch_info);
594
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100595 return _sort__dso_cmp(left->branch_info->to.map,
596 right->branch_info->to.map);
597}
598
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300599static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100600 size_t size, unsigned int width)
601{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200602 if (he->branch_info)
603 return _hist_entry__dso_snprintf(he->branch_info->to.map,
604 bf, size, width);
605 else
606 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100607}
608
Namhyung Kim54430102016-02-25 00:13:37 +0900609static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
610 const void *arg)
611{
612 const struct dso *dso = arg;
613
614 if (type != HIST_FILTER__DSO)
615 return -1;
616
617 return dso && (!he->branch_info || !he->branch_info->to.map ||
618 he->branch_info->to.map->dso != dso);
619}
620
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100621static int64_t
622sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
623{
624 struct addr_map_symbol *from_l = &left->branch_info->from;
625 struct addr_map_symbol *from_r = &right->branch_info->from;
626
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200627 if (!left->branch_info || !right->branch_info)
628 return cmp_null(left->branch_info, right->branch_info);
629
630 from_l = &left->branch_info->from;
631 from_r = &right->branch_info->from;
632
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100633 if (!from_l->sym && !from_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900634 return _sort__addr_cmp(from_l->addr, from_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100635
Namhyung Kim51f27d12013-02-06 14:57:15 +0900636 return _sort__sym_cmp(from_l->sym, from_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100637}
638
639static int64_t
640sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
641{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200642 struct addr_map_symbol *to_l, *to_r;
643
644 if (!left->branch_info || !right->branch_info)
645 return cmp_null(left->branch_info, right->branch_info);
646
647 to_l = &left->branch_info->to;
648 to_r = &right->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100649
650 if (!to_l->sym && !to_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900651 return _sort__addr_cmp(to_l->addr, to_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100652
Namhyung Kim51f27d12013-02-06 14:57:15 +0900653 return _sort__sym_cmp(to_l->sym, to_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100654}
655
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300656static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900657 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100658{
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200659 if (he->branch_info) {
660 struct addr_map_symbol *from = &he->branch_info->from;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100661
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200662 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
663 he->level, bf, size, width);
664 }
665
666 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100667}
668
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300669static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900670 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100671{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200672 if (he->branch_info) {
673 struct addr_map_symbol *to = &he->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100674
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200675 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
676 he->level, bf, size, width);
677 }
678
679 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100680}
681
Namhyung Kim54430102016-02-25 00:13:37 +0900682static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
683 const void *arg)
684{
685 const char *sym = arg;
686
687 if (type != HIST_FILTER__SYMBOL)
688 return -1;
689
690 return sym && !(he->branch_info && he->branch_info->from.sym &&
691 strstr(he->branch_info->from.sym->name, sym));
692}
693
694static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
695 const void *arg)
696{
697 const char *sym = arg;
698
699 if (type != HIST_FILTER__SYMBOL)
700 return -1;
701
702 return sym && !(he->branch_info && he->branch_info->to.sym &&
703 strstr(he->branch_info->to.sym->name, sym));
704}
705
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900706struct sort_entry sort_dso_from = {
707 .se_header = "Source Shared Object",
708 .se_cmp = sort__dso_from_cmp,
709 .se_snprintf = hist_entry__dso_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900710 .se_filter = hist_entry__dso_from_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900711 .se_width_idx = HISTC_DSO_FROM,
712};
713
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100714struct sort_entry sort_dso_to = {
715 .se_header = "Target Shared Object",
716 .se_cmp = sort__dso_to_cmp,
717 .se_snprintf = hist_entry__dso_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900718 .se_filter = hist_entry__dso_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100719 .se_width_idx = HISTC_DSO_TO,
720};
721
722struct sort_entry sort_sym_from = {
723 .se_header = "Source Symbol",
724 .se_cmp = sort__sym_from_cmp,
725 .se_snprintf = hist_entry__sym_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900726 .se_filter = hist_entry__sym_from_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100727 .se_width_idx = HISTC_SYMBOL_FROM,
728};
729
730struct sort_entry sort_sym_to = {
731 .se_header = "Target Symbol",
732 .se_cmp = sort__sym_to_cmp,
733 .se_snprintf = hist_entry__sym_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900734 .se_filter = hist_entry__sym_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100735 .se_width_idx = HISTC_SYMBOL_TO,
736};
737
738static int64_t
739sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
740{
Jiri Olsa428560e2014-10-16 16:07:03 +0200741 unsigned char mp, p;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100742
Jiri Olsa428560e2014-10-16 16:07:03 +0200743 if (!left->branch_info || !right->branch_info)
744 return cmp_null(left->branch_info, right->branch_info);
745
746 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
747 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100748 return mp || p;
749}
750
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300751static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100752 size_t size, unsigned int width){
753 static const char *out = "N/A";
754
Jiri Olsa428560e2014-10-16 16:07:03 +0200755 if (he->branch_info) {
756 if (he->branch_info->flags.predicted)
757 out = "N";
758 else if (he->branch_info->flags.mispred)
759 out = "Y";
760 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100761
Namhyung Kim5b591662014-07-31 14:47:38 +0900762 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100763}
764
Andi Kleen0e332f02015-07-18 08:24:46 -0700765static int64_t
766sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
767{
768 return left->branch_info->flags.cycles -
769 right->branch_info->flags.cycles;
770}
771
772static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
773 size_t size, unsigned int width)
774{
775 if (he->branch_info->flags.cycles == 0)
776 return repsep_snprintf(bf, size, "%-*s", width, "-");
777 return repsep_snprintf(bf, size, "%-*hd", width,
778 he->branch_info->flags.cycles);
779}
780
781struct sort_entry sort_cycles = {
782 .se_header = "Basic Block Cycles",
783 .se_cmp = sort__cycles_cmp,
784 .se_snprintf = hist_entry__cycles_snprintf,
785 .se_width_idx = HISTC_CYCLES,
786};
787
Stephane Eranian98a3b322013-01-24 16:10:35 +0100788/* --sort daddr_sym */
789static int64_t
790sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
791{
792 uint64_t l = 0, r = 0;
793
794 if (left->mem_info)
795 l = left->mem_info->daddr.addr;
796 if (right->mem_info)
797 r = right->mem_info->daddr.addr;
798
799 return (int64_t)(r - l);
800}
801
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300802static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100803 size_t size, unsigned int width)
804{
805 uint64_t addr = 0;
806 struct map *map = NULL;
807 struct symbol *sym = NULL;
808
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300809 if (he->mem_info) {
810 addr = he->mem_info->daddr.addr;
811 map = he->mem_info->daddr.map;
812 sym = he->mem_info->daddr.sym;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100813 }
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300814 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100815 width);
816}
817
818static int64_t
Don Zickus28e6db22015-10-05 20:06:07 +0200819sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
820{
821 uint64_t l = 0, r = 0;
822
823 if (left->mem_info)
824 l = left->mem_info->iaddr.addr;
825 if (right->mem_info)
826 r = right->mem_info->iaddr.addr;
827
828 return (int64_t)(r - l);
829}
830
831static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
832 size_t size, unsigned int width)
833{
834 uint64_t addr = 0;
835 struct map *map = NULL;
836 struct symbol *sym = NULL;
837
838 if (he->mem_info) {
839 addr = he->mem_info->iaddr.addr;
840 map = he->mem_info->iaddr.map;
841 sym = he->mem_info->iaddr.sym;
842 }
843 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
844 width);
845}
846
847static int64_t
Stephane Eranian98a3b322013-01-24 16:10:35 +0100848sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
849{
850 struct map *map_l = NULL;
851 struct map *map_r = NULL;
852
853 if (left->mem_info)
854 map_l = left->mem_info->daddr.map;
855 if (right->mem_info)
856 map_r = right->mem_info->daddr.map;
857
858 return _sort__dso_cmp(map_l, map_r);
859}
860
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300861static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100862 size_t size, unsigned int width)
863{
864 struct map *map = NULL;
865
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300866 if (he->mem_info)
867 map = he->mem_info->daddr.map;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100868
869 return _hist_entry__dso_snprintf(map, bf, size, width);
870}
871
872static int64_t
873sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
874{
875 union perf_mem_data_src data_src_l;
876 union perf_mem_data_src data_src_r;
877
878 if (left->mem_info)
879 data_src_l = left->mem_info->data_src;
880 else
881 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
882
883 if (right->mem_info)
884 data_src_r = right->mem_info->data_src;
885 else
886 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
887
888 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
889}
890
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300891static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100892 size_t size, unsigned int width)
893{
Jiri Olsa69a77272016-02-24 09:46:49 +0100894 char out[10];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100895
Jiri Olsa69a77272016-02-24 09:46:49 +0100896 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300897 return repsep_snprintf(bf, size, "%.*s", width, out);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100898}
899
900static int64_t
901sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
902{
903 union perf_mem_data_src data_src_l;
904 union perf_mem_data_src data_src_r;
905
906 if (left->mem_info)
907 data_src_l = left->mem_info->data_src;
908 else
909 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
910
911 if (right->mem_info)
912 data_src_r = right->mem_info->data_src;
913 else
914 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
915
916 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
917}
918
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300919static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100920 size_t size, unsigned int width)
921{
922 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100923
Jiri Olsa0c877d72016-02-24 09:46:46 +0100924 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100925 return repsep_snprintf(bf, size, "%-*s", width, out);
926}
927
928static int64_t
929sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
930{
931 union perf_mem_data_src data_src_l;
932 union perf_mem_data_src data_src_r;
933
934 if (left->mem_info)
935 data_src_l = left->mem_info->data_src;
936 else
937 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
938
939 if (right->mem_info)
940 data_src_r = right->mem_info->data_src;
941 else
942 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
943
944 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
945}
946
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300947static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100948 size_t size, unsigned int width)
949{
950 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100951
Jiri Olsa071e9a12016-02-24 09:46:47 +0100952 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100953 return repsep_snprintf(bf, size, "%-*s", width, out);
954}
955
956static int64_t
957sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
958{
959 union perf_mem_data_src data_src_l;
960 union perf_mem_data_src data_src_r;
961
962 if (left->mem_info)
963 data_src_l = left->mem_info->data_src;
964 else
965 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
966
967 if (right->mem_info)
968 data_src_r = right->mem_info->data_src;
969 else
970 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
971
972 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
973}
974
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300975static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100976 size_t size, unsigned int width)
977{
978 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100979
Jiri Olsa2c07af12016-02-24 09:46:48 +0100980 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100981 return repsep_snprintf(bf, size, "%-*s", width, out);
982}
983
Don Zickus9b32ba72014-06-01 15:38:29 +0200984static int64_t
985sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
986{
987 u64 l, r;
988 struct map *l_map, *r_map;
989
990 if (!left->mem_info) return -1;
991 if (!right->mem_info) return 1;
992
993 /* group event types together */
994 if (left->cpumode > right->cpumode) return -1;
995 if (left->cpumode < right->cpumode) return 1;
996
997 l_map = left->mem_info->daddr.map;
998 r_map = right->mem_info->daddr.map;
999
1000 /* if both are NULL, jump to sort on al_addr instead */
1001 if (!l_map && !r_map)
1002 goto addr;
1003
1004 if (!l_map) return -1;
1005 if (!r_map) return 1;
1006
1007 if (l_map->maj > r_map->maj) return -1;
1008 if (l_map->maj < r_map->maj) return 1;
1009
1010 if (l_map->min > r_map->min) return -1;
1011 if (l_map->min < r_map->min) return 1;
1012
1013 if (l_map->ino > r_map->ino) return -1;
1014 if (l_map->ino < r_map->ino) return 1;
1015
1016 if (l_map->ino_generation > r_map->ino_generation) return -1;
1017 if (l_map->ino_generation < r_map->ino_generation) return 1;
1018
1019 /*
1020 * Addresses with no major/minor numbers are assumed to be
1021 * anonymous in userspace. Sort those on pid then address.
1022 *
1023 * The kernel and non-zero major/minor mapped areas are
1024 * assumed to be unity mapped. Sort those on address.
1025 */
1026
1027 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1028 (!(l_map->flags & MAP_SHARED)) &&
1029 !l_map->maj && !l_map->min && !l_map->ino &&
1030 !l_map->ino_generation) {
1031 /* userspace anonymous */
1032
1033 if (left->thread->pid_ > right->thread->pid_) return -1;
1034 if (left->thread->pid_ < right->thread->pid_) return 1;
1035 }
1036
1037addr:
1038 /* al_addr does all the right addr - start + offset calculations */
1039 l = cl_address(left->mem_info->daddr.al_addr);
1040 r = cl_address(right->mem_info->daddr.al_addr);
1041
1042 if (l > r) return -1;
1043 if (l < r) return 1;
1044
1045 return 0;
1046}
1047
1048static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1049 size_t size, unsigned int width)
1050{
1051
1052 uint64_t addr = 0;
1053 struct map *map = NULL;
1054 struct symbol *sym = NULL;
1055 char level = he->level;
1056
1057 if (he->mem_info) {
1058 addr = cl_address(he->mem_info->daddr.al_addr);
1059 map = he->mem_info->daddr.map;
1060 sym = he->mem_info->daddr.sym;
1061
1062 /* print [s] for shared data mmaps */
1063 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1064 map && (map->type == MAP__VARIABLE) &&
1065 (map->flags & MAP_SHARED) &&
1066 (map->maj || map->min || map->ino ||
1067 map->ino_generation))
1068 level = 's';
1069 else if (!map)
1070 level = 'X';
1071 }
1072 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1073 width);
1074}
1075
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001076struct sort_entry sort_mispredict = {
1077 .se_header = "Branch Mispredicted",
1078 .se_cmp = sort__mispredict_cmp,
1079 .se_snprintf = hist_entry__mispredict_snprintf,
1080 .se_width_idx = HISTC_MISPREDICT,
1081};
1082
Andi Kleen05484292013-01-24 16:10:29 +01001083static u64 he_weight(struct hist_entry *he)
1084{
1085 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1086}
1087
1088static int64_t
1089sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1090{
1091 return he_weight(left) - he_weight(right);
1092}
1093
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001094static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001095 size_t size, unsigned int width)
1096{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001097 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
Andi Kleen05484292013-01-24 16:10:29 +01001098}
1099
1100struct sort_entry sort_local_weight = {
1101 .se_header = "Local Weight",
1102 .se_cmp = sort__local_weight_cmp,
1103 .se_snprintf = hist_entry__local_weight_snprintf,
1104 .se_width_idx = HISTC_LOCAL_WEIGHT,
1105};
1106
1107static int64_t
1108sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1109{
1110 return left->stat.weight - right->stat.weight;
1111}
1112
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001113static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001114 size_t size, unsigned int width)
1115{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001116 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
Andi Kleen05484292013-01-24 16:10:29 +01001117}
1118
1119struct sort_entry sort_global_weight = {
1120 .se_header = "Weight",
1121 .se_cmp = sort__global_weight_cmp,
1122 .se_snprintf = hist_entry__global_weight_snprintf,
1123 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1124};
1125
Stephane Eranian98a3b322013-01-24 16:10:35 +01001126struct sort_entry sort_mem_daddr_sym = {
1127 .se_header = "Data Symbol",
1128 .se_cmp = sort__daddr_cmp,
1129 .se_snprintf = hist_entry__daddr_snprintf,
1130 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1131};
1132
Don Zickus28e6db22015-10-05 20:06:07 +02001133struct sort_entry sort_mem_iaddr_sym = {
1134 .se_header = "Code Symbol",
1135 .se_cmp = sort__iaddr_cmp,
1136 .se_snprintf = hist_entry__iaddr_snprintf,
1137 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1138};
1139
Stephane Eranian98a3b322013-01-24 16:10:35 +01001140struct sort_entry sort_mem_daddr_dso = {
1141 .se_header = "Data Object",
1142 .se_cmp = sort__dso_daddr_cmp,
1143 .se_snprintf = hist_entry__dso_daddr_snprintf,
1144 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1145};
1146
1147struct sort_entry sort_mem_locked = {
1148 .se_header = "Locked",
1149 .se_cmp = sort__locked_cmp,
1150 .se_snprintf = hist_entry__locked_snprintf,
1151 .se_width_idx = HISTC_MEM_LOCKED,
1152};
1153
1154struct sort_entry sort_mem_tlb = {
1155 .se_header = "TLB access",
1156 .se_cmp = sort__tlb_cmp,
1157 .se_snprintf = hist_entry__tlb_snprintf,
1158 .se_width_idx = HISTC_MEM_TLB,
1159};
1160
1161struct sort_entry sort_mem_lvl = {
1162 .se_header = "Memory access",
1163 .se_cmp = sort__lvl_cmp,
1164 .se_snprintf = hist_entry__lvl_snprintf,
1165 .se_width_idx = HISTC_MEM_LVL,
1166};
1167
1168struct sort_entry sort_mem_snoop = {
1169 .se_header = "Snoop",
1170 .se_cmp = sort__snoop_cmp,
1171 .se_snprintf = hist_entry__snoop_snprintf,
1172 .se_width_idx = HISTC_MEM_SNOOP,
1173};
1174
Don Zickus9b32ba72014-06-01 15:38:29 +02001175struct sort_entry sort_mem_dcacheline = {
1176 .se_header = "Data Cacheline",
1177 .se_cmp = sort__dcacheline_cmp,
1178 .se_snprintf = hist_entry__dcacheline_snprintf,
1179 .se_width_idx = HISTC_MEM_DCACHELINE,
1180};
1181
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001182static int64_t
1183sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1184{
Jiri Olsa49f47442014-10-16 16:07:01 +02001185 if (!left->branch_info || !right->branch_info)
1186 return cmp_null(left->branch_info, right->branch_info);
1187
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001188 return left->branch_info->flags.abort !=
1189 right->branch_info->flags.abort;
1190}
1191
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001192static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001193 size_t size, unsigned int width)
1194{
Jiri Olsa49f47442014-10-16 16:07:01 +02001195 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001196
Jiri Olsa49f47442014-10-16 16:07:01 +02001197 if (he->branch_info) {
1198 if (he->branch_info->flags.abort)
1199 out = "A";
1200 else
1201 out = ".";
1202 }
1203
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001204 return repsep_snprintf(bf, size, "%-*s", width, out);
1205}
1206
1207struct sort_entry sort_abort = {
1208 .se_header = "Transaction abort",
1209 .se_cmp = sort__abort_cmp,
1210 .se_snprintf = hist_entry__abort_snprintf,
1211 .se_width_idx = HISTC_ABORT,
1212};
1213
1214static int64_t
1215sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1216{
Jiri Olsa0199d242014-10-16 16:07:02 +02001217 if (!left->branch_info || !right->branch_info)
1218 return cmp_null(left->branch_info, right->branch_info);
1219
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001220 return left->branch_info->flags.in_tx !=
1221 right->branch_info->flags.in_tx;
1222}
1223
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001224static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001225 size_t size, unsigned int width)
1226{
Jiri Olsa0199d242014-10-16 16:07:02 +02001227 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001228
Jiri Olsa0199d242014-10-16 16:07:02 +02001229 if (he->branch_info) {
1230 if (he->branch_info->flags.in_tx)
1231 out = "T";
1232 else
1233 out = ".";
1234 }
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001235
1236 return repsep_snprintf(bf, size, "%-*s", width, out);
1237}
1238
1239struct sort_entry sort_in_tx = {
1240 .se_header = "Branch in transaction",
1241 .se_cmp = sort__in_tx_cmp,
1242 .se_snprintf = hist_entry__in_tx_snprintf,
1243 .se_width_idx = HISTC_IN_TX,
1244};
1245
Andi Kleen475eeab2013-09-20 07:40:43 -07001246static int64_t
1247sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1248{
1249 return left->transaction - right->transaction;
1250}
1251
1252static inline char *add_str(char *p, const char *str)
1253{
1254 strcpy(p, str);
1255 return p + strlen(str);
1256}
1257
1258static struct txbit {
1259 unsigned flag;
1260 const char *name;
1261 int skip_for_len;
1262} txbits[] = {
1263 { PERF_TXN_ELISION, "EL ", 0 },
1264 { PERF_TXN_TRANSACTION, "TX ", 1 },
1265 { PERF_TXN_SYNC, "SYNC ", 1 },
1266 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1267 { PERF_TXN_RETRY, "RETRY ", 0 },
1268 { PERF_TXN_CONFLICT, "CON ", 0 },
1269 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1270 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1271 { 0, NULL, 0 }
1272};
1273
1274int hist_entry__transaction_len(void)
1275{
1276 int i;
1277 int len = 0;
1278
1279 for (i = 0; txbits[i].name; i++) {
1280 if (!txbits[i].skip_for_len)
1281 len += strlen(txbits[i].name);
1282 }
1283 len += 4; /* :XX<space> */
1284 return len;
1285}
1286
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001287static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
Andi Kleen475eeab2013-09-20 07:40:43 -07001288 size_t size, unsigned int width)
1289{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001290 u64 t = he->transaction;
Andi Kleen475eeab2013-09-20 07:40:43 -07001291 char buf[128];
1292 char *p = buf;
1293 int i;
1294
1295 buf[0] = 0;
1296 for (i = 0; txbits[i].name; i++)
1297 if (txbits[i].flag & t)
1298 p = add_str(p, txbits[i].name);
1299 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1300 p = add_str(p, "NEITHER ");
1301 if (t & PERF_TXN_ABORT_MASK) {
1302 sprintf(p, ":%" PRIx64,
1303 (t & PERF_TXN_ABORT_MASK) >>
1304 PERF_TXN_ABORT_SHIFT);
1305 p += strlen(p);
1306 }
1307
1308 return repsep_snprintf(bf, size, "%-*s", width, buf);
1309}
1310
1311struct sort_entry sort_transaction = {
1312 .se_header = "Transaction ",
1313 .se_cmp = sort__transaction_cmp,
1314 .se_snprintf = hist_entry__transaction_snprintf,
1315 .se_width_idx = HISTC_TRANSACTION,
1316};
1317
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001318struct sort_dimension {
1319 const char *name;
1320 struct sort_entry *entry;
1321 int taken;
1322};
1323
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001324#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1325
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001326static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001327 DIM(SORT_PID, "pid", sort_thread),
1328 DIM(SORT_COMM, "comm", sort_comm),
1329 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001330 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001331 DIM(SORT_PARENT, "parent", sort_parent),
1332 DIM(SORT_CPU, "cpu", sort_cpu),
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001333 DIM(SORT_SOCKET, "socket", sort_socket),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -03001334 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Andi Kleen31191a82015-08-07 15:54:24 -07001335 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
Andi Kleenf9ea55d2013-07-18 15:58:53 -07001336 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1337 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
Andi Kleen475eeab2013-09-20 07:40:43 -07001338 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
Namhyung Kima34bb6a2015-12-23 02:07:04 +09001339 DIM(SORT_TRACE, "trace", sort_trace),
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001340};
1341
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001342#undef DIM
1343
1344#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1345
1346static struct sort_dimension bstack_sort_dimensions[] = {
1347 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1348 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1349 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1350 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1351 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001352 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1353 DIM(SORT_ABORT, "abort", sort_abort),
Andi Kleen0e332f02015-07-18 08:24:46 -07001354 DIM(SORT_CYCLES, "cycles", sort_cycles),
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001355};
1356
1357#undef DIM
1358
Namhyung Kimafab87b2013-04-03 21:26:11 +09001359#define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1360
1361static struct sort_dimension memory_sort_dimensions[] = {
Namhyung Kimafab87b2013-04-03 21:26:11 +09001362 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
Don Zickus28e6db22015-10-05 20:06:07 +02001363 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001364 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1365 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1366 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1367 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1368 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
Don Zickus9b32ba72014-06-01 15:38:29 +02001369 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001370};
1371
1372#undef DIM
1373
Namhyung Kima2ce0672014-03-04 09:06:42 +09001374struct hpp_dimension {
1375 const char *name;
1376 struct perf_hpp_fmt *fmt;
1377 int taken;
1378};
1379
1380#define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1381
1382static struct hpp_dimension hpp_sort_dimensions[] = {
1383 DIM(PERF_HPP__OVERHEAD, "overhead"),
1384 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1385 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1386 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1387 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
Namhyung Kim594dcbf2013-10-30 16:06:59 +09001388 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
Namhyung Kima2ce0672014-03-04 09:06:42 +09001389 DIM(PERF_HPP__SAMPLES, "sample"),
1390 DIM(PERF_HPP__PERIOD, "period"),
1391};
1392
1393#undef DIM
1394
Namhyung Kim8b536992014-03-03 11:46:55 +09001395struct hpp_sort_entry {
1396 struct perf_hpp_fmt hpp;
1397 struct sort_entry *se;
1398};
1399
Namhyung Kime0d66c72014-07-31 14:47:37 +09001400void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
Namhyung Kim678a5002014-03-20 11:18:54 +09001401{
1402 struct hpp_sort_entry *hse;
1403
1404 if (!perf_hpp__is_sort_entry(fmt))
1405 return;
1406
1407 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001408 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
Namhyung Kim678a5002014-03-20 11:18:54 +09001409}
1410
Namhyung Kim8b536992014-03-03 11:46:55 +09001411static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1412 struct perf_evsel *evsel)
1413{
1414 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001415 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001416
1417 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001418
Namhyung Kim5b591662014-07-31 14:47:38 +09001419 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001420 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001421
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001422 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
Namhyung Kim8b536992014-03-03 11:46:55 +09001423}
1424
1425static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1426 struct perf_hpp *hpp __maybe_unused,
1427 struct perf_evsel *evsel)
1428{
1429 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001430 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001431
1432 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1433
Namhyung Kim5b591662014-07-31 14:47:38 +09001434 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001435 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001436
1437 return len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001438}
1439
1440static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1441 struct hist_entry *he)
1442{
1443 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001444 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001445
1446 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim5b591662014-07-31 14:47:38 +09001447
1448 if (!len)
1449 len = hists__col_len(he->hists, hse->se->se_width_idx);
Namhyung Kim8b536992014-03-03 11:46:55 +09001450
1451 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1452}
1453
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001454static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1455 struct hist_entry *a, struct hist_entry *b)
1456{
1457 struct hpp_sort_entry *hse;
1458
1459 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1460 return hse->se->se_cmp(a, b);
1461}
1462
1463static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1464 struct hist_entry *a, struct hist_entry *b)
1465{
1466 struct hpp_sort_entry *hse;
1467 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1468
1469 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1470 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1471 return collapse_fn(a, b);
1472}
1473
1474static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1475 struct hist_entry *a, struct hist_entry *b)
1476{
1477 struct hpp_sort_entry *hse;
1478 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1479
1480 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1481 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1482 return sort_fn(a, b);
1483}
1484
Jiri Olsa97358082016-01-18 10:24:03 +01001485bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1486{
1487 return format->header == __sort__hpp_header;
1488}
1489
Namhyung Kim4945cf22016-03-09 22:46:57 +09001490#define MK_SORT_ENTRY_CHK(key) \
1491bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1492{ \
1493 struct hpp_sort_entry *hse; \
1494 \
1495 if (!perf_hpp__is_sort_entry(fmt)) \
1496 return false; \
1497 \
1498 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1499 return hse->se == &sort_ ## key ; \
Namhyung Kima9c6e462016-02-25 00:13:33 +09001500}
1501
Namhyung Kim4945cf22016-03-09 22:46:57 +09001502MK_SORT_ENTRY_CHK(trace)
1503MK_SORT_ENTRY_CHK(srcline)
1504MK_SORT_ENTRY_CHK(srcfile)
1505MK_SORT_ENTRY_CHK(thread)
1506MK_SORT_ENTRY_CHK(comm)
1507MK_SORT_ENTRY_CHK(dso)
1508MK_SORT_ENTRY_CHK(sym)
Namhyung Kima9c6e462016-02-25 00:13:33 +09001509
Namhyung Kima9c6e462016-02-25 00:13:33 +09001510
Jiri Olsa97358082016-01-18 10:24:03 +01001511static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1512{
1513 struct hpp_sort_entry *hse_a;
1514 struct hpp_sort_entry *hse_b;
1515
1516 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1517 return false;
1518
1519 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1520 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1521
1522 return hse_a->se == hse_b->se;
1523}
1524
Jiri Olsa564132f2016-01-18 10:24:09 +01001525static void hse_free(struct perf_hpp_fmt *fmt)
1526{
1527 struct hpp_sort_entry *hse;
1528
1529 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1530 free(hse);
1531}
1532
Namhyung Kima7d945b2014-03-04 10:46:34 +09001533static struct hpp_sort_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001534__sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
Namhyung Kim8b536992014-03-03 11:46:55 +09001535{
1536 struct hpp_sort_entry *hse;
1537
1538 hse = malloc(sizeof(*hse));
1539 if (hse == NULL) {
1540 pr_err("Memory allocation failed\n");
Namhyung Kima7d945b2014-03-04 10:46:34 +09001541 return NULL;
Namhyung Kim8b536992014-03-03 11:46:55 +09001542 }
1543
1544 hse->se = sd->entry;
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001545 hse->hpp.name = sd->entry->se_header;
Namhyung Kim8b536992014-03-03 11:46:55 +09001546 hse->hpp.header = __sort__hpp_header;
1547 hse->hpp.width = __sort__hpp_width;
1548 hse->hpp.entry = __sort__hpp_entry;
1549 hse->hpp.color = NULL;
1550
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001551 hse->hpp.cmp = __sort__hpp_cmp;
1552 hse->hpp.collapse = __sort__hpp_collapse;
1553 hse->hpp.sort = __sort__hpp_sort;
Jiri Olsa97358082016-01-18 10:24:03 +01001554 hse->hpp.equal = __sort__hpp_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001555 hse->hpp.free = hse_free;
Namhyung Kim8b536992014-03-03 11:46:55 +09001556
1557 INIT_LIST_HEAD(&hse->hpp.list);
1558 INIT_LIST_HEAD(&hse->hpp.sort_list);
Jiri Olsaf2998422014-05-23 17:15:47 +02001559 hse->hpp.elide = false;
Namhyung Kime0d66c72014-07-31 14:47:37 +09001560 hse->hpp.len = 0;
Namhyung Kim5b591662014-07-31 14:47:38 +09001561 hse->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001562 hse->hpp.level = level;
Namhyung Kim8b536992014-03-03 11:46:55 +09001563
Namhyung Kima7d945b2014-03-04 10:46:34 +09001564 return hse;
1565}
1566
Jiri Olsa564132f2016-01-18 10:24:09 +01001567static void hpp_free(struct perf_hpp_fmt *fmt)
1568{
1569 free(fmt);
1570}
1571
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001572static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1573 int level)
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001574{
1575 struct perf_hpp_fmt *fmt;
1576
1577 fmt = memdup(hd->fmt, sizeof(*fmt));
1578 if (fmt) {
1579 INIT_LIST_HEAD(&fmt->list);
1580 INIT_LIST_HEAD(&fmt->sort_list);
Jiri Olsa564132f2016-01-18 10:24:09 +01001581 fmt->free = hpp_free;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001582 fmt->level = level;
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001583 }
1584
1585 return fmt;
1586}
1587
Namhyung Kim54430102016-02-25 00:13:37 +09001588int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1589{
1590 struct perf_hpp_fmt *fmt;
1591 struct hpp_sort_entry *hse;
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001592 int ret = -1;
1593 int r;
Namhyung Kim54430102016-02-25 00:13:37 +09001594
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001595 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1596 if (!perf_hpp__is_sort_entry(fmt))
1597 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001598
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001599 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1600 if (hse->se->se_filter == NULL)
1601 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001602
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001603 /*
1604 * hist entry is filtered if any of sort key in the hpp list
1605 * is applied. But it should skip non-matched filter types.
1606 */
1607 r = hse->se->se_filter(he, type, arg);
1608 if (r >= 0) {
1609 if (ret < 0)
1610 ret = 0;
1611 ret |= r;
1612 }
1613 }
1614
1615 return ret;
Namhyung Kim54430102016-02-25 00:13:37 +09001616}
1617
Jiri Olsad7b617f2016-03-09 11:04:17 +01001618static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1619 struct perf_hpp_list *list,
1620 int level)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001621{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001622 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001623
1624 if (hse == NULL)
1625 return -1;
1626
Jiri Olsad7b617f2016-03-09 11:04:17 +01001627 perf_hpp_list__register_sort_field(list, &hse->hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001628 return 0;
1629}
1630
Jiri Olsad7b617f2016-03-09 11:04:17 +01001631static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1632 struct perf_hpp_list *list)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001633{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001634 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001635
1636 if (hse == NULL)
1637 return -1;
1638
Jiri Olsa07600022016-01-18 10:24:16 +01001639 perf_hpp_list__column_register(list, &hse->hpp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001640 return 0;
1641}
1642
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001643struct hpp_dynamic_entry {
1644 struct perf_hpp_fmt hpp;
1645 struct perf_evsel *evsel;
1646 struct format_field *field;
1647 unsigned dynamic_len;
Namhyung Kim053a3982015-12-23 02:07:05 +09001648 bool raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001649};
1650
1651static int hde_width(struct hpp_dynamic_entry *hde)
1652{
1653 if (!hde->hpp.len) {
1654 int len = hde->dynamic_len;
1655 int namelen = strlen(hde->field->name);
1656 int fieldlen = hde->field->size;
1657
1658 if (namelen > len)
1659 len = namelen;
1660
1661 if (!(hde->field->flags & FIELD_IS_STRING)) {
1662 /* length for print hex numbers */
1663 fieldlen = hde->field->size * 2 + 2;
1664 }
1665 if (fieldlen > len)
1666 len = fieldlen;
1667
1668 hde->hpp.len = len;
1669 }
1670 return hde->hpp.len;
1671}
1672
Namhyung Kim60517d22015-12-23 02:07:03 +09001673static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1674 struct hist_entry *he)
1675{
1676 char *str, *pos;
1677 struct format_field *field = hde->field;
1678 size_t namelen;
1679 bool last = false;
1680
Namhyung Kim053a3982015-12-23 02:07:05 +09001681 if (hde->raw_trace)
1682 return;
1683
Namhyung Kim60517d22015-12-23 02:07:03 +09001684 /* parse pretty print result and update max length */
1685 if (!he->trace_output)
1686 he->trace_output = get_trace_output(he);
1687
1688 namelen = strlen(field->name);
1689 str = he->trace_output;
1690
1691 while (str) {
1692 pos = strchr(str, ' ');
1693 if (pos == NULL) {
1694 last = true;
1695 pos = str + strlen(str);
1696 }
1697
1698 if (!strncmp(str, field->name, namelen)) {
1699 size_t len;
1700
1701 str += namelen + 1;
1702 len = pos - str;
1703
1704 if (len > hde->dynamic_len)
1705 hde->dynamic_len = len;
1706 break;
1707 }
1708
1709 if (last)
1710 str = NULL;
1711 else
1712 str = pos + 1;
1713 }
1714}
1715
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001716static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1717 struct perf_evsel *evsel __maybe_unused)
1718{
1719 struct hpp_dynamic_entry *hde;
1720 size_t len = fmt->user_len;
1721
1722 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1723
1724 if (!len)
1725 len = hde_width(hde);
1726
1727 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1728}
1729
1730static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1731 struct perf_hpp *hpp __maybe_unused,
1732 struct perf_evsel *evsel __maybe_unused)
1733{
1734 struct hpp_dynamic_entry *hde;
1735 size_t len = fmt->user_len;
1736
1737 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1738
1739 if (!len)
1740 len = hde_width(hde);
1741
1742 return len;
1743}
1744
Namhyung Kim361459f2015-12-23 02:07:08 +09001745bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1746{
1747 struct hpp_dynamic_entry *hde;
1748
1749 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1750
1751 return hists_to_evsel(hists) == hde->evsel;
1752}
1753
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001754static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1755 struct hist_entry *he)
1756{
1757 struct hpp_dynamic_entry *hde;
1758 size_t len = fmt->user_len;
Namhyung Kim60517d22015-12-23 02:07:03 +09001759 char *str, *pos;
1760 struct format_field *field;
1761 size_t namelen;
1762 bool last = false;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001763 int ret;
1764
1765 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1766
1767 if (!len)
1768 len = hde_width(hde);
1769
Namhyung Kim053a3982015-12-23 02:07:05 +09001770 if (hde->raw_trace)
1771 goto raw_field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001772
Namhyung Kime049d4a2016-02-27 03:52:46 +09001773 if (!he->trace_output)
1774 he->trace_output = get_trace_output(he);
1775
Namhyung Kim053a3982015-12-23 02:07:05 +09001776 field = hde->field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001777 namelen = strlen(field->name);
1778 str = he->trace_output;
1779
1780 while (str) {
1781 pos = strchr(str, ' ');
1782 if (pos == NULL) {
1783 last = true;
1784 pos = str + strlen(str);
1785 }
1786
1787 if (!strncmp(str, field->name, namelen)) {
1788 str += namelen + 1;
1789 str = strndup(str, pos - str);
1790
1791 if (str == NULL)
1792 return scnprintf(hpp->buf, hpp->size,
1793 "%*.*s", len, len, "ERROR");
1794 break;
1795 }
1796
1797 if (last)
1798 str = NULL;
1799 else
1800 str = pos + 1;
1801 }
1802
1803 if (str == NULL) {
1804 struct trace_seq seq;
Namhyung Kim053a3982015-12-23 02:07:05 +09001805raw_field:
Namhyung Kim60517d22015-12-23 02:07:03 +09001806 trace_seq_init(&seq);
1807 pevent_print_field(&seq, he->raw_data, hde->field);
1808 str = seq.buffer;
1809 }
1810
1811 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1812 free(str);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001813 return ret;
1814}
1815
1816static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1817 struct hist_entry *a, struct hist_entry *b)
1818{
1819 struct hpp_dynamic_entry *hde;
1820 struct format_field *field;
1821 unsigned offset, size;
1822
1823 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1824
Namhyung Kimabab5e72016-02-27 03:52:47 +09001825 if (b == NULL) {
1826 update_dynamic_len(hde, a);
1827 return 0;
1828 }
1829
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001830 field = hde->field;
1831 if (field->flags & FIELD_IS_DYNAMIC) {
1832 unsigned long long dyn;
1833
1834 pevent_read_number_field(field, a->raw_data, &dyn);
1835 offset = dyn & 0xffff;
1836 size = (dyn >> 16) & 0xffff;
1837
1838 /* record max width for output */
1839 if (size > hde->dynamic_len)
1840 hde->dynamic_len = size;
1841 } else {
1842 offset = field->offset;
1843 size = field->size;
1844 }
1845
1846 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1847}
1848
Namhyung Kim361459f2015-12-23 02:07:08 +09001849bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1850{
1851 return fmt->cmp == __sort__hde_cmp;
1852}
1853
Namhyung Kim665aa752016-02-21 23:22:35 +09001854static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1855{
1856 struct hpp_dynamic_entry *hde_a;
1857 struct hpp_dynamic_entry *hde_b;
1858
1859 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1860 return false;
1861
1862 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1863 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1864
1865 return hde_a->field == hde_b->field;
1866}
1867
Jiri Olsa564132f2016-01-18 10:24:09 +01001868static void hde_free(struct perf_hpp_fmt *fmt)
1869{
1870 struct hpp_dynamic_entry *hde;
1871
1872 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1873 free(hde);
1874}
1875
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001876static struct hpp_dynamic_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001877__alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1878 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001879{
1880 struct hpp_dynamic_entry *hde;
1881
1882 hde = malloc(sizeof(*hde));
1883 if (hde == NULL) {
1884 pr_debug("Memory allocation failed\n");
1885 return NULL;
1886 }
1887
1888 hde->evsel = evsel;
1889 hde->field = field;
1890 hde->dynamic_len = 0;
1891
1892 hde->hpp.name = field->name;
1893 hde->hpp.header = __sort__hde_header;
1894 hde->hpp.width = __sort__hde_width;
1895 hde->hpp.entry = __sort__hde_entry;
1896 hde->hpp.color = NULL;
1897
1898 hde->hpp.cmp = __sort__hde_cmp;
1899 hde->hpp.collapse = __sort__hde_cmp;
1900 hde->hpp.sort = __sort__hde_cmp;
Namhyung Kim665aa752016-02-21 23:22:35 +09001901 hde->hpp.equal = __sort__hde_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001902 hde->hpp.free = hde_free;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001903
1904 INIT_LIST_HEAD(&hde->hpp.list);
1905 INIT_LIST_HEAD(&hde->hpp.sort_list);
1906 hde->hpp.elide = false;
1907 hde->hpp.len = 0;
1908 hde->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001909 hde->hpp.level = level;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001910
1911 return hde;
1912}
1913
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03001914struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1915{
1916 struct perf_hpp_fmt *new_fmt = NULL;
1917
1918 if (perf_hpp__is_sort_entry(fmt)) {
1919 struct hpp_sort_entry *hse, *new_hse;
1920
1921 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1922 new_hse = memdup(hse, sizeof(*hse));
1923 if (new_hse)
1924 new_fmt = &new_hse->hpp;
1925 } else if (perf_hpp__is_dynamic_entry(fmt)) {
1926 struct hpp_dynamic_entry *hde, *new_hde;
1927
1928 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1929 new_hde = memdup(hde, sizeof(*hde));
1930 if (new_hde)
1931 new_fmt = &new_hde->hpp;
1932 } else {
1933 new_fmt = memdup(fmt, sizeof(*fmt));
1934 }
1935
1936 INIT_LIST_HEAD(&new_fmt->list);
1937 INIT_LIST_HEAD(&new_fmt->sort_list);
1938
1939 return new_fmt;
1940}
1941
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001942static int parse_field_name(char *str, char **event, char **field, char **opt)
1943{
1944 char *event_name, *field_name, *opt_name;
1945
1946 event_name = str;
1947 field_name = strchr(str, '.');
1948
1949 if (field_name) {
1950 *field_name++ = '\0';
1951 } else {
1952 event_name = NULL;
1953 field_name = str;
1954 }
1955
1956 opt_name = strchr(field_name, '/');
1957 if (opt_name)
1958 *opt_name++ = '\0';
1959
1960 *event = event_name;
1961 *field = field_name;
1962 *opt = opt_name;
1963
1964 return 0;
1965}
1966
1967/* find match evsel using a given event name. The event name can be:
Namhyung Kim9735be22016-01-05 19:58:35 +09001968 * 1. '%' + event index (e.g. '%1' for first event)
1969 * 2. full event name (e.g. sched:sched_switch)
1970 * 3. partial event name (should not contain ':')
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001971 */
1972static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1973{
1974 struct perf_evsel *evsel = NULL;
1975 struct perf_evsel *pos;
1976 bool full_name;
1977
1978 /* case 1 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001979 if (event_name[0] == '%') {
1980 int nr = strtol(event_name+1, NULL, 0);
1981
1982 if (nr > evlist->nr_entries)
1983 return NULL;
1984
1985 evsel = perf_evlist__first(evlist);
1986 while (--nr > 0)
1987 evsel = perf_evsel__next(evsel);
1988
1989 return evsel;
1990 }
1991
1992 full_name = !!strchr(event_name, ':');
1993 evlist__for_each(evlist, pos) {
Namhyung Kim9735be22016-01-05 19:58:35 +09001994 /* case 2 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001995 if (full_name && !strcmp(pos->name, event_name))
1996 return pos;
Namhyung Kim9735be22016-01-05 19:58:35 +09001997 /* case 3 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001998 if (!full_name && strstr(pos->name, event_name)) {
1999 if (evsel) {
2000 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2001 event_name, evsel->name, pos->name);
2002 return NULL;
2003 }
2004 evsel = pos;
2005 }
2006 }
2007
2008 return evsel;
2009}
2010
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002011static int __dynamic_dimension__add(struct perf_evsel *evsel,
2012 struct format_field *field,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002013 bool raw_trace, int level)
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002014{
2015 struct hpp_dynamic_entry *hde;
2016
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002017 hde = __alloc_dynamic_entry(evsel, field, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002018 if (hde == NULL)
2019 return -ENOMEM;
2020
2021 hde->raw_trace = raw_trace;
2022
2023 perf_hpp__register_sort_field(&hde->hpp);
2024 return 0;
2025}
2026
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002027static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002028{
2029 int ret;
2030 struct format_field *field;
2031
2032 field = evsel->tp_format->format.fields;
2033 while (field) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002034 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002035 if (ret < 0)
2036 return ret;
2037
2038 field = field->next;
2039 }
2040 return 0;
2041}
2042
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002043static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2044 int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002045{
2046 int ret;
2047 struct perf_evsel *evsel;
2048
2049 evlist__for_each(evlist, evsel) {
2050 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2051 continue;
2052
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002053 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002054 if (ret < 0)
2055 return ret;
2056 }
2057 return 0;
2058}
2059
Namhyung Kim9735be22016-01-05 19:58:35 +09002060static int add_all_matching_fields(struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002061 char *field_name, bool raw_trace, int level)
Namhyung Kim9735be22016-01-05 19:58:35 +09002062{
2063 int ret = -ESRCH;
2064 struct perf_evsel *evsel;
2065 struct format_field *field;
2066
2067 evlist__for_each(evlist, evsel) {
2068 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2069 continue;
2070
2071 field = pevent_find_any_field(evsel->tp_format, field_name);
2072 if (field == NULL)
2073 continue;
2074
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002075 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002076 if (ret < 0)
2077 break;
2078 }
2079 return ret;
2080}
2081
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002082static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2083 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002084{
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002085 char *str, *event_name, *field_name, *opt_name;
2086 struct perf_evsel *evsel;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002087 struct format_field *field;
Namhyung Kim053a3982015-12-23 02:07:05 +09002088 bool raw_trace = symbol_conf.raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002089 int ret = 0;
2090
2091 if (evlist == NULL)
2092 return -ENOENT;
2093
2094 str = strdup(tok);
2095 if (str == NULL)
2096 return -ENOMEM;
2097
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002098 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002099 ret = -EINVAL;
2100 goto out;
2101 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002102
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002103 if (opt_name) {
2104 if (strcmp(opt_name, "raw")) {
2105 pr_debug("unsupported field option %s\n", opt_name);
Namhyung Kim053a3982015-12-23 02:07:05 +09002106 ret = -EINVAL;
2107 goto out;
2108 }
2109 raw_trace = true;
2110 }
2111
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002112 if (!strcmp(field_name, "trace_fields")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002113 ret = add_all_dynamic_fields(evlist, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002114 goto out;
2115 }
2116
Namhyung Kim9735be22016-01-05 19:58:35 +09002117 if (event_name == NULL) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002118 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002119 goto out;
2120 }
2121
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002122 evsel = find_evsel(evlist, event_name);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002123 if (evsel == NULL) {
2124 pr_debug("Cannot find event: %s\n", event_name);
2125 ret = -ENOENT;
2126 goto out;
2127 }
2128
2129 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2130 pr_debug("%s is not a tracepoint event\n", event_name);
2131 ret = -EINVAL;
2132 goto out;
2133 }
2134
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002135 if (!strcmp(field_name, "*")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002136 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002137 } else {
2138 field = pevent_find_any_field(evsel->tp_format, field_name);
2139 if (field == NULL) {
2140 pr_debug("Cannot find event field for %s.%s\n",
2141 event_name, field_name);
2142 return -ENOENT;
2143 }
2144
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002145 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002146 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002147
2148out:
2149 free(str);
2150 return ret;
2151}
2152
Jiri Olsad7b617f2016-03-09 11:04:17 +01002153static int __sort_dimension__add(struct sort_dimension *sd,
2154 struct perf_hpp_list *list,
2155 int level)
Namhyung Kim2f532d092013-04-03 21:26:10 +09002156{
2157 if (sd->taken)
Namhyung Kim8b536992014-03-03 11:46:55 +09002158 return 0;
2159
Jiri Olsad7b617f2016-03-09 11:04:17 +01002160 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
Namhyung Kim8b536992014-03-03 11:46:55 +09002161 return -1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002162
2163 if (sd->entry->se_collapse)
Jiri Olsa52225032016-05-03 13:54:42 +02002164 list->need_collapse = 1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002165
Namhyung Kim2f532d092013-04-03 21:26:10 +09002166 sd->taken = 1;
Namhyung Kim8b536992014-03-03 11:46:55 +09002167
2168 return 0;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002169}
2170
Jiri Olsad7b617f2016-03-09 11:04:17 +01002171static int __hpp_dimension__add(struct hpp_dimension *hd,
2172 struct perf_hpp_list *list,
2173 int level)
Namhyung Kima2ce0672014-03-04 09:06:42 +09002174{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002175 struct perf_hpp_fmt *fmt;
Namhyung Kima2ce0672014-03-04 09:06:42 +09002176
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002177 if (hd->taken)
2178 return 0;
2179
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002180 fmt = __hpp_dimension__alloc_hpp(hd, level);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002181 if (!fmt)
2182 return -1;
2183
2184 hd->taken = 1;
Jiri Olsad7b617f2016-03-09 11:04:17 +01002185 perf_hpp_list__register_sort_field(list, fmt);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002186 return 0;
2187}
2188
Jiri Olsa07600022016-01-18 10:24:16 +01002189static int __sort_dimension__add_output(struct perf_hpp_list *list,
2190 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002191{
2192 if (sd->taken)
2193 return 0;
2194
Jiri Olsad7b617f2016-03-09 11:04:17 +01002195 if (__sort_dimension__add_hpp_output(sd, list) < 0)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002196 return -1;
2197
2198 sd->taken = 1;
2199 return 0;
2200}
2201
Jiri Olsa07600022016-01-18 10:24:16 +01002202static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2203 struct hpp_dimension *hd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002204{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002205 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002206
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002207 if (hd->taken)
2208 return 0;
2209
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002210 fmt = __hpp_dimension__alloc_hpp(hd, 0);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002211 if (!fmt)
2212 return -1;
2213
2214 hd->taken = 1;
Jiri Olsa07600022016-01-18 10:24:16 +01002215 perf_hpp_list__column_register(list, fmt);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002216 return 0;
2217}
2218
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002219int hpp_dimension__add_output(unsigned col)
2220{
2221 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa07600022016-01-18 10:24:16 +01002222 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002223}
2224
Jiri Olsad7b617f2016-03-09 11:04:17 +01002225static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -03002226 struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002227 int level)
John Kacurdd68ada2009-09-24 18:02:49 +02002228{
2229 unsigned int i;
2230
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002231 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2232 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +02002233
John Kacurdd68ada2009-09-24 18:02:49 +02002234 if (strncasecmp(tok, sd->name, strlen(tok)))
2235 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002236
John Kacurdd68ada2009-09-24 18:02:49 +02002237 if (sd->entry == &sort_parent) {
2238 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2239 if (ret) {
2240 char err[BUFSIZ];
2241
2242 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -03002243 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2244 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +02002245 }
Jiri Olsade7e6a72016-05-03 13:54:43 +02002246 list->parent = 1;
Namhyung Kim930477b2013-04-05 10:26:36 +09002247 } else if (sd->entry == &sort_sym) {
Namhyung Kim1af556402012-09-14 17:35:27 +09002248 sort__has_sym = 1;
Kan Liang94ba4622015-02-09 05:39:44 +00002249 /*
2250 * perf diff displays the performance difference amongst
2251 * two or more perf.data files. Those files could come
2252 * from different binaries. So we should not compare
2253 * their ips, but the name of symbol.
2254 */
2255 if (sort__mode == SORT_MODE__DIFF)
2256 sd->entry->se_collapse = sort__sym_sort;
2257
Namhyung Kim68f6d022013-12-18 14:21:10 +09002258 } else if (sd->entry == &sort_dso) {
2259 sort__has_dso = 1;
Kan Liang2e7ea3a2015-09-04 10:45:43 -04002260 } else if (sd->entry == &sort_socket) {
2261 sort__has_socket = 1;
Namhyung Kimcfd92da2016-01-21 19:13:24 -03002262 } else if (sd->entry == &sort_thread) {
2263 sort__has_thread = 1;
Namhyung Kim078b8d42016-03-09 23:20:51 +09002264 } else if (sd->entry == &sort_comm) {
2265 sort__has_comm = 1;
John Kacurdd68ada2009-09-24 18:02:49 +02002266 }
2267
Jiri Olsad7b617f2016-03-09 11:04:17 +01002268 return __sort_dimension__add(sd, list, level);
John Kacurdd68ada2009-09-24 18:02:49 +02002269 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002270
Namhyung Kima2ce0672014-03-04 09:06:42 +09002271 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2272 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2273
2274 if (strncasecmp(tok, hd->name, strlen(tok)))
2275 continue;
2276
Jiri Olsad7b617f2016-03-09 11:04:17 +01002277 return __hpp_dimension__add(hd, list, level);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002278 }
2279
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002280 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2281 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2282
2283 if (strncasecmp(tok, sd->name, strlen(tok)))
2284 continue;
2285
Namhyung Kim55369fc2013-04-01 20:35:20 +09002286 if (sort__mode != SORT_MODE__BRANCH)
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002287 return -EINVAL;
2288
2289 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2290 sort__has_sym = 1;
2291
Jiri Olsad7b617f2016-03-09 11:04:17 +01002292 __sort_dimension__add(sd, list, level);
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002293 return 0;
2294 }
2295
Namhyung Kimafab87b2013-04-03 21:26:11 +09002296 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2297 struct sort_dimension *sd = &memory_sort_dimensions[i];
2298
2299 if (strncasecmp(tok, sd->name, strlen(tok)))
2300 continue;
2301
2302 if (sort__mode != SORT_MODE__MEMORY)
2303 return -EINVAL;
2304
2305 if (sd->entry == &sort_mem_daddr_sym)
2306 sort__has_sym = 1;
2307
Jiri Olsad7b617f2016-03-09 11:04:17 +01002308 __sort_dimension__add(sd, list, level);
Namhyung Kimafab87b2013-04-03 21:26:11 +09002309 return 0;
2310 }
2311
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002312 if (!add_dynamic_entry(evlist, tok, level))
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002313 return 0;
2314
John Kacurdd68ada2009-09-24 18:02:49 +02002315 return -ESRCH;
2316}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002317
Jiri Olsad7b617f2016-03-09 11:04:17 +01002318static int setup_sort_list(struct perf_hpp_list *list, char *str,
2319 struct perf_evlist *evlist)
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002320{
2321 char *tmp, *tok;
2322 int ret = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002323 int level = 0;
Namhyung Kima23f37e2016-03-07 16:44:47 -03002324 int next_level = 1;
2325 bool in_group = false;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002326
Namhyung Kima23f37e2016-03-07 16:44:47 -03002327 do {
2328 tok = str;
2329 tmp = strpbrk(str, "{}, ");
2330 if (tmp) {
2331 if (in_group)
2332 next_level = level;
2333 else
2334 next_level = level + 1;
2335
2336 if (*tmp == '{')
2337 in_group = true;
2338 else if (*tmp == '}')
2339 in_group = false;
2340
2341 *tmp = '\0';
2342 str = tmp + 1;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002343 }
Namhyung Kima23f37e2016-03-07 16:44:47 -03002344
2345 if (*tok) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002346 ret = sort_dimension__add(list, tok, evlist, level);
Namhyung Kima23f37e2016-03-07 16:44:47 -03002347 if (ret == -EINVAL) {
2348 error("Invalid --sort key: `%s'", tok);
2349 break;
2350 } else if (ret == -ESRCH) {
2351 error("Unknown --sort key: `%s'", tok);
2352 break;
2353 }
2354 }
2355
2356 level = next_level;
2357 } while (tmp);
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002358
2359 return ret;
2360}
2361
Namhyung Kimd49dade2015-12-23 02:07:10 +09002362static const char *get_default_sort_order(struct perf_evlist *evlist)
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002363{
2364 const char *default_sort_orders[] = {
2365 default_sort_order,
2366 default_branch_sort_order,
2367 default_mem_sort_order,
2368 default_top_sort_order,
2369 default_diff_sort_order,
Namhyung Kimd49dade2015-12-23 02:07:10 +09002370 default_tracepoint_sort_order,
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002371 };
Namhyung Kimd49dade2015-12-23 02:07:10 +09002372 bool use_trace = true;
2373 struct perf_evsel *evsel;
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002374
2375 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2376
Namhyung Kimd49dade2015-12-23 02:07:10 +09002377 if (evlist == NULL)
2378 goto out_no_evlist;
2379
2380 evlist__for_each(evlist, evsel) {
2381 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2382 use_trace = false;
2383 break;
2384 }
2385 }
2386
2387 if (use_trace) {
2388 sort__mode = SORT_MODE__TRACEPOINT;
2389 if (symbol_conf.raw_trace)
2390 return "trace_fields";
2391 }
2392out_no_evlist:
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002393 return default_sort_orders[sort__mode];
2394}
2395
Namhyung Kimd49dade2015-12-23 02:07:10 +09002396static int setup_sort_order(struct perf_evlist *evlist)
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002397{
2398 char *new_sort_order;
2399
2400 /*
2401 * Append '+'-prefixed sort order to the default sort
2402 * order string.
2403 */
2404 if (!sort_order || is_strict_order(sort_order))
2405 return 0;
2406
2407 if (sort_order[1] == '\0') {
2408 error("Invalid --sort key: `+'");
2409 return -EINVAL;
2410 }
2411
2412 /*
2413 * We allocate new sort_order string, but we never free it,
2414 * because it's checked over the rest of the code.
2415 */
2416 if (asprintf(&new_sort_order, "%s,%s",
Namhyung Kimd49dade2015-12-23 02:07:10 +09002417 get_default_sort_order(evlist), sort_order + 1) < 0) {
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002418 error("Not enough memory to set up --sort");
2419 return -ENOMEM;
2420 }
2421
2422 sort_order = new_sort_order;
2423 return 0;
2424}
2425
Jiri Olsab97511c2016-01-07 10:14:08 +01002426/*
2427 * Adds 'pre,' prefix into 'str' is 'pre' is
2428 * not already part of 'str'.
2429 */
2430static char *prefix_if_not_in(const char *pre, char *str)
2431{
2432 char *n;
2433
2434 if (!str || strstr(str, pre))
2435 return str;
2436
2437 if (asprintf(&n, "%s,%s", pre, str) < 0)
2438 return NULL;
2439
2440 free(str);
2441 return n;
2442}
2443
2444static char *setup_overhead(char *keys)
2445{
2446 keys = prefix_if_not_in("overhead", keys);
2447
2448 if (symbol_conf.cumulate_callchain)
2449 keys = prefix_if_not_in("overhead_children", keys);
2450
2451 return keys;
2452}
2453
Namhyung Kim40184c42015-12-23 02:07:01 +09002454static int __setup_sorting(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002455{
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002456 char *str;
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002457 const char *sort_keys;
Namhyung Kim55309982013-02-06 14:57:16 +09002458 int ret = 0;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002459
Namhyung Kimd49dade2015-12-23 02:07:10 +09002460 ret = setup_sort_order(evlist);
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002461 if (ret)
2462 return ret;
2463
2464 sort_keys = sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002465 if (sort_keys == NULL) {
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002466 if (is_strict_order(field_order)) {
Namhyung Kima7d945b2014-03-04 10:46:34 +09002467 /*
2468 * If user specified field order but no sort order,
2469 * we'll honor it and not add default sort orders.
2470 */
2471 return 0;
2472 }
2473
Namhyung Kimd49dade2015-12-23 02:07:10 +09002474 sort_keys = get_default_sort_order(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002475 }
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002476
2477 str = strdup(sort_keys);
Namhyung Kim5936f542013-02-06 14:57:17 +09002478 if (str == NULL) {
2479 error("Not enough memory to setup sort keys");
2480 return -ENOMEM;
2481 }
2482
Jiri Olsab97511c2016-01-07 10:14:08 +01002483 /*
2484 * Prepend overhead fields for backward compatibility.
2485 */
2486 if (!is_strict_order(field_order)) {
2487 str = setup_overhead(str);
2488 if (str == NULL) {
2489 error("Not enough memory to setup overhead keys");
2490 return -ENOMEM;
2491 }
2492 }
2493
Jiri Olsad7b617f2016-03-09 11:04:17 +01002494 ret = setup_sort_list(&perf_hpp_list, str, evlist);
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002495
2496 free(str);
Namhyung Kim55309982013-02-06 14:57:16 +09002497 return ret;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002498}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002499
Jiri Olsaf2998422014-05-23 17:15:47 +02002500void perf_hpp__set_elide(int idx, bool elide)
Namhyung Kime67d49a2014-03-18 13:00:59 +09002501{
Jiri Olsaf2998422014-05-23 17:15:47 +02002502 struct perf_hpp_fmt *fmt;
2503 struct hpp_sort_entry *hse;
Namhyung Kime67d49a2014-03-18 13:00:59 +09002504
Jiri Olsacf094042016-01-18 10:24:17 +01002505 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002506 if (!perf_hpp__is_sort_entry(fmt))
2507 continue;
2508
2509 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2510 if (hse->se->se_width_idx == idx) {
2511 fmt->elide = elide;
2512 break;
2513 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002514 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002515}
2516
Jiri Olsaf2998422014-05-23 17:15:47 +02002517static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002518{
2519 if (list && strlist__nr_entries(list) == 1) {
2520 if (fp != NULL)
2521 fprintf(fp, "# %s: %s\n", list_name,
2522 strlist__entry(list, 0)->s);
Jiri Olsaf2998422014-05-23 17:15:47 +02002523 return true;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002524 }
Jiri Olsaf2998422014-05-23 17:15:47 +02002525 return false;
2526}
2527
2528static bool get_elide(int idx, FILE *output)
2529{
2530 switch (idx) {
2531 case HISTC_SYMBOL:
2532 return __get_elide(symbol_conf.sym_list, "symbol", output);
2533 case HISTC_DSO:
2534 return __get_elide(symbol_conf.dso_list, "dso", output);
2535 case HISTC_COMM:
2536 return __get_elide(symbol_conf.comm_list, "comm", output);
2537 default:
2538 break;
2539 }
2540
2541 if (sort__mode != SORT_MODE__BRANCH)
2542 return false;
2543
2544 switch (idx) {
2545 case HISTC_SYMBOL_FROM:
2546 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2547 case HISTC_SYMBOL_TO:
2548 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2549 case HISTC_DSO_FROM:
2550 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2551 case HISTC_DSO_TO:
2552 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2553 default:
2554 break;
2555 }
2556
2557 return false;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002558}
Namhyung Kim08e71542013-04-03 21:26:19 +09002559
2560void sort__setup_elide(FILE *output)
2561{
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002562 struct perf_hpp_fmt *fmt;
2563 struct hpp_sort_entry *hse;
Namhyung Kim7524f632013-11-08 17:53:42 +09002564
Jiri Olsacf094042016-01-18 10:24:17 +01002565 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002566 if (!perf_hpp__is_sort_entry(fmt))
2567 continue;
Namhyung Kim08e71542013-04-03 21:26:19 +09002568
Jiri Olsaf2998422014-05-23 17:15:47 +02002569 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2570 fmt->elide = get_elide(hse->se->se_width_idx, output);
Namhyung Kim08e71542013-04-03 21:26:19 +09002571 }
2572
Namhyung Kim7524f632013-11-08 17:53:42 +09002573 /*
2574 * It makes no sense to elide all of sort entries.
2575 * Just revert them to show up again.
2576 */
Jiri Olsacf094042016-01-18 10:24:17 +01002577 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002578 if (!perf_hpp__is_sort_entry(fmt))
2579 continue;
2580
Jiri Olsaf2998422014-05-23 17:15:47 +02002581 if (!fmt->elide)
Namhyung Kim7524f632013-11-08 17:53:42 +09002582 return;
2583 }
2584
Jiri Olsacf094042016-01-18 10:24:17 +01002585 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002586 if (!perf_hpp__is_sort_entry(fmt))
2587 continue;
2588
Jiri Olsaf2998422014-05-23 17:15:47 +02002589 fmt->elide = false;
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002590 }
Namhyung Kim08e71542013-04-03 21:26:19 +09002591}
Namhyung Kima7d945b2014-03-04 10:46:34 +09002592
Jiri Olsa07600022016-01-18 10:24:16 +01002593static int output_field_add(struct perf_hpp_list *list, char *tok)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002594{
2595 unsigned int i;
2596
2597 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2598 struct sort_dimension *sd = &common_sort_dimensions[i];
2599
2600 if (strncasecmp(tok, sd->name, strlen(tok)))
2601 continue;
2602
Jiri Olsa07600022016-01-18 10:24:16 +01002603 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002604 }
2605
2606 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2607 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2608
2609 if (strncasecmp(tok, hd->name, strlen(tok)))
2610 continue;
2611
Jiri Olsa07600022016-01-18 10:24:16 +01002612 return __hpp_dimension__add_output(list, hd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002613 }
2614
2615 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2616 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2617
2618 if (strncasecmp(tok, sd->name, strlen(tok)))
2619 continue;
2620
Jiri Olsa07600022016-01-18 10:24:16 +01002621 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002622 }
2623
2624 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2625 struct sort_dimension *sd = &memory_sort_dimensions[i];
2626
2627 if (strncasecmp(tok, sd->name, strlen(tok)))
2628 continue;
2629
Jiri Olsa07600022016-01-18 10:24:16 +01002630 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002631 }
2632
2633 return -ESRCH;
2634}
2635
Jiri Olsa07600022016-01-18 10:24:16 +01002636static int setup_output_list(struct perf_hpp_list *list, char *str)
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002637{
2638 char *tmp, *tok;
2639 int ret = 0;
2640
2641 for (tok = strtok_r(str, ", ", &tmp);
2642 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Jiri Olsa07600022016-01-18 10:24:16 +01002643 ret = output_field_add(list, tok);
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002644 if (ret == -EINVAL) {
2645 error("Invalid --fields key: `%s'", tok);
2646 break;
2647 } else if (ret == -ESRCH) {
2648 error("Unknown --fields key: `%s'", tok);
2649 break;
2650 }
2651 }
2652
2653 return ret;
2654}
2655
Namhyung Kima7d945b2014-03-04 10:46:34 +09002656static void reset_dimensions(void)
2657{
2658 unsigned int i;
2659
2660 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2661 common_sort_dimensions[i].taken = 0;
2662
2663 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2664 hpp_sort_dimensions[i].taken = 0;
2665
2666 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2667 bstack_sort_dimensions[i].taken = 0;
2668
2669 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2670 memory_sort_dimensions[i].taken = 0;
2671}
2672
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002673bool is_strict_order(const char *order)
2674{
2675 return order && (*order != '+');
2676}
2677
Namhyung Kima7d945b2014-03-04 10:46:34 +09002678static int __setup_output_field(void)
2679{
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002680 char *str, *strp;
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002681 int ret = -EINVAL;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002682
2683 if (field_order == NULL)
2684 return 0;
2685
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002686 strp = str = strdup(field_order);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002687 if (str == NULL) {
2688 error("Not enough memory to setup output fields");
2689 return -ENOMEM;
2690 }
2691
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002692 if (!is_strict_order(field_order))
2693 strp++;
2694
2695 if (!strlen(strp)) {
2696 error("Invalid --fields key: `+'");
2697 goto out;
2698 }
2699
Jiri Olsa07600022016-01-18 10:24:16 +01002700 ret = setup_output_list(&perf_hpp_list, strp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002701
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002702out:
Namhyung Kima7d945b2014-03-04 10:46:34 +09002703 free(str);
2704 return ret;
2705}
2706
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002707int setup_sorting(struct perf_evlist *evlist)
2708{
2709 int err;
2710
2711 err = __setup_sorting(evlist);
2712 if (err < 0)
2713 return err;
2714
2715 if (parent_pattern != default_parent_pattern) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002716 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002717 if (err < 0)
2718 return err;
2719 }
2720
Namhyung Kima7d945b2014-03-04 10:46:34 +09002721 reset_dimensions();
2722
2723 /*
2724 * perf diff doesn't use default hpp output fields.
2725 */
2726 if (sort__mode != SORT_MODE__DIFF)
2727 perf_hpp__init();
2728
2729 err = __setup_output_field();
2730 if (err < 0)
2731 return err;
2732
2733 /* copy sort keys to output fields */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002734 perf_hpp__setup_output_field(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002735 /* and then copy output fields to sort keys */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002736 perf_hpp__append_sort_keys(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002737
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002738 /* setup hists-specific output fields */
2739 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2740 return -1;
2741
Namhyung Kima7d945b2014-03-04 10:46:34 +09002742 return 0;
2743}
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002744
2745void reset_output_field(void)
2746{
Jiri Olsa52225032016-05-03 13:54:42 +02002747 perf_hpp_list.need_collapse = 0;
Jiri Olsade7e6a72016-05-03 13:54:43 +02002748 perf_hpp_list.parent = 0;
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002749 sort__has_sym = 0;
2750 sort__has_dso = 0;
2751
Namhyung Kimd69b2962014-05-23 10:59:01 +09002752 field_order = NULL;
2753 sort_order = NULL;
2754
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002755 reset_dimensions();
Jiri Olsa43e0a682016-01-18 10:24:21 +01002756 perf_hpp__reset_output_field(&perf_hpp_list);
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002757}