xref: /linux/tools/perf/ui/hist.c (revision d55214860794ba5509d8ad861a5b2d97a7af3521)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <math.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <linux/compiler.h>
8 
9 #include "../util/callchain.h"
10 #include "../util/debug.h"
11 #include "../util/hist.h"
12 #include "../util/sort.h"
13 #include "../util/evsel.h"
14 #include "../util/evlist.h"
15 #include "../util/mem-events.h"
16 #include "../util/string2.h"
17 #include "../util/thread.h"
18 #include "../util/util.h"
19 
20 /* hist period print (hpp) functions */
21 
22 #define hpp__call_print_fn(hpp, fn, fmt, ...)			\
23 ({								\
24 	int __ret = fn(hpp, fmt, ##__VA_ARGS__);		\
25 	advance_hpp(hpp, __ret);				\
26 	__ret;							\
27 })
28 
29 static int __hpp__fmt_print(struct perf_hpp *hpp, struct hists *hists, u64 val,
30 			    int nr_samples, const char *fmt, int len,
31 			    hpp_snprint_fn print_fn, enum perf_hpp_fmt_type fmtype)
32 {
33 	if (fmtype == PERF_HPP_FMT_TYPE__PERCENT || fmtype == PERF_HPP_FMT_TYPE__LATENCY) {
34 		double percent = 0.0;
35 		u64 total = fmtype == PERF_HPP_FMT_TYPE__PERCENT ? hists__total_period(hists) :
36 			hists__total_latency(hists);
37 
38 		if (total)
39 			percent = 100.0 * val / total;
40 
41 		return hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
42 	}
43 
44 	if (fmtype == PERF_HPP_FMT_TYPE__AVERAGE) {
45 		double avg = nr_samples ? (1.0 * val / nr_samples) : 0;
46 
47 		return hpp__call_print_fn(hpp, print_fn, fmt, len, avg);
48 	}
49 
50 	return hpp__call_print_fn(hpp, print_fn, fmt, len, val);
51 }
52 
53 struct hpp_fmt_value {
54 	struct hists *hists;
55 	u64 val;
56 	int samples;
57 };
58 
59 static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
60 		      hpp_field_fn get_field, const char *fmtstr, int len,
61 		      hpp_snprint_fn print_fn, enum perf_hpp_fmt_type fmtype,
62 		      struct perf_hpp_fmt *fmt __maybe_unused)
63 {
64 	int ret = 0;
65 	struct hists *hists = he->hists;
66 	struct evsel *evsel = hists_to_evsel(hists);
67 	struct evsel *pos;
68 	char *buf = hpp->buf;
69 	size_t size = hpp->size;
70 	int i = 0, nr_members = 1;
71 	struct hpp_fmt_value *values;
72 
73 	if (evsel__is_group_event(evsel))
74 		nr_members = evsel->core.nr_members;
75 
76 	values = calloc(nr_members, sizeof(*values));
77 	if (values == NULL)
78 		return 0;
79 
80 	values[0].hists = evsel__hists(evsel);
81 	values[0].val = get_field(he);
82 	values[0].samples = he->stat.nr_events;
83 
84 	if (evsel__is_group_event(evsel)) {
85 		struct hist_entry *pair;
86 
87 		for_each_group_member(pos, evsel)
88 			values[++i].hists = evsel__hists(pos);
89 
90 		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
91 			for (i = 0; i < nr_members; i++) {
92 				if (values[i].hists != pair->hists)
93 					continue;
94 
95 				values[i].val = get_field(pair);
96 				values[i].samples = pair->stat.nr_events;
97 				break;
98 			}
99 		}
100 	}
101 
102 	/* Note, merge_entries implies !symbol_conf.report_hierarchy. */
103 	if (he->hists->merge_entries) {
104 		u64 total_val = 0;
105 		u64 total_samples = 0;
106 		u64 total_period = 0;
107 
108 		for (i = 0; i < nr_members; i++) {
109 			struct evsel *member_evsel = hists_to_evsel(values[i].hists);
110 
111 			struct evsel *he_evsel = hists_to_evsel(he->hists);
112 
113 			if (member_evsel != he_evsel &&
114 			    member_evsel->first_wildcard_match != he_evsel)
115 				continue;
116 
117 			total_val += values[i].val;
118 			total_samples += values[i].samples;
119 			total_period += fmtype == PERF_HPP_FMT_TYPE__PERCENT ?
120 					hists__total_period(values[i].hists) :
121 					hists__total_latency(values[i].hists);
122 		}
123 
124 		if (fmtype == PERF_HPP_FMT_TYPE__PERCENT || fmtype == PERF_HPP_FMT_TYPE__LATENCY) {
125 			double percent = 0.0;
126 
127 			if (total_period)
128 				percent = 100.0 * total_val / total_period;
129 			ret += hpp__call_print_fn(hpp, print_fn, fmtstr, len, percent);
130 		} else if (fmtype == PERF_HPP_FMT_TYPE__AVERAGE) {
131 			double avg = total_samples ? (1.0 * total_val / total_samples) : 0;
132 
133 			ret += hpp__call_print_fn(hpp, print_fn, fmtstr, len, avg);
134 		} else {
135 			ret += hpp__call_print_fn(hpp, print_fn, fmtstr, len, total_val);
136 		}
137 	}
138 
139 	for (i = 0; i < nr_members; i++) {
140 		if (symbol_conf.skip_empty &&
141 		    values[i].hists->stats.nr_samples == 0)
142 			continue;
143 
144 		ret += __hpp__fmt_print(hpp, values[i].hists, values[i].val,
145 					values[i].samples, fmtstr, len,
146 					print_fn, fmtype);
147 	}
148 
149 	free(values);
150 
151 	/*
152 	 * Restore original buf and size as it's where caller expects
153 	 * the result will be saved.
154 	 */
155 	hpp->buf = buf;
156 	hpp->size = size;
157 
158 	return ret;
159 }
160 
161 int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
162 	     struct hist_entry *he, hpp_field_fn get_field,
163 	     const char *fmtstr, hpp_snprint_fn print_fn,
164 	     enum perf_hpp_fmt_type fmtype)
165 {
166 	int len = max(fmt->user_len ?: fmt->len, (int)strlen(fmt->name));
167 
168 	if (symbol_conf.field_sep) {
169 		return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
170 				  print_fn, fmtype, fmt);
171 	}
172 
173 	if (fmtype == PERF_HPP_FMT_TYPE__PERCENT || fmtype == PERF_HPP_FMT_TYPE__LATENCY)
174 		len -= 2; /* 2 for a space and a % sign */
175 	else
176 		len -= 1;
177 
178 	return  __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmtype, fmt);
179 }
180 
181 int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
182 		 struct hist_entry *he, hpp_field_fn get_field,
183 		 const char *fmtstr, hpp_snprint_fn print_fn,
184 		 enum perf_hpp_fmt_type fmtype)
185 {
186 	if (!symbol_conf.cumulate_callchain) {
187 		int len = fmt->user_len ?: fmt->len;
188 		return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
189 	}
190 
191 	return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmtype);
192 }
193 
194 int hpp__fmt_mem_stat(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
195 		      struct hist_entry *he, enum mem_stat_type mst,
196 		      const char *fmtstr, hpp_snprint_fn print_fn)
197 {
198 	struct hists *hists = he->hists;
199 	int mem_stat_idx = -1;
200 	char *buf = hpp->buf;
201 	size_t size = hpp->size;
202 	u64 total = 0;
203 	int ret = 0;
204 
205 	for (int i = 0; i < hists->nr_mem_stats; i++) {
206 		if (hists->mem_stat_types[i] == mst) {
207 			mem_stat_idx = i;
208 			break;
209 		}
210 	}
211 	assert(mem_stat_idx != -1);
212 
213 	for (int i = 0; i < MEM_STAT_LEN; i++)
214 		total += hists->mem_stat_total[mem_stat_idx].entries[i];
215 	assert(total != 0);
216 
217 	for (int i = 0; i < MEM_STAT_LEN; i++) {
218 		u64 val = he->mem_stat[mem_stat_idx].entries[i];
219 
220 		if (hists->mem_stat_total[mem_stat_idx].entries[i] == 0)
221 			continue;
222 
223 		ret += hpp__call_print_fn(hpp, print_fn, fmtstr, 100.0 * val / total);
224 	}
225 
226 	/*
227 	 * Restore original buf and size as it's where caller expects
228 	 * the result will be saved.
229 	 */
230 	hpp->buf = buf;
231 	hpp->size = size;
232 
233 	return ret;
234 }
235 
236 static int field_cmp(u64 field_a, u64 field_b)
237 {
238 	if (field_a > field_b)
239 		return 1;
240 	if (field_a < field_b)
241 		return -1;
242 	return 0;
243 }
244 
245 static int hist_entry__new_pair(struct hist_entry *a, struct hist_entry *b,
246 				hpp_field_fn get_field, int nr_members,
247 				u64 **fields_a, u64 **fields_b)
248 {
249 	u64 *fa = calloc(nr_members, sizeof(*fa)),
250 	    *fb = calloc(nr_members, sizeof(*fb));
251 	struct hist_entry *pair;
252 
253 	if (!fa || !fb)
254 		goto out_free;
255 
256 	list_for_each_entry(pair, &a->pairs.head, pairs.node) {
257 		struct evsel *evsel = hists_to_evsel(pair->hists);
258 		fa[evsel__group_idx(evsel)] = get_field(pair);
259 	}
260 
261 	list_for_each_entry(pair, &b->pairs.head, pairs.node) {
262 		struct evsel *evsel = hists_to_evsel(pair->hists);
263 		fb[evsel__group_idx(evsel)] = get_field(pair);
264 	}
265 
266 	*fields_a = fa;
267 	*fields_b = fb;
268 	return 0;
269 out_free:
270 	free(fa);
271 	free(fb);
272 	*fields_a = *fields_b = NULL;
273 	return -1;
274 }
275 
276 static int __hpp__group_sort_idx(struct hist_entry *a, struct hist_entry *b,
277 				 hpp_field_fn get_field, int idx)
278 {
279 	struct evsel *evsel = hists_to_evsel(a->hists);
280 	u64 *fields_a, *fields_b;
281 	int cmp, nr_members, ret, i;
282 
283 	cmp = field_cmp(get_field(a), get_field(b));
284 	if (!evsel__is_group_event(evsel))
285 		return cmp;
286 
287 	nr_members = evsel->core.nr_members;
288 	if (idx < 1 || idx >= nr_members)
289 		return cmp;
290 
291 	ret = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
292 	if (ret) {
293 		ret = cmp;
294 		goto out;
295 	}
296 
297 	ret = field_cmp(fields_a[idx], fields_b[idx]);
298 	if (ret)
299 		goto out;
300 
301 	for (i = 1; i < nr_members; i++) {
302 		if (i != idx) {
303 			ret = field_cmp(fields_a[i], fields_b[i]);
304 			if (ret)
305 				goto out;
306 		}
307 	}
308 
309 out:
310 	free(fields_a);
311 	free(fields_b);
312 
313 	return ret;
314 }
315 
316 static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
317 		       hpp_field_fn get_field)
318 {
319 	s64 ret;
320 	int i, nr_members;
321 	struct evsel *evsel;
322 	u64 *fields_a, *fields_b;
323 
324 	if (symbol_conf.group_sort_idx && symbol_conf.event_group) {
325 		return __hpp__group_sort_idx(a, b, get_field,
326 					     symbol_conf.group_sort_idx);
327 	}
328 	/*
329 	 * Relies on merge_entries being only enabled if there are
330 	 * only matching events. If that is ever relaxed will need
331 	 * more logic here. merge_entries also implies that
332 	 * symbol_conf.report_hierarchy is false.
333 	 */
334 	if (a->hists->merge_entries && b->hists->merge_entries) {
335 		u64 val_a = get_field(a), val_b = get_field(b);
336 		struct hist_entry *pair;
337 		struct evsel *evsel_a = hists_to_evsel(a->hists);
338 		struct evsel *evsel_b = hists_to_evsel(b->hists);
339 
340 		list_for_each_entry(pair, &a->pairs.head, pairs.node) {
341 			struct evsel *pair_evsel = hists_to_evsel(pair->hists);
342 
343 			if (pair_evsel->first_wildcard_match == evsel_a)
344 				val_a += get_field(pair);
345 		}
346 		list_for_each_entry(pair, &b->pairs.head, pairs.node) {
347 			struct evsel *pair_evsel = hists_to_evsel(pair->hists);
348 
349 			if (pair_evsel->first_wildcard_match == evsel_b)
350 				val_b += get_field(pair);
351 		}
352 
353 		ret = field_cmp(val_a, val_b);
354 		if (ret)
355 			return ret;
356 	}
357 
358 	ret = field_cmp(get_field(a), get_field(b));
359 	if (ret || !symbol_conf.event_group)
360 		return ret;
361 
362 	evsel = hists_to_evsel(a->hists);
363 	if (!evsel__is_group_event(evsel))
364 		return ret;
365 
366 	nr_members = evsel->core.nr_members;
367 	i = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
368 	if (i)
369 		goto out;
370 
371 	for (i = 1; i < nr_members; i++) {
372 		ret = field_cmp(fields_a[i], fields_b[i]);
373 		if (ret)
374 			break;
375 	}
376 
377 out:
378 	free(fields_a);
379 	free(fields_b);
380 
381 	return ret;
382 }
383 
384 static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
385 			   hpp_field_fn get_field)
386 {
387 	s64 ret = 0;
388 
389 	if (symbol_conf.cumulate_callchain) {
390 		/*
391 		 * Put caller above callee when they have equal period.
392 		 */
393 		if (a->hists->merge_entries && b->hists->merge_entries) {
394 			u64 val_a = get_field(a), val_b = get_field(b);
395 			struct hist_entry *pair;
396 			struct evsel *evsel_a = hists_to_evsel(a->hists);
397 			struct evsel *evsel_b = hists_to_evsel(b->hists);
398 
399 			list_for_each_entry(pair, &a->pairs.head, pairs.node) {
400 				struct evsel *pair_evsel = hists_to_evsel(pair->hists);
401 
402 				if (pair_evsel->first_wildcard_match == evsel_a)
403 					val_a += get_field(pair);
404 			}
405 			list_for_each_entry(pair, &b->pairs.head, pairs.node) {
406 				struct evsel *pair_evsel = hists_to_evsel(pair->hists);
407 
408 				if (pair_evsel->first_wildcard_match == evsel_b)
409 					val_b += get_field(pair);
410 			}
411 
412 			ret = field_cmp(val_a, val_b);
413 		} else {
414 			ret = field_cmp(get_field(a), get_field(b));
415 		}
416 		if (ret)
417 			return ret;
418 
419 		if ((a->thread == NULL ? NULL : RC_CHK_ACCESS(a->thread)) !=
420 		    (b->thread == NULL ? NULL : RC_CHK_ACCESS(b->thread)) ||
421 		    !hist_entry__has_callchains(a) || !symbol_conf.use_callchain)
422 			return 0;
423 
424 		ret = b->callchain->max_depth - a->callchain->max_depth;
425 		if (callchain_param.order == ORDER_CALLER)
426 			ret = -ret;
427 	}
428 	return ret;
429 }
430 
431 static bool perf_hpp__is_mem_stat_entry(struct perf_hpp_fmt *fmt);
432 
433 static enum mem_stat_type hpp__mem_stat_type(struct perf_hpp_fmt *fmt)
434 {
435 	if (!perf_hpp__is_mem_stat_entry(fmt))
436 		return -1;
437 
438 	switch (fmt->idx) {
439 	case PERF_HPP__MEM_STAT_OP:
440 		return PERF_MEM_STAT_OP;
441 	case PERF_HPP__MEM_STAT_CACHE:
442 		return PERF_MEM_STAT_CACHE;
443 	case PERF_HPP__MEM_STAT_MEMORY:
444 		return PERF_MEM_STAT_MEMORY;
445 	case PERF_HPP__MEM_STAT_SNOOP:
446 		return PERF_MEM_STAT_SNOOP;
447 	case PERF_HPP__MEM_STAT_DTLB:
448 		return PERF_MEM_STAT_DTLB;
449 	default:
450 		break;
451 	}
452 	pr_debug("Should not reach here\n");
453 	return -1;
454 }
455 
456 static int64_t hpp__sort_mem_stat(struct perf_hpp_fmt *fmt __maybe_unused,
457 				  struct hist_entry *a, struct hist_entry *b)
458 {
459 	return a->stat.period - b->stat.period;
460 }
461 
462 static int hpp__width_fn(struct perf_hpp_fmt *fmt,
463 			 struct perf_hpp *hpp __maybe_unused,
464 			 struct hists *hists)
465 {
466 	int len = fmt->user_len ?: fmt->len;
467 	struct evsel *evsel = hists_to_evsel(hists);
468 
469 	if (symbol_conf.event_group) {
470 		int nr = 0;
471 		struct evsel *pos;
472 
473 		for_each_group_evsel(pos, evsel) {
474 			if (!symbol_conf.skip_empty ||
475 			    evsel__hists(pos)->stats.nr_samples)
476 				nr++;
477 		}
478 		if (hists->merge_entries)
479 			nr++; /* Add 1 extra unit of width generically for the 'Total' */
480 
481 		len = max(len, nr * fmt->len);
482 	}
483 
484 	if (len < (int)strlen(fmt->name))
485 		len = strlen(fmt->name);
486 
487 	return len;
488 }
489 
490 static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
491 			  struct hists *hists, int line,
492 			  int *span __maybe_unused)
493 {
494 	int len = hpp__width_fn(fmt, hpp, hists);
495 	const char *hdr = "";
496 
497 	if (line == hists->hpp_list->nr_header_lines - 1) {
498 		hdr = fmt->name;
499 		if (hists->merge_entries) {
500 			int w = 0;
501 			int f_len = fmt->user_len ?: fmt->len;
502 			struct evsel *pos, *evsel = hists_to_evsel(hists);
503 			int string_len = f_len;
504 
505 			for_each_group_evsel(pos, evsel) {
506 				if (symbol_conf.skip_empty &&
507 				    evsel__hists(pos)->stats.nr_samples == 0)
508 					continue;
509 				string_len += f_len;
510 			}
511 
512 			if (len > string_len) {
513 				w += scnprintf(hpp->buf + w, hpp->size - w, "%*s",
514 					       len - string_len, "");
515 			}
516 
517 			w += scnprintf(hpp->buf + w, hpp->size - w, "%*.*s",
518 				       f_len, f_len, fmt->name);
519 			for_each_group_evsel(pos, evsel) {
520 				if (symbol_conf.skip_empty &&
521 				    evsel__hists(pos)->stats.nr_samples == 0)
522 					continue;
523 				w += scnprintf(hpp->buf + w, hpp->size - w, " %*.*s",
524 					       f_len - 1, f_len - 1, evsel__name(pos));
525 			}
526 			return w;
527 		}
528 	}
529 
530 	return scnprintf(hpp->buf, hpp->size, "%*s", len, hdr);
531 }
532 
533 static int hpp__header_mem_stat_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
534 				   struct hists *hists, int line,
535 				   int *span __maybe_unused)
536 {
537 	char *buf = hpp->buf;
538 	int ret = 0;
539 	int len;
540 	enum mem_stat_type mst = hpp__mem_stat_type(fmt);
541 	int mem_stat_idx = -1;
542 
543 	for (int i = 0; i < hists->nr_mem_stats; i++) {
544 		if (hists->mem_stat_types[i] == mst) {
545 			mem_stat_idx = i;
546 			break;
547 		}
548 	}
549 	assert(mem_stat_idx != -1);
550 
551 	if (line == 0) {
552 		int left, right;
553 
554 		len = 0;
555 		/* update fmt->len for actually used columns only */
556 		for (int i = 0; i < MEM_STAT_LEN; i++) {
557 			if (hists->mem_stat_total[mem_stat_idx].entries[i])
558 				len += MEM_STAT_PRINT_LEN;
559 		}
560 		fmt->len = len;
561 
562 		/* print header directly if single column only */
563 		if (len == MEM_STAT_PRINT_LEN)
564 			return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
565 
566 		left = (len - strlen(fmt->name)) / 2 - 1;
567 		right = len - left - strlen(fmt->name) - 2;
568 
569 		if (left < 0)
570 			left = 0;
571 		if (right < 0)
572 			right = 0;
573 
574 		return scnprintf(hpp->buf, hpp->size, "%.*s %s %.*s",
575 				 left, graph_dotted_line, fmt->name, right, graph_dotted_line);
576 	}
577 
578 
579 	len = hpp->size;
580 	for (int i = 0; i < MEM_STAT_LEN; i++) {
581 		int printed;
582 
583 		if (hists->mem_stat_total[mem_stat_idx].entries[i] == 0)
584 			continue;
585 
586 		printed = scnprintf(buf, len, "%*s", MEM_STAT_PRINT_LEN,
587 				    mem_stat_name(mst, i));
588 		ret += printed;
589 		buf += printed;
590 		len -= printed;
591 	}
592 	return ret;
593 }
594 
595 int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
596 {
597 	va_list args;
598 	ssize_t ssize = hpp->size;
599 	double percent;
600 	int ret, len;
601 
602 	va_start(args, fmt);
603 	len = va_arg(args, int);
604 	percent = va_arg(args, double);
605 	ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
606 	va_end(args);
607 
608 	return (ret >= ssize) ? (ssize - 1) : ret;
609 }
610 
611 static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
612 {
613 	va_list args;
614 	ssize_t ssize = hpp->size;
615 	int ret;
616 
617 	va_start(args, fmt);
618 	ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
619 	va_end(args);
620 
621 	return (ret >= ssize) ? (ssize - 1) : ret;
622 }
623 
624 #define __HPP_COLOR_PERCENT_FN(_type, _field, _fmttype)				\
625 static u64 he_get_##_field(struct hist_entry *he)				\
626 {										\
627 	return he->stat._field;							\
628 }										\
629 										\
630 static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
631 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
632 {										\
633 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
634 			hpp_color_scnprintf, _fmttype);				\
635 }
636 
637 #define __HPP_ENTRY_PERCENT_FN(_type, _field, _fmttype)				\
638 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
639 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
640 {										\
641 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
642 			hpp_entry_scnprintf, _fmttype);	\
643 }
644 
645 #define __HPP_SORT_FN(_type, _field)						\
646 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
647 				 struct hist_entry *a, struct hist_entry *b) 	\
648 {										\
649 	return __hpp__sort(a, b, he_get_##_field);				\
650 }
651 
652 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field, _fmttype)			\
653 static u64 he_get_acc_##_field(struct hist_entry *he)				\
654 {										\
655 	return he->stat_acc->_field;						\
656 }										\
657 										\
658 static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
659 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
660 {										\
661 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", 	\
662 			    hpp_color_scnprintf, _fmttype);			\
663 }
664 
665 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field, _fmttype)			\
666 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
667 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
668 {										\
669 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%",	\
670 			    hpp_entry_scnprintf, _fmttype);			\
671 }
672 
673 #define __HPP_SORT_ACC_FN(_type, _field)					\
674 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
675 				 struct hist_entry *a, struct hist_entry *b) 	\
676 {										\
677 	return __hpp__sort_acc(a, b, he_get_acc_##_field);			\
678 }
679 
680 #define __HPP_ENTRY_RAW_FN(_type, _field)					\
681 static u64 he_get_raw_##_field(struct hist_entry *he)				\
682 {										\
683 	return he->stat._field;							\
684 }										\
685 										\
686 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
687 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
688 {										\
689 	return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, 	\
690 			hpp_entry_scnprintf, PERF_HPP_FMT_TYPE__RAW);		\
691 }
692 
693 #define __HPP_SORT_RAW_FN(_type, _field)					\
694 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
695 				 struct hist_entry *a, struct hist_entry *b) 	\
696 {										\
697 	return __hpp__sort(a, b, he_get_raw_##_field);				\
698 }
699 
700 #define __HPP_ENTRY_AVERAGE_FN(_type, _field)					\
701 static u64 he_get_##_field(struct hist_entry *he)				\
702 {										\
703 	return he->stat._field;							\
704 }										\
705 										\
706 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
707 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
708 {										\
709 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.1f",		\
710 			hpp_entry_scnprintf, PERF_HPP_FMT_TYPE__AVERAGE);	\
711 }
712 
713 #define __HPP_SORT_AVERAGE_FN(_type, _field)					\
714 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
715 				 struct hist_entry *a, struct hist_entry *b) 	\
716 {										\
717 	return __hpp__sort(a, b, he_get_##_field);				\
718 }
719 
720 #define __HPP_COLOR_MEM_STAT_FN(_name, _type)					\
721 static int hpp__color_mem_stat_##_name(struct perf_hpp_fmt *fmt,		\
722 				       struct perf_hpp *hpp,			\
723 				       struct hist_entry *he)			\
724 {										\
725 	return hpp__fmt_mem_stat(fmt, hpp, he, PERF_MEM_STAT_##_type,		\
726 				 " %5.1f%%", hpp_color_scnprintf);		\
727 }
728 
729 #define __HPP_ENTRY_MEM_STAT_FN(_name, _type)					\
730 static int hpp__entry_mem_stat_##_name(struct perf_hpp_fmt *fmt, 		\
731 				       struct perf_hpp *hpp,			\
732 				       struct hist_entry *he)			\
733 {										\
734 	return hpp__fmt_mem_stat(fmt, hpp, he, PERF_MEM_STAT_##_type,		\
735 				 " %5.1f%%", hpp_entry_scnprintf);		\
736 }
737 
738 #define HPP_PERCENT_FNS(_type, _field, _fmttype)			\
739 __HPP_COLOR_PERCENT_FN(_type, _field, _fmttype)				\
740 __HPP_ENTRY_PERCENT_FN(_type, _field, _fmttype)				\
741 __HPP_SORT_FN(_type, _field)
742 
743 #define HPP_PERCENT_ACC_FNS(_type, _field, _fmttype)			\
744 __HPP_COLOR_ACC_PERCENT_FN(_type, _field, _fmttype)			\
745 __HPP_ENTRY_ACC_PERCENT_FN(_type, _field, _fmttype)			\
746 __HPP_SORT_ACC_FN(_type, _field)
747 
748 #define HPP_RAW_FNS(_type, _field)					\
749 __HPP_ENTRY_RAW_FN(_type, _field)					\
750 __HPP_SORT_RAW_FN(_type, _field)
751 
752 #define HPP_AVERAGE_FNS(_type, _field)					\
753 __HPP_ENTRY_AVERAGE_FN(_type, _field)					\
754 __HPP_SORT_AVERAGE_FN(_type, _field)
755 
756 #define HPP_MEM_STAT_FNS(_name, _type)					\
757 __HPP_COLOR_MEM_STAT_FN(_name, _type)					\
758 __HPP_ENTRY_MEM_STAT_FN(_name, _type)
759 
760 HPP_PERCENT_FNS(overhead, period, PERF_HPP_FMT_TYPE__PERCENT)
761 HPP_PERCENT_FNS(latency, latency, PERF_HPP_FMT_TYPE__LATENCY)
762 HPP_PERCENT_FNS(overhead_sys, period_sys, PERF_HPP_FMT_TYPE__PERCENT)
763 HPP_PERCENT_FNS(overhead_us, period_us, PERF_HPP_FMT_TYPE__PERCENT)
764 HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys, PERF_HPP_FMT_TYPE__PERCENT)
765 HPP_PERCENT_FNS(overhead_guest_us, period_guest_us, PERF_HPP_FMT_TYPE__PERCENT)
766 HPP_PERCENT_ACC_FNS(overhead_acc, period, PERF_HPP_FMT_TYPE__PERCENT)
767 HPP_PERCENT_ACC_FNS(latency_acc, latency, PERF_HPP_FMT_TYPE__LATENCY)
768 
769 HPP_RAW_FNS(samples, nr_events)
770 HPP_RAW_FNS(period, period)
771 
772 HPP_AVERAGE_FNS(weight1, weight1)
773 HPP_AVERAGE_FNS(weight2, weight2)
774 HPP_AVERAGE_FNS(weight3, weight3)
775 
776 HPP_MEM_STAT_FNS(op, OP)
777 HPP_MEM_STAT_FNS(cache, CACHE)
778 HPP_MEM_STAT_FNS(memory, MEMORY)
779 HPP_MEM_STAT_FNS(snoop, SNOOP)
780 HPP_MEM_STAT_FNS(dtlb, DTLB)
781 
782 static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
783 			    struct hist_entry *a __maybe_unused,
784 			    struct hist_entry *b __maybe_unused)
785 {
786 	return 0;
787 }
788 
789 static bool perf_hpp__is_mem_stat_entry(struct perf_hpp_fmt *fmt)
790 {
791 	return fmt->sort == hpp__sort_mem_stat;
792 }
793 
794 static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
795 {
796 	return a->header == hpp__header_fn;
797 }
798 
799 static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
800 {
801 	if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
802 		return false;
803 
804 	return a->idx == b->idx;
805 }
806 
807 static bool hpp__equal_mem_stat(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
808 {
809 	if (!perf_hpp__is_mem_stat_entry(a) || !perf_hpp__is_mem_stat_entry(b))
810 		return false;
811 
812 	return a->entry == b->entry;
813 }
814 
815 #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx)		\
816 	{						\
817 		.name   = _name,			\
818 		.header	= hpp__header_fn,		\
819 		.width	= hpp__width_fn,		\
820 		.color	= hpp__color_ ## _fn,		\
821 		.entry	= hpp__entry_ ## _fn,		\
822 		.cmp	= hpp__nop_cmp,			\
823 		.collapse = hpp__nop_cmp,		\
824 		.sort	= hpp__sort_ ## _fn,		\
825 		.idx	= PERF_HPP__ ## _idx,		\
826 		.equal	= hpp__equal,			\
827 	}
828 
829 #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx)	\
830 	{						\
831 		.name   = _name,			\
832 		.header	= hpp__header_fn,		\
833 		.width	= hpp__width_fn,		\
834 		.color	= hpp__color_ ## _fn,		\
835 		.entry	= hpp__entry_ ## _fn,		\
836 		.cmp	= hpp__nop_cmp,			\
837 		.collapse = hpp__nop_cmp,		\
838 		.sort	= hpp__sort_ ## _fn,		\
839 		.idx	= PERF_HPP__ ## _idx,		\
840 		.equal	= hpp__equal,			\
841 	}
842 
843 #define HPP__PRINT_FNS(_name, _fn, _idx)		\
844 	{						\
845 		.name   = _name,			\
846 		.header	= hpp__header_fn,		\
847 		.width	= hpp__width_fn,		\
848 		.entry	= hpp__entry_ ## _fn,		\
849 		.cmp	= hpp__nop_cmp,			\
850 		.collapse = hpp__nop_cmp,		\
851 		.sort	= hpp__sort_ ## _fn,		\
852 		.idx	= PERF_HPP__ ## _idx,		\
853 		.equal	= hpp__equal,			\
854 	}
855 
856 #define HPP__MEM_STAT_PRINT_FNS(_name, _fn, _type)	\
857 	{						\
858 		.name   = _name,			\
859 		.header	= hpp__header_mem_stat_fn,	\
860 		.width	= hpp__width_fn,		\
861 		.color	= hpp__color_mem_stat_ ## _fn,	\
862 		.entry	= hpp__entry_mem_stat_ ## _fn,	\
863 		.cmp	= hpp__nop_cmp,			\
864 		.collapse = hpp__nop_cmp,		\
865 		.sort	= hpp__sort_mem_stat,		\
866 		.idx	= PERF_HPP__MEM_STAT_ ## _type,	\
867 		.equal	= hpp__equal_mem_stat,		\
868 	}
869 
870 struct perf_hpp_fmt perf_hpp__format[] = {
871 	HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
872 	HPP__COLOR_PRINT_FNS("Latency", latency, LATENCY),
873 	HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
874 	HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
875 	HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
876 	HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
877 	HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
878 	HPP__COLOR_ACC_PRINT_FNS("Latency", latency_acc, LATENCY_ACC),
879 	HPP__PRINT_FNS("Samples", samples, SAMPLES),
880 	HPP__PRINT_FNS("Period", period, PERIOD),
881 	HPP__PRINT_FNS("Weight1", weight1, WEIGHT1),
882 	HPP__PRINT_FNS("Weight2", weight2, WEIGHT2),
883 	HPP__PRINT_FNS("Weight3", weight3, WEIGHT3),
884 	HPP__MEM_STAT_PRINT_FNS("Mem Op", op, OP),
885 	HPP__MEM_STAT_PRINT_FNS("Cache", cache, CACHE),
886 	HPP__MEM_STAT_PRINT_FNS("Memory", memory, MEMORY),
887 	HPP__MEM_STAT_PRINT_FNS("Snoop", snoop, SNOOP),
888 	HPP__MEM_STAT_PRINT_FNS("D-TLB", dtlb, DTLB),
889 };
890 
891 struct perf_hpp_list perf_hpp_list = {
892 	.fields	= LIST_HEAD_INIT(perf_hpp_list.fields),
893 	.sorts	= LIST_HEAD_INIT(perf_hpp_list.sorts),
894 	.nr_header_lines = 1,
895 };
896 
897 #undef HPP__COLOR_PRINT_FNS
898 #undef HPP__COLOR_ACC_PRINT_FNS
899 #undef HPP__PRINT_FNS
900 #undef HPP__MEM_STAT_PRINT_FNS
901 
902 #undef HPP_PERCENT_FNS
903 #undef HPP_PERCENT_ACC_FNS
904 #undef HPP_RAW_FNS
905 #undef HPP_AVERAGE_FNS
906 #undef HPP_MEM_STAT_FNS
907 
908 #undef __HPP_HEADER_FN
909 #undef __HPP_WIDTH_FN
910 #undef __HPP_COLOR_PERCENT_FN
911 #undef __HPP_ENTRY_PERCENT_FN
912 #undef __HPP_COLOR_ACC_PERCENT_FN
913 #undef __HPP_ENTRY_ACC_PERCENT_FN
914 #undef __HPP_ENTRY_RAW_FN
915 #undef __HPP_ENTRY_AVERAGE_FN
916 #undef __HPP_COLOR_MEM_STAT_FN
917 #undef __HPP_ENTRY_MEM_STAT_FN
918 
919 #undef __HPP_SORT_FN
920 #undef __HPP_SORT_ACC_FN
921 #undef __HPP_SORT_RAW_FN
922 #undef __HPP_SORT_AVERAGE_FN
923 
924 static void fmt_free(struct perf_hpp_fmt *fmt)
925 {
926 	/*
927 	 * At this point fmt should be completely
928 	 * unhooked, if not it's a bug.
929 	 */
930 	BUG_ON(!list_empty(&fmt->list));
931 	BUG_ON(!list_empty(&fmt->sort_list));
932 
933 	if (fmt->free)
934 		fmt->free(fmt);
935 }
936 
937 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
938 {
939 	return a->equal && a->equal(a, b);
940 }
941 
942 void perf_hpp__init(void)
943 {
944 	int i;
945 
946 	for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
947 		struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
948 
949 		INIT_LIST_HEAD(&fmt->list);
950 
951 		/* sort_list may be linked by setup_sorting() */
952 		if (fmt->sort_list.next == NULL)
953 			INIT_LIST_HEAD(&fmt->sort_list);
954 	}
955 
956 	/*
957 	 * If user specified field order, no need to setup default fields.
958 	 */
959 	if (is_strict_order(field_order))
960 		return;
961 
962 	/*
963 	 * Overhead and latency columns are added in setup_overhead(),
964 	 * so they are added implicitly here only if they were added
965 	 * by setup_overhead() before (have was_taken flag set).
966 	 * This is required because setup_overhead() has more complex
967 	 * logic, in particular it does not add "overhead" if user
968 	 * specified "latency" in sort order, and vise versa.
969 	 */
970 	if (symbol_conf.cumulate_callchain) {
971 		/*
972 		 * Addition of fields is idempotent, so we add latency
973 		 * column twice to get desired order with simpler logic.
974 		 */
975 		if (symbol_conf.prefer_latency)
976 			hpp_dimension__add_output(PERF_HPP__LATENCY_ACC, true);
977 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC, true);
978 		if (symbol_conf.enable_latency)
979 			hpp_dimension__add_output(PERF_HPP__LATENCY_ACC, true);
980 		perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
981 	}
982 
983 	if (symbol_conf.prefer_latency)
984 		hpp_dimension__add_output(PERF_HPP__LATENCY, true);
985 	hpp_dimension__add_output(PERF_HPP__OVERHEAD, true);
986 	if (symbol_conf.enable_latency)
987 		hpp_dimension__add_output(PERF_HPP__LATENCY, true);
988 
989 	if (symbol_conf.show_cpu_utilization) {
990 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS, false);
991 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_US, false);
992 
993 		if (perf_guest) {
994 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS, false);
995 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US, false);
996 		}
997 	}
998 
999 	if (symbol_conf.show_nr_samples)
1000 		hpp_dimension__add_output(PERF_HPP__SAMPLES, false);
1001 
1002 	if (symbol_conf.show_total_period)
1003 		hpp_dimension__add_output(PERF_HPP__PERIOD, false);
1004 }
1005 
1006 void perf_hpp_list__column_register(struct perf_hpp_list *list,
1007 				    struct perf_hpp_fmt *format)
1008 {
1009 	list_add_tail(&format->list, &list->fields);
1010 }
1011 
1012 void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
1013 					struct perf_hpp_fmt *format)
1014 {
1015 	list_add_tail(&format->sort_list, &list->sorts);
1016 }
1017 
1018 void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
1019 				       struct perf_hpp_fmt *format)
1020 {
1021 	list_add(&format->sort_list, &list->sorts);
1022 }
1023 
1024 static void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
1025 {
1026 	list_del_init(&format->list);
1027 	list_del_init(&format->sort_list);
1028 	fmt_free(format);
1029 }
1030 
1031 void perf_hpp__cancel_cumulate(struct evlist *evlist)
1032 {
1033 	struct perf_hpp_fmt *fmt, *acc, *ovh, *acc_lat, *tmp;
1034 	struct evsel *evsel;
1035 
1036 	if (is_strict_order(field_order))
1037 		return;
1038 
1039 	ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
1040 	acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
1041 	acc_lat = &perf_hpp__format[PERF_HPP__LATENCY_ACC];
1042 
1043 	perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
1044 		if (fmt_equal(acc, fmt) || fmt_equal(acc_lat, fmt)) {
1045 			perf_hpp__column_unregister(fmt);
1046 			continue;
1047 		}
1048 
1049 		if (fmt_equal(ovh, fmt))
1050 			fmt->name = "Overhead";
1051 	}
1052 
1053 	evlist__for_each_entry(evlist, evsel) {
1054 		struct hists *hists = evsel__hists(evsel);
1055 		struct perf_hpp_list_node *node;
1056 
1057 		list_for_each_entry(node, &hists->hpp_formats, list) {
1058 			perf_hpp_list__for_each_format_safe(&node->hpp, fmt, tmp) {
1059 				if (fmt_equal(acc, fmt) || fmt_equal(acc_lat, fmt)) {
1060 					perf_hpp__column_unregister(fmt);
1061 					continue;
1062 				}
1063 
1064 				if (fmt_equal(ovh, fmt))
1065 					fmt->name = "Overhead";
1066 			}
1067 		}
1068 	}
1069 }
1070 
1071 void perf_hpp__cancel_latency(struct evlist *evlist)
1072 {
1073 	struct perf_hpp_fmt *fmt, *lat, *acc, *tmp;
1074 	struct evsel *evsel;
1075 
1076 	if (is_strict_order(field_order))
1077 		return;
1078 	if (sort_order && strstr(sort_order, "latency"))
1079 		return;
1080 
1081 	lat = &perf_hpp__format[PERF_HPP__LATENCY];
1082 	acc = &perf_hpp__format[PERF_HPP__LATENCY_ACC];
1083 
1084 	perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
1085 		if (fmt_equal(lat, fmt) || fmt_equal(acc, fmt))
1086 			perf_hpp__column_unregister(fmt);
1087 	}
1088 
1089 	evlist__for_each_entry(evlist, evsel) {
1090 		struct hists *hists = evsel__hists(evsel);
1091 		struct perf_hpp_list_node *node;
1092 
1093 		list_for_each_entry(node, &hists->hpp_formats, list) {
1094 			perf_hpp_list__for_each_format_safe(&node->hpp, fmt, tmp) {
1095 				if (fmt_equal(lat, fmt) || fmt_equal(acc, fmt))
1096 					perf_hpp__column_unregister(fmt);
1097 			}
1098 		}
1099 	}
1100 }
1101 
1102 void perf_hpp__setup_output_field(struct perf_hpp_list *list)
1103 {
1104 	struct perf_hpp_fmt *fmt;
1105 
1106 	/* append sort keys to output field */
1107 	perf_hpp_list__for_each_sort_list(list, fmt) {
1108 		struct perf_hpp_fmt *pos;
1109 
1110 		/* skip sort-only fields ("sort_compute" in perf diff) */
1111 		if (!fmt->entry && !fmt->color)
1112 			continue;
1113 
1114 		perf_hpp_list__for_each_format(list, pos) {
1115 			if (fmt_equal(fmt, pos))
1116 				goto next;
1117 		}
1118 
1119 		perf_hpp__column_register(fmt);
1120 next:
1121 		continue;
1122 	}
1123 }
1124 
1125 void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
1126 {
1127 	struct perf_hpp_fmt *fmt;
1128 
1129 	/* append output fields to sort keys */
1130 	perf_hpp_list__for_each_format(list, fmt) {
1131 		struct perf_hpp_fmt *pos;
1132 
1133 		perf_hpp_list__for_each_sort_list(list, pos) {
1134 			if (fmt_equal(fmt, pos))
1135 				goto next;
1136 		}
1137 
1138 		perf_hpp__register_sort_field(fmt);
1139 next:
1140 		continue;
1141 	}
1142 }
1143 
1144 
1145 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
1146 {
1147 	struct perf_hpp_fmt *fmt, *tmp;
1148 
1149 	/* reset output fields */
1150 	perf_hpp_list__for_each_format_safe(list, fmt, tmp)
1151 		perf_hpp__column_unregister(fmt);
1152 
1153 	/* reset sort keys */
1154 	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp)
1155 		perf_hpp__column_unregister(fmt);
1156 }
1157 
1158 /*
1159  * See hists__fprintf to match the column widths
1160  */
1161 unsigned int hists__sort_list_width(struct hists *hists)
1162 {
1163 	struct perf_hpp_fmt *fmt;
1164 	int ret = 0;
1165 	bool first = true;
1166 	struct perf_hpp dummy_hpp;
1167 
1168 	hists__for_each_format(hists, fmt) {
1169 		if (perf_hpp__should_skip(fmt, hists))
1170 			continue;
1171 
1172 		if (first)
1173 			first = false;
1174 		else
1175 			ret += 2;
1176 
1177 		ret += fmt->width(fmt, &dummy_hpp, hists);
1178 	}
1179 
1180 	if (verbose > 0 && hists__has(hists, sym)) /* Addr + origin */
1181 		ret += 3 + BITS_PER_LONG / 4;
1182 
1183 	return ret;
1184 }
1185 
1186 unsigned int hists__overhead_width(struct hists *hists)
1187 {
1188 	struct perf_hpp_fmt *fmt;
1189 	int ret = 0;
1190 	bool first = true;
1191 	struct perf_hpp dummy_hpp;
1192 
1193 	hists__for_each_format(hists, fmt) {
1194 		if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1195 			break;
1196 
1197 		if (first)
1198 			first = false;
1199 		else
1200 			ret += 2;
1201 
1202 		ret += fmt->width(fmt, &dummy_hpp, hists);
1203 	}
1204 
1205 	return ret;
1206 }
1207 
1208 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
1209 {
1210 	if (perf_hpp__is_sort_entry(fmt))
1211 		return perf_hpp__reset_sort_width(fmt, hists);
1212 
1213 	if (perf_hpp__is_dynamic_entry(fmt))
1214 		return;
1215 
1216 	BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
1217 
1218 	switch (fmt->idx) {
1219 	case PERF_HPP__OVERHEAD:
1220 	case PERF_HPP__LATENCY:
1221 	case PERF_HPP__OVERHEAD_SYS:
1222 	case PERF_HPP__OVERHEAD_US:
1223 	case PERF_HPP__OVERHEAD_ACC:
1224 		fmt->len = 8;
1225 		break;
1226 
1227 	case PERF_HPP__OVERHEAD_GUEST_SYS:
1228 	case PERF_HPP__OVERHEAD_GUEST_US:
1229 		fmt->len = 9;
1230 		break;
1231 
1232 	case PERF_HPP__SAMPLES:
1233 	case PERF_HPP__PERIOD:
1234 		fmt->len = 12;
1235 		break;
1236 
1237 	case PERF_HPP__WEIGHT1:
1238 	case PERF_HPP__WEIGHT2:
1239 	case PERF_HPP__WEIGHT3:
1240 		fmt->len = 8;
1241 		break;
1242 
1243 	case PERF_HPP__MEM_STAT_OP:
1244 	case PERF_HPP__MEM_STAT_CACHE:
1245 	case PERF_HPP__MEM_STAT_MEMORY:
1246 	case PERF_HPP__MEM_STAT_SNOOP:
1247 	case PERF_HPP__MEM_STAT_DTLB:
1248 		fmt->len = MEM_STAT_LEN * MEM_STAT_PRINT_LEN;
1249 		break;
1250 
1251 	default:
1252 		break;
1253 	}
1254 }
1255 
1256 void hists__reset_column_width(struct hists *hists)
1257 {
1258 	struct perf_hpp_fmt *fmt;
1259 	struct perf_hpp_list_node *node;
1260 
1261 	hists__for_each_format(hists, fmt)
1262 		perf_hpp__reset_width(fmt, hists);
1263 
1264 	/* hierarchy entries have their own hpp list */
1265 	list_for_each_entry(node, &hists->hpp_formats, list) {
1266 		perf_hpp_list__for_each_format(&node->hpp, fmt)
1267 			perf_hpp__reset_width(fmt, hists);
1268 	}
1269 }
1270 
1271 void perf_hpp__set_user_width(const char *width_list_str)
1272 {
1273 	struct perf_hpp_fmt *fmt;
1274 	const char *ptr = width_list_str;
1275 
1276 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
1277 		char *p;
1278 
1279 		int len = strtol(ptr, &p, 10);
1280 		fmt->user_len = len;
1281 
1282 		if (*p == ',')
1283 			ptr = p + 1;
1284 		else
1285 			break;
1286 	}
1287 }
1288 
1289 static int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
1290 {
1291 	struct perf_hpp_list_node *node = NULL;
1292 	struct perf_hpp_fmt *fmt_copy;
1293 	bool found = false;
1294 	bool skip = perf_hpp__should_skip(fmt, hists);
1295 
1296 	list_for_each_entry(node, &hists->hpp_formats, list) {
1297 		if (node->level == fmt->level) {
1298 			found = true;
1299 			break;
1300 		}
1301 	}
1302 
1303 	if (!found) {
1304 		node = malloc(sizeof(*node));
1305 		if (node == NULL)
1306 			return -1;
1307 
1308 		node->skip = skip;
1309 		node->level = fmt->level;
1310 		perf_hpp_list__init(&node->hpp);
1311 
1312 		hists->nr_hpp_node++;
1313 		list_add_tail(&node->list, &hists->hpp_formats);
1314 	}
1315 
1316 	fmt_copy = perf_hpp_fmt__dup(fmt);
1317 	if (fmt_copy == NULL)
1318 		return -1;
1319 
1320 	if (!skip)
1321 		node->skip = false;
1322 
1323 	list_add_tail(&fmt_copy->list, &node->hpp.fields);
1324 	list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
1325 
1326 	return 0;
1327 }
1328 
1329 int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
1330 				  struct evlist *evlist)
1331 {
1332 	struct evsel *evsel;
1333 	struct perf_hpp_fmt *fmt;
1334 	struct hists *hists;
1335 	int ret;
1336 
1337 	if (!symbol_conf.report_hierarchy)
1338 		return 0;
1339 
1340 	evlist__for_each_entry(evlist, evsel) {
1341 		hists = evsel__hists(evsel);
1342 
1343 		perf_hpp_list__for_each_sort_list(list, fmt) {
1344 			if (perf_hpp__is_dynamic_entry(fmt) &&
1345 			    !perf_hpp__defined_dynamic_entry(fmt, hists))
1346 				continue;
1347 
1348 			ret = add_hierarchy_fmt(hists, fmt);
1349 			if (ret < 0)
1350 				return ret;
1351 		}
1352 	}
1353 
1354 	return 0;
1355 }
1356 
1357 int perf_hpp__alloc_mem_stats(struct perf_hpp_list *list, struct evlist *evlist)
1358 {
1359 	struct perf_hpp_fmt *fmt;
1360 	struct evsel *evsel;
1361 	enum mem_stat_type mst[16];
1362 	unsigned nr_mem_stats = 0;
1363 
1364 	perf_hpp_list__for_each_format(list, fmt) {
1365 		if (!perf_hpp__is_mem_stat_entry(fmt))
1366 			continue;
1367 
1368 		assert(nr_mem_stats < ARRAY_SIZE(mst));
1369 		mst[nr_mem_stats++] = hpp__mem_stat_type(fmt);
1370 	}
1371 
1372 	if (nr_mem_stats == 0)
1373 		return 0;
1374 
1375 	list->nr_header_lines = 2;
1376 
1377 	evlist__for_each_entry(evlist, evsel) {
1378 		struct hists *hists = evsel__hists(evsel);
1379 
1380 		hists->mem_stat_types = calloc(nr_mem_stats,
1381 					       sizeof(*hists->mem_stat_types));
1382 		if (hists->mem_stat_types == NULL)
1383 			return -ENOMEM;
1384 
1385 		hists->mem_stat_total = calloc(nr_mem_stats,
1386 					       sizeof(*hists->mem_stat_total));
1387 		if (hists->mem_stat_total == NULL)
1388 			return -ENOMEM;
1389 
1390 		memcpy(hists->mem_stat_types, mst, nr_mem_stats * sizeof(*mst));
1391 		hists->nr_mem_stats = nr_mem_stats;
1392 	}
1393 	return 0;
1394 }
1395 
1396 float hist_entry__get_percent_limit_merged(struct hist_entry *he)
1397 {
1398 	struct hist_entry *pair;
1399 	u64 period = he->stat.period;
1400 	u64 total_period = hists__total_period(he->hists);
1401 	struct evsel *evsel = hists_to_evsel(he->hists);
1402 	struct evsel *pos;
1403 
1404 	/* Accumulate global total_period across all merged hists matching hybrid type */
1405 	for_each_group_member(pos, evsel) {
1406 		if (pos->first_wildcard_match == evsel)
1407 			total_period += hists__total_period(evsel__hists(pos));
1408 	}
1409 
1410 	if (unlikely(total_period == 0))
1411 		return 0;
1412 
1413 	if (symbol_conf.cumulate_callchain) {
1414 		period = he->stat_acc->period;
1415 		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
1416 			struct evsel *pair_evsel = hists_to_evsel(pair->hists);
1417 
1418 			if (pair_evsel->first_wildcard_match == evsel)
1419 				period += pair->stat_acc->period;
1420 		}
1421 	} else {
1422 		/* Accumulate symbol specific period across pairs matching hybrid type */
1423 		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
1424 			struct evsel *pair_evsel = hists_to_evsel(pair->hists);
1425 
1426 			if (pair_evsel->first_wildcard_match == evsel)
1427 				period += pair->stat.period;
1428 		}
1429 	}
1430 
1431 	return period * 100.0 / total_period;
1432 }
1433 
1434 void evlist__merge_hists_hybrid(struct evlist *evlist, bool refresh_hists)
1435 {
1436 	struct evsel *pos;
1437 	struct evsel *member;
1438 	bool hybrid_group;
1439 
1440 	/*
1441 	 * Merged hists display all the events of a group in a single set of
1442 	 * entries, which the hierarchy display has no way to render. Keeping
1443 	 * hists->merge_entries false here means the rest of the display code
1444 	 * can assume merge_entries implies !symbol_conf.report_hierarchy.
1445 	 */
1446 	if (symbol_conf.report_hierarchy)
1447 		return;
1448 
1449 	/* Set merge_entries flag strictly on leaders formulated by hybrid topology */
1450 	evlist__for_each_entry(evlist, pos) {
1451 		if (evsel__is_dummy_event(pos))
1452 			continue;
1453 
1454 		if (pos->core.leader == &pos->core && pos->core.nr_members > 1) {
1455 			hybrid_group = false;
1456 
1457 			if (pos->first_wildcard_match || pos->merged_hybrid_group) {
1458 				hybrid_group = true;
1459 			} else {
1460 				for_each_group_member(member, pos) {
1461 					if (member->first_wildcard_match ||
1462 					    member->merged_hybrid_group) {
1463 						hybrid_group = true;
1464 						break;
1465 					}
1466 				}
1467 			}
1468 
1469 			if (hybrid_group) {
1470 				evsel__hists(pos)->merge_entries = true;
1471 				symbol_conf.event_group = true;
1472 				symbol_conf.hybrid_merge = true;
1473 				if (!pos->group_name) {
1474 					pos->group_name = strdup("Merged hybrid events");
1475 					if (!pos->group_name)
1476 						pr_warning("Failed to allocate hybrid group name\n");
1477 				}
1478 			}
1479 		}
1480 	}
1481 
1482 	if (!refresh_hists)
1483 		return;
1484 
1485 	evlist__for_each_entry(evlist, pos) {
1486 		/* Match histograms dynamically since parsing happened before group toggling */
1487 		if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
1488 			struct hists *leader_hists = evsel__hists(evsel__leader(pos));
1489 			struct hists *hists = evsel__hists(pos);
1490 
1491 			hists__match(leader_hists, hists);
1492 			hists__link(leader_hists, hists);
1493 		}
1494 	}
1495 
1496 	/* Now that links are formed, safely resort the active tree so the UI renders accurately */
1497 	if (symbol_conf.event_group) {
1498 		evlist__for_each_entry(evlist, pos) {
1499 			if (evsel__is_dummy_event(pos) || !evsel__is_group_leader(pos))
1500 				continue;
1501 			if (pos->core.nr_members > 1 && evsel__hists(pos)->merge_entries)
1502 				hists__output_resort(evsel__hists(pos), NULL);
1503 		}
1504 	}
1505 }
1506