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_counter_aggrdata(struct perf_stat_config *config,
598 struct perf_evsel *counter, int s,
599 char *prefix, bool metric_only,
603 FILE *output = config->output;
608 ad.id = id = config->aggr_map->map[s];
609 ad.val = ad.ena = ad.run = 0;
611 if (!collect_data(config, counter, aggr_cb, &ad))
618 if (*first && metric_only) {
620 aggr_printout(config, counter, id, nr);
622 if (prefix && !metric_only)
623 fprintf(output, "%s", prefix);
625 uval = val * counter->scale;
626 printout(config, id, nr, counter, uval, prefix,
627 run, ena, 1.0, &rt_stat);
632 static void print_aggr(struct perf_stat_config *config,
633 struct perf_evlist *evlist,
636 bool metric_only = config->metric_only;
637 FILE *output = config->output;
638 struct perf_evsel *counter;
642 if (!(config->aggr_map || config->aggr_get_id))
645 aggr_update_shadow(config, evlist);
648 * With metric_only everything is on a single line.
649 * Without each counter has its own line.
651 for (s = 0; s < config->aggr_map->nr; s++) {
652 if (prefix && metric_only)
653 fprintf(output, "%s", prefix);
656 evlist__for_each_entry(evlist, counter) {
657 print_counter_aggrdata(config, counter, s,
666 static int cmp_val(const void *a, const void *b)
668 return ((struct perf_aggr_thread_value *)b)->val -
669 ((struct perf_aggr_thread_value *)a)->val;
672 static struct perf_aggr_thread_value *sort_aggr_thread(
673 struct perf_evsel *counter,
674 int nthreads, int ncpus,
676 struct target *_target)
678 int cpu, thread, i = 0;
680 struct perf_aggr_thread_value *buf;
682 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
686 for (thread = 0; thread < nthreads; thread++) {
687 u64 ena = 0, run = 0, val = 0;
689 for (cpu = 0; cpu < ncpus; cpu++) {
690 val += perf_counts(counter->counts, cpu, thread)->val;
691 ena += perf_counts(counter->counts, cpu, thread)->ena;
692 run += perf_counts(counter->counts, cpu, thread)->run;
695 uval = val * counter->scale;
698 * Skip value 0 when enabling --per-thread globally,
699 * otherwise too many 0 output.
701 if (uval == 0.0 && target__has_per_thread(_target))
704 buf[i].counter = counter;
713 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
721 static void print_aggr_thread(struct perf_stat_config *config,
722 struct target *_target,
723 struct perf_evsel *counter, char *prefix)
725 FILE *output = config->output;
726 int nthreads = thread_map__nr(counter->threads);
727 int ncpus = cpu_map__nr(counter->cpus);
728 int thread, sorted_threads, id;
729 struct perf_aggr_thread_value *buf;
731 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
733 perror("cannot sort aggr thread");
737 for (thread = 0; thread < sorted_threads; thread++) {
739 fprintf(output, "%s", prefix);
743 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
744 prefix, buf[thread].run, buf[thread].ena, 1.0,
747 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
748 prefix, buf[thread].run, buf[thread].ena, 1.0,
757 double avg, avg_enabled, avg_running;
760 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
761 struct perf_evsel *counter, void *data,
762 bool first __maybe_unused)
764 struct caggr_data *cd = data;
765 struct perf_stat_evsel *ps = counter->stats;
767 cd->avg += avg_stats(&ps->res_stats[0]);
768 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
769 cd->avg_running += avg_stats(&ps->res_stats[2]);
773 * Print out the results of a single counter:
774 * aggregated counts in system-wide mode
776 static void print_counter_aggr(struct perf_stat_config *config,
777 struct perf_evsel *counter, char *prefix)
779 bool metric_only = config->metric_only;
780 FILE *output = config->output;
782 struct caggr_data cd = { .avg = 0.0 };
784 if (!collect_data(config, counter, counter_aggr_cb, &cd))
787 if (prefix && !metric_only)
788 fprintf(output, "%s", prefix);
790 uval = cd.avg * counter->scale;
791 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
794 fprintf(output, "\n");
797 static void counter_cb(struct perf_stat_config *config __maybe_unused,
798 struct perf_evsel *counter, void *data,
799 bool first __maybe_unused)
801 struct aggr_data *ad = data;
803 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
804 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
805 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
809 * Print out the results of a single counter:
810 * does not use aggregated count in system-wide
812 static void print_counter(struct perf_stat_config *config,
813 struct perf_evsel *counter, char *prefix)
815 FILE *output = config->output;
820 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
821 struct aggr_data ad = { .cpu = cpu };
823 if (!collect_data(config, counter, counter_cb, &ad))
830 fprintf(output, "%s", prefix);
832 uval = val * counter->scale;
833 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
840 static void print_no_aggr_metric(struct perf_stat_config *config,
841 struct perf_evlist *evlist,
846 struct perf_evsel *counter;
850 nrcpus = evlist->cpus->nr;
851 for (cpu = 0; cpu < nrcpus; cpu++) {
855 fputs(prefix, config->output);
856 evlist__for_each_entry(evlist, counter) {
858 aggr_printout(config, counter, cpu, 0);
861 val = perf_counts(counter->counts, cpu, 0)->val;
862 ena = perf_counts(counter->counts, cpu, 0)->ena;
863 run = perf_counts(counter->counts, cpu, 0)->run;
865 uval = val * counter->scale;
866 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
869 fputc('\n', config->output);
873 static int aggr_header_lens[] = {
881 static const char *aggr_header_csv[] = {
882 [AGGR_CORE] = "core,cpus,",
883 [AGGR_SOCKET] = "socket,cpus",
884 [AGGR_NONE] = "cpu,",
885 [AGGR_THREAD] = "comm-pid,",
889 static void print_metric_headers(struct perf_stat_config *config,
890 struct perf_evlist *evlist,
891 const char *prefix, bool no_indent)
893 struct perf_stat_output_ctx out;
894 struct perf_evsel *counter;
895 struct outstate os = {
900 fprintf(config->output, "%s", prefix);
902 if (!config->csv_output && !no_indent)
903 fprintf(config->output, "%*s",
904 aggr_header_lens[config->aggr_mode], "");
905 if (config->csv_output) {
906 if (config->interval)
907 fputs("time,", config->output);
908 fputs(aggr_header_csv[config->aggr_mode], config->output);
911 /* Print metrics headers only */
912 evlist__for_each_entry(evlist, counter) {
915 out.print_metric = print_metric_header;
916 out.new_line = new_line_metric;
917 out.force_header = true;
919 perf_stat__print_shadow_stats(config, counter, 0,
922 &config->metric_events,
925 fputc('\n', config->output);
928 static void print_interval(struct perf_stat_config *config,
929 struct perf_evlist *evlist,
930 char *prefix, struct timespec *ts)
932 bool metric_only = config->metric_only;
933 unsigned int unit_width = config->unit_width;
934 FILE *output = config->output;
935 static int num_print_interval;
937 if (config->interval_clear)
940 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
942 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
943 switch (config->aggr_mode) {
945 fprintf(output, "# time socket cpus");
947 fprintf(output, " counts %*s events\n", unit_width, "unit");
950 fprintf(output, "# time core cpus");
952 fprintf(output, " counts %*s events\n", unit_width, "unit");
955 fprintf(output, "# time CPU ");
957 fprintf(output, " counts %*s events\n", unit_width, "unit");
960 fprintf(output, "# time comm-pid");
962 fprintf(output, " counts %*s events\n", unit_width, "unit");
966 fprintf(output, "# time");
968 fprintf(output, " counts %*s events\n", unit_width, "unit");
974 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
975 print_metric_headers(config, evlist, " ", true);
976 if (++num_print_interval == 25)
977 num_print_interval = 0;
980 static void print_header(struct perf_stat_config *config,
981 struct target *_target,
982 int argc, const char **argv)
984 FILE *output = config->output;
989 if (!config->csv_output) {
990 fprintf(output, "\n");
991 fprintf(output, " Performance counter stats for ");
992 if (_target->system_wide)
993 fprintf(output, "\'system wide");
994 else if (_target->cpu_list)
995 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
996 else if (!target__has_task(_target)) {
997 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
998 for (i = 1; argv && (i < argc); i++)
999 fprintf(output, " %s", argv[i]);
1000 } else if (_target->pid)
1001 fprintf(output, "process id \'%s", _target->pid);
1003 fprintf(output, "thread id \'%s", _target->tid);
1005 fprintf(output, "\'");
1006 if (config->run_count > 1)
1007 fprintf(output, " (%d runs)", config->run_count);
1008 fprintf(output, ":\n\n");
1012 static int get_precision(double num)
1017 return lround(ceil(-log10(num)));
1020 static void print_table(struct perf_stat_config *config,
1021 FILE *output, int precision, double avg)
1024 int idx, indent = 0;
1026 scnprintf(tmp, 64, " %17.*f", precision, avg);
1027 while (tmp[indent] == ' ')
1030 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1032 for (idx = 0; idx < config->run_count; idx++) {
1033 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1034 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1036 fprintf(output, " %17.*f (%+.*f) ",
1037 precision, run, precision, run - avg);
1039 for (h = 0; h < n; h++)
1040 fprintf(output, "#");
1042 fprintf(output, "\n");
1045 fprintf(output, "\n%*s# Final result:\n", indent, "");
1048 static double timeval2double(struct timeval *t)
1050 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1053 static void print_footer(struct perf_stat_config *config)
1055 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1056 FILE *output = config->output;
1059 if (!config->null_run)
1060 fprintf(output, "\n");
1062 if (config->run_count == 1) {
1063 fprintf(output, " %17.9f seconds time elapsed", avg);
1065 if (config->ru_display) {
1066 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1067 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1069 fprintf(output, "\n\n");
1070 fprintf(output, " %17.9f seconds user\n", ru_utime);
1071 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1074 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1076 * Display at most 2 more significant
1077 * digits than the stddev inaccuracy.
1079 int precision = get_precision(sd) + 2;
1081 if (config->walltime_run_table)
1082 print_table(config, output, precision, avg);
1084 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1085 precision, avg, precision, sd);
1087 print_noise_pct(config, sd, avg);
1089 fprintf(output, "\n\n");
1091 if (config->print_free_counters_hint &&
1092 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1095 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1096 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1098 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1100 if (config->print_mixed_hw_group_error)
1102 "The events in group usually have to be from "
1103 "the same PMU. Try reorganizing the group.\n");
1107 perf_evlist__print_counters(struct perf_evlist *evlist,
1108 struct perf_stat_config *config,
1109 struct target *_target,
1110 struct timespec *ts,
1111 int argc, const char **argv)
1113 bool metric_only = config->metric_only;
1114 int interval = config->interval;
1115 struct perf_evsel *counter;
1116 char buf[64], *prefix = NULL;
1119 print_interval(config, evlist, prefix = buf, ts);
1121 print_header(config, _target, argc, argv);
1124 static int num_print_iv;
1126 if (num_print_iv == 0 && !interval)
1127 print_metric_headers(config, evlist, prefix, false);
1128 if (num_print_iv++ == 25)
1130 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1131 fprintf(config->output, "%s", prefix);
1134 switch (config->aggr_mode) {
1137 print_aggr(config, evlist, prefix);
1140 evlist__for_each_entry(evlist, counter) {
1141 print_aggr_thread(config, _target, counter, prefix);
1145 evlist__for_each_entry(evlist, counter) {
1146 print_counter_aggr(config, counter, prefix);
1149 fputc('\n', config->output);
1153 print_no_aggr_metric(config, evlist, prefix);
1155 evlist__for_each_entry(evlist, counter) {
1156 print_counter(config, counter, prefix);
1165 if (!interval && !config->csv_output)
1166 print_footer(config);
1168 fflush(config->output);