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