4 #include <linux/string.h>
5 #include <linux/time64.h>
13 #include "thread_map.h"
16 #include <linux/ctype.h>
18 #include <api/fs/fs.h>
21 #define CNTR_NOT_SUPPORTED "<not supported>"
22 #define CNTR_NOT_COUNTED "<not counted>"
24 static void print_running(struct perf_stat_config *config,
27 if (config->csv_output) {
28 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
32 ena ? 100.0 * run / ena : 100.0);
33 } else if (run != ena) {
34 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
38 static void print_noise_pct(struct perf_stat_config *config,
39 double total, double avg)
41 double pct = rel_stddev_stats(total, avg);
43 if (config->csv_output)
44 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
46 fprintf(config->output, " ( +-%6.2f%% )", pct);
49 static void print_noise(struct perf_stat_config *config,
50 struct evsel *evsel, double avg)
52 struct perf_stat_evsel *ps;
54 if (config->run_count == 1)
58 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
61 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
64 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
65 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
70 static void aggr_printout(struct perf_stat_config *config,
71 struct evsel *evsel, struct aggr_cpu_id id, int nr)
73 switch (config->aggr_mode) {
75 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
78 config->csv_output ? 0 : -8,
81 config->csv_output ? 0 : 4,
86 fprintf(config->output, "S%d-D%*d%s%*d%s",
88 config->csv_output ? 0 : -8,
91 config->csv_output ? 0 : 4,
96 fprintf(config->output, "S%*d%s%*d%s",
97 config->csv_output ? 0 : -5,
100 config->csv_output ? 0 : 4,
105 fprintf(config->output, "N%*d%s%*d%s",
106 config->csv_output ? 0 : -5,
109 config->csv_output ? 0 : 4,
114 if (evsel->percore && !config->percore_show_thread) {
115 fprintf(config->output, "S%d-D%d-C%*d%s",
118 config->csv_output ? 0 : -3,
119 id.core, config->csv_sep);
120 } else if (id.core > -1) {
121 fprintf(config->output, "CPU%*d%s",
122 config->csv_output ? 0 : -7,
123 evsel__cpus(evsel)->map[id.core],
128 fprintf(config->output, "%*s-%*d%s",
129 config->csv_output ? 0 : 16,
130 perf_thread_map__comm(evsel->core.threads, id.thread),
131 config->csv_output ? 0 : -8,
132 perf_thread_map__pid(evsel->core.threads, id.thread),
148 struct aggr_cpu_id id;
152 #define METRIC_LEN 35
154 static void new_line_std(struct perf_stat_config *config __maybe_unused,
157 struct outstate *os = ctx;
162 static void do_new_line_std(struct perf_stat_config *config,
166 fputs(os->prefix, os->fh);
167 aggr_printout(config, os->evsel, os->id, os->nr);
168 if (config->aggr_mode == AGGR_NONE)
169 fprintf(os->fh, " ");
170 fprintf(os->fh, " ");
173 static void print_metric_std(struct perf_stat_config *config,
174 void *ctx, const char *color, const char *fmt,
175 const char *unit, double val)
177 struct outstate *os = ctx;
180 bool newline = os->newline;
184 if (unit == NULL || fmt == NULL) {
185 fprintf(out, "%-*s", METRIC_LEN, "");
190 do_new_line_std(config, os);
192 n = fprintf(out, " # ");
194 n += color_fprintf(out, color, fmt, val);
196 n += fprintf(out, fmt, val);
197 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
200 static void new_line_csv(struct perf_stat_config *config, void *ctx)
202 struct outstate *os = ctx;
207 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
208 aggr_printout(config, os->evsel, os->id, os->nr);
209 for (i = 0; i < os->nfields; i++)
210 fputs(config->csv_sep, os->fh);
213 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
215 const char *color __maybe_unused,
216 const char *fmt, const char *unit, double val)
218 struct outstate *os = ctx;
220 char buf[64], *vals, *ends;
222 if (unit == NULL || fmt == NULL) {
223 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
226 snprintf(buf, sizeof(buf), fmt, val);
227 ends = vals = skip_spaces(buf);
228 while (isdigit(*ends) || *ends == '.')
231 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
234 /* Filter out some columns that don't work well in metrics only mode */
236 static bool valid_only_metric(const char *unit)
240 if (strstr(unit, "/sec") ||
241 strstr(unit, "CPUs utilized"))
246 static const char *fixunit(char *buf, struct evsel *evsel,
249 if (!strncmp(unit, "of all", 6)) {
250 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
257 static void print_metric_only(struct perf_stat_config *config,
258 void *ctx, const char *color, const char *fmt,
259 const char *unit, double val)
261 struct outstate *os = ctx;
263 char buf[1024], str[1024];
264 unsigned mlen = config->metric_only_len;
266 if (!valid_only_metric(unit))
268 unit = fixunit(buf, os->evsel, unit);
269 if (mlen < strlen(unit))
270 mlen = strlen(unit) + 1;
273 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
275 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
276 fprintf(out, "%*s ", mlen, str);
279 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
280 void *ctx, const char *color __maybe_unused,
282 const char *unit, double val)
284 struct outstate *os = ctx;
286 char buf[64], *vals, *ends;
289 if (!valid_only_metric(unit))
291 unit = fixunit(tbuf, os->evsel, unit);
292 snprintf(buf, sizeof buf, fmt, val);
293 ends = vals = skip_spaces(buf);
294 while (isdigit(*ends) || *ends == '.')
297 fprintf(out, "%s%s", vals, config->csv_sep);
300 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
301 void *ctx __maybe_unused)
305 static void print_metric_header(struct perf_stat_config *config,
306 void *ctx, const char *color __maybe_unused,
307 const char *fmt __maybe_unused,
308 const char *unit, double val __maybe_unused)
310 struct outstate *os = ctx;
313 if (!valid_only_metric(unit))
315 unit = fixunit(tbuf, os->evsel, unit);
316 if (config->csv_output)
317 fprintf(os->fh, "%s%s", unit, config->csv_sep);
319 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
322 static int first_shadow_cpu(struct perf_stat_config *config,
323 struct evsel *evsel, struct aggr_cpu_id id)
325 struct evlist *evlist = evsel->evlist;
328 if (config->aggr_mode == AGGR_NONE)
331 if (!config->aggr_get_id)
334 for (i = 0; i < evsel__nr_cpus(evsel); i++) {
335 int cpu2 = evsel__cpus(evsel)->map[i];
337 if (cpu_map__compare_aggr_cpu_id(
338 config->aggr_get_id(config, evlist->core.cpus, cpu2),
346 static void abs_printout(struct perf_stat_config *config,
347 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
349 FILE *output = config->output;
350 double sc = evsel->scale;
353 if (config->csv_output) {
354 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
357 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
359 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
362 aggr_printout(config, evsel, id, nr);
364 fprintf(output, fmt, avg, config->csv_sep);
367 fprintf(output, "%-*s%s",
368 config->csv_output ? 0 : config->unit_width,
369 evsel->unit, config->csv_sep);
371 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
373 print_cgroup(config, evsel);
376 static bool is_mixed_hw_group(struct evsel *counter)
378 struct evlist *evlist = counter->evlist;
379 u32 pmu_type = counter->core.attr.type;
382 if (counter->core.nr_members < 2)
385 evlist__for_each_entry(evlist, pos) {
386 /* software events can be part of any hardware group */
387 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
389 if (pmu_type == PERF_TYPE_SOFTWARE) {
390 pmu_type = pos->core.attr.type;
393 if (pmu_type != pos->core.attr.type)
400 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
401 struct evsel *counter, double uval,
402 char *prefix, u64 run, u64 ena, double noise,
403 struct runtime_stat *st)
405 struct perf_stat_output_ctx out;
406 struct outstate os = {
407 .fh = config->output,
408 .prefix = prefix ? prefix : "",
413 print_metric_t pm = print_metric_std;
416 if (config->metric_only) {
417 nl = new_line_metric;
418 if (config->csv_output)
419 pm = print_metric_only_csv;
421 pm = print_metric_only;
425 if (config->csv_output && !config->metric_only) {
426 static int aggr_fields[] = {
435 pm = print_metric_csv;
438 os.nfields += aggr_fields[config->aggr_mode];
442 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
443 if (config->metric_only) {
444 pm(config, &os, NULL, "", "", 0);
447 aggr_printout(config, counter, id, nr);
449 fprintf(config->output, "%*s%s",
450 config->csv_output ? 0 : 18,
451 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
454 if (counter->supported) {
455 config->print_free_counters_hint = 1;
456 if (is_mixed_hw_group(counter))
457 config->print_mixed_hw_group_error = 1;
460 fprintf(config->output, "%-*s%s",
461 config->csv_output ? 0 : config->unit_width,
462 counter->unit, config->csv_sep);
464 fprintf(config->output, "%*s",
465 config->csv_output ? 0 : -25, evsel__name(counter));
467 print_cgroup(config, counter);
469 if (!config->csv_output)
470 pm(config, &os, NULL, NULL, "", 0);
471 print_noise(config, counter, noise);
472 print_running(config, run, ena);
473 if (config->csv_output)
474 pm(config, &os, NULL, NULL, "", 0);
478 if (!config->metric_only)
479 abs_printout(config, id, nr, counter, uval);
481 out.print_metric = pm;
484 out.force_header = false;
486 if (config->csv_output && !config->metric_only) {
487 print_noise(config, counter, noise);
488 print_running(config, run, ena);
491 perf_stat__print_shadow_stats(config, counter, uval,
492 first_shadow_cpu(config, counter, id),
493 &out, &config->metric_events, st);
494 if (!config->csv_output && !config->metric_only) {
495 print_noise(config, counter, noise);
496 print_running(config, run, ena);
500 static void aggr_update_shadow(struct perf_stat_config *config,
501 struct evlist *evlist)
504 struct aggr_cpu_id s2, id;
506 struct evsel *counter;
508 for (s = 0; s < config->aggr_map->nr; s++) {
509 id = config->aggr_map->map[s];
510 evlist__for_each_entry(evlist, counter) {
512 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
513 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
514 if (!cpu_map__compare_aggr_cpu_id(s2, id))
516 val += perf_counts(counter->counts, cpu, 0)->val;
518 perf_stat__update_shadow_stats(counter, val,
519 first_shadow_cpu(config, counter, id),
525 static void uniquify_event_name(struct evsel *counter)
530 if (counter->uniquified_name ||
531 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
532 strlen(counter->pmu_name)))
535 config = strchr(counter->name, '/');
537 if (asprintf(&new_name,
538 "%s%s", counter->pmu_name, config) > 0) {
540 counter->name = new_name;
543 if (asprintf(&new_name,
544 "%s [%s]", counter->name, counter->pmu_name) > 0) {
546 counter->name = new_name;
550 counter->uniquified_name = true;
553 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
554 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
558 struct evlist *evlist = counter->evlist;
561 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
562 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
563 if (strcmp(evsel__name(alias), evsel__name(counter)) ||
564 alias->scale != counter->scale ||
565 alias->cgrp != counter->cgrp ||
566 strcmp(alias->unit, counter->unit) ||
567 evsel__is_clock(alias) != evsel__is_clock(counter) ||
568 !strcmp(alias->pmu_name, counter->pmu_name))
570 alias->merged_stat = true;
571 cb(config, alias, data, false);
575 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
576 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
580 if (counter->merged_stat)
582 cb(config, counter, data, true);
583 if (config->no_merge)
584 uniquify_event_name(counter);
585 else if (counter->auto_merge_stats)
586 collect_all_aliases(config, counter, cb, data);
592 struct aggr_cpu_id id;
597 static void aggr_cb(struct perf_stat_config *config,
598 struct evsel *counter, void *data, bool first)
600 struct aggr_data *ad = data;
602 struct aggr_cpu_id s2;
604 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
605 struct perf_counts_values *counts;
607 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
608 if (!cpu_map__compare_aggr_cpu_id(s2, ad->id))
612 counts = perf_counts(counter->counts, cpu, 0);
614 * When any result is bad, make them all to give
615 * consistent output in interval mode.
617 if (counts->ena == 0 || counts->run == 0 ||
618 counter->counts->scaled == -1) {
623 ad->val += counts->val;
624 ad->ena += counts->ena;
625 ad->run += counts->run;
629 static void print_counter_aggrdata(struct perf_stat_config *config,
630 struct evsel *counter, int s,
631 char *prefix, bool metric_only,
632 bool *first, int cpu)
635 FILE *output = config->output;
638 struct aggr_cpu_id id;
641 ad.id = id = config->aggr_map->map[s];
642 ad.val = ad.ena = ad.run = 0;
644 if (!collect_data(config, counter, aggr_cb, &ad))
651 if (*first && metric_only) {
653 aggr_printout(config, counter, id, nr);
655 if (prefix && !metric_only)
656 fprintf(output, "%s", prefix);
658 uval = val * counter->scale;
660 id = cpu_map__empty_aggr_cpu_id();
663 printout(config, id, nr, counter, uval,
664 prefix, run, ena, 1.0, &rt_stat);
669 static void print_aggr(struct perf_stat_config *config,
670 struct evlist *evlist,
673 bool metric_only = config->metric_only;
674 FILE *output = config->output;
675 struct evsel *counter;
679 if (!config->aggr_map || !config->aggr_get_id)
682 aggr_update_shadow(config, evlist);
685 * With metric_only everything is on a single line.
686 * Without each counter has its own line.
688 for (s = 0; s < config->aggr_map->nr; s++) {
689 if (prefix && metric_only)
690 fprintf(output, "%s", prefix);
693 evlist__for_each_entry(evlist, counter) {
694 print_counter_aggrdata(config, counter, s,
703 static int cmp_val(const void *a, const void *b)
705 return ((struct perf_aggr_thread_value *)b)->val -
706 ((struct perf_aggr_thread_value *)a)->val;
709 static struct perf_aggr_thread_value *sort_aggr_thread(
710 struct evsel *counter,
711 int nthreads, int ncpus,
713 struct target *_target)
715 int cpu, thread, i = 0;
717 struct perf_aggr_thread_value *buf;
719 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
723 for (thread = 0; thread < nthreads; thread++) {
724 u64 ena = 0, run = 0, val = 0;
726 for (cpu = 0; cpu < ncpus; cpu++) {
727 val += perf_counts(counter->counts, cpu, thread)->val;
728 ena += perf_counts(counter->counts, cpu, thread)->ena;
729 run += perf_counts(counter->counts, cpu, thread)->run;
732 uval = val * counter->scale;
735 * Skip value 0 when enabling --per-thread globally,
736 * otherwise too many 0 output.
738 if (uval == 0.0 && target__has_per_thread(_target))
741 buf[i].counter = counter;
742 buf[i].id = cpu_map__empty_aggr_cpu_id();
743 buf[i].id.thread = thread;
751 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
759 static void print_aggr_thread(struct perf_stat_config *config,
760 struct target *_target,
761 struct evsel *counter, char *prefix)
763 FILE *output = config->output;
764 int nthreads = perf_thread_map__nr(counter->core.threads);
765 int ncpus = perf_cpu_map__nr(counter->core.cpus);
766 int thread, sorted_threads;
767 struct aggr_cpu_id id;
768 struct perf_aggr_thread_value *buf;
770 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
772 perror("cannot sort aggr thread");
776 for (thread = 0; thread < sorted_threads; thread++) {
778 fprintf(output, "%s", prefix);
782 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
783 prefix, buf[thread].run, buf[thread].ena, 1.0,
784 &config->stats[id.thread]);
786 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
787 prefix, buf[thread].run, buf[thread].ena, 1.0,
796 double avg, avg_enabled, avg_running;
799 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
800 struct evsel *counter, void *data,
801 bool first __maybe_unused)
803 struct caggr_data *cd = data;
804 struct perf_stat_evsel *ps = counter->stats;
806 cd->avg += avg_stats(&ps->res_stats[0]);
807 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
808 cd->avg_running += avg_stats(&ps->res_stats[2]);
812 * Print out the results of a single counter:
813 * aggregated counts in system-wide mode
815 static void print_counter_aggr(struct perf_stat_config *config,
816 struct evsel *counter, char *prefix)
818 bool metric_only = config->metric_only;
819 FILE *output = config->output;
821 struct caggr_data cd = { .avg = 0.0 };
823 if (!collect_data(config, counter, counter_aggr_cb, &cd))
826 if (prefix && !metric_only)
827 fprintf(output, "%s", prefix);
829 uval = cd.avg * counter->scale;
830 printout(config, cpu_map__empty_aggr_cpu_id(), 0, counter, uval, prefix, cd.avg_running,
831 cd.avg_enabled, cd.avg, &rt_stat);
833 fprintf(output, "\n");
836 static void counter_cb(struct perf_stat_config *config __maybe_unused,
837 struct evsel *counter, void *data,
838 bool first __maybe_unused)
840 struct aggr_data *ad = data;
842 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
843 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
844 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
848 * Print out the results of a single counter:
849 * does not use aggregated count in system-wide
851 static void print_counter(struct perf_stat_config *config,
852 struct evsel *counter, char *prefix)
854 FILE *output = config->output;
858 struct aggr_cpu_id id;
860 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
861 struct aggr_data ad = { .cpu = cpu };
863 if (!collect_data(config, counter, counter_cb, &ad))
870 fprintf(output, "%s", prefix);
872 uval = val * counter->scale;
873 id = cpu_map__empty_aggr_cpu_id();
875 printout(config, id, 0, counter, uval, prefix,
876 run, ena, 1.0, &rt_stat);
882 static void print_no_aggr_metric(struct perf_stat_config *config,
883 struct evlist *evlist,
888 struct evsel *counter;
891 struct aggr_cpu_id id;
893 nrcpus = evlist->core.cpus->nr;
894 for (cpu = 0; cpu < nrcpus; cpu++) {
898 fputs(prefix, config->output);
899 evlist__for_each_entry(evlist, counter) {
900 id = cpu_map__empty_aggr_cpu_id();
903 aggr_printout(config, counter, id, 0);
906 val = perf_counts(counter->counts, cpu, 0)->val;
907 ena = perf_counts(counter->counts, cpu, 0)->ena;
908 run = perf_counts(counter->counts, cpu, 0)->run;
910 uval = val * counter->scale;
911 printout(config, id, 0, counter, uval, prefix,
912 run, ena, 1.0, &rt_stat);
914 fputc('\n', config->output);
918 static int aggr_header_lens[] = {
927 static const char *aggr_header_csv[] = {
928 [AGGR_CORE] = "core,cpus,",
929 [AGGR_DIE] = "die,cpus",
930 [AGGR_SOCKET] = "socket,cpus",
931 [AGGR_NONE] = "cpu,",
932 [AGGR_THREAD] = "comm-pid,",
936 static void print_metric_headers(struct perf_stat_config *config,
937 struct evlist *evlist,
938 const char *prefix, bool no_indent)
940 struct perf_stat_output_ctx out;
941 struct evsel *counter;
942 struct outstate os = {
947 fprintf(config->output, "%s", prefix);
949 if (!config->csv_output && !no_indent)
950 fprintf(config->output, "%*s",
951 aggr_header_lens[config->aggr_mode], "");
952 if (config->csv_output) {
953 if (config->interval)
954 fputs("time,", config->output);
955 fputs(aggr_header_csv[config->aggr_mode], config->output);
958 /* Print metrics headers only */
959 evlist__for_each_entry(evlist, counter) {
962 out.print_metric = print_metric_header;
963 out.new_line = new_line_metric;
964 out.force_header = true;
965 perf_stat__print_shadow_stats(config, counter, 0,
968 &config->metric_events,
971 fputc('\n', config->output);
974 static void print_interval(struct perf_stat_config *config,
975 struct evlist *evlist,
976 char *prefix, struct timespec *ts)
978 bool metric_only = config->metric_only;
979 unsigned int unit_width = config->unit_width;
980 FILE *output = config->output;
981 static int num_print_interval;
983 if (config->interval_clear)
986 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
988 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
989 switch (config->aggr_mode) {
991 fprintf(output, "# time node cpus");
993 fprintf(output, " counts %*s events\n", unit_width, "unit");
996 fprintf(output, "# time socket cpus");
998 fprintf(output, " counts %*s events\n", unit_width, "unit");
1001 fprintf(output, "# time die cpus");
1003 fprintf(output, " counts %*s events\n", unit_width, "unit");
1006 fprintf(output, "# time core cpus");
1008 fprintf(output, " counts %*s events\n", unit_width, "unit");
1011 fprintf(output, "# time CPU ");
1013 fprintf(output, " counts %*s events\n", unit_width, "unit");
1016 fprintf(output, "# time comm-pid");
1018 fprintf(output, " counts %*s events\n", unit_width, "unit");
1022 fprintf(output, "# time");
1024 fprintf(output, " counts %*s events\n", unit_width, "unit");
1030 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1031 print_metric_headers(config, evlist, " ", true);
1032 if (++num_print_interval == 25)
1033 num_print_interval = 0;
1036 static void print_header(struct perf_stat_config *config,
1037 struct target *_target,
1038 int argc, const char **argv)
1040 FILE *output = config->output;
1045 if (!config->csv_output) {
1046 fprintf(output, "\n");
1047 fprintf(output, " Performance counter stats for ");
1048 if (_target->bpf_str)
1049 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1050 else if (_target->system_wide)
1051 fprintf(output, "\'system wide");
1052 else if (_target->cpu_list)
1053 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1054 else if (!target__has_task(_target)) {
1055 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1056 for (i = 1; argv && (i < argc); i++)
1057 fprintf(output, " %s", argv[i]);
1058 } else if (_target->pid)
1059 fprintf(output, "process id \'%s", _target->pid);
1061 fprintf(output, "thread id \'%s", _target->tid);
1063 fprintf(output, "\'");
1064 if (config->run_count > 1)
1065 fprintf(output, " (%d runs)", config->run_count);
1066 fprintf(output, ":\n\n");
1070 static int get_precision(double num)
1075 return lround(ceil(-log10(num)));
1078 static void print_table(struct perf_stat_config *config,
1079 FILE *output, int precision, double avg)
1082 int idx, indent = 0;
1084 scnprintf(tmp, 64, " %17.*f", precision, avg);
1085 while (tmp[indent] == ' ')
1088 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1090 for (idx = 0; idx < config->run_count; idx++) {
1091 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1092 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1094 fprintf(output, " %17.*f (%+.*f) ",
1095 precision, run, precision, run - avg);
1097 for (h = 0; h < n; h++)
1098 fprintf(output, "#");
1100 fprintf(output, "\n");
1103 fprintf(output, "\n%*s# Final result:\n", indent, "");
1106 static double timeval2double(struct timeval *t)
1108 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1111 static void print_footer(struct perf_stat_config *config)
1113 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1114 FILE *output = config->output;
1116 if (!config->null_run)
1117 fprintf(output, "\n");
1119 if (config->run_count == 1) {
1120 fprintf(output, " %17.9f seconds time elapsed", avg);
1122 if (config->ru_display) {
1123 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1124 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1126 fprintf(output, "\n\n");
1127 fprintf(output, " %17.9f seconds user\n", ru_utime);
1128 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1131 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1133 * Display at most 2 more significant
1134 * digits than the stddev inaccuracy.
1136 int precision = get_precision(sd) + 2;
1138 if (config->walltime_run_table)
1139 print_table(config, output, precision, avg);
1141 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1142 precision, avg, precision, sd);
1144 print_noise_pct(config, sd, avg);
1146 fprintf(output, "\n\n");
1148 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1150 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1151 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1153 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1155 if (config->print_mixed_hw_group_error)
1157 "The events in group usually have to be from "
1158 "the same PMU. Try reorganizing the group.\n");
1161 static void print_percore_thread(struct perf_stat_config *config,
1162 struct evsel *counter, char *prefix)
1165 struct aggr_cpu_id s2, id;
1168 for (int i = 0; i < evsel__nr_cpus(counter); i++) {
1169 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1170 for (s = 0; s < config->aggr_map->nr; s++) {
1171 id = config->aggr_map->map[s];
1172 if (cpu_map__compare_aggr_cpu_id(s2, id))
1176 print_counter_aggrdata(config, counter, s,
1182 static void print_percore(struct perf_stat_config *config,
1183 struct evsel *counter, char *prefix)
1185 bool metric_only = config->metric_only;
1186 FILE *output = config->output;
1190 if (!config->aggr_map || !config->aggr_get_id)
1193 if (config->percore_show_thread)
1194 return print_percore_thread(config, counter, prefix);
1196 for (s = 0; s < config->aggr_map->nr; s++) {
1197 if (prefix && metric_only)
1198 fprintf(output, "%s", prefix);
1200 print_counter_aggrdata(config, counter, s,
1201 prefix, metric_only,
1206 fputc('\n', output);
1209 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1210 struct target *_target, struct timespec *ts, int argc, const char **argv)
1212 bool metric_only = config->metric_only;
1213 int interval = config->interval;
1214 struct evsel *counter;
1215 char buf[64], *prefix = NULL;
1218 print_interval(config, evlist, prefix = buf, ts);
1220 print_header(config, _target, argc, argv);
1223 static int num_print_iv;
1225 if (num_print_iv == 0 && !interval)
1226 print_metric_headers(config, evlist, prefix, false);
1227 if (num_print_iv++ == 25)
1229 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1230 fprintf(config->output, "%s", prefix);
1233 switch (config->aggr_mode) {
1238 print_aggr(config, evlist, prefix);
1241 evlist__for_each_entry(evlist, counter) {
1242 print_aggr_thread(config, _target, counter, prefix);
1246 evlist__for_each_entry(evlist, counter) {
1247 print_counter_aggr(config, counter, prefix);
1250 fputc('\n', config->output);
1254 print_no_aggr_metric(config, evlist, prefix);
1256 evlist__for_each_entry(evlist, counter) {
1257 if (counter->percore)
1258 print_percore(config, counter, prefix);
1260 print_counter(config, counter, prefix);
1269 if (!interval && !config->csv_output)
1270 print_footer(config);
1272 fflush(config->output);