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