Loading...
Note: File does not exist in v4.6.
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 "debug.h"
11#include "evlist.h"
12#include "evsel.h"
13#include "stat.h"
14#include "top.h"
15#include "thread_map.h"
16#include "cpumap.h"
17#include "string2.h"
18#include <linux/ctype.h>
19#include "cgroup.h"
20#include <api/fs/fs.h>
21#include "util.h"
22#include "iostat.h"
23#include "pmu.h"
24#include "pmus.h"
25#include "tool_pmu.h"
26
27#define CNTR_NOT_SUPPORTED "<not supported>"
28#define CNTR_NOT_COUNTED "<not counted>"
29
30#define MGROUP_LEN 50
31#define METRIC_LEN 38
32#define EVNAME_LEN 32
33#define COUNTS_LEN 18
34#define INTERVAL_LEN 16
35#define CGROUP_LEN 16
36#define COMM_LEN 16
37#define PID_LEN 7
38#define CPUS_LEN 4
39
40static int aggr_header_lens[] = {
41 [AGGR_CORE] = 18,
42 [AGGR_CACHE] = 22,
43 [AGGR_CLUSTER] = 20,
44 [AGGR_DIE] = 12,
45 [AGGR_SOCKET] = 6,
46 [AGGR_NODE] = 6,
47 [AGGR_NONE] = 6,
48 [AGGR_THREAD] = 16,
49 [AGGR_GLOBAL] = 0,
50};
51
52static const char *aggr_header_csv[] = {
53 [AGGR_CORE] = "core,cpus,",
54 [AGGR_CACHE] = "cache,cpus,",
55 [AGGR_CLUSTER] = "cluster,cpus,",
56 [AGGR_DIE] = "die,cpus,",
57 [AGGR_SOCKET] = "socket,cpus,",
58 [AGGR_NONE] = "cpu,",
59 [AGGR_THREAD] = "comm-pid,",
60 [AGGR_NODE] = "node,",
61 [AGGR_GLOBAL] = ""
62};
63
64static const char *aggr_header_std[] = {
65 [AGGR_CORE] = "core",
66 [AGGR_CACHE] = "cache",
67 [AGGR_CLUSTER] = "cluster",
68 [AGGR_DIE] = "die",
69 [AGGR_SOCKET] = "socket",
70 [AGGR_NONE] = "cpu",
71 [AGGR_THREAD] = "comm-pid",
72 [AGGR_NODE] = "node",
73 [AGGR_GLOBAL] = ""
74};
75
76const char *metric_threshold_classify__color(enum metric_threshold_classify thresh)
77{
78 const char * const colors[] = {
79 "", /* unknown */
80 PERF_COLOR_RED, /* bad */
81 PERF_COLOR_MAGENTA, /* nearly bad */
82 PERF_COLOR_YELLOW, /* less good */
83 PERF_COLOR_GREEN, /* good */
84 };
85 static_assert(ARRAY_SIZE(colors) - 1 == METRIC_THRESHOLD_GOOD, "missing enum value");
86 return colors[thresh];
87}
88
89static const char *metric_threshold_classify__str(enum metric_threshold_classify thresh)
90{
91 const char * const strs[] = {
92 "unknown",
93 "bad",
94 "nearly bad",
95 "less good",
96 "good",
97 };
98 static_assert(ARRAY_SIZE(strs) - 1 == METRIC_THRESHOLD_GOOD, "missing enum value");
99 return strs[thresh];
100}
101
102static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
103{
104 if (run != ena)
105 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
106}
107
108static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
109{
110 double enabled_percent = 100;
111
112 if (run != ena)
113 enabled_percent = 100 * run / ena;
114 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
115 config->csv_sep, run, config->csv_sep, enabled_percent);
116}
117struct outstate {
118 FILE *fh;
119 bool newline;
120 bool first;
121 const char *prefix;
122 int nfields;
123 int aggr_nr;
124 struct aggr_cpu_id id;
125 struct evsel *evsel;
126 struct cgroup *cgrp;
127};
128
129static const char *json_sep(struct outstate *os)
130{
131 const char *sep = os->first ? "" : ", ";
132
133 os->first = false;
134 return sep;
135}
136
137#define json_out(os, format, ...) fprintf((os)->fh, "%s" format, json_sep(os), ##__VA_ARGS__)
138
139static void print_running_json(struct outstate *os, u64 run, u64 ena)
140{
141 double enabled_percent = 100;
142
143 if (run != ena)
144 enabled_percent = 100 * run / ena;
145 json_out(os, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f",
146 run, enabled_percent);
147}
148
149static void print_running(struct perf_stat_config *config, struct outstate *os,
150 u64 run, u64 ena, bool before_metric)
151{
152 if (config->json_output) {
153 if (before_metric)
154 print_running_json(os, run, ena);
155 } else if (config->csv_output) {
156 if (before_metric)
157 print_running_csv(config, run, ena);
158 } else {
159 if (!before_metric)
160 print_running_std(config, run, ena);
161 }
162}
163
164static void print_noise_pct_std(struct perf_stat_config *config,
165 double pct)
166{
167 if (pct)
168 fprintf(config->output, " ( +-%6.2f%% )", pct);
169}
170
171static void print_noise_pct_csv(struct perf_stat_config *config,
172 double pct)
173{
174 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
175}
176
177static void print_noise_pct_json(struct outstate *os,
178 double pct)
179{
180 json_out(os, "\"variance\" : %.2f", pct);
181}
182
183static void print_noise_pct(struct perf_stat_config *config, struct outstate *os,
184 double total, double avg, bool before_metric)
185{
186 double pct = rel_stddev_stats(total, avg);
187
188 if (config->json_output) {
189 if (before_metric)
190 print_noise_pct_json(os, pct);
191 } else if (config->csv_output) {
192 if (before_metric)
193 print_noise_pct_csv(config, pct);
194 } else {
195 if (!before_metric)
196 print_noise_pct_std(config, pct);
197 }
198}
199
200static void print_noise(struct perf_stat_config *config, struct outstate *os,
201 struct evsel *evsel, double avg, bool before_metric)
202{
203 struct perf_stat_evsel *ps;
204
205 if (config->run_count == 1)
206 return;
207
208 ps = evsel->stats;
209 print_noise_pct(config, os, stddev_stats(&ps->res_stats), avg, before_metric);
210}
211
212static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
213{
214 fprintf(config->output, " %-*s", CGROUP_LEN, cgrp_name);
215}
216
217static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
218{
219 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
220}
221
222static void print_cgroup_json(struct outstate *os, const char *cgrp_name)
223{
224 json_out(os, "\"cgroup\" : \"%s\"", cgrp_name);
225}
226
227static void print_cgroup(struct perf_stat_config *config, struct outstate *os,
228 struct cgroup *cgrp)
229{
230 if (nr_cgroups || config->cgroup_list) {
231 const char *cgrp_name = cgrp ? cgrp->name : "";
232
233 if (config->json_output)
234 print_cgroup_json(os, cgrp_name);
235 else if (config->csv_output)
236 print_cgroup_csv(config, cgrp_name);
237 else
238 print_cgroup_std(config, cgrp_name);
239 }
240}
241
242static void print_aggr_id_std(struct perf_stat_config *config,
243 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
244{
245 FILE *output = config->output;
246 int idx = config->aggr_mode;
247 char buf[128];
248
249 switch (config->aggr_mode) {
250 case AGGR_CORE:
251 snprintf(buf, sizeof(buf), "S%d-D%d-C%d", id.socket, id.die, id.core);
252 break;
253 case AGGR_CACHE:
254 snprintf(buf, sizeof(buf), "S%d-D%d-L%d-ID%d",
255 id.socket, id.die, id.cache_lvl, id.cache);
256 break;
257 case AGGR_CLUSTER:
258 snprintf(buf, sizeof(buf), "S%d-D%d-CLS%d", id.socket, id.die, id.cluster);
259 break;
260 case AGGR_DIE:
261 snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
262 break;
263 case AGGR_SOCKET:
264 snprintf(buf, sizeof(buf), "S%d", id.socket);
265 break;
266 case AGGR_NODE:
267 snprintf(buf, sizeof(buf), "N%d", id.node);
268 break;
269 case AGGR_NONE:
270 if (evsel->percore && !config->percore_show_thread) {
271 snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
272 id.socket, id.die, id.core);
273 fprintf(output, "%-*s ",
274 aggr_header_lens[AGGR_CORE], buf);
275 } else if (id.cpu.cpu > -1) {
276 fprintf(output, "CPU%-*d ",
277 aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
278 }
279 return;
280 case AGGR_THREAD:
281 fprintf(output, "%*s-%-*d ",
282 COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
283 PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
284 return;
285 case AGGR_GLOBAL:
286 case AGGR_UNSET:
287 case AGGR_MAX:
288 default:
289 return;
290 }
291
292 fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, 4, aggr_nr);
293}
294
295static void print_aggr_id_csv(struct perf_stat_config *config,
296 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
297{
298 FILE *output = config->output;
299 const char *sep = config->csv_sep;
300
301 switch (config->aggr_mode) {
302 case AGGR_CORE:
303 fprintf(output, "S%d-D%d-C%d%s%d%s",
304 id.socket, id.die, id.core, sep, aggr_nr, sep);
305 break;
306 case AGGR_CACHE:
307 fprintf(config->output, "S%d-D%d-L%d-ID%d%s%d%s",
308 id.socket, id.die, id.cache_lvl, id.cache, sep, aggr_nr, sep);
309 break;
310 case AGGR_CLUSTER:
311 fprintf(config->output, "S%d-D%d-CLS%d%s%d%s",
312 id.socket, id.die, id.cluster, sep, aggr_nr, sep);
313 break;
314 case AGGR_DIE:
315 fprintf(output, "S%d-D%d%s%d%s",
316 id.socket, id.die, sep, aggr_nr, sep);
317 break;
318 case AGGR_SOCKET:
319 fprintf(output, "S%d%s%d%s",
320 id.socket, sep, aggr_nr, sep);
321 break;
322 case AGGR_NODE:
323 fprintf(output, "N%d%s%d%s",
324 id.node, sep, aggr_nr, sep);
325 break;
326 case AGGR_NONE:
327 if (evsel->percore && !config->percore_show_thread) {
328 fprintf(output, "S%d-D%d-C%d%s",
329 id.socket, id.die, id.core, sep);
330 } else if (id.cpu.cpu > -1) {
331 fprintf(output, "CPU%d%s",
332 id.cpu.cpu, sep);
333 }
334 break;
335 case AGGR_THREAD:
336 fprintf(output, "%s-%d%s",
337 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
338 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
339 sep);
340 break;
341 case AGGR_GLOBAL:
342 case AGGR_UNSET:
343 case AGGR_MAX:
344 default:
345 break;
346 }
347}
348
349static void print_aggr_id_json(struct perf_stat_config *config, struct outstate *os,
350 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
351{
352 switch (config->aggr_mode) {
353 case AGGR_CORE:
354 json_out(os, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d",
355 id.socket, id.die, id.core, aggr_nr);
356 break;
357 case AGGR_CACHE:
358 json_out(os, "\"cache\" : \"S%d-D%d-L%d-ID%d\", \"aggregate-number\" : %d",
359 id.socket, id.die, id.cache_lvl, id.cache, aggr_nr);
360 break;
361 case AGGR_CLUSTER:
362 json_out(os, "\"cluster\" : \"S%d-D%d-CLS%d\", \"aggregate-number\" : %d",
363 id.socket, id.die, id.cluster, aggr_nr);
364 break;
365 case AGGR_DIE:
366 json_out(os, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d",
367 id.socket, id.die, aggr_nr);
368 break;
369 case AGGR_SOCKET:
370 json_out(os, "\"socket\" : \"S%d\", \"aggregate-number\" : %d",
371 id.socket, aggr_nr);
372 break;
373 case AGGR_NODE:
374 json_out(os, "\"node\" : \"N%d\", \"aggregate-number\" : %d",
375 id.node, aggr_nr);
376 break;
377 case AGGR_NONE:
378 if (evsel->percore && !config->percore_show_thread) {
379 json_out(os, "\"core\" : \"S%d-D%d-C%d\"",
380 id.socket, id.die, id.core);
381 } else if (id.cpu.cpu > -1) {
382 json_out(os, "\"cpu\" : \"%d\"",
383 id.cpu.cpu);
384 }
385 break;
386 case AGGR_THREAD:
387 json_out(os, "\"thread\" : \"%s-%d\"",
388 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
389 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
390 break;
391 case AGGR_GLOBAL:
392 case AGGR_UNSET:
393 case AGGR_MAX:
394 default:
395 break;
396 }
397}
398
399static void aggr_printout(struct perf_stat_config *config, struct outstate *os,
400 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
401{
402 if (config->json_output)
403 print_aggr_id_json(config, os, evsel, id, aggr_nr);
404 else if (config->csv_output)
405 print_aggr_id_csv(config, evsel, id, aggr_nr);
406 else
407 print_aggr_id_std(config, evsel, id, aggr_nr);
408}
409
410static void new_line_std(struct perf_stat_config *config __maybe_unused,
411 void *ctx)
412{
413 struct outstate *os = ctx;
414
415 os->newline = true;
416}
417
418static inline void __new_line_std_csv(struct perf_stat_config *config,
419 struct outstate *os)
420{
421 fputc('\n', os->fh);
422 if (os->prefix)
423 fputs(os->prefix, os->fh);
424 aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
425}
426
427static inline void __new_line_std(struct outstate *os)
428{
429 fprintf(os->fh, " ");
430}
431
432static void do_new_line_std(struct perf_stat_config *config,
433 struct outstate *os)
434{
435 __new_line_std_csv(config, os);
436 if (config->aggr_mode == AGGR_NONE)
437 fprintf(os->fh, " ");
438 __new_line_std(os);
439}
440
441static void print_metric_std(struct perf_stat_config *config,
442 void *ctx, enum metric_threshold_classify thresh,
443 const char *fmt, const char *unit, double val)
444{
445 struct outstate *os = ctx;
446 FILE *out = os->fh;
447 int n;
448 bool newline = os->newline;
449 const char *color = metric_threshold_classify__color(thresh);
450
451 os->newline = false;
452
453 if (unit == NULL || fmt == NULL) {
454 fprintf(out, "%-*s", METRIC_LEN, "");
455 return;
456 }
457
458 if (newline)
459 do_new_line_std(config, os);
460
461 n = fprintf(out, " # ");
462 if (color)
463 n += color_fprintf(out, color, fmt, val);
464 else
465 n += fprintf(out, fmt, val);
466 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
467}
468
469static void new_line_csv(struct perf_stat_config *config, void *ctx)
470{
471 struct outstate *os = ctx;
472 int i;
473
474 __new_line_std_csv(config, os);
475 for (i = 0; i < os->nfields; i++)
476 fputs(config->csv_sep, os->fh);
477}
478
479static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
480 void *ctx,
481 enum metric_threshold_classify thresh __maybe_unused,
482 const char *fmt, const char *unit, double val)
483{
484 struct outstate *os = ctx;
485 FILE *out = os->fh;
486 char buf[64], *vals, *ends;
487
488 if (unit == NULL || fmt == NULL) {
489 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
490 return;
491 }
492 snprintf(buf, sizeof(buf), fmt, val);
493 ends = vals = skip_spaces(buf);
494 while (isdigit(*ends) || *ends == '.')
495 ends++;
496 *ends = 0;
497 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
498}
499
500static void print_metric_json(struct perf_stat_config *config __maybe_unused,
501 void *ctx,
502 enum metric_threshold_classify thresh,
503 const char *fmt __maybe_unused,
504 const char *unit, double val)
505{
506 struct outstate *os = ctx;
507 FILE *out = os->fh;
508
509 if (unit) {
510 json_out(os, "\"metric-value\" : \"%f\", \"metric-unit\" : \"%s\"", val, unit);
511 if (thresh != METRIC_THRESHOLD_UNKNOWN) {
512 json_out(os, "\"metric-threshold\" : \"%s\"",
513 metric_threshold_classify__str(thresh));
514 }
515 }
516 if (!config->metric_only)
517 fprintf(out, "}");
518}
519
520static void new_line_json(struct perf_stat_config *config, void *ctx)
521{
522 struct outstate *os = ctx;
523
524 fputs("\n{", os->fh);
525 os->first = true;
526 if (os->prefix)
527 json_out(os, "%s", os->prefix);
528
529 aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
530}
531
532static void print_metricgroup_header_json(struct perf_stat_config *config,
533 void *ctx,
534 const char *metricgroup_name)
535{
536 if (!metricgroup_name)
537 return;
538
539 json_out((struct outstate *) ctx, "\"metricgroup\" : \"%s\"}", metricgroup_name);
540 new_line_json(config, ctx);
541}
542
543static void print_metricgroup_header_csv(struct perf_stat_config *config,
544 void *ctx,
545 const char *metricgroup_name)
546{
547 struct outstate *os = ctx;
548 int i;
549
550 if (!metricgroup_name) {
551 /* Leave space for running and enabling */
552 for (i = 0; i < os->nfields - 2; i++)
553 fputs(config->csv_sep, os->fh);
554 return;
555 }
556
557 for (i = 0; i < os->nfields; i++)
558 fputs(config->csv_sep, os->fh);
559 fprintf(config->output, "%s", metricgroup_name);
560 new_line_csv(config, ctx);
561}
562
563static void print_metricgroup_header_std(struct perf_stat_config *config,
564 void *ctx,
565 const char *metricgroup_name)
566{
567 struct outstate *os = ctx;
568 int n;
569
570 if (!metricgroup_name) {
571 __new_line_std(os);
572 return;
573 }
574
575 n = fprintf(config->output, " %*s", EVNAME_LEN, metricgroup_name);
576
577 fprintf(config->output, "%*s", MGROUP_LEN - n - 1, "");
578}
579
580/* Filter out some columns that don't work well in metrics only mode */
581
582static bool valid_only_metric(const char *unit)
583{
584 if (!unit)
585 return false;
586 if (strstr(unit, "/sec") ||
587 strstr(unit, "CPUs utilized"))
588 return false;
589 return true;
590}
591
592static const char *fixunit(char *buf, struct evsel *evsel,
593 const char *unit)
594{
595 if (!strncmp(unit, "of all", 6)) {
596 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
597 unit);
598 return buf;
599 }
600 return unit;
601}
602
603static void print_metric_only(struct perf_stat_config *config,
604 void *ctx, enum metric_threshold_classify thresh,
605 const char *fmt, const char *unit, double val)
606{
607 struct outstate *os = ctx;
608 FILE *out = os->fh;
609 char buf[1024], str[1024];
610 unsigned mlen = config->metric_only_len;
611 const char *color = metric_threshold_classify__color(thresh);
612
613 if (!valid_only_metric(unit))
614 return;
615 unit = fixunit(buf, os->evsel, unit);
616 if (mlen < strlen(unit))
617 mlen = strlen(unit) + 1;
618
619 if (color)
620 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
621
622 color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);
623 fprintf(out, "%*s ", mlen, str);
624 os->first = false;
625}
626
627static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
628 void *ctx,
629 enum metric_threshold_classify thresh __maybe_unused,
630 const char *fmt,
631 const char *unit, double val)
632{
633 struct outstate *os = ctx;
634 FILE *out = os->fh;
635 char buf[64], *vals, *ends;
636 char tbuf[1024];
637
638 if (!valid_only_metric(unit))
639 return;
640 unit = fixunit(tbuf, os->evsel, unit);
641 snprintf(buf, sizeof(buf), fmt ?: "", val);
642 ends = vals = skip_spaces(buf);
643 while (isdigit(*ends) || *ends == '.')
644 ends++;
645 *ends = 0;
646 fprintf(out, "%s%s", vals, config->csv_sep);
647 os->first = false;
648}
649
650static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
651 void *ctx,
652 enum metric_threshold_classify thresh __maybe_unused,
653 const char *fmt,
654 const char *unit, double val)
655{
656 struct outstate *os = ctx;
657 char buf[64], *ends;
658 char tbuf[1024];
659 const char *vals;
660
661 if (!valid_only_metric(unit))
662 return;
663 unit = fixunit(tbuf, os->evsel, unit);
664 if (!unit[0])
665 return;
666 snprintf(buf, sizeof(buf), fmt ?: "", val);
667 vals = ends = skip_spaces(buf);
668 while (isdigit(*ends) || *ends == '.')
669 ends++;
670 *ends = 0;
671 if (!vals[0])
672 vals = "none";
673 json_out(os, "\"%s\" : \"%s\"", unit, vals);
674}
675
676static void new_line_metric(struct perf_stat_config *config __maybe_unused,
677 void *ctx __maybe_unused)
678{
679}
680
681static void print_metric_header(struct perf_stat_config *config,
682 void *ctx,
683 enum metric_threshold_classify thresh __maybe_unused,
684 const char *fmt __maybe_unused,
685 const char *unit, double val __maybe_unused)
686{
687 struct outstate *os = ctx;
688 char tbuf[1024];
689
690 /* In case of iostat, print metric header for first root port only */
691 if (config->iostat_run &&
692 os->evsel->priv != os->evsel->evlist->selected->priv)
693 return;
694
695 if (os->evsel->cgrp != os->cgrp)
696 return;
697
698 if (!valid_only_metric(unit))
699 return;
700 unit = fixunit(tbuf, os->evsel, unit);
701
702 if (config->json_output)
703 return;
704 else if (config->csv_output)
705 fprintf(os->fh, "%s%s", unit, config->csv_sep);
706 else
707 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
708}
709
710static void print_counter_value_std(struct perf_stat_config *config,
711 struct evsel *evsel, double avg, bool ok)
712{
713 FILE *output = config->output;
714 double sc = evsel->scale;
715 const char *fmt;
716 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
717
718 if (config->big_num)
719 fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
720 else
721 fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
722
723 if (ok)
724 fprintf(output, fmt, COUNTS_LEN, avg);
725 else
726 fprintf(output, "%*s ", COUNTS_LEN, bad_count);
727
728 if (evsel->unit)
729 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
730
731 fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
732}
733
734static void print_counter_value_csv(struct perf_stat_config *config,
735 struct evsel *evsel, double avg, bool ok)
736{
737 FILE *output = config->output;
738 double sc = evsel->scale;
739 const char *sep = config->csv_sep;
740 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
741 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
742
743 if (ok)
744 fprintf(output, fmt, avg, sep);
745 else
746 fprintf(output, "%s%s", bad_count, sep);
747
748 if (evsel->unit)
749 fprintf(output, "%s%s", evsel->unit, sep);
750
751 fprintf(output, "%s", evsel__name(evsel));
752}
753
754static void print_counter_value_json(struct outstate *os,
755 struct evsel *evsel, double avg, bool ok)
756{
757 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
758
759 if (ok)
760 json_out(os, "\"counter-value\" : \"%f\"", avg);
761 else
762 json_out(os, "\"counter-value\" : \"%s\"", bad_count);
763
764 if (evsel->unit)
765 json_out(os, "\"unit\" : \"%s\"", evsel->unit);
766
767 json_out(os, "\"event\" : \"%s\"", evsel__name(evsel));
768}
769
770static void print_counter_value(struct perf_stat_config *config, struct outstate *os,
771 struct evsel *evsel, double avg, bool ok)
772{
773 if (config->json_output)
774 print_counter_value_json(os, evsel, avg, ok);
775 else if (config->csv_output)
776 print_counter_value_csv(config, evsel, avg, ok);
777 else
778 print_counter_value_std(config, evsel, avg, ok);
779}
780
781static void abs_printout(struct perf_stat_config *config,
782 struct outstate *os,
783 struct aggr_cpu_id id, int aggr_nr,
784 struct evsel *evsel, double avg, bool ok)
785{
786 aggr_printout(config, os, evsel, id, aggr_nr);
787 print_counter_value(config, os, evsel, avg, ok);
788 print_cgroup(config, os, evsel->cgrp);
789}
790
791static bool is_mixed_hw_group(struct evsel *counter)
792{
793 struct evlist *evlist = counter->evlist;
794 u32 pmu_type = counter->core.attr.type;
795 struct evsel *pos;
796
797 if (counter->core.nr_members < 2)
798 return false;
799
800 evlist__for_each_entry(evlist, pos) {
801 /* software events can be part of any hardware group */
802 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
803 continue;
804 if (pmu_type == PERF_TYPE_SOFTWARE) {
805 pmu_type = pos->core.attr.type;
806 continue;
807 }
808 if (pmu_type != pos->core.attr.type)
809 return true;
810 }
811
812 return false;
813}
814
815static bool evlist__has_hybrid(struct evlist *evlist)
816{
817 struct evsel *evsel;
818
819 if (perf_pmus__num_core_pmus() == 1)
820 return false;
821
822 evlist__for_each_entry(evlist, evsel) {
823 if (evsel->core.is_pmu_core)
824 return true;
825 }
826
827 return false;
828}
829
830static void printout(struct perf_stat_config *config, struct outstate *os,
831 double uval, u64 run, u64 ena, double noise, int aggr_idx)
832{
833 struct perf_stat_output_ctx out;
834 print_metric_t pm;
835 new_line_t nl;
836 print_metricgroup_header_t pmh;
837 bool ok = true;
838 struct evsel *counter = os->evsel;
839
840 if (config->csv_output) {
841 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
842 nl = config->metric_only ? new_line_metric : new_line_csv;
843 pmh = print_metricgroup_header_csv;
844 os->nfields = 4 + (counter->cgrp ? 1 : 0);
845 } else if (config->json_output) {
846 pm = config->metric_only ? print_metric_only_json : print_metric_json;
847 nl = config->metric_only ? new_line_metric : new_line_json;
848 pmh = print_metricgroup_header_json;
849 } else {
850 pm = config->metric_only ? print_metric_only : print_metric_std;
851 nl = config->metric_only ? new_line_metric : new_line_std;
852 pmh = print_metricgroup_header_std;
853 }
854
855 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
856 if (config->metric_only) {
857 pm(config, os, METRIC_THRESHOLD_UNKNOWN, "", "", 0);
858 return;
859 }
860
861 ok = false;
862
863 if (counter->supported) {
864 if (!evlist__has_hybrid(counter->evlist)) {
865 config->print_free_counters_hint = 1;
866 if (is_mixed_hw_group(counter))
867 config->print_mixed_hw_group_error = 1;
868 }
869 }
870 }
871
872 out.print_metric = pm;
873 out.new_line = nl;
874 out.print_metricgroup_header = pmh;
875 out.ctx = os;
876 out.force_header = false;
877
878 if (!config->metric_only && !counter->default_metricgroup) {
879 abs_printout(config, os, os->id, os->aggr_nr, counter, uval, ok);
880
881 print_noise(config, os, counter, noise, /*before_metric=*/true);
882 print_running(config, os, run, ena, /*before_metric=*/true);
883 }
884
885 if (ok) {
886 if (!config->metric_only && counter->default_metricgroup) {
887 void *from = NULL;
888
889 aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
890 /* Print out all the metricgroup with the same metric event. */
891 do {
892 int num = 0;
893
894 /* Print out the new line for the next new metricgroup. */
895 if (from) {
896 if (config->json_output)
897 new_line_json(config, (void *)os);
898 else
899 __new_line_std_csv(config, os);
900 }
901
902 print_noise(config, os, counter, noise, /*before_metric=*/true);
903 print_running(config, os, run, ena, /*before_metric=*/true);
904 from = perf_stat__print_shadow_stats_metricgroup(config, counter, aggr_idx,
905 &num, from, &out,
906 &config->metric_events);
907 } while (from != NULL);
908 } else
909 perf_stat__print_shadow_stats(config, counter, uval, aggr_idx,
910 &out, &config->metric_events);
911 } else {
912 pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL, /*unit=*/"", /*val=*/0);
913 }
914
915 if (!config->metric_only) {
916 print_noise(config, os, counter, noise, /*before_metric=*/false);
917 print_running(config, os, run, ena, /*before_metric=*/false);
918 }
919}
920
921static void uniquify_event_name(struct evsel *counter)
922{
923 const char *name, *pmu_name;
924 char *new_name, *config;
925 int ret;
926
927 /* The evsel was already uniquified. */
928 if (counter->uniquified_name)
929 return;
930
931 /* Avoid checking to uniquify twice. */
932 counter->uniquified_name = true;
933
934 /* The evsel has a "name=" config term or is from libpfm. */
935 if (counter->use_config_name || counter->is_libpfm_event)
936 return;
937
938 /* Legacy no PMU event, don't uniquify. */
939 if (!counter->pmu ||
940 (counter->pmu->type < PERF_TYPE_MAX && counter->pmu->type != PERF_TYPE_RAW))
941 return;
942
943 /* A sysfs or json event replacing a legacy event, don't uniquify. */
944 if (counter->pmu->is_core && counter->alternate_hw_config != PERF_COUNT_HW_MAX)
945 return;
946
947 name = evsel__name(counter);
948 pmu_name = counter->pmu->name;
949 /* Already prefixed by the PMU name. */
950 if (!strncmp(name, pmu_name, strlen(pmu_name)))
951 return;
952
953 config = strchr(name, '/');
954 if (config) {
955 int len = config - name;
956
957 if (config[1] == '/') {
958 /* case: event// */
959 ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 2);
960 } else {
961 /* case: event/.../ */
962 ret = asprintf(&new_name, "%s/%.*s,%s", pmu_name, len, name, config + 1);
963 }
964 } else {
965 config = strchr(name, ':');
966 if (config) {
967 /* case: event:.. */
968 int len = config - name;
969
970 ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 1);
971 } else {
972 /* case: event */
973 ret = asprintf(&new_name, "%s/%s/", pmu_name, name);
974 }
975 }
976 if (ret > 0) {
977 free(counter->name);
978 counter->name = new_name;
979 } else {
980 /* ENOMEM from asprintf. */
981 counter->uniquified_name = false;
982 }
983}
984
985static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
986{
987 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
988}
989
990static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
991{
992 if (config->aggr_mode == AGGR_NONE || hybrid_uniquify(counter, config))
993 uniquify_event_name(counter);
994}
995
996/**
997 * should_skip_zero_count() - Check if the event should print 0 values.
998 * @config: The perf stat configuration (including aggregation mode).
999 * @counter: The evsel with its associated cpumap.
1000 * @id: The aggregation id that is being queried.
1001 *
1002 * Due to mismatch between the event cpumap or thread-map and the
1003 * aggregation mode, sometimes it'd iterate the counter with the map
1004 * which does not contain any values.
1005 *
1006 * For example, uncore events have dedicated CPUs to manage them,
1007 * result for other CPUs should be zero and skipped.
1008 *
1009 * Return: %true if the value should NOT be printed, %false if the value
1010 * needs to be printed like "<not counted>" or "<not supported>".
1011 */
1012static bool should_skip_zero_counter(struct perf_stat_config *config,
1013 struct evsel *counter,
1014 const struct aggr_cpu_id *id)
1015{
1016 struct perf_cpu cpu;
1017 int idx;
1018
1019 /*
1020 * Skip unsupported default events when not verbose. (default events
1021 * are all marked 'skippable').
1022 */
1023 if (verbose == 0 && counter->skippable && !counter->supported)
1024 return true;
1025
1026 /*
1027 * Skip value 0 when enabling --per-thread globally,
1028 * otherwise it will have too many 0 output.
1029 */
1030 if (config->aggr_mode == AGGR_THREAD && config->system_wide)
1031 return true;
1032
1033 /*
1034 * Many tool events are only gathered on the first index, skip other
1035 * zero values.
1036 */
1037 if (evsel__is_tool(counter)) {
1038 struct aggr_cpu_id own_id =
1039 config->aggr_get_id(config, (struct perf_cpu){ .cpu = 0 });
1040
1041 return !aggr_cpu_id__equal(id, &own_id);
1042 }
1043
1044 /*
1045 * Skip value 0 when it's an uncore event and the given aggr id
1046 * does not belong to the PMU cpumask.
1047 */
1048 if (!counter->pmu || !counter->pmu->is_uncore)
1049 return false;
1050
1051 perf_cpu_map__for_each_cpu(cpu, idx, counter->pmu->cpus) {
1052 struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
1053
1054 if (aggr_cpu_id__equal(id, &own_id))
1055 return false;
1056 }
1057 return true;
1058}
1059
1060static void print_counter_aggrdata(struct perf_stat_config *config,
1061 struct evsel *counter, int aggr_idx,
1062 struct outstate *os)
1063{
1064 FILE *output = config->output;
1065 u64 ena, run, val;
1066 double uval;
1067 struct perf_stat_evsel *ps = counter->stats;
1068 struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
1069 struct aggr_cpu_id id = config->aggr_map->map[aggr_idx];
1070 double avg = aggr->counts.val;
1071 bool metric_only = config->metric_only;
1072
1073 os->id = id;
1074 os->aggr_nr = aggr->nr;
1075 os->evsel = counter;
1076
1077 /* Skip already merged uncore/hybrid events */
1078 if (counter->merged_stat)
1079 return;
1080
1081 uniquify_counter(config, counter);
1082
1083 val = aggr->counts.val;
1084 ena = aggr->counts.ena;
1085 run = aggr->counts.run;
1086
1087 if (perf_stat__skip_metric_event(counter, &config->metric_events, ena, run))
1088 return;
1089
1090 if (val == 0 && should_skip_zero_counter(config, counter, &id))
1091 return;
1092
1093 if (!metric_only) {
1094 if (config->json_output) {
1095 os->first = true;
1096 fputc('{', output);
1097 }
1098 if (os->prefix) {
1099 if (config->json_output)
1100 json_out(os, "%s", os->prefix);
1101 else
1102 fprintf(output, "%s", os->prefix);
1103 } else if (config->summary && config->csv_output &&
1104 !config->no_csv_summary && !config->interval)
1105 fprintf(output, "%s%s", "summary", config->csv_sep);
1106 }
1107
1108 uval = val * counter->scale;
1109
1110 printout(config, os, uval, run, ena, avg, aggr_idx);
1111
1112 if (!metric_only)
1113 fputc('\n', output);
1114}
1115
1116static void print_metric_begin(struct perf_stat_config *config,
1117 struct evlist *evlist,
1118 struct outstate *os, int aggr_idx)
1119{
1120 struct perf_stat_aggr *aggr;
1121 struct aggr_cpu_id id;
1122 struct evsel *evsel;
1123
1124 os->first = true;
1125 if (!config->metric_only)
1126 return;
1127
1128 if (config->json_output)
1129 fputc('{', config->output);
1130
1131 if (os->prefix) {
1132 if (config->json_output)
1133 json_out(os, "%s", os->prefix);
1134 else
1135 fprintf(config->output, "%s", os->prefix);
1136 }
1137 evsel = evlist__first(evlist);
1138 id = config->aggr_map->map[aggr_idx];
1139 aggr = &evsel->stats->aggr[aggr_idx];
1140 aggr_printout(config, os, evsel, id, aggr->nr);
1141
1142 print_cgroup(config, os, os->cgrp ? : evsel->cgrp);
1143}
1144
1145static void print_metric_end(struct perf_stat_config *config, struct outstate *os)
1146{
1147 FILE *output = config->output;
1148
1149 if (!config->metric_only)
1150 return;
1151
1152 if (config->json_output) {
1153 if (os->first)
1154 fputs("\"metric-value\" : \"none\"", output);
1155 fputc('}', output);
1156 }
1157 fputc('\n', output);
1158}
1159
1160static void print_aggr(struct perf_stat_config *config,
1161 struct evlist *evlist,
1162 struct outstate *os)
1163{
1164 struct evsel *counter;
1165 int aggr_idx;
1166
1167 if (!config->aggr_map || !config->aggr_get_id)
1168 return;
1169
1170 /*
1171 * With metric_only everything is on a single line.
1172 * Without each counter has its own line.
1173 */
1174 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1175 print_metric_begin(config, evlist, os, aggr_idx);
1176
1177 evlist__for_each_entry(evlist, counter) {
1178 print_counter_aggrdata(config, counter, aggr_idx, os);
1179 }
1180 print_metric_end(config, os);
1181 }
1182}
1183
1184static void print_aggr_cgroup(struct perf_stat_config *config,
1185 struct evlist *evlist,
1186 struct outstate *os)
1187{
1188 struct evsel *counter, *evsel;
1189 int aggr_idx;
1190
1191 if (!config->aggr_map || !config->aggr_get_id)
1192 return;
1193
1194 evlist__for_each_entry(evlist, evsel) {
1195 if (os->cgrp == evsel->cgrp)
1196 continue;
1197
1198 os->cgrp = evsel->cgrp;
1199
1200 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1201 print_metric_begin(config, evlist, os, aggr_idx);
1202
1203 evlist__for_each_entry(evlist, counter) {
1204 if (counter->cgrp != os->cgrp)
1205 continue;
1206
1207 print_counter_aggrdata(config, counter, aggr_idx, os);
1208 }
1209 print_metric_end(config, os);
1210 }
1211 }
1212}
1213
1214static void print_counter(struct perf_stat_config *config,
1215 struct evsel *counter, struct outstate *os)
1216{
1217 int aggr_idx;
1218
1219 /* AGGR_THREAD doesn't have config->aggr_get_id */
1220 if (!config->aggr_map)
1221 return;
1222
1223 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1224 print_counter_aggrdata(config, counter, aggr_idx, os);
1225 }
1226}
1227
1228static void print_no_aggr_metric(struct perf_stat_config *config,
1229 struct evlist *evlist,
1230 struct outstate *os)
1231{
1232 int all_idx;
1233 struct perf_cpu cpu;
1234
1235 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1236 struct evsel *counter;
1237 bool first = true;
1238
1239 evlist__for_each_entry(evlist, counter) {
1240 u64 ena, run, val;
1241 double uval;
1242 struct perf_stat_evsel *ps = counter->stats;
1243 int aggr_idx = 0;
1244
1245 if (!perf_cpu_map__has(evsel__cpus(counter), cpu))
1246 continue;
1247
1248 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1249 if (config->aggr_map->map[aggr_idx].cpu.cpu == cpu.cpu)
1250 break;
1251 }
1252
1253 os->evsel = counter;
1254 os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1255 if (first) {
1256 print_metric_begin(config, evlist, os, aggr_idx);
1257 first = false;
1258 }
1259 val = ps->aggr[aggr_idx].counts.val;
1260 ena = ps->aggr[aggr_idx].counts.ena;
1261 run = ps->aggr[aggr_idx].counts.run;
1262
1263 uval = val * counter->scale;
1264 printout(config, os, uval, run, ena, 1.0, aggr_idx);
1265 }
1266 if (!first)
1267 print_metric_end(config, os);
1268 }
1269}
1270
1271static void print_metric_headers_std(struct perf_stat_config *config,
1272 bool no_indent)
1273{
1274 fputc(' ', config->output);
1275
1276 if (!no_indent) {
1277 int len = aggr_header_lens[config->aggr_mode];
1278
1279 if (nr_cgroups || config->cgroup_list)
1280 len += CGROUP_LEN + 1;
1281
1282 fprintf(config->output, "%*s", len, "");
1283 }
1284}
1285
1286static void print_metric_headers_csv(struct perf_stat_config *config,
1287 bool no_indent __maybe_unused)
1288{
1289 const char *p;
1290
1291 if (config->interval)
1292 fprintf(config->output, "time%s", config->csv_sep);
1293 if (config->iostat_run)
1294 return;
1295
1296 p = aggr_header_csv[config->aggr_mode];
1297 while (*p) {
1298 if (*p == ',')
1299 fputs(config->csv_sep, config->output);
1300 else
1301 fputc(*p, config->output);
1302 p++;
1303 }
1304}
1305
1306static void print_metric_headers_json(struct perf_stat_config *config __maybe_unused,
1307 bool no_indent __maybe_unused)
1308{
1309}
1310
1311static void print_metric_headers(struct perf_stat_config *config,
1312 struct evlist *evlist, bool no_indent)
1313{
1314 struct evsel *counter;
1315 struct outstate os = {
1316 .fh = config->output
1317 };
1318 struct perf_stat_output_ctx out = {
1319 .ctx = &os,
1320 .print_metric = print_metric_header,
1321 .new_line = new_line_metric,
1322 .force_header = true,
1323 };
1324
1325 if (config->json_output)
1326 print_metric_headers_json(config, no_indent);
1327 else if (config->csv_output)
1328 print_metric_headers_csv(config, no_indent);
1329 else
1330 print_metric_headers_std(config, no_indent);
1331
1332 if (config->iostat_run)
1333 iostat_print_header_prefix(config);
1334
1335 if (config->cgroup_list)
1336 os.cgrp = evlist__first(evlist)->cgrp;
1337
1338 /* Print metrics headers only */
1339 evlist__for_each_entry(evlist, counter) {
1340 if (!config->iostat_run &&
1341 config->aggr_mode != AGGR_NONE && counter->metric_leader != counter)
1342 continue;
1343
1344 os.evsel = counter;
1345
1346 perf_stat__print_shadow_stats(config, counter, 0,
1347 0,
1348 &out,
1349 &config->metric_events);
1350 }
1351
1352 if (!config->json_output)
1353 fputc('\n', config->output);
1354}
1355
1356static void prepare_interval(struct perf_stat_config *config,
1357 char *prefix, size_t len, struct timespec *ts)
1358{
1359 if (config->iostat_run)
1360 return;
1361
1362 if (config->json_output)
1363 scnprintf(prefix, len, "\"interval\" : %lu.%09lu",
1364 (unsigned long) ts->tv_sec, ts->tv_nsec);
1365 else if (config->csv_output)
1366 scnprintf(prefix, len, "%lu.%09lu%s",
1367 (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1368 else
1369 scnprintf(prefix, len, "%6lu.%09lu ",
1370 (unsigned long) ts->tv_sec, ts->tv_nsec);
1371}
1372
1373static void print_header_interval_std(struct perf_stat_config *config,
1374 struct target *_target __maybe_unused,
1375 struct evlist *evlist,
1376 int argc __maybe_unused,
1377 const char **argv __maybe_unused)
1378{
1379 FILE *output = config->output;
1380
1381 switch (config->aggr_mode) {
1382 case AGGR_NODE:
1383 case AGGR_SOCKET:
1384 case AGGR_DIE:
1385 case AGGR_CLUSTER:
1386 case AGGR_CACHE:
1387 case AGGR_CORE:
1388 fprintf(output, "#%*s %-*s cpus",
1389 INTERVAL_LEN - 1, "time",
1390 aggr_header_lens[config->aggr_mode],
1391 aggr_header_std[config->aggr_mode]);
1392 break;
1393 case AGGR_NONE:
1394 fprintf(output, "#%*s %-*s",
1395 INTERVAL_LEN - 1, "time",
1396 aggr_header_lens[config->aggr_mode],
1397 aggr_header_std[config->aggr_mode]);
1398 break;
1399 case AGGR_THREAD:
1400 fprintf(output, "#%*s %*s-%-*s",
1401 INTERVAL_LEN - 1, "time",
1402 COMM_LEN, "comm", PID_LEN, "pid");
1403 break;
1404 case AGGR_GLOBAL:
1405 default:
1406 if (!config->iostat_run)
1407 fprintf(output, "#%*s",
1408 INTERVAL_LEN - 1, "time");
1409 case AGGR_UNSET:
1410 case AGGR_MAX:
1411 break;
1412 }
1413
1414 if (config->metric_only)
1415 print_metric_headers(config, evlist, true);
1416 else
1417 fprintf(output, " %*s %*s events\n",
1418 COUNTS_LEN, "counts", config->unit_width, "unit");
1419}
1420
1421static void print_header_std(struct perf_stat_config *config,
1422 struct target *_target, struct evlist *evlist,
1423 int argc, const char **argv)
1424{
1425 FILE *output = config->output;
1426 int i;
1427
1428 fprintf(output, "\n");
1429 fprintf(output, " Performance counter stats for ");
1430 if (_target->bpf_str)
1431 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1432 else if (_target->system_wide)
1433 fprintf(output, "\'system wide");
1434 else if (_target->cpu_list)
1435 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1436 else if (!target__has_task(_target)) {
1437 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1438 for (i = 1; argv && (i < argc); i++)
1439 fprintf(output, " %s", argv[i]);
1440 } else if (_target->pid)
1441 fprintf(output, "process id \'%s", _target->pid);
1442 else
1443 fprintf(output, "thread id \'%s", _target->tid);
1444
1445 fprintf(output, "\'");
1446 if (config->run_count > 1)
1447 fprintf(output, " (%d runs)", config->run_count);
1448 fprintf(output, ":\n\n");
1449
1450 if (config->metric_only)
1451 print_metric_headers(config, evlist, false);
1452}
1453
1454static void print_header_csv(struct perf_stat_config *config,
1455 struct target *_target __maybe_unused,
1456 struct evlist *evlist,
1457 int argc __maybe_unused,
1458 const char **argv __maybe_unused)
1459{
1460 if (config->metric_only)
1461 print_metric_headers(config, evlist, true);
1462}
1463static void print_header_json(struct perf_stat_config *config,
1464 struct target *_target __maybe_unused,
1465 struct evlist *evlist,
1466 int argc __maybe_unused,
1467 const char **argv __maybe_unused)
1468{
1469 if (config->metric_only)
1470 print_metric_headers(config, evlist, true);
1471}
1472
1473static void print_header(struct perf_stat_config *config,
1474 struct target *_target,
1475 struct evlist *evlist,
1476 int argc, const char **argv)
1477{
1478 static int num_print_iv;
1479
1480 fflush(stdout);
1481
1482 if (config->interval_clear)
1483 puts(CONSOLE_CLEAR);
1484
1485 if (num_print_iv == 0 || config->interval_clear) {
1486 if (config->json_output)
1487 print_header_json(config, _target, evlist, argc, argv);
1488 else if (config->csv_output)
1489 print_header_csv(config, _target, evlist, argc, argv);
1490 else if (config->interval)
1491 print_header_interval_std(config, _target, evlist, argc, argv);
1492 else
1493 print_header_std(config, _target, evlist, argc, argv);
1494 }
1495
1496 if (num_print_iv++ == 25)
1497 num_print_iv = 0;
1498}
1499
1500static int get_precision(double num)
1501{
1502 if (num > 1)
1503 return 0;
1504
1505 return lround(ceil(-log10(num)));
1506}
1507
1508static void print_table(struct perf_stat_config *config,
1509 FILE *output, int precision, double avg)
1510{
1511 char tmp[64];
1512 int idx, indent = 0;
1513
1514 scnprintf(tmp, 64, " %17.*f", precision, avg);
1515 while (tmp[indent] == ' ')
1516 indent++;
1517
1518 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1519
1520 for (idx = 0; idx < config->run_count; idx++) {
1521 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1522 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1523
1524 fprintf(output, " %17.*f (%+.*f) ",
1525 precision, run, precision, run - avg);
1526
1527 for (h = 0; h < n; h++)
1528 fprintf(output, "#");
1529
1530 fprintf(output, "\n");
1531 }
1532
1533 fprintf(output, "\n%*s# Final result:\n", indent, "");
1534}
1535
1536static double timeval2double(struct timeval *t)
1537{
1538 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1539}
1540
1541static void print_footer(struct perf_stat_config *config)
1542{
1543 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1544 FILE *output = config->output;
1545
1546 if (config->interval || config->csv_output || config->json_output)
1547 return;
1548
1549 if (!config->null_run)
1550 fprintf(output, "\n");
1551
1552 if (config->run_count == 1) {
1553 fprintf(output, " %17.9f seconds time elapsed", avg);
1554
1555 if (config->ru_display) {
1556 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1557 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1558
1559 fprintf(output, "\n\n");
1560 fprintf(output, " %17.9f seconds user\n", ru_utime);
1561 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1562 }
1563 } else {
1564 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1565 /*
1566 * Display at most 2 more significant
1567 * digits than the stddev inaccuracy.
1568 */
1569 int precision = get_precision(sd) + 2;
1570
1571 if (config->walltime_run_table)
1572 print_table(config, output, precision, avg);
1573
1574 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1575 precision, avg, precision, sd);
1576
1577 print_noise_pct(config, NULL, sd, avg, /*before_metric=*/false);
1578 }
1579 fprintf(output, "\n\n");
1580
1581 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1582 fprintf(output,
1583"Some events weren't counted. Try disabling the NMI watchdog:\n"
1584" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1585" perf stat ...\n"
1586" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1587
1588 if (config->print_mixed_hw_group_error)
1589 fprintf(output,
1590 "The events in group usually have to be from "
1591 "the same PMU. Try reorganizing the group.\n");
1592}
1593
1594static void print_percore(struct perf_stat_config *config,
1595 struct evsel *counter, struct outstate *os)
1596{
1597 bool metric_only = config->metric_only;
1598 FILE *output = config->output;
1599 struct cpu_aggr_map *core_map;
1600 int aggr_idx, core_map_len = 0;
1601
1602 if (!config->aggr_map || !config->aggr_get_id)
1603 return;
1604
1605 if (config->percore_show_thread)
1606 return print_counter(config, counter, os);
1607
1608 /*
1609 * core_map will hold the aggr_cpu_id for the cores that have been
1610 * printed so that each core is printed just once.
1611 */
1612 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1613 if (core_map == NULL) {
1614 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1615 return;
1616 }
1617
1618 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1619 struct perf_cpu curr_cpu = config->aggr_map->map[aggr_idx].cpu;
1620 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1621 bool found = false;
1622
1623 for (int i = 0; i < core_map_len; i++) {
1624 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1625 found = true;
1626 break;
1627 }
1628 }
1629 if (found)
1630 continue;
1631
1632 print_counter_aggrdata(config, counter, aggr_idx, os);
1633
1634 core_map->map[core_map_len++] = core_id;
1635 }
1636 free(core_map);
1637
1638 if (metric_only)
1639 fputc('\n', output);
1640}
1641
1642static void print_cgroup_counter(struct perf_stat_config *config, struct evlist *evlist,
1643 struct outstate *os)
1644{
1645 struct evsel *counter;
1646
1647 evlist__for_each_entry(evlist, counter) {
1648 if (os->cgrp != counter->cgrp) {
1649 if (os->cgrp != NULL)
1650 print_metric_end(config, os);
1651
1652 os->cgrp = counter->cgrp;
1653 print_metric_begin(config, evlist, os, /*aggr_idx=*/0);
1654 }
1655
1656 print_counter(config, counter, os);
1657 }
1658 if (os->cgrp)
1659 print_metric_end(config, os);
1660}
1661
1662static void disable_uniquify(struct evlist *evlist)
1663{
1664 struct evsel *counter;
1665 struct perf_pmu *last_pmu = NULL;
1666 bool first = true;
1667
1668 evlist__for_each_entry(evlist, counter) {
1669 /* If PMUs vary then uniquify can be useful. */
1670 if (!first && counter->pmu != last_pmu)
1671 return;
1672 first = false;
1673 if (counter->pmu) {
1674 /* Allow uniquify for uncore PMUs. */
1675 if (!counter->pmu->is_core)
1676 return;
1677 /* Keep hybrid event names uniquified for clarity. */
1678 if (perf_pmus__num_core_pmus() > 1)
1679 return;
1680 }
1681 }
1682 evlist__for_each_entry_continue(evlist, counter) {
1683 counter->uniquified_name = true;
1684 }
1685}
1686
1687void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1688 struct target *_target, struct timespec *ts,
1689 int argc, const char **argv)
1690{
1691 bool metric_only = config->metric_only;
1692 int interval = config->interval;
1693 struct evsel *counter;
1694 char buf[64];
1695 struct outstate os = {
1696 .fh = config->output,
1697 .first = true,
1698 };
1699
1700 disable_uniquify(evlist);
1701
1702 if (config->iostat_run)
1703 evlist->selected = evlist__first(evlist);
1704
1705 if (interval) {
1706 os.prefix = buf;
1707 prepare_interval(config, buf, sizeof(buf), ts);
1708 }
1709
1710 print_header(config, _target, evlist, argc, argv);
1711
1712 switch (config->aggr_mode) {
1713 case AGGR_CORE:
1714 case AGGR_CACHE:
1715 case AGGR_CLUSTER:
1716 case AGGR_DIE:
1717 case AGGR_SOCKET:
1718 case AGGR_NODE:
1719 if (config->cgroup_list)
1720 print_aggr_cgroup(config, evlist, &os);
1721 else
1722 print_aggr(config, evlist, &os);
1723 break;
1724 case AGGR_THREAD:
1725 case AGGR_GLOBAL:
1726 if (config->iostat_run) {
1727 iostat_print_counters(evlist, config, ts, buf,
1728 (iostat_print_counter_t)print_counter, &os);
1729 } else if (config->cgroup_list) {
1730 print_cgroup_counter(config, evlist, &os);
1731 } else {
1732 print_metric_begin(config, evlist, &os, /*aggr_idx=*/0);
1733 evlist__for_each_entry(evlist, counter) {
1734 print_counter(config, counter, &os);
1735 }
1736 print_metric_end(config, &os);
1737 }
1738 break;
1739 case AGGR_NONE:
1740 if (metric_only)
1741 print_no_aggr_metric(config, evlist, &os);
1742 else {
1743 evlist__for_each_entry(evlist, counter) {
1744 if (counter->percore)
1745 print_percore(config, counter, &os);
1746 else
1747 print_counter(config, counter, &os);
1748 }
1749 }
1750 break;
1751 case AGGR_MAX:
1752 case AGGR_UNSET:
1753 default:
1754 break;
1755 }
1756
1757 print_footer(config);
1758
1759 fflush(config->output);
1760}