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 : -5,
119 cpu_map__id_to_cpu(id), config->csv_sep);
121 fprintf(config->output, "CPU%*d%s ",
122 config->csv_output ? 0 : -5,
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, "hz") ||
241 strstr(unit, "Hz") ||
242 strstr(unit, "CPUs utilized"))
247 static const char *fixunit(char *buf, struct evsel *evsel,
250 if (!strncmp(unit, "of all", 6)) {
251 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
258 static void print_metric_only(struct perf_stat_config *config,
259 void *ctx, const char *color, const char *fmt,
260 const char *unit, double val)
262 struct outstate *os = ctx;
264 char buf[1024], str[1024];
265 unsigned mlen = config->metric_only_len;
267 if (!valid_only_metric(unit))
269 unit = fixunit(buf, os->evsel, unit);
270 if (mlen < strlen(unit))
271 mlen = strlen(unit) + 1;
274 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
276 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
277 fprintf(out, "%*s ", mlen, str);
280 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
281 void *ctx, const char *color __maybe_unused,
283 const char *unit, double val)
285 struct outstate *os = ctx;
287 char buf[64], *vals, *ends;
290 if (!valid_only_metric(unit))
292 unit = fixunit(tbuf, os->evsel, unit);
293 snprintf(buf, sizeof buf, fmt, val);
294 ends = vals = skip_spaces(buf);
295 while (isdigit(*ends) || *ends == '.')
298 fprintf(out, "%s%s", vals, config->csv_sep);
301 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
302 void *ctx __maybe_unused)
306 static void print_metric_header(struct perf_stat_config *config,
307 void *ctx, const char *color __maybe_unused,
308 const char *fmt __maybe_unused,
309 const char *unit, double val __maybe_unused)
311 struct outstate *os = ctx;
314 if (!valid_only_metric(unit))
316 unit = fixunit(tbuf, os->evsel, unit);
317 if (config->csv_output)
318 fprintf(os->fh, "%s%s", unit, config->csv_sep);
320 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
323 static int first_shadow_cpu(struct perf_stat_config *config,
324 struct evsel *evsel, int id)
326 struct evlist *evlist = evsel->evlist;
329 if (!config->aggr_get_id)
332 if (config->aggr_mode == AGGR_NONE)
335 if (config->aggr_mode == AGGR_GLOBAL)
338 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
339 int cpu2 = evsel__cpus(evsel)->map[i];
341 if (config->aggr_get_id(config, evlist->core.cpus, cpu2) == id)
347 static void abs_printout(struct perf_stat_config *config,
348 int id, int nr, struct evsel *evsel, double avg)
350 FILE *output = config->output;
351 double sc = evsel->scale;
354 if (config->csv_output) {
355 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
358 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
360 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
363 aggr_printout(config, evsel, id, nr);
365 fprintf(output, fmt, avg, config->csv_sep);
368 fprintf(output, "%-*s%s",
369 config->csv_output ? 0 : config->unit_width,
370 evsel->unit, config->csv_sep);
372 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
374 print_cgroup(config, evsel);
377 static bool is_mixed_hw_group(struct evsel *counter)
379 struct evlist *evlist = counter->evlist;
380 u32 pmu_type = counter->core.attr.type;
383 if (counter->core.nr_members < 2)
386 evlist__for_each_entry(evlist, pos) {
387 /* software events can be part of any hardware group */
388 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
390 if (pmu_type == PERF_TYPE_SOFTWARE) {
391 pmu_type = pos->core.attr.type;
394 if (pmu_type != pos->core.attr.type)
401 static void printout(struct perf_stat_config *config, int id, int nr,
402 struct evsel *counter, double uval,
403 char *prefix, u64 run, u64 ena, double noise,
404 struct runtime_stat *st)
406 struct perf_stat_output_ctx out;
407 struct outstate os = {
408 .fh = config->output,
409 .prefix = prefix ? prefix : "",
414 print_metric_t pm = print_metric_std;
417 if (config->metric_only) {
418 nl = new_line_metric;
419 if (config->csv_output)
420 pm = print_metric_only_csv;
422 pm = print_metric_only;
426 if (config->csv_output && !config->metric_only) {
427 static int aggr_fields[] = {
436 pm = print_metric_csv;
439 os.nfields += aggr_fields[config->aggr_mode];
443 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
444 if (config->metric_only) {
445 pm(config, &os, NULL, "", "", 0);
448 aggr_printout(config, counter, id, nr);
450 fprintf(config->output, "%*s%s",
451 config->csv_output ? 0 : 18,
452 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
455 if (counter->supported) {
456 config->print_free_counters_hint = 1;
457 if (is_mixed_hw_group(counter))
458 config->print_mixed_hw_group_error = 1;
461 fprintf(config->output, "%-*s%s",
462 config->csv_output ? 0 : config->unit_width,
463 counter->unit, config->csv_sep);
465 fprintf(config->output, "%*s",
466 config->csv_output ? 0 : -25,
467 perf_evsel__name(counter));
469 print_cgroup(config, counter);
471 if (!config->csv_output)
472 pm(config, &os, NULL, NULL, "", 0);
473 print_noise(config, counter, noise);
474 print_running(config, run, ena);
475 if (config->csv_output)
476 pm(config, &os, NULL, NULL, "", 0);
480 if (!config->metric_only)
481 abs_printout(config, id, nr, counter, uval);
483 out.print_metric = pm;
486 out.force_header = false;
488 if (config->csv_output && !config->metric_only) {
489 print_noise(config, counter, noise);
490 print_running(config, run, ena);
493 perf_stat__print_shadow_stats(config, counter, uval,
494 first_shadow_cpu(config, counter, id),
495 &out, &config->metric_events, st);
496 if (!config->csv_output && !config->metric_only) {
497 print_noise(config, counter, noise);
498 print_running(config, run, ena);
502 static void aggr_update_shadow(struct perf_stat_config *config,
503 struct evlist *evlist)
507 struct evsel *counter;
509 for (s = 0; s < config->aggr_map->nr; s++) {
510 id = config->aggr_map->map[s];
511 evlist__for_each_entry(evlist, counter) {
513 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
514 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
517 val += perf_counts(counter->counts, cpu, 0)->val;
519 perf_stat__update_shadow_stats(counter, val,
520 first_shadow_cpu(config, counter, id),
526 static void uniquify_event_name(struct evsel *counter)
531 if (counter->uniquified_name ||
532 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
533 strlen(counter->pmu_name)))
536 config = strchr(counter->name, '/');
538 if (asprintf(&new_name,
539 "%s%s", counter->pmu_name, config) > 0) {
541 counter->name = new_name;
544 if (asprintf(&new_name,
545 "%s [%s]", counter->name, counter->pmu_name) > 0) {
547 counter->name = new_name;
551 counter->uniquified_name = true;
554 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
555 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
559 struct evlist *evlist = counter->evlist;
562 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
563 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
564 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
565 alias->scale != counter->scale ||
566 alias->cgrp != counter->cgrp ||
567 strcmp(alias->unit, counter->unit) ||
568 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter) ||
569 !strcmp(alias->pmu_name, counter->pmu_name))
571 alias->merged_stat = true;
572 cb(config, alias, data, false);
576 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
577 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
581 if (counter->merged_stat)
583 cb(config, counter, data, true);
584 if (config->no_merge)
585 uniquify_event_name(counter);
586 else if (counter->auto_merge_stats)
587 collect_all_aliases(config, counter, cb, data);
598 static void aggr_cb(struct perf_stat_config *config,
599 struct evsel *counter, void *data, bool first)
601 struct aggr_data *ad = data;
604 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
605 struct perf_counts_values *counts;
607 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
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;
640 ad.id = id = config->aggr_map->map[s];
641 ad.val = ad.ena = ad.run = 0;
643 if (!collect_data(config, counter, aggr_cb, &ad))
650 if (*first && metric_only) {
652 aggr_printout(config, counter, id, nr);
654 if (prefix && !metric_only)
655 fprintf(output, "%s", prefix);
657 uval = val * counter->scale;
658 printout(config, cpu != -1 ? cpu : id, nr, counter, uval, prefix,
659 run, ena, 1.0, &rt_stat);
664 static void print_aggr(struct perf_stat_config *config,
665 struct evlist *evlist,
668 bool metric_only = config->metric_only;
669 FILE *output = config->output;
670 struct evsel *counter;
674 if (!(config->aggr_map || config->aggr_get_id))
677 aggr_update_shadow(config, evlist);
680 * With metric_only everything is on a single line.
681 * Without each counter has its own line.
683 for (s = 0; s < config->aggr_map->nr; s++) {
684 if (prefix && metric_only)
685 fprintf(output, "%s", prefix);
688 evlist__for_each_entry(evlist, counter) {
689 print_counter_aggrdata(config, counter, s,
698 static int cmp_val(const void *a, const void *b)
700 return ((struct perf_aggr_thread_value *)b)->val -
701 ((struct perf_aggr_thread_value *)a)->val;
704 static struct perf_aggr_thread_value *sort_aggr_thread(
705 struct evsel *counter,
706 int nthreads, int ncpus,
708 struct target *_target)
710 int cpu, thread, i = 0;
712 struct perf_aggr_thread_value *buf;
714 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
718 for (thread = 0; thread < nthreads; thread++) {
719 u64 ena = 0, run = 0, val = 0;
721 for (cpu = 0; cpu < ncpus; cpu++) {
722 val += perf_counts(counter->counts, cpu, thread)->val;
723 ena += perf_counts(counter->counts, cpu, thread)->ena;
724 run += perf_counts(counter->counts, cpu, thread)->run;
727 uval = val * counter->scale;
730 * Skip value 0 when enabling --per-thread globally,
731 * otherwise too many 0 output.
733 if (uval == 0.0 && target__has_per_thread(_target))
736 buf[i].counter = counter;
745 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
753 static void print_aggr_thread(struct perf_stat_config *config,
754 struct target *_target,
755 struct evsel *counter, char *prefix)
757 FILE *output = config->output;
758 int nthreads = perf_thread_map__nr(counter->core.threads);
759 int ncpus = perf_cpu_map__nr(counter->core.cpus);
760 int thread, sorted_threads, id;
761 struct perf_aggr_thread_value *buf;
763 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
765 perror("cannot sort aggr thread");
769 for (thread = 0; thread < sorted_threads; thread++) {
771 fprintf(output, "%s", prefix);
775 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
776 prefix, buf[thread].run, buf[thread].ena, 1.0,
779 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
780 prefix, buf[thread].run, buf[thread].ena, 1.0,
789 double avg, avg_enabled, avg_running;
792 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
793 struct evsel *counter, void *data,
794 bool first __maybe_unused)
796 struct caggr_data *cd = data;
797 struct perf_stat_evsel *ps = counter->stats;
799 cd->avg += avg_stats(&ps->res_stats[0]);
800 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
801 cd->avg_running += avg_stats(&ps->res_stats[2]);
805 * Print out the results of a single counter:
806 * aggregated counts in system-wide mode
808 static void print_counter_aggr(struct perf_stat_config *config,
809 struct evsel *counter, char *prefix)
811 bool metric_only = config->metric_only;
812 FILE *output = config->output;
814 struct caggr_data cd = { .avg = 0.0 };
816 if (!collect_data(config, counter, counter_aggr_cb, &cd))
819 if (prefix && !metric_only)
820 fprintf(output, "%s", prefix);
822 uval = cd.avg * counter->scale;
823 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
826 fprintf(output, "\n");
829 static void counter_cb(struct perf_stat_config *config __maybe_unused,
830 struct evsel *counter, void *data,
831 bool first __maybe_unused)
833 struct aggr_data *ad = data;
835 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
836 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
837 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
841 * Print out the results of a single counter:
842 * does not use aggregated count in system-wide
844 static void print_counter(struct perf_stat_config *config,
845 struct evsel *counter, char *prefix)
847 FILE *output = config->output;
852 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
853 struct aggr_data ad = { .cpu = cpu };
855 if (!collect_data(config, counter, counter_cb, &ad))
862 fprintf(output, "%s", prefix);
864 uval = val * counter->scale;
865 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
872 static void print_no_aggr_metric(struct perf_stat_config *config,
873 struct evlist *evlist,
878 struct evsel *counter;
882 nrcpus = evlist->core.cpus->nr;
883 for (cpu = 0; cpu < nrcpus; cpu++) {
887 fputs(prefix, config->output);
888 evlist__for_each_entry(evlist, counter) {
890 aggr_printout(config, counter, cpu, 0);
893 val = perf_counts(counter->counts, cpu, 0)->val;
894 ena = perf_counts(counter->counts, cpu, 0)->ena;
895 run = perf_counts(counter->counts, cpu, 0)->run;
897 uval = val * counter->scale;
898 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
901 fputc('\n', config->output);
905 static int aggr_header_lens[] = {
914 static const char *aggr_header_csv[] = {
915 [AGGR_CORE] = "core,cpus,",
916 [AGGR_DIE] = "die,cpus",
917 [AGGR_SOCKET] = "socket,cpus",
918 [AGGR_NONE] = "cpu,",
919 [AGGR_THREAD] = "comm-pid,",
923 static void print_metric_headers(struct perf_stat_config *config,
924 struct evlist *evlist,
925 const char *prefix, bool no_indent)
927 struct perf_stat_output_ctx out;
928 struct evsel *counter;
929 struct outstate os = {
934 fprintf(config->output, "%s", prefix);
936 if (!config->csv_output && !no_indent)
937 fprintf(config->output, "%*s",
938 aggr_header_lens[config->aggr_mode], "");
939 if (config->csv_output) {
940 if (config->interval)
941 fputs("time,", config->output);
942 fputs(aggr_header_csv[config->aggr_mode], config->output);
945 /* Print metrics headers only */
946 evlist__for_each_entry(evlist, counter) {
949 out.print_metric = print_metric_header;
950 out.new_line = new_line_metric;
951 out.force_header = true;
953 perf_stat__print_shadow_stats(config, counter, 0,
956 &config->metric_events,
959 fputc('\n', config->output);
962 static void print_interval(struct perf_stat_config *config,
963 struct evlist *evlist,
964 char *prefix, struct timespec *ts)
966 bool metric_only = config->metric_only;
967 unsigned int unit_width = config->unit_width;
968 FILE *output = config->output;
969 static int num_print_interval;
971 if (config->interval_clear)
974 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
976 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
977 switch (config->aggr_mode) {
979 fprintf(output, "# time node cpus");
981 fprintf(output, " counts %*s events\n", unit_width, "unit");
984 fprintf(output, "# time socket cpus");
986 fprintf(output, " counts %*s events\n", unit_width, "unit");
989 fprintf(output, "# time die cpus");
991 fprintf(output, " counts %*s events\n", unit_width, "unit");
994 fprintf(output, "# time core cpus");
996 fprintf(output, " counts %*s events\n", unit_width, "unit");
999 fprintf(output, "# time CPU ");
1001 fprintf(output, " counts %*s events\n", unit_width, "unit");
1004 fprintf(output, "# time comm-pid");
1006 fprintf(output, " counts %*s events\n", unit_width, "unit");
1010 fprintf(output, "# time");
1012 fprintf(output, " counts %*s events\n", unit_width, "unit");
1018 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1019 print_metric_headers(config, evlist, " ", true);
1020 if (++num_print_interval == 25)
1021 num_print_interval = 0;
1024 static void print_header(struct perf_stat_config *config,
1025 struct target *_target,
1026 int argc, const char **argv)
1028 FILE *output = config->output;
1033 if (!config->csv_output) {
1034 fprintf(output, "\n");
1035 fprintf(output, " Performance counter stats for ");
1036 if (_target->system_wide)
1037 fprintf(output, "\'system wide");
1038 else if (_target->cpu_list)
1039 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1040 else if (!target__has_task(_target)) {
1041 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1042 for (i = 1; argv && (i < argc); i++)
1043 fprintf(output, " %s", argv[i]);
1044 } else if (_target->pid)
1045 fprintf(output, "process id \'%s", _target->pid);
1047 fprintf(output, "thread id \'%s", _target->tid);
1049 fprintf(output, "\'");
1050 if (config->run_count > 1)
1051 fprintf(output, " (%d runs)", config->run_count);
1052 fprintf(output, ":\n\n");
1056 static int get_precision(double num)
1061 return lround(ceil(-log10(num)));
1064 static void print_table(struct perf_stat_config *config,
1065 FILE *output, int precision, double avg)
1068 int idx, indent = 0;
1070 scnprintf(tmp, 64, " %17.*f", precision, avg);
1071 while (tmp[indent] == ' ')
1074 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1076 for (idx = 0; idx < config->run_count; idx++) {
1077 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1078 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1080 fprintf(output, " %17.*f (%+.*f) ",
1081 precision, run, precision, run - avg);
1083 for (h = 0; h < n; h++)
1084 fprintf(output, "#");
1086 fprintf(output, "\n");
1089 fprintf(output, "\n%*s# Final result:\n", indent, "");
1092 static double timeval2double(struct timeval *t)
1094 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1097 static void print_footer(struct perf_stat_config *config)
1099 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1100 FILE *output = config->output;
1102 if (!config->null_run)
1103 fprintf(output, "\n");
1105 if (config->run_count == 1) {
1106 fprintf(output, " %17.9f seconds time elapsed", avg);
1108 if (config->ru_display) {
1109 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1110 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1112 fprintf(output, "\n\n");
1113 fprintf(output, " %17.9f seconds user\n", ru_utime);
1114 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1117 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1119 * Display at most 2 more significant
1120 * digits than the stddev inaccuracy.
1122 int precision = get_precision(sd) + 2;
1124 if (config->walltime_run_table)
1125 print_table(config, output, precision, avg);
1127 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1128 precision, avg, precision, sd);
1130 print_noise_pct(config, sd, avg);
1132 fprintf(output, "\n\n");
1134 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1136 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1137 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1139 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1141 if (config->print_mixed_hw_group_error)
1143 "The events in group usually have to be from "
1144 "the same PMU. Try reorganizing the group.\n");
1147 static void print_percore_thread(struct perf_stat_config *config,
1148 struct evsel *counter, char *prefix)
1153 for (int i = 0; i < perf_evsel__nr_cpus(counter); i++) {
1154 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1155 for (s = 0; s < config->aggr_map->nr; s++) {
1156 id = config->aggr_map->map[s];
1161 print_counter_aggrdata(config, counter, s,
1167 static void print_percore(struct perf_stat_config *config,
1168 struct evsel *counter, char *prefix)
1170 bool metric_only = config->metric_only;
1171 FILE *output = config->output;
1175 if (!(config->aggr_map || config->aggr_get_id))
1178 if (config->percore_show_thread)
1179 return print_percore_thread(config, counter, prefix);
1181 for (s = 0; s < config->aggr_map->nr; s++) {
1182 if (prefix && metric_only)
1183 fprintf(output, "%s", prefix);
1185 print_counter_aggrdata(config, counter, s,
1186 prefix, metric_only,
1191 fputc('\n', output);
1195 perf_evlist__print_counters(struct evlist *evlist,
1196 struct perf_stat_config *config,
1197 struct target *_target,
1198 struct timespec *ts,
1199 int argc, const char **argv)
1201 bool metric_only = config->metric_only;
1202 int interval = config->interval;
1203 struct evsel *counter;
1204 char buf[64], *prefix = NULL;
1207 print_interval(config, evlist, prefix = buf, ts);
1209 print_header(config, _target, argc, argv);
1212 static int num_print_iv;
1214 if (num_print_iv == 0 && !interval)
1215 print_metric_headers(config, evlist, prefix, false);
1216 if (num_print_iv++ == 25)
1218 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1219 fprintf(config->output, "%s", prefix);
1222 switch (config->aggr_mode) {
1227 print_aggr(config, evlist, prefix);
1230 evlist__for_each_entry(evlist, counter) {
1231 print_aggr_thread(config, _target, counter, prefix);
1235 evlist__for_each_entry(evlist, counter) {
1236 print_counter_aggr(config, counter, prefix);
1239 fputc('\n', config->output);
1243 print_no_aggr_metric(config, evlist, prefix);
1245 evlist__for_each_entry(evlist, counter) {
1246 if (counter->percore)
1247 print_percore(config, counter, prefix);
1249 print_counter(config, counter, prefix);
1258 if (!interval && !config->csv_output)
1259 print_footer(config);
1261 fflush(config->output);