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