4 #include <linux/string.h>
5 #include <linux/time64.h>
7 #include <perf/cpumap.h>
14 #include "thread_map.h"
17 #include <linux/ctype.h>
19 #include <api/fs/fs.h>
22 #include "pmu-hybrid.h"
23 #include "evlist-hybrid.h"
25 #define CNTR_NOT_SUPPORTED "<not supported>"
26 #define CNTR_NOT_COUNTED "<not counted>"
28 static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
31 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
34 static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
36 double enabled_percent = 100;
39 enabled_percent = 100 * run / ena;
40 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
41 config->csv_sep, run, config->csv_sep, enabled_percent);
44 static void print_running_json(struct perf_stat_config *config, u64 run, u64 ena)
46 double enabled_percent = 100;
49 enabled_percent = 100 * run / ena;
50 fprintf(config->output, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
51 run, enabled_percent);
54 static void print_running(struct perf_stat_config *config,
57 if (config->json_output)
58 print_running_json(config, run, ena);
59 else if (config->csv_output)
60 print_running_csv(config, run, ena);
62 print_running_std(config, run, ena);
65 static void print_noise_pct_std(struct perf_stat_config *config,
69 fprintf(config->output, " ( +-%6.2f%% )", pct);
72 static void print_noise_pct_csv(struct perf_stat_config *config,
75 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
78 static void print_noise_pct_json(struct perf_stat_config *config,
81 fprintf(config->output, "\"variance\" : %.2f, ", pct);
84 static void print_noise_pct(struct perf_stat_config *config,
85 double total, double avg)
87 double pct = rel_stddev_stats(total, avg);
89 if (config->json_output)
90 print_noise_pct_json(config, pct);
91 else if (config->csv_output)
92 print_noise_pct_csv(config, pct);
94 print_noise_pct_std(config, pct);
97 static void print_noise(struct perf_stat_config *config,
98 struct evsel *evsel, double avg)
100 struct perf_stat_evsel *ps;
102 if (config->run_count == 1)
106 print_noise_pct(config, stddev_stats(&ps->res_stats), avg);
109 static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
111 fprintf(config->output, " %s", cgrp_name);
114 static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
116 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
119 static void print_cgroup_json(struct perf_stat_config *config, const char *cgrp_name)
121 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
124 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
127 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
129 if (config->json_output)
130 print_cgroup_json(config, cgrp_name);
131 if (config->csv_output)
132 print_cgroup_csv(config, cgrp_name);
134 print_cgroup_std(config, cgrp_name);
139 static void aggr_printout(struct perf_stat_config *config,
140 struct evsel *evsel, struct aggr_cpu_id id, int nr)
144 if (config->json_output && !config->interval)
145 fprintf(config->output, "{");
147 switch (config->aggr_mode) {
149 if (config->json_output) {
150 fprintf(config->output,
151 "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
157 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
160 config->csv_output ? 0 : -8,
163 config->csv_output ? 0 : 4,
169 if (config->json_output) {
170 fprintf(config->output,
171 "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
176 fprintf(config->output, "S%d-D%*d%s%*d%s",
178 config->csv_output ? 0 : -8,
181 config->csv_output ? 0 : 4,
187 if (config->json_output) {
188 fprintf(config->output,
189 "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
193 fprintf(config->output, "S%*d%s%*d%s",
194 config->csv_output ? 0 : -5,
197 config->csv_output ? 0 : 4,
203 if (config->json_output) {
204 fprintf(config->output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
208 fprintf(config->output, "N%*d%s%*d%s",
209 config->csv_output ? 0 : -5,
212 config->csv_output ? 0 : 4,
218 if (config->json_output) {
219 if (evsel->percore && !config->percore_show_thread) {
220 fprintf(config->output, "\"core\" : \"S%d-D%d-C%d\"",
224 } else if (id.cpu.cpu > -1) {
225 fprintf(config->output, "\"cpu\" : \"%d\", ",
229 if (evsel->percore && !config->percore_show_thread) {
230 fprintf(config->output, "S%d-D%d-C%*d%s",
233 config->csv_output ? 0 : -3,
234 id.core, config->csv_sep);
235 } else if (id.cpu.cpu > -1) {
236 fprintf(config->output, "CPU%*d%s",
237 config->csv_output ? 0 : -7,
238 id.cpu.cpu, config->csv_sep);
243 if (config->json_output) {
244 fprintf(config->output, "\"thread\" : \"%s-%d\", ",
245 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
246 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
248 fprintf(config->output, "%*s-%*d%s",
249 config->csv_output ? 0 : 16,
250 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
251 config->csv_output ? 0 : -8,
252 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
270 struct aggr_cpu_id id;
274 #define METRIC_LEN 38
276 static void new_line_std(struct perf_stat_config *config __maybe_unused,
279 struct outstate *os = ctx;
284 static void do_new_line_std(struct perf_stat_config *config,
288 fputs(os->prefix, os->fh);
289 aggr_printout(config, os->evsel, os->id, os->nr);
290 if (config->aggr_mode == AGGR_NONE)
291 fprintf(os->fh, " ");
292 fprintf(os->fh, " ");
295 static void print_metric_std(struct perf_stat_config *config,
296 void *ctx, const char *color, const char *fmt,
297 const char *unit, double val)
299 struct outstate *os = ctx;
302 bool newline = os->newline;
306 if (unit == NULL || fmt == NULL) {
307 fprintf(out, "%-*s", METRIC_LEN, "");
312 do_new_line_std(config, os);
314 n = fprintf(out, " # ");
316 n += color_fprintf(out, color, fmt, val);
318 n += fprintf(out, fmt, val);
319 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
322 static void new_line_csv(struct perf_stat_config *config, void *ctx)
324 struct outstate *os = ctx;
329 fprintf(os->fh, "%s", os->prefix);
330 aggr_printout(config, os->evsel, os->id, os->nr);
331 for (i = 0; i < os->nfields; i++)
332 fputs(config->csv_sep, os->fh);
335 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
337 const char *color __maybe_unused,
338 const char *fmt, const char *unit, double val)
340 struct outstate *os = ctx;
342 char buf[64], *vals, *ends;
344 if (unit == NULL || fmt == NULL) {
345 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
348 snprintf(buf, sizeof(buf), fmt, val);
349 ends = vals = skip_spaces(buf);
350 while (isdigit(*ends) || *ends == '.')
353 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
356 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
358 const char *color __maybe_unused,
359 const char *fmt __maybe_unused,
360 const char *unit, double val)
362 struct outstate *os = ctx;
365 fprintf(out, "\"metric-value\" : %f, ", val);
366 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
367 if (!config->metric_only)
371 static void new_line_json(struct perf_stat_config *config, void *ctx)
373 struct outstate *os = ctx;
377 fprintf(os->fh, "%s", os->prefix);
378 aggr_printout(config, os->evsel, os->id, os->nr);
381 /* Filter out some columns that don't work well in metrics only mode */
383 static bool valid_only_metric(const char *unit)
387 if (strstr(unit, "/sec") ||
388 strstr(unit, "CPUs utilized"))
393 static const char *fixunit(char *buf, struct evsel *evsel,
396 if (!strncmp(unit, "of all", 6)) {
397 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
404 static void print_metric_only(struct perf_stat_config *config,
405 void *ctx, const char *color, const char *fmt,
406 const char *unit, double val)
408 struct outstate *os = ctx;
410 char buf[1024], str[1024];
411 unsigned mlen = config->metric_only_len;
413 if (!valid_only_metric(unit))
415 unit = fixunit(buf, os->evsel, unit);
416 if (mlen < strlen(unit))
417 mlen = strlen(unit) + 1;
420 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
422 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
423 fprintf(out, "%*s ", mlen, str);
426 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
427 void *ctx, const char *color __maybe_unused,
429 const char *unit, double val)
431 struct outstate *os = ctx;
433 char buf[64], *vals, *ends;
436 if (!valid_only_metric(unit))
438 unit = fixunit(tbuf, os->evsel, unit);
439 snprintf(buf, sizeof buf, fmt, val);
440 ends = vals = skip_spaces(buf);
441 while (isdigit(*ends) || *ends == '.')
444 fprintf(out, "%s%s", vals, config->csv_sep);
447 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
448 void *ctx, const char *color __maybe_unused,
450 const char *unit, double val)
452 struct outstate *os = ctx;
454 char buf[64], *vals, *ends;
457 if (!valid_only_metric(unit))
459 unit = fixunit(tbuf, os->evsel, unit);
460 snprintf(buf, sizeof(buf), fmt, val);
461 ends = vals = skip_spaces(buf);
462 while (isdigit(*ends) || *ends == '.')
465 fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
468 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
469 void *ctx __maybe_unused)
473 static void print_metric_header(struct perf_stat_config *config,
474 void *ctx, const char *color __maybe_unused,
475 const char *fmt __maybe_unused,
476 const char *unit, double val __maybe_unused)
478 struct outstate *os = ctx;
481 /* In case of iostat, print metric header for first root port only */
482 if (config->iostat_run &&
483 os->evsel->priv != os->evsel->evlist->selected->priv)
486 if (!valid_only_metric(unit))
488 unit = fixunit(tbuf, os->evsel, unit);
490 if (config->json_output)
491 fprintf(os->fh, "{\"unit\" : \"%s\"}", unit);
492 else if (config->csv_output)
493 fprintf(os->fh, "%s%s", unit, config->csv_sep);
495 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
498 static void abs_printout(struct perf_stat_config *config,
499 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
501 FILE *output = config->output;
502 double sc = evsel->scale;
505 if (config->csv_output) {
506 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
509 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
511 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
514 aggr_printout(config, evsel, id, nr);
516 if (config->json_output)
517 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
519 fprintf(output, fmt, avg, config->csv_sep);
521 if (config->json_output) {
523 fprintf(output, "\"unit\" : \"%s\", ",
528 fprintf(output, "%-*s%s",
529 config->csv_output ? 0 : config->unit_width,
530 evsel->unit, config->csv_sep);
533 if (config->json_output)
534 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
536 fprintf(output, "%-*s", config->csv_output ? 0 : 32, evsel__name(evsel));
538 print_cgroup(config, evsel);
541 static bool is_mixed_hw_group(struct evsel *counter)
543 struct evlist *evlist = counter->evlist;
544 u32 pmu_type = counter->core.attr.type;
547 if (counter->core.nr_members < 2)
550 evlist__for_each_entry(evlist, pos) {
551 /* software events can be part of any hardware group */
552 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
554 if (pmu_type == PERF_TYPE_SOFTWARE) {
555 pmu_type = pos->core.attr.type;
558 if (pmu_type != pos->core.attr.type)
565 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
566 struct evsel *counter, double uval,
567 char *prefix, u64 run, u64 ena, double noise,
568 struct runtime_stat *st, int map_idx)
570 struct perf_stat_output_ctx out;
571 struct outstate os = {
572 .fh = config->output,
573 .prefix = prefix ? prefix : "",
581 if (config->csv_output) {
582 static const int aggr_fields[AGGR_MAX] = {
593 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
594 nl = config->metric_only ? new_line_metric : new_line_csv;
595 os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
596 } else if (config->json_output) {
597 pm = config->metric_only ? print_metric_only_json : print_metric_json;
598 nl = config->metric_only ? new_line_metric : new_line_json;
600 pm = config->metric_only ? print_metric_only : print_metric_std;
601 nl = config->metric_only ? new_line_metric : new_line_std;
604 if (!config->no_csv_summary && config->csv_output &&
605 config->summary && !config->interval && !config->metric_only) {
606 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
609 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
610 if (config->metric_only) {
611 pm(config, &os, NULL, "", "", 0);
614 aggr_printout(config, counter, id, nr);
616 if (config->json_output) {
617 fprintf(config->output, "\"counter-value\" : \"%s\", ",
618 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED);
620 fprintf(config->output, "%*s%s",
621 config->csv_output ? 0 : 18,
622 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
626 if (counter->supported) {
627 if (!evlist__has_hybrid(counter->evlist)) {
628 config->print_free_counters_hint = 1;
629 if (is_mixed_hw_group(counter))
630 config->print_mixed_hw_group_error = 1;
634 if (config->json_output) {
635 fprintf(config->output, "\"unit\" : \"%s\", ", counter->unit);
637 fprintf(config->output, "%-*s%s",
638 config->csv_output ? 0 : config->unit_width,
639 counter->unit, config->csv_sep);
642 if (config->json_output) {
643 fprintf(config->output, "\"event\" : \"%s\", ",
644 evsel__name(counter));
646 fprintf(config->output, "%*s",
647 config->csv_output ? 0 : -25, evsel__name(counter));
650 print_cgroup(config, counter);
652 if (!config->csv_output && !config->json_output)
653 pm(config, &os, NULL, NULL, "", 0);
654 print_noise(config, counter, noise);
655 print_running(config, run, ena);
656 if (config->csv_output || config->json_output)
657 pm(config, &os, NULL, NULL, "", 0);
661 if (!config->metric_only)
662 abs_printout(config, id, nr, counter, uval);
664 out.print_metric = pm;
667 out.force_header = false;
669 if (config->csv_output && !config->metric_only) {
670 print_noise(config, counter, noise);
671 print_running(config, run, ena);
672 } else if (config->json_output && !config->metric_only) {
673 print_noise(config, counter, noise);
674 print_running(config, run, ena);
677 perf_stat__print_shadow_stats(config, counter, uval, map_idx,
678 &out, &config->metric_events, st);
679 if (!config->csv_output && !config->metric_only && !config->json_output) {
680 print_noise(config, counter, noise);
681 print_running(config, run, ena);
685 static void uniquify_event_name(struct evsel *counter)
691 if (counter->uniquified_name || counter->use_config_name ||
692 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
693 strlen(counter->pmu_name)))
696 config = strchr(counter->name, '/');
698 if (asprintf(&new_name,
699 "%s%s", counter->pmu_name, config) > 0) {
701 counter->name = new_name;
704 if (evsel__is_hybrid(counter)) {
705 ret = asprintf(&new_name, "%s/%s/",
706 counter->pmu_name, counter->name);
708 ret = asprintf(&new_name, "%s [%s]",
709 counter->name, counter->pmu_name);
714 counter->name = new_name;
718 counter->uniquified_name = true;
721 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
723 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
726 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
728 if (config->no_merge || hybrid_uniquify(counter, config))
729 uniquify_event_name(counter);
732 static void print_counter_aggrdata(struct perf_stat_config *config,
733 struct evsel *counter, int s,
734 char *prefix, bool metric_only,
737 FILE *output = config->output;
740 struct perf_stat_evsel *ps = counter->stats;
741 struct perf_stat_aggr *aggr = &ps->aggr[s];
742 struct aggr_cpu_id id = config->aggr_map->map[s];
743 double avg = aggr->counts.val;
745 if (counter->supported && aggr->nr == 0)
748 uniquify_counter(config, counter);
750 val = aggr->counts.val;
751 ena = aggr->counts.ena;
752 run = aggr->counts.run;
754 if (*first && metric_only) {
756 aggr_printout(config, counter, id, aggr->nr);
758 if (prefix && !metric_only)
759 fprintf(output, "%s", prefix);
761 uval = val * counter->scale;
763 printout(config, id, aggr->nr, counter, uval,
764 prefix, run, ena, avg, &rt_stat, s);
770 static void print_aggr(struct perf_stat_config *config,
771 struct evlist *evlist,
774 bool metric_only = config->metric_only;
775 FILE *output = config->output;
776 struct evsel *counter;
780 if (!config->aggr_map || !config->aggr_get_id)
784 * With metric_only everything is on a single line.
785 * Without each counter has its own line.
787 for (s = 0; s < config->aggr_map->nr; s++) {
790 fprintf(output, "%s", prefix);
791 else if (config->summary && !config->no_csv_summary &&
792 config->csv_output && !config->interval)
793 fprintf(output, "%16s%s", "summary", config->csv_sep);
797 evlist__for_each_entry(evlist, counter) {
798 if (counter->merged_stat)
801 print_counter_aggrdata(config, counter, s,
810 static void print_counter(struct perf_stat_config *config,
811 struct evsel *counter, char *prefix)
813 bool metric_only = config->metric_only;
817 /* AGGR_THREAD doesn't have config->aggr_get_id */
818 if (!config->aggr_map)
821 if (counter->merged_stat)
824 for (s = 0; s < config->aggr_map->nr; s++) {
825 print_counter_aggrdata(config, counter, s,
831 static void print_no_aggr_metric(struct perf_stat_config *config,
832 struct evlist *evlist,
838 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
839 struct evsel *counter;
842 evlist__for_each_entry(evlist, counter) {
845 struct aggr_cpu_id id;
846 struct perf_stat_evsel *ps = counter->stats;
847 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
852 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
855 fputs(prefix, config->output);
856 aggr_printout(config, counter, id, 0);
859 val = ps->aggr[counter_idx].counts.val;
860 ena = ps->aggr[counter_idx].counts.ena;
861 run = ps->aggr[counter_idx].counts.run;
863 uval = val * counter->scale;
864 printout(config, id, 0, counter, uval, prefix,
865 run, ena, 1.0, &rt_stat, counter_idx);
868 fputc('\n', config->output);
872 static int aggr_header_lens[] = {
882 static const char *aggr_header_csv[] = {
883 [AGGR_CORE] = "core,cpus,",
884 [AGGR_DIE] = "die,cpus,",
885 [AGGR_SOCKET] = "socket,cpus,",
886 [AGGR_NONE] = "cpu,",
887 [AGGR_THREAD] = "comm-pid,",
888 [AGGR_NODE] = "node,",
892 static void print_metric_headers(struct perf_stat_config *config,
893 struct evlist *evlist,
894 const char *prefix, bool no_indent)
896 struct evsel *counter;
897 struct outstate os = {
900 struct perf_stat_output_ctx out = {
902 .print_metric = print_metric_header,
903 .new_line = new_line_metric,
904 .force_header = true,
907 if (prefix && !config->json_output)
908 fprintf(config->output, "%s", prefix);
910 if (!config->csv_output && !config->json_output && !no_indent)
911 fprintf(config->output, "%*s",
912 aggr_header_lens[config->aggr_mode], "");
913 if (config->csv_output) {
914 if (config->interval)
915 fputs("time,", config->output);
916 if (!config->iostat_run)
917 fputs(aggr_header_csv[config->aggr_mode], config->output);
919 if (config->json_output) {
920 if (config->interval)
921 fputs("{\"unit\" : \"sec\"}", config->output);
923 if (config->iostat_run)
924 iostat_print_header_prefix(config);
926 /* Print metrics headers only */
927 evlist__for_each_entry(evlist, counter) {
930 perf_stat__print_shadow_stats(config, counter, 0,
933 &config->metric_events,
936 fputc('\n', config->output);
939 static void print_interval(struct perf_stat_config *config,
940 struct evlist *evlist,
941 char *prefix, struct timespec *ts)
943 bool metric_only = config->metric_only;
944 unsigned int unit_width = config->unit_width;
945 FILE *output = config->output;
946 static int num_print_interval;
948 if (config->interval_clear && isatty(fileno(output)))
951 if (!config->iostat_run && !config->json_output)
952 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
953 ts->tv_nsec, config->csv_sep);
954 if (!config->iostat_run && config->json_output && !config->metric_only)
955 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
956 ts->tv_sec, ts->tv_nsec);
957 if (!config->iostat_run && config->json_output && config->metric_only)
958 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
959 ts->tv_sec, ts->tv_nsec);
961 if ((num_print_interval == 0 || config->interval_clear) &&
962 !config->csv_output && !config->json_output) {
963 switch (config->aggr_mode) {
965 fprintf(output, "# time node cpus");
967 fprintf(output, " counts %*s events\n", unit_width, "unit");
970 fprintf(output, "# time socket cpus");
972 fprintf(output, " counts %*s events\n", unit_width, "unit");
975 fprintf(output, "# time die cpus");
977 fprintf(output, " counts %*s events\n", unit_width, "unit");
980 fprintf(output, "# time core cpus");
982 fprintf(output, " counts %*s events\n", unit_width, "unit");
985 fprintf(output, "# time CPU ");
987 fprintf(output, " counts %*s events\n", unit_width, "unit");
990 fprintf(output, "# time comm-pid");
992 fprintf(output, " counts %*s events\n", unit_width, "unit");
996 if (!config->iostat_run) {
997 fprintf(output, "# time");
999 fprintf(output, " counts %*s events\n", unit_width, "unit");
1007 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1008 print_metric_headers(config, evlist, " ", true);
1009 if (++num_print_interval == 25)
1010 num_print_interval = 0;
1013 static void print_header(struct perf_stat_config *config,
1014 struct target *_target,
1015 int argc, const char **argv)
1017 FILE *output = config->output;
1022 if (!config->csv_output && !config->json_output) {
1023 fprintf(output, "\n");
1024 fprintf(output, " Performance counter stats for ");
1025 if (_target->bpf_str)
1026 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1027 else if (_target->system_wide)
1028 fprintf(output, "\'system wide");
1029 else if (_target->cpu_list)
1030 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1031 else if (!target__has_task(_target)) {
1032 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1033 for (i = 1; argv && (i < argc); i++)
1034 fprintf(output, " %s", argv[i]);
1035 } else if (_target->pid)
1036 fprintf(output, "process id \'%s", _target->pid);
1038 fprintf(output, "thread id \'%s", _target->tid);
1040 fprintf(output, "\'");
1041 if (config->run_count > 1)
1042 fprintf(output, " (%d runs)", config->run_count);
1043 fprintf(output, ":\n\n");
1047 static int get_precision(double num)
1052 return lround(ceil(-log10(num)));
1055 static void print_table(struct perf_stat_config *config,
1056 FILE *output, int precision, double avg)
1059 int idx, indent = 0;
1061 scnprintf(tmp, 64, " %17.*f", precision, avg);
1062 while (tmp[indent] == ' ')
1065 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1067 for (idx = 0; idx < config->run_count; idx++) {
1068 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1069 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1071 fprintf(output, " %17.*f (%+.*f) ",
1072 precision, run, precision, run - avg);
1074 for (h = 0; h < n; h++)
1075 fprintf(output, "#");
1077 fprintf(output, "\n");
1080 fprintf(output, "\n%*s# Final result:\n", indent, "");
1083 static double timeval2double(struct timeval *t)
1085 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1088 static void print_footer(struct perf_stat_config *config)
1090 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1091 FILE *output = config->output;
1093 if (!config->null_run)
1094 fprintf(output, "\n");
1096 if (config->run_count == 1) {
1097 fprintf(output, " %17.9f seconds time elapsed", avg);
1099 if (config->ru_display) {
1100 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1101 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1103 fprintf(output, "\n\n");
1104 fprintf(output, " %17.9f seconds user\n", ru_utime);
1105 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1108 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1110 * Display at most 2 more significant
1111 * digits than the stddev inaccuracy.
1113 int precision = get_precision(sd) + 2;
1115 if (config->walltime_run_table)
1116 print_table(config, output, precision, avg);
1118 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1119 precision, avg, precision, sd);
1121 print_noise_pct(config, sd, avg);
1123 fprintf(output, "\n\n");
1125 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1127 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1128 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1130 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1132 if (config->print_mixed_hw_group_error)
1134 "The events in group usually have to be from "
1135 "the same PMU. Try reorganizing the group.\n");
1138 static void print_percore(struct perf_stat_config *config,
1139 struct evsel *counter, char *prefix)
1141 bool metric_only = config->metric_only;
1142 FILE *output = config->output;
1143 struct cpu_aggr_map *core_map;
1147 if (!config->aggr_map || !config->aggr_get_id)
1150 if (config->percore_show_thread)
1151 return print_counter(config, counter, prefix);
1153 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1154 if (core_map == NULL) {
1155 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1159 for (s = 0, c = 0; s < config->aggr_map->nr; s++) {
1160 struct perf_cpu curr_cpu = config->aggr_map->map[s].cpu;
1161 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1164 for (i = 0; i < c; i++) {
1165 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1173 if (prefix && metric_only)
1174 fprintf(output, "%s", prefix);
1176 print_counter_aggrdata(config, counter, s,
1177 prefix, metric_only, &first);
1179 core_map->map[c++] = core_id;
1184 fputc('\n', output);
1187 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1188 struct target *_target, struct timespec *ts, int argc, const char **argv)
1190 bool metric_only = config->metric_only;
1191 int interval = config->interval;
1192 struct evsel *counter;
1193 char buf[64], *prefix = NULL;
1195 if (config->iostat_run)
1196 evlist->selected = evlist__first(evlist);
1199 print_interval(config, evlist, prefix = buf, ts);
1201 print_header(config, _target, argc, argv);
1204 static int num_print_iv;
1206 if (num_print_iv == 0 && !interval)
1207 print_metric_headers(config, evlist, prefix, false);
1208 if (num_print_iv++ == 25)
1210 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1211 fprintf(config->output, "%s", prefix);
1213 if (config->json_output && !config->metric_only)
1214 fprintf(config->output, "}");
1217 switch (config->aggr_mode) {
1222 print_aggr(config, evlist, prefix);
1226 if (config->iostat_run)
1227 iostat_print_counters(evlist, config, ts, prefix = buf,
1230 evlist__for_each_entry(evlist, counter) {
1231 print_counter(config, counter, prefix);
1234 fputc('\n', config->output);
1239 print_no_aggr_metric(config, evlist, prefix);
1241 evlist__for_each_entry(evlist, counter) {
1242 if (counter->percore)
1243 print_percore(config, counter, prefix);
1245 print_counter(config, counter, prefix);
1255 if (!interval && !config->csv_output && !config->json_output)
1256 print_footer(config);
1258 fflush(config->output);