blob: d26c6b9fe3484af3d66f74cdc50daa0e031bc36a [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 Kim55369fc2013-04-01 20:35:20 +090030enum sort_mode sort__mode = SORT_MODE__NORMAL;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020031
Arnaldo Carvalho de Melo37d9bb52016-02-12 11:27:51 -030032/*
33 * Replaces all occurrences of a char used with the:
34 *
35 * -t, --field-separator
36 *
37 * option, that uses a special separator character and don't pad with spaces,
38 * replacing all occurances of this separator in symbol names (and other
39 * output) with a '.' character, that thus it's the only non valid separator.
40*/
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030041static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020042{
43 int n;
44 va_list ap;
45
46 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030047 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020048 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030049 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020050
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030051 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020052 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030053 if (sep == NULL)
54 break;
55 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020056 }
John Kacurdd68ada2009-09-24 18:02:49 +020057 }
58 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110059
60 if (n >= (int)size)
61 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020062 return n;
63}
64
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020065static int64_t cmp_null(const void *l, const void *r)
Frederic Weisbecker872a8782011-06-29 03:14:52 +020066{
67 if (!l && !r)
68 return 0;
69 else if (!l)
70 return -1;
71 else
72 return 1;
73}
74
75/* --sort pid */
76
77static int64_t
78sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
79{
Adrian Hunter38051232013-07-04 16:20:31 +030080 return right->thread->tid - left->thread->tid;
Frederic Weisbecker872a8782011-06-29 03:14:52 +020081}
82
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030083static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030084 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020085{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020086 const char *comm = thread__comm_str(he->thread);
Namhyung Kim5b591662014-07-31 14:47:38 +090087
88 width = max(7U, width) - 6;
89 return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
90 width, width, comm ?: "");
John Kacurdd68ada2009-09-24 18:02:49 +020091}
92
Namhyung Kim54430102016-02-25 00:13:37 +090093static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
94{
95 const struct thread *th = arg;
96
97 if (type != HIST_FILTER__THREAD)
98 return -1;
99
100 return th && he->thread != th;
101}
102
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200103struct sort_entry sort_thread = {
Namhyung Kim8246de82014-07-31 14:47:35 +0900104 .se_header = " Pid:Command",
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200105 .se_cmp = sort__thread_cmp,
106 .se_snprintf = hist_entry__thread_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900107 .se_filter = hist_entry__thread_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200108 .se_width_idx = HISTC_THREAD,
109};
110
111/* --sort comm */
112
113static int64_t
114sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
115{
Frederic Weisbeckerfedd63d2013-09-11 17:18:09 +0200116 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200117 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200118}
119
120static int64_t
121sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
122{
Namhyung Kim4dfced32013-09-13 16:28:57 +0900123 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200124 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200125}
126
Namhyung Kim202e7a62014-03-04 11:01:41 +0900127static int64_t
128sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
129{
130 return strcmp(comm__str(right->comm), comm__str(left->comm));
131}
132
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300133static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300134 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200135{
Namhyung Kim5b591662014-07-31 14:47:38 +0900136 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
John Kacurdd68ada2009-09-24 18:02:49 +0200137}
138
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900139struct sort_entry sort_comm = {
140 .se_header = "Command",
141 .se_cmp = sort__comm_cmp,
142 .se_collapse = sort__comm_collapse,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900143 .se_sort = sort__comm_sort,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900144 .se_snprintf = hist_entry__comm_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900145 .se_filter = hist_entry__thread_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900146 .se_width_idx = HISTC_COMM,
147};
148
149/* --sort dso */
150
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100151static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200152{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100153 struct dso *dso_l = map_l ? map_l->dso : NULL;
154 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300155 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200156
157 if (!dso_l || !dso_r)
Namhyung Kim202e7a62014-03-04 11:01:41 +0900158 return cmp_null(dso_r, dso_l);
John Kacurdd68ada2009-09-24 18:02:49 +0200159
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300160 if (verbose) {
161 dso_name_l = dso_l->long_name;
162 dso_name_r = dso_r->long_name;
163 } else {
164 dso_name_l = dso_l->short_name;
165 dso_name_r = dso_r->short_name;
166 }
167
168 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200169}
170
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100171static int64_t
172sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200173{
Namhyung Kim202e7a62014-03-04 11:01:41 +0900174 return _sort__dso_cmp(right->ms.map, left->ms.map);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100175}
176
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100177static int _hist_entry__dso_snprintf(struct map *map, char *bf,
178 size_t size, unsigned int width)
179{
180 if (map && map->dso) {
181 const char *dso_name = !verbose ? map->dso->short_name :
182 map->dso->long_name;
Namhyung Kim5b591662014-07-31 14:47:38 +0900183 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300184 }
John Kacurdd68ada2009-09-24 18:02:49 +0200185
Namhyung Kim5b591662014-07-31 14:47:38 +0900186 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200187}
188
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300189static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100190 size_t size, unsigned int width)
191{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300192 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100193}
194
Namhyung Kim54430102016-02-25 00:13:37 +0900195static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
196{
197 const struct dso *dso = arg;
198
199 if (type != HIST_FILTER__DSO)
200 return -1;
201
202 return dso && (!he->ms.map || he->ms.map->dso != dso);
203}
204
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900205struct sort_entry sort_dso = {
206 .se_header = "Shared Object",
207 .se_cmp = sort__dso_cmp,
208 .se_snprintf = hist_entry__dso_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900209 .se_filter = hist_entry__dso_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900210 .se_width_idx = HISTC_DSO,
211};
212
213/* --sort symbol */
214
Namhyung Kim2037be52013-12-18 14:21:09 +0900215static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
216{
217 return (int64_t)(right_ip - left_ip);
218}
219
Namhyung Kim51f27d12013-02-06 14:57:15 +0900220static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900221{
222 if (!sym_l || !sym_r)
223 return cmp_null(sym_l, sym_r);
224
225 if (sym_l == sym_r)
226 return 0;
227
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700228 if (sym_l->start != sym_r->start)
229 return (int64_t)(sym_r->start - sym_l->start);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900230
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700231 return (int64_t)(sym_r->end - sym_l->end);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900232}
233
234static int64_t
235sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
236{
Namhyung Kim09600e02013-10-15 11:01:56 +0900237 int64_t ret;
238
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900239 if (!left->ms.sym && !right->ms.sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900240 return _sort__addr_cmp(left->ip, right->ip);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900241
Namhyung Kim09600e02013-10-15 11:01:56 +0900242 /*
243 * comparing symbol address alone is not enough since it's a
244 * relative address within a dso.
245 */
Namhyung Kim68f6d022013-12-18 14:21:10 +0900246 if (!sort__has_dso) {
247 ret = sort__dso_cmp(left, right);
248 if (ret != 0)
249 return ret;
250 }
Namhyung Kim09600e02013-10-15 11:01:56 +0900251
Namhyung Kim51f27d12013-02-06 14:57:15 +0900252 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900253}
254
Namhyung Kim202e7a62014-03-04 11:01:41 +0900255static int64_t
256sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
257{
258 if (!left->ms.sym || !right->ms.sym)
259 return cmp_null(left->ms.sym, right->ms.sym);
260
261 return strcmp(right->ms.sym->name, left->ms.sym->name);
262}
263
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100264static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
265 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900266 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100267{
268 size_t ret = 0;
269
270 if (verbose) {
271 char o = map ? dso__symtab_origin(map->dso) : '!';
272 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Namhyung Kimded19d52013-04-01 20:35:19 +0900273 BITS_PER_LONG / 4 + 2, ip, o);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100274 }
275
276 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100277 if (sym && map) {
278 if (map->type == MAP__VARIABLE) {
279 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
280 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
Namhyung Kim62667742013-01-24 16:10:42 +0100281 ip - map->unmap_ip(map, sym->start));
Stephane Eranian98a3b322013-01-24 16:10:35 +0100282 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300283 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
Stephane Eranian98a3b322013-01-24 16:10:35 +0100284 width - ret,
285 sym->name);
286 }
287 } else {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100288 size_t len = BITS_PER_LONG / 4;
289 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
290 len, ip);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100291 }
292
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300293 return ret;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100294}
295
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300296static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900297 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100298{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300299 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
300 he->level, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100301}
John Kacurdd68ada2009-09-24 18:02:49 +0200302
Namhyung Kim54430102016-02-25 00:13:37 +0900303static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
304{
305 const char *sym = arg;
306
307 if (type != HIST_FILTER__SYMBOL)
308 return -1;
309
310 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
311}
312
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200313struct sort_entry sort_sym = {
314 .se_header = "Symbol",
315 .se_cmp = sort__sym_cmp,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900316 .se_sort = sort__sym_sort,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200317 .se_snprintf = hist_entry__sym_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900318 .se_filter = hist_entry__sym_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200319 .se_width_idx = HISTC_SYMBOL,
320};
John Kacurdd68ada2009-09-24 18:02:49 +0200321
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300322/* --sort srcline */
323
Namhyung Kimcecaec62016-02-22 09:31:51 +0900324static char *hist_entry__get_srcline(struct hist_entry *he)
325{
326 struct map *map = he->ms.map;
327
328 if (!map)
329 return SRCLINE_UNKNOWN;
330
331 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
332 he->ms.sym, true);
333}
334
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300335static int64_t
336sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
337{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900338 if (!left->srcline)
339 left->srcline = hist_entry__get_srcline(left);
340 if (!right->srcline)
341 right->srcline = hist_entry__get_srcline(right);
342
Namhyung Kim202e7a62014-03-04 11:01:41 +0900343 return strcmp(right->srcline, left->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300344}
345
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300346static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim5b591662014-07-31 14:47:38 +0900347 size_t size, unsigned int width)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300348{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900349 if (!he->srcline)
350 he->srcline = hist_entry__get_srcline(he);
351
Namhyung Kim2960ed62016-02-22 09:32:33 +0900352 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300353}
354
355struct sort_entry sort_srcline = {
356 .se_header = "Source:Line",
357 .se_cmp = sort__srcline_cmp,
358 .se_snprintf = hist_entry__srcline_snprintf,
359 .se_width_idx = HISTC_SRCLINE,
360};
361
Andi Kleen31191a82015-08-07 15:54:24 -0700362/* --sort srcfile */
363
364static char no_srcfile[1];
365
Namhyung Kimcecaec62016-02-22 09:31:51 +0900366static char *hist_entry__get_srcfile(struct hist_entry *e)
Andi Kleen31191a82015-08-07 15:54:24 -0700367{
368 char *sf, *p;
369 struct map *map = e->ms.map;
370
Namhyung Kimcecaec62016-02-22 09:31:51 +0900371 if (!map)
372 return no_srcfile;
373
Andi Kleen2f84b422015-09-01 11:47:19 -0700374 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
375 e->ms.sym, false, true);
Andi Kleen76b10652015-08-11 06:36:55 -0700376 if (!strcmp(sf, SRCLINE_UNKNOWN))
377 return no_srcfile;
Andi Kleen31191a82015-08-07 15:54:24 -0700378 p = strchr(sf, ':');
379 if (p && *sf) {
380 *p = 0;
381 return sf;
382 }
383 free(sf);
384 return no_srcfile;
385}
386
387static int64_t
388sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
389{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900390 if (!left->srcfile)
391 left->srcfile = hist_entry__get_srcfile(left);
392 if (!right->srcfile)
393 right->srcfile = hist_entry__get_srcfile(right);
394
Andi Kleen31191a82015-08-07 15:54:24 -0700395 return strcmp(right->srcfile, left->srcfile);
396}
397
398static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
399 size_t size, unsigned int width)
400{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900401 if (!he->srcfile)
402 he->srcfile = hist_entry__get_srcfile(he);
403
Namhyung Kim2960ed62016-02-22 09:32:33 +0900404 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
Andi Kleen31191a82015-08-07 15:54:24 -0700405}
406
407struct sort_entry sort_srcfile = {
408 .se_header = "Source File",
409 .se_cmp = sort__srcfile_cmp,
410 .se_snprintf = hist_entry__srcfile_snprintf,
411 .se_width_idx = HISTC_SRCFILE,
412};
413
John Kacurdd68ada2009-09-24 18:02:49 +0200414/* --sort parent */
415
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200416static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200417sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
418{
419 struct symbol *sym_l = left->parent;
420 struct symbol *sym_r = right->parent;
421
422 if (!sym_l || !sym_r)
423 return cmp_null(sym_l, sym_r);
424
Namhyung Kim202e7a62014-03-04 11:01:41 +0900425 return strcmp(sym_r->name, sym_l->name);
John Kacurdd68ada2009-09-24 18:02:49 +0200426}
427
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300428static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300429 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200430{
Namhyung Kim5b591662014-07-31 14:47:38 +0900431 return repsep_snprintf(bf, size, "%-*.*s", width, width,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300432 he->parent ? he->parent->name : "[other]");
John Kacurdd68ada2009-09-24 18:02:49 +0200433}
434
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200435struct sort_entry sort_parent = {
436 .se_header = "Parent symbol",
437 .se_cmp = sort__parent_cmp,
438 .se_snprintf = hist_entry__parent_snprintf,
439 .se_width_idx = HISTC_PARENT,
440};
441
Arun Sharmaf60f3592010-06-04 11:27:10 -0300442/* --sort cpu */
443
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200444static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300445sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
446{
447 return right->cpu - left->cpu;
448}
449
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300450static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
451 size_t size, unsigned int width)
Arun Sharmaf60f3592010-06-04 11:27:10 -0300452{
Namhyung Kim5b591662014-07-31 14:47:38 +0900453 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300454}
455
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200456struct sort_entry sort_cpu = {
457 .se_header = "CPU",
458 .se_cmp = sort__cpu_cmp,
459 .se_snprintf = hist_entry__cpu_snprintf,
460 .se_width_idx = HISTC_CPU,
461};
462
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400463/* --sort socket */
464
465static int64_t
466sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
467{
468 return right->socket - left->socket;
469}
470
471static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
472 size_t size, unsigned int width)
473{
474 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
475}
476
Namhyung Kim54430102016-02-25 00:13:37 +0900477static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
478{
479 int sk = *(const int *)arg;
480
481 if (type != HIST_FILTER__SOCKET)
482 return -1;
483
484 return sk >= 0 && he->socket != sk;
485}
486
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400487struct sort_entry sort_socket = {
488 .se_header = "Socket",
489 .se_cmp = sort__socket_cmp,
490 .se_snprintf = hist_entry__socket_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900491 .se_filter = hist_entry__socket_filter,
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400492 .se_width_idx = HISTC_SOCKET,
493};
494
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900495/* --sort trace */
496
497static char *get_trace_output(struct hist_entry *he)
498{
499 struct trace_seq seq;
500 struct perf_evsel *evsel;
501 struct pevent_record rec = {
502 .data = he->raw_data,
503 .size = he->raw_size,
504 };
505
506 evsel = hists_to_evsel(he->hists);
507
508 trace_seq_init(&seq);
Namhyung Kim053a3982015-12-23 02:07:05 +0900509 if (symbol_conf.raw_trace) {
510 pevent_print_fields(&seq, he->raw_data, he->raw_size,
511 evsel->tp_format);
512 } else {
513 pevent_event_info(&seq, evsel->tp_format, &rec);
514 }
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900515 return seq.buffer;
516}
517
518static int64_t
519sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
520{
521 struct perf_evsel *evsel;
522
523 evsel = hists_to_evsel(left->hists);
524 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
525 return 0;
526
527 if (left->trace_output == NULL)
528 left->trace_output = get_trace_output(left);
529 if (right->trace_output == NULL)
530 right->trace_output = get_trace_output(right);
531
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900532 return strcmp(right->trace_output, left->trace_output);
533}
534
535static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
536 size_t size, unsigned int width)
537{
538 struct perf_evsel *evsel;
539
540 evsel = hists_to_evsel(he->hists);
541 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
Namhyung Kim2960ed62016-02-22 09:32:33 +0900542 return scnprintf(bf, size, "%-.*s", width, "N/A");
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900543
544 if (he->trace_output == NULL)
545 he->trace_output = get_trace_output(he);
Namhyung Kim2960ed62016-02-22 09:32:33 +0900546 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900547}
548
549struct sort_entry sort_trace = {
550 .se_header = "Trace output",
551 .se_cmp = sort__trace_cmp,
552 .se_snprintf = hist_entry__trace_snprintf,
553 .se_width_idx = HISTC_TRACE,
554};
555
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900556/* sort keys for branch stacks */
557
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100558static int64_t
559sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
560{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200561 if (!left->branch_info || !right->branch_info)
562 return cmp_null(left->branch_info, right->branch_info);
563
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100564 return _sort__dso_cmp(left->branch_info->from.map,
565 right->branch_info->from.map);
566}
567
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300568static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100569 size_t size, unsigned int width)
570{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200571 if (he->branch_info)
572 return _hist_entry__dso_snprintf(he->branch_info->from.map,
573 bf, size, width);
574 else
575 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100576}
577
Namhyung Kim54430102016-02-25 00:13:37 +0900578static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
579 const void *arg)
580{
581 const struct dso *dso = arg;
582
583 if (type != HIST_FILTER__DSO)
584 return -1;
585
586 return dso && (!he->branch_info || !he->branch_info->from.map ||
587 he->branch_info->from.map->dso != dso);
588}
589
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100590static int64_t
591sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
592{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200593 if (!left->branch_info || !right->branch_info)
594 return cmp_null(left->branch_info, right->branch_info);
595
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100596 return _sort__dso_cmp(left->branch_info->to.map,
597 right->branch_info->to.map);
598}
599
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300600static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100601 size_t size, unsigned int width)
602{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200603 if (he->branch_info)
604 return _hist_entry__dso_snprintf(he->branch_info->to.map,
605 bf, size, width);
606 else
607 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100608}
609
Namhyung Kim54430102016-02-25 00:13:37 +0900610static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
611 const void *arg)
612{
613 const struct dso *dso = arg;
614
615 if (type != HIST_FILTER__DSO)
616 return -1;
617
618 return dso && (!he->branch_info || !he->branch_info->to.map ||
619 he->branch_info->to.map->dso != dso);
620}
621
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100622static int64_t
623sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
624{
625 struct addr_map_symbol *from_l = &left->branch_info->from;
626 struct addr_map_symbol *from_r = &right->branch_info->from;
627
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200628 if (!left->branch_info || !right->branch_info)
629 return cmp_null(left->branch_info, right->branch_info);
630
631 from_l = &left->branch_info->from;
632 from_r = &right->branch_info->from;
633
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100634 if (!from_l->sym && !from_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900635 return _sort__addr_cmp(from_l->addr, from_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100636
Namhyung Kim51f27d12013-02-06 14:57:15 +0900637 return _sort__sym_cmp(from_l->sym, from_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100638}
639
640static int64_t
641sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
642{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200643 struct addr_map_symbol *to_l, *to_r;
644
645 if (!left->branch_info || !right->branch_info)
646 return cmp_null(left->branch_info, right->branch_info);
647
648 to_l = &left->branch_info->to;
649 to_r = &right->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100650
651 if (!to_l->sym && !to_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900652 return _sort__addr_cmp(to_l->addr, to_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100653
Namhyung Kim51f27d12013-02-06 14:57:15 +0900654 return _sort__sym_cmp(to_l->sym, to_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100655}
656
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300657static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900658 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100659{
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200660 if (he->branch_info) {
661 struct addr_map_symbol *from = &he->branch_info->from;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100662
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200663 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
664 he->level, bf, size, width);
665 }
666
667 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100668}
669
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300670static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900671 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100672{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200673 if (he->branch_info) {
674 struct addr_map_symbol *to = &he->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100675
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200676 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
677 he->level, bf, size, width);
678 }
679
680 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100681}
682
Namhyung Kim54430102016-02-25 00:13:37 +0900683static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
684 const void *arg)
685{
686 const char *sym = arg;
687
688 if (type != HIST_FILTER__SYMBOL)
689 return -1;
690
691 return sym && !(he->branch_info && he->branch_info->from.sym &&
692 strstr(he->branch_info->from.sym->name, sym));
693}
694
695static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
696 const void *arg)
697{
698 const char *sym = arg;
699
700 if (type != HIST_FILTER__SYMBOL)
701 return -1;
702
703 return sym && !(he->branch_info && he->branch_info->to.sym &&
704 strstr(he->branch_info->to.sym->name, sym));
705}
706
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900707struct sort_entry sort_dso_from = {
708 .se_header = "Source Shared Object",
709 .se_cmp = sort__dso_from_cmp,
710 .se_snprintf = hist_entry__dso_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900711 .se_filter = hist_entry__dso_from_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900712 .se_width_idx = HISTC_DSO_FROM,
713};
714
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100715struct sort_entry sort_dso_to = {
716 .se_header = "Target Shared Object",
717 .se_cmp = sort__dso_to_cmp,
718 .se_snprintf = hist_entry__dso_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900719 .se_filter = hist_entry__dso_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100720 .se_width_idx = HISTC_DSO_TO,
721};
722
723struct sort_entry sort_sym_from = {
724 .se_header = "Source Symbol",
725 .se_cmp = sort__sym_from_cmp,
726 .se_snprintf = hist_entry__sym_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900727 .se_filter = hist_entry__sym_from_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100728 .se_width_idx = HISTC_SYMBOL_FROM,
729};
730
731struct sort_entry sort_sym_to = {
732 .se_header = "Target Symbol",
733 .se_cmp = sort__sym_to_cmp,
734 .se_snprintf = hist_entry__sym_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900735 .se_filter = hist_entry__sym_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100736 .se_width_idx = HISTC_SYMBOL_TO,
737};
738
739static int64_t
740sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
741{
Jiri Olsa428560e2014-10-16 16:07:03 +0200742 unsigned char mp, p;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100743
Jiri Olsa428560e2014-10-16 16:07:03 +0200744 if (!left->branch_info || !right->branch_info)
745 return cmp_null(left->branch_info, right->branch_info);
746
747 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
748 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100749 return mp || p;
750}
751
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300752static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100753 size_t size, unsigned int width){
754 static const char *out = "N/A";
755
Jiri Olsa428560e2014-10-16 16:07:03 +0200756 if (he->branch_info) {
757 if (he->branch_info->flags.predicted)
758 out = "N";
759 else if (he->branch_info->flags.mispred)
760 out = "Y";
761 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100762
Namhyung Kim5b591662014-07-31 14:47:38 +0900763 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100764}
765
Andi Kleen0e332f02015-07-18 08:24:46 -0700766static int64_t
767sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
768{
769 return left->branch_info->flags.cycles -
770 right->branch_info->flags.cycles;
771}
772
773static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
774 size_t size, unsigned int width)
775{
776 if (he->branch_info->flags.cycles == 0)
777 return repsep_snprintf(bf, size, "%-*s", width, "-");
778 return repsep_snprintf(bf, size, "%-*hd", width,
779 he->branch_info->flags.cycles);
780}
781
782struct sort_entry sort_cycles = {
783 .se_header = "Basic Block Cycles",
784 .se_cmp = sort__cycles_cmp,
785 .se_snprintf = hist_entry__cycles_snprintf,
786 .se_width_idx = HISTC_CYCLES,
787};
788
Stephane Eranian98a3b322013-01-24 16:10:35 +0100789/* --sort daddr_sym */
790static int64_t
791sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
792{
793 uint64_t l = 0, r = 0;
794
795 if (left->mem_info)
796 l = left->mem_info->daddr.addr;
797 if (right->mem_info)
798 r = right->mem_info->daddr.addr;
799
800 return (int64_t)(r - l);
801}
802
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300803static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100804 size_t size, unsigned int width)
805{
806 uint64_t addr = 0;
807 struct map *map = NULL;
808 struct symbol *sym = NULL;
809
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300810 if (he->mem_info) {
811 addr = he->mem_info->daddr.addr;
812 map = he->mem_info->daddr.map;
813 sym = he->mem_info->daddr.sym;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100814 }
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300815 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100816 width);
817}
818
819static int64_t
Don Zickus28e6db22015-10-05 20:06:07 +0200820sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
821{
822 uint64_t l = 0, r = 0;
823
824 if (left->mem_info)
825 l = left->mem_info->iaddr.addr;
826 if (right->mem_info)
827 r = right->mem_info->iaddr.addr;
828
829 return (int64_t)(r - l);
830}
831
832static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
833 size_t size, unsigned int width)
834{
835 uint64_t addr = 0;
836 struct map *map = NULL;
837 struct symbol *sym = NULL;
838
839 if (he->mem_info) {
840 addr = he->mem_info->iaddr.addr;
841 map = he->mem_info->iaddr.map;
842 sym = he->mem_info->iaddr.sym;
843 }
844 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
845 width);
846}
847
848static int64_t
Stephane Eranian98a3b322013-01-24 16:10:35 +0100849sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
850{
851 struct map *map_l = NULL;
852 struct map *map_r = NULL;
853
854 if (left->mem_info)
855 map_l = left->mem_info->daddr.map;
856 if (right->mem_info)
857 map_r = right->mem_info->daddr.map;
858
859 return _sort__dso_cmp(map_l, map_r);
860}
861
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300862static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100863 size_t size, unsigned int width)
864{
865 struct map *map = NULL;
866
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300867 if (he->mem_info)
868 map = he->mem_info->daddr.map;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100869
870 return _hist_entry__dso_snprintf(map, bf, size, width);
871}
872
873static int64_t
874sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
875{
876 union perf_mem_data_src data_src_l;
877 union perf_mem_data_src data_src_r;
878
879 if (left->mem_info)
880 data_src_l = left->mem_info->data_src;
881 else
882 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
883
884 if (right->mem_info)
885 data_src_r = right->mem_info->data_src;
886 else
887 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
888
889 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
890}
891
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300892static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100893 size_t size, unsigned int width)
894{
Jiri Olsa69a77272016-02-24 09:46:49 +0100895 char out[10];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100896
Jiri Olsa69a77272016-02-24 09:46:49 +0100897 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300898 return repsep_snprintf(bf, size, "%.*s", width, out);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100899}
900
901static int64_t
902sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
903{
904 union perf_mem_data_src data_src_l;
905 union perf_mem_data_src data_src_r;
906
907 if (left->mem_info)
908 data_src_l = left->mem_info->data_src;
909 else
910 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
911
912 if (right->mem_info)
913 data_src_r = right->mem_info->data_src;
914 else
915 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
916
917 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
918}
919
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300920static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100921 size_t size, unsigned int width)
922{
923 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100924
Jiri Olsa0c877d72016-02-24 09:46:46 +0100925 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100926 return repsep_snprintf(bf, size, "%-*s", width, out);
927}
928
929static int64_t
930sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
931{
932 union perf_mem_data_src data_src_l;
933 union perf_mem_data_src data_src_r;
934
935 if (left->mem_info)
936 data_src_l = left->mem_info->data_src;
937 else
938 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
939
940 if (right->mem_info)
941 data_src_r = right->mem_info->data_src;
942 else
943 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
944
945 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
946}
947
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300948static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100949 size_t size, unsigned int width)
950{
951 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100952
Jiri Olsa071e9a12016-02-24 09:46:47 +0100953 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100954 return repsep_snprintf(bf, size, "%-*s", width, out);
955}
956
957static int64_t
958sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
959{
960 union perf_mem_data_src data_src_l;
961 union perf_mem_data_src data_src_r;
962
963 if (left->mem_info)
964 data_src_l = left->mem_info->data_src;
965 else
966 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
967
968 if (right->mem_info)
969 data_src_r = right->mem_info->data_src;
970 else
971 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
972
973 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
974}
975
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300976static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100977 size_t size, unsigned int width)
978{
979 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100980
Jiri Olsa2c07af12016-02-24 09:46:48 +0100981 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100982 return repsep_snprintf(bf, size, "%-*s", width, out);
983}
984
Don Zickus9b32ba72014-06-01 15:38:29 +0200985static int64_t
986sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
987{
988 u64 l, r;
989 struct map *l_map, *r_map;
990
991 if (!left->mem_info) return -1;
992 if (!right->mem_info) return 1;
993
994 /* group event types together */
995 if (left->cpumode > right->cpumode) return -1;
996 if (left->cpumode < right->cpumode) return 1;
997
998 l_map = left->mem_info->daddr.map;
999 r_map = right->mem_info->daddr.map;
1000
1001 /* if both are NULL, jump to sort on al_addr instead */
1002 if (!l_map && !r_map)
1003 goto addr;
1004
1005 if (!l_map) return -1;
1006 if (!r_map) return 1;
1007
1008 if (l_map->maj > r_map->maj) return -1;
1009 if (l_map->maj < r_map->maj) return 1;
1010
1011 if (l_map->min > r_map->min) return -1;
1012 if (l_map->min < r_map->min) return 1;
1013
1014 if (l_map->ino > r_map->ino) return -1;
1015 if (l_map->ino < r_map->ino) return 1;
1016
1017 if (l_map->ino_generation > r_map->ino_generation) return -1;
1018 if (l_map->ino_generation < r_map->ino_generation) return 1;
1019
1020 /*
1021 * Addresses with no major/minor numbers are assumed to be
1022 * anonymous in userspace. Sort those on pid then address.
1023 *
1024 * The kernel and non-zero major/minor mapped areas are
1025 * assumed to be unity mapped. Sort those on address.
1026 */
1027
1028 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1029 (!(l_map->flags & MAP_SHARED)) &&
1030 !l_map->maj && !l_map->min && !l_map->ino &&
1031 !l_map->ino_generation) {
1032 /* userspace anonymous */
1033
1034 if (left->thread->pid_ > right->thread->pid_) return -1;
1035 if (left->thread->pid_ < right->thread->pid_) return 1;
1036 }
1037
1038addr:
1039 /* al_addr does all the right addr - start + offset calculations */
1040 l = cl_address(left->mem_info->daddr.al_addr);
1041 r = cl_address(right->mem_info->daddr.al_addr);
1042
1043 if (l > r) return -1;
1044 if (l < r) return 1;
1045
1046 return 0;
1047}
1048
1049static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1050 size_t size, unsigned int width)
1051{
1052
1053 uint64_t addr = 0;
1054 struct map *map = NULL;
1055 struct symbol *sym = NULL;
1056 char level = he->level;
1057
1058 if (he->mem_info) {
1059 addr = cl_address(he->mem_info->daddr.al_addr);
1060 map = he->mem_info->daddr.map;
1061 sym = he->mem_info->daddr.sym;
1062
1063 /* print [s] for shared data mmaps */
1064 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1065 map && (map->type == MAP__VARIABLE) &&
1066 (map->flags & MAP_SHARED) &&
1067 (map->maj || map->min || map->ino ||
1068 map->ino_generation))
1069 level = 's';
1070 else if (!map)
1071 level = 'X';
1072 }
1073 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1074 width);
1075}
1076
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001077struct sort_entry sort_mispredict = {
1078 .se_header = "Branch Mispredicted",
1079 .se_cmp = sort__mispredict_cmp,
1080 .se_snprintf = hist_entry__mispredict_snprintf,
1081 .se_width_idx = HISTC_MISPREDICT,
1082};
1083
Andi Kleen05484292013-01-24 16:10:29 +01001084static u64 he_weight(struct hist_entry *he)
1085{
1086 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1087}
1088
1089static int64_t
1090sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1091{
1092 return he_weight(left) - he_weight(right);
1093}
1094
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001095static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001096 size_t size, unsigned int width)
1097{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001098 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
Andi Kleen05484292013-01-24 16:10:29 +01001099}
1100
1101struct sort_entry sort_local_weight = {
1102 .se_header = "Local Weight",
1103 .se_cmp = sort__local_weight_cmp,
1104 .se_snprintf = hist_entry__local_weight_snprintf,
1105 .se_width_idx = HISTC_LOCAL_WEIGHT,
1106};
1107
1108static int64_t
1109sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1110{
1111 return left->stat.weight - right->stat.weight;
1112}
1113
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001114static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001115 size_t size, unsigned int width)
1116{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001117 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
Andi Kleen05484292013-01-24 16:10:29 +01001118}
1119
1120struct sort_entry sort_global_weight = {
1121 .se_header = "Weight",
1122 .se_cmp = sort__global_weight_cmp,
1123 .se_snprintf = hist_entry__global_weight_snprintf,
1124 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1125};
1126
Stephane Eranian98a3b322013-01-24 16:10:35 +01001127struct sort_entry sort_mem_daddr_sym = {
1128 .se_header = "Data Symbol",
1129 .se_cmp = sort__daddr_cmp,
1130 .se_snprintf = hist_entry__daddr_snprintf,
1131 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1132};
1133
Don Zickus28e6db22015-10-05 20:06:07 +02001134struct sort_entry sort_mem_iaddr_sym = {
1135 .se_header = "Code Symbol",
1136 .se_cmp = sort__iaddr_cmp,
1137 .se_snprintf = hist_entry__iaddr_snprintf,
1138 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1139};
1140
Stephane Eranian98a3b322013-01-24 16:10:35 +01001141struct sort_entry sort_mem_daddr_dso = {
1142 .se_header = "Data Object",
1143 .se_cmp = sort__dso_daddr_cmp,
1144 .se_snprintf = hist_entry__dso_daddr_snprintf,
1145 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1146};
1147
1148struct sort_entry sort_mem_locked = {
1149 .se_header = "Locked",
1150 .se_cmp = sort__locked_cmp,
1151 .se_snprintf = hist_entry__locked_snprintf,
1152 .se_width_idx = HISTC_MEM_LOCKED,
1153};
1154
1155struct sort_entry sort_mem_tlb = {
1156 .se_header = "TLB access",
1157 .se_cmp = sort__tlb_cmp,
1158 .se_snprintf = hist_entry__tlb_snprintf,
1159 .se_width_idx = HISTC_MEM_TLB,
1160};
1161
1162struct sort_entry sort_mem_lvl = {
1163 .se_header = "Memory access",
1164 .se_cmp = sort__lvl_cmp,
1165 .se_snprintf = hist_entry__lvl_snprintf,
1166 .se_width_idx = HISTC_MEM_LVL,
1167};
1168
1169struct sort_entry sort_mem_snoop = {
1170 .se_header = "Snoop",
1171 .se_cmp = sort__snoop_cmp,
1172 .se_snprintf = hist_entry__snoop_snprintf,
1173 .se_width_idx = HISTC_MEM_SNOOP,
1174};
1175
Don Zickus9b32ba72014-06-01 15:38:29 +02001176struct sort_entry sort_mem_dcacheline = {
1177 .se_header = "Data Cacheline",
1178 .se_cmp = sort__dcacheline_cmp,
1179 .se_snprintf = hist_entry__dcacheline_snprintf,
1180 .se_width_idx = HISTC_MEM_DCACHELINE,
1181};
1182
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001183static int64_t
1184sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1185{
Jiri Olsa49f47442014-10-16 16:07:01 +02001186 if (!left->branch_info || !right->branch_info)
1187 return cmp_null(left->branch_info, right->branch_info);
1188
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001189 return left->branch_info->flags.abort !=
1190 right->branch_info->flags.abort;
1191}
1192
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001193static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001194 size_t size, unsigned int width)
1195{
Jiri Olsa49f47442014-10-16 16:07:01 +02001196 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001197
Jiri Olsa49f47442014-10-16 16:07:01 +02001198 if (he->branch_info) {
1199 if (he->branch_info->flags.abort)
1200 out = "A";
1201 else
1202 out = ".";
1203 }
1204
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001205 return repsep_snprintf(bf, size, "%-*s", width, out);
1206}
1207
1208struct sort_entry sort_abort = {
1209 .se_header = "Transaction abort",
1210 .se_cmp = sort__abort_cmp,
1211 .se_snprintf = hist_entry__abort_snprintf,
1212 .se_width_idx = HISTC_ABORT,
1213};
1214
1215static int64_t
1216sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1217{
Jiri Olsa0199d242014-10-16 16:07:02 +02001218 if (!left->branch_info || !right->branch_info)
1219 return cmp_null(left->branch_info, right->branch_info);
1220
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001221 return left->branch_info->flags.in_tx !=
1222 right->branch_info->flags.in_tx;
1223}
1224
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001225static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001226 size_t size, unsigned int width)
1227{
Jiri Olsa0199d242014-10-16 16:07:02 +02001228 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001229
Jiri Olsa0199d242014-10-16 16:07:02 +02001230 if (he->branch_info) {
1231 if (he->branch_info->flags.in_tx)
1232 out = "T";
1233 else
1234 out = ".";
1235 }
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001236
1237 return repsep_snprintf(bf, size, "%-*s", width, out);
1238}
1239
1240struct sort_entry sort_in_tx = {
1241 .se_header = "Branch in transaction",
1242 .se_cmp = sort__in_tx_cmp,
1243 .se_snprintf = hist_entry__in_tx_snprintf,
1244 .se_width_idx = HISTC_IN_TX,
1245};
1246
Andi Kleen475eeab2013-09-20 07:40:43 -07001247static int64_t
1248sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1249{
1250 return left->transaction - right->transaction;
1251}
1252
1253static inline char *add_str(char *p, const char *str)
1254{
1255 strcpy(p, str);
1256 return p + strlen(str);
1257}
1258
1259static struct txbit {
1260 unsigned flag;
1261 const char *name;
1262 int skip_for_len;
1263} txbits[] = {
1264 { PERF_TXN_ELISION, "EL ", 0 },
1265 { PERF_TXN_TRANSACTION, "TX ", 1 },
1266 { PERF_TXN_SYNC, "SYNC ", 1 },
1267 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1268 { PERF_TXN_RETRY, "RETRY ", 0 },
1269 { PERF_TXN_CONFLICT, "CON ", 0 },
1270 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1271 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1272 { 0, NULL, 0 }
1273};
1274
1275int hist_entry__transaction_len(void)
1276{
1277 int i;
1278 int len = 0;
1279
1280 for (i = 0; txbits[i].name; i++) {
1281 if (!txbits[i].skip_for_len)
1282 len += strlen(txbits[i].name);
1283 }
1284 len += 4; /* :XX<space> */
1285 return len;
1286}
1287
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001288static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
Andi Kleen475eeab2013-09-20 07:40:43 -07001289 size_t size, unsigned int width)
1290{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001291 u64 t = he->transaction;
Andi Kleen475eeab2013-09-20 07:40:43 -07001292 char buf[128];
1293 char *p = buf;
1294 int i;
1295
1296 buf[0] = 0;
1297 for (i = 0; txbits[i].name; i++)
1298 if (txbits[i].flag & t)
1299 p = add_str(p, txbits[i].name);
1300 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1301 p = add_str(p, "NEITHER ");
1302 if (t & PERF_TXN_ABORT_MASK) {
1303 sprintf(p, ":%" PRIx64,
1304 (t & PERF_TXN_ABORT_MASK) >>
1305 PERF_TXN_ABORT_SHIFT);
1306 p += strlen(p);
1307 }
1308
1309 return repsep_snprintf(bf, size, "%-*s", width, buf);
1310}
1311
1312struct sort_entry sort_transaction = {
1313 .se_header = "Transaction ",
1314 .se_cmp = sort__transaction_cmp,
1315 .se_snprintf = hist_entry__transaction_snprintf,
1316 .se_width_idx = HISTC_TRANSACTION,
1317};
1318
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001319struct sort_dimension {
1320 const char *name;
1321 struct sort_entry *entry;
1322 int taken;
1323};
1324
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001325#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1326
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001327static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001328 DIM(SORT_PID, "pid", sort_thread),
1329 DIM(SORT_COMM, "comm", sort_comm),
1330 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001331 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001332 DIM(SORT_PARENT, "parent", sort_parent),
1333 DIM(SORT_CPU, "cpu", sort_cpu),
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001334 DIM(SORT_SOCKET, "socket", sort_socket),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -03001335 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Andi Kleen31191a82015-08-07 15:54:24 -07001336 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
Andi Kleenf9ea55d2013-07-18 15:58:53 -07001337 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1338 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
Andi Kleen475eeab2013-09-20 07:40:43 -07001339 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
Namhyung Kima34bb6a2015-12-23 02:07:04 +09001340 DIM(SORT_TRACE, "trace", sort_trace),
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001341};
1342
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001343#undef DIM
1344
1345#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1346
1347static struct sort_dimension bstack_sort_dimensions[] = {
1348 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1349 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1350 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1351 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1352 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001353 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1354 DIM(SORT_ABORT, "abort", sort_abort),
Andi Kleen0e332f02015-07-18 08:24:46 -07001355 DIM(SORT_CYCLES, "cycles", sort_cycles),
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001356};
1357
1358#undef DIM
1359
Namhyung Kimafab87b2013-04-03 21:26:11 +09001360#define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1361
1362static struct sort_dimension memory_sort_dimensions[] = {
Namhyung Kimafab87b2013-04-03 21:26:11 +09001363 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
Don Zickus28e6db22015-10-05 20:06:07 +02001364 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001365 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1366 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1367 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1368 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1369 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
Don Zickus9b32ba72014-06-01 15:38:29 +02001370 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001371};
1372
1373#undef DIM
1374
Namhyung Kima2ce0672014-03-04 09:06:42 +09001375struct hpp_dimension {
1376 const char *name;
1377 struct perf_hpp_fmt *fmt;
1378 int taken;
1379};
1380
1381#define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1382
1383static struct hpp_dimension hpp_sort_dimensions[] = {
1384 DIM(PERF_HPP__OVERHEAD, "overhead"),
1385 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1386 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1387 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1388 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
Namhyung Kim594dcbf2013-10-30 16:06:59 +09001389 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
Namhyung Kima2ce0672014-03-04 09:06:42 +09001390 DIM(PERF_HPP__SAMPLES, "sample"),
1391 DIM(PERF_HPP__PERIOD, "period"),
1392};
1393
1394#undef DIM
1395
Namhyung Kim8b536992014-03-03 11:46:55 +09001396struct hpp_sort_entry {
1397 struct perf_hpp_fmt hpp;
1398 struct sort_entry *se;
1399};
1400
Namhyung Kime0d66c72014-07-31 14:47:37 +09001401void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
Namhyung Kim678a5002014-03-20 11:18:54 +09001402{
1403 struct hpp_sort_entry *hse;
1404
1405 if (!perf_hpp__is_sort_entry(fmt))
1406 return;
1407
1408 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001409 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
Namhyung Kim678a5002014-03-20 11:18:54 +09001410}
1411
Namhyung Kim8b536992014-03-03 11:46:55 +09001412static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1413 struct perf_evsel *evsel)
1414{
1415 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001416 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001417
1418 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001419
Namhyung Kim5b591662014-07-31 14:47:38 +09001420 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001421 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001422
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001423 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
Namhyung Kim8b536992014-03-03 11:46:55 +09001424}
1425
1426static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1427 struct perf_hpp *hpp __maybe_unused,
1428 struct perf_evsel *evsel)
1429{
1430 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001431 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001432
1433 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1434
Namhyung Kim5b591662014-07-31 14:47:38 +09001435 if (!len)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001436 len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001437
1438 return len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001439}
1440
1441static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1442 struct hist_entry *he)
1443{
1444 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001445 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001446
1447 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim5b591662014-07-31 14:47:38 +09001448
1449 if (!len)
1450 len = hists__col_len(he->hists, hse->se->se_width_idx);
Namhyung Kim8b536992014-03-03 11:46:55 +09001451
1452 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1453}
1454
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001455static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1456 struct hist_entry *a, struct hist_entry *b)
1457{
1458 struct hpp_sort_entry *hse;
1459
1460 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1461 return hse->se->se_cmp(a, b);
1462}
1463
1464static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1465 struct hist_entry *a, struct hist_entry *b)
1466{
1467 struct hpp_sort_entry *hse;
1468 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1469
1470 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1471 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1472 return collapse_fn(a, b);
1473}
1474
1475static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1476 struct hist_entry *a, struct hist_entry *b)
1477{
1478 struct hpp_sort_entry *hse;
1479 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1480
1481 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1482 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1483 return sort_fn(a, b);
1484}
1485
Jiri Olsa97358082016-01-18 10:24:03 +01001486bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1487{
1488 return format->header == __sort__hpp_header;
1489}
1490
Namhyung Kima9c6e462016-02-25 00:13:33 +09001491bool perf_hpp__is_trace_entry(struct perf_hpp_fmt *fmt)
1492{
1493 struct hpp_sort_entry *hse;
1494
1495 if (!perf_hpp__is_sort_entry(fmt))
1496 return false;
1497
1498 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1499 return hse->se == &sort_trace;
1500}
1501
1502bool perf_hpp__is_srcline_entry(struct perf_hpp_fmt *fmt)
1503{
1504 struct hpp_sort_entry *hse;
1505
1506 if (!perf_hpp__is_sort_entry(fmt))
1507 return false;
1508
1509 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1510 return hse->se == &sort_srcline;
1511}
1512
1513bool perf_hpp__is_srcfile_entry(struct perf_hpp_fmt *fmt)
1514{
1515 struct hpp_sort_entry *hse;
1516
1517 if (!perf_hpp__is_sort_entry(fmt))
1518 return false;
1519
1520 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1521 return hse->se == &sort_srcfile;
1522}
1523
Jiri Olsa97358082016-01-18 10:24:03 +01001524static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1525{
1526 struct hpp_sort_entry *hse_a;
1527 struct hpp_sort_entry *hse_b;
1528
1529 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1530 return false;
1531
1532 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1533 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1534
1535 return hse_a->se == hse_b->se;
1536}
1537
Jiri Olsa564132f2016-01-18 10:24:09 +01001538static void hse_free(struct perf_hpp_fmt *fmt)
1539{
1540 struct hpp_sort_entry *hse;
1541
1542 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1543 free(hse);
1544}
1545
Namhyung Kima7d945b2014-03-04 10:46:34 +09001546static struct hpp_sort_entry *
1547__sort_dimension__alloc_hpp(struct sort_dimension *sd)
Namhyung Kim8b536992014-03-03 11:46:55 +09001548{
1549 struct hpp_sort_entry *hse;
1550
1551 hse = malloc(sizeof(*hse));
1552 if (hse == NULL) {
1553 pr_err("Memory allocation failed\n");
Namhyung Kima7d945b2014-03-04 10:46:34 +09001554 return NULL;
Namhyung Kim8b536992014-03-03 11:46:55 +09001555 }
1556
1557 hse->se = sd->entry;
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001558 hse->hpp.name = sd->entry->se_header;
Namhyung Kim8b536992014-03-03 11:46:55 +09001559 hse->hpp.header = __sort__hpp_header;
1560 hse->hpp.width = __sort__hpp_width;
1561 hse->hpp.entry = __sort__hpp_entry;
1562 hse->hpp.color = NULL;
1563
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001564 hse->hpp.cmp = __sort__hpp_cmp;
1565 hse->hpp.collapse = __sort__hpp_collapse;
1566 hse->hpp.sort = __sort__hpp_sort;
Jiri Olsa97358082016-01-18 10:24:03 +01001567 hse->hpp.equal = __sort__hpp_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001568 hse->hpp.free = hse_free;
Namhyung Kim8b536992014-03-03 11:46:55 +09001569
1570 INIT_LIST_HEAD(&hse->hpp.list);
1571 INIT_LIST_HEAD(&hse->hpp.sort_list);
Jiri Olsaf2998422014-05-23 17:15:47 +02001572 hse->hpp.elide = false;
Namhyung Kime0d66c72014-07-31 14:47:37 +09001573 hse->hpp.len = 0;
Namhyung Kim5b591662014-07-31 14:47:38 +09001574 hse->hpp.user_len = 0;
Namhyung Kim8b536992014-03-03 11:46:55 +09001575
Namhyung Kima7d945b2014-03-04 10:46:34 +09001576 return hse;
1577}
1578
Jiri Olsa564132f2016-01-18 10:24:09 +01001579static void hpp_free(struct perf_hpp_fmt *fmt)
1580{
1581 free(fmt);
1582}
1583
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001584static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd)
1585{
1586 struct perf_hpp_fmt *fmt;
1587
1588 fmt = memdup(hd->fmt, sizeof(*fmt));
1589 if (fmt) {
1590 INIT_LIST_HEAD(&fmt->list);
1591 INIT_LIST_HEAD(&fmt->sort_list);
Jiri Olsa564132f2016-01-18 10:24:09 +01001592 fmt->free = hpp_free;
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001593 }
1594
1595 return fmt;
1596}
1597
Namhyung Kim54430102016-02-25 00:13:37 +09001598int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1599{
1600 struct perf_hpp_fmt *fmt;
1601 struct hpp_sort_entry *hse;
1602
1603 fmt = he->fmt;
1604 if (fmt == NULL || !perf_hpp__is_sort_entry(fmt))
1605 return -1;
1606
1607 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1608 if (hse->se->se_filter == NULL)
1609 return -1;
1610
1611 return hse->se->se_filter(he, type, arg);
1612}
1613
Namhyung Kima7d945b2014-03-04 10:46:34 +09001614static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd)
1615{
1616 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
1617
1618 if (hse == NULL)
1619 return -1;
1620
Namhyung Kim8b536992014-03-03 11:46:55 +09001621 perf_hpp__register_sort_field(&hse->hpp);
1622 return 0;
1623}
1624
Jiri Olsa07600022016-01-18 10:24:16 +01001625static int __sort_dimension__add_hpp_output(struct perf_hpp_list *list,
1626 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001627{
1628 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
1629
1630 if (hse == NULL)
1631 return -1;
1632
Jiri Olsa07600022016-01-18 10:24:16 +01001633 perf_hpp_list__column_register(list, &hse->hpp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001634 return 0;
1635}
1636
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001637struct hpp_dynamic_entry {
1638 struct perf_hpp_fmt hpp;
1639 struct perf_evsel *evsel;
1640 struct format_field *field;
1641 unsigned dynamic_len;
Namhyung Kim053a3982015-12-23 02:07:05 +09001642 bool raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001643};
1644
1645static int hde_width(struct hpp_dynamic_entry *hde)
1646{
1647 if (!hde->hpp.len) {
1648 int len = hde->dynamic_len;
1649 int namelen = strlen(hde->field->name);
1650 int fieldlen = hde->field->size;
1651
1652 if (namelen > len)
1653 len = namelen;
1654
1655 if (!(hde->field->flags & FIELD_IS_STRING)) {
1656 /* length for print hex numbers */
1657 fieldlen = hde->field->size * 2 + 2;
1658 }
1659 if (fieldlen > len)
1660 len = fieldlen;
1661
1662 hde->hpp.len = len;
1663 }
1664 return hde->hpp.len;
1665}
1666
Namhyung Kim60517d22015-12-23 02:07:03 +09001667static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1668 struct hist_entry *he)
1669{
1670 char *str, *pos;
1671 struct format_field *field = hde->field;
1672 size_t namelen;
1673 bool last = false;
1674
Namhyung Kim053a3982015-12-23 02:07:05 +09001675 if (hde->raw_trace)
1676 return;
1677
Namhyung Kim60517d22015-12-23 02:07:03 +09001678 /* parse pretty print result and update max length */
1679 if (!he->trace_output)
1680 he->trace_output = get_trace_output(he);
1681
1682 namelen = strlen(field->name);
1683 str = he->trace_output;
1684
1685 while (str) {
1686 pos = strchr(str, ' ');
1687 if (pos == NULL) {
1688 last = true;
1689 pos = str + strlen(str);
1690 }
1691
1692 if (!strncmp(str, field->name, namelen)) {
1693 size_t len;
1694
1695 str += namelen + 1;
1696 len = pos - str;
1697
1698 if (len > hde->dynamic_len)
1699 hde->dynamic_len = len;
1700 break;
1701 }
1702
1703 if (last)
1704 str = NULL;
1705 else
1706 str = pos + 1;
1707 }
1708}
1709
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001710static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1711 struct perf_evsel *evsel __maybe_unused)
1712{
1713 struct hpp_dynamic_entry *hde;
1714 size_t len = fmt->user_len;
1715
1716 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1717
1718 if (!len)
1719 len = hde_width(hde);
1720
1721 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1722}
1723
1724static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1725 struct perf_hpp *hpp __maybe_unused,
1726 struct perf_evsel *evsel __maybe_unused)
1727{
1728 struct hpp_dynamic_entry *hde;
1729 size_t len = fmt->user_len;
1730
1731 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1732
1733 if (!len)
1734 len = hde_width(hde);
1735
1736 return len;
1737}
1738
Namhyung Kim361459f2015-12-23 02:07:08 +09001739bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1740{
1741 struct hpp_dynamic_entry *hde;
1742
1743 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1744
1745 return hists_to_evsel(hists) == hde->evsel;
1746}
1747
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001748static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1749 struct hist_entry *he)
1750{
1751 struct hpp_dynamic_entry *hde;
1752 size_t len = fmt->user_len;
Namhyung Kim60517d22015-12-23 02:07:03 +09001753 char *str, *pos;
1754 struct format_field *field;
1755 size_t namelen;
1756 bool last = false;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001757 int ret;
1758
1759 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1760
1761 if (!len)
1762 len = hde_width(hde);
1763
Namhyung Kim053a3982015-12-23 02:07:05 +09001764 if (hde->raw_trace)
1765 goto raw_field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001766
Namhyung Kime049d4a2016-02-27 03:52:46 +09001767 if (!he->trace_output)
1768 he->trace_output = get_trace_output(he);
1769
Namhyung Kim053a3982015-12-23 02:07:05 +09001770 field = hde->field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001771 namelen = strlen(field->name);
1772 str = he->trace_output;
1773
1774 while (str) {
1775 pos = strchr(str, ' ');
1776 if (pos == NULL) {
1777 last = true;
1778 pos = str + strlen(str);
1779 }
1780
1781 if (!strncmp(str, field->name, namelen)) {
1782 str += namelen + 1;
1783 str = strndup(str, pos - str);
1784
1785 if (str == NULL)
1786 return scnprintf(hpp->buf, hpp->size,
1787 "%*.*s", len, len, "ERROR");
1788 break;
1789 }
1790
1791 if (last)
1792 str = NULL;
1793 else
1794 str = pos + 1;
1795 }
1796
1797 if (str == NULL) {
1798 struct trace_seq seq;
Namhyung Kim053a3982015-12-23 02:07:05 +09001799raw_field:
Namhyung Kim60517d22015-12-23 02:07:03 +09001800 trace_seq_init(&seq);
1801 pevent_print_field(&seq, he->raw_data, hde->field);
1802 str = seq.buffer;
1803 }
1804
1805 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1806 free(str);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001807 return ret;
1808}
1809
1810static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1811 struct hist_entry *a, struct hist_entry *b)
1812{
1813 struct hpp_dynamic_entry *hde;
1814 struct format_field *field;
1815 unsigned offset, size;
1816
1817 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1818
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001819 field = hde->field;
1820 if (field->flags & FIELD_IS_DYNAMIC) {
1821 unsigned long long dyn;
1822
1823 pevent_read_number_field(field, a->raw_data, &dyn);
1824 offset = dyn & 0xffff;
1825 size = (dyn >> 16) & 0xffff;
1826
1827 /* record max width for output */
1828 if (size > hde->dynamic_len)
1829 hde->dynamic_len = size;
1830 } else {
1831 offset = field->offset;
1832 size = field->size;
Namhyung Kim60517d22015-12-23 02:07:03 +09001833
1834 update_dynamic_len(hde, a);
1835 update_dynamic_len(hde, b);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001836 }
1837
1838 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1839}
1840
Namhyung Kim361459f2015-12-23 02:07:08 +09001841bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1842{
1843 return fmt->cmp == __sort__hde_cmp;
1844}
1845
Namhyung Kim665aa752016-02-21 23:22:35 +09001846static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1847{
1848 struct hpp_dynamic_entry *hde_a;
1849 struct hpp_dynamic_entry *hde_b;
1850
1851 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1852 return false;
1853
1854 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1855 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1856
1857 return hde_a->field == hde_b->field;
1858}
1859
Jiri Olsa564132f2016-01-18 10:24:09 +01001860static void hde_free(struct perf_hpp_fmt *fmt)
1861{
1862 struct hpp_dynamic_entry *hde;
1863
1864 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1865 free(hde);
1866}
1867
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001868static struct hpp_dynamic_entry *
1869__alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field)
1870{
1871 struct hpp_dynamic_entry *hde;
1872
1873 hde = malloc(sizeof(*hde));
1874 if (hde == NULL) {
1875 pr_debug("Memory allocation failed\n");
1876 return NULL;
1877 }
1878
1879 hde->evsel = evsel;
1880 hde->field = field;
1881 hde->dynamic_len = 0;
1882
1883 hde->hpp.name = field->name;
1884 hde->hpp.header = __sort__hde_header;
1885 hde->hpp.width = __sort__hde_width;
1886 hde->hpp.entry = __sort__hde_entry;
1887 hde->hpp.color = NULL;
1888
1889 hde->hpp.cmp = __sort__hde_cmp;
1890 hde->hpp.collapse = __sort__hde_cmp;
1891 hde->hpp.sort = __sort__hde_cmp;
Namhyung Kim665aa752016-02-21 23:22:35 +09001892 hde->hpp.equal = __sort__hde_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001893 hde->hpp.free = hde_free;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001894
1895 INIT_LIST_HEAD(&hde->hpp.list);
1896 INIT_LIST_HEAD(&hde->hpp.sort_list);
1897 hde->hpp.elide = false;
1898 hde->hpp.len = 0;
1899 hde->hpp.user_len = 0;
1900
1901 return hde;
1902}
1903
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001904static int parse_field_name(char *str, char **event, char **field, char **opt)
1905{
1906 char *event_name, *field_name, *opt_name;
1907
1908 event_name = str;
1909 field_name = strchr(str, '.');
1910
1911 if (field_name) {
1912 *field_name++ = '\0';
1913 } else {
1914 event_name = NULL;
1915 field_name = str;
1916 }
1917
1918 opt_name = strchr(field_name, '/');
1919 if (opt_name)
1920 *opt_name++ = '\0';
1921
1922 *event = event_name;
1923 *field = field_name;
1924 *opt = opt_name;
1925
1926 return 0;
1927}
1928
1929/* find match evsel using a given event name. The event name can be:
Namhyung Kim9735be22016-01-05 19:58:35 +09001930 * 1. '%' + event index (e.g. '%1' for first event)
1931 * 2. full event name (e.g. sched:sched_switch)
1932 * 3. partial event name (should not contain ':')
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001933 */
1934static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
1935{
1936 struct perf_evsel *evsel = NULL;
1937 struct perf_evsel *pos;
1938 bool full_name;
1939
1940 /* case 1 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001941 if (event_name[0] == '%') {
1942 int nr = strtol(event_name+1, NULL, 0);
1943
1944 if (nr > evlist->nr_entries)
1945 return NULL;
1946
1947 evsel = perf_evlist__first(evlist);
1948 while (--nr > 0)
1949 evsel = perf_evsel__next(evsel);
1950
1951 return evsel;
1952 }
1953
1954 full_name = !!strchr(event_name, ':');
1955 evlist__for_each(evlist, pos) {
Namhyung Kim9735be22016-01-05 19:58:35 +09001956 /* case 2 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001957 if (full_name && !strcmp(pos->name, event_name))
1958 return pos;
Namhyung Kim9735be22016-01-05 19:58:35 +09001959 /* case 3 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09001960 if (!full_name && strstr(pos->name, event_name)) {
1961 if (evsel) {
1962 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
1963 event_name, evsel->name, pos->name);
1964 return NULL;
1965 }
1966 evsel = pos;
1967 }
1968 }
1969
1970 return evsel;
1971}
1972
Namhyung Kim3b099bf52015-12-23 02:07:07 +09001973static int __dynamic_dimension__add(struct perf_evsel *evsel,
1974 struct format_field *field,
1975 bool raw_trace)
1976{
1977 struct hpp_dynamic_entry *hde;
1978
1979 hde = __alloc_dynamic_entry(evsel, field);
1980 if (hde == NULL)
1981 return -ENOMEM;
1982
1983 hde->raw_trace = raw_trace;
1984
1985 perf_hpp__register_sort_field(&hde->hpp);
1986 return 0;
1987}
1988
Namhyung Kim2e422fd2015-12-23 02:07:09 +09001989static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace)
1990{
1991 int ret;
1992 struct format_field *field;
1993
1994 field = evsel->tp_format->format.fields;
1995 while (field) {
1996 ret = __dynamic_dimension__add(evsel, field, raw_trace);
1997 if (ret < 0)
1998 return ret;
1999
2000 field = field->next;
2001 }
2002 return 0;
2003}
2004
2005static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace)
2006{
2007 int ret;
2008 struct perf_evsel *evsel;
2009
2010 evlist__for_each(evlist, evsel) {
2011 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2012 continue;
2013
2014 ret = add_evsel_fields(evsel, raw_trace);
2015 if (ret < 0)
2016 return ret;
2017 }
2018 return 0;
2019}
2020
Namhyung Kim9735be22016-01-05 19:58:35 +09002021static int add_all_matching_fields(struct perf_evlist *evlist,
2022 char *field_name, bool raw_trace)
2023{
2024 int ret = -ESRCH;
2025 struct perf_evsel *evsel;
2026 struct format_field *field;
2027
2028 evlist__for_each(evlist, evsel) {
2029 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2030 continue;
2031
2032 field = pevent_find_any_field(evsel->tp_format, field_name);
2033 if (field == NULL)
2034 continue;
2035
2036 ret = __dynamic_dimension__add(evsel, field, raw_trace);
2037 if (ret < 0)
2038 break;
2039 }
2040 return ret;
2041}
2042
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002043static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok)
2044{
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002045 char *str, *event_name, *field_name, *opt_name;
2046 struct perf_evsel *evsel;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002047 struct format_field *field;
Namhyung Kim053a3982015-12-23 02:07:05 +09002048 bool raw_trace = symbol_conf.raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002049 int ret = 0;
2050
2051 if (evlist == NULL)
2052 return -ENOENT;
2053
2054 str = strdup(tok);
2055 if (str == NULL)
2056 return -ENOMEM;
2057
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002058 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002059 ret = -EINVAL;
2060 goto out;
2061 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002062
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002063 if (opt_name) {
2064 if (strcmp(opt_name, "raw")) {
2065 pr_debug("unsupported field option %s\n", opt_name);
Namhyung Kim053a3982015-12-23 02:07:05 +09002066 ret = -EINVAL;
2067 goto out;
2068 }
2069 raw_trace = true;
2070 }
2071
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002072 if (!strcmp(field_name, "trace_fields")) {
2073 ret = add_all_dynamic_fields(evlist, raw_trace);
2074 goto out;
2075 }
2076
Namhyung Kim9735be22016-01-05 19:58:35 +09002077 if (event_name == NULL) {
2078 ret = add_all_matching_fields(evlist, field_name, raw_trace);
2079 goto out;
2080 }
2081
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002082 evsel = find_evsel(evlist, event_name);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002083 if (evsel == NULL) {
2084 pr_debug("Cannot find event: %s\n", event_name);
2085 ret = -ENOENT;
2086 goto out;
2087 }
2088
2089 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2090 pr_debug("%s is not a tracepoint event\n", event_name);
2091 ret = -EINVAL;
2092 goto out;
2093 }
2094
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002095 if (!strcmp(field_name, "*")) {
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002096 ret = add_evsel_fields(evsel, raw_trace);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002097 } else {
2098 field = pevent_find_any_field(evsel->tp_format, field_name);
2099 if (field == NULL) {
2100 pr_debug("Cannot find event field for %s.%s\n",
2101 event_name, field_name);
2102 return -ENOENT;
2103 }
2104
2105 ret = __dynamic_dimension__add(evsel, field, raw_trace);
2106 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002107
2108out:
2109 free(str);
2110 return ret;
2111}
2112
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002113static int __sort_dimension__add(struct sort_dimension *sd)
Namhyung Kim2f532d092013-04-03 21:26:10 +09002114{
2115 if (sd->taken)
Namhyung Kim8b536992014-03-03 11:46:55 +09002116 return 0;
2117
Namhyung Kima7d945b2014-03-04 10:46:34 +09002118 if (__sort_dimension__add_hpp_sort(sd) < 0)
Namhyung Kim8b536992014-03-03 11:46:55 +09002119 return -1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002120
2121 if (sd->entry->se_collapse)
2122 sort__need_collapse = 1;
2123
Namhyung Kim2f532d092013-04-03 21:26:10 +09002124 sd->taken = 1;
Namhyung Kim8b536992014-03-03 11:46:55 +09002125
2126 return 0;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002127}
2128
Namhyung Kima2ce0672014-03-04 09:06:42 +09002129static int __hpp_dimension__add(struct hpp_dimension *hd)
2130{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002131 struct perf_hpp_fmt *fmt;
Namhyung Kima2ce0672014-03-04 09:06:42 +09002132
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002133 if (hd->taken)
2134 return 0;
2135
2136 fmt = __hpp_dimension__alloc_hpp(hd);
2137 if (!fmt)
2138 return -1;
2139
2140 hd->taken = 1;
2141 perf_hpp__register_sort_field(fmt);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002142 return 0;
2143}
2144
Jiri Olsa07600022016-01-18 10:24:16 +01002145static int __sort_dimension__add_output(struct perf_hpp_list *list,
2146 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002147{
2148 if (sd->taken)
2149 return 0;
2150
Jiri Olsa07600022016-01-18 10:24:16 +01002151 if (__sort_dimension__add_hpp_output(list, sd) < 0)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002152 return -1;
2153
2154 sd->taken = 1;
2155 return 0;
2156}
2157
Jiri Olsa07600022016-01-18 10:24:16 +01002158static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2159 struct hpp_dimension *hd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002160{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002161 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002162
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002163 if (hd->taken)
2164 return 0;
2165
2166 fmt = __hpp_dimension__alloc_hpp(hd);
2167 if (!fmt)
2168 return -1;
2169
2170 hd->taken = 1;
Jiri Olsa07600022016-01-18 10:24:16 +01002171 perf_hpp_list__column_register(list, fmt);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002172 return 0;
2173}
2174
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002175int hpp_dimension__add_output(unsigned col)
2176{
2177 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa07600022016-01-18 10:24:16 +01002178 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002179}
2180
Namhyung Kim40184c42015-12-23 02:07:01 +09002181static int sort_dimension__add(const char *tok,
2182 struct perf_evlist *evlist __maybe_unused)
John Kacurdd68ada2009-09-24 18:02:49 +02002183{
2184 unsigned int i;
2185
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002186 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2187 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +02002188
John Kacurdd68ada2009-09-24 18:02:49 +02002189 if (strncasecmp(tok, sd->name, strlen(tok)))
2190 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002191
John Kacurdd68ada2009-09-24 18:02:49 +02002192 if (sd->entry == &sort_parent) {
2193 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2194 if (ret) {
2195 char err[BUFSIZ];
2196
2197 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -03002198 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2199 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +02002200 }
2201 sort__has_parent = 1;
Namhyung Kim930477b2013-04-05 10:26:36 +09002202 } else if (sd->entry == &sort_sym) {
Namhyung Kim1af556402012-09-14 17:35:27 +09002203 sort__has_sym = 1;
Kan Liang94ba4622015-02-09 05:39:44 +00002204 /*
2205 * perf diff displays the performance difference amongst
2206 * two or more perf.data files. Those files could come
2207 * from different binaries. So we should not compare
2208 * their ips, but the name of symbol.
2209 */
2210 if (sort__mode == SORT_MODE__DIFF)
2211 sd->entry->se_collapse = sort__sym_sort;
2212
Namhyung Kim68f6d022013-12-18 14:21:10 +09002213 } else if (sd->entry == &sort_dso) {
2214 sort__has_dso = 1;
Kan Liang2e7ea3a2015-09-04 10:45:43 -04002215 } else if (sd->entry == &sort_socket) {
2216 sort__has_socket = 1;
Namhyung Kimcfd92da2016-01-21 19:13:24 -03002217 } else if (sd->entry == &sort_thread) {
2218 sort__has_thread = 1;
John Kacurdd68ada2009-09-24 18:02:49 +02002219 }
2220
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002221 return __sort_dimension__add(sd);
John Kacurdd68ada2009-09-24 18:02:49 +02002222 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002223
Namhyung Kima2ce0672014-03-04 09:06:42 +09002224 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2225 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2226
2227 if (strncasecmp(tok, hd->name, strlen(tok)))
2228 continue;
2229
2230 return __hpp_dimension__add(hd);
2231 }
2232
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002233 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2234 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2235
2236 if (strncasecmp(tok, sd->name, strlen(tok)))
2237 continue;
2238
Namhyung Kim55369fc2013-04-01 20:35:20 +09002239 if (sort__mode != SORT_MODE__BRANCH)
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002240 return -EINVAL;
2241
2242 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2243 sort__has_sym = 1;
2244
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002245 __sort_dimension__add(sd);
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002246 return 0;
2247 }
2248
Namhyung Kimafab87b2013-04-03 21:26:11 +09002249 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2250 struct sort_dimension *sd = &memory_sort_dimensions[i];
2251
2252 if (strncasecmp(tok, sd->name, strlen(tok)))
2253 continue;
2254
2255 if (sort__mode != SORT_MODE__MEMORY)
2256 return -EINVAL;
2257
2258 if (sd->entry == &sort_mem_daddr_sym)
2259 sort__has_sym = 1;
2260
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002261 __sort_dimension__add(sd);
Namhyung Kimafab87b2013-04-03 21:26:11 +09002262 return 0;
2263 }
2264
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002265 if (!add_dynamic_entry(evlist, tok))
2266 return 0;
2267
John Kacurdd68ada2009-09-24 18:02:49 +02002268 return -ESRCH;
2269}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002270
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002271static int setup_sort_list(char *str, struct perf_evlist *evlist)
2272{
2273 char *tmp, *tok;
2274 int ret = 0;
2275
2276 for (tok = strtok_r(str, ", ", &tmp);
2277 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2278 ret = sort_dimension__add(tok, evlist);
2279 if (ret == -EINVAL) {
2280 error("Invalid --sort key: `%s'", tok);
2281 break;
2282 } else if (ret == -ESRCH) {
2283 error("Unknown --sort key: `%s'", tok);
2284 break;
2285 }
2286 }
2287
2288 return ret;
2289}
2290
Namhyung Kimd49dade2015-12-23 02:07:10 +09002291static const char *get_default_sort_order(struct perf_evlist *evlist)
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002292{
2293 const char *default_sort_orders[] = {
2294 default_sort_order,
2295 default_branch_sort_order,
2296 default_mem_sort_order,
2297 default_top_sort_order,
2298 default_diff_sort_order,
Namhyung Kimd49dade2015-12-23 02:07:10 +09002299 default_tracepoint_sort_order,
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002300 };
Namhyung Kimd49dade2015-12-23 02:07:10 +09002301 bool use_trace = true;
2302 struct perf_evsel *evsel;
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002303
2304 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2305
Namhyung Kimd49dade2015-12-23 02:07:10 +09002306 if (evlist == NULL)
2307 goto out_no_evlist;
2308
2309 evlist__for_each(evlist, evsel) {
2310 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2311 use_trace = false;
2312 break;
2313 }
2314 }
2315
2316 if (use_trace) {
2317 sort__mode = SORT_MODE__TRACEPOINT;
2318 if (symbol_conf.raw_trace)
2319 return "trace_fields";
2320 }
2321out_no_evlist:
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002322 return default_sort_orders[sort__mode];
2323}
2324
Namhyung Kimd49dade2015-12-23 02:07:10 +09002325static int setup_sort_order(struct perf_evlist *evlist)
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002326{
2327 char *new_sort_order;
2328
2329 /*
2330 * Append '+'-prefixed sort order to the default sort
2331 * order string.
2332 */
2333 if (!sort_order || is_strict_order(sort_order))
2334 return 0;
2335
2336 if (sort_order[1] == '\0') {
2337 error("Invalid --sort key: `+'");
2338 return -EINVAL;
2339 }
2340
2341 /*
2342 * We allocate new sort_order string, but we never free it,
2343 * because it's checked over the rest of the code.
2344 */
2345 if (asprintf(&new_sort_order, "%s,%s",
Namhyung Kimd49dade2015-12-23 02:07:10 +09002346 get_default_sort_order(evlist), sort_order + 1) < 0) {
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002347 error("Not enough memory to set up --sort");
2348 return -ENOMEM;
2349 }
2350
2351 sort_order = new_sort_order;
2352 return 0;
2353}
2354
Jiri Olsab97511c2016-01-07 10:14:08 +01002355/*
2356 * Adds 'pre,' prefix into 'str' is 'pre' is
2357 * not already part of 'str'.
2358 */
2359static char *prefix_if_not_in(const char *pre, char *str)
2360{
2361 char *n;
2362
2363 if (!str || strstr(str, pre))
2364 return str;
2365
2366 if (asprintf(&n, "%s,%s", pre, str) < 0)
2367 return NULL;
2368
2369 free(str);
2370 return n;
2371}
2372
2373static char *setup_overhead(char *keys)
2374{
2375 keys = prefix_if_not_in("overhead", keys);
2376
2377 if (symbol_conf.cumulate_callchain)
2378 keys = prefix_if_not_in("overhead_children", keys);
2379
2380 return keys;
2381}
2382
Namhyung Kim40184c42015-12-23 02:07:01 +09002383static int __setup_sorting(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002384{
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002385 char *str;
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002386 const char *sort_keys;
Namhyung Kim55309982013-02-06 14:57:16 +09002387 int ret = 0;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002388
Namhyung Kimd49dade2015-12-23 02:07:10 +09002389 ret = setup_sort_order(evlist);
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002390 if (ret)
2391 return ret;
2392
2393 sort_keys = sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002394 if (sort_keys == NULL) {
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002395 if (is_strict_order(field_order)) {
Namhyung Kima7d945b2014-03-04 10:46:34 +09002396 /*
2397 * If user specified field order but no sort order,
2398 * we'll honor it and not add default sort orders.
2399 */
2400 return 0;
2401 }
2402
Namhyung Kimd49dade2015-12-23 02:07:10 +09002403 sort_keys = get_default_sort_order(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002404 }
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002405
2406 str = strdup(sort_keys);
Namhyung Kim5936f542013-02-06 14:57:17 +09002407 if (str == NULL) {
2408 error("Not enough memory to setup sort keys");
2409 return -ENOMEM;
2410 }
2411
Jiri Olsab97511c2016-01-07 10:14:08 +01002412 /*
2413 * Prepend overhead fields for backward compatibility.
2414 */
2415 if (!is_strict_order(field_order)) {
2416 str = setup_overhead(str);
2417 if (str == NULL) {
2418 error("Not enough memory to setup overhead keys");
2419 return -ENOMEM;
2420 }
2421 }
2422
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002423 ret = setup_sort_list(str, evlist);
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002424
2425 free(str);
Namhyung Kim55309982013-02-06 14:57:16 +09002426 return ret;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002427}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002428
Jiri Olsaf2998422014-05-23 17:15:47 +02002429void perf_hpp__set_elide(int idx, bool elide)
Namhyung Kime67d49a2014-03-18 13:00:59 +09002430{
Jiri Olsaf2998422014-05-23 17:15:47 +02002431 struct perf_hpp_fmt *fmt;
2432 struct hpp_sort_entry *hse;
Namhyung Kime67d49a2014-03-18 13:00:59 +09002433
Jiri Olsacf094042016-01-18 10:24:17 +01002434 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002435 if (!perf_hpp__is_sort_entry(fmt))
2436 continue;
2437
2438 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2439 if (hse->se->se_width_idx == idx) {
2440 fmt->elide = elide;
2441 break;
2442 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002443 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002444}
2445
Jiri Olsaf2998422014-05-23 17:15:47 +02002446static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002447{
2448 if (list && strlist__nr_entries(list) == 1) {
2449 if (fp != NULL)
2450 fprintf(fp, "# %s: %s\n", list_name,
2451 strlist__entry(list, 0)->s);
Jiri Olsaf2998422014-05-23 17:15:47 +02002452 return true;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002453 }
Jiri Olsaf2998422014-05-23 17:15:47 +02002454 return false;
2455}
2456
2457static bool get_elide(int idx, FILE *output)
2458{
2459 switch (idx) {
2460 case HISTC_SYMBOL:
2461 return __get_elide(symbol_conf.sym_list, "symbol", output);
2462 case HISTC_DSO:
2463 return __get_elide(symbol_conf.dso_list, "dso", output);
2464 case HISTC_COMM:
2465 return __get_elide(symbol_conf.comm_list, "comm", output);
2466 default:
2467 break;
2468 }
2469
2470 if (sort__mode != SORT_MODE__BRANCH)
2471 return false;
2472
2473 switch (idx) {
2474 case HISTC_SYMBOL_FROM:
2475 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2476 case HISTC_SYMBOL_TO:
2477 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2478 case HISTC_DSO_FROM:
2479 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2480 case HISTC_DSO_TO:
2481 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2482 default:
2483 break;
2484 }
2485
2486 return false;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002487}
Namhyung Kim08e71542013-04-03 21:26:19 +09002488
2489void sort__setup_elide(FILE *output)
2490{
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002491 struct perf_hpp_fmt *fmt;
2492 struct hpp_sort_entry *hse;
Namhyung Kim7524f632013-11-08 17:53:42 +09002493
Jiri Olsacf094042016-01-18 10:24:17 +01002494 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002495 if (!perf_hpp__is_sort_entry(fmt))
2496 continue;
Namhyung Kim08e71542013-04-03 21:26:19 +09002497
Jiri Olsaf2998422014-05-23 17:15:47 +02002498 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2499 fmt->elide = get_elide(hse->se->se_width_idx, output);
Namhyung Kim08e71542013-04-03 21:26:19 +09002500 }
2501
Namhyung Kim7524f632013-11-08 17:53:42 +09002502 /*
2503 * It makes no sense to elide all of sort entries.
2504 * Just revert them to show up again.
2505 */
Jiri Olsacf094042016-01-18 10:24:17 +01002506 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002507 if (!perf_hpp__is_sort_entry(fmt))
2508 continue;
2509
Jiri Olsaf2998422014-05-23 17:15:47 +02002510 if (!fmt->elide)
Namhyung Kim7524f632013-11-08 17:53:42 +09002511 return;
2512 }
2513
Jiri Olsacf094042016-01-18 10:24:17 +01002514 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002515 if (!perf_hpp__is_sort_entry(fmt))
2516 continue;
2517
Jiri Olsaf2998422014-05-23 17:15:47 +02002518 fmt->elide = false;
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002519 }
Namhyung Kim08e71542013-04-03 21:26:19 +09002520}
Namhyung Kima7d945b2014-03-04 10:46:34 +09002521
Jiri Olsa07600022016-01-18 10:24:16 +01002522static int output_field_add(struct perf_hpp_list *list, char *tok)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002523{
2524 unsigned int i;
2525
2526 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2527 struct sort_dimension *sd = &common_sort_dimensions[i];
2528
2529 if (strncasecmp(tok, sd->name, strlen(tok)))
2530 continue;
2531
Jiri Olsa07600022016-01-18 10:24:16 +01002532 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002533 }
2534
2535 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2536 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2537
2538 if (strncasecmp(tok, hd->name, strlen(tok)))
2539 continue;
2540
Jiri Olsa07600022016-01-18 10:24:16 +01002541 return __hpp_dimension__add_output(list, hd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002542 }
2543
2544 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2545 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2546
2547 if (strncasecmp(tok, sd->name, strlen(tok)))
2548 continue;
2549
Jiri Olsa07600022016-01-18 10:24:16 +01002550 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002551 }
2552
2553 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2554 struct sort_dimension *sd = &memory_sort_dimensions[i];
2555
2556 if (strncasecmp(tok, sd->name, strlen(tok)))
2557 continue;
2558
Jiri Olsa07600022016-01-18 10:24:16 +01002559 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002560 }
2561
2562 return -ESRCH;
2563}
2564
Jiri Olsa07600022016-01-18 10:24:16 +01002565static int setup_output_list(struct perf_hpp_list *list, char *str)
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002566{
2567 char *tmp, *tok;
2568 int ret = 0;
2569
2570 for (tok = strtok_r(str, ", ", &tmp);
2571 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Jiri Olsa07600022016-01-18 10:24:16 +01002572 ret = output_field_add(list, tok);
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002573 if (ret == -EINVAL) {
2574 error("Invalid --fields key: `%s'", tok);
2575 break;
2576 } else if (ret == -ESRCH) {
2577 error("Unknown --fields key: `%s'", tok);
2578 break;
2579 }
2580 }
2581
2582 return ret;
2583}
2584
Namhyung Kima7d945b2014-03-04 10:46:34 +09002585static void reset_dimensions(void)
2586{
2587 unsigned int i;
2588
2589 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2590 common_sort_dimensions[i].taken = 0;
2591
2592 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2593 hpp_sort_dimensions[i].taken = 0;
2594
2595 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2596 bstack_sort_dimensions[i].taken = 0;
2597
2598 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2599 memory_sort_dimensions[i].taken = 0;
2600}
2601
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002602bool is_strict_order(const char *order)
2603{
2604 return order && (*order != '+');
2605}
2606
Namhyung Kima7d945b2014-03-04 10:46:34 +09002607static int __setup_output_field(void)
2608{
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002609 char *str, *strp;
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002610 int ret = -EINVAL;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002611
2612 if (field_order == NULL)
2613 return 0;
2614
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002615 strp = str = strdup(field_order);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002616 if (str == NULL) {
2617 error("Not enough memory to setup output fields");
2618 return -ENOMEM;
2619 }
2620
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002621 if (!is_strict_order(field_order))
2622 strp++;
2623
2624 if (!strlen(strp)) {
2625 error("Invalid --fields key: `+'");
2626 goto out;
2627 }
2628
Jiri Olsa07600022016-01-18 10:24:16 +01002629 ret = setup_output_list(&perf_hpp_list, strp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002630
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002631out:
Namhyung Kima7d945b2014-03-04 10:46:34 +09002632 free(str);
2633 return ret;
2634}
2635
Namhyung Kim40184c42015-12-23 02:07:01 +09002636int setup_sorting(struct perf_evlist *evlist)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002637{
2638 int err;
Namhyung Kimd3a72fd2016-02-27 03:52:44 +09002639 struct hists *hists;
2640 struct perf_evsel *evsel;
2641 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002642
Namhyung Kim40184c42015-12-23 02:07:01 +09002643 err = __setup_sorting(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002644 if (err < 0)
2645 return err;
2646
2647 if (parent_pattern != default_parent_pattern) {
Namhyung Kim40184c42015-12-23 02:07:01 +09002648 err = sort_dimension__add("parent", evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002649 if (err < 0)
2650 return err;
2651 }
2652
Namhyung Kimd3a72fd2016-02-27 03:52:44 +09002653 evlist__for_each(evlist, evsel) {
2654 hists = evsel__hists(evsel);
2655 hists->nr_sort_keys = perf_hpp_list.nr_sort_keys;
2656
2657 /*
2658 * If dynamic entries were used, it might add multiple
2659 * entries to each evsel for a single field name. Set
2660 * actual number of sort keys for each hists.
2661 */
2662 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
2663 if (perf_hpp__is_dynamic_entry(fmt) &&
2664 !perf_hpp__defined_dynamic_entry(fmt, hists))
2665 hists->nr_sort_keys--;
2666 }
2667 }
2668
Namhyung Kima7d945b2014-03-04 10:46:34 +09002669 reset_dimensions();
2670
2671 /*
2672 * perf diff doesn't use default hpp output fields.
2673 */
2674 if (sort__mode != SORT_MODE__DIFF)
2675 perf_hpp__init();
2676
2677 err = __setup_output_field();
2678 if (err < 0)
2679 return err;
2680
2681 /* copy sort keys to output fields */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002682 perf_hpp__setup_output_field(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002683 /* and then copy output fields to sort keys */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002684 perf_hpp__append_sort_keys(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002685
2686 return 0;
2687}
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002688
2689void reset_output_field(void)
2690{
2691 sort__need_collapse = 0;
2692 sort__has_parent = 0;
2693 sort__has_sym = 0;
2694 sort__has_dso = 0;
2695
Namhyung Kimd69b2962014-05-23 10:59:01 +09002696 field_order = NULL;
2697 sort_order = NULL;
2698
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002699 reset_dimensions();
Jiri Olsa43e0a682016-01-18 10:24:21 +01002700 perf_hpp__reset_output_field(&perf_hpp_list);
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002701}