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