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