blob: a556de751018ad471986f007ae6224023db547f6 [file] [log] [blame]
Don Zickus9b32ba72014-06-01 15:38:29 +02001#include <sys/mman.h>
John Kacurdd68ada2009-09-24 18:02:49 +02002#include "sort.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03003#include "hist.h"
Namhyung Kim4dfced32013-09-13 16:28:57 +09004#include "comm.h"
Namhyung Kim08e71542013-04-03 21:26:19 +09005#include "symbol.h"
Namhyung Kim8b536992014-03-03 11:46:55 +09006#include "evsel.h"
Namhyung Kim40184c42015-12-23 02:07:01 +09007#include "evlist.h"
8#include <traceevent/event-parse.h>
Jiri Olsa0c877d72016-02-24 09:46:46 +01009#include "mem-events.h"
John Kacurdd68ada2009-09-24 18:02:49 +020010
11regex_t parent_regex;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030012const char default_parent_pattern[] = "^sys_|^do_page_fault";
13const char *parent_pattern = default_parent_pattern;
14const char default_sort_order[] = "comm,dso,symbol";
Andi Kleen40997d62015-07-18 08:24:53 -070015const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
Namhyung Kim512ae1b2014-03-18 11:31:39 +090016const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
17const char default_top_sort_order[] = "dso,symbol";
18const char default_diff_sort_order[] = "dso,symbol";
Namhyung Kimd49dade2015-12-23 02:07:10 +090019const char default_tracepoint_sort_order[] = "trace";
Namhyung Kim512ae1b2014-03-18 11:31:39 +090020const char *sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +090021const char *field_order;
Greg Priceb21484f2012-12-06 21:48:05 -080022regex_t ignore_callees_regex;
23int have_ignore_callees = 0;
Namhyung Kim55369fc2013-04-01 20:35:20 +090024enum sort_mode sort__mode = SORT_MODE__NORMAL;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020025
Arnaldo Carvalho de Melo37d9bb52016-02-12 11:27:51 -030026/*
27 * Replaces all occurrences of a char used with the:
28 *
29 * -t, --field-separator
30 *
31 * option, that uses a special separator character and don't pad with spaces,
32 * replacing all occurances of this separator in symbol names (and other
33 * output) with a '.' character, that thus it's the only non valid separator.
34*/
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030035static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
John Kacurdd68ada2009-09-24 18:02:49 +020036{
37 int n;
38 va_list ap;
39
40 va_start(ap, fmt);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030041 n = vsnprintf(bf, size, fmt, ap);
Jiri Olsa0ca0c132012-09-06 17:46:56 +020042 if (symbol_conf.field_sep && n > 0) {
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030043 char *sep = bf;
John Kacurdd68ada2009-09-24 18:02:49 +020044
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030045 while (1) {
Jiri Olsa0ca0c132012-09-06 17:46:56 +020046 sep = strchr(sep, *symbol_conf.field_sep);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030047 if (sep == NULL)
48 break;
49 *sep = '.';
John Kacurdd68ada2009-09-24 18:02:49 +020050 }
John Kacurdd68ada2009-09-24 18:02:49 +020051 }
52 va_end(ap);
Anton Blanchardb8327962012-03-07 11:42:49 +110053
54 if (n >= (int)size)
55 return size - 1;
John Kacurdd68ada2009-09-24 18:02:49 +020056 return n;
57}
58
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020059static int64_t cmp_null(const void *l, const void *r)
Frederic Weisbecker872a8782011-06-29 03:14:52 +020060{
61 if (!l && !r)
62 return 0;
63 else if (!l)
64 return -1;
65 else
66 return 1;
67}
68
69/* --sort pid */
70
71static int64_t
72sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
73{
Adrian Hunter38051232013-07-04 16:20:31 +030074 return right->thread->tid - left->thread->tid;
Frederic Weisbecker872a8782011-06-29 03:14:52 +020075}
76
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030077static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -030078 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +020079{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020080 const char *comm = thread__comm_str(he->thread);
Namhyung Kim5b591662014-07-31 14:47:38 +090081
Jiri Olsa89c7cb22016-06-20 23:58:19 +020082 width = max(7U, width) - 8;
83 return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
Namhyung Kim5b591662014-07-31 14:47:38 +090084 width, width, comm ?: "");
John Kacurdd68ada2009-09-24 18:02:49 +020085}
86
Namhyung Kim54430102016-02-25 00:13:37 +090087static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
88{
89 const struct thread *th = arg;
90
91 if (type != HIST_FILTER__THREAD)
92 return -1;
93
94 return th && he->thread != th;
95}
96
Frederic Weisbecker872a8782011-06-29 03:14:52 +020097struct sort_entry sort_thread = {
Jiri Olsa89c7cb22016-06-20 23:58:19 +020098 .se_header = " Pid:Command",
Frederic Weisbecker872a8782011-06-29 03:14:52 +020099 .se_cmp = sort__thread_cmp,
100 .se_snprintf = hist_entry__thread_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900101 .se_filter = hist_entry__thread_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200102 .se_width_idx = HISTC_THREAD,
103};
104
105/* --sort comm */
106
107static int64_t
108sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
109{
Frederic Weisbeckerfedd63d2013-09-11 17:18:09 +0200110 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200111 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200112}
113
114static int64_t
115sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
116{
Namhyung Kim4dfced32013-09-13 16:28:57 +0900117 /* Compare the addr that should be unique among comm */
Jiri Olsa2f15bd82015-05-15 17:54:28 +0200118 return strcmp(comm__str(right->comm), comm__str(left->comm));
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200119}
120
Namhyung Kim202e7a62014-03-04 11:01:41 +0900121static int64_t
122sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
123{
124 return strcmp(comm__str(right->comm), comm__str(left->comm));
125}
126
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300127static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300128 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200129{
Namhyung Kim5b591662014-07-31 14:47:38 +0900130 return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
John Kacurdd68ada2009-09-24 18:02:49 +0200131}
132
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900133struct sort_entry sort_comm = {
134 .se_header = "Command",
135 .se_cmp = sort__comm_cmp,
136 .se_collapse = sort__comm_collapse,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900137 .se_sort = sort__comm_sort,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900138 .se_snprintf = hist_entry__comm_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900139 .se_filter = hist_entry__thread_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900140 .se_width_idx = HISTC_COMM,
141};
142
143/* --sort dso */
144
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100145static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
John Kacurdd68ada2009-09-24 18:02:49 +0200146{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100147 struct dso *dso_l = map_l ? map_l->dso : NULL;
148 struct dso *dso_r = map_r ? map_r->dso : NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300149 const char *dso_name_l, *dso_name_r;
John Kacurdd68ada2009-09-24 18:02:49 +0200150
151 if (!dso_l || !dso_r)
Namhyung Kim202e7a62014-03-04 11:01:41 +0900152 return cmp_null(dso_r, dso_l);
John Kacurdd68ada2009-09-24 18:02:49 +0200153
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300154 if (verbose) {
155 dso_name_l = dso_l->long_name;
156 dso_name_r = dso_r->long_name;
157 } else {
158 dso_name_l = dso_l->short_name;
159 dso_name_r = dso_r->short_name;
160 }
161
162 return strcmp(dso_name_l, dso_name_r);
John Kacurdd68ada2009-09-24 18:02:49 +0200163}
164
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100165static int64_t
166sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
John Kacurdd68ada2009-09-24 18:02:49 +0200167{
Namhyung Kim202e7a62014-03-04 11:01:41 +0900168 return _sort__dso_cmp(right->ms.map, left->ms.map);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100169}
170
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100171static int _hist_entry__dso_snprintf(struct map *map, char *bf,
172 size_t size, unsigned int width)
173{
174 if (map && map->dso) {
175 const char *dso_name = !verbose ? map->dso->short_name :
176 map->dso->long_name;
Namhyung Kim5b591662014-07-31 14:47:38 +0900177 return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300178 }
John Kacurdd68ada2009-09-24 18:02:49 +0200179
Namhyung Kim5b591662014-07-31 14:47:38 +0900180 return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
John Kacurdd68ada2009-09-24 18:02:49 +0200181}
182
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300183static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100184 size_t size, unsigned int width)
185{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300186 return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100187}
188
Namhyung Kim54430102016-02-25 00:13:37 +0900189static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
190{
191 const struct dso *dso = arg;
192
193 if (type != HIST_FILTER__DSO)
194 return -1;
195
196 return dso && (!he->ms.map || he->ms.map->dso != dso);
197}
198
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900199struct sort_entry sort_dso = {
200 .se_header = "Shared Object",
201 .se_cmp = sort__dso_cmp,
202 .se_snprintf = hist_entry__dso_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900203 .se_filter = hist_entry__dso_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900204 .se_width_idx = HISTC_DSO,
205};
206
207/* --sort symbol */
208
Namhyung Kim2037be52013-12-18 14:21:09 +0900209static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
210{
211 return (int64_t)(right_ip - left_ip);
212}
213
Namhyung Kim51f27d12013-02-06 14:57:15 +0900214static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900215{
216 if (!sym_l || !sym_r)
217 return cmp_null(sym_l, sym_r);
218
219 if (sym_l == sym_r)
220 return 0;
221
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700222 if (sym_l->start != sym_r->start)
223 return (int64_t)(sym_r->start - sym_l->start);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900224
Yannick Brosseauc05676c2015-06-17 16:41:10 -0700225 return (int64_t)(sym_r->end - sym_l->end);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900226}
227
228static int64_t
229sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
230{
Namhyung Kim09600e02013-10-15 11:01:56 +0900231 int64_t ret;
232
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900233 if (!left->ms.sym && !right->ms.sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900234 return _sort__addr_cmp(left->ip, right->ip);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900235
Namhyung Kim09600e02013-10-15 11:01:56 +0900236 /*
237 * comparing symbol address alone is not enough since it's a
238 * relative address within a dso.
239 */
Jiri Olsa69849fc2016-05-03 13:54:45 +0200240 if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
Namhyung Kim68f6d022013-12-18 14:21:10 +0900241 ret = sort__dso_cmp(left, right);
242 if (ret != 0)
243 return ret;
244 }
Namhyung Kim09600e02013-10-15 11:01:56 +0900245
Namhyung Kim51f27d12013-02-06 14:57:15 +0900246 return _sort__sym_cmp(left->ms.sym, right->ms.sym);
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900247}
248
Namhyung Kim202e7a62014-03-04 11:01:41 +0900249static int64_t
250sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
251{
252 if (!left->ms.sym || !right->ms.sym)
253 return cmp_null(left->ms.sym, right->ms.sym);
254
255 return strcmp(right->ms.sym->name, left->ms.sym->name);
256}
257
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100258static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
259 u64 ip, char level, char *bf, size_t size,
Namhyung Kim43355522012-12-27 18:11:39 +0900260 unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100261{
262 size_t ret = 0;
263
264 if (verbose) {
265 char o = map ? dso__symtab_origin(map->dso) : '!';
266 ret += repsep_snprintf(bf, size, "%-#*llx %c ",
Namhyung Kimded19d52013-04-01 20:35:19 +0900267 BITS_PER_LONG / 4 + 2, ip, o);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100268 }
269
270 ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100271 if (sym && map) {
272 if (map->type == MAP__VARIABLE) {
273 ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
274 ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
Namhyung Kim62667742013-01-24 16:10:42 +0100275 ip - map->unmap_ip(map, sym->start));
Stephane Eranian98a3b322013-01-24 16:10:35 +0100276 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300277 ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
Stephane Eranian98a3b322013-01-24 16:10:35 +0100278 width - ret,
279 sym->name);
280 }
281 } else {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100282 size_t len = BITS_PER_LONG / 4;
283 ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
284 len, ip);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100285 }
286
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300287 return ret;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100288}
289
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300290static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900291 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100292{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300293 return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
294 he->level, bf, size, width);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100295}
John Kacurdd68ada2009-09-24 18:02:49 +0200296
Namhyung Kim54430102016-02-25 00:13:37 +0900297static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
298{
299 const char *sym = arg;
300
301 if (type != HIST_FILTER__SYMBOL)
302 return -1;
303
304 return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
305}
306
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200307struct sort_entry sort_sym = {
308 .se_header = "Symbol",
309 .se_cmp = sort__sym_cmp,
Namhyung Kim202e7a62014-03-04 11:01:41 +0900310 .se_sort = sort__sym_sort,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200311 .se_snprintf = hist_entry__sym_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900312 .se_filter = hist_entry__sym_filter,
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200313 .se_width_idx = HISTC_SYMBOL,
314};
John Kacurdd68ada2009-09-24 18:02:49 +0200315
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300316/* --sort srcline */
317
Namhyung Kimcecaec62016-02-22 09:31:51 +0900318static char *hist_entry__get_srcline(struct hist_entry *he)
319{
320 struct map *map = he->ms.map;
321
322 if (!map)
323 return SRCLINE_UNKNOWN;
324
325 return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
326 he->ms.sym, true);
327}
328
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300329static int64_t
330sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
331{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900332 if (!left->srcline)
333 left->srcline = hist_entry__get_srcline(left);
334 if (!right->srcline)
335 right->srcline = hist_entry__get_srcline(right);
336
Namhyung Kim202e7a62014-03-04 11:01:41 +0900337 return strcmp(right->srcline, left->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300338}
339
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300340static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim5b591662014-07-31 14:47:38 +0900341 size_t size, unsigned int width)
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300342{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900343 if (!he->srcline)
344 he->srcline = hist_entry__get_srcline(he);
345
Namhyung Kim2960ed62016-02-22 09:32:33 +0900346 return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -0300347}
348
349struct sort_entry sort_srcline = {
350 .se_header = "Source:Line",
351 .se_cmp = sort__srcline_cmp,
352 .se_snprintf = hist_entry__srcline_snprintf,
353 .se_width_idx = HISTC_SRCLINE,
354};
355
Andi Kleen508be0d2016-05-20 13:15:08 -0700356/* --sort srcline_from */
357
358static int64_t
359sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
360{
361 if (!left->branch_info->srcline_from) {
362 struct map *map = left->branch_info->from.map;
363 if (!map)
364 left->branch_info->srcline_from = SRCLINE_UNKNOWN;
365 else
366 left->branch_info->srcline_from = get_srcline(map->dso,
367 map__rip_2objdump(map,
368 left->branch_info->from.al_addr),
369 left->branch_info->from.sym, true);
370 }
371 if (!right->branch_info->srcline_from) {
372 struct map *map = right->branch_info->from.map;
373 if (!map)
374 right->branch_info->srcline_from = SRCLINE_UNKNOWN;
375 else
376 right->branch_info->srcline_from = get_srcline(map->dso,
377 map__rip_2objdump(map,
378 right->branch_info->from.al_addr),
379 right->branch_info->from.sym, true);
380 }
381 return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
382}
383
384static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
385 size_t size, unsigned int width)
386{
387 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
388}
389
390struct sort_entry sort_srcline_from = {
391 .se_header = "From Source:Line",
392 .se_cmp = sort__srcline_from_cmp,
393 .se_snprintf = hist_entry__srcline_from_snprintf,
394 .se_width_idx = HISTC_SRCLINE_FROM,
395};
396
397/* --sort srcline_to */
398
399static int64_t
400sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
401{
402 if (!left->branch_info->srcline_to) {
403 struct map *map = left->branch_info->to.map;
404 if (!map)
405 left->branch_info->srcline_to = SRCLINE_UNKNOWN;
406 else
407 left->branch_info->srcline_to = get_srcline(map->dso,
408 map__rip_2objdump(map,
409 left->branch_info->to.al_addr),
410 left->branch_info->from.sym, true);
411 }
412 if (!right->branch_info->srcline_to) {
413 struct map *map = right->branch_info->to.map;
414 if (!map)
415 right->branch_info->srcline_to = SRCLINE_UNKNOWN;
416 else
417 right->branch_info->srcline_to = get_srcline(map->dso,
418 map__rip_2objdump(map,
419 right->branch_info->to.al_addr),
420 right->branch_info->to.sym, true);
421 }
422 return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
423}
424
425static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
426 size_t size, unsigned int width)
427{
428 return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
429}
430
431struct sort_entry sort_srcline_to = {
432 .se_header = "To Source:Line",
433 .se_cmp = sort__srcline_to_cmp,
434 .se_snprintf = hist_entry__srcline_to_snprintf,
435 .se_width_idx = HISTC_SRCLINE_TO,
436};
437
Andi Kleen31191a82015-08-07 15:54:24 -0700438/* --sort srcfile */
439
440static char no_srcfile[1];
441
Namhyung Kimcecaec62016-02-22 09:31:51 +0900442static char *hist_entry__get_srcfile(struct hist_entry *e)
Andi Kleen31191a82015-08-07 15:54:24 -0700443{
444 char *sf, *p;
445 struct map *map = e->ms.map;
446
Namhyung Kimcecaec62016-02-22 09:31:51 +0900447 if (!map)
448 return no_srcfile;
449
Andi Kleen2f84b422015-09-01 11:47:19 -0700450 sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
451 e->ms.sym, false, true);
Andi Kleen76b10652015-08-11 06:36:55 -0700452 if (!strcmp(sf, SRCLINE_UNKNOWN))
453 return no_srcfile;
Andi Kleen31191a82015-08-07 15:54:24 -0700454 p = strchr(sf, ':');
455 if (p && *sf) {
456 *p = 0;
457 return sf;
458 }
459 free(sf);
460 return no_srcfile;
461}
462
463static int64_t
464sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
465{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900466 if (!left->srcfile)
467 left->srcfile = hist_entry__get_srcfile(left);
468 if (!right->srcfile)
469 right->srcfile = hist_entry__get_srcfile(right);
470
Andi Kleen31191a82015-08-07 15:54:24 -0700471 return strcmp(right->srcfile, left->srcfile);
472}
473
474static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
475 size_t size, unsigned int width)
476{
Namhyung Kimcecaec62016-02-22 09:31:51 +0900477 if (!he->srcfile)
478 he->srcfile = hist_entry__get_srcfile(he);
479
Namhyung Kim2960ed62016-02-22 09:32:33 +0900480 return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
Andi Kleen31191a82015-08-07 15:54:24 -0700481}
482
483struct sort_entry sort_srcfile = {
484 .se_header = "Source File",
485 .se_cmp = sort__srcfile_cmp,
486 .se_snprintf = hist_entry__srcfile_snprintf,
487 .se_width_idx = HISTC_SRCFILE,
488};
489
John Kacurdd68ada2009-09-24 18:02:49 +0200490/* --sort parent */
491
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200492static int64_t
John Kacurdd68ada2009-09-24 18:02:49 +0200493sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
494{
495 struct symbol *sym_l = left->parent;
496 struct symbol *sym_r = right->parent;
497
498 if (!sym_l || !sym_r)
499 return cmp_null(sym_l, sym_r);
500
Namhyung Kim202e7a62014-03-04 11:01:41 +0900501 return strcmp(sym_r->name, sym_l->name);
John Kacurdd68ada2009-09-24 18:02:49 +0200502}
503
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300504static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300505 size_t size, unsigned int width)
John Kacurdd68ada2009-09-24 18:02:49 +0200506{
Namhyung Kim5b591662014-07-31 14:47:38 +0900507 return repsep_snprintf(bf, size, "%-*.*s", width, width,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300508 he->parent ? he->parent->name : "[other]");
John Kacurdd68ada2009-09-24 18:02:49 +0200509}
510
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200511struct sort_entry sort_parent = {
512 .se_header = "Parent symbol",
513 .se_cmp = sort__parent_cmp,
514 .se_snprintf = hist_entry__parent_snprintf,
515 .se_width_idx = HISTC_PARENT,
516};
517
Arun Sharmaf60f3592010-06-04 11:27:10 -0300518/* --sort cpu */
519
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200520static int64_t
Arun Sharmaf60f3592010-06-04 11:27:10 -0300521sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
522{
523 return right->cpu - left->cpu;
524}
525
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300526static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
527 size_t size, unsigned int width)
Arun Sharmaf60f3592010-06-04 11:27:10 -0300528{
Namhyung Kim5b591662014-07-31 14:47:38 +0900529 return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
Arun Sharmaf60f3592010-06-04 11:27:10 -0300530}
531
Frederic Weisbecker872a8782011-06-29 03:14:52 +0200532struct sort_entry sort_cpu = {
533 .se_header = "CPU",
534 .se_cmp = sort__cpu_cmp,
535 .se_snprintf = hist_entry__cpu_snprintf,
536 .se_width_idx = HISTC_CPU,
537};
538
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400539/* --sort socket */
540
541static int64_t
542sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
543{
544 return right->socket - left->socket;
545}
546
547static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
548 size_t size, unsigned int width)
549{
550 return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
551}
552
Namhyung Kim54430102016-02-25 00:13:37 +0900553static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
554{
555 int sk = *(const int *)arg;
556
557 if (type != HIST_FILTER__SOCKET)
558 return -1;
559
560 return sk >= 0 && he->socket != sk;
561}
562
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400563struct sort_entry sort_socket = {
564 .se_header = "Socket",
565 .se_cmp = sort__socket_cmp,
566 .se_snprintf = hist_entry__socket_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900567 .se_filter = hist_entry__socket_filter,
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400568 .se_width_idx = HISTC_SOCKET,
569};
570
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900571/* --sort trace */
572
573static char *get_trace_output(struct hist_entry *he)
574{
575 struct trace_seq seq;
576 struct perf_evsel *evsel;
577 struct pevent_record rec = {
578 .data = he->raw_data,
579 .size = he->raw_size,
580 };
581
582 evsel = hists_to_evsel(he->hists);
583
584 trace_seq_init(&seq);
Namhyung Kim053a3982015-12-23 02:07:05 +0900585 if (symbol_conf.raw_trace) {
586 pevent_print_fields(&seq, he->raw_data, he->raw_size,
587 evsel->tp_format);
588 } else {
589 pevent_event_info(&seq, evsel->tp_format, &rec);
590 }
Arnaldo Carvalho de Melo887fa862016-08-05 12:37:21 -0300591 /*
592 * Trim the buffer, it starts at 4KB and we're not going to
593 * add anything more to this buffer.
594 */
595 return realloc(seq.buffer, seq.len + 1);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900596}
597
598static int64_t
599sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
600{
601 struct perf_evsel *evsel;
602
603 evsel = hists_to_evsel(left->hists);
604 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
605 return 0;
606
607 if (left->trace_output == NULL)
608 left->trace_output = get_trace_output(left);
609 if (right->trace_output == NULL)
610 right->trace_output = get_trace_output(right);
611
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900612 return strcmp(right->trace_output, left->trace_output);
613}
614
615static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
616 size_t size, unsigned int width)
617{
618 struct perf_evsel *evsel;
619
620 evsel = hists_to_evsel(he->hists);
621 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
Namhyung Kim2960ed62016-02-22 09:32:33 +0900622 return scnprintf(bf, size, "%-.*s", width, "N/A");
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900623
624 if (he->trace_output == NULL)
625 he->trace_output = get_trace_output(he);
Namhyung Kim2960ed62016-02-22 09:32:33 +0900626 return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
Namhyung Kima34bb6a2015-12-23 02:07:04 +0900627}
628
629struct sort_entry sort_trace = {
630 .se_header = "Trace output",
631 .se_cmp = sort__trace_cmp,
632 .se_snprintf = hist_entry__trace_snprintf,
633 .se_width_idx = HISTC_TRACE,
634};
635
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900636/* sort keys for branch stacks */
637
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100638static int64_t
639sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
640{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200641 if (!left->branch_info || !right->branch_info)
642 return cmp_null(left->branch_info, right->branch_info);
643
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100644 return _sort__dso_cmp(left->branch_info->from.map,
645 right->branch_info->from.map);
646}
647
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300648static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100649 size_t size, unsigned int width)
650{
Jiri Olsa288a4b92014-10-16 16:07:07 +0200651 if (he->branch_info)
652 return _hist_entry__dso_snprintf(he->branch_info->from.map,
653 bf, size, width);
654 else
655 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100656}
657
Namhyung Kim54430102016-02-25 00:13:37 +0900658static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
659 const void *arg)
660{
661 const struct dso *dso = arg;
662
663 if (type != HIST_FILTER__DSO)
664 return -1;
665
666 return dso && (!he->branch_info || !he->branch_info->from.map ||
667 he->branch_info->from.map->dso != dso);
668}
669
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100670static int64_t
671sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
672{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200673 if (!left->branch_info || !right->branch_info)
674 return cmp_null(left->branch_info, right->branch_info);
675
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100676 return _sort__dso_cmp(left->branch_info->to.map,
677 right->branch_info->to.map);
678}
679
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300680static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100681 size_t size, unsigned int width)
682{
Jiri Olsa8b62fa52014-10-16 16:07:06 +0200683 if (he->branch_info)
684 return _hist_entry__dso_snprintf(he->branch_info->to.map,
685 bf, size, width);
686 else
687 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100688}
689
Namhyung Kim54430102016-02-25 00:13:37 +0900690static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
691 const void *arg)
692{
693 const struct dso *dso = arg;
694
695 if (type != HIST_FILTER__DSO)
696 return -1;
697
698 return dso && (!he->branch_info || !he->branch_info->to.map ||
699 he->branch_info->to.map->dso != dso);
700}
701
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100702static int64_t
703sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
704{
705 struct addr_map_symbol *from_l = &left->branch_info->from;
706 struct addr_map_symbol *from_r = &right->branch_info->from;
707
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200708 if (!left->branch_info || !right->branch_info)
709 return cmp_null(left->branch_info, right->branch_info);
710
711 from_l = &left->branch_info->from;
712 from_r = &right->branch_info->from;
713
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100714 if (!from_l->sym && !from_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900715 return _sort__addr_cmp(from_l->addr, from_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100716
Namhyung Kim51f27d12013-02-06 14:57:15 +0900717 return _sort__sym_cmp(from_l->sym, from_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100718}
719
720static int64_t
721sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
722{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200723 struct addr_map_symbol *to_l, *to_r;
724
725 if (!left->branch_info || !right->branch_info)
726 return cmp_null(left->branch_info, right->branch_info);
727
728 to_l = &left->branch_info->to;
729 to_r = &right->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100730
731 if (!to_l->sym && !to_r->sym)
Namhyung Kim2037be52013-12-18 14:21:09 +0900732 return _sort__addr_cmp(to_l->addr, to_r->addr);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100733
Namhyung Kim51f27d12013-02-06 14:57:15 +0900734 return _sort__sym_cmp(to_l->sym, to_r->sym);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100735}
736
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300737static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900738 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100739{
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200740 if (he->branch_info) {
741 struct addr_map_symbol *from = &he->branch_info->from;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100742
Jiri Olsa1b9e97a2014-10-16 16:07:05 +0200743 return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
744 he->level, bf, size, width);
745 }
746
747 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100748}
749
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300750static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
Namhyung Kim43355522012-12-27 18:11:39 +0900751 size_t size, unsigned int width)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100752{
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200753 if (he->branch_info) {
754 struct addr_map_symbol *to = &he->branch_info->to;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100755
Jiri Olsa38cdbd32014-10-16 16:07:04 +0200756 return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
757 he->level, bf, size, width);
758 }
759
760 return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100761}
762
Namhyung Kim54430102016-02-25 00:13:37 +0900763static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
764 const void *arg)
765{
766 const char *sym = arg;
767
768 if (type != HIST_FILTER__SYMBOL)
769 return -1;
770
771 return sym && !(he->branch_info && he->branch_info->from.sym &&
772 strstr(he->branch_info->from.sym->name, sym));
773}
774
775static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
776 const void *arg)
777{
778 const char *sym = arg;
779
780 if (type != HIST_FILTER__SYMBOL)
781 return -1;
782
783 return sym && !(he->branch_info && he->branch_info->to.sym &&
784 strstr(he->branch_info->to.sym->name, sym));
785}
786
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900787struct sort_entry sort_dso_from = {
788 .se_header = "Source Shared Object",
789 .se_cmp = sort__dso_from_cmp,
790 .se_snprintf = hist_entry__dso_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900791 .se_filter = hist_entry__dso_from_filter,
Namhyung Kim14d1ac72012-12-27 18:11:38 +0900792 .se_width_idx = HISTC_DSO_FROM,
793};
794
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100795struct sort_entry sort_dso_to = {
796 .se_header = "Target Shared Object",
797 .se_cmp = sort__dso_to_cmp,
798 .se_snprintf = hist_entry__dso_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900799 .se_filter = hist_entry__dso_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100800 .se_width_idx = HISTC_DSO_TO,
801};
802
803struct sort_entry sort_sym_from = {
804 .se_header = "Source Symbol",
805 .se_cmp = sort__sym_from_cmp,
806 .se_snprintf = hist_entry__sym_from_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900807 .se_filter = hist_entry__sym_from_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100808 .se_width_idx = HISTC_SYMBOL_FROM,
809};
810
811struct sort_entry sort_sym_to = {
812 .se_header = "Target Symbol",
813 .se_cmp = sort__sym_to_cmp,
814 .se_snprintf = hist_entry__sym_to_snprintf,
Namhyung Kim54430102016-02-25 00:13:37 +0900815 .se_filter = hist_entry__sym_to_filter,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100816 .se_width_idx = HISTC_SYMBOL_TO,
817};
818
819static int64_t
820sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
821{
Jiri Olsa428560e2014-10-16 16:07:03 +0200822 unsigned char mp, p;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100823
Jiri Olsa428560e2014-10-16 16:07:03 +0200824 if (!left->branch_info || !right->branch_info)
825 return cmp_null(left->branch_info, right->branch_info);
826
827 mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
828 p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100829 return mp || p;
830}
831
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300832static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100833 size_t size, unsigned int width){
834 static const char *out = "N/A";
835
Jiri Olsa428560e2014-10-16 16:07:03 +0200836 if (he->branch_info) {
837 if (he->branch_info->flags.predicted)
838 out = "N";
839 else if (he->branch_info->flags.mispred)
840 out = "Y";
841 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100842
Namhyung Kim5b591662014-07-31 14:47:38 +0900843 return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100844}
845
Andi Kleen0e332f02015-07-18 08:24:46 -0700846static int64_t
847sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
848{
849 return left->branch_info->flags.cycles -
850 right->branch_info->flags.cycles;
851}
852
853static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
854 size_t size, unsigned int width)
855{
856 if (he->branch_info->flags.cycles == 0)
857 return repsep_snprintf(bf, size, "%-*s", width, "-");
858 return repsep_snprintf(bf, size, "%-*hd", width,
859 he->branch_info->flags.cycles);
860}
861
862struct sort_entry sort_cycles = {
863 .se_header = "Basic Block Cycles",
864 .se_cmp = sort__cycles_cmp,
865 .se_snprintf = hist_entry__cycles_snprintf,
866 .se_width_idx = HISTC_CYCLES,
867};
868
Stephane Eranian98a3b322013-01-24 16:10:35 +0100869/* --sort daddr_sym */
870static int64_t
871sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
872{
873 uint64_t l = 0, r = 0;
874
875 if (left->mem_info)
876 l = left->mem_info->daddr.addr;
877 if (right->mem_info)
878 r = right->mem_info->daddr.addr;
879
880 return (int64_t)(r - l);
881}
882
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300883static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100884 size_t size, unsigned int width)
885{
886 uint64_t addr = 0;
887 struct map *map = NULL;
888 struct symbol *sym = NULL;
889
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300890 if (he->mem_info) {
891 addr = he->mem_info->daddr.addr;
892 map = he->mem_info->daddr.map;
893 sym = he->mem_info->daddr.sym;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100894 }
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300895 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100896 width);
897}
898
899static int64_t
Don Zickus28e6db22015-10-05 20:06:07 +0200900sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
901{
902 uint64_t l = 0, r = 0;
903
904 if (left->mem_info)
905 l = left->mem_info->iaddr.addr;
906 if (right->mem_info)
907 r = right->mem_info->iaddr.addr;
908
909 return (int64_t)(r - l);
910}
911
912static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
913 size_t size, unsigned int width)
914{
915 uint64_t addr = 0;
916 struct map *map = NULL;
917 struct symbol *sym = NULL;
918
919 if (he->mem_info) {
920 addr = he->mem_info->iaddr.addr;
921 map = he->mem_info->iaddr.map;
922 sym = he->mem_info->iaddr.sym;
923 }
924 return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
925 width);
926}
927
928static int64_t
Stephane Eranian98a3b322013-01-24 16:10:35 +0100929sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
930{
931 struct map *map_l = NULL;
932 struct map *map_r = NULL;
933
934 if (left->mem_info)
935 map_l = left->mem_info->daddr.map;
936 if (right->mem_info)
937 map_r = right->mem_info->daddr.map;
938
939 return _sort__dso_cmp(map_l, map_r);
940}
941
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300942static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100943 size_t size, unsigned int width)
944{
945 struct map *map = NULL;
946
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300947 if (he->mem_info)
948 map = he->mem_info->daddr.map;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100949
950 return _hist_entry__dso_snprintf(map, bf, size, width);
951}
952
953static int64_t
954sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
955{
956 union perf_mem_data_src data_src_l;
957 union perf_mem_data_src data_src_r;
958
959 if (left->mem_info)
960 data_src_l = left->mem_info->data_src;
961 else
962 data_src_l.mem_lock = PERF_MEM_LOCK_NA;
963
964 if (right->mem_info)
965 data_src_r = right->mem_info->data_src;
966 else
967 data_src_r.mem_lock = PERF_MEM_LOCK_NA;
968
969 return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
970}
971
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300972static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100973 size_t size, unsigned int width)
974{
Jiri Olsa69a77272016-02-24 09:46:49 +0100975 char out[10];
Stephane Eranian98a3b322013-01-24 16:10:35 +0100976
Jiri Olsa69a77272016-02-24 09:46:49 +0100977 perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -0300978 return repsep_snprintf(bf, size, "%.*s", width, out);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100979}
980
981static int64_t
982sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
983{
984 union perf_mem_data_src data_src_l;
985 union perf_mem_data_src data_src_r;
986
987 if (left->mem_info)
988 data_src_l = left->mem_info->data_src;
989 else
990 data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
991
992 if (right->mem_info)
993 data_src_r = right->mem_info->data_src;
994 else
995 data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
996
997 return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
998}
999
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001000static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001001 size_t size, unsigned int width)
1002{
1003 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +01001004
Jiri Olsa0c877d72016-02-24 09:46:46 +01001005 perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001006 return repsep_snprintf(bf, size, "%-*s", width, out);
1007}
1008
1009static int64_t
1010sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1011{
1012 union perf_mem_data_src data_src_l;
1013 union perf_mem_data_src data_src_r;
1014
1015 if (left->mem_info)
1016 data_src_l = left->mem_info->data_src;
1017 else
1018 data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1019
1020 if (right->mem_info)
1021 data_src_r = right->mem_info->data_src;
1022 else
1023 data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1024
1025 return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1026}
1027
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001028static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001029 size_t size, unsigned int width)
1030{
1031 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +01001032
Jiri Olsa071e9a12016-02-24 09:46:47 +01001033 perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001034 return repsep_snprintf(bf, size, "%-*s", width, out);
1035}
1036
1037static int64_t
1038sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1039{
1040 union perf_mem_data_src data_src_l;
1041 union perf_mem_data_src data_src_r;
1042
1043 if (left->mem_info)
1044 data_src_l = left->mem_info->data_src;
1045 else
1046 data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1047
1048 if (right->mem_info)
1049 data_src_r = right->mem_info->data_src;
1050 else
1051 data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1052
1053 return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1054}
1055
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001056static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001057 size_t size, unsigned int width)
1058{
1059 char out[64];
Stephane Eranian98a3b322013-01-24 16:10:35 +01001060
Jiri Olsa2c07af12016-02-24 09:46:48 +01001061 perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001062 return repsep_snprintf(bf, size, "%-*s", width, out);
1063}
1064
Don Zickus9b32ba72014-06-01 15:38:29 +02001065static int64_t
1066sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1067{
1068 u64 l, r;
1069 struct map *l_map, *r_map;
1070
1071 if (!left->mem_info) return -1;
1072 if (!right->mem_info) return 1;
1073
1074 /* group event types together */
1075 if (left->cpumode > right->cpumode) return -1;
1076 if (left->cpumode < right->cpumode) return 1;
1077
1078 l_map = left->mem_info->daddr.map;
1079 r_map = right->mem_info->daddr.map;
1080
1081 /* if both are NULL, jump to sort on al_addr instead */
1082 if (!l_map && !r_map)
1083 goto addr;
1084
1085 if (!l_map) return -1;
1086 if (!r_map) return 1;
1087
1088 if (l_map->maj > r_map->maj) return -1;
1089 if (l_map->maj < r_map->maj) return 1;
1090
1091 if (l_map->min > r_map->min) return -1;
1092 if (l_map->min < r_map->min) return 1;
1093
1094 if (l_map->ino > r_map->ino) return -1;
1095 if (l_map->ino < r_map->ino) return 1;
1096
1097 if (l_map->ino_generation > r_map->ino_generation) return -1;
1098 if (l_map->ino_generation < r_map->ino_generation) return 1;
1099
1100 /*
1101 * Addresses with no major/minor numbers are assumed to be
1102 * anonymous in userspace. Sort those on pid then address.
1103 *
1104 * The kernel and non-zero major/minor mapped areas are
1105 * assumed to be unity mapped. Sort those on address.
1106 */
1107
1108 if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1109 (!(l_map->flags & MAP_SHARED)) &&
1110 !l_map->maj && !l_map->min && !l_map->ino &&
1111 !l_map->ino_generation) {
1112 /* userspace anonymous */
1113
1114 if (left->thread->pid_ > right->thread->pid_) return -1;
1115 if (left->thread->pid_ < right->thread->pid_) return 1;
1116 }
1117
1118addr:
1119 /* al_addr does all the right addr - start + offset calculations */
1120 l = cl_address(left->mem_info->daddr.al_addr);
1121 r = cl_address(right->mem_info->daddr.al_addr);
1122
1123 if (l > r) return -1;
1124 if (l < r) return 1;
1125
1126 return 0;
1127}
1128
1129static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1130 size_t size, unsigned int width)
1131{
1132
1133 uint64_t addr = 0;
1134 struct map *map = NULL;
1135 struct symbol *sym = NULL;
1136 char level = he->level;
1137
1138 if (he->mem_info) {
1139 addr = cl_address(he->mem_info->daddr.al_addr);
1140 map = he->mem_info->daddr.map;
1141 sym = he->mem_info->daddr.sym;
1142
1143 /* print [s] for shared data mmaps */
1144 if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1145 map && (map->type == MAP__VARIABLE) &&
1146 (map->flags & MAP_SHARED) &&
1147 (map->maj || map->min || map->ino ||
1148 map->ino_generation))
1149 level = 's';
1150 else if (!map)
1151 level = 'X';
1152 }
1153 return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1154 width);
1155}
1156
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001157struct sort_entry sort_mispredict = {
1158 .se_header = "Branch Mispredicted",
1159 .se_cmp = sort__mispredict_cmp,
1160 .se_snprintf = hist_entry__mispredict_snprintf,
1161 .se_width_idx = HISTC_MISPREDICT,
1162};
1163
Andi Kleen05484292013-01-24 16:10:29 +01001164static u64 he_weight(struct hist_entry *he)
1165{
1166 return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1167}
1168
1169static int64_t
1170sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1171{
1172 return he_weight(left) - he_weight(right);
1173}
1174
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001175static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001176 size_t size, unsigned int width)
1177{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001178 return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
Andi Kleen05484292013-01-24 16:10:29 +01001179}
1180
1181struct sort_entry sort_local_weight = {
1182 .se_header = "Local Weight",
1183 .se_cmp = sort__local_weight_cmp,
1184 .se_snprintf = hist_entry__local_weight_snprintf,
1185 .se_width_idx = HISTC_LOCAL_WEIGHT,
1186};
1187
1188static int64_t
1189sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1190{
1191 return left->stat.weight - right->stat.weight;
1192}
1193
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001194static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
Andi Kleen05484292013-01-24 16:10:29 +01001195 size_t size, unsigned int width)
1196{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001197 return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
Andi Kleen05484292013-01-24 16:10:29 +01001198}
1199
1200struct sort_entry sort_global_weight = {
1201 .se_header = "Weight",
1202 .se_cmp = sort__global_weight_cmp,
1203 .se_snprintf = hist_entry__global_weight_snprintf,
1204 .se_width_idx = HISTC_GLOBAL_WEIGHT,
1205};
1206
Stephane Eranian98a3b322013-01-24 16:10:35 +01001207struct sort_entry sort_mem_daddr_sym = {
1208 .se_header = "Data Symbol",
1209 .se_cmp = sort__daddr_cmp,
1210 .se_snprintf = hist_entry__daddr_snprintf,
1211 .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
1212};
1213
Don Zickus28e6db22015-10-05 20:06:07 +02001214struct sort_entry sort_mem_iaddr_sym = {
1215 .se_header = "Code Symbol",
1216 .se_cmp = sort__iaddr_cmp,
1217 .se_snprintf = hist_entry__iaddr_snprintf,
1218 .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
1219};
1220
Stephane Eranian98a3b322013-01-24 16:10:35 +01001221struct sort_entry sort_mem_daddr_dso = {
1222 .se_header = "Data Object",
1223 .se_cmp = sort__dso_daddr_cmp,
1224 .se_snprintf = hist_entry__dso_daddr_snprintf,
Jiri Olsa94c39982016-06-14 20:19:12 +02001225 .se_width_idx = HISTC_MEM_DADDR_DSO,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001226};
1227
1228struct sort_entry sort_mem_locked = {
1229 .se_header = "Locked",
1230 .se_cmp = sort__locked_cmp,
1231 .se_snprintf = hist_entry__locked_snprintf,
1232 .se_width_idx = HISTC_MEM_LOCKED,
1233};
1234
1235struct sort_entry sort_mem_tlb = {
1236 .se_header = "TLB access",
1237 .se_cmp = sort__tlb_cmp,
1238 .se_snprintf = hist_entry__tlb_snprintf,
1239 .se_width_idx = HISTC_MEM_TLB,
1240};
1241
1242struct sort_entry sort_mem_lvl = {
1243 .se_header = "Memory access",
1244 .se_cmp = sort__lvl_cmp,
1245 .se_snprintf = hist_entry__lvl_snprintf,
1246 .se_width_idx = HISTC_MEM_LVL,
1247};
1248
1249struct sort_entry sort_mem_snoop = {
1250 .se_header = "Snoop",
1251 .se_cmp = sort__snoop_cmp,
1252 .se_snprintf = hist_entry__snoop_snprintf,
1253 .se_width_idx = HISTC_MEM_SNOOP,
1254};
1255
Don Zickus9b32ba72014-06-01 15:38:29 +02001256struct sort_entry sort_mem_dcacheline = {
1257 .se_header = "Data Cacheline",
1258 .se_cmp = sort__dcacheline_cmp,
1259 .se_snprintf = hist_entry__dcacheline_snprintf,
1260 .se_width_idx = HISTC_MEM_DCACHELINE,
1261};
1262
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001263static int64_t
1264sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1265{
Jiri Olsa49f47442014-10-16 16:07:01 +02001266 if (!left->branch_info || !right->branch_info)
1267 return cmp_null(left->branch_info, right->branch_info);
1268
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001269 return left->branch_info->flags.abort !=
1270 right->branch_info->flags.abort;
1271}
1272
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001273static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001274 size_t size, unsigned int width)
1275{
Jiri Olsa49f47442014-10-16 16:07:01 +02001276 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001277
Jiri Olsa49f47442014-10-16 16:07:01 +02001278 if (he->branch_info) {
1279 if (he->branch_info->flags.abort)
1280 out = "A";
1281 else
1282 out = ".";
1283 }
1284
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001285 return repsep_snprintf(bf, size, "%-*s", width, out);
1286}
1287
1288struct sort_entry sort_abort = {
1289 .se_header = "Transaction abort",
1290 .se_cmp = sort__abort_cmp,
1291 .se_snprintf = hist_entry__abort_snprintf,
1292 .se_width_idx = HISTC_ABORT,
1293};
1294
1295static int64_t
1296sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1297{
Jiri Olsa0199d242014-10-16 16:07:02 +02001298 if (!left->branch_info || !right->branch_info)
1299 return cmp_null(left->branch_info, right->branch_info);
1300
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001301 return left->branch_info->flags.in_tx !=
1302 right->branch_info->flags.in_tx;
1303}
1304
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001305static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001306 size_t size, unsigned int width)
1307{
Jiri Olsa0199d242014-10-16 16:07:02 +02001308 static const char *out = "N/A";
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001309
Jiri Olsa0199d242014-10-16 16:07:02 +02001310 if (he->branch_info) {
1311 if (he->branch_info->flags.in_tx)
1312 out = "T";
1313 else
1314 out = ".";
1315 }
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001316
1317 return repsep_snprintf(bf, size, "%-*s", width, out);
1318}
1319
1320struct sort_entry sort_in_tx = {
1321 .se_header = "Branch in transaction",
1322 .se_cmp = sort__in_tx_cmp,
1323 .se_snprintf = hist_entry__in_tx_snprintf,
1324 .se_width_idx = HISTC_IN_TX,
1325};
1326
Andi Kleen475eeab2013-09-20 07:40:43 -07001327static int64_t
1328sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1329{
1330 return left->transaction - right->transaction;
1331}
1332
1333static inline char *add_str(char *p, const char *str)
1334{
1335 strcpy(p, str);
1336 return p + strlen(str);
1337}
1338
1339static struct txbit {
1340 unsigned flag;
1341 const char *name;
1342 int skip_for_len;
1343} txbits[] = {
1344 { PERF_TXN_ELISION, "EL ", 0 },
1345 { PERF_TXN_TRANSACTION, "TX ", 1 },
1346 { PERF_TXN_SYNC, "SYNC ", 1 },
1347 { PERF_TXN_ASYNC, "ASYNC ", 0 },
1348 { PERF_TXN_RETRY, "RETRY ", 0 },
1349 { PERF_TXN_CONFLICT, "CON ", 0 },
1350 { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1351 { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
1352 { 0, NULL, 0 }
1353};
1354
1355int hist_entry__transaction_len(void)
1356{
1357 int i;
1358 int len = 0;
1359
1360 for (i = 0; txbits[i].name; i++) {
1361 if (!txbits[i].skip_for_len)
1362 len += strlen(txbits[i].name);
1363 }
1364 len += 4; /* :XX<space> */
1365 return len;
1366}
1367
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001368static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
Andi Kleen475eeab2013-09-20 07:40:43 -07001369 size_t size, unsigned int width)
1370{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -03001371 u64 t = he->transaction;
Andi Kleen475eeab2013-09-20 07:40:43 -07001372 char buf[128];
1373 char *p = buf;
1374 int i;
1375
1376 buf[0] = 0;
1377 for (i = 0; txbits[i].name; i++)
1378 if (txbits[i].flag & t)
1379 p = add_str(p, txbits[i].name);
1380 if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1381 p = add_str(p, "NEITHER ");
1382 if (t & PERF_TXN_ABORT_MASK) {
1383 sprintf(p, ":%" PRIx64,
1384 (t & PERF_TXN_ABORT_MASK) >>
1385 PERF_TXN_ABORT_SHIFT);
1386 p += strlen(p);
1387 }
1388
1389 return repsep_snprintf(bf, size, "%-*s", width, buf);
1390}
1391
1392struct sort_entry sort_transaction = {
1393 .se_header = "Transaction ",
1394 .se_cmp = sort__transaction_cmp,
1395 .se_snprintf = hist_entry__transaction_snprintf,
1396 .se_width_idx = HISTC_TRANSACTION,
1397};
1398
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001399struct sort_dimension {
1400 const char *name;
1401 struct sort_entry *entry;
1402 int taken;
1403};
1404
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001405#define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1406
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001407static struct sort_dimension common_sort_dimensions[] = {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001408 DIM(SORT_PID, "pid", sort_thread),
1409 DIM(SORT_COMM, "comm", sort_comm),
1410 DIM(SORT_DSO, "dso", sort_dso),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001411 DIM(SORT_SYM, "symbol", sort_sym),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001412 DIM(SORT_PARENT, "parent", sort_parent),
1413 DIM(SORT_CPU, "cpu", sort_cpu),
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001414 DIM(SORT_SOCKET, "socket", sort_socket),
Arnaldo Carvalho de Melo409a8be2012-05-30 10:33:24 -03001415 DIM(SORT_SRCLINE, "srcline", sort_srcline),
Andi Kleen31191a82015-08-07 15:54:24 -07001416 DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
Andi Kleenf9ea55d2013-07-18 15:58:53 -07001417 DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1418 DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
Andi Kleen475eeab2013-09-20 07:40:43 -07001419 DIM(SORT_TRANSACTION, "transaction", sort_transaction),
Namhyung Kima34bb6a2015-12-23 02:07:04 +09001420 DIM(SORT_TRACE, "trace", sort_trace),
Frederic Weisbecker872a8782011-06-29 03:14:52 +02001421};
1422
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001423#undef DIM
1424
1425#define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1426
1427static struct sort_dimension bstack_sort_dimensions[] = {
1428 DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1429 DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1430 DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1431 DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1432 DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
Andi Kleenf5d05bc2013-09-20 07:40:41 -07001433 DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1434 DIM(SORT_ABORT, "abort", sort_abort),
Andi Kleen0e332f02015-07-18 08:24:46 -07001435 DIM(SORT_CYCLES, "cycles", sort_cycles),
Andi Kleen508be0d2016-05-20 13:15:08 -07001436 DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1437 DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
Namhyung Kimfc5871e2012-12-27 18:11:46 +09001438};
1439
1440#undef DIM
1441
Namhyung Kimafab87b2013-04-03 21:26:11 +09001442#define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1443
1444static struct sort_dimension memory_sort_dimensions[] = {
Namhyung Kimafab87b2013-04-03 21:26:11 +09001445 DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
Don Zickus28e6db22015-10-05 20:06:07 +02001446 DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001447 DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1448 DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1449 DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1450 DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1451 DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
Don Zickus9b32ba72014-06-01 15:38:29 +02001452 DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
Namhyung Kimafab87b2013-04-03 21:26:11 +09001453};
1454
1455#undef DIM
1456
Namhyung Kima2ce0672014-03-04 09:06:42 +09001457struct hpp_dimension {
1458 const char *name;
1459 struct perf_hpp_fmt *fmt;
1460 int taken;
1461};
1462
1463#define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1464
1465static struct hpp_dimension hpp_sort_dimensions[] = {
1466 DIM(PERF_HPP__OVERHEAD, "overhead"),
1467 DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1468 DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1469 DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1470 DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
Namhyung Kim594dcbf2013-10-30 16:06:59 +09001471 DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
Namhyung Kima2ce0672014-03-04 09:06:42 +09001472 DIM(PERF_HPP__SAMPLES, "sample"),
1473 DIM(PERF_HPP__PERIOD, "period"),
1474};
1475
1476#undef DIM
1477
Namhyung Kim8b536992014-03-03 11:46:55 +09001478struct hpp_sort_entry {
1479 struct perf_hpp_fmt hpp;
1480 struct sort_entry *se;
1481};
1482
Namhyung Kime0d66c72014-07-31 14:47:37 +09001483void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
Namhyung Kim678a5002014-03-20 11:18:54 +09001484{
1485 struct hpp_sort_entry *hse;
1486
1487 if (!perf_hpp__is_sort_entry(fmt))
1488 return;
1489
1490 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001491 hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
Namhyung Kim678a5002014-03-20 11:18:54 +09001492}
1493
Namhyung Kim8b536992014-03-03 11:46:55 +09001494static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsa74bb43f2016-08-07 17:28:27 +02001495 struct hists *hists, int line __maybe_unused)
Namhyung Kim8b536992014-03-03 11:46:55 +09001496{
1497 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001498 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001499
1500 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001501
Namhyung Kim5b591662014-07-31 14:47:38 +09001502 if (!len)
Jiri Olsa05372172016-06-14 20:19:19 +02001503 len = hists__col_len(hists, hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001504
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001505 return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
Namhyung Kim8b536992014-03-03 11:46:55 +09001506}
1507
1508static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1509 struct perf_hpp *hpp __maybe_unused,
Jiri Olsada1b0402016-06-14 20:19:20 +02001510 struct hists *hists)
Namhyung Kim8b536992014-03-03 11:46:55 +09001511{
1512 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001513 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001514
1515 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1516
Namhyung Kim5b591662014-07-31 14:47:38 +09001517 if (!len)
Jiri Olsada1b0402016-06-14 20:19:20 +02001518 len = hists__col_len(hists, hse->se->se_width_idx);
Namhyung Kim5b591662014-07-31 14:47:38 +09001519
1520 return len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001521}
1522
1523static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1524 struct hist_entry *he)
1525{
1526 struct hpp_sort_entry *hse;
Namhyung Kim5b591662014-07-31 14:47:38 +09001527 size_t len = fmt->user_len;
Namhyung Kim8b536992014-03-03 11:46:55 +09001528
1529 hse = container_of(fmt, struct hpp_sort_entry, hpp);
Namhyung Kim5b591662014-07-31 14:47:38 +09001530
1531 if (!len)
1532 len = hists__col_len(he->hists, hse->se->se_width_idx);
Namhyung Kim8b536992014-03-03 11:46:55 +09001533
1534 return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1535}
1536
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001537static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1538 struct hist_entry *a, struct hist_entry *b)
1539{
1540 struct hpp_sort_entry *hse;
1541
1542 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1543 return hse->se->se_cmp(a, b);
1544}
1545
1546static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1547 struct hist_entry *a, struct hist_entry *b)
1548{
1549 struct hpp_sort_entry *hse;
1550 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1551
1552 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1553 collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1554 return collapse_fn(a, b);
1555}
1556
1557static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1558 struct hist_entry *a, struct hist_entry *b)
1559{
1560 struct hpp_sort_entry *hse;
1561 int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1562
1563 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1564 sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1565 return sort_fn(a, b);
1566}
1567
Jiri Olsa97358082016-01-18 10:24:03 +01001568bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1569{
1570 return format->header == __sort__hpp_header;
1571}
1572
Namhyung Kim4945cf22016-03-09 22:46:57 +09001573#define MK_SORT_ENTRY_CHK(key) \
1574bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
1575{ \
1576 struct hpp_sort_entry *hse; \
1577 \
1578 if (!perf_hpp__is_sort_entry(fmt)) \
1579 return false; \
1580 \
1581 hse = container_of(fmt, struct hpp_sort_entry, hpp); \
1582 return hse->se == &sort_ ## key ; \
Namhyung Kima9c6e462016-02-25 00:13:33 +09001583}
1584
Namhyung Kim4945cf22016-03-09 22:46:57 +09001585MK_SORT_ENTRY_CHK(trace)
1586MK_SORT_ENTRY_CHK(srcline)
1587MK_SORT_ENTRY_CHK(srcfile)
1588MK_SORT_ENTRY_CHK(thread)
1589MK_SORT_ENTRY_CHK(comm)
1590MK_SORT_ENTRY_CHK(dso)
1591MK_SORT_ENTRY_CHK(sym)
Namhyung Kima9c6e462016-02-25 00:13:33 +09001592
Namhyung Kima9c6e462016-02-25 00:13:33 +09001593
Jiri Olsa97358082016-01-18 10:24:03 +01001594static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1595{
1596 struct hpp_sort_entry *hse_a;
1597 struct hpp_sort_entry *hse_b;
1598
1599 if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1600 return false;
1601
1602 hse_a = container_of(a, struct hpp_sort_entry, hpp);
1603 hse_b = container_of(b, struct hpp_sort_entry, hpp);
1604
1605 return hse_a->se == hse_b->se;
1606}
1607
Jiri Olsa564132f2016-01-18 10:24:09 +01001608static void hse_free(struct perf_hpp_fmt *fmt)
1609{
1610 struct hpp_sort_entry *hse;
1611
1612 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1613 free(hse);
1614}
1615
Namhyung Kima7d945b2014-03-04 10:46:34 +09001616static struct hpp_sort_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001617__sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
Namhyung Kim8b536992014-03-03 11:46:55 +09001618{
1619 struct hpp_sort_entry *hse;
1620
1621 hse = malloc(sizeof(*hse));
1622 if (hse == NULL) {
1623 pr_err("Memory allocation failed\n");
Namhyung Kima7d945b2014-03-04 10:46:34 +09001624 return NULL;
Namhyung Kim8b536992014-03-03 11:46:55 +09001625 }
1626
1627 hse->se = sd->entry;
Namhyung Kim1ecd4452014-07-31 14:47:40 +09001628 hse->hpp.name = sd->entry->se_header;
Namhyung Kim8b536992014-03-03 11:46:55 +09001629 hse->hpp.header = __sort__hpp_header;
1630 hse->hpp.width = __sort__hpp_width;
1631 hse->hpp.entry = __sort__hpp_entry;
1632 hse->hpp.color = NULL;
1633
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001634 hse->hpp.cmp = __sort__hpp_cmp;
1635 hse->hpp.collapse = __sort__hpp_collapse;
1636 hse->hpp.sort = __sort__hpp_sort;
Jiri Olsa97358082016-01-18 10:24:03 +01001637 hse->hpp.equal = __sort__hpp_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001638 hse->hpp.free = hse_free;
Namhyung Kim8b536992014-03-03 11:46:55 +09001639
1640 INIT_LIST_HEAD(&hse->hpp.list);
1641 INIT_LIST_HEAD(&hse->hpp.sort_list);
Jiri Olsaf2998422014-05-23 17:15:47 +02001642 hse->hpp.elide = false;
Namhyung Kime0d66c72014-07-31 14:47:37 +09001643 hse->hpp.len = 0;
Namhyung Kim5b591662014-07-31 14:47:38 +09001644 hse->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001645 hse->hpp.level = level;
Namhyung Kim8b536992014-03-03 11:46:55 +09001646
Namhyung Kima7d945b2014-03-04 10:46:34 +09001647 return hse;
1648}
1649
Jiri Olsa564132f2016-01-18 10:24:09 +01001650static void hpp_free(struct perf_hpp_fmt *fmt)
1651{
1652 free(fmt);
1653}
1654
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001655static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1656 int level)
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001657{
1658 struct perf_hpp_fmt *fmt;
1659
1660 fmt = memdup(hd->fmt, sizeof(*fmt));
1661 if (fmt) {
1662 INIT_LIST_HEAD(&fmt->list);
1663 INIT_LIST_HEAD(&fmt->sort_list);
Jiri Olsa564132f2016-01-18 10:24:09 +01001664 fmt->free = hpp_free;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001665 fmt->level = level;
Jiri Olsa1945c3e2016-01-18 10:24:07 +01001666 }
1667
1668 return fmt;
1669}
1670
Namhyung Kim54430102016-02-25 00:13:37 +09001671int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1672{
1673 struct perf_hpp_fmt *fmt;
1674 struct hpp_sort_entry *hse;
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001675 int ret = -1;
1676 int r;
Namhyung Kim54430102016-02-25 00:13:37 +09001677
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001678 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1679 if (!perf_hpp__is_sort_entry(fmt))
1680 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001681
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001682 hse = container_of(fmt, struct hpp_sort_entry, hpp);
1683 if (hse->se->se_filter == NULL)
1684 continue;
Namhyung Kim54430102016-02-25 00:13:37 +09001685
Namhyung Kimf4954cf2016-03-09 22:46:56 +09001686 /*
1687 * hist entry is filtered if any of sort key in the hpp list
1688 * is applied. But it should skip non-matched filter types.
1689 */
1690 r = hse->se->se_filter(he, type, arg);
1691 if (r >= 0) {
1692 if (ret < 0)
1693 ret = 0;
1694 ret |= r;
1695 }
1696 }
1697
1698 return ret;
Namhyung Kim54430102016-02-25 00:13:37 +09001699}
1700
Jiri Olsad7b617f2016-03-09 11:04:17 +01001701static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1702 struct perf_hpp_list *list,
1703 int level)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001704{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001705 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001706
1707 if (hse == NULL)
1708 return -1;
1709
Jiri Olsad7b617f2016-03-09 11:04:17 +01001710 perf_hpp_list__register_sort_field(list, &hse->hpp);
Namhyung Kim8b536992014-03-03 11:46:55 +09001711 return 0;
1712}
1713
Jiri Olsad7b617f2016-03-09 11:04:17 +01001714static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1715 struct perf_hpp_list *list)
Namhyung Kima7d945b2014-03-04 10:46:34 +09001716{
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001717 struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001718
1719 if (hse == NULL)
1720 return -1;
1721
Jiri Olsa07600022016-01-18 10:24:16 +01001722 perf_hpp_list__column_register(list, &hse->hpp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09001723 return 0;
1724}
1725
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001726struct hpp_dynamic_entry {
1727 struct perf_hpp_fmt hpp;
1728 struct perf_evsel *evsel;
1729 struct format_field *field;
1730 unsigned dynamic_len;
Namhyung Kim053a3982015-12-23 02:07:05 +09001731 bool raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001732};
1733
1734static int hde_width(struct hpp_dynamic_entry *hde)
1735{
1736 if (!hde->hpp.len) {
1737 int len = hde->dynamic_len;
1738 int namelen = strlen(hde->field->name);
1739 int fieldlen = hde->field->size;
1740
1741 if (namelen > len)
1742 len = namelen;
1743
1744 if (!(hde->field->flags & FIELD_IS_STRING)) {
1745 /* length for print hex numbers */
1746 fieldlen = hde->field->size * 2 + 2;
1747 }
1748 if (fieldlen > len)
1749 len = fieldlen;
1750
1751 hde->hpp.len = len;
1752 }
1753 return hde->hpp.len;
1754}
1755
Namhyung Kim60517d22015-12-23 02:07:03 +09001756static void update_dynamic_len(struct hpp_dynamic_entry *hde,
1757 struct hist_entry *he)
1758{
1759 char *str, *pos;
1760 struct format_field *field = hde->field;
1761 size_t namelen;
1762 bool last = false;
1763
Namhyung Kim053a3982015-12-23 02:07:05 +09001764 if (hde->raw_trace)
1765 return;
1766
Namhyung Kim60517d22015-12-23 02:07:03 +09001767 /* parse pretty print result and update max length */
1768 if (!he->trace_output)
1769 he->trace_output = get_trace_output(he);
1770
1771 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 size_t len;
1783
1784 str += namelen + 1;
1785 len = pos - str;
1786
1787 if (len > hde->dynamic_len)
1788 hde->dynamic_len = len;
1789 break;
1790 }
1791
1792 if (last)
1793 str = NULL;
1794 else
1795 str = pos + 1;
1796 }
1797}
1798
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001799static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsa74bb43f2016-08-07 17:28:27 +02001800 struct hists *hists __maybe_unused,
1801 int line __maybe_unused)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001802{
1803 struct hpp_dynamic_entry *hde;
1804 size_t len = fmt->user_len;
1805
1806 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1807
1808 if (!len)
1809 len = hde_width(hde);
1810
1811 return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
1812}
1813
1814static int __sort__hde_width(struct perf_hpp_fmt *fmt,
1815 struct perf_hpp *hpp __maybe_unused,
Jiri Olsada1b0402016-06-14 20:19:20 +02001816 struct hists *hists __maybe_unused)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001817{
1818 struct hpp_dynamic_entry *hde;
1819 size_t len = fmt->user_len;
1820
1821 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1822
1823 if (!len)
1824 len = hde_width(hde);
1825
1826 return len;
1827}
1828
Namhyung Kim361459f2015-12-23 02:07:08 +09001829bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
1830{
1831 struct hpp_dynamic_entry *hde;
1832
1833 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1834
1835 return hists_to_evsel(hists) == hde->evsel;
1836}
1837
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001838static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1839 struct hist_entry *he)
1840{
1841 struct hpp_dynamic_entry *hde;
1842 size_t len = fmt->user_len;
Namhyung Kim60517d22015-12-23 02:07:03 +09001843 char *str, *pos;
1844 struct format_field *field;
1845 size_t namelen;
1846 bool last = false;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001847 int ret;
1848
1849 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1850
1851 if (!len)
1852 len = hde_width(hde);
1853
Namhyung Kim053a3982015-12-23 02:07:05 +09001854 if (hde->raw_trace)
1855 goto raw_field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001856
Namhyung Kime049d4a2016-02-27 03:52:46 +09001857 if (!he->trace_output)
1858 he->trace_output = get_trace_output(he);
1859
Namhyung Kim053a3982015-12-23 02:07:05 +09001860 field = hde->field;
Namhyung Kim60517d22015-12-23 02:07:03 +09001861 namelen = strlen(field->name);
1862 str = he->trace_output;
1863
1864 while (str) {
1865 pos = strchr(str, ' ');
1866 if (pos == NULL) {
1867 last = true;
1868 pos = str + strlen(str);
1869 }
1870
1871 if (!strncmp(str, field->name, namelen)) {
1872 str += namelen + 1;
1873 str = strndup(str, pos - str);
1874
1875 if (str == NULL)
1876 return scnprintf(hpp->buf, hpp->size,
1877 "%*.*s", len, len, "ERROR");
1878 break;
1879 }
1880
1881 if (last)
1882 str = NULL;
1883 else
1884 str = pos + 1;
1885 }
1886
1887 if (str == NULL) {
1888 struct trace_seq seq;
Namhyung Kim053a3982015-12-23 02:07:05 +09001889raw_field:
Namhyung Kim60517d22015-12-23 02:07:03 +09001890 trace_seq_init(&seq);
1891 pevent_print_field(&seq, he->raw_data, hde->field);
1892 str = seq.buffer;
1893 }
1894
1895 ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
1896 free(str);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001897 return ret;
1898}
1899
1900static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
1901 struct hist_entry *a, struct hist_entry *b)
1902{
1903 struct hpp_dynamic_entry *hde;
1904 struct format_field *field;
1905 unsigned offset, size;
1906
1907 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1908
Namhyung Kimabab5e72016-02-27 03:52:47 +09001909 if (b == NULL) {
1910 update_dynamic_len(hde, a);
1911 return 0;
1912 }
1913
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001914 field = hde->field;
1915 if (field->flags & FIELD_IS_DYNAMIC) {
1916 unsigned long long dyn;
1917
1918 pevent_read_number_field(field, a->raw_data, &dyn);
1919 offset = dyn & 0xffff;
1920 size = (dyn >> 16) & 0xffff;
1921
1922 /* record max width for output */
1923 if (size > hde->dynamic_len)
1924 hde->dynamic_len = size;
1925 } else {
1926 offset = field->offset;
1927 size = field->size;
1928 }
1929
1930 return memcmp(a->raw_data + offset, b->raw_data + offset, size);
1931}
1932
Namhyung Kim361459f2015-12-23 02:07:08 +09001933bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
1934{
1935 return fmt->cmp == __sort__hde_cmp;
1936}
1937
Namhyung Kim665aa752016-02-21 23:22:35 +09001938static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1939{
1940 struct hpp_dynamic_entry *hde_a;
1941 struct hpp_dynamic_entry *hde_b;
1942
1943 if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
1944 return false;
1945
1946 hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
1947 hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
1948
1949 return hde_a->field == hde_b->field;
1950}
1951
Jiri Olsa564132f2016-01-18 10:24:09 +01001952static void hde_free(struct perf_hpp_fmt *fmt)
1953{
1954 struct hpp_dynamic_entry *hde;
1955
1956 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
1957 free(hde);
1958}
1959
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001960static struct hpp_dynamic_entry *
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001961__alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
1962 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001963{
1964 struct hpp_dynamic_entry *hde;
1965
1966 hde = malloc(sizeof(*hde));
1967 if (hde == NULL) {
1968 pr_debug("Memory allocation failed\n");
1969 return NULL;
1970 }
1971
1972 hde->evsel = evsel;
1973 hde->field = field;
1974 hde->dynamic_len = 0;
1975
1976 hde->hpp.name = field->name;
1977 hde->hpp.header = __sort__hde_header;
1978 hde->hpp.width = __sort__hde_width;
1979 hde->hpp.entry = __sort__hde_entry;
1980 hde->hpp.color = NULL;
1981
1982 hde->hpp.cmp = __sort__hde_cmp;
1983 hde->hpp.collapse = __sort__hde_cmp;
1984 hde->hpp.sort = __sort__hde_cmp;
Namhyung Kim665aa752016-02-21 23:22:35 +09001985 hde->hpp.equal = __sort__hde_equal;
Jiri Olsa564132f2016-01-18 10:24:09 +01001986 hde->hpp.free = hde_free;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001987
1988 INIT_LIST_HEAD(&hde->hpp.list);
1989 INIT_LIST_HEAD(&hde->hpp.sort_list);
1990 hde->hpp.elide = false;
1991 hde->hpp.len = 0;
1992 hde->hpp.user_len = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03001993 hde->hpp.level = level;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09001994
1995 return hde;
1996}
1997
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03001998struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
1999{
2000 struct perf_hpp_fmt *new_fmt = NULL;
2001
2002 if (perf_hpp__is_sort_entry(fmt)) {
2003 struct hpp_sort_entry *hse, *new_hse;
2004
2005 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2006 new_hse = memdup(hse, sizeof(*hse));
2007 if (new_hse)
2008 new_fmt = &new_hse->hpp;
2009 } else if (perf_hpp__is_dynamic_entry(fmt)) {
2010 struct hpp_dynamic_entry *hde, *new_hde;
2011
2012 hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2013 new_hde = memdup(hde, sizeof(*hde));
2014 if (new_hde)
2015 new_fmt = &new_hde->hpp;
2016 } else {
2017 new_fmt = memdup(fmt, sizeof(*fmt));
2018 }
2019
2020 INIT_LIST_HEAD(&new_fmt->list);
2021 INIT_LIST_HEAD(&new_fmt->sort_list);
2022
2023 return new_fmt;
2024}
2025
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002026static int parse_field_name(char *str, char **event, char **field, char **opt)
2027{
2028 char *event_name, *field_name, *opt_name;
2029
2030 event_name = str;
2031 field_name = strchr(str, '.');
2032
2033 if (field_name) {
2034 *field_name++ = '\0';
2035 } else {
2036 event_name = NULL;
2037 field_name = str;
2038 }
2039
2040 opt_name = strchr(field_name, '/');
2041 if (opt_name)
2042 *opt_name++ = '\0';
2043
2044 *event = event_name;
2045 *field = field_name;
2046 *opt = opt_name;
2047
2048 return 0;
2049}
2050
2051/* find match evsel using a given event name. The event name can be:
Namhyung Kim9735be22016-01-05 19:58:35 +09002052 * 1. '%' + event index (e.g. '%1' for first event)
2053 * 2. full event name (e.g. sched:sched_switch)
2054 * 3. partial event name (should not contain ':')
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002055 */
2056static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
2057{
2058 struct perf_evsel *evsel = NULL;
2059 struct perf_evsel *pos;
2060 bool full_name;
2061
2062 /* case 1 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002063 if (event_name[0] == '%') {
2064 int nr = strtol(event_name+1, NULL, 0);
2065
2066 if (nr > evlist->nr_entries)
2067 return NULL;
2068
2069 evsel = perf_evlist__first(evlist);
2070 while (--nr > 0)
2071 evsel = perf_evsel__next(evsel);
2072
2073 return evsel;
2074 }
2075
2076 full_name = !!strchr(event_name, ':');
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002077 evlist__for_each_entry(evlist, pos) {
Namhyung Kim9735be22016-01-05 19:58:35 +09002078 /* case 2 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002079 if (full_name && !strcmp(pos->name, event_name))
2080 return pos;
Namhyung Kim9735be22016-01-05 19:58:35 +09002081 /* case 3 */
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002082 if (!full_name && strstr(pos->name, event_name)) {
2083 if (evsel) {
2084 pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2085 event_name, evsel->name, pos->name);
2086 return NULL;
2087 }
2088 evsel = pos;
2089 }
2090 }
2091
2092 return evsel;
2093}
2094
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002095static int __dynamic_dimension__add(struct perf_evsel *evsel,
2096 struct format_field *field,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002097 bool raw_trace, int level)
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002098{
2099 struct hpp_dynamic_entry *hde;
2100
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002101 hde = __alloc_dynamic_entry(evsel, field, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002102 if (hde == NULL)
2103 return -ENOMEM;
2104
2105 hde->raw_trace = raw_trace;
2106
2107 perf_hpp__register_sort_field(&hde->hpp);
2108 return 0;
2109}
2110
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002111static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002112{
2113 int ret;
2114 struct format_field *field;
2115
2116 field = evsel->tp_format->format.fields;
2117 while (field) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002118 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002119 if (ret < 0)
2120 return ret;
2121
2122 field = field->next;
2123 }
2124 return 0;
2125}
2126
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002127static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
2128 int level)
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002129{
2130 int ret;
2131 struct perf_evsel *evsel;
2132
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002133 evlist__for_each_entry(evlist, evsel) {
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002134 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2135 continue;
2136
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002137 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002138 if (ret < 0)
2139 return ret;
2140 }
2141 return 0;
2142}
2143
Namhyung Kim9735be22016-01-05 19:58:35 +09002144static int add_all_matching_fields(struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002145 char *field_name, bool raw_trace, int level)
Namhyung Kim9735be22016-01-05 19:58:35 +09002146{
2147 int ret = -ESRCH;
2148 struct perf_evsel *evsel;
2149 struct format_field *field;
2150
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002151 evlist__for_each_entry(evlist, evsel) {
Namhyung Kim9735be22016-01-05 19:58:35 +09002152 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
2153 continue;
2154
2155 field = pevent_find_any_field(evsel->tp_format, field_name);
2156 if (field == NULL)
2157 continue;
2158
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002159 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002160 if (ret < 0)
2161 break;
2162 }
2163 return ret;
2164}
2165
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002166static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
2167 int level)
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002168{
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002169 char *str, *event_name, *field_name, *opt_name;
2170 struct perf_evsel *evsel;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002171 struct format_field *field;
Namhyung Kim053a3982015-12-23 02:07:05 +09002172 bool raw_trace = symbol_conf.raw_trace;
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002173 int ret = 0;
2174
2175 if (evlist == NULL)
2176 return -ENOENT;
2177
2178 str = strdup(tok);
2179 if (str == NULL)
2180 return -ENOMEM;
2181
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002182 if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002183 ret = -EINVAL;
2184 goto out;
2185 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002186
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002187 if (opt_name) {
2188 if (strcmp(opt_name, "raw")) {
2189 pr_debug("unsupported field option %s\n", opt_name);
Namhyung Kim053a3982015-12-23 02:07:05 +09002190 ret = -EINVAL;
2191 goto out;
2192 }
2193 raw_trace = true;
2194 }
2195
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002196 if (!strcmp(field_name, "trace_fields")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002197 ret = add_all_dynamic_fields(evlist, raw_trace, level);
Namhyung Kim2e422fd2015-12-23 02:07:09 +09002198 goto out;
2199 }
2200
Namhyung Kim9735be22016-01-05 19:58:35 +09002201 if (event_name == NULL) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002202 ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
Namhyung Kim9735be22016-01-05 19:58:35 +09002203 goto out;
2204 }
2205
Namhyung Kim5d0cff92015-12-23 02:07:06 +09002206 evsel = find_evsel(evlist, event_name);
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002207 if (evsel == NULL) {
2208 pr_debug("Cannot find event: %s\n", event_name);
2209 ret = -ENOENT;
2210 goto out;
2211 }
2212
2213 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2214 pr_debug("%s is not a tracepoint event\n", event_name);
2215 ret = -EINVAL;
2216 goto out;
2217 }
2218
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002219 if (!strcmp(field_name, "*")) {
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002220 ret = add_evsel_fields(evsel, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002221 } else {
2222 field = pevent_find_any_field(evsel->tp_format, field_name);
2223 if (field == NULL) {
2224 pr_debug("Cannot find event field for %s.%s\n",
2225 event_name, field_name);
2226 return -ENOENT;
2227 }
2228
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002229 ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
Namhyung Kim3b099bf52015-12-23 02:07:07 +09002230 }
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002231
2232out:
2233 free(str);
2234 return ret;
2235}
2236
Jiri Olsad7b617f2016-03-09 11:04:17 +01002237static int __sort_dimension__add(struct sort_dimension *sd,
2238 struct perf_hpp_list *list,
2239 int level)
Namhyung Kim2f532d092013-04-03 21:26:10 +09002240{
2241 if (sd->taken)
Namhyung Kim8b536992014-03-03 11:46:55 +09002242 return 0;
2243
Jiri Olsad7b617f2016-03-09 11:04:17 +01002244 if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
Namhyung Kim8b536992014-03-03 11:46:55 +09002245 return -1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002246
2247 if (sd->entry->se_collapse)
Jiri Olsa52225032016-05-03 13:54:42 +02002248 list->need_collapse = 1;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002249
Namhyung Kim2f532d092013-04-03 21:26:10 +09002250 sd->taken = 1;
Namhyung Kim8b536992014-03-03 11:46:55 +09002251
2252 return 0;
Namhyung Kim2f532d092013-04-03 21:26:10 +09002253}
2254
Jiri Olsad7b617f2016-03-09 11:04:17 +01002255static int __hpp_dimension__add(struct hpp_dimension *hd,
2256 struct perf_hpp_list *list,
2257 int level)
Namhyung Kima2ce0672014-03-04 09:06:42 +09002258{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002259 struct perf_hpp_fmt *fmt;
Namhyung Kima2ce0672014-03-04 09:06:42 +09002260
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002261 if (hd->taken)
2262 return 0;
2263
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002264 fmt = __hpp_dimension__alloc_hpp(hd, level);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002265 if (!fmt)
2266 return -1;
2267
2268 hd->taken = 1;
Jiri Olsad7b617f2016-03-09 11:04:17 +01002269 perf_hpp_list__register_sort_field(list, fmt);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002270 return 0;
2271}
2272
Jiri Olsa07600022016-01-18 10:24:16 +01002273static int __sort_dimension__add_output(struct perf_hpp_list *list,
2274 struct sort_dimension *sd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002275{
2276 if (sd->taken)
2277 return 0;
2278
Jiri Olsad7b617f2016-03-09 11:04:17 +01002279 if (__sort_dimension__add_hpp_output(sd, list) < 0)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002280 return -1;
2281
2282 sd->taken = 1;
2283 return 0;
2284}
2285
Jiri Olsa07600022016-01-18 10:24:16 +01002286static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2287 struct hpp_dimension *hd)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002288{
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002289 struct perf_hpp_fmt *fmt;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002290
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002291 if (hd->taken)
2292 return 0;
2293
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002294 fmt = __hpp_dimension__alloc_hpp(hd, 0);
Jiri Olsa1945c3e2016-01-18 10:24:07 +01002295 if (!fmt)
2296 return -1;
2297
2298 hd->taken = 1;
Jiri Olsa07600022016-01-18 10:24:16 +01002299 perf_hpp_list__column_register(list, fmt);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002300 return 0;
2301}
2302
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002303int hpp_dimension__add_output(unsigned col)
2304{
2305 BUG_ON(col >= PERF_HPP__MAX_INDEX);
Jiri Olsa07600022016-01-18 10:24:16 +01002306 return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
Jiri Olsabeeaaeb2015-10-06 14:25:11 +02002307}
2308
Jiri Olsad7b617f2016-03-09 11:04:17 +01002309static int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -03002310 struct perf_evlist *evlist,
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002311 int level)
John Kacurdd68ada2009-09-24 18:02:49 +02002312{
2313 unsigned int i;
2314
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002315 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2316 struct sort_dimension *sd = &common_sort_dimensions[i];
John Kacurdd68ada2009-09-24 18:02:49 +02002317
John Kacurdd68ada2009-09-24 18:02:49 +02002318 if (strncasecmp(tok, sd->name, strlen(tok)))
2319 continue;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002320
John Kacurdd68ada2009-09-24 18:02:49 +02002321 if (sd->entry == &sort_parent) {
2322 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2323 if (ret) {
2324 char err[BUFSIZ];
2325
2326 regerror(ret, &parent_regex, err, sizeof(err));
Arnaldo Carvalho de Melo2aefa4f2010-04-02 12:30:57 -03002327 pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2328 return -EINVAL;
John Kacurdd68ada2009-09-24 18:02:49 +02002329 }
Jiri Olsade7e6a72016-05-03 13:54:43 +02002330 list->parent = 1;
Namhyung Kim930477b2013-04-05 10:26:36 +09002331 } else if (sd->entry == &sort_sym) {
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002332 list->sym = 1;
Kan Liang94ba4622015-02-09 05:39:44 +00002333 /*
2334 * perf diff displays the performance difference amongst
2335 * two or more perf.data files. Those files could come
2336 * from different binaries. So we should not compare
2337 * their ips, but the name of symbol.
2338 */
2339 if (sort__mode == SORT_MODE__DIFF)
2340 sd->entry->se_collapse = sort__sym_sort;
2341
Namhyung Kim68f6d022013-12-18 14:21:10 +09002342 } else if (sd->entry == &sort_dso) {
Jiri Olsa69849fc2016-05-03 13:54:45 +02002343 list->dso = 1;
Kan Liang2e7ea3a2015-09-04 10:45:43 -04002344 } else if (sd->entry == &sort_socket) {
Jiri Olsa35a634f2016-05-03 13:54:46 +02002345 list->socket = 1;
Namhyung Kimcfd92da2016-01-21 19:13:24 -03002346 } else if (sd->entry == &sort_thread) {
Jiri Olsafa829112016-05-03 13:54:47 +02002347 list->thread = 1;
Namhyung Kim078b8d42016-03-09 23:20:51 +09002348 } else if (sd->entry == &sort_comm) {
Jiri Olsa7cecb7f2016-05-03 13:54:48 +02002349 list->comm = 1;
John Kacurdd68ada2009-09-24 18:02:49 +02002350 }
2351
Jiri Olsad7b617f2016-03-09 11:04:17 +01002352 return __sort_dimension__add(sd, list, level);
John Kacurdd68ada2009-09-24 18:02:49 +02002353 }
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002354
Namhyung Kima2ce0672014-03-04 09:06:42 +09002355 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2356 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2357
2358 if (strncasecmp(tok, hd->name, strlen(tok)))
2359 continue;
2360
Jiri Olsad7b617f2016-03-09 11:04:17 +01002361 return __hpp_dimension__add(hd, list, level);
Namhyung Kima2ce0672014-03-04 09:06:42 +09002362 }
2363
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002364 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2365 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2366
2367 if (strncasecmp(tok, sd->name, strlen(tok)))
2368 continue;
2369
Namhyung Kim55369fc2013-04-01 20:35:20 +09002370 if (sort__mode != SORT_MODE__BRANCH)
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002371 return -EINVAL;
2372
2373 if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002374 list->sym = 1;
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002375
Jiri Olsad7b617f2016-03-09 11:04:17 +01002376 __sort_dimension__add(sd, list, level);
Namhyung Kimfc5871e2012-12-27 18:11:46 +09002377 return 0;
2378 }
2379
Namhyung Kimafab87b2013-04-03 21:26:11 +09002380 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2381 struct sort_dimension *sd = &memory_sort_dimensions[i];
2382
2383 if (strncasecmp(tok, sd->name, strlen(tok)))
2384 continue;
2385
2386 if (sort__mode != SORT_MODE__MEMORY)
2387 return -EINVAL;
2388
Arnaldo Carvalho de Melo0d203162016-07-15 10:08:43 -03002389 if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
2390 return -EINVAL;
2391
Namhyung Kimafab87b2013-04-03 21:26:11 +09002392 if (sd->entry == &sort_mem_daddr_sym)
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002393 list->sym = 1;
Namhyung Kimafab87b2013-04-03 21:26:11 +09002394
Jiri Olsad7b617f2016-03-09 11:04:17 +01002395 __sort_dimension__add(sd, list, level);
Namhyung Kimafab87b2013-04-03 21:26:11 +09002396 return 0;
2397 }
2398
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002399 if (!add_dynamic_entry(evlist, tok, level))
Namhyung Kimc7c2a5e2015-12-23 02:07:02 +09002400 return 0;
2401
John Kacurdd68ada2009-09-24 18:02:49 +02002402 return -ESRCH;
2403}
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002404
Jiri Olsad7b617f2016-03-09 11:04:17 +01002405static int setup_sort_list(struct perf_hpp_list *list, char *str,
2406 struct perf_evlist *evlist)
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002407{
2408 char *tmp, *tok;
2409 int ret = 0;
Namhyung Kim4b633eb2016-03-07 16:44:43 -03002410 int level = 0;
Namhyung Kima23f37e2016-03-07 16:44:47 -03002411 int next_level = 1;
2412 bool in_group = false;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002413
Namhyung Kima23f37e2016-03-07 16:44:47 -03002414 do {
2415 tok = str;
2416 tmp = strpbrk(str, "{}, ");
2417 if (tmp) {
2418 if (in_group)
2419 next_level = level;
2420 else
2421 next_level = level + 1;
2422
2423 if (*tmp == '{')
2424 in_group = true;
2425 else if (*tmp == '}')
2426 in_group = false;
2427
2428 *tmp = '\0';
2429 str = tmp + 1;
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002430 }
Namhyung Kima23f37e2016-03-07 16:44:47 -03002431
2432 if (*tok) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002433 ret = sort_dimension__add(list, tok, evlist, level);
Namhyung Kima23f37e2016-03-07 16:44:47 -03002434 if (ret == -EINVAL) {
Arnaldo Carvalho de Melo0d203162016-07-15 10:08:43 -03002435 if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
2436 error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2437 else
2438 error("Invalid --sort key: `%s'", tok);
Namhyung Kima23f37e2016-03-07 16:44:47 -03002439 break;
2440 } else if (ret == -ESRCH) {
2441 error("Unknown --sort key: `%s'", tok);
2442 break;
2443 }
2444 }
2445
2446 level = next_level;
2447 } while (tmp);
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002448
2449 return ret;
2450}
2451
Namhyung Kimd49dade2015-12-23 02:07:10 +09002452static const char *get_default_sort_order(struct perf_evlist *evlist)
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002453{
2454 const char *default_sort_orders[] = {
2455 default_sort_order,
2456 default_branch_sort_order,
2457 default_mem_sort_order,
2458 default_top_sort_order,
2459 default_diff_sort_order,
Namhyung Kimd49dade2015-12-23 02:07:10 +09002460 default_tracepoint_sort_order,
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002461 };
Namhyung Kimd49dade2015-12-23 02:07:10 +09002462 bool use_trace = true;
2463 struct perf_evsel *evsel;
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002464
2465 BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2466
Namhyung Kimd49dade2015-12-23 02:07:10 +09002467 if (evlist == NULL)
2468 goto out_no_evlist;
2469
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002470 evlist__for_each_entry(evlist, evsel) {
Namhyung Kimd49dade2015-12-23 02:07:10 +09002471 if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2472 use_trace = false;
2473 break;
2474 }
2475 }
2476
2477 if (use_trace) {
2478 sort__mode = SORT_MODE__TRACEPOINT;
2479 if (symbol_conf.raw_trace)
2480 return "trace_fields";
2481 }
2482out_no_evlist:
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002483 return default_sort_orders[sort__mode];
2484}
2485
Namhyung Kimd49dade2015-12-23 02:07:10 +09002486static int setup_sort_order(struct perf_evlist *evlist)
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002487{
2488 char *new_sort_order;
2489
2490 /*
2491 * Append '+'-prefixed sort order to the default sort
2492 * order string.
2493 */
2494 if (!sort_order || is_strict_order(sort_order))
2495 return 0;
2496
2497 if (sort_order[1] == '\0') {
2498 error("Invalid --sort key: `+'");
2499 return -EINVAL;
2500 }
2501
2502 /*
2503 * We allocate new sort_order string, but we never free it,
2504 * because it's checked over the rest of the code.
2505 */
2506 if (asprintf(&new_sort_order, "%s,%s",
Namhyung Kimd49dade2015-12-23 02:07:10 +09002507 get_default_sort_order(evlist), sort_order + 1) < 0) {
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002508 error("Not enough memory to set up --sort");
2509 return -ENOMEM;
2510 }
2511
2512 sort_order = new_sort_order;
2513 return 0;
2514}
2515
Jiri Olsab97511c2016-01-07 10:14:08 +01002516/*
2517 * Adds 'pre,' prefix into 'str' is 'pre' is
2518 * not already part of 'str'.
2519 */
2520static char *prefix_if_not_in(const char *pre, char *str)
2521{
2522 char *n;
2523
2524 if (!str || strstr(str, pre))
2525 return str;
2526
2527 if (asprintf(&n, "%s,%s", pre, str) < 0)
2528 return NULL;
2529
2530 free(str);
2531 return n;
2532}
2533
2534static char *setup_overhead(char *keys)
2535{
Namhyung Kime9d848c2016-05-10 11:26:24 -03002536 if (sort__mode == SORT_MODE__DIFF)
2537 return keys;
2538
Jiri Olsab97511c2016-01-07 10:14:08 +01002539 keys = prefix_if_not_in("overhead", keys);
2540
2541 if (symbol_conf.cumulate_callchain)
2542 keys = prefix_if_not_in("overhead_children", keys);
2543
2544 return keys;
2545}
2546
Namhyung Kim40184c42015-12-23 02:07:01 +09002547static int __setup_sorting(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002548{
Jiri Olsa2fbaa392016-01-18 10:24:10 +01002549 char *str;
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002550 const char *sort_keys;
Namhyung Kim55309982013-02-06 14:57:16 +09002551 int ret = 0;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002552
Namhyung Kimd49dade2015-12-23 02:07:10 +09002553 ret = setup_sort_order(evlist);
Jiri Olsa1a1c0ff2014-08-23 14:59:48 +02002554 if (ret)
2555 return ret;
2556
2557 sort_keys = sort_order;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002558 if (sort_keys == NULL) {
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002559 if (is_strict_order(field_order)) {
Namhyung Kima7d945b2014-03-04 10:46:34 +09002560 /*
2561 * If user specified field order but no sort order,
2562 * we'll honor it and not add default sort orders.
2563 */
2564 return 0;
2565 }
2566
Namhyung Kimd49dade2015-12-23 02:07:10 +09002567 sort_keys = get_default_sort_order(evlist);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002568 }
Namhyung Kim512ae1b2014-03-18 11:31:39 +09002569
2570 str = strdup(sort_keys);
Namhyung Kim5936f542013-02-06 14:57:17 +09002571 if (str == NULL) {
2572 error("Not enough memory to setup sort keys");
2573 return -ENOMEM;
2574 }
2575
Jiri Olsab97511c2016-01-07 10:14:08 +01002576 /*
2577 * Prepend overhead fields for backward compatibility.
2578 */
2579 if (!is_strict_order(field_order)) {
2580 str = setup_overhead(str);
2581 if (str == NULL) {
2582 error("Not enough memory to setup overhead keys");
2583 return -ENOMEM;
2584 }
2585 }
2586
Jiri Olsad7b617f2016-03-09 11:04:17 +01002587 ret = setup_sort_list(&perf_hpp_list, str, evlist);
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002588
2589 free(str);
Namhyung Kim55309982013-02-06 14:57:16 +09002590 return ret;
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -02002591}
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002592
Jiri Olsaf2998422014-05-23 17:15:47 +02002593void perf_hpp__set_elide(int idx, bool elide)
Namhyung Kime67d49a2014-03-18 13:00:59 +09002594{
Jiri Olsaf2998422014-05-23 17:15:47 +02002595 struct perf_hpp_fmt *fmt;
2596 struct hpp_sort_entry *hse;
Namhyung Kime67d49a2014-03-18 13:00:59 +09002597
Jiri Olsacf094042016-01-18 10:24:17 +01002598 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002599 if (!perf_hpp__is_sort_entry(fmt))
2600 continue;
2601
2602 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2603 if (hse->se->se_width_idx == idx) {
2604 fmt->elide = elide;
2605 break;
2606 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002607 }
Namhyung Kime67d49a2014-03-18 13:00:59 +09002608}
2609
Jiri Olsaf2998422014-05-23 17:15:47 +02002610static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002611{
2612 if (list && strlist__nr_entries(list) == 1) {
2613 if (fp != NULL)
2614 fprintf(fp, "# %s: %s\n", list_name,
2615 strlist__entry(list, 0)->s);
Jiri Olsaf2998422014-05-23 17:15:47 +02002616 return true;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002617 }
Jiri Olsaf2998422014-05-23 17:15:47 +02002618 return false;
2619}
2620
2621static bool get_elide(int idx, FILE *output)
2622{
2623 switch (idx) {
2624 case HISTC_SYMBOL:
2625 return __get_elide(symbol_conf.sym_list, "symbol", output);
2626 case HISTC_DSO:
2627 return __get_elide(symbol_conf.dso_list, "dso", output);
2628 case HISTC_COMM:
2629 return __get_elide(symbol_conf.comm_list, "comm", output);
2630 default:
2631 break;
2632 }
2633
2634 if (sort__mode != SORT_MODE__BRANCH)
2635 return false;
2636
2637 switch (idx) {
2638 case HISTC_SYMBOL_FROM:
2639 return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2640 case HISTC_SYMBOL_TO:
2641 return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2642 case HISTC_DSO_FROM:
2643 return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2644 case HISTC_DSO_TO:
2645 return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2646 default:
2647 break;
2648 }
2649
2650 return false;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02002651}
Namhyung Kim08e71542013-04-03 21:26:19 +09002652
2653void sort__setup_elide(FILE *output)
2654{
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002655 struct perf_hpp_fmt *fmt;
2656 struct hpp_sort_entry *hse;
Namhyung Kim7524f632013-11-08 17:53:42 +09002657
Jiri Olsacf094042016-01-18 10:24:17 +01002658 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Jiri Olsaf2998422014-05-23 17:15:47 +02002659 if (!perf_hpp__is_sort_entry(fmt))
2660 continue;
Namhyung Kim08e71542013-04-03 21:26:19 +09002661
Jiri Olsaf2998422014-05-23 17:15:47 +02002662 hse = container_of(fmt, struct hpp_sort_entry, hpp);
2663 fmt->elide = get_elide(hse->se->se_width_idx, output);
Namhyung Kim08e71542013-04-03 21:26:19 +09002664 }
2665
Namhyung Kim7524f632013-11-08 17:53:42 +09002666 /*
2667 * It makes no sense to elide all of sort entries.
2668 * Just revert them to show up again.
2669 */
Jiri Olsacf094042016-01-18 10:24:17 +01002670 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002671 if (!perf_hpp__is_sort_entry(fmt))
2672 continue;
2673
Jiri Olsaf2998422014-05-23 17:15:47 +02002674 if (!fmt->elide)
Namhyung Kim7524f632013-11-08 17:53:42 +09002675 return;
2676 }
2677
Jiri Olsacf094042016-01-18 10:24:17 +01002678 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002679 if (!perf_hpp__is_sort_entry(fmt))
2680 continue;
2681
Jiri Olsaf2998422014-05-23 17:15:47 +02002682 fmt->elide = false;
Namhyung Kimcfaa1542014-05-19 14:19:30 +09002683 }
Namhyung Kim08e71542013-04-03 21:26:19 +09002684}
Namhyung Kima7d945b2014-03-04 10:46:34 +09002685
Jiri Olsa07600022016-01-18 10:24:16 +01002686static int output_field_add(struct perf_hpp_list *list, char *tok)
Namhyung Kima7d945b2014-03-04 10:46:34 +09002687{
2688 unsigned int i;
2689
2690 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2691 struct sort_dimension *sd = &common_sort_dimensions[i];
2692
2693 if (strncasecmp(tok, sd->name, strlen(tok)))
2694 continue;
2695
Jiri Olsa07600022016-01-18 10:24:16 +01002696 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002697 }
2698
2699 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2700 struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2701
2702 if (strncasecmp(tok, hd->name, strlen(tok)))
2703 continue;
2704
Jiri Olsa07600022016-01-18 10:24:16 +01002705 return __hpp_dimension__add_output(list, hd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002706 }
2707
2708 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2709 struct sort_dimension *sd = &bstack_sort_dimensions[i];
2710
2711 if (strncasecmp(tok, sd->name, strlen(tok)))
2712 continue;
2713
Jiri Olsa07600022016-01-18 10:24:16 +01002714 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002715 }
2716
2717 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2718 struct sort_dimension *sd = &memory_sort_dimensions[i];
2719
2720 if (strncasecmp(tok, sd->name, strlen(tok)))
2721 continue;
2722
Jiri Olsa07600022016-01-18 10:24:16 +01002723 return __sort_dimension__add_output(list, sd);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002724 }
2725
2726 return -ESRCH;
2727}
2728
Jiri Olsa07600022016-01-18 10:24:16 +01002729static int setup_output_list(struct perf_hpp_list *list, char *str)
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002730{
2731 char *tmp, *tok;
2732 int ret = 0;
2733
2734 for (tok = strtok_r(str, ", ", &tmp);
2735 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Jiri Olsa07600022016-01-18 10:24:16 +01002736 ret = output_field_add(list, tok);
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002737 if (ret == -EINVAL) {
2738 error("Invalid --fields key: `%s'", tok);
2739 break;
2740 } else if (ret == -ESRCH) {
2741 error("Unknown --fields key: `%s'", tok);
2742 break;
2743 }
2744 }
2745
2746 return ret;
2747}
2748
Namhyung Kima7d945b2014-03-04 10:46:34 +09002749static void reset_dimensions(void)
2750{
2751 unsigned int i;
2752
2753 for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
2754 common_sort_dimensions[i].taken = 0;
2755
2756 for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
2757 hpp_sort_dimensions[i].taken = 0;
2758
2759 for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
2760 bstack_sort_dimensions[i].taken = 0;
2761
2762 for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
2763 memory_sort_dimensions[i].taken = 0;
2764}
2765
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002766bool is_strict_order(const char *order)
2767{
2768 return order && (*order != '+');
2769}
2770
Namhyung Kima7d945b2014-03-04 10:46:34 +09002771static int __setup_output_field(void)
2772{
Jiri Olsa6d3375e2016-01-18 10:24:11 +01002773 char *str, *strp;
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002774 int ret = -EINVAL;
Namhyung Kima7d945b2014-03-04 10:46:34 +09002775
2776 if (field_order == NULL)
2777 return 0;
2778
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002779 strp = str = strdup(field_order);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002780 if (str == NULL) {
2781 error("Not enough memory to setup output fields");
2782 return -ENOMEM;
2783 }
2784
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002785 if (!is_strict_order(field_order))
2786 strp++;
2787
2788 if (!strlen(strp)) {
2789 error("Invalid --fields key: `+'");
2790 goto out;
2791 }
2792
Jiri Olsa07600022016-01-18 10:24:16 +01002793 ret = setup_output_list(&perf_hpp_list, strp);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002794
Jiri Olsa2f3f9bc2014-08-22 15:58:38 +02002795out:
Namhyung Kima7d945b2014-03-04 10:46:34 +09002796 free(str);
2797 return ret;
2798}
2799
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002800int setup_sorting(struct perf_evlist *evlist)
2801{
2802 int err;
2803
2804 err = __setup_sorting(evlist);
2805 if (err < 0)
2806 return err;
2807
2808 if (parent_pattern != default_parent_pattern) {
Jiri Olsad7b617f2016-03-09 11:04:17 +01002809 err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
Arnaldo Carvalho de Melo9b240632016-03-02 09:58:00 -03002810 if (err < 0)
2811 return err;
2812 }
2813
Namhyung Kima7d945b2014-03-04 10:46:34 +09002814 reset_dimensions();
2815
2816 /*
2817 * perf diff doesn't use default hpp output fields.
2818 */
2819 if (sort__mode != SORT_MODE__DIFF)
2820 perf_hpp__init();
2821
2822 err = __setup_output_field();
2823 if (err < 0)
2824 return err;
2825
2826 /* copy sort keys to output fields */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002827 perf_hpp__setup_output_field(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002828 /* and then copy output fields to sort keys */
Jiri Olsa43e0a682016-01-18 10:24:21 +01002829 perf_hpp__append_sort_keys(&perf_hpp_list);
Namhyung Kima7d945b2014-03-04 10:46:34 +09002830
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002831 /* setup hists-specific output fields */
2832 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
2833 return -1;
2834
Namhyung Kima7d945b2014-03-04 10:46:34 +09002835 return 0;
2836}
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002837
2838void reset_output_field(void)
2839{
Jiri Olsa52225032016-05-03 13:54:42 +02002840 perf_hpp_list.need_collapse = 0;
Jiri Olsade7e6a72016-05-03 13:54:43 +02002841 perf_hpp_list.parent = 0;
Jiri Olsa2e0453a2016-05-03 13:54:44 +02002842 perf_hpp_list.sym = 0;
Jiri Olsa69849fc2016-05-03 13:54:45 +02002843 perf_hpp_list.dso = 0;
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002844
Namhyung Kimd69b2962014-05-23 10:59:01 +09002845 field_order = NULL;
2846 sort_order = NULL;
2847
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002848 reset_dimensions();
Jiri Olsa43e0a682016-01-18 10:24:21 +01002849 perf_hpp__reset_output_field(&perf_hpp_list);
Namhyung Kim1c89fe92014-05-07 18:42:24 +09002850}