3 #include <linux/time64.h>
9 #include "thread_map.h"
12 #include "sane_ctype.h"
15 #include <api/fs/fs.h>
17 #define CNTR_NOT_SUPPORTED "<not supported>"
18 #define CNTR_NOT_COUNTED "<not counted>"
20 static bool is_duration_time(struct perf_evsel *evsel)
22 return !strcmp(evsel->name, "duration_time");
25 static void print_running(struct perf_stat_config *config,
28 if (config->csv_output) {
29 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
33 ena ? 100.0 * run / ena : 100.0);
34 } else if (run != ena) {
35 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
39 static void print_noise_pct(struct perf_stat_config *config,
40 double total, double avg)
42 double pct = rel_stddev_stats(total, avg);
44 if (config->csv_output)
45 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
47 fprintf(config->output, " ( +-%6.2f%% )", pct);
50 static void print_noise(struct perf_stat_config *config,
51 struct perf_evsel *evsel, double avg)
53 struct perf_stat_evsel *ps;
55 if (config->run_count == 1)
59 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
62 static void print_cgroup(struct perf_stat_config *config, struct perf_evsel *evsel)
65 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
66 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
71 static void aggr_printout(struct perf_stat_config *config,
72 struct perf_evsel *evsel, int id, int nr)
74 switch (config->aggr_mode) {
76 fprintf(config->output, "S%d-C%*d%s%*d%s",
77 cpu_map__id_to_socket(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%s%*d%s",
87 config->csv_output ? 0 : -5,
90 config->csv_output ? 0 : 4,
95 fprintf(config->output, "CPU%*d%s",
96 config->csv_output ? 0 : -4,
97 perf_evsel__cpus(evsel)->map[id], config->csv_sep);
100 fprintf(config->output, "%*s-%*d%s",
101 config->csv_output ? 0 : 16,
102 thread_map__comm(evsel->threads, id),
103 config->csv_output ? 0 : -8,
104 thread_map__pid(evsel->threads, id),
120 struct perf_evsel *evsel;
123 #define METRIC_LEN 35
125 static void new_line_std(struct perf_stat_config *config __maybe_unused,
128 struct outstate *os = ctx;
133 static void do_new_line_std(struct perf_stat_config *config,
137 fputs(os->prefix, os->fh);
138 aggr_printout(config, os->evsel, os->id, os->nr);
139 if (config->aggr_mode == AGGR_NONE)
140 fprintf(os->fh, " ");
141 fprintf(os->fh, " ");
144 static void print_metric_std(struct perf_stat_config *config,
145 void *ctx, const char *color, const char *fmt,
146 const char *unit, double val)
148 struct outstate *os = ctx;
151 bool newline = os->newline;
155 if (unit == NULL || fmt == NULL) {
156 fprintf(out, "%-*s", METRIC_LEN, "");
161 do_new_line_std(config, os);
163 n = fprintf(out, " # ");
165 n += color_fprintf(out, color, fmt, val);
167 n += fprintf(out, fmt, val);
168 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
171 static void new_line_csv(struct perf_stat_config *config, void *ctx)
173 struct outstate *os = ctx;
178 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
179 aggr_printout(config, os->evsel, os->id, os->nr);
180 for (i = 0; i < os->nfields; i++)
181 fputs(config->csv_sep, os->fh);
184 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
186 const char *color __maybe_unused,
187 const char *fmt, const char *unit, double val)
189 struct outstate *os = ctx;
191 char buf[64], *vals, *ends;
193 if (unit == NULL || fmt == NULL) {
194 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
197 snprintf(buf, sizeof(buf), fmt, val);
198 ends = vals = ltrim(buf);
199 while (isdigit(*ends) || *ends == '.')
202 while (isspace(*unit))
204 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit);
207 /* Filter out some columns that don't work well in metrics only mode */
209 static bool valid_only_metric(const char *unit)
213 if (strstr(unit, "/sec") ||
214 strstr(unit, "hz") ||
215 strstr(unit, "Hz") ||
216 strstr(unit, "CPUs utilized"))
221 static const char *fixunit(char *buf, struct perf_evsel *evsel,
224 if (!strncmp(unit, "of all", 6)) {
225 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
232 static void print_metric_only(struct perf_stat_config *config,
233 void *ctx, const char *color, const char *fmt,
234 const char *unit, double val)
236 struct outstate *os = ctx;
238 char buf[1024], str[1024];
239 unsigned mlen = config->metric_only_len;
241 if (!valid_only_metric(unit))
243 unit = fixunit(buf, os->evsel, unit);
244 if (mlen < strlen(unit))
245 mlen = strlen(unit) + 1;
248 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
250 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
251 fprintf(out, "%*s ", mlen, str);
254 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
255 void *ctx, const char *color __maybe_unused,
257 const char *unit, double val)
259 struct outstate *os = ctx;
261 char buf[64], *vals, *ends;
264 if (!valid_only_metric(unit))
266 unit = fixunit(tbuf, os->evsel, unit);
267 snprintf(buf, sizeof buf, fmt, val);
268 ends = vals = ltrim(buf);
269 while (isdigit(*ends) || *ends == '.')
272 fprintf(out, "%s%s", vals, config->csv_sep);
275 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
276 void *ctx __maybe_unused)
280 static void print_metric_header(struct perf_stat_config *config,
281 void *ctx, const char *color __maybe_unused,
282 const char *fmt __maybe_unused,
283 const char *unit, double val __maybe_unused)
285 struct outstate *os = ctx;
288 if (!valid_only_metric(unit))
290 unit = fixunit(tbuf, os->evsel, unit);
291 if (config->csv_output)
292 fprintf(os->fh, "%s%s", unit, config->csv_sep);
294 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
297 static int first_shadow_cpu(struct perf_stat_config *config,
298 struct perf_evsel *evsel, int id)
300 struct perf_evlist *evlist = evsel->evlist;
303 if (!config->aggr_get_id)
306 if (config->aggr_mode == AGGR_NONE)
309 if (config->aggr_mode == AGGR_GLOBAL)
312 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
313 int cpu2 = perf_evsel__cpus(evsel)->map[i];
315 if (config->aggr_get_id(config, evlist->cpus, cpu2) == id)
321 static void abs_printout(struct perf_stat_config *config,
322 int id, int nr, struct perf_evsel *evsel, double avg)
324 FILE *output = config->output;
325 double sc = evsel->scale;
328 if (config->csv_output) {
329 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
332 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
334 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
337 aggr_printout(config, evsel, id, nr);
339 fprintf(output, fmt, avg, config->csv_sep);
342 fprintf(output, "%-*s%s",
343 config->csv_output ? 0 : config->unit_width,
344 evsel->unit, config->csv_sep);
346 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
348 print_cgroup(config, evsel);
351 static bool is_mixed_hw_group(struct perf_evsel *counter)
353 struct perf_evlist *evlist = counter->evlist;
354 u32 pmu_type = counter->attr.type;
355 struct perf_evsel *pos;
357 if (counter->nr_members < 2)
360 evlist__for_each_entry(evlist, pos) {
361 /* software events can be part of any hardware group */
362 if (pos->attr.type == PERF_TYPE_SOFTWARE)
364 if (pmu_type == PERF_TYPE_SOFTWARE) {
365 pmu_type = pos->attr.type;
368 if (pmu_type != pos->attr.type)
375 static void printout(struct perf_stat_config *config, int id, int nr,
376 struct perf_evsel *counter, double uval,
377 char *prefix, u64 run, u64 ena, double noise,
378 struct runtime_stat *st)
380 struct perf_stat_output_ctx out;
381 struct outstate os = {
382 .fh = config->output,
383 .prefix = prefix ? prefix : "",
388 print_metric_t pm = print_metric_std;
391 if (config->metric_only) {
392 nl = new_line_metric;
393 if (config->csv_output)
394 pm = print_metric_only_csv;
396 pm = print_metric_only;
400 if (config->csv_output && !config->metric_only) {
401 static int aggr_fields[] = {
409 pm = print_metric_csv;
412 os.nfields += aggr_fields[config->aggr_mode];
416 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
417 if (config->metric_only) {
418 pm(config, &os, NULL, "", "", 0);
421 aggr_printout(config, counter, id, nr);
423 fprintf(config->output, "%*s%s",
424 config->csv_output ? 0 : 18,
425 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
428 if (counter->supported) {
429 config->print_free_counters_hint = 1;
430 if (is_mixed_hw_group(counter))
431 config->print_mixed_hw_group_error = 1;
434 fprintf(config->output, "%-*s%s",
435 config->csv_output ? 0 : config->unit_width,
436 counter->unit, config->csv_sep);
438 fprintf(config->output, "%*s",
439 config->csv_output ? 0 : -25,
440 perf_evsel__name(counter));
442 print_cgroup(config, counter);
444 if (!config->csv_output)
445 pm(config, &os, NULL, NULL, "", 0);
446 print_noise(config, counter, noise);
447 print_running(config, run, ena);
448 if (config->csv_output)
449 pm(config, &os, NULL, NULL, "", 0);
453 if (!config->metric_only)
454 abs_printout(config, id, nr, counter, uval);
456 out.print_metric = pm;
459 out.force_header = false;
461 if (config->csv_output && !config->metric_only) {
462 print_noise(config, counter, noise);
463 print_running(config, run, ena);
466 perf_stat__print_shadow_stats(config, counter, uval,
467 first_shadow_cpu(config, counter, id),
468 &out, &config->metric_events, st);
469 if (!config->csv_output && !config->metric_only) {
470 print_noise(config, counter, noise);
471 print_running(config, run, ena);
475 static void aggr_update_shadow(struct perf_stat_config *config,
476 struct perf_evlist *evlist)
480 struct perf_evsel *counter;
482 for (s = 0; s < config->aggr_map->nr; s++) {
483 id = config->aggr_map->map[s];
484 evlist__for_each_entry(evlist, counter) {
486 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
487 s2 = config->aggr_get_id(config, evlist->cpus, cpu);
490 val += perf_counts(counter->counts, cpu, 0)->val;
492 perf_stat__update_shadow_stats(counter, val,
493 first_shadow_cpu(config, counter, id),
499 static void uniquify_event_name(struct perf_evsel *counter)
504 if (counter->uniquified_name ||
505 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
506 strlen(counter->pmu_name)))
509 config = strchr(counter->name, '/');
511 if (asprintf(&new_name,
512 "%s%s", counter->pmu_name, config) > 0) {
514 counter->name = new_name;
517 if (asprintf(&new_name,
518 "%s [%s]", counter->name, counter->pmu_name) > 0) {
520 counter->name = new_name;
524 counter->uniquified_name = true;
527 static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter,
528 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
532 struct perf_evlist *evlist = counter->evlist;
533 struct perf_evsel *alias;
535 alias = list_prepare_entry(counter, &(evlist->entries), node);
536 list_for_each_entry_continue (alias, &evlist->entries, node) {
537 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
538 alias->scale != counter->scale ||
539 alias->cgrp != counter->cgrp ||
540 strcmp(alias->unit, counter->unit) ||
541 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
543 alias->merged_stat = true;
544 cb(config, alias, data, false);
548 static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter,
549 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
553 if (counter->merged_stat)
555 cb(config, counter, data, true);
556 if (config->no_merge)
557 uniquify_event_name(counter);
558 else if (counter->auto_merge_stats)
559 collect_all_aliases(config, counter, cb, data);
570 static void aggr_cb(struct perf_stat_config *config,
571 struct perf_evsel *counter, void *data, bool first)
573 struct aggr_data *ad = data;
576 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
577 struct perf_counts_values *counts;
579 s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu);
584 counts = perf_counts(counter->counts, cpu, 0);
586 * When any result is bad, make them all to give
587 * consistent output in interval mode.
589 if (counts->ena == 0 || counts->run == 0 ||
590 counter->counts->scaled == -1) {
595 ad->val += counts->val;
596 ad->ena += counts->ena;
597 ad->run += counts->run;
601 static void print_aggr(struct perf_stat_config *config,
602 struct perf_evlist *evlist,
605 bool metric_only = config->metric_only;
606 FILE *output = config->output;
607 struct perf_evsel *counter;
613 if (!(config->aggr_map || config->aggr_get_id))
616 aggr_update_shadow(config, evlist);
619 * With metric_only everything is on a single line.
620 * Without each counter has its own line.
622 for (s = 0; s < config->aggr_map->nr; s++) {
624 if (prefix && metric_only)
625 fprintf(output, "%s", prefix);
627 ad.id = id = config->aggr_map->map[s];
629 evlist__for_each_entry(evlist, counter) {
630 if (is_duration_time(counter))
633 ad.val = ad.ena = ad.run = 0;
635 if (!collect_data(config, counter, aggr_cb, &ad))
641 if (first && metric_only) {
643 aggr_printout(config, counter, id, nr);
645 if (prefix && !metric_only)
646 fprintf(output, "%s", prefix);
648 uval = val * counter->scale;
649 printout(config, id, nr, counter, uval, prefix,
650 run, ena, 1.0, &rt_stat);
659 static int cmp_val(const void *a, const void *b)
661 return ((struct perf_aggr_thread_value *)b)->val -
662 ((struct perf_aggr_thread_value *)a)->val;
665 static struct perf_aggr_thread_value *sort_aggr_thread(
666 struct perf_evsel *counter,
667 int nthreads, int ncpus,
669 struct target *_target)
671 int cpu, thread, i = 0;
673 struct perf_aggr_thread_value *buf;
675 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
679 for (thread = 0; thread < nthreads; thread++) {
680 u64 ena = 0, run = 0, val = 0;
682 for (cpu = 0; cpu < ncpus; cpu++) {
683 val += perf_counts(counter->counts, cpu, thread)->val;
684 ena += perf_counts(counter->counts, cpu, thread)->ena;
685 run += perf_counts(counter->counts, cpu, thread)->run;
688 uval = val * counter->scale;
691 * Skip value 0 when enabling --per-thread globally,
692 * otherwise too many 0 output.
694 if (uval == 0.0 && target__has_per_thread(_target))
697 buf[i].counter = counter;
706 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
714 static void print_aggr_thread(struct perf_stat_config *config,
715 struct target *_target,
716 struct perf_evsel *counter, char *prefix)
718 FILE *output = config->output;
719 int nthreads = thread_map__nr(counter->threads);
720 int ncpus = cpu_map__nr(counter->cpus);
721 int thread, sorted_threads, id;
722 struct perf_aggr_thread_value *buf;
724 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
726 perror("cannot sort aggr thread");
730 for (thread = 0; thread < sorted_threads; thread++) {
732 fprintf(output, "%s", prefix);
736 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
737 prefix, buf[thread].run, buf[thread].ena, 1.0,
740 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
741 prefix, buf[thread].run, buf[thread].ena, 1.0,
750 double avg, avg_enabled, avg_running;
753 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
754 struct perf_evsel *counter, void *data,
755 bool first __maybe_unused)
757 struct caggr_data *cd = data;
758 struct perf_stat_evsel *ps = counter->stats;
760 cd->avg += avg_stats(&ps->res_stats[0]);
761 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
762 cd->avg_running += avg_stats(&ps->res_stats[2]);
766 * Print out the results of a single counter:
767 * aggregated counts in system-wide mode
769 static void print_counter_aggr(struct perf_stat_config *config,
770 struct perf_evsel *counter, char *prefix)
772 bool metric_only = config->metric_only;
773 FILE *output = config->output;
775 struct caggr_data cd = { .avg = 0.0 };
777 if (!collect_data(config, counter, counter_aggr_cb, &cd))
780 if (prefix && !metric_only)
781 fprintf(output, "%s", prefix);
783 uval = cd.avg * counter->scale;
784 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
787 fprintf(output, "\n");
790 static void counter_cb(struct perf_stat_config *config __maybe_unused,
791 struct perf_evsel *counter, void *data,
792 bool first __maybe_unused)
794 struct aggr_data *ad = data;
796 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
797 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
798 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
802 * Print out the results of a single counter:
803 * does not use aggregated count in system-wide
805 static void print_counter(struct perf_stat_config *config,
806 struct perf_evsel *counter, char *prefix)
808 FILE *output = config->output;
813 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
814 struct aggr_data ad = { .cpu = cpu };
816 if (!collect_data(config, counter, counter_cb, &ad))
823 fprintf(output, "%s", prefix);
825 uval = val * counter->scale;
826 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
833 static void print_no_aggr_metric(struct perf_stat_config *config,
834 struct perf_evlist *evlist,
839 struct perf_evsel *counter;
843 nrcpus = evlist->cpus->nr;
844 for (cpu = 0; cpu < nrcpus; cpu++) {
848 fputs(prefix, config->output);
849 evlist__for_each_entry(evlist, counter) {
850 if (is_duration_time(counter))
853 aggr_printout(config, counter, cpu, 0);
856 val = perf_counts(counter->counts, cpu, 0)->val;
857 ena = perf_counts(counter->counts, cpu, 0)->ena;
858 run = perf_counts(counter->counts, cpu, 0)->run;
860 uval = val * counter->scale;
861 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
864 fputc('\n', config->output);
868 static int aggr_header_lens[] = {
876 static const char *aggr_header_csv[] = {
877 [AGGR_CORE] = "core,cpus,",
878 [AGGR_SOCKET] = "socket,cpus",
879 [AGGR_NONE] = "cpu,",
880 [AGGR_THREAD] = "comm-pid,",
884 static void print_metric_headers(struct perf_stat_config *config,
885 struct perf_evlist *evlist,
886 const char *prefix, bool no_indent)
888 struct perf_stat_output_ctx out;
889 struct perf_evsel *counter;
890 struct outstate os = {
895 fprintf(config->output, "%s", prefix);
897 if (!config->csv_output && !no_indent)
898 fprintf(config->output, "%*s",
899 aggr_header_lens[config->aggr_mode], "");
900 if (config->csv_output) {
901 if (config->interval)
902 fputs("time,", config->output);
903 fputs(aggr_header_csv[config->aggr_mode], config->output);
906 /* Print metrics headers only */
907 evlist__for_each_entry(evlist, counter) {
908 if (is_duration_time(counter))
912 out.print_metric = print_metric_header;
913 out.new_line = new_line_metric;
914 out.force_header = true;
916 perf_stat__print_shadow_stats(config, counter, 0,
919 &config->metric_events,
922 fputc('\n', config->output);
925 static void print_interval(struct perf_stat_config *config,
926 struct perf_evlist *evlist,
927 char *prefix, struct timespec *ts)
929 bool metric_only = config->metric_only;
930 unsigned int unit_width = config->unit_width;
931 FILE *output = config->output;
932 static int num_print_interval;
934 if (config->interval_clear)
937 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
939 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
940 switch (config->aggr_mode) {
942 fprintf(output, "# time socket cpus");
944 fprintf(output, " counts %*s events\n", unit_width, "unit");
947 fprintf(output, "# time core cpus");
949 fprintf(output, " counts %*s events\n", unit_width, "unit");
952 fprintf(output, "# time CPU ");
954 fprintf(output, " counts %*s events\n", unit_width, "unit");
957 fprintf(output, "# time comm-pid");
959 fprintf(output, " counts %*s events\n", unit_width, "unit");
963 fprintf(output, "# time");
965 fprintf(output, " counts %*s events\n", unit_width, "unit");
971 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
972 print_metric_headers(config, evlist, " ", true);
973 if (++num_print_interval == 25)
974 num_print_interval = 0;
977 static void print_header(struct perf_stat_config *config,
978 struct target *_target,
979 int argc, const char **argv)
981 FILE *output = config->output;
986 if (!config->csv_output) {
987 fprintf(output, "\n");
988 fprintf(output, " Performance counter stats for ");
989 if (_target->system_wide)
990 fprintf(output, "\'system wide");
991 else if (_target->cpu_list)
992 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
993 else if (!target__has_task(_target)) {
994 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
995 for (i = 1; argv && (i < argc); i++)
996 fprintf(output, " %s", argv[i]);
997 } else if (_target->pid)
998 fprintf(output, "process id \'%s", _target->pid);
1000 fprintf(output, "thread id \'%s", _target->tid);
1002 fprintf(output, "\'");
1003 if (config->run_count > 1)
1004 fprintf(output, " (%d runs)", config->run_count);
1005 fprintf(output, ":\n\n");
1009 static int get_precision(double num)
1014 return lround(ceil(-log10(num)));
1017 static void print_table(struct perf_stat_config *config,
1018 FILE *output, int precision, double avg)
1021 int idx, indent = 0;
1023 scnprintf(tmp, 64, " %17.*f", precision, avg);
1024 while (tmp[indent] == ' ')
1027 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1029 for (idx = 0; idx < config->run_count; idx++) {
1030 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1031 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1033 fprintf(output, " %17.*f (%+.*f) ",
1034 precision, run, precision, run - avg);
1036 for (h = 0; h < n; h++)
1037 fprintf(output, "#");
1039 fprintf(output, "\n");
1042 fprintf(output, "\n%*s# Final result:\n", indent, "");
1045 static double timeval2double(struct timeval *t)
1047 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1050 static void print_footer(struct perf_stat_config *config)
1052 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1053 FILE *output = config->output;
1056 if (!config->null_run)
1057 fprintf(output, "\n");
1059 if (config->run_count == 1) {
1060 fprintf(output, " %17.9f seconds time elapsed", avg);
1062 if (config->ru_display) {
1063 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1064 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1066 fprintf(output, "\n\n");
1067 fprintf(output, " %17.9f seconds user\n", ru_utime);
1068 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1071 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1073 * Display at most 2 more significant
1074 * digits than the stddev inaccuracy.
1076 int precision = get_precision(sd) + 2;
1078 if (config->walltime_run_table)
1079 print_table(config, output, precision, avg);
1081 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1082 precision, avg, precision, sd);
1084 print_noise_pct(config, sd, avg);
1086 fprintf(output, "\n\n");
1088 if (config->print_free_counters_hint &&
1089 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1092 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1093 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1095 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1097 if (config->print_mixed_hw_group_error)
1099 "The events in group usually have to be from "
1100 "the same PMU. Try reorganizing the group.\n");
1104 perf_evlist__print_counters(struct perf_evlist *evlist,
1105 struct perf_stat_config *config,
1106 struct target *_target,
1107 struct timespec *ts,
1108 int argc, const char **argv)
1110 bool metric_only = config->metric_only;
1111 int interval = config->interval;
1112 struct perf_evsel *counter;
1113 char buf[64], *prefix = NULL;
1116 print_interval(config, evlist, prefix = buf, ts);
1118 print_header(config, _target, argc, argv);
1121 static int num_print_iv;
1123 if (num_print_iv == 0 && !interval)
1124 print_metric_headers(config, evlist, prefix, false);
1125 if (num_print_iv++ == 25)
1127 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1128 fprintf(config->output, "%s", prefix);
1131 switch (config->aggr_mode) {
1134 print_aggr(config, evlist, prefix);
1137 evlist__for_each_entry(evlist, counter) {
1138 if (is_duration_time(counter))
1140 print_aggr_thread(config, _target, counter, prefix);
1144 evlist__for_each_entry(evlist, counter) {
1145 if (is_duration_time(counter))
1147 print_counter_aggr(config, counter, prefix);
1150 fputc('\n', config->output);
1154 print_no_aggr_metric(config, evlist, prefix);
1156 evlist__for_each_entry(evlist, counter) {
1157 if (is_duration_time(counter))
1159 print_counter(config, counter, prefix);
1168 if (!interval && !config->csv_output)
1169 print_footer(config);
1171 fflush(config->output);