• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <regex.h>
5 #include <stdlib.h>
6 #include <linux/mman.h>
7 #include <linux/time64.h>
8 #include "debug.h"
9 #include "dso.h"
10 #include "sort.h"
11 #include "hist.h"
12 #include "cacheline.h"
13 #include "comm.h"
14 #include "map.h"
15 #include "symbol.h"
16 #include "map_symbol.h"
17 #include "branch.h"
18 #include "thread.h"
19 #include "evsel.h"
20 #include "evlist.h"
21 #include "srcline.h"
22 #include "strlist.h"
23 #include "strbuf.h"
24 #include <traceevent/event-parse.h>
25 #include "mem-events.h"
26 #include "annotate.h"
27 #include "time-utils.h"
28 #include <linux/kernel.h>
29 #include <linux/string.h>
30 
31 regex_t		parent_regex;
32 const char	default_parent_pattern[] = "^sys_|^do_page_fault";
33 const char	*parent_pattern = default_parent_pattern;
34 const char	*default_sort_order = "comm,dso,symbol";
35 const char	default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
36 const char	default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
37 const char	default_top_sort_order[] = "dso,symbol";
38 const char	default_diff_sort_order[] = "dso,symbol";
39 const char	default_tracepoint_sort_order[] = "trace";
40 const char	*sort_order;
41 const char	*field_order;
42 regex_t		ignore_callees_regex;
43 int		have_ignore_callees = 0;
44 enum sort_mode	sort__mode = SORT_MODE__NORMAL;
45 
46 /*
47  * Replaces all occurrences of a char used with the:
48  *
49  * -t, --field-separator
50  *
51  * option, that uses a special separator character and don't pad with spaces,
52  * replacing all occurrences of this separator in symbol names (and other
53  * output) with a '.' character, that thus it's the only non valid separator.
54 */
repsep_snprintf(char * bf,size_t size,const char * fmt,...)55 static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
56 {
57 	int n;
58 	va_list ap;
59 
60 	va_start(ap, fmt);
61 	n = vsnprintf(bf, size, fmt, ap);
62 	if (symbol_conf.field_sep && n > 0) {
63 		char *sep = bf;
64 
65 		while (1) {
66 			sep = strchr(sep, *symbol_conf.field_sep);
67 			if (sep == NULL)
68 				break;
69 			*sep = '.';
70 		}
71 	}
72 	va_end(ap);
73 
74 	if (n >= (int)size)
75 		return size - 1;
76 	return n;
77 }
78 
cmp_null(const void * l,const void * r)79 static int64_t cmp_null(const void *l, const void *r)
80 {
81 	if (!l && !r)
82 		return 0;
83 	else if (!l)
84 		return -1;
85 	else
86 		return 1;
87 }
88 
89 /* --sort pid */
90 
91 static int64_t
sort__thread_cmp(struct hist_entry * left,struct hist_entry * right)92 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
93 {
94 	return right->thread->tid - left->thread->tid;
95 }
96 
hist_entry__thread_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)97 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
98 				       size_t size, unsigned int width)
99 {
100 	const char *comm = thread__comm_str(he->thread);
101 
102 	width = max(7U, width) - 8;
103 	return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
104 			       width, width, comm ?: "");
105 }
106 
hist_entry__thread_filter(struct hist_entry * he,int type,const void * arg)107 static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
108 {
109 	const struct thread *th = arg;
110 
111 	if (type != HIST_FILTER__THREAD)
112 		return -1;
113 
114 	return th && he->thread != th;
115 }
116 
117 struct sort_entry sort_thread = {
118 	.se_header	= "    Pid:Command",
119 	.se_cmp		= sort__thread_cmp,
120 	.se_snprintf	= hist_entry__thread_snprintf,
121 	.se_filter	= hist_entry__thread_filter,
122 	.se_width_idx	= HISTC_THREAD,
123 };
124 
125 /* --sort comm */
126 
127 /*
128  * We can't use pointer comparison in functions below,
129  * because it gives different results based on pointer
130  * values, which could break some sorting assumptions.
131  */
132 static int64_t
sort__comm_cmp(struct hist_entry * left,struct hist_entry * right)133 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
134 {
135 	return strcmp(comm__str(right->comm), comm__str(left->comm));
136 }
137 
138 static int64_t
sort__comm_collapse(struct hist_entry * left,struct hist_entry * right)139 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
140 {
141 	return strcmp(comm__str(right->comm), comm__str(left->comm));
142 }
143 
144 static int64_t
sort__comm_sort(struct hist_entry * left,struct hist_entry * right)145 sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
146 {
147 	return strcmp(comm__str(right->comm), comm__str(left->comm));
148 }
149 
hist_entry__comm_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)150 static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
151 				     size_t size, unsigned int width)
152 {
153 	return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
154 }
155 
156 struct sort_entry sort_comm = {
157 	.se_header	= "Command",
158 	.se_cmp		= sort__comm_cmp,
159 	.se_collapse	= sort__comm_collapse,
160 	.se_sort	= sort__comm_sort,
161 	.se_snprintf	= hist_entry__comm_snprintf,
162 	.se_filter	= hist_entry__thread_filter,
163 	.se_width_idx	= HISTC_COMM,
164 };
165 
166 /* --sort dso */
167 
_sort__dso_cmp(struct map * map_l,struct map * map_r)168 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
169 {
170 	struct dso *dso_l = map_l ? map_l->dso : NULL;
171 	struct dso *dso_r = map_r ? map_r->dso : NULL;
172 	const char *dso_name_l, *dso_name_r;
173 
174 	if (!dso_l || !dso_r)
175 		return cmp_null(dso_r, dso_l);
176 
177 	if (verbose > 0) {
178 		dso_name_l = dso_l->long_name;
179 		dso_name_r = dso_r->long_name;
180 	} else {
181 		dso_name_l = dso_l->short_name;
182 		dso_name_r = dso_r->short_name;
183 	}
184 
185 	return strcmp(dso_name_l, dso_name_r);
186 }
187 
188 static int64_t
sort__dso_cmp(struct hist_entry * left,struct hist_entry * right)189 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
190 {
191 	return _sort__dso_cmp(right->ms.map, left->ms.map);
192 }
193 
_hist_entry__dso_snprintf(struct map * map,char * bf,size_t size,unsigned int width)194 static int _hist_entry__dso_snprintf(struct map *map, char *bf,
195 				     size_t size, unsigned int width)
196 {
197 	if (map && map->dso) {
198 		const char *dso_name = verbose > 0 ? map->dso->long_name :
199 			map->dso->short_name;
200 		return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
201 	}
202 
203 	return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
204 }
205 
hist_entry__dso_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)206 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
207 				    size_t size, unsigned int width)
208 {
209 	return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
210 }
211 
hist_entry__dso_filter(struct hist_entry * he,int type,const void * arg)212 static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
213 {
214 	const struct dso *dso = arg;
215 
216 	if (type != HIST_FILTER__DSO)
217 		return -1;
218 
219 	return dso && (!he->ms.map || he->ms.map->dso != dso);
220 }
221 
222 struct sort_entry sort_dso = {
223 	.se_header	= "Shared Object",
224 	.se_cmp		= sort__dso_cmp,
225 	.se_snprintf	= hist_entry__dso_snprintf,
226 	.se_filter	= hist_entry__dso_filter,
227 	.se_width_idx	= HISTC_DSO,
228 };
229 
230 /* --sort symbol */
231 
_sort__addr_cmp(u64 left_ip,u64 right_ip)232 static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
233 {
234 	return (int64_t)(right_ip - left_ip);
235 }
236 
_sort__sym_cmp(struct symbol * sym_l,struct symbol * sym_r)237 static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
238 {
239 	if (!sym_l || !sym_r)
240 		return cmp_null(sym_l, sym_r);
241 
242 	if (sym_l == sym_r)
243 		return 0;
244 
245 	if (sym_l->inlined || sym_r->inlined) {
246 		int ret = strcmp(sym_l->name, sym_r->name);
247 
248 		if (ret)
249 			return ret;
250 		if ((sym_l->start <= sym_r->end) && (sym_l->end >= sym_r->start))
251 			return 0;
252 	}
253 
254 	if (sym_l->start != sym_r->start)
255 		return (int64_t)(sym_r->start - sym_l->start);
256 
257 	return (int64_t)(sym_r->end - sym_l->end);
258 }
259 
260 static int64_t
sort__sym_cmp(struct hist_entry * left,struct hist_entry * right)261 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
262 {
263 	int64_t ret;
264 
265 	if (!left->ms.sym && !right->ms.sym)
266 		return _sort__addr_cmp(left->ip, right->ip);
267 
268 	/*
269 	 * comparing symbol address alone is not enough since it's a
270 	 * relative address within a dso.
271 	 */
272 	if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
273 		ret = sort__dso_cmp(left, right);
274 		if (ret != 0)
275 			return ret;
276 	}
277 
278 	return _sort__sym_cmp(left->ms.sym, right->ms.sym);
279 }
280 
281 static int64_t
sort__sym_sort(struct hist_entry * left,struct hist_entry * right)282 sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
283 {
284 	if (!left->ms.sym || !right->ms.sym)
285 		return cmp_null(left->ms.sym, right->ms.sym);
286 
287 	return strcmp(right->ms.sym->name, left->ms.sym->name);
288 }
289 
_hist_entry__sym_snprintf(struct map * map,struct symbol * sym,u64 ip,char level,char * bf,size_t size,unsigned int width)290 static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
291 				     u64 ip, char level, char *bf, size_t size,
292 				     unsigned int width)
293 {
294 	size_t ret = 0;
295 
296 	if (verbose > 0) {
297 		char o = map ? dso__symtab_origin(map->dso) : '!';
298 		ret += repsep_snprintf(bf, size, "%-#*llx %c ",
299 				       BITS_PER_LONG / 4 + 2, ip, o);
300 	}
301 
302 	ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
303 	if (sym && map) {
304 		if (sym->type == STT_OBJECT) {
305 			ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
306 			ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
307 					ip - map->unmap_ip(map, sym->start));
308 		} else {
309 			ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
310 					       width - ret,
311 					       sym->name);
312 			if (sym->inlined)
313 				ret += repsep_snprintf(bf + ret, size - ret,
314 						       " (inlined)");
315 		}
316 	} else {
317 		size_t len = BITS_PER_LONG / 4;
318 		ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
319 				       len, ip);
320 	}
321 
322 	return ret;
323 }
324 
hist_entry__sym_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)325 static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
326 				    size_t size, unsigned int width)
327 {
328 	return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
329 					 he->level, bf, size, width);
330 }
331 
hist_entry__sym_filter(struct hist_entry * he,int type,const void * arg)332 static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
333 {
334 	const char *sym = arg;
335 
336 	if (type != HIST_FILTER__SYMBOL)
337 		return -1;
338 
339 	return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
340 }
341 
342 struct sort_entry sort_sym = {
343 	.se_header	= "Symbol",
344 	.se_cmp		= sort__sym_cmp,
345 	.se_sort	= sort__sym_sort,
346 	.se_snprintf	= hist_entry__sym_snprintf,
347 	.se_filter	= hist_entry__sym_filter,
348 	.se_width_idx	= HISTC_SYMBOL,
349 };
350 
351 /* --sort srcline */
352 
hist_entry__srcline(struct hist_entry * he)353 char *hist_entry__srcline(struct hist_entry *he)
354 {
355 	return map__srcline(he->ms.map, he->ip, he->ms.sym);
356 }
357 
358 static int64_t
sort__srcline_cmp(struct hist_entry * left,struct hist_entry * right)359 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
360 {
361 	if (!left->srcline)
362 		left->srcline = hist_entry__srcline(left);
363 	if (!right->srcline)
364 		right->srcline = hist_entry__srcline(right);
365 
366 	return strcmp(right->srcline, left->srcline);
367 }
368 
hist_entry__srcline_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)369 static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
370 					size_t size, unsigned int width)
371 {
372 	if (!he->srcline)
373 		he->srcline = hist_entry__srcline(he);
374 
375 	return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
376 }
377 
378 struct sort_entry sort_srcline = {
379 	.se_header	= "Source:Line",
380 	.se_cmp		= sort__srcline_cmp,
381 	.se_snprintf	= hist_entry__srcline_snprintf,
382 	.se_width_idx	= HISTC_SRCLINE,
383 };
384 
385 /* --sort srcline_from */
386 
addr_map_symbol__srcline(struct addr_map_symbol * ams)387 static char *addr_map_symbol__srcline(struct addr_map_symbol *ams)
388 {
389 	return map__srcline(ams->map, ams->al_addr, ams->sym);
390 }
391 
392 static int64_t
sort__srcline_from_cmp(struct hist_entry * left,struct hist_entry * right)393 sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
394 {
395 	if (!left->branch_info->srcline_from)
396 		left->branch_info->srcline_from = addr_map_symbol__srcline(&left->branch_info->from);
397 
398 	if (!right->branch_info->srcline_from)
399 		right->branch_info->srcline_from = addr_map_symbol__srcline(&right->branch_info->from);
400 
401 	return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
402 }
403 
hist_entry__srcline_from_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)404 static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
405 					size_t size, unsigned int width)
406 {
407 	return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
408 }
409 
410 struct sort_entry sort_srcline_from = {
411 	.se_header	= "From Source:Line",
412 	.se_cmp		= sort__srcline_from_cmp,
413 	.se_snprintf	= hist_entry__srcline_from_snprintf,
414 	.se_width_idx	= HISTC_SRCLINE_FROM,
415 };
416 
417 /* --sort srcline_to */
418 
419 static int64_t
sort__srcline_to_cmp(struct hist_entry * left,struct hist_entry * right)420 sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
421 {
422 	if (!left->branch_info->srcline_to)
423 		left->branch_info->srcline_to = addr_map_symbol__srcline(&left->branch_info->to);
424 
425 	if (!right->branch_info->srcline_to)
426 		right->branch_info->srcline_to = addr_map_symbol__srcline(&right->branch_info->to);
427 
428 	return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
429 }
430 
hist_entry__srcline_to_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)431 static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
432 					size_t size, unsigned int width)
433 {
434 	return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
435 }
436 
437 struct sort_entry sort_srcline_to = {
438 	.se_header	= "To Source:Line",
439 	.se_cmp		= sort__srcline_to_cmp,
440 	.se_snprintf	= hist_entry__srcline_to_snprintf,
441 	.se_width_idx	= HISTC_SRCLINE_TO,
442 };
443 
hist_entry__sym_ipc_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)444 static int hist_entry__sym_ipc_snprintf(struct hist_entry *he, char *bf,
445 					size_t size, unsigned int width)
446 {
447 
448 	struct symbol *sym = he->ms.sym;
449 	struct annotation *notes;
450 	double ipc = 0.0, coverage = 0.0;
451 	char tmp[64];
452 
453 	if (!sym)
454 		return repsep_snprintf(bf, size, "%-*s", width, "-");
455 
456 	notes = symbol__annotation(sym);
457 
458 	if (notes->hit_cycles)
459 		ipc = notes->hit_insn / ((double)notes->hit_cycles);
460 
461 	if (notes->total_insn) {
462 		coverage = notes->cover_insn * 100.0 /
463 			((double)notes->total_insn);
464 	}
465 
466 	snprintf(tmp, sizeof(tmp), "%-5.2f [%5.1f%%]", ipc, coverage);
467 	return repsep_snprintf(bf, size, "%-*s", width, tmp);
468 }
469 
470 struct sort_entry sort_sym_ipc = {
471 	.se_header	= "IPC   [IPC Coverage]",
472 	.se_cmp		= sort__sym_cmp,
473 	.se_snprintf	= hist_entry__sym_ipc_snprintf,
474 	.se_width_idx	= HISTC_SYMBOL_IPC,
475 };
476 
hist_entry__sym_ipc_null_snprintf(struct hist_entry * he __maybe_unused,char * bf,size_t size,unsigned int width)477 static int hist_entry__sym_ipc_null_snprintf(struct hist_entry *he
478 					     __maybe_unused,
479 					     char *bf, size_t size,
480 					     unsigned int width)
481 {
482 	char tmp[64];
483 
484 	snprintf(tmp, sizeof(tmp), "%-5s %2s", "-", "-");
485 	return repsep_snprintf(bf, size, "%-*s", width, tmp);
486 }
487 
488 struct sort_entry sort_sym_ipc_null = {
489 	.se_header	= "IPC   [IPC Coverage]",
490 	.se_cmp		= sort__sym_cmp,
491 	.se_snprintf	= hist_entry__sym_ipc_null_snprintf,
492 	.se_width_idx	= HISTC_SYMBOL_IPC,
493 };
494 
495 /* --sort srcfile */
496 
497 static char no_srcfile[1];
498 
hist_entry__get_srcfile(struct hist_entry * e)499 static char *hist_entry__get_srcfile(struct hist_entry *e)
500 {
501 	char *sf, *p;
502 	struct map *map = e->ms.map;
503 
504 	if (!map)
505 		return no_srcfile;
506 
507 	sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
508 			 e->ms.sym, false, true, true, e->ip);
509 	if (!strcmp(sf, SRCLINE_UNKNOWN))
510 		return no_srcfile;
511 	p = strchr(sf, ':');
512 	if (p && *sf) {
513 		*p = 0;
514 		return sf;
515 	}
516 	free(sf);
517 	return no_srcfile;
518 }
519 
520 static int64_t
sort__srcfile_cmp(struct hist_entry * left,struct hist_entry * right)521 sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
522 {
523 	if (!left->srcfile)
524 		left->srcfile = hist_entry__get_srcfile(left);
525 	if (!right->srcfile)
526 		right->srcfile = hist_entry__get_srcfile(right);
527 
528 	return strcmp(right->srcfile, left->srcfile);
529 }
530 
hist_entry__srcfile_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)531 static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
532 					size_t size, unsigned int width)
533 {
534 	if (!he->srcfile)
535 		he->srcfile = hist_entry__get_srcfile(he);
536 
537 	return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
538 }
539 
540 struct sort_entry sort_srcfile = {
541 	.se_header	= "Source File",
542 	.se_cmp		= sort__srcfile_cmp,
543 	.se_snprintf	= hist_entry__srcfile_snprintf,
544 	.se_width_idx	= HISTC_SRCFILE,
545 };
546 
547 /* --sort parent */
548 
549 static int64_t
sort__parent_cmp(struct hist_entry * left,struct hist_entry * right)550 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
551 {
552 	struct symbol *sym_l = left->parent;
553 	struct symbol *sym_r = right->parent;
554 
555 	if (!sym_l || !sym_r)
556 		return cmp_null(sym_l, sym_r);
557 
558 	return strcmp(sym_r->name, sym_l->name);
559 }
560 
hist_entry__parent_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)561 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
562 				       size_t size, unsigned int width)
563 {
564 	return repsep_snprintf(bf, size, "%-*.*s", width, width,
565 			      he->parent ? he->parent->name : "[other]");
566 }
567 
568 struct sort_entry sort_parent = {
569 	.se_header	= "Parent symbol",
570 	.se_cmp		= sort__parent_cmp,
571 	.se_snprintf	= hist_entry__parent_snprintf,
572 	.se_width_idx	= HISTC_PARENT,
573 };
574 
575 /* --sort cpu */
576 
577 static int64_t
sort__cpu_cmp(struct hist_entry * left,struct hist_entry * right)578 sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
579 {
580 	return right->cpu - left->cpu;
581 }
582 
hist_entry__cpu_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)583 static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
584 				    size_t size, unsigned int width)
585 {
586 	return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
587 }
588 
589 struct sort_entry sort_cpu = {
590 	.se_header      = "CPU",
591 	.se_cmp	        = sort__cpu_cmp,
592 	.se_snprintf    = hist_entry__cpu_snprintf,
593 	.se_width_idx	= HISTC_CPU,
594 };
595 
596 /* --sort cgroup_id */
597 
_sort__cgroup_dev_cmp(u64 left_dev,u64 right_dev)598 static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
599 {
600 	return (int64_t)(right_dev - left_dev);
601 }
602 
_sort__cgroup_inode_cmp(u64 left_ino,u64 right_ino)603 static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
604 {
605 	return (int64_t)(right_ino - left_ino);
606 }
607 
608 static int64_t
sort__cgroup_id_cmp(struct hist_entry * left,struct hist_entry * right)609 sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
610 {
611 	int64_t ret;
612 
613 	ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
614 	if (ret != 0)
615 		return ret;
616 
617 	return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
618 				       left->cgroup_id.ino);
619 }
620 
hist_entry__cgroup_id_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width __maybe_unused)621 static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
622 					  char *bf, size_t size,
623 					  unsigned int width __maybe_unused)
624 {
625 	return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
626 			       he->cgroup_id.ino);
627 }
628 
629 struct sort_entry sort_cgroup_id = {
630 	.se_header      = "cgroup id (dev/inode)",
631 	.se_cmp	        = sort__cgroup_id_cmp,
632 	.se_snprintf    = hist_entry__cgroup_id_snprintf,
633 	.se_width_idx	= HISTC_CGROUP_ID,
634 };
635 
636 /* --sort socket */
637 
638 static int64_t
sort__socket_cmp(struct hist_entry * left,struct hist_entry * right)639 sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
640 {
641 	return right->socket - left->socket;
642 }
643 
hist_entry__socket_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)644 static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
645 				    size_t size, unsigned int width)
646 {
647 	return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
648 }
649 
hist_entry__socket_filter(struct hist_entry * he,int type,const void * arg)650 static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
651 {
652 	int sk = *(const int *)arg;
653 
654 	if (type != HIST_FILTER__SOCKET)
655 		return -1;
656 
657 	return sk >= 0 && he->socket != sk;
658 }
659 
660 struct sort_entry sort_socket = {
661 	.se_header      = "Socket",
662 	.se_cmp	        = sort__socket_cmp,
663 	.se_snprintf    = hist_entry__socket_snprintf,
664 	.se_filter      = hist_entry__socket_filter,
665 	.se_width_idx	= HISTC_SOCKET,
666 };
667 
668 /* --sort time */
669 
670 static int64_t
sort__time_cmp(struct hist_entry * left,struct hist_entry * right)671 sort__time_cmp(struct hist_entry *left, struct hist_entry *right)
672 {
673 	return right->time - left->time;
674 }
675 
hist_entry__time_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)676 static int hist_entry__time_snprintf(struct hist_entry *he, char *bf,
677 				    size_t size, unsigned int width)
678 {
679 	char he_time[32];
680 
681 	if (symbol_conf.nanosecs)
682 		timestamp__scnprintf_nsec(he->time, he_time,
683 					  sizeof(he_time));
684 	else
685 		timestamp__scnprintf_usec(he->time, he_time,
686 					  sizeof(he_time));
687 
688 	return repsep_snprintf(bf, size, "%-.*s", width, he_time);
689 }
690 
691 struct sort_entry sort_time = {
692 	.se_header      = "Time",
693 	.se_cmp	        = sort__time_cmp,
694 	.se_snprintf    = hist_entry__time_snprintf,
695 	.se_width_idx	= HISTC_TIME,
696 };
697 
698 /* --sort trace */
699 
get_trace_output(struct hist_entry * he)700 static char *get_trace_output(struct hist_entry *he)
701 {
702 	struct trace_seq seq;
703 	struct evsel *evsel;
704 	struct tep_record rec = {
705 		.data = he->raw_data,
706 		.size = he->raw_size,
707 	};
708 
709 	evsel = hists_to_evsel(he->hists);
710 
711 	trace_seq_init(&seq);
712 	if (symbol_conf.raw_trace) {
713 		tep_print_fields(&seq, he->raw_data, he->raw_size,
714 				 evsel->tp_format);
715 	} else {
716 		tep_print_event(evsel->tp_format->tep,
717 				&seq, &rec, "%s", TEP_PRINT_INFO);
718 	}
719 	/*
720 	 * Trim the buffer, it starts at 4KB and we're not going to
721 	 * add anything more to this buffer.
722 	 */
723 	return realloc(seq.buffer, seq.len + 1);
724 }
725 
726 static int64_t
sort__trace_cmp(struct hist_entry * left,struct hist_entry * right)727 sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
728 {
729 	struct evsel *evsel;
730 
731 	evsel = hists_to_evsel(left->hists);
732 	if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
733 		return 0;
734 
735 	if (left->trace_output == NULL)
736 		left->trace_output = get_trace_output(left);
737 	if (right->trace_output == NULL)
738 		right->trace_output = get_trace_output(right);
739 
740 	return strcmp(right->trace_output, left->trace_output);
741 }
742 
hist_entry__trace_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)743 static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
744 				    size_t size, unsigned int width)
745 {
746 	struct evsel *evsel;
747 
748 	evsel = hists_to_evsel(he->hists);
749 	if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
750 		return scnprintf(bf, size, "%-.*s", width, "N/A");
751 
752 	if (he->trace_output == NULL)
753 		he->trace_output = get_trace_output(he);
754 	return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
755 }
756 
757 struct sort_entry sort_trace = {
758 	.se_header      = "Trace output",
759 	.se_cmp	        = sort__trace_cmp,
760 	.se_snprintf    = hist_entry__trace_snprintf,
761 	.se_width_idx	= HISTC_TRACE,
762 };
763 
764 /* sort keys for branch stacks */
765 
766 static int64_t
sort__dso_from_cmp(struct hist_entry * left,struct hist_entry * right)767 sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
768 {
769 	if (!left->branch_info || !right->branch_info)
770 		return cmp_null(left->branch_info, right->branch_info);
771 
772 	return _sort__dso_cmp(left->branch_info->from.map,
773 			      right->branch_info->from.map);
774 }
775 
hist_entry__dso_from_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)776 static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
777 				    size_t size, unsigned int width)
778 {
779 	if (he->branch_info)
780 		return _hist_entry__dso_snprintf(he->branch_info->from.map,
781 						 bf, size, width);
782 	else
783 		return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
784 }
785 
hist_entry__dso_from_filter(struct hist_entry * he,int type,const void * arg)786 static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
787 				       const void *arg)
788 {
789 	const struct dso *dso = arg;
790 
791 	if (type != HIST_FILTER__DSO)
792 		return -1;
793 
794 	return dso && (!he->branch_info || !he->branch_info->from.map ||
795 		       he->branch_info->from.map->dso != dso);
796 }
797 
798 static int64_t
sort__dso_to_cmp(struct hist_entry * left,struct hist_entry * right)799 sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
800 {
801 	if (!left->branch_info || !right->branch_info)
802 		return cmp_null(left->branch_info, right->branch_info);
803 
804 	return _sort__dso_cmp(left->branch_info->to.map,
805 			      right->branch_info->to.map);
806 }
807 
hist_entry__dso_to_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)808 static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
809 				       size_t size, unsigned int width)
810 {
811 	if (he->branch_info)
812 		return _hist_entry__dso_snprintf(he->branch_info->to.map,
813 						 bf, size, width);
814 	else
815 		return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
816 }
817 
hist_entry__dso_to_filter(struct hist_entry * he,int type,const void * arg)818 static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
819 				     const void *arg)
820 {
821 	const struct dso *dso = arg;
822 
823 	if (type != HIST_FILTER__DSO)
824 		return -1;
825 
826 	return dso && (!he->branch_info || !he->branch_info->to.map ||
827 		       he->branch_info->to.map->dso != dso);
828 }
829 
830 static int64_t
sort__sym_from_cmp(struct hist_entry * left,struct hist_entry * right)831 sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
832 {
833 	struct addr_map_symbol *from_l, *from_r;
834 
835 	if (!left->branch_info || !right->branch_info)
836 		return cmp_null(left->branch_info, right->branch_info);
837 
838 	from_l = &left->branch_info->from;
839 	from_r = &right->branch_info->from;
840 
841 	if (!from_l->sym && !from_r->sym)
842 		return _sort__addr_cmp(from_l->addr, from_r->addr);
843 
844 	return _sort__sym_cmp(from_l->sym, from_r->sym);
845 }
846 
847 static int64_t
sort__sym_to_cmp(struct hist_entry * left,struct hist_entry * right)848 sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
849 {
850 	struct addr_map_symbol *to_l, *to_r;
851 
852 	if (!left->branch_info || !right->branch_info)
853 		return cmp_null(left->branch_info, right->branch_info);
854 
855 	to_l = &left->branch_info->to;
856 	to_r = &right->branch_info->to;
857 
858 	if (!to_l->sym && !to_r->sym)
859 		return _sort__addr_cmp(to_l->addr, to_r->addr);
860 
861 	return _sort__sym_cmp(to_l->sym, to_r->sym);
862 }
863 
hist_entry__sym_from_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)864 static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
865 					 size_t size, unsigned int width)
866 {
867 	if (he->branch_info) {
868 		struct addr_map_symbol *from = &he->branch_info->from;
869 
870 		return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
871 						 he->level, bf, size, width);
872 	}
873 
874 	return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
875 }
876 
hist_entry__sym_to_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)877 static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
878 				       size_t size, unsigned int width)
879 {
880 	if (he->branch_info) {
881 		struct addr_map_symbol *to = &he->branch_info->to;
882 
883 		return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
884 						 he->level, bf, size, width);
885 	}
886 
887 	return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
888 }
889 
hist_entry__sym_from_filter(struct hist_entry * he,int type,const void * arg)890 static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
891 				       const void *arg)
892 {
893 	const char *sym = arg;
894 
895 	if (type != HIST_FILTER__SYMBOL)
896 		return -1;
897 
898 	return sym && !(he->branch_info && he->branch_info->from.sym &&
899 			strstr(he->branch_info->from.sym->name, sym));
900 }
901 
hist_entry__sym_to_filter(struct hist_entry * he,int type,const void * arg)902 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
903 				       const void *arg)
904 {
905 	const char *sym = arg;
906 
907 	if (type != HIST_FILTER__SYMBOL)
908 		return -1;
909 
910 	return sym && !(he->branch_info && he->branch_info->to.sym &&
911 		        strstr(he->branch_info->to.sym->name, sym));
912 }
913 
914 struct sort_entry sort_dso_from = {
915 	.se_header	= "Source Shared Object",
916 	.se_cmp		= sort__dso_from_cmp,
917 	.se_snprintf	= hist_entry__dso_from_snprintf,
918 	.se_filter	= hist_entry__dso_from_filter,
919 	.se_width_idx	= HISTC_DSO_FROM,
920 };
921 
922 struct sort_entry sort_dso_to = {
923 	.se_header	= "Target Shared Object",
924 	.se_cmp		= sort__dso_to_cmp,
925 	.se_snprintf	= hist_entry__dso_to_snprintf,
926 	.se_filter	= hist_entry__dso_to_filter,
927 	.se_width_idx	= HISTC_DSO_TO,
928 };
929 
930 struct sort_entry sort_sym_from = {
931 	.se_header	= "Source Symbol",
932 	.se_cmp		= sort__sym_from_cmp,
933 	.se_snprintf	= hist_entry__sym_from_snprintf,
934 	.se_filter	= hist_entry__sym_from_filter,
935 	.se_width_idx	= HISTC_SYMBOL_FROM,
936 };
937 
938 struct sort_entry sort_sym_to = {
939 	.se_header	= "Target Symbol",
940 	.se_cmp		= sort__sym_to_cmp,
941 	.se_snprintf	= hist_entry__sym_to_snprintf,
942 	.se_filter	= hist_entry__sym_to_filter,
943 	.se_width_idx	= HISTC_SYMBOL_TO,
944 };
945 
946 static int64_t
sort__mispredict_cmp(struct hist_entry * left,struct hist_entry * right)947 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
948 {
949 	unsigned char mp, p;
950 
951 	if (!left->branch_info || !right->branch_info)
952 		return cmp_null(left->branch_info, right->branch_info);
953 
954 	mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
955 	p  = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
956 	return mp || p;
957 }
958 
hist_entry__mispredict_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)959 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
960 				    size_t size, unsigned int width){
961 	static const char *out = "N/A";
962 
963 	if (he->branch_info) {
964 		if (he->branch_info->flags.predicted)
965 			out = "N";
966 		else if (he->branch_info->flags.mispred)
967 			out = "Y";
968 	}
969 
970 	return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
971 }
972 
973 static int64_t
sort__cycles_cmp(struct hist_entry * left,struct hist_entry * right)974 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
975 {
976 	if (!left->branch_info || !right->branch_info)
977 		return cmp_null(left->branch_info, right->branch_info);
978 
979 	return left->branch_info->flags.cycles -
980 		right->branch_info->flags.cycles;
981 }
982 
hist_entry__cycles_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)983 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
984 				    size_t size, unsigned int width)
985 {
986 	if (!he->branch_info)
987 		return scnprintf(bf, size, "%-.*s", width, "N/A");
988 	if (he->branch_info->flags.cycles == 0)
989 		return repsep_snprintf(bf, size, "%-*s", width, "-");
990 	return repsep_snprintf(bf, size, "%-*hd", width,
991 			       he->branch_info->flags.cycles);
992 }
993 
994 struct sort_entry sort_cycles = {
995 	.se_header	= "Basic Block Cycles",
996 	.se_cmp		= sort__cycles_cmp,
997 	.se_snprintf	= hist_entry__cycles_snprintf,
998 	.se_width_idx	= HISTC_CYCLES,
999 };
1000 
1001 /* --sort daddr_sym */
1002 int64_t
sort__daddr_cmp(struct hist_entry * left,struct hist_entry * right)1003 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1004 {
1005 	uint64_t l = 0, r = 0;
1006 
1007 	if (left->mem_info)
1008 		l = left->mem_info->daddr.addr;
1009 	if (right->mem_info)
1010 		r = right->mem_info->daddr.addr;
1011 
1012 	return (int64_t)(r - l);
1013 }
1014 
hist_entry__daddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1015 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
1016 				    size_t size, unsigned int width)
1017 {
1018 	uint64_t addr = 0;
1019 	struct map *map = NULL;
1020 	struct symbol *sym = NULL;
1021 
1022 	if (he->mem_info) {
1023 		addr = he->mem_info->daddr.addr;
1024 		map = he->mem_info->daddr.map;
1025 		sym = he->mem_info->daddr.sym;
1026 	}
1027 	return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
1028 					 width);
1029 }
1030 
1031 int64_t
sort__iaddr_cmp(struct hist_entry * left,struct hist_entry * right)1032 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
1033 {
1034 	uint64_t l = 0, r = 0;
1035 
1036 	if (left->mem_info)
1037 		l = left->mem_info->iaddr.addr;
1038 	if (right->mem_info)
1039 		r = right->mem_info->iaddr.addr;
1040 
1041 	return (int64_t)(r - l);
1042 }
1043 
hist_entry__iaddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1044 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
1045 				    size_t size, unsigned int width)
1046 {
1047 	uint64_t addr = 0;
1048 	struct map *map = NULL;
1049 	struct symbol *sym = NULL;
1050 
1051 	if (he->mem_info) {
1052 		addr = he->mem_info->iaddr.addr;
1053 		map  = he->mem_info->iaddr.map;
1054 		sym  = he->mem_info->iaddr.sym;
1055 	}
1056 	return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
1057 					 width);
1058 }
1059 
1060 static int64_t
sort__dso_daddr_cmp(struct hist_entry * left,struct hist_entry * right)1061 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1062 {
1063 	struct map *map_l = NULL;
1064 	struct map *map_r = NULL;
1065 
1066 	if (left->mem_info)
1067 		map_l = left->mem_info->daddr.map;
1068 	if (right->mem_info)
1069 		map_r = right->mem_info->daddr.map;
1070 
1071 	return _sort__dso_cmp(map_l, map_r);
1072 }
1073 
hist_entry__dso_daddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1074 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
1075 				    size_t size, unsigned int width)
1076 {
1077 	struct map *map = NULL;
1078 
1079 	if (he->mem_info)
1080 		map = he->mem_info->daddr.map;
1081 
1082 	return _hist_entry__dso_snprintf(map, bf, size, width);
1083 }
1084 
1085 static int64_t
sort__locked_cmp(struct hist_entry * left,struct hist_entry * right)1086 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1087 {
1088 	union perf_mem_data_src data_src_l;
1089 	union perf_mem_data_src data_src_r;
1090 
1091 	if (left->mem_info)
1092 		data_src_l = left->mem_info->data_src;
1093 	else
1094 		data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1095 
1096 	if (right->mem_info)
1097 		data_src_r = right->mem_info->data_src;
1098 	else
1099 		data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1100 
1101 	return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1102 }
1103 
hist_entry__locked_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1104 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1105 				    size_t size, unsigned int width)
1106 {
1107 	char out[10];
1108 
1109 	perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1110 	return repsep_snprintf(bf, size, "%.*s", width, out);
1111 }
1112 
1113 static int64_t
sort__tlb_cmp(struct hist_entry * left,struct hist_entry * right)1114 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1115 {
1116 	union perf_mem_data_src data_src_l;
1117 	union perf_mem_data_src data_src_r;
1118 
1119 	if (left->mem_info)
1120 		data_src_l = left->mem_info->data_src;
1121 	else
1122 		data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1123 
1124 	if (right->mem_info)
1125 		data_src_r = right->mem_info->data_src;
1126 	else
1127 		data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1128 
1129 	return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1130 }
1131 
hist_entry__tlb_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1132 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1133 				    size_t size, unsigned int width)
1134 {
1135 	char out[64];
1136 
1137 	perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1138 	return repsep_snprintf(bf, size, "%-*s", width, out);
1139 }
1140 
1141 static int64_t
sort__lvl_cmp(struct hist_entry * left,struct hist_entry * right)1142 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1143 {
1144 	union perf_mem_data_src data_src_l;
1145 	union perf_mem_data_src data_src_r;
1146 
1147 	if (left->mem_info)
1148 		data_src_l = left->mem_info->data_src;
1149 	else
1150 		data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1151 
1152 	if (right->mem_info)
1153 		data_src_r = right->mem_info->data_src;
1154 	else
1155 		data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1156 
1157 	return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1158 }
1159 
hist_entry__lvl_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1160 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1161 				    size_t size, unsigned int width)
1162 {
1163 	char out[64];
1164 
1165 	perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1166 	return repsep_snprintf(bf, size, "%-*s", width, out);
1167 }
1168 
1169 static int64_t
sort__snoop_cmp(struct hist_entry * left,struct hist_entry * right)1170 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1171 {
1172 	union perf_mem_data_src data_src_l;
1173 	union perf_mem_data_src data_src_r;
1174 
1175 	if (left->mem_info)
1176 		data_src_l = left->mem_info->data_src;
1177 	else
1178 		data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1179 
1180 	if (right->mem_info)
1181 		data_src_r = right->mem_info->data_src;
1182 	else
1183 		data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1184 
1185 	return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1186 }
1187 
hist_entry__snoop_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1188 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1189 				    size_t size, unsigned int width)
1190 {
1191 	char out[64];
1192 
1193 	perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1194 	return repsep_snprintf(bf, size, "%-*s", width, out);
1195 }
1196 
1197 int64_t
sort__dcacheline_cmp(struct hist_entry * left,struct hist_entry * right)1198 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1199 {
1200 	u64 l, r;
1201 	struct map *l_map, *r_map;
1202 
1203 	if (!left->mem_info)  return -1;
1204 	if (!right->mem_info) return 1;
1205 
1206 	/* group event types together */
1207 	if (left->cpumode > right->cpumode) return -1;
1208 	if (left->cpumode < right->cpumode) return 1;
1209 
1210 	l_map = left->mem_info->daddr.map;
1211 	r_map = right->mem_info->daddr.map;
1212 
1213 	/* if both are NULL, jump to sort on al_addr instead */
1214 	if (!l_map && !r_map)
1215 		goto addr;
1216 
1217 	if (!l_map) return -1;
1218 	if (!r_map) return 1;
1219 
1220 	if (l_map->maj > r_map->maj) return -1;
1221 	if (l_map->maj < r_map->maj) return 1;
1222 
1223 	if (l_map->min > r_map->min) return -1;
1224 	if (l_map->min < r_map->min) return 1;
1225 
1226 	if (l_map->ino > r_map->ino) return -1;
1227 	if (l_map->ino < r_map->ino) return 1;
1228 
1229 	if (l_map->ino_generation > r_map->ino_generation) return -1;
1230 	if (l_map->ino_generation < r_map->ino_generation) return 1;
1231 
1232 	/*
1233 	 * Addresses with no major/minor numbers are assumed to be
1234 	 * anonymous in userspace.  Sort those on pid then address.
1235 	 *
1236 	 * The kernel and non-zero major/minor mapped areas are
1237 	 * assumed to be unity mapped.  Sort those on address.
1238 	 */
1239 
1240 	if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1241 	    (!(l_map->flags & MAP_SHARED)) &&
1242 	    !l_map->maj && !l_map->min && !l_map->ino &&
1243 	    !l_map->ino_generation) {
1244 		/* userspace anonymous */
1245 
1246 		if (left->thread->pid_ > right->thread->pid_) return -1;
1247 		if (left->thread->pid_ < right->thread->pid_) return 1;
1248 	}
1249 
1250 addr:
1251 	/* al_addr does all the right addr - start + offset calculations */
1252 	l = cl_address(left->mem_info->daddr.al_addr);
1253 	r = cl_address(right->mem_info->daddr.al_addr);
1254 
1255 	if (l > r) return -1;
1256 	if (l < r) return 1;
1257 
1258 	return 0;
1259 }
1260 
hist_entry__dcacheline_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1261 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1262 					  size_t size, unsigned int width)
1263 {
1264 
1265 	uint64_t addr = 0;
1266 	struct map *map = NULL;
1267 	struct symbol *sym = NULL;
1268 	char level = he->level;
1269 
1270 	if (he->mem_info) {
1271 		addr = cl_address(he->mem_info->daddr.al_addr);
1272 		map = he->mem_info->daddr.map;
1273 		sym = he->mem_info->daddr.sym;
1274 
1275 		/* print [s] for shared data mmaps */
1276 		if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1277 		     map && !(map->prot & PROT_EXEC) &&
1278 		    (map->flags & MAP_SHARED) &&
1279 		    (map->maj || map->min || map->ino ||
1280 		     map->ino_generation))
1281 			level = 's';
1282 		else if (!map)
1283 			level = 'X';
1284 	}
1285 	return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
1286 					 width);
1287 }
1288 
1289 struct sort_entry sort_mispredict = {
1290 	.se_header	= "Branch Mispredicted",
1291 	.se_cmp		= sort__mispredict_cmp,
1292 	.se_snprintf	= hist_entry__mispredict_snprintf,
1293 	.se_width_idx	= HISTC_MISPREDICT,
1294 };
1295 
he_weight(struct hist_entry * he)1296 static u64 he_weight(struct hist_entry *he)
1297 {
1298 	return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
1299 }
1300 
1301 static int64_t
sort__local_weight_cmp(struct hist_entry * left,struct hist_entry * right)1302 sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1303 {
1304 	return he_weight(left) - he_weight(right);
1305 }
1306 
hist_entry__local_weight_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1307 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1308 				    size_t size, unsigned int width)
1309 {
1310 	return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
1311 }
1312 
1313 struct sort_entry sort_local_weight = {
1314 	.se_header	= "Local Weight",
1315 	.se_cmp		= sort__local_weight_cmp,
1316 	.se_snprintf	= hist_entry__local_weight_snprintf,
1317 	.se_width_idx	= HISTC_LOCAL_WEIGHT,
1318 };
1319 
1320 static int64_t
sort__global_weight_cmp(struct hist_entry * left,struct hist_entry * right)1321 sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
1322 {
1323 	return left->stat.weight - right->stat.weight;
1324 }
1325 
hist_entry__global_weight_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1326 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1327 					      size_t size, unsigned int width)
1328 {
1329 	return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
1330 }
1331 
1332 struct sort_entry sort_global_weight = {
1333 	.se_header	= "Weight",
1334 	.se_cmp		= sort__global_weight_cmp,
1335 	.se_snprintf	= hist_entry__global_weight_snprintf,
1336 	.se_width_idx	= HISTC_GLOBAL_WEIGHT,
1337 };
1338 
1339 struct sort_entry sort_mem_daddr_sym = {
1340 	.se_header	= "Data Symbol",
1341 	.se_cmp		= sort__daddr_cmp,
1342 	.se_snprintf	= hist_entry__daddr_snprintf,
1343 	.se_width_idx	= HISTC_MEM_DADDR_SYMBOL,
1344 };
1345 
1346 struct sort_entry sort_mem_iaddr_sym = {
1347 	.se_header	= "Code Symbol",
1348 	.se_cmp		= sort__iaddr_cmp,
1349 	.se_snprintf	= hist_entry__iaddr_snprintf,
1350 	.se_width_idx	= HISTC_MEM_IADDR_SYMBOL,
1351 };
1352 
1353 struct sort_entry sort_mem_daddr_dso = {
1354 	.se_header	= "Data Object",
1355 	.se_cmp		= sort__dso_daddr_cmp,
1356 	.se_snprintf	= hist_entry__dso_daddr_snprintf,
1357 	.se_width_idx	= HISTC_MEM_DADDR_DSO,
1358 };
1359 
1360 struct sort_entry sort_mem_locked = {
1361 	.se_header	= "Locked",
1362 	.se_cmp		= sort__locked_cmp,
1363 	.se_snprintf	= hist_entry__locked_snprintf,
1364 	.se_width_idx	= HISTC_MEM_LOCKED,
1365 };
1366 
1367 struct sort_entry sort_mem_tlb = {
1368 	.se_header	= "TLB access",
1369 	.se_cmp		= sort__tlb_cmp,
1370 	.se_snprintf	= hist_entry__tlb_snprintf,
1371 	.se_width_idx	= HISTC_MEM_TLB,
1372 };
1373 
1374 struct sort_entry sort_mem_lvl = {
1375 	.se_header	= "Memory access",
1376 	.se_cmp		= sort__lvl_cmp,
1377 	.se_snprintf	= hist_entry__lvl_snprintf,
1378 	.se_width_idx	= HISTC_MEM_LVL,
1379 };
1380 
1381 struct sort_entry sort_mem_snoop = {
1382 	.se_header	= "Snoop",
1383 	.se_cmp		= sort__snoop_cmp,
1384 	.se_snprintf	= hist_entry__snoop_snprintf,
1385 	.se_width_idx	= HISTC_MEM_SNOOP,
1386 };
1387 
1388 struct sort_entry sort_mem_dcacheline = {
1389 	.se_header	= "Data Cacheline",
1390 	.se_cmp		= sort__dcacheline_cmp,
1391 	.se_snprintf	= hist_entry__dcacheline_snprintf,
1392 	.se_width_idx	= HISTC_MEM_DCACHELINE,
1393 };
1394 
1395 static int64_t
sort__phys_daddr_cmp(struct hist_entry * left,struct hist_entry * right)1396 sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1397 {
1398 	uint64_t l = 0, r = 0;
1399 
1400 	if (left->mem_info)
1401 		l = left->mem_info->daddr.phys_addr;
1402 	if (right->mem_info)
1403 		r = right->mem_info->daddr.phys_addr;
1404 
1405 	return (int64_t)(r - l);
1406 }
1407 
hist_entry__phys_daddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1408 static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
1409 					   size_t size, unsigned int width)
1410 {
1411 	uint64_t addr = 0;
1412 	size_t ret = 0;
1413 	size_t len = BITS_PER_LONG / 4;
1414 
1415 	addr = he->mem_info->daddr.phys_addr;
1416 
1417 	ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
1418 
1419 	ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
1420 
1421 	ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
1422 
1423 	if (ret > width)
1424 		bf[width] = '\0';
1425 
1426 	return width;
1427 }
1428 
1429 struct sort_entry sort_mem_phys_daddr = {
1430 	.se_header	= "Data Physical Address",
1431 	.se_cmp		= sort__phys_daddr_cmp,
1432 	.se_snprintf	= hist_entry__phys_daddr_snprintf,
1433 	.se_width_idx	= HISTC_MEM_PHYS_DADDR,
1434 };
1435 
1436 static int64_t
sort__abort_cmp(struct hist_entry * left,struct hist_entry * right)1437 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1438 {
1439 	if (!left->branch_info || !right->branch_info)
1440 		return cmp_null(left->branch_info, right->branch_info);
1441 
1442 	return left->branch_info->flags.abort !=
1443 		right->branch_info->flags.abort;
1444 }
1445 
hist_entry__abort_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1446 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1447 				    size_t size, unsigned int width)
1448 {
1449 	static const char *out = "N/A";
1450 
1451 	if (he->branch_info) {
1452 		if (he->branch_info->flags.abort)
1453 			out = "A";
1454 		else
1455 			out = ".";
1456 	}
1457 
1458 	return repsep_snprintf(bf, size, "%-*s", width, out);
1459 }
1460 
1461 struct sort_entry sort_abort = {
1462 	.se_header	= "Transaction abort",
1463 	.se_cmp		= sort__abort_cmp,
1464 	.se_snprintf	= hist_entry__abort_snprintf,
1465 	.se_width_idx	= HISTC_ABORT,
1466 };
1467 
1468 static int64_t
sort__in_tx_cmp(struct hist_entry * left,struct hist_entry * right)1469 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1470 {
1471 	if (!left->branch_info || !right->branch_info)
1472 		return cmp_null(left->branch_info, right->branch_info);
1473 
1474 	return left->branch_info->flags.in_tx !=
1475 		right->branch_info->flags.in_tx;
1476 }
1477 
hist_entry__in_tx_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1478 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1479 				    size_t size, unsigned int width)
1480 {
1481 	static const char *out = "N/A";
1482 
1483 	if (he->branch_info) {
1484 		if (he->branch_info->flags.in_tx)
1485 			out = "T";
1486 		else
1487 			out = ".";
1488 	}
1489 
1490 	return repsep_snprintf(bf, size, "%-*s", width, out);
1491 }
1492 
1493 struct sort_entry sort_in_tx = {
1494 	.se_header	= "Branch in transaction",
1495 	.se_cmp		= sort__in_tx_cmp,
1496 	.se_snprintf	= hist_entry__in_tx_snprintf,
1497 	.se_width_idx	= HISTC_IN_TX,
1498 };
1499 
1500 static int64_t
sort__transaction_cmp(struct hist_entry * left,struct hist_entry * right)1501 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1502 {
1503 	return left->transaction - right->transaction;
1504 }
1505 
add_str(char * p,const char * str)1506 static inline char *add_str(char *p, const char *str)
1507 {
1508 	strcpy(p, str);
1509 	return p + strlen(str);
1510 }
1511 
1512 static struct txbit {
1513 	unsigned flag;
1514 	const char *name;
1515 	int skip_for_len;
1516 } txbits[] = {
1517 	{ PERF_TXN_ELISION,        "EL ",        0 },
1518 	{ PERF_TXN_TRANSACTION,    "TX ",        1 },
1519 	{ PERF_TXN_SYNC,           "SYNC ",      1 },
1520 	{ PERF_TXN_ASYNC,          "ASYNC ",     0 },
1521 	{ PERF_TXN_RETRY,          "RETRY ",     0 },
1522 	{ PERF_TXN_CONFLICT,       "CON ",       0 },
1523 	{ PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1524 	{ PERF_TXN_CAPACITY_READ,  "CAP-READ ",  0 },
1525 	{ 0, NULL, 0 }
1526 };
1527 
hist_entry__transaction_len(void)1528 int hist_entry__transaction_len(void)
1529 {
1530 	int i;
1531 	int len = 0;
1532 
1533 	for (i = 0; txbits[i].name; i++) {
1534 		if (!txbits[i].skip_for_len)
1535 			len += strlen(txbits[i].name);
1536 	}
1537 	len += 4; /* :XX<space> */
1538 	return len;
1539 }
1540 
hist_entry__transaction_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1541 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1542 					    size_t size, unsigned int width)
1543 {
1544 	u64 t = he->transaction;
1545 	char buf[128];
1546 	char *p = buf;
1547 	int i;
1548 
1549 	buf[0] = 0;
1550 	for (i = 0; txbits[i].name; i++)
1551 		if (txbits[i].flag & t)
1552 			p = add_str(p, txbits[i].name);
1553 	if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1554 		p = add_str(p, "NEITHER ");
1555 	if (t & PERF_TXN_ABORT_MASK) {
1556 		sprintf(p, ":%" PRIx64,
1557 			(t & PERF_TXN_ABORT_MASK) >>
1558 			PERF_TXN_ABORT_SHIFT);
1559 		p += strlen(p);
1560 	}
1561 
1562 	return repsep_snprintf(bf, size, "%-*s", width, buf);
1563 }
1564 
1565 struct sort_entry sort_transaction = {
1566 	.se_header	= "Transaction                ",
1567 	.se_cmp		= sort__transaction_cmp,
1568 	.se_snprintf	= hist_entry__transaction_snprintf,
1569 	.se_width_idx	= HISTC_TRANSACTION,
1570 };
1571 
1572 /* --sort symbol_size */
1573 
_sort__sym_size_cmp(struct symbol * sym_l,struct symbol * sym_r)1574 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1575 {
1576 	int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1577 	int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1578 
1579 	return size_l < size_r ? -1 :
1580 		size_l == size_r ? 0 : 1;
1581 }
1582 
1583 static int64_t
sort__sym_size_cmp(struct hist_entry * left,struct hist_entry * right)1584 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1585 {
1586 	return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1587 }
1588 
_hist_entry__sym_size_snprintf(struct symbol * sym,char * bf,size_t bf_size,unsigned int width)1589 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1590 					  size_t bf_size, unsigned int width)
1591 {
1592 	if (sym)
1593 		return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1594 
1595 	return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1596 }
1597 
hist_entry__sym_size_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1598 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1599 					 size_t size, unsigned int width)
1600 {
1601 	return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1602 }
1603 
1604 struct sort_entry sort_sym_size = {
1605 	.se_header	= "Symbol size",
1606 	.se_cmp		= sort__sym_size_cmp,
1607 	.se_snprintf	= hist_entry__sym_size_snprintf,
1608 	.se_width_idx	= HISTC_SYM_SIZE,
1609 };
1610 
1611 /* --sort dso_size */
1612 
_sort__dso_size_cmp(struct map * map_l,struct map * map_r)1613 static int64_t _sort__dso_size_cmp(struct map *map_l, struct map *map_r)
1614 {
1615 	int64_t size_l = map_l != NULL ? map__size(map_l) : 0;
1616 	int64_t size_r = map_r != NULL ? map__size(map_r) : 0;
1617 
1618 	return size_l < size_r ? -1 :
1619 		size_l == size_r ? 0 : 1;
1620 }
1621 
1622 static int64_t
sort__dso_size_cmp(struct hist_entry * left,struct hist_entry * right)1623 sort__dso_size_cmp(struct hist_entry *left, struct hist_entry *right)
1624 {
1625 	return _sort__dso_size_cmp(right->ms.map, left->ms.map);
1626 }
1627 
_hist_entry__dso_size_snprintf(struct map * map,char * bf,size_t bf_size,unsigned int width)1628 static int _hist_entry__dso_size_snprintf(struct map *map, char *bf,
1629 					  size_t bf_size, unsigned int width)
1630 {
1631 	if (map && map->dso)
1632 		return repsep_snprintf(bf, bf_size, "%*d", width,
1633 				       map__size(map));
1634 
1635 	return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1636 }
1637 
hist_entry__dso_size_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1638 static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
1639 					 size_t size, unsigned int width)
1640 {
1641 	return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
1642 }
1643 
1644 struct sort_entry sort_dso_size = {
1645 	.se_header	= "DSO size",
1646 	.se_cmp		= sort__dso_size_cmp,
1647 	.se_snprintf	= hist_entry__dso_size_snprintf,
1648 	.se_width_idx	= HISTC_DSO_SIZE,
1649 };
1650 
1651 
1652 struct sort_dimension {
1653 	const char		*name;
1654 	struct sort_entry	*entry;
1655 	int			taken;
1656 };
1657 
1658 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
1659 
1660 static struct sort_dimension common_sort_dimensions[] = {
1661 	DIM(SORT_PID, "pid", sort_thread),
1662 	DIM(SORT_COMM, "comm", sort_comm),
1663 	DIM(SORT_DSO, "dso", sort_dso),
1664 	DIM(SORT_SYM, "symbol", sort_sym),
1665 	DIM(SORT_PARENT, "parent", sort_parent),
1666 	DIM(SORT_CPU, "cpu", sort_cpu),
1667 	DIM(SORT_SOCKET, "socket", sort_socket),
1668 	DIM(SORT_SRCLINE, "srcline", sort_srcline),
1669 	DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
1670 	DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
1671 	DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
1672 	DIM(SORT_TRANSACTION, "transaction", sort_transaction),
1673 	DIM(SORT_TRACE, "trace", sort_trace),
1674 	DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
1675 	DIM(SORT_DSO_SIZE, "dso_size", sort_dso_size),
1676 	DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
1677 	DIM(SORT_SYM_IPC_NULL, "ipc_null", sort_sym_ipc_null),
1678 	DIM(SORT_TIME, "time", sort_time),
1679 };
1680 
1681 #undef DIM
1682 
1683 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
1684 
1685 static struct sort_dimension bstack_sort_dimensions[] = {
1686 	DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
1687 	DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
1688 	DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
1689 	DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
1690 	DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
1691 	DIM(SORT_IN_TX, "in_tx", sort_in_tx),
1692 	DIM(SORT_ABORT, "abort", sort_abort),
1693 	DIM(SORT_CYCLES, "cycles", sort_cycles),
1694 	DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
1695 	DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
1696 	DIM(SORT_SYM_IPC, "ipc_lbr", sort_sym_ipc),
1697 };
1698 
1699 #undef DIM
1700 
1701 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
1702 
1703 static struct sort_dimension memory_sort_dimensions[] = {
1704 	DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
1705 	DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
1706 	DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
1707 	DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
1708 	DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
1709 	DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
1710 	DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
1711 	DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
1712 	DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
1713 };
1714 
1715 #undef DIM
1716 
1717 struct hpp_dimension {
1718 	const char		*name;
1719 	struct perf_hpp_fmt	*fmt;
1720 	int			taken;
1721 };
1722 
1723 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
1724 
1725 static struct hpp_dimension hpp_sort_dimensions[] = {
1726 	DIM(PERF_HPP__OVERHEAD, "overhead"),
1727 	DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
1728 	DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
1729 	DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
1730 	DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
1731 	DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
1732 	DIM(PERF_HPP__SAMPLES, "sample"),
1733 	DIM(PERF_HPP__PERIOD, "period"),
1734 };
1735 
1736 #undef DIM
1737 
1738 struct hpp_sort_entry {
1739 	struct perf_hpp_fmt hpp;
1740 	struct sort_entry *se;
1741 };
1742 
perf_hpp__reset_sort_width(struct perf_hpp_fmt * fmt,struct hists * hists)1743 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1744 {
1745 	struct hpp_sort_entry *hse;
1746 
1747 	if (!perf_hpp__is_sort_entry(fmt))
1748 		return;
1749 
1750 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1751 	hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
1752 }
1753 
__sort__hpp_header(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hists * hists,int line __maybe_unused,int * span __maybe_unused)1754 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1755 			      struct hists *hists, int line __maybe_unused,
1756 			      int *span __maybe_unused)
1757 {
1758 	struct hpp_sort_entry *hse;
1759 	size_t len = fmt->user_len;
1760 
1761 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1762 
1763 	if (!len)
1764 		len = hists__col_len(hists, hse->se->se_width_idx);
1765 
1766 	return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
1767 }
1768 
__sort__hpp_width(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp __maybe_unused,struct hists * hists)1769 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
1770 			     struct perf_hpp *hpp __maybe_unused,
1771 			     struct hists *hists)
1772 {
1773 	struct hpp_sort_entry *hse;
1774 	size_t len = fmt->user_len;
1775 
1776 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1777 
1778 	if (!len)
1779 		len = hists__col_len(hists, hse->se->se_width_idx);
1780 
1781 	return len;
1782 }
1783 
__sort__hpp_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)1784 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1785 			     struct hist_entry *he)
1786 {
1787 	struct hpp_sort_entry *hse;
1788 	size_t len = fmt->user_len;
1789 
1790 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1791 
1792 	if (!len)
1793 		len = hists__col_len(he->hists, hse->se->se_width_idx);
1794 
1795 	return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
1796 }
1797 
__sort__hpp_cmp(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)1798 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
1799 			       struct hist_entry *a, struct hist_entry *b)
1800 {
1801 	struct hpp_sort_entry *hse;
1802 
1803 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1804 	return hse->se->se_cmp(a, b);
1805 }
1806 
__sort__hpp_collapse(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)1807 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
1808 				    struct hist_entry *a, struct hist_entry *b)
1809 {
1810 	struct hpp_sort_entry *hse;
1811 	int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
1812 
1813 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1814 	collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
1815 	return collapse_fn(a, b);
1816 }
1817 
__sort__hpp_sort(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)1818 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
1819 				struct hist_entry *a, struct hist_entry *b)
1820 {
1821 	struct hpp_sort_entry *hse;
1822 	int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
1823 
1824 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1825 	sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
1826 	return sort_fn(a, b);
1827 }
1828 
perf_hpp__is_sort_entry(struct perf_hpp_fmt * format)1829 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
1830 {
1831 	return format->header == __sort__hpp_header;
1832 }
1833 
1834 #define MK_SORT_ENTRY_CHK(key)					\
1835 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt)	\
1836 {								\
1837 	struct hpp_sort_entry *hse;				\
1838 								\
1839 	if (!perf_hpp__is_sort_entry(fmt))			\
1840 		return false;					\
1841 								\
1842 	hse = container_of(fmt, struct hpp_sort_entry, hpp);	\
1843 	return hse->se == &sort_ ## key ;			\
1844 }
1845 
1846 MK_SORT_ENTRY_CHK(trace)
MK_SORT_ENTRY_CHK(srcline)1847 MK_SORT_ENTRY_CHK(srcline)
1848 MK_SORT_ENTRY_CHK(srcfile)
1849 MK_SORT_ENTRY_CHK(thread)
1850 MK_SORT_ENTRY_CHK(comm)
1851 MK_SORT_ENTRY_CHK(dso)
1852 MK_SORT_ENTRY_CHK(sym)
1853 
1854 
1855 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
1856 {
1857 	struct hpp_sort_entry *hse_a;
1858 	struct hpp_sort_entry *hse_b;
1859 
1860 	if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
1861 		return false;
1862 
1863 	hse_a = container_of(a, struct hpp_sort_entry, hpp);
1864 	hse_b = container_of(b, struct hpp_sort_entry, hpp);
1865 
1866 	return hse_a->se == hse_b->se;
1867 }
1868 
hse_free(struct perf_hpp_fmt * fmt)1869 static void hse_free(struct perf_hpp_fmt *fmt)
1870 {
1871 	struct hpp_sort_entry *hse;
1872 
1873 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
1874 	free(hse);
1875 }
1876 
1877 static struct hpp_sort_entry *
__sort_dimension__alloc_hpp(struct sort_dimension * sd,int level)1878 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
1879 {
1880 	struct hpp_sort_entry *hse;
1881 
1882 	hse = malloc(sizeof(*hse));
1883 	if (hse == NULL) {
1884 		pr_err("Memory allocation failed\n");
1885 		return NULL;
1886 	}
1887 
1888 	hse->se = sd->entry;
1889 	hse->hpp.name = sd->entry->se_header;
1890 	hse->hpp.header = __sort__hpp_header;
1891 	hse->hpp.width = __sort__hpp_width;
1892 	hse->hpp.entry = __sort__hpp_entry;
1893 	hse->hpp.color = NULL;
1894 
1895 	hse->hpp.cmp = __sort__hpp_cmp;
1896 	hse->hpp.collapse = __sort__hpp_collapse;
1897 	hse->hpp.sort = __sort__hpp_sort;
1898 	hse->hpp.equal = __sort__hpp_equal;
1899 	hse->hpp.free = hse_free;
1900 
1901 	INIT_LIST_HEAD(&hse->hpp.list);
1902 	INIT_LIST_HEAD(&hse->hpp.sort_list);
1903 	hse->hpp.elide = false;
1904 	hse->hpp.len = 0;
1905 	hse->hpp.user_len = 0;
1906 	hse->hpp.level = level;
1907 
1908 	return hse;
1909 }
1910 
hpp_free(struct perf_hpp_fmt * fmt)1911 static void hpp_free(struct perf_hpp_fmt *fmt)
1912 {
1913 	free(fmt);
1914 }
1915 
__hpp_dimension__alloc_hpp(struct hpp_dimension * hd,int level)1916 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
1917 						       int level)
1918 {
1919 	struct perf_hpp_fmt *fmt;
1920 
1921 	fmt = memdup(hd->fmt, sizeof(*fmt));
1922 	if (fmt) {
1923 		INIT_LIST_HEAD(&fmt->list);
1924 		INIT_LIST_HEAD(&fmt->sort_list);
1925 		fmt->free = hpp_free;
1926 		fmt->level = level;
1927 	}
1928 
1929 	return fmt;
1930 }
1931 
hist_entry__filter(struct hist_entry * he,int type,const void * arg)1932 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
1933 {
1934 	struct perf_hpp_fmt *fmt;
1935 	struct hpp_sort_entry *hse;
1936 	int ret = -1;
1937 	int r;
1938 
1939 	perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1940 		if (!perf_hpp__is_sort_entry(fmt))
1941 			continue;
1942 
1943 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
1944 		if (hse->se->se_filter == NULL)
1945 			continue;
1946 
1947 		/*
1948 		 * hist entry is filtered if any of sort key in the hpp list
1949 		 * is applied.  But it should skip non-matched filter types.
1950 		 */
1951 		r = hse->se->se_filter(he, type, arg);
1952 		if (r >= 0) {
1953 			if (ret < 0)
1954 				ret = 0;
1955 			ret |= r;
1956 		}
1957 	}
1958 
1959 	return ret;
1960 }
1961 
__sort_dimension__add_hpp_sort(struct sort_dimension * sd,struct perf_hpp_list * list,int level)1962 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
1963 					  struct perf_hpp_list *list,
1964 					  int level)
1965 {
1966 	struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
1967 
1968 	if (hse == NULL)
1969 		return -1;
1970 
1971 	perf_hpp_list__register_sort_field(list, &hse->hpp);
1972 	return 0;
1973 }
1974 
__sort_dimension__add_hpp_output(struct sort_dimension * sd,struct perf_hpp_list * list)1975 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
1976 					    struct perf_hpp_list *list)
1977 {
1978 	struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
1979 
1980 	if (hse == NULL)
1981 		return -1;
1982 
1983 	perf_hpp_list__column_register(list, &hse->hpp);
1984 	return 0;
1985 }
1986 
1987 struct hpp_dynamic_entry {
1988 	struct perf_hpp_fmt hpp;
1989 	struct evsel *evsel;
1990 	struct tep_format_field *field;
1991 	unsigned dynamic_len;
1992 	bool raw_trace;
1993 };
1994 
hde_width(struct hpp_dynamic_entry * hde)1995 static int hde_width(struct hpp_dynamic_entry *hde)
1996 {
1997 	if (!hde->hpp.len) {
1998 		int len = hde->dynamic_len;
1999 		int namelen = strlen(hde->field->name);
2000 		int fieldlen = hde->field->size;
2001 
2002 		if (namelen > len)
2003 			len = namelen;
2004 
2005 		if (!(hde->field->flags & TEP_FIELD_IS_STRING)) {
2006 			/* length for print hex numbers */
2007 			fieldlen = hde->field->size * 2 + 2;
2008 		}
2009 		if (fieldlen > len)
2010 			len = fieldlen;
2011 
2012 		hde->hpp.len = len;
2013 	}
2014 	return hde->hpp.len;
2015 }
2016 
update_dynamic_len(struct hpp_dynamic_entry * hde,struct hist_entry * he)2017 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
2018 			       struct hist_entry *he)
2019 {
2020 	char *str, *pos;
2021 	struct tep_format_field *field = hde->field;
2022 	size_t namelen;
2023 	bool last = false;
2024 
2025 	if (hde->raw_trace)
2026 		return;
2027 
2028 	/* parse pretty print result and update max length */
2029 	if (!he->trace_output)
2030 		he->trace_output = get_trace_output(he);
2031 
2032 	namelen = strlen(field->name);
2033 	str = he->trace_output;
2034 
2035 	while (str) {
2036 		pos = strchr(str, ' ');
2037 		if (pos == NULL) {
2038 			last = true;
2039 			pos = str + strlen(str);
2040 		}
2041 
2042 		if (!strncmp(str, field->name, namelen)) {
2043 			size_t len;
2044 
2045 			str += namelen + 1;
2046 			len = pos - str;
2047 
2048 			if (len > hde->dynamic_len)
2049 				hde->dynamic_len = len;
2050 			break;
2051 		}
2052 
2053 		if (last)
2054 			str = NULL;
2055 		else
2056 			str = pos + 1;
2057 	}
2058 }
2059 
__sort__hde_header(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hists * hists __maybe_unused,int line __maybe_unused,int * span __maybe_unused)2060 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2061 			      struct hists *hists __maybe_unused,
2062 			      int line __maybe_unused,
2063 			      int *span __maybe_unused)
2064 {
2065 	struct hpp_dynamic_entry *hde;
2066 	size_t len = fmt->user_len;
2067 
2068 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2069 
2070 	if (!len)
2071 		len = hde_width(hde);
2072 
2073 	return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
2074 }
2075 
__sort__hde_width(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp __maybe_unused,struct hists * hists __maybe_unused)2076 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
2077 			     struct perf_hpp *hpp __maybe_unused,
2078 			     struct hists *hists __maybe_unused)
2079 {
2080 	struct hpp_dynamic_entry *hde;
2081 	size_t len = fmt->user_len;
2082 
2083 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2084 
2085 	if (!len)
2086 		len = hde_width(hde);
2087 
2088 	return len;
2089 }
2090 
perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt * fmt,struct hists * hists)2091 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
2092 {
2093 	struct hpp_dynamic_entry *hde;
2094 
2095 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2096 
2097 	return hists_to_evsel(hists) == hde->evsel;
2098 }
2099 
__sort__hde_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)2100 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2101 			     struct hist_entry *he)
2102 {
2103 	struct hpp_dynamic_entry *hde;
2104 	size_t len = fmt->user_len;
2105 	char *str, *pos;
2106 	struct tep_format_field *field;
2107 	size_t namelen;
2108 	bool last = false;
2109 	int ret;
2110 
2111 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2112 
2113 	if (!len)
2114 		len = hde_width(hde);
2115 
2116 	if (hde->raw_trace)
2117 		goto raw_field;
2118 
2119 	if (!he->trace_output)
2120 		he->trace_output = get_trace_output(he);
2121 
2122 	field = hde->field;
2123 	namelen = strlen(field->name);
2124 	str = he->trace_output;
2125 
2126 	while (str) {
2127 		pos = strchr(str, ' ');
2128 		if (pos == NULL) {
2129 			last = true;
2130 			pos = str + strlen(str);
2131 		}
2132 
2133 		if (!strncmp(str, field->name, namelen)) {
2134 			str += namelen + 1;
2135 			str = strndup(str, pos - str);
2136 
2137 			if (str == NULL)
2138 				return scnprintf(hpp->buf, hpp->size,
2139 						 "%*.*s", len, len, "ERROR");
2140 			break;
2141 		}
2142 
2143 		if (last)
2144 			str = NULL;
2145 		else
2146 			str = pos + 1;
2147 	}
2148 
2149 	if (str == NULL) {
2150 		struct trace_seq seq;
2151 raw_field:
2152 		trace_seq_init(&seq);
2153 		tep_print_field(&seq, he->raw_data, hde->field);
2154 		str = seq.buffer;
2155 	}
2156 
2157 	ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
2158 	free(str);
2159 	return ret;
2160 }
2161 
__sort__hde_cmp(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)2162 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
2163 			       struct hist_entry *a, struct hist_entry *b)
2164 {
2165 	struct hpp_dynamic_entry *hde;
2166 	struct tep_format_field *field;
2167 	unsigned offset, size;
2168 
2169 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2170 
2171 	if (b == NULL) {
2172 		update_dynamic_len(hde, a);
2173 		return 0;
2174 	}
2175 
2176 	field = hde->field;
2177 	if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2178 		unsigned long long dyn;
2179 
2180 		tep_read_number_field(field, a->raw_data, &dyn);
2181 		offset = dyn & 0xffff;
2182 		size = (dyn >> 16) & 0xffff;
2183 
2184 		/* record max width for output */
2185 		if (size > hde->dynamic_len)
2186 			hde->dynamic_len = size;
2187 	} else {
2188 		offset = field->offset;
2189 		size = field->size;
2190 	}
2191 
2192 	return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2193 }
2194 
perf_hpp__is_dynamic_entry(struct perf_hpp_fmt * fmt)2195 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2196 {
2197 	return fmt->cmp == __sort__hde_cmp;
2198 }
2199 
__sort__hde_equal(struct perf_hpp_fmt * a,struct perf_hpp_fmt * b)2200 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2201 {
2202 	struct hpp_dynamic_entry *hde_a;
2203 	struct hpp_dynamic_entry *hde_b;
2204 
2205 	if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2206 		return false;
2207 
2208 	hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2209 	hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2210 
2211 	return hde_a->field == hde_b->field;
2212 }
2213 
hde_free(struct perf_hpp_fmt * fmt)2214 static void hde_free(struct perf_hpp_fmt *fmt)
2215 {
2216 	struct hpp_dynamic_entry *hde;
2217 
2218 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2219 	free(hde);
2220 }
2221 
2222 static struct hpp_dynamic_entry *
__alloc_dynamic_entry(struct evsel * evsel,struct tep_format_field * field,int level)2223 __alloc_dynamic_entry(struct evsel *evsel, struct tep_format_field *field,
2224 		      int level)
2225 {
2226 	struct hpp_dynamic_entry *hde;
2227 
2228 	hde = malloc(sizeof(*hde));
2229 	if (hde == NULL) {
2230 		pr_debug("Memory allocation failed\n");
2231 		return NULL;
2232 	}
2233 
2234 	hde->evsel = evsel;
2235 	hde->field = field;
2236 	hde->dynamic_len = 0;
2237 
2238 	hde->hpp.name = field->name;
2239 	hde->hpp.header = __sort__hde_header;
2240 	hde->hpp.width  = __sort__hde_width;
2241 	hde->hpp.entry  = __sort__hde_entry;
2242 	hde->hpp.color  = NULL;
2243 
2244 	hde->hpp.cmp = __sort__hde_cmp;
2245 	hde->hpp.collapse = __sort__hde_cmp;
2246 	hde->hpp.sort = __sort__hde_cmp;
2247 	hde->hpp.equal = __sort__hde_equal;
2248 	hde->hpp.free = hde_free;
2249 
2250 	INIT_LIST_HEAD(&hde->hpp.list);
2251 	INIT_LIST_HEAD(&hde->hpp.sort_list);
2252 	hde->hpp.elide = false;
2253 	hde->hpp.len = 0;
2254 	hde->hpp.user_len = 0;
2255 	hde->hpp.level = level;
2256 
2257 	return hde;
2258 }
2259 
perf_hpp_fmt__dup(struct perf_hpp_fmt * fmt)2260 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2261 {
2262 	struct perf_hpp_fmt *new_fmt = NULL;
2263 
2264 	if (perf_hpp__is_sort_entry(fmt)) {
2265 		struct hpp_sort_entry *hse, *new_hse;
2266 
2267 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
2268 		new_hse = memdup(hse, sizeof(*hse));
2269 		if (new_hse)
2270 			new_fmt = &new_hse->hpp;
2271 	} else if (perf_hpp__is_dynamic_entry(fmt)) {
2272 		struct hpp_dynamic_entry *hde, *new_hde;
2273 
2274 		hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2275 		new_hde = memdup(hde, sizeof(*hde));
2276 		if (new_hde)
2277 			new_fmt = &new_hde->hpp;
2278 	} else {
2279 		new_fmt = memdup(fmt, sizeof(*fmt));
2280 	}
2281 
2282 	INIT_LIST_HEAD(&new_fmt->list);
2283 	INIT_LIST_HEAD(&new_fmt->sort_list);
2284 
2285 	return new_fmt;
2286 }
2287 
parse_field_name(char * str,char ** event,char ** field,char ** opt)2288 static int parse_field_name(char *str, char **event, char **field, char **opt)
2289 {
2290 	char *event_name, *field_name, *opt_name;
2291 
2292 	event_name = str;
2293 	field_name = strchr(str, '.');
2294 
2295 	if (field_name) {
2296 		*field_name++ = '\0';
2297 	} else {
2298 		event_name = NULL;
2299 		field_name = str;
2300 	}
2301 
2302 	opt_name = strchr(field_name, '/');
2303 	if (opt_name)
2304 		*opt_name++ = '\0';
2305 
2306 	*event = event_name;
2307 	*field = field_name;
2308 	*opt   = opt_name;
2309 
2310 	return 0;
2311 }
2312 
2313 /* find match evsel using a given event name.  The event name can be:
2314  *   1. '%' + event index (e.g. '%1' for first event)
2315  *   2. full event name (e.g. sched:sched_switch)
2316  *   3. partial event name (should not contain ':')
2317  */
find_evsel(struct evlist * evlist,char * event_name)2318 static struct evsel *find_evsel(struct evlist *evlist, char *event_name)
2319 {
2320 	struct evsel *evsel = NULL;
2321 	struct evsel *pos;
2322 	bool full_name;
2323 
2324 	/* case 1 */
2325 	if (event_name[0] == '%') {
2326 		int nr = strtol(event_name+1, NULL, 0);
2327 
2328 		if (nr > evlist->core.nr_entries)
2329 			return NULL;
2330 
2331 		evsel = evlist__first(evlist);
2332 		while (--nr > 0)
2333 			evsel = perf_evsel__next(evsel);
2334 
2335 		return evsel;
2336 	}
2337 
2338 	full_name = !!strchr(event_name, ':');
2339 	evlist__for_each_entry(evlist, pos) {
2340 		/* case 2 */
2341 		if (full_name && !strcmp(pos->name, event_name))
2342 			return pos;
2343 		/* case 3 */
2344 		if (!full_name && strstr(pos->name, event_name)) {
2345 			if (evsel) {
2346 				pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2347 					 event_name, evsel->name, pos->name);
2348 				return NULL;
2349 			}
2350 			evsel = pos;
2351 		}
2352 	}
2353 
2354 	return evsel;
2355 }
2356 
__dynamic_dimension__add(struct evsel * evsel,struct tep_format_field * field,bool raw_trace,int level)2357 static int __dynamic_dimension__add(struct evsel *evsel,
2358 				    struct tep_format_field *field,
2359 				    bool raw_trace, int level)
2360 {
2361 	struct hpp_dynamic_entry *hde;
2362 
2363 	hde = __alloc_dynamic_entry(evsel, field, level);
2364 	if (hde == NULL)
2365 		return -ENOMEM;
2366 
2367 	hde->raw_trace = raw_trace;
2368 
2369 	perf_hpp__register_sort_field(&hde->hpp);
2370 	return 0;
2371 }
2372 
add_evsel_fields(struct evsel * evsel,bool raw_trace,int level)2373 static int add_evsel_fields(struct evsel *evsel, bool raw_trace, int level)
2374 {
2375 	int ret;
2376 	struct tep_format_field *field;
2377 
2378 	field = evsel->tp_format->format.fields;
2379 	while (field) {
2380 		ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2381 		if (ret < 0)
2382 			return ret;
2383 
2384 		field = field->next;
2385 	}
2386 	return 0;
2387 }
2388 
add_all_dynamic_fields(struct evlist * evlist,bool raw_trace,int level)2389 static int add_all_dynamic_fields(struct evlist *evlist, bool raw_trace,
2390 				  int level)
2391 {
2392 	int ret;
2393 	struct evsel *evsel;
2394 
2395 	evlist__for_each_entry(evlist, evsel) {
2396 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2397 			continue;
2398 
2399 		ret = add_evsel_fields(evsel, raw_trace, level);
2400 		if (ret < 0)
2401 			return ret;
2402 	}
2403 	return 0;
2404 }
2405 
add_all_matching_fields(struct evlist * evlist,char * field_name,bool raw_trace,int level)2406 static int add_all_matching_fields(struct evlist *evlist,
2407 				   char *field_name, bool raw_trace, int level)
2408 {
2409 	int ret = -ESRCH;
2410 	struct evsel *evsel;
2411 	struct tep_format_field *field;
2412 
2413 	evlist__for_each_entry(evlist, evsel) {
2414 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2415 			continue;
2416 
2417 		field = tep_find_any_field(evsel->tp_format, field_name);
2418 		if (field == NULL)
2419 			continue;
2420 
2421 		ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2422 		if (ret < 0)
2423 			break;
2424 	}
2425 	return ret;
2426 }
2427 
add_dynamic_entry(struct evlist * evlist,const char * tok,int level)2428 static int add_dynamic_entry(struct evlist *evlist, const char *tok,
2429 			     int level)
2430 {
2431 	char *str, *event_name, *field_name, *opt_name;
2432 	struct evsel *evsel;
2433 	struct tep_format_field *field;
2434 	bool raw_trace = symbol_conf.raw_trace;
2435 	int ret = 0;
2436 
2437 	if (evlist == NULL)
2438 		return -ENOENT;
2439 
2440 	str = strdup(tok);
2441 	if (str == NULL)
2442 		return -ENOMEM;
2443 
2444 	if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2445 		ret = -EINVAL;
2446 		goto out;
2447 	}
2448 
2449 	if (opt_name) {
2450 		if (strcmp(opt_name, "raw")) {
2451 			pr_debug("unsupported field option %s\n", opt_name);
2452 			ret = -EINVAL;
2453 			goto out;
2454 		}
2455 		raw_trace = true;
2456 	}
2457 
2458 	if (!strcmp(field_name, "trace_fields")) {
2459 		ret = add_all_dynamic_fields(evlist, raw_trace, level);
2460 		goto out;
2461 	}
2462 
2463 	if (event_name == NULL) {
2464 		ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2465 		goto out;
2466 	}
2467 
2468 	evsel = find_evsel(evlist, event_name);
2469 	if (evsel == NULL) {
2470 		pr_debug("Cannot find event: %s\n", event_name);
2471 		ret = -ENOENT;
2472 		goto out;
2473 	}
2474 
2475 	if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2476 		pr_debug("%s is not a tracepoint event\n", event_name);
2477 		ret = -EINVAL;
2478 		goto out;
2479 	}
2480 
2481 	if (!strcmp(field_name, "*")) {
2482 		ret = add_evsel_fields(evsel, raw_trace, level);
2483 	} else {
2484 		field = tep_find_any_field(evsel->tp_format, field_name);
2485 		if (field == NULL) {
2486 			pr_debug("Cannot find event field for %s.%s\n",
2487 				 event_name, field_name);
2488 			return -ENOENT;
2489 		}
2490 
2491 		ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2492 	}
2493 
2494 out:
2495 	free(str);
2496 	return ret;
2497 }
2498 
__sort_dimension__add(struct sort_dimension * sd,struct perf_hpp_list * list,int level)2499 static int __sort_dimension__add(struct sort_dimension *sd,
2500 				 struct perf_hpp_list *list,
2501 				 int level)
2502 {
2503 	if (sd->taken)
2504 		return 0;
2505 
2506 	if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2507 		return -1;
2508 
2509 	if (sd->entry->se_collapse)
2510 		list->need_collapse = 1;
2511 
2512 	sd->taken = 1;
2513 
2514 	return 0;
2515 }
2516 
__hpp_dimension__add(struct hpp_dimension * hd,struct perf_hpp_list * list,int level)2517 static int __hpp_dimension__add(struct hpp_dimension *hd,
2518 				struct perf_hpp_list *list,
2519 				int level)
2520 {
2521 	struct perf_hpp_fmt *fmt;
2522 
2523 	if (hd->taken)
2524 		return 0;
2525 
2526 	fmt = __hpp_dimension__alloc_hpp(hd, level);
2527 	if (!fmt)
2528 		return -1;
2529 
2530 	hd->taken = 1;
2531 	perf_hpp_list__register_sort_field(list, fmt);
2532 	return 0;
2533 }
2534 
__sort_dimension__add_output(struct perf_hpp_list * list,struct sort_dimension * sd)2535 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2536 					struct sort_dimension *sd)
2537 {
2538 	if (sd->taken)
2539 		return 0;
2540 
2541 	if (__sort_dimension__add_hpp_output(sd, list) < 0)
2542 		return -1;
2543 
2544 	sd->taken = 1;
2545 	return 0;
2546 }
2547 
__hpp_dimension__add_output(struct perf_hpp_list * list,struct hpp_dimension * hd)2548 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2549 				       struct hpp_dimension *hd)
2550 {
2551 	struct perf_hpp_fmt *fmt;
2552 
2553 	if (hd->taken)
2554 		return 0;
2555 
2556 	fmt = __hpp_dimension__alloc_hpp(hd, 0);
2557 	if (!fmt)
2558 		return -1;
2559 
2560 	hd->taken = 1;
2561 	perf_hpp_list__column_register(list, fmt);
2562 	return 0;
2563 }
2564 
hpp_dimension__add_output(unsigned col)2565 int hpp_dimension__add_output(unsigned col)
2566 {
2567 	BUG_ON(col >= PERF_HPP__MAX_INDEX);
2568 	return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2569 }
2570 
sort_dimension__add(struct perf_hpp_list * list,const char * tok,struct evlist * evlist,int level)2571 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2572 			struct evlist *evlist,
2573 			int level)
2574 {
2575 	unsigned int i;
2576 
2577 	for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2578 		struct sort_dimension *sd = &common_sort_dimensions[i];
2579 
2580 		if (strncasecmp(tok, sd->name, strlen(tok)))
2581 			continue;
2582 
2583 		if (sd->entry == &sort_parent) {
2584 			int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2585 			if (ret) {
2586 				char err[BUFSIZ];
2587 
2588 				regerror(ret, &parent_regex, err, sizeof(err));
2589 				pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2590 				return -EINVAL;
2591 			}
2592 			list->parent = 1;
2593 		} else if (sd->entry == &sort_sym) {
2594 			list->sym = 1;
2595 			/*
2596 			 * perf diff displays the performance difference amongst
2597 			 * two or more perf.data files. Those files could come
2598 			 * from different binaries. So we should not compare
2599 			 * their ips, but the name of symbol.
2600 			 */
2601 			if (sort__mode == SORT_MODE__DIFF)
2602 				sd->entry->se_collapse = sort__sym_sort;
2603 
2604 		} else if (sd->entry == &sort_dso) {
2605 			list->dso = 1;
2606 		} else if (sd->entry == &sort_socket) {
2607 			list->socket = 1;
2608 		} else if (sd->entry == &sort_thread) {
2609 			list->thread = 1;
2610 		} else if (sd->entry == &sort_comm) {
2611 			list->comm = 1;
2612 		}
2613 
2614 		return __sort_dimension__add(sd, list, level);
2615 	}
2616 
2617 	for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2618 		struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2619 
2620 		if (strncasecmp(tok, hd->name, strlen(tok)))
2621 			continue;
2622 
2623 		return __hpp_dimension__add(hd, list, level);
2624 	}
2625 
2626 	for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2627 		struct sort_dimension *sd = &bstack_sort_dimensions[i];
2628 
2629 		if (strncasecmp(tok, sd->name, strlen(tok)))
2630 			continue;
2631 
2632 		if (sort__mode != SORT_MODE__BRANCH)
2633 			return -EINVAL;
2634 
2635 		if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
2636 			list->sym = 1;
2637 
2638 		__sort_dimension__add(sd, list, level);
2639 		return 0;
2640 	}
2641 
2642 	for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2643 		struct sort_dimension *sd = &memory_sort_dimensions[i];
2644 
2645 		if (strncasecmp(tok, sd->name, strlen(tok)))
2646 			continue;
2647 
2648 		if (sort__mode != SORT_MODE__MEMORY)
2649 			return -EINVAL;
2650 
2651 		if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
2652 			return -EINVAL;
2653 
2654 		if (sd->entry == &sort_mem_daddr_sym)
2655 			list->sym = 1;
2656 
2657 		__sort_dimension__add(sd, list, level);
2658 		return 0;
2659 	}
2660 
2661 	if (!add_dynamic_entry(evlist, tok, level))
2662 		return 0;
2663 
2664 	return -ESRCH;
2665 }
2666 
setup_sort_list(struct perf_hpp_list * list,char * str,struct evlist * evlist)2667 static int setup_sort_list(struct perf_hpp_list *list, char *str,
2668 			   struct evlist *evlist)
2669 {
2670 	char *tmp, *tok;
2671 	int ret = 0;
2672 	int level = 0;
2673 	int next_level = 1;
2674 	bool in_group = false;
2675 
2676 	do {
2677 		tok = str;
2678 		tmp = strpbrk(str, "{}, ");
2679 		if (tmp) {
2680 			if (in_group)
2681 				next_level = level;
2682 			else
2683 				next_level = level + 1;
2684 
2685 			if (*tmp == '{')
2686 				in_group = true;
2687 			else if (*tmp == '}')
2688 				in_group = false;
2689 
2690 			*tmp = '\0';
2691 			str = tmp + 1;
2692 		}
2693 
2694 		if (*tok) {
2695 			ret = sort_dimension__add(list, tok, evlist, level);
2696 			if (ret == -EINVAL) {
2697 				if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
2698 					pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
2699 				else
2700 					pr_err("Invalid --sort key: `%s'", tok);
2701 				break;
2702 			} else if (ret == -ESRCH) {
2703 				pr_err("Unknown --sort key: `%s'", tok);
2704 				break;
2705 			}
2706 		}
2707 
2708 		level = next_level;
2709 	} while (tmp);
2710 
2711 	return ret;
2712 }
2713 
get_default_sort_order(struct evlist * evlist)2714 static const char *get_default_sort_order(struct evlist *evlist)
2715 {
2716 	const char *default_sort_orders[] = {
2717 		default_sort_order,
2718 		default_branch_sort_order,
2719 		default_mem_sort_order,
2720 		default_top_sort_order,
2721 		default_diff_sort_order,
2722 		default_tracepoint_sort_order,
2723 	};
2724 	bool use_trace = true;
2725 	struct evsel *evsel;
2726 
2727 	BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
2728 
2729 	if (evlist == NULL || perf_evlist__empty(evlist))
2730 		goto out_no_evlist;
2731 
2732 	evlist__for_each_entry(evlist, evsel) {
2733 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2734 			use_trace = false;
2735 			break;
2736 		}
2737 	}
2738 
2739 	if (use_trace) {
2740 		sort__mode = SORT_MODE__TRACEPOINT;
2741 		if (symbol_conf.raw_trace)
2742 			return "trace_fields";
2743 	}
2744 out_no_evlist:
2745 	return default_sort_orders[sort__mode];
2746 }
2747 
setup_sort_order(struct evlist * evlist)2748 static int setup_sort_order(struct evlist *evlist)
2749 {
2750 	char *new_sort_order;
2751 
2752 	/*
2753 	 * Append '+'-prefixed sort order to the default sort
2754 	 * order string.
2755 	 */
2756 	if (!sort_order || is_strict_order(sort_order))
2757 		return 0;
2758 
2759 	if (sort_order[1] == '\0') {
2760 		pr_err("Invalid --sort key: `+'");
2761 		return -EINVAL;
2762 	}
2763 
2764 	/*
2765 	 * We allocate new sort_order string, but we never free it,
2766 	 * because it's checked over the rest of the code.
2767 	 */
2768 	if (asprintf(&new_sort_order, "%s,%s",
2769 		     get_default_sort_order(evlist), sort_order + 1) < 0) {
2770 		pr_err("Not enough memory to set up --sort");
2771 		return -ENOMEM;
2772 	}
2773 
2774 	sort_order = new_sort_order;
2775 	return 0;
2776 }
2777 
2778 /*
2779  * Adds 'pre,' prefix into 'str' is 'pre' is
2780  * not already part of 'str'.
2781  */
prefix_if_not_in(const char * pre,char * str)2782 static char *prefix_if_not_in(const char *pre, char *str)
2783 {
2784 	char *n;
2785 
2786 	if (!str || strstr(str, pre))
2787 		return str;
2788 
2789 	if (asprintf(&n, "%s,%s", pre, str) < 0)
2790 		n = NULL;
2791 
2792 	free(str);
2793 	return n;
2794 }
2795 
setup_overhead(char * keys)2796 static char *setup_overhead(char *keys)
2797 {
2798 	if (sort__mode == SORT_MODE__DIFF)
2799 		return keys;
2800 
2801 	keys = prefix_if_not_in("overhead", keys);
2802 
2803 	if (symbol_conf.cumulate_callchain)
2804 		keys = prefix_if_not_in("overhead_children", keys);
2805 
2806 	return keys;
2807 }
2808 
__setup_sorting(struct evlist * evlist)2809 static int __setup_sorting(struct evlist *evlist)
2810 {
2811 	char *str;
2812 	const char *sort_keys;
2813 	int ret = 0;
2814 
2815 	ret = setup_sort_order(evlist);
2816 	if (ret)
2817 		return ret;
2818 
2819 	sort_keys = sort_order;
2820 	if (sort_keys == NULL) {
2821 		if (is_strict_order(field_order)) {
2822 			/*
2823 			 * If user specified field order but no sort order,
2824 			 * we'll honor it and not add default sort orders.
2825 			 */
2826 			return 0;
2827 		}
2828 
2829 		sort_keys = get_default_sort_order(evlist);
2830 	}
2831 
2832 	str = strdup(sort_keys);
2833 	if (str == NULL) {
2834 		pr_err("Not enough memory to setup sort keys");
2835 		return -ENOMEM;
2836 	}
2837 
2838 	/*
2839 	 * Prepend overhead fields for backward compatibility.
2840 	 */
2841 	if (!is_strict_order(field_order)) {
2842 		str = setup_overhead(str);
2843 		if (str == NULL) {
2844 			pr_err("Not enough memory to setup overhead keys");
2845 			return -ENOMEM;
2846 		}
2847 	}
2848 
2849 	ret = setup_sort_list(&perf_hpp_list, str, evlist);
2850 
2851 	free(str);
2852 	return ret;
2853 }
2854 
perf_hpp__set_elide(int idx,bool elide)2855 void perf_hpp__set_elide(int idx, bool elide)
2856 {
2857 	struct perf_hpp_fmt *fmt;
2858 	struct hpp_sort_entry *hse;
2859 
2860 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2861 		if (!perf_hpp__is_sort_entry(fmt))
2862 			continue;
2863 
2864 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
2865 		if (hse->se->se_width_idx == idx) {
2866 			fmt->elide = elide;
2867 			break;
2868 		}
2869 	}
2870 }
2871 
__get_elide(struct strlist * list,const char * list_name,FILE * fp)2872 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
2873 {
2874 	if (list && strlist__nr_entries(list) == 1) {
2875 		if (fp != NULL)
2876 			fprintf(fp, "# %s: %s\n", list_name,
2877 				strlist__entry(list, 0)->s);
2878 		return true;
2879 	}
2880 	return false;
2881 }
2882 
get_elide(int idx,FILE * output)2883 static bool get_elide(int idx, FILE *output)
2884 {
2885 	switch (idx) {
2886 	case HISTC_SYMBOL:
2887 		return __get_elide(symbol_conf.sym_list, "symbol", output);
2888 	case HISTC_DSO:
2889 		return __get_elide(symbol_conf.dso_list, "dso", output);
2890 	case HISTC_COMM:
2891 		return __get_elide(symbol_conf.comm_list, "comm", output);
2892 	default:
2893 		break;
2894 	}
2895 
2896 	if (sort__mode != SORT_MODE__BRANCH)
2897 		return false;
2898 
2899 	switch (idx) {
2900 	case HISTC_SYMBOL_FROM:
2901 		return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
2902 	case HISTC_SYMBOL_TO:
2903 		return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
2904 	case HISTC_DSO_FROM:
2905 		return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
2906 	case HISTC_DSO_TO:
2907 		return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
2908 	default:
2909 		break;
2910 	}
2911 
2912 	return false;
2913 }
2914 
sort__setup_elide(FILE * output)2915 void sort__setup_elide(FILE *output)
2916 {
2917 	struct perf_hpp_fmt *fmt;
2918 	struct hpp_sort_entry *hse;
2919 
2920 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2921 		if (!perf_hpp__is_sort_entry(fmt))
2922 			continue;
2923 
2924 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
2925 		fmt->elide = get_elide(hse->se->se_width_idx, output);
2926 	}
2927 
2928 	/*
2929 	 * It makes no sense to elide all of sort entries.
2930 	 * Just revert them to show up again.
2931 	 */
2932 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2933 		if (!perf_hpp__is_sort_entry(fmt))
2934 			continue;
2935 
2936 		if (!fmt->elide)
2937 			return;
2938 	}
2939 
2940 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
2941 		if (!perf_hpp__is_sort_entry(fmt))
2942 			continue;
2943 
2944 		fmt->elide = false;
2945 	}
2946 }
2947 
output_field_add(struct perf_hpp_list * list,char * tok)2948 int output_field_add(struct perf_hpp_list *list, char *tok)
2949 {
2950 	unsigned int i;
2951 
2952 	for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2953 		struct sort_dimension *sd = &common_sort_dimensions[i];
2954 
2955 		if (strncasecmp(tok, sd->name, strlen(tok)))
2956 			continue;
2957 
2958 		return __sort_dimension__add_output(list, sd);
2959 	}
2960 
2961 	for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
2962 		struct hpp_dimension *hd = &hpp_sort_dimensions[i];
2963 
2964 		if (strncasecmp(tok, hd->name, strlen(tok)))
2965 			continue;
2966 
2967 		return __hpp_dimension__add_output(list, hd);
2968 	}
2969 
2970 	for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
2971 		struct sort_dimension *sd = &bstack_sort_dimensions[i];
2972 
2973 		if (strncasecmp(tok, sd->name, strlen(tok)))
2974 			continue;
2975 
2976 		return __sort_dimension__add_output(list, sd);
2977 	}
2978 
2979 	for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
2980 		struct sort_dimension *sd = &memory_sort_dimensions[i];
2981 
2982 		if (strncasecmp(tok, sd->name, strlen(tok)))
2983 			continue;
2984 
2985 		return __sort_dimension__add_output(list, sd);
2986 	}
2987 
2988 	return -ESRCH;
2989 }
2990 
setup_output_list(struct perf_hpp_list * list,char * str)2991 static int setup_output_list(struct perf_hpp_list *list, char *str)
2992 {
2993 	char *tmp, *tok;
2994 	int ret = 0;
2995 
2996 	for (tok = strtok_r(str, ", ", &tmp);
2997 			tok; tok = strtok_r(NULL, ", ", &tmp)) {
2998 		ret = output_field_add(list, tok);
2999 		if (ret == -EINVAL) {
3000 			ui__error("Invalid --fields key: `%s'", tok);
3001 			break;
3002 		} else if (ret == -ESRCH) {
3003 			ui__error("Unknown --fields key: `%s'", tok);
3004 			break;
3005 		}
3006 	}
3007 
3008 	return ret;
3009 }
3010 
reset_dimensions(void)3011 void reset_dimensions(void)
3012 {
3013 	unsigned int i;
3014 
3015 	for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
3016 		common_sort_dimensions[i].taken = 0;
3017 
3018 	for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
3019 		hpp_sort_dimensions[i].taken = 0;
3020 
3021 	for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
3022 		bstack_sort_dimensions[i].taken = 0;
3023 
3024 	for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
3025 		memory_sort_dimensions[i].taken = 0;
3026 }
3027 
is_strict_order(const char * order)3028 bool is_strict_order(const char *order)
3029 {
3030 	return order && (*order != '+');
3031 }
3032 
__setup_output_field(void)3033 static int __setup_output_field(void)
3034 {
3035 	char *str, *strp;
3036 	int ret = -EINVAL;
3037 
3038 	if (field_order == NULL)
3039 		return 0;
3040 
3041 	strp = str = strdup(field_order);
3042 	if (str == NULL) {
3043 		pr_err("Not enough memory to setup output fields");
3044 		return -ENOMEM;
3045 	}
3046 
3047 	if (!is_strict_order(field_order))
3048 		strp++;
3049 
3050 	if (!strlen(strp)) {
3051 		pr_err("Invalid --fields key: `+'");
3052 		goto out;
3053 	}
3054 
3055 	ret = setup_output_list(&perf_hpp_list, strp);
3056 
3057 out:
3058 	free(str);
3059 	return ret;
3060 }
3061 
setup_sorting(struct evlist * evlist)3062 int setup_sorting(struct evlist *evlist)
3063 {
3064 	int err;
3065 
3066 	err = __setup_sorting(evlist);
3067 	if (err < 0)
3068 		return err;
3069 
3070 	if (parent_pattern != default_parent_pattern) {
3071 		err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
3072 		if (err < 0)
3073 			return err;
3074 	}
3075 
3076 	reset_dimensions();
3077 
3078 	/*
3079 	 * perf diff doesn't use default hpp output fields.
3080 	 */
3081 	if (sort__mode != SORT_MODE__DIFF)
3082 		perf_hpp__init();
3083 
3084 	err = __setup_output_field();
3085 	if (err < 0)
3086 		return err;
3087 
3088 	/* copy sort keys to output fields */
3089 	perf_hpp__setup_output_field(&perf_hpp_list);
3090 	/* and then copy output fields to sort keys */
3091 	perf_hpp__append_sort_keys(&perf_hpp_list);
3092 
3093 	/* setup hists-specific output fields */
3094 	if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
3095 		return -1;
3096 
3097 	return 0;
3098 }
3099 
reset_output_field(void)3100 void reset_output_field(void)
3101 {
3102 	perf_hpp_list.need_collapse = 0;
3103 	perf_hpp_list.parent = 0;
3104 	perf_hpp_list.sym = 0;
3105 	perf_hpp_list.dso = 0;
3106 
3107 	field_order = NULL;
3108 	sort_order = NULL;
3109 
3110 	reset_dimensions();
3111 	perf_hpp__reset_output_field(&perf_hpp_list);
3112 }
3113 
3114 #define INDENT (3*8 + 1)
3115 
add_key(struct strbuf * sb,const char * str,int * llen)3116 static void add_key(struct strbuf *sb, const char *str, int *llen)
3117 {
3118 	if (*llen >= 75) {
3119 		strbuf_addstr(sb, "\n\t\t\t ");
3120 		*llen = INDENT;
3121 	}
3122 	strbuf_addf(sb, " %s", str);
3123 	*llen += strlen(str) + 1;
3124 }
3125 
add_sort_string(struct strbuf * sb,struct sort_dimension * s,int n,int * llen)3126 static void add_sort_string(struct strbuf *sb, struct sort_dimension *s, int n,
3127 			    int *llen)
3128 {
3129 	int i;
3130 
3131 	for (i = 0; i < n; i++)
3132 		add_key(sb, s[i].name, llen);
3133 }
3134 
add_hpp_sort_string(struct strbuf * sb,struct hpp_dimension * s,int n,int * llen)3135 static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int n,
3136 				int *llen)
3137 {
3138 	int i;
3139 
3140 	for (i = 0; i < n; i++)
3141 		add_key(sb, s[i].name, llen);
3142 }
3143 
sort_help(const char * prefix)3144 const char *sort_help(const char *prefix)
3145 {
3146 	struct strbuf sb;
3147 	char *s;
3148 	int len = strlen(prefix) + INDENT;
3149 
3150 	strbuf_init(&sb, 300);
3151 	strbuf_addstr(&sb, prefix);
3152 	add_hpp_sort_string(&sb, hpp_sort_dimensions,
3153 			    ARRAY_SIZE(hpp_sort_dimensions), &len);
3154 	add_sort_string(&sb, common_sort_dimensions,
3155 			    ARRAY_SIZE(common_sort_dimensions), &len);
3156 	add_sort_string(&sb, bstack_sort_dimensions,
3157 			    ARRAY_SIZE(bstack_sort_dimensions), &len);
3158 	add_sort_string(&sb, memory_sort_dimensions,
3159 			    ARRAY_SIZE(memory_sort_dimensions), &len);
3160 	s = strbuf_detach(&sb, NULL);
3161 	strbuf_release(&sb);
3162 	return s;
3163 }
3164