blob: f5ba111cd9fb2a00757a36af8d706a3f4380c92e [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;
Frederic Weisbeckeraf0a6fa2009-10-22 23:23:22 +020024int sort__need_collapse = 0;
25int sort__has_parent = 0;
Namhyung Kim1af556402012-09-14 17:35:27 +090026int sort__has_sym = 0;
Namhyung Kim68f6d022013-12-18 14:21:10 +090027int sort__has_dso = 0;
Kan Liang2e7ea3a2015-09-04 10:45:43 -040028int sort__has_socket = 0;
Namhyung Kimcfd92da2016-01-21 19:13:24 -030029int sort__has_thread = 0;
Namhyung Kim078b8d42016-03-09 23:20:51 +090030int sort__has_comm = 0;
Namhyung Kim55369fc2013-04-01 20:35:20 +090031enum sort_mode sort__mode = SORT_MODE__NORMAL;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020032
Arnaldo Carvalho de Melo37d9bb52016-02-12 11:27:51 -030033/*
34 * Replaces all occurrences of a char used with the:
35 *
36 * -t, --field-separator
37 *
38 * option, that uses a special separator character and don't pad with spaces,
39 * replacing all occurances of this separator in symbol names (and other
40 * output) with a '.' character, that thus it's the only non valid separator.
41*/
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030042static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020043{
44 int n;
45 va_list ap;
46
47 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030048 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020049 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030050 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020051
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030052 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020053 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030054 if (sep == NULL)
55 break;
56 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020057 }
John Kacurdd68ada2009-09-24 18:02:49 +020058 }
59 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110060
61 if (n >= (int)size)
62 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020063 return n;
64}
65
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020066static int64_t cmp_null(const void *l, const void *r)
Frederic Weisbecker872a8782011-06-29 03:14:52 +020067{
68 if (!l && !r)
69 return 0;
70 else if (!l)
71 return -1;
72 else
73 return 1;
74}
75
76/* --sort pid */
77
78static int64_t
79sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
80{
Adrian Hunter38051232013-07-04 16:20:31 +030081 return right->thread->tid - left->thread->tid;
Frederic Weisbecker872a8782011-06-29 03:14:52 +020082}
83
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030084static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030085 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020086{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020087 const char *comm = thread__comm_str(he->thread);
Namhyung Kim5b591662014-07-31 14:47:38 +090088
89 width = max(7U, width) - 6;
90 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
91 width, width, comm ?: "");
John Kacurdd68ada2009-09-24 18:02:49 +020092}
93
Namhyung Kim54430102016-02-25 00:13:37 +090094static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
95{
96 const struct thread *th = arg;
97
98 if (type != HIST_FILTER__THREAD)
99 return -1;
100
101 return th && he->thread != th;
102}
103
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200104struct sort_entry sort_thread = {
Namhyung Kim8246de82014-07-31 14:47:35 +0900105 .se_header = " Pid:Command",
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200106 .se_cmp = sort__thread_cmp,
107 .se_snprintf = hist_entry__thread_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900108 .se_filter = hist_entry__thread_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200109 .se_width_idx = HISTC_THREAD,
110};
111
112/* --sort comm */
113
114static int64_t
115sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
116{
Frederic Weisbeckerfedd63d2013-09-11 17:18:09 +0200117 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200118 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200119}
120
121static int64_t
122sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
123{
Namhyung Kim4dfced32013-09-13 16:28:57 +0900124 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200125 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200126}
127
Namhyung Kim202e7a62014-03-04 11:01:41 +0900128static int64_t
129sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
130{
131 return strcmp(comm__str(right->comm), comm__str(left->comm));
132}
133
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300134static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300135 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200136{
Namhyung Kim5b591662014-07-31 14:47:38 +0900137 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
John Kacurdd68ada2009-09-24 18:02:49 +0200138}
139
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900140struct sort_entry sort_comm = {
141 .se_header = "Command",
142 .se_cmp = sort__comm_cmp,
143 .se_collapse = sort__comm_collapse,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900144 .se_sort = sort__comm_sort,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900145 .se_snprintf = hist_entry__comm_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900146 .se_filter = hist_entry__thread_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900147 .se_width_idx = HISTC_COMM,
148};
149
150/* --sort dso */
151
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100152static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200153{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100154 struct dso *dso_l = map_l ? map_l->dso : NULL;
155 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300156 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200157
158 if (!dso_l || !dso_r)
Namhyung Kim202e7a62014-03-04 11:01:41 +0900159 return cmp_null(dso_r, dso_l);
John Kacurdd68ada2009-09-24 18:02:49 +0200160
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300161 if (verbose) {
162 dso_name_l = dso_l->long_name;
163 dso_name_r = dso_r->long_name;
164 } else {
165 dso_name_l = dso_l->short_name;
166 dso_name_r = dso_r->short_name;
167 }
168
169 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200170}
171
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100172static int64_t
173sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200174{
Namhyung Kim202e7a62014-03-04 11:01:41 +0900175 return _sort__dso_cmp(right->ms.map, left->ms.map);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100176}
177
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100178static int _hist_entry__dso_snprintf(struct map *map, char *bf,
179 size_t size, unsigned int width)
180{
181 if (map && map->dso) {
182 const char *dso_name = !verbose ? map->dso->short_name :
183 map->dso->long_name;
Namhyung Kim5b591662014-07-31 14:47:38 +0900184 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300185 }
John Kacurdd68ada2009-09-24 18:02:49 +0200186
Namhyung Kim5b591662014-07-31 14:47:38 +0900187 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200188}
189
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300190static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100191 size_t size, unsigned int width)
192{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300193 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100194}
195
Namhyung Kim54430102016-02-25 00:13:37 +0900196static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
197{
198 const struct dso *dso = arg;
199
200 if (type != HIST_FILTER__DSO)
201 return -1;
202
203 return dso && (!he->ms.map || he->ms.map->dso != dso);
204}
205
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900206struct sort_entry sort_dso = {
207 .se_header = "Shared Object",
208 .se_cmp = sort__dso_cmp,
209 .se_snprintf = hist_entry__dso_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900210 .se_filter = hist_entry__dso_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900211 .se_width_idx = HISTC_DSO,
212};
213
214/* --sort symbol */
215
Namhyung Kim2037be52013-12-18 14:21:09 +0900216static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
217{
218 return (int64_t)(right_ip - left_ip);
219}
220
Namhyung Kim51f27d12013-02-06 14:57:15 +0900221static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900222{
223 if (!sym_l || !sym_r)
224 return cmp_null(sym_l, sym_r);
225
226 if (sym_l == sym_r)
227 return 0;
228
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700229 if (sym_l->start != sym_r->start)
230 return (int64_t)(sym_r->start - sym_l->start);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900231
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700232 return (int64_t)(sym_r->end - sym_l->end);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900233}
234
235static int64_t
236sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
237{
Namhyung Kim09600e02013-10-15 11:01:56 +0900238 int64_t ret;
239
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900240 if (!left->ms.sym && !right->ms.sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900241 return _sort__addr_cmp(left->ip, right->ip);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900242
Namhyung Kim09600e02013-10-15 11:01:56 +0900243 /*
244 * comparing symbol address alone is not enough since it's a
245 * relative address within a dso.
246 */
Namhyung Kim68f6d022013-12-18 14:21:10 +0900247 if (!sort__has_dso) {
248 ret = sort__dso_cmp(left, right);
249 if (ret != 0)
250 return ret;
251 }
Namhyung Kim09600e02013-10-15 11:01:56 +0900252
Namhyung Kim51f27d12013-02-06 14:57:15 +0900253 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900254}
255
Namhyung Kim202e7a62014-03-04 11:01:41 +0900256static int64_t
257sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
258{
259 if (!left->ms.sym || !right->ms.sym)
260 return cmp_null(left->ms.sym, right->ms.sym);
261
262 return strcmp(right->ms.sym->name, left->ms.sym->name);
263}
264
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100265static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
266 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900267 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100268{
269 size_t ret = 0;
270
271 if (verbose) {
272 char o = map ? dso__symtab_origin(map->dso) : '!';
273 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Namhyung Kimded19d52013-04-01 20:35:19 +0900274 BITS_PER_LONG / 4 + 2, ip, o);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100275 }
276
277 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100278 if (sym && map) {
279 if (map->type == MAP__VARIABLE) {
280 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
281 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
Namhyung Kim62667742013-01-24 16:10:42 +0100282 ip - map->unmap_ip(map, sym->start));
Stephane Eranian98a3b322013-01-24 16:10:35 +0100283 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300284 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
Stephane Eranian98a3b322013-01-24 16:10:35 +0100285 width - ret,
286 sym->name);
287 }
288 } else {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100289 size_t len = BITS_PER_LONG / 4;
290 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
291 len, ip);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100292 }
293
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300294 return ret;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100295}
296
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300297static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900298 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100299{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300300 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
301 he->level, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100302}
John Kacurdd68ada2009-09-24 18:02:49 +0200303
Namhyung Kim54430102016-02-25 00:13:37 +0900304static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
305{
306 const char *sym = arg;
307
308 if (type != HIST_FILTER__SYMBOL)
309 return -1;
310
311 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
312}
313
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200314struct sort_entry sort_sym = {
315 .se_header = "Symbol",
316 .se_cmp = sort__sym_cmp,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900317 .se_sort = sort__sym_sort,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200318 .se_snprintf = hist_entry__sym_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900319 .se_filter = hist_entry__sym_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200320 .se_width_idx = HISTC_SYMBOL,
321};
John Kacurdd68ada2009-09-24 18:02:49 +0200322
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300323/* --sort srcline */
324
Namhyung Kimcecaec62016-02-22 09:31:51 +0900325static char *hist_entry__get_srcline(struct hist_entry *he)
326{
327 struct map *map = he->ms.map;
328
329 if (!map)
330 return SRCLINE_UNKNOWN;
331
332 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
333 he->ms.sym, true);
334}
335
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300336static int64_t
337sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
338{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900339 if (!left->srcline)
340 left->srcline = hist_entry__get_srcline(left);
341 if (!right->srcline)
342 right->srcline = hist_entry__get_srcline(right);
343
Namhyung Kim202e7a62014-03-04 11:01:41 +0900344 return strcmp(right->srcline, left->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300345}
346
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300347static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim5b591662014-07-31 14:47:38 +0900348 size_t size, unsigned int width)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300349{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900350 if (!he->srcline)
351 he->srcline = hist_entry__get_srcline(he);
352
Namhyung Kim2960ed62016-02-22 09:32:33 +0900353 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300354}
355
356struct sort_entry sort_srcline = {
357 .se_header = "Source:Line",
358 .se_cmp = sort__srcline_cmp,
359 .se_snprintf = hist_entry__srcline_snprintf,
360 .se_width_idx = HISTC_SRCLINE,
361};
362
Andi Kleen31191a82015-08-07 15:54:24 -0700363/* --sort srcfile */
364
365static char no_srcfile[1];
366
Namhyung Kimcecaec62016-02-22 09:31:51 +0900367static char *hist_entry__get_srcfile(struct hist_entry *e)
Andi Kleen31191a82015-08-07 15:54:24 -0700368{
369 char *sf, *p;
370 struct map *map = e->ms.map;
371
Namhyung Kimcecaec62016-02-22 09:31:51 +0900372 if (!map)
373 return no_srcfile;
374
Andi Kleen2f84b422015-09-01 11:47:19 -0700375 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
376 e->ms.sym, false, true);
Andi Kleen76b10652015-08-11 06:36:55 -0700377 if (!strcmp(sf, SRCLINE_UNKNOWN))
378 return no_srcfile;
Andi Kleen31191a82015-08-07 15:54:24 -0700379 p = strchr(sf, ':');
380 if (p && *sf) {
381 *p = 0;
382 return sf;
383 }
384 free(sf);
385 return no_srcfile;
386}
387
388static int64_t
389sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
390{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900391 if (!left->srcfile)
392 left->srcfile = hist_entry__get_srcfile(left);
393 if (!right->srcfile)
394 right->srcfile = hist_entry__get_srcfile(right);
395
Andi Kleen31191a82015-08-07 15:54:24 -0700396 return strcmp(right->srcfile, left->srcfile);
397}
398
399static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
400 size_t size, unsigned int width)
401{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900402 if (!he->srcfile)
403 he->srcfile = hist_entry__get_srcfile(he);
404
Namhyung Kim2960ed62016-02-22 09:32:33 +0900405 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
Andi Kleen31191a82015-08-07 15:54:24 -0700406}
407
408struct sort_entry sort_srcfile = {
409 .se_header = "Source File",
410 .se_cmp = sort__srcfile_cmp,
411 .se_snprintf = hist_entry__srcfile_snprintf,
412 .se_width_idx = HISTC_SRCFILE,
413};
414
John Kacurdd68ada2009-09-24 18:02:49 +0200415/* --sort parent */
416
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200417static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200418sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
419{
420 struct symbol *sym_l = left->parent;
421 struct symbol *sym_r = right->parent;
422
423 if (!sym_l || !sym_r)
424 return cmp_null(sym_l, sym_r);
425
Namhyung Kim202e7a62014-03-04 11:01:41 +0900426 return strcmp(sym_r->name, sym_l->name);
John Kacurdd68ada2009-09-24 18:02:49 +0200427}
428
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300429static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300430 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200431{
Namhyung Kim5b591662014-07-31 14:47:38 +0900432 return repsep_snprintf(bf, size, "%-*.*s", width, width,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300433 he->parent ? he->parent->name : "[other]");
John Kacurdd68ada2009-09-24 18:02:49 +0200434}
435
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200436struct sort_entry sort_parent = {
437 .se_header = "Parent symbol",
438 .se_cmp = sort__parent_cmp,
439 .se_snprintf = hist_entry__parent_snprintf,
440 .se_width_idx = HISTC_PARENT,
441};
442
Arun Sharmaf60f3592010-06-04 11:27:10 -0300443/* --sort cpu */
444
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200445static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300446sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
447{
448 return right->cpu - left->cpu;
449}
450
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300451static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
452 size_t size, unsigned int width)
Arun Sharmaf60f3592010-06-04 11:27:10 -0300453{
Namhyung Kim5b591662014-07-31 14:47:38 +0900454 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300455}
456
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200457struct sort_entry sort_cpu = {
458 .se_header = "CPU",
459 .se_cmp = sort__cpu_cmp,
460 .se_snprintf = hist_entry__cpu_snprintf,
461 .se_width_idx = HISTC_CPU,
462};
463
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400464/* --sort socket */
465
466static int64_t
467sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
468{
469 return right->socket - left->socket;
470}
471
472static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
473 size_t size, unsigned int width)
474{
475 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
476}
477
Namhyung Kim54430102016-02-25 00:13:37 +0900478static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
479{
480 int sk = *(const int *)arg;
481
482 if (type != HIST_FILTER__SOCKET)
483 return -1;
484
485 return sk >= 0 && he->socket != sk;
486}
487
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400488struct sort_entry sort_socket = {
489 .se_header = "Socket",
490 .se_cmp = sort__socket_cmp,
491 .se_snprintf = hist_entry__socket_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900492 .se_filter = hist_entry__socket_filter,
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400493 .se_width_idx = HISTC_SOCKET,
494};
495
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900496/* --sort trace */
497
498static char *get_trace_output(struct hist_entry *he)
499{
500 struct trace_seq seq;
501 struct perf_evsel *evsel;
502 struct pevent_record rec = {
503 .data = he->raw_data,
504 .size = he->raw_size,
505 };
506
507 evsel = hists_to_evsel(he->hists);
508
509 trace_seq_init(&seq);
Namhyung Kim053a3982015-12-23 02:07:05 +0900510 if (symbol_conf.raw_trace) {
511 pevent_print_fields(&seq, he->raw_data, he->raw_size,
512 evsel->tp_format);
513 } else {
514 pevent_event_info(&seq, evsel->tp_format, &rec);
515 }
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900516 return seq.buffer;
517}
518
519static int64_t
520sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
521{
522 struct perf_evsel *evsel;
523
524 evsel = hists_to_evsel(left->hists);
525 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
526 return 0;
527
528 if (left->trace_output == NULL)
529 left->trace_output = get_trace_output(left);
530 if (right->trace_output == NULL)
531 right->trace_output = get_trace_output(right);
532
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900533 return strcmp(right->trace_output, left->trace_output);
534}
535
536static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
537 size_t size, unsigned int width)
538{
539 struct perf_evsel *evsel;
540
541 evsel = hists_to_evsel(he->hists);
542 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
Namhyung Kim2960ed62016-02-22 09:32:33 +0900543 return scnprintf(bf, size, "%-.*s", width, "N/A");
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900544
545 if (he->trace_output == NULL)
546 he->trace_output = get_trace_output(he);
Namhyung Kim2960ed62016-02-22 09:32:33 +0900547 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900548}
549
550struct sort_entry sort_trace = {
551 .se_header = "Trace output",
552 .se_cmp = sort__trace_cmp,
553 .se_snprintf = hist_entry__trace_snprintf,
554 .se_width_idx = HISTC_TRACE,
555};
556
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900557/* sort keys for branch stacks */
558
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100559static int64_t
560sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
561{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200562 if (!left->branch_info || !right->branch_info)
563 return cmp_null(left->branch_info, right->branch_info);
564
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100565 return _sort__dso_cmp(left->branch_info->from.map,
566 right->branch_info->from.map);
567}
568
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300569static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100570 size_t size, unsigned int width)
571{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200572 if (he->branch_info)
573 return _hist_entry__dso_snprintf(he->branch_info->from.map,
574 bf, size, width);
575 else
576 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100577}
578
Namhyung Kim54430102016-02-25 00:13:37 +0900579static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
580 const void *arg)
581{
582 const struct dso *dso = arg;
583
584 if (type != HIST_FILTER__DSO)
585 return -1;
586
587 return dso && (!he->branch_info || !he->branch_info->from.map ||
588 he->branch_info->from.map->dso != dso);
589}
590
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100591static int64_t
592sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
593{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200594 if (!left->branch_info || !right->branch_info)
595 return cmp_null(left->branch_info, right->branch_info);
596
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100597 return _sort__dso_cmp(left->branch_info->to.map,
598 right->branch_info->to.map);
599}
600
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300601static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100602 size_t size, unsigned int width)
603{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200604 if (he->branch_info)
605 return _hist_entry__dso_snprintf(he->branch_info->to.map,
606 bf, size, width);
607 else
608 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100609}
610
Namhyung Kim54430102016-02-25 00:13:37 +0900611static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
612 const void *arg)
613{
614 const struct dso *dso = arg;
615
616 if (type != HIST_FILTER__DSO)
617 return -1;
618
619 return dso && (!he->branch_info || !he->branch_info->to.map ||
620 he->branch_info->to.map->dso != dso);
621}
622
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100623static int64_t
624sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
625{
626 struct addr_map_symbol *from_l = &left->branch_info->from;
627 struct addr_map_symbol *from_r = &right->branch_info->from;
628
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200629 if (!left->branch_info || !right->branch_info)
630 return cmp_null(left->branch_info, right->branch_info);
631
632 from_l = &left->branch_info->from;
633 from_r = &right->branch_info->from;
634
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100635 if (!from_l->sym && !from_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900636 return _sort__addr_cmp(from_l->addr, from_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100637
Namhyung Kim51f27d12013-02-06 14:57:15 +0900638 return _sort__sym_cmp(from_l->sym, from_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100639}
640
641static int64_t
642sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
643{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200644 struct addr_map_symbol *to_l, *to_r;
645
646 if (!left->branch_info || !right->branch_info)
647 return cmp_null(left->branch_info, right->branch_info);
648
649 to_l = &left->branch_info->to;
650 to_r = &right->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100651
652 if (!to_l->sym && !to_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900653 return _sort__addr_cmp(to_l->addr, to_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100654
Namhyung Kim51f27d12013-02-06 14:57:15 +0900655 return _sort__sym_cmp(to_l->sym, to_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100656}
657
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300658static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900659 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100660{
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200661 if (he->branch_info) {
662 struct addr_map_symbol *from = &he->branch_info->from;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100663
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200664 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
665 he->level, bf, size, width);
666 }
667
668 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100669}
670
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300671static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900672 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100673{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200674 if (he->branch_info) {
675 struct addr_map_symbol *to = &he->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100676
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200677 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
678 he->level, bf, size, width);
679 }
680
681 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100682}
683
Namhyung Kim54430102016-02-25 00:13:37 +0900684static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
685 const void *arg)
686{
687 const char *sym = arg;
688
689 if (type != HIST_FILTER__SYMBOL)
690 return -1;
691
692 return sym && !(he->branch_info && he->branch_info->from.sym &&
693 strstr(he->branch_info->from.sym->name, sym));
694}
695
696static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
697 const void *arg)
698{
699 const char *sym = arg;
700
701 if (type != HIST_FILTER__SYMBOL)
702 return -1;
703
704 return sym && !(he->branch_info && he->branch_info->to.sym &&
705 strstr(he->branch_info->to.sym->name, sym));
706}
707
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900708struct sort_entry sort_dso_from = {
709 .se_header = "Source Shared Object",
710 .se_cmp = sort__dso_from_cmp,
711 .se_snprintf = hist_entry__dso_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900712 .se_filter = hist_entry__dso_from_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900713 .se_width_idx = HISTC_DSO_FROM,
714};
715
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100716struct sort_entry sort_dso_to = {
717 .se_header = "Target Shared Object",
718 .se_cmp = sort__dso_to_cmp,
719 .se_snprintf = hist_entry__dso_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900720 .se_filter = hist_entry__dso_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100721 .se_width_idx = HISTC_DSO_TO,
722};
723
724struct sort_entry sort_sym_from = {
725 .se_header = "Source Symbol",
726 .se_cmp = sort__sym_from_cmp,
727 .se_snprintf = hist_entry__sym_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900728 .se_filter = hist_entry__sym_from_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100729 .se_width_idx = HISTC_SYMBOL_FROM,
730};
731
732struct sort_entry sort_sym_to = {
733 .se_header = "Target Symbol",
734 .se_cmp = sort__sym_to_cmp,
735 .se_snprintf = hist_entry__sym_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900736 .se_filter = hist_entry__sym_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100737 .se_width_idx = HISTC_SYMBOL_TO,
738};
739
740static int64_t
741sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
742{
Jiri Olsa428560e2014-10-16 16:07:03 +0200743 unsigned char mp, p;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100744
Jiri Olsa428560e2014-10-16 16:07:03 +0200745 if (!left->branch_info || !right->branch_info)
746 return cmp_null(left->branch_info, right->branch_info);
747
748 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
749 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100750 return mp || p;
751}
752
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300753static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100754 size_t size, unsigned int width){
755 static const char *out = "N/A";
756
Jiri Olsa428560e2014-10-16 16:07:03 +0200757 if (he->branch_info) {
758 if (he->branch_info->flags.predicted)
759 out = "N";
760 else if (he->branch_info->flags.mispred)
761 out = "Y";
762 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100763
Namhyung Kim5b591662014-07-31 14:47:38 +0900764 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100765}
766
Andi Kleen0e332f02015-07-18 08:24:46 -0700767static int64_t
768sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
769{
770 return left->branch_info->flags.cycles -
771 right->branch_info->flags.cycles;
772}
773
774static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
775 size_t size, unsigned int width)
776{
777 if (he->branch_info->flags.cycles == 0)
778 return repsep_snprintf(bf, size, "%-*s", width, "-");
779 return repsep_snprintf(bf, size, "%-*hd", width,
780 he->branch_info->flags.cycles);
781}
782
783struct sort_entry sort_cycles = {
784 .se_header = "Basic Block Cycles",
785 .se_cmp = sort__cycles_cmp,
786 .se_snprintf = hist_entry__cycles_snprintf,
787 .se_width_idx = HISTC_CYCLES,
788};
789
Stephane Eranian98a3b322013-01-24 16:10:35 +0100790/* --sort daddr_sym */
791static int64_t
792sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
793{
794 uint64_t l = 0, r = 0;
795
796 if (left->mem_info)
797 l = left->mem_info->daddr.addr;
798 if (right->mem_info)
799 r = right->mem_info->daddr.addr;
800
801 return (int64_t)(r - l);
802}
803
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300804static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100805 size_t size, unsigned int width)
806{
807 uint64_t addr = 0;
808 struct map *map = NULL;
809 struct symbol *sym = NULL;
810
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300811 if (he->mem_info) {
812 addr = he->mem_info->daddr.addr;
813 map = he->mem_info->daddr.map;
814 sym = he->mem_info->daddr.sym;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100815 }
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300816 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100817 width);
818}
819
820static int64_t
Don Zickus28e6db22015-10-05 20:06:07 +0200821sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
822{
823 uint64_t l = 0, r = 0;
824
825 if (left->mem_info)
826 l = left->mem_info->iaddr.addr;
827 if (right->mem_info)
828 r = right->mem_info->iaddr.addr;
829
830 return (int64_t)(r - l);
831}
832
833static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
834 size_t size, unsigned int width)
835{
836 uint64_t addr = 0;
837 struct map *map = NULL;
838 struct symbol *sym = NULL;
839
840 if (he->mem_info) {
841 addr = he->mem_info->iaddr.addr;
842 map = he->mem_info->iaddr.map;
843 sym = he->mem_info->iaddr.sym;
844 }
845 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
846 width);
847}
848
849static int64_t
Stephane Eranian98a3b322013-01-24 16:10:35 +0100850sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
851{
852 struct map *map_l = NULL;
853 struct map *map_r = NULL;
854
855 if (left->mem_info)
856 map_l = left->mem_info->daddr.map;
857 if (right->mem_info)
858 map_r = right->mem_info->daddr.map;
859
860 return _sort__dso_cmp(map_l, map_r);
861}
862
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300863static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100864 size_t size, unsigned int width)
865{
866 struct map *map = NULL;
867
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300868 if (he->mem_info)
869 map = he->mem_info->daddr.map;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100870
871 return _hist_entry__dso_snprintf(map, bf, size, width);
872}
873
874static int64_t
875sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
876{
877 union perf_mem_data_src data_src_l;
878 union perf_mem_data_src data_src_r;
879
880 if (left->mem_info)
881 data_src_l = left->mem_info->data_src;
882 else
883 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
884
885 if (right->mem_info)
886 data_src_r = right->mem_info->data_src;
887 else
888 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
889
890 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
891}
892
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300893static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100894 size_t size, unsigned int width)
895{
Jiri Olsa69a77272016-02-24 09:46:49 +0100896 char out[10];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100897
Jiri Olsa69a77272016-02-24 09:46:49 +0100898 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300899 return repsep_snprintf(bf, size, "%.*s", width, out);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100900}
901
902static int64_t
903sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
904{
905 union perf_mem_data_src data_src_l;
906 union perf_mem_data_src data_src_r;
907
908 if (left->mem_info)
909 data_src_l = left->mem_info->data_src;
910 else
911 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
912
913 if (right->mem_info)
914 data_src_r = right->mem_info->data_src;
915 else
916 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
917
918 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
919}
920
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300921static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100922 size_t size, unsigned int width)
923{
924 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100925
Jiri Olsa0c877d72016-02-24 09:46:46 +0100926 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100927 return repsep_snprintf(bf, size, "%-*s", width, out);
928}
929
930static int64_t
931sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
932{
933 union perf_mem_data_src data_src_l;
934 union perf_mem_data_src data_src_r;
935
936 if (left->mem_info)
937 data_src_l = left->mem_info->data_src;
938 else
939 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
940
941 if (right->mem_info)
942 data_src_r = right->mem_info->data_src;
943 else
944 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
945
946 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
947}
948
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300949static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100950 size_t size, unsigned int width)
951{
952 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100953
Jiri Olsa071e9a12016-02-24 09:46:47 +0100954 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100955 return repsep_snprintf(bf, size, "%-*s", width, out);
956}
957
958static int64_t
959sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
960{
961 union perf_mem_data_src data_src_l;
962 union perf_mem_data_src data_src_r;
963
964 if (left->mem_info)
965 data_src_l = left->mem_info->data_src;
966 else
967 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
968
969 if (right->mem_info)
970 data_src_r = right->mem_info->data_src;
971 else
972 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
973
974 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
975}
976
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300977static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100978 size_t size, unsigned int width)
979{
980 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100981
Jiri Olsa2c07af12016-02-24 09:46:48 +0100982 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100983 return repsep_snprintf(bf, size, "%-*s", width, out);
984}
985
Don Zickus9b32ba72014-06-01 15:38:29 +0200986static int64_t
987sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
988{
989 u64 l, r;
990 struct map *l_map, *r_map;
991
992 if (!left->mem_info) return -1;
993 if (!right->mem_info) return 1;
994
995 /* group event types together */
996 if (left->cpumode > right->cpumode) return -1;
997 if (left->cpumode < right->cpumode) return 1;
998
999 l_map = left->mem_info->daddr.map;
1000 r_map = right->mem_info->daddr.map;
1001
1002 /* if both are NULL, jump to sort on al_addr instead */
1003 if (!l_map && !r_map)
1004 goto addr;
1005
1006 if (!l_map) return -1;
1007 if (!r_map) return 1;
1008
1009 if (l_map->maj > r_map->maj) return -1;
1010 if (l_map->maj < r_map->maj) return 1;
1011
1012 if (l_map->min > r_map->min) return -1;
1013 if (l_map->min < r_map->min) return 1;
1014
1015 if (l_map->ino > r_map->ino) return -1;
1016 if (l_map->ino < r_map->ino) return 1;
1017
1018 if (l_map->ino_generation > r_map->ino_generation) return -1;
1019 if (l_map->ino_generation < r_map->ino_generation) return 1;
1020
1021 /*
1022 * Addresses with no major/minor numbers are assumed to be
1023 * anonymous in userspace. Sort those on pid then address.
1024 *
1025 * The kernel and non-zero major/minor mapped areas are
1026 * assumed to be unity mapped. Sort those on address.
1027 */
1028
1029 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1030 (!(l_map->flags & MAP_SHARED)) &&
1031 !l_map->maj && !l_map->min && !l_map->ino &&
1032 !l_map->ino_generation) {
1033 /* userspace anonymous */
1034
1035 if (left->thread->pid_ > right->thread->pid_) return -1;
1036 if (left->thread->pid_ < right->thread->pid_) return 1;
1037 }
1038
1039addr:
1040 /* al_addr does all the right addr - start + offset calculations */
1041 l = cl_address(left->mem_info->daddr.al_addr);
1042 r = cl_address(right->mem_info->daddr.al_addr);
1043
1044 if (l > r) return -1;
1045 if (l < r) return 1;
1046
1047 return 0;
1048}
1049
1050static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1051 size_t size, unsigned int width)
1052{
1053
1054 uint64_t addr = 0;
1055 struct map *map = NULL;
1056 struct symbol *sym = NULL;
1057 char level = he->level;
1058
1059 if (he->mem_info) {
1060 addr = cl_address(he->mem_info->daddr.al_addr);
1061 map = he->mem_info->daddr.map;
1062 sym = he->mem_info->daddr.sym;
1063
1064 /* print [s] for shared data mmaps */
1065 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1066 map && (map->type == MAP__VARIABLE) &&
1067 (map->flags & MAP_SHARED) &&
1068 (map->maj || map->min || map->ino ||
1069 map->ino_generation))
1070 level = 's';
1071 else if (!map)
1072 level = 'X';
1073 }
1074 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1075 width);
1076}
1077
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001078struct sort_entry sort_mispredict = {
1079 .se_header = "Branch Mispredicted",
1080 .se_cmp = sort__mispredict_cmp,
1081 .se_snprintf = hist_entry__mispredict_snprintf,
1082 .se_width_idx = HISTC_MISPREDICT,
1083};
1084
Andi Kleen05484292013-01-24 16:10:29 +01001085static u64 he_weight(struct hist_entry *he)
1086{
1087 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1088}
1089
1090static int64_t
1091sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1092{
1093 return he_weight(left) - he_weight(right);
1094}
1095
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001096static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001097 size_t size, unsigned int width)
1098{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001099 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
Andi Kleen05484292013-01-24 16:10:29 +01001100}
1101
1102struct sort_entry sort_local_weight = {
1103 .se_header = "Local Weight",
1104 .se_cmp = sort__local_weight_cmp,
1105 .se_snprintf = hist_entry__local_weight_snprintf,
1106 .se_width_idx = HISTC_LOCAL_WEIGHT,
1107};
1108
1109static int64_t
1110sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1111{
1112 return left->stat.weight - right->stat.weight;
1113}
1114
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001115static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001116 size_t size, unsigned int width)
1117{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001118 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
Andi Kleen05484292013-01-24 16:10:29 +01001119}
1120
1121struct sort_entry sort_global_weight = {
1122 .se_header = "Weight",
1123 .se_cmp = sort__global_weight_cmp,
1124 .se_snprintf = hist_entry__global_weight_snprintf,
1125 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1126};
1127
Stephane Eranian98a3b322013-01-24 16:10:35 +01001128struct sort_entry sort_mem_daddr_sym = {
1129 .se_header = "Data Symbol",
1130 .se_cmp = sort__daddr_cmp,
1131 .se_snprintf = hist_entry__daddr_snprintf,
1132 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1133};
1134
Don Zickus28e6db22015-10-05 20:06:07 +02001135struct sort_entry sort_mem_iaddr_sym = {
1136 .se_header = "Code Symbol",
1137 .se_cmp = sort__iaddr_cmp,
1138 .se_snprintf = hist_entry__iaddr_snprintf,
1139 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1140};
1141
Stephane Eranian98a3b322013-01-24 16:10:35 +01001142struct sort_entry sort_mem_daddr_dso = {
1143 .se_header = "Data Object",
1144 .se_cmp = sort__dso_daddr_cmp,
1145 .se_snprintf = hist_entry__dso_daddr_snprintf,
1146 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1147};
1148
1149struct sort_entry sort_mem_locked = {
1150 .se_header = "Locked",
1151 .se_cmp = sort__locked_cmp,
1152 .se_snprintf = hist_entry__locked_snprintf,
1153 .se_width_idx = HISTC_MEM_LOCKED,
1154};
1155
1156struct sort_entry sort_mem_tlb = {
1157 .se_header = "TLB access",
1158 .se_cmp = sort__tlb_cmp,
1159 .se_snprintf = hist_entry__tlb_snprintf,
1160 .se_width_idx = HISTC_MEM_TLB,
1161};
1162
1163struct sort_entry sort_mem_lvl = {
1164 .se_header = "Memory access",
1165 .se_cmp = sort__lvl_cmp,
1166 .se_snprintf = hist_entry__lvl_snprintf,
1167 .se_width_idx = HISTC_MEM_LVL,
1168};
1169
1170struct sort_entry sort_mem_snoop = {
1171 .se_header = "Snoop",
1172 .se_cmp = sort__snoop_cmp,
1173 .se_snprintf = hist_entry__snoop_snprintf,
1174 .se_width_idx = HISTC_MEM_SNOOP,
1175};
1176
Don Zickus9b32ba72014-06-01 15:38:29 +02001177struct sort_entry sort_mem_dcacheline = {
1178 .se_header = "Data Cacheline",
1179 .se_cmp = sort__dcacheline_cmp,
1180 .se_snprintf = hist_entry__dcacheline_snprintf,
1181 .se_width_idx = HISTC_MEM_DCACHELINE,
1182};
1183
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001184static int64_t
1185sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1186{
Jiri Olsa49f47442014-10-16 16:07:01 +02001187 if (!left->branch_info || !right->branch_info)
1188 return cmp_null(left->branch_info, right->branch_info);
1189
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001190 return left->branch_info->flags.abort !=
1191 right->branch_info->flags.abort;
1192}
1193
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001194static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001195 size_t size, unsigned int width)
1196{
Jiri Olsa49f47442014-10-16 16:07:01 +02001197 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001198
Jiri Olsa49f47442014-10-16 16:07:01 +02001199 if (he->branch_info) {
1200 if (he->branch_info->flags.abort)
1201 out = "A";
1202 else
1203 out = ".";
1204 }
1205
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001206 return repsep_snprintf(bf, size, "%-*s", width, out);
1207}
1208
1209struct sort_entry sort_abort = {
1210 .se_header = "Transaction abort",
1211 .se_cmp = sort__abort_cmp,
1212 .se_snprintf = hist_entry__abort_snprintf,
1213 .se_width_idx = HISTC_ABORT,
1214};
1215
1216static int64_t
1217sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1218{
Jiri Olsa0199d242014-10-16 16:07:02 +02001219 if (!left->branch_info || !right->branch_info)
1220 return cmp_null(left->branch_info, right->branch_info);
1221
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001222 return left->branch_info->flags.in_tx !=
1223 right->branch_info->flags.in_tx;
1224}
1225
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001226static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001227 size_t size, unsigned int width)
1228{
Jiri Olsa0199d242014-10-16 16:07:02 +02001229 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001230
Jiri Olsa0199d242014-10-16 16:07:02 +02001231 if (he->branch_info) {
1232 if (he->branch_info->flags.in_tx)
1233 out = "T";
1234 else
1235 out = ".";
1236 }
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001237
1238 return repsep_snprintf(bf, size, "%-*s", width, out);
1239}
1240
1241struct sort_entry sort_in_tx = {
1242 .se_header = "Branch in transaction",
1243 .se_cmp = sort__in_tx_cmp,
1244 .se_snprintf = hist_entry__in_tx_snprintf,
1245 .se_width_idx = HISTC_IN_TX,
1246};
1247
Andi Kleen475eeab2013-09-20 07:40:43 -07001248static int64_t
1249sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1250{
1251 return left->transaction - right->transaction;
1252}
1253
1254static inline char *add_str(char *p, const char *str)
1255{
1256 strcpy(p, str);
1257 return p + strlen(str);
1258}
1259
1260static struct txbit {
1261 unsigned flag;
1262 const char *name;
1263 int skip_for_len;
1264} txbits[] = {
1265 { PERF_TXN_ELISION, "EL ", 0 },
1266 { PERF_TXN_TRANSACTION, "TX ", 1 },
1267 { PERF_TXN_SYNC, "SYNC ", 1 },
1268 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1269 { PERF_TXN_RETRY, "RETRY ", 0 },
1270 { PERF_TXN_CONFLICT, "CON ", 0 },
1271 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1272 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1273 { 0, NULL, 0 }
1274};
1275
1276int hist_entry__transaction_len(void)
1277{
1278 int i;
1279 int len = 0;
1280
1281 for (i = 0; txbits[i].name; i++) {
1282 if (!txbits[i].skip_for_len)
1283 len += strlen(txbits[i].name);
1284 }
1285 len += 4; /* :XX<space> */
1286 return len;
1287}
1288
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001289static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
Andi Kleen475eeab2013-09-20 07:40:43 -07001290 size_t size, unsigned int width)
1291{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001292 u64 t = he->transaction;
Andi Kleen475eeab2013-09-20 07:40:43 -07001293 char buf[128];
1294 char *p = buf;
1295 int i;
1296
1297 buf[0] = 0;
1298 for (i = 0; txbits[i].name; i++)
1299 if (txbits[i].flag & t)
1300 p = add_str(p, txbits[i].name);
1301 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1302 p = add_str(p, "NEITHER ");
1303 if (t & PERF_TXN_ABORT_MASK) {
1304 sprintf(p, ":%" PRIx64,
1305 (t & PERF_TXN_ABORT_MASK) >>
1306 PERF_TXN_ABORT_SHIFT);
1307 p += strlen(p);
1308 }
1309
1310 return repsep_snprintf(bf, size, "%-*s", width, buf);
1311}
1312
1313struct sort_entry sort_transaction = {
1314 .se_header = "Transaction ",
1315 .se_cmp = sort__transaction_cmp,
1316 .se_snprintf = hist_entry__transaction_snprintf,
1317 .se_width_idx = HISTC_TRANSACTION,
1318};
1319
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001320struct sort_dimension {
1321 const char *name;
1322 struct sort_entry *entry;
1323 int taken;
1324};
1325
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001326#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1327
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001328static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001329 DIM(SORT_PID, "pid", sort_thread),
1330 DIM(SORT_COMM, "comm", sort_comm),
1331 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001332 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001333 DIM(SORT_PARENT, "parent", sort_parent),
1334 DIM(SORT_CPU, "cpu", sort_cpu),
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001335 DIM(SORT_SOCKET, "socket", sort_socket),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -03001336 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Andi Kleen31191a82015-08-07 15:54:24 -07001337 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
Andi Kleenf9ea55d2013-07-18 15:58:53 -07001338 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1339 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
Andi Kleen475eeab2013-09-20 07:40:43 -07001340 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
Namhyung Kima34bb6a2015-12-23 02:07:04 +09001341 DIM(SORT_TRACE, "trace", sort_trace),
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001342};
1343
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001344#undef DIM
1345
1346#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1347
1348static struct sort_dimension bstack_sort_dimensions[] = {
1349 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1350 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1351 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1352 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1353 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001354 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1355 DIM(SORT_ABORT, "abort", sort_abort),
Andi Kleen0e332f02015-07-18 08:24:46 -07001356 DIM(SORT_CYCLES, "cycles", sort_cycles),
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001357};
1358
1359#undef DIM
1360
Namhyung Kimafab87b2013-04-03 21:26:11 +09001361#define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1362
1363static struct sort_dimension memory_sort_dimensions[] = {
Namhyung Kimafab87b2013-04-03 21:26:11 +09001364 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
Don Zickus28e6db22015-10-05 20:06:07 +02001365 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001366 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1367 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1368 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1369 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1370 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
Don Zickus9b32ba72014-06-01 15:38:29 +02001371 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001372};
1373
1374#undef DIM
1375
Namhyung Kima2ce0672014-03-04 09:06:42 +09001376struct hpp_dimension {
1377 const char *name;
1378 struct perf_hpp_fmt *fmt;
1379 int taken;
1380};
1381
1382#define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1383
1384static struct hpp_dimension hpp_sort_dimensions[] = {
1385 DIM(PERF_HPP__OVERHEAD, "overhead"),
1386 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1387 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1388 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1389 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
Namhyung Kim594dcbf2013-10-30 16:06:59 +09001390 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
Namhyung Kima2ce0672014-03-04 09:06:42 +09001391 DIM(PERF_HPP__SAMPLES, "sample"),
1392 DIM(PERF_HPP__PERIOD, "period"),
1393};
1394
1395#undef DIM
1396
Namhyung Kim8b536992014-03-03 11:46:55 +09001397struct hpp_sort_entry {
1398 struct perf_hpp_fmt hpp;
1399 struct sort_entry *se;
1400};
1401
Namhyung Kime0d66c72014-07-31 14:47:37 +09001402void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
Namhyung Kim678a5002014-03-20 11:18:54 +09001403{
1404 struct hpp_sort_entry *hse;
1405
1406 if (!perf_hpp__is_sort_entry(fmt))
1407 return;
1408
1409 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001410 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
Namhyung Kim678a5002014-03-20 11:18:54 +09001411}
1412
Namhyung Kim8b536992014-03-03 11:46:55 +09001413static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1414 struct perf_evsel *evsel)
1415{
1416 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001417 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001418
1419 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001420
Namhyung Kim5b591662014-07-31 14:47:38 +09001421 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001422 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001423
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001424 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
Namhyung Kim8b536992014-03-03 11:46:55 +09001425}
1426
1427static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1428 struct perf_hpp *hpp __maybe_unused,
1429 struct perf_evsel *evsel)
1430{
1431 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001432 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001433
1434 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1435
Namhyung Kim5b591662014-07-31 14:47:38 +09001436 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001437 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001438
1439 return len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001440}
1441
1442static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1443 struct hist_entry *he)
1444{
1445 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001446 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001447
1448 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim5b591662014-07-31 14:47:38 +09001449
1450 if (!len)
1451 len = hists__col_len(he->hists, hse->se->se_width_idx);
Namhyung Kim8b536992014-03-03 11:46:55 +09001452
1453 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1454}
1455
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001456static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1457 struct hist_entry *a, struct hist_entry *b)
1458{
1459 struct hpp_sort_entry *hse;
1460
1461 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1462 return hse->se->se_cmp(a, b);
1463}
1464
1465static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1466 struct hist_entry *a, struct hist_entry *b)
1467{
1468 struct hpp_sort_entry *hse;
1469 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1470
1471 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1472 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1473 return collapse_fn(a, b);
1474}
1475
1476static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1477 struct hist_entry *a, struct hist_entry *b)
1478{
1479 struct hpp_sort_entry *hse;
1480 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1481
1482 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1483 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1484 return sort_fn(a, b);
1485}
1486
Jiri Olsa97358082016-01-18 10:24:03 +01001487bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1488{
1489 return format->header == __sort__hpp_header;
1490}
1491
Namhyung Kim4945cf22016-03-09 22:46:57 +09001492#define MK_SORT_ENTRY_CHK(key) \
1493bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1494{ \
1495 struct hpp_sort_entry *hse; \
1496 \
1497 if (!perf_hpp__is_sort_entry(fmt)) \
1498 return false; \
1499 \
1500 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1501 return hse->se == &sort_ ## key ; \
Namhyung Kima9c6e462016-02-25 00:13:33 +09001502}
1503
Namhyung Kim4945cf22016-03-09 22:46:57 +09001504MK_SORT_ENTRY_CHK(trace)
1505MK_SORT_ENTRY_CHK(srcline)
1506MK_SORT_ENTRY_CHK(srcfile)
1507MK_SORT_ENTRY_CHK(thread)
1508MK_SORT_ENTRY_CHK(comm)
1509MK_SORT_ENTRY_CHK(dso)
1510MK_SORT_ENTRY_CHK(sym)
Namhyung Kima9c6e462016-02-25 00:13:33 +09001511
Namhyung Kima9c6e462016-02-25 00:13:33 +09001512
Jiri Olsa97358082016-01-18 10:24:03 +01001513static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1514{
1515 struct hpp_sort_entry *hse_a;
1516 struct hpp_sort_entry *hse_b;
1517
1518 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1519 return false;
1520
1521 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1522 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1523
1524 return hse_a->se == hse_b->se;
1525}
1526
Jiri Olsa564132f2016-01-18 10:24:09 +01001527static void hse_free(struct perf_hpp_fmt *fmt)
1528{
1529 struct hpp_sort_entry *hse;
1530
1531 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1532 free(hse);
1533}
1534
Namhyung Kima7d945b2014-03-04 10:46:34 +09001535static struct hpp_sort_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001536__sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
Namhyung Kim8b536992014-03-03 11:46:55 +09001537{
1538 struct hpp_sort_entry *hse;
1539
1540 hse = malloc(sizeof(*hse));
1541 if (hse == NULL) {
1542 pr_err("Memory allocation failed\n");
Namhyung Kima7d945b2014-03-04 10:46:34 +09001543 return NULL;
Namhyung Kim8b536992014-03-03 11:46:55 +09001544 }
1545
1546 hse->se = sd->entry;
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001547 hse->hpp.name = sd->entry->se_header;
Namhyung Kim8b536992014-03-03 11:46:55 +09001548 hse->hpp.header = __sort__hpp_header;
1549 hse->hpp.width = __sort__hpp_width;
1550 hse->hpp.entry = __sort__hpp_entry;
1551 hse->hpp.color = NULL;
1552
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001553 hse->hpp.cmp = __sort__hpp_cmp;
1554 hse->hpp.collapse = __sort__hpp_collapse;
1555 hse->hpp.sort = __sort__hpp_sort;
Jiri Olsa97358082016-01-18 10:24:03 +01001556 hse->hpp.equal = __sort__hpp_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001557 hse->hpp.free = hse_free;
Namhyung Kim8b536992014-03-03 11:46:55 +09001558
1559 INIT_LIST_HEAD(&hse->hpp.list);
1560 INIT_LIST_HEAD(&hse->hpp.sort_list);
Jiri Olsaf2998422014-05-23 17:15:47 +02001561 hse->hpp.elide = false;
Namhyung Kime0d66c72014-07-31 14:47:37 +09001562 hse->hpp.len = 0;
Namhyung Kim5b591662014-07-31 14:47:38 +09001563 hse->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001564 hse->hpp.level = level;
Namhyung Kim8b536992014-03-03 11:46:55 +09001565
Namhyung Kima7d945b2014-03-04 10:46:34 +09001566 return hse;
1567}
1568
Jiri Olsa564132f2016-01-18 10:24:09 +01001569static void hpp_free(struct perf_hpp_fmt *fmt)
1570{
1571 free(fmt);
1572}
1573
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001574static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1575 int level)
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001576{
1577 struct perf_hpp_fmt *fmt;
1578
1579 fmt = memdup(hd->fmt, sizeof(*fmt));
1580 if (fmt) {
1581 INIT_LIST_HEAD(&fmt->list);
1582 INIT_LIST_HEAD(&fmt->sort_list);
Jiri Olsa564132f2016-01-18 10:24:09 +01001583 fmt->free = hpp_free;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001584 fmt->level = level;
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001585 }
1586
1587 return fmt;
1588}
1589
Namhyung Kim54430102016-02-25 00:13:37 +09001590int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1591{
1592 struct perf_hpp_fmt *fmt;
1593 struct hpp_sort_entry *hse;
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001594 int ret = -1;
1595 int r;
Namhyung Kim54430102016-02-25 00:13:37 +09001596
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001597 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1598 if (!perf_hpp__is_sort_entry(fmt))
1599 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001600
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001601 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1602 if (hse->se->se_filter == NULL)
1603 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001604
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001605 /*
1606 * hist entry is filtered if any of sort key in the hpp list
1607 * is applied. But it should skip non-matched filter types.
1608 */
1609 r = hse->se->se_filter(he, type, arg);
1610 if (r >= 0) {
1611 if (ret < 0)
1612 ret = 0;
1613 ret |= r;
1614 }
1615 }
1616
1617 return ret;
Namhyung Kim54430102016-02-25 00:13:37 +09001618}
1619
Jiri Olsad7b617f2016-03-09 11:04:17 +01001620static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1621 struct perf_hpp_list *list,
1622 int level)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001623{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001624 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001625
1626 if (hse == NULL)
1627 return -1;
1628
Jiri Olsad7b617f2016-03-09 11:04:17 +01001629 perf_hpp_list__register_sort_field(list, &hse->hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001630 return 0;
1631}
1632
Jiri Olsad7b617f2016-03-09 11:04:17 +01001633static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1634 struct perf_hpp_list *list)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001635{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001636 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001637
1638 if (hse == NULL)
1639 return -1;
1640
Jiri Olsa07600022016-01-18 10:24:16 +01001641 perf_hpp_list__column_register(list, &hse->hpp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001642 return 0;
1643}
1644
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001645struct hpp_dynamic_entry {
1646 struct perf_hpp_fmt hpp;
1647 struct perf_evsel *evsel;
1648 struct format_field *field;
1649 unsigned dynamic_len;
Namhyung Kim053a3982015-12-23 02:07:05 +09001650 bool raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001651};
1652
1653static int hde_width(struct hpp_dynamic_entry *hde)
1654{
1655 if (!hde->hpp.len) {
1656 int len = hde->dynamic_len;
1657 int namelen = strlen(hde->field->name);
1658 int fieldlen = hde->field->size;
1659
1660 if (namelen > len)
1661 len = namelen;
1662
1663 if (!(hde->field->flags & FIELD_IS_STRING)) {
1664 /* length for print hex numbers */
1665 fieldlen = hde->field->size * 2 + 2;
1666 }
1667 if (fieldlen > len)
1668 len = fieldlen;
1669
1670 hde->hpp.len = len;
1671 }
1672 return hde->hpp.len;
1673}
1674
Namhyung Kim60517d22015-12-23 02:07:03 +09001675static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1676 struct hist_entry *he)
1677{
1678 char *str, *pos;
1679 struct format_field *field = hde->field;
1680 size_t namelen;
1681 bool last = false;
1682
Namhyung Kim053a3982015-12-23 02:07:05 +09001683 if (hde->raw_trace)
1684 return;
1685
Namhyung Kim60517d22015-12-23 02:07:03 +09001686 /* parse pretty print result and update max length */
1687 if (!he->trace_output)
1688 he->trace_output = get_trace_output(he);
1689
1690 namelen = strlen(field->name);
1691 str = he->trace_output;
1692
1693 while (str) {
1694 pos = strchr(str, ' ');
1695 if (pos == NULL) {
1696 last = true;
1697 pos = str + strlen(str);
1698 }
1699
1700 if (!strncmp(str, field->name, namelen)) {
1701 size_t len;
1702
1703 str += namelen + 1;
1704 len = pos - str;
1705
1706 if (len > hde->dynamic_len)
1707 hde->dynamic_len = len;
1708 break;
1709 }
1710
1711 if (last)
1712 str = NULL;
1713 else
1714 str = pos + 1;
1715 }
1716}
1717
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001718static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1719 struct perf_evsel *evsel __maybe_unused)
1720{
1721 struct hpp_dynamic_entry *hde;
1722 size_t len = fmt->user_len;
1723
1724 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1725
1726 if (!len)
1727 len = hde_width(hde);
1728
1729 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1730}
1731
1732static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1733 struct perf_hpp *hpp __maybe_unused,
1734 struct perf_evsel *evsel __maybe_unused)
1735{
1736 struct hpp_dynamic_entry *hde;
1737 size_t len = fmt->user_len;
1738
1739 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1740
1741 if (!len)
1742 len = hde_width(hde);
1743
1744 return len;
1745}
1746
Namhyung Kim361459f2015-12-23 02:07:08 +09001747bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1748{
1749 struct hpp_dynamic_entry *hde;
1750
1751 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1752
1753 return hists_to_evsel(hists) == hde->evsel;
1754}
1755
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001756static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1757 struct hist_entry *he)
1758{
1759 struct hpp_dynamic_entry *hde;
1760 size_t len = fmt->user_len;
Namhyung Kim60517d22015-12-23 02:07:03 +09001761 char *str, *pos;
1762 struct format_field *field;
1763 size_t namelen;
1764 bool last = false;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001765 int ret;
1766
1767 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1768
1769 if (!len)
1770 len = hde_width(hde);
1771
Namhyung Kim053a3982015-12-23 02:07:05 +09001772 if (hde->raw_trace)
1773 goto raw_field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001774
Namhyung Kime049d4a2016-02-27 03:52:46 +09001775 if (!he->trace_output)
1776 he->trace_output = get_trace_output(he);
1777
Namhyung Kim053a3982015-12-23 02:07:05 +09001778 field = hde->field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001779 namelen = strlen(field->name);
1780 str = he->trace_output;
1781
1782 while (str) {
1783 pos = strchr(str, ' ');
1784 if (pos == NULL) {
1785 last = true;
1786 pos = str + strlen(str);
1787 }
1788
1789 if (!strncmp(str, field->name, namelen)) {
1790 str += namelen + 1;
1791 str = strndup(str, pos - str);
1792
1793 if (str == NULL)
1794 return scnprintf(hpp->buf, hpp->size,
1795 "%*.*s", len, len, "ERROR");
1796 break;
1797 }
1798
1799 if (last)
1800 str = NULL;
1801 else
1802 str = pos + 1;
1803 }
1804
1805 if (str == NULL) {
1806 struct trace_seq seq;
Namhyung Kim053a3982015-12-23 02:07:05 +09001807raw_field:
Namhyung Kim60517d22015-12-23 02:07:03 +09001808 trace_seq_init(&seq);
1809 pevent_print_field(&seq, he->raw_data, hde->field);
1810 str = seq.buffer;
1811 }
1812
1813 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1814 free(str);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001815 return ret;
1816}
1817
1818static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1819 struct hist_entry *a, struct hist_entry *b)
1820{
1821 struct hpp_dynamic_entry *hde;
1822 struct format_field *field;
1823 unsigned offset, size;
1824
1825 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1826
Namhyung Kimabab5e72016-02-27 03:52:47 +09001827 if (b == NULL) {
1828 update_dynamic_len(hde, a);
1829 return 0;
1830 }
1831
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001832 field = hde->field;
1833 if (field->flags & FIELD_IS_DYNAMIC) {
1834 unsigned long long dyn;
1835
1836 pevent_read_number_field(field, a->raw_data, &dyn);
1837 offset = dyn & 0xffff;
1838 size = (dyn >> 16) & 0xffff;
1839
1840 /* record max width for output */
1841 if (size > hde->dynamic_len)
1842 hde->dynamic_len = size;
1843 } else {
1844 offset = field->offset;
1845 size = field->size;
1846 }
1847
1848 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1849}
1850
Namhyung Kim361459f2015-12-23 02:07:08 +09001851bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1852{
1853 return fmt->cmp == __sort__hde_cmp;
1854}
1855
Namhyung Kim665aa752016-02-21 23:22:35 +09001856static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1857{
1858 struct hpp_dynamic_entry *hde_a;
1859 struct hpp_dynamic_entry *hde_b;
1860
1861 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1862 return false;
1863
1864 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1865 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1866
1867 return hde_a->field == hde_b->field;
1868}
1869
Jiri Olsa564132f2016-01-18 10:24:09 +01001870static void hde_free(struct perf_hpp_fmt *fmt)
1871{
1872 struct hpp_dynamic_entry *hde;
1873
1874 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1875 free(hde);
1876}
1877
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001878static struct hpp_dynamic_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001879__alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1880 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001881{
1882 struct hpp_dynamic_entry *hde;
1883
1884 hde = malloc(sizeof(*hde));
1885 if (hde == NULL) {
1886 pr_debug("Memory allocation failed\n");
1887 return NULL;
1888 }
1889
1890 hde->evsel = evsel;
1891 hde->field = field;
1892 hde->dynamic_len = 0;
1893
1894 hde->hpp.name = field->name;
1895 hde->hpp.header = __sort__hde_header;
1896 hde->hpp.width = __sort__hde_width;
1897 hde->hpp.entry = __sort__hde_entry;
1898 hde->hpp.color = NULL;
1899
1900 hde->hpp.cmp = __sort__hde_cmp;
1901 hde->hpp.collapse = __sort__hde_cmp;
1902 hde->hpp.sort = __sort__hde_cmp;
Namhyung Kim665aa752016-02-21 23:22:35 +09001903 hde->hpp.equal = __sort__hde_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001904 hde->hpp.free = hde_free;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001905
1906 INIT_LIST_HEAD(&hde->hpp.list);
1907 INIT_LIST_HEAD(&hde->hpp.sort_list);
1908 hde->hpp.elide = false;
1909 hde->hpp.len = 0;
1910 hde->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001911 hde->hpp.level = level;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001912
1913 return hde;
1914}
1915
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03001916struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1917{
1918 struct perf_hpp_fmt *new_fmt = NULL;
1919
1920 if (perf_hpp__is_sort_entry(fmt)) {
1921 struct hpp_sort_entry *hse, *new_hse;
1922
1923 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1924 new_hse = memdup(hse, sizeof(*hse));
1925 if (new_hse)
1926 new_fmt = &new_hse->hpp;
1927 } else if (perf_hpp__is_dynamic_entry(fmt)) {
1928 struct hpp_dynamic_entry *hde, *new_hde;
1929
1930 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1931 new_hde = memdup(hde, sizeof(*hde));
1932 if (new_hde)
1933 new_fmt = &new_hde->hpp;
1934 } else {
1935 new_fmt = memdup(fmt, sizeof(*fmt));
1936 }
1937
1938 INIT_LIST_HEAD(&new_fmt->list);
1939 INIT_LIST_HEAD(&new_fmt->sort_list);
1940
1941 return new_fmt;
1942}
1943
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001944static int parse_field_name(char *str, char **event, char **field, char **opt)
1945{
1946 char *event_name, *field_name, *opt_name;
1947
1948 event_name = str;
1949 field_name = strchr(str, '.');
1950
1951 if (field_name) {
1952 *field_name++ = '\0';
1953 } else {
1954 event_name = NULL;
1955 field_name = str;
1956 }
1957
1958 opt_name = strchr(field_name, '/');
1959 if (opt_name)
1960 *opt_name++ = '\0';
1961
1962 *event = event_name;
1963 *field = field_name;
1964 *opt = opt_name;
1965
1966 return 0;
1967}
1968
1969/* find match evsel using a given event name. The event name can be:
Namhyung Kim9735be22016-01-05 19:58:35 +09001970 * 1. '%' + event index (e.g. '%1' for first event)
1971 * 2. full event name (e.g. sched:sched_switch)
1972 * 3. partial event name (should not contain ':')
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001973 */
1974static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1975{
1976 struct perf_evsel *evsel = NULL;
1977 struct perf_evsel *pos;
1978 bool full_name;
1979
1980 /* case 1 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001981 if (event_name[0] == '%') {
1982 int nr = strtol(event_name+1, NULL, 0);
1983
1984 if (nr > evlist->nr_entries)
1985 return NULL;
1986
1987 evsel = perf_evlist__first(evlist);
1988 while (--nr > 0)
1989 evsel = perf_evsel__next(evsel);
1990
1991 return evsel;
1992 }
1993
1994 full_name = !!strchr(event_name, ':');
1995 evlist__for_each(evlist, pos) {
Namhyung Kim9735be22016-01-05 19:58:35 +09001996 /* case 2 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001997 if (full_name && !strcmp(pos->name, event_name))
1998 return pos;
Namhyung Kim9735be22016-01-05 19:58:35 +09001999 /* case 3 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002000 if (!full_name && strstr(pos->name, event_name)) {
2001 if (evsel) {
2002 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2003 event_name, evsel->name, pos->name);
2004 return NULL;
2005 }
2006 evsel = pos;
2007 }
2008 }
2009
2010 return evsel;
2011}
2012
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002013static int __dynamic_dimension__add(struct perf_evsel *evsel,
2014 struct format_field *field,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002015 bool raw_trace, int level)
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002016{
2017 struct hpp_dynamic_entry *hde;
2018
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002019 hde = __alloc_dynamic_entry(evsel, field, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002020 if (hde == NULL)
2021 return -ENOMEM;
2022
2023 hde->raw_trace = raw_trace;
2024
2025 perf_hpp__register_sort_field(&hde->hpp);
2026 return 0;
2027}
2028
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002029static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002030{
2031 int ret;
2032 struct format_field *field;
2033
2034 field = evsel->tp_format->format.fields;
2035 while (field) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002036 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002037 if (ret < 0)
2038 return ret;
2039
2040 field = field->next;
2041 }
2042 return 0;
2043}
2044
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002045static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2046 int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002047{
2048 int ret;
2049 struct perf_evsel *evsel;
2050
2051 evlist__for_each(evlist, evsel) {
2052 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2053 continue;
2054
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002055 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002056 if (ret < 0)
2057 return ret;
2058 }
2059 return 0;
2060}
2061
Namhyung Kim9735be22016-01-05 19:58:35 +09002062static int add_all_matching_fields(struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002063 char *field_name, bool raw_trace, int level)
Namhyung Kim9735be22016-01-05 19:58:35 +09002064{
2065 int ret = -ESRCH;
2066 struct perf_evsel *evsel;
2067 struct format_field *field;
2068
2069 evlist__for_each(evlist, evsel) {
2070 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2071 continue;
2072
2073 field = pevent_find_any_field(evsel->tp_format, field_name);
2074 if (field == NULL)
2075 continue;
2076
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002077 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002078 if (ret < 0)
2079 break;
2080 }
2081 return ret;
2082}
2083
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002084static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2085 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002086{
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002087 char *str, *event_name, *field_name, *opt_name;
2088 struct perf_evsel *evsel;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002089 struct format_field *field;
Namhyung Kim053a3982015-12-23 02:07:05 +09002090 bool raw_trace = symbol_conf.raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002091 int ret = 0;
2092
2093 if (evlist == NULL)
2094 return -ENOENT;
2095
2096 str = strdup(tok);
2097 if (str == NULL)
2098 return -ENOMEM;
2099
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002100 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002101 ret = -EINVAL;
2102 goto out;
2103 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002104
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002105 if (opt_name) {
2106 if (strcmp(opt_name, "raw")) {
2107 pr_debug("unsupported field option %s\n", opt_name);
Namhyung Kim053a3982015-12-23 02:07:05 +09002108 ret = -EINVAL;
2109 goto out;
2110 }
2111 raw_trace = true;
2112 }
2113
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002114 if (!strcmp(field_name, "trace_fields")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002115 ret = add_all_dynamic_fields(evlist, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002116 goto out;
2117 }
2118
Namhyung Kim9735be22016-01-05 19:58:35 +09002119 if (event_name == NULL) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002120 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002121 goto out;
2122 }
2123
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002124 evsel = find_evsel(evlist, event_name);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002125 if (evsel == NULL) {
2126 pr_debug("Cannot find event: %s\n", event_name);
2127 ret = -ENOENT;
2128 goto out;
2129 }
2130
2131 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2132 pr_debug("%s is not a tracepoint event\n", event_name);
2133 ret = -EINVAL;
2134 goto out;
2135 }
2136
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002137 if (!strcmp(field_name, "*")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002138 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002139 } else {
2140 field = pevent_find_any_field(evsel->tp_format, field_name);
2141 if (field == NULL) {
2142 pr_debug("Cannot find event field for %s.%s\n",
2143 event_name, field_name);
2144 return -ENOENT;
2145 }
2146
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002147 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002148 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002149
2150out:
2151 free(str);
2152 return ret;
2153}
2154
Jiri Olsad7b617f2016-03-09 11:04:17 +01002155static int __sort_dimension__add(struct sort_dimension *sd,
2156 struct perf_hpp_list *list,
2157 int level)
Namhyung Kim2f532d092013-04-03 21:26:10 +09002158{
2159 if (sd->taken)
Namhyung Kim8b536992014-03-03 11:46:55 +09002160 return 0;
2161
Jiri Olsad7b617f2016-03-09 11:04:17 +01002162 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
Namhyung Kim8b536992014-03-03 11:46:55 +09002163 return -1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002164
2165 if (sd->entry->se_collapse)
2166 sort__need_collapse = 1;
2167
Namhyung Kim2f532d092013-04-03 21:26:10 +09002168 sd->taken = 1;
Namhyung Kim8b536992014-03-03 11:46:55 +09002169
2170 return 0;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002171}
2172
Jiri Olsad7b617f2016-03-09 11:04:17 +01002173static int __hpp_dimension__add(struct hpp_dimension *hd,
2174 struct perf_hpp_list *list,
2175 int level)
Namhyung Kima2ce0672014-03-04 09:06:42 +09002176{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002177 struct perf_hpp_fmt *fmt;
Namhyung Kima2ce0672014-03-04 09:06:42 +09002178
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002179 if (hd->taken)
2180 return 0;
2181
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002182 fmt = __hpp_dimension__alloc_hpp(hd, level);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002183 if (!fmt)
2184 return -1;
2185
2186 hd->taken = 1;
Jiri Olsad7b617f2016-03-09 11:04:17 +01002187 perf_hpp_list__register_sort_field(list, fmt);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002188 return 0;
2189}
2190
Jiri Olsa07600022016-01-18 10:24:16 +01002191static int __sort_dimension__add_output(struct perf_hpp_list *list,
2192 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002193{
2194 if (sd->taken)
2195 return 0;
2196
Jiri Olsad7b617f2016-03-09 11:04:17 +01002197 if (__sort_dimension__add_hpp_output(sd, list) < 0)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002198 return -1;
2199
2200 sd->taken = 1;
2201 return 0;
2202}
2203
Jiri Olsa07600022016-01-18 10:24:16 +01002204static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2205 struct hpp_dimension *hd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002206{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002207 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002208
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002209 if (hd->taken)
2210 return 0;
2211
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002212 fmt = __hpp_dimension__alloc_hpp(hd, 0);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002213 if (!fmt)
2214 return -1;
2215
2216 hd->taken = 1;
Jiri Olsa07600022016-01-18 10:24:16 +01002217 perf_hpp_list__column_register(list, fmt);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002218 return 0;
2219}
2220
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002221int hpp_dimension__add_output(unsigned col)
2222{
2223 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa07600022016-01-18 10:24:16 +01002224 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002225}
2226
Jiri Olsad7b617f2016-03-09 11:04:17 +01002227static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -03002228 struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002229 int level)
John Kacurdd68ada2009-09-24 18:02:49 +02002230{
2231 unsigned int i;
2232
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002233 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2234 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +02002235
John Kacurdd68ada2009-09-24 18:02:49 +02002236 if (strncasecmp(tok, sd->name, strlen(tok)))
2237 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002238
John Kacurdd68ada2009-09-24 18:02:49 +02002239 if (sd->entry == &sort_parent) {
2240 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2241 if (ret) {
2242 char err[BUFSIZ];
2243
2244 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -03002245 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2246 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +02002247 }
2248 sort__has_parent = 1;
Namhyung Kim930477b2013-04-05 10:26:36 +09002249 } else if (sd->entry == &sort_sym) {
Namhyung Kim1af556402012-09-14 17:35:27 +09002250 sort__has_sym = 1;
Kan Liang94ba4622015-02-09 05:39:44 +00002251 /*
2252 * perf diff displays the performance difference amongst
2253 * two or more perf.data files. Those files could come
2254 * from different binaries. So we should not compare
2255 * their ips, but the name of symbol.
2256 */
2257 if (sort__mode == SORT_MODE__DIFF)
2258 sd->entry->se_collapse = sort__sym_sort;
2259
Namhyung Kim68f6d022013-12-18 14:21:10 +09002260 } else if (sd->entry == &sort_dso) {
2261 sort__has_dso = 1;
Kan Liang2e7ea3a2015-09-04 10:45:43 -04002262 } else if (sd->entry == &sort_socket) {
2263 sort__has_socket = 1;
Namhyung Kimcfd92da2016-01-21 19:13:24 -03002264 } else if (sd->entry == &sort_thread) {
2265 sort__has_thread = 1;
Namhyung Kim078b8d42016-03-09 23:20:51 +09002266 } else if (sd->entry == &sort_comm) {
2267 sort__has_comm = 1;
John Kacurdd68ada2009-09-24 18:02:49 +02002268 }
2269
Jiri Olsad7b617f2016-03-09 11:04:17 +01002270 return __sort_dimension__add(sd, list, level);
John Kacurdd68ada2009-09-24 18:02:49 +02002271 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002272
Namhyung Kima2ce0672014-03-04 09:06:42 +09002273 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2274 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2275
2276 if (strncasecmp(tok, hd->name, strlen(tok)))
2277 continue;
2278
Jiri Olsad7b617f2016-03-09 11:04:17 +01002279 return __hpp_dimension__add(hd, list, level);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002280 }
2281
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002282 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2283 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2284
2285 if (strncasecmp(tok, sd->name, strlen(tok)))
2286 continue;
2287
Namhyung Kim55369fc2013-04-01 20:35:20 +09002288 if (sort__mode != SORT_MODE__BRANCH)
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002289 return -EINVAL;
2290
2291 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2292 sort__has_sym = 1;
2293
Jiri Olsad7b617f2016-03-09 11:04:17 +01002294 __sort_dimension__add(sd, list, level);
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002295 return 0;
2296 }
2297
Namhyung Kimafab87b2013-04-03 21:26:11 +09002298 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2299 struct sort_dimension *sd = &memory_sort_dimensions[i];
2300
2301 if (strncasecmp(tok, sd->name, strlen(tok)))
2302 continue;
2303
2304 if (sort__mode != SORT_MODE__MEMORY)
2305 return -EINVAL;
2306
2307 if (sd->entry == &sort_mem_daddr_sym)
2308 sort__has_sym = 1;
2309
Jiri Olsad7b617f2016-03-09 11:04:17 +01002310 __sort_dimension__add(sd, list, level);
Namhyung Kimafab87b2013-04-03 21:26:11 +09002311 return 0;
2312 }
2313
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002314 if (!add_dynamic_entry(evlist, tok, level))
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002315 return 0;
2316
John Kacurdd68ada2009-09-24 18:02:49 +02002317 return -ESRCH;
2318}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002319
Jiri Olsad7b617f2016-03-09 11:04:17 +01002320static int setup_sort_list(struct perf_hpp_list *list, char *str,
2321 struct perf_evlist *evlist)
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002322{
2323 char *tmp, *tok;
2324 int ret = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002325 int level = 0;
Namhyung Kima23f37e2016-03-07 16:44:47 -03002326 int next_level = 1;
2327 bool in_group = false;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002328
Namhyung Kima23f37e2016-03-07 16:44:47 -03002329 do {
2330 tok = str;
2331 tmp = strpbrk(str, "{}, ");
2332 if (tmp) {
2333 if (in_group)
2334 next_level = level;
2335 else
2336 next_level = level + 1;
2337
2338 if (*tmp == '{')
2339 in_group = true;
2340 else if (*tmp == '}')
2341 in_group = false;
2342
2343 *tmp = '\0';
2344 str = tmp + 1;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002345 }
Namhyung Kima23f37e2016-03-07 16:44:47 -03002346
2347 if (*tok) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002348 ret = sort_dimension__add(list, tok, evlist, level);
Namhyung Kima23f37e2016-03-07 16:44:47 -03002349 if (ret == -EINVAL) {
2350 error("Invalid --sort key: `%s'", tok);
2351 break;
2352 } else if (ret == -ESRCH) {
2353 error("Unknown --sort key: `%s'", tok);
2354 break;
2355 }
2356 }
2357
2358 level = next_level;
2359 } while (tmp);
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002360
2361 return ret;
2362}
2363
Namhyung Kimd49dade2015-12-23 02:07:10 +09002364static const char *get_default_sort_order(struct perf_evlist *evlist)
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002365{
2366 const char *default_sort_orders[] = {
2367 default_sort_order,
2368 default_branch_sort_order,
2369 default_mem_sort_order,
2370 default_top_sort_order,
2371 default_diff_sort_order,
Namhyung Kimd49dade2015-12-23 02:07:10 +09002372 default_tracepoint_sort_order,
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002373 };
Namhyung Kimd49dade2015-12-23 02:07:10 +09002374 bool use_trace = true;
2375 struct perf_evsel *evsel;
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002376
2377 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2378
Namhyung Kimd49dade2015-12-23 02:07:10 +09002379 if (evlist == NULL)
2380 goto out_no_evlist;
2381
2382 evlist__for_each(evlist, evsel) {
2383 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2384 use_trace = false;
2385 break;
2386 }
2387 }
2388
2389 if (use_trace) {
2390 sort__mode = SORT_MODE__TRACEPOINT;
2391 if (symbol_conf.raw_trace)
2392 return "trace_fields";
2393 }
2394out_no_evlist:
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002395 return default_sort_orders[sort__mode];
2396}
2397
Namhyung Kimd49dade2015-12-23 02:07:10 +09002398static int setup_sort_order(struct perf_evlist *evlist)
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002399{
2400 char *new_sort_order;
2401
2402 /*
2403 * Append '+'-prefixed sort order to the default sort
2404 * order string.
2405 */
2406 if (!sort_order || is_strict_order(sort_order))
2407 return 0;
2408
2409 if (sort_order[1] == '\0') {
2410 error("Invalid --sort key: `+'");
2411 return -EINVAL;
2412 }
2413
2414 /*
2415 * We allocate new sort_order string, but we never free it,
2416 * because it's checked over the rest of the code.
2417 */
2418 if (asprintf(&new_sort_order, "%s,%s",
Namhyung Kimd49dade2015-12-23 02:07:10 +09002419 get_default_sort_order(evlist), sort_order + 1) < 0) {
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002420 error("Not enough memory to set up --sort");
2421 return -ENOMEM;
2422 }
2423
2424 sort_order = new_sort_order;
2425 return 0;
2426}
2427
Jiri Olsab97511c2016-01-07 10:14:08 +01002428/*
2429 * Adds 'pre,' prefix into 'str' is 'pre' is
2430 * not already part of 'str'.
2431 */
2432static char *prefix_if_not_in(const char *pre, char *str)
2433{
2434 char *n;
2435
2436 if (!str || strstr(str, pre))
2437 return str;
2438
2439 if (asprintf(&n, "%s,%s", pre, str) < 0)
2440 return NULL;
2441
2442 free(str);
2443 return n;
2444}
2445
2446static char *setup_overhead(char *keys)
2447{
Namhyung Kime9d848c2016-05-10 11:26:24 -03002448 if (sort__mode == SORT_MODE__DIFF)
2449 return keys;
2450
Jiri Olsab97511c2016-01-07 10:14:08 +01002451 keys = prefix_if_not_in("overhead", keys);
2452
2453 if (symbol_conf.cumulate_callchain)
2454 keys = prefix_if_not_in("overhead_children", keys);
2455
2456 return keys;
2457}
2458
Namhyung Kim40184c42015-12-23 02:07:01 +09002459static int __setup_sorting(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002460{
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002461 char *str;
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002462 const char *sort_keys;
Namhyung Kim55309982013-02-06 14:57:16 +09002463 int ret = 0;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002464
Namhyung Kimd49dade2015-12-23 02:07:10 +09002465 ret = setup_sort_order(evlist);
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002466 if (ret)
2467 return ret;
2468
2469 sort_keys = sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002470 if (sort_keys == NULL) {
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002471 if (is_strict_order(field_order)) {
Namhyung Kima7d945b2014-03-04 10:46:34 +09002472 /*
2473 * If user specified field order but no sort order,
2474 * we'll honor it and not add default sort orders.
2475 */
2476 return 0;
2477 }
2478
Namhyung Kimd49dade2015-12-23 02:07:10 +09002479 sort_keys = get_default_sort_order(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002480 }
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002481
2482 str = strdup(sort_keys);
Namhyung Kim5936f542013-02-06 14:57:17 +09002483 if (str == NULL) {
2484 error("Not enough memory to setup sort keys");
2485 return -ENOMEM;
2486 }
2487
Jiri Olsab97511c2016-01-07 10:14:08 +01002488 /*
2489 * Prepend overhead fields for backward compatibility.
2490 */
2491 if (!is_strict_order(field_order)) {
2492 str = setup_overhead(str);
2493 if (str == NULL) {
2494 error("Not enough memory to setup overhead keys");
2495 return -ENOMEM;
2496 }
2497 }
2498
Jiri Olsad7b617f2016-03-09 11:04:17 +01002499 ret = setup_sort_list(&perf_hpp_list, str, evlist);
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002500
2501 free(str);
Namhyung Kim55309982013-02-06 14:57:16 +09002502 return ret;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002503}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002504
Jiri Olsaf2998422014-05-23 17:15:47 +02002505void perf_hpp__set_elide(int idx, bool elide)
Namhyung Kime67d49a2014-03-18 13:00:59 +09002506{
Jiri Olsaf2998422014-05-23 17:15:47 +02002507 struct perf_hpp_fmt *fmt;
2508 struct hpp_sort_entry *hse;
Namhyung Kime67d49a2014-03-18 13:00:59 +09002509
Jiri Olsacf094042016-01-18 10:24:17 +01002510 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002511 if (!perf_hpp__is_sort_entry(fmt))
2512 continue;
2513
2514 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2515 if (hse->se->se_width_idx == idx) {
2516 fmt->elide = elide;
2517 break;
2518 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002519 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002520}
2521
Jiri Olsaf2998422014-05-23 17:15:47 +02002522static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002523{
2524 if (list && strlist__nr_entries(list) == 1) {
2525 if (fp != NULL)
2526 fprintf(fp, "# %s: %s\n", list_name,
2527 strlist__entry(list, 0)->s);
Jiri Olsaf2998422014-05-23 17:15:47 +02002528 return true;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002529 }
Jiri Olsaf2998422014-05-23 17:15:47 +02002530 return false;
2531}
2532
2533static bool get_elide(int idx, FILE *output)
2534{
2535 switch (idx) {
2536 case HISTC_SYMBOL:
2537 return __get_elide(symbol_conf.sym_list, "symbol", output);
2538 case HISTC_DSO:
2539 return __get_elide(symbol_conf.dso_list, "dso", output);
2540 case HISTC_COMM:
2541 return __get_elide(symbol_conf.comm_list, "comm", output);
2542 default:
2543 break;
2544 }
2545
2546 if (sort__mode != SORT_MODE__BRANCH)
2547 return false;
2548
2549 switch (idx) {
2550 case HISTC_SYMBOL_FROM:
2551 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2552 case HISTC_SYMBOL_TO:
2553 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2554 case HISTC_DSO_FROM:
2555 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2556 case HISTC_DSO_TO:
2557 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2558 default:
2559 break;
2560 }
2561
2562 return false;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002563}
Namhyung Kim08e71542013-04-03 21:26:19 +09002564
2565void sort__setup_elide(FILE *output)
2566{
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002567 struct perf_hpp_fmt *fmt;
2568 struct hpp_sort_entry *hse;
Namhyung Kim7524f632013-11-08 17:53:42 +09002569
Jiri Olsacf094042016-01-18 10:24:17 +01002570 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002571 if (!perf_hpp__is_sort_entry(fmt))
2572 continue;
Namhyung Kim08e71542013-04-03 21:26:19 +09002573
Jiri Olsaf2998422014-05-23 17:15:47 +02002574 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2575 fmt->elide = get_elide(hse->se->se_width_idx, output);
Namhyung Kim08e71542013-04-03 21:26:19 +09002576 }
2577
Namhyung Kim7524f632013-11-08 17:53:42 +09002578 /*
2579 * It makes no sense to elide all of sort entries.
2580 * Just revert them to show up again.
2581 */
Jiri Olsacf094042016-01-18 10:24:17 +01002582 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002583 if (!perf_hpp__is_sort_entry(fmt))
2584 continue;
2585
Jiri Olsaf2998422014-05-23 17:15:47 +02002586 if (!fmt->elide)
Namhyung Kim7524f632013-11-08 17:53:42 +09002587 return;
2588 }
2589
Jiri Olsacf094042016-01-18 10:24:17 +01002590 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002591 if (!perf_hpp__is_sort_entry(fmt))
2592 continue;
2593
Jiri Olsaf2998422014-05-23 17:15:47 +02002594 fmt->elide = false;
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002595 }
Namhyung Kim08e71542013-04-03 21:26:19 +09002596}
Namhyung Kima7d945b2014-03-04 10:46:34 +09002597
Jiri Olsa07600022016-01-18 10:24:16 +01002598static int output_field_add(struct perf_hpp_list *list, char *tok)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002599{
2600 unsigned int i;
2601
2602 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2603 struct sort_dimension *sd = &common_sort_dimensions[i];
2604
2605 if (strncasecmp(tok, sd->name, strlen(tok)))
2606 continue;
2607
Jiri Olsa07600022016-01-18 10:24:16 +01002608 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002609 }
2610
2611 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2612 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2613
2614 if (strncasecmp(tok, hd->name, strlen(tok)))
2615 continue;
2616
Jiri Olsa07600022016-01-18 10:24:16 +01002617 return __hpp_dimension__add_output(list, hd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002618 }
2619
2620 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2621 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2622
2623 if (strncasecmp(tok, sd->name, strlen(tok)))
2624 continue;
2625
Jiri Olsa07600022016-01-18 10:24:16 +01002626 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002627 }
2628
2629 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2630 struct sort_dimension *sd = &memory_sort_dimensions[i];
2631
2632 if (strncasecmp(tok, sd->name, strlen(tok)))
2633 continue;
2634
Jiri Olsa07600022016-01-18 10:24:16 +01002635 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002636 }
2637
2638 return -ESRCH;
2639}
2640
Jiri Olsa07600022016-01-18 10:24:16 +01002641static int setup_output_list(struct perf_hpp_list *list, char *str)
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002642{
2643 char *tmp, *tok;
2644 int ret = 0;
2645
2646 for (tok = strtok_r(str, ", ", &tmp);
2647 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Jiri Olsa07600022016-01-18 10:24:16 +01002648 ret = output_field_add(list, tok);
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002649 if (ret == -EINVAL) {
2650 error("Invalid --fields key: `%s'", tok);
2651 break;
2652 } else if (ret == -ESRCH) {
2653 error("Unknown --fields key: `%s'", tok);
2654 break;
2655 }
2656 }
2657
2658 return ret;
2659}
2660
Namhyung Kima7d945b2014-03-04 10:46:34 +09002661static void reset_dimensions(void)
2662{
2663 unsigned int i;
2664
2665 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2666 common_sort_dimensions[i].taken = 0;
2667
2668 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2669 hpp_sort_dimensions[i].taken = 0;
2670
2671 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2672 bstack_sort_dimensions[i].taken = 0;
2673
2674 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2675 memory_sort_dimensions[i].taken = 0;
2676}
2677
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002678bool is_strict_order(const char *order)
2679{
2680 return order && (*order != '+');
2681}
2682
Namhyung Kima7d945b2014-03-04 10:46:34 +09002683static int __setup_output_field(void)
2684{
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002685 char *str, *strp;
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002686 int ret = -EINVAL;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002687
2688 if (field_order == NULL)
2689 return 0;
2690
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002691 strp = str = strdup(field_order);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002692 if (str == NULL) {
2693 error("Not enough memory to setup output fields");
2694 return -ENOMEM;
2695 }
2696
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002697 if (!is_strict_order(field_order))
2698 strp++;
2699
2700 if (!strlen(strp)) {
2701 error("Invalid --fields key: `+'");
2702 goto out;
2703 }
2704
Jiri Olsa07600022016-01-18 10:24:16 +01002705 ret = setup_output_list(&perf_hpp_list, strp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002706
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002707out:
Namhyung Kima7d945b2014-03-04 10:46:34 +09002708 free(str);
2709 return ret;
2710}
2711
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002712int setup_sorting(struct perf_evlist *evlist)
2713{
2714 int err;
2715
2716 err = __setup_sorting(evlist);
2717 if (err < 0)
2718 return err;
2719
2720 if (parent_pattern != default_parent_pattern) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002721 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002722 if (err < 0)
2723 return err;
2724 }
2725
Namhyung Kima7d945b2014-03-04 10:46:34 +09002726 reset_dimensions();
2727
2728 /*
2729 * perf diff doesn't use default hpp output fields.
2730 */
2731 if (sort__mode != SORT_MODE__DIFF)
2732 perf_hpp__init();
2733
2734 err = __setup_output_field();
2735 if (err < 0)
2736 return err;
2737
2738 /* copy sort keys to output fields */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002739 perf_hpp__setup_output_field(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002740 /* and then copy output fields to sort keys */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002741 perf_hpp__append_sort_keys(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002742
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002743 /* setup hists-specific output fields */
2744 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2745 return -1;
2746
Namhyung Kima7d945b2014-03-04 10:46:34 +09002747 return 0;
2748}
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002749
2750void reset_output_field(void)
2751{
2752 sort__need_collapse = 0;
2753 sort__has_parent = 0;
2754 sort__has_sym = 0;
2755 sort__has_dso = 0;
2756
Namhyung Kimd69b2962014-05-23 10:59:01 +09002757 field_order = NULL;
2758 sort_order = NULL;
2759
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002760 reset_dimensions();
Jiri Olsa43e0a682016-01-18 10:24:21 +01002761 perf_hpp__reset_output_field(&perf_hpp_list);
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002762}