3 #include <linux/time64.h>
10 #include "thread_map.h"
13 #include "sane_ctype.h"
16 #include <api/fs/fs.h>
18 #define CNTR_NOT_SUPPORTED "<not supported>"
19 #define CNTR_NOT_COUNTED "<not counted>"
21 static void print_running(struct perf_stat_config *config,
24 if (config->csv_output) {
25 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
29 ena ? 100.0 * run / ena : 100.0);
30 } else if (run != ena) {
31 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
35 static void print_noise_pct(struct perf_stat_config *config,
36 double total, double avg)
38 double pct = rel_stddev_stats(total, avg);
40 if (config->csv_output)
41 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
43 fprintf(config->output, " ( +-%6.2f%% )", pct);
46 static void print_noise(struct perf_stat_config *config,
47 struct perf_evsel *evsel, double avg)
49 struct perf_stat_evsel *ps;
51 if (config->run_count == 1)
55 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
58 static void print_cgroup(struct perf_stat_config *config, struct perf_evsel *evsel)
61 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
62 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
67 static void aggr_printout(struct perf_stat_config *config,
68 struct perf_evsel *evsel, int id, int nr)
70 switch (config->aggr_mode) {
72 fprintf(config->output, "S%d-C%*d%s%*d%s",
73 cpu_map__id_to_socket(id),
74 config->csv_output ? 0 : -8,
75 cpu_map__id_to_cpu(id),
77 config->csv_output ? 0 : 4,
82 fprintf(config->output, "S%*d%s%*d%s",
83 config->csv_output ? 0 : -5,
86 config->csv_output ? 0 : 4,
91 fprintf(config->output, "CPU%*d%s",
92 config->csv_output ? 0 : -4,
93 perf_evsel__cpus(evsel)->map[id], config->csv_sep);
96 fprintf(config->output, "%*s-%*d%s",
97 config->csv_output ? 0 : 16,
98 thread_map__comm(evsel->threads, id),
99 config->csv_output ? 0 : -8,
100 thread_map__pid(evsel->threads, id),
116 struct perf_evsel *evsel;
119 #define METRIC_LEN 35
121 static void new_line_std(struct perf_stat_config *config __maybe_unused,
124 struct outstate *os = ctx;
129 static void do_new_line_std(struct perf_stat_config *config,
133 fputs(os->prefix, os->fh);
134 aggr_printout(config, os->evsel, os->id, os->nr);
135 if (config->aggr_mode == AGGR_NONE)
136 fprintf(os->fh, " ");
137 fprintf(os->fh, " ");
140 static void print_metric_std(struct perf_stat_config *config,
141 void *ctx, const char *color, const char *fmt,
142 const char *unit, double val)
144 struct outstate *os = ctx;
147 bool newline = os->newline;
151 if (unit == NULL || fmt == NULL) {
152 fprintf(out, "%-*s", METRIC_LEN, "");
157 do_new_line_std(config, os);
159 n = fprintf(out, " # ");
161 n += color_fprintf(out, color, fmt, val);
163 n += fprintf(out, fmt, val);
164 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
167 static void new_line_csv(struct perf_stat_config *config, void *ctx)
169 struct outstate *os = ctx;
174 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
175 aggr_printout(config, os->evsel, os->id, os->nr);
176 for (i = 0; i < os->nfields; i++)
177 fputs(config->csv_sep, os->fh);
180 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
182 const char *color __maybe_unused,
183 const char *fmt, const char *unit, double val)
185 struct outstate *os = ctx;
187 char buf[64], *vals, *ends;
189 if (unit == NULL || fmt == NULL) {
190 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
193 snprintf(buf, sizeof(buf), fmt, val);
194 ends = vals = ltrim(buf);
195 while (isdigit(*ends) || *ends == '.')
198 while (isspace(*unit))
200 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit);
203 /* Filter out some columns that don't work well in metrics only mode */
205 static bool valid_only_metric(const char *unit)
209 if (strstr(unit, "/sec") ||
210 strstr(unit, "hz") ||
211 strstr(unit, "Hz") ||
212 strstr(unit, "CPUs utilized"))
217 static const char *fixunit(char *buf, struct perf_evsel *evsel,
220 if (!strncmp(unit, "of all", 6)) {
221 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
228 static void print_metric_only(struct perf_stat_config *config,
229 void *ctx, const char *color, const char *fmt,
230 const char *unit, double val)
232 struct outstate *os = ctx;
234 char buf[1024], str[1024];
235 unsigned mlen = config->metric_only_len;
237 if (!valid_only_metric(unit))
239 unit = fixunit(buf, os->evsel, unit);
240 if (mlen < strlen(unit))
241 mlen = strlen(unit) + 1;
244 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
246 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
247 fprintf(out, "%*s ", mlen, str);
250 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
251 void *ctx, const char *color __maybe_unused,
253 const char *unit, double val)
255 struct outstate *os = ctx;
257 char buf[64], *vals, *ends;
260 if (!valid_only_metric(unit))
262 unit = fixunit(tbuf, os->evsel, unit);
263 snprintf(buf, sizeof buf, fmt, val);
264 ends = vals = ltrim(buf);
265 while (isdigit(*ends) || *ends == '.')
268 fprintf(out, "%s%s", vals, config->csv_sep);
271 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
272 void *ctx __maybe_unused)
276 static void print_metric_header(struct perf_stat_config *config,
277 void *ctx, const char *color __maybe_unused,
278 const char *fmt __maybe_unused,
279 const char *unit, double val __maybe_unused)
281 struct outstate *os = ctx;
284 if (!valid_only_metric(unit))
286 unit = fixunit(tbuf, os->evsel, unit);
287 if (config->csv_output)
288 fprintf(os->fh, "%s%s", unit, config->csv_sep);
290 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
293 static int first_shadow_cpu(struct perf_stat_config *config,
294 struct perf_evsel *evsel, int id)
296 struct perf_evlist *evlist = evsel->evlist;
299 if (!config->aggr_get_id)
302 if (config->aggr_mode == AGGR_NONE)
305 if (config->aggr_mode == AGGR_GLOBAL)
308 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
309 int cpu2 = perf_evsel__cpus(evsel)->map[i];
311 if (config->aggr_get_id(config, evlist->cpus, cpu2) == id)
317 static void abs_printout(struct perf_stat_config *config,
318 int id, int nr, struct perf_evsel *evsel, double avg)
320 FILE *output = config->output;
321 double sc = evsel->scale;
324 if (config->csv_output) {
325 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
328 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
330 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
333 aggr_printout(config, evsel, id, nr);
335 fprintf(output, fmt, avg, config->csv_sep);
338 fprintf(output, "%-*s%s",
339 config->csv_output ? 0 : config->unit_width,
340 evsel->unit, config->csv_sep);
342 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
344 print_cgroup(config, evsel);
347 static bool is_mixed_hw_group(struct perf_evsel *counter)
349 struct perf_evlist *evlist = counter->evlist;
350 u32 pmu_type = counter->attr.type;
351 struct perf_evsel *pos;
353 if (counter->nr_members < 2)
356 evlist__for_each_entry(evlist, pos) {
357 /* software events can be part of any hardware group */
358 if (pos->attr.type == PERF_TYPE_SOFTWARE)
360 if (pmu_type == PERF_TYPE_SOFTWARE) {
361 pmu_type = pos->attr.type;
364 if (pmu_type != pos->attr.type)
371 static void printout(struct perf_stat_config *config, int id, int nr,
372 struct perf_evsel *counter, double uval,
373 char *prefix, u64 run, u64 ena, double noise,
374 struct runtime_stat *st)
376 struct perf_stat_output_ctx out;
377 struct outstate os = {
378 .fh = config->output,
379 .prefix = prefix ? prefix : "",
384 print_metric_t pm = print_metric_std;
387 if (config->metric_only) {
388 nl = new_line_metric;
389 if (config->csv_output)
390 pm = print_metric_only_csv;
392 pm = print_metric_only;
396 if (config->csv_output && !config->metric_only) {
397 static int aggr_fields[] = {
405 pm = print_metric_csv;
408 os.nfields += aggr_fields[config->aggr_mode];
412 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
413 if (config->metric_only) {
414 pm(config, &os, NULL, "", "", 0);
417 aggr_printout(config, counter, id, nr);
419 fprintf(config->output, "%*s%s",
420 config->csv_output ? 0 : 18,
421 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
424 if (counter->supported) {
425 config->print_free_counters_hint = 1;
426 if (is_mixed_hw_group(counter))
427 config->print_mixed_hw_group_error = 1;
430 fprintf(config->output, "%-*s%s",
431 config->csv_output ? 0 : config->unit_width,
432 counter->unit, config->csv_sep);
434 fprintf(config->output, "%*s",
435 config->csv_output ? 0 : -25,
436 perf_evsel__name(counter));
438 print_cgroup(config, counter);
440 if (!config->csv_output)
441 pm(config, &os, NULL, NULL, "", 0);
442 print_noise(config, counter, noise);
443 print_running(config, run, ena);
444 if (config->csv_output)
445 pm(config, &os, NULL, NULL, "", 0);
449 if (!config->metric_only)
450 abs_printout(config, id, nr, counter, uval);
452 out.print_metric = pm;
455 out.force_header = false;
457 if (config->csv_output && !config->metric_only) {
458 print_noise(config, counter, noise);
459 print_running(config, run, ena);
462 perf_stat__print_shadow_stats(config, counter, uval,
463 first_shadow_cpu(config, counter, id),
464 &out, &config->metric_events, st);
465 if (!config->csv_output && !config->metric_only) {
466 print_noise(config, counter, noise);
467 print_running(config, run, ena);
471 static void aggr_update_shadow(struct perf_stat_config *config,
472 struct perf_evlist *evlist)
476 struct perf_evsel *counter;
478 for (s = 0; s < config->aggr_map->nr; s++) {
479 id = config->aggr_map->map[s];
480 evlist__for_each_entry(evlist, counter) {
482 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
483 s2 = config->aggr_get_id(config, evlist->cpus, cpu);
486 val += perf_counts(counter->counts, cpu, 0)->val;
488 perf_stat__update_shadow_stats(counter, val,
489 first_shadow_cpu(config, counter, id),
495 static void uniquify_event_name(struct perf_evsel *counter)
500 if (counter->uniquified_name ||
501 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
502 strlen(counter->pmu_name)))
505 config = strchr(counter->name, '/');
507 if (asprintf(&new_name,
508 "%s%s", counter->pmu_name, config) > 0) {
510 counter->name = new_name;
513 if (asprintf(&new_name,
514 "%s [%s]", counter->name, counter->pmu_name) > 0) {
516 counter->name = new_name;
520 counter->uniquified_name = true;
523 static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter,
524 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
528 struct perf_evlist *evlist = counter->evlist;
529 struct perf_evsel *alias;
531 alias = list_prepare_entry(counter, &(evlist->entries), node);
532 list_for_each_entry_continue (alias, &evlist->entries, node) {
533 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
534 alias->scale != counter->scale ||
535 alias->cgrp != counter->cgrp ||
536 strcmp(alias->unit, counter->unit) ||
537 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
539 alias->merged_stat = true;
540 cb(config, alias, data, false);
544 static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter,
545 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
549 if (counter->merged_stat)
551 cb(config, counter, data, true);
552 if (config->no_merge)
553 uniquify_event_name(counter);
554 else if (counter->auto_merge_stats)
555 collect_all_aliases(config, counter, cb, data);
566 static void aggr_cb(struct perf_stat_config *config,
567 struct perf_evsel *counter, void *data, bool first)
569 struct aggr_data *ad = data;
572 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
573 struct perf_counts_values *counts;
575 s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu);
580 counts = perf_counts(counter->counts, cpu, 0);
582 * When any result is bad, make them all to give
583 * consistent output in interval mode.
585 if (counts->ena == 0 || counts->run == 0 ||
586 counter->counts->scaled == -1) {
591 ad->val += counts->val;
592 ad->ena += counts->ena;
593 ad->run += counts->run;
597 static void print_aggr(struct perf_stat_config *config,
598 struct perf_evlist *evlist,
601 bool metric_only = config->metric_only;
602 FILE *output = config->output;
603 struct perf_evsel *counter;
609 if (!(config->aggr_map || config->aggr_get_id))
612 aggr_update_shadow(config, evlist);
615 * With metric_only everything is on a single line.
616 * Without each counter has its own line.
618 for (s = 0; s < config->aggr_map->nr; s++) {
620 if (prefix && metric_only)
621 fprintf(output, "%s", prefix);
623 ad.id = id = config->aggr_map->map[s];
625 evlist__for_each_entry(evlist, counter) {
626 ad.val = ad.ena = ad.run = 0;
628 if (!collect_data(config, counter, aggr_cb, &ad))
634 if (first && metric_only) {
636 aggr_printout(config, counter, id, nr);
638 if (prefix && !metric_only)
639 fprintf(output, "%s", prefix);
641 uval = val * counter->scale;
642 printout(config, id, nr, counter, uval, prefix,
643 run, ena, 1.0, &rt_stat);
652 static int cmp_val(const void *a, const void *b)
654 return ((struct perf_aggr_thread_value *)b)->val -
655 ((struct perf_aggr_thread_value *)a)->val;
658 static struct perf_aggr_thread_value *sort_aggr_thread(
659 struct perf_evsel *counter,
660 int nthreads, int ncpus,
662 struct target *_target)
664 int cpu, thread, i = 0;
666 struct perf_aggr_thread_value *buf;
668 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
672 for (thread = 0; thread < nthreads; thread++) {
673 u64 ena = 0, run = 0, val = 0;
675 for (cpu = 0; cpu < ncpus; cpu++) {
676 val += perf_counts(counter->counts, cpu, thread)->val;
677 ena += perf_counts(counter->counts, cpu, thread)->ena;
678 run += perf_counts(counter->counts, cpu, thread)->run;
681 uval = val * counter->scale;
684 * Skip value 0 when enabling --per-thread globally,
685 * otherwise too many 0 output.
687 if (uval == 0.0 && target__has_per_thread(_target))
690 buf[i].counter = counter;
699 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
707 static void print_aggr_thread(struct perf_stat_config *config,
708 struct target *_target,
709 struct perf_evsel *counter, char *prefix)
711 FILE *output = config->output;
712 int nthreads = thread_map__nr(counter->threads);
713 int ncpus = cpu_map__nr(counter->cpus);
714 int thread, sorted_threads, id;
715 struct perf_aggr_thread_value *buf;
717 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
719 perror("cannot sort aggr thread");
723 for (thread = 0; thread < sorted_threads; thread++) {
725 fprintf(output, "%s", prefix);
729 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
730 prefix, buf[thread].run, buf[thread].ena, 1.0,
733 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
734 prefix, buf[thread].run, buf[thread].ena, 1.0,
743 double avg, avg_enabled, avg_running;
746 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
747 struct perf_evsel *counter, void *data,
748 bool first __maybe_unused)
750 struct caggr_data *cd = data;
751 struct perf_stat_evsel *ps = counter->stats;
753 cd->avg += avg_stats(&ps->res_stats[0]);
754 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
755 cd->avg_running += avg_stats(&ps->res_stats[2]);
759 * Print out the results of a single counter:
760 * aggregated counts in system-wide mode
762 static void print_counter_aggr(struct perf_stat_config *config,
763 struct perf_evsel *counter, char *prefix)
765 bool metric_only = config->metric_only;
766 FILE *output = config->output;
768 struct caggr_data cd = { .avg = 0.0 };
770 if (!collect_data(config, counter, counter_aggr_cb, &cd))
773 if (prefix && !metric_only)
774 fprintf(output, "%s", prefix);
776 uval = cd.avg * counter->scale;
777 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
780 fprintf(output, "\n");
783 static void counter_cb(struct perf_stat_config *config __maybe_unused,
784 struct perf_evsel *counter, void *data,
785 bool first __maybe_unused)
787 struct aggr_data *ad = data;
789 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
790 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
791 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
795 * Print out the results of a single counter:
796 * does not use aggregated count in system-wide
798 static void print_counter(struct perf_stat_config *config,
799 struct perf_evsel *counter, char *prefix)
801 FILE *output = config->output;
806 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
807 struct aggr_data ad = { .cpu = cpu };
809 if (!collect_data(config, counter, counter_cb, &ad))
816 fprintf(output, "%s", prefix);
818 uval = val * counter->scale;
819 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
826 static void print_no_aggr_metric(struct perf_stat_config *config,
827 struct perf_evlist *evlist,
832 struct perf_evsel *counter;
836 nrcpus = evlist->cpus->nr;
837 for (cpu = 0; cpu < nrcpus; cpu++) {
841 fputs(prefix, config->output);
842 evlist__for_each_entry(evlist, counter) {
844 aggr_printout(config, counter, cpu, 0);
847 val = perf_counts(counter->counts, cpu, 0)->val;
848 ena = perf_counts(counter->counts, cpu, 0)->ena;
849 run = perf_counts(counter->counts, cpu, 0)->run;
851 uval = val * counter->scale;
852 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
855 fputc('\n', config->output);
859 static int aggr_header_lens[] = {
867 static const char *aggr_header_csv[] = {
868 [AGGR_CORE] = "core,cpus,",
869 [AGGR_SOCKET] = "socket,cpus",
870 [AGGR_NONE] = "cpu,",
871 [AGGR_THREAD] = "comm-pid,",
875 static void print_metric_headers(struct perf_stat_config *config,
876 struct perf_evlist *evlist,
877 const char *prefix, bool no_indent)
879 struct perf_stat_output_ctx out;
880 struct perf_evsel *counter;
881 struct outstate os = {
886 fprintf(config->output, "%s", prefix);
888 if (!config->csv_output && !no_indent)
889 fprintf(config->output, "%*s",
890 aggr_header_lens[config->aggr_mode], "");
891 if (config->csv_output) {
892 if (config->interval)
893 fputs("time,", config->output);
894 fputs(aggr_header_csv[config->aggr_mode], config->output);
897 /* Print metrics headers only */
898 evlist__for_each_entry(evlist, counter) {
901 out.print_metric = print_metric_header;
902 out.new_line = new_line_metric;
903 out.force_header = true;
905 perf_stat__print_shadow_stats(config, counter, 0,
908 &config->metric_events,
911 fputc('\n', config->output);
914 static void print_interval(struct perf_stat_config *config,
915 struct perf_evlist *evlist,
916 char *prefix, struct timespec *ts)
918 bool metric_only = config->metric_only;
919 unsigned int unit_width = config->unit_width;
920 FILE *output = config->output;
921 static int num_print_interval;
923 if (config->interval_clear)
926 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
928 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
929 switch (config->aggr_mode) {
931 fprintf(output, "# time socket cpus");
933 fprintf(output, " counts %*s events\n", unit_width, "unit");
936 fprintf(output, "# time core cpus");
938 fprintf(output, " counts %*s events\n", unit_width, "unit");
941 fprintf(output, "# time CPU ");
943 fprintf(output, " counts %*s events\n", unit_width, "unit");
946 fprintf(output, "# time comm-pid");
948 fprintf(output, " counts %*s events\n", unit_width, "unit");
952 fprintf(output, "# time");
954 fprintf(output, " counts %*s events\n", unit_width, "unit");
960 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
961 print_metric_headers(config, evlist, " ", true);
962 if (++num_print_interval == 25)
963 num_print_interval = 0;
966 static void print_header(struct perf_stat_config *config,
967 struct target *_target,
968 int argc, const char **argv)
970 FILE *output = config->output;
975 if (!config->csv_output) {
976 fprintf(output, "\n");
977 fprintf(output, " Performance counter stats for ");
978 if (_target->system_wide)
979 fprintf(output, "\'system wide");
980 else if (_target->cpu_list)
981 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
982 else if (!target__has_task(_target)) {
983 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
984 for (i = 1; argv && (i < argc); i++)
985 fprintf(output, " %s", argv[i]);
986 } else if (_target->pid)
987 fprintf(output, "process id \'%s", _target->pid);
989 fprintf(output, "thread id \'%s", _target->tid);
991 fprintf(output, "\'");
992 if (config->run_count > 1)
993 fprintf(output, " (%d runs)", config->run_count);
994 fprintf(output, ":\n\n");
998 static int get_precision(double num)
1003 return lround(ceil(-log10(num)));
1006 static void print_table(struct perf_stat_config *config,
1007 FILE *output, int precision, double avg)
1010 int idx, indent = 0;
1012 scnprintf(tmp, 64, " %17.*f", precision, avg);
1013 while (tmp[indent] == ' ')
1016 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1018 for (idx = 0; idx < config->run_count; idx++) {
1019 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1020 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1022 fprintf(output, " %17.*f (%+.*f) ",
1023 precision, run, precision, run - avg);
1025 for (h = 0; h < n; h++)
1026 fprintf(output, "#");
1028 fprintf(output, "\n");
1031 fprintf(output, "\n%*s# Final result:\n", indent, "");
1034 static double timeval2double(struct timeval *t)
1036 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1039 static void print_footer(struct perf_stat_config *config)
1041 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1042 FILE *output = config->output;
1045 if (!config->null_run)
1046 fprintf(output, "\n");
1048 if (config->run_count == 1) {
1049 fprintf(output, " %17.9f seconds time elapsed", avg);
1051 if (config->ru_display) {
1052 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1053 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1055 fprintf(output, "\n\n");
1056 fprintf(output, " %17.9f seconds user\n", ru_utime);
1057 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1060 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1062 * Display at most 2 more significant
1063 * digits than the stddev inaccuracy.
1065 int precision = get_precision(sd) + 2;
1067 if (config->walltime_run_table)
1068 print_table(config, output, precision, avg);
1070 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1071 precision, avg, precision, sd);
1073 print_noise_pct(config, sd, avg);
1075 fprintf(output, "\n\n");
1077 if (config->print_free_counters_hint &&
1078 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1081 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1082 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1084 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1086 if (config->print_mixed_hw_group_error)
1088 "The events in group usually have to be from "
1089 "the same PMU. Try reorganizing the group.\n");
1093 perf_evlist__print_counters(struct perf_evlist *evlist,
1094 struct perf_stat_config *config,
1095 struct target *_target,
1096 struct timespec *ts,
1097 int argc, const char **argv)
1099 bool metric_only = config->metric_only;
1100 int interval = config->interval;
1101 struct perf_evsel *counter;
1102 char buf[64], *prefix = NULL;
1105 print_interval(config, evlist, prefix = buf, ts);
1107 print_header(config, _target, argc, argv);
1110 static int num_print_iv;
1112 if (num_print_iv == 0 && !interval)
1113 print_metric_headers(config, evlist, prefix, false);
1114 if (num_print_iv++ == 25)
1116 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1117 fprintf(config->output, "%s", prefix);
1120 switch (config->aggr_mode) {
1123 print_aggr(config, evlist, prefix);
1126 evlist__for_each_entry(evlist, counter) {
1127 print_aggr_thread(config, _target, counter, prefix);
1131 evlist__for_each_entry(evlist, counter) {
1132 print_counter_aggr(config, counter, prefix);
1135 fputc('\n', config->output);
1139 print_no_aggr_metric(config, evlist, prefix);
1141 evlist__for_each_entry(evlist, counter) {
1142 print_counter(config, counter, prefix);
1151 if (!interval && !config->csv_output)
1152 print_footer(config);
1154 fflush(config->output);