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-CLS%d", id.socket, id.die, id.cluster);
208 snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
211 snprintf(buf, sizeof(buf), "S%d", id.socket);
214 snprintf(buf, sizeof(buf), "N%d", id.node);
217 if (evsel->percore && !config->percore_show_thread) {
218 snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
219 id.socket, id.die, id.core);
220 fprintf(output, "%-*s ",
221 aggr_header_lens[AGGR_CORE], buf);
222 } else if (id.cpu.cpu > -1) {
223 fprintf(output, "CPU%-*d ",
224 aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
228 fprintf(output, "%*s-%-*d ",
229 COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
230 PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
239 fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, 4, aggr_nr);
242 static void print_aggr_id_csv(struct perf_stat_config *config,
243 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
245 FILE *output = config->output;
246 const char *sep = config->csv_sep;
248 switch (config->aggr_mode) {
250 fprintf(output, "S%d-D%d-C%d%s%d%s",
251 id.socket, id.die, id.core, sep, aggr_nr, sep);
254 fprintf(config->output, "S%d-D%d-L%d-ID%d%s%d%s",
255 id.socket, id.die, id.cache_lvl, id.cache, sep, aggr_nr, sep);
258 fprintf(config->output, "S%d-D%d-CLS%d%s%d%s",
259 id.socket, id.die, id.cluster, sep, aggr_nr, sep);
262 fprintf(output, "S%d-D%d%s%d%s",
263 id.socket, id.die, sep, aggr_nr, sep);
266 fprintf(output, "S%d%s%d%s",
267 id.socket, sep, aggr_nr, sep);
270 fprintf(output, "N%d%s%d%s",
271 id.node, sep, aggr_nr, sep);
274 if (evsel->percore && !config->percore_show_thread) {
275 fprintf(output, "S%d-D%d-C%d%s",
276 id.socket, id.die, id.core, sep);
277 } else if (id.cpu.cpu > -1) {
278 fprintf(output, "CPU%d%s",
283 fprintf(output, "%s-%d%s",
284 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
285 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
296 static void print_aggr_id_json(struct perf_stat_config *config,
297 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
299 FILE *output = config->output;
301 switch (config->aggr_mode) {
303 fprintf(output, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
304 id.socket, id.die, id.core, aggr_nr);
307 fprintf(output, "\"cache\" : \"S%d-D%d-L%d-ID%d\", \"aggregate-number\" : %d, ",
308 id.socket, id.die, id.cache_lvl, id.cache, aggr_nr);
311 fprintf(output, "\"cluster\" : \"S%d-D%d-CLS%d\", \"aggregate-number\" : %d, ",
312 id.socket, id.die, id.cluster, aggr_nr);
315 fprintf(output, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
316 id.socket, id.die, aggr_nr);
319 fprintf(output, "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
323 fprintf(output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
327 if (evsel->percore && !config->percore_show_thread) {
328 fprintf(output, "\"core\" : \"S%d-D%d-C%d\"",
329 id.socket, id.die, id.core);
330 } else if (id.cpu.cpu > -1) {
331 fprintf(output, "\"cpu\" : \"%d\", ",
336 fprintf(output, "\"thread\" : \"%s-%d\", ",
337 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
338 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
348 static void aggr_printout(struct perf_stat_config *config,
349 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
351 if (config->json_output)
352 print_aggr_id_json(config, evsel, id, aggr_nr);
353 else if (config->csv_output)
354 print_aggr_id_csv(config, evsel, id, aggr_nr);
356 print_aggr_id_std(config, evsel, id, aggr_nr);
366 struct aggr_cpu_id id;
371 static void new_line_std(struct perf_stat_config *config __maybe_unused,
374 struct outstate *os = ctx;
379 static inline void __new_line_std_csv(struct perf_stat_config *config,
384 fputs(os->prefix, os->fh);
385 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
388 static inline void __new_line_std(struct outstate *os)
390 fprintf(os->fh, " ");
393 static void do_new_line_std(struct perf_stat_config *config,
396 __new_line_std_csv(config, os);
397 if (config->aggr_mode == AGGR_NONE)
398 fprintf(os->fh, " ");
402 static void print_metric_std(struct perf_stat_config *config,
403 void *ctx, const char *color, const char *fmt,
404 const char *unit, double val)
406 struct outstate *os = ctx;
409 bool newline = os->newline;
413 if (unit == NULL || fmt == NULL) {
414 fprintf(out, "%-*s", METRIC_LEN, "");
419 do_new_line_std(config, os);
421 n = fprintf(out, " # ");
423 n += color_fprintf(out, color, fmt, val);
425 n += fprintf(out, fmt, val);
426 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
429 static void new_line_csv(struct perf_stat_config *config, void *ctx)
431 struct outstate *os = ctx;
434 __new_line_std_csv(config, os);
435 for (i = 0; i < os->nfields; i++)
436 fputs(config->csv_sep, os->fh);
439 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
441 const char *color __maybe_unused,
442 const char *fmt, const char *unit, double val)
444 struct outstate *os = ctx;
446 char buf[64], *vals, *ends;
448 if (unit == NULL || fmt == NULL) {
449 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
452 snprintf(buf, sizeof(buf), fmt, val);
453 ends = vals = skip_spaces(buf);
454 while (isdigit(*ends) || *ends == '.')
457 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
460 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
462 const char *color __maybe_unused,
463 const char *fmt __maybe_unused,
464 const char *unit, double val)
466 struct outstate *os = ctx;
469 fprintf(out, "\"metric-value\" : \"%f\", ", val);
470 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
471 if (!config->metric_only)
475 static void new_line_json(struct perf_stat_config *config, void *ctx)
477 struct outstate *os = ctx;
479 fputs("\n{", os->fh);
481 fprintf(os->fh, "%s", os->prefix);
482 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
485 static void print_metricgroup_header_json(struct perf_stat_config *config,
487 const char *metricgroup_name)
489 if (!metricgroup_name)
492 fprintf(config->output, "\"metricgroup\" : \"%s\"}", metricgroup_name);
493 new_line_json(config, ctx);
496 static void print_metricgroup_header_csv(struct perf_stat_config *config,
498 const char *metricgroup_name)
500 struct outstate *os = ctx;
503 if (!metricgroup_name) {
504 /* Leave space for running and enabling */
505 for (i = 0; i < os->nfields - 2; i++)
506 fputs(config->csv_sep, os->fh);
510 for (i = 0; i < os->nfields; i++)
511 fputs(config->csv_sep, os->fh);
512 fprintf(config->output, "%s", metricgroup_name);
513 new_line_csv(config, ctx);
516 static void print_metricgroup_header_std(struct perf_stat_config *config,
518 const char *metricgroup_name)
520 struct outstate *os = ctx;
523 if (!metricgroup_name) {
528 n = fprintf(config->output, " %*s", EVNAME_LEN, metricgroup_name);
530 fprintf(config->output, "%*s", MGROUP_LEN - n - 1, "");
533 /* Filter out some columns that don't work well in metrics only mode */
535 static bool valid_only_metric(const char *unit)
539 if (strstr(unit, "/sec") ||
540 strstr(unit, "CPUs utilized"))
545 static const char *fixunit(char *buf, struct evsel *evsel,
548 if (!strncmp(unit, "of all", 6)) {
549 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
556 static void print_metric_only(struct perf_stat_config *config,
557 void *ctx, const char *color, const char *fmt,
558 const char *unit, double val)
560 struct outstate *os = ctx;
562 char buf[1024], str[1024];
563 unsigned mlen = config->metric_only_len;
565 if (!valid_only_metric(unit))
567 unit = fixunit(buf, os->evsel, unit);
568 if (mlen < strlen(unit))
569 mlen = strlen(unit) + 1;
572 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
574 color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);
575 fprintf(out, "%*s ", mlen, str);
579 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
580 void *ctx, const char *color __maybe_unused,
582 const char *unit, double val)
584 struct outstate *os = ctx;
586 char buf[64], *vals, *ends;
589 if (!valid_only_metric(unit))
591 unit = fixunit(tbuf, os->evsel, unit);
592 snprintf(buf, sizeof(buf), fmt ?: "", val);
593 ends = vals = skip_spaces(buf);
594 while (isdigit(*ends) || *ends == '.')
597 fprintf(out, "%s%s", vals, config->csv_sep);
601 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
602 void *ctx, const char *color __maybe_unused,
604 const char *unit, double val)
606 struct outstate *os = ctx;
608 char buf[64], *vals, *ends;
611 if (!valid_only_metric(unit))
613 unit = fixunit(tbuf, os->evsel, unit);
614 snprintf(buf, sizeof(buf), fmt ?: "", val);
615 ends = vals = skip_spaces(buf);
616 while (isdigit(*ends) || *ends == '.')
619 if (!unit[0] || !vals[0])
621 fprintf(out, "%s\"%s\" : \"%s\"", os->first ? "" : ", ", unit, vals);
625 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
626 void *ctx __maybe_unused)
630 static void print_metric_header(struct perf_stat_config *config,
631 void *ctx, const char *color __maybe_unused,
632 const char *fmt __maybe_unused,
633 const char *unit, double val __maybe_unused)
635 struct outstate *os = ctx;
638 /* In case of iostat, print metric header for first root port only */
639 if (config->iostat_run &&
640 os->evsel->priv != os->evsel->evlist->selected->priv)
643 if (os->evsel->cgrp != os->cgrp)
646 if (!valid_only_metric(unit))
648 unit = fixunit(tbuf, os->evsel, unit);
650 if (config->json_output)
652 else if (config->csv_output)
653 fprintf(os->fh, "%s%s", unit, config->csv_sep);
655 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
658 static void print_counter_value_std(struct perf_stat_config *config,
659 struct evsel *evsel, double avg, bool ok)
661 FILE *output = config->output;
662 double sc = evsel->scale;
664 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
667 fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
669 fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
672 fprintf(output, fmt, COUNTS_LEN, avg);
674 fprintf(output, "%*s ", COUNTS_LEN, bad_count);
677 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
679 fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
682 static void print_counter_value_csv(struct perf_stat_config *config,
683 struct evsel *evsel, double avg, bool ok)
685 FILE *output = config->output;
686 double sc = evsel->scale;
687 const char *sep = config->csv_sep;
688 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
689 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
692 fprintf(output, fmt, avg, sep);
694 fprintf(output, "%s%s", bad_count, sep);
697 fprintf(output, "%s%s", evsel->unit, sep);
699 fprintf(output, "%s", evsel__name(evsel));
702 static void print_counter_value_json(struct perf_stat_config *config,
703 struct evsel *evsel, double avg, bool ok)
705 FILE *output = config->output;
706 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
709 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
711 fprintf(output, "\"counter-value\" : \"%s\", ", bad_count);
714 fprintf(output, "\"unit\" : \"%s\", ", evsel->unit);
716 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
719 static void print_counter_value(struct perf_stat_config *config,
720 struct evsel *evsel, double avg, bool ok)
722 if (config->json_output)
723 print_counter_value_json(config, evsel, avg, ok);
724 else if (config->csv_output)
725 print_counter_value_csv(config, evsel, avg, ok);
727 print_counter_value_std(config, evsel, avg, ok);
730 static void abs_printout(struct perf_stat_config *config,
731 struct aggr_cpu_id id, int aggr_nr,
732 struct evsel *evsel, double avg, bool ok)
734 aggr_printout(config, evsel, id, aggr_nr);
735 print_counter_value(config, evsel, avg, ok);
736 print_cgroup(config, evsel->cgrp);
739 static bool is_mixed_hw_group(struct evsel *counter)
741 struct evlist *evlist = counter->evlist;
742 u32 pmu_type = counter->core.attr.type;
745 if (counter->core.nr_members < 2)
748 evlist__for_each_entry(evlist, pos) {
749 /* software events can be part of any hardware group */
750 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
752 if (pmu_type == PERF_TYPE_SOFTWARE) {
753 pmu_type = pos->core.attr.type;
756 if (pmu_type != pos->core.attr.type)
763 static bool evlist__has_hybrid(struct evlist *evlist)
767 if (perf_pmus__num_core_pmus() == 1)
770 evlist__for_each_entry(evlist, evsel) {
771 if (evsel->core.is_pmu_core)
778 static void printout(struct perf_stat_config *config, struct outstate *os,
779 double uval, u64 run, u64 ena, double noise, int aggr_idx)
781 struct perf_stat_output_ctx out;
784 print_metricgroup_header_t pmh;
786 struct evsel *counter = os->evsel;
788 if (config->csv_output) {
789 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
790 nl = config->metric_only ? new_line_metric : new_line_csv;
791 pmh = print_metricgroup_header_csv;
792 os->nfields = 4 + (counter->cgrp ? 1 : 0);
793 } else if (config->json_output) {
794 pm = config->metric_only ? print_metric_only_json : print_metric_json;
795 nl = config->metric_only ? new_line_metric : new_line_json;
796 pmh = print_metricgroup_header_json;
798 pm = config->metric_only ? print_metric_only : print_metric_std;
799 nl = config->metric_only ? new_line_metric : new_line_std;
800 pmh = print_metricgroup_header_std;
803 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
804 if (config->metric_only) {
805 pm(config, os, NULL, "", "", 0);
811 if (counter->supported) {
812 if (!evlist__has_hybrid(counter->evlist)) {
813 config->print_free_counters_hint = 1;
814 if (is_mixed_hw_group(counter))
815 config->print_mixed_hw_group_error = 1;
820 out.print_metric = pm;
822 out.print_metricgroup_header = pmh;
824 out.force_header = false;
826 if (!config->metric_only && !counter->default_metricgroup) {
827 abs_printout(config, os->id, os->aggr_nr, counter, uval, ok);
829 print_noise(config, counter, noise, /*before_metric=*/true);
830 print_running(config, run, ena, /*before_metric=*/true);
834 if (!config->metric_only && counter->default_metricgroup) {
837 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
838 /* Print out all the metricgroup with the same metric event. */
842 /* Print out the new line for the next new metricgroup. */
844 if (config->json_output)
845 new_line_json(config, (void *)os);
847 __new_line_std_csv(config, os);
850 print_noise(config, counter, noise, /*before_metric=*/true);
851 print_running(config, run, ena, /*before_metric=*/true);
852 from = perf_stat__print_shadow_stats_metricgroup(config, counter, aggr_idx,
854 &config->metric_events);
855 } while (from != NULL);
857 perf_stat__print_shadow_stats(config, counter, uval, aggr_idx,
858 &out, &config->metric_events);
860 pm(config, os, /*color=*/NULL, /*format=*/NULL, /*unit=*/"", /*val=*/0);
863 if (!config->metric_only) {
864 print_noise(config, counter, noise, /*before_metric=*/false);
865 print_running(config, run, ena, /*before_metric=*/false);
869 static void uniquify_event_name(struct evsel *counter)
875 if (counter->uniquified_name || counter->use_config_name ||
876 !counter->pmu_name || !strncmp(evsel__name(counter), counter->pmu_name,
877 strlen(counter->pmu_name)))
880 config = strchr(counter->name, '/');
882 if (asprintf(&new_name,
883 "%s%s", counter->pmu_name, config) > 0) {
885 counter->name = new_name;
888 if (evsel__is_hybrid(counter)) {
889 ret = asprintf(&new_name, "%s/%s/",
890 counter->pmu_name, counter->name);
892 ret = asprintf(&new_name, "%s [%s]",
893 counter->name, counter->pmu_name);
898 counter->name = new_name;
902 counter->uniquified_name = true;
905 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
907 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
910 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
912 if (config->aggr_mode == AGGR_NONE || hybrid_uniquify(counter, config))
913 uniquify_event_name(counter);
917 * should_skip_zero_count() - Check if the event should print 0 values.
918 * @config: The perf stat configuration (including aggregation mode).
919 * @counter: The evsel with its associated cpumap.
920 * @id: The aggregation id that is being queried.
922 * Due to mismatch between the event cpumap or thread-map and the
923 * aggregation mode, sometimes it'd iterate the counter with the map
924 * which does not contain any values.
926 * For example, uncore events have dedicated CPUs to manage them,
927 * result for other CPUs should be zero and skipped.
929 * Return: %true if the value should NOT be printed, %false if the value
930 * needs to be printed like "<not counted>" or "<not supported>".
932 static bool should_skip_zero_counter(struct perf_stat_config *config,
933 struct evsel *counter,
934 const struct aggr_cpu_id *id)
940 * Skip value 0 when enabling --per-thread globally,
941 * otherwise it will have too many 0 output.
943 if (config->aggr_mode == AGGR_THREAD && config->system_wide)
946 /* Tool events have the software PMU but are only gathered on 1. */
947 if (evsel__is_tool(counter))
951 * Skip value 0 when it's an uncore event and the given aggr id
952 * does not belong to the PMU cpumask.
954 if (!counter->pmu || !counter->pmu->is_uncore)
957 perf_cpu_map__for_each_cpu(cpu, idx, counter->pmu->cpus) {
958 struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
960 if (aggr_cpu_id__equal(id, &own_id))
966 static void print_counter_aggrdata(struct perf_stat_config *config,
967 struct evsel *counter, int aggr_idx,
970 FILE *output = config->output;
973 struct perf_stat_evsel *ps = counter->stats;
974 struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
975 struct aggr_cpu_id id = config->aggr_map->map[aggr_idx];
976 double avg = aggr->counts.val;
977 bool metric_only = config->metric_only;
980 os->aggr_nr = aggr->nr;
983 /* Skip already merged uncore/hybrid events */
984 if (counter->merged_stat)
987 uniquify_counter(config, counter);
989 val = aggr->counts.val;
990 ena = aggr->counts.ena;
991 run = aggr->counts.run;
993 if (perf_stat__skip_metric_event(counter, &config->metric_events, ena, run))
996 if (val == 0 && should_skip_zero_counter(config, counter, &id))
1000 if (config->json_output)
1003 fprintf(output, "%s", os->prefix);
1004 else if (config->summary && config->csv_output &&
1005 !config->no_csv_summary && !config->interval)
1006 fprintf(output, "%s%s", "summary", config->csv_sep);
1009 uval = val * counter->scale;
1011 printout(config, os, uval, run, ena, avg, aggr_idx);
1014 fputc('\n', output);
1017 static void print_metric_begin(struct perf_stat_config *config,
1018 struct evlist *evlist,
1019 struct outstate *os, int aggr_idx)
1021 struct perf_stat_aggr *aggr;
1022 struct aggr_cpu_id id;
1023 struct evsel *evsel;
1026 if (!config->metric_only)
1029 if (config->json_output)
1030 fputc('{', config->output);
1032 fprintf(config->output, "%s", os->prefix);
1034 evsel = evlist__first(evlist);
1035 id = config->aggr_map->map[aggr_idx];
1036 aggr = &evsel->stats->aggr[aggr_idx];
1037 aggr_printout(config, evsel, id, aggr->nr);
1039 print_cgroup(config, os->cgrp ? : evsel->cgrp);
1042 static void print_metric_end(struct perf_stat_config *config, struct outstate *os)
1044 FILE *output = config->output;
1046 if (!config->metric_only)
1049 if (config->json_output) {
1051 fputs("\"metric-value\" : \"none\"", output);
1054 fputc('\n', output);
1057 static void print_aggr(struct perf_stat_config *config,
1058 struct evlist *evlist,
1059 struct outstate *os)
1061 struct evsel *counter;
1064 if (!config->aggr_map || !config->aggr_get_id)
1068 * With metric_only everything is on a single line.
1069 * Without each counter has its own line.
1071 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1072 print_metric_begin(config, evlist, os, aggr_idx);
1074 evlist__for_each_entry(evlist, counter) {
1075 print_counter_aggrdata(config, counter, aggr_idx, os);
1077 print_metric_end(config, os);
1081 static void print_aggr_cgroup(struct perf_stat_config *config,
1082 struct evlist *evlist,
1083 struct outstate *os)
1085 struct evsel *counter, *evsel;
1088 if (!config->aggr_map || !config->aggr_get_id)
1091 evlist__for_each_entry(evlist, evsel) {
1092 if (os->cgrp == evsel->cgrp)
1095 os->cgrp = evsel->cgrp;
1097 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1098 print_metric_begin(config, evlist, os, aggr_idx);
1100 evlist__for_each_entry(evlist, counter) {
1101 if (counter->cgrp != os->cgrp)
1104 print_counter_aggrdata(config, counter, aggr_idx, os);
1106 print_metric_end(config, os);
1111 static void print_counter(struct perf_stat_config *config,
1112 struct evsel *counter, struct outstate *os)
1116 /* AGGR_THREAD doesn't have config->aggr_get_id */
1117 if (!config->aggr_map)
1120 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1121 print_counter_aggrdata(config, counter, aggr_idx, os);
1125 static void print_no_aggr_metric(struct perf_stat_config *config,
1126 struct evlist *evlist,
1127 struct outstate *os)
1130 struct perf_cpu cpu;
1132 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1133 struct evsel *counter;
1136 evlist__for_each_entry(evlist, counter) {
1139 struct perf_stat_evsel *ps = counter->stats;
1142 if (!perf_cpu_map__has(evsel__cpus(counter), cpu))
1145 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1146 if (config->aggr_map->map[aggr_idx].cpu.cpu == cpu.cpu)
1150 os->evsel = counter;
1151 os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1153 print_metric_begin(config, evlist, os, aggr_idx);
1156 val = ps->aggr[aggr_idx].counts.val;
1157 ena = ps->aggr[aggr_idx].counts.ena;
1158 run = ps->aggr[aggr_idx].counts.run;
1160 uval = val * counter->scale;
1161 printout(config, os, uval, run, ena, 1.0, aggr_idx);
1164 print_metric_end(config, os);
1168 static void print_metric_headers_std(struct perf_stat_config *config,
1171 fputc(' ', config->output);
1174 int len = aggr_header_lens[config->aggr_mode];
1176 if (nr_cgroups || config->cgroup_list)
1177 len += CGROUP_LEN + 1;
1179 fprintf(config->output, "%*s", len, "");
1183 static void print_metric_headers_csv(struct perf_stat_config *config,
1184 bool no_indent __maybe_unused)
1186 if (config->interval)
1187 fputs("time,", config->output);
1188 if (!config->iostat_run)
1189 fputs(aggr_header_csv[config->aggr_mode], config->output);
1192 static void print_metric_headers_json(struct perf_stat_config *config __maybe_unused,
1193 bool no_indent __maybe_unused)
1197 static void print_metric_headers(struct perf_stat_config *config,
1198 struct evlist *evlist, bool no_indent)
1200 struct evsel *counter;
1201 struct outstate os = {
1202 .fh = config->output
1204 struct perf_stat_output_ctx out = {
1206 .print_metric = print_metric_header,
1207 .new_line = new_line_metric,
1208 .force_header = true,
1211 if (config->json_output)
1212 print_metric_headers_json(config, no_indent);
1213 else if (config->csv_output)
1214 print_metric_headers_csv(config, no_indent);
1216 print_metric_headers_std(config, no_indent);
1218 if (config->iostat_run)
1219 iostat_print_header_prefix(config);
1221 if (config->cgroup_list)
1222 os.cgrp = evlist__first(evlist)->cgrp;
1224 /* Print metrics headers only */
1225 evlist__for_each_entry(evlist, counter) {
1228 perf_stat__print_shadow_stats(config, counter, 0,
1231 &config->metric_events);
1234 if (!config->json_output)
1235 fputc('\n', config->output);
1238 static void prepare_interval(struct perf_stat_config *config,
1239 char *prefix, size_t len, struct timespec *ts)
1241 if (config->iostat_run)
1244 if (config->json_output)
1245 scnprintf(prefix, len, "\"interval\" : %lu.%09lu, ",
1246 (unsigned long) ts->tv_sec, ts->tv_nsec);
1247 else if (config->csv_output)
1248 scnprintf(prefix, len, "%lu.%09lu%s",
1249 (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1251 scnprintf(prefix, len, "%6lu.%09lu ",
1252 (unsigned long) ts->tv_sec, ts->tv_nsec);
1255 static void print_header_interval_std(struct perf_stat_config *config,
1256 struct target *_target __maybe_unused,
1257 struct evlist *evlist,
1258 int argc __maybe_unused,
1259 const char **argv __maybe_unused)
1261 FILE *output = config->output;
1263 switch (config->aggr_mode) {
1270 fprintf(output, "#%*s %-*s cpus",
1271 INTERVAL_LEN - 1, "time",
1272 aggr_header_lens[config->aggr_mode],
1273 aggr_header_std[config->aggr_mode]);
1276 fprintf(output, "#%*s %-*s",
1277 INTERVAL_LEN - 1, "time",
1278 aggr_header_lens[config->aggr_mode],
1279 aggr_header_std[config->aggr_mode]);
1282 fprintf(output, "#%*s %*s-%-*s",
1283 INTERVAL_LEN - 1, "time",
1284 COMM_LEN, "comm", PID_LEN, "pid");
1288 if (!config->iostat_run)
1289 fprintf(output, "#%*s",
1290 INTERVAL_LEN - 1, "time");
1296 if (config->metric_only)
1297 print_metric_headers(config, evlist, true);
1299 fprintf(output, " %*s %*s events\n",
1300 COUNTS_LEN, "counts", config->unit_width, "unit");
1303 static void print_header_std(struct perf_stat_config *config,
1304 struct target *_target, struct evlist *evlist,
1305 int argc, const char **argv)
1307 FILE *output = config->output;
1310 fprintf(output, "\n");
1311 fprintf(output, " Performance counter stats for ");
1312 if (_target->bpf_str)
1313 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1314 else if (_target->system_wide)
1315 fprintf(output, "\'system wide");
1316 else if (_target->cpu_list)
1317 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1318 else if (!target__has_task(_target)) {
1319 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1320 for (i = 1; argv && (i < argc); i++)
1321 fprintf(output, " %s", argv[i]);
1322 } else if (_target->pid)
1323 fprintf(output, "process id \'%s", _target->pid);
1325 fprintf(output, "thread id \'%s", _target->tid);
1327 fprintf(output, "\'");
1328 if (config->run_count > 1)
1329 fprintf(output, " (%d runs)", config->run_count);
1330 fprintf(output, ":\n\n");
1332 if (config->metric_only)
1333 print_metric_headers(config, evlist, false);
1336 static void print_header_csv(struct perf_stat_config *config,
1337 struct target *_target __maybe_unused,
1338 struct evlist *evlist,
1339 int argc __maybe_unused,
1340 const char **argv __maybe_unused)
1342 if (config->metric_only)
1343 print_metric_headers(config, evlist, true);
1345 static void print_header_json(struct perf_stat_config *config,
1346 struct target *_target __maybe_unused,
1347 struct evlist *evlist,
1348 int argc __maybe_unused,
1349 const char **argv __maybe_unused)
1351 if (config->metric_only)
1352 print_metric_headers(config, evlist, true);
1355 static void print_header(struct perf_stat_config *config,
1356 struct target *_target,
1357 struct evlist *evlist,
1358 int argc, const char **argv)
1360 static int num_print_iv;
1364 if (config->interval_clear)
1365 puts(CONSOLE_CLEAR);
1367 if (num_print_iv == 0 || config->interval_clear) {
1368 if (config->json_output)
1369 print_header_json(config, _target, evlist, argc, argv);
1370 else if (config->csv_output)
1371 print_header_csv(config, _target, evlist, argc, argv);
1372 else if (config->interval)
1373 print_header_interval_std(config, _target, evlist, argc, argv);
1375 print_header_std(config, _target, evlist, argc, argv);
1378 if (num_print_iv++ == 25)
1382 static int get_precision(double num)
1387 return lround(ceil(-log10(num)));
1390 static void print_table(struct perf_stat_config *config,
1391 FILE *output, int precision, double avg)
1394 int idx, indent = 0;
1396 scnprintf(tmp, 64, " %17.*f", precision, avg);
1397 while (tmp[indent] == ' ')
1400 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1402 for (idx = 0; idx < config->run_count; idx++) {
1403 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1404 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1406 fprintf(output, " %17.*f (%+.*f) ",
1407 precision, run, precision, run - avg);
1409 for (h = 0; h < n; h++)
1410 fprintf(output, "#");
1412 fprintf(output, "\n");
1415 fprintf(output, "\n%*s# Final result:\n", indent, "");
1418 static double timeval2double(struct timeval *t)
1420 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1423 static void print_footer(struct perf_stat_config *config)
1425 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1426 FILE *output = config->output;
1428 if (config->interval || config->csv_output || config->json_output)
1431 if (!config->null_run)
1432 fprintf(output, "\n");
1434 if (config->run_count == 1) {
1435 fprintf(output, " %17.9f seconds time elapsed", avg);
1437 if (config->ru_display) {
1438 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1439 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1441 fprintf(output, "\n\n");
1442 fprintf(output, " %17.9f seconds user\n", ru_utime);
1443 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1446 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1448 * Display at most 2 more significant
1449 * digits than the stddev inaccuracy.
1451 int precision = get_precision(sd) + 2;
1453 if (config->walltime_run_table)
1454 print_table(config, output, precision, avg);
1456 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1457 precision, avg, precision, sd);
1459 print_noise_pct(config, sd, avg, /*before_metric=*/false);
1461 fprintf(output, "\n\n");
1463 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1465 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1466 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1468 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1470 if (config->print_mixed_hw_group_error)
1472 "The events in group usually have to be from "
1473 "the same PMU. Try reorganizing the group.\n");
1476 static void print_percore(struct perf_stat_config *config,
1477 struct evsel *counter, struct outstate *os)
1479 bool metric_only = config->metric_only;
1480 FILE *output = config->output;
1481 struct cpu_aggr_map *core_map;
1482 int aggr_idx, core_map_len = 0;
1484 if (!config->aggr_map || !config->aggr_get_id)
1487 if (config->percore_show_thread)
1488 return print_counter(config, counter, os);
1491 * core_map will hold the aggr_cpu_id for the cores that have been
1492 * printed so that each core is printed just once.
1494 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1495 if (core_map == NULL) {
1496 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1500 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1501 struct perf_cpu curr_cpu = config->aggr_map->map[aggr_idx].cpu;
1502 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1505 for (int i = 0; i < core_map_len; i++) {
1506 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1514 print_counter_aggrdata(config, counter, aggr_idx, os);
1516 core_map->map[core_map_len++] = core_id;
1521 fputc('\n', output);
1524 static void print_cgroup_counter(struct perf_stat_config *config, struct evlist *evlist,
1525 struct outstate *os)
1527 struct evsel *counter;
1529 evlist__for_each_entry(evlist, counter) {
1530 if (os->cgrp != counter->cgrp) {
1531 if (os->cgrp != NULL)
1532 print_metric_end(config, os);
1534 os->cgrp = counter->cgrp;
1535 print_metric_begin(config, evlist, os, /*aggr_idx=*/0);
1538 print_counter(config, counter, os);
1541 print_metric_end(config, os);
1544 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1545 struct target *_target, struct timespec *ts,
1546 int argc, const char **argv)
1548 bool metric_only = config->metric_only;
1549 int interval = config->interval;
1550 struct evsel *counter;
1552 struct outstate os = {
1553 .fh = config->output,
1557 if (config->iostat_run)
1558 evlist->selected = evlist__first(evlist);
1562 prepare_interval(config, buf, sizeof(buf), ts);
1565 print_header(config, _target, evlist, argc, argv);
1567 switch (config->aggr_mode) {
1574 if (config->cgroup_list)
1575 print_aggr_cgroup(config, evlist, &os);
1577 print_aggr(config, evlist, &os);
1581 if (config->iostat_run) {
1582 iostat_print_counters(evlist, config, ts, buf,
1583 (iostat_print_counter_t)print_counter, &os);
1584 } else if (config->cgroup_list) {
1585 print_cgroup_counter(config, evlist, &os);
1587 print_metric_begin(config, evlist, &os, /*aggr_idx=*/0);
1588 evlist__for_each_entry(evlist, counter) {
1589 print_counter(config, counter, &os);
1591 print_metric_end(config, &os);
1596 print_no_aggr_metric(config, evlist, &os);
1598 evlist__for_each_entry(evlist, counter) {
1599 if (counter->percore)
1600 print_percore(config, counter, &os);
1602 print_counter(config, counter, &os);
1612 print_footer(config);
1614 fflush(config->output);