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