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>
25 #define CNTR_NOT_SUPPORTED "<not supported>"
26 #define CNTR_NOT_COUNTED "<not counted>"
32 #define INTERVAL_LEN 16
38 static int aggr_header_lens[] = {
49 static const char *aggr_header_csv[] = {
50 [AGGR_CORE] = "core,cpus,",
51 [AGGR_CACHE] = "cache,cpus,",
52 [AGGR_DIE] = "die,cpus,",
53 [AGGR_SOCKET] = "socket,cpus,",
55 [AGGR_THREAD] = "comm-pid,",
56 [AGGR_NODE] = "node,",
60 static const char *aggr_header_std[] = {
62 [AGGR_CACHE] = "cache",
64 [AGGR_SOCKET] = "socket",
66 [AGGR_THREAD] = "comm-pid",
71 static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
74 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
77 static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
79 double enabled_percent = 100;
82 enabled_percent = 100 * run / ena;
83 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
84 config->csv_sep, run, config->csv_sep, enabled_percent);
87 static void print_running_json(struct perf_stat_config *config, u64 run, u64 ena)
89 double enabled_percent = 100;
92 enabled_percent = 100 * run / ena;
93 fprintf(config->output, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
94 run, enabled_percent);
97 static void print_running(struct perf_stat_config *config,
98 u64 run, u64 ena, bool before_metric)
100 if (config->json_output) {
102 print_running_json(config, run, ena);
103 } else if (config->csv_output) {
105 print_running_csv(config, run, ena);
108 print_running_std(config, run, ena);
112 static void print_noise_pct_std(struct perf_stat_config *config,
116 fprintf(config->output, " ( +-%6.2f%% )", pct);
119 static void print_noise_pct_csv(struct perf_stat_config *config,
122 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
125 static void print_noise_pct_json(struct perf_stat_config *config,
128 fprintf(config->output, "\"variance\" : %.2f, ", pct);
131 static void print_noise_pct(struct perf_stat_config *config,
132 double total, double avg, bool before_metric)
134 double pct = rel_stddev_stats(total, avg);
136 if (config->json_output) {
138 print_noise_pct_json(config, pct);
139 } else if (config->csv_output) {
141 print_noise_pct_csv(config, pct);
144 print_noise_pct_std(config, pct);
148 static void print_noise(struct perf_stat_config *config,
149 struct evsel *evsel, double avg, bool before_metric)
151 struct perf_stat_evsel *ps;
153 if (config->run_count == 1)
157 print_noise_pct(config, stddev_stats(&ps->res_stats), avg, before_metric);
160 static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
162 fprintf(config->output, " %-*s", CGROUP_LEN, cgrp_name);
165 static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
167 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
170 static void print_cgroup_json(struct perf_stat_config *config, const char *cgrp_name)
172 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
175 static void print_cgroup(struct perf_stat_config *config, struct cgroup *cgrp)
177 if (nr_cgroups || config->cgroup_list) {
178 const char *cgrp_name = cgrp ? cgrp->name : "";
180 if (config->json_output)
181 print_cgroup_json(config, cgrp_name);
182 else if (config->csv_output)
183 print_cgroup_csv(config, cgrp_name);
185 print_cgroup_std(config, cgrp_name);
189 static void print_aggr_id_std(struct perf_stat_config *config,
190 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
192 FILE *output = config->output;
193 int idx = config->aggr_mode;
196 switch (config->aggr_mode) {
198 snprintf(buf, sizeof(buf), "S%d-D%d-C%d", id.socket, id.die, id.core);
201 snprintf(buf, sizeof(buf), "S%d-D%d-L%d-ID%d",
202 id.socket, id.die, id.cache_lvl, id.cache);
205 snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
208 snprintf(buf, sizeof(buf), "S%d", id.socket);
211 snprintf(buf, sizeof(buf), "N%d", id.node);
214 if (evsel->percore && !config->percore_show_thread) {
215 snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
216 id.socket, id.die, id.core);
217 fprintf(output, "%-*s ",
218 aggr_header_lens[AGGR_CORE], buf);
219 } else if (id.cpu.cpu > -1) {
220 fprintf(output, "CPU%-*d ",
221 aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
225 fprintf(output, "%*s-%-*d ",
226 COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
227 PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
236 fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, 4, aggr_nr);
239 static void print_aggr_id_csv(struct perf_stat_config *config,
240 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
242 FILE *output = config->output;
243 const char *sep = config->csv_sep;
245 switch (config->aggr_mode) {
247 fprintf(output, "S%d-D%d-C%d%s%d%s",
248 id.socket, id.die, id.core, sep, aggr_nr, sep);
251 fprintf(config->output, "S%d-D%d-L%d-ID%d%s%d%s",
252 id.socket, id.die, id.cache_lvl, id.cache, sep, aggr_nr, sep);
255 fprintf(output, "S%d-D%d%s%d%s",
256 id.socket, id.die, sep, aggr_nr, sep);
259 fprintf(output, "S%d%s%d%s",
260 id.socket, sep, aggr_nr, sep);
263 fprintf(output, "N%d%s%d%s",
264 id.node, sep, aggr_nr, sep);
267 if (evsel->percore && !config->percore_show_thread) {
268 fprintf(output, "S%d-D%d-C%d%s",
269 id.socket, id.die, id.core, sep);
270 } else if (id.cpu.cpu > -1) {
271 fprintf(output, "CPU%d%s",
276 fprintf(output, "%s-%d%s",
277 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
278 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
289 static void print_aggr_id_json(struct perf_stat_config *config,
290 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
292 FILE *output = config->output;
294 switch (config->aggr_mode) {
296 fprintf(output, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
297 id.socket, id.die, id.core, aggr_nr);
300 fprintf(output, "\"cache\" : \"S%d-D%d-L%d-ID%d\", \"aggregate-number\" : %d, ",
301 id.socket, id.die, id.cache_lvl, id.cache, aggr_nr);
304 fprintf(output, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
305 id.socket, id.die, aggr_nr);
308 fprintf(output, "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
312 fprintf(output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
316 if (evsel->percore && !config->percore_show_thread) {
317 fprintf(output, "\"core\" : \"S%d-D%d-C%d\"",
318 id.socket, id.die, id.core);
319 } else if (id.cpu.cpu > -1) {
320 fprintf(output, "\"cpu\" : \"%d\", ",
325 fprintf(output, "\"thread\" : \"%s-%d\", ",
326 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
327 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
337 static void aggr_printout(struct perf_stat_config *config,
338 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
340 if (config->json_output)
341 print_aggr_id_json(config, evsel, id, aggr_nr);
342 else if (config->csv_output)
343 print_aggr_id_csv(config, evsel, id, aggr_nr);
345 print_aggr_id_std(config, evsel, id, aggr_nr);
355 struct aggr_cpu_id id;
360 static void new_line_std(struct perf_stat_config *config __maybe_unused,
363 struct outstate *os = ctx;
368 static inline void __new_line_std_csv(struct perf_stat_config *config,
373 fputs(os->prefix, os->fh);
374 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
377 static inline void __new_line_std(struct outstate *os)
379 fprintf(os->fh, " ");
382 static void do_new_line_std(struct perf_stat_config *config,
385 __new_line_std_csv(config, os);
386 if (config->aggr_mode == AGGR_NONE)
387 fprintf(os->fh, " ");
391 static void print_metric_std(struct perf_stat_config *config,
392 void *ctx, const char *color, const char *fmt,
393 const char *unit, double val)
395 struct outstate *os = ctx;
398 bool newline = os->newline;
402 if (unit == NULL || fmt == NULL) {
403 fprintf(out, "%-*s", METRIC_LEN, "");
408 do_new_line_std(config, os);
410 n = fprintf(out, " # ");
412 n += color_fprintf(out, color, fmt, val);
414 n += fprintf(out, fmt, val);
415 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
418 static void new_line_csv(struct perf_stat_config *config, void *ctx)
420 struct outstate *os = ctx;
423 __new_line_std_csv(config, os);
424 for (i = 0; i < os->nfields; i++)
425 fputs(config->csv_sep, os->fh);
428 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
430 const char *color __maybe_unused,
431 const char *fmt, const char *unit, double val)
433 struct outstate *os = ctx;
435 char buf[64], *vals, *ends;
437 if (unit == NULL || fmt == NULL) {
438 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
441 snprintf(buf, sizeof(buf), fmt, val);
442 ends = vals = skip_spaces(buf);
443 while (isdigit(*ends) || *ends == '.')
446 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
449 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
451 const char *color __maybe_unused,
452 const char *fmt __maybe_unused,
453 const char *unit, double val)
455 struct outstate *os = ctx;
458 fprintf(out, "\"metric-value\" : \"%f\", ", val);
459 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
460 if (!config->metric_only)
464 static void new_line_json(struct perf_stat_config *config, void *ctx)
466 struct outstate *os = ctx;
468 fputs("\n{", os->fh);
470 fprintf(os->fh, "%s", os->prefix);
471 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
474 static void print_metricgroup_header_json(struct perf_stat_config *config,
476 const char *metricgroup_name)
478 if (!metricgroup_name)
481 fprintf(config->output, "\"metricgroup\" : \"%s\"}", metricgroup_name);
482 new_line_json(config, ctx);
485 static void print_metricgroup_header_csv(struct perf_stat_config *config,
487 const char *metricgroup_name)
489 struct outstate *os = ctx;
492 if (!metricgroup_name) {
493 /* Leave space for running and enabling */
494 for (i = 0; i < os->nfields - 2; i++)
495 fputs(config->csv_sep, os->fh);
499 for (i = 0; i < os->nfields; i++)
500 fputs(config->csv_sep, os->fh);
501 fprintf(config->output, "%s", metricgroup_name);
502 new_line_csv(config, ctx);
505 static void print_metricgroup_header_std(struct perf_stat_config *config,
507 const char *metricgroup_name)
509 struct outstate *os = ctx;
512 if (!metricgroup_name) {
517 n = fprintf(config->output, " %*s", EVNAME_LEN, metricgroup_name);
519 fprintf(config->output, "%*s", MGROUP_LEN - n - 1, "");
522 /* Filter out some columns that don't work well in metrics only mode */
524 static bool valid_only_metric(const char *unit)
528 if (strstr(unit, "/sec") ||
529 strstr(unit, "CPUs utilized"))
534 static const char *fixunit(char *buf, struct evsel *evsel,
537 if (!strncmp(unit, "of all", 6)) {
538 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
545 static void print_metric_only(struct perf_stat_config *config,
546 void *ctx, const char *color, const char *fmt,
547 const char *unit, double val)
549 struct outstate *os = ctx;
551 char buf[1024], str[1024];
552 unsigned mlen = config->metric_only_len;
554 if (!valid_only_metric(unit))
556 unit = fixunit(buf, os->evsel, unit);
557 if (mlen < strlen(unit))
558 mlen = strlen(unit) + 1;
561 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
563 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
564 fprintf(out, "%*s ", mlen, str);
568 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
569 void *ctx, const char *color __maybe_unused,
571 const char *unit, double val)
573 struct outstate *os = ctx;
575 char buf[64], *vals, *ends;
578 if (!valid_only_metric(unit))
580 unit = fixunit(tbuf, os->evsel, unit);
581 snprintf(buf, sizeof buf, fmt, val);
582 ends = vals = skip_spaces(buf);
583 while (isdigit(*ends) || *ends == '.')
586 fprintf(out, "%s%s", vals, config->csv_sep);
590 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
591 void *ctx, const char *color __maybe_unused,
593 const char *unit, double val)
595 struct outstate *os = ctx;
597 char buf[64], *vals, *ends;
600 if (!valid_only_metric(unit))
602 unit = fixunit(tbuf, os->evsel, unit);
603 snprintf(buf, sizeof(buf), fmt, val);
604 ends = vals = skip_spaces(buf);
605 while (isdigit(*ends) || *ends == '.')
608 if (!unit[0] || !vals[0])
610 fprintf(out, "%s\"%s\" : \"%s\"", os->first ? "" : ", ", unit, vals);
614 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
615 void *ctx __maybe_unused)
619 static void print_metric_header(struct perf_stat_config *config,
620 void *ctx, const char *color __maybe_unused,
621 const char *fmt __maybe_unused,
622 const char *unit, double val __maybe_unused)
624 struct outstate *os = ctx;
627 /* In case of iostat, print metric header for first root port only */
628 if (config->iostat_run &&
629 os->evsel->priv != os->evsel->evlist->selected->priv)
632 if (os->evsel->cgrp != os->cgrp)
635 if (!valid_only_metric(unit))
637 unit = fixunit(tbuf, os->evsel, unit);
639 if (config->json_output)
641 else if (config->csv_output)
642 fprintf(os->fh, "%s%s", unit, config->csv_sep);
644 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
647 static void print_counter_value_std(struct perf_stat_config *config,
648 struct evsel *evsel, double avg, bool ok)
650 FILE *output = config->output;
651 double sc = evsel->scale;
653 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
656 fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
658 fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
661 fprintf(output, fmt, COUNTS_LEN, avg);
663 fprintf(output, "%*s ", COUNTS_LEN, bad_count);
666 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
668 fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
671 static void print_counter_value_csv(struct perf_stat_config *config,
672 struct evsel *evsel, double avg, bool ok)
674 FILE *output = config->output;
675 double sc = evsel->scale;
676 const char *sep = config->csv_sep;
677 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
678 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
681 fprintf(output, fmt, avg, sep);
683 fprintf(output, "%s%s", bad_count, sep);
686 fprintf(output, "%s%s", evsel->unit, sep);
688 fprintf(output, "%s", evsel__name(evsel));
691 static void print_counter_value_json(struct perf_stat_config *config,
692 struct evsel *evsel, double avg, bool ok)
694 FILE *output = config->output;
695 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
698 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
700 fprintf(output, "\"counter-value\" : \"%s\", ", bad_count);
703 fprintf(output, "\"unit\" : \"%s\", ", evsel->unit);
705 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
708 static void print_counter_value(struct perf_stat_config *config,
709 struct evsel *evsel, double avg, bool ok)
711 if (config->json_output)
712 print_counter_value_json(config, evsel, avg, ok);
713 else if (config->csv_output)
714 print_counter_value_csv(config, evsel, avg, ok);
716 print_counter_value_std(config, evsel, avg, ok);
719 static void abs_printout(struct perf_stat_config *config,
720 struct aggr_cpu_id id, int aggr_nr,
721 struct evsel *evsel, double avg, bool ok)
723 aggr_printout(config, evsel, id, aggr_nr);
724 print_counter_value(config, evsel, avg, ok);
725 print_cgroup(config, evsel->cgrp);
728 static bool is_mixed_hw_group(struct evsel *counter)
730 struct evlist *evlist = counter->evlist;
731 u32 pmu_type = counter->core.attr.type;
734 if (counter->core.nr_members < 2)
737 evlist__for_each_entry(evlist, pos) {
738 /* software events can be part of any hardware group */
739 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
741 if (pmu_type == PERF_TYPE_SOFTWARE) {
742 pmu_type = pos->core.attr.type;
745 if (pmu_type != pos->core.attr.type)
752 static bool evlist__has_hybrid(struct evlist *evlist)
756 if (perf_pmus__num_core_pmus() == 1)
759 evlist__for_each_entry(evlist, evsel) {
760 if (evsel->core.is_pmu_core)
767 static void printout(struct perf_stat_config *config, struct outstate *os,
768 double uval, u64 run, u64 ena, double noise, int aggr_idx)
770 struct perf_stat_output_ctx out;
773 print_metricgroup_header_t pmh;
775 struct evsel *counter = os->evsel;
777 if (config->csv_output) {
778 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
779 nl = config->metric_only ? new_line_metric : new_line_csv;
780 pmh = print_metricgroup_header_csv;
781 os->nfields = 4 + (counter->cgrp ? 1 : 0);
782 } else if (config->json_output) {
783 pm = config->metric_only ? print_metric_only_json : print_metric_json;
784 nl = config->metric_only ? new_line_metric : new_line_json;
785 pmh = print_metricgroup_header_json;
787 pm = config->metric_only ? print_metric_only : print_metric_std;
788 nl = config->metric_only ? new_line_metric : new_line_std;
789 pmh = print_metricgroup_header_std;
792 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
793 if (config->metric_only) {
794 pm(config, os, NULL, "", "", 0);
800 if (counter->supported) {
801 if (!evlist__has_hybrid(counter->evlist)) {
802 config->print_free_counters_hint = 1;
803 if (is_mixed_hw_group(counter))
804 config->print_mixed_hw_group_error = 1;
809 out.print_metric = pm;
811 out.print_metricgroup_header = pmh;
813 out.force_header = false;
815 if (!config->metric_only && !counter->default_metricgroup) {
816 abs_printout(config, os->id, os->aggr_nr, counter, uval, ok);
818 print_noise(config, counter, noise, /*before_metric=*/true);
819 print_running(config, run, ena, /*before_metric=*/true);
823 if (!config->metric_only && counter->default_metricgroup) {
826 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
827 /* Print out all the metricgroup with the same metric event. */
831 /* Print out the new line for the next new metricgroup. */
833 if (config->json_output)
834 new_line_json(config, (void *)os);
836 __new_line_std_csv(config, os);
839 print_noise(config, counter, noise, /*before_metric=*/true);
840 print_running(config, run, ena, /*before_metric=*/true);
841 from = perf_stat__print_shadow_stats_metricgroup(config, counter, aggr_idx,
843 &config->metric_events);
844 } while (from != NULL);
846 perf_stat__print_shadow_stats(config, counter, uval, aggr_idx,
847 &out, &config->metric_events);
849 pm(config, os, /*color=*/NULL, /*format=*/NULL, /*unit=*/"", /*val=*/0);
852 if (!config->metric_only) {
853 print_noise(config, counter, noise, /*before_metric=*/false);
854 print_running(config, run, ena, /*before_metric=*/false);
858 static void uniquify_event_name(struct evsel *counter)
864 if (counter->uniquified_name || counter->use_config_name ||
865 !counter->pmu_name || !strncmp(evsel__name(counter), counter->pmu_name,
866 strlen(counter->pmu_name)))
869 config = strchr(counter->name, '/');
871 if (asprintf(&new_name,
872 "%s%s", counter->pmu_name, config) > 0) {
874 counter->name = new_name;
877 if (evsel__is_hybrid(counter)) {
878 ret = asprintf(&new_name, "%s/%s/",
879 counter->pmu_name, counter->name);
881 ret = asprintf(&new_name, "%s [%s]",
882 counter->name, counter->pmu_name);
887 counter->name = new_name;
891 counter->uniquified_name = true;
894 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
896 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
899 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
901 if (config->no_merge || hybrid_uniquify(counter, config))
902 uniquify_event_name(counter);
906 * should_skip_zero_count() - Check if the event should print 0 values.
907 * @config: The perf stat configuration (including aggregation mode).
908 * @counter: The evsel with its associated cpumap.
909 * @id: The aggregation id that is being queried.
911 * Due to mismatch between the event cpumap or thread-map and the
912 * aggregation mode, sometimes it'd iterate the counter with the map
913 * which does not contain any values.
915 * For example, uncore events have dedicated CPUs to manage them,
916 * result for other CPUs should be zero and skipped.
918 * Return: %true if the value should NOT be printed, %false if the value
919 * needs to be printed like "<not counted>" or "<not supported>".
921 static bool should_skip_zero_counter(struct perf_stat_config *config,
922 struct evsel *counter,
923 const struct aggr_cpu_id *id)
929 * Skip value 0 when enabling --per-thread globally,
930 * otherwise it will have too many 0 output.
932 if (config->aggr_mode == AGGR_THREAD && config->system_wide)
935 /* Tool events have the software PMU but are only gathered on 1. */
936 if (evsel__is_tool(counter))
940 * Skip value 0 when it's an uncore event and the given aggr id
941 * does not belong to the PMU cpumask.
943 if (!counter->pmu || !counter->pmu->is_uncore)
946 perf_cpu_map__for_each_cpu(cpu, idx, counter->pmu->cpus) {
947 struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
949 if (aggr_cpu_id__equal(id, &own_id))
955 static void print_counter_aggrdata(struct perf_stat_config *config,
956 struct evsel *counter, int aggr_idx,
959 FILE *output = config->output;
962 struct perf_stat_evsel *ps = counter->stats;
963 struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
964 struct aggr_cpu_id id = config->aggr_map->map[aggr_idx];
965 double avg = aggr->counts.val;
966 bool metric_only = config->metric_only;
969 os->aggr_nr = aggr->nr;
972 /* Skip already merged uncore/hybrid events */
973 if (counter->merged_stat)
976 uniquify_counter(config, counter);
978 val = aggr->counts.val;
979 ena = aggr->counts.ena;
980 run = aggr->counts.run;
982 if (perf_stat__skip_metric_event(counter, &config->metric_events, ena, run))
985 if (val == 0 && should_skip_zero_counter(config, counter, &id))
989 if (config->json_output)
992 fprintf(output, "%s", os->prefix);
993 else if (config->summary && config->csv_output &&
994 !config->no_csv_summary && !config->interval)
995 fprintf(output, "%s%s", "summary", config->csv_sep);
998 uval = val * counter->scale;
1000 printout(config, os, uval, run, ena, avg, aggr_idx);
1003 fputc('\n', output);
1006 static void print_metric_begin(struct perf_stat_config *config,
1007 struct evlist *evlist,
1008 struct outstate *os, int aggr_idx)
1010 struct perf_stat_aggr *aggr;
1011 struct aggr_cpu_id id;
1012 struct evsel *evsel;
1015 if (!config->metric_only)
1018 if (config->json_output)
1019 fputc('{', config->output);
1021 fprintf(config->output, "%s", os->prefix);
1023 evsel = evlist__first(evlist);
1024 id = config->aggr_map->map[aggr_idx];
1025 aggr = &evsel->stats->aggr[aggr_idx];
1026 aggr_printout(config, evsel, id, aggr->nr);
1028 print_cgroup(config, os->cgrp ? : evsel->cgrp);
1031 static void print_metric_end(struct perf_stat_config *config, struct outstate *os)
1033 FILE *output = config->output;
1035 if (!config->metric_only)
1038 if (config->json_output) {
1040 fputs("\"metric-value\" : \"none\"", output);
1043 fputc('\n', output);
1046 static void print_aggr(struct perf_stat_config *config,
1047 struct evlist *evlist,
1048 struct outstate *os)
1050 struct evsel *counter;
1053 if (!config->aggr_map || !config->aggr_get_id)
1057 * With metric_only everything is on a single line.
1058 * Without each counter has its own line.
1060 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1061 print_metric_begin(config, evlist, os, aggr_idx);
1063 evlist__for_each_entry(evlist, counter) {
1064 print_counter_aggrdata(config, counter, aggr_idx, os);
1066 print_metric_end(config, os);
1070 static void print_aggr_cgroup(struct perf_stat_config *config,
1071 struct evlist *evlist,
1072 struct outstate *os)
1074 struct evsel *counter, *evsel;
1077 if (!config->aggr_map || !config->aggr_get_id)
1080 evlist__for_each_entry(evlist, evsel) {
1081 if (os->cgrp == evsel->cgrp)
1084 os->cgrp = evsel->cgrp;
1086 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1087 print_metric_begin(config, evlist, os, aggr_idx);
1089 evlist__for_each_entry(evlist, counter) {
1090 if (counter->cgrp != os->cgrp)
1093 print_counter_aggrdata(config, counter, aggr_idx, os);
1095 print_metric_end(config, os);
1100 static void print_counter(struct perf_stat_config *config,
1101 struct evsel *counter, struct outstate *os)
1105 /* AGGR_THREAD doesn't have config->aggr_get_id */
1106 if (!config->aggr_map)
1109 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1110 print_counter_aggrdata(config, counter, aggr_idx, os);
1114 static void print_no_aggr_metric(struct perf_stat_config *config,
1115 struct evlist *evlist,
1116 struct outstate *os)
1119 struct perf_cpu cpu;
1121 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1122 struct evsel *counter;
1125 evlist__for_each_entry(evlist, counter) {
1128 struct perf_stat_evsel *ps = counter->stats;
1129 int aggr_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1134 os->evsel = counter;
1135 os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1137 print_metric_begin(config, evlist, os, aggr_idx);
1140 val = ps->aggr[aggr_idx].counts.val;
1141 ena = ps->aggr[aggr_idx].counts.ena;
1142 run = ps->aggr[aggr_idx].counts.run;
1144 uval = val * counter->scale;
1145 printout(config, os, uval, run, ena, 1.0, aggr_idx);
1148 print_metric_end(config, os);
1152 static void print_metric_headers_std(struct perf_stat_config *config,
1155 fputc(' ', config->output);
1158 int len = aggr_header_lens[config->aggr_mode];
1160 if (nr_cgroups || config->cgroup_list)
1161 len += CGROUP_LEN + 1;
1163 fprintf(config->output, "%*s", len, "");
1167 static void print_metric_headers_csv(struct perf_stat_config *config,
1168 bool no_indent __maybe_unused)
1170 if (config->interval)
1171 fputs("time,", config->output);
1172 if (!config->iostat_run)
1173 fputs(aggr_header_csv[config->aggr_mode], config->output);
1176 static void print_metric_headers_json(struct perf_stat_config *config __maybe_unused,
1177 bool no_indent __maybe_unused)
1181 static void print_metric_headers(struct perf_stat_config *config,
1182 struct evlist *evlist, bool no_indent)
1184 struct evsel *counter;
1185 struct outstate os = {
1186 .fh = config->output
1188 struct perf_stat_output_ctx out = {
1190 .print_metric = print_metric_header,
1191 .new_line = new_line_metric,
1192 .force_header = true,
1195 if (config->json_output)
1196 print_metric_headers_json(config, no_indent);
1197 else if (config->csv_output)
1198 print_metric_headers_csv(config, no_indent);
1200 print_metric_headers_std(config, no_indent);
1202 if (config->iostat_run)
1203 iostat_print_header_prefix(config);
1205 if (config->cgroup_list)
1206 os.cgrp = evlist__first(evlist)->cgrp;
1208 /* Print metrics headers only */
1209 evlist__for_each_entry(evlist, counter) {
1212 perf_stat__print_shadow_stats(config, counter, 0,
1215 &config->metric_events);
1218 if (!config->json_output)
1219 fputc('\n', config->output);
1222 static void prepare_interval(struct perf_stat_config *config,
1223 char *prefix, size_t len, struct timespec *ts)
1225 if (config->iostat_run)
1228 if (config->json_output)
1229 scnprintf(prefix, len, "\"interval\" : %lu.%09lu, ",
1230 (unsigned long) ts->tv_sec, ts->tv_nsec);
1231 else if (config->csv_output)
1232 scnprintf(prefix, len, "%lu.%09lu%s",
1233 (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1235 scnprintf(prefix, len, "%6lu.%09lu ",
1236 (unsigned long) ts->tv_sec, ts->tv_nsec);
1239 static void print_header_interval_std(struct perf_stat_config *config,
1240 struct target *_target __maybe_unused,
1241 struct evlist *evlist,
1242 int argc __maybe_unused,
1243 const char **argv __maybe_unused)
1245 FILE *output = config->output;
1247 switch (config->aggr_mode) {
1253 fprintf(output, "#%*s %-*s cpus",
1254 INTERVAL_LEN - 1, "time",
1255 aggr_header_lens[config->aggr_mode],
1256 aggr_header_std[config->aggr_mode]);
1259 fprintf(output, "#%*s %-*s",
1260 INTERVAL_LEN - 1, "time",
1261 aggr_header_lens[config->aggr_mode],
1262 aggr_header_std[config->aggr_mode]);
1265 fprintf(output, "#%*s %*s-%-*s",
1266 INTERVAL_LEN - 1, "time",
1267 COMM_LEN, "comm", PID_LEN, "pid");
1271 if (!config->iostat_run)
1272 fprintf(output, "#%*s",
1273 INTERVAL_LEN - 1, "time");
1279 if (config->metric_only)
1280 print_metric_headers(config, evlist, true);
1282 fprintf(output, " %*s %*s events\n",
1283 COUNTS_LEN, "counts", config->unit_width, "unit");
1286 static void print_header_std(struct perf_stat_config *config,
1287 struct target *_target, struct evlist *evlist,
1288 int argc, const char **argv)
1290 FILE *output = config->output;
1293 fprintf(output, "\n");
1294 fprintf(output, " Performance counter stats for ");
1295 if (_target->bpf_str)
1296 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1297 else if (_target->system_wide)
1298 fprintf(output, "\'system wide");
1299 else if (_target->cpu_list)
1300 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1301 else if (!target__has_task(_target)) {
1302 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1303 for (i = 1; argv && (i < argc); i++)
1304 fprintf(output, " %s", argv[i]);
1305 } else if (_target->pid)
1306 fprintf(output, "process id \'%s", _target->pid);
1308 fprintf(output, "thread id \'%s", _target->tid);
1310 fprintf(output, "\'");
1311 if (config->run_count > 1)
1312 fprintf(output, " (%d runs)", config->run_count);
1313 fprintf(output, ":\n\n");
1315 if (config->metric_only)
1316 print_metric_headers(config, evlist, false);
1319 static void print_header_csv(struct perf_stat_config *config,
1320 struct target *_target __maybe_unused,
1321 struct evlist *evlist,
1322 int argc __maybe_unused,
1323 const char **argv __maybe_unused)
1325 if (config->metric_only)
1326 print_metric_headers(config, evlist, true);
1328 static void print_header_json(struct perf_stat_config *config,
1329 struct target *_target __maybe_unused,
1330 struct evlist *evlist,
1331 int argc __maybe_unused,
1332 const char **argv __maybe_unused)
1334 if (config->metric_only)
1335 print_metric_headers(config, evlist, true);
1338 static void print_header(struct perf_stat_config *config,
1339 struct target *_target,
1340 struct evlist *evlist,
1341 int argc, const char **argv)
1343 static int num_print_iv;
1347 if (config->interval_clear)
1348 puts(CONSOLE_CLEAR);
1350 if (num_print_iv == 0 || config->interval_clear) {
1351 if (config->json_output)
1352 print_header_json(config, _target, evlist, argc, argv);
1353 else if (config->csv_output)
1354 print_header_csv(config, _target, evlist, argc, argv);
1355 else if (config->interval)
1356 print_header_interval_std(config, _target, evlist, argc, argv);
1358 print_header_std(config, _target, evlist, argc, argv);
1361 if (num_print_iv++ == 25)
1365 static int get_precision(double num)
1370 return lround(ceil(-log10(num)));
1373 static void print_table(struct perf_stat_config *config,
1374 FILE *output, int precision, double avg)
1377 int idx, indent = 0;
1379 scnprintf(tmp, 64, " %17.*f", precision, avg);
1380 while (tmp[indent] == ' ')
1383 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1385 for (idx = 0; idx < config->run_count; idx++) {
1386 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1387 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1389 fprintf(output, " %17.*f (%+.*f) ",
1390 precision, run, precision, run - avg);
1392 for (h = 0; h < n; h++)
1393 fprintf(output, "#");
1395 fprintf(output, "\n");
1398 fprintf(output, "\n%*s# Final result:\n", indent, "");
1401 static double timeval2double(struct timeval *t)
1403 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1406 static void print_footer(struct perf_stat_config *config)
1408 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1409 FILE *output = config->output;
1411 if (config->interval || config->csv_output || config->json_output)
1414 if (!config->null_run)
1415 fprintf(output, "\n");
1417 if (config->run_count == 1) {
1418 fprintf(output, " %17.9f seconds time elapsed", avg);
1420 if (config->ru_display) {
1421 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1422 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1424 fprintf(output, "\n\n");
1425 fprintf(output, " %17.9f seconds user\n", ru_utime);
1426 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1429 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1431 * Display at most 2 more significant
1432 * digits than the stddev inaccuracy.
1434 int precision = get_precision(sd) + 2;
1436 if (config->walltime_run_table)
1437 print_table(config, output, precision, avg);
1439 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1440 precision, avg, precision, sd);
1442 print_noise_pct(config, sd, avg, /*before_metric=*/false);
1444 fprintf(output, "\n\n");
1446 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1448 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1449 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1451 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1453 if (config->print_mixed_hw_group_error)
1455 "The events in group usually have to be from "
1456 "the same PMU. Try reorganizing the group.\n");
1459 static void print_percore(struct perf_stat_config *config,
1460 struct evsel *counter, struct outstate *os)
1462 bool metric_only = config->metric_only;
1463 FILE *output = config->output;
1464 struct cpu_aggr_map *core_map;
1465 int aggr_idx, core_map_len = 0;
1467 if (!config->aggr_map || !config->aggr_get_id)
1470 if (config->percore_show_thread)
1471 return print_counter(config, counter, os);
1474 * core_map will hold the aggr_cpu_id for the cores that have been
1475 * printed so that each core is printed just once.
1477 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1478 if (core_map == NULL) {
1479 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1483 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1484 struct perf_cpu curr_cpu = config->aggr_map->map[aggr_idx].cpu;
1485 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1488 for (int i = 0; i < core_map_len; i++) {
1489 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1497 print_counter_aggrdata(config, counter, aggr_idx, os);
1499 core_map->map[core_map_len++] = core_id;
1504 fputc('\n', output);
1507 static void print_cgroup_counter(struct perf_stat_config *config, struct evlist *evlist,
1508 struct outstate *os)
1510 struct evsel *counter;
1512 evlist__for_each_entry(evlist, counter) {
1513 if (os->cgrp != counter->cgrp) {
1514 if (os->cgrp != NULL)
1515 print_metric_end(config, os);
1517 os->cgrp = counter->cgrp;
1518 print_metric_begin(config, evlist, os, /*aggr_idx=*/0);
1521 print_counter(config, counter, os);
1524 print_metric_end(config, os);
1527 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1528 struct target *_target, struct timespec *ts,
1529 int argc, const char **argv)
1531 bool metric_only = config->metric_only;
1532 int interval = config->interval;
1533 struct evsel *counter;
1535 struct outstate os = {
1536 .fh = config->output,
1540 if (config->iostat_run)
1541 evlist->selected = evlist__first(evlist);
1545 prepare_interval(config, buf, sizeof(buf), ts);
1548 print_header(config, _target, evlist, argc, argv);
1550 switch (config->aggr_mode) {
1556 if (config->cgroup_list)
1557 print_aggr_cgroup(config, evlist, &os);
1559 print_aggr(config, evlist, &os);
1563 if (config->iostat_run) {
1564 iostat_print_counters(evlist, config, ts, buf,
1565 (iostat_print_counter_t)print_counter, &os);
1566 } else if (config->cgroup_list) {
1567 print_cgroup_counter(config, evlist, &os);
1569 print_metric_begin(config, evlist, &os, /*aggr_idx=*/0);
1570 evlist__for_each_entry(evlist, counter) {
1571 print_counter(config, counter, &os);
1573 print_metric_end(config, &os);
1578 print_no_aggr_metric(config, evlist, &os);
1580 evlist__for_each_entry(evlist, counter) {
1581 if (counter->percore)
1582 print_percore(config, counter, &os);
1584 print_counter(config, counter, &os);
1594 print_footer(config);
1596 fflush(config->output);