xref: /openbmc/linux/tools/perf/util/stat-display.c (revision 278d3ba6)
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <linux/string.h>
5 #include <linux/time64.h>
6 #include <math.h>
7 #include <perf/cpumap.h>
8 #include "color.h"
9 #include "counts.h"
10 #include "evlist.h"
11 #include "evsel.h"
12 #include "stat.h"
13 #include "top.h"
14 #include "thread_map.h"
15 #include "cpumap.h"
16 #include "string2.h"
17 #include <linux/ctype.h>
18 #include "cgroup.h"
19 #include <api/fs/fs.h>
20 #include "util.h"
21 #include "iostat.h"
22 #include "pmu-hybrid.h"
23 #include "evlist-hybrid.h"
24 
25 #define CNTR_NOT_SUPPORTED	"<not supported>"
26 #define CNTR_NOT_COUNTED	"<not counted>"
27 
28 static void print_running(struct perf_stat_config *config,
29 			  u64 run, u64 ena)
30 {
31 
32 	double enabled_percent = 100;
33 
34 	if (run != ena)
35 		enabled_percent = 100 * run / ena;
36 	if (config->json_output)
37 		fprintf(config->output,
38 			"\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
39 			run, enabled_percent);
40 	else if (config->csv_output)
41 		fprintf(config->output,
42 			"%s%" PRIu64 "%s%.2f", config->csv_sep,
43 			run, config->csv_sep, enabled_percent);
44 	else if (run != ena)
45 		fprintf(config->output, "  (%.2f%%)", 100.0 * run / ena);
46 }
47 
48 static void print_noise_pct(struct perf_stat_config *config,
49 			    double total, double avg)
50 {
51 	double pct = rel_stddev_stats(total, avg);
52 
53 	if (config->json_output)
54 		fprintf(config->output, "\"variance\" : %.2f, ", pct);
55 	else if (config->csv_output)
56 		fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
57 	else if (pct)
58 		fprintf(config->output, "  ( +-%6.2f%% )", pct);
59 }
60 
61 static void print_noise(struct perf_stat_config *config,
62 			struct evsel *evsel, double avg)
63 {
64 	struct perf_stat_evsel *ps;
65 
66 	if (config->run_count == 1)
67 		return;
68 
69 	ps = evsel->stats;
70 	print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
71 }
72 
73 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
74 {
75 	if (nr_cgroups) {
76 		const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name  : "";
77 
78 		if (config->json_output)
79 			fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
80 		else
81 			fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
82 	}
83 }
84 
85 
86 static void aggr_printout(struct perf_stat_config *config,
87 			  struct evsel *evsel, struct aggr_cpu_id id, int nr)
88 {
89 
90 
91 	if (config->json_output && !config->interval)
92 		fprintf(config->output, "{");
93 
94 	switch (config->aggr_mode) {
95 	case AGGR_CORE:
96 		if (config->json_output) {
97 			fprintf(config->output,
98 				"\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
99 				id.socket,
100 				id.die,
101 				id.core,
102 				nr);
103 		} else {
104 			fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
105 				id.socket,
106 				id.die,
107 				config->csv_output ? 0 : -8,
108 				id.core,
109 				config->csv_sep,
110 				config->csv_output ? 0 : 4,
111 				nr,
112 				config->csv_sep);
113 		}
114 		break;
115 	case AGGR_DIE:
116 		if (config->json_output) {
117 			fprintf(config->output,
118 				"\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
119 				id.socket,
120 				id.die,
121 				nr);
122 		} else {
123 			fprintf(config->output, "S%d-D%*d%s%*d%s",
124 				id.socket,
125 				config->csv_output ? 0 : -8,
126 				id.die,
127 				config->csv_sep,
128 				config->csv_output ? 0 : 4,
129 				nr,
130 				config->csv_sep);
131 		}
132 		break;
133 	case AGGR_SOCKET:
134 		if (config->json_output) {
135 			fprintf(config->output,
136 				"\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
137 				id.socket,
138 				nr);
139 		} else {
140 			fprintf(config->output, "S%*d%s%*d%s",
141 				config->csv_output ? 0 : -5,
142 				id.socket,
143 				config->csv_sep,
144 				config->csv_output ? 0 : 4,
145 				nr,
146 				config->csv_sep);
147 		}
148 		break;
149 	case AGGR_NODE:
150 		if (config->json_output) {
151 			fprintf(config->output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
152 				id.node,
153 				nr);
154 		} else {
155 			fprintf(config->output, "N%*d%s%*d%s",
156 				config->csv_output ? 0 : -5,
157 				id.node,
158 				config->csv_sep,
159 				config->csv_output ? 0 : 4,
160 				nr,
161 				config->csv_sep);
162 		}
163 		break;
164 	case AGGR_NONE:
165 		if (config->json_output) {
166 			if (evsel->percore && !config->percore_show_thread) {
167 				fprintf(config->output, "\"core\" : \"S%d-D%d-C%d\"",
168 					id.socket,
169 					id.die,
170 					id.core);
171 			} else if (id.core > -1) {
172 				fprintf(config->output, "\"cpu\" : \"%d\", ",
173 					id.cpu.cpu);
174 			}
175 		} else {
176 			if (evsel->percore && !config->percore_show_thread) {
177 				fprintf(config->output, "S%d-D%d-C%*d%s",
178 					id.socket,
179 					id.die,
180 					config->csv_output ? 0 : -3,
181 					id.core, config->csv_sep);
182 			} else if (id.core > -1) {
183 				fprintf(config->output, "CPU%*d%s",
184 					config->csv_output ? 0 : -7,
185 					id.cpu.cpu, config->csv_sep);
186 			}
187 		}
188 		break;
189 	case AGGR_THREAD:
190 		if (config->json_output) {
191 			fprintf(config->output, "\"thread\" : \"%s-%d\", ",
192 				perf_thread_map__comm(evsel->core.threads, id.thread),
193 				perf_thread_map__pid(evsel->core.threads, id.thread));
194 		} else {
195 			fprintf(config->output, "%*s-%*d%s",
196 				config->csv_output ? 0 : 16,
197 				perf_thread_map__comm(evsel->core.threads, id.thread),
198 				config->csv_output ? 0 : -8,
199 				perf_thread_map__pid(evsel->core.threads, id.thread),
200 				config->csv_sep);
201 		}
202 		break;
203 	case AGGR_GLOBAL:
204 	case AGGR_UNSET:
205 	case AGGR_MAX:
206 	default:
207 		break;
208 	}
209 }
210 
211 struct outstate {
212 	FILE *fh;
213 	bool newline;
214 	const char *prefix;
215 	int  nfields;
216 	int  nr;
217 	struct aggr_cpu_id id;
218 	struct evsel *evsel;
219 };
220 
221 #define METRIC_LEN  35
222 
223 static void new_line_std(struct perf_stat_config *config __maybe_unused,
224 			 void *ctx)
225 {
226 	struct outstate *os = ctx;
227 
228 	os->newline = true;
229 }
230 
231 static void do_new_line_std(struct perf_stat_config *config,
232 			    struct outstate *os)
233 {
234 	fputc('\n', os->fh);
235 	fputs(os->prefix, os->fh);
236 	aggr_printout(config, os->evsel, os->id, os->nr);
237 	if (config->aggr_mode == AGGR_NONE)
238 		fprintf(os->fh, "        ");
239 	fprintf(os->fh, "                                                 ");
240 }
241 
242 static void print_metric_std(struct perf_stat_config *config,
243 			     void *ctx, const char *color, const char *fmt,
244 			     const char *unit, double val)
245 {
246 	struct outstate *os = ctx;
247 	FILE *out = os->fh;
248 	int n;
249 	bool newline = os->newline;
250 
251 	os->newline = false;
252 
253 	if (unit == NULL || fmt == NULL) {
254 		fprintf(out, "%-*s", METRIC_LEN, "");
255 		return;
256 	}
257 
258 	if (newline)
259 		do_new_line_std(config, os);
260 
261 	n = fprintf(out, " # ");
262 	if (color)
263 		n += color_fprintf(out, color, fmt, val);
264 	else
265 		n += fprintf(out, fmt, val);
266 	fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
267 }
268 
269 static void new_line_csv(struct perf_stat_config *config, void *ctx)
270 {
271 	struct outstate *os = ctx;
272 	int i;
273 
274 	fputc('\n', os->fh);
275 	if (os->prefix)
276 		fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
277 	aggr_printout(config, os->evsel, os->id, os->nr);
278 	for (i = 0; i < os->nfields; i++)
279 		fputs(config->csv_sep, os->fh);
280 }
281 
282 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
283 			     void *ctx,
284 			     const char *color __maybe_unused,
285 			     const char *fmt, const char *unit, double val)
286 {
287 	struct outstate *os = ctx;
288 	FILE *out = os->fh;
289 	char buf[64], *vals, *ends;
290 
291 	if (unit == NULL || fmt == NULL) {
292 		fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
293 		return;
294 	}
295 	snprintf(buf, sizeof(buf), fmt, val);
296 	ends = vals = skip_spaces(buf);
297 	while (isdigit(*ends) || *ends == '.')
298 		ends++;
299 	*ends = 0;
300 	fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
301 }
302 
303 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
304 			     void *ctx,
305 			     const char *color __maybe_unused,
306 			     const char *fmt __maybe_unused,
307 			     const char *unit, double val)
308 {
309 	struct outstate *os = ctx;
310 	FILE *out = os->fh;
311 
312 	fprintf(out, "\"metric-value\" : %f, ", val);
313 	fprintf(out, "\"metric-unit\" : \"%s\"", unit);
314 	if (!config->metric_only)
315 		fprintf(out, "}");
316 }
317 
318 static void new_line_json(struct perf_stat_config *config, void *ctx)
319 {
320 	struct outstate *os = ctx;
321 
322 	fputc('\n', os->fh);
323 	if (os->prefix)
324 		fprintf(os->fh, "%s", os->prefix);
325 	aggr_printout(config, os->evsel, os->id, os->nr);
326 }
327 
328 /* Filter out some columns that don't work well in metrics only mode */
329 
330 static bool valid_only_metric(const char *unit)
331 {
332 	if (!unit)
333 		return false;
334 	if (strstr(unit, "/sec") ||
335 	    strstr(unit, "CPUs utilized"))
336 		return false;
337 	return true;
338 }
339 
340 static const char *fixunit(char *buf, struct evsel *evsel,
341 			   const char *unit)
342 {
343 	if (!strncmp(unit, "of all", 6)) {
344 		snprintf(buf, 1024, "%s %s", evsel__name(evsel),
345 			 unit);
346 		return buf;
347 	}
348 	return unit;
349 }
350 
351 static void print_metric_only(struct perf_stat_config *config,
352 			      void *ctx, const char *color, const char *fmt,
353 			      const char *unit, double val)
354 {
355 	struct outstate *os = ctx;
356 	FILE *out = os->fh;
357 	char buf[1024], str[1024];
358 	unsigned mlen = config->metric_only_len;
359 
360 	if (!valid_only_metric(unit))
361 		return;
362 	unit = fixunit(buf, os->evsel, unit);
363 	if (mlen < strlen(unit))
364 		mlen = strlen(unit) + 1;
365 
366 	if (color)
367 		mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
368 
369 	color_snprintf(str, sizeof(str), color ?: "", fmt, val);
370 	fprintf(out, "%*s ", mlen, str);
371 }
372 
373 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
374 				  void *ctx, const char *color __maybe_unused,
375 				  const char *fmt,
376 				  const char *unit, double val)
377 {
378 	struct outstate *os = ctx;
379 	FILE *out = os->fh;
380 	char buf[64], *vals, *ends;
381 	char tbuf[1024];
382 
383 	if (!valid_only_metric(unit))
384 		return;
385 	unit = fixunit(tbuf, os->evsel, unit);
386 	snprintf(buf, sizeof buf, fmt, val);
387 	ends = vals = skip_spaces(buf);
388 	while (isdigit(*ends) || *ends == '.')
389 		ends++;
390 	*ends = 0;
391 	fprintf(out, "%s%s", vals, config->csv_sep);
392 }
393 
394 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
395 				  void *ctx, const char *color __maybe_unused,
396 				  const char *fmt,
397 				  const char *unit, double val)
398 {
399 	struct outstate *os = ctx;
400 	FILE *out = os->fh;
401 	char buf[64], *vals, *ends;
402 	char tbuf[1024];
403 
404 	if (!valid_only_metric(unit))
405 		return;
406 	unit = fixunit(tbuf, os->evsel, unit);
407 	snprintf(buf, sizeof(buf), fmt, val);
408 	ends = vals = skip_spaces(buf);
409 	while (isdigit(*ends) || *ends == '.')
410 		ends++;
411 	*ends = 0;
412 	fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
413 }
414 
415 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
416 			    void *ctx __maybe_unused)
417 {
418 }
419 
420 static void print_metric_header(struct perf_stat_config *config,
421 				void *ctx, const char *color __maybe_unused,
422 				const char *fmt __maybe_unused,
423 				const char *unit, double val __maybe_unused)
424 {
425 	struct outstate *os = ctx;
426 	char tbuf[1024];
427 
428 	/* In case of iostat, print metric header for first root port only */
429 	if (config->iostat_run &&
430 	    os->evsel->priv != os->evsel->evlist->selected->priv)
431 		return;
432 
433 	if (!valid_only_metric(unit) && !config->json_output)
434 		return;
435 	unit = fixunit(tbuf, os->evsel, unit);
436 
437 	if (config->json_output)
438 		fprintf(os->fh, "\"unit\" : \"%s\"", unit);
439 	else if (config->csv_output)
440 		fprintf(os->fh, "%s%s", unit, config->csv_sep);
441 	else
442 		fprintf(os->fh, "%*s ", config->metric_only_len, unit);
443 }
444 
445 static int first_shadow_cpu_map_idx(struct perf_stat_config *config,
446 				struct evsel *evsel, const struct aggr_cpu_id *id)
447 {
448 	struct perf_cpu_map *cpus = evsel__cpus(evsel);
449 	struct perf_cpu cpu;
450 	int idx;
451 
452 	if (config->aggr_mode == AGGR_NONE)
453 		return perf_cpu_map__idx(cpus, id->cpu);
454 
455 	if (!config->aggr_get_id)
456 		return 0;
457 
458 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
459 		struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
460 
461 		if (aggr_cpu_id__equal(&cpu_id, id))
462 			return idx;
463 	}
464 	return 0;
465 }
466 
467 static void abs_printout(struct perf_stat_config *config,
468 			 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
469 {
470 	FILE *output = config->output;
471 	double sc =  evsel->scale;
472 	const char *fmt;
473 
474 	if (config->csv_output) {
475 		fmt = floor(sc) != sc ?  "%.2f%s" : "%.0f%s";
476 	} else {
477 		if (config->big_num)
478 			fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
479 		else
480 			fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
481 	}
482 
483 	aggr_printout(config, evsel, id, nr);
484 
485 	if (config->json_output)
486 		fprintf(output, "\"counter-value\" : \"%f\", ", avg);
487 	else
488 		fprintf(output, fmt, avg, config->csv_sep);
489 
490 	if (config->json_output) {
491 		if (evsel->unit) {
492 			fprintf(output, "\"unit\" : \"%s\", ",
493 				evsel->unit);
494 		}
495 	} else {
496 		if (evsel->unit)
497 			fprintf(output, "%-*s%s",
498 				config->csv_output ? 0 : config->unit_width,
499 				evsel->unit, config->csv_sep);
500 	}
501 
502 	if (config->json_output)
503 		fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
504 	else
505 		fprintf(output, "%-*s", config->csv_output ? 0 : 32, evsel__name(evsel));
506 
507 	print_cgroup(config, evsel);
508 }
509 
510 static bool is_mixed_hw_group(struct evsel *counter)
511 {
512 	struct evlist *evlist = counter->evlist;
513 	u32 pmu_type = counter->core.attr.type;
514 	struct evsel *pos;
515 
516 	if (counter->core.nr_members < 2)
517 		return false;
518 
519 	evlist__for_each_entry(evlist, pos) {
520 		/* software events can be part of any hardware group */
521 		if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
522 			continue;
523 		if (pmu_type == PERF_TYPE_SOFTWARE) {
524 			pmu_type = pos->core.attr.type;
525 			continue;
526 		}
527 		if (pmu_type != pos->core.attr.type)
528 			return true;
529 	}
530 
531 	return false;
532 }
533 
534 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
535 		     struct evsel *counter, double uval,
536 		     char *prefix, u64 run, u64 ena, double noise,
537 		     struct runtime_stat *st)
538 {
539 	struct perf_stat_output_ctx out;
540 	struct outstate os = {
541 		.fh = config->output,
542 		.prefix = prefix ? prefix : "",
543 		.id = id,
544 		.nr = nr,
545 		.evsel = counter,
546 	};
547 	print_metric_t pm;
548 	new_line_t nl;
549 
550 	if (config->csv_output) {
551 		static const int aggr_fields[AGGR_MAX] = {
552 			[AGGR_NONE] = 1,
553 			[AGGR_GLOBAL] = 0,
554 			[AGGR_SOCKET] = 2,
555 			[AGGR_DIE] = 2,
556 			[AGGR_CORE] = 2,
557 			[AGGR_THREAD] = 1,
558 			[AGGR_UNSET] = 0,
559 			[AGGR_NODE] = 0,
560 		};
561 
562 		pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
563 		nl = config->metric_only ? new_line_metric : new_line_csv;
564 		os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
565 	} else if (config->json_output) {
566 		pm = config->metric_only ? print_metric_only_json : print_metric_json;
567 		nl = config->metric_only ? new_line_metric : new_line_json;
568 	} else {
569 		pm = config->metric_only ? print_metric_only : print_metric_std;
570 		nl = config->metric_only ? new_line_metric : new_line_std;
571 	}
572 
573 	if (!config->no_csv_summary && config->csv_output &&
574 	    config->summary && !config->interval) {
575 		fprintf(config->output, "%16s%s", "summary", config->csv_sep);
576 	}
577 
578 	if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
579 		if (config->metric_only) {
580 			pm(config, &os, NULL, "", "", 0);
581 			return;
582 		}
583 		aggr_printout(config, counter, id, nr);
584 
585 		if (config->json_output) {
586 			fprintf(config->output, "\"counter-value\" : \"%s\", ",
587 					counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED);
588 		} else {
589 			fprintf(config->output, "%*s%s",
590 				config->csv_output ? 0 : 18,
591 				counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
592 				config->csv_sep);
593 		}
594 
595 		if (counter->supported) {
596 			if (!evlist__has_hybrid(counter->evlist)) {
597 				config->print_free_counters_hint = 1;
598 				if (is_mixed_hw_group(counter))
599 					config->print_mixed_hw_group_error = 1;
600 			}
601 		}
602 
603 		if (config->json_output) {
604 			fprintf(config->output, "\"unit\" : \"%s\", ", counter->unit);
605 		} else {
606 			fprintf(config->output, "%-*s%s",
607 				config->csv_output ? 0 : config->unit_width,
608 				counter->unit, config->csv_sep);
609 		}
610 
611 		if (config->json_output) {
612 			fprintf(config->output, "\"event\" : \"%s\", ",
613 				evsel__name(counter));
614 		} else {
615 			fprintf(config->output, "%*s",
616 				 config->csv_output ? 0 : -25, evsel__name(counter));
617 		}
618 
619 		print_cgroup(config, counter);
620 
621 		if (!config->csv_output && !config->json_output)
622 			pm(config, &os, NULL, NULL, "", 0);
623 		print_noise(config, counter, noise);
624 		print_running(config, run, ena);
625 		if (config->csv_output)
626 			pm(config, &os, NULL, NULL, "", 0);
627 		else if (config->json_output)
628 			pm(config, &os, NULL, NULL, "", 0);
629 		return;
630 	}
631 
632 	if (!config->metric_only)
633 		abs_printout(config, id, nr, counter, uval);
634 
635 	out.print_metric = pm;
636 	out.new_line = nl;
637 	out.ctx = &os;
638 	out.force_header = false;
639 
640 	if (config->csv_output && !config->metric_only) {
641 		print_noise(config, counter, noise);
642 		print_running(config, run, ena);
643 	} else if (config->json_output && !config->metric_only) {
644 		print_noise(config, counter, noise);
645 		print_running(config, run, ena);
646 	}
647 
648 	perf_stat__print_shadow_stats(config, counter, uval,
649 				first_shadow_cpu_map_idx(config, counter, &id),
650 				&out, &config->metric_events, st);
651 	if (!config->csv_output && !config->metric_only && !config->json_output) {
652 		print_noise(config, counter, noise);
653 		print_running(config, run, ena);
654 	}
655 }
656 
657 static void aggr_update_shadow(struct perf_stat_config *config,
658 			       struct evlist *evlist)
659 {
660 	int idx, s;
661 	struct perf_cpu cpu;
662 	struct aggr_cpu_id s2, id;
663 	u64 val;
664 	struct evsel *counter;
665 	struct perf_cpu_map *cpus;
666 
667 	for (s = 0; s < config->aggr_map->nr; s++) {
668 		id = config->aggr_map->map[s];
669 		evlist__for_each_entry(evlist, counter) {
670 			cpus = evsel__cpus(counter);
671 			val = 0;
672 			perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
673 				s2 = config->aggr_get_id(config, cpu);
674 				if (!aggr_cpu_id__equal(&s2, &id))
675 					continue;
676 				val += perf_counts(counter->counts, idx, 0)->val;
677 			}
678 			perf_stat__update_shadow_stats(counter, val,
679 					first_shadow_cpu_map_idx(config, counter, &id),
680 					&rt_stat);
681 		}
682 	}
683 }
684 
685 static void uniquify_event_name(struct evsel *counter)
686 {
687 	char *new_name;
688 	char *config;
689 	int ret = 0;
690 
691 	if (counter->uniquified_name || counter->use_config_name ||
692 	    !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
693 					   strlen(counter->pmu_name)))
694 		return;
695 
696 	config = strchr(counter->name, '/');
697 	if (config) {
698 		if (asprintf(&new_name,
699 			     "%s%s", counter->pmu_name, config) > 0) {
700 			free(counter->name);
701 			counter->name = new_name;
702 		}
703 	} else {
704 		if (perf_pmu__has_hybrid()) {
705 			ret = asprintf(&new_name, "%s/%s/",
706 				       counter->pmu_name, counter->name);
707 		} else {
708 			ret = asprintf(&new_name, "%s [%s]",
709 				       counter->name, counter->pmu_name);
710 		}
711 
712 		if (ret) {
713 			free(counter->name);
714 			counter->name = new_name;
715 		}
716 	}
717 
718 	counter->uniquified_name = true;
719 }
720 
721 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
722 			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
723 				       bool first),
724 			    void *data)
725 {
726 	struct evlist *evlist = counter->evlist;
727 	struct evsel *alias;
728 
729 	alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
730 	list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
731 		/* Merge events with the same name, etc. but on different PMUs. */
732 		if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
733 			alias->scale == counter->scale &&
734 			alias->cgrp == counter->cgrp &&
735 			!strcmp(alias->unit, counter->unit) &&
736 			evsel__is_clock(alias) == evsel__is_clock(counter) &&
737 			strcmp(alias->pmu_name, counter->pmu_name)) {
738 			alias->merged_stat = true;
739 			cb(config, alias, data, false);
740 		}
741 	}
742 }
743 
744 static bool is_uncore(struct evsel *evsel)
745 {
746 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
747 
748 	return pmu && pmu->is_uncore;
749 }
750 
751 static bool hybrid_uniquify(struct evsel *evsel)
752 {
753 	return perf_pmu__has_hybrid() && !is_uncore(evsel);
754 }
755 
756 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
757 			 bool check)
758 {
759 	if (hybrid_uniquify(counter)) {
760 		if (check)
761 			return config && config->hybrid_merge;
762 		else
763 			return config && !config->hybrid_merge;
764 	}
765 
766 	return false;
767 }
768 
769 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
770 			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
771 				       bool first),
772 			    void *data)
773 {
774 	if (counter->merged_stat)
775 		return false;
776 	cb(config, counter, data, true);
777 	if (config->no_merge || hybrid_merge(counter, config, false))
778 		uniquify_event_name(counter);
779 	else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
780 		collect_all_aliases(config, counter, cb, data);
781 	return true;
782 }
783 
784 struct aggr_data {
785 	u64 ena, run, val;
786 	struct aggr_cpu_id id;
787 	int nr;
788 	int cpu_map_idx;
789 };
790 
791 static void aggr_cb(struct perf_stat_config *config,
792 		    struct evsel *counter, void *data, bool first)
793 {
794 	struct aggr_data *ad = data;
795 	int idx;
796 	struct perf_cpu cpu;
797 	struct perf_cpu_map *cpus;
798 	struct aggr_cpu_id s2;
799 
800 	cpus = evsel__cpus(counter);
801 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
802 		struct perf_counts_values *counts;
803 
804 		s2 = config->aggr_get_id(config, cpu);
805 		if (!aggr_cpu_id__equal(&s2, &ad->id))
806 			continue;
807 		if (first)
808 			ad->nr++;
809 		counts = perf_counts(counter->counts, idx, 0);
810 		/*
811 		 * When any result is bad, make them all to give
812 		 * consistent output in interval mode.
813 		 */
814 		if (counts->ena == 0 || counts->run == 0 ||
815 		    counter->counts->scaled == -1) {
816 			ad->ena = 0;
817 			ad->run = 0;
818 			break;
819 		}
820 		ad->val += counts->val;
821 		ad->ena += counts->ena;
822 		ad->run += counts->run;
823 	}
824 }
825 
826 static void print_counter_aggrdata(struct perf_stat_config *config,
827 				   struct evsel *counter, int s,
828 				   char *prefix, bool metric_only,
829 				   bool *first, struct perf_cpu cpu)
830 {
831 	struct aggr_data ad;
832 	FILE *output = config->output;
833 	u64 ena, run, val;
834 	int nr;
835 	struct aggr_cpu_id id;
836 	double uval;
837 
838 	ad.id = id = config->aggr_map->map[s];
839 	ad.val = ad.ena = ad.run = 0;
840 	ad.nr = 0;
841 	if (!collect_data(config, counter, aggr_cb, &ad))
842 		return;
843 
844 	if (perf_pmu__has_hybrid() && ad.ena == 0)
845 		return;
846 
847 	nr = ad.nr;
848 	ena = ad.ena;
849 	run = ad.run;
850 	val = ad.val;
851 	if (*first && metric_only) {
852 		*first = false;
853 		aggr_printout(config, counter, id, nr);
854 	}
855 	if (prefix && !metric_only)
856 		fprintf(output, "%s", prefix);
857 
858 	uval = val * counter->scale;
859 	if (cpu.cpu != -1)
860 		id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
861 
862 	printout(config, id, nr, counter, uval,
863 		 prefix, run, ena, 1.0, &rt_stat);
864 	if (!metric_only)
865 		fputc('\n', output);
866 }
867 
868 static void print_aggr(struct perf_stat_config *config,
869 		       struct evlist *evlist,
870 		       char *prefix)
871 {
872 	bool metric_only = config->metric_only;
873 	FILE *output = config->output;
874 	struct evsel *counter;
875 	int s;
876 	bool first;
877 
878 	if (!config->aggr_map || !config->aggr_get_id)
879 		return;
880 
881 	aggr_update_shadow(config, evlist);
882 
883 	/*
884 	 * With metric_only everything is on a single line.
885 	 * Without each counter has its own line.
886 	 */
887 	for (s = 0; s < config->aggr_map->nr; s++) {
888 		if (prefix && metric_only)
889 			fprintf(output, "%s", prefix);
890 
891 		first = true;
892 		evlist__for_each_entry(evlist, counter) {
893 			print_counter_aggrdata(config, counter, s,
894 					prefix, metric_only,
895 					&first, (struct perf_cpu){ .cpu = -1 });
896 		}
897 		if (metric_only)
898 			fputc('\n', output);
899 	}
900 }
901 
902 static int cmp_val(const void *a, const void *b)
903 {
904 	return ((struct perf_aggr_thread_value *)b)->val -
905 		((struct perf_aggr_thread_value *)a)->val;
906 }
907 
908 static struct perf_aggr_thread_value *sort_aggr_thread(
909 					struct evsel *counter,
910 					int *ret,
911 					struct target *_target)
912 {
913 	int nthreads = perf_thread_map__nr(counter->core.threads);
914 	int i = 0;
915 	double uval;
916 	struct perf_aggr_thread_value *buf;
917 
918 	buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
919 	if (!buf)
920 		return NULL;
921 
922 	for (int thread = 0; thread < nthreads; thread++) {
923 		int idx;
924 		u64 ena = 0, run = 0, val = 0;
925 
926 		perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
927 			struct perf_counts_values *counts =
928 				perf_counts(counter->counts, idx, thread);
929 
930 			val += counts->val;
931 			ena += counts->ena;
932 			run += counts->run;
933 		}
934 
935 		uval = val * counter->scale;
936 
937 		/*
938 		 * Skip value 0 when enabling --per-thread globally,
939 		 * otherwise too many 0 output.
940 		 */
941 		if (uval == 0.0 && target__has_per_thread(_target))
942 			continue;
943 
944 		buf[i].counter = counter;
945 		buf[i].id = aggr_cpu_id__empty();
946 		buf[i].id.thread = thread;
947 		buf[i].uval = uval;
948 		buf[i].val = val;
949 		buf[i].run = run;
950 		buf[i].ena = ena;
951 		i++;
952 	}
953 
954 	qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
955 
956 	if (ret)
957 		*ret = i;
958 
959 	return buf;
960 }
961 
962 static void print_aggr_thread(struct perf_stat_config *config,
963 			      struct target *_target,
964 			      struct evsel *counter, char *prefix)
965 {
966 	FILE *output = config->output;
967 	int thread, sorted_threads;
968 	struct aggr_cpu_id id;
969 	struct perf_aggr_thread_value *buf;
970 
971 	buf = sort_aggr_thread(counter, &sorted_threads, _target);
972 	if (!buf) {
973 		perror("cannot sort aggr thread");
974 		return;
975 	}
976 
977 	for (thread = 0; thread < sorted_threads; thread++) {
978 		if (prefix)
979 			fprintf(output, "%s", prefix);
980 
981 		id = buf[thread].id;
982 		if (config->stats)
983 			printout(config, id, 0, buf[thread].counter, buf[thread].uval,
984 				 prefix, buf[thread].run, buf[thread].ena, 1.0,
985 				 &config->stats[id.thread]);
986 		else
987 			printout(config, id, 0, buf[thread].counter, buf[thread].uval,
988 				 prefix, buf[thread].run, buf[thread].ena, 1.0,
989 				 &rt_stat);
990 		fputc('\n', output);
991 	}
992 
993 	free(buf);
994 }
995 
996 struct caggr_data {
997 	double avg, avg_enabled, avg_running;
998 };
999 
1000 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
1001 			    struct evsel *counter, void *data,
1002 			    bool first __maybe_unused)
1003 {
1004 	struct caggr_data *cd = data;
1005 	struct perf_counts_values *aggr = &counter->counts->aggr;
1006 
1007 	cd->avg += aggr->val;
1008 	cd->avg_enabled += aggr->ena;
1009 	cd->avg_running += aggr->run;
1010 }
1011 
1012 /*
1013  * Print out the results of a single counter:
1014  * aggregated counts in system-wide mode
1015  */
1016 static void print_counter_aggr(struct perf_stat_config *config,
1017 			       struct evsel *counter, char *prefix)
1018 {
1019 	bool metric_only = config->metric_only;
1020 	FILE *output = config->output;
1021 	double uval;
1022 	struct caggr_data cd = { .avg = 0.0 };
1023 
1024 	if (!collect_data(config, counter, counter_aggr_cb, &cd))
1025 		return;
1026 
1027 	if (prefix && !metric_only)
1028 		fprintf(output, "%s", prefix);
1029 
1030 	uval = cd.avg * counter->scale;
1031 	printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
1032 		 cd.avg_enabled, cd.avg, &rt_stat);
1033 	if (!metric_only)
1034 		fprintf(output, "\n");
1035 }
1036 
1037 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1038 		       struct evsel *counter, void *data,
1039 		       bool first __maybe_unused)
1040 {
1041 	struct aggr_data *ad = data;
1042 
1043 	ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
1044 	ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
1045 	ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
1046 }
1047 
1048 /*
1049  * Print out the results of a single counter:
1050  * does not use aggregated count in system-wide
1051  */
1052 static void print_counter(struct perf_stat_config *config,
1053 			  struct evsel *counter, char *prefix)
1054 {
1055 	FILE *output = config->output;
1056 	u64 ena, run, val;
1057 	double uval;
1058 	int idx;
1059 	struct perf_cpu cpu;
1060 	struct aggr_cpu_id id;
1061 
1062 	perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
1063 		struct aggr_data ad = { .cpu_map_idx = idx };
1064 
1065 		if (!collect_data(config, counter, counter_cb, &ad))
1066 			return;
1067 		val = ad.val;
1068 		ena = ad.ena;
1069 		run = ad.run;
1070 
1071 		if (prefix)
1072 			fprintf(output, "%s", prefix);
1073 
1074 		uval = val * counter->scale;
1075 		id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1076 		printout(config, id, 0, counter, uval, prefix,
1077 			 run, ena, 1.0, &rt_stat);
1078 
1079 		fputc('\n', output);
1080 	}
1081 }
1082 
1083 static void print_no_aggr_metric(struct perf_stat_config *config,
1084 				 struct evlist *evlist,
1085 				 char *prefix)
1086 {
1087 	int all_idx;
1088 	struct perf_cpu cpu;
1089 
1090 	perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1091 		struct evsel *counter;
1092 		bool first = true;
1093 
1094 		evlist__for_each_entry(evlist, counter) {
1095 			u64 ena, run, val;
1096 			double uval;
1097 			struct aggr_cpu_id id;
1098 			int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1099 
1100 			if (counter_idx < 0)
1101 				continue;
1102 
1103 			id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1104 			if (first) {
1105 				if (prefix)
1106 					fputs(prefix, config->output);
1107 				aggr_printout(config, counter, id, 0);
1108 				first = false;
1109 			}
1110 			val = perf_counts(counter->counts, counter_idx, 0)->val;
1111 			ena = perf_counts(counter->counts, counter_idx, 0)->ena;
1112 			run = perf_counts(counter->counts, counter_idx, 0)->run;
1113 
1114 			uval = val * counter->scale;
1115 			printout(config, id, 0, counter, uval, prefix,
1116 				 run, ena, 1.0, &rt_stat);
1117 		}
1118 		if (!first)
1119 			fputc('\n', config->output);
1120 	}
1121 }
1122 
1123 static int aggr_header_lens[] = {
1124 	[AGGR_CORE] = 24,
1125 	[AGGR_DIE] = 18,
1126 	[AGGR_SOCKET] = 12,
1127 	[AGGR_NONE] = 6,
1128 	[AGGR_THREAD] = 24,
1129 	[AGGR_GLOBAL] = 0,
1130 };
1131 
1132 static const char *aggr_header_csv[] = {
1133 	[AGGR_CORE] 	= 	"core,cpus,",
1134 	[AGGR_DIE] 	= 	"die,cpus",
1135 	[AGGR_SOCKET] 	= 	"socket,cpus",
1136 	[AGGR_NONE] 	= 	"cpu,",
1137 	[AGGR_THREAD] 	= 	"comm-pid,",
1138 	[AGGR_GLOBAL] 	=	""
1139 };
1140 
1141 static void print_metric_headers(struct perf_stat_config *config,
1142 				 struct evlist *evlist,
1143 				 const char *prefix, bool no_indent)
1144 {
1145 	struct perf_stat_output_ctx out;
1146 	struct evsel *counter;
1147 	struct outstate os = {
1148 		.fh = config->output
1149 	};
1150 	bool first = true;
1151 
1152 		if (config->json_output && !config->interval)
1153 			fprintf(config->output, "{");
1154 
1155 	if (prefix && !config->json_output)
1156 		fprintf(config->output, "%s", prefix);
1157 
1158 	if (!config->csv_output && !no_indent)
1159 		fprintf(config->output, "%*s",
1160 			aggr_header_lens[config->aggr_mode], "");
1161 	if (config->csv_output) {
1162 		if (config->interval)
1163 			fputs("time,", config->output);
1164 		if (!config->iostat_run)
1165 			fputs(aggr_header_csv[config->aggr_mode], config->output);
1166 	}
1167 	if (config->iostat_run)
1168 		iostat_print_header_prefix(config);
1169 
1170 	/* Print metrics headers only */
1171 	evlist__for_each_entry(evlist, counter) {
1172 		os.evsel = counter;
1173 		out.ctx = &os;
1174 		out.print_metric = print_metric_header;
1175 		if (!first && config->json_output)
1176 			fprintf(config->output, ", ");
1177 		first = false;
1178 		out.new_line = new_line_metric;
1179 		out.force_header = true;
1180 		perf_stat__print_shadow_stats(config, counter, 0,
1181 					      0,
1182 					      &out,
1183 					      &config->metric_events,
1184 					      &rt_stat);
1185 	}
1186 	if (config->json_output)
1187 		fprintf(config->output, "}");
1188 	fputc('\n', config->output);
1189 }
1190 
1191 static void print_interval(struct perf_stat_config *config,
1192 			   struct evlist *evlist,
1193 			   char *prefix, struct timespec *ts)
1194 {
1195 	bool metric_only = config->metric_only;
1196 	unsigned int unit_width = config->unit_width;
1197 	FILE *output = config->output;
1198 	static int num_print_interval;
1199 
1200 	if (config->interval_clear)
1201 		puts(CONSOLE_CLEAR);
1202 
1203 	if (!config->iostat_run && !config->json_output)
1204 		sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1205 				 ts->tv_nsec, config->csv_sep);
1206 	if (!config->iostat_run && config->json_output && !config->metric_only)
1207 		sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1208 				 ts->tv_sec, ts->tv_nsec);
1209 	if (!config->iostat_run && config->json_output && config->metric_only)
1210 		sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1211 				 ts->tv_sec, ts->tv_nsec);
1212 
1213 	if ((num_print_interval == 0 && !config->csv_output && !config->json_output)
1214 			 || config->interval_clear) {
1215 		switch (config->aggr_mode) {
1216 		case AGGR_NODE:
1217 			fprintf(output, "#           time node   cpus");
1218 			if (!metric_only)
1219 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1220 			break;
1221 		case AGGR_SOCKET:
1222 			fprintf(output, "#           time socket cpus");
1223 			if (!metric_only)
1224 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1225 			break;
1226 		case AGGR_DIE:
1227 			fprintf(output, "#           time die          cpus");
1228 			if (!metric_only)
1229 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1230 			break;
1231 		case AGGR_CORE:
1232 			fprintf(output, "#           time core            cpus");
1233 			if (!metric_only)
1234 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1235 			break;
1236 		case AGGR_NONE:
1237 			fprintf(output, "#           time CPU    ");
1238 			if (!metric_only)
1239 				fprintf(output, "                counts %*s events\n", unit_width, "unit");
1240 			break;
1241 		case AGGR_THREAD:
1242 			fprintf(output, "#           time             comm-pid");
1243 			if (!metric_only)
1244 				fprintf(output, "                  counts %*s events\n", unit_width, "unit");
1245 			break;
1246 		case AGGR_GLOBAL:
1247 		default:
1248 			if (!config->iostat_run) {
1249 				fprintf(output, "#           time");
1250 				if (!metric_only)
1251 					fprintf(output, "             counts %*s events\n", unit_width, "unit");
1252 			}
1253 		case AGGR_UNSET:
1254 		case AGGR_MAX:
1255 			break;
1256 		}
1257 	}
1258 
1259 	if ((num_print_interval == 0 || config->interval_clear)
1260 			 && metric_only && !config->json_output)
1261 		print_metric_headers(config, evlist, " ", true);
1262 	if ((num_print_interval == 0 || config->interval_clear)
1263 			 && metric_only && config->json_output) {
1264 		fprintf(output, "{");
1265 		print_metric_headers(config, evlist, " ", true);
1266 	}
1267 	if (++num_print_interval == 25)
1268 		num_print_interval = 0;
1269 }
1270 
1271 static void print_header(struct perf_stat_config *config,
1272 			 struct target *_target,
1273 			 int argc, const char **argv)
1274 {
1275 	FILE *output = config->output;
1276 	int i;
1277 
1278 	fflush(stdout);
1279 
1280 	if (!config->csv_output && !config->json_output) {
1281 		fprintf(output, "\n");
1282 		fprintf(output, " Performance counter stats for ");
1283 		if (_target->bpf_str)
1284 			fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1285 		else if (_target->system_wide)
1286 			fprintf(output, "\'system wide");
1287 		else if (_target->cpu_list)
1288 			fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1289 		else if (!target__has_task(_target)) {
1290 			fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1291 			for (i = 1; argv && (i < argc); i++)
1292 				fprintf(output, " %s", argv[i]);
1293 		} else if (_target->pid)
1294 			fprintf(output, "process id \'%s", _target->pid);
1295 		else
1296 			fprintf(output, "thread id \'%s", _target->tid);
1297 
1298 		fprintf(output, "\'");
1299 		if (config->run_count > 1)
1300 			fprintf(output, " (%d runs)", config->run_count);
1301 		fprintf(output, ":\n\n");
1302 	}
1303 }
1304 
1305 static int get_precision(double num)
1306 {
1307 	if (num > 1)
1308 		return 0;
1309 
1310 	return lround(ceil(-log10(num)));
1311 }
1312 
1313 static void print_table(struct perf_stat_config *config,
1314 			FILE *output, int precision, double avg)
1315 {
1316 	char tmp[64];
1317 	int idx, indent = 0;
1318 
1319 	scnprintf(tmp, 64, " %17.*f", precision, avg);
1320 	while (tmp[indent] == ' ')
1321 		indent++;
1322 
1323 	fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1324 
1325 	for (idx = 0; idx < config->run_count; idx++) {
1326 		double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1327 		int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1328 
1329 		fprintf(output, " %17.*f (%+.*f) ",
1330 			precision, run, precision, run - avg);
1331 
1332 		for (h = 0; h < n; h++)
1333 			fprintf(output, "#");
1334 
1335 		fprintf(output, "\n");
1336 	}
1337 
1338 	fprintf(output, "\n%*s# Final result:\n", indent, "");
1339 }
1340 
1341 static double timeval2double(struct timeval *t)
1342 {
1343 	return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1344 }
1345 
1346 static void print_footer(struct perf_stat_config *config)
1347 {
1348 	double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1349 	FILE *output = config->output;
1350 
1351 	if (!config->null_run)
1352 		fprintf(output, "\n");
1353 
1354 	if (config->run_count == 1) {
1355 		fprintf(output, " %17.9f seconds time elapsed", avg);
1356 
1357 		if (config->ru_display) {
1358 			double ru_utime = timeval2double(&config->ru_data.ru_utime);
1359 			double ru_stime = timeval2double(&config->ru_data.ru_stime);
1360 
1361 			fprintf(output, "\n\n");
1362 			fprintf(output, " %17.9f seconds user\n", ru_utime);
1363 			fprintf(output, " %17.9f seconds sys\n", ru_stime);
1364 		}
1365 	} else {
1366 		double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1367 		/*
1368 		 * Display at most 2 more significant
1369 		 * digits than the stddev inaccuracy.
1370 		 */
1371 		int precision = get_precision(sd) + 2;
1372 
1373 		if (config->walltime_run_table)
1374 			print_table(config, output, precision, avg);
1375 
1376 		fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1377 			precision, avg, precision, sd);
1378 
1379 		print_noise_pct(config, sd, avg);
1380 	}
1381 	fprintf(output, "\n\n");
1382 
1383 	if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1384 		fprintf(output,
1385 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1386 "	echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1387 "	perf stat ...\n"
1388 "	echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1389 
1390 	if (config->print_mixed_hw_group_error)
1391 		fprintf(output,
1392 			"The events in group usually have to be from "
1393 			"the same PMU. Try reorganizing the group.\n");
1394 }
1395 
1396 static void print_percore_thread(struct perf_stat_config *config,
1397 				 struct evsel *counter, char *prefix)
1398 {
1399 	int s;
1400 	struct aggr_cpu_id s2, id;
1401 	struct perf_cpu_map *cpus;
1402 	bool first = true;
1403 	int idx;
1404 	struct perf_cpu cpu;
1405 
1406 	cpus = evsel__cpus(counter);
1407 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1408 		s2 = config->aggr_get_id(config, cpu);
1409 		for (s = 0; s < config->aggr_map->nr; s++) {
1410 			id = config->aggr_map->map[s];
1411 			if (aggr_cpu_id__equal(&s2, &id))
1412 				break;
1413 		}
1414 
1415 		print_counter_aggrdata(config, counter, s,
1416 				       prefix, false,
1417 				       &first, cpu);
1418 	}
1419 }
1420 
1421 static void print_percore(struct perf_stat_config *config,
1422 			  struct evsel *counter, char *prefix)
1423 {
1424 	bool metric_only = config->metric_only;
1425 	FILE *output = config->output;
1426 	int s;
1427 	bool first = true;
1428 
1429 	if (!config->aggr_map || !config->aggr_get_id)
1430 		return;
1431 
1432 	if (config->percore_show_thread)
1433 		return print_percore_thread(config, counter, prefix);
1434 
1435 	for (s = 0; s < config->aggr_map->nr; s++) {
1436 		if (prefix && metric_only)
1437 			fprintf(output, "%s", prefix);
1438 
1439 		print_counter_aggrdata(config, counter, s,
1440 				prefix, metric_only,
1441 				&first, (struct perf_cpu){ .cpu = -1 });
1442 	}
1443 
1444 	if (metric_only)
1445 		fputc('\n', output);
1446 }
1447 
1448 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1449 			    struct target *_target, struct timespec *ts, int argc, const char **argv)
1450 {
1451 	bool metric_only = config->metric_only;
1452 	int interval = config->interval;
1453 	struct evsel *counter;
1454 	char buf[64], *prefix = NULL;
1455 
1456 	if (config->iostat_run)
1457 		evlist->selected = evlist__first(evlist);
1458 
1459 	if (interval)
1460 		print_interval(config, evlist, prefix = buf, ts);
1461 	else
1462 		print_header(config, _target, argc, argv);
1463 
1464 	if (metric_only) {
1465 		static int num_print_iv;
1466 
1467 		if (num_print_iv == 0 && !interval)
1468 			print_metric_headers(config, evlist, prefix, false);
1469 		if (num_print_iv++ == 25)
1470 			num_print_iv = 0;
1471 		if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1472 			fprintf(config->output, "%s", prefix);
1473 
1474 		if (config->json_output && !config->metric_only)
1475 			fprintf(config->output, "}");
1476 	}
1477 
1478 	switch (config->aggr_mode) {
1479 	case AGGR_CORE:
1480 	case AGGR_DIE:
1481 	case AGGR_SOCKET:
1482 	case AGGR_NODE:
1483 		print_aggr(config, evlist, prefix);
1484 		break;
1485 	case AGGR_THREAD:
1486 		evlist__for_each_entry(evlist, counter) {
1487 			print_aggr_thread(config, _target, counter, prefix);
1488 		}
1489 		break;
1490 	case AGGR_GLOBAL:
1491 		if (config->iostat_run)
1492 			iostat_print_counters(evlist, config, ts, prefix = buf,
1493 					      print_counter_aggr);
1494 		else {
1495 			evlist__for_each_entry(evlist, counter) {
1496 				print_counter_aggr(config, counter, prefix);
1497 			}
1498 			if (metric_only)
1499 				fputc('\n', config->output);
1500 		}
1501 		break;
1502 	case AGGR_NONE:
1503 		if (metric_only)
1504 			print_no_aggr_metric(config, evlist, prefix);
1505 		else {
1506 			evlist__for_each_entry(evlist, counter) {
1507 				if (counter->percore)
1508 					print_percore(config, counter, prefix);
1509 				else
1510 					print_counter(config, counter, prefix);
1511 			}
1512 		}
1513 		break;
1514 	case AGGR_MAX:
1515 	case AGGR_UNSET:
1516 	default:
1517 		break;
1518 	}
1519 
1520 	if (!interval && !config->csv_output && !config->json_output)
1521 		print_footer(config);
1522 
1523 	fflush(config->output);
1524 }
1525