4 #include <linux/string.h>
5 #include <linux/time64.h>
7 #include <perf/cpumap.h>
14 #include "thread_map.h"
17 #include <linux/ctype.h>
19 #include <api/fs/fs.h>
22 #include "pmu-hybrid.h"
23 #include "evlist-hybrid.h"
25 #define CNTR_NOT_SUPPORTED "<not supported>"
26 #define CNTR_NOT_COUNTED "<not counted>"
28 static void print_running(struct perf_stat_config *config,
32 double enabled_percent = 100;
35 enabled_percent = 100 * run / ena;
36 if (config->json_output)
37 fprintf(config->output,
38 "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
39 run, enabled_percent);
40 else if (config->csv_output)
41 fprintf(config->output,
42 "%s%" PRIu64 "%s%.2f", config->csv_sep,
43 run, config->csv_sep, enabled_percent);
45 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
48 static void print_noise_pct(struct perf_stat_config *config,
49 double total, double avg)
51 double pct = rel_stddev_stats(total, avg);
53 if (config->json_output)
54 fprintf(config->output, "\"variance\" : %.2f, ", pct);
55 else if (config->csv_output)
56 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
58 fprintf(config->output, " ( +-%6.2f%% )", pct);
61 static void print_noise(struct perf_stat_config *config,
62 struct evsel *evsel, double avg)
64 struct perf_stat_evsel *ps;
66 if (config->run_count == 1)
70 print_noise_pct(config, stddev_stats(&ps->res_stats), avg);
73 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
76 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
78 if (config->json_output)
79 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
81 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
86 static void aggr_printout(struct perf_stat_config *config,
87 struct evsel *evsel, struct aggr_cpu_id id, int nr)
91 if (config->json_output && !config->interval)
92 fprintf(config->output, "{");
94 switch (config->aggr_mode) {
96 if (config->json_output) {
97 fprintf(config->output,
98 "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
104 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
107 config->csv_output ? 0 : -8,
110 config->csv_output ? 0 : 4,
116 if (config->json_output) {
117 fprintf(config->output,
118 "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
123 fprintf(config->output, "S%d-D%*d%s%*d%s",
125 config->csv_output ? 0 : -8,
128 config->csv_output ? 0 : 4,
134 if (config->json_output) {
135 fprintf(config->output,
136 "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
140 fprintf(config->output, "S%*d%s%*d%s",
141 config->csv_output ? 0 : -5,
144 config->csv_output ? 0 : 4,
150 if (config->json_output) {
151 fprintf(config->output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
155 fprintf(config->output, "N%*d%s%*d%s",
156 config->csv_output ? 0 : -5,
159 config->csv_output ? 0 : 4,
165 if (config->json_output) {
166 if (evsel->percore && !config->percore_show_thread) {
167 fprintf(config->output, "\"core\" : \"S%d-D%d-C%d\"",
171 } else if (id.cpu.cpu > -1) {
172 fprintf(config->output, "\"cpu\" : \"%d\", ",
176 if (evsel->percore && !config->percore_show_thread) {
177 fprintf(config->output, "S%d-D%d-C%*d%s",
180 config->csv_output ? 0 : -3,
181 id.core, config->csv_sep);
182 } else if (id.cpu.cpu > -1) {
183 fprintf(config->output, "CPU%*d%s",
184 config->csv_output ? 0 : -7,
185 id.cpu.cpu, config->csv_sep);
190 if (config->json_output) {
191 fprintf(config->output, "\"thread\" : \"%s-%d\", ",
192 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
193 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
195 fprintf(config->output, "%*s-%*d%s",
196 config->csv_output ? 0 : 16,
197 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
198 config->csv_output ? 0 : -8,
199 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
217 struct aggr_cpu_id id;
221 #define METRIC_LEN 35
223 static void new_line_std(struct perf_stat_config *config __maybe_unused,
226 struct outstate *os = ctx;
231 static void do_new_line_std(struct perf_stat_config *config,
235 fputs(os->prefix, os->fh);
236 aggr_printout(config, os->evsel, os->id, os->nr);
237 if (config->aggr_mode == AGGR_NONE)
238 fprintf(os->fh, " ");
239 fprintf(os->fh, " ");
242 static void print_metric_std(struct perf_stat_config *config,
243 void *ctx, const char *color, const char *fmt,
244 const char *unit, double val)
246 struct outstate *os = ctx;
249 bool newline = os->newline;
253 if (unit == NULL || fmt == NULL) {
254 fprintf(out, "%-*s", METRIC_LEN, "");
259 do_new_line_std(config, os);
261 n = fprintf(out, " # ");
263 n += color_fprintf(out, color, fmt, val);
265 n += fprintf(out, fmt, val);
266 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
269 static void new_line_csv(struct perf_stat_config *config, void *ctx)
271 struct outstate *os = ctx;
276 fprintf(os->fh, "%s", os->prefix);
277 aggr_printout(config, os->evsel, os->id, os->nr);
278 for (i = 0; i < os->nfields; i++)
279 fputs(config->csv_sep, os->fh);
282 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
284 const char *color __maybe_unused,
285 const char *fmt, const char *unit, double val)
287 struct outstate *os = ctx;
289 char buf[64], *vals, *ends;
291 if (unit == NULL || fmt == NULL) {
292 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
295 snprintf(buf, sizeof(buf), fmt, val);
296 ends = vals = skip_spaces(buf);
297 while (isdigit(*ends) || *ends == '.')
300 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
303 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
305 const char *color __maybe_unused,
306 const char *fmt __maybe_unused,
307 const char *unit, double val)
309 struct outstate *os = ctx;
312 fprintf(out, "\"metric-value\" : %f, ", val);
313 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
314 if (!config->metric_only)
318 static void new_line_json(struct perf_stat_config *config, void *ctx)
320 struct outstate *os = ctx;
324 fprintf(os->fh, "%s", os->prefix);
325 aggr_printout(config, os->evsel, os->id, os->nr);
328 /* Filter out some columns that don't work well in metrics only mode */
330 static bool valid_only_metric(const char *unit)
334 if (strstr(unit, "/sec") ||
335 strstr(unit, "CPUs utilized"))
340 static const char *fixunit(char *buf, struct evsel *evsel,
343 if (!strncmp(unit, "of all", 6)) {
344 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
351 static void print_metric_only(struct perf_stat_config *config,
352 void *ctx, const char *color, const char *fmt,
353 const char *unit, double val)
355 struct outstate *os = ctx;
357 char buf[1024], str[1024];
358 unsigned mlen = config->metric_only_len;
360 if (!valid_only_metric(unit))
362 unit = fixunit(buf, os->evsel, unit);
363 if (mlen < strlen(unit))
364 mlen = strlen(unit) + 1;
367 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
369 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
370 fprintf(out, "%*s ", mlen, str);
373 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
374 void *ctx, const char *color __maybe_unused,
376 const char *unit, double val)
378 struct outstate *os = ctx;
380 char buf[64], *vals, *ends;
383 if (!valid_only_metric(unit))
385 unit = fixunit(tbuf, os->evsel, unit);
386 snprintf(buf, sizeof buf, fmt, val);
387 ends = vals = skip_spaces(buf);
388 while (isdigit(*ends) || *ends == '.')
391 fprintf(out, "%s%s", vals, config->csv_sep);
394 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
395 void *ctx, const char *color __maybe_unused,
397 const char *unit, double val)
399 struct outstate *os = ctx;
401 char buf[64], *vals, *ends;
404 if (!valid_only_metric(unit))
406 unit = fixunit(tbuf, os->evsel, unit);
407 snprintf(buf, sizeof(buf), fmt, val);
408 ends = vals = skip_spaces(buf);
409 while (isdigit(*ends) || *ends == '.')
412 fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
415 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
416 void *ctx __maybe_unused)
420 static void print_metric_header(struct perf_stat_config *config,
421 void *ctx, const char *color __maybe_unused,
422 const char *fmt __maybe_unused,
423 const char *unit, double val __maybe_unused)
425 struct outstate *os = ctx;
428 /* In case of iostat, print metric header for first root port only */
429 if (config->iostat_run &&
430 os->evsel->priv != os->evsel->evlist->selected->priv)
433 if (!valid_only_metric(unit) && !config->json_output)
435 unit = fixunit(tbuf, os->evsel, unit);
437 if (config->json_output)
438 fprintf(os->fh, "\"unit\" : \"%s\"", unit);
439 else if (config->csv_output)
440 fprintf(os->fh, "%s%s", unit, config->csv_sep);
442 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
445 static int first_shadow_map_idx(struct perf_stat_config *config,
446 struct evsel *evsel, const struct aggr_cpu_id *id)
448 struct perf_cpu_map *cpus = evsel__cpus(evsel);
452 if (config->aggr_mode == AGGR_NONE)
453 return perf_cpu_map__idx(cpus, id->cpu);
455 if (config->aggr_mode == AGGR_THREAD)
456 return id->thread_idx;
458 if (!config->aggr_get_id)
461 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
462 struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
464 if (aggr_cpu_id__equal(&cpu_id, id))
470 static void abs_printout(struct perf_stat_config *config,
471 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
473 FILE *output = config->output;
474 double sc = evsel->scale;
477 if (config->csv_output) {
478 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
481 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
483 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
486 aggr_printout(config, evsel, id, nr);
488 if (config->json_output)
489 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
491 fprintf(output, fmt, avg, config->csv_sep);
493 if (config->json_output) {
495 fprintf(output, "\"unit\" : \"%s\", ",
500 fprintf(output, "%-*s%s",
501 config->csv_output ? 0 : config->unit_width,
502 evsel->unit, config->csv_sep);
505 if (config->json_output)
506 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
508 fprintf(output, "%-*s", config->csv_output ? 0 : 32, evsel__name(evsel));
510 print_cgroup(config, evsel);
513 static bool is_mixed_hw_group(struct evsel *counter)
515 struct evlist *evlist = counter->evlist;
516 u32 pmu_type = counter->core.attr.type;
519 if (counter->core.nr_members < 2)
522 evlist__for_each_entry(evlist, pos) {
523 /* software events can be part of any hardware group */
524 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
526 if (pmu_type == PERF_TYPE_SOFTWARE) {
527 pmu_type = pos->core.attr.type;
530 if (pmu_type != pos->core.attr.type)
537 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
538 struct evsel *counter, double uval,
539 char *prefix, u64 run, u64 ena, double noise,
540 struct runtime_stat *st)
542 struct perf_stat_output_ctx out;
543 struct outstate os = {
544 .fh = config->output,
545 .prefix = prefix ? prefix : "",
553 if (config->csv_output) {
554 static const int aggr_fields[AGGR_MAX] = {
565 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
566 nl = config->metric_only ? new_line_metric : new_line_csv;
567 os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
568 } else if (config->json_output) {
569 pm = config->metric_only ? print_metric_only_json : print_metric_json;
570 nl = config->metric_only ? new_line_metric : new_line_json;
572 pm = config->metric_only ? print_metric_only : print_metric_std;
573 nl = config->metric_only ? new_line_metric : new_line_std;
576 if (!config->no_csv_summary && config->csv_output &&
577 config->summary && !config->interval) {
578 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
581 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
582 if (config->metric_only) {
583 pm(config, &os, NULL, "", "", 0);
586 aggr_printout(config, counter, id, nr);
588 if (config->json_output) {
589 fprintf(config->output, "\"counter-value\" : \"%s\", ",
590 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED);
592 fprintf(config->output, "%*s%s",
593 config->csv_output ? 0 : 18,
594 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
598 if (counter->supported) {
599 if (!evlist__has_hybrid(counter->evlist)) {
600 config->print_free_counters_hint = 1;
601 if (is_mixed_hw_group(counter))
602 config->print_mixed_hw_group_error = 1;
606 if (config->json_output) {
607 fprintf(config->output, "\"unit\" : \"%s\", ", counter->unit);
609 fprintf(config->output, "%-*s%s",
610 config->csv_output ? 0 : config->unit_width,
611 counter->unit, config->csv_sep);
614 if (config->json_output) {
615 fprintf(config->output, "\"event\" : \"%s\", ",
616 evsel__name(counter));
618 fprintf(config->output, "%*s",
619 config->csv_output ? 0 : -25, evsel__name(counter));
622 print_cgroup(config, counter);
624 if (!config->csv_output && !config->json_output)
625 pm(config, &os, NULL, NULL, "", 0);
626 print_noise(config, counter, noise);
627 print_running(config, run, ena);
628 if (config->csv_output)
629 pm(config, &os, NULL, NULL, "", 0);
630 else if (config->json_output)
631 pm(config, &os, NULL, NULL, "", 0);
635 if (!config->metric_only)
636 abs_printout(config, id, nr, counter, uval);
638 out.print_metric = pm;
641 out.force_header = false;
643 if (config->csv_output && !config->metric_only) {
644 print_noise(config, counter, noise);
645 print_running(config, run, ena);
646 } else if (config->json_output && !config->metric_only) {
647 print_noise(config, counter, noise);
648 print_running(config, run, ena);
651 perf_stat__print_shadow_stats(config, counter, uval,
652 first_shadow_map_idx(config, counter, &id),
653 &out, &config->metric_events, st);
654 if (!config->csv_output && !config->metric_only && !config->json_output) {
655 print_noise(config, counter, noise);
656 print_running(config, run, ena);
660 static void aggr_update_shadow(struct perf_stat_config *config,
661 struct evlist *evlist)
665 struct aggr_cpu_id s2, id;
667 struct evsel *counter;
668 struct perf_cpu_map *cpus;
670 for (s = 0; s < config->aggr_map->nr; s++) {
671 id = config->aggr_map->map[s];
672 evlist__for_each_entry(evlist, counter) {
673 cpus = evsel__cpus(counter);
675 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
676 s2 = config->aggr_get_id(config, cpu);
677 if (!aggr_cpu_id__equal(&s2, &id))
679 val += perf_counts(counter->counts, idx, 0)->val;
681 perf_stat__update_shadow_stats(counter, val,
682 first_shadow_map_idx(config, counter, &id),
688 static void uniquify_event_name(struct evsel *counter)
694 if (counter->uniquified_name || counter->use_config_name ||
695 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
696 strlen(counter->pmu_name)))
699 config = strchr(counter->name, '/');
701 if (asprintf(&new_name,
702 "%s%s", counter->pmu_name, config) > 0) {
704 counter->name = new_name;
707 if (evsel__is_hybrid(counter)) {
708 ret = asprintf(&new_name, "%s/%s/",
709 counter->pmu_name, counter->name);
711 ret = asprintf(&new_name, "%s [%s]",
712 counter->name, counter->pmu_name);
717 counter->name = new_name;
721 counter->uniquified_name = true;
724 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
725 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
729 struct evlist *evlist = counter->evlist;
732 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
733 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
734 /* Merge events with the same name, etc. but on different PMUs. */
735 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
736 alias->scale == counter->scale &&
737 alias->cgrp == counter->cgrp &&
738 !strcmp(alias->unit, counter->unit) &&
739 evsel__is_clock(alias) == evsel__is_clock(counter) &&
740 strcmp(alias->pmu_name, counter->pmu_name)) {
741 alias->merged_stat = true;
742 cb(config, alias, data, false);
747 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
750 if (evsel__is_hybrid(counter)) {
752 return config->hybrid_merge;
754 return !config->hybrid_merge;
760 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
761 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
765 if (counter->merged_stat)
767 cb(config, counter, data, true);
768 if (config->no_merge || hybrid_merge(counter, config, false))
769 uniquify_event_name(counter);
770 else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
771 collect_all_aliases(config, counter, cb, data);
777 struct aggr_cpu_id id;
782 static void aggr_cb(struct perf_stat_config *config,
783 struct evsel *counter, void *data, bool first)
785 struct aggr_data *ad = data;
788 struct perf_cpu_map *cpus;
789 struct aggr_cpu_id s2;
791 cpus = evsel__cpus(counter);
792 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
793 struct perf_counts_values *counts;
795 s2 = config->aggr_get_id(config, cpu);
796 if (!aggr_cpu_id__equal(&s2, &ad->id))
800 counts = perf_counts(counter->counts, idx, 0);
802 * When any result is bad, make them all to give
803 * consistent output in interval mode.
805 if (counts->ena == 0 || counts->run == 0 ||
806 counter->counts->scaled == -1) {
811 ad->val += counts->val;
812 ad->ena += counts->ena;
813 ad->run += counts->run;
817 static void print_counter_aggrdata(struct perf_stat_config *config,
818 struct evsel *counter, int s,
819 char *prefix, bool metric_only,
820 bool *first, struct perf_cpu cpu)
823 FILE *output = config->output;
826 struct aggr_cpu_id id;
829 ad.id = id = config->aggr_map->map[s];
830 ad.val = ad.ena = ad.run = 0;
832 if (!collect_data(config, counter, aggr_cb, &ad))
835 if (perf_pmu__has_hybrid() && ad.ena == 0)
842 if (*first && metric_only) {
844 aggr_printout(config, counter, id, nr);
846 if (prefix && !metric_only)
847 fprintf(output, "%s", prefix);
849 uval = val * counter->scale;
851 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
853 printout(config, id, nr, counter, uval,
854 prefix, run, ena, 1.0, &rt_stat);
859 static void print_aggr(struct perf_stat_config *config,
860 struct evlist *evlist,
863 bool metric_only = config->metric_only;
864 FILE *output = config->output;
865 struct evsel *counter;
869 if (!config->aggr_map || !config->aggr_get_id)
872 aggr_update_shadow(config, evlist);
875 * With metric_only everything is on a single line.
876 * Without each counter has its own line.
878 for (s = 0; s < config->aggr_map->nr; s++) {
879 if (prefix && metric_only)
880 fprintf(output, "%s", prefix);
883 evlist__for_each_entry(evlist, counter) {
884 print_counter_aggrdata(config, counter, s,
886 &first, (struct perf_cpu){ .cpu = -1 });
893 static int cmp_val(const void *a, const void *b)
895 return ((struct perf_aggr_thread_value *)b)->val -
896 ((struct perf_aggr_thread_value *)a)->val;
899 static struct perf_aggr_thread_value *sort_aggr_thread(
900 struct evsel *counter,
902 struct target *_target)
904 int nthreads = perf_thread_map__nr(counter->core.threads);
907 struct perf_aggr_thread_value *buf;
909 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
913 for (int thread = 0; thread < nthreads; thread++) {
915 u64 ena = 0, run = 0, val = 0;
917 perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
918 struct perf_counts_values *counts =
919 perf_counts(counter->counts, idx, thread);
926 uval = val * counter->scale;
929 * Skip value 0 when enabling --per-thread globally,
930 * otherwise too many 0 output.
932 if (uval == 0.0 && target__has_per_thread(_target))
935 buf[i].counter = counter;
936 buf[i].id = aggr_cpu_id__empty();
937 buf[i].id.thread_idx = thread;
945 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
953 static void print_aggr_thread(struct perf_stat_config *config,
954 struct target *_target,
955 struct evsel *counter, char *prefix)
957 FILE *output = config->output;
958 int thread, sorted_threads;
959 struct aggr_cpu_id id;
960 struct perf_aggr_thread_value *buf;
962 buf = sort_aggr_thread(counter, &sorted_threads, _target);
964 perror("cannot sort aggr thread");
968 for (thread = 0; thread < sorted_threads; thread++) {
970 fprintf(output, "%s", prefix);
973 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
974 prefix, buf[thread].run, buf[thread].ena, 1.0,
983 double avg, avg_enabled, avg_running;
986 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
987 struct evsel *counter, void *data,
988 bool first __maybe_unused)
990 struct caggr_data *cd = data;
991 struct perf_counts_values *aggr = &counter->counts->aggr;
993 cd->avg += aggr->val;
994 cd->avg_enabled += aggr->ena;
995 cd->avg_running += aggr->run;
999 * Print out the results of a single counter:
1000 * aggregated counts in system-wide mode
1002 static void print_counter_aggr(struct perf_stat_config *config,
1003 struct evsel *counter, char *prefix)
1005 bool metric_only = config->metric_only;
1006 FILE *output = config->output;
1008 struct caggr_data cd = { .avg = 0.0 };
1010 if (!collect_data(config, counter, counter_aggr_cb, &cd))
1013 if (prefix && !metric_only)
1014 fprintf(output, "%s", prefix);
1016 uval = cd.avg * counter->scale;
1017 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
1018 cd.avg_enabled, cd.avg, &rt_stat);
1020 fprintf(output, "\n");
1023 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1024 struct evsel *counter, void *data,
1025 bool first __maybe_unused)
1027 struct aggr_data *ad = data;
1029 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
1030 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
1031 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
1035 * Print out the results of a single counter:
1036 * does not use aggregated count in system-wide
1038 static void print_counter(struct perf_stat_config *config,
1039 struct evsel *counter, char *prefix)
1041 FILE *output = config->output;
1045 struct perf_cpu cpu;
1046 struct aggr_cpu_id id;
1048 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
1049 struct aggr_data ad = { .cpu_map_idx = idx };
1051 if (!collect_data(config, counter, counter_cb, &ad))
1058 fprintf(output, "%s", prefix);
1060 uval = val * counter->scale;
1061 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1062 printout(config, id, 0, counter, uval, prefix,
1063 run, ena, 1.0, &rt_stat);
1065 fputc('\n', output);
1069 static void print_no_aggr_metric(struct perf_stat_config *config,
1070 struct evlist *evlist,
1074 struct perf_cpu cpu;
1076 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1077 struct evsel *counter;
1080 evlist__for_each_entry(evlist, counter) {
1083 struct aggr_cpu_id id;
1084 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1086 if (counter_idx < 0)
1089 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1092 fputs(prefix, config->output);
1093 aggr_printout(config, counter, id, 0);
1096 val = perf_counts(counter->counts, counter_idx, 0)->val;
1097 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
1098 run = perf_counts(counter->counts, counter_idx, 0)->run;
1100 uval = val * counter->scale;
1101 printout(config, id, 0, counter, uval, prefix,
1102 run, ena, 1.0, &rt_stat);
1105 fputc('\n', config->output);
1109 static int aggr_header_lens[] = {
1119 static const char *aggr_header_csv[] = {
1120 [AGGR_CORE] = "core,cpus,",
1121 [AGGR_DIE] = "die,cpus",
1122 [AGGR_SOCKET] = "socket,cpus",
1123 [AGGR_NONE] = "cpu,",
1124 [AGGR_THREAD] = "comm-pid,",
1125 [AGGR_NODE] = "node,",
1129 static void print_metric_headers(struct perf_stat_config *config,
1130 struct evlist *evlist,
1131 const char *prefix, bool no_indent)
1133 struct evsel *counter;
1134 struct outstate os = {
1135 .fh = config->output
1137 struct perf_stat_output_ctx out = {
1139 .print_metric = print_metric_header,
1140 .new_line = new_line_metric,
1141 .force_header = true,
1145 if (config->json_output && !config->interval)
1146 fprintf(config->output, "{");
1148 if (prefix && !config->json_output)
1149 fprintf(config->output, "%s", prefix);
1151 if (!config->csv_output && !no_indent)
1152 fprintf(config->output, "%*s",
1153 aggr_header_lens[config->aggr_mode], "");
1154 if (config->csv_output) {
1155 if (config->interval)
1156 fputs("time,", config->output);
1157 if (!config->iostat_run)
1158 fputs(aggr_header_csv[config->aggr_mode], config->output);
1160 if (config->iostat_run)
1161 iostat_print_header_prefix(config);
1163 /* Print metrics headers only */
1164 evlist__for_each_entry(evlist, counter) {
1167 if (!first && config->json_output)
1168 fprintf(config->output, ", ");
1171 perf_stat__print_shadow_stats(config, counter, 0,
1174 &config->metric_events,
1177 if (config->json_output)
1178 fprintf(config->output, "}");
1179 fputc('\n', config->output);
1182 static void print_interval(struct perf_stat_config *config,
1183 struct evlist *evlist,
1184 char *prefix, struct timespec *ts)
1186 bool metric_only = config->metric_only;
1187 unsigned int unit_width = config->unit_width;
1188 FILE *output = config->output;
1189 static int num_print_interval;
1191 if (config->interval_clear)
1192 puts(CONSOLE_CLEAR);
1194 if (!config->iostat_run && !config->json_output)
1195 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1196 ts->tv_nsec, config->csv_sep);
1197 if (!config->iostat_run && config->json_output && !config->metric_only)
1198 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1199 ts->tv_sec, ts->tv_nsec);
1200 if (!config->iostat_run && config->json_output && config->metric_only)
1201 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1202 ts->tv_sec, ts->tv_nsec);
1204 if ((num_print_interval == 0 && !config->csv_output && !config->json_output)
1205 || config->interval_clear) {
1206 switch (config->aggr_mode) {
1208 fprintf(output, "# time node cpus");
1210 fprintf(output, " counts %*s events\n", unit_width, "unit");
1213 fprintf(output, "# time socket cpus");
1215 fprintf(output, " counts %*s events\n", unit_width, "unit");
1218 fprintf(output, "# time die cpus");
1220 fprintf(output, " counts %*s events\n", unit_width, "unit");
1223 fprintf(output, "# time core cpus");
1225 fprintf(output, " counts %*s events\n", unit_width, "unit");
1228 fprintf(output, "# time CPU ");
1230 fprintf(output, " counts %*s events\n", unit_width, "unit");
1233 fprintf(output, "# time comm-pid");
1235 fprintf(output, " counts %*s events\n", unit_width, "unit");
1239 if (!config->iostat_run) {
1240 fprintf(output, "# time");
1242 fprintf(output, " counts %*s events\n", unit_width, "unit");
1250 if ((num_print_interval == 0 || config->interval_clear)
1251 && metric_only && !config->json_output)
1252 print_metric_headers(config, evlist, " ", true);
1253 if ((num_print_interval == 0 || config->interval_clear)
1254 && metric_only && config->json_output) {
1255 fprintf(output, "{");
1256 print_metric_headers(config, evlist, " ", true);
1258 if (++num_print_interval == 25)
1259 num_print_interval = 0;
1262 static void print_header(struct perf_stat_config *config,
1263 struct target *_target,
1264 int argc, const char **argv)
1266 FILE *output = config->output;
1271 if (!config->csv_output && !config->json_output) {
1272 fprintf(output, "\n");
1273 fprintf(output, " Performance counter stats for ");
1274 if (_target->bpf_str)
1275 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1276 else if (_target->system_wide)
1277 fprintf(output, "\'system wide");
1278 else if (_target->cpu_list)
1279 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1280 else if (!target__has_task(_target)) {
1281 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1282 for (i = 1; argv && (i < argc); i++)
1283 fprintf(output, " %s", argv[i]);
1284 } else if (_target->pid)
1285 fprintf(output, "process id \'%s", _target->pid);
1287 fprintf(output, "thread id \'%s", _target->tid);
1289 fprintf(output, "\'");
1290 if (config->run_count > 1)
1291 fprintf(output, " (%d runs)", config->run_count);
1292 fprintf(output, ":\n\n");
1296 static int get_precision(double num)
1301 return lround(ceil(-log10(num)));
1304 static void print_table(struct perf_stat_config *config,
1305 FILE *output, int precision, double avg)
1308 int idx, indent = 0;
1310 scnprintf(tmp, 64, " %17.*f", precision, avg);
1311 while (tmp[indent] == ' ')
1314 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1316 for (idx = 0; idx < config->run_count; idx++) {
1317 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1318 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1320 fprintf(output, " %17.*f (%+.*f) ",
1321 precision, run, precision, run - avg);
1323 for (h = 0; h < n; h++)
1324 fprintf(output, "#");
1326 fprintf(output, "\n");
1329 fprintf(output, "\n%*s# Final result:\n", indent, "");
1332 static double timeval2double(struct timeval *t)
1334 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1337 static void print_footer(struct perf_stat_config *config)
1339 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1340 FILE *output = config->output;
1342 if (!config->null_run)
1343 fprintf(output, "\n");
1345 if (config->run_count == 1) {
1346 fprintf(output, " %17.9f seconds time elapsed", avg);
1348 if (config->ru_display) {
1349 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1350 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1352 fprintf(output, "\n\n");
1353 fprintf(output, " %17.9f seconds user\n", ru_utime);
1354 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1357 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1359 * Display at most 2 more significant
1360 * digits than the stddev inaccuracy.
1362 int precision = get_precision(sd) + 2;
1364 if (config->walltime_run_table)
1365 print_table(config, output, precision, avg);
1367 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1368 precision, avg, precision, sd);
1370 print_noise_pct(config, sd, avg);
1372 fprintf(output, "\n\n");
1374 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1376 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1377 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1379 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1381 if (config->print_mixed_hw_group_error)
1383 "The events in group usually have to be from "
1384 "the same PMU. Try reorganizing the group.\n");
1387 static void print_percore_thread(struct perf_stat_config *config,
1388 struct evsel *counter, char *prefix)
1391 struct aggr_cpu_id s2, id;
1392 struct perf_cpu_map *cpus;
1395 struct perf_cpu cpu;
1397 cpus = evsel__cpus(counter);
1398 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1399 s2 = config->aggr_get_id(config, cpu);
1400 for (s = 0; s < config->aggr_map->nr; s++) {
1401 id = config->aggr_map->map[s];
1402 if (aggr_cpu_id__equal(&s2, &id))
1406 print_counter_aggrdata(config, counter, s,
1412 static void print_percore(struct perf_stat_config *config,
1413 struct evsel *counter, char *prefix)
1415 bool metric_only = config->metric_only;
1416 FILE *output = config->output;
1420 if (!config->aggr_map || !config->aggr_get_id)
1423 if (config->percore_show_thread)
1424 return print_percore_thread(config, counter, prefix);
1426 for (s = 0; s < config->aggr_map->nr; s++) {
1427 if (prefix && metric_only)
1428 fprintf(output, "%s", prefix);
1430 print_counter_aggrdata(config, counter, s,
1431 prefix, metric_only,
1432 &first, (struct perf_cpu){ .cpu = -1 });
1436 fputc('\n', output);
1439 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1440 struct target *_target, struct timespec *ts, int argc, const char **argv)
1442 bool metric_only = config->metric_only;
1443 int interval = config->interval;
1444 struct evsel *counter;
1445 char buf[64], *prefix = NULL;
1447 if (config->iostat_run)
1448 evlist->selected = evlist__first(evlist);
1451 print_interval(config, evlist, prefix = buf, ts);
1453 print_header(config, _target, argc, argv);
1456 static int num_print_iv;
1458 if (num_print_iv == 0 && !interval)
1459 print_metric_headers(config, evlist, prefix, false);
1460 if (num_print_iv++ == 25)
1462 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1463 fprintf(config->output, "%s", prefix);
1465 if (config->json_output && !config->metric_only)
1466 fprintf(config->output, "}");
1469 switch (config->aggr_mode) {
1474 print_aggr(config, evlist, prefix);
1477 evlist__for_each_entry(evlist, counter) {
1478 print_aggr_thread(config, _target, counter, prefix);
1482 if (config->iostat_run)
1483 iostat_print_counters(evlist, config, ts, prefix = buf,
1484 print_counter_aggr);
1486 evlist__for_each_entry(evlist, counter) {
1487 print_counter_aggr(config, counter, prefix);
1490 fputc('\n', config->output);
1495 print_no_aggr_metric(config, evlist, prefix);
1497 evlist__for_each_entry(evlist, counter) {
1498 if (counter->percore)
1499 print_percore(config, counter, prefix);
1501 print_counter(config, counter, prefix);
1511 if (!interval && !config->csv_output && !config->json_output)
1512 print_footer(config);
1514 fflush(config->output);