4 #include <linux/string.h>
5 #include <linux/time64.h>
7 #include <perf/cpumap.h>
14 #include "thread_map.h"
17 #include <linux/ctype.h>
19 #include <api/fs/fs.h>
22 #include "pmu-hybrid.h"
23 #include "evlist-hybrid.h"
25 #define CNTR_NOT_SUPPORTED "<not supported>"
26 #define CNTR_NOT_COUNTED "<not counted>"
31 #define INTERVAL_LEN 16
37 static int aggr_header_lens[] = {
47 static const char *aggr_header_csv[] = {
48 [AGGR_CORE] = "core,cpus,",
49 [AGGR_DIE] = "die,cpus,",
50 [AGGR_SOCKET] = "socket,cpus,",
52 [AGGR_THREAD] = "comm-pid,",
53 [AGGR_NODE] = "node,",
57 static const char *aggr_header_std[] = {
60 [AGGR_SOCKET] = "socket",
62 [AGGR_THREAD] = "comm-pid",
67 static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
70 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
73 static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
75 double enabled_percent = 100;
78 enabled_percent = 100 * run / ena;
79 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
80 config->csv_sep, run, config->csv_sep, enabled_percent);
83 static void print_running_json(struct perf_stat_config *config, u64 run, u64 ena)
85 double enabled_percent = 100;
88 enabled_percent = 100 * run / ena;
89 fprintf(config->output, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
90 run, enabled_percent);
93 static void print_running(struct perf_stat_config *config,
94 u64 run, u64 ena, bool before_metric)
96 if (config->json_output) {
98 print_running_json(config, run, ena);
99 } else if (config->csv_output) {
101 print_running_csv(config, run, ena);
104 print_running_std(config, run, ena);
108 static void print_noise_pct_std(struct perf_stat_config *config,
112 fprintf(config->output, " ( +-%6.2f%% )", pct);
115 static void print_noise_pct_csv(struct perf_stat_config *config,
118 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
121 static void print_noise_pct_json(struct perf_stat_config *config,
124 fprintf(config->output, "\"variance\" : %.2f, ", pct);
127 static void print_noise_pct(struct perf_stat_config *config,
128 double total, double avg, bool before_metric)
130 double pct = rel_stddev_stats(total, avg);
132 if (config->json_output) {
134 print_noise_pct_json(config, pct);
135 } else if (config->csv_output) {
137 print_noise_pct_csv(config, pct);
140 print_noise_pct_std(config, pct);
144 static void print_noise(struct perf_stat_config *config,
145 struct evsel *evsel, double avg, bool before_metric)
147 struct perf_stat_evsel *ps;
149 if (config->run_count == 1)
153 print_noise_pct(config, stddev_stats(&ps->res_stats), avg, before_metric);
156 static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
158 fprintf(config->output, " %-*s", CGROUP_LEN, cgrp_name);
161 static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
163 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
166 static void print_cgroup_json(struct perf_stat_config *config, const char *cgrp_name)
168 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
171 static void print_cgroup(struct perf_stat_config *config, struct cgroup *cgrp)
173 if (nr_cgroups || config->cgroup_list) {
174 const char *cgrp_name = cgrp ? cgrp->name : "";
176 if (config->json_output)
177 print_cgroup_json(config, cgrp_name);
178 else if (config->csv_output)
179 print_cgroup_csv(config, cgrp_name);
181 print_cgroup_std(config, cgrp_name);
185 static void print_aggr_id_std(struct perf_stat_config *config,
186 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
188 FILE *output = config->output;
189 int idx = config->aggr_mode;
192 switch (config->aggr_mode) {
194 snprintf(buf, sizeof(buf), "S%d-D%d-C%d", id.socket, id.die, id.core);
197 snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
200 snprintf(buf, sizeof(buf), "S%d", id.socket);
203 snprintf(buf, sizeof(buf), "N%d", id.node);
206 if (evsel->percore && !config->percore_show_thread) {
207 snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
208 id.socket, id.die, id.core);
209 fprintf(output, "%-*s ",
210 aggr_header_lens[AGGR_CORE], buf);
211 } else if (id.cpu.cpu > -1) {
212 fprintf(output, "CPU%-*d ",
213 aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
217 fprintf(output, "%*s-%-*d ",
218 COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
219 PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
228 fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, 4, aggr_nr);
231 static void print_aggr_id_csv(struct perf_stat_config *config,
232 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
234 FILE *output = config->output;
235 const char *sep = config->csv_sep;
237 switch (config->aggr_mode) {
239 fprintf(output, "S%d-D%d-C%d%s%d%s",
240 id.socket, id.die, id.core, sep, aggr_nr, sep);
243 fprintf(output, "S%d-D%d%s%d%s",
244 id.socket, id.die, sep, aggr_nr, sep);
247 fprintf(output, "S%d%s%d%s",
248 id.socket, sep, aggr_nr, sep);
251 fprintf(output, "N%d%s%d%s",
252 id.node, sep, aggr_nr, sep);
255 if (evsel->percore && !config->percore_show_thread) {
256 fprintf(output, "S%d-D%d-C%d%s",
257 id.socket, id.die, id.core, sep);
258 } else if (id.cpu.cpu > -1) {
259 fprintf(output, "CPU%d%s",
264 fprintf(output, "%s-%d%s",
265 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
266 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
277 static void print_aggr_id_json(struct perf_stat_config *config,
278 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
280 FILE *output = config->output;
282 switch (config->aggr_mode) {
284 fprintf(output, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
285 id.socket, id.die, id.core, aggr_nr);
288 fprintf(output, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
289 id.socket, id.die, aggr_nr);
292 fprintf(output, "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
296 fprintf(output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
300 if (evsel->percore && !config->percore_show_thread) {
301 fprintf(output, "\"core\" : \"S%d-D%d-C%d\"",
302 id.socket, id.die, id.core);
303 } else if (id.cpu.cpu > -1) {
304 fprintf(output, "\"cpu\" : \"%d\", ",
309 fprintf(output, "\"thread\" : \"%s-%d\", ",
310 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
311 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
321 static void aggr_printout(struct perf_stat_config *config,
322 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
324 if (config->json_output)
325 print_aggr_id_json(config, evsel, id, aggr_nr);
326 else if (config->csv_output)
327 print_aggr_id_csv(config, evsel, id, aggr_nr);
329 print_aggr_id_std(config, evsel, id, aggr_nr);
339 struct aggr_cpu_id id;
344 static void new_line_std(struct perf_stat_config *config __maybe_unused,
347 struct outstate *os = ctx;
352 static void do_new_line_std(struct perf_stat_config *config,
357 fputs(os->prefix, os->fh);
358 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
359 if (config->aggr_mode == AGGR_NONE)
360 fprintf(os->fh, " ");
361 fprintf(os->fh, " ");
364 static void print_metric_std(struct perf_stat_config *config,
365 void *ctx, const char *color, const char *fmt,
366 const char *unit, double val)
368 struct outstate *os = ctx;
371 bool newline = os->newline;
375 if (unit == NULL || fmt == NULL) {
376 fprintf(out, "%-*s", METRIC_LEN, "");
381 do_new_line_std(config, os);
383 n = fprintf(out, " # ");
385 n += color_fprintf(out, color, fmt, val);
387 n += fprintf(out, fmt, val);
388 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
391 static void new_line_csv(struct perf_stat_config *config, void *ctx)
393 struct outstate *os = ctx;
398 fprintf(os->fh, "%s", os->prefix);
399 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
400 for (i = 0; i < os->nfields; i++)
401 fputs(config->csv_sep, os->fh);
404 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
406 const char *color __maybe_unused,
407 const char *fmt, const char *unit, double val)
409 struct outstate *os = ctx;
411 char buf[64], *vals, *ends;
413 if (unit == NULL || fmt == NULL) {
414 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
417 snprintf(buf, sizeof(buf), fmt, val);
418 ends = vals = skip_spaces(buf);
419 while (isdigit(*ends) || *ends == '.')
422 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
425 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
427 const char *color __maybe_unused,
428 const char *fmt __maybe_unused,
429 const char *unit, double val)
431 struct outstate *os = ctx;
434 fprintf(out, "\"metric-value\" : %f, ", val);
435 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
436 if (!config->metric_only)
440 static void new_line_json(struct perf_stat_config *config, void *ctx)
442 struct outstate *os = ctx;
444 fputs("\n{", os->fh);
446 fprintf(os->fh, "%s", os->prefix);
447 aggr_printout(config, os->evsel, os->id, os->aggr_nr);
450 /* Filter out some columns that don't work well in metrics only mode */
452 static bool valid_only_metric(const char *unit)
456 if (strstr(unit, "/sec") ||
457 strstr(unit, "CPUs utilized"))
462 static const char *fixunit(char *buf, struct evsel *evsel,
465 if (!strncmp(unit, "of all", 6)) {
466 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
473 static void print_metric_only(struct perf_stat_config *config,
474 void *ctx, const char *color, const char *fmt,
475 const char *unit, double val)
477 struct outstate *os = ctx;
479 char buf[1024], str[1024];
480 unsigned mlen = config->metric_only_len;
482 if (!valid_only_metric(unit))
484 unit = fixunit(buf, os->evsel, unit);
485 if (mlen < strlen(unit))
486 mlen = strlen(unit) + 1;
489 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
491 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
492 fprintf(out, "%*s ", mlen, str);
496 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
497 void *ctx, const char *color __maybe_unused,
499 const char *unit, double val)
501 struct outstate *os = ctx;
503 char buf[64], *vals, *ends;
506 if (!valid_only_metric(unit))
508 unit = fixunit(tbuf, os->evsel, unit);
509 snprintf(buf, sizeof buf, fmt, val);
510 ends = vals = skip_spaces(buf);
511 while (isdigit(*ends) || *ends == '.')
514 fprintf(out, "%s%s", vals, config->csv_sep);
518 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
519 void *ctx, const char *color __maybe_unused,
521 const char *unit, double val)
523 struct outstate *os = ctx;
525 char buf[64], *vals, *ends;
528 if (!valid_only_metric(unit))
530 unit = fixunit(tbuf, os->evsel, unit);
531 snprintf(buf, sizeof(buf), fmt, val);
532 ends = vals = skip_spaces(buf);
533 while (isdigit(*ends) || *ends == '.')
536 if (!unit[0] || !vals[0])
538 fprintf(out, "%s\"%s\" : \"%s\"", os->first ? "" : ", ", unit, vals);
542 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
543 void *ctx __maybe_unused)
547 static void print_metric_header(struct perf_stat_config *config,
548 void *ctx, const char *color __maybe_unused,
549 const char *fmt __maybe_unused,
550 const char *unit, double val __maybe_unused)
552 struct outstate *os = ctx;
555 /* In case of iostat, print metric header for first root port only */
556 if (config->iostat_run &&
557 os->evsel->priv != os->evsel->evlist->selected->priv)
560 if (os->evsel->cgrp != os->cgrp)
563 if (!valid_only_metric(unit))
565 unit = fixunit(tbuf, os->evsel, unit);
567 if (config->json_output)
569 else if (config->csv_output)
570 fprintf(os->fh, "%s%s", unit, config->csv_sep);
572 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
575 static void print_counter_value_std(struct perf_stat_config *config,
576 struct evsel *evsel, double avg, bool ok)
578 FILE *output = config->output;
579 double sc = evsel->scale;
581 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
584 fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
586 fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
589 fprintf(output, fmt, COUNTS_LEN, avg);
591 fprintf(output, "%*s ", COUNTS_LEN, bad_count);
594 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
596 fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
599 static void print_counter_value_csv(struct perf_stat_config *config,
600 struct evsel *evsel, double avg, bool ok)
602 FILE *output = config->output;
603 double sc = evsel->scale;
604 const char *sep = config->csv_sep;
605 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
606 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
609 fprintf(output, fmt, avg, sep);
611 fprintf(output, "%s%s", bad_count, sep);
614 fprintf(output, "%s%s", evsel->unit, sep);
616 fprintf(output, "%s", evsel__name(evsel));
619 static void print_counter_value_json(struct perf_stat_config *config,
620 struct evsel *evsel, double avg, bool ok)
622 FILE *output = config->output;
623 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
626 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
628 fprintf(output, "\"counter-value\" : \"%s\", ", bad_count);
631 fprintf(output, "\"unit\" : \"%s\", ", evsel->unit);
633 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
636 static void print_counter_value(struct perf_stat_config *config,
637 struct evsel *evsel, double avg, bool ok)
639 if (config->json_output)
640 print_counter_value_json(config, evsel, avg, ok);
641 else if (config->csv_output)
642 print_counter_value_csv(config, evsel, avg, ok);
644 print_counter_value_std(config, evsel, avg, ok);
647 static void abs_printout(struct perf_stat_config *config,
648 struct aggr_cpu_id id, int aggr_nr,
649 struct evsel *evsel, double avg, bool ok)
651 aggr_printout(config, evsel, id, aggr_nr);
652 print_counter_value(config, evsel, avg, ok);
653 print_cgroup(config, evsel->cgrp);
656 static bool is_mixed_hw_group(struct evsel *counter)
658 struct evlist *evlist = counter->evlist;
659 u32 pmu_type = counter->core.attr.type;
662 if (counter->core.nr_members < 2)
665 evlist__for_each_entry(evlist, pos) {
666 /* software events can be part of any hardware group */
667 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
669 if (pmu_type == PERF_TYPE_SOFTWARE) {
670 pmu_type = pos->core.attr.type;
673 if (pmu_type != pos->core.attr.type)
680 static void printout(struct perf_stat_config *config, struct outstate *os,
681 double uval, u64 run, u64 ena, double noise, int aggr_idx)
683 struct perf_stat_output_ctx out;
687 struct evsel *counter = os->evsel;
689 if (config->csv_output) {
690 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
691 nl = config->metric_only ? new_line_metric : new_line_csv;
692 os->nfields = 4 + (counter->cgrp ? 1 : 0);
693 } else if (config->json_output) {
694 pm = config->metric_only ? print_metric_only_json : print_metric_json;
695 nl = config->metric_only ? new_line_metric : new_line_json;
697 pm = config->metric_only ? print_metric_only : print_metric_std;
698 nl = config->metric_only ? new_line_metric : new_line_std;
701 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
702 if (config->metric_only) {
703 pm(config, os, NULL, "", "", 0);
709 if (counter->supported) {
710 if (!evlist__has_hybrid(counter->evlist)) {
711 config->print_free_counters_hint = 1;
712 if (is_mixed_hw_group(counter))
713 config->print_mixed_hw_group_error = 1;
718 out.print_metric = pm;
721 out.force_header = false;
723 if (!config->metric_only) {
724 abs_printout(config, os->id, os->aggr_nr, counter, uval, ok);
726 print_noise(config, counter, noise, /*before_metric=*/true);
727 print_running(config, run, ena, /*before_metric=*/true);
731 perf_stat__print_shadow_stats(config, counter, uval, aggr_idx,
732 &out, &config->metric_events);
734 pm(config, os, /*color=*/NULL, /*format=*/NULL, /*unit=*/"", /*val=*/0);
737 if (!config->metric_only) {
738 print_noise(config, counter, noise, /*before_metric=*/false);
739 print_running(config, run, ena, /*before_metric=*/false);
743 static void uniquify_event_name(struct evsel *counter)
749 if (counter->uniquified_name || counter->use_config_name ||
750 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
751 strlen(counter->pmu_name)))
754 config = strchr(counter->name, '/');
756 if (asprintf(&new_name,
757 "%s%s", counter->pmu_name, config) > 0) {
759 counter->name = new_name;
762 if (evsel__is_hybrid(counter)) {
763 ret = asprintf(&new_name, "%s/%s/",
764 counter->pmu_name, counter->name);
766 ret = asprintf(&new_name, "%s [%s]",
767 counter->name, counter->pmu_name);
772 counter->name = new_name;
776 counter->uniquified_name = true;
779 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
781 return evsel__is_hybrid(evsel) && !config->hybrid_merge;
784 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
786 if (config->no_merge || hybrid_uniquify(counter, config))
787 uniquify_event_name(counter);
791 * should_skip_zero_count() - Check if the event should print 0 values.
792 * @config: The perf stat configuration (including aggregation mode).
793 * @counter: The evsel with its associated cpumap.
794 * @id: The aggregation id that is being queried.
796 * Due to mismatch between the event cpumap or thread-map and the
797 * aggregation mode, sometimes it'd iterate the counter with the map
798 * which does not contain any values.
800 * For example, uncore events have dedicated CPUs to manage them,
801 * result for other CPUs should be zero and skipped.
803 * Return: %true if the value should NOT be printed, %false if the value
804 * needs to be printed like "<not counted>" or "<not supported>".
806 static bool should_skip_zero_counter(struct perf_stat_config *config,
807 struct evsel *counter,
808 const struct aggr_cpu_id *id)
814 * Skip value 0 when enabling --per-thread globally,
815 * otherwise it will have too many 0 output.
817 if (config->aggr_mode == AGGR_THREAD && config->system_wide)
820 * Skip value 0 when it's an uncore event and the given aggr id
821 * does not belong to the PMU cpumask.
823 if (!counter->pmu || !counter->pmu->is_uncore)
826 perf_cpu_map__for_each_cpu(cpu, idx, counter->pmu->cpus) {
827 struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
829 if (aggr_cpu_id__equal(id, &own_id))
835 static void print_counter_aggrdata(struct perf_stat_config *config,
836 struct evsel *counter, int aggr_idx,
839 FILE *output = config->output;
842 struct perf_stat_evsel *ps = counter->stats;
843 struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
844 struct aggr_cpu_id id = config->aggr_map->map[aggr_idx];
845 double avg = aggr->counts.val;
846 bool metric_only = config->metric_only;
849 os->aggr_nr = aggr->nr;
852 /* Skip already merged uncore/hybrid events */
853 if (counter->merged_stat)
856 uniquify_counter(config, counter);
858 val = aggr->counts.val;
859 ena = aggr->counts.ena;
860 run = aggr->counts.run;
862 if (val == 0 && should_skip_zero_counter(config, counter, &id))
866 if (config->json_output)
869 fprintf(output, "%s", os->prefix);
870 else if (config->summary && config->csv_output &&
871 !config->no_csv_summary && !config->interval)
872 fprintf(output, "%s%s", "summary", config->csv_sep);
875 uval = val * counter->scale;
877 printout(config, os, uval, run, ena, avg, aggr_idx);
883 static void print_metric_begin(struct perf_stat_config *config,
884 struct evlist *evlist,
885 struct outstate *os, int aggr_idx)
887 struct perf_stat_aggr *aggr;
888 struct aggr_cpu_id id;
892 if (!config->metric_only)
895 if (config->json_output)
896 fputc('{', config->output);
898 fprintf(config->output, "%s", os->prefix);
900 evsel = evlist__first(evlist);
901 id = config->aggr_map->map[aggr_idx];
902 aggr = &evsel->stats->aggr[aggr_idx];
903 aggr_printout(config, evsel, id, aggr->nr);
905 print_cgroup(config, os->cgrp ? : evsel->cgrp);
908 static void print_metric_end(struct perf_stat_config *config, struct outstate *os)
910 FILE *output = config->output;
912 if (!config->metric_only)
915 if (config->json_output) {
917 fputs("\"metric-value\" : \"none\"", output);
923 static void print_aggr(struct perf_stat_config *config,
924 struct evlist *evlist,
927 struct evsel *counter;
930 if (!config->aggr_map || !config->aggr_get_id)
934 * With metric_only everything is on a single line.
935 * Without each counter has its own line.
937 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
938 print_metric_begin(config, evlist, os, aggr_idx);
940 evlist__for_each_entry(evlist, counter) {
941 print_counter_aggrdata(config, counter, aggr_idx, os);
943 print_metric_end(config, os);
947 static void print_aggr_cgroup(struct perf_stat_config *config,
948 struct evlist *evlist,
951 struct evsel *counter, *evsel;
954 if (!config->aggr_map || !config->aggr_get_id)
957 evlist__for_each_entry(evlist, evsel) {
958 if (os->cgrp == evsel->cgrp)
961 os->cgrp = evsel->cgrp;
963 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
964 print_metric_begin(config, evlist, os, aggr_idx);
966 evlist__for_each_entry(evlist, counter) {
967 if (counter->cgrp != os->cgrp)
970 print_counter_aggrdata(config, counter, aggr_idx, os);
972 print_metric_end(config, os);
977 static void print_counter(struct perf_stat_config *config,
978 struct evsel *counter, struct outstate *os)
982 /* AGGR_THREAD doesn't have config->aggr_get_id */
983 if (!config->aggr_map)
986 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
987 print_counter_aggrdata(config, counter, aggr_idx, os);
991 static void print_no_aggr_metric(struct perf_stat_config *config,
992 struct evlist *evlist,
998 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
999 struct evsel *counter;
1002 evlist__for_each_entry(evlist, counter) {
1005 struct perf_stat_evsel *ps = counter->stats;
1006 int aggr_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1011 os->evsel = counter;
1012 os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1014 print_metric_begin(config, evlist, os, aggr_idx);
1017 val = ps->aggr[aggr_idx].counts.val;
1018 ena = ps->aggr[aggr_idx].counts.ena;
1019 run = ps->aggr[aggr_idx].counts.run;
1021 uval = val * counter->scale;
1022 printout(config, os, uval, run, ena, 1.0, aggr_idx);
1025 print_metric_end(config, os);
1029 static void print_metric_headers_std(struct perf_stat_config *config,
1032 fputc(' ', config->output);
1035 int len = aggr_header_lens[config->aggr_mode];
1037 if (nr_cgroups || config->cgroup_list)
1038 len += CGROUP_LEN + 1;
1040 fprintf(config->output, "%*s", len, "");
1044 static void print_metric_headers_csv(struct perf_stat_config *config,
1045 bool no_indent __maybe_unused)
1047 if (config->interval)
1048 fputs("time,", config->output);
1049 if (!config->iostat_run)
1050 fputs(aggr_header_csv[config->aggr_mode], config->output);
1053 static void print_metric_headers_json(struct perf_stat_config *config __maybe_unused,
1054 bool no_indent __maybe_unused)
1058 static void print_metric_headers(struct perf_stat_config *config,
1059 struct evlist *evlist, bool no_indent)
1061 struct evsel *counter;
1062 struct outstate os = {
1063 .fh = config->output
1065 struct perf_stat_output_ctx out = {
1067 .print_metric = print_metric_header,
1068 .new_line = new_line_metric,
1069 .force_header = true,
1072 if (config->json_output)
1073 print_metric_headers_json(config, no_indent);
1074 else if (config->csv_output)
1075 print_metric_headers_csv(config, no_indent);
1077 print_metric_headers_std(config, no_indent);
1079 if (config->iostat_run)
1080 iostat_print_header_prefix(config);
1082 if (config->cgroup_list)
1083 os.cgrp = evlist__first(evlist)->cgrp;
1085 /* Print metrics headers only */
1086 evlist__for_each_entry(evlist, counter) {
1089 perf_stat__print_shadow_stats(config, counter, 0,
1092 &config->metric_events);
1095 if (!config->json_output)
1096 fputc('\n', config->output);
1099 static void prepare_interval(struct perf_stat_config *config,
1100 char *prefix, size_t len, struct timespec *ts)
1102 if (config->iostat_run)
1105 if (config->json_output)
1106 scnprintf(prefix, len, "\"interval\" : %lu.%09lu, ",
1107 (unsigned long) ts->tv_sec, ts->tv_nsec);
1108 else if (config->csv_output)
1109 scnprintf(prefix, len, "%lu.%09lu%s",
1110 (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1112 scnprintf(prefix, len, "%6lu.%09lu ",
1113 (unsigned long) ts->tv_sec, ts->tv_nsec);
1116 static void print_header_interval_std(struct perf_stat_config *config,
1117 struct target *_target __maybe_unused,
1118 struct evlist *evlist,
1119 int argc __maybe_unused,
1120 const char **argv __maybe_unused)
1122 FILE *output = config->output;
1124 switch (config->aggr_mode) {
1129 fprintf(output, "#%*s %-*s cpus",
1130 INTERVAL_LEN - 1, "time",
1131 aggr_header_lens[config->aggr_mode],
1132 aggr_header_std[config->aggr_mode]);
1135 fprintf(output, "#%*s %-*s",
1136 INTERVAL_LEN - 1, "time",
1137 aggr_header_lens[config->aggr_mode],
1138 aggr_header_std[config->aggr_mode]);
1141 fprintf(output, "#%*s %*s-%-*s",
1142 INTERVAL_LEN - 1, "time",
1143 COMM_LEN, "comm", PID_LEN, "pid");
1147 if (!config->iostat_run)
1148 fprintf(output, "#%*s",
1149 INTERVAL_LEN - 1, "time");
1155 if (config->metric_only)
1156 print_metric_headers(config, evlist, true);
1158 fprintf(output, " %*s %*s events\n",
1159 COUNTS_LEN, "counts", config->unit_width, "unit");
1162 static void print_header_std(struct perf_stat_config *config,
1163 struct target *_target, struct evlist *evlist,
1164 int argc, const char **argv)
1166 FILE *output = config->output;
1169 fprintf(output, "\n");
1170 fprintf(output, " Performance counter stats for ");
1171 if (_target->bpf_str)
1172 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1173 else if (_target->system_wide)
1174 fprintf(output, "\'system wide");
1175 else if (_target->cpu_list)
1176 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1177 else if (!target__has_task(_target)) {
1178 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1179 for (i = 1; argv && (i < argc); i++)
1180 fprintf(output, " %s", argv[i]);
1181 } else if (_target->pid)
1182 fprintf(output, "process id \'%s", _target->pid);
1184 fprintf(output, "thread id \'%s", _target->tid);
1186 fprintf(output, "\'");
1187 if (config->run_count > 1)
1188 fprintf(output, " (%d runs)", config->run_count);
1189 fprintf(output, ":\n\n");
1191 if (config->metric_only)
1192 print_metric_headers(config, evlist, false);
1195 static void print_header_csv(struct perf_stat_config *config,
1196 struct target *_target __maybe_unused,
1197 struct evlist *evlist,
1198 int argc __maybe_unused,
1199 const char **argv __maybe_unused)
1201 if (config->metric_only)
1202 print_metric_headers(config, evlist, true);
1204 static void print_header_json(struct perf_stat_config *config,
1205 struct target *_target __maybe_unused,
1206 struct evlist *evlist,
1207 int argc __maybe_unused,
1208 const char **argv __maybe_unused)
1210 if (config->metric_only)
1211 print_metric_headers(config, evlist, true);
1214 static void print_header(struct perf_stat_config *config,
1215 struct target *_target,
1216 struct evlist *evlist,
1217 int argc, const char **argv)
1219 static int num_print_iv;
1223 if (config->interval_clear)
1224 puts(CONSOLE_CLEAR);
1226 if (num_print_iv == 0 || config->interval_clear) {
1227 if (config->json_output)
1228 print_header_json(config, _target, evlist, argc, argv);
1229 else if (config->csv_output)
1230 print_header_csv(config, _target, evlist, argc, argv);
1231 else if (config->interval)
1232 print_header_interval_std(config, _target, evlist, argc, argv);
1234 print_header_std(config, _target, evlist, argc, argv);
1237 if (num_print_iv++ == 25)
1241 static int get_precision(double num)
1246 return lround(ceil(-log10(num)));
1249 static void print_table(struct perf_stat_config *config,
1250 FILE *output, int precision, double avg)
1253 int idx, indent = 0;
1255 scnprintf(tmp, 64, " %17.*f", precision, avg);
1256 while (tmp[indent] == ' ')
1259 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1261 for (idx = 0; idx < config->run_count; idx++) {
1262 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1263 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1265 fprintf(output, " %17.*f (%+.*f) ",
1266 precision, run, precision, run - avg);
1268 for (h = 0; h < n; h++)
1269 fprintf(output, "#");
1271 fprintf(output, "\n");
1274 fprintf(output, "\n%*s# Final result:\n", indent, "");
1277 static double timeval2double(struct timeval *t)
1279 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1282 static void print_footer(struct perf_stat_config *config)
1284 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1285 FILE *output = config->output;
1287 if (config->interval || config->csv_output || config->json_output)
1290 if (!config->null_run)
1291 fprintf(output, "\n");
1293 if (config->run_count == 1) {
1294 fprintf(output, " %17.9f seconds time elapsed", avg);
1296 if (config->ru_display) {
1297 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1298 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1300 fprintf(output, "\n\n");
1301 fprintf(output, " %17.9f seconds user\n", ru_utime);
1302 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1305 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1307 * Display at most 2 more significant
1308 * digits than the stddev inaccuracy.
1310 int precision = get_precision(sd) + 2;
1312 if (config->walltime_run_table)
1313 print_table(config, output, precision, avg);
1315 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1316 precision, avg, precision, sd);
1318 print_noise_pct(config, sd, avg, /*before_metric=*/false);
1320 fprintf(output, "\n\n");
1322 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1324 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1325 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1327 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1329 if (config->print_mixed_hw_group_error)
1331 "The events in group usually have to be from "
1332 "the same PMU. Try reorganizing the group.\n");
1335 static void print_percore(struct perf_stat_config *config,
1336 struct evsel *counter, struct outstate *os)
1338 bool metric_only = config->metric_only;
1339 FILE *output = config->output;
1340 struct cpu_aggr_map *core_map;
1341 int aggr_idx, core_map_len = 0;
1343 if (!config->aggr_map || !config->aggr_get_id)
1346 if (config->percore_show_thread)
1347 return print_counter(config, counter, os);
1350 * core_map will hold the aggr_cpu_id for the cores that have been
1351 * printed so that each core is printed just once.
1353 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1354 if (core_map == NULL) {
1355 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1359 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1360 struct perf_cpu curr_cpu = config->aggr_map->map[aggr_idx].cpu;
1361 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1364 for (int i = 0; i < core_map_len; i++) {
1365 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1373 print_counter_aggrdata(config, counter, aggr_idx, os);
1375 core_map->map[core_map_len++] = core_id;
1380 fputc('\n', output);
1383 static void print_cgroup_counter(struct perf_stat_config *config, struct evlist *evlist,
1384 struct outstate *os)
1386 struct evsel *counter;
1388 evlist__for_each_entry(evlist, counter) {
1389 if (os->cgrp != counter->cgrp) {
1390 if (os->cgrp != NULL)
1391 print_metric_end(config, os);
1393 os->cgrp = counter->cgrp;
1394 print_metric_begin(config, evlist, os, /*aggr_idx=*/0);
1397 print_counter(config, counter, os);
1400 print_metric_end(config, os);
1403 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1404 struct target *_target, struct timespec *ts,
1405 int argc, const char **argv)
1407 bool metric_only = config->metric_only;
1408 int interval = config->interval;
1409 struct evsel *counter;
1411 struct outstate os = {
1412 .fh = config->output,
1416 if (config->iostat_run)
1417 evlist->selected = evlist__first(evlist);
1421 prepare_interval(config, buf, sizeof(buf), ts);
1424 print_header(config, _target, evlist, argc, argv);
1426 switch (config->aggr_mode) {
1431 if (config->cgroup_list)
1432 print_aggr_cgroup(config, evlist, &os);
1434 print_aggr(config, evlist, &os);
1438 if (config->iostat_run) {
1439 iostat_print_counters(evlist, config, ts, buf,
1440 (iostat_print_counter_t)print_counter, &os);
1441 } else if (config->cgroup_list) {
1442 print_cgroup_counter(config, evlist, &os);
1444 print_metric_begin(config, evlist, &os, /*aggr_idx=*/0);
1445 evlist__for_each_entry(evlist, counter) {
1446 print_counter(config, counter, &os);
1448 print_metric_end(config, &os);
1453 print_no_aggr_metric(config, evlist, &os);
1455 evlist__for_each_entry(evlist, counter) {
1456 if (counter->percore)
1457 print_percore(config, counter, &os);
1459 print_counter(config, counter, &os);
1469 print_footer(config);
1471 fflush(config->output);