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>"
31 #define INTERVAL_LEN 16
37 static int aggr_header_lens[] = {
47 static const char *aggr_header_csv[] = {
48 [AGGR_CORE] = "core,cpus,",
49 [AGGR_DIE] = "die,cpus,",
50 [AGGR_SOCKET] = "socket,cpus,",
52 [AGGR_THREAD] = "comm-pid,",
53 [AGGR_NODE] = "node,",
57 static const char *aggr_header_std[] = {
60 [AGGR_SOCKET] = "socket",
62 [AGGR_THREAD] = "comm-pid",
67 static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
70 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
73 static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
75 double enabled_percent = 100;
78 enabled_percent = 100 * run / ena;
79 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
80 config->csv_sep, run, config->csv_sep, enabled_percent);
83 static void print_running_json(struct perf_stat_config *config, u64 run, u64 ena)
85 double enabled_percent = 100;
88 enabled_percent = 100 * run / ena;
89 fprintf(config->output, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
90 run, enabled_percent);
93 static void print_running(struct perf_stat_config *config,
94 u64 run, u64 ena, bool before_metric)
96 if (config->json_output) {
98 print_running_json(config, run, ena);
99 } else if (config->csv_output) {
101 print_running_csv(config, run, ena);
104 print_running_std(config, run, ena);
108 static void print_noise_pct_std(struct perf_stat_config *config,
112 fprintf(config->output, " ( +-%6.2f%% )", pct);
115 static void print_noise_pct_csv(struct perf_stat_config *config,
118 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
121 static void print_noise_pct_json(struct perf_stat_config *config,
124 fprintf(config->output, "\"variance\" : %.2f, ", pct);
127 static void print_noise_pct(struct perf_stat_config *config,
128 double total, double avg, bool before_metric)
130 double pct = rel_stddev_stats(total, avg);
132 if (config->json_output) {
134 print_noise_pct_json(config, pct);
135 } else if (config->csv_output) {
137 print_noise_pct_csv(config, pct);
140 print_noise_pct_std(config, pct);
144 static void print_noise(struct perf_stat_config *config,
145 struct evsel *evsel, double avg, bool before_metric)
147 struct perf_stat_evsel *ps;
149 if (config->run_count == 1)
153 print_noise_pct(config, stddev_stats(&ps->res_stats), avg, before_metric);
156 static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
158 fprintf(config->output, " %-*s", CGROUP_LEN, cgrp_name);
161 static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
163 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
166 static void print_cgroup_json(struct perf_stat_config *config, const char *cgrp_name)
168 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
171 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
174 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
176 if (config->json_output)
177 print_cgroup_json(config, cgrp_name);
178 if (config->csv_output)
179 print_cgroup_csv(config, cgrp_name);
181 print_cgroup_std(config, cgrp_name);
185 static void print_aggr_id_std(struct perf_stat_config *config,
186 struct evsel *evsel, struct aggr_cpu_id id, int nr)
188 FILE *output = config->output;
189 int idx = config->aggr_mode;
192 switch (config->aggr_mode) {
194 snprintf(buf, sizeof(buf), "S%d-D%d-C%d", id.socket, id.die, id.core);
197 snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
200 snprintf(buf, sizeof(buf), "S%d", id.socket);
203 snprintf(buf, sizeof(buf), "N%d", id.node);
206 if (evsel->percore && !config->percore_show_thread) {
207 snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
208 id.socket, id.die, id.core);
209 fprintf(output, "%-*s ",
210 aggr_header_lens[AGGR_CORE], buf);
211 } else if (id.cpu.cpu > -1) {
212 fprintf(output, "CPU%-*d ",
213 aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
217 fprintf(output, "%*s-%-*d ",
218 COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
219 PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
228 fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, 4, nr);
231 static void print_aggr_id_csv(struct perf_stat_config *config,
232 struct evsel *evsel, struct aggr_cpu_id id, int nr)
234 FILE *output = config->output;
235 const char *sep = config->csv_sep;
237 switch (config->aggr_mode) {
239 fprintf(output, "S%d-D%d-C%d%s%d%s",
240 id.socket, id.die, id.core, sep, nr, sep);
243 fprintf(output, "S%d-D%d%s%d%s",
244 id.socket, id.die, sep, nr, sep);
247 fprintf(output, "S%d%s%d%s",
248 id.socket, sep, nr, sep);
251 fprintf(output, "N%d%s%d%s",
252 id.node, sep, nr, sep);
255 if (evsel->percore && !config->percore_show_thread) {
256 fprintf(output, "S%d-D%d-C%d%s",
257 id.socket, id.die, id.core, sep);
258 } else if (id.cpu.cpu > -1) {
259 fprintf(output, "CPU%d%s",
264 fprintf(output, "%s-%d%s",
265 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
266 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
277 static void print_aggr_id_json(struct perf_stat_config *config,
278 struct evsel *evsel, struct aggr_cpu_id id, int nr)
280 FILE *output = config->output;
282 if (!config->interval)
285 switch (config->aggr_mode) {
287 fprintf(output, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
288 id.socket, id.die, id.core, nr);
291 fprintf(output, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
292 id.socket, id.die, nr);
295 fprintf(output, "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
299 fprintf(output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
303 if (evsel->percore && !config->percore_show_thread) {
304 fprintf(output, "\"core\" : \"S%d-D%d-C%d\"",
305 id.socket, id.die, id.core);
306 } else if (id.cpu.cpu > -1) {
307 fprintf(output, "\"cpu\" : \"%d\", ",
312 fprintf(output, "\"thread\" : \"%s-%d\", ",
313 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
314 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
324 static void aggr_printout(struct perf_stat_config *config,
325 struct evsel *evsel, struct aggr_cpu_id id, int nr)
327 if (config->json_output)
328 print_aggr_id_json(config, evsel, id, nr);
329 else if (config->csv_output)
330 print_aggr_id_csv(config, evsel, id, nr);
332 print_aggr_id_std(config, evsel, id, nr);
341 struct aggr_cpu_id id;
345 static void new_line_std(struct perf_stat_config *config __maybe_unused,
348 struct outstate *os = ctx;
353 static void do_new_line_std(struct perf_stat_config *config,
357 fputs(os->prefix, os->fh);
358 aggr_printout(config, os->evsel, os->id, os->nr);
359 if (config->aggr_mode == AGGR_NONE)
360 fprintf(os->fh, " ");
361 fprintf(os->fh, " ");
364 static void print_metric_std(struct perf_stat_config *config,
365 void *ctx, const char *color, const char *fmt,
366 const char *unit, double val)
368 struct outstate *os = ctx;
371 bool newline = os->newline;
375 if (unit == NULL || fmt == NULL) {
376 fprintf(out, "%-*s", METRIC_LEN, "");
381 do_new_line_std(config, os);
383 n = fprintf(out, " # ");
385 n += color_fprintf(out, color, fmt, val);
387 n += fprintf(out, fmt, val);
388 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
391 static void new_line_csv(struct perf_stat_config *config, void *ctx)
393 struct outstate *os = ctx;
398 fprintf(os->fh, "%s", os->prefix);
399 aggr_printout(config, os->evsel, os->id, os->nr);
400 for (i = 0; i < os->nfields; i++)
401 fputs(config->csv_sep, os->fh);
404 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
406 const char *color __maybe_unused,
407 const char *fmt, const char *unit, double val)
409 struct outstate *os = ctx;
411 char buf[64], *vals, *ends;
413 if (unit == NULL || fmt == NULL) {
414 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
417 snprintf(buf, sizeof(buf), fmt, val);
418 ends = vals = skip_spaces(buf);
419 while (isdigit(*ends) || *ends == '.')
422 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
425 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
427 const char *color __maybe_unused,
428 const char *fmt __maybe_unused,
429 const char *unit, double val)
431 struct outstate *os = ctx;
434 fprintf(out, "\"metric-value\" : %f, ", val);
435 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
436 if (!config->metric_only)
440 static void new_line_json(struct perf_stat_config *config, void *ctx)
442 struct outstate *os = ctx;
446 fprintf(os->fh, "%s", os->prefix);
447 aggr_printout(config, os->evsel, os->id, os->nr);
450 /* Filter out some columns that don't work well in metrics only mode */
452 static bool valid_only_metric(const char *unit)
456 if (strstr(unit, "/sec") ||
457 strstr(unit, "CPUs utilized"))
462 static const char *fixunit(char *buf, struct evsel *evsel,
465 if (!strncmp(unit, "of all", 6)) {
466 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
473 static void print_metric_only(struct perf_stat_config *config,
474 void *ctx, const char *color, const char *fmt,
475 const char *unit, double val)
477 struct outstate *os = ctx;
479 char buf[1024], str[1024];
480 unsigned mlen = config->metric_only_len;
482 if (!valid_only_metric(unit))
484 unit = fixunit(buf, os->evsel, unit);
485 if (mlen < strlen(unit))
486 mlen = strlen(unit) + 1;
489 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
491 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
492 fprintf(out, "%*s ", mlen, str);
495 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
496 void *ctx, const char *color __maybe_unused,
498 const char *unit, double val)
500 struct outstate *os = ctx;
502 char buf[64], *vals, *ends;
505 if (!valid_only_metric(unit))
507 unit = fixunit(tbuf, os->evsel, unit);
508 snprintf(buf, sizeof buf, fmt, val);
509 ends = vals = skip_spaces(buf);
510 while (isdigit(*ends) || *ends == '.')
513 fprintf(out, "%s%s", vals, config->csv_sep);
516 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
517 void *ctx, const char *color __maybe_unused,
519 const char *unit, double val)
521 struct outstate *os = ctx;
523 char buf[64], *vals, *ends;
526 if (!valid_only_metric(unit))
528 unit = fixunit(tbuf, os->evsel, unit);
529 snprintf(buf, sizeof(buf), fmt, val);
530 ends = vals = skip_spaces(buf);
531 while (isdigit(*ends) || *ends == '.')
534 fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
537 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
538 void *ctx __maybe_unused)
542 static void print_metric_header(struct perf_stat_config *config,
543 void *ctx, const char *color __maybe_unused,
544 const char *fmt __maybe_unused,
545 const char *unit, double val __maybe_unused)
547 struct outstate *os = ctx;
550 /* In case of iostat, print metric header for first root port only */
551 if (config->iostat_run &&
552 os->evsel->priv != os->evsel->evlist->selected->priv)
555 if (!valid_only_metric(unit))
557 unit = fixunit(tbuf, os->evsel, unit);
559 if (config->json_output)
560 fprintf(os->fh, "{\"unit\" : \"%s\"}", unit);
561 else if (config->csv_output)
562 fprintf(os->fh, "%s%s", unit, config->csv_sep);
564 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
567 static void print_counter_value_std(struct perf_stat_config *config,
568 struct evsel *evsel, double avg, bool ok)
570 FILE *output = config->output;
571 double sc = evsel->scale;
573 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
576 fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
578 fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
581 fprintf(output, fmt, COUNTS_LEN, avg);
583 fprintf(output, "%*s ", COUNTS_LEN, bad_count);
586 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
588 fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
591 static void print_counter_value_csv(struct perf_stat_config *config,
592 struct evsel *evsel, double avg, bool ok)
594 FILE *output = config->output;
595 double sc = evsel->scale;
596 const char *sep = config->csv_sep;
597 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
598 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
601 fprintf(output, fmt, avg, sep);
603 fprintf(output, "%s%s", bad_count, sep);
606 fprintf(output, "%s%s", evsel->unit, sep);
608 fprintf(output, "%s", evsel__name(evsel));
611 static void print_counter_value_json(struct perf_stat_config *config,
612 struct evsel *evsel, double avg, bool ok)
614 FILE *output = config->output;
615 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
618 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
620 fprintf(output, "\"counter-value\" : \"%s\", ", bad_count);
623 fprintf(output, "\"unit\" : \"%s\", ", evsel->unit);
625 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
628 static void print_counter_value(struct perf_stat_config *config,
629 struct evsel *evsel, double avg, bool ok)
631 if (config->json_output)
632 print_counter_value_json(config, evsel, avg, ok);
633 else if (config->csv_output)
634 print_counter_value_csv(config, evsel, avg, ok);
636 print_counter_value_std(config, evsel, avg, ok);
639 static void abs_printout(struct perf_stat_config *config,
640 struct aggr_cpu_id id, int nr,
641 struct evsel *evsel, double avg, bool ok)
643 aggr_printout(config, evsel, id, nr);
644 print_counter_value(config, evsel, avg, ok);
645 print_cgroup(config, evsel);
648 static bool is_mixed_hw_group(struct evsel *counter)
650 struct evlist *evlist = counter->evlist;
651 u32 pmu_type = counter->core.attr.type;
654 if (counter->core.nr_members < 2)
657 evlist__for_each_entry(evlist, pos) {
658 /* software events can be part of any hardware group */
659 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
661 if (pmu_type == PERF_TYPE_SOFTWARE) {
662 pmu_type = pos->core.attr.type;
665 if (pmu_type != pos->core.attr.type)
672 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
673 struct evsel *counter, double uval,
674 char *prefix, u64 run, u64 ena, double noise,
675 struct runtime_stat *st, int map_idx)
677 struct perf_stat_output_ctx out;
678 struct outstate os = {
679 .fh = config->output,
680 .prefix = prefix ? prefix : "",
689 if (config->csv_output) {
690 static const int aggr_fields[AGGR_MAX] = {
701 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
702 nl = config->metric_only ? new_line_metric : new_line_csv;
703 os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
704 } else if (config->json_output) {
705 pm = config->metric_only ? print_metric_only_json : print_metric_json;
706 nl = config->metric_only ? new_line_metric : new_line_json;
708 pm = config->metric_only ? print_metric_only : print_metric_std;
709 nl = config->metric_only ? new_line_metric : new_line_std;
712 if (!config->no_csv_summary && config->csv_output &&
713 config->summary && !config->interval && !config->metric_only) {
714 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
717 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
718 if (config->metric_only) {
719 pm(config, &os, NULL, "", "", 0);
725 if (counter->supported) {
726 if (!evlist__has_hybrid(counter->evlist)) {
727 config->print_free_counters_hint = 1;
728 if (is_mixed_hw_group(counter))
729 config->print_mixed_hw_group_error = 1;
734 out.print_metric = pm;
737 out.force_header = false;
739 if (!config->metric_only) {
740 abs_printout(config, id, nr, counter, uval, ok);
742 print_noise(config, counter, noise, /*before_metric=*/true);
743 print_running(config, run, ena, /*before_metric=*/true);
747 perf_stat__print_shadow_stats(config, counter, uval, map_idx,
748 &out, &config->metric_events, st);
750 pm(config, &os, /*color=*/NULL, /*format=*/NULL, /*unit=*/"", /*val=*/0);
753 if (!config->metric_only) {
754 print_noise(config, counter, noise, /*before_metric=*/false);
755 print_running(config, run, ena, /*before_metric=*/false);
759 static void uniquify_event_name(struct evsel *counter)
765 if (counter->uniquified_name || counter->use_config_name ||
766 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
767 strlen(counter->pmu_name)))
770 config = strchr(counter->name, '/');
772 if (asprintf(&new_name,
773 "%s%s", counter->pmu_name, config) > 0) {
775 counter->name = new_name;
778 if (evsel__is_hybrid(counter)) {
779 ret = asprintf(&new_name, "%s/%s/",
780 counter->pmu_name, counter->name);
782 ret = asprintf(&new_name, "%s [%s]",
783 counter->name, counter->pmu_name);
788 counter->name = new_name;
792 counter->uniquified_name = true;
795 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
797 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
800 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
802 if (config->no_merge || hybrid_uniquify(counter, config))
803 uniquify_event_name(counter);
806 static void print_counter_aggrdata(struct perf_stat_config *config,
807 struct evsel *counter, int s,
808 char *prefix, bool metric_only,
811 FILE *output = config->output;
814 struct perf_stat_evsel *ps = counter->stats;
815 struct perf_stat_aggr *aggr = &ps->aggr[s];
816 struct aggr_cpu_id id = config->aggr_map->map[s];
817 double avg = aggr->counts.val;
819 if (counter->supported && aggr->nr == 0)
822 uniquify_counter(config, counter);
824 val = aggr->counts.val;
825 ena = aggr->counts.ena;
826 run = aggr->counts.run;
828 if (*first && metric_only) {
830 aggr_printout(config, counter, id, aggr->nr);
832 if (prefix && !metric_only)
833 fprintf(output, "%s", prefix);
835 uval = val * counter->scale;
837 printout(config, id, aggr->nr, counter, uval,
838 prefix, run, ena, avg, &rt_stat, s);
844 static void print_aggr(struct perf_stat_config *config,
845 struct evlist *evlist,
848 bool metric_only = config->metric_only;
849 FILE *output = config->output;
850 struct evsel *counter;
854 if (!config->aggr_map || !config->aggr_get_id)
858 * With metric_only everything is on a single line.
859 * Without each counter has its own line.
861 for (s = 0; s < config->aggr_map->nr; s++) {
864 fprintf(output, "%s", prefix);
865 else if (config->summary && !config->no_csv_summary &&
866 config->csv_output && !config->interval)
867 fprintf(output, "%16s%s", "summary", config->csv_sep);
871 evlist__for_each_entry(evlist, counter) {
872 if (counter->merged_stat)
875 print_counter_aggrdata(config, counter, s,
884 static void print_counter(struct perf_stat_config *config,
885 struct evsel *counter, char *prefix)
887 bool metric_only = config->metric_only;
891 /* AGGR_THREAD doesn't have config->aggr_get_id */
892 if (!config->aggr_map)
895 if (counter->merged_stat)
898 for (s = 0; s < config->aggr_map->nr; s++) {
899 print_counter_aggrdata(config, counter, s,
905 static void print_no_aggr_metric(struct perf_stat_config *config,
906 struct evlist *evlist,
912 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
913 struct evsel *counter;
916 evlist__for_each_entry(evlist, counter) {
919 struct aggr_cpu_id id;
920 struct perf_stat_evsel *ps = counter->stats;
921 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
926 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
929 fputs(prefix, config->output);
930 aggr_printout(config, counter, id, 0);
933 val = ps->aggr[counter_idx].counts.val;
934 ena = ps->aggr[counter_idx].counts.ena;
935 run = ps->aggr[counter_idx].counts.run;
937 uval = val * counter->scale;
938 printout(config, id, 0, counter, uval, prefix,
939 run, ena, 1.0, &rt_stat, counter_idx);
942 fputc('\n', config->output);
946 static void print_metric_headers_std(struct perf_stat_config *config,
947 const char *prefix, bool no_indent)
950 fprintf(config->output, "%s", prefix);
953 int len = aggr_header_lens[config->aggr_mode];
956 len += CGROUP_LEN + 1;
958 fprintf(config->output, "%*s", len, "");
962 static void print_metric_headers_csv(struct perf_stat_config *config,
964 bool no_indent __maybe_unused)
967 fprintf(config->output, "%s", prefix);
968 if (config->interval)
969 fputs("time,", config->output);
970 if (!config->iostat_run)
971 fputs(aggr_header_csv[config->aggr_mode], config->output);
974 static void print_metric_headers_json(struct perf_stat_config *config,
975 const char *prefix __maybe_unused,
976 bool no_indent __maybe_unused)
978 if (config->interval)
979 fputs("{\"unit\" : \"sec\"}", config->output);
982 static void print_metric_headers(struct perf_stat_config *config,
983 struct evlist *evlist,
984 const char *prefix, bool no_indent)
986 struct evsel *counter;
987 struct outstate os = {
990 struct perf_stat_output_ctx out = {
992 .print_metric = print_metric_header,
993 .new_line = new_line_metric,
994 .force_header = true,
997 if (config->json_output)
998 print_metric_headers_json(config, prefix, no_indent);
999 else if (config->csv_output)
1000 print_metric_headers_csv(config, prefix, no_indent);
1002 print_metric_headers_std(config, prefix, no_indent);
1004 if (config->iostat_run)
1005 iostat_print_header_prefix(config);
1007 /* Print metrics headers only */
1008 evlist__for_each_entry(evlist, counter) {
1011 perf_stat__print_shadow_stats(config, counter, 0,
1014 &config->metric_events,
1017 fputc('\n', config->output);
1020 static void prepare_interval(struct perf_stat_config *config,
1021 char *prefix, struct timespec *ts)
1023 if (config->iostat_run)
1026 if (!config->json_output)
1027 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1028 ts->tv_nsec, config->csv_sep);
1029 else if (!config->metric_only)
1030 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1031 ts->tv_sec, ts->tv_nsec);
1033 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1034 ts->tv_sec, ts->tv_nsec);
1037 static void print_interval(struct perf_stat_config *config,
1038 struct evlist *evlist)
1040 bool metric_only = config->metric_only;
1041 unsigned int unit_width = config->unit_width;
1042 FILE *output = config->output;
1043 static int num_print_interval;
1045 if (config->interval_clear && isatty(fileno(output)))
1046 puts(CONSOLE_CLEAR);
1048 if ((num_print_interval == 0 || config->interval_clear) &&
1049 !config->csv_output && !config->json_output) {
1050 switch (config->aggr_mode) {
1055 fprintf(output, "#%*s %-*s cpus",
1056 INTERVAL_LEN - 1, "time",
1057 aggr_header_lens[config->aggr_mode],
1058 aggr_header_std[config->aggr_mode]);
1061 fprintf(output, "#%*s %-*s",
1062 INTERVAL_LEN - 1, "time",
1063 aggr_header_lens[config->aggr_mode],
1064 aggr_header_std[config->aggr_mode]);
1067 fprintf(output, "#%*s %*s-%-*s",
1068 INTERVAL_LEN - 1, "time",
1069 COMM_LEN, "comm", PID_LEN, "pid");
1073 if (!config->iostat_run)
1074 fprintf(output, "#%*s",
1075 INTERVAL_LEN - 1, "time");
1082 fprintf(output, " %*s %*s events\n",
1083 COUNTS_LEN, "counts", unit_width, "unit");
1087 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1088 print_metric_headers(config, evlist, " ", true);
1089 if (++num_print_interval == 25)
1090 num_print_interval = 0;
1093 static void print_header(struct perf_stat_config *config,
1094 struct target *_target,
1095 int argc, const char **argv)
1097 FILE *output = config->output;
1102 if (!config->csv_output && !config->json_output) {
1103 fprintf(output, "\n");
1104 fprintf(output, " Performance counter stats for ");
1105 if (_target->bpf_str)
1106 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1107 else if (_target->system_wide)
1108 fprintf(output, "\'system wide");
1109 else if (_target->cpu_list)
1110 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1111 else if (!target__has_task(_target)) {
1112 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1113 for (i = 1; argv && (i < argc); i++)
1114 fprintf(output, " %s", argv[i]);
1115 } else if (_target->pid)
1116 fprintf(output, "process id \'%s", _target->pid);
1118 fprintf(output, "thread id \'%s", _target->tid);
1120 fprintf(output, "\'");
1121 if (config->run_count > 1)
1122 fprintf(output, " (%d runs)", config->run_count);
1123 fprintf(output, ":\n\n");
1127 static int get_precision(double num)
1132 return lround(ceil(-log10(num)));
1135 static void print_table(struct perf_stat_config *config,
1136 FILE *output, int precision, double avg)
1139 int idx, indent = 0;
1141 scnprintf(tmp, 64, " %17.*f", precision, avg);
1142 while (tmp[indent] == ' ')
1145 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1147 for (idx = 0; idx < config->run_count; idx++) {
1148 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1149 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1151 fprintf(output, " %17.*f (%+.*f) ",
1152 precision, run, precision, run - avg);
1154 for (h = 0; h < n; h++)
1155 fprintf(output, "#");
1157 fprintf(output, "\n");
1160 fprintf(output, "\n%*s# Final result:\n", indent, "");
1163 static double timeval2double(struct timeval *t)
1165 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1168 static void print_footer(struct perf_stat_config *config)
1170 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1171 FILE *output = config->output;
1173 if (!config->null_run)
1174 fprintf(output, "\n");
1176 if (config->run_count == 1) {
1177 fprintf(output, " %17.9f seconds time elapsed", avg);
1179 if (config->ru_display) {
1180 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1181 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1183 fprintf(output, "\n\n");
1184 fprintf(output, " %17.9f seconds user\n", ru_utime);
1185 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1188 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1190 * Display at most 2 more significant
1191 * digits than the stddev inaccuracy.
1193 int precision = get_precision(sd) + 2;
1195 if (config->walltime_run_table)
1196 print_table(config, output, precision, avg);
1198 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1199 precision, avg, precision, sd);
1201 print_noise_pct(config, sd, avg, /*before_metric=*/false);
1203 fprintf(output, "\n\n");
1205 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1207 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1208 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1210 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1212 if (config->print_mixed_hw_group_error)
1214 "The events in group usually have to be from "
1215 "the same PMU. Try reorganizing the group.\n");
1218 static void print_percore(struct perf_stat_config *config,
1219 struct evsel *counter, char *prefix)
1221 bool metric_only = config->metric_only;
1222 FILE *output = config->output;
1223 struct cpu_aggr_map *core_map;
1227 if (!config->aggr_map || !config->aggr_get_id)
1230 if (config->percore_show_thread)
1231 return print_counter(config, counter, prefix);
1233 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1234 if (core_map == NULL) {
1235 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1239 for (s = 0, c = 0; s < config->aggr_map->nr; s++) {
1240 struct perf_cpu curr_cpu = config->aggr_map->map[s].cpu;
1241 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1244 for (i = 0; i < c; i++) {
1245 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1253 if (prefix && metric_only)
1254 fprintf(output, "%s", prefix);
1256 print_counter_aggrdata(config, counter, s,
1257 prefix, metric_only, &first);
1259 core_map->map[c++] = core_id;
1264 fputc('\n', output);
1267 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1268 struct target *_target, struct timespec *ts, int argc, const char **argv)
1270 bool metric_only = config->metric_only;
1271 int interval = config->interval;
1272 struct evsel *counter;
1273 char buf[64], *prefix = NULL;
1275 if (config->iostat_run)
1276 evlist->selected = evlist__first(evlist);
1280 prepare_interval(config, prefix, ts);
1281 print_interval(config, evlist);
1283 print_header(config, _target, argc, argv);
1287 static int num_print_iv;
1289 if (num_print_iv == 0 && !interval)
1290 print_metric_headers(config, evlist, prefix, false);
1291 if (num_print_iv++ == 25)
1293 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1294 fprintf(config->output, "%s", prefix);
1297 switch (config->aggr_mode) {
1302 print_aggr(config, evlist, prefix);
1306 if (config->iostat_run)
1307 iostat_print_counters(evlist, config, ts, prefix = buf,
1310 evlist__for_each_entry(evlist, counter) {
1311 print_counter(config, counter, prefix);
1314 fputc('\n', config->output);
1319 print_no_aggr_metric(config, evlist, prefix);
1321 evlist__for_each_entry(evlist, counter) {
1322 if (counter->percore)
1323 print_percore(config, counter, prefix);
1325 print_counter(config, counter, prefix);
1335 if (!interval && !config->csv_output && !config->json_output)
1336 print_footer(config);
1338 fflush(config->output);