• 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,local_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 static const char *const dynamic_headers[] = {"local_ins_lat", "ins_lat", "local_p_stage_cyc", "p_stage_cyc"};
50 static const char *const arch_specific_sort_keys[] = {"local_p_stage_cyc", "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 						 from->al_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 						 to->al_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 
_hist_entry__addr_snprintf(struct map_symbol * ms,u64 ip,char level,char * bf,size_t size,unsigned int width)992 static int _hist_entry__addr_snprintf(struct map_symbol *ms,
993 				     u64 ip, char level, char *bf, size_t size,
994 				     unsigned int width)
995 {
996 	struct symbol *sym = ms->sym;
997 	struct map *map = ms->map;
998 	size_t ret = 0, offs;
999 
1000 	ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
1001 	if (sym && map) {
1002 		if (sym->type == STT_OBJECT) {
1003 			ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
1004 			ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
1005 					ip - map->unmap_ip(map, sym->start));
1006 		} else {
1007 			ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
1008 					       width - ret,
1009 					       sym->name);
1010 			offs = ip - sym->start;
1011 			if (offs)
1012 				ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx", offs);
1013 		}
1014 	} else {
1015 		size_t len = BITS_PER_LONG / 4;
1016 		ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
1017 				       len, ip);
1018 	}
1019 
1020 	return ret;
1021 }
1022 
hist_entry__addr_from_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1023 static int hist_entry__addr_from_snprintf(struct hist_entry *he, char *bf,
1024 					 size_t size, unsigned int width)
1025 {
1026 	if (he->branch_info) {
1027 		struct addr_map_symbol *from = &he->branch_info->from;
1028 
1029 		return _hist_entry__addr_snprintf(&from->ms, from->al_addr,
1030 						 he->level, bf, size, width);
1031 	}
1032 
1033 	return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
1034 }
1035 
hist_entry__addr_to_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1036 static int hist_entry__addr_to_snprintf(struct hist_entry *he, char *bf,
1037 				       size_t size, unsigned int width)
1038 {
1039 	if (he->branch_info) {
1040 		struct addr_map_symbol *to = &he->branch_info->to;
1041 
1042 		return _hist_entry__addr_snprintf(&to->ms, to->al_addr,
1043 						 he->level, bf, size, width);
1044 	}
1045 
1046 	return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
1047 }
1048 
1049 static int64_t
sort__addr_from_cmp(struct hist_entry * left,struct hist_entry * right)1050 sort__addr_from_cmp(struct hist_entry *left, struct hist_entry *right)
1051 {
1052 	struct addr_map_symbol *from_l;
1053 	struct addr_map_symbol *from_r;
1054 	int64_t ret;
1055 
1056 	if (!left->branch_info || !right->branch_info)
1057 		return cmp_null(left->branch_info, right->branch_info);
1058 
1059 	from_l = &left->branch_info->from;
1060 	from_r = &right->branch_info->from;
1061 
1062 	/*
1063 	 * comparing symbol address alone is not enough since it's a
1064 	 * relative address within a dso.
1065 	 */
1066 	ret = _sort__dso_cmp(from_l->ms.map, from_r->ms.map);
1067 	if (ret != 0)
1068 		return ret;
1069 
1070 	return _sort__addr_cmp(from_l->addr, from_r->addr);
1071 }
1072 
1073 static int64_t
sort__addr_to_cmp(struct hist_entry * left,struct hist_entry * right)1074 sort__addr_to_cmp(struct hist_entry *left, struct hist_entry *right)
1075 {
1076 	struct addr_map_symbol *to_l;
1077 	struct addr_map_symbol *to_r;
1078 	int64_t ret;
1079 
1080 	if (!left->branch_info || !right->branch_info)
1081 		return cmp_null(left->branch_info, right->branch_info);
1082 
1083 	to_l = &left->branch_info->to;
1084 	to_r = &right->branch_info->to;
1085 
1086 	/*
1087 	 * comparing symbol address alone is not enough since it's a
1088 	 * relative address within a dso.
1089 	 */
1090 	ret = _sort__dso_cmp(to_l->ms.map, to_r->ms.map);
1091 	if (ret != 0)
1092 		return ret;
1093 
1094 	return _sort__addr_cmp(to_l->addr, to_r->addr);
1095 }
1096 
1097 struct sort_entry sort_addr_from = {
1098 	.se_header	= "Source Address",
1099 	.se_cmp		= sort__addr_from_cmp,
1100 	.se_snprintf	= hist_entry__addr_from_snprintf,
1101 	.se_filter	= hist_entry__sym_from_filter, /* shared with sym_from */
1102 	.se_width_idx	= HISTC_ADDR_FROM,
1103 };
1104 
1105 struct sort_entry sort_addr_to = {
1106 	.se_header	= "Target Address",
1107 	.se_cmp		= sort__addr_to_cmp,
1108 	.se_snprintf	= hist_entry__addr_to_snprintf,
1109 	.se_filter	= hist_entry__sym_to_filter, /* shared with sym_to */
1110 	.se_width_idx	= HISTC_ADDR_TO,
1111 };
1112 
1113 
1114 static int64_t
sort__mispredict_cmp(struct hist_entry * left,struct hist_entry * right)1115 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
1116 {
1117 	unsigned char mp, p;
1118 
1119 	if (!left->branch_info || !right->branch_info)
1120 		return cmp_null(left->branch_info, right->branch_info);
1121 
1122 	mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
1123 	p  = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
1124 	return mp || p;
1125 }
1126 
hist_entry__mispredict_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1127 static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
1128 				    size_t size, unsigned int width){
1129 	static const char *out = "N/A";
1130 
1131 	if (he->branch_info) {
1132 		if (he->branch_info->flags.predicted)
1133 			out = "N";
1134 		else if (he->branch_info->flags.mispred)
1135 			out = "Y";
1136 	}
1137 
1138 	return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
1139 }
1140 
1141 static int64_t
sort__cycles_cmp(struct hist_entry * left,struct hist_entry * right)1142 sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
1143 {
1144 	if (!left->branch_info || !right->branch_info)
1145 		return cmp_null(left->branch_info, right->branch_info);
1146 
1147 	return left->branch_info->flags.cycles -
1148 		right->branch_info->flags.cycles;
1149 }
1150 
hist_entry__cycles_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1151 static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
1152 				    size_t size, unsigned int width)
1153 {
1154 	if (!he->branch_info)
1155 		return scnprintf(bf, size, "%-.*s", width, "N/A");
1156 	if (he->branch_info->flags.cycles == 0)
1157 		return repsep_snprintf(bf, size, "%-*s", width, "-");
1158 	return repsep_snprintf(bf, size, "%-*hd", width,
1159 			       he->branch_info->flags.cycles);
1160 }
1161 
1162 struct sort_entry sort_cycles = {
1163 	.se_header	= "Basic Block Cycles",
1164 	.se_cmp		= sort__cycles_cmp,
1165 	.se_snprintf	= hist_entry__cycles_snprintf,
1166 	.se_width_idx	= HISTC_CYCLES,
1167 };
1168 
1169 /* --sort daddr_sym */
1170 int64_t
sort__daddr_cmp(struct hist_entry * left,struct hist_entry * right)1171 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1172 {
1173 	uint64_t l = 0, r = 0;
1174 
1175 	if (left->mem_info)
1176 		l = left->mem_info->daddr.addr;
1177 	if (right->mem_info)
1178 		r = right->mem_info->daddr.addr;
1179 
1180 	return (int64_t)(r - l);
1181 }
1182 
hist_entry__daddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1183 static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
1184 				    size_t size, unsigned int width)
1185 {
1186 	uint64_t addr = 0;
1187 	struct map_symbol *ms = NULL;
1188 
1189 	if (he->mem_info) {
1190 		addr = he->mem_info->daddr.addr;
1191 		ms = &he->mem_info->daddr.ms;
1192 	}
1193 	return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1194 }
1195 
1196 int64_t
sort__iaddr_cmp(struct hist_entry * left,struct hist_entry * right)1197 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
1198 {
1199 	uint64_t l = 0, r = 0;
1200 
1201 	if (left->mem_info)
1202 		l = left->mem_info->iaddr.addr;
1203 	if (right->mem_info)
1204 		r = right->mem_info->iaddr.addr;
1205 
1206 	return (int64_t)(r - l);
1207 }
1208 
hist_entry__iaddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1209 static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
1210 				    size_t size, unsigned int width)
1211 {
1212 	uint64_t addr = 0;
1213 	struct map_symbol *ms = NULL;
1214 
1215 	if (he->mem_info) {
1216 		addr = he->mem_info->iaddr.addr;
1217 		ms   = &he->mem_info->iaddr.ms;
1218 	}
1219 	return _hist_entry__sym_snprintf(ms, addr, he->level, bf, size, width);
1220 }
1221 
1222 static int64_t
sort__dso_daddr_cmp(struct hist_entry * left,struct hist_entry * right)1223 sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1224 {
1225 	struct map *map_l = NULL;
1226 	struct map *map_r = NULL;
1227 
1228 	if (left->mem_info)
1229 		map_l = left->mem_info->daddr.ms.map;
1230 	if (right->mem_info)
1231 		map_r = right->mem_info->daddr.ms.map;
1232 
1233 	return _sort__dso_cmp(map_l, map_r);
1234 }
1235 
hist_entry__dso_daddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1236 static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
1237 				    size_t size, unsigned int width)
1238 {
1239 	struct map *map = NULL;
1240 
1241 	if (he->mem_info)
1242 		map = he->mem_info->daddr.ms.map;
1243 
1244 	return _hist_entry__dso_snprintf(map, bf, size, width);
1245 }
1246 
1247 static int64_t
sort__locked_cmp(struct hist_entry * left,struct hist_entry * right)1248 sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
1249 {
1250 	union perf_mem_data_src data_src_l;
1251 	union perf_mem_data_src data_src_r;
1252 
1253 	if (left->mem_info)
1254 		data_src_l = left->mem_info->data_src;
1255 	else
1256 		data_src_l.mem_lock = PERF_MEM_LOCK_NA;
1257 
1258 	if (right->mem_info)
1259 		data_src_r = right->mem_info->data_src;
1260 	else
1261 		data_src_r.mem_lock = PERF_MEM_LOCK_NA;
1262 
1263 	return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
1264 }
1265 
hist_entry__locked_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1266 static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
1267 				    size_t size, unsigned int width)
1268 {
1269 	char out[10];
1270 
1271 	perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
1272 	return repsep_snprintf(bf, size, "%.*s", width, out);
1273 }
1274 
1275 static int64_t
sort__tlb_cmp(struct hist_entry * left,struct hist_entry * right)1276 sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
1277 {
1278 	union perf_mem_data_src data_src_l;
1279 	union perf_mem_data_src data_src_r;
1280 
1281 	if (left->mem_info)
1282 		data_src_l = left->mem_info->data_src;
1283 	else
1284 		data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
1285 
1286 	if (right->mem_info)
1287 		data_src_r = right->mem_info->data_src;
1288 	else
1289 		data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
1290 
1291 	return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
1292 }
1293 
hist_entry__tlb_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1294 static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
1295 				    size_t size, unsigned int width)
1296 {
1297 	char out[64];
1298 
1299 	perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
1300 	return repsep_snprintf(bf, size, "%-*s", width, out);
1301 }
1302 
1303 static int64_t
sort__lvl_cmp(struct hist_entry * left,struct hist_entry * right)1304 sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
1305 {
1306 	union perf_mem_data_src data_src_l;
1307 	union perf_mem_data_src data_src_r;
1308 
1309 	if (left->mem_info)
1310 		data_src_l = left->mem_info->data_src;
1311 	else
1312 		data_src_l.mem_lvl = PERF_MEM_LVL_NA;
1313 
1314 	if (right->mem_info)
1315 		data_src_r = right->mem_info->data_src;
1316 	else
1317 		data_src_r.mem_lvl = PERF_MEM_LVL_NA;
1318 
1319 	return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
1320 }
1321 
hist_entry__lvl_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1322 static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
1323 				    size_t size, unsigned int width)
1324 {
1325 	char out[64];
1326 
1327 	perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
1328 	return repsep_snprintf(bf, size, "%-*s", width, out);
1329 }
1330 
1331 static int64_t
sort__snoop_cmp(struct hist_entry * left,struct hist_entry * right)1332 sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
1333 {
1334 	union perf_mem_data_src data_src_l;
1335 	union perf_mem_data_src data_src_r;
1336 
1337 	if (left->mem_info)
1338 		data_src_l = left->mem_info->data_src;
1339 	else
1340 		data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
1341 
1342 	if (right->mem_info)
1343 		data_src_r = right->mem_info->data_src;
1344 	else
1345 		data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
1346 
1347 	return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
1348 }
1349 
hist_entry__snoop_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1350 static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
1351 				    size_t size, unsigned int width)
1352 {
1353 	char out[64];
1354 
1355 	perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
1356 	return repsep_snprintf(bf, size, "%-*s", width, out);
1357 }
1358 
1359 int64_t
sort__dcacheline_cmp(struct hist_entry * left,struct hist_entry * right)1360 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
1361 {
1362 	u64 l, r;
1363 	struct map *l_map, *r_map;
1364 	int rc;
1365 
1366 	if (!left->mem_info)  return -1;
1367 	if (!right->mem_info) return 1;
1368 
1369 	/* group event types together */
1370 	if (left->cpumode > right->cpumode) return -1;
1371 	if (left->cpumode < right->cpumode) return 1;
1372 
1373 	l_map = left->mem_info->daddr.ms.map;
1374 	r_map = right->mem_info->daddr.ms.map;
1375 
1376 	/* if both are NULL, jump to sort on al_addr instead */
1377 	if (!l_map && !r_map)
1378 		goto addr;
1379 
1380 	if (!l_map) return -1;
1381 	if (!r_map) return 1;
1382 
1383 	rc = dso__cmp_id(l_map->dso, r_map->dso);
1384 	if (rc)
1385 		return rc;
1386 	/*
1387 	 * Addresses with no major/minor numbers are assumed to be
1388 	 * anonymous in userspace.  Sort those on pid then address.
1389 	 *
1390 	 * The kernel and non-zero major/minor mapped areas are
1391 	 * assumed to be unity mapped.  Sort those on address.
1392 	 */
1393 
1394 	if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
1395 	    (!(l_map->flags & MAP_SHARED)) &&
1396 	    !l_map->dso->id.maj && !l_map->dso->id.min &&
1397 	    !l_map->dso->id.ino && !l_map->dso->id.ino_generation) {
1398 		/* userspace anonymous */
1399 
1400 		if (left->thread->pid_ > right->thread->pid_) return -1;
1401 		if (left->thread->pid_ < right->thread->pid_) return 1;
1402 	}
1403 
1404 addr:
1405 	/* al_addr does all the right addr - start + offset calculations */
1406 	l = cl_address(left->mem_info->daddr.al_addr);
1407 	r = cl_address(right->mem_info->daddr.al_addr);
1408 
1409 	if (l > r) return -1;
1410 	if (l < r) return 1;
1411 
1412 	return 0;
1413 }
1414 
hist_entry__dcacheline_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1415 static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
1416 					  size_t size, unsigned int width)
1417 {
1418 
1419 	uint64_t addr = 0;
1420 	struct map_symbol *ms = NULL;
1421 	char level = he->level;
1422 
1423 	if (he->mem_info) {
1424 		struct map *map = he->mem_info->daddr.ms.map;
1425 
1426 		addr = cl_address(he->mem_info->daddr.al_addr);
1427 		ms = &he->mem_info->daddr.ms;
1428 
1429 		/* print [s] for shared data mmaps */
1430 		if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
1431 		     map && !(map->prot & PROT_EXEC) &&
1432 		    (map->flags & MAP_SHARED) &&
1433 		    (map->dso->id.maj || map->dso->id.min ||
1434 		     map->dso->id.ino || map->dso->id.ino_generation))
1435 			level = 's';
1436 		else if (!map)
1437 			level = 'X';
1438 	}
1439 	return _hist_entry__sym_snprintf(ms, addr, level, bf, size, width);
1440 }
1441 
1442 struct sort_entry sort_mispredict = {
1443 	.se_header	= "Branch Mispredicted",
1444 	.se_cmp		= sort__mispredict_cmp,
1445 	.se_snprintf	= hist_entry__mispredict_snprintf,
1446 	.se_width_idx	= HISTC_MISPREDICT,
1447 };
1448 
1449 static int64_t
sort__weight_cmp(struct hist_entry * left,struct hist_entry * right)1450 sort__weight_cmp(struct hist_entry *left, struct hist_entry *right)
1451 {
1452 	return left->weight - right->weight;
1453 }
1454 
hist_entry__local_weight_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1455 static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
1456 				    size_t size, unsigned int width)
1457 {
1458 	return repsep_snprintf(bf, size, "%-*llu", width, he->weight);
1459 }
1460 
1461 struct sort_entry sort_local_weight = {
1462 	.se_header	= "Local Weight",
1463 	.se_cmp		= sort__weight_cmp,
1464 	.se_snprintf	= hist_entry__local_weight_snprintf,
1465 	.se_width_idx	= HISTC_LOCAL_WEIGHT,
1466 };
1467 
hist_entry__global_weight_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1468 static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
1469 					      size_t size, unsigned int width)
1470 {
1471 	return repsep_snprintf(bf, size, "%-*llu", width,
1472 			       he->weight * he->stat.nr_events);
1473 }
1474 
1475 struct sort_entry sort_global_weight = {
1476 	.se_header	= "Weight",
1477 	.se_cmp		= sort__weight_cmp,
1478 	.se_snprintf	= hist_entry__global_weight_snprintf,
1479 	.se_width_idx	= HISTC_GLOBAL_WEIGHT,
1480 };
1481 
1482 static int64_t
sort__ins_lat_cmp(struct hist_entry * left,struct hist_entry * right)1483 sort__ins_lat_cmp(struct hist_entry *left, struct hist_entry *right)
1484 {
1485 	return left->ins_lat - right->ins_lat;
1486 }
1487 
hist_entry__local_ins_lat_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1488 static int hist_entry__local_ins_lat_snprintf(struct hist_entry *he, char *bf,
1489 					      size_t size, unsigned int width)
1490 {
1491 	return repsep_snprintf(bf, size, "%-*u", width, he->ins_lat);
1492 }
1493 
1494 struct sort_entry sort_local_ins_lat = {
1495 	.se_header	= "Local INSTR Latency",
1496 	.se_cmp		= sort__ins_lat_cmp,
1497 	.se_snprintf	= hist_entry__local_ins_lat_snprintf,
1498 	.se_width_idx	= HISTC_LOCAL_INS_LAT,
1499 };
1500 
hist_entry__global_ins_lat_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1501 static int hist_entry__global_ins_lat_snprintf(struct hist_entry *he, char *bf,
1502 					       size_t size, unsigned int width)
1503 {
1504 	return repsep_snprintf(bf, size, "%-*u", width,
1505 			       he->ins_lat * he->stat.nr_events);
1506 }
1507 
1508 struct sort_entry sort_global_ins_lat = {
1509 	.se_header	= "INSTR Latency",
1510 	.se_cmp		= sort__ins_lat_cmp,
1511 	.se_snprintf	= hist_entry__global_ins_lat_snprintf,
1512 	.se_width_idx	= HISTC_GLOBAL_INS_LAT,
1513 };
1514 
1515 static int64_t
sort__p_stage_cyc_cmp(struct hist_entry * left,struct hist_entry * right)1516 sort__p_stage_cyc_cmp(struct hist_entry *left, struct hist_entry *right)
1517 {
1518 	return left->p_stage_cyc - right->p_stage_cyc;
1519 }
1520 
hist_entry__global_p_stage_cyc_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1521 static int hist_entry__global_p_stage_cyc_snprintf(struct hist_entry *he, char *bf,
1522 					size_t size, unsigned int width)
1523 {
1524 	return repsep_snprintf(bf, size, "%-*u", width,
1525 			he->p_stage_cyc * he->stat.nr_events);
1526 }
1527 
1528 
hist_entry__p_stage_cyc_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1529 static int hist_entry__p_stage_cyc_snprintf(struct hist_entry *he, char *bf,
1530 					size_t size, unsigned int width)
1531 {
1532 	return repsep_snprintf(bf, size, "%-*u", width, he->p_stage_cyc);
1533 }
1534 
1535 struct sort_entry sort_local_p_stage_cyc = {
1536 	.se_header      = "Local Pipeline Stage Cycle",
1537 	.se_cmp         = sort__p_stage_cyc_cmp,
1538 	.se_snprintf	= hist_entry__p_stage_cyc_snprintf,
1539 	.se_width_idx	= HISTC_LOCAL_P_STAGE_CYC,
1540 };
1541 
1542 struct sort_entry sort_global_p_stage_cyc = {
1543 	.se_header      = "Pipeline Stage Cycle",
1544 	.se_cmp         = sort__p_stage_cyc_cmp,
1545 	.se_snprintf    = hist_entry__global_p_stage_cyc_snprintf,
1546 	.se_width_idx   = HISTC_GLOBAL_P_STAGE_CYC,
1547 };
1548 
1549 struct sort_entry sort_mem_daddr_sym = {
1550 	.se_header	= "Data Symbol",
1551 	.se_cmp		= sort__daddr_cmp,
1552 	.se_snprintf	= hist_entry__daddr_snprintf,
1553 	.se_width_idx	= HISTC_MEM_DADDR_SYMBOL,
1554 };
1555 
1556 struct sort_entry sort_mem_iaddr_sym = {
1557 	.se_header	= "Code Symbol",
1558 	.se_cmp		= sort__iaddr_cmp,
1559 	.se_snprintf	= hist_entry__iaddr_snprintf,
1560 	.se_width_idx	= HISTC_MEM_IADDR_SYMBOL,
1561 };
1562 
1563 struct sort_entry sort_mem_daddr_dso = {
1564 	.se_header	= "Data Object",
1565 	.se_cmp		= sort__dso_daddr_cmp,
1566 	.se_snprintf	= hist_entry__dso_daddr_snprintf,
1567 	.se_width_idx	= HISTC_MEM_DADDR_DSO,
1568 };
1569 
1570 struct sort_entry sort_mem_locked = {
1571 	.se_header	= "Locked",
1572 	.se_cmp		= sort__locked_cmp,
1573 	.se_snprintf	= hist_entry__locked_snprintf,
1574 	.se_width_idx	= HISTC_MEM_LOCKED,
1575 };
1576 
1577 struct sort_entry sort_mem_tlb = {
1578 	.se_header	= "TLB access",
1579 	.se_cmp		= sort__tlb_cmp,
1580 	.se_snprintf	= hist_entry__tlb_snprintf,
1581 	.se_width_idx	= HISTC_MEM_TLB,
1582 };
1583 
1584 struct sort_entry sort_mem_lvl = {
1585 	.se_header	= "Memory access",
1586 	.se_cmp		= sort__lvl_cmp,
1587 	.se_snprintf	= hist_entry__lvl_snprintf,
1588 	.se_width_idx	= HISTC_MEM_LVL,
1589 };
1590 
1591 struct sort_entry sort_mem_snoop = {
1592 	.se_header	= "Snoop",
1593 	.se_cmp		= sort__snoop_cmp,
1594 	.se_snprintf	= hist_entry__snoop_snprintf,
1595 	.se_width_idx	= HISTC_MEM_SNOOP,
1596 };
1597 
1598 struct sort_entry sort_mem_dcacheline = {
1599 	.se_header	= "Data Cacheline",
1600 	.se_cmp		= sort__dcacheline_cmp,
1601 	.se_snprintf	= hist_entry__dcacheline_snprintf,
1602 	.se_width_idx	= HISTC_MEM_DCACHELINE,
1603 };
1604 
1605 static int64_t
sort__blocked_cmp(struct hist_entry * left,struct hist_entry * right)1606 sort__blocked_cmp(struct hist_entry *left, struct hist_entry *right)
1607 {
1608 	union perf_mem_data_src data_src_l;
1609 	union perf_mem_data_src data_src_r;
1610 
1611 	if (left->mem_info)
1612 		data_src_l = left->mem_info->data_src;
1613 	else
1614 		data_src_l.mem_blk = PERF_MEM_BLK_NA;
1615 
1616 	if (right->mem_info)
1617 		data_src_r = right->mem_info->data_src;
1618 	else
1619 		data_src_r.mem_blk = PERF_MEM_BLK_NA;
1620 
1621 	return (int64_t)(data_src_r.mem_blk - data_src_l.mem_blk);
1622 }
1623 
hist_entry__blocked_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1624 static int hist_entry__blocked_snprintf(struct hist_entry *he, char *bf,
1625 					size_t size, unsigned int width)
1626 {
1627 	char out[16];
1628 
1629 	perf_mem__blk_scnprintf(out, sizeof(out), he->mem_info);
1630 	return repsep_snprintf(bf, size, "%.*s", width, out);
1631 }
1632 
1633 struct sort_entry sort_mem_blocked = {
1634 	.se_header	= "Blocked",
1635 	.se_cmp		= sort__blocked_cmp,
1636 	.se_snprintf	= hist_entry__blocked_snprintf,
1637 	.se_width_idx	= HISTC_MEM_BLOCKED,
1638 };
1639 
1640 static int64_t
sort__phys_daddr_cmp(struct hist_entry * left,struct hist_entry * right)1641 sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
1642 {
1643 	uint64_t l = 0, r = 0;
1644 
1645 	if (left->mem_info)
1646 		l = left->mem_info->daddr.phys_addr;
1647 	if (right->mem_info)
1648 		r = right->mem_info->daddr.phys_addr;
1649 
1650 	return (int64_t)(r - l);
1651 }
1652 
hist_entry__phys_daddr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1653 static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
1654 					   size_t size, unsigned int width)
1655 {
1656 	uint64_t addr = 0;
1657 	size_t ret = 0;
1658 	size_t len = BITS_PER_LONG / 4;
1659 
1660 	addr = he->mem_info->daddr.phys_addr;
1661 
1662 	ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
1663 
1664 	ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
1665 
1666 	ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
1667 
1668 	if (ret > width)
1669 		bf[width] = '\0';
1670 
1671 	return width;
1672 }
1673 
1674 struct sort_entry sort_mem_phys_daddr = {
1675 	.se_header	= "Data Physical Address",
1676 	.se_cmp		= sort__phys_daddr_cmp,
1677 	.se_snprintf	= hist_entry__phys_daddr_snprintf,
1678 	.se_width_idx	= HISTC_MEM_PHYS_DADDR,
1679 };
1680 
1681 static int64_t
sort__data_page_size_cmp(struct hist_entry * left,struct hist_entry * right)1682 sort__data_page_size_cmp(struct hist_entry *left, struct hist_entry *right)
1683 {
1684 	uint64_t l = 0, r = 0;
1685 
1686 	if (left->mem_info)
1687 		l = left->mem_info->daddr.data_page_size;
1688 	if (right->mem_info)
1689 		r = right->mem_info->daddr.data_page_size;
1690 
1691 	return (int64_t)(r - l);
1692 }
1693 
hist_entry__data_page_size_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1694 static int hist_entry__data_page_size_snprintf(struct hist_entry *he, char *bf,
1695 					  size_t size, unsigned int width)
1696 {
1697 	char str[PAGE_SIZE_NAME_LEN];
1698 
1699 	return repsep_snprintf(bf, size, "%-*s", width,
1700 			       get_page_size_name(he->mem_info->daddr.data_page_size, str));
1701 }
1702 
1703 struct sort_entry sort_mem_data_page_size = {
1704 	.se_header	= "Data Page Size",
1705 	.se_cmp		= sort__data_page_size_cmp,
1706 	.se_snprintf	= hist_entry__data_page_size_snprintf,
1707 	.se_width_idx	= HISTC_MEM_DATA_PAGE_SIZE,
1708 };
1709 
1710 static int64_t
sort__code_page_size_cmp(struct hist_entry * left,struct hist_entry * right)1711 sort__code_page_size_cmp(struct hist_entry *left, struct hist_entry *right)
1712 {
1713 	uint64_t l = left->code_page_size;
1714 	uint64_t r = right->code_page_size;
1715 
1716 	return (int64_t)(r - l);
1717 }
1718 
hist_entry__code_page_size_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1719 static int hist_entry__code_page_size_snprintf(struct hist_entry *he, char *bf,
1720 					  size_t size, unsigned int width)
1721 {
1722 	char str[PAGE_SIZE_NAME_LEN];
1723 
1724 	return repsep_snprintf(bf, size, "%-*s", width,
1725 			       get_page_size_name(he->code_page_size, str));
1726 }
1727 
1728 struct sort_entry sort_code_page_size = {
1729 	.se_header	= "Code Page Size",
1730 	.se_cmp		= sort__code_page_size_cmp,
1731 	.se_snprintf	= hist_entry__code_page_size_snprintf,
1732 	.se_width_idx	= HISTC_CODE_PAGE_SIZE,
1733 };
1734 
1735 static int64_t
sort__abort_cmp(struct hist_entry * left,struct hist_entry * right)1736 sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
1737 {
1738 	if (!left->branch_info || !right->branch_info)
1739 		return cmp_null(left->branch_info, right->branch_info);
1740 
1741 	return left->branch_info->flags.abort !=
1742 		right->branch_info->flags.abort;
1743 }
1744 
hist_entry__abort_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1745 static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
1746 				    size_t size, unsigned int width)
1747 {
1748 	static const char *out = "N/A";
1749 
1750 	if (he->branch_info) {
1751 		if (he->branch_info->flags.abort)
1752 			out = "A";
1753 		else
1754 			out = ".";
1755 	}
1756 
1757 	return repsep_snprintf(bf, size, "%-*s", width, out);
1758 }
1759 
1760 struct sort_entry sort_abort = {
1761 	.se_header	= "Transaction abort",
1762 	.se_cmp		= sort__abort_cmp,
1763 	.se_snprintf	= hist_entry__abort_snprintf,
1764 	.se_width_idx	= HISTC_ABORT,
1765 };
1766 
1767 static int64_t
sort__in_tx_cmp(struct hist_entry * left,struct hist_entry * right)1768 sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
1769 {
1770 	if (!left->branch_info || !right->branch_info)
1771 		return cmp_null(left->branch_info, right->branch_info);
1772 
1773 	return left->branch_info->flags.in_tx !=
1774 		right->branch_info->flags.in_tx;
1775 }
1776 
hist_entry__in_tx_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1777 static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
1778 				    size_t size, unsigned int width)
1779 {
1780 	static const char *out = "N/A";
1781 
1782 	if (he->branch_info) {
1783 		if (he->branch_info->flags.in_tx)
1784 			out = "T";
1785 		else
1786 			out = ".";
1787 	}
1788 
1789 	return repsep_snprintf(bf, size, "%-*s", width, out);
1790 }
1791 
1792 struct sort_entry sort_in_tx = {
1793 	.se_header	= "Branch in transaction",
1794 	.se_cmp		= sort__in_tx_cmp,
1795 	.se_snprintf	= hist_entry__in_tx_snprintf,
1796 	.se_width_idx	= HISTC_IN_TX,
1797 };
1798 
1799 static int64_t
sort__transaction_cmp(struct hist_entry * left,struct hist_entry * right)1800 sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
1801 {
1802 	return left->transaction - right->transaction;
1803 }
1804 
add_str(char * p,const char * str)1805 static inline char *add_str(char *p, const char *str)
1806 {
1807 	strcpy(p, str);
1808 	return p + strlen(str);
1809 }
1810 
1811 static struct txbit {
1812 	unsigned flag;
1813 	const char *name;
1814 	int skip_for_len;
1815 } txbits[] = {
1816 	{ PERF_TXN_ELISION,        "EL ",        0 },
1817 	{ PERF_TXN_TRANSACTION,    "TX ",        1 },
1818 	{ PERF_TXN_SYNC,           "SYNC ",      1 },
1819 	{ PERF_TXN_ASYNC,          "ASYNC ",     0 },
1820 	{ PERF_TXN_RETRY,          "RETRY ",     0 },
1821 	{ PERF_TXN_CONFLICT,       "CON ",       0 },
1822 	{ PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
1823 	{ PERF_TXN_CAPACITY_READ,  "CAP-READ ",  0 },
1824 	{ 0, NULL, 0 }
1825 };
1826 
hist_entry__transaction_len(void)1827 int hist_entry__transaction_len(void)
1828 {
1829 	int i;
1830 	int len = 0;
1831 
1832 	for (i = 0; txbits[i].name; i++) {
1833 		if (!txbits[i].skip_for_len)
1834 			len += strlen(txbits[i].name);
1835 	}
1836 	len += 4; /* :XX<space> */
1837 	return len;
1838 }
1839 
hist_entry__transaction_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1840 static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
1841 					    size_t size, unsigned int width)
1842 {
1843 	u64 t = he->transaction;
1844 	char buf[128];
1845 	char *p = buf;
1846 	int i;
1847 
1848 	buf[0] = 0;
1849 	for (i = 0; txbits[i].name; i++)
1850 		if (txbits[i].flag & t)
1851 			p = add_str(p, txbits[i].name);
1852 	if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
1853 		p = add_str(p, "NEITHER ");
1854 	if (t & PERF_TXN_ABORT_MASK) {
1855 		sprintf(p, ":%" PRIx64,
1856 			(t & PERF_TXN_ABORT_MASK) >>
1857 			PERF_TXN_ABORT_SHIFT);
1858 		p += strlen(p);
1859 	}
1860 
1861 	return repsep_snprintf(bf, size, "%-*s", width, buf);
1862 }
1863 
1864 struct sort_entry sort_transaction = {
1865 	.se_header	= "Transaction                ",
1866 	.se_cmp		= sort__transaction_cmp,
1867 	.se_snprintf	= hist_entry__transaction_snprintf,
1868 	.se_width_idx	= HISTC_TRANSACTION,
1869 };
1870 
1871 /* --sort symbol_size */
1872 
_sort__sym_size_cmp(struct symbol * sym_l,struct symbol * sym_r)1873 static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
1874 {
1875 	int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
1876 	int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
1877 
1878 	return size_l < size_r ? -1 :
1879 		size_l == size_r ? 0 : 1;
1880 }
1881 
1882 static int64_t
sort__sym_size_cmp(struct hist_entry * left,struct hist_entry * right)1883 sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
1884 {
1885 	return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
1886 }
1887 
_hist_entry__sym_size_snprintf(struct symbol * sym,char * bf,size_t bf_size,unsigned int width)1888 static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
1889 					  size_t bf_size, unsigned int width)
1890 {
1891 	if (sym)
1892 		return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
1893 
1894 	return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1895 }
1896 
hist_entry__sym_size_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1897 static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
1898 					 size_t size, unsigned int width)
1899 {
1900 	return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
1901 }
1902 
1903 struct sort_entry sort_sym_size = {
1904 	.se_header	= "Symbol size",
1905 	.se_cmp		= sort__sym_size_cmp,
1906 	.se_snprintf	= hist_entry__sym_size_snprintf,
1907 	.se_width_idx	= HISTC_SYM_SIZE,
1908 };
1909 
1910 /* --sort dso_size */
1911 
_sort__dso_size_cmp(struct map * map_l,struct map * map_r)1912 static int64_t _sort__dso_size_cmp(struct map *map_l, struct map *map_r)
1913 {
1914 	int64_t size_l = map_l != NULL ? map__size(map_l) : 0;
1915 	int64_t size_r = map_r != NULL ? map__size(map_r) : 0;
1916 
1917 	return size_l < size_r ? -1 :
1918 		size_l == size_r ? 0 : 1;
1919 }
1920 
1921 static int64_t
sort__dso_size_cmp(struct hist_entry * left,struct hist_entry * right)1922 sort__dso_size_cmp(struct hist_entry *left, struct hist_entry *right)
1923 {
1924 	return _sort__dso_size_cmp(right->ms.map, left->ms.map);
1925 }
1926 
_hist_entry__dso_size_snprintf(struct map * map,char * bf,size_t bf_size,unsigned int width)1927 static int _hist_entry__dso_size_snprintf(struct map *map, char *bf,
1928 					  size_t bf_size, unsigned int width)
1929 {
1930 	if (map && map->dso)
1931 		return repsep_snprintf(bf, bf_size, "%*d", width,
1932 				       map__size(map));
1933 
1934 	return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
1935 }
1936 
hist_entry__dso_size_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1937 static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
1938 					 size_t size, unsigned int width)
1939 {
1940 	return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
1941 }
1942 
1943 struct sort_entry sort_dso_size = {
1944 	.se_header	= "DSO size",
1945 	.se_cmp		= sort__dso_size_cmp,
1946 	.se_snprintf	= hist_entry__dso_size_snprintf,
1947 	.se_width_idx	= HISTC_DSO_SIZE,
1948 };
1949 
1950 /* --sort dso_size */
1951 
1952 static int64_t
sort__addr_cmp(struct hist_entry * left,struct hist_entry * right)1953 sort__addr_cmp(struct hist_entry *left, struct hist_entry *right)
1954 {
1955 	u64 left_ip = left->ip;
1956 	u64 right_ip = right->ip;
1957 	struct map *left_map = left->ms.map;
1958 	struct map *right_map = right->ms.map;
1959 
1960 	if (left_map)
1961 		left_ip = left_map->unmap_ip(left_map, left_ip);
1962 	if (right_map)
1963 		right_ip = right_map->unmap_ip(right_map, right_ip);
1964 
1965 	return _sort__addr_cmp(left_ip, right_ip);
1966 }
1967 
hist_entry__addr_snprintf(struct hist_entry * he,char * bf,size_t size,unsigned int width)1968 static int hist_entry__addr_snprintf(struct hist_entry *he, char *bf,
1969 				     size_t size, unsigned int width)
1970 {
1971 	u64 ip = he->ip;
1972 	struct map *map = he->ms.map;
1973 
1974 	if (map)
1975 		ip = map->unmap_ip(map, ip);
1976 
1977 	return repsep_snprintf(bf, size, "%-#*llx", width, ip);
1978 }
1979 
1980 struct sort_entry sort_addr = {
1981 	.se_header	= "Address",
1982 	.se_cmp		= sort__addr_cmp,
1983 	.se_snprintf	= hist_entry__addr_snprintf,
1984 	.se_width_idx	= HISTC_ADDR,
1985 };
1986 
1987 
1988 struct sort_dimension {
1989 	const char		*name;
1990 	struct sort_entry	*entry;
1991 	int			taken;
1992 };
1993 
arch_support_sort_key(const char * sort_key __maybe_unused)1994 int __weak arch_support_sort_key(const char *sort_key __maybe_unused)
1995 {
1996 	return 0;
1997 }
1998 
arch_perf_header_entry(const char * se_header)1999 const char * __weak arch_perf_header_entry(const char *se_header)
2000 {
2001 	return se_header;
2002 }
2003 
sort_dimension_add_dynamic_header(struct sort_dimension * sd)2004 static void sort_dimension_add_dynamic_header(struct sort_dimension *sd)
2005 {
2006 	sd->entry->se_header = arch_perf_header_entry(sd->entry->se_header);
2007 }
2008 
2009 #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
2010 
2011 static struct sort_dimension common_sort_dimensions[] = {
2012 	DIM(SORT_PID, "pid", sort_thread),
2013 	DIM(SORT_COMM, "comm", sort_comm),
2014 	DIM(SORT_DSO, "dso", sort_dso),
2015 	DIM(SORT_SYM, "symbol", sort_sym),
2016 	DIM(SORT_PARENT, "parent", sort_parent),
2017 	DIM(SORT_CPU, "cpu", sort_cpu),
2018 	DIM(SORT_SOCKET, "socket", sort_socket),
2019 	DIM(SORT_SRCLINE, "srcline", sort_srcline),
2020 	DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
2021 	DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
2022 	DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
2023 	DIM(SORT_TRANSACTION, "transaction", sort_transaction),
2024 	DIM(SORT_TRACE, "trace", sort_trace),
2025 	DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
2026 	DIM(SORT_DSO_SIZE, "dso_size", sort_dso_size),
2027 	DIM(SORT_CGROUP, "cgroup", sort_cgroup),
2028 	DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
2029 	DIM(SORT_SYM_IPC_NULL, "ipc_null", sort_sym_ipc_null),
2030 	DIM(SORT_TIME, "time", sort_time),
2031 	DIM(SORT_CODE_PAGE_SIZE, "code_page_size", sort_code_page_size),
2032 	DIM(SORT_LOCAL_INS_LAT, "local_ins_lat", sort_local_ins_lat),
2033 	DIM(SORT_GLOBAL_INS_LAT, "ins_lat", sort_global_ins_lat),
2034 	DIM(SORT_LOCAL_PIPELINE_STAGE_CYC, "local_p_stage_cyc", sort_local_p_stage_cyc),
2035 	DIM(SORT_GLOBAL_PIPELINE_STAGE_CYC, "p_stage_cyc", sort_global_p_stage_cyc),
2036 	DIM(SORT_ADDR, "addr", sort_addr),
2037 };
2038 
2039 #undef DIM
2040 
2041 #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
2042 
2043 static struct sort_dimension bstack_sort_dimensions[] = {
2044 	DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
2045 	DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
2046 	DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
2047 	DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
2048 	DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
2049 	DIM(SORT_IN_TX, "in_tx", sort_in_tx),
2050 	DIM(SORT_ABORT, "abort", sort_abort),
2051 	DIM(SORT_CYCLES, "cycles", sort_cycles),
2052 	DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
2053 	DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
2054 	DIM(SORT_SYM_IPC, "ipc_lbr", sort_sym_ipc),
2055 	DIM(SORT_ADDR_FROM, "addr_from", sort_addr_from),
2056 	DIM(SORT_ADDR_TO, "addr_to", sort_addr_to),
2057 };
2058 
2059 #undef DIM
2060 
2061 #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
2062 
2063 static struct sort_dimension memory_sort_dimensions[] = {
2064 	DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
2065 	DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
2066 	DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
2067 	DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
2068 	DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
2069 	DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
2070 	DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
2071 	DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
2072 	DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
2073 	DIM(SORT_MEM_DATA_PAGE_SIZE, "data_page_size", sort_mem_data_page_size),
2074 	DIM(SORT_MEM_BLOCKED, "blocked", sort_mem_blocked),
2075 };
2076 
2077 #undef DIM
2078 
2079 struct hpp_dimension {
2080 	const char		*name;
2081 	struct perf_hpp_fmt	*fmt;
2082 	int			taken;
2083 };
2084 
2085 #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
2086 
2087 static struct hpp_dimension hpp_sort_dimensions[] = {
2088 	DIM(PERF_HPP__OVERHEAD, "overhead"),
2089 	DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
2090 	DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
2091 	DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
2092 	DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
2093 	DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
2094 	DIM(PERF_HPP__SAMPLES, "sample"),
2095 	DIM(PERF_HPP__PERIOD, "period"),
2096 };
2097 
2098 #undef DIM
2099 
2100 struct hpp_sort_entry {
2101 	struct perf_hpp_fmt hpp;
2102 	struct sort_entry *se;
2103 };
2104 
perf_hpp__reset_sort_width(struct perf_hpp_fmt * fmt,struct hists * hists)2105 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
2106 {
2107 	struct hpp_sort_entry *hse;
2108 
2109 	if (!perf_hpp__is_sort_entry(fmt))
2110 		return;
2111 
2112 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2113 	hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
2114 }
2115 
__sort__hpp_header(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hists * hists,int line __maybe_unused,int * span __maybe_unused)2116 static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2117 			      struct hists *hists, int line __maybe_unused,
2118 			      int *span __maybe_unused)
2119 {
2120 	struct hpp_sort_entry *hse;
2121 	size_t len = fmt->user_len;
2122 
2123 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2124 
2125 	if (!len)
2126 		len = hists__col_len(hists, hse->se->se_width_idx);
2127 
2128 	return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
2129 }
2130 
__sort__hpp_width(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp __maybe_unused,struct hists * hists)2131 static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
2132 			     struct perf_hpp *hpp __maybe_unused,
2133 			     struct hists *hists)
2134 {
2135 	struct hpp_sort_entry *hse;
2136 	size_t len = fmt->user_len;
2137 
2138 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2139 
2140 	if (!len)
2141 		len = hists__col_len(hists, hse->se->se_width_idx);
2142 
2143 	return len;
2144 }
2145 
__sort__hpp_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)2146 static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2147 			     struct hist_entry *he)
2148 {
2149 	struct hpp_sort_entry *hse;
2150 	size_t len = fmt->user_len;
2151 
2152 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2153 
2154 	if (!len)
2155 		len = hists__col_len(he->hists, hse->se->se_width_idx);
2156 
2157 	return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
2158 }
2159 
__sort__hpp_cmp(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)2160 static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
2161 			       struct hist_entry *a, struct hist_entry *b)
2162 {
2163 	struct hpp_sort_entry *hse;
2164 
2165 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2166 	return hse->se->se_cmp(a, b);
2167 }
2168 
__sort__hpp_collapse(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)2169 static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
2170 				    struct hist_entry *a, struct hist_entry *b)
2171 {
2172 	struct hpp_sort_entry *hse;
2173 	int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
2174 
2175 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2176 	collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
2177 	return collapse_fn(a, b);
2178 }
2179 
__sort__hpp_sort(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)2180 static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
2181 				struct hist_entry *a, struct hist_entry *b)
2182 {
2183 	struct hpp_sort_entry *hse;
2184 	int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
2185 
2186 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2187 	sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
2188 	return sort_fn(a, b);
2189 }
2190 
perf_hpp__is_sort_entry(struct perf_hpp_fmt * format)2191 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
2192 {
2193 	return format->header == __sort__hpp_header;
2194 }
2195 
2196 #define MK_SORT_ENTRY_CHK(key)					\
2197 bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt)	\
2198 {								\
2199 	struct hpp_sort_entry *hse;				\
2200 								\
2201 	if (!perf_hpp__is_sort_entry(fmt))			\
2202 		return false;					\
2203 								\
2204 	hse = container_of(fmt, struct hpp_sort_entry, hpp);	\
2205 	return hse->se == &sort_ ## key ;			\
2206 }
2207 
2208 MK_SORT_ENTRY_CHK(trace)
MK_SORT_ENTRY_CHK(srcline)2209 MK_SORT_ENTRY_CHK(srcline)
2210 MK_SORT_ENTRY_CHK(srcfile)
2211 MK_SORT_ENTRY_CHK(thread)
2212 MK_SORT_ENTRY_CHK(comm)
2213 MK_SORT_ENTRY_CHK(dso)
2214 MK_SORT_ENTRY_CHK(sym)
2215 
2216 
2217 static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2218 {
2219 	struct hpp_sort_entry *hse_a;
2220 	struct hpp_sort_entry *hse_b;
2221 
2222 	if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
2223 		return false;
2224 
2225 	hse_a = container_of(a, struct hpp_sort_entry, hpp);
2226 	hse_b = container_of(b, struct hpp_sort_entry, hpp);
2227 
2228 	return hse_a->se == hse_b->se;
2229 }
2230 
hse_free(struct perf_hpp_fmt * fmt)2231 static void hse_free(struct perf_hpp_fmt *fmt)
2232 {
2233 	struct hpp_sort_entry *hse;
2234 
2235 	hse = container_of(fmt, struct hpp_sort_entry, hpp);
2236 	free(hse);
2237 }
2238 
2239 static struct hpp_sort_entry *
__sort_dimension__alloc_hpp(struct sort_dimension * sd,int level)2240 __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
2241 {
2242 	struct hpp_sort_entry *hse;
2243 
2244 	hse = malloc(sizeof(*hse));
2245 	if (hse == NULL) {
2246 		pr_err("Memory allocation failed\n");
2247 		return NULL;
2248 	}
2249 
2250 	hse->se = sd->entry;
2251 	hse->hpp.name = sd->entry->se_header;
2252 	hse->hpp.header = __sort__hpp_header;
2253 	hse->hpp.width = __sort__hpp_width;
2254 	hse->hpp.entry = __sort__hpp_entry;
2255 	hse->hpp.color = NULL;
2256 
2257 	hse->hpp.cmp = __sort__hpp_cmp;
2258 	hse->hpp.collapse = __sort__hpp_collapse;
2259 	hse->hpp.sort = __sort__hpp_sort;
2260 	hse->hpp.equal = __sort__hpp_equal;
2261 	hse->hpp.free = hse_free;
2262 
2263 	INIT_LIST_HEAD(&hse->hpp.list);
2264 	INIT_LIST_HEAD(&hse->hpp.sort_list);
2265 	hse->hpp.elide = false;
2266 	hse->hpp.len = 0;
2267 	hse->hpp.user_len = 0;
2268 	hse->hpp.level = level;
2269 
2270 	return hse;
2271 }
2272 
hpp_free(struct perf_hpp_fmt * fmt)2273 static void hpp_free(struct perf_hpp_fmt *fmt)
2274 {
2275 	free(fmt);
2276 }
2277 
__hpp_dimension__alloc_hpp(struct hpp_dimension * hd,int level)2278 static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
2279 						       int level)
2280 {
2281 	struct perf_hpp_fmt *fmt;
2282 
2283 	fmt = memdup(hd->fmt, sizeof(*fmt));
2284 	if (fmt) {
2285 		INIT_LIST_HEAD(&fmt->list);
2286 		INIT_LIST_HEAD(&fmt->sort_list);
2287 		fmt->free = hpp_free;
2288 		fmt->level = level;
2289 	}
2290 
2291 	return fmt;
2292 }
2293 
hist_entry__filter(struct hist_entry * he,int type,const void * arg)2294 int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
2295 {
2296 	struct perf_hpp_fmt *fmt;
2297 	struct hpp_sort_entry *hse;
2298 	int ret = -1;
2299 	int r;
2300 
2301 	perf_hpp_list__for_each_format(he->hpp_list, fmt) {
2302 		if (!perf_hpp__is_sort_entry(fmt))
2303 			continue;
2304 
2305 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
2306 		if (hse->se->se_filter == NULL)
2307 			continue;
2308 
2309 		/*
2310 		 * hist entry is filtered if any of sort key in the hpp list
2311 		 * is applied.  But it should skip non-matched filter types.
2312 		 */
2313 		r = hse->se->se_filter(he, type, arg);
2314 		if (r >= 0) {
2315 			if (ret < 0)
2316 				ret = 0;
2317 			ret |= r;
2318 		}
2319 	}
2320 
2321 	return ret;
2322 }
2323 
__sort_dimension__add_hpp_sort(struct sort_dimension * sd,struct perf_hpp_list * list,int level)2324 static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
2325 					  struct perf_hpp_list *list,
2326 					  int level)
2327 {
2328 	struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
2329 
2330 	if (hse == NULL)
2331 		return -1;
2332 
2333 	perf_hpp_list__register_sort_field(list, &hse->hpp);
2334 	return 0;
2335 }
2336 
__sort_dimension__add_hpp_output(struct sort_dimension * sd,struct perf_hpp_list * list)2337 static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
2338 					    struct perf_hpp_list *list)
2339 {
2340 	struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
2341 
2342 	if (hse == NULL)
2343 		return -1;
2344 
2345 	perf_hpp_list__column_register(list, &hse->hpp);
2346 	return 0;
2347 }
2348 
2349 struct hpp_dynamic_entry {
2350 	struct perf_hpp_fmt hpp;
2351 	struct evsel *evsel;
2352 	struct tep_format_field *field;
2353 	unsigned dynamic_len;
2354 	bool raw_trace;
2355 };
2356 
hde_width(struct hpp_dynamic_entry * hde)2357 static int hde_width(struct hpp_dynamic_entry *hde)
2358 {
2359 	if (!hde->hpp.len) {
2360 		int len = hde->dynamic_len;
2361 		int namelen = strlen(hde->field->name);
2362 		int fieldlen = hde->field->size;
2363 
2364 		if (namelen > len)
2365 			len = namelen;
2366 
2367 		if (!(hde->field->flags & TEP_FIELD_IS_STRING)) {
2368 			/* length for print hex numbers */
2369 			fieldlen = hde->field->size * 2 + 2;
2370 		}
2371 		if (fieldlen > len)
2372 			len = fieldlen;
2373 
2374 		hde->hpp.len = len;
2375 	}
2376 	return hde->hpp.len;
2377 }
2378 
update_dynamic_len(struct hpp_dynamic_entry * hde,struct hist_entry * he)2379 static void update_dynamic_len(struct hpp_dynamic_entry *hde,
2380 			       struct hist_entry *he)
2381 {
2382 	char *str, *pos;
2383 	struct tep_format_field *field = hde->field;
2384 	size_t namelen;
2385 	bool last = false;
2386 
2387 	if (hde->raw_trace)
2388 		return;
2389 
2390 	/* parse pretty print result and update max length */
2391 	if (!he->trace_output)
2392 		he->trace_output = get_trace_output(he);
2393 
2394 	namelen = strlen(field->name);
2395 	str = he->trace_output;
2396 
2397 	while (str) {
2398 		pos = strchr(str, ' ');
2399 		if (pos == NULL) {
2400 			last = true;
2401 			pos = str + strlen(str);
2402 		}
2403 
2404 		if (!strncmp(str, field->name, namelen)) {
2405 			size_t len;
2406 
2407 			str += namelen + 1;
2408 			len = pos - str;
2409 
2410 			if (len > hde->dynamic_len)
2411 				hde->dynamic_len = len;
2412 			break;
2413 		}
2414 
2415 		if (last)
2416 			str = NULL;
2417 		else
2418 			str = pos + 1;
2419 	}
2420 }
2421 
__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)2422 static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2423 			      struct hists *hists __maybe_unused,
2424 			      int line __maybe_unused,
2425 			      int *span __maybe_unused)
2426 {
2427 	struct hpp_dynamic_entry *hde;
2428 	size_t len = fmt->user_len;
2429 
2430 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2431 
2432 	if (!len)
2433 		len = hde_width(hde);
2434 
2435 	return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
2436 }
2437 
__sort__hde_width(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp __maybe_unused,struct hists * hists __maybe_unused)2438 static int __sort__hde_width(struct perf_hpp_fmt *fmt,
2439 			     struct perf_hpp *hpp __maybe_unused,
2440 			     struct hists *hists __maybe_unused)
2441 {
2442 	struct hpp_dynamic_entry *hde;
2443 	size_t len = fmt->user_len;
2444 
2445 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2446 
2447 	if (!len)
2448 		len = hde_width(hde);
2449 
2450 	return len;
2451 }
2452 
perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt * fmt,struct hists * hists)2453 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
2454 {
2455 	struct hpp_dynamic_entry *hde;
2456 
2457 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2458 
2459 	return hists_to_evsel(hists) == hde->evsel;
2460 }
2461 
__sort__hde_entry(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he)2462 static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
2463 			     struct hist_entry *he)
2464 {
2465 	struct hpp_dynamic_entry *hde;
2466 	size_t len = fmt->user_len;
2467 	char *str, *pos;
2468 	struct tep_format_field *field;
2469 	size_t namelen;
2470 	bool last = false;
2471 	int ret;
2472 
2473 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2474 
2475 	if (!len)
2476 		len = hde_width(hde);
2477 
2478 	if (hde->raw_trace)
2479 		goto raw_field;
2480 
2481 	if (!he->trace_output)
2482 		he->trace_output = get_trace_output(he);
2483 
2484 	field = hde->field;
2485 	namelen = strlen(field->name);
2486 	str = he->trace_output;
2487 
2488 	while (str) {
2489 		pos = strchr(str, ' ');
2490 		if (pos == NULL) {
2491 			last = true;
2492 			pos = str + strlen(str);
2493 		}
2494 
2495 		if (!strncmp(str, field->name, namelen)) {
2496 			str += namelen + 1;
2497 			str = strndup(str, pos - str);
2498 
2499 			if (str == NULL)
2500 				return scnprintf(hpp->buf, hpp->size,
2501 						 "%*.*s", len, len, "ERROR");
2502 			break;
2503 		}
2504 
2505 		if (last)
2506 			str = NULL;
2507 		else
2508 			str = pos + 1;
2509 	}
2510 
2511 	if (str == NULL) {
2512 		struct trace_seq seq;
2513 raw_field:
2514 		trace_seq_init(&seq);
2515 		tep_print_field(&seq, he->raw_data, hde->field);
2516 		str = seq.buffer;
2517 	}
2518 
2519 	ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
2520 	free(str);
2521 	return ret;
2522 }
2523 
__sort__hde_cmp(struct perf_hpp_fmt * fmt,struct hist_entry * a,struct hist_entry * b)2524 static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
2525 			       struct hist_entry *a, struct hist_entry *b)
2526 {
2527 	struct hpp_dynamic_entry *hde;
2528 	struct tep_format_field *field;
2529 	unsigned offset, size;
2530 
2531 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2532 
2533 	if (b == NULL) {
2534 		update_dynamic_len(hde, a);
2535 		return 0;
2536 	}
2537 
2538 	field = hde->field;
2539 	if (field->flags & TEP_FIELD_IS_DYNAMIC) {
2540 		unsigned long long dyn;
2541 
2542 		tep_read_number_field(field, a->raw_data, &dyn);
2543 		offset = dyn & 0xffff;
2544 		size = (dyn >> 16) & 0xffff;
2545 		if (field->flags & TEP_FIELD_IS_RELATIVE)
2546 			offset += field->offset + field->size;
2547 
2548 		/* record max width for output */
2549 		if (size > hde->dynamic_len)
2550 			hde->dynamic_len = size;
2551 	} else {
2552 		offset = field->offset;
2553 		size = field->size;
2554 	}
2555 
2556 	return memcmp(a->raw_data + offset, b->raw_data + offset, size);
2557 }
2558 
perf_hpp__is_dynamic_entry(struct perf_hpp_fmt * fmt)2559 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
2560 {
2561 	return fmt->cmp == __sort__hde_cmp;
2562 }
2563 
__sort__hde_equal(struct perf_hpp_fmt * a,struct perf_hpp_fmt * b)2564 static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
2565 {
2566 	struct hpp_dynamic_entry *hde_a;
2567 	struct hpp_dynamic_entry *hde_b;
2568 
2569 	if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
2570 		return false;
2571 
2572 	hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
2573 	hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
2574 
2575 	return hde_a->field == hde_b->field;
2576 }
2577 
hde_free(struct perf_hpp_fmt * fmt)2578 static void hde_free(struct perf_hpp_fmt *fmt)
2579 {
2580 	struct hpp_dynamic_entry *hde;
2581 
2582 	hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2583 	free(hde);
2584 }
2585 
2586 static struct hpp_dynamic_entry *
__alloc_dynamic_entry(struct evsel * evsel,struct tep_format_field * field,int level)2587 __alloc_dynamic_entry(struct evsel *evsel, struct tep_format_field *field,
2588 		      int level)
2589 {
2590 	struct hpp_dynamic_entry *hde;
2591 
2592 	hde = malloc(sizeof(*hde));
2593 	if (hde == NULL) {
2594 		pr_debug("Memory allocation failed\n");
2595 		return NULL;
2596 	}
2597 
2598 	hde->evsel = evsel;
2599 	hde->field = field;
2600 	hde->dynamic_len = 0;
2601 
2602 	hde->hpp.name = field->name;
2603 	hde->hpp.header = __sort__hde_header;
2604 	hde->hpp.width  = __sort__hde_width;
2605 	hde->hpp.entry  = __sort__hde_entry;
2606 	hde->hpp.color  = NULL;
2607 
2608 	hde->hpp.cmp = __sort__hde_cmp;
2609 	hde->hpp.collapse = __sort__hde_cmp;
2610 	hde->hpp.sort = __sort__hde_cmp;
2611 	hde->hpp.equal = __sort__hde_equal;
2612 	hde->hpp.free = hde_free;
2613 
2614 	INIT_LIST_HEAD(&hde->hpp.list);
2615 	INIT_LIST_HEAD(&hde->hpp.sort_list);
2616 	hde->hpp.elide = false;
2617 	hde->hpp.len = 0;
2618 	hde->hpp.user_len = 0;
2619 	hde->hpp.level = level;
2620 
2621 	return hde;
2622 }
2623 
perf_hpp_fmt__dup(struct perf_hpp_fmt * fmt)2624 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
2625 {
2626 	struct perf_hpp_fmt *new_fmt = NULL;
2627 
2628 	if (perf_hpp__is_sort_entry(fmt)) {
2629 		struct hpp_sort_entry *hse, *new_hse;
2630 
2631 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
2632 		new_hse = memdup(hse, sizeof(*hse));
2633 		if (new_hse)
2634 			new_fmt = &new_hse->hpp;
2635 	} else if (perf_hpp__is_dynamic_entry(fmt)) {
2636 		struct hpp_dynamic_entry *hde, *new_hde;
2637 
2638 		hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
2639 		new_hde = memdup(hde, sizeof(*hde));
2640 		if (new_hde)
2641 			new_fmt = &new_hde->hpp;
2642 	} else {
2643 		new_fmt = memdup(fmt, sizeof(*fmt));
2644 	}
2645 
2646 	INIT_LIST_HEAD(&new_fmt->list);
2647 	INIT_LIST_HEAD(&new_fmt->sort_list);
2648 
2649 	return new_fmt;
2650 }
2651 
parse_field_name(char * str,char ** event,char ** field,char ** opt)2652 static int parse_field_name(char *str, char **event, char **field, char **opt)
2653 {
2654 	char *event_name, *field_name, *opt_name;
2655 
2656 	event_name = str;
2657 	field_name = strchr(str, '.');
2658 
2659 	if (field_name) {
2660 		*field_name++ = '\0';
2661 	} else {
2662 		event_name = NULL;
2663 		field_name = str;
2664 	}
2665 
2666 	opt_name = strchr(field_name, '/');
2667 	if (opt_name)
2668 		*opt_name++ = '\0';
2669 
2670 	*event = event_name;
2671 	*field = field_name;
2672 	*opt   = opt_name;
2673 
2674 	return 0;
2675 }
2676 
2677 /* find match evsel using a given event name.  The event name can be:
2678  *   1. '%' + event index (e.g. '%1' for first event)
2679  *   2. full event name (e.g. sched:sched_switch)
2680  *   3. partial event name (should not contain ':')
2681  */
find_evsel(struct evlist * evlist,char * event_name)2682 static struct evsel *find_evsel(struct evlist *evlist, char *event_name)
2683 {
2684 	struct evsel *evsel = NULL;
2685 	struct evsel *pos;
2686 	bool full_name;
2687 
2688 	/* case 1 */
2689 	if (event_name[0] == '%') {
2690 		int nr = strtol(event_name+1, NULL, 0);
2691 
2692 		if (nr > evlist->core.nr_entries)
2693 			return NULL;
2694 
2695 		evsel = evlist__first(evlist);
2696 		while (--nr > 0)
2697 			evsel = evsel__next(evsel);
2698 
2699 		return evsel;
2700 	}
2701 
2702 	full_name = !!strchr(event_name, ':');
2703 	evlist__for_each_entry(evlist, pos) {
2704 		/* case 2 */
2705 		if (full_name && !strcmp(pos->name, event_name))
2706 			return pos;
2707 		/* case 3 */
2708 		if (!full_name && strstr(pos->name, event_name)) {
2709 			if (evsel) {
2710 				pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
2711 					 event_name, evsel->name, pos->name);
2712 				return NULL;
2713 			}
2714 			evsel = pos;
2715 		}
2716 	}
2717 
2718 	return evsel;
2719 }
2720 
__dynamic_dimension__add(struct evsel * evsel,struct tep_format_field * field,bool raw_trace,int level)2721 static int __dynamic_dimension__add(struct evsel *evsel,
2722 				    struct tep_format_field *field,
2723 				    bool raw_trace, int level)
2724 {
2725 	struct hpp_dynamic_entry *hde;
2726 
2727 	hde = __alloc_dynamic_entry(evsel, field, level);
2728 	if (hde == NULL)
2729 		return -ENOMEM;
2730 
2731 	hde->raw_trace = raw_trace;
2732 
2733 	perf_hpp__register_sort_field(&hde->hpp);
2734 	return 0;
2735 }
2736 
add_evsel_fields(struct evsel * evsel,bool raw_trace,int level)2737 static int add_evsel_fields(struct evsel *evsel, bool raw_trace, int level)
2738 {
2739 	int ret;
2740 	struct tep_format_field *field;
2741 
2742 	field = evsel->tp_format->format.fields;
2743 	while (field) {
2744 		ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2745 		if (ret < 0)
2746 			return ret;
2747 
2748 		field = field->next;
2749 	}
2750 	return 0;
2751 }
2752 
add_all_dynamic_fields(struct evlist * evlist,bool raw_trace,int level)2753 static int add_all_dynamic_fields(struct evlist *evlist, bool raw_trace,
2754 				  int level)
2755 {
2756 	int ret;
2757 	struct evsel *evsel;
2758 
2759 	evlist__for_each_entry(evlist, evsel) {
2760 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2761 			continue;
2762 
2763 		ret = add_evsel_fields(evsel, raw_trace, level);
2764 		if (ret < 0)
2765 			return ret;
2766 	}
2767 	return 0;
2768 }
2769 
add_all_matching_fields(struct evlist * evlist,char * field_name,bool raw_trace,int level)2770 static int add_all_matching_fields(struct evlist *evlist,
2771 				   char *field_name, bool raw_trace, int level)
2772 {
2773 	int ret = -ESRCH;
2774 	struct evsel *evsel;
2775 	struct tep_format_field *field;
2776 
2777 	evlist__for_each_entry(evlist, evsel) {
2778 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
2779 			continue;
2780 
2781 		field = tep_find_any_field(evsel->tp_format, field_name);
2782 		if (field == NULL)
2783 			continue;
2784 
2785 		ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2786 		if (ret < 0)
2787 			break;
2788 	}
2789 	return ret;
2790 }
2791 
add_dynamic_entry(struct evlist * evlist,const char * tok,int level)2792 static int add_dynamic_entry(struct evlist *evlist, const char *tok,
2793 			     int level)
2794 {
2795 	char *str, *event_name, *field_name, *opt_name;
2796 	struct evsel *evsel;
2797 	struct tep_format_field *field;
2798 	bool raw_trace = symbol_conf.raw_trace;
2799 	int ret = 0;
2800 
2801 	if (evlist == NULL)
2802 		return -ENOENT;
2803 
2804 	str = strdup(tok);
2805 	if (str == NULL)
2806 		return -ENOMEM;
2807 
2808 	if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
2809 		ret = -EINVAL;
2810 		goto out;
2811 	}
2812 
2813 	if (opt_name) {
2814 		if (strcmp(opt_name, "raw")) {
2815 			pr_debug("unsupported field option %s\n", opt_name);
2816 			ret = -EINVAL;
2817 			goto out;
2818 		}
2819 		raw_trace = true;
2820 	}
2821 
2822 	if (!strcmp(field_name, "trace_fields")) {
2823 		ret = add_all_dynamic_fields(evlist, raw_trace, level);
2824 		goto out;
2825 	}
2826 
2827 	if (event_name == NULL) {
2828 		ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
2829 		goto out;
2830 	}
2831 
2832 	evsel = find_evsel(evlist, event_name);
2833 	if (evsel == NULL) {
2834 		pr_debug("Cannot find event: %s\n", event_name);
2835 		ret = -ENOENT;
2836 		goto out;
2837 	}
2838 
2839 	if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2840 		pr_debug("%s is not a tracepoint event\n", event_name);
2841 		ret = -EINVAL;
2842 		goto out;
2843 	}
2844 
2845 	if (!strcmp(field_name, "*")) {
2846 		ret = add_evsel_fields(evsel, raw_trace, level);
2847 	} else {
2848 		field = tep_find_any_field(evsel->tp_format, field_name);
2849 		if (field == NULL) {
2850 			pr_debug("Cannot find event field for %s.%s\n",
2851 				 event_name, field_name);
2852 			return -ENOENT;
2853 		}
2854 
2855 		ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
2856 	}
2857 
2858 out:
2859 	free(str);
2860 	return ret;
2861 }
2862 
__sort_dimension__add(struct sort_dimension * sd,struct perf_hpp_list * list,int level)2863 static int __sort_dimension__add(struct sort_dimension *sd,
2864 				 struct perf_hpp_list *list,
2865 				 int level)
2866 {
2867 	if (sd->taken)
2868 		return 0;
2869 
2870 	if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
2871 		return -1;
2872 
2873 	if (sd->entry->se_collapse)
2874 		list->need_collapse = 1;
2875 
2876 	sd->taken = 1;
2877 
2878 	return 0;
2879 }
2880 
__hpp_dimension__add(struct hpp_dimension * hd,struct perf_hpp_list * list,int level)2881 static int __hpp_dimension__add(struct hpp_dimension *hd,
2882 				struct perf_hpp_list *list,
2883 				int level)
2884 {
2885 	struct perf_hpp_fmt *fmt;
2886 
2887 	if (hd->taken)
2888 		return 0;
2889 
2890 	fmt = __hpp_dimension__alloc_hpp(hd, level);
2891 	if (!fmt)
2892 		return -1;
2893 
2894 	hd->taken = 1;
2895 	perf_hpp_list__register_sort_field(list, fmt);
2896 	return 0;
2897 }
2898 
__sort_dimension__add_output(struct perf_hpp_list * list,struct sort_dimension * sd)2899 static int __sort_dimension__add_output(struct perf_hpp_list *list,
2900 					struct sort_dimension *sd)
2901 {
2902 	if (sd->taken)
2903 		return 0;
2904 
2905 	if (__sort_dimension__add_hpp_output(sd, list) < 0)
2906 		return -1;
2907 
2908 	sd->taken = 1;
2909 	return 0;
2910 }
2911 
__hpp_dimension__add_output(struct perf_hpp_list * list,struct hpp_dimension * hd)2912 static int __hpp_dimension__add_output(struct perf_hpp_list *list,
2913 				       struct hpp_dimension *hd)
2914 {
2915 	struct perf_hpp_fmt *fmt;
2916 
2917 	if (hd->taken)
2918 		return 0;
2919 
2920 	fmt = __hpp_dimension__alloc_hpp(hd, 0);
2921 	if (!fmt)
2922 		return -1;
2923 
2924 	hd->taken = 1;
2925 	perf_hpp_list__column_register(list, fmt);
2926 	return 0;
2927 }
2928 
hpp_dimension__add_output(unsigned col)2929 int hpp_dimension__add_output(unsigned col)
2930 {
2931 	BUG_ON(col >= PERF_HPP__MAX_INDEX);
2932 	return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
2933 }
2934 
sort_dimension__add(struct perf_hpp_list * list,const char * tok,struct evlist * evlist,int level)2935 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
2936 			struct evlist *evlist,
2937 			int level)
2938 {
2939 	unsigned int i, j;
2940 
2941 	/*
2942 	 * Check to see if there are any arch specific
2943 	 * sort dimensions not applicable for the current
2944 	 * architecture. If so, Skip that sort key since
2945 	 * we don't want to display it in the output fields.
2946 	 */
2947 	for (j = 0; j < ARRAY_SIZE(arch_specific_sort_keys); j++) {
2948 		if (!strcmp(arch_specific_sort_keys[j], tok) &&
2949 				!arch_support_sort_key(tok)) {
2950 			return 0;
2951 		}
2952 	}
2953 
2954 	for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
2955 		struct sort_dimension *sd = &common_sort_dimensions[i];
2956 
2957 		if (strncasecmp(tok, sd->name, strlen(tok)))
2958 			continue;
2959 
2960 		for (j = 0; j < ARRAY_SIZE(dynamic_headers); j++) {
2961 			if (!strcmp(dynamic_headers[j], sd->name))
2962 				sort_dimension_add_dynamic_header(sd);
2963 		}
2964 
2965 		if (sd->entry == &sort_parent) {
2966 			int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
2967 			if (ret) {
2968 				char err[BUFSIZ];
2969 
2970 				regerror(ret, &parent_regex, err, sizeof(err));
2971 				pr_err("Invalid regex: %s\n%s", parent_pattern, err);
2972 				return -EINVAL;
2973 			}
2974 			list->parent = 1;
2975 		} else if (sd->entry == &sort_sym) {
2976 			list->sym = 1;
2977 			/*
2978 			 * perf diff displays the performance difference amongst
2979 			 * two or more perf.data files. Those files could come
2980 			 * from different binaries. So we should not compare
2981 			 * their ips, but the name of symbol.
2982 			 */
2983 			if (sort__mode == SORT_MODE__DIFF)
2984 				sd->entry->se_collapse = sort__sym_sort;
2985 
2986 		} else if (sd->entry == &sort_dso) {
2987 			list->dso = 1;
2988 		} else if (sd->entry == &sort_socket) {
2989 			list->socket = 1;
2990 		} else if (sd->entry == &sort_thread) {
2991 			list->thread = 1;
2992 		} else if (sd->entry == &sort_comm) {
2993 			list->comm = 1;
2994 		}
2995 
2996 		return __sort_dimension__add(sd, list, level);
2997 	}
2998 
2999 	for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
3000 		struct hpp_dimension *hd = &hpp_sort_dimensions[i];
3001 
3002 		if (strncasecmp(tok, hd->name, strlen(tok)))
3003 			continue;
3004 
3005 		return __hpp_dimension__add(hd, list, level);
3006 	}
3007 
3008 	for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
3009 		struct sort_dimension *sd = &bstack_sort_dimensions[i];
3010 
3011 		if (strncasecmp(tok, sd->name, strlen(tok)))
3012 			continue;
3013 
3014 		if (sort__mode != SORT_MODE__BRANCH)
3015 			return -EINVAL;
3016 
3017 		if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
3018 			list->sym = 1;
3019 
3020 		__sort_dimension__add(sd, list, level);
3021 		return 0;
3022 	}
3023 
3024 	for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
3025 		struct sort_dimension *sd = &memory_sort_dimensions[i];
3026 
3027 		if (strncasecmp(tok, sd->name, strlen(tok)))
3028 			continue;
3029 
3030 		if (sort__mode != SORT_MODE__MEMORY)
3031 			return -EINVAL;
3032 
3033 		if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
3034 			return -EINVAL;
3035 
3036 		if (sd->entry == &sort_mem_daddr_sym)
3037 			list->sym = 1;
3038 
3039 		__sort_dimension__add(sd, list, level);
3040 		return 0;
3041 	}
3042 
3043 	if (!add_dynamic_entry(evlist, tok, level))
3044 		return 0;
3045 
3046 	return -ESRCH;
3047 }
3048 
setup_sort_list(struct perf_hpp_list * list,char * str,struct evlist * evlist)3049 static int setup_sort_list(struct perf_hpp_list *list, char *str,
3050 			   struct evlist *evlist)
3051 {
3052 	char *tmp, *tok;
3053 	int ret = 0;
3054 	int level = 0;
3055 	int next_level = 1;
3056 	bool in_group = false;
3057 
3058 	do {
3059 		tok = str;
3060 		tmp = strpbrk(str, "{}, ");
3061 		if (tmp) {
3062 			if (in_group)
3063 				next_level = level;
3064 			else
3065 				next_level = level + 1;
3066 
3067 			if (*tmp == '{')
3068 				in_group = true;
3069 			else if (*tmp == '}')
3070 				in_group = false;
3071 
3072 			*tmp = '\0';
3073 			str = tmp + 1;
3074 		}
3075 
3076 		if (*tok) {
3077 			ret = sort_dimension__add(list, tok, evlist, level);
3078 			if (ret == -EINVAL) {
3079 				if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
3080 					ui__error("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
3081 				else
3082 					ui__error("Invalid --sort key: `%s'", tok);
3083 				break;
3084 			} else if (ret == -ESRCH) {
3085 				ui__error("Unknown --sort key: `%s'", tok);
3086 				break;
3087 			}
3088 		}
3089 
3090 		level = next_level;
3091 	} while (tmp);
3092 
3093 	return ret;
3094 }
3095 
get_default_sort_order(struct evlist * evlist)3096 static const char *get_default_sort_order(struct evlist *evlist)
3097 {
3098 	const char *default_sort_orders[] = {
3099 		default_sort_order,
3100 		default_branch_sort_order,
3101 		default_mem_sort_order,
3102 		default_top_sort_order,
3103 		default_diff_sort_order,
3104 		default_tracepoint_sort_order,
3105 	};
3106 	bool use_trace = true;
3107 	struct evsel *evsel;
3108 
3109 	BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
3110 
3111 	if (evlist == NULL || evlist__empty(evlist))
3112 		goto out_no_evlist;
3113 
3114 	evlist__for_each_entry(evlist, evsel) {
3115 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
3116 			use_trace = false;
3117 			break;
3118 		}
3119 	}
3120 
3121 	if (use_trace) {
3122 		sort__mode = SORT_MODE__TRACEPOINT;
3123 		if (symbol_conf.raw_trace)
3124 			return "trace_fields";
3125 	}
3126 out_no_evlist:
3127 	return default_sort_orders[sort__mode];
3128 }
3129 
setup_sort_order(struct evlist * evlist)3130 static int setup_sort_order(struct evlist *evlist)
3131 {
3132 	char *new_sort_order;
3133 
3134 	/*
3135 	 * Append '+'-prefixed sort order to the default sort
3136 	 * order string.
3137 	 */
3138 	if (!sort_order || is_strict_order(sort_order))
3139 		return 0;
3140 
3141 	if (sort_order[1] == '\0') {
3142 		ui__error("Invalid --sort key: `+'");
3143 		return -EINVAL;
3144 	}
3145 
3146 	/*
3147 	 * We allocate new sort_order string, but we never free it,
3148 	 * because it's checked over the rest of the code.
3149 	 */
3150 	if (asprintf(&new_sort_order, "%s,%s",
3151 		     get_default_sort_order(evlist), sort_order + 1) < 0) {
3152 		pr_err("Not enough memory to set up --sort");
3153 		return -ENOMEM;
3154 	}
3155 
3156 	sort_order = new_sort_order;
3157 	return 0;
3158 }
3159 
3160 /*
3161  * Adds 'pre,' prefix into 'str' is 'pre' is
3162  * not already part of 'str'.
3163  */
prefix_if_not_in(const char * pre,char * str)3164 static char *prefix_if_not_in(const char *pre, char *str)
3165 {
3166 	char *n;
3167 
3168 	if (!str || strstr(str, pre))
3169 		return str;
3170 
3171 	if (asprintf(&n, "%s,%s", pre, str) < 0)
3172 		n = NULL;
3173 
3174 	free(str);
3175 	return n;
3176 }
3177 
setup_overhead(char * keys)3178 static char *setup_overhead(char *keys)
3179 {
3180 	if (sort__mode == SORT_MODE__DIFF)
3181 		return keys;
3182 
3183 	keys = prefix_if_not_in("overhead", keys);
3184 
3185 	if (symbol_conf.cumulate_callchain)
3186 		keys = prefix_if_not_in("overhead_children", keys);
3187 
3188 	return keys;
3189 }
3190 
__setup_sorting(struct evlist * evlist)3191 static int __setup_sorting(struct evlist *evlist)
3192 {
3193 	char *str;
3194 	const char *sort_keys;
3195 	int ret = 0;
3196 
3197 	ret = setup_sort_order(evlist);
3198 	if (ret)
3199 		return ret;
3200 
3201 	sort_keys = sort_order;
3202 	if (sort_keys == NULL) {
3203 		if (is_strict_order(field_order)) {
3204 			/*
3205 			 * If user specified field order but no sort order,
3206 			 * we'll honor it and not add default sort orders.
3207 			 */
3208 			return 0;
3209 		}
3210 
3211 		sort_keys = get_default_sort_order(evlist);
3212 	}
3213 
3214 	str = strdup(sort_keys);
3215 	if (str == NULL) {
3216 		pr_err("Not enough memory to setup sort keys");
3217 		return -ENOMEM;
3218 	}
3219 
3220 	/*
3221 	 * Prepend overhead fields for backward compatibility.
3222 	 */
3223 	if (!is_strict_order(field_order)) {
3224 		str = setup_overhead(str);
3225 		if (str == NULL) {
3226 			pr_err("Not enough memory to setup overhead keys");
3227 			return -ENOMEM;
3228 		}
3229 	}
3230 
3231 	ret = setup_sort_list(&perf_hpp_list, str, evlist);
3232 
3233 	free(str);
3234 	return ret;
3235 }
3236 
perf_hpp__set_elide(int idx,bool elide)3237 void perf_hpp__set_elide(int idx, bool elide)
3238 {
3239 	struct perf_hpp_fmt *fmt;
3240 	struct hpp_sort_entry *hse;
3241 
3242 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3243 		if (!perf_hpp__is_sort_entry(fmt))
3244 			continue;
3245 
3246 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
3247 		if (hse->se->se_width_idx == idx) {
3248 			fmt->elide = elide;
3249 			break;
3250 		}
3251 	}
3252 }
3253 
__get_elide(struct strlist * list,const char * list_name,FILE * fp)3254 static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
3255 {
3256 	if (list && strlist__nr_entries(list) == 1) {
3257 		if (fp != NULL)
3258 			fprintf(fp, "# %s: %s\n", list_name,
3259 				strlist__entry(list, 0)->s);
3260 		return true;
3261 	}
3262 	return false;
3263 }
3264 
get_elide(int idx,FILE * output)3265 static bool get_elide(int idx, FILE *output)
3266 {
3267 	switch (idx) {
3268 	case HISTC_SYMBOL:
3269 		return __get_elide(symbol_conf.sym_list, "symbol", output);
3270 	case HISTC_DSO:
3271 		return __get_elide(symbol_conf.dso_list, "dso", output);
3272 	case HISTC_COMM:
3273 		return __get_elide(symbol_conf.comm_list, "comm", output);
3274 	default:
3275 		break;
3276 	}
3277 
3278 	if (sort__mode != SORT_MODE__BRANCH)
3279 		return false;
3280 
3281 	switch (idx) {
3282 	case HISTC_SYMBOL_FROM:
3283 		return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
3284 	case HISTC_SYMBOL_TO:
3285 		return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
3286 	case HISTC_DSO_FROM:
3287 		return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
3288 	case HISTC_DSO_TO:
3289 		return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
3290 	case HISTC_ADDR_FROM:
3291 		return __get_elide(symbol_conf.sym_from_list, "addr_from", output);
3292 	case HISTC_ADDR_TO:
3293 		return __get_elide(symbol_conf.sym_to_list, "addr_to", output);
3294 	default:
3295 		break;
3296 	}
3297 
3298 	return false;
3299 }
3300 
sort__setup_elide(FILE * output)3301 void sort__setup_elide(FILE *output)
3302 {
3303 	struct perf_hpp_fmt *fmt;
3304 	struct hpp_sort_entry *hse;
3305 
3306 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3307 		if (!perf_hpp__is_sort_entry(fmt))
3308 			continue;
3309 
3310 		hse = container_of(fmt, struct hpp_sort_entry, hpp);
3311 		fmt->elide = get_elide(hse->se->se_width_idx, output);
3312 	}
3313 
3314 	/*
3315 	 * It makes no sense to elide all of sort entries.
3316 	 * Just revert them to show up again.
3317 	 */
3318 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3319 		if (!perf_hpp__is_sort_entry(fmt))
3320 			continue;
3321 
3322 		if (!fmt->elide)
3323 			return;
3324 	}
3325 
3326 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
3327 		if (!perf_hpp__is_sort_entry(fmt))
3328 			continue;
3329 
3330 		fmt->elide = false;
3331 	}
3332 }
3333 
output_field_add(struct perf_hpp_list * list,char * tok)3334 int output_field_add(struct perf_hpp_list *list, char *tok)
3335 {
3336 	unsigned int i;
3337 
3338 	for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
3339 		struct sort_dimension *sd = &common_sort_dimensions[i];
3340 
3341 		if (strncasecmp(tok, sd->name, strlen(tok)))
3342 			continue;
3343 
3344 		return __sort_dimension__add_output(list, sd);
3345 	}
3346 
3347 	for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
3348 		struct hpp_dimension *hd = &hpp_sort_dimensions[i];
3349 
3350 		if (strncasecmp(tok, hd->name, strlen(tok)))
3351 			continue;
3352 
3353 		return __hpp_dimension__add_output(list, hd);
3354 	}
3355 
3356 	for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
3357 		struct sort_dimension *sd = &bstack_sort_dimensions[i];
3358 
3359 		if (strncasecmp(tok, sd->name, strlen(tok)))
3360 			continue;
3361 
3362 		if (sort__mode != SORT_MODE__BRANCH)
3363 			return -EINVAL;
3364 
3365 		return __sort_dimension__add_output(list, sd);
3366 	}
3367 
3368 	for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
3369 		struct sort_dimension *sd = &memory_sort_dimensions[i];
3370 
3371 		if (strncasecmp(tok, sd->name, strlen(tok)))
3372 			continue;
3373 
3374 		if (sort__mode != SORT_MODE__MEMORY)
3375 			return -EINVAL;
3376 
3377 		return __sort_dimension__add_output(list, sd);
3378 	}
3379 
3380 	return -ESRCH;
3381 }
3382 
setup_output_list(struct perf_hpp_list * list,char * str)3383 static int setup_output_list(struct perf_hpp_list *list, char *str)
3384 {
3385 	char *tmp, *tok;
3386 	int ret = 0;
3387 
3388 	for (tok = strtok_r(str, ", ", &tmp);
3389 			tok; tok = strtok_r(NULL, ", ", &tmp)) {
3390 		ret = output_field_add(list, tok);
3391 		if (ret == -EINVAL) {
3392 			ui__error("Invalid --fields key: `%s'", tok);
3393 			break;
3394 		} else if (ret == -ESRCH) {
3395 			ui__error("Unknown --fields key: `%s'", tok);
3396 			break;
3397 		}
3398 	}
3399 
3400 	return ret;
3401 }
3402 
reset_dimensions(void)3403 void reset_dimensions(void)
3404 {
3405 	unsigned int i;
3406 
3407 	for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
3408 		common_sort_dimensions[i].taken = 0;
3409 
3410 	for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
3411 		hpp_sort_dimensions[i].taken = 0;
3412 
3413 	for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
3414 		bstack_sort_dimensions[i].taken = 0;
3415 
3416 	for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
3417 		memory_sort_dimensions[i].taken = 0;
3418 }
3419 
is_strict_order(const char * order)3420 bool is_strict_order(const char *order)
3421 {
3422 	return order && (*order != '+');
3423 }
3424 
__setup_output_field(void)3425 static int __setup_output_field(void)
3426 {
3427 	char *str, *strp;
3428 	int ret = -EINVAL;
3429 
3430 	if (field_order == NULL)
3431 		return 0;
3432 
3433 	strp = str = strdup(field_order);
3434 	if (str == NULL) {
3435 		pr_err("Not enough memory to setup output fields");
3436 		return -ENOMEM;
3437 	}
3438 
3439 	if (!is_strict_order(field_order))
3440 		strp++;
3441 
3442 	if (!strlen(strp)) {
3443 		ui__error("Invalid --fields key: `+'");
3444 		goto out;
3445 	}
3446 
3447 	ret = setup_output_list(&perf_hpp_list, strp);
3448 
3449 out:
3450 	free(str);
3451 	return ret;
3452 }
3453 
setup_sorting(struct evlist * evlist)3454 int setup_sorting(struct evlist *evlist)
3455 {
3456 	int err;
3457 
3458 	err = __setup_sorting(evlist);
3459 	if (err < 0)
3460 		return err;
3461 
3462 	if (parent_pattern != default_parent_pattern) {
3463 		err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
3464 		if (err < 0)
3465 			return err;
3466 	}
3467 
3468 	reset_dimensions();
3469 
3470 	/*
3471 	 * perf diff doesn't use default hpp output fields.
3472 	 */
3473 	if (sort__mode != SORT_MODE__DIFF)
3474 		perf_hpp__init();
3475 
3476 	err = __setup_output_field();
3477 	if (err < 0)
3478 		return err;
3479 
3480 	/* copy sort keys to output fields */
3481 	perf_hpp__setup_output_field(&perf_hpp_list);
3482 	/* and then copy output fields to sort keys */
3483 	perf_hpp__append_sort_keys(&perf_hpp_list);
3484 
3485 	/* setup hists-specific output fields */
3486 	if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
3487 		return -1;
3488 
3489 	return 0;
3490 }
3491 
reset_output_field(void)3492 void reset_output_field(void)
3493 {
3494 	perf_hpp_list.need_collapse = 0;
3495 	perf_hpp_list.parent = 0;
3496 	perf_hpp_list.sym = 0;
3497 	perf_hpp_list.dso = 0;
3498 
3499 	field_order = NULL;
3500 	sort_order = NULL;
3501 
3502 	reset_dimensions();
3503 	perf_hpp__reset_output_field(&perf_hpp_list);
3504 }
3505 
3506 #define INDENT (3*8 + 1)
3507 
add_key(struct strbuf * sb,const char * str,int * llen)3508 static void add_key(struct strbuf *sb, const char *str, int *llen)
3509 {
3510 	if (*llen >= 75) {
3511 		strbuf_addstr(sb, "\n\t\t\t ");
3512 		*llen = INDENT;
3513 	}
3514 	strbuf_addf(sb, " %s", str);
3515 	*llen += strlen(str) + 1;
3516 }
3517 
add_sort_string(struct strbuf * sb,struct sort_dimension * s,int n,int * llen)3518 static void add_sort_string(struct strbuf *sb, struct sort_dimension *s, int n,
3519 			    int *llen)
3520 {
3521 	int i;
3522 
3523 	for (i = 0; i < n; i++)
3524 		add_key(sb, s[i].name, llen);
3525 }
3526 
add_hpp_sort_string(struct strbuf * sb,struct hpp_dimension * s,int n,int * llen)3527 static void add_hpp_sort_string(struct strbuf *sb, struct hpp_dimension *s, int n,
3528 				int *llen)
3529 {
3530 	int i;
3531 
3532 	for (i = 0; i < n; i++)
3533 		add_key(sb, s[i].name, llen);
3534 }
3535 
sort_help(const char * prefix)3536 char *sort_help(const char *prefix)
3537 {
3538 	struct strbuf sb;
3539 	char *s;
3540 	int len = strlen(prefix) + INDENT;
3541 
3542 	strbuf_init(&sb, 300);
3543 	strbuf_addstr(&sb, prefix);
3544 	add_hpp_sort_string(&sb, hpp_sort_dimensions,
3545 			    ARRAY_SIZE(hpp_sort_dimensions), &len);
3546 	add_sort_string(&sb, common_sort_dimensions,
3547 			    ARRAY_SIZE(common_sort_dimensions), &len);
3548 	add_sort_string(&sb, bstack_sort_dimensions,
3549 			    ARRAY_SIZE(bstack_sort_dimensions), &len);
3550 	add_sort_string(&sb, memory_sort_dimensions,
3551 			    ARRAY_SIZE(memory_sort_dimensions), &len);
3552 	s = strbuf_detach(&sb, NULL);
3553 	strbuf_release(&sb);
3554 	return s;
3555 }
3556