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