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 #include "pmu-hybrid.h"
23 #define CNTR_NOT_SUPPORTED "<not supported>"
24 #define CNTR_NOT_COUNTED "<not counted>"
26 static void print_running(struct perf_stat_config *config,
29 if (config->csv_output) {
30 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
34 ena ? 100.0 * run / ena : 100.0);
35 } else if (run != ena) {
36 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
40 static void print_noise_pct(struct perf_stat_config *config,
41 double total, double avg)
43 double pct = rel_stddev_stats(total, avg);
45 if (config->csv_output)
46 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
48 fprintf(config->output, " ( +-%6.2f%% )", pct);
51 static void print_noise(struct perf_stat_config *config,
52 struct evsel *evsel, double avg)
54 struct perf_stat_evsel *ps;
56 if (config->run_count == 1)
60 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
63 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
66 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
67 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
72 static void aggr_printout(struct perf_stat_config *config,
73 struct evsel *evsel, struct aggr_cpu_id id, int nr)
75 switch (config->aggr_mode) {
77 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
80 config->csv_output ? 0 : -8,
83 config->csv_output ? 0 : 4,
88 fprintf(config->output, "S%d-D%*d%s%*d%s",
90 config->csv_output ? 0 : -8,
93 config->csv_output ? 0 : 4,
98 fprintf(config->output, "S%*d%s%*d%s",
99 config->csv_output ? 0 : -5,
102 config->csv_output ? 0 : 4,
107 fprintf(config->output, "N%*d%s%*d%s",
108 config->csv_output ? 0 : -5,
111 config->csv_output ? 0 : 4,
116 if (evsel->percore && !config->percore_show_thread) {
117 fprintf(config->output, "S%d-D%d-C%*d%s",
120 config->csv_output ? 0 : -3,
121 id.core, config->csv_sep);
122 } else if (id.core > -1) {
123 fprintf(config->output, "CPU%*d%s",
124 config->csv_output ? 0 : -7,
125 evsel__cpus(evsel)->map[id.core],
130 fprintf(config->output, "%*s-%*d%s",
131 config->csv_output ? 0 : 16,
132 perf_thread_map__comm(evsel->core.threads, id.thread),
133 config->csv_output ? 0 : -8,
134 perf_thread_map__pid(evsel->core.threads, id.thread),
150 struct aggr_cpu_id id;
154 #define METRIC_LEN 35
156 static void new_line_std(struct perf_stat_config *config __maybe_unused,
159 struct outstate *os = ctx;
164 static void do_new_line_std(struct perf_stat_config *config,
168 fputs(os->prefix, os->fh);
169 aggr_printout(config, os->evsel, os->id, os->nr);
170 if (config->aggr_mode == AGGR_NONE)
171 fprintf(os->fh, " ");
172 fprintf(os->fh, " ");
175 static void print_metric_std(struct perf_stat_config *config,
176 void *ctx, const char *color, const char *fmt,
177 const char *unit, double val)
179 struct outstate *os = ctx;
182 bool newline = os->newline;
186 if (unit == NULL || fmt == NULL) {
187 fprintf(out, "%-*s", METRIC_LEN, "");
192 do_new_line_std(config, os);
194 n = fprintf(out, " # ");
196 n += color_fprintf(out, color, fmt, val);
198 n += fprintf(out, fmt, val);
199 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
202 static void new_line_csv(struct perf_stat_config *config, void *ctx)
204 struct outstate *os = ctx;
209 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
210 aggr_printout(config, os->evsel, os->id, os->nr);
211 for (i = 0; i < os->nfields; i++)
212 fputs(config->csv_sep, os->fh);
215 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
217 const char *color __maybe_unused,
218 const char *fmt, const char *unit, double val)
220 struct outstate *os = ctx;
222 char buf[64], *vals, *ends;
224 if (unit == NULL || fmt == NULL) {
225 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
228 snprintf(buf, sizeof(buf), fmt, val);
229 ends = vals = skip_spaces(buf);
230 while (isdigit(*ends) || *ends == '.')
233 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
236 /* Filter out some columns that don't work well in metrics only mode */
238 static bool valid_only_metric(const char *unit)
242 if (strstr(unit, "/sec") ||
243 strstr(unit, "CPUs utilized"))
248 static const char *fixunit(char *buf, struct evsel *evsel,
251 if (!strncmp(unit, "of all", 6)) {
252 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
259 static void print_metric_only(struct perf_stat_config *config,
260 void *ctx, const char *color, const char *fmt,
261 const char *unit, double val)
263 struct outstate *os = ctx;
265 char buf[1024], str[1024];
266 unsigned mlen = config->metric_only_len;
268 if (!valid_only_metric(unit))
270 unit = fixunit(buf, os->evsel, unit);
271 if (mlen < strlen(unit))
272 mlen = strlen(unit) + 1;
275 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
277 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
278 fprintf(out, "%*s ", mlen, str);
281 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
282 void *ctx, const char *color __maybe_unused,
284 const char *unit, double val)
286 struct outstate *os = ctx;
288 char buf[64], *vals, *ends;
291 if (!valid_only_metric(unit))
293 unit = fixunit(tbuf, os->evsel, unit);
294 snprintf(buf, sizeof buf, fmt, val);
295 ends = vals = skip_spaces(buf);
296 while (isdigit(*ends) || *ends == '.')
299 fprintf(out, "%s%s", vals, config->csv_sep);
302 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
303 void *ctx __maybe_unused)
307 static void print_metric_header(struct perf_stat_config *config,
308 void *ctx, const char *color __maybe_unused,
309 const char *fmt __maybe_unused,
310 const char *unit, double val __maybe_unused)
312 struct outstate *os = ctx;
315 /* In case of iostat, print metric header for first root port only */
316 if (config->iostat_run &&
317 os->evsel->priv != os->evsel->evlist->selected->priv)
320 if (!valid_only_metric(unit))
322 unit = fixunit(tbuf, os->evsel, unit);
323 if (config->csv_output)
324 fprintf(os->fh, "%s%s", unit, config->csv_sep);
326 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
329 static int first_shadow_cpu(struct perf_stat_config *config,
330 struct evsel *evsel, struct aggr_cpu_id id)
332 struct evlist *evlist = evsel->evlist;
335 if (config->aggr_mode == AGGR_NONE)
338 if (!config->aggr_get_id)
341 for (i = 0; i < evsel__nr_cpus(evsel); i++) {
342 int cpu2 = evsel__cpus(evsel)->map[i];
344 if (cpu_map__compare_aggr_cpu_id(
345 config->aggr_get_id(config, evlist->core.cpus, cpu2),
353 static void abs_printout(struct perf_stat_config *config,
354 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
356 FILE *output = config->output;
357 double sc = evsel->scale;
360 if (config->csv_output) {
361 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
364 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
366 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
369 aggr_printout(config, evsel, id, nr);
371 fprintf(output, fmt, avg, config->csv_sep);
374 fprintf(output, "%-*s%s",
375 config->csv_output ? 0 : config->unit_width,
376 evsel->unit, config->csv_sep);
378 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
380 print_cgroup(config, evsel);
383 static bool is_mixed_hw_group(struct evsel *counter)
385 struct evlist *evlist = counter->evlist;
386 u32 pmu_type = counter->core.attr.type;
389 if (counter->core.nr_members < 2)
392 evlist__for_each_entry(evlist, pos) {
393 /* software events can be part of any hardware group */
394 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
396 if (pmu_type == PERF_TYPE_SOFTWARE) {
397 pmu_type = pos->core.attr.type;
400 if (pmu_type != pos->core.attr.type)
407 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
408 struct evsel *counter, double uval,
409 char *prefix, u64 run, u64 ena, double noise,
410 struct runtime_stat *st)
412 struct perf_stat_output_ctx out;
413 struct outstate os = {
414 .fh = config->output,
415 .prefix = prefix ? prefix : "",
420 print_metric_t pm = print_metric_std;
423 if (config->metric_only) {
424 nl = new_line_metric;
425 if (config->csv_output)
426 pm = print_metric_only_csv;
428 pm = print_metric_only;
432 if (config->csv_output && !config->metric_only) {
433 static int aggr_fields[] = {
442 pm = print_metric_csv;
445 os.nfields += aggr_fields[config->aggr_mode];
450 if (!config->no_csv_summary && config->csv_output &&
451 config->summary && !config->interval) {
452 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
455 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
456 if (config->metric_only) {
457 pm(config, &os, NULL, "", "", 0);
460 aggr_printout(config, counter, id, nr);
462 fprintf(config->output, "%*s%s",
463 config->csv_output ? 0 : 18,
464 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
467 if (counter->supported) {
468 config->print_free_counters_hint = 1;
469 if (is_mixed_hw_group(counter))
470 config->print_mixed_hw_group_error = 1;
473 fprintf(config->output, "%-*s%s",
474 config->csv_output ? 0 : config->unit_width,
475 counter->unit, config->csv_sep);
477 fprintf(config->output, "%*s",
478 config->csv_output ? 0 : -25, evsel__name(counter));
480 print_cgroup(config, counter);
482 if (!config->csv_output)
483 pm(config, &os, NULL, NULL, "", 0);
484 print_noise(config, counter, noise);
485 print_running(config, run, ena);
486 if (config->csv_output)
487 pm(config, &os, NULL, NULL, "", 0);
491 if (!config->metric_only)
492 abs_printout(config, id, nr, counter, uval);
494 out.print_metric = pm;
497 out.force_header = false;
499 if (config->csv_output && !config->metric_only) {
500 print_noise(config, counter, noise);
501 print_running(config, run, ena);
504 perf_stat__print_shadow_stats(config, counter, uval,
505 first_shadow_cpu(config, counter, id),
506 &out, &config->metric_events, st);
507 if (!config->csv_output && !config->metric_only) {
508 print_noise(config, counter, noise);
509 print_running(config, run, ena);
513 static void aggr_update_shadow(struct perf_stat_config *config,
514 struct evlist *evlist)
517 struct aggr_cpu_id s2, id;
519 struct evsel *counter;
521 for (s = 0; s < config->aggr_map->nr; s++) {
522 id = config->aggr_map->map[s];
523 evlist__for_each_entry(evlist, counter) {
525 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
526 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
527 if (!cpu_map__compare_aggr_cpu_id(s2, id))
529 val += perf_counts(counter->counts, cpu, 0)->val;
531 perf_stat__update_shadow_stats(counter, val,
532 first_shadow_cpu(config, counter, id),
538 static void uniquify_event_name(struct evsel *counter)
544 if (counter->uniquified_name || counter->use_config_name ||
545 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
546 strlen(counter->pmu_name)))
549 config = strchr(counter->name, '/');
551 if (asprintf(&new_name,
552 "%s%s", counter->pmu_name, config) > 0) {
554 counter->name = new_name;
557 if (perf_pmu__has_hybrid()) {
558 ret = asprintf(&new_name, "%s/%s/",
559 counter->pmu_name, counter->name);
561 ret = asprintf(&new_name, "%s [%s]",
562 counter->name, counter->pmu_name);
567 counter->name = new_name;
571 counter->uniquified_name = true;
574 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
575 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
579 struct evlist *evlist = counter->evlist;
582 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
583 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
584 if (strcmp(evsel__name(alias), evsel__name(counter)) ||
585 alias->scale != counter->scale ||
586 alias->cgrp != counter->cgrp ||
587 strcmp(alias->unit, counter->unit) ||
588 evsel__is_clock(alias) != evsel__is_clock(counter) ||
589 !strcmp(alias->pmu_name, counter->pmu_name))
591 alias->merged_stat = true;
592 cb(config, alias, data, false);
596 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
597 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
601 if (counter->merged_stat)
603 cb(config, counter, data, true);
604 if (config->no_merge)
605 uniquify_event_name(counter);
606 else if (counter->auto_merge_stats)
607 collect_all_aliases(config, counter, cb, data);
613 struct aggr_cpu_id id;
618 static void aggr_cb(struct perf_stat_config *config,
619 struct evsel *counter, void *data, bool first)
621 struct aggr_data *ad = data;
623 struct aggr_cpu_id s2;
625 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
626 struct perf_counts_values *counts;
628 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
629 if (!cpu_map__compare_aggr_cpu_id(s2, ad->id))
633 counts = perf_counts(counter->counts, cpu, 0);
635 * When any result is bad, make them all to give
636 * consistent output in interval mode.
638 if (counts->ena == 0 || counts->run == 0 ||
639 counter->counts->scaled == -1) {
644 ad->val += counts->val;
645 ad->ena += counts->ena;
646 ad->run += counts->run;
650 static void print_counter_aggrdata(struct perf_stat_config *config,
651 struct evsel *counter, int s,
652 char *prefix, bool metric_only,
653 bool *first, int cpu)
656 FILE *output = config->output;
659 struct aggr_cpu_id id;
662 ad.id = id = config->aggr_map->map[s];
663 ad.val = ad.ena = ad.run = 0;
665 if (!collect_data(config, counter, aggr_cb, &ad))
668 if (perf_pmu__has_hybrid() && ad.ena == 0)
675 if (*first && metric_only) {
677 aggr_printout(config, counter, id, nr);
679 if (prefix && !metric_only)
680 fprintf(output, "%s", prefix);
682 uval = val * counter->scale;
684 id = cpu_map__empty_aggr_cpu_id();
687 printout(config, id, nr, counter, uval,
688 prefix, run, ena, 1.0, &rt_stat);
693 static void print_aggr(struct perf_stat_config *config,
694 struct evlist *evlist,
697 bool metric_only = config->metric_only;
698 FILE *output = config->output;
699 struct evsel *counter;
703 if (!config->aggr_map || !config->aggr_get_id)
706 aggr_update_shadow(config, evlist);
709 * With metric_only everything is on a single line.
710 * Without each counter has its own line.
712 for (s = 0; s < config->aggr_map->nr; s++) {
713 if (prefix && metric_only)
714 fprintf(output, "%s", prefix);
717 evlist__for_each_entry(evlist, counter) {
718 print_counter_aggrdata(config, counter, s,
727 static int cmp_val(const void *a, const void *b)
729 return ((struct perf_aggr_thread_value *)b)->val -
730 ((struct perf_aggr_thread_value *)a)->val;
733 static struct perf_aggr_thread_value *sort_aggr_thread(
734 struct evsel *counter,
735 int nthreads, int ncpus,
737 struct target *_target)
739 int cpu, thread, i = 0;
741 struct perf_aggr_thread_value *buf;
743 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
747 for (thread = 0; thread < nthreads; thread++) {
748 u64 ena = 0, run = 0, val = 0;
750 for (cpu = 0; cpu < ncpus; cpu++) {
751 val += perf_counts(counter->counts, cpu, thread)->val;
752 ena += perf_counts(counter->counts, cpu, thread)->ena;
753 run += perf_counts(counter->counts, cpu, thread)->run;
756 uval = val * counter->scale;
759 * Skip value 0 when enabling --per-thread globally,
760 * otherwise too many 0 output.
762 if (uval == 0.0 && target__has_per_thread(_target))
765 buf[i].counter = counter;
766 buf[i].id = cpu_map__empty_aggr_cpu_id();
767 buf[i].id.thread = thread;
775 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
783 static void print_aggr_thread(struct perf_stat_config *config,
784 struct target *_target,
785 struct evsel *counter, char *prefix)
787 FILE *output = config->output;
788 int nthreads = perf_thread_map__nr(counter->core.threads);
789 int ncpus = perf_cpu_map__nr(counter->core.cpus);
790 int thread, sorted_threads;
791 struct aggr_cpu_id id;
792 struct perf_aggr_thread_value *buf;
794 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
796 perror("cannot sort aggr thread");
800 for (thread = 0; thread < sorted_threads; thread++) {
802 fprintf(output, "%s", prefix);
806 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
807 prefix, buf[thread].run, buf[thread].ena, 1.0,
808 &config->stats[id.thread]);
810 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
811 prefix, buf[thread].run, buf[thread].ena, 1.0,
820 double avg, avg_enabled, avg_running;
823 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
824 struct evsel *counter, void *data,
825 bool first __maybe_unused)
827 struct caggr_data *cd = data;
828 struct perf_stat_evsel *ps = counter->stats;
830 cd->avg += avg_stats(&ps->res_stats[0]);
831 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
832 cd->avg_running += avg_stats(&ps->res_stats[2]);
836 * Print out the results of a single counter:
837 * aggregated counts in system-wide mode
839 static void print_counter_aggr(struct perf_stat_config *config,
840 struct evsel *counter, char *prefix)
842 bool metric_only = config->metric_only;
843 FILE *output = config->output;
845 struct caggr_data cd = { .avg = 0.0 };
847 if (!collect_data(config, counter, counter_aggr_cb, &cd))
850 if (prefix && !metric_only)
851 fprintf(output, "%s", prefix);
853 uval = cd.avg * counter->scale;
854 printout(config, cpu_map__empty_aggr_cpu_id(), 0, counter, uval, prefix, cd.avg_running,
855 cd.avg_enabled, cd.avg, &rt_stat);
857 fprintf(output, "\n");
860 static void counter_cb(struct perf_stat_config *config __maybe_unused,
861 struct evsel *counter, void *data,
862 bool first __maybe_unused)
864 struct aggr_data *ad = data;
866 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
867 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
868 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
872 * Print out the results of a single counter:
873 * does not use aggregated count in system-wide
875 static void print_counter(struct perf_stat_config *config,
876 struct evsel *counter, char *prefix)
878 FILE *output = config->output;
882 struct aggr_cpu_id id;
884 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
885 struct aggr_data ad = { .cpu = cpu };
887 if (!collect_data(config, counter, counter_cb, &ad))
894 fprintf(output, "%s", prefix);
896 uval = val * counter->scale;
897 id = cpu_map__empty_aggr_cpu_id();
899 printout(config, id, 0, counter, uval, prefix,
900 run, ena, 1.0, &rt_stat);
906 static void print_no_aggr_metric(struct perf_stat_config *config,
907 struct evlist *evlist,
912 struct evsel *counter;
915 struct aggr_cpu_id id;
917 nrcpus = evlist->core.cpus->nr;
918 for (cpu = 0; cpu < nrcpus; cpu++) {
922 fputs(prefix, config->output);
923 evlist__for_each_entry(evlist, counter) {
924 id = cpu_map__empty_aggr_cpu_id();
927 aggr_printout(config, counter, id, 0);
930 val = perf_counts(counter->counts, cpu, 0)->val;
931 ena = perf_counts(counter->counts, cpu, 0)->ena;
932 run = perf_counts(counter->counts, cpu, 0)->run;
934 uval = val * counter->scale;
935 printout(config, id, 0, counter, uval, prefix,
936 run, ena, 1.0, &rt_stat);
938 fputc('\n', config->output);
942 static int aggr_header_lens[] = {
951 static const char *aggr_header_csv[] = {
952 [AGGR_CORE] = "core,cpus,",
953 [AGGR_DIE] = "die,cpus",
954 [AGGR_SOCKET] = "socket,cpus",
955 [AGGR_NONE] = "cpu,",
956 [AGGR_THREAD] = "comm-pid,",
960 static void print_metric_headers(struct perf_stat_config *config,
961 struct evlist *evlist,
962 const char *prefix, bool no_indent)
964 struct perf_stat_output_ctx out;
965 struct evsel *counter;
966 struct outstate os = {
971 fprintf(config->output, "%s", prefix);
973 if (!config->csv_output && !no_indent)
974 fprintf(config->output, "%*s",
975 aggr_header_lens[config->aggr_mode], "");
976 if (config->csv_output) {
977 if (config->interval)
978 fputs("time,", config->output);
979 if (!config->iostat_run)
980 fputs(aggr_header_csv[config->aggr_mode], config->output);
982 if (config->iostat_run)
983 iostat_print_header_prefix(config);
985 /* Print metrics headers only */
986 evlist__for_each_entry(evlist, counter) {
989 out.print_metric = print_metric_header;
990 out.new_line = new_line_metric;
991 out.force_header = true;
992 perf_stat__print_shadow_stats(config, counter, 0,
995 &config->metric_events,
998 fputc('\n', config->output);
1001 static void print_interval(struct perf_stat_config *config,
1002 struct evlist *evlist,
1003 char *prefix, struct timespec *ts)
1005 bool metric_only = config->metric_only;
1006 unsigned int unit_width = config->unit_width;
1007 FILE *output = config->output;
1008 static int num_print_interval;
1010 if (config->interval_clear)
1011 puts(CONSOLE_CLEAR);
1013 if (!config->iostat_run)
1014 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1016 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1017 switch (config->aggr_mode) {
1019 fprintf(output, "# time node cpus");
1021 fprintf(output, " counts %*s events\n", unit_width, "unit");
1024 fprintf(output, "# time socket cpus");
1026 fprintf(output, " counts %*s events\n", unit_width, "unit");
1029 fprintf(output, "# time die cpus");
1031 fprintf(output, " counts %*s events\n", unit_width, "unit");
1034 fprintf(output, "# time core cpus");
1036 fprintf(output, " counts %*s events\n", unit_width, "unit");
1039 fprintf(output, "# time CPU ");
1041 fprintf(output, " counts %*s events\n", unit_width, "unit");
1044 fprintf(output, "# time comm-pid");
1046 fprintf(output, " counts %*s events\n", unit_width, "unit");
1050 if (!config->iostat_run) {
1051 fprintf(output, "# time");
1053 fprintf(output, " counts %*s events\n", unit_width, "unit");
1060 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1061 print_metric_headers(config, evlist, " ", true);
1062 if (++num_print_interval == 25)
1063 num_print_interval = 0;
1066 static void print_header(struct perf_stat_config *config,
1067 struct target *_target,
1068 int argc, const char **argv)
1070 FILE *output = config->output;
1075 if (!config->csv_output) {
1076 fprintf(output, "\n");
1077 fprintf(output, " Performance counter stats for ");
1078 if (_target->bpf_str)
1079 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1080 else if (_target->system_wide)
1081 fprintf(output, "\'system wide");
1082 else if (_target->cpu_list)
1083 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1084 else if (!target__has_task(_target)) {
1085 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1086 for (i = 1; argv && (i < argc); i++)
1087 fprintf(output, " %s", argv[i]);
1088 } else if (_target->pid)
1089 fprintf(output, "process id \'%s", _target->pid);
1091 fprintf(output, "thread id \'%s", _target->tid);
1093 fprintf(output, "\'");
1094 if (config->run_count > 1)
1095 fprintf(output, " (%d runs)", config->run_count);
1096 fprintf(output, ":\n\n");
1100 static int get_precision(double num)
1105 return lround(ceil(-log10(num)));
1108 static void print_table(struct perf_stat_config *config,
1109 FILE *output, int precision, double avg)
1112 int idx, indent = 0;
1114 scnprintf(tmp, 64, " %17.*f", precision, avg);
1115 while (tmp[indent] == ' ')
1118 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1120 for (idx = 0; idx < config->run_count; idx++) {
1121 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1122 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1124 fprintf(output, " %17.*f (%+.*f) ",
1125 precision, run, precision, run - avg);
1127 for (h = 0; h < n; h++)
1128 fprintf(output, "#");
1130 fprintf(output, "\n");
1133 fprintf(output, "\n%*s# Final result:\n", indent, "");
1136 static double timeval2double(struct timeval *t)
1138 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1141 static void print_footer(struct perf_stat_config *config)
1143 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1144 FILE *output = config->output;
1146 if (!config->null_run)
1147 fprintf(output, "\n");
1149 if (config->run_count == 1) {
1150 fprintf(output, " %17.9f seconds time elapsed", avg);
1152 if (config->ru_display) {
1153 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1154 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1156 fprintf(output, "\n\n");
1157 fprintf(output, " %17.9f seconds user\n", ru_utime);
1158 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1161 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1163 * Display at most 2 more significant
1164 * digits than the stddev inaccuracy.
1166 int precision = get_precision(sd) + 2;
1168 if (config->walltime_run_table)
1169 print_table(config, output, precision, avg);
1171 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1172 precision, avg, precision, sd);
1174 print_noise_pct(config, sd, avg);
1176 fprintf(output, "\n\n");
1178 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1180 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1181 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1183 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1185 if (config->print_mixed_hw_group_error)
1187 "The events in group usually have to be from "
1188 "the same PMU. Try reorganizing the group.\n");
1191 static void print_percore_thread(struct perf_stat_config *config,
1192 struct evsel *counter, char *prefix)
1195 struct aggr_cpu_id s2, id;
1198 for (int i = 0; i < evsel__nr_cpus(counter); i++) {
1199 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1200 for (s = 0; s < config->aggr_map->nr; s++) {
1201 id = config->aggr_map->map[s];
1202 if (cpu_map__compare_aggr_cpu_id(s2, id))
1206 print_counter_aggrdata(config, counter, s,
1212 static void print_percore(struct perf_stat_config *config,
1213 struct evsel *counter, char *prefix)
1215 bool metric_only = config->metric_only;
1216 FILE *output = config->output;
1220 if (!config->aggr_map || !config->aggr_get_id)
1223 if (config->percore_show_thread)
1224 return print_percore_thread(config, counter, prefix);
1226 for (s = 0; s < config->aggr_map->nr; s++) {
1227 if (prefix && metric_only)
1228 fprintf(output, "%s", prefix);
1230 print_counter_aggrdata(config, counter, s,
1231 prefix, metric_only,
1236 fputc('\n', output);
1239 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1240 struct target *_target, struct timespec *ts, int argc, const char **argv)
1242 bool metric_only = config->metric_only;
1243 int interval = config->interval;
1244 struct evsel *counter;
1245 char buf[64], *prefix = NULL;
1247 if (config->iostat_run)
1248 evlist->selected = evlist__first(evlist);
1251 print_interval(config, evlist, prefix = buf, ts);
1253 print_header(config, _target, argc, argv);
1256 static int num_print_iv;
1258 if (num_print_iv == 0 && !interval)
1259 print_metric_headers(config, evlist, prefix, false);
1260 if (num_print_iv++ == 25)
1262 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1263 fprintf(config->output, "%s", prefix);
1266 switch (config->aggr_mode) {
1271 print_aggr(config, evlist, prefix);
1274 evlist__for_each_entry(evlist, counter) {
1275 print_aggr_thread(config, _target, counter, prefix);
1279 if (config->iostat_run)
1280 iostat_print_counters(evlist, config, ts, prefix = buf,
1281 print_counter_aggr);
1283 evlist__for_each_entry(evlist, counter) {
1284 print_counter_aggr(config, counter, prefix);
1287 fputc('\n', config->output);
1292 print_no_aggr_metric(config, evlist, prefix);
1294 evlist__for_each_entry(evlist, counter) {
1295 if (counter->percore)
1296 print_percore(config, counter, prefix);
1298 print_counter(config, counter, prefix);
1307 if (!interval && !config->csv_output)
1308 print_footer(config);
1310 fflush(config->output);