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