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, int id, int nr)
73 switch (config->aggr_mode) {
75 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
76 cpu_map__id_to_socket(id),
77 cpu_map__id_to_die(id),
78 config->csv_output ? 0 : -8,
79 cpu_map__id_to_cpu(id),
81 config->csv_output ? 0 : 4,
86 fprintf(config->output, "S%d-D%*d%s%*d%s",
87 cpu_map__id_to_socket(id << 16),
88 config->csv_output ? 0 : -8,
89 cpu_map__id_to_die(id << 16),
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",
116 cpu_map__id_to_socket(id),
117 cpu_map__id_to_die(id),
118 config->csv_output ? 0 : -3,
119 cpu_map__id_to_cpu(id), config->csv_sep);
120 } else if (id > -1) {
121 fprintf(config->output, "CPU%*d%s",
122 config->csv_output ? 0 : -7,
123 evsel__cpus(evsel)->map[id],
128 fprintf(config->output, "%*s-%*d%s",
129 config->csv_output ? 0 : 16,
130 perf_thread_map__comm(evsel->core.threads, id),
131 config->csv_output ? 0 : -8,
132 perf_thread_map__pid(evsel->core.threads, id),
151 #define METRIC_LEN 35
153 static void new_line_std(struct perf_stat_config *config __maybe_unused,
156 struct outstate *os = ctx;
161 static void do_new_line_std(struct perf_stat_config *config,
165 fputs(os->prefix, os->fh);
166 aggr_printout(config, os->evsel, os->id, os->nr);
167 if (config->aggr_mode == AGGR_NONE)
168 fprintf(os->fh, " ");
169 fprintf(os->fh, " ");
172 static void print_metric_std(struct perf_stat_config *config,
173 void *ctx, const char *color, const char *fmt,
174 const char *unit, double val)
176 struct outstate *os = ctx;
179 bool newline = os->newline;
183 if (unit == NULL || fmt == NULL) {
184 fprintf(out, "%-*s", METRIC_LEN, "");
189 do_new_line_std(config, os);
191 n = fprintf(out, " # ");
193 n += color_fprintf(out, color, fmt, val);
195 n += fprintf(out, fmt, val);
196 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
199 static void new_line_csv(struct perf_stat_config *config, void *ctx)
201 struct outstate *os = ctx;
206 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
207 aggr_printout(config, os->evsel, os->id, os->nr);
208 for (i = 0; i < os->nfields; i++)
209 fputs(config->csv_sep, os->fh);
212 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
214 const char *color __maybe_unused,
215 const char *fmt, const char *unit, double val)
217 struct outstate *os = ctx;
219 char buf[64], *vals, *ends;
221 if (unit == NULL || fmt == NULL) {
222 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
225 snprintf(buf, sizeof(buf), fmt, val);
226 ends = vals = skip_spaces(buf);
227 while (isdigit(*ends) || *ends == '.')
230 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
233 /* Filter out some columns that don't work well in metrics only mode */
235 static bool valid_only_metric(const char *unit)
239 if (strstr(unit, "/sec") ||
240 strstr(unit, "CPUs utilized"))
245 static const char *fixunit(char *buf, struct evsel *evsel,
248 if (!strncmp(unit, "of all", 6)) {
249 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
256 static void print_metric_only(struct perf_stat_config *config,
257 void *ctx, const char *color, const char *fmt,
258 const char *unit, double val)
260 struct outstate *os = ctx;
262 char buf[1024], str[1024];
263 unsigned mlen = config->metric_only_len;
265 if (!valid_only_metric(unit))
267 unit = fixunit(buf, os->evsel, unit);
268 if (mlen < strlen(unit))
269 mlen = strlen(unit) + 1;
272 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
274 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
275 fprintf(out, "%*s ", mlen, str);
278 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
279 void *ctx, const char *color __maybe_unused,
281 const char *unit, double val)
283 struct outstate *os = ctx;
285 char buf[64], *vals, *ends;
288 if (!valid_only_metric(unit))
290 unit = fixunit(tbuf, os->evsel, unit);
291 snprintf(buf, sizeof buf, fmt, val);
292 ends = vals = skip_spaces(buf);
293 while (isdigit(*ends) || *ends == '.')
296 fprintf(out, "%s%s", vals, config->csv_sep);
299 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
300 void *ctx __maybe_unused)
304 static void print_metric_header(struct perf_stat_config *config,
305 void *ctx, const char *color __maybe_unused,
306 const char *fmt __maybe_unused,
307 const char *unit, double val __maybe_unused)
309 struct outstate *os = ctx;
312 if (!valid_only_metric(unit))
314 unit = fixunit(tbuf, os->evsel, unit);
315 if (config->csv_output)
316 fprintf(os->fh, "%s%s", unit, config->csv_sep);
318 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
321 static int first_shadow_cpu(struct perf_stat_config *config,
322 struct evsel *evsel, int id)
324 struct evlist *evlist = evsel->evlist;
327 if (config->aggr_mode == AGGR_NONE)
330 if (!config->aggr_get_id)
333 for (i = 0; i < evsel__nr_cpus(evsel); i++) {
334 int cpu2 = evsel__cpus(evsel)->map[i];
336 if (config->aggr_get_id(config, evlist->core.cpus, cpu2) == id)
342 static void abs_printout(struct perf_stat_config *config,
343 int id, int nr, struct evsel *evsel, double avg)
345 FILE *output = config->output;
346 double sc = evsel->scale;
349 if (config->csv_output) {
350 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
353 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
355 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
358 aggr_printout(config, evsel, id, nr);
360 fprintf(output, fmt, avg, config->csv_sep);
363 fprintf(output, "%-*s%s",
364 config->csv_output ? 0 : config->unit_width,
365 evsel->unit, config->csv_sep);
367 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
369 print_cgroup(config, evsel);
372 static bool is_mixed_hw_group(struct evsel *counter)
374 struct evlist *evlist = counter->evlist;
375 u32 pmu_type = counter->core.attr.type;
378 if (counter->core.nr_members < 2)
381 evlist__for_each_entry(evlist, pos) {
382 /* software events can be part of any hardware group */
383 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
385 if (pmu_type == PERF_TYPE_SOFTWARE) {
386 pmu_type = pos->core.attr.type;
389 if (pmu_type != pos->core.attr.type)
396 static void printout(struct perf_stat_config *config, int id, int nr,
397 struct evsel *counter, double uval,
398 char *prefix, u64 run, u64 ena, double noise,
399 struct runtime_stat *st)
401 struct perf_stat_output_ctx out;
402 struct outstate os = {
403 .fh = config->output,
404 .prefix = prefix ? prefix : "",
409 print_metric_t pm = print_metric_std;
412 if (config->metric_only) {
413 nl = new_line_metric;
414 if (config->csv_output)
415 pm = print_metric_only_csv;
417 pm = print_metric_only;
421 if (config->csv_output && !config->metric_only) {
422 static int aggr_fields[] = {
431 pm = print_metric_csv;
434 os.nfields += aggr_fields[config->aggr_mode];
438 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
439 if (config->metric_only) {
440 pm(config, &os, NULL, "", "", 0);
443 aggr_printout(config, counter, id, nr);
445 fprintf(config->output, "%*s%s",
446 config->csv_output ? 0 : 18,
447 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
450 if (counter->supported) {
451 config->print_free_counters_hint = 1;
452 if (is_mixed_hw_group(counter))
453 config->print_mixed_hw_group_error = 1;
456 fprintf(config->output, "%-*s%s",
457 config->csv_output ? 0 : config->unit_width,
458 counter->unit, config->csv_sep);
460 fprintf(config->output, "%*s",
461 config->csv_output ? 0 : -25, evsel__name(counter));
463 print_cgroup(config, counter);
465 if (!config->csv_output)
466 pm(config, &os, NULL, NULL, "", 0);
467 print_noise(config, counter, noise);
468 print_running(config, run, ena);
469 if (config->csv_output)
470 pm(config, &os, NULL, NULL, "", 0);
474 if (!config->metric_only)
475 abs_printout(config, id, nr, counter, uval);
477 out.print_metric = pm;
480 out.force_header = false;
482 if (config->csv_output && !config->metric_only) {
483 print_noise(config, counter, noise);
484 print_running(config, run, ena);
487 perf_stat__print_shadow_stats(config, counter, uval,
488 first_shadow_cpu(config, counter, id),
489 &out, &config->metric_events, st);
490 if (!config->csv_output && !config->metric_only) {
491 print_noise(config, counter, noise);
492 print_running(config, run, ena);
496 static void aggr_update_shadow(struct perf_stat_config *config,
497 struct evlist *evlist)
501 struct evsel *counter;
503 for (s = 0; s < config->aggr_map->nr; s++) {
504 id = config->aggr_map->map[s];
505 evlist__for_each_entry(evlist, counter) {
507 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
508 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
511 val += perf_counts(counter->counts, cpu, 0)->val;
513 perf_stat__update_shadow_stats(counter, val,
514 first_shadow_cpu(config, counter, id),
520 static void uniquify_event_name(struct evsel *counter)
525 if (counter->uniquified_name ||
526 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
527 strlen(counter->pmu_name)))
530 config = strchr(counter->name, '/');
532 if (asprintf(&new_name,
533 "%s%s", counter->pmu_name, config) > 0) {
535 counter->name = new_name;
538 if (asprintf(&new_name,
539 "%s [%s]", counter->name, counter->pmu_name) > 0) {
541 counter->name = new_name;
545 counter->uniquified_name = true;
548 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
549 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
553 struct evlist *evlist = counter->evlist;
556 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
557 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
558 if (strcmp(evsel__name(alias), evsel__name(counter)) ||
559 alias->scale != counter->scale ||
560 alias->cgrp != counter->cgrp ||
561 strcmp(alias->unit, counter->unit) ||
562 evsel__is_clock(alias) != evsel__is_clock(counter) ||
563 !strcmp(alias->pmu_name, counter->pmu_name))
565 alias->merged_stat = true;
566 cb(config, alias, data, false);
570 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
571 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
575 if (counter->merged_stat)
577 cb(config, counter, data, true);
578 if (config->no_merge)
579 uniquify_event_name(counter);
580 else if (counter->auto_merge_stats)
581 collect_all_aliases(config, counter, cb, data);
592 static void aggr_cb(struct perf_stat_config *config,
593 struct evsel *counter, void *data, bool first)
595 struct aggr_data *ad = data;
598 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
599 struct perf_counts_values *counts;
601 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
606 counts = perf_counts(counter->counts, cpu, 0);
608 * When any result is bad, make them all to give
609 * consistent output in interval mode.
611 if (counts->ena == 0 || counts->run == 0 ||
612 counter->counts->scaled == -1) {
617 ad->val += counts->val;
618 ad->ena += counts->ena;
619 ad->run += counts->run;
623 static void print_counter_aggrdata(struct perf_stat_config *config,
624 struct evsel *counter, int s,
625 char *prefix, bool metric_only,
626 bool *first, int cpu)
629 FILE *output = config->output;
634 ad.id = id = config->aggr_map->map[s];
635 ad.val = ad.ena = ad.run = 0;
637 if (!collect_data(config, counter, aggr_cb, &ad))
644 if (*first && metric_only) {
646 aggr_printout(config, counter, id, nr);
648 if (prefix && !metric_only)
649 fprintf(output, "%s", prefix);
651 uval = val * counter->scale;
652 printout(config, cpu != -1 ? cpu : id, nr, counter, uval, prefix,
653 run, ena, 1.0, &rt_stat);
658 static void print_aggr(struct perf_stat_config *config,
659 struct evlist *evlist,
662 bool metric_only = config->metric_only;
663 FILE *output = config->output;
664 struct evsel *counter;
668 if (!config->aggr_map || !config->aggr_get_id)
671 aggr_update_shadow(config, evlist);
674 * With metric_only everything is on a single line.
675 * Without each counter has its own line.
677 for (s = 0; s < config->aggr_map->nr; s++) {
678 if (prefix && metric_only)
679 fprintf(output, "%s", prefix);
682 evlist__for_each_entry(evlist, counter) {
683 print_counter_aggrdata(config, counter, s,
692 static int cmp_val(const void *a, const void *b)
694 return ((struct perf_aggr_thread_value *)b)->val -
695 ((struct perf_aggr_thread_value *)a)->val;
698 static struct perf_aggr_thread_value *sort_aggr_thread(
699 struct evsel *counter,
700 int nthreads, int ncpus,
702 struct target *_target)
704 int cpu, thread, i = 0;
706 struct perf_aggr_thread_value *buf;
708 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
712 for (thread = 0; thread < nthreads; thread++) {
713 u64 ena = 0, run = 0, val = 0;
715 for (cpu = 0; cpu < ncpus; cpu++) {
716 val += perf_counts(counter->counts, cpu, thread)->val;
717 ena += perf_counts(counter->counts, cpu, thread)->ena;
718 run += perf_counts(counter->counts, cpu, thread)->run;
721 uval = val * counter->scale;
724 * Skip value 0 when enabling --per-thread globally,
725 * otherwise too many 0 output.
727 if (uval == 0.0 && target__has_per_thread(_target))
730 buf[i].counter = counter;
739 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
747 static void print_aggr_thread(struct perf_stat_config *config,
748 struct target *_target,
749 struct evsel *counter, char *prefix)
751 FILE *output = config->output;
752 int nthreads = perf_thread_map__nr(counter->core.threads);
753 int ncpus = perf_cpu_map__nr(counter->core.cpus);
754 int thread, sorted_threads, id;
755 struct perf_aggr_thread_value *buf;
757 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
759 perror("cannot sort aggr thread");
763 for (thread = 0; thread < sorted_threads; thread++) {
765 fprintf(output, "%s", prefix);
769 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
770 prefix, buf[thread].run, buf[thread].ena, 1.0,
773 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
774 prefix, buf[thread].run, buf[thread].ena, 1.0,
783 double avg, avg_enabled, avg_running;
786 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
787 struct evsel *counter, void *data,
788 bool first __maybe_unused)
790 struct caggr_data *cd = data;
791 struct perf_stat_evsel *ps = counter->stats;
793 cd->avg += avg_stats(&ps->res_stats[0]);
794 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
795 cd->avg_running += avg_stats(&ps->res_stats[2]);
799 * Print out the results of a single counter:
800 * aggregated counts in system-wide mode
802 static void print_counter_aggr(struct perf_stat_config *config,
803 struct evsel *counter, char *prefix)
805 bool metric_only = config->metric_only;
806 FILE *output = config->output;
808 struct caggr_data cd = { .avg = 0.0 };
810 if (!collect_data(config, counter, counter_aggr_cb, &cd))
813 if (prefix && !metric_only)
814 fprintf(output, "%s", prefix);
816 uval = cd.avg * counter->scale;
817 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
820 fprintf(output, "\n");
823 static void counter_cb(struct perf_stat_config *config __maybe_unused,
824 struct evsel *counter, void *data,
825 bool first __maybe_unused)
827 struct aggr_data *ad = data;
829 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
830 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
831 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
835 * Print out the results of a single counter:
836 * does not use aggregated count in system-wide
838 static void print_counter(struct perf_stat_config *config,
839 struct evsel *counter, char *prefix)
841 FILE *output = config->output;
846 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
847 struct aggr_data ad = { .cpu = cpu };
849 if (!collect_data(config, counter, counter_cb, &ad))
856 fprintf(output, "%s", prefix);
858 uval = val * counter->scale;
859 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
866 static void print_no_aggr_metric(struct perf_stat_config *config,
867 struct evlist *evlist,
872 struct evsel *counter;
876 nrcpus = evlist->core.cpus->nr;
877 for (cpu = 0; cpu < nrcpus; cpu++) {
881 fputs(prefix, config->output);
882 evlist__for_each_entry(evlist, counter) {
884 aggr_printout(config, counter, cpu, 0);
887 val = perf_counts(counter->counts, cpu, 0)->val;
888 ena = perf_counts(counter->counts, cpu, 0)->ena;
889 run = perf_counts(counter->counts, cpu, 0)->run;
891 uval = val * counter->scale;
892 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
895 fputc('\n', config->output);
899 static int aggr_header_lens[] = {
908 static const char *aggr_header_csv[] = {
909 [AGGR_CORE] = "core,cpus,",
910 [AGGR_DIE] = "die,cpus",
911 [AGGR_SOCKET] = "socket,cpus",
912 [AGGR_NONE] = "cpu,",
913 [AGGR_THREAD] = "comm-pid,",
917 static void print_metric_headers(struct perf_stat_config *config,
918 struct evlist *evlist,
919 const char *prefix, bool no_indent)
921 struct perf_stat_output_ctx out;
922 struct evsel *counter;
923 struct outstate os = {
928 fprintf(config->output, "%s", prefix);
930 if (!config->csv_output && !no_indent)
931 fprintf(config->output, "%*s",
932 aggr_header_lens[config->aggr_mode], "");
933 if (config->csv_output) {
934 if (config->interval)
935 fputs("time,", config->output);
936 fputs(aggr_header_csv[config->aggr_mode], config->output);
939 /* Print metrics headers only */
940 evlist__for_each_entry(evlist, counter) {
943 out.print_metric = print_metric_header;
944 out.new_line = new_line_metric;
945 out.force_header = true;
946 perf_stat__print_shadow_stats(config, counter, 0,
949 &config->metric_events,
952 fputc('\n', config->output);
955 static void print_interval(struct perf_stat_config *config,
956 struct evlist *evlist,
957 char *prefix, struct timespec *ts)
959 bool metric_only = config->metric_only;
960 unsigned int unit_width = config->unit_width;
961 FILE *output = config->output;
962 static int num_print_interval;
964 if (config->interval_clear)
967 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
969 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
970 switch (config->aggr_mode) {
972 fprintf(output, "# time node cpus");
974 fprintf(output, " counts %*s events\n", unit_width, "unit");
977 fprintf(output, "# time socket cpus");
979 fprintf(output, " counts %*s events\n", unit_width, "unit");
982 fprintf(output, "# time die cpus");
984 fprintf(output, " counts %*s events\n", unit_width, "unit");
987 fprintf(output, "# time core cpus");
989 fprintf(output, " counts %*s events\n", unit_width, "unit");
992 fprintf(output, "# time CPU ");
994 fprintf(output, " counts %*s events\n", unit_width, "unit");
997 fprintf(output, "# time comm-pid");
999 fprintf(output, " counts %*s events\n", unit_width, "unit");
1003 fprintf(output, "# time");
1005 fprintf(output, " counts %*s events\n", unit_width, "unit");
1011 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1012 print_metric_headers(config, evlist, " ", true);
1013 if (++num_print_interval == 25)
1014 num_print_interval = 0;
1017 static void print_header(struct perf_stat_config *config,
1018 struct target *_target,
1019 int argc, const char **argv)
1021 FILE *output = config->output;
1026 if (!config->csv_output) {
1027 fprintf(output, "\n");
1028 fprintf(output, " Performance counter stats for ");
1029 if (_target->system_wide)
1030 fprintf(output, "\'system wide");
1031 else if (_target->cpu_list)
1032 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1033 else if (!target__has_task(_target)) {
1034 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1035 for (i = 1; argv && (i < argc); i++)
1036 fprintf(output, " %s", argv[i]);
1037 } else if (_target->pid)
1038 fprintf(output, "process id \'%s", _target->pid);
1040 fprintf(output, "thread id \'%s", _target->tid);
1042 fprintf(output, "\'");
1043 if (config->run_count > 1)
1044 fprintf(output, " (%d runs)", config->run_count);
1045 fprintf(output, ":\n\n");
1049 static int get_precision(double num)
1054 return lround(ceil(-log10(num)));
1057 static void print_table(struct perf_stat_config *config,
1058 FILE *output, int precision, double avg)
1061 int idx, indent = 0;
1063 scnprintf(tmp, 64, " %17.*f", precision, avg);
1064 while (tmp[indent] == ' ')
1067 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1069 for (idx = 0; idx < config->run_count; idx++) {
1070 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1071 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1073 fprintf(output, " %17.*f (%+.*f) ",
1074 precision, run, precision, run - avg);
1076 for (h = 0; h < n; h++)
1077 fprintf(output, "#");
1079 fprintf(output, "\n");
1082 fprintf(output, "\n%*s# Final result:\n", indent, "");
1085 static double timeval2double(struct timeval *t)
1087 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1090 static void print_footer(struct perf_stat_config *config)
1092 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1093 FILE *output = config->output;
1095 if (!config->null_run)
1096 fprintf(output, "\n");
1098 if (config->run_count == 1) {
1099 fprintf(output, " %17.9f seconds time elapsed", avg);
1101 if (config->ru_display) {
1102 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1103 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1105 fprintf(output, "\n\n");
1106 fprintf(output, " %17.9f seconds user\n", ru_utime);
1107 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1110 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1112 * Display at most 2 more significant
1113 * digits than the stddev inaccuracy.
1115 int precision = get_precision(sd) + 2;
1117 if (config->walltime_run_table)
1118 print_table(config, output, precision, avg);
1120 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1121 precision, avg, precision, sd);
1123 print_noise_pct(config, sd, avg);
1125 fprintf(output, "\n\n");
1127 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1129 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1130 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1132 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1134 if (config->print_mixed_hw_group_error)
1136 "The events in group usually have to be from "
1137 "the same PMU. Try reorganizing the group.\n");
1140 static void print_percore_thread(struct perf_stat_config *config,
1141 struct evsel *counter, char *prefix)
1146 for (int i = 0; i < evsel__nr_cpus(counter); i++) {
1147 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1148 for (s = 0; s < config->aggr_map->nr; s++) {
1149 id = config->aggr_map->map[s];
1154 print_counter_aggrdata(config, counter, s,
1160 static void print_percore(struct perf_stat_config *config,
1161 struct evsel *counter, char *prefix)
1163 bool metric_only = config->metric_only;
1164 FILE *output = config->output;
1168 if (!config->aggr_map || !config->aggr_get_id)
1171 if (config->percore_show_thread)
1172 return print_percore_thread(config, counter, prefix);
1174 for (s = 0; s < config->aggr_map->nr; s++) {
1175 if (prefix && metric_only)
1176 fprintf(output, "%s", prefix);
1178 print_counter_aggrdata(config, counter, s,
1179 prefix, metric_only,
1184 fputc('\n', output);
1188 perf_evlist__print_counters(struct evlist *evlist,
1189 struct perf_stat_config *config,
1190 struct target *_target,
1191 struct timespec *ts,
1192 int argc, const char **argv)
1194 bool metric_only = config->metric_only;
1195 int interval = config->interval;
1196 struct evsel *counter;
1197 char buf[64], *prefix = NULL;
1200 print_interval(config, evlist, prefix = buf, ts);
1202 print_header(config, _target, argc, argv);
1205 static int num_print_iv;
1207 if (num_print_iv == 0 && !interval)
1208 print_metric_headers(config, evlist, prefix, false);
1209 if (num_print_iv++ == 25)
1211 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1212 fprintf(config->output, "%s", prefix);
1215 switch (config->aggr_mode) {
1220 print_aggr(config, evlist, prefix);
1223 evlist__for_each_entry(evlist, counter) {
1224 print_aggr_thread(config, _target, counter, prefix);
1228 evlist__for_each_entry(evlist, counter) {
1229 print_counter_aggr(config, counter, prefix);
1232 fputc('\n', config->output);
1236 print_no_aggr_metric(config, evlist, prefix);
1238 evlist__for_each_entry(evlist, counter) {
1239 if (counter->percore)
1240 print_percore(config, counter, prefix);
1242 print_counter(config, counter, prefix);
1251 if (!interval && !config->csv_output)
1252 print_footer(config);
1254 fflush(config->output);