xref: /openbmc/linux/tools/perf/util/stat-display.c (revision 354575c00d61c174e0ff070f56cf3cdbe6d23f9e)
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", perf_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 < perf_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, perf_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,
465 			perf_evsel__name(counter));
466 
467 		print_cgroup(config, counter);
468 
469 		if (!config->csv_output)
470 			pm(config, &os, NULL, NULL, "", 0);
471 		print_noise(config, counter, noise);
472 		print_running(config, run, ena);
473 		if (config->csv_output)
474 			pm(config, &os, NULL, NULL, "", 0);
475 		return;
476 	}
477 
478 	if (!config->metric_only)
479 		abs_printout(config, id, nr, counter, uval);
480 
481 	out.print_metric = pm;
482 	out.new_line = nl;
483 	out.ctx = &os;
484 	out.force_header = false;
485 
486 	if (config->csv_output && !config->metric_only) {
487 		print_noise(config, counter, noise);
488 		print_running(config, run, ena);
489 	}
490 
491 	perf_stat__print_shadow_stats(config, counter, uval,
492 				first_shadow_cpu(config, counter, id),
493 				&out, &config->metric_events, st);
494 	if (!config->csv_output && !config->metric_only) {
495 		print_noise(config, counter, noise);
496 		print_running(config, run, ena);
497 	}
498 }
499 
500 static void aggr_update_shadow(struct perf_stat_config *config,
501 			       struct evlist *evlist)
502 {
503 	int cpu, s2, id, s;
504 	u64 val;
505 	struct evsel *counter;
506 
507 	for (s = 0; s < config->aggr_map->nr; s++) {
508 		id = config->aggr_map->map[s];
509 		evlist__for_each_entry(evlist, counter) {
510 			val = 0;
511 			for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
512 				s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
513 				if (s2 != id)
514 					continue;
515 				val += perf_counts(counter->counts, cpu, 0)->val;
516 			}
517 			perf_stat__update_shadow_stats(counter, val,
518 					first_shadow_cpu(config, counter, id),
519 					&rt_stat);
520 		}
521 	}
522 }
523 
524 static void uniquify_event_name(struct evsel *counter)
525 {
526 	char *new_name;
527 	char *config;
528 
529 	if (counter->uniquified_name ||
530 	    !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
531 					   strlen(counter->pmu_name)))
532 		return;
533 
534 	config = strchr(counter->name, '/');
535 	if (config) {
536 		if (asprintf(&new_name,
537 			     "%s%s", counter->pmu_name, config) > 0) {
538 			free(counter->name);
539 			counter->name = new_name;
540 		}
541 	} else {
542 		if (asprintf(&new_name,
543 			     "%s [%s]", counter->name, counter->pmu_name) > 0) {
544 			free(counter->name);
545 			counter->name = new_name;
546 		}
547 	}
548 
549 	counter->uniquified_name = true;
550 }
551 
552 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
553 			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
554 				       bool first),
555 			    void *data)
556 {
557 	struct evlist *evlist = counter->evlist;
558 	struct evsel *alias;
559 
560 	alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
561 	list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
562 		if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
563 		    alias->scale != counter->scale ||
564 		    alias->cgrp != counter->cgrp ||
565 		    strcmp(alias->unit, counter->unit) ||
566 		    perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter) ||
567 		    !strcmp(alias->pmu_name, counter->pmu_name))
568 			break;
569 		alias->merged_stat = true;
570 		cb(config, alias, data, false);
571 	}
572 }
573 
574 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
575 			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
576 				       bool first),
577 			    void *data)
578 {
579 	if (counter->merged_stat)
580 		return false;
581 	cb(config, counter, data, true);
582 	if (config->no_merge)
583 		uniquify_event_name(counter);
584 	else if (counter->auto_merge_stats)
585 		collect_all_aliases(config, counter, cb, data);
586 	return true;
587 }
588 
589 struct aggr_data {
590 	u64 ena, run, val;
591 	int id;
592 	int nr;
593 	int cpu;
594 };
595 
596 static void aggr_cb(struct perf_stat_config *config,
597 		    struct evsel *counter, void *data, bool first)
598 {
599 	struct aggr_data *ad = data;
600 	int cpu, s2;
601 
602 	for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
603 		struct perf_counts_values *counts;
604 
605 		s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
606 		if (s2 != ad->id)
607 			continue;
608 		if (first)
609 			ad->nr++;
610 		counts = perf_counts(counter->counts, cpu, 0);
611 		/*
612 		 * When any result is bad, make them all to give
613 		 * consistent output in interval mode.
614 		 */
615 		if (counts->ena == 0 || counts->run == 0 ||
616 		    counter->counts->scaled == -1) {
617 			ad->ena = 0;
618 			ad->run = 0;
619 			break;
620 		}
621 		ad->val += counts->val;
622 		ad->ena += counts->ena;
623 		ad->run += counts->run;
624 	}
625 }
626 
627 static void print_counter_aggrdata(struct perf_stat_config *config,
628 				   struct evsel *counter, int s,
629 				   char *prefix, bool metric_only,
630 				   bool *first, int cpu)
631 {
632 	struct aggr_data ad;
633 	FILE *output = config->output;
634 	u64 ena, run, val;
635 	int id, nr;
636 	double uval;
637 
638 	ad.id = id = config->aggr_map->map[s];
639 	ad.val = ad.ena = ad.run = 0;
640 	ad.nr = 0;
641 	if (!collect_data(config, counter, aggr_cb, &ad))
642 		return;
643 
644 	nr = ad.nr;
645 	ena = ad.ena;
646 	run = ad.run;
647 	val = ad.val;
648 	if (*first && metric_only) {
649 		*first = false;
650 		aggr_printout(config, counter, id, nr);
651 	}
652 	if (prefix && !metric_only)
653 		fprintf(output, "%s", prefix);
654 
655 	uval = val * counter->scale;
656 	printout(config, cpu != -1 ? cpu : id, nr, counter, uval, prefix,
657 		 run, ena, 1.0, &rt_stat);
658 	if (!metric_only)
659 		fputc('\n', output);
660 }
661 
662 static void print_aggr(struct perf_stat_config *config,
663 		       struct evlist *evlist,
664 		       char *prefix)
665 {
666 	bool metric_only = config->metric_only;
667 	FILE *output = config->output;
668 	struct evsel *counter;
669 	int s;
670 	bool first;
671 
672 	if (!(config->aggr_map || config->aggr_get_id))
673 		return;
674 
675 	aggr_update_shadow(config, evlist);
676 
677 	/*
678 	 * With metric_only everything is on a single line.
679 	 * Without each counter has its own line.
680 	 */
681 	for (s = 0; s < config->aggr_map->nr; s++) {
682 		if (prefix && metric_only)
683 			fprintf(output, "%s", prefix);
684 
685 		first = true;
686 		evlist__for_each_entry(evlist, counter) {
687 			print_counter_aggrdata(config, counter, s,
688 					       prefix, metric_only,
689 					       &first, -1);
690 		}
691 		if (metric_only)
692 			fputc('\n', output);
693 	}
694 }
695 
696 static int cmp_val(const void *a, const void *b)
697 {
698 	return ((struct perf_aggr_thread_value *)b)->val -
699 		((struct perf_aggr_thread_value *)a)->val;
700 }
701 
702 static struct perf_aggr_thread_value *sort_aggr_thread(
703 					struct evsel *counter,
704 					int nthreads, int ncpus,
705 					int *ret,
706 					struct target *_target)
707 {
708 	int cpu, thread, i = 0;
709 	double uval;
710 	struct perf_aggr_thread_value *buf;
711 
712 	buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
713 	if (!buf)
714 		return NULL;
715 
716 	for (thread = 0; thread < nthreads; thread++) {
717 		u64 ena = 0, run = 0, val = 0;
718 
719 		for (cpu = 0; cpu < ncpus; cpu++) {
720 			val += perf_counts(counter->counts, cpu, thread)->val;
721 			ena += perf_counts(counter->counts, cpu, thread)->ena;
722 			run += perf_counts(counter->counts, cpu, thread)->run;
723 		}
724 
725 		uval = val * counter->scale;
726 
727 		/*
728 		 * Skip value 0 when enabling --per-thread globally,
729 		 * otherwise too many 0 output.
730 		 */
731 		if (uval == 0.0 && target__has_per_thread(_target))
732 			continue;
733 
734 		buf[i].counter = counter;
735 		buf[i].id = thread;
736 		buf[i].uval = uval;
737 		buf[i].val = val;
738 		buf[i].run = run;
739 		buf[i].ena = ena;
740 		i++;
741 	}
742 
743 	qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
744 
745 	if (ret)
746 		*ret = i;
747 
748 	return buf;
749 }
750 
751 static void print_aggr_thread(struct perf_stat_config *config,
752 			      struct target *_target,
753 			      struct evsel *counter, char *prefix)
754 {
755 	FILE *output = config->output;
756 	int nthreads = perf_thread_map__nr(counter->core.threads);
757 	int ncpus = perf_cpu_map__nr(counter->core.cpus);
758 	int thread, sorted_threads, id;
759 	struct perf_aggr_thread_value *buf;
760 
761 	buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
762 	if (!buf) {
763 		perror("cannot sort aggr thread");
764 		return;
765 	}
766 
767 	for (thread = 0; thread < sorted_threads; thread++) {
768 		if (prefix)
769 			fprintf(output, "%s", prefix);
770 
771 		id = buf[thread].id;
772 		if (config->stats)
773 			printout(config, id, 0, buf[thread].counter, buf[thread].uval,
774 				 prefix, buf[thread].run, buf[thread].ena, 1.0,
775 				 &config->stats[id]);
776 		else
777 			printout(config, id, 0, buf[thread].counter, buf[thread].uval,
778 				 prefix, buf[thread].run, buf[thread].ena, 1.0,
779 				 &rt_stat);
780 		fputc('\n', output);
781 	}
782 
783 	free(buf);
784 }
785 
786 struct caggr_data {
787 	double avg, avg_enabled, avg_running;
788 };
789 
790 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
791 			    struct evsel *counter, void *data,
792 			    bool first __maybe_unused)
793 {
794 	struct caggr_data *cd = data;
795 	struct perf_stat_evsel *ps = counter->stats;
796 
797 	cd->avg += avg_stats(&ps->res_stats[0]);
798 	cd->avg_enabled += avg_stats(&ps->res_stats[1]);
799 	cd->avg_running += avg_stats(&ps->res_stats[2]);
800 }
801 
802 /*
803  * Print out the results of a single counter:
804  * aggregated counts in system-wide mode
805  */
806 static void print_counter_aggr(struct perf_stat_config *config,
807 			       struct evsel *counter, char *prefix)
808 {
809 	bool metric_only = config->metric_only;
810 	FILE *output = config->output;
811 	double uval;
812 	struct caggr_data cd = { .avg = 0.0 };
813 
814 	if (!collect_data(config, counter, counter_aggr_cb, &cd))
815 		return;
816 
817 	if (prefix && !metric_only)
818 		fprintf(output, "%s", prefix);
819 
820 	uval = cd.avg * counter->scale;
821 	printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
822 		 cd.avg, &rt_stat);
823 	if (!metric_only)
824 		fprintf(output, "\n");
825 }
826 
827 static void counter_cb(struct perf_stat_config *config __maybe_unused,
828 		       struct evsel *counter, void *data,
829 		       bool first __maybe_unused)
830 {
831 	struct aggr_data *ad = data;
832 
833 	ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
834 	ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
835 	ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
836 }
837 
838 /*
839  * Print out the results of a single counter:
840  * does not use aggregated count in system-wide
841  */
842 static void print_counter(struct perf_stat_config *config,
843 			  struct evsel *counter, char *prefix)
844 {
845 	FILE *output = config->output;
846 	u64 ena, run, val;
847 	double uval;
848 	int cpu;
849 
850 	for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
851 		struct aggr_data ad = { .cpu = cpu };
852 
853 		if (!collect_data(config, counter, counter_cb, &ad))
854 			return;
855 		val = ad.val;
856 		ena = ad.ena;
857 		run = ad.run;
858 
859 		if (prefix)
860 			fprintf(output, "%s", prefix);
861 
862 		uval = val * counter->scale;
863 		printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
864 			 &rt_stat);
865 
866 		fputc('\n', output);
867 	}
868 }
869 
870 static void print_no_aggr_metric(struct perf_stat_config *config,
871 				 struct evlist *evlist,
872 				 char *prefix)
873 {
874 	int cpu;
875 	int nrcpus = 0;
876 	struct evsel *counter;
877 	u64 ena, run, val;
878 	double uval;
879 
880 	nrcpus = evlist->core.cpus->nr;
881 	for (cpu = 0; cpu < nrcpus; cpu++) {
882 		bool first = true;
883 
884 		if (prefix)
885 			fputs(prefix, config->output);
886 		evlist__for_each_entry(evlist, counter) {
887 			if (first) {
888 				aggr_printout(config, counter, cpu, 0);
889 				first = false;
890 			}
891 			val = perf_counts(counter->counts, cpu, 0)->val;
892 			ena = perf_counts(counter->counts, cpu, 0)->ena;
893 			run = perf_counts(counter->counts, cpu, 0)->run;
894 
895 			uval = val * counter->scale;
896 			printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
897 				 &rt_stat);
898 		}
899 		fputc('\n', config->output);
900 	}
901 }
902 
903 static int aggr_header_lens[] = {
904 	[AGGR_CORE] = 24,
905 	[AGGR_DIE] = 18,
906 	[AGGR_SOCKET] = 12,
907 	[AGGR_NONE] = 6,
908 	[AGGR_THREAD] = 24,
909 	[AGGR_GLOBAL] = 0,
910 };
911 
912 static const char *aggr_header_csv[] = {
913 	[AGGR_CORE] 	= 	"core,cpus,",
914 	[AGGR_DIE] 	= 	"die,cpus",
915 	[AGGR_SOCKET] 	= 	"socket,cpus",
916 	[AGGR_NONE] 	= 	"cpu,",
917 	[AGGR_THREAD] 	= 	"comm-pid,",
918 	[AGGR_GLOBAL] 	=	""
919 };
920 
921 static void print_metric_headers(struct perf_stat_config *config,
922 				 struct evlist *evlist,
923 				 const char *prefix, bool no_indent)
924 {
925 	struct perf_stat_output_ctx out;
926 	struct evsel *counter;
927 	struct outstate os = {
928 		.fh = config->output
929 	};
930 
931 	if (prefix)
932 		fprintf(config->output, "%s", prefix);
933 
934 	if (!config->csv_output && !no_indent)
935 		fprintf(config->output, "%*s",
936 			aggr_header_lens[config->aggr_mode], "");
937 	if (config->csv_output) {
938 		if (config->interval)
939 			fputs("time,", config->output);
940 		fputs(aggr_header_csv[config->aggr_mode], config->output);
941 	}
942 
943 	/* Print metrics headers only */
944 	evlist__for_each_entry(evlist, counter) {
945 		os.evsel = counter;
946 		out.ctx = &os;
947 		out.print_metric = print_metric_header;
948 		out.new_line = new_line_metric;
949 		out.force_header = true;
950 		os.evsel = counter;
951 		perf_stat__print_shadow_stats(config, counter, 0,
952 					      0,
953 					      &out,
954 					      &config->metric_events,
955 					      &rt_stat);
956 	}
957 	fputc('\n', config->output);
958 }
959 
960 static void print_interval(struct perf_stat_config *config,
961 			   struct evlist *evlist,
962 			   char *prefix, struct timespec *ts)
963 {
964 	bool metric_only = config->metric_only;
965 	unsigned int unit_width = config->unit_width;
966 	FILE *output = config->output;
967 	static int num_print_interval;
968 
969 	if (config->interval_clear)
970 		puts(CONSOLE_CLEAR);
971 
972 	sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
973 
974 	if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
975 		switch (config->aggr_mode) {
976 		case AGGR_NODE:
977 			fprintf(output, "#           time node   cpus");
978 			if (!metric_only)
979 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
980 			break;
981 		case AGGR_SOCKET:
982 			fprintf(output, "#           time socket cpus");
983 			if (!metric_only)
984 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
985 			break;
986 		case AGGR_DIE:
987 			fprintf(output, "#           time die          cpus");
988 			if (!metric_only)
989 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
990 			break;
991 		case AGGR_CORE:
992 			fprintf(output, "#           time core            cpus");
993 			if (!metric_only)
994 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
995 			break;
996 		case AGGR_NONE:
997 			fprintf(output, "#           time CPU    ");
998 			if (!metric_only)
999 				fprintf(output, "                counts %*s events\n", unit_width, "unit");
1000 			break;
1001 		case AGGR_THREAD:
1002 			fprintf(output, "#           time             comm-pid");
1003 			if (!metric_only)
1004 				fprintf(output, "                  counts %*s events\n", unit_width, "unit");
1005 			break;
1006 		case AGGR_GLOBAL:
1007 		default:
1008 			fprintf(output, "#           time");
1009 			if (!metric_only)
1010 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1011 		case AGGR_UNSET:
1012 			break;
1013 		}
1014 	}
1015 
1016 	if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1017 		print_metric_headers(config, evlist, " ", true);
1018 	if (++num_print_interval == 25)
1019 		num_print_interval = 0;
1020 }
1021 
1022 static void print_header(struct perf_stat_config *config,
1023 			 struct target *_target,
1024 			 int argc, const char **argv)
1025 {
1026 	FILE *output = config->output;
1027 	int i;
1028 
1029 	fflush(stdout);
1030 
1031 	if (!config->csv_output) {
1032 		fprintf(output, "\n");
1033 		fprintf(output, " Performance counter stats for ");
1034 		if (_target->system_wide)
1035 			fprintf(output, "\'system wide");
1036 		else if (_target->cpu_list)
1037 			fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1038 		else if (!target__has_task(_target)) {
1039 			fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1040 			for (i = 1; argv && (i < argc); i++)
1041 				fprintf(output, " %s", argv[i]);
1042 		} else if (_target->pid)
1043 			fprintf(output, "process id \'%s", _target->pid);
1044 		else
1045 			fprintf(output, "thread id \'%s", _target->tid);
1046 
1047 		fprintf(output, "\'");
1048 		if (config->run_count > 1)
1049 			fprintf(output, " (%d runs)", config->run_count);
1050 		fprintf(output, ":\n\n");
1051 	}
1052 }
1053 
1054 static int get_precision(double num)
1055 {
1056 	if (num > 1)
1057 		return 0;
1058 
1059 	return lround(ceil(-log10(num)));
1060 }
1061 
1062 static void print_table(struct perf_stat_config *config,
1063 			FILE *output, int precision, double avg)
1064 {
1065 	char tmp[64];
1066 	int idx, indent = 0;
1067 
1068 	scnprintf(tmp, 64, " %17.*f", precision, avg);
1069 	while (tmp[indent] == ' ')
1070 		indent++;
1071 
1072 	fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1073 
1074 	for (idx = 0; idx < config->run_count; idx++) {
1075 		double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1076 		int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1077 
1078 		fprintf(output, " %17.*f (%+.*f) ",
1079 			precision, run, precision, run - avg);
1080 
1081 		for (h = 0; h < n; h++)
1082 			fprintf(output, "#");
1083 
1084 		fprintf(output, "\n");
1085 	}
1086 
1087 	fprintf(output, "\n%*s# Final result:\n", indent, "");
1088 }
1089 
1090 static double timeval2double(struct timeval *t)
1091 {
1092 	return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1093 }
1094 
1095 static void print_footer(struct perf_stat_config *config)
1096 {
1097 	double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1098 	FILE *output = config->output;
1099 
1100 	if (!config->null_run)
1101 		fprintf(output, "\n");
1102 
1103 	if (config->run_count == 1) {
1104 		fprintf(output, " %17.9f seconds time elapsed", avg);
1105 
1106 		if (config->ru_display) {
1107 			double ru_utime = timeval2double(&config->ru_data.ru_utime);
1108 			double ru_stime = timeval2double(&config->ru_data.ru_stime);
1109 
1110 			fprintf(output, "\n\n");
1111 			fprintf(output, " %17.9f seconds user\n", ru_utime);
1112 			fprintf(output, " %17.9f seconds sys\n", ru_stime);
1113 		}
1114 	} else {
1115 		double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1116 		/*
1117 		 * Display at most 2 more significant
1118 		 * digits than the stddev inaccuracy.
1119 		 */
1120 		int precision = get_precision(sd) + 2;
1121 
1122 		if (config->walltime_run_table)
1123 			print_table(config, output, precision, avg);
1124 
1125 		fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1126 			precision, avg, precision, sd);
1127 
1128 		print_noise_pct(config, sd, avg);
1129 	}
1130 	fprintf(output, "\n\n");
1131 
1132 	if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1133 		fprintf(output,
1134 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1135 "	echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1136 "	perf stat ...\n"
1137 "	echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1138 
1139 	if (config->print_mixed_hw_group_error)
1140 		fprintf(output,
1141 			"The events in group usually have to be from "
1142 			"the same PMU. Try reorganizing the group.\n");
1143 }
1144 
1145 static void print_percore_thread(struct perf_stat_config *config,
1146 				 struct evsel *counter, char *prefix)
1147 {
1148 	int s, s2, id;
1149 	bool first = true;
1150 
1151 	for (int i = 0; i < perf_evsel__nr_cpus(counter); i++) {
1152 		s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1153 		for (s = 0; s < config->aggr_map->nr; s++) {
1154 			id = config->aggr_map->map[s];
1155 			if (s2 == id)
1156 				break;
1157 		}
1158 
1159 		print_counter_aggrdata(config, counter, s,
1160 				       prefix, false,
1161 				       &first, i);
1162 	}
1163 }
1164 
1165 static void print_percore(struct perf_stat_config *config,
1166 			  struct evsel *counter, char *prefix)
1167 {
1168 	bool metric_only = config->metric_only;
1169 	FILE *output = config->output;
1170 	int s;
1171 	bool first = true;
1172 
1173 	if (!(config->aggr_map || config->aggr_get_id))
1174 		return;
1175 
1176 	if (config->percore_show_thread)
1177 		return print_percore_thread(config, counter, prefix);
1178 
1179 	for (s = 0; s < config->aggr_map->nr; s++) {
1180 		if (prefix && metric_only)
1181 			fprintf(output, "%s", prefix);
1182 
1183 		print_counter_aggrdata(config, counter, s,
1184 				       prefix, metric_only,
1185 				       &first, -1);
1186 	}
1187 
1188 	if (metric_only)
1189 		fputc('\n', output);
1190 }
1191 
1192 void
1193 perf_evlist__print_counters(struct evlist *evlist,
1194 			    struct perf_stat_config *config,
1195 			    struct target *_target,
1196 			    struct timespec *ts,
1197 			    int argc, const char **argv)
1198 {
1199 	bool metric_only = config->metric_only;
1200 	int interval = config->interval;
1201 	struct evsel *counter;
1202 	char buf[64], *prefix = NULL;
1203 
1204 	if (interval)
1205 		print_interval(config, evlist, prefix = buf, ts);
1206 	else
1207 		print_header(config, _target, argc, argv);
1208 
1209 	if (metric_only) {
1210 		static int num_print_iv;
1211 
1212 		if (num_print_iv == 0 && !interval)
1213 			print_metric_headers(config, evlist, prefix, false);
1214 		if (num_print_iv++ == 25)
1215 			num_print_iv = 0;
1216 		if (config->aggr_mode == AGGR_GLOBAL && prefix)
1217 			fprintf(config->output, "%s", prefix);
1218 	}
1219 
1220 	switch (config->aggr_mode) {
1221 	case AGGR_CORE:
1222 	case AGGR_DIE:
1223 	case AGGR_SOCKET:
1224 	case AGGR_NODE:
1225 		print_aggr(config, evlist, prefix);
1226 		break;
1227 	case AGGR_THREAD:
1228 		evlist__for_each_entry(evlist, counter) {
1229 			print_aggr_thread(config, _target, counter, prefix);
1230 		}
1231 		break;
1232 	case AGGR_GLOBAL:
1233 		evlist__for_each_entry(evlist, counter) {
1234 			print_counter_aggr(config, counter, prefix);
1235 		}
1236 		if (metric_only)
1237 			fputc('\n', config->output);
1238 		break;
1239 	case AGGR_NONE:
1240 		if (metric_only)
1241 			print_no_aggr_metric(config, evlist, prefix);
1242 		else {
1243 			evlist__for_each_entry(evlist, counter) {
1244 				if (counter->percore)
1245 					print_percore(config, counter, prefix);
1246 				else
1247 					print_counter(config, counter, prefix);
1248 			}
1249 		}
1250 		break;
1251 	case AGGR_UNSET:
1252 	default:
1253 		break;
1254 	}
1255 
1256 	if (!interval && !config->csv_output)
1257 		print_footer(config);
1258 
1259 	fflush(config->output);
1260 }
1261