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