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