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", os->prefix);
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 /* Merge events with the same name, etc. but on different PMUs. */
559 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
560 alias->scale == counter->scale &&
561 alias->cgrp == counter->cgrp &&
562 !strcmp(alias->unit, counter->unit) &&
563 evsel__is_clock(alias) == evsel__is_clock(counter) &&
564 strcmp(alias->pmu_name, counter->pmu_name)) {
565 alias->merged_stat = true;
566 cb(config, alias, data, false);
571 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
572 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
576 if (counter->merged_stat)
578 cb(config, counter, data, true);
579 if (config->no_merge)
580 uniquify_event_name(counter);
581 else if (counter->auto_merge_stats)
582 collect_all_aliases(config, counter, cb, data);
593 static void aggr_cb(struct perf_stat_config *config,
594 struct evsel *counter, void *data, bool first)
596 struct aggr_data *ad = data;
599 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
600 struct perf_counts_values *counts;
602 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
607 counts = perf_counts(counter->counts, cpu, 0);
609 * When any result is bad, make them all to give
610 * consistent output in interval mode.
612 if (counts->ena == 0 || counts->run == 0 ||
613 counter->counts->scaled == -1) {
618 ad->val += counts->val;
619 ad->ena += counts->ena;
620 ad->run += counts->run;
624 static void print_counter_aggrdata(struct perf_stat_config *config,
625 struct evsel *counter, int s,
626 char *prefix, bool metric_only,
627 bool *first, int cpu)
630 FILE *output = config->output;
635 ad.id = id = config->aggr_map->map[s];
636 ad.val = ad.ena = ad.run = 0;
638 if (!collect_data(config, counter, aggr_cb, &ad))
645 if (*first && metric_only) {
647 aggr_printout(config, counter, id, nr);
649 if (prefix && !metric_only)
650 fprintf(output, "%s", prefix);
652 uval = val * counter->scale;
653 printout(config, cpu != -1 ? cpu : id, nr, counter, uval, prefix,
654 run, ena, 1.0, &rt_stat);
659 static void print_aggr(struct perf_stat_config *config,
660 struct evlist *evlist,
663 bool metric_only = config->metric_only;
664 FILE *output = config->output;
665 struct evsel *counter;
669 if (!config->aggr_map || !config->aggr_get_id)
672 aggr_update_shadow(config, evlist);
675 * With metric_only everything is on a single line.
676 * Without each counter has its own line.
678 for (s = 0; s < config->aggr_map->nr; s++) {
679 if (prefix && metric_only)
680 fprintf(output, "%s", prefix);
683 evlist__for_each_entry(evlist, counter) {
684 print_counter_aggrdata(config, counter, s,
693 static int cmp_val(const void *a, const void *b)
695 return ((struct perf_aggr_thread_value *)b)->val -
696 ((struct perf_aggr_thread_value *)a)->val;
699 static struct perf_aggr_thread_value *sort_aggr_thread(
700 struct evsel *counter,
701 int nthreads, int ncpus,
703 struct target *_target)
705 int cpu, thread, i = 0;
707 struct perf_aggr_thread_value *buf;
709 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
713 for (thread = 0; thread < nthreads; thread++) {
714 u64 ena = 0, run = 0, val = 0;
716 for (cpu = 0; cpu < ncpus; cpu++) {
717 val += perf_counts(counter->counts, cpu, thread)->val;
718 ena += perf_counts(counter->counts, cpu, thread)->ena;
719 run += perf_counts(counter->counts, cpu, thread)->run;
722 uval = val * counter->scale;
725 * Skip value 0 when enabling --per-thread globally,
726 * otherwise too many 0 output.
728 if (uval == 0.0 && target__has_per_thread(_target))
731 buf[i].counter = counter;
740 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
748 static void print_aggr_thread(struct perf_stat_config *config,
749 struct target *_target,
750 struct evsel *counter, char *prefix)
752 FILE *output = config->output;
753 int nthreads = perf_thread_map__nr(counter->core.threads);
754 int ncpus = perf_cpu_map__nr(counter->core.cpus);
755 int thread, sorted_threads, id;
756 struct perf_aggr_thread_value *buf;
758 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
760 perror("cannot sort aggr thread");
764 for (thread = 0; thread < sorted_threads; thread++) {
766 fprintf(output, "%s", prefix);
770 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
771 prefix, buf[thread].run, buf[thread].ena, 1.0,
774 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
775 prefix, buf[thread].run, buf[thread].ena, 1.0,
784 double avg, avg_enabled, avg_running;
787 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
788 struct evsel *counter, void *data,
789 bool first __maybe_unused)
791 struct caggr_data *cd = data;
792 struct perf_stat_evsel *ps = counter->stats;
794 cd->avg += avg_stats(&ps->res_stats[0]);
795 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
796 cd->avg_running += avg_stats(&ps->res_stats[2]);
800 * Print out the results of a single counter:
801 * aggregated counts in system-wide mode
803 static void print_counter_aggr(struct perf_stat_config *config,
804 struct evsel *counter, char *prefix)
806 bool metric_only = config->metric_only;
807 FILE *output = config->output;
809 struct caggr_data cd = { .avg = 0.0 };
811 if (!collect_data(config, counter, counter_aggr_cb, &cd))
814 if (prefix && !metric_only)
815 fprintf(output, "%s", prefix);
817 uval = cd.avg * counter->scale;
818 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
821 fprintf(output, "\n");
824 static void counter_cb(struct perf_stat_config *config __maybe_unused,
825 struct evsel *counter, void *data,
826 bool first __maybe_unused)
828 struct aggr_data *ad = data;
830 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
831 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
832 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
836 * Print out the results of a single counter:
837 * does not use aggregated count in system-wide
839 static void print_counter(struct perf_stat_config *config,
840 struct evsel *counter, char *prefix)
842 FILE *output = config->output;
847 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
848 struct aggr_data ad = { .cpu = cpu };
850 if (!collect_data(config, counter, counter_cb, &ad))
857 fprintf(output, "%s", prefix);
859 uval = val * counter->scale;
860 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
867 static void print_no_aggr_metric(struct perf_stat_config *config,
868 struct evlist *evlist,
873 struct evsel *counter;
877 nrcpus = evlist->core.cpus->nr;
878 for (cpu = 0; cpu < nrcpus; cpu++) {
882 fputs(prefix, config->output);
883 evlist__for_each_entry(evlist, counter) {
885 aggr_printout(config, counter, cpu, 0);
888 val = perf_counts(counter->counts, cpu, 0)->val;
889 ena = perf_counts(counter->counts, cpu, 0)->ena;
890 run = perf_counts(counter->counts, cpu, 0)->run;
892 uval = val * counter->scale;
893 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
896 fputc('\n', config->output);
900 static int aggr_header_lens[] = {
909 static const char *aggr_header_csv[] = {
910 [AGGR_CORE] = "core,cpus,",
911 [AGGR_DIE] = "die,cpus",
912 [AGGR_SOCKET] = "socket,cpus",
913 [AGGR_NONE] = "cpu,",
914 [AGGR_THREAD] = "comm-pid,",
918 static void print_metric_headers(struct perf_stat_config *config,
919 struct evlist *evlist,
920 const char *prefix, bool no_indent)
922 struct perf_stat_output_ctx out;
923 struct evsel *counter;
924 struct outstate os = {
929 fprintf(config->output, "%s", prefix);
931 if (!config->csv_output && !no_indent)
932 fprintf(config->output, "%*s",
933 aggr_header_lens[config->aggr_mode], "");
934 if (config->csv_output) {
935 if (config->interval)
936 fputs("time,", config->output);
937 fputs(aggr_header_csv[config->aggr_mode], config->output);
940 /* Print metrics headers only */
941 evlist__for_each_entry(evlist, counter) {
944 out.print_metric = print_metric_header;
945 out.new_line = new_line_metric;
946 out.force_header = true;
947 perf_stat__print_shadow_stats(config, counter, 0,
950 &config->metric_events,
953 fputc('\n', config->output);
956 static void print_interval(struct perf_stat_config *config,
957 struct evlist *evlist,
958 char *prefix, struct timespec *ts)
960 bool metric_only = config->metric_only;
961 unsigned int unit_width = config->unit_width;
962 FILE *output = config->output;
963 static int num_print_interval;
965 if (config->interval_clear)
968 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
970 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
971 switch (config->aggr_mode) {
973 fprintf(output, "# time node cpus");
975 fprintf(output, " counts %*s events\n", unit_width, "unit");
978 fprintf(output, "# time socket cpus");
980 fprintf(output, " counts %*s events\n", unit_width, "unit");
983 fprintf(output, "# time die cpus");
985 fprintf(output, " counts %*s events\n", unit_width, "unit");
988 fprintf(output, "# time core cpus");
990 fprintf(output, " counts %*s events\n", unit_width, "unit");
993 fprintf(output, "# time CPU ");
995 fprintf(output, " counts %*s events\n", unit_width, "unit");
998 fprintf(output, "# time comm-pid");
1000 fprintf(output, " counts %*s events\n", unit_width, "unit");
1004 fprintf(output, "# time");
1006 fprintf(output, " counts %*s events\n", unit_width, "unit");
1012 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1013 print_metric_headers(config, evlist, " ", true);
1014 if (++num_print_interval == 25)
1015 num_print_interval = 0;
1018 static void print_header(struct perf_stat_config *config,
1019 struct target *_target,
1020 int argc, const char **argv)
1022 FILE *output = config->output;
1027 if (!config->csv_output) {
1028 fprintf(output, "\n");
1029 fprintf(output, " Performance counter stats for ");
1030 if (_target->system_wide)
1031 fprintf(output, "\'system wide");
1032 else if (_target->cpu_list)
1033 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1034 else if (!target__has_task(_target)) {
1035 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1036 for (i = 1; argv && (i < argc); i++)
1037 fprintf(output, " %s", argv[i]);
1038 } else if (_target->pid)
1039 fprintf(output, "process id \'%s", _target->pid);
1041 fprintf(output, "thread id \'%s", _target->tid);
1043 fprintf(output, "\'");
1044 if (config->run_count > 1)
1045 fprintf(output, " (%d runs)", config->run_count);
1046 fprintf(output, ":\n\n");
1050 static int get_precision(double num)
1055 return lround(ceil(-log10(num)));
1058 static void print_table(struct perf_stat_config *config,
1059 FILE *output, int precision, double avg)
1062 int idx, indent = 0;
1064 scnprintf(tmp, 64, " %17.*f", precision, avg);
1065 while (tmp[indent] == ' ')
1068 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1070 for (idx = 0; idx < config->run_count; idx++) {
1071 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1072 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1074 fprintf(output, " %17.*f (%+.*f) ",
1075 precision, run, precision, run - avg);
1077 for (h = 0; h < n; h++)
1078 fprintf(output, "#");
1080 fprintf(output, "\n");
1083 fprintf(output, "\n%*s# Final result:\n", indent, "");
1086 static double timeval2double(struct timeval *t)
1088 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1091 static void print_footer(struct perf_stat_config *config)
1093 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1094 FILE *output = config->output;
1096 if (!config->null_run)
1097 fprintf(output, "\n");
1099 if (config->run_count == 1) {
1100 fprintf(output, " %17.9f seconds time elapsed", avg);
1102 if (config->ru_display) {
1103 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1104 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1106 fprintf(output, "\n\n");
1107 fprintf(output, " %17.9f seconds user\n", ru_utime);
1108 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1111 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1113 * Display at most 2 more significant
1114 * digits than the stddev inaccuracy.
1116 int precision = get_precision(sd) + 2;
1118 if (config->walltime_run_table)
1119 print_table(config, output, precision, avg);
1121 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1122 precision, avg, precision, sd);
1124 print_noise_pct(config, sd, avg);
1126 fprintf(output, "\n\n");
1128 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1130 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1131 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1133 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1135 if (config->print_mixed_hw_group_error)
1137 "The events in group usually have to be from "
1138 "the same PMU. Try reorganizing the group.\n");
1141 static void print_percore_thread(struct perf_stat_config *config,
1142 struct evsel *counter, char *prefix)
1147 for (int i = 0; i < evsel__nr_cpus(counter); i++) {
1148 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1149 for (s = 0; s < config->aggr_map->nr; s++) {
1150 id = config->aggr_map->map[s];
1155 print_counter_aggrdata(config, counter, s,
1161 static void print_percore(struct perf_stat_config *config,
1162 struct evsel *counter, char *prefix)
1164 bool metric_only = config->metric_only;
1165 FILE *output = config->output;
1169 if (!config->aggr_map || !config->aggr_get_id)
1172 if (config->percore_show_thread)
1173 return print_percore_thread(config, counter, prefix);
1175 for (s = 0; s < config->aggr_map->nr; s++) {
1176 if (prefix && metric_only)
1177 fprintf(output, "%s", prefix);
1179 print_counter_aggrdata(config, counter, s,
1180 prefix, metric_only,
1185 fputc('\n', output);
1189 perf_evlist__print_counters(struct evlist *evlist,
1190 struct perf_stat_config *config,
1191 struct target *_target,
1192 struct timespec *ts,
1193 int argc, const char **argv)
1195 bool metric_only = config->metric_only;
1196 int interval = config->interval;
1197 struct evsel *counter;
1198 char buf[64], *prefix = NULL;
1201 print_interval(config, evlist, prefix = buf, ts);
1203 print_header(config, _target, argc, argv);
1206 static int num_print_iv;
1208 if (num_print_iv == 0 && !interval)
1209 print_metric_headers(config, evlist, prefix, false);
1210 if (num_print_iv++ == 25)
1212 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1213 fprintf(config->output, "%s", prefix);
1216 switch (config->aggr_mode) {
1221 print_aggr(config, evlist, prefix);
1224 evlist__for_each_entry(evlist, counter) {
1225 print_aggr_thread(config, _target, counter, prefix);
1229 evlist__for_each_entry(evlist, counter) {
1230 print_counter_aggr(config, counter, prefix);
1233 fputc('\n', config->output);
1237 print_no_aggr_metric(config, evlist, prefix);
1239 evlist__for_each_entry(evlist, counter) {
1240 if (counter->percore)
1241 print_percore(config, counter, prefix);
1243 print_counter(config, counter, prefix);
1252 if (!interval && !config->csv_output)
1253 print_footer(config);
1255 fflush(config->output);