xref: /openbmc/linux/tools/perf/ui/hist.c (revision b42090256fba05dce1a0482a4ccd9bb6464cc499)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <math.h>
4 #include <linux/compiler.h>
5 
6 #include "../util/callchain.h"
7 #include "../util/debug.h"
8 #include "../util/hist.h"
9 #include "../util/util.h"
10 #include "../util/sort.h"
11 #include "../util/evsel.h"
12 #include "../util/evlist.h"
13 #include "../perf.h"
14 
15 /* hist period print (hpp) functions */
16 
17 #define hpp__call_print_fn(hpp, fn, fmt, ...)			\
18 ({								\
19 	int __ret = fn(hpp, fmt, ##__VA_ARGS__);		\
20 	advance_hpp(hpp, __ret);				\
21 	__ret;							\
22 })
23 
24 static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
25 		      hpp_field_fn get_field, const char *fmt, int len,
26 		      hpp_snprint_fn print_fn, bool fmt_percent)
27 {
28 	int ret;
29 	struct hists *hists = he->hists;
30 	struct evsel *evsel = hists_to_evsel(hists);
31 	char *buf = hpp->buf;
32 	size_t size = hpp->size;
33 
34 	if (fmt_percent) {
35 		double percent = 0.0;
36 		u64 total = hists__total_period(hists);
37 
38 		if (total)
39 			percent = 100.0 * get_field(he) / total;
40 
41 		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
42 	} else
43 		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
44 
45 	if (perf_evsel__is_group_event(evsel)) {
46 		int prev_idx, idx_delta;
47 		struct hist_entry *pair;
48 		int nr_members = evsel->core.nr_members;
49 
50 		prev_idx = perf_evsel__group_idx(evsel);
51 
52 		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
53 			u64 period = get_field(pair);
54 			u64 total = hists__total_period(pair->hists);
55 
56 			if (!total)
57 				continue;
58 
59 			evsel = hists_to_evsel(pair->hists);
60 			idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
61 
62 			while (idx_delta--) {
63 				/*
64 				 * zero-fill group members in the middle which
65 				 * have no sample
66 				 */
67 				if (fmt_percent) {
68 					ret += hpp__call_print_fn(hpp, print_fn,
69 								  fmt, len, 0.0);
70 				} else {
71 					ret += hpp__call_print_fn(hpp, print_fn,
72 								  fmt, len, 0ULL);
73 				}
74 			}
75 
76 			if (fmt_percent) {
77 				ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
78 							  100.0 * period / total);
79 			} else {
80 				ret += hpp__call_print_fn(hpp, print_fn, fmt,
81 							  len, period);
82 			}
83 
84 			prev_idx = perf_evsel__group_idx(evsel);
85 		}
86 
87 		idx_delta = nr_members - prev_idx - 1;
88 
89 		while (idx_delta--) {
90 			/*
91 			 * zero-fill group members at last which have no sample
92 			 */
93 			if (fmt_percent) {
94 				ret += hpp__call_print_fn(hpp, print_fn,
95 							  fmt, len, 0.0);
96 			} else {
97 				ret += hpp__call_print_fn(hpp, print_fn,
98 							  fmt, len, 0ULL);
99 			}
100 		}
101 	}
102 
103 	/*
104 	 * Restore original buf and size as it's where caller expects
105 	 * the result will be saved.
106 	 */
107 	hpp->buf = buf;
108 	hpp->size = size;
109 
110 	return ret;
111 }
112 
113 int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
114 	     struct hist_entry *he, hpp_field_fn get_field,
115 	     const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
116 {
117 	int len = fmt->user_len ?: fmt->len;
118 
119 	if (symbol_conf.field_sep) {
120 		return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
121 				  print_fn, fmt_percent);
122 	}
123 
124 	if (fmt_percent)
125 		len -= 2; /* 2 for a space and a % sign */
126 	else
127 		len -= 1;
128 
129 	return  __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
130 }
131 
132 int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
133 		 struct hist_entry *he, hpp_field_fn get_field,
134 		 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
135 {
136 	if (!symbol_conf.cumulate_callchain) {
137 		int len = fmt->user_len ?: fmt->len;
138 		return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
139 	}
140 
141 	return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
142 }
143 
144 static int field_cmp(u64 field_a, u64 field_b)
145 {
146 	if (field_a > field_b)
147 		return 1;
148 	if (field_a < field_b)
149 		return -1;
150 	return 0;
151 }
152 
153 static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
154 		       hpp_field_fn get_field)
155 {
156 	s64 ret;
157 	int i, nr_members;
158 	struct evsel *evsel;
159 	struct hist_entry *pair;
160 	u64 *fields_a, *fields_b;
161 
162 	ret = field_cmp(get_field(a), get_field(b));
163 	if (ret || !symbol_conf.event_group)
164 		return ret;
165 
166 	evsel = hists_to_evsel(a->hists);
167 	if (!perf_evsel__is_group_event(evsel))
168 		return ret;
169 
170 	nr_members = evsel->core.nr_members;
171 	fields_a = calloc(nr_members, sizeof(*fields_a));
172 	fields_b = calloc(nr_members, sizeof(*fields_b));
173 
174 	if (!fields_a || !fields_b)
175 		goto out;
176 
177 	list_for_each_entry(pair, &a->pairs.head, pairs.node) {
178 		evsel = hists_to_evsel(pair->hists);
179 		fields_a[perf_evsel__group_idx(evsel)] = get_field(pair);
180 	}
181 
182 	list_for_each_entry(pair, &b->pairs.head, pairs.node) {
183 		evsel = hists_to_evsel(pair->hists);
184 		fields_b[perf_evsel__group_idx(evsel)] = get_field(pair);
185 	}
186 
187 	for (i = 1; i < nr_members; i++) {
188 		ret = field_cmp(fields_a[i], fields_b[i]);
189 		if (ret)
190 			break;
191 	}
192 
193 out:
194 	free(fields_a);
195 	free(fields_b);
196 
197 	return ret;
198 }
199 
200 static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
201 			   hpp_field_fn get_field)
202 {
203 	s64 ret = 0;
204 
205 	if (symbol_conf.cumulate_callchain) {
206 		/*
207 		 * Put caller above callee when they have equal period.
208 		 */
209 		ret = field_cmp(get_field(a), get_field(b));
210 		if (ret)
211 			return ret;
212 
213 		if (a->thread != b->thread || !hist_entry__has_callchains(a) || !symbol_conf.use_callchain)
214 			return 0;
215 
216 		ret = b->callchain->max_depth - a->callchain->max_depth;
217 		if (callchain_param.order == ORDER_CALLER)
218 			ret = -ret;
219 	}
220 	return ret;
221 }
222 
223 static int hpp__width_fn(struct perf_hpp_fmt *fmt,
224 			 struct perf_hpp *hpp __maybe_unused,
225 			 struct hists *hists)
226 {
227 	int len = fmt->user_len ?: fmt->len;
228 	struct evsel *evsel = hists_to_evsel(hists);
229 
230 	if (symbol_conf.event_group)
231 		len = max(len, evsel->core.nr_members * fmt->len);
232 
233 	if (len < (int)strlen(fmt->name))
234 		len = strlen(fmt->name);
235 
236 	return len;
237 }
238 
239 static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
240 			  struct hists *hists, int line __maybe_unused,
241 			  int *span __maybe_unused)
242 {
243 	int len = hpp__width_fn(fmt, hpp, hists);
244 	return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
245 }
246 
247 int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
248 {
249 	va_list args;
250 	ssize_t ssize = hpp->size;
251 	double percent;
252 	int ret, len;
253 
254 	va_start(args, fmt);
255 	len = va_arg(args, int);
256 	percent = va_arg(args, double);
257 	ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
258 	va_end(args);
259 
260 	return (ret >= ssize) ? (ssize - 1) : ret;
261 }
262 
263 static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
264 {
265 	va_list args;
266 	ssize_t ssize = hpp->size;
267 	int ret;
268 
269 	va_start(args, fmt);
270 	ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
271 	va_end(args);
272 
273 	return (ret >= ssize) ? (ssize - 1) : ret;
274 }
275 
276 #define __HPP_COLOR_PERCENT_FN(_type, _field)					\
277 static u64 he_get_##_field(struct hist_entry *he)				\
278 {										\
279 	return he->stat._field;							\
280 }										\
281 										\
282 static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
283 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
284 {										\
285 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
286 			hpp_color_scnprintf, true);				\
287 }
288 
289 #define __HPP_ENTRY_PERCENT_FN(_type, _field)					\
290 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
291 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
292 {										\
293 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
294 			hpp_entry_scnprintf, true);				\
295 }
296 
297 #define __HPP_SORT_FN(_type, _field)						\
298 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
299 				 struct hist_entry *a, struct hist_entry *b) 	\
300 {										\
301 	return __hpp__sort(a, b, he_get_##_field);				\
302 }
303 
304 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
305 static u64 he_get_acc_##_field(struct hist_entry *he)				\
306 {										\
307 	return he->stat_acc->_field;						\
308 }										\
309 										\
310 static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
311 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
312 {										\
313 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", 	\
314 			    hpp_color_scnprintf, true);				\
315 }
316 
317 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
318 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
319 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
320 {										\
321 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%",	\
322 			    hpp_entry_scnprintf, true);				\
323 }
324 
325 #define __HPP_SORT_ACC_FN(_type, _field)					\
326 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
327 				 struct hist_entry *a, struct hist_entry *b) 	\
328 {										\
329 	return __hpp__sort_acc(a, b, he_get_acc_##_field);			\
330 }
331 
332 #define __HPP_ENTRY_RAW_FN(_type, _field)					\
333 static u64 he_get_raw_##_field(struct hist_entry *he)				\
334 {										\
335 	return he->stat._field;							\
336 }										\
337 										\
338 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
339 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
340 {										\
341 	return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, 	\
342 			hpp_entry_scnprintf, false);				\
343 }
344 
345 #define __HPP_SORT_RAW_FN(_type, _field)					\
346 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
347 				 struct hist_entry *a, struct hist_entry *b) 	\
348 {										\
349 	return __hpp__sort(a, b, he_get_raw_##_field);				\
350 }
351 
352 
353 #define HPP_PERCENT_FNS(_type, _field)					\
354 __HPP_COLOR_PERCENT_FN(_type, _field)					\
355 __HPP_ENTRY_PERCENT_FN(_type, _field)					\
356 __HPP_SORT_FN(_type, _field)
357 
358 #define HPP_PERCENT_ACC_FNS(_type, _field)				\
359 __HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
360 __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
361 __HPP_SORT_ACC_FN(_type, _field)
362 
363 #define HPP_RAW_FNS(_type, _field)					\
364 __HPP_ENTRY_RAW_FN(_type, _field)					\
365 __HPP_SORT_RAW_FN(_type, _field)
366 
367 HPP_PERCENT_FNS(overhead, period)
368 HPP_PERCENT_FNS(overhead_sys, period_sys)
369 HPP_PERCENT_FNS(overhead_us, period_us)
370 HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
371 HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
372 HPP_PERCENT_ACC_FNS(overhead_acc, period)
373 
374 HPP_RAW_FNS(samples, nr_events)
375 HPP_RAW_FNS(period, period)
376 
377 static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
378 			    struct hist_entry *a __maybe_unused,
379 			    struct hist_entry *b __maybe_unused)
380 {
381 	return 0;
382 }
383 
384 static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
385 {
386 	return a->header == hpp__header_fn;
387 }
388 
389 static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
390 {
391 	if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
392 		return false;
393 
394 	return a->idx == b->idx;
395 }
396 
397 #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx)		\
398 	{						\
399 		.name   = _name,			\
400 		.header	= hpp__header_fn,		\
401 		.width	= hpp__width_fn,		\
402 		.color	= hpp__color_ ## _fn,		\
403 		.entry	= hpp__entry_ ## _fn,		\
404 		.cmp	= hpp__nop_cmp,			\
405 		.collapse = hpp__nop_cmp,		\
406 		.sort	= hpp__sort_ ## _fn,		\
407 		.idx	= PERF_HPP__ ## _idx,		\
408 		.equal	= hpp__equal,			\
409 	}
410 
411 #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx)	\
412 	{						\
413 		.name   = _name,			\
414 		.header	= hpp__header_fn,		\
415 		.width	= hpp__width_fn,		\
416 		.color	= hpp__color_ ## _fn,		\
417 		.entry	= hpp__entry_ ## _fn,		\
418 		.cmp	= hpp__nop_cmp,			\
419 		.collapse = hpp__nop_cmp,		\
420 		.sort	= hpp__sort_ ## _fn,		\
421 		.idx	= PERF_HPP__ ## _idx,		\
422 		.equal	= hpp__equal,			\
423 	}
424 
425 #define HPP__PRINT_FNS(_name, _fn, _idx)		\
426 	{						\
427 		.name   = _name,			\
428 		.header	= hpp__header_fn,		\
429 		.width	= hpp__width_fn,		\
430 		.entry	= hpp__entry_ ## _fn,		\
431 		.cmp	= hpp__nop_cmp,			\
432 		.collapse = hpp__nop_cmp,		\
433 		.sort	= hpp__sort_ ## _fn,		\
434 		.idx	= PERF_HPP__ ## _idx,		\
435 		.equal	= hpp__equal,			\
436 	}
437 
438 struct perf_hpp_fmt perf_hpp__format[] = {
439 	HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
440 	HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
441 	HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
442 	HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
443 	HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
444 	HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
445 	HPP__PRINT_FNS("Samples", samples, SAMPLES),
446 	HPP__PRINT_FNS("Period", period, PERIOD)
447 };
448 
449 struct perf_hpp_list perf_hpp_list = {
450 	.fields	= LIST_HEAD_INIT(perf_hpp_list.fields),
451 	.sorts	= LIST_HEAD_INIT(perf_hpp_list.sorts),
452 	.nr_header_lines = 1,
453 };
454 
455 #undef HPP__COLOR_PRINT_FNS
456 #undef HPP__COLOR_ACC_PRINT_FNS
457 #undef HPP__PRINT_FNS
458 
459 #undef HPP_PERCENT_FNS
460 #undef HPP_PERCENT_ACC_FNS
461 #undef HPP_RAW_FNS
462 
463 #undef __HPP_HEADER_FN
464 #undef __HPP_WIDTH_FN
465 #undef __HPP_COLOR_PERCENT_FN
466 #undef __HPP_ENTRY_PERCENT_FN
467 #undef __HPP_COLOR_ACC_PERCENT_FN
468 #undef __HPP_ENTRY_ACC_PERCENT_FN
469 #undef __HPP_ENTRY_RAW_FN
470 #undef __HPP_SORT_FN
471 #undef __HPP_SORT_ACC_FN
472 #undef __HPP_SORT_RAW_FN
473 
474 
475 void perf_hpp__init(void)
476 {
477 	int i;
478 
479 	for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
480 		struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
481 
482 		INIT_LIST_HEAD(&fmt->list);
483 
484 		/* sort_list may be linked by setup_sorting() */
485 		if (fmt->sort_list.next == NULL)
486 			INIT_LIST_HEAD(&fmt->sort_list);
487 	}
488 
489 	/*
490 	 * If user specified field order, no need to setup default fields.
491 	 */
492 	if (is_strict_order(field_order))
493 		return;
494 
495 	if (symbol_conf.cumulate_callchain) {
496 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
497 		perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
498 	}
499 
500 	hpp_dimension__add_output(PERF_HPP__OVERHEAD);
501 
502 	if (symbol_conf.show_cpu_utilization) {
503 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
504 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
505 
506 		if (perf_guest) {
507 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
508 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
509 		}
510 	}
511 
512 	if (symbol_conf.show_nr_samples)
513 		hpp_dimension__add_output(PERF_HPP__SAMPLES);
514 
515 	if (symbol_conf.show_total_period)
516 		hpp_dimension__add_output(PERF_HPP__PERIOD);
517 }
518 
519 void perf_hpp_list__column_register(struct perf_hpp_list *list,
520 				    struct perf_hpp_fmt *format)
521 {
522 	list_add_tail(&format->list, &list->fields);
523 }
524 
525 void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
526 					struct perf_hpp_fmt *format)
527 {
528 	list_add_tail(&format->sort_list, &list->sorts);
529 }
530 
531 void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
532 				       struct perf_hpp_fmt *format)
533 {
534 	list_add(&format->sort_list, &list->sorts);
535 }
536 
537 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
538 {
539 	list_del_init(&format->list);
540 }
541 
542 void perf_hpp__cancel_cumulate(void)
543 {
544 	struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
545 
546 	if (is_strict_order(field_order))
547 		return;
548 
549 	ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
550 	acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
551 
552 	perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
553 		if (acc->equal(acc, fmt)) {
554 			perf_hpp__column_unregister(fmt);
555 			continue;
556 		}
557 
558 		if (ovh->equal(ovh, fmt))
559 			fmt->name = "Overhead";
560 	}
561 }
562 
563 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
564 {
565 	return a->equal && a->equal(a, b);
566 }
567 
568 void perf_hpp__setup_output_field(struct perf_hpp_list *list)
569 {
570 	struct perf_hpp_fmt *fmt;
571 
572 	/* append sort keys to output field */
573 	perf_hpp_list__for_each_sort_list(list, fmt) {
574 		struct perf_hpp_fmt *pos;
575 
576 		/* skip sort-only fields ("sort_compute" in perf diff) */
577 		if (!fmt->entry && !fmt->color)
578 			continue;
579 
580 		perf_hpp_list__for_each_format(list, pos) {
581 			if (fmt_equal(fmt, pos))
582 				goto next;
583 		}
584 
585 		perf_hpp__column_register(fmt);
586 next:
587 		continue;
588 	}
589 }
590 
591 void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
592 {
593 	struct perf_hpp_fmt *fmt;
594 
595 	/* append output fields to sort keys */
596 	perf_hpp_list__for_each_format(list, fmt) {
597 		struct perf_hpp_fmt *pos;
598 
599 		perf_hpp_list__for_each_sort_list(list, pos) {
600 			if (fmt_equal(fmt, pos))
601 				goto next;
602 		}
603 
604 		perf_hpp__register_sort_field(fmt);
605 next:
606 		continue;
607 	}
608 }
609 
610 
611 static void fmt_free(struct perf_hpp_fmt *fmt)
612 {
613 	/*
614 	 * At this point fmt should be completely
615 	 * unhooked, if not it's a bug.
616 	 */
617 	BUG_ON(!list_empty(&fmt->list));
618 	BUG_ON(!list_empty(&fmt->sort_list));
619 
620 	if (fmt->free)
621 		fmt->free(fmt);
622 }
623 
624 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
625 {
626 	struct perf_hpp_fmt *fmt, *tmp;
627 
628 	/* reset output fields */
629 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
630 		list_del_init(&fmt->list);
631 		list_del_init(&fmt->sort_list);
632 		fmt_free(fmt);
633 	}
634 
635 	/* reset sort keys */
636 	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
637 		list_del_init(&fmt->list);
638 		list_del_init(&fmt->sort_list);
639 		fmt_free(fmt);
640 	}
641 }
642 
643 /*
644  * See hists__fprintf to match the column widths
645  */
646 unsigned int hists__sort_list_width(struct hists *hists)
647 {
648 	struct perf_hpp_fmt *fmt;
649 	int ret = 0;
650 	bool first = true;
651 	struct perf_hpp dummy_hpp;
652 
653 	hists__for_each_format(hists, fmt) {
654 		if (perf_hpp__should_skip(fmt, hists))
655 			continue;
656 
657 		if (first)
658 			first = false;
659 		else
660 			ret += 2;
661 
662 		ret += fmt->width(fmt, &dummy_hpp, hists);
663 	}
664 
665 	if (verbose > 0 && hists__has(hists, sym)) /* Addr + origin */
666 		ret += 3 + BITS_PER_LONG / 4;
667 
668 	return ret;
669 }
670 
671 unsigned int hists__overhead_width(struct hists *hists)
672 {
673 	struct perf_hpp_fmt *fmt;
674 	int ret = 0;
675 	bool first = true;
676 	struct perf_hpp dummy_hpp;
677 
678 	hists__for_each_format(hists, fmt) {
679 		if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
680 			break;
681 
682 		if (first)
683 			first = false;
684 		else
685 			ret += 2;
686 
687 		ret += fmt->width(fmt, &dummy_hpp, hists);
688 	}
689 
690 	return ret;
691 }
692 
693 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
694 {
695 	if (perf_hpp__is_sort_entry(fmt))
696 		return perf_hpp__reset_sort_width(fmt, hists);
697 
698 	if (perf_hpp__is_dynamic_entry(fmt))
699 		return;
700 
701 	BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
702 
703 	switch (fmt->idx) {
704 	case PERF_HPP__OVERHEAD:
705 	case PERF_HPP__OVERHEAD_SYS:
706 	case PERF_HPP__OVERHEAD_US:
707 	case PERF_HPP__OVERHEAD_ACC:
708 		fmt->len = 8;
709 		break;
710 
711 	case PERF_HPP__OVERHEAD_GUEST_SYS:
712 	case PERF_HPP__OVERHEAD_GUEST_US:
713 		fmt->len = 9;
714 		break;
715 
716 	case PERF_HPP__SAMPLES:
717 	case PERF_HPP__PERIOD:
718 		fmt->len = 12;
719 		break;
720 
721 	default:
722 		break;
723 	}
724 }
725 
726 void hists__reset_column_width(struct hists *hists)
727 {
728 	struct perf_hpp_fmt *fmt;
729 	struct perf_hpp_list_node *node;
730 
731 	hists__for_each_format(hists, fmt)
732 		perf_hpp__reset_width(fmt, hists);
733 
734 	/* hierarchy entries have their own hpp list */
735 	list_for_each_entry(node, &hists->hpp_formats, list) {
736 		perf_hpp_list__for_each_format(&node->hpp, fmt)
737 			perf_hpp__reset_width(fmt, hists);
738 	}
739 }
740 
741 void perf_hpp__set_user_width(const char *width_list_str)
742 {
743 	struct perf_hpp_fmt *fmt;
744 	const char *ptr = width_list_str;
745 
746 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
747 		char *p;
748 
749 		int len = strtol(ptr, &p, 10);
750 		fmt->user_len = len;
751 
752 		if (*p == ',')
753 			ptr = p + 1;
754 		else
755 			break;
756 	}
757 }
758 
759 static int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
760 {
761 	struct perf_hpp_list_node *node = NULL;
762 	struct perf_hpp_fmt *fmt_copy;
763 	bool found = false;
764 	bool skip = perf_hpp__should_skip(fmt, hists);
765 
766 	list_for_each_entry(node, &hists->hpp_formats, list) {
767 		if (node->level == fmt->level) {
768 			found = true;
769 			break;
770 		}
771 	}
772 
773 	if (!found) {
774 		node = malloc(sizeof(*node));
775 		if (node == NULL)
776 			return -1;
777 
778 		node->skip = skip;
779 		node->level = fmt->level;
780 		perf_hpp_list__init(&node->hpp);
781 
782 		hists->nr_hpp_node++;
783 		list_add_tail(&node->list, &hists->hpp_formats);
784 	}
785 
786 	fmt_copy = perf_hpp_fmt__dup(fmt);
787 	if (fmt_copy == NULL)
788 		return -1;
789 
790 	if (!skip)
791 		node->skip = false;
792 
793 	list_add_tail(&fmt_copy->list, &node->hpp.fields);
794 	list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
795 
796 	return 0;
797 }
798 
799 int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
800 				  struct evlist *evlist)
801 {
802 	struct evsel *evsel;
803 	struct perf_hpp_fmt *fmt;
804 	struct hists *hists;
805 	int ret;
806 
807 	if (!symbol_conf.report_hierarchy)
808 		return 0;
809 
810 	evlist__for_each_entry(evlist, evsel) {
811 		hists = evsel__hists(evsel);
812 
813 		perf_hpp_list__for_each_sort_list(list, fmt) {
814 			if (perf_hpp__is_dynamic_entry(fmt) &&
815 			    !perf_hpp__defined_dynamic_entry(fmt, hists))
816 				continue;
817 
818 			ret = add_hierarchy_fmt(hists, fmt);
819 			if (ret < 0)
820 				return ret;
821 		}
822 	}
823 
824 	return 0;
825 }
826