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