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