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,
55 u64 run, u64 ena, bool before_metric)
57 if (config->json_output) {
59 print_running_json(config, run, ena);
60 } else if (config->csv_output) {
62 print_running_csv(config, run, ena);
65 print_running_std(config, run, ena);
69 static void print_noise_pct_std(struct perf_stat_config *config,
73 fprintf(config->output, " ( +-%6.2f%% )", pct);
76 static void print_noise_pct_csv(struct perf_stat_config *config,
79 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
82 static void print_noise_pct_json(struct perf_stat_config *config,
85 fprintf(config->output, "\"variance\" : %.2f, ", pct);
88 static void print_noise_pct(struct perf_stat_config *config,
89 double total, double avg, bool before_metric)
91 double pct = rel_stddev_stats(total, avg);
93 if (config->json_output) {
95 print_noise_pct_json(config, pct);
96 } else if (config->csv_output) {
98 print_noise_pct_csv(config, pct);
101 print_noise_pct_std(config, pct);
105 static void print_noise(struct perf_stat_config *config,
106 struct evsel *evsel, double avg, bool before_metric)
108 struct perf_stat_evsel *ps;
110 if (config->run_count == 1)
114 print_noise_pct(config, stddev_stats(&ps->res_stats), avg, before_metric);
117 static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
119 fprintf(config->output, " %-16s", cgrp_name);
122 static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
124 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
127 static void print_cgroup_json(struct perf_stat_config *config, const char *cgrp_name)
129 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
132 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
135 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
137 if (config->json_output)
138 print_cgroup_json(config, cgrp_name);
139 if (config->csv_output)
140 print_cgroup_csv(config, cgrp_name);
142 print_cgroup_std(config, cgrp_name);
146 static void print_aggr_id_std(struct perf_stat_config *config,
147 struct evsel *evsel, struct aggr_cpu_id id, int nr)
149 FILE *output = config->output;
151 switch (config->aggr_mode) {
153 fprintf(output, "S%d-D%d-C%*d %*d ",
154 id.socket, id.die, -8, id.core, 4, nr);
157 fprintf(output, "S%d-D%*d %*d ",
158 id.socket, -8, id.die, 4, nr);
161 fprintf(output, "S%*d %*d ",
162 -5, id.socket, 4, nr);
165 fprintf(output, "N%*d %*d ",
169 if (evsel->percore && !config->percore_show_thread) {
170 fprintf(output, "S%d-D%d-C%*d ",
171 id.socket, id.die, -3, id.core);
172 } else if (id.cpu.cpu > -1) {
173 fprintf(output, "CPU%*d ",
178 fprintf(output, "%*s-%*d ",
179 16, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
180 -8, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
190 static void print_aggr_id_csv(struct perf_stat_config *config,
191 struct evsel *evsel, struct aggr_cpu_id id, int nr)
193 FILE *output = config->output;
194 const char *sep = config->csv_sep;
196 switch (config->aggr_mode) {
198 fprintf(output, "S%d-D%d-C%d%s%d%s",
199 id.socket, id.die, id.core, sep, nr, sep);
202 fprintf(output, "S%d-D%d%s%d%s",
203 id.socket, id.die, sep, nr, sep);
206 fprintf(output, "S%d%s%d%s",
207 id.socket, sep, nr, sep);
210 fprintf(output, "N%d%s%d%s",
211 id.node, sep, nr, sep);
214 if (evsel->percore && !config->percore_show_thread) {
215 fprintf(output, "S%d-D%d-C%d%s",
216 id.socket, id.die, id.core, sep);
217 } else if (id.cpu.cpu > -1) {
218 fprintf(output, "CPU%d%s",
223 fprintf(output, "%s-%d%s",
224 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
225 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
236 static void print_aggr_id_json(struct perf_stat_config *config,
237 struct evsel *evsel, struct aggr_cpu_id id, int nr)
239 FILE *output = config->output;
241 if (!config->interval)
244 switch (config->aggr_mode) {
246 fprintf(output, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
247 id.socket, id.die, id.core, nr);
250 fprintf(output, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
251 id.socket, id.die, nr);
254 fprintf(output, "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
258 fprintf(output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
262 if (evsel->percore && !config->percore_show_thread) {
263 fprintf(output, "\"core\" : \"S%d-D%d-C%d\"",
264 id.socket, id.die, id.core);
265 } else if (id.cpu.cpu > -1) {
266 fprintf(output, "\"cpu\" : \"%d\", ",
271 fprintf(output, "\"thread\" : \"%s-%d\", ",
272 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
273 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
283 static void aggr_printout(struct perf_stat_config *config,
284 struct evsel *evsel, struct aggr_cpu_id id, int nr)
286 if (config->json_output)
287 print_aggr_id_json(config, evsel, id, nr);
288 else if (config->csv_output)
289 print_aggr_id_csv(config, evsel, id, nr);
291 print_aggr_id_std(config, evsel, id, nr);
300 struct aggr_cpu_id id;
304 #define METRIC_LEN 38
306 static void new_line_std(struct perf_stat_config *config __maybe_unused,
309 struct outstate *os = ctx;
314 static void do_new_line_std(struct perf_stat_config *config,
318 fputs(os->prefix, os->fh);
319 aggr_printout(config, os->evsel, os->id, os->nr);
320 if (config->aggr_mode == AGGR_NONE)
321 fprintf(os->fh, " ");
322 fprintf(os->fh, " ");
325 static void print_metric_std(struct perf_stat_config *config,
326 void *ctx, const char *color, const char *fmt,
327 const char *unit, double val)
329 struct outstate *os = ctx;
332 bool newline = os->newline;
336 if (unit == NULL || fmt == NULL) {
337 fprintf(out, "%-*s", METRIC_LEN, "");
342 do_new_line_std(config, os);
344 n = fprintf(out, " # ");
346 n += color_fprintf(out, color, fmt, val);
348 n += fprintf(out, fmt, val);
349 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
352 static void new_line_csv(struct perf_stat_config *config, void *ctx)
354 struct outstate *os = ctx;
359 fprintf(os->fh, "%s", os->prefix);
360 aggr_printout(config, os->evsel, os->id, os->nr);
361 for (i = 0; i < os->nfields; i++)
362 fputs(config->csv_sep, os->fh);
365 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
367 const char *color __maybe_unused,
368 const char *fmt, const char *unit, double val)
370 struct outstate *os = ctx;
372 char buf[64], *vals, *ends;
374 if (unit == NULL || fmt == NULL) {
375 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
378 snprintf(buf, sizeof(buf), fmt, val);
379 ends = vals = skip_spaces(buf);
380 while (isdigit(*ends) || *ends == '.')
383 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
386 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
388 const char *color __maybe_unused,
389 const char *fmt __maybe_unused,
390 const char *unit, double val)
392 struct outstate *os = ctx;
395 fprintf(out, "\"metric-value\" : %f, ", val);
396 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
397 if (!config->metric_only)
401 static void new_line_json(struct perf_stat_config *config, void *ctx)
403 struct outstate *os = ctx;
407 fprintf(os->fh, "%s", os->prefix);
408 aggr_printout(config, os->evsel, os->id, os->nr);
411 /* Filter out some columns that don't work well in metrics only mode */
413 static bool valid_only_metric(const char *unit)
417 if (strstr(unit, "/sec") ||
418 strstr(unit, "CPUs utilized"))
423 static const char *fixunit(char *buf, struct evsel *evsel,
426 if (!strncmp(unit, "of all", 6)) {
427 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
434 static void print_metric_only(struct perf_stat_config *config,
435 void *ctx, const char *color, const char *fmt,
436 const char *unit, double val)
438 struct outstate *os = ctx;
440 char buf[1024], str[1024];
441 unsigned mlen = config->metric_only_len;
443 if (!valid_only_metric(unit))
445 unit = fixunit(buf, os->evsel, unit);
446 if (mlen < strlen(unit))
447 mlen = strlen(unit) + 1;
450 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
452 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
453 fprintf(out, "%*s ", mlen, str);
456 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
457 void *ctx, const char *color __maybe_unused,
459 const char *unit, double val)
461 struct outstate *os = ctx;
463 char buf[64], *vals, *ends;
466 if (!valid_only_metric(unit))
468 unit = fixunit(tbuf, os->evsel, unit);
469 snprintf(buf, sizeof buf, fmt, val);
470 ends = vals = skip_spaces(buf);
471 while (isdigit(*ends) || *ends == '.')
474 fprintf(out, "%s%s", vals, config->csv_sep);
477 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
478 void *ctx, const char *color __maybe_unused,
480 const char *unit, double val)
482 struct outstate *os = ctx;
484 char buf[64], *vals, *ends;
487 if (!valid_only_metric(unit))
489 unit = fixunit(tbuf, os->evsel, unit);
490 snprintf(buf, sizeof(buf), fmt, val);
491 ends = vals = skip_spaces(buf);
492 while (isdigit(*ends) || *ends == '.')
495 fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
498 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
499 void *ctx __maybe_unused)
503 static void print_metric_header(struct perf_stat_config *config,
504 void *ctx, const char *color __maybe_unused,
505 const char *fmt __maybe_unused,
506 const char *unit, double val __maybe_unused)
508 struct outstate *os = ctx;
511 /* In case of iostat, print metric header for first root port only */
512 if (config->iostat_run &&
513 os->evsel->priv != os->evsel->evlist->selected->priv)
516 if (!valid_only_metric(unit))
518 unit = fixunit(tbuf, os->evsel, unit);
520 if (config->json_output)
521 fprintf(os->fh, "{\"unit\" : \"%s\"}", unit);
522 else if (config->csv_output)
523 fprintf(os->fh, "%s%s", unit, config->csv_sep);
525 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
528 static void print_counter_value_std(struct perf_stat_config *config,
529 struct evsel *evsel, double avg, bool ok)
531 FILE *output = config->output;
532 double sc = evsel->scale;
534 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
537 fmt = floor(sc) != sc ? "%'18.2f " : "%'18.0f ";
539 fmt = floor(sc) != sc ? "%18.2f " : "%18.0f ";
542 fprintf(output, fmt, avg);
544 fprintf(output, "%18s ", bad_count);
547 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
549 fprintf(output, "%-*s", 32, evsel__name(evsel));
552 static void print_counter_value_csv(struct perf_stat_config *config,
553 struct evsel *evsel, double avg, bool ok)
555 FILE *output = config->output;
556 double sc = evsel->scale;
557 const char *sep = config->csv_sep;
558 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
559 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
562 fprintf(output, fmt, avg, sep);
564 fprintf(output, "%s%s", bad_count, sep);
567 fprintf(output, "%s%s", evsel->unit, sep);
569 fprintf(output, "%s", evsel__name(evsel));
572 static void print_counter_value_json(struct perf_stat_config *config,
573 struct evsel *evsel, double avg, bool ok)
575 FILE *output = config->output;
576 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
579 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
581 fprintf(output, "\"counter-value\" : \"%s\", ", bad_count);
584 fprintf(output, "\"unit\" : \"%s\", ", evsel->unit);
586 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
589 static void print_counter_value(struct perf_stat_config *config,
590 struct evsel *evsel, double avg, bool ok)
592 if (config->json_output)
593 print_counter_value_json(config, evsel, avg, ok);
594 else if (config->csv_output)
595 print_counter_value_csv(config, evsel, avg, ok);
597 print_counter_value_std(config, evsel, avg, ok);
600 static void abs_printout(struct perf_stat_config *config,
601 struct aggr_cpu_id id, int nr,
602 struct evsel *evsel, double avg, bool ok)
604 aggr_printout(config, evsel, id, nr);
605 print_counter_value(config, evsel, avg, ok);
606 print_cgroup(config, evsel);
609 static bool is_mixed_hw_group(struct evsel *counter)
611 struct evlist *evlist = counter->evlist;
612 u32 pmu_type = counter->core.attr.type;
615 if (counter->core.nr_members < 2)
618 evlist__for_each_entry(evlist, pos) {
619 /* software events can be part of any hardware group */
620 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
622 if (pmu_type == PERF_TYPE_SOFTWARE) {
623 pmu_type = pos->core.attr.type;
626 if (pmu_type != pos->core.attr.type)
633 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
634 struct evsel *counter, double uval,
635 char *prefix, u64 run, u64 ena, double noise,
636 struct runtime_stat *st, int map_idx)
638 struct perf_stat_output_ctx out;
639 struct outstate os = {
640 .fh = config->output,
641 .prefix = prefix ? prefix : "",
650 if (config->csv_output) {
651 static const int aggr_fields[AGGR_MAX] = {
662 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
663 nl = config->metric_only ? new_line_metric : new_line_csv;
664 os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
665 } else if (config->json_output) {
666 pm = config->metric_only ? print_metric_only_json : print_metric_json;
667 nl = config->metric_only ? new_line_metric : new_line_json;
669 pm = config->metric_only ? print_metric_only : print_metric_std;
670 nl = config->metric_only ? new_line_metric : new_line_std;
673 if (!config->no_csv_summary && config->csv_output &&
674 config->summary && !config->interval && !config->metric_only) {
675 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
678 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
679 if (config->metric_only) {
680 pm(config, &os, NULL, "", "", 0);
686 if (counter->supported) {
687 if (!evlist__has_hybrid(counter->evlist)) {
688 config->print_free_counters_hint = 1;
689 if (is_mixed_hw_group(counter))
690 config->print_mixed_hw_group_error = 1;
695 out.print_metric = pm;
698 out.force_header = false;
700 if (!config->metric_only) {
701 abs_printout(config, id, nr, counter, uval, ok);
703 print_noise(config, counter, noise, /*before_metric=*/true);
704 print_running(config, run, ena, /*before_metric=*/true);
708 perf_stat__print_shadow_stats(config, counter, uval, map_idx,
709 &out, &config->metric_events, st);
711 pm(config, &os, /*color=*/NULL, /*format=*/NULL, /*unit=*/"", /*val=*/0);
714 if (!config->metric_only) {
715 print_noise(config, counter, noise, /*before_metric=*/false);
716 print_running(config, run, ena, /*before_metric=*/false);
720 static void uniquify_event_name(struct evsel *counter)
726 if (counter->uniquified_name || counter->use_config_name ||
727 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
728 strlen(counter->pmu_name)))
731 config = strchr(counter->name, '/');
733 if (asprintf(&new_name,
734 "%s%s", counter->pmu_name, config) > 0) {
736 counter->name = new_name;
739 if (evsel__is_hybrid(counter)) {
740 ret = asprintf(&new_name, "%s/%s/",
741 counter->pmu_name, counter->name);
743 ret = asprintf(&new_name, "%s [%s]",
744 counter->name, counter->pmu_name);
749 counter->name = new_name;
753 counter->uniquified_name = true;
756 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
758 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
761 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
763 if (config->no_merge || hybrid_uniquify(counter, config))
764 uniquify_event_name(counter);
767 static void print_counter_aggrdata(struct perf_stat_config *config,
768 struct evsel *counter, int s,
769 char *prefix, bool metric_only,
772 FILE *output = config->output;
775 struct perf_stat_evsel *ps = counter->stats;
776 struct perf_stat_aggr *aggr = &ps->aggr[s];
777 struct aggr_cpu_id id = config->aggr_map->map[s];
778 double avg = aggr->counts.val;
780 if (counter->supported && aggr->nr == 0)
783 uniquify_counter(config, counter);
785 val = aggr->counts.val;
786 ena = aggr->counts.ena;
787 run = aggr->counts.run;
789 if (*first && metric_only) {
791 aggr_printout(config, counter, id, aggr->nr);
793 if (prefix && !metric_only)
794 fprintf(output, "%s", prefix);
796 uval = val * counter->scale;
798 printout(config, id, aggr->nr, counter, uval,
799 prefix, run, ena, avg, &rt_stat, s);
805 static void print_aggr(struct perf_stat_config *config,
806 struct evlist *evlist,
809 bool metric_only = config->metric_only;
810 FILE *output = config->output;
811 struct evsel *counter;
815 if (!config->aggr_map || !config->aggr_get_id)
819 * With metric_only everything is on a single line.
820 * Without each counter has its own line.
822 for (s = 0; s < config->aggr_map->nr; s++) {
825 fprintf(output, "%s", prefix);
826 else if (config->summary && !config->no_csv_summary &&
827 config->csv_output && !config->interval)
828 fprintf(output, "%16s%s", "summary", config->csv_sep);
832 evlist__for_each_entry(evlist, counter) {
833 if (counter->merged_stat)
836 print_counter_aggrdata(config, counter, s,
845 static void print_counter(struct perf_stat_config *config,
846 struct evsel *counter, char *prefix)
848 bool metric_only = config->metric_only;
852 /* AGGR_THREAD doesn't have config->aggr_get_id */
853 if (!config->aggr_map)
856 if (counter->merged_stat)
859 for (s = 0; s < config->aggr_map->nr; s++) {
860 print_counter_aggrdata(config, counter, s,
866 static void print_no_aggr_metric(struct perf_stat_config *config,
867 struct evlist *evlist,
873 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
874 struct evsel *counter;
877 evlist__for_each_entry(evlist, counter) {
880 struct aggr_cpu_id id;
881 struct perf_stat_evsel *ps = counter->stats;
882 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
887 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
890 fputs(prefix, config->output);
891 aggr_printout(config, counter, id, 0);
894 val = ps->aggr[counter_idx].counts.val;
895 ena = ps->aggr[counter_idx].counts.ena;
896 run = ps->aggr[counter_idx].counts.run;
898 uval = val * counter->scale;
899 printout(config, id, 0, counter, uval, prefix,
900 run, ena, 1.0, &rt_stat, counter_idx);
903 fputc('\n', config->output);
907 static int aggr_header_lens[] = {
917 static const char *aggr_header_csv[] = {
918 [AGGR_CORE] = "core,cpus,",
919 [AGGR_DIE] = "die,cpus,",
920 [AGGR_SOCKET] = "socket,cpus,",
921 [AGGR_NONE] = "cpu,",
922 [AGGR_THREAD] = "comm-pid,",
923 [AGGR_NODE] = "node,",
927 static void print_metric_headers_std(struct perf_stat_config *config,
928 const char *prefix, bool no_indent)
931 fprintf(config->output, "%s", prefix);
933 fprintf(config->output, "%*s",
934 aggr_header_lens[config->aggr_mode], "");
938 static void print_metric_headers_csv(struct perf_stat_config *config,
940 bool no_indent __maybe_unused)
943 fprintf(config->output, "%s", prefix);
944 if (config->interval)
945 fputs("time,", config->output);
946 if (!config->iostat_run)
947 fputs(aggr_header_csv[config->aggr_mode], config->output);
950 static void print_metric_headers_json(struct perf_stat_config *config,
951 const char *prefix __maybe_unused,
952 bool no_indent __maybe_unused)
954 if (config->interval)
955 fputs("{\"unit\" : \"sec\"}", config->output);
958 static void print_metric_headers(struct perf_stat_config *config,
959 struct evlist *evlist,
960 const char *prefix, bool no_indent)
962 struct evsel *counter;
963 struct outstate os = {
966 struct perf_stat_output_ctx out = {
968 .print_metric = print_metric_header,
969 .new_line = new_line_metric,
970 .force_header = true,
973 if (config->json_output)
974 print_metric_headers_json(config, prefix, no_indent);
975 else if (config->csv_output)
976 print_metric_headers_csv(config, prefix, no_indent);
978 print_metric_headers_std(config, prefix, no_indent);
980 if (config->iostat_run)
981 iostat_print_header_prefix(config);
983 /* Print metrics headers only */
984 evlist__for_each_entry(evlist, counter) {
987 perf_stat__print_shadow_stats(config, counter, 0,
990 &config->metric_events,
993 fputc('\n', config->output);
996 static void prepare_interval(struct perf_stat_config *config,
997 char *prefix, struct timespec *ts)
999 if (config->iostat_run)
1002 if (!config->json_output)
1003 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1004 ts->tv_nsec, config->csv_sep);
1005 else if (!config->metric_only)
1006 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1007 ts->tv_sec, ts->tv_nsec);
1009 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1010 ts->tv_sec, ts->tv_nsec);
1013 static void print_interval(struct perf_stat_config *config,
1014 struct evlist *evlist)
1016 bool metric_only = config->metric_only;
1017 unsigned int unit_width = config->unit_width;
1018 FILE *output = config->output;
1019 static int num_print_interval;
1021 if (config->interval_clear && isatty(fileno(output)))
1022 puts(CONSOLE_CLEAR);
1024 if ((num_print_interval == 0 || config->interval_clear) &&
1025 !config->csv_output && !config->json_output) {
1026 switch (config->aggr_mode) {
1028 fprintf(output, "# time node cpus");
1030 fprintf(output, " counts %*s events\n", unit_width, "unit");
1033 fprintf(output, "# time socket cpus");
1035 fprintf(output, " counts %*s events\n", unit_width, "unit");
1038 fprintf(output, "# time die cpus");
1040 fprintf(output, " counts %*s events\n", unit_width, "unit");
1043 fprintf(output, "# time core cpus");
1045 fprintf(output, " counts %*s events\n", unit_width, "unit");
1048 fprintf(output, "# time CPU ");
1050 fprintf(output, " counts %*s events\n", unit_width, "unit");
1053 fprintf(output, "# time comm-pid");
1055 fprintf(output, " counts %*s events\n", unit_width, "unit");
1059 if (!config->iostat_run) {
1060 fprintf(output, "# time");
1062 fprintf(output, " counts %*s events\n", unit_width, "unit");
1070 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1071 print_metric_headers(config, evlist, " ", true);
1072 if (++num_print_interval == 25)
1073 num_print_interval = 0;
1076 static void print_header(struct perf_stat_config *config,
1077 struct target *_target,
1078 int argc, const char **argv)
1080 FILE *output = config->output;
1085 if (!config->csv_output && !config->json_output) {
1086 fprintf(output, "\n");
1087 fprintf(output, " Performance counter stats for ");
1088 if (_target->bpf_str)
1089 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1090 else if (_target->system_wide)
1091 fprintf(output, "\'system wide");
1092 else if (_target->cpu_list)
1093 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1094 else if (!target__has_task(_target)) {
1095 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1096 for (i = 1; argv && (i < argc); i++)
1097 fprintf(output, " %s", argv[i]);
1098 } else if (_target->pid)
1099 fprintf(output, "process id \'%s", _target->pid);
1101 fprintf(output, "thread id \'%s", _target->tid);
1103 fprintf(output, "\'");
1104 if (config->run_count > 1)
1105 fprintf(output, " (%d runs)", config->run_count);
1106 fprintf(output, ":\n\n");
1110 static int get_precision(double num)
1115 return lround(ceil(-log10(num)));
1118 static void print_table(struct perf_stat_config *config,
1119 FILE *output, int precision, double avg)
1122 int idx, indent = 0;
1124 scnprintf(tmp, 64, " %17.*f", precision, avg);
1125 while (tmp[indent] == ' ')
1128 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1130 for (idx = 0; idx < config->run_count; idx++) {
1131 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1132 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1134 fprintf(output, " %17.*f (%+.*f) ",
1135 precision, run, precision, run - avg);
1137 for (h = 0; h < n; h++)
1138 fprintf(output, "#");
1140 fprintf(output, "\n");
1143 fprintf(output, "\n%*s# Final result:\n", indent, "");
1146 static double timeval2double(struct timeval *t)
1148 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1151 static void print_footer(struct perf_stat_config *config)
1153 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1154 FILE *output = config->output;
1156 if (!config->null_run)
1157 fprintf(output, "\n");
1159 if (config->run_count == 1) {
1160 fprintf(output, " %17.9f seconds time elapsed", avg);
1162 if (config->ru_display) {
1163 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1164 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1166 fprintf(output, "\n\n");
1167 fprintf(output, " %17.9f seconds user\n", ru_utime);
1168 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1171 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1173 * Display at most 2 more significant
1174 * digits than the stddev inaccuracy.
1176 int precision = get_precision(sd) + 2;
1178 if (config->walltime_run_table)
1179 print_table(config, output, precision, avg);
1181 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1182 precision, avg, precision, sd);
1184 print_noise_pct(config, sd, avg, /*before_metric=*/false);
1186 fprintf(output, "\n\n");
1188 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1190 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1191 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1193 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1195 if (config->print_mixed_hw_group_error)
1197 "The events in group usually have to be from "
1198 "the same PMU. Try reorganizing the group.\n");
1201 static void print_percore(struct perf_stat_config *config,
1202 struct evsel *counter, char *prefix)
1204 bool metric_only = config->metric_only;
1205 FILE *output = config->output;
1206 struct cpu_aggr_map *core_map;
1210 if (!config->aggr_map || !config->aggr_get_id)
1213 if (config->percore_show_thread)
1214 return print_counter(config, counter, prefix);
1216 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1217 if (core_map == NULL) {
1218 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1222 for (s = 0, c = 0; s < config->aggr_map->nr; s++) {
1223 struct perf_cpu curr_cpu = config->aggr_map->map[s].cpu;
1224 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1227 for (i = 0; i < c; i++) {
1228 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1236 if (prefix && metric_only)
1237 fprintf(output, "%s", prefix);
1239 print_counter_aggrdata(config, counter, s,
1240 prefix, metric_only, &first);
1242 core_map->map[c++] = core_id;
1247 fputc('\n', output);
1250 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1251 struct target *_target, struct timespec *ts, int argc, const char **argv)
1253 bool metric_only = config->metric_only;
1254 int interval = config->interval;
1255 struct evsel *counter;
1256 char buf[64], *prefix = NULL;
1258 if (config->iostat_run)
1259 evlist->selected = evlist__first(evlist);
1263 prepare_interval(config, prefix, ts);
1264 print_interval(config, evlist);
1266 print_header(config, _target, argc, argv);
1270 static int num_print_iv;
1272 if (num_print_iv == 0 && !interval)
1273 print_metric_headers(config, evlist, prefix, false);
1274 if (num_print_iv++ == 25)
1276 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1277 fprintf(config->output, "%s", prefix);
1279 if (config->json_output && !config->metric_only)
1280 fprintf(config->output, "}");
1283 switch (config->aggr_mode) {
1288 print_aggr(config, evlist, prefix);
1292 if (config->iostat_run)
1293 iostat_print_counters(evlist, config, ts, prefix = buf,
1296 evlist__for_each_entry(evlist, counter) {
1297 print_counter(config, counter, prefix);
1300 fputc('\n', config->output);
1305 print_no_aggr_metric(config, evlist, prefix);
1307 evlist__for_each_entry(evlist, counter) {
1308 if (counter->percore)
1309 print_percore(config, counter, prefix);
1311 print_counter(config, counter, prefix);
1321 if (!interval && !config->csv_output && !config->json_output)
1322 print_footer(config);
1324 fflush(config->output);