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