blob: 544ab376ac42c71ccb16eafb61b598fd21c67a62 [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 Kim68f6d022013-12-18 14:21:10 +090024int sort__has_dso = 0;
Kan Liang2e7ea3a2015-09-04 10:45:43 -040025int sort__has_socket = 0;
Namhyung Kimcfd92da2016-01-21 19:13:24 -030026int sort__has_thread = 0;
Namhyung Kim078b8d42016-03-09 23:20:51 +090027int sort__has_comm = 0;
Namhyung Kim55369fc2013-04-01 20:35:20 +090028enum sort_mode sort__mode = SORT_MODE__NORMAL;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020029
Arnaldo Carvalho de Melo37d9bb52016-02-12 11:27:51 -030030/*
31 * Replaces all occurrences of a char used with the:
32 *
33 * -t, --field-separator
34 *
35 * option, that uses a special separator character and don't pad with spaces,
36 * replacing all occurances of this separator in symbol names (and other
37 * output) with a '.' character, that thus it's the only non valid separator.
38*/
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030039static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020040{
41 int n;
42 va_list ap;
43
44 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030045 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020046 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030047 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020048
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030049 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020050 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030051 if (sep == NULL)
52 break;
53 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020054 }
John Kacurdd68ada2009-09-24 18:02:49 +020055 }
56 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110057
58 if (n >= (int)size)
59 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020060 return n;
61}
62
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020063static int64_t cmp_null(const void *l, const void *r)
Frederic Weisbecker872a8782011-06-29 03:14:52 +020064{
65 if (!l && !r)
66 return 0;
67 else if (!l)
68 return -1;
69 else
70 return 1;
71}
72
73/* --sort pid */
74
75static int64_t
76sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
77{
Adrian Hunter38051232013-07-04 16:20:31 +030078 return right->thread->tid - left->thread->tid;
Frederic Weisbecker872a8782011-06-29 03:14:52 +020079}
80
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030081static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030082 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020083{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020084 const char *comm = thread__comm_str(he->thread);
Namhyung Kim5b591662014-07-31 14:47:38 +090085
86 width = max(7U, width) - 6;
87 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
88 width, width, comm ?: "");
John Kacurdd68ada2009-09-24 18:02:49 +020089}
90
Namhyung Kim54430102016-02-25 00:13:37 +090091static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
92{
93 const struct thread *th = arg;
94
95 if (type != HIST_FILTER__THREAD)
96 return -1;
97
98 return th && he->thread != th;
99}
100
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200101struct sort_entry sort_thread = {
Namhyung Kim8246de82014-07-31 14:47:35 +0900102 .se_header = " Pid:Command",
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200103 .se_cmp = sort__thread_cmp,
104 .se_snprintf = hist_entry__thread_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900105 .se_filter = hist_entry__thread_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200106 .se_width_idx = HISTC_THREAD,
107};
108
109/* --sort comm */
110
111static int64_t
112sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
113{
Frederic Weisbeckerfedd63d2013-09-11 17:18:09 +0200114 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200115 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200116}
117
118static int64_t
119sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
120{
Namhyung Kim4dfced32013-09-13 16:28:57 +0900121 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200122 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200123}
124
Namhyung Kim202e7a62014-03-04 11:01:41 +0900125static int64_t
126sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
127{
128 return strcmp(comm__str(right->comm), comm__str(left->comm));
129}
130
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300131static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300132 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200133{
Namhyung Kim5b591662014-07-31 14:47:38 +0900134 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
John Kacurdd68ada2009-09-24 18:02:49 +0200135}
136
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900137struct sort_entry sort_comm = {
138 .se_header = "Command",
139 .se_cmp = sort__comm_cmp,
140 .se_collapse = sort__comm_collapse,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900141 .se_sort = sort__comm_sort,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900142 .se_snprintf = hist_entry__comm_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900143 .se_filter = hist_entry__thread_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900144 .se_width_idx = HISTC_COMM,
145};
146
147/* --sort dso */
148
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100149static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200150{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100151 struct dso *dso_l = map_l ? map_l->dso : NULL;
152 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300153 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200154
155 if (!dso_l || !dso_r)
Namhyung Kim202e7a62014-03-04 11:01:41 +0900156 return cmp_null(dso_r, dso_l);
John Kacurdd68ada2009-09-24 18:02:49 +0200157
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300158 if (verbose) {
159 dso_name_l = dso_l->long_name;
160 dso_name_r = dso_r->long_name;
161 } else {
162 dso_name_l = dso_l->short_name;
163 dso_name_r = dso_r->short_name;
164 }
165
166 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200167}
168
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100169static int64_t
170sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200171{
Namhyung Kim202e7a62014-03-04 11:01:41 +0900172 return _sort__dso_cmp(right->ms.map, left->ms.map);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100173}
174
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100175static int _hist_entry__dso_snprintf(struct map *map, char *bf,
176 size_t size, unsigned int width)
177{
178 if (map && map->dso) {
179 const char *dso_name = !verbose ? map->dso->short_name :
180 map->dso->long_name;
Namhyung Kim5b591662014-07-31 14:47:38 +0900181 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300182 }
John Kacurdd68ada2009-09-24 18:02:49 +0200183
Namhyung Kim5b591662014-07-31 14:47:38 +0900184 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200185}
186
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300187static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100188 size_t size, unsigned int width)
189{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300190 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100191}
192
Namhyung Kim54430102016-02-25 00:13:37 +0900193static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
194{
195 const struct dso *dso = arg;
196
197 if (type != HIST_FILTER__DSO)
198 return -1;
199
200 return dso && (!he->ms.map || he->ms.map->dso != dso);
201}
202
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900203struct sort_entry sort_dso = {
204 .se_header = "Shared Object",
205 .se_cmp = sort__dso_cmp,
206 .se_snprintf = hist_entry__dso_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900207 .se_filter = hist_entry__dso_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900208 .se_width_idx = HISTC_DSO,
209};
210
211/* --sort symbol */
212
Namhyung Kim2037be52013-12-18 14:21:09 +0900213static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
214{
215 return (int64_t)(right_ip - left_ip);
216}
217
Namhyung Kim51f27d12013-02-06 14:57:15 +0900218static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900219{
220 if (!sym_l || !sym_r)
221 return cmp_null(sym_l, sym_r);
222
223 if (sym_l == sym_r)
224 return 0;
225
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700226 if (sym_l->start != sym_r->start)
227 return (int64_t)(sym_r->start - sym_l->start);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900228
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700229 return (int64_t)(sym_r->end - sym_l->end);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900230}
231
232static int64_t
233sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
234{
Namhyung Kim09600e02013-10-15 11:01:56 +0900235 int64_t ret;
236
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900237 if (!left->ms.sym && !right->ms.sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900238 return _sort__addr_cmp(left->ip, right->ip);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900239
Namhyung Kim09600e02013-10-15 11:01:56 +0900240 /*
241 * comparing symbol address alone is not enough since it's a
242 * relative address within a dso.
243 */
Namhyung Kim68f6d022013-12-18 14:21:10 +0900244 if (!sort__has_dso) {
245 ret = sort__dso_cmp(left, right);
246 if (ret != 0)
247 return ret;
248 }
Namhyung Kim09600e02013-10-15 11:01:56 +0900249
Namhyung Kim51f27d12013-02-06 14:57:15 +0900250 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900251}
252
Namhyung Kim202e7a62014-03-04 11:01:41 +0900253static int64_t
254sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
255{
256 if (!left->ms.sym || !right->ms.sym)
257 return cmp_null(left->ms.sym, right->ms.sym);
258
259 return strcmp(right->ms.sym->name, left->ms.sym->name);
260}
261
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100262static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
263 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900264 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100265{
266 size_t ret = 0;
267
268 if (verbose) {
269 char o = map ? dso__symtab_origin(map->dso) : '!';
270 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Namhyung Kimded19d52013-04-01 20:35:19 +0900271 BITS_PER_LONG / 4 + 2, ip, o);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100272 }
273
274 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100275 if (sym && map) {
276 if (map->type == MAP__VARIABLE) {
277 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
278 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
Namhyung Kim62667742013-01-24 16:10:42 +0100279 ip - map->unmap_ip(map, sym->start));
Stephane Eranian98a3b322013-01-24 16:10:35 +0100280 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300281 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
Stephane Eranian98a3b322013-01-24 16:10:35 +0100282 width - ret,
283 sym->name);
284 }
285 } else {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100286 size_t len = BITS_PER_LONG / 4;
287 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
288 len, ip);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100289 }
290
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300291 return ret;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100292}
293
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300294static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900295 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100296{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300297 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
298 he->level, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100299}
John Kacurdd68ada2009-09-24 18:02:49 +0200300
Namhyung Kim54430102016-02-25 00:13:37 +0900301static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
302{
303 const char *sym = arg;
304
305 if (type != HIST_FILTER__SYMBOL)
306 return -1;
307
308 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
309}
310
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200311struct sort_entry sort_sym = {
312 .se_header = "Symbol",
313 .se_cmp = sort__sym_cmp,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900314 .se_sort = sort__sym_sort,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200315 .se_snprintf = hist_entry__sym_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900316 .se_filter = hist_entry__sym_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200317 .se_width_idx = HISTC_SYMBOL,
318};
John Kacurdd68ada2009-09-24 18:02:49 +0200319
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300320/* --sort srcline */
321
Namhyung Kimcecaec62016-02-22 09:31:51 +0900322static char *hist_entry__get_srcline(struct hist_entry *he)
323{
324 struct map *map = he->ms.map;
325
326 if (!map)
327 return SRCLINE_UNKNOWN;
328
329 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
330 he->ms.sym, true);
331}
332
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300333static int64_t
334sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
335{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900336 if (!left->srcline)
337 left->srcline = hist_entry__get_srcline(left);
338 if (!right->srcline)
339 right->srcline = hist_entry__get_srcline(right);
340
Namhyung Kim202e7a62014-03-04 11:01:41 +0900341 return strcmp(right->srcline, left->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300342}
343
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300344static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim5b591662014-07-31 14:47:38 +0900345 size_t size, unsigned int width)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300346{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900347 if (!he->srcline)
348 he->srcline = hist_entry__get_srcline(he);
349
Namhyung Kim2960ed62016-02-22 09:32:33 +0900350 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300351}
352
353struct sort_entry sort_srcline = {
354 .se_header = "Source:Line",
355 .se_cmp = sort__srcline_cmp,
356 .se_snprintf = hist_entry__srcline_snprintf,
357 .se_width_idx = HISTC_SRCLINE,
358};
359
Andi Kleen31191a82015-08-07 15:54:24 -0700360/* --sort srcfile */
361
362static char no_srcfile[1];
363
Namhyung Kimcecaec62016-02-22 09:31:51 +0900364static char *hist_entry__get_srcfile(struct hist_entry *e)
Andi Kleen31191a82015-08-07 15:54:24 -0700365{
366 char *sf, *p;
367 struct map *map = e->ms.map;
368
Namhyung Kimcecaec62016-02-22 09:31:51 +0900369 if (!map)
370 return no_srcfile;
371
Andi Kleen2f84b422015-09-01 11:47:19 -0700372 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
373 e->ms.sym, false, true);
Andi Kleen76b10652015-08-11 06:36:55 -0700374 if (!strcmp(sf, SRCLINE_UNKNOWN))
375 return no_srcfile;
Andi Kleen31191a82015-08-07 15:54:24 -0700376 p = strchr(sf, ':');
377 if (p && *sf) {
378 *p = 0;
379 return sf;
380 }
381 free(sf);
382 return no_srcfile;
383}
384
385static int64_t
386sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
387{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900388 if (!left->srcfile)
389 left->srcfile = hist_entry__get_srcfile(left);
390 if (!right->srcfile)
391 right->srcfile = hist_entry__get_srcfile(right);
392
Andi Kleen31191a82015-08-07 15:54:24 -0700393 return strcmp(right->srcfile, left->srcfile);
394}
395
396static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
397 size_t size, unsigned int width)
398{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900399 if (!he->srcfile)
400 he->srcfile = hist_entry__get_srcfile(he);
401
Namhyung Kim2960ed62016-02-22 09:32:33 +0900402 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
Andi Kleen31191a82015-08-07 15:54:24 -0700403}
404
405struct sort_entry sort_srcfile = {
406 .se_header = "Source File",
407 .se_cmp = sort__srcfile_cmp,
408 .se_snprintf = hist_entry__srcfile_snprintf,
409 .se_width_idx = HISTC_SRCFILE,
410};
411
John Kacurdd68ada2009-09-24 18:02:49 +0200412/* --sort parent */
413
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200414static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200415sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
416{
417 struct symbol *sym_l = left->parent;
418 struct symbol *sym_r = right->parent;
419
420 if (!sym_l || !sym_r)
421 return cmp_null(sym_l, sym_r);
422
Namhyung Kim202e7a62014-03-04 11:01:41 +0900423 return strcmp(sym_r->name, sym_l->name);
John Kacurdd68ada2009-09-24 18:02:49 +0200424}
425
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300426static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300427 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200428{
Namhyung Kim5b591662014-07-31 14:47:38 +0900429 return repsep_snprintf(bf, size, "%-*.*s", width, width,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300430 he->parent ? he->parent->name : "[other]");
John Kacurdd68ada2009-09-24 18:02:49 +0200431}
432
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200433struct sort_entry sort_parent = {
434 .se_header = "Parent symbol",
435 .se_cmp = sort__parent_cmp,
436 .se_snprintf = hist_entry__parent_snprintf,
437 .se_width_idx = HISTC_PARENT,
438};
439
Arun Sharmaf60f3592010-06-04 11:27:10 -0300440/* --sort cpu */
441
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200442static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300443sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
444{
445 return right->cpu - left->cpu;
446}
447
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300448static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
449 size_t size, unsigned int width)
Arun Sharmaf60f3592010-06-04 11:27:10 -0300450{
Namhyung Kim5b591662014-07-31 14:47:38 +0900451 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300452}
453
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200454struct sort_entry sort_cpu = {
455 .se_header = "CPU",
456 .se_cmp = sort__cpu_cmp,
457 .se_snprintf = hist_entry__cpu_snprintf,
458 .se_width_idx = HISTC_CPU,
459};
460
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400461/* --sort socket */
462
463static int64_t
464sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
465{
466 return right->socket - left->socket;
467}
468
469static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
470 size_t size, unsigned int width)
471{
472 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
473}
474
Namhyung Kim54430102016-02-25 00:13:37 +0900475static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
476{
477 int sk = *(const int *)arg;
478
479 if (type != HIST_FILTER__SOCKET)
480 return -1;
481
482 return sk >= 0 && he->socket != sk;
483}
484
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400485struct sort_entry sort_socket = {
486 .se_header = "Socket",
487 .se_cmp = sort__socket_cmp,
488 .se_snprintf = hist_entry__socket_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900489 .se_filter = hist_entry__socket_filter,
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400490 .se_width_idx = HISTC_SOCKET,
491};
492
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900493/* --sort trace */
494
495static char *get_trace_output(struct hist_entry *he)
496{
497 struct trace_seq seq;
498 struct perf_evsel *evsel;
499 struct pevent_record rec = {
500 .data = he->raw_data,
501 .size = he->raw_size,
502 };
503
504 evsel = hists_to_evsel(he->hists);
505
506 trace_seq_init(&seq);
Namhyung Kim053a3982015-12-23 02:07:05 +0900507 if (symbol_conf.raw_trace) {
508 pevent_print_fields(&seq, he->raw_data, he->raw_size,
509 evsel->tp_format);
510 } else {
511 pevent_event_info(&seq, evsel->tp_format, &rec);
512 }
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900513 return seq.buffer;
514}
515
516static int64_t
517sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
518{
519 struct perf_evsel *evsel;
520
521 evsel = hists_to_evsel(left->hists);
522 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
523 return 0;
524
525 if (left->trace_output == NULL)
526 left->trace_output = get_trace_output(left);
527 if (right->trace_output == NULL)
528 right->trace_output = get_trace_output(right);
529
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900530 return strcmp(right->trace_output, left->trace_output);
531}
532
533static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
534 size_t size, unsigned int width)
535{
536 struct perf_evsel *evsel;
537
538 evsel = hists_to_evsel(he->hists);
539 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
Namhyung Kim2960ed62016-02-22 09:32:33 +0900540 return scnprintf(bf, size, "%-.*s", width, "N/A");
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900541
542 if (he->trace_output == NULL)
543 he->trace_output = get_trace_output(he);
Namhyung Kim2960ed62016-02-22 09:32:33 +0900544 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900545}
546
547struct sort_entry sort_trace = {
548 .se_header = "Trace output",
549 .se_cmp = sort__trace_cmp,
550 .se_snprintf = hist_entry__trace_snprintf,
551 .se_width_idx = HISTC_TRACE,
552};
553
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900554/* sort keys for branch stacks */
555
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100556static int64_t
557sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
558{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200559 if (!left->branch_info || !right->branch_info)
560 return cmp_null(left->branch_info, right->branch_info);
561
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100562 return _sort__dso_cmp(left->branch_info->from.map,
563 right->branch_info->from.map);
564}
565
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300566static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100567 size_t size, unsigned int width)
568{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200569 if (he->branch_info)
570 return _hist_entry__dso_snprintf(he->branch_info->from.map,
571 bf, size, width);
572 else
573 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100574}
575
Namhyung Kim54430102016-02-25 00:13:37 +0900576static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
577 const void *arg)
578{
579 const struct dso *dso = arg;
580
581 if (type != HIST_FILTER__DSO)
582 return -1;
583
584 return dso && (!he->branch_info || !he->branch_info->from.map ||
585 he->branch_info->from.map->dso != dso);
586}
587
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100588static int64_t
589sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
590{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200591 if (!left->branch_info || !right->branch_info)
592 return cmp_null(left->branch_info, right->branch_info);
593
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100594 return _sort__dso_cmp(left->branch_info->to.map,
595 right->branch_info->to.map);
596}
597
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300598static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100599 size_t size, unsigned int width)
600{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200601 if (he->branch_info)
602 return _hist_entry__dso_snprintf(he->branch_info->to.map,
603 bf, size, width);
604 else
605 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100606}
607
Namhyung Kim54430102016-02-25 00:13:37 +0900608static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
609 const void *arg)
610{
611 const struct dso *dso = arg;
612
613 if (type != HIST_FILTER__DSO)
614 return -1;
615
616 return dso && (!he->branch_info || !he->branch_info->to.map ||
617 he->branch_info->to.map->dso != dso);
618}
619
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100620static int64_t
621sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
622{
623 struct addr_map_symbol *from_l = &left->branch_info->from;
624 struct addr_map_symbol *from_r = &right->branch_info->from;
625
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200626 if (!left->branch_info || !right->branch_info)
627 return cmp_null(left->branch_info, right->branch_info);
628
629 from_l = &left->branch_info->from;
630 from_r = &right->branch_info->from;
631
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100632 if (!from_l->sym && !from_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900633 return _sort__addr_cmp(from_l->addr, from_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100634
Namhyung Kim51f27d12013-02-06 14:57:15 +0900635 return _sort__sym_cmp(from_l->sym, from_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100636}
637
638static int64_t
639sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
640{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200641 struct addr_map_symbol *to_l, *to_r;
642
643 if (!left->branch_info || !right->branch_info)
644 return cmp_null(left->branch_info, right->branch_info);
645
646 to_l = &left->branch_info->to;
647 to_r = &right->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100648
649 if (!to_l->sym && !to_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900650 return _sort__addr_cmp(to_l->addr, to_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100651
Namhyung Kim51f27d12013-02-06 14:57:15 +0900652 return _sort__sym_cmp(to_l->sym, to_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100653}
654
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300655static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900656 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100657{
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200658 if (he->branch_info) {
659 struct addr_map_symbol *from = &he->branch_info->from;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100660
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200661 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
662 he->level, bf, size, width);
663 }
664
665 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100666}
667
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300668static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900669 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100670{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200671 if (he->branch_info) {
672 struct addr_map_symbol *to = &he->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100673
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200674 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
675 he->level, bf, size, width);
676 }
677
678 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100679}
680
Namhyung Kim54430102016-02-25 00:13:37 +0900681static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
682 const void *arg)
683{
684 const char *sym = arg;
685
686 if (type != HIST_FILTER__SYMBOL)
687 return -1;
688
689 return sym && !(he->branch_info && he->branch_info->from.sym &&
690 strstr(he->branch_info->from.sym->name, sym));
691}
692
693static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
694 const void *arg)
695{
696 const char *sym = arg;
697
698 if (type != HIST_FILTER__SYMBOL)
699 return -1;
700
701 return sym && !(he->branch_info && he->branch_info->to.sym &&
702 strstr(he->branch_info->to.sym->name, sym));
703}
704
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900705struct sort_entry sort_dso_from = {
706 .se_header = "Source Shared Object",
707 .se_cmp = sort__dso_from_cmp,
708 .se_snprintf = hist_entry__dso_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900709 .se_filter = hist_entry__dso_from_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900710 .se_width_idx = HISTC_DSO_FROM,
711};
712
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100713struct sort_entry sort_dso_to = {
714 .se_header = "Target Shared Object",
715 .se_cmp = sort__dso_to_cmp,
716 .se_snprintf = hist_entry__dso_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900717 .se_filter = hist_entry__dso_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100718 .se_width_idx = HISTC_DSO_TO,
719};
720
721struct sort_entry sort_sym_from = {
722 .se_header = "Source Symbol",
723 .se_cmp = sort__sym_from_cmp,
724 .se_snprintf = hist_entry__sym_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900725 .se_filter = hist_entry__sym_from_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100726 .se_width_idx = HISTC_SYMBOL_FROM,
727};
728
729struct sort_entry sort_sym_to = {
730 .se_header = "Target Symbol",
731 .se_cmp = sort__sym_to_cmp,
732 .se_snprintf = hist_entry__sym_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900733 .se_filter = hist_entry__sym_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100734 .se_width_idx = HISTC_SYMBOL_TO,
735};
736
737static int64_t
738sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
739{
Jiri Olsa428560e2014-10-16 16:07:03 +0200740 unsigned char mp, p;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100741
Jiri Olsa428560e2014-10-16 16:07:03 +0200742 if (!left->branch_info || !right->branch_info)
743 return cmp_null(left->branch_info, right->branch_info);
744
745 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
746 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100747 return mp || p;
748}
749
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300750static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100751 size_t size, unsigned int width){
752 static const char *out = "N/A";
753
Jiri Olsa428560e2014-10-16 16:07:03 +0200754 if (he->branch_info) {
755 if (he->branch_info->flags.predicted)
756 out = "N";
757 else if (he->branch_info->flags.mispred)
758 out = "Y";
759 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100760
Namhyung Kim5b591662014-07-31 14:47:38 +0900761 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100762}
763
Andi Kleen0e332f02015-07-18 08:24:46 -0700764static int64_t
765sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
766{
767 return left->branch_info->flags.cycles -
768 right->branch_info->flags.cycles;
769}
770
771static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
772 size_t size, unsigned int width)
773{
774 if (he->branch_info->flags.cycles == 0)
775 return repsep_snprintf(bf, size, "%-*s", width, "-");
776 return repsep_snprintf(bf, size, "%-*hd", width,
777 he->branch_info->flags.cycles);
778}
779
780struct sort_entry sort_cycles = {
781 .se_header = "Basic Block Cycles",
782 .se_cmp = sort__cycles_cmp,
783 .se_snprintf = hist_entry__cycles_snprintf,
784 .se_width_idx = HISTC_CYCLES,
785};
786
Stephane Eranian98a3b322013-01-24 16:10:35 +0100787/* --sort daddr_sym */
788static int64_t
789sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
790{
791 uint64_t l = 0, r = 0;
792
793 if (left->mem_info)
794 l = left->mem_info->daddr.addr;
795 if (right->mem_info)
796 r = right->mem_info->daddr.addr;
797
798 return (int64_t)(r - l);
799}
800
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300801static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100802 size_t size, unsigned int width)
803{
804 uint64_t addr = 0;
805 struct map *map = NULL;
806 struct symbol *sym = NULL;
807
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300808 if (he->mem_info) {
809 addr = he->mem_info->daddr.addr;
810 map = he->mem_info->daddr.map;
811 sym = he->mem_info->daddr.sym;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100812 }
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300813 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100814 width);
815}
816
817static int64_t
Don Zickus28e6db22015-10-05 20:06:07 +0200818sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
819{
820 uint64_t l = 0, r = 0;
821
822 if (left->mem_info)
823 l = left->mem_info->iaddr.addr;
824 if (right->mem_info)
825 r = right->mem_info->iaddr.addr;
826
827 return (int64_t)(r - l);
828}
829
830static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
831 size_t size, unsigned int width)
832{
833 uint64_t addr = 0;
834 struct map *map = NULL;
835 struct symbol *sym = NULL;
836
837 if (he->mem_info) {
838 addr = he->mem_info->iaddr.addr;
839 map = he->mem_info->iaddr.map;
840 sym = he->mem_info->iaddr.sym;
841 }
842 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
843 width);
844}
845
846static int64_t
Stephane Eranian98a3b322013-01-24 16:10:35 +0100847sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
848{
849 struct map *map_l = NULL;
850 struct map *map_r = NULL;
851
852 if (left->mem_info)
853 map_l = left->mem_info->daddr.map;
854 if (right->mem_info)
855 map_r = right->mem_info->daddr.map;
856
857 return _sort__dso_cmp(map_l, map_r);
858}
859
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300860static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100861 size_t size, unsigned int width)
862{
863 struct map *map = NULL;
864
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300865 if (he->mem_info)
866 map = he->mem_info->daddr.map;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100867
868 return _hist_entry__dso_snprintf(map, bf, size, width);
869}
870
871static int64_t
872sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
873{
874 union perf_mem_data_src data_src_l;
875 union perf_mem_data_src data_src_r;
876
877 if (left->mem_info)
878 data_src_l = left->mem_info->data_src;
879 else
880 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
881
882 if (right->mem_info)
883 data_src_r = right->mem_info->data_src;
884 else
885 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
886
887 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
888}
889
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300890static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100891 size_t size, unsigned int width)
892{
Jiri Olsa69a77272016-02-24 09:46:49 +0100893 char out[10];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100894
Jiri Olsa69a77272016-02-24 09:46:49 +0100895 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300896 return repsep_snprintf(bf, size, "%.*s", width, out);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100897}
898
899static int64_t
900sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
901{
902 union perf_mem_data_src data_src_l;
903 union perf_mem_data_src data_src_r;
904
905 if (left->mem_info)
906 data_src_l = left->mem_info->data_src;
907 else
908 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
909
910 if (right->mem_info)
911 data_src_r = right->mem_info->data_src;
912 else
913 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
914
915 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
916}
917
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300918static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100919 size_t size, unsigned int width)
920{
921 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100922
Jiri Olsa0c877d72016-02-24 09:46:46 +0100923 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100924 return repsep_snprintf(bf, size, "%-*s", width, out);
925}
926
927static int64_t
928sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
929{
930 union perf_mem_data_src data_src_l;
931 union perf_mem_data_src data_src_r;
932
933 if (left->mem_info)
934 data_src_l = left->mem_info->data_src;
935 else
936 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
937
938 if (right->mem_info)
939 data_src_r = right->mem_info->data_src;
940 else
941 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
942
943 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
944}
945
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300946static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100947 size_t size, unsigned int width)
948{
949 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100950
Jiri Olsa071e9a12016-02-24 09:46:47 +0100951 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100952 return repsep_snprintf(bf, size, "%-*s", width, out);
953}
954
955static int64_t
956sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
957{
958 union perf_mem_data_src data_src_l;
959 union perf_mem_data_src data_src_r;
960
961 if (left->mem_info)
962 data_src_l = left->mem_info->data_src;
963 else
964 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
965
966 if (right->mem_info)
967 data_src_r = right->mem_info->data_src;
968 else
969 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
970
971 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
972}
973
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300974static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100975 size_t size, unsigned int width)
976{
977 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100978
Jiri Olsa2c07af12016-02-24 09:46:48 +0100979 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100980 return repsep_snprintf(bf, size, "%-*s", width, out);
981}
982
Don Zickus9b32ba72014-06-01 15:38:29 +0200983static int64_t
984sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
985{
986 u64 l, r;
987 struct map *l_map, *r_map;
988
989 if (!left->mem_info) return -1;
990 if (!right->mem_info) return 1;
991
992 /* group event types together */
993 if (left->cpumode > right->cpumode) return -1;
994 if (left->cpumode < right->cpumode) return 1;
995
996 l_map = left->mem_info->daddr.map;
997 r_map = right->mem_info->daddr.map;
998
999 /* if both are NULL, jump to sort on al_addr instead */
1000 if (!l_map && !r_map)
1001 goto addr;
1002
1003 if (!l_map) return -1;
1004 if (!r_map) return 1;
1005
1006 if (l_map->maj > r_map->maj) return -1;
1007 if (l_map->maj < r_map->maj) return 1;
1008
1009 if (l_map->min > r_map->min) return -1;
1010 if (l_map->min < r_map->min) return 1;
1011
1012 if (l_map->ino > r_map->ino) return -1;
1013 if (l_map->ino < r_map->ino) return 1;
1014
1015 if (l_map->ino_generation > r_map->ino_generation) return -1;
1016 if (l_map->ino_generation < r_map->ino_generation) return 1;
1017
1018 /*
1019 * Addresses with no major/minor numbers are assumed to be
1020 * anonymous in userspace. Sort those on pid then address.
1021 *
1022 * The kernel and non-zero major/minor mapped areas are
1023 * assumed to be unity mapped. Sort those on address.
1024 */
1025
1026 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1027 (!(l_map->flags & MAP_SHARED)) &&
1028 !l_map->maj && !l_map->min && !l_map->ino &&
1029 !l_map->ino_generation) {
1030 /* userspace anonymous */
1031
1032 if (left->thread->pid_ > right->thread->pid_) return -1;
1033 if (left->thread->pid_ < right->thread->pid_) return 1;
1034 }
1035
1036addr:
1037 /* al_addr does all the right addr - start + offset calculations */
1038 l = cl_address(left->mem_info->daddr.al_addr);
1039 r = cl_address(right->mem_info->daddr.al_addr);
1040
1041 if (l > r) return -1;
1042 if (l < r) return 1;
1043
1044 return 0;
1045}
1046
1047static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1048 size_t size, unsigned int width)
1049{
1050
1051 uint64_t addr = 0;
1052 struct map *map = NULL;
1053 struct symbol *sym = NULL;
1054 char level = he->level;
1055
1056 if (he->mem_info) {
1057 addr = cl_address(he->mem_info->daddr.al_addr);
1058 map = he->mem_info->daddr.map;
1059 sym = he->mem_info->daddr.sym;
1060
1061 /* print [s] for shared data mmaps */
1062 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1063 map && (map->type == MAP__VARIABLE) &&
1064 (map->flags & MAP_SHARED) &&
1065 (map->maj || map->min || map->ino ||
1066 map->ino_generation))
1067 level = 's';
1068 else if (!map)
1069 level = 'X';
1070 }
1071 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1072 width);
1073}
1074
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001075struct sort_entry sort_mispredict = {
1076 .se_header = "Branch Mispredicted",
1077 .se_cmp = sort__mispredict_cmp,
1078 .se_snprintf = hist_entry__mispredict_snprintf,
1079 .se_width_idx = HISTC_MISPREDICT,
1080};
1081
Andi Kleen05484292013-01-24 16:10:29 +01001082static u64 he_weight(struct hist_entry *he)
1083{
1084 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1085}
1086
1087static int64_t
1088sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1089{
1090 return he_weight(left) - he_weight(right);
1091}
1092
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001093static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001094 size_t size, unsigned int width)
1095{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001096 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
Andi Kleen05484292013-01-24 16:10:29 +01001097}
1098
1099struct sort_entry sort_local_weight = {
1100 .se_header = "Local Weight",
1101 .se_cmp = sort__local_weight_cmp,
1102 .se_snprintf = hist_entry__local_weight_snprintf,
1103 .se_width_idx = HISTC_LOCAL_WEIGHT,
1104};
1105
1106static int64_t
1107sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1108{
1109 return left->stat.weight - right->stat.weight;
1110}
1111
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001112static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001113 size_t size, unsigned int width)
1114{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001115 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
Andi Kleen05484292013-01-24 16:10:29 +01001116}
1117
1118struct sort_entry sort_global_weight = {
1119 .se_header = "Weight",
1120 .se_cmp = sort__global_weight_cmp,
1121 .se_snprintf = hist_entry__global_weight_snprintf,
1122 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1123};
1124
Stephane Eranian98a3b322013-01-24 16:10:35 +01001125struct sort_entry sort_mem_daddr_sym = {
1126 .se_header = "Data Symbol",
1127 .se_cmp = sort__daddr_cmp,
1128 .se_snprintf = hist_entry__daddr_snprintf,
1129 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1130};
1131
Don Zickus28e6db22015-10-05 20:06:07 +02001132struct sort_entry sort_mem_iaddr_sym = {
1133 .se_header = "Code Symbol",
1134 .se_cmp = sort__iaddr_cmp,
1135 .se_snprintf = hist_entry__iaddr_snprintf,
1136 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1137};
1138
Stephane Eranian98a3b322013-01-24 16:10:35 +01001139struct sort_entry sort_mem_daddr_dso = {
1140 .se_header = "Data Object",
1141 .se_cmp = sort__dso_daddr_cmp,
1142 .se_snprintf = hist_entry__dso_daddr_snprintf,
1143 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1144};
1145
1146struct sort_entry sort_mem_locked = {
1147 .se_header = "Locked",
1148 .se_cmp = sort__locked_cmp,
1149 .se_snprintf = hist_entry__locked_snprintf,
1150 .se_width_idx = HISTC_MEM_LOCKED,
1151};
1152
1153struct sort_entry sort_mem_tlb = {
1154 .se_header = "TLB access",
1155 .se_cmp = sort__tlb_cmp,
1156 .se_snprintf = hist_entry__tlb_snprintf,
1157 .se_width_idx = HISTC_MEM_TLB,
1158};
1159
1160struct sort_entry sort_mem_lvl = {
1161 .se_header = "Memory access",
1162 .se_cmp = sort__lvl_cmp,
1163 .se_snprintf = hist_entry__lvl_snprintf,
1164 .se_width_idx = HISTC_MEM_LVL,
1165};
1166
1167struct sort_entry sort_mem_snoop = {
1168 .se_header = "Snoop",
1169 .se_cmp = sort__snoop_cmp,
1170 .se_snprintf = hist_entry__snoop_snprintf,
1171 .se_width_idx = HISTC_MEM_SNOOP,
1172};
1173
Don Zickus9b32ba72014-06-01 15:38:29 +02001174struct sort_entry sort_mem_dcacheline = {
1175 .se_header = "Data Cacheline",
1176 .se_cmp = sort__dcacheline_cmp,
1177 .se_snprintf = hist_entry__dcacheline_snprintf,
1178 .se_width_idx = HISTC_MEM_DCACHELINE,
1179};
1180
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001181static int64_t
1182sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1183{
Jiri Olsa49f47442014-10-16 16:07:01 +02001184 if (!left->branch_info || !right->branch_info)
1185 return cmp_null(left->branch_info, right->branch_info);
1186
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001187 return left->branch_info->flags.abort !=
1188 right->branch_info->flags.abort;
1189}
1190
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001191static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001192 size_t size, unsigned int width)
1193{
Jiri Olsa49f47442014-10-16 16:07:01 +02001194 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001195
Jiri Olsa49f47442014-10-16 16:07:01 +02001196 if (he->branch_info) {
1197 if (he->branch_info->flags.abort)
1198 out = "A";
1199 else
1200 out = ".";
1201 }
1202
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001203 return repsep_snprintf(bf, size, "%-*s", width, out);
1204}
1205
1206struct sort_entry sort_abort = {
1207 .se_header = "Transaction abort",
1208 .se_cmp = sort__abort_cmp,
1209 .se_snprintf = hist_entry__abort_snprintf,
1210 .se_width_idx = HISTC_ABORT,
1211};
1212
1213static int64_t
1214sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1215{
Jiri Olsa0199d242014-10-16 16:07:02 +02001216 if (!left->branch_info || !right->branch_info)
1217 return cmp_null(left->branch_info, right->branch_info);
1218
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001219 return left->branch_info->flags.in_tx !=
1220 right->branch_info->flags.in_tx;
1221}
1222
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001223static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001224 size_t size, unsigned int width)
1225{
Jiri Olsa0199d242014-10-16 16:07:02 +02001226 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001227
Jiri Olsa0199d242014-10-16 16:07:02 +02001228 if (he->branch_info) {
1229 if (he->branch_info->flags.in_tx)
1230 out = "T";
1231 else
1232 out = ".";
1233 }
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001234
1235 return repsep_snprintf(bf, size, "%-*s", width, out);
1236}
1237
1238struct sort_entry sort_in_tx = {
1239 .se_header = "Branch in transaction",
1240 .se_cmp = sort__in_tx_cmp,
1241 .se_snprintf = hist_entry__in_tx_snprintf,
1242 .se_width_idx = HISTC_IN_TX,
1243};
1244
Andi Kleen475eeab2013-09-20 07:40:43 -07001245static int64_t
1246sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1247{
1248 return left->transaction - right->transaction;
1249}
1250
1251static inline char *add_str(char *p, const char *str)
1252{
1253 strcpy(p, str);
1254 return p + strlen(str);
1255}
1256
1257static struct txbit {
1258 unsigned flag;
1259 const char *name;
1260 int skip_for_len;
1261} txbits[] = {
1262 { PERF_TXN_ELISION, "EL ", 0 },
1263 { PERF_TXN_TRANSACTION, "TX ", 1 },
1264 { PERF_TXN_SYNC, "SYNC ", 1 },
1265 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1266 { PERF_TXN_RETRY, "RETRY ", 0 },
1267 { PERF_TXN_CONFLICT, "CON ", 0 },
1268 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1269 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1270 { 0, NULL, 0 }
1271};
1272
1273int hist_entry__transaction_len(void)
1274{
1275 int i;
1276 int len = 0;
1277
1278 for (i = 0; txbits[i].name; i++) {
1279 if (!txbits[i].skip_for_len)
1280 len += strlen(txbits[i].name);
1281 }
1282 len += 4; /* :XX<space> */
1283 return len;
1284}
1285
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001286static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
Andi Kleen475eeab2013-09-20 07:40:43 -07001287 size_t size, unsigned int width)
1288{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001289 u64 t = he->transaction;
Andi Kleen475eeab2013-09-20 07:40:43 -07001290 char buf[128];
1291 char *p = buf;
1292 int i;
1293
1294 buf[0] = 0;
1295 for (i = 0; txbits[i].name; i++)
1296 if (txbits[i].flag & t)
1297 p = add_str(p, txbits[i].name);
1298 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1299 p = add_str(p, "NEITHER ");
1300 if (t & PERF_TXN_ABORT_MASK) {
1301 sprintf(p, ":%" PRIx64,
1302 (t & PERF_TXN_ABORT_MASK) >>
1303 PERF_TXN_ABORT_SHIFT);
1304 p += strlen(p);
1305 }
1306
1307 return repsep_snprintf(bf, size, "%-*s", width, buf);
1308}
1309
1310struct sort_entry sort_transaction = {
1311 .se_header = "Transaction ",
1312 .se_cmp = sort__transaction_cmp,
1313 .se_snprintf = hist_entry__transaction_snprintf,
1314 .se_width_idx = HISTC_TRANSACTION,
1315};
1316
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001317struct sort_dimension {
1318 const char *name;
1319 struct sort_entry *entry;
1320 int taken;
1321};
1322
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001323#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1324
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001325static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001326 DIM(SORT_PID, "pid", sort_thread),
1327 DIM(SORT_COMM, "comm", sort_comm),
1328 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001329 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001330 DIM(SORT_PARENT, "parent", sort_parent),
1331 DIM(SORT_CPU, "cpu", sort_cpu),
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001332 DIM(SORT_SOCKET, "socket", sort_socket),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -03001333 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Andi Kleen31191a82015-08-07 15:54:24 -07001334 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
Andi Kleenf9ea55d2013-07-18 15:58:53 -07001335 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1336 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
Andi Kleen475eeab2013-09-20 07:40:43 -07001337 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
Namhyung Kima34bb6a2015-12-23 02:07:04 +09001338 DIM(SORT_TRACE, "trace", sort_trace),
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001339};
1340
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001341#undef DIM
1342
1343#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1344
1345static struct sort_dimension bstack_sort_dimensions[] = {
1346 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1347 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1348 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1349 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1350 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001351 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1352 DIM(SORT_ABORT, "abort", sort_abort),
Andi Kleen0e332f02015-07-18 08:24:46 -07001353 DIM(SORT_CYCLES, "cycles", sort_cycles),
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001354};
1355
1356#undef DIM
1357
Namhyung Kimafab87b2013-04-03 21:26:11 +09001358#define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1359
1360static struct sort_dimension memory_sort_dimensions[] = {
Namhyung Kimafab87b2013-04-03 21:26:11 +09001361 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
Don Zickus28e6db22015-10-05 20:06:07 +02001362 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001363 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1364 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1365 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1366 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1367 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
Don Zickus9b32ba72014-06-01 15:38:29 +02001368 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001369};
1370
1371#undef DIM
1372
Namhyung Kima2ce0672014-03-04 09:06:42 +09001373struct hpp_dimension {
1374 const char *name;
1375 struct perf_hpp_fmt *fmt;
1376 int taken;
1377};
1378
1379#define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1380
1381static struct hpp_dimension hpp_sort_dimensions[] = {
1382 DIM(PERF_HPP__OVERHEAD, "overhead"),
1383 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1384 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1385 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1386 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
Namhyung Kim594dcbf2013-10-30 16:06:59 +09001387 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
Namhyung Kima2ce0672014-03-04 09:06:42 +09001388 DIM(PERF_HPP__SAMPLES, "sample"),
1389 DIM(PERF_HPP__PERIOD, "period"),
1390};
1391
1392#undef DIM
1393
Namhyung Kim8b536992014-03-03 11:46:55 +09001394struct hpp_sort_entry {
1395 struct perf_hpp_fmt hpp;
1396 struct sort_entry *se;
1397};
1398
Namhyung Kime0d66c72014-07-31 14:47:37 +09001399void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
Namhyung Kim678a5002014-03-20 11:18:54 +09001400{
1401 struct hpp_sort_entry *hse;
1402
1403 if (!perf_hpp__is_sort_entry(fmt))
1404 return;
1405
1406 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001407 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
Namhyung Kim678a5002014-03-20 11:18:54 +09001408}
1409
Namhyung Kim8b536992014-03-03 11:46:55 +09001410static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1411 struct perf_evsel *evsel)
1412{
1413 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001414 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001415
1416 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001417
Namhyung Kim5b591662014-07-31 14:47:38 +09001418 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001419 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001420
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001421 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
Namhyung Kim8b536992014-03-03 11:46:55 +09001422}
1423
1424static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1425 struct perf_hpp *hpp __maybe_unused,
1426 struct perf_evsel *evsel)
1427{
1428 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001429 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001430
1431 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1432
Namhyung Kim5b591662014-07-31 14:47:38 +09001433 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001434 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001435
1436 return len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001437}
1438
1439static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1440 struct hist_entry *he)
1441{
1442 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001443 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001444
1445 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim5b591662014-07-31 14:47:38 +09001446
1447 if (!len)
1448 len = hists__col_len(he->hists, hse->se->se_width_idx);
Namhyung Kim8b536992014-03-03 11:46:55 +09001449
1450 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1451}
1452
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001453static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1454 struct hist_entry *a, struct hist_entry *b)
1455{
1456 struct hpp_sort_entry *hse;
1457
1458 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1459 return hse->se->se_cmp(a, b);
1460}
1461
1462static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1463 struct hist_entry *a, struct hist_entry *b)
1464{
1465 struct hpp_sort_entry *hse;
1466 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1467
1468 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1469 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1470 return collapse_fn(a, b);
1471}
1472
1473static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1474 struct hist_entry *a, struct hist_entry *b)
1475{
1476 struct hpp_sort_entry *hse;
1477 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1478
1479 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1480 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1481 return sort_fn(a, b);
1482}
1483
Jiri Olsa97358082016-01-18 10:24:03 +01001484bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1485{
1486 return format->header == __sort__hpp_header;
1487}
1488
Namhyung Kim4945cf22016-03-09 22:46:57 +09001489#define MK_SORT_ENTRY_CHK(key) \
1490bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1491{ \
1492 struct hpp_sort_entry *hse; \
1493 \
1494 if (!perf_hpp__is_sort_entry(fmt)) \
1495 return false; \
1496 \
1497 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1498 return hse->se == &sort_ ## key ; \
Namhyung Kima9c6e462016-02-25 00:13:33 +09001499}
1500
Namhyung Kim4945cf22016-03-09 22:46:57 +09001501MK_SORT_ENTRY_CHK(trace)
1502MK_SORT_ENTRY_CHK(srcline)
1503MK_SORT_ENTRY_CHK(srcfile)
1504MK_SORT_ENTRY_CHK(thread)
1505MK_SORT_ENTRY_CHK(comm)
1506MK_SORT_ENTRY_CHK(dso)
1507MK_SORT_ENTRY_CHK(sym)
Namhyung Kima9c6e462016-02-25 00:13:33 +09001508
Namhyung Kima9c6e462016-02-25 00:13:33 +09001509
Jiri Olsa97358082016-01-18 10:24:03 +01001510static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1511{
1512 struct hpp_sort_entry *hse_a;
1513 struct hpp_sort_entry *hse_b;
1514
1515 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1516 return false;
1517
1518 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1519 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1520
1521 return hse_a->se == hse_b->se;
1522}
1523
Jiri Olsa564132f2016-01-18 10:24:09 +01001524static void hse_free(struct perf_hpp_fmt *fmt)
1525{
1526 struct hpp_sort_entry *hse;
1527
1528 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1529 free(hse);
1530}
1531
Namhyung Kima7d945b2014-03-04 10:46:34 +09001532static struct hpp_sort_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001533__sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
Namhyung Kim8b536992014-03-03 11:46:55 +09001534{
1535 struct hpp_sort_entry *hse;
1536
1537 hse = malloc(sizeof(*hse));
1538 if (hse == NULL) {
1539 pr_err("Memory allocation failed\n");
Namhyung Kima7d945b2014-03-04 10:46:34 +09001540 return NULL;
Namhyung Kim8b536992014-03-03 11:46:55 +09001541 }
1542
1543 hse->se = sd->entry;
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001544 hse->hpp.name = sd->entry->se_header;
Namhyung Kim8b536992014-03-03 11:46:55 +09001545 hse->hpp.header = __sort__hpp_header;
1546 hse->hpp.width = __sort__hpp_width;
1547 hse->hpp.entry = __sort__hpp_entry;
1548 hse->hpp.color = NULL;
1549
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001550 hse->hpp.cmp = __sort__hpp_cmp;
1551 hse->hpp.collapse = __sort__hpp_collapse;
1552 hse->hpp.sort = __sort__hpp_sort;
Jiri Olsa97358082016-01-18 10:24:03 +01001553 hse->hpp.equal = __sort__hpp_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001554 hse->hpp.free = hse_free;
Namhyung Kim8b536992014-03-03 11:46:55 +09001555
1556 INIT_LIST_HEAD(&hse->hpp.list);
1557 INIT_LIST_HEAD(&hse->hpp.sort_list);
Jiri Olsaf2998422014-05-23 17:15:47 +02001558 hse->hpp.elide = false;
Namhyung Kime0d66c72014-07-31 14:47:37 +09001559 hse->hpp.len = 0;
Namhyung Kim5b591662014-07-31 14:47:38 +09001560 hse->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001561 hse->hpp.level = level;
Namhyung Kim8b536992014-03-03 11:46:55 +09001562
Namhyung Kima7d945b2014-03-04 10:46:34 +09001563 return hse;
1564}
1565
Jiri Olsa564132f2016-01-18 10:24:09 +01001566static void hpp_free(struct perf_hpp_fmt *fmt)
1567{
1568 free(fmt);
1569}
1570
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001571static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1572 int level)
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001573{
1574 struct perf_hpp_fmt *fmt;
1575
1576 fmt = memdup(hd->fmt, sizeof(*fmt));
1577 if (fmt) {
1578 INIT_LIST_HEAD(&fmt->list);
1579 INIT_LIST_HEAD(&fmt->sort_list);
Jiri Olsa564132f2016-01-18 10:24:09 +01001580 fmt->free = hpp_free;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001581 fmt->level = level;
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001582 }
1583
1584 return fmt;
1585}
1586
Namhyung Kim54430102016-02-25 00:13:37 +09001587int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1588{
1589 struct perf_hpp_fmt *fmt;
1590 struct hpp_sort_entry *hse;
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001591 int ret = -1;
1592 int r;
Namhyung Kim54430102016-02-25 00:13:37 +09001593
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001594 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1595 if (!perf_hpp__is_sort_entry(fmt))
1596 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001597
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001598 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1599 if (hse->se->se_filter == NULL)
1600 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001601
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001602 /*
1603 * hist entry is filtered if any of sort key in the hpp list
1604 * is applied. But it should skip non-matched filter types.
1605 */
1606 r = hse->se->se_filter(he, type, arg);
1607 if (r >= 0) {
1608 if (ret < 0)
1609 ret = 0;
1610 ret |= r;
1611 }
1612 }
1613
1614 return ret;
Namhyung Kim54430102016-02-25 00:13:37 +09001615}
1616
Jiri Olsad7b617f2016-03-09 11:04:17 +01001617static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1618 struct perf_hpp_list *list,
1619 int level)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001620{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001621 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001622
1623 if (hse == NULL)
1624 return -1;
1625
Jiri Olsad7b617f2016-03-09 11:04:17 +01001626 perf_hpp_list__register_sort_field(list, &hse->hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001627 return 0;
1628}
1629
Jiri Olsad7b617f2016-03-09 11:04:17 +01001630static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1631 struct perf_hpp_list *list)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001632{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001633 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001634
1635 if (hse == NULL)
1636 return -1;
1637
Jiri Olsa07600022016-01-18 10:24:16 +01001638 perf_hpp_list__column_register(list, &hse->hpp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001639 return 0;
1640}
1641
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001642struct hpp_dynamic_entry {
1643 struct perf_hpp_fmt hpp;
1644 struct perf_evsel *evsel;
1645 struct format_field *field;
1646 unsigned dynamic_len;
Namhyung Kim053a3982015-12-23 02:07:05 +09001647 bool raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001648};
1649
1650static int hde_width(struct hpp_dynamic_entry *hde)
1651{
1652 if (!hde->hpp.len) {
1653 int len = hde->dynamic_len;
1654 int namelen = strlen(hde->field->name);
1655 int fieldlen = hde->field->size;
1656
1657 if (namelen > len)
1658 len = namelen;
1659
1660 if (!(hde->field->flags & FIELD_IS_STRING)) {
1661 /* length for print hex numbers */
1662 fieldlen = hde->field->size * 2 + 2;
1663 }
1664 if (fieldlen > len)
1665 len = fieldlen;
1666
1667 hde->hpp.len = len;
1668 }
1669 return hde->hpp.len;
1670}
1671
Namhyung Kim60517d22015-12-23 02:07:03 +09001672static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1673 struct hist_entry *he)
1674{
1675 char *str, *pos;
1676 struct format_field *field = hde->field;
1677 size_t namelen;
1678 bool last = false;
1679
Namhyung Kim053a3982015-12-23 02:07:05 +09001680 if (hde->raw_trace)
1681 return;
1682
Namhyung Kim60517d22015-12-23 02:07:03 +09001683 /* parse pretty print result and update max length */
1684 if (!he->trace_output)
1685 he->trace_output = get_trace_output(he);
1686
1687 namelen = strlen(field->name);
1688 str = he->trace_output;
1689
1690 while (str) {
1691 pos = strchr(str, ' ');
1692 if (pos == NULL) {
1693 last = true;
1694 pos = str + strlen(str);
1695 }
1696
1697 if (!strncmp(str, field->name, namelen)) {
1698 size_t len;
1699
1700 str += namelen + 1;
1701 len = pos - str;
1702
1703 if (len > hde->dynamic_len)
1704 hde->dynamic_len = len;
1705 break;
1706 }
1707
1708 if (last)
1709 str = NULL;
1710 else
1711 str = pos + 1;
1712 }
1713}
1714
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001715static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1716 struct perf_evsel *evsel __maybe_unused)
1717{
1718 struct hpp_dynamic_entry *hde;
1719 size_t len = fmt->user_len;
1720
1721 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1722
1723 if (!len)
1724 len = hde_width(hde);
1725
1726 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1727}
1728
1729static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1730 struct perf_hpp *hpp __maybe_unused,
1731 struct perf_evsel *evsel __maybe_unused)
1732{
1733 struct hpp_dynamic_entry *hde;
1734 size_t len = fmt->user_len;
1735
1736 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1737
1738 if (!len)
1739 len = hde_width(hde);
1740
1741 return len;
1742}
1743
Namhyung Kim361459f2015-12-23 02:07:08 +09001744bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1745{
1746 struct hpp_dynamic_entry *hde;
1747
1748 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1749
1750 return hists_to_evsel(hists) == hde->evsel;
1751}
1752
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001753static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1754 struct hist_entry *he)
1755{
1756 struct hpp_dynamic_entry *hde;
1757 size_t len = fmt->user_len;
Namhyung Kim60517d22015-12-23 02:07:03 +09001758 char *str, *pos;
1759 struct format_field *field;
1760 size_t namelen;
1761 bool last = false;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001762 int ret;
1763
1764 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1765
1766 if (!len)
1767 len = hde_width(hde);
1768
Namhyung Kim053a3982015-12-23 02:07:05 +09001769 if (hde->raw_trace)
1770 goto raw_field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001771
Namhyung Kime049d4a2016-02-27 03:52:46 +09001772 if (!he->trace_output)
1773 he->trace_output = get_trace_output(he);
1774
Namhyung Kim053a3982015-12-23 02:07:05 +09001775 field = hde->field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001776 namelen = strlen(field->name);
1777 str = he->trace_output;
1778
1779 while (str) {
1780 pos = strchr(str, ' ');
1781 if (pos == NULL) {
1782 last = true;
1783 pos = str + strlen(str);
1784 }
1785
1786 if (!strncmp(str, field->name, namelen)) {
1787 str += namelen + 1;
1788 str = strndup(str, pos - str);
1789
1790 if (str == NULL)
1791 return scnprintf(hpp->buf, hpp->size,
1792 "%*.*s", len, len, "ERROR");
1793 break;
1794 }
1795
1796 if (last)
1797 str = NULL;
1798 else
1799 str = pos + 1;
1800 }
1801
1802 if (str == NULL) {
1803 struct trace_seq seq;
Namhyung Kim053a3982015-12-23 02:07:05 +09001804raw_field:
Namhyung Kim60517d22015-12-23 02:07:03 +09001805 trace_seq_init(&seq);
1806 pevent_print_field(&seq, he->raw_data, hde->field);
1807 str = seq.buffer;
1808 }
1809
1810 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1811 free(str);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001812 return ret;
1813}
1814
1815static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1816 struct hist_entry *a, struct hist_entry *b)
1817{
1818 struct hpp_dynamic_entry *hde;
1819 struct format_field *field;
1820 unsigned offset, size;
1821
1822 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1823
Namhyung Kimabab5e72016-02-27 03:52:47 +09001824 if (b == NULL) {
1825 update_dynamic_len(hde, a);
1826 return 0;
1827 }
1828
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001829 field = hde->field;
1830 if (field->flags & FIELD_IS_DYNAMIC) {
1831 unsigned long long dyn;
1832
1833 pevent_read_number_field(field, a->raw_data, &dyn);
1834 offset = dyn & 0xffff;
1835 size = (dyn >> 16) & 0xffff;
1836
1837 /* record max width for output */
1838 if (size > hde->dynamic_len)
1839 hde->dynamic_len = size;
1840 } else {
1841 offset = field->offset;
1842 size = field->size;
1843 }
1844
1845 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1846}
1847
Namhyung Kim361459f2015-12-23 02:07:08 +09001848bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1849{
1850 return fmt->cmp == __sort__hde_cmp;
1851}
1852
Namhyung Kim665aa752016-02-21 23:22:35 +09001853static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1854{
1855 struct hpp_dynamic_entry *hde_a;
1856 struct hpp_dynamic_entry *hde_b;
1857
1858 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1859 return false;
1860
1861 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1862 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1863
1864 return hde_a->field == hde_b->field;
1865}
1866
Jiri Olsa564132f2016-01-18 10:24:09 +01001867static void hde_free(struct perf_hpp_fmt *fmt)
1868{
1869 struct hpp_dynamic_entry *hde;
1870
1871 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1872 free(hde);
1873}
1874
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001875static struct hpp_dynamic_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001876__alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1877 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001878{
1879 struct hpp_dynamic_entry *hde;
1880
1881 hde = malloc(sizeof(*hde));
1882 if (hde == NULL) {
1883 pr_debug("Memory allocation failed\n");
1884 return NULL;
1885 }
1886
1887 hde->evsel = evsel;
1888 hde->field = field;
1889 hde->dynamic_len = 0;
1890
1891 hde->hpp.name = field->name;
1892 hde->hpp.header = __sort__hde_header;
1893 hde->hpp.width = __sort__hde_width;
1894 hde->hpp.entry = __sort__hde_entry;
1895 hde->hpp.color = NULL;
1896
1897 hde->hpp.cmp = __sort__hde_cmp;
1898 hde->hpp.collapse = __sort__hde_cmp;
1899 hde->hpp.sort = __sort__hde_cmp;
Namhyung Kim665aa752016-02-21 23:22:35 +09001900 hde->hpp.equal = __sort__hde_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001901 hde->hpp.free = hde_free;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001902
1903 INIT_LIST_HEAD(&hde->hpp.list);
1904 INIT_LIST_HEAD(&hde->hpp.sort_list);
1905 hde->hpp.elide = false;
1906 hde->hpp.len = 0;
1907 hde->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001908 hde->hpp.level = level;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001909
1910 return hde;
1911}
1912
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03001913struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1914{
1915 struct perf_hpp_fmt *new_fmt = NULL;
1916
1917 if (perf_hpp__is_sort_entry(fmt)) {
1918 struct hpp_sort_entry *hse, *new_hse;
1919
1920 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1921 new_hse = memdup(hse, sizeof(*hse));
1922 if (new_hse)
1923 new_fmt = &new_hse->hpp;
1924 } else if (perf_hpp__is_dynamic_entry(fmt)) {
1925 struct hpp_dynamic_entry *hde, *new_hde;
1926
1927 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1928 new_hde = memdup(hde, sizeof(*hde));
1929 if (new_hde)
1930 new_fmt = &new_hde->hpp;
1931 } else {
1932 new_fmt = memdup(fmt, sizeof(*fmt));
1933 }
1934
1935 INIT_LIST_HEAD(&new_fmt->list);
1936 INIT_LIST_HEAD(&new_fmt->sort_list);
1937
1938 return new_fmt;
1939}
1940
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001941static int parse_field_name(char *str, char **event, char **field, char **opt)
1942{
1943 char *event_name, *field_name, *opt_name;
1944
1945 event_name = str;
1946 field_name = strchr(str, '.');
1947
1948 if (field_name) {
1949 *field_name++ = '\0';
1950 } else {
1951 event_name = NULL;
1952 field_name = str;
1953 }
1954
1955 opt_name = strchr(field_name, '/');
1956 if (opt_name)
1957 *opt_name++ = '\0';
1958
1959 *event = event_name;
1960 *field = field_name;
1961 *opt = opt_name;
1962
1963 return 0;
1964}
1965
1966/* find match evsel using a given event name. The event name can be:
Namhyung Kim9735be22016-01-05 19:58:35 +09001967 * 1. '%' + event index (e.g. '%1' for first event)
1968 * 2. full event name (e.g. sched:sched_switch)
1969 * 3. partial event name (should not contain ':')
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001970 */
1971static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1972{
1973 struct perf_evsel *evsel = NULL;
1974 struct perf_evsel *pos;
1975 bool full_name;
1976
1977 /* case 1 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001978 if (event_name[0] == '%') {
1979 int nr = strtol(event_name+1, NULL, 0);
1980
1981 if (nr > evlist->nr_entries)
1982 return NULL;
1983
1984 evsel = perf_evlist__first(evlist);
1985 while (--nr > 0)
1986 evsel = perf_evsel__next(evsel);
1987
1988 return evsel;
1989 }
1990
1991 full_name = !!strchr(event_name, ':');
1992 evlist__for_each(evlist, pos) {
Namhyung Kim9735be22016-01-05 19:58:35 +09001993 /* case 2 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001994 if (full_name && !strcmp(pos->name, event_name))
1995 return pos;
Namhyung Kim9735be22016-01-05 19:58:35 +09001996 /* case 3 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001997 if (!full_name && strstr(pos->name, event_name)) {
1998 if (evsel) {
1999 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2000 event_name, evsel->name, pos->name);
2001 return NULL;
2002 }
2003 evsel = pos;
2004 }
2005 }
2006
2007 return evsel;
2008}
2009
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002010static int __dynamic_dimension__add(struct perf_evsel *evsel,
2011 struct format_field *field,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002012 bool raw_trace, int level)
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002013{
2014 struct hpp_dynamic_entry *hde;
2015
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002016 hde = __alloc_dynamic_entry(evsel, field, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002017 if (hde == NULL)
2018 return -ENOMEM;
2019
2020 hde->raw_trace = raw_trace;
2021
2022 perf_hpp__register_sort_field(&hde->hpp);
2023 return 0;
2024}
2025
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002026static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002027{
2028 int ret;
2029 struct format_field *field;
2030
2031 field = evsel->tp_format->format.fields;
2032 while (field) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002033 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002034 if (ret < 0)
2035 return ret;
2036
2037 field = field->next;
2038 }
2039 return 0;
2040}
2041
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002042static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2043 int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002044{
2045 int ret;
2046 struct perf_evsel *evsel;
2047
2048 evlist__for_each(evlist, evsel) {
2049 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2050 continue;
2051
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002052 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002053 if (ret < 0)
2054 return ret;
2055 }
2056 return 0;
2057}
2058
Namhyung Kim9735be22016-01-05 19:58:35 +09002059static int add_all_matching_fields(struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002060 char *field_name, bool raw_trace, int level)
Namhyung Kim9735be22016-01-05 19:58:35 +09002061{
2062 int ret = -ESRCH;
2063 struct perf_evsel *evsel;
2064 struct format_field *field;
2065
2066 evlist__for_each(evlist, evsel) {
2067 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2068 continue;
2069
2070 field = pevent_find_any_field(evsel->tp_format, field_name);
2071 if (field == NULL)
2072 continue;
2073
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002074 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002075 if (ret < 0)
2076 break;
2077 }
2078 return ret;
2079}
2080
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002081static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2082 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002083{
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002084 char *str, *event_name, *field_name, *opt_name;
2085 struct perf_evsel *evsel;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002086 struct format_field *field;
Namhyung Kim053a3982015-12-23 02:07:05 +09002087 bool raw_trace = symbol_conf.raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002088 int ret = 0;
2089
2090 if (evlist == NULL)
2091 return -ENOENT;
2092
2093 str = strdup(tok);
2094 if (str == NULL)
2095 return -ENOMEM;
2096
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002097 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002098 ret = -EINVAL;
2099 goto out;
2100 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002101
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002102 if (opt_name) {
2103 if (strcmp(opt_name, "raw")) {
2104 pr_debug("unsupported field option %s\n", opt_name);
Namhyung Kim053a3982015-12-23 02:07:05 +09002105 ret = -EINVAL;
2106 goto out;
2107 }
2108 raw_trace = true;
2109 }
2110
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002111 if (!strcmp(field_name, "trace_fields")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002112 ret = add_all_dynamic_fields(evlist, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002113 goto out;
2114 }
2115
Namhyung Kim9735be22016-01-05 19:58:35 +09002116 if (event_name == NULL) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002117 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002118 goto out;
2119 }
2120
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002121 evsel = find_evsel(evlist, event_name);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002122 if (evsel == NULL) {
2123 pr_debug("Cannot find event: %s\n", event_name);
2124 ret = -ENOENT;
2125 goto out;
2126 }
2127
2128 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2129 pr_debug("%s is not a tracepoint event\n", event_name);
2130 ret = -EINVAL;
2131 goto out;
2132 }
2133
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002134 if (!strcmp(field_name, "*")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002135 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002136 } else {
2137 field = pevent_find_any_field(evsel->tp_format, field_name);
2138 if (field == NULL) {
2139 pr_debug("Cannot find event field for %s.%s\n",
2140 event_name, field_name);
2141 return -ENOENT;
2142 }
2143
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002144 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002145 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002146
2147out:
2148 free(str);
2149 return ret;
2150}
2151
Jiri Olsad7b617f2016-03-09 11:04:17 +01002152static int __sort_dimension__add(struct sort_dimension *sd,
2153 struct perf_hpp_list *list,
2154 int level)
Namhyung Kim2f532d092013-04-03 21:26:10 +09002155{
2156 if (sd->taken)
Namhyung Kim8b536992014-03-03 11:46:55 +09002157 return 0;
2158
Jiri Olsad7b617f2016-03-09 11:04:17 +01002159 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
Namhyung Kim8b536992014-03-03 11:46:55 +09002160 return -1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002161
2162 if (sd->entry->se_collapse)
Jiri Olsa52225032016-05-03 13:54:42 +02002163 list->need_collapse = 1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002164
Namhyung Kim2f532d092013-04-03 21:26:10 +09002165 sd->taken = 1;
Namhyung Kim8b536992014-03-03 11:46:55 +09002166
2167 return 0;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002168}
2169
Jiri Olsad7b617f2016-03-09 11:04:17 +01002170static int __hpp_dimension__add(struct hpp_dimension *hd,
2171 struct perf_hpp_list *list,
2172 int level)
Namhyung Kima2ce0672014-03-04 09:06:42 +09002173{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002174 struct perf_hpp_fmt *fmt;
Namhyung Kima2ce0672014-03-04 09:06:42 +09002175
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002176 if (hd->taken)
2177 return 0;
2178
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002179 fmt = __hpp_dimension__alloc_hpp(hd, level);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002180 if (!fmt)
2181 return -1;
2182
2183 hd->taken = 1;
Jiri Olsad7b617f2016-03-09 11:04:17 +01002184 perf_hpp_list__register_sort_field(list, fmt);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002185 return 0;
2186}
2187
Jiri Olsa07600022016-01-18 10:24:16 +01002188static int __sort_dimension__add_output(struct perf_hpp_list *list,
2189 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002190{
2191 if (sd->taken)
2192 return 0;
2193
Jiri Olsad7b617f2016-03-09 11:04:17 +01002194 if (__sort_dimension__add_hpp_output(sd, list) < 0)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002195 return -1;
2196
2197 sd->taken = 1;
2198 return 0;
2199}
2200
Jiri Olsa07600022016-01-18 10:24:16 +01002201static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2202 struct hpp_dimension *hd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002203{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002204 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002205
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002206 if (hd->taken)
2207 return 0;
2208
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002209 fmt = __hpp_dimension__alloc_hpp(hd, 0);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002210 if (!fmt)
2211 return -1;
2212
2213 hd->taken = 1;
Jiri Olsa07600022016-01-18 10:24:16 +01002214 perf_hpp_list__column_register(list, fmt);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002215 return 0;
2216}
2217
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002218int hpp_dimension__add_output(unsigned col)
2219{
2220 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa07600022016-01-18 10:24:16 +01002221 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002222}
2223
Jiri Olsad7b617f2016-03-09 11:04:17 +01002224static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -03002225 struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002226 int level)
John Kacurdd68ada2009-09-24 18:02:49 +02002227{
2228 unsigned int i;
2229
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002230 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2231 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +02002232
John Kacurdd68ada2009-09-24 18:02:49 +02002233 if (strncasecmp(tok, sd->name, strlen(tok)))
2234 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002235
John Kacurdd68ada2009-09-24 18:02:49 +02002236 if (sd->entry == &sort_parent) {
2237 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2238 if (ret) {
2239 char err[BUFSIZ];
2240
2241 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -03002242 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2243 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +02002244 }
Jiri Olsade7e6a72016-05-03 13:54:43 +02002245 list->parent = 1;
Namhyung Kim930477b2013-04-05 10:26:36 +09002246 } else if (sd->entry == &sort_sym) {
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002247 list->sym = 1;
Kan Liang94ba4622015-02-09 05:39:44 +00002248 /*
2249 * perf diff displays the performance difference amongst
2250 * two or more perf.data files. Those files could come
2251 * from different binaries. So we should not compare
2252 * their ips, but the name of symbol.
2253 */
2254 if (sort__mode == SORT_MODE__DIFF)
2255 sd->entry->se_collapse = sort__sym_sort;
2256
Namhyung Kim68f6d022013-12-18 14:21:10 +09002257 } else if (sd->entry == &sort_dso) {
2258 sort__has_dso = 1;
Kan Liang2e7ea3a2015-09-04 10:45:43 -04002259 } else if (sd->entry == &sort_socket) {
2260 sort__has_socket = 1;
Namhyung Kimcfd92da2016-01-21 19:13:24 -03002261 } else if (sd->entry == &sort_thread) {
2262 sort__has_thread = 1;
Namhyung Kim078b8d42016-03-09 23:20:51 +09002263 } else if (sd->entry == &sort_comm) {
2264 sort__has_comm = 1;
John Kacurdd68ada2009-09-24 18:02:49 +02002265 }
2266
Jiri Olsad7b617f2016-03-09 11:04:17 +01002267 return __sort_dimension__add(sd, list, level);
John Kacurdd68ada2009-09-24 18:02:49 +02002268 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002269
Namhyung Kima2ce0672014-03-04 09:06:42 +09002270 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2271 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2272
2273 if (strncasecmp(tok, hd->name, strlen(tok)))
2274 continue;
2275
Jiri Olsad7b617f2016-03-09 11:04:17 +01002276 return __hpp_dimension__add(hd, list, level);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002277 }
2278
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002279 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2280 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2281
2282 if (strncasecmp(tok, sd->name, strlen(tok)))
2283 continue;
2284
Namhyung Kim55369fc2013-04-01 20:35:20 +09002285 if (sort__mode != SORT_MODE__BRANCH)
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002286 return -EINVAL;
2287
2288 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002289 list->sym = 1;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002290
Jiri Olsad7b617f2016-03-09 11:04:17 +01002291 __sort_dimension__add(sd, list, level);
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002292 return 0;
2293 }
2294
Namhyung Kimafab87b2013-04-03 21:26:11 +09002295 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2296 struct sort_dimension *sd = &memory_sort_dimensions[i];
2297
2298 if (strncasecmp(tok, sd->name, strlen(tok)))
2299 continue;
2300
2301 if (sort__mode != SORT_MODE__MEMORY)
2302 return -EINVAL;
2303
2304 if (sd->entry == &sort_mem_daddr_sym)
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002305 list->sym = 1;
Namhyung Kimafab87b2013-04-03 21:26:11 +09002306
Jiri Olsad7b617f2016-03-09 11:04:17 +01002307 __sort_dimension__add(sd, list, level);
Namhyung Kimafab87b2013-04-03 21:26:11 +09002308 return 0;
2309 }
2310
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002311 if (!add_dynamic_entry(evlist, tok, level))
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002312 return 0;
2313
John Kacurdd68ada2009-09-24 18:02:49 +02002314 return -ESRCH;
2315}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002316
Jiri Olsad7b617f2016-03-09 11:04:17 +01002317static int setup_sort_list(struct perf_hpp_list *list, char *str,
2318 struct perf_evlist *evlist)
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002319{
2320 char *tmp, *tok;
2321 int ret = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002322 int level = 0;
Namhyung Kima23f37e2016-03-07 16:44:47 -03002323 int next_level = 1;
2324 bool in_group = false;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002325
Namhyung Kima23f37e2016-03-07 16:44:47 -03002326 do {
2327 tok = str;
2328 tmp = strpbrk(str, "{}, ");
2329 if (tmp) {
2330 if (in_group)
2331 next_level = level;
2332 else
2333 next_level = level + 1;
2334
2335 if (*tmp == '{')
2336 in_group = true;
2337 else if (*tmp == '}')
2338 in_group = false;
2339
2340 *tmp = '\0';
2341 str = tmp + 1;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002342 }
Namhyung Kima23f37e2016-03-07 16:44:47 -03002343
2344 if (*tok) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002345 ret = sort_dimension__add(list, tok, evlist, level);
Namhyung Kima23f37e2016-03-07 16:44:47 -03002346 if (ret == -EINVAL) {
2347 error("Invalid --sort key: `%s'", tok);
2348 break;
2349 } else if (ret == -ESRCH) {
2350 error("Unknown --sort key: `%s'", tok);
2351 break;
2352 }
2353 }
2354
2355 level = next_level;
2356 } while (tmp);
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002357
2358 return ret;
2359}
2360
Namhyung Kimd49dade2015-12-23 02:07:10 +09002361static const char *get_default_sort_order(struct perf_evlist *evlist)
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002362{
2363 const char *default_sort_orders[] = {
2364 default_sort_order,
2365 default_branch_sort_order,
2366 default_mem_sort_order,
2367 default_top_sort_order,
2368 default_diff_sort_order,
Namhyung Kimd49dade2015-12-23 02:07:10 +09002369 default_tracepoint_sort_order,
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002370 };
Namhyung Kimd49dade2015-12-23 02:07:10 +09002371 bool use_trace = true;
2372 struct perf_evsel *evsel;
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002373
2374 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2375
Namhyung Kimd49dade2015-12-23 02:07:10 +09002376 if (evlist == NULL)
2377 goto out_no_evlist;
2378
2379 evlist__for_each(evlist, evsel) {
2380 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2381 use_trace = false;
2382 break;
2383 }
2384 }
2385
2386 if (use_trace) {
2387 sort__mode = SORT_MODE__TRACEPOINT;
2388 if (symbol_conf.raw_trace)
2389 return "trace_fields";
2390 }
2391out_no_evlist:
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002392 return default_sort_orders[sort__mode];
2393}
2394
Namhyung Kimd49dade2015-12-23 02:07:10 +09002395static int setup_sort_order(struct perf_evlist *evlist)
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002396{
2397 char *new_sort_order;
2398
2399 /*
2400 * Append '+'-prefixed sort order to the default sort
2401 * order string.
2402 */
2403 if (!sort_order || is_strict_order(sort_order))
2404 return 0;
2405
2406 if (sort_order[1] == '\0') {
2407 error("Invalid --sort key: `+'");
2408 return -EINVAL;
2409 }
2410
2411 /*
2412 * We allocate new sort_order string, but we never free it,
2413 * because it's checked over the rest of the code.
2414 */
2415 if (asprintf(&new_sort_order, "%s,%s",
Namhyung Kimd49dade2015-12-23 02:07:10 +09002416 get_default_sort_order(evlist), sort_order + 1) < 0) {
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002417 error("Not enough memory to set up --sort");
2418 return -ENOMEM;
2419 }
2420
2421 sort_order = new_sort_order;
2422 return 0;
2423}
2424
Jiri Olsab97511c2016-01-07 10:14:08 +01002425/*
2426 * Adds 'pre,' prefix into 'str' is 'pre' is
2427 * not already part of 'str'.
2428 */
2429static char *prefix_if_not_in(const char *pre, char *str)
2430{
2431 char *n;
2432
2433 if (!str || strstr(str, pre))
2434 return str;
2435
2436 if (asprintf(&n, "%s,%s", pre, str) < 0)
2437 return NULL;
2438
2439 free(str);
2440 return n;
2441}
2442
2443static char *setup_overhead(char *keys)
2444{
2445 keys = prefix_if_not_in("overhead", keys);
2446
2447 if (symbol_conf.cumulate_callchain)
2448 keys = prefix_if_not_in("overhead_children", keys);
2449
2450 return keys;
2451}
2452
Namhyung Kim40184c42015-12-23 02:07:01 +09002453static int __setup_sorting(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002454{
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002455 char *str;
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002456 const char *sort_keys;
Namhyung Kim55309982013-02-06 14:57:16 +09002457 int ret = 0;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002458
Namhyung Kimd49dade2015-12-23 02:07:10 +09002459 ret = setup_sort_order(evlist);
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002460 if (ret)
2461 return ret;
2462
2463 sort_keys = sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002464 if (sort_keys == NULL) {
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002465 if (is_strict_order(field_order)) {
Namhyung Kima7d945b2014-03-04 10:46:34 +09002466 /*
2467 * If user specified field order but no sort order,
2468 * we'll honor it and not add default sort orders.
2469 */
2470 return 0;
2471 }
2472
Namhyung Kimd49dade2015-12-23 02:07:10 +09002473 sort_keys = get_default_sort_order(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002474 }
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002475
2476 str = strdup(sort_keys);
Namhyung Kim5936f542013-02-06 14:57:17 +09002477 if (str == NULL) {
2478 error("Not enough memory to setup sort keys");
2479 return -ENOMEM;
2480 }
2481
Jiri Olsab97511c2016-01-07 10:14:08 +01002482 /*
2483 * Prepend overhead fields for backward compatibility.
2484 */
2485 if (!is_strict_order(field_order)) {
2486 str = setup_overhead(str);
2487 if (str == NULL) {
2488 error("Not enough memory to setup overhead keys");
2489 return -ENOMEM;
2490 }
2491 }
2492
Jiri Olsad7b617f2016-03-09 11:04:17 +01002493 ret = setup_sort_list(&perf_hpp_list, str, evlist);
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002494
2495 free(str);
Namhyung Kim55309982013-02-06 14:57:16 +09002496 return ret;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002497}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002498
Jiri Olsaf2998422014-05-23 17:15:47 +02002499void perf_hpp__set_elide(int idx, bool elide)
Namhyung Kime67d49a2014-03-18 13:00:59 +09002500{
Jiri Olsaf2998422014-05-23 17:15:47 +02002501 struct perf_hpp_fmt *fmt;
2502 struct hpp_sort_entry *hse;
Namhyung Kime67d49a2014-03-18 13:00:59 +09002503
Jiri Olsacf094042016-01-18 10:24:17 +01002504 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002505 if (!perf_hpp__is_sort_entry(fmt))
2506 continue;
2507
2508 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2509 if (hse->se->se_width_idx == idx) {
2510 fmt->elide = elide;
2511 break;
2512 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002513 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002514}
2515
Jiri Olsaf2998422014-05-23 17:15:47 +02002516static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002517{
2518 if (list && strlist__nr_entries(list) == 1) {
2519 if (fp != NULL)
2520 fprintf(fp, "# %s: %s\n", list_name,
2521 strlist__entry(list, 0)->s);
Jiri Olsaf2998422014-05-23 17:15:47 +02002522 return true;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002523 }
Jiri Olsaf2998422014-05-23 17:15:47 +02002524 return false;
2525}
2526
2527static bool get_elide(int idx, FILE *output)
2528{
2529 switch (idx) {
2530 case HISTC_SYMBOL:
2531 return __get_elide(symbol_conf.sym_list, "symbol", output);
2532 case HISTC_DSO:
2533 return __get_elide(symbol_conf.dso_list, "dso", output);
2534 case HISTC_COMM:
2535 return __get_elide(symbol_conf.comm_list, "comm", output);
2536 default:
2537 break;
2538 }
2539
2540 if (sort__mode != SORT_MODE__BRANCH)
2541 return false;
2542
2543 switch (idx) {
2544 case HISTC_SYMBOL_FROM:
2545 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2546 case HISTC_SYMBOL_TO:
2547 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2548 case HISTC_DSO_FROM:
2549 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2550 case HISTC_DSO_TO:
2551 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2552 default:
2553 break;
2554 }
2555
2556 return false;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002557}
Namhyung Kim08e71542013-04-03 21:26:19 +09002558
2559void sort__setup_elide(FILE *output)
2560{
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002561 struct perf_hpp_fmt *fmt;
2562 struct hpp_sort_entry *hse;
Namhyung Kim7524f632013-11-08 17:53:42 +09002563
Jiri Olsacf094042016-01-18 10:24:17 +01002564 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002565 if (!perf_hpp__is_sort_entry(fmt))
2566 continue;
Namhyung Kim08e71542013-04-03 21:26:19 +09002567
Jiri Olsaf2998422014-05-23 17:15:47 +02002568 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2569 fmt->elide = get_elide(hse->se->se_width_idx, output);
Namhyung Kim08e71542013-04-03 21:26:19 +09002570 }
2571
Namhyung Kim7524f632013-11-08 17:53:42 +09002572 /*
2573 * It makes no sense to elide all of sort entries.
2574 * Just revert them to show up again.
2575 */
Jiri Olsacf094042016-01-18 10:24:17 +01002576 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002577 if (!perf_hpp__is_sort_entry(fmt))
2578 continue;
2579
Jiri Olsaf2998422014-05-23 17:15:47 +02002580 if (!fmt->elide)
Namhyung Kim7524f632013-11-08 17:53:42 +09002581 return;
2582 }
2583
Jiri Olsacf094042016-01-18 10:24:17 +01002584 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002585 if (!perf_hpp__is_sort_entry(fmt))
2586 continue;
2587
Jiri Olsaf2998422014-05-23 17:15:47 +02002588 fmt->elide = false;
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002589 }
Namhyung Kim08e71542013-04-03 21:26:19 +09002590}
Namhyung Kima7d945b2014-03-04 10:46:34 +09002591
Jiri Olsa07600022016-01-18 10:24:16 +01002592static int output_field_add(struct perf_hpp_list *list, char *tok)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002593{
2594 unsigned int i;
2595
2596 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2597 struct sort_dimension *sd = &common_sort_dimensions[i];
2598
2599 if (strncasecmp(tok, sd->name, strlen(tok)))
2600 continue;
2601
Jiri Olsa07600022016-01-18 10:24:16 +01002602 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002603 }
2604
2605 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2606 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2607
2608 if (strncasecmp(tok, hd->name, strlen(tok)))
2609 continue;
2610
Jiri Olsa07600022016-01-18 10:24:16 +01002611 return __hpp_dimension__add_output(list, hd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002612 }
2613
2614 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2615 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2616
2617 if (strncasecmp(tok, sd->name, strlen(tok)))
2618 continue;
2619
Jiri Olsa07600022016-01-18 10:24:16 +01002620 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002621 }
2622
2623 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2624 struct sort_dimension *sd = &memory_sort_dimensions[i];
2625
2626 if (strncasecmp(tok, sd->name, strlen(tok)))
2627 continue;
2628
Jiri Olsa07600022016-01-18 10:24:16 +01002629 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002630 }
2631
2632 return -ESRCH;
2633}
2634
Jiri Olsa07600022016-01-18 10:24:16 +01002635static int setup_output_list(struct perf_hpp_list *list, char *str)
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002636{
2637 char *tmp, *tok;
2638 int ret = 0;
2639
2640 for (tok = strtok_r(str, ", ", &tmp);
2641 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Jiri Olsa07600022016-01-18 10:24:16 +01002642 ret = output_field_add(list, tok);
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002643 if (ret == -EINVAL) {
2644 error("Invalid --fields key: `%s'", tok);
2645 break;
2646 } else if (ret == -ESRCH) {
2647 error("Unknown --fields key: `%s'", tok);
2648 break;
2649 }
2650 }
2651
2652 return ret;
2653}
2654
Namhyung Kima7d945b2014-03-04 10:46:34 +09002655static void reset_dimensions(void)
2656{
2657 unsigned int i;
2658
2659 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2660 common_sort_dimensions[i].taken = 0;
2661
2662 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2663 hpp_sort_dimensions[i].taken = 0;
2664
2665 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2666 bstack_sort_dimensions[i].taken = 0;
2667
2668 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2669 memory_sort_dimensions[i].taken = 0;
2670}
2671
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002672bool is_strict_order(const char *order)
2673{
2674 return order && (*order != '+');
2675}
2676
Namhyung Kima7d945b2014-03-04 10:46:34 +09002677static int __setup_output_field(void)
2678{
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002679 char *str, *strp;
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002680 int ret = -EINVAL;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002681
2682 if (field_order == NULL)
2683 return 0;
2684
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002685 strp = str = strdup(field_order);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002686 if (str == NULL) {
2687 error("Not enough memory to setup output fields");
2688 return -ENOMEM;
2689 }
2690
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002691 if (!is_strict_order(field_order))
2692 strp++;
2693
2694 if (!strlen(strp)) {
2695 error("Invalid --fields key: `+'");
2696 goto out;
2697 }
2698
Jiri Olsa07600022016-01-18 10:24:16 +01002699 ret = setup_output_list(&perf_hpp_list, strp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002700
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002701out:
Namhyung Kima7d945b2014-03-04 10:46:34 +09002702 free(str);
2703 return ret;
2704}
2705
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002706int setup_sorting(struct perf_evlist *evlist)
2707{
2708 int err;
2709
2710 err = __setup_sorting(evlist);
2711 if (err < 0)
2712 return err;
2713
2714 if (parent_pattern != default_parent_pattern) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002715 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002716 if (err < 0)
2717 return err;
2718 }
2719
Namhyung Kima7d945b2014-03-04 10:46:34 +09002720 reset_dimensions();
2721
2722 /*
2723 * perf diff doesn't use default hpp output fields.
2724 */
2725 if (sort__mode != SORT_MODE__DIFF)
2726 perf_hpp__init();
2727
2728 err = __setup_output_field();
2729 if (err < 0)
2730 return err;
2731
2732 /* copy sort keys to output fields */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002733 perf_hpp__setup_output_field(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002734 /* and then copy output fields to sort keys */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002735 perf_hpp__append_sort_keys(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002736
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002737 /* setup hists-specific output fields */
2738 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2739 return -1;
2740
Namhyung Kima7d945b2014-03-04 10:46:34 +09002741 return 0;
2742}
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002743
2744void reset_output_field(void)
2745{
Jiri Olsa52225032016-05-03 13:54:42 +02002746 perf_hpp_list.need_collapse = 0;
Jiri Olsade7e6a72016-05-03 13:54:43 +02002747 perf_hpp_list.parent = 0;
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002748 perf_hpp_list.sym = 0;
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002749 sort__has_dso = 0;
2750
Namhyung Kimd69b2962014-05-23 10:59:01 +09002751 field_order = NULL;
2752 sort_order = NULL;
2753
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002754 reset_dimensions();
Jiri Olsa43e0a682016-01-18 10:24:21 +01002755 perf_hpp__reset_output_field(&perf_hpp_list);
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002756}