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