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 (perf_pmu__has_hybrid()) {
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 is_uncore(struct evsel *evsel)
749 struct perf_pmu *pmu = evsel__find_pmu(evsel);
751 return pmu && pmu->is_uncore;
754 static bool hybrid_uniquify(struct evsel *evsel)
756 return perf_pmu__has_hybrid() && !is_uncore(evsel);
759 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
762 if (hybrid_uniquify(counter)) {
764 return config && config->hybrid_merge;
766 return config && !config->hybrid_merge;
772 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
773 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
777 if (counter->merged_stat)
779 cb(config, counter, data, true);
780 if (config->no_merge || hybrid_merge(counter, config, false))
781 uniquify_event_name(counter);
782 else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
783 collect_all_aliases(config, counter, cb, data);
789 struct aggr_cpu_id id;
794 static void aggr_cb(struct perf_stat_config *config,
795 struct evsel *counter, void *data, bool first)
797 struct aggr_data *ad = data;
800 struct perf_cpu_map *cpus;
801 struct aggr_cpu_id s2;
803 cpus = evsel__cpus(counter);
804 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
805 struct perf_counts_values *counts;
807 s2 = config->aggr_get_id(config, cpu);
808 if (!aggr_cpu_id__equal(&s2, &ad->id))
812 counts = perf_counts(counter->counts, idx, 0);
814 * When any result is bad, make them all to give
815 * consistent output in interval mode.
817 if (counts->ena == 0 || counts->run == 0 ||
818 counter->counts->scaled == -1) {
823 ad->val += counts->val;
824 ad->ena += counts->ena;
825 ad->run += counts->run;
829 static void print_counter_aggrdata(struct perf_stat_config *config,
830 struct evsel *counter, int s,
831 char *prefix, bool metric_only,
832 bool *first, struct perf_cpu cpu)
835 FILE *output = config->output;
838 struct aggr_cpu_id id;
841 ad.id = id = config->aggr_map->map[s];
842 ad.val = ad.ena = ad.run = 0;
844 if (!collect_data(config, counter, aggr_cb, &ad))
847 if (perf_pmu__has_hybrid() && ad.ena == 0)
854 if (*first && metric_only) {
856 aggr_printout(config, counter, id, nr);
858 if (prefix && !metric_only)
859 fprintf(output, "%s", prefix);
861 uval = val * counter->scale;
863 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
865 printout(config, id, nr, counter, uval,
866 prefix, run, ena, 1.0, &rt_stat);
871 static void print_aggr(struct perf_stat_config *config,
872 struct evlist *evlist,
875 bool metric_only = config->metric_only;
876 FILE *output = config->output;
877 struct evsel *counter;
881 if (!config->aggr_map || !config->aggr_get_id)
884 aggr_update_shadow(config, evlist);
887 * With metric_only everything is on a single line.
888 * Without each counter has its own line.
890 for (s = 0; s < config->aggr_map->nr; s++) {
891 if (prefix && metric_only)
892 fprintf(output, "%s", prefix);
895 evlist__for_each_entry(evlist, counter) {
896 print_counter_aggrdata(config, counter, s,
898 &first, (struct perf_cpu){ .cpu = -1 });
905 static int cmp_val(const void *a, const void *b)
907 return ((struct perf_aggr_thread_value *)b)->val -
908 ((struct perf_aggr_thread_value *)a)->val;
911 static struct perf_aggr_thread_value *sort_aggr_thread(
912 struct evsel *counter,
914 struct target *_target)
916 int nthreads = perf_thread_map__nr(counter->core.threads);
919 struct perf_aggr_thread_value *buf;
921 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
925 for (int thread = 0; thread < nthreads; thread++) {
927 u64 ena = 0, run = 0, val = 0;
929 perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
930 struct perf_counts_values *counts =
931 perf_counts(counter->counts, idx, thread);
938 uval = val * counter->scale;
941 * Skip value 0 when enabling --per-thread globally,
942 * otherwise too many 0 output.
944 if (uval == 0.0 && target__has_per_thread(_target))
947 buf[i].counter = counter;
948 buf[i].id = aggr_cpu_id__empty();
949 buf[i].id.thread_idx = thread;
957 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
965 static void print_aggr_thread(struct perf_stat_config *config,
966 struct target *_target,
967 struct evsel *counter, char *prefix)
969 FILE *output = config->output;
970 int thread, sorted_threads;
971 struct aggr_cpu_id id;
972 struct perf_aggr_thread_value *buf;
974 buf = sort_aggr_thread(counter, &sorted_threads, _target);
976 perror("cannot sort aggr thread");
980 for (thread = 0; thread < sorted_threads; thread++) {
982 fprintf(output, "%s", prefix);
985 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
986 prefix, buf[thread].run, buf[thread].ena, 1.0,
995 double avg, avg_enabled, avg_running;
998 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
999 struct evsel *counter, void *data,
1000 bool first __maybe_unused)
1002 struct caggr_data *cd = data;
1003 struct perf_counts_values *aggr = &counter->counts->aggr;
1005 cd->avg += aggr->val;
1006 cd->avg_enabled += aggr->ena;
1007 cd->avg_running += aggr->run;
1011 * Print out the results of a single counter:
1012 * aggregated counts in system-wide mode
1014 static void print_counter_aggr(struct perf_stat_config *config,
1015 struct evsel *counter, char *prefix)
1017 bool metric_only = config->metric_only;
1018 FILE *output = config->output;
1020 struct caggr_data cd = { .avg = 0.0 };
1022 if (!collect_data(config, counter, counter_aggr_cb, &cd))
1025 if (prefix && !metric_only)
1026 fprintf(output, "%s", prefix);
1028 uval = cd.avg * counter->scale;
1029 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
1030 cd.avg_enabled, cd.avg, &rt_stat);
1032 fprintf(output, "\n");
1035 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1036 struct evsel *counter, void *data,
1037 bool first __maybe_unused)
1039 struct aggr_data *ad = data;
1041 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
1042 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
1043 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
1047 * Print out the results of a single counter:
1048 * does not use aggregated count in system-wide
1050 static void print_counter(struct perf_stat_config *config,
1051 struct evsel *counter, char *prefix)
1053 FILE *output = config->output;
1057 struct perf_cpu cpu;
1058 struct aggr_cpu_id id;
1060 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
1061 struct aggr_data ad = { .cpu_map_idx = idx };
1063 if (!collect_data(config, counter, counter_cb, &ad))
1070 fprintf(output, "%s", prefix);
1072 uval = val * counter->scale;
1073 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1074 printout(config, id, 0, counter, uval, prefix,
1075 run, ena, 1.0, &rt_stat);
1077 fputc('\n', output);
1081 static void print_no_aggr_metric(struct perf_stat_config *config,
1082 struct evlist *evlist,
1086 struct perf_cpu cpu;
1088 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1089 struct evsel *counter;
1092 evlist__for_each_entry(evlist, counter) {
1095 struct aggr_cpu_id id;
1096 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1098 if (counter_idx < 0)
1101 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1104 fputs(prefix, config->output);
1105 aggr_printout(config, counter, id, 0);
1108 val = perf_counts(counter->counts, counter_idx, 0)->val;
1109 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
1110 run = perf_counts(counter->counts, counter_idx, 0)->run;
1112 uval = val * counter->scale;
1113 printout(config, id, 0, counter, uval, prefix,
1114 run, ena, 1.0, &rt_stat);
1117 fputc('\n', config->output);
1121 static int aggr_header_lens[] = {
1131 static const char *aggr_header_csv[] = {
1132 [AGGR_CORE] = "core,cpus,",
1133 [AGGR_DIE] = "die,cpus",
1134 [AGGR_SOCKET] = "socket,cpus",
1135 [AGGR_NONE] = "cpu,",
1136 [AGGR_THREAD] = "comm-pid,",
1137 [AGGR_NODE] = "node,",
1141 static void print_metric_headers(struct perf_stat_config *config,
1142 struct evlist *evlist,
1143 const char *prefix, bool no_indent)
1145 struct perf_stat_output_ctx out;
1146 struct evsel *counter;
1147 struct outstate os = {
1148 .fh = config->output
1152 if (config->json_output && !config->interval)
1153 fprintf(config->output, "{");
1155 if (prefix && !config->json_output)
1156 fprintf(config->output, "%s", prefix);
1158 if (!config->csv_output && !no_indent)
1159 fprintf(config->output, "%*s",
1160 aggr_header_lens[config->aggr_mode], "");
1161 if (config->csv_output) {
1162 if (config->interval)
1163 fputs("time,", config->output);
1164 if (!config->iostat_run)
1165 fputs(aggr_header_csv[config->aggr_mode], config->output);
1167 if (config->iostat_run)
1168 iostat_print_header_prefix(config);
1170 /* Print metrics headers only */
1171 evlist__for_each_entry(evlist, counter) {
1174 out.print_metric = print_metric_header;
1175 if (!first && config->json_output)
1176 fprintf(config->output, ", ");
1178 out.new_line = new_line_metric;
1179 out.force_header = true;
1180 perf_stat__print_shadow_stats(config, counter, 0,
1183 &config->metric_events,
1186 if (config->json_output)
1187 fprintf(config->output, "}");
1188 fputc('\n', config->output);
1191 static void print_interval(struct perf_stat_config *config,
1192 struct evlist *evlist,
1193 char *prefix, struct timespec *ts)
1195 bool metric_only = config->metric_only;
1196 unsigned int unit_width = config->unit_width;
1197 FILE *output = config->output;
1198 static int num_print_interval;
1200 if (config->interval_clear)
1201 puts(CONSOLE_CLEAR);
1203 if (!config->iostat_run && !config->json_output)
1204 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1205 ts->tv_nsec, config->csv_sep);
1206 if (!config->iostat_run && config->json_output && !config->metric_only)
1207 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1208 ts->tv_sec, ts->tv_nsec);
1209 if (!config->iostat_run && config->json_output && config->metric_only)
1210 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1211 ts->tv_sec, ts->tv_nsec);
1213 if ((num_print_interval == 0 && !config->csv_output && !config->json_output)
1214 || config->interval_clear) {
1215 switch (config->aggr_mode) {
1217 fprintf(output, "# time node cpus");
1219 fprintf(output, " counts %*s events\n", unit_width, "unit");
1222 fprintf(output, "# time socket cpus");
1224 fprintf(output, " counts %*s events\n", unit_width, "unit");
1227 fprintf(output, "# time die cpus");
1229 fprintf(output, " counts %*s events\n", unit_width, "unit");
1232 fprintf(output, "# time core cpus");
1234 fprintf(output, " counts %*s events\n", unit_width, "unit");
1237 fprintf(output, "# time CPU ");
1239 fprintf(output, " counts %*s events\n", unit_width, "unit");
1242 fprintf(output, "# time comm-pid");
1244 fprintf(output, " counts %*s events\n", unit_width, "unit");
1248 if (!config->iostat_run) {
1249 fprintf(output, "# time");
1251 fprintf(output, " counts %*s events\n", unit_width, "unit");
1259 if ((num_print_interval == 0 || config->interval_clear)
1260 && metric_only && !config->json_output)
1261 print_metric_headers(config, evlist, " ", true);
1262 if ((num_print_interval == 0 || config->interval_clear)
1263 && metric_only && config->json_output) {
1264 fprintf(output, "{");
1265 print_metric_headers(config, evlist, " ", true);
1267 if (++num_print_interval == 25)
1268 num_print_interval = 0;
1271 static void print_header(struct perf_stat_config *config,
1272 struct target *_target,
1273 int argc, const char **argv)
1275 FILE *output = config->output;
1280 if (!config->csv_output && !config->json_output) {
1281 fprintf(output, "\n");
1282 fprintf(output, " Performance counter stats for ");
1283 if (_target->bpf_str)
1284 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1285 else if (_target->system_wide)
1286 fprintf(output, "\'system wide");
1287 else if (_target->cpu_list)
1288 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1289 else if (!target__has_task(_target)) {
1290 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1291 for (i = 1; argv && (i < argc); i++)
1292 fprintf(output, " %s", argv[i]);
1293 } else if (_target->pid)
1294 fprintf(output, "process id \'%s", _target->pid);
1296 fprintf(output, "thread id \'%s", _target->tid);
1298 fprintf(output, "\'");
1299 if (config->run_count > 1)
1300 fprintf(output, " (%d runs)", config->run_count);
1301 fprintf(output, ":\n\n");
1305 static int get_precision(double num)
1310 return lround(ceil(-log10(num)));
1313 static void print_table(struct perf_stat_config *config,
1314 FILE *output, int precision, double avg)
1317 int idx, indent = 0;
1319 scnprintf(tmp, 64, " %17.*f", precision, avg);
1320 while (tmp[indent] == ' ')
1323 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1325 for (idx = 0; idx < config->run_count; idx++) {
1326 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1327 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1329 fprintf(output, " %17.*f (%+.*f) ",
1330 precision, run, precision, run - avg);
1332 for (h = 0; h < n; h++)
1333 fprintf(output, "#");
1335 fprintf(output, "\n");
1338 fprintf(output, "\n%*s# Final result:\n", indent, "");
1341 static double timeval2double(struct timeval *t)
1343 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1346 static void print_footer(struct perf_stat_config *config)
1348 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1349 FILE *output = config->output;
1351 if (!config->null_run)
1352 fprintf(output, "\n");
1354 if (config->run_count == 1) {
1355 fprintf(output, " %17.9f seconds time elapsed", avg);
1357 if (config->ru_display) {
1358 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1359 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1361 fprintf(output, "\n\n");
1362 fprintf(output, " %17.9f seconds user\n", ru_utime);
1363 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1366 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1368 * Display at most 2 more significant
1369 * digits than the stddev inaccuracy.
1371 int precision = get_precision(sd) + 2;
1373 if (config->walltime_run_table)
1374 print_table(config, output, precision, avg);
1376 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1377 precision, avg, precision, sd);
1379 print_noise_pct(config, sd, avg);
1381 fprintf(output, "\n\n");
1383 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1385 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1386 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1388 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1390 if (config->print_mixed_hw_group_error)
1392 "The events in group usually have to be from "
1393 "the same PMU. Try reorganizing the group.\n");
1396 static void print_percore_thread(struct perf_stat_config *config,
1397 struct evsel *counter, char *prefix)
1400 struct aggr_cpu_id s2, id;
1401 struct perf_cpu_map *cpus;
1404 struct perf_cpu cpu;
1406 cpus = evsel__cpus(counter);
1407 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1408 s2 = config->aggr_get_id(config, cpu);
1409 for (s = 0; s < config->aggr_map->nr; s++) {
1410 id = config->aggr_map->map[s];
1411 if (aggr_cpu_id__equal(&s2, &id))
1415 print_counter_aggrdata(config, counter, s,
1421 static void print_percore(struct perf_stat_config *config,
1422 struct evsel *counter, char *prefix)
1424 bool metric_only = config->metric_only;
1425 FILE *output = config->output;
1429 if (!config->aggr_map || !config->aggr_get_id)
1432 if (config->percore_show_thread)
1433 return print_percore_thread(config, counter, prefix);
1435 for (s = 0; s < config->aggr_map->nr; s++) {
1436 if (prefix && metric_only)
1437 fprintf(output, "%s", prefix);
1439 print_counter_aggrdata(config, counter, s,
1440 prefix, metric_only,
1441 &first, (struct perf_cpu){ .cpu = -1 });
1445 fputc('\n', output);
1448 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1449 struct target *_target, struct timespec *ts, int argc, const char **argv)
1451 bool metric_only = config->metric_only;
1452 int interval = config->interval;
1453 struct evsel *counter;
1454 char buf[64], *prefix = NULL;
1456 if (config->iostat_run)
1457 evlist->selected = evlist__first(evlist);
1460 print_interval(config, evlist, prefix = buf, ts);
1462 print_header(config, _target, argc, argv);
1465 static int num_print_iv;
1467 if (num_print_iv == 0 && !interval)
1468 print_metric_headers(config, evlist, prefix, false);
1469 if (num_print_iv++ == 25)
1471 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1472 fprintf(config->output, "%s", prefix);
1474 if (config->json_output && !config->metric_only)
1475 fprintf(config->output, "}");
1478 switch (config->aggr_mode) {
1483 print_aggr(config, evlist, prefix);
1486 evlist__for_each_entry(evlist, counter) {
1487 print_aggr_thread(config, _target, counter, prefix);
1491 if (config->iostat_run)
1492 iostat_print_counters(evlist, config, ts, prefix = buf,
1493 print_counter_aggr);
1495 evlist__for_each_entry(evlist, counter) {
1496 print_counter_aggr(config, counter, prefix);
1499 fputc('\n', config->output);
1504 print_no_aggr_metric(config, evlist, prefix);
1506 evlist__for_each_entry(evlist, counter) {
1507 if (counter->percore)
1508 print_percore(config, counter, prefix);
1510 print_counter(config, counter, prefix);
1520 if (!interval && !config->csv_output && !config->json_output)
1521 print_footer(config);
1523 fflush(config->output);