xref: /openbmc/linux/tools/perf/util/stat-display.c (revision fadbafc1)
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), 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.cpu.cpu > -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.cpu.cpu > -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_idx),
193 				perf_thread_map__pid(evsel->core.threads, id.thread_idx));
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_idx),
198 				config->csv_output ? 0 : -8,
199 				perf_thread_map__pid(evsel->core.threads, id.thread_idx),
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_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_mode == AGGR_THREAD)
456 		return id->thread_idx;
457 
458 	if (!config->aggr_get_id)
459 		return 0;
460 
461 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
462 		struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
463 
464 		if (aggr_cpu_id__equal(&cpu_id, id))
465 			return idx;
466 	}
467 	return 0;
468 }
469 
470 static void abs_printout(struct perf_stat_config *config,
471 			 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
472 {
473 	FILE *output = config->output;
474 	double sc =  evsel->scale;
475 	const char *fmt;
476 
477 	if (config->csv_output) {
478 		fmt = floor(sc) != sc ?  "%.2f%s" : "%.0f%s";
479 	} else {
480 		if (config->big_num)
481 			fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
482 		else
483 			fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
484 	}
485 
486 	aggr_printout(config, evsel, id, nr);
487 
488 	if (config->json_output)
489 		fprintf(output, "\"counter-value\" : \"%f\", ", avg);
490 	else
491 		fprintf(output, fmt, avg, config->csv_sep);
492 
493 	if (config->json_output) {
494 		if (evsel->unit) {
495 			fprintf(output, "\"unit\" : \"%s\", ",
496 				evsel->unit);
497 		}
498 	} else {
499 		if (evsel->unit)
500 			fprintf(output, "%-*s%s",
501 				config->csv_output ? 0 : config->unit_width,
502 				evsel->unit, config->csv_sep);
503 	}
504 
505 	if (config->json_output)
506 		fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
507 	else
508 		fprintf(output, "%-*s", config->csv_output ? 0 : 32, evsel__name(evsel));
509 
510 	print_cgroup(config, evsel);
511 }
512 
513 static bool is_mixed_hw_group(struct evsel *counter)
514 {
515 	struct evlist *evlist = counter->evlist;
516 	u32 pmu_type = counter->core.attr.type;
517 	struct evsel *pos;
518 
519 	if (counter->core.nr_members < 2)
520 		return false;
521 
522 	evlist__for_each_entry(evlist, pos) {
523 		/* software events can be part of any hardware group */
524 		if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
525 			continue;
526 		if (pmu_type == PERF_TYPE_SOFTWARE) {
527 			pmu_type = pos->core.attr.type;
528 			continue;
529 		}
530 		if (pmu_type != pos->core.attr.type)
531 			return true;
532 	}
533 
534 	return false;
535 }
536 
537 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
538 		     struct evsel *counter, double uval,
539 		     char *prefix, u64 run, u64 ena, double noise,
540 		     struct runtime_stat *st)
541 {
542 	struct perf_stat_output_ctx out;
543 	struct outstate os = {
544 		.fh = config->output,
545 		.prefix = prefix ? prefix : "",
546 		.id = id,
547 		.nr = nr,
548 		.evsel = counter,
549 	};
550 	print_metric_t pm;
551 	new_line_t nl;
552 
553 	if (config->csv_output) {
554 		static const int aggr_fields[AGGR_MAX] = {
555 			[AGGR_NONE] = 1,
556 			[AGGR_GLOBAL] = 0,
557 			[AGGR_SOCKET] = 2,
558 			[AGGR_DIE] = 2,
559 			[AGGR_CORE] = 2,
560 			[AGGR_THREAD] = 1,
561 			[AGGR_UNSET] = 0,
562 			[AGGR_NODE] = 0,
563 		};
564 
565 		pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
566 		nl = config->metric_only ? new_line_metric : new_line_csv;
567 		os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
568 	} else if (config->json_output) {
569 		pm = config->metric_only ? print_metric_only_json : print_metric_json;
570 		nl = config->metric_only ? new_line_metric : new_line_json;
571 	} else {
572 		pm = config->metric_only ? print_metric_only : print_metric_std;
573 		nl = config->metric_only ? new_line_metric : new_line_std;
574 	}
575 
576 	if (!config->no_csv_summary && config->csv_output &&
577 	    config->summary && !config->interval) {
578 		fprintf(config->output, "%16s%s", "summary", config->csv_sep);
579 	}
580 
581 	if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
582 		if (config->metric_only) {
583 			pm(config, &os, NULL, "", "", 0);
584 			return;
585 		}
586 		aggr_printout(config, counter, id, nr);
587 
588 		if (config->json_output) {
589 			fprintf(config->output, "\"counter-value\" : \"%s\", ",
590 					counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED);
591 		} else {
592 			fprintf(config->output, "%*s%s",
593 				config->csv_output ? 0 : 18,
594 				counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
595 				config->csv_sep);
596 		}
597 
598 		if (counter->supported) {
599 			if (!evlist__has_hybrid(counter->evlist)) {
600 				config->print_free_counters_hint = 1;
601 				if (is_mixed_hw_group(counter))
602 					config->print_mixed_hw_group_error = 1;
603 			}
604 		}
605 
606 		if (config->json_output) {
607 			fprintf(config->output, "\"unit\" : \"%s\", ", counter->unit);
608 		} else {
609 			fprintf(config->output, "%-*s%s",
610 				config->csv_output ? 0 : config->unit_width,
611 				counter->unit, config->csv_sep);
612 		}
613 
614 		if (config->json_output) {
615 			fprintf(config->output, "\"event\" : \"%s\", ",
616 				evsel__name(counter));
617 		} else {
618 			fprintf(config->output, "%*s",
619 				 config->csv_output ? 0 : -25, evsel__name(counter));
620 		}
621 
622 		print_cgroup(config, counter);
623 
624 		if (!config->csv_output && !config->json_output)
625 			pm(config, &os, NULL, NULL, "", 0);
626 		print_noise(config, counter, noise);
627 		print_running(config, run, ena);
628 		if (config->csv_output)
629 			pm(config, &os, NULL, NULL, "", 0);
630 		else if (config->json_output)
631 			pm(config, &os, NULL, NULL, "", 0);
632 		return;
633 	}
634 
635 	if (!config->metric_only)
636 		abs_printout(config, id, nr, counter, uval);
637 
638 	out.print_metric = pm;
639 	out.new_line = nl;
640 	out.ctx = &os;
641 	out.force_header = false;
642 
643 	if (config->csv_output && !config->metric_only) {
644 		print_noise(config, counter, noise);
645 		print_running(config, run, ena);
646 	} else if (config->json_output && !config->metric_only) {
647 		print_noise(config, counter, noise);
648 		print_running(config, run, ena);
649 	}
650 
651 	perf_stat__print_shadow_stats(config, counter, uval,
652 				first_shadow_map_idx(config, counter, &id),
653 				&out, &config->metric_events, st);
654 	if (!config->csv_output && !config->metric_only && !config->json_output) {
655 		print_noise(config, counter, noise);
656 		print_running(config, run, ena);
657 	}
658 }
659 
660 static void aggr_update_shadow(struct perf_stat_config *config,
661 			       struct evlist *evlist)
662 {
663 	int idx, s;
664 	struct perf_cpu cpu;
665 	struct aggr_cpu_id s2, id;
666 	u64 val;
667 	struct evsel *counter;
668 	struct perf_cpu_map *cpus;
669 
670 	for (s = 0; s < config->aggr_map->nr; s++) {
671 		id = config->aggr_map->map[s];
672 		evlist__for_each_entry(evlist, counter) {
673 			cpus = evsel__cpus(counter);
674 			val = 0;
675 			perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
676 				s2 = config->aggr_get_id(config, cpu);
677 				if (!aggr_cpu_id__equal(&s2, &id))
678 					continue;
679 				val += perf_counts(counter->counts, idx, 0)->val;
680 			}
681 			perf_stat__update_shadow_stats(counter, val,
682 					first_shadow_map_idx(config, counter, &id),
683 					&rt_stat);
684 		}
685 	}
686 }
687 
688 static void uniquify_event_name(struct evsel *counter)
689 {
690 	char *new_name;
691 	char *config;
692 	int ret = 0;
693 
694 	if (counter->uniquified_name || counter->use_config_name ||
695 	    !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
696 					   strlen(counter->pmu_name)))
697 		return;
698 
699 	config = strchr(counter->name, '/');
700 	if (config) {
701 		if (asprintf(&new_name,
702 			     "%s%s", counter->pmu_name, config) > 0) {
703 			free(counter->name);
704 			counter->name = new_name;
705 		}
706 	} else {
707 		if (perf_pmu__has_hybrid()) {
708 			ret = asprintf(&new_name, "%s/%s/",
709 				       counter->pmu_name, counter->name);
710 		} else {
711 			ret = asprintf(&new_name, "%s [%s]",
712 				       counter->name, counter->pmu_name);
713 		}
714 
715 		if (ret) {
716 			free(counter->name);
717 			counter->name = new_name;
718 		}
719 	}
720 
721 	counter->uniquified_name = true;
722 }
723 
724 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
725 			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
726 				       bool first),
727 			    void *data)
728 {
729 	struct evlist *evlist = counter->evlist;
730 	struct evsel *alias;
731 
732 	alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
733 	list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
734 		/* Merge events with the same name, etc. but on different PMUs. */
735 		if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
736 			alias->scale == counter->scale &&
737 			alias->cgrp == counter->cgrp &&
738 			!strcmp(alias->unit, counter->unit) &&
739 			evsel__is_clock(alias) == evsel__is_clock(counter) &&
740 			strcmp(alias->pmu_name, counter->pmu_name)) {
741 			alias->merged_stat = true;
742 			cb(config, alias, data, false);
743 		}
744 	}
745 }
746 
747 static bool is_uncore(struct evsel *evsel)
748 {
749 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
750 
751 	return pmu && pmu->is_uncore;
752 }
753 
754 static bool hybrid_uniquify(struct evsel *evsel)
755 {
756 	return perf_pmu__has_hybrid() && !is_uncore(evsel);
757 }
758 
759 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
760 			 bool check)
761 {
762 	if (hybrid_uniquify(counter)) {
763 		if (check)
764 			return config && config->hybrid_merge;
765 		else
766 			return config && !config->hybrid_merge;
767 	}
768 
769 	return false;
770 }
771 
772 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
773 			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
774 				       bool first),
775 			    void *data)
776 {
777 	if (counter->merged_stat)
778 		return false;
779 	cb(config, counter, data, true);
780 	if (config->no_merge || hybrid_merge(counter, config, false))
781 		uniquify_event_name(counter);
782 	else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
783 		collect_all_aliases(config, counter, cb, data);
784 	return true;
785 }
786 
787 struct aggr_data {
788 	u64 ena, run, val;
789 	struct aggr_cpu_id id;
790 	int nr;
791 	int cpu_map_idx;
792 };
793 
794 static void aggr_cb(struct perf_stat_config *config,
795 		    struct evsel *counter, void *data, bool first)
796 {
797 	struct aggr_data *ad = data;
798 	int idx;
799 	struct perf_cpu cpu;
800 	struct perf_cpu_map *cpus;
801 	struct aggr_cpu_id s2;
802 
803 	cpus = evsel__cpus(counter);
804 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
805 		struct perf_counts_values *counts;
806 
807 		s2 = config->aggr_get_id(config, cpu);
808 		if (!aggr_cpu_id__equal(&s2, &ad->id))
809 			continue;
810 		if (first)
811 			ad->nr++;
812 		counts = perf_counts(counter->counts, idx, 0);
813 		/*
814 		 * When any result is bad, make them all to give
815 		 * consistent output in interval mode.
816 		 */
817 		if (counts->ena == 0 || counts->run == 0 ||
818 		    counter->counts->scaled == -1) {
819 			ad->ena = 0;
820 			ad->run = 0;
821 			break;
822 		}
823 		ad->val += counts->val;
824 		ad->ena += counts->ena;
825 		ad->run += counts->run;
826 	}
827 }
828 
829 static void print_counter_aggrdata(struct perf_stat_config *config,
830 				   struct evsel *counter, int s,
831 				   char *prefix, bool metric_only,
832 				   bool *first, struct perf_cpu cpu)
833 {
834 	struct aggr_data ad;
835 	FILE *output = config->output;
836 	u64 ena, run, val;
837 	int nr;
838 	struct aggr_cpu_id id;
839 	double uval;
840 
841 	ad.id = id = config->aggr_map->map[s];
842 	ad.val = ad.ena = ad.run = 0;
843 	ad.nr = 0;
844 	if (!collect_data(config, counter, aggr_cb, &ad))
845 		return;
846 
847 	if (perf_pmu__has_hybrid() && ad.ena == 0)
848 		return;
849 
850 	nr = ad.nr;
851 	ena = ad.ena;
852 	run = ad.run;
853 	val = ad.val;
854 	if (*first && metric_only) {
855 		*first = false;
856 		aggr_printout(config, counter, id, nr);
857 	}
858 	if (prefix && !metric_only)
859 		fprintf(output, "%s", prefix);
860 
861 	uval = val * counter->scale;
862 	if (cpu.cpu != -1)
863 		id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
864 
865 	printout(config, id, nr, counter, uval,
866 		 prefix, run, ena, 1.0, &rt_stat);
867 	if (!metric_only)
868 		fputc('\n', output);
869 }
870 
871 static void print_aggr(struct perf_stat_config *config,
872 		       struct evlist *evlist,
873 		       char *prefix)
874 {
875 	bool metric_only = config->metric_only;
876 	FILE *output = config->output;
877 	struct evsel *counter;
878 	int s;
879 	bool first;
880 
881 	if (!config->aggr_map || !config->aggr_get_id)
882 		return;
883 
884 	aggr_update_shadow(config, evlist);
885 
886 	/*
887 	 * With metric_only everything is on a single line.
888 	 * Without each counter has its own line.
889 	 */
890 	for (s = 0; s < config->aggr_map->nr; s++) {
891 		if (prefix && metric_only)
892 			fprintf(output, "%s", prefix);
893 
894 		first = true;
895 		evlist__for_each_entry(evlist, counter) {
896 			print_counter_aggrdata(config, counter, s,
897 					prefix, metric_only,
898 					&first, (struct perf_cpu){ .cpu = -1 });
899 		}
900 		if (metric_only)
901 			fputc('\n', output);
902 	}
903 }
904 
905 static int cmp_val(const void *a, const void *b)
906 {
907 	return ((struct perf_aggr_thread_value *)b)->val -
908 		((struct perf_aggr_thread_value *)a)->val;
909 }
910 
911 static struct perf_aggr_thread_value *sort_aggr_thread(
912 					struct evsel *counter,
913 					int *ret,
914 					struct target *_target)
915 {
916 	int nthreads = perf_thread_map__nr(counter->core.threads);
917 	int i = 0;
918 	double uval;
919 	struct perf_aggr_thread_value *buf;
920 
921 	buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
922 	if (!buf)
923 		return NULL;
924 
925 	for (int thread = 0; thread < nthreads; thread++) {
926 		int idx;
927 		u64 ena = 0, run = 0, val = 0;
928 
929 		perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
930 			struct perf_counts_values *counts =
931 				perf_counts(counter->counts, idx, thread);
932 
933 			val += counts->val;
934 			ena += counts->ena;
935 			run += counts->run;
936 		}
937 
938 		uval = val * counter->scale;
939 
940 		/*
941 		 * Skip value 0 when enabling --per-thread globally,
942 		 * otherwise too many 0 output.
943 		 */
944 		if (uval == 0.0 && target__has_per_thread(_target))
945 			continue;
946 
947 		buf[i].counter = counter;
948 		buf[i].id = aggr_cpu_id__empty();
949 		buf[i].id.thread_idx = thread;
950 		buf[i].uval = uval;
951 		buf[i].val = val;
952 		buf[i].run = run;
953 		buf[i].ena = ena;
954 		i++;
955 	}
956 
957 	qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
958 
959 	if (ret)
960 		*ret = i;
961 
962 	return buf;
963 }
964 
965 static void print_aggr_thread(struct perf_stat_config *config,
966 			      struct target *_target,
967 			      struct evsel *counter, char *prefix)
968 {
969 	FILE *output = config->output;
970 	int thread, sorted_threads;
971 	struct aggr_cpu_id id;
972 	struct perf_aggr_thread_value *buf;
973 
974 	buf = sort_aggr_thread(counter, &sorted_threads, _target);
975 	if (!buf) {
976 		perror("cannot sort aggr thread");
977 		return;
978 	}
979 
980 	for (thread = 0; thread < sorted_threads; thread++) {
981 		if (prefix)
982 			fprintf(output, "%s", prefix);
983 
984 		id = buf[thread].id;
985 		printout(config, id, 0, buf[thread].counter, buf[thread].uval,
986 			 prefix, buf[thread].run, buf[thread].ena, 1.0,
987 			 &rt_stat);
988 		fputc('\n', output);
989 	}
990 
991 	free(buf);
992 }
993 
994 struct caggr_data {
995 	double avg, avg_enabled, avg_running;
996 };
997 
998 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
999 			    struct evsel *counter, void *data,
1000 			    bool first __maybe_unused)
1001 {
1002 	struct caggr_data *cd = data;
1003 	struct perf_counts_values *aggr = &counter->counts->aggr;
1004 
1005 	cd->avg += aggr->val;
1006 	cd->avg_enabled += aggr->ena;
1007 	cd->avg_running += aggr->run;
1008 }
1009 
1010 /*
1011  * Print out the results of a single counter:
1012  * aggregated counts in system-wide mode
1013  */
1014 static void print_counter_aggr(struct perf_stat_config *config,
1015 			       struct evsel *counter, char *prefix)
1016 {
1017 	bool metric_only = config->metric_only;
1018 	FILE *output = config->output;
1019 	double uval;
1020 	struct caggr_data cd = { .avg = 0.0 };
1021 
1022 	if (!collect_data(config, counter, counter_aggr_cb, &cd))
1023 		return;
1024 
1025 	if (prefix && !metric_only)
1026 		fprintf(output, "%s", prefix);
1027 
1028 	uval = cd.avg * counter->scale;
1029 	printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
1030 		 cd.avg_enabled, cd.avg, &rt_stat);
1031 	if (!metric_only)
1032 		fprintf(output, "\n");
1033 }
1034 
1035 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1036 		       struct evsel *counter, void *data,
1037 		       bool first __maybe_unused)
1038 {
1039 	struct aggr_data *ad = data;
1040 
1041 	ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
1042 	ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
1043 	ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
1044 }
1045 
1046 /*
1047  * Print out the results of a single counter:
1048  * does not use aggregated count in system-wide
1049  */
1050 static void print_counter(struct perf_stat_config *config,
1051 			  struct evsel *counter, char *prefix)
1052 {
1053 	FILE *output = config->output;
1054 	u64 ena, run, val;
1055 	double uval;
1056 	int idx;
1057 	struct perf_cpu cpu;
1058 	struct aggr_cpu_id id;
1059 
1060 	perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
1061 		struct aggr_data ad = { .cpu_map_idx = idx };
1062 
1063 		if (!collect_data(config, counter, counter_cb, &ad))
1064 			return;
1065 		val = ad.val;
1066 		ena = ad.ena;
1067 		run = ad.run;
1068 
1069 		if (prefix)
1070 			fprintf(output, "%s", prefix);
1071 
1072 		uval = val * counter->scale;
1073 		id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1074 		printout(config, id, 0, counter, uval, prefix,
1075 			 run, ena, 1.0, &rt_stat);
1076 
1077 		fputc('\n', output);
1078 	}
1079 }
1080 
1081 static void print_no_aggr_metric(struct perf_stat_config *config,
1082 				 struct evlist *evlist,
1083 				 char *prefix)
1084 {
1085 	int all_idx;
1086 	struct perf_cpu cpu;
1087 
1088 	perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1089 		struct evsel *counter;
1090 		bool first = true;
1091 
1092 		evlist__for_each_entry(evlist, counter) {
1093 			u64 ena, run, val;
1094 			double uval;
1095 			struct aggr_cpu_id id;
1096 			int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1097 
1098 			if (counter_idx < 0)
1099 				continue;
1100 
1101 			id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1102 			if (first) {
1103 				if (prefix)
1104 					fputs(prefix, config->output);
1105 				aggr_printout(config, counter, id, 0);
1106 				first = false;
1107 			}
1108 			val = perf_counts(counter->counts, counter_idx, 0)->val;
1109 			ena = perf_counts(counter->counts, counter_idx, 0)->ena;
1110 			run = perf_counts(counter->counts, counter_idx, 0)->run;
1111 
1112 			uval = val * counter->scale;
1113 			printout(config, id, 0, counter, uval, prefix,
1114 				 run, ena, 1.0, &rt_stat);
1115 		}
1116 		if (!first)
1117 			fputc('\n', config->output);
1118 	}
1119 }
1120 
1121 static int aggr_header_lens[] = {
1122 	[AGGR_CORE] = 24,
1123 	[AGGR_DIE] = 18,
1124 	[AGGR_SOCKET] = 12,
1125 	[AGGR_NONE] = 6,
1126 	[AGGR_THREAD] = 24,
1127 	[AGGR_GLOBAL] = 0,
1128 };
1129 
1130 static const char *aggr_header_csv[] = {
1131 	[AGGR_CORE] 	= 	"core,cpus,",
1132 	[AGGR_DIE] 	= 	"die,cpus",
1133 	[AGGR_SOCKET] 	= 	"socket,cpus",
1134 	[AGGR_NONE] 	= 	"cpu,",
1135 	[AGGR_THREAD] 	= 	"comm-pid,",
1136 	[AGGR_GLOBAL] 	=	""
1137 };
1138 
1139 static void print_metric_headers(struct perf_stat_config *config,
1140 				 struct evlist *evlist,
1141 				 const char *prefix, bool no_indent)
1142 {
1143 	struct perf_stat_output_ctx out;
1144 	struct evsel *counter;
1145 	struct outstate os = {
1146 		.fh = config->output
1147 	};
1148 	bool first = true;
1149 
1150 		if (config->json_output && !config->interval)
1151 			fprintf(config->output, "{");
1152 
1153 	if (prefix && !config->json_output)
1154 		fprintf(config->output, "%s", prefix);
1155 
1156 	if (!config->csv_output && !no_indent)
1157 		fprintf(config->output, "%*s",
1158 			aggr_header_lens[config->aggr_mode], "");
1159 	if (config->csv_output) {
1160 		if (config->interval)
1161 			fputs("time,", config->output);
1162 		if (!config->iostat_run)
1163 			fputs(aggr_header_csv[config->aggr_mode], config->output);
1164 	}
1165 	if (config->iostat_run)
1166 		iostat_print_header_prefix(config);
1167 
1168 	/* Print metrics headers only */
1169 	evlist__for_each_entry(evlist, counter) {
1170 		os.evsel = counter;
1171 		out.ctx = &os;
1172 		out.print_metric = print_metric_header;
1173 		if (!first && config->json_output)
1174 			fprintf(config->output, ", ");
1175 		first = false;
1176 		out.new_line = new_line_metric;
1177 		out.force_header = true;
1178 		perf_stat__print_shadow_stats(config, counter, 0,
1179 					      0,
1180 					      &out,
1181 					      &config->metric_events,
1182 					      &rt_stat);
1183 	}
1184 	if (config->json_output)
1185 		fprintf(config->output, "}");
1186 	fputc('\n', config->output);
1187 }
1188 
1189 static void print_interval(struct perf_stat_config *config,
1190 			   struct evlist *evlist,
1191 			   char *prefix, struct timespec *ts)
1192 {
1193 	bool metric_only = config->metric_only;
1194 	unsigned int unit_width = config->unit_width;
1195 	FILE *output = config->output;
1196 	static int num_print_interval;
1197 
1198 	if (config->interval_clear)
1199 		puts(CONSOLE_CLEAR);
1200 
1201 	if (!config->iostat_run && !config->json_output)
1202 		sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1203 				 ts->tv_nsec, config->csv_sep);
1204 	if (!config->iostat_run && config->json_output && !config->metric_only)
1205 		sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1206 				 ts->tv_sec, ts->tv_nsec);
1207 	if (!config->iostat_run && config->json_output && config->metric_only)
1208 		sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1209 				 ts->tv_sec, ts->tv_nsec);
1210 
1211 	if ((num_print_interval == 0 && !config->csv_output && !config->json_output)
1212 			 || config->interval_clear) {
1213 		switch (config->aggr_mode) {
1214 		case AGGR_NODE:
1215 			fprintf(output, "#           time node   cpus");
1216 			if (!metric_only)
1217 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1218 			break;
1219 		case AGGR_SOCKET:
1220 			fprintf(output, "#           time socket cpus");
1221 			if (!metric_only)
1222 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1223 			break;
1224 		case AGGR_DIE:
1225 			fprintf(output, "#           time die          cpus");
1226 			if (!metric_only)
1227 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1228 			break;
1229 		case AGGR_CORE:
1230 			fprintf(output, "#           time core            cpus");
1231 			if (!metric_only)
1232 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1233 			break;
1234 		case AGGR_NONE:
1235 			fprintf(output, "#           time CPU    ");
1236 			if (!metric_only)
1237 				fprintf(output, "                counts %*s events\n", unit_width, "unit");
1238 			break;
1239 		case AGGR_THREAD:
1240 			fprintf(output, "#           time             comm-pid");
1241 			if (!metric_only)
1242 				fprintf(output, "                  counts %*s events\n", unit_width, "unit");
1243 			break;
1244 		case AGGR_GLOBAL:
1245 		default:
1246 			if (!config->iostat_run) {
1247 				fprintf(output, "#           time");
1248 				if (!metric_only)
1249 					fprintf(output, "             counts %*s events\n", unit_width, "unit");
1250 			}
1251 		case AGGR_UNSET:
1252 		case AGGR_MAX:
1253 			break;
1254 		}
1255 	}
1256 
1257 	if ((num_print_interval == 0 || config->interval_clear)
1258 			 && metric_only && !config->json_output)
1259 		print_metric_headers(config, evlist, " ", true);
1260 	if ((num_print_interval == 0 || config->interval_clear)
1261 			 && metric_only && config->json_output) {
1262 		fprintf(output, "{");
1263 		print_metric_headers(config, evlist, " ", true);
1264 	}
1265 	if (++num_print_interval == 25)
1266 		num_print_interval = 0;
1267 }
1268 
1269 static void print_header(struct perf_stat_config *config,
1270 			 struct target *_target,
1271 			 int argc, const char **argv)
1272 {
1273 	FILE *output = config->output;
1274 	int i;
1275 
1276 	fflush(stdout);
1277 
1278 	if (!config->csv_output && !config->json_output) {
1279 		fprintf(output, "\n");
1280 		fprintf(output, " Performance counter stats for ");
1281 		if (_target->bpf_str)
1282 			fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1283 		else if (_target->system_wide)
1284 			fprintf(output, "\'system wide");
1285 		else if (_target->cpu_list)
1286 			fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1287 		else if (!target__has_task(_target)) {
1288 			fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1289 			for (i = 1; argv && (i < argc); i++)
1290 				fprintf(output, " %s", argv[i]);
1291 		} else if (_target->pid)
1292 			fprintf(output, "process id \'%s", _target->pid);
1293 		else
1294 			fprintf(output, "thread id \'%s", _target->tid);
1295 
1296 		fprintf(output, "\'");
1297 		if (config->run_count > 1)
1298 			fprintf(output, " (%d runs)", config->run_count);
1299 		fprintf(output, ":\n\n");
1300 	}
1301 }
1302 
1303 static int get_precision(double num)
1304 {
1305 	if (num > 1)
1306 		return 0;
1307 
1308 	return lround(ceil(-log10(num)));
1309 }
1310 
1311 static void print_table(struct perf_stat_config *config,
1312 			FILE *output, int precision, double avg)
1313 {
1314 	char tmp[64];
1315 	int idx, indent = 0;
1316 
1317 	scnprintf(tmp, 64, " %17.*f", precision, avg);
1318 	while (tmp[indent] == ' ')
1319 		indent++;
1320 
1321 	fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1322 
1323 	for (idx = 0; idx < config->run_count; idx++) {
1324 		double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1325 		int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1326 
1327 		fprintf(output, " %17.*f (%+.*f) ",
1328 			precision, run, precision, run - avg);
1329 
1330 		for (h = 0; h < n; h++)
1331 			fprintf(output, "#");
1332 
1333 		fprintf(output, "\n");
1334 	}
1335 
1336 	fprintf(output, "\n%*s# Final result:\n", indent, "");
1337 }
1338 
1339 static double timeval2double(struct timeval *t)
1340 {
1341 	return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1342 }
1343 
1344 static void print_footer(struct perf_stat_config *config)
1345 {
1346 	double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1347 	FILE *output = config->output;
1348 
1349 	if (!config->null_run)
1350 		fprintf(output, "\n");
1351 
1352 	if (config->run_count == 1) {
1353 		fprintf(output, " %17.9f seconds time elapsed", avg);
1354 
1355 		if (config->ru_display) {
1356 			double ru_utime = timeval2double(&config->ru_data.ru_utime);
1357 			double ru_stime = timeval2double(&config->ru_data.ru_stime);
1358 
1359 			fprintf(output, "\n\n");
1360 			fprintf(output, " %17.9f seconds user\n", ru_utime);
1361 			fprintf(output, " %17.9f seconds sys\n", ru_stime);
1362 		}
1363 	} else {
1364 		double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1365 		/*
1366 		 * Display at most 2 more significant
1367 		 * digits than the stddev inaccuracy.
1368 		 */
1369 		int precision = get_precision(sd) + 2;
1370 
1371 		if (config->walltime_run_table)
1372 			print_table(config, output, precision, avg);
1373 
1374 		fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1375 			precision, avg, precision, sd);
1376 
1377 		print_noise_pct(config, sd, avg);
1378 	}
1379 	fprintf(output, "\n\n");
1380 
1381 	if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1382 		fprintf(output,
1383 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1384 "	echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1385 "	perf stat ...\n"
1386 "	echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1387 
1388 	if (config->print_mixed_hw_group_error)
1389 		fprintf(output,
1390 			"The events in group usually have to be from "
1391 			"the same PMU. Try reorganizing the group.\n");
1392 }
1393 
1394 static void print_percore_thread(struct perf_stat_config *config,
1395 				 struct evsel *counter, char *prefix)
1396 {
1397 	int s;
1398 	struct aggr_cpu_id s2, id;
1399 	struct perf_cpu_map *cpus;
1400 	bool first = true;
1401 	int idx;
1402 	struct perf_cpu cpu;
1403 
1404 	cpus = evsel__cpus(counter);
1405 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1406 		s2 = config->aggr_get_id(config, cpu);
1407 		for (s = 0; s < config->aggr_map->nr; s++) {
1408 			id = config->aggr_map->map[s];
1409 			if (aggr_cpu_id__equal(&s2, &id))
1410 				break;
1411 		}
1412 
1413 		print_counter_aggrdata(config, counter, s,
1414 				       prefix, false,
1415 				       &first, cpu);
1416 	}
1417 }
1418 
1419 static void print_percore(struct perf_stat_config *config,
1420 			  struct evsel *counter, char *prefix)
1421 {
1422 	bool metric_only = config->metric_only;
1423 	FILE *output = config->output;
1424 	int s;
1425 	bool first = true;
1426 
1427 	if (!config->aggr_map || !config->aggr_get_id)
1428 		return;
1429 
1430 	if (config->percore_show_thread)
1431 		return print_percore_thread(config, counter, prefix);
1432 
1433 	for (s = 0; s < config->aggr_map->nr; s++) {
1434 		if (prefix && metric_only)
1435 			fprintf(output, "%s", prefix);
1436 
1437 		print_counter_aggrdata(config, counter, s,
1438 				prefix, metric_only,
1439 				&first, (struct perf_cpu){ .cpu = -1 });
1440 	}
1441 
1442 	if (metric_only)
1443 		fputc('\n', output);
1444 }
1445 
1446 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1447 			    struct target *_target, struct timespec *ts, int argc, const char **argv)
1448 {
1449 	bool metric_only = config->metric_only;
1450 	int interval = config->interval;
1451 	struct evsel *counter;
1452 	char buf[64], *prefix = NULL;
1453 
1454 	if (config->iostat_run)
1455 		evlist->selected = evlist__first(evlist);
1456 
1457 	if (interval)
1458 		print_interval(config, evlist, prefix = buf, ts);
1459 	else
1460 		print_header(config, _target, argc, argv);
1461 
1462 	if (metric_only) {
1463 		static int num_print_iv;
1464 
1465 		if (num_print_iv == 0 && !interval)
1466 			print_metric_headers(config, evlist, prefix, false);
1467 		if (num_print_iv++ == 25)
1468 			num_print_iv = 0;
1469 		if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1470 			fprintf(config->output, "%s", prefix);
1471 
1472 		if (config->json_output && !config->metric_only)
1473 			fprintf(config->output, "}");
1474 	}
1475 
1476 	switch (config->aggr_mode) {
1477 	case AGGR_CORE:
1478 	case AGGR_DIE:
1479 	case AGGR_SOCKET:
1480 	case AGGR_NODE:
1481 		print_aggr(config, evlist, prefix);
1482 		break;
1483 	case AGGR_THREAD:
1484 		evlist__for_each_entry(evlist, counter) {
1485 			print_aggr_thread(config, _target, counter, prefix);
1486 		}
1487 		break;
1488 	case AGGR_GLOBAL:
1489 		if (config->iostat_run)
1490 			iostat_print_counters(evlist, config, ts, prefix = buf,
1491 					      print_counter_aggr);
1492 		else {
1493 			evlist__for_each_entry(evlist, counter) {
1494 				print_counter_aggr(config, counter, prefix);
1495 			}
1496 			if (metric_only)
1497 				fputc('\n', config->output);
1498 		}
1499 		break;
1500 	case AGGR_NONE:
1501 		if (metric_only)
1502 			print_no_aggr_metric(config, evlist, prefix);
1503 		else {
1504 			evlist__for_each_entry(evlist, counter) {
1505 				if (counter->percore)
1506 					print_percore(config, counter, prefix);
1507 				else
1508 					print_counter(config, counter, prefix);
1509 			}
1510 		}
1511 		break;
1512 	case AGGR_MAX:
1513 	case AGGR_UNSET:
1514 	default:
1515 		break;
1516 	}
1517 
1518 	if (!interval && !config->csv_output && !config->json_output)
1519 		print_footer(config);
1520 
1521 	fflush(config->output);
1522 }
1523