4 #include <linux/string.h>
5 #include <linux/time64.h>
7 #include <perf/cpumap.h>
14 #include "thread_map.h"
17 #include <linux/ctype.h>
19 #include <api/fs/fs.h>
22 #include "pmu-hybrid.h"
23 #include "evlist-hybrid.h"
25 #define CNTR_NOT_SUPPORTED "<not supported>"
26 #define CNTR_NOT_COUNTED "<not counted>"
28 static void print_running(struct perf_stat_config *config,
31 if (config->csv_output) {
32 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
36 ena ? 100.0 * run / ena : 100.0);
37 } else if (run != ena) {
38 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
42 static void print_noise_pct(struct perf_stat_config *config,
43 double total, double avg)
45 double pct = rel_stddev_stats(total, avg);
47 if (config->csv_output)
48 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
50 fprintf(config->output, " ( +-%6.2f%% )", pct);
53 static void print_noise(struct perf_stat_config *config,
54 struct evsel *evsel, double avg)
56 struct perf_stat_evsel *ps;
58 if (config->run_count == 1)
62 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
65 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
68 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
69 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
74 static void aggr_printout(struct perf_stat_config *config,
75 struct evsel *evsel, struct aggr_cpu_id id, int nr)
77 switch (config->aggr_mode) {
79 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
82 config->csv_output ? 0 : -8,
85 config->csv_output ? 0 : 4,
90 fprintf(config->output, "S%d-D%*d%s%*d%s",
92 config->csv_output ? 0 : -8,
95 config->csv_output ? 0 : 4,
100 fprintf(config->output, "S%*d%s%*d%s",
101 config->csv_output ? 0 : -5,
104 config->csv_output ? 0 : 4,
109 fprintf(config->output, "N%*d%s%*d%s",
110 config->csv_output ? 0 : -5,
113 config->csv_output ? 0 : 4,
118 if (evsel->percore && !config->percore_show_thread) {
119 fprintf(config->output, "S%d-D%d-C%*d%s",
122 config->csv_output ? 0 : -3,
123 id.core, config->csv_sep);
124 } else if (id.cpu.cpu > -1) {
125 fprintf(config->output, "CPU%*d%s",
126 config->csv_output ? 0 : -7,
127 id.cpu.cpu, config->csv_sep);
131 fprintf(config->output, "%*s-%*d%s",
132 config->csv_output ? 0 : 16,
133 perf_thread_map__comm(evsel->core.threads, id.thread),
134 config->csv_output ? 0 : -8,
135 perf_thread_map__pid(evsel->core.threads, id.thread),
151 struct aggr_cpu_id id;
155 #define METRIC_LEN 35
157 static void new_line_std(struct perf_stat_config *config __maybe_unused,
160 struct outstate *os = ctx;
165 static void do_new_line_std(struct perf_stat_config *config,
169 fputs(os->prefix, os->fh);
170 aggr_printout(config, os->evsel, os->id, os->nr);
171 if (config->aggr_mode == AGGR_NONE)
172 fprintf(os->fh, " ");
173 fprintf(os->fh, " ");
176 static void print_metric_std(struct perf_stat_config *config,
177 void *ctx, const char *color, const char *fmt,
178 const char *unit, double val)
180 struct outstate *os = ctx;
183 bool newline = os->newline;
187 if (unit == NULL || fmt == NULL) {
188 fprintf(out, "%-*s", METRIC_LEN, "");
193 do_new_line_std(config, os);
195 n = fprintf(out, " # ");
197 n += color_fprintf(out, color, fmt, val);
199 n += fprintf(out, fmt, val);
200 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
203 static void new_line_csv(struct perf_stat_config *config, void *ctx)
205 struct outstate *os = ctx;
210 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
211 aggr_printout(config, os->evsel, os->id, os->nr);
212 for (i = 0; i < os->nfields; i++)
213 fputs(config->csv_sep, os->fh);
216 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
218 const char *color __maybe_unused,
219 const char *fmt, const char *unit, double val)
221 struct outstate *os = ctx;
223 char buf[64], *vals, *ends;
225 if (unit == NULL || fmt == NULL) {
226 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
229 snprintf(buf, sizeof(buf), fmt, val);
230 ends = vals = skip_spaces(buf);
231 while (isdigit(*ends) || *ends == '.')
234 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
237 /* Filter out some columns that don't work well in metrics only mode */
239 static bool valid_only_metric(const char *unit)
243 if (strstr(unit, "/sec") ||
244 strstr(unit, "CPUs utilized"))
249 static const char *fixunit(char *buf, struct evsel *evsel,
252 if (!strncmp(unit, "of all", 6)) {
253 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
260 static void print_metric_only(struct perf_stat_config *config,
261 void *ctx, const char *color, const char *fmt,
262 const char *unit, double val)
264 struct outstate *os = ctx;
266 char buf[1024], str[1024];
267 unsigned mlen = config->metric_only_len;
269 if (!valid_only_metric(unit))
271 unit = fixunit(buf, os->evsel, unit);
272 if (mlen < strlen(unit))
273 mlen = strlen(unit) + 1;
276 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
278 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
279 fprintf(out, "%*s ", mlen, str);
282 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
283 void *ctx, const char *color __maybe_unused,
285 const char *unit, double val)
287 struct outstate *os = ctx;
289 char buf[64], *vals, *ends;
292 if (!valid_only_metric(unit))
294 unit = fixunit(tbuf, os->evsel, unit);
295 snprintf(buf, sizeof buf, fmt, val);
296 ends = vals = skip_spaces(buf);
297 while (isdigit(*ends) || *ends == '.')
300 fprintf(out, "%s%s", vals, config->csv_sep);
303 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
304 void *ctx __maybe_unused)
308 static void print_metric_header(struct perf_stat_config *config,
309 void *ctx, const char *color __maybe_unused,
310 const char *fmt __maybe_unused,
311 const char *unit, double val __maybe_unused)
313 struct outstate *os = ctx;
316 /* In case of iostat, print metric header for first root port only */
317 if (config->iostat_run &&
318 os->evsel->priv != os->evsel->evlist->selected->priv)
321 if (!valid_only_metric(unit))
323 unit = fixunit(tbuf, os->evsel, unit);
324 if (config->csv_output)
325 fprintf(os->fh, "%s%s", unit, config->csv_sep);
327 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
330 static int first_shadow_cpu_map_idx(struct perf_stat_config *config,
331 struct evsel *evsel, const struct aggr_cpu_id *id)
333 struct perf_cpu_map *cpus = evsel__cpus(evsel);
337 if (config->aggr_mode == AGGR_NONE)
338 return perf_cpu_map__idx(cpus, id->cpu);
340 if (!config->aggr_get_id)
343 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
344 struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
346 if (aggr_cpu_id__equal(&cpu_id, id))
352 static void abs_printout(struct perf_stat_config *config,
353 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
355 FILE *output = config->output;
356 double sc = evsel->scale;
359 if (config->csv_output) {
360 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
363 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
365 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
368 aggr_printout(config, evsel, id, nr);
370 fprintf(output, fmt, avg, config->csv_sep);
373 fprintf(output, "%-*s%s",
374 config->csv_output ? 0 : config->unit_width,
375 evsel->unit, config->csv_sep);
377 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
379 print_cgroup(config, evsel);
382 static bool is_mixed_hw_group(struct evsel *counter)
384 struct evlist *evlist = counter->evlist;
385 u32 pmu_type = counter->core.attr.type;
388 if (counter->core.nr_members < 2)
391 evlist__for_each_entry(evlist, pos) {
392 /* software events can be part of any hardware group */
393 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
395 if (pmu_type == PERF_TYPE_SOFTWARE) {
396 pmu_type = pos->core.attr.type;
399 if (pmu_type != pos->core.attr.type)
406 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
407 struct evsel *counter, double uval,
408 char *prefix, u64 run, u64 ena, double noise,
409 struct runtime_stat *st)
411 struct perf_stat_output_ctx out;
412 struct outstate os = {
413 .fh = config->output,
414 .prefix = prefix ? prefix : "",
419 print_metric_t pm = print_metric_std;
422 if (config->metric_only) {
423 nl = new_line_metric;
424 if (config->csv_output)
425 pm = print_metric_only_csv;
427 pm = print_metric_only;
431 if (config->csv_output && !config->metric_only) {
432 static int aggr_fields[] = {
441 pm = print_metric_csv;
444 os.nfields += aggr_fields[config->aggr_mode];
449 if (!config->no_csv_summary && config->csv_output &&
450 config->summary && !config->interval) {
451 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
454 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
455 if (config->metric_only) {
456 pm(config, &os, NULL, "", "", 0);
459 aggr_printout(config, counter, id, nr);
461 fprintf(config->output, "%*s%s",
462 config->csv_output ? 0 : 18,
463 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
466 if (counter->supported) {
467 if (!evlist__has_hybrid(counter->evlist)) {
468 config->print_free_counters_hint = 1;
469 if (is_mixed_hw_group(counter))
470 config->print_mixed_hw_group_error = 1;
474 fprintf(config->output, "%-*s%s",
475 config->csv_output ? 0 : config->unit_width,
476 counter->unit, config->csv_sep);
478 fprintf(config->output, "%*s",
479 config->csv_output ? 0 : -25, evsel__name(counter));
481 print_cgroup(config, counter);
483 if (!config->csv_output)
484 pm(config, &os, NULL, NULL, "", 0);
485 print_noise(config, counter, noise);
486 print_running(config, run, ena);
487 if (config->csv_output)
488 pm(config, &os, NULL, NULL, "", 0);
492 if (!config->metric_only)
493 abs_printout(config, id, nr, counter, uval);
495 out.print_metric = pm;
498 out.force_header = false;
500 if (config->csv_output && !config->metric_only) {
501 print_noise(config, counter, noise);
502 print_running(config, run, ena);
505 perf_stat__print_shadow_stats(config, counter, uval,
506 first_shadow_cpu_map_idx(config, counter, &id),
507 &out, &config->metric_events, st);
508 if (!config->csv_output && !config->metric_only) {
509 print_noise(config, counter, noise);
510 print_running(config, run, ena);
514 static void aggr_update_shadow(struct perf_stat_config *config,
515 struct evlist *evlist)
519 struct aggr_cpu_id s2, id;
521 struct evsel *counter;
522 struct perf_cpu_map *cpus;
524 for (s = 0; s < config->aggr_map->nr; s++) {
525 id = config->aggr_map->map[s];
526 evlist__for_each_entry(evlist, counter) {
527 cpus = evsel__cpus(counter);
529 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
530 s2 = config->aggr_get_id(config, cpu);
531 if (!aggr_cpu_id__equal(&s2, &id))
533 val += perf_counts(counter->counts, idx, 0)->val;
535 perf_stat__update_shadow_stats(counter, val,
536 first_shadow_cpu_map_idx(config, counter, &id),
542 static void uniquify_event_name(struct evsel *counter)
548 if (counter->uniquified_name || counter->use_config_name ||
549 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
550 strlen(counter->pmu_name)))
553 config = strchr(counter->name, '/');
555 if (asprintf(&new_name,
556 "%s%s", counter->pmu_name, config) > 0) {
558 counter->name = new_name;
561 if (perf_pmu__has_hybrid()) {
562 ret = asprintf(&new_name, "%s/%s/",
563 counter->pmu_name, counter->name);
565 ret = asprintf(&new_name, "%s [%s]",
566 counter->name, counter->pmu_name);
571 counter->name = new_name;
575 counter->uniquified_name = true;
578 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
579 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
583 struct evlist *evlist = counter->evlist;
586 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
587 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
588 /* Merge events with the same name, etc. but on different PMUs. */
589 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
590 alias->scale == counter->scale &&
591 alias->cgrp == counter->cgrp &&
592 !strcmp(alias->unit, counter->unit) &&
593 evsel__is_clock(alias) == evsel__is_clock(counter) &&
594 strcmp(alias->pmu_name, counter->pmu_name)) {
595 alias->merged_stat = true;
596 cb(config, alias, data, false);
601 static bool is_uncore(struct evsel *evsel)
603 struct perf_pmu *pmu = evsel__find_pmu(evsel);
605 return pmu && pmu->is_uncore;
608 static bool hybrid_uniquify(struct evsel *evsel)
610 return perf_pmu__has_hybrid() && !is_uncore(evsel);
613 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
616 if (hybrid_uniquify(counter)) {
618 return config && config->hybrid_merge;
620 return config && !config->hybrid_merge;
626 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
627 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
631 if (counter->merged_stat)
633 cb(config, counter, data, true);
634 if (config->no_merge || hybrid_merge(counter, config, false))
635 uniquify_event_name(counter);
636 else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
637 collect_all_aliases(config, counter, cb, data);
643 struct aggr_cpu_id id;
648 static void aggr_cb(struct perf_stat_config *config,
649 struct evsel *counter, void *data, bool first)
651 struct aggr_data *ad = data;
654 struct perf_cpu_map *cpus;
655 struct aggr_cpu_id s2;
657 cpus = evsel__cpus(counter);
658 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
659 struct perf_counts_values *counts;
661 s2 = config->aggr_get_id(config, cpu);
662 if (!aggr_cpu_id__equal(&s2, &ad->id))
666 counts = perf_counts(counter->counts, idx, 0);
668 * When any result is bad, make them all to give
669 * consistent output in interval mode.
671 if (counts->ena == 0 || counts->run == 0 ||
672 counter->counts->scaled == -1) {
677 ad->val += counts->val;
678 ad->ena += counts->ena;
679 ad->run += counts->run;
683 static void print_counter_aggrdata(struct perf_stat_config *config,
684 struct evsel *counter, int s,
685 char *prefix, bool metric_only,
686 bool *first, struct perf_cpu cpu)
689 FILE *output = config->output;
692 struct aggr_cpu_id id;
695 ad.id = id = config->aggr_map->map[s];
696 ad.val = ad.ena = ad.run = 0;
698 if (!collect_data(config, counter, aggr_cb, &ad))
701 if (perf_pmu__has_hybrid() && ad.ena == 0)
708 if (*first && metric_only) {
710 aggr_printout(config, counter, id, nr);
712 if (prefix && !metric_only)
713 fprintf(output, "%s", prefix);
715 uval = val * counter->scale;
717 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
719 printout(config, id, nr, counter, uval,
720 prefix, run, ena, 1.0, &rt_stat);
725 static void print_aggr(struct perf_stat_config *config,
726 struct evlist *evlist,
729 bool metric_only = config->metric_only;
730 FILE *output = config->output;
731 struct evsel *counter;
735 if (!config->aggr_map || !config->aggr_get_id)
738 aggr_update_shadow(config, evlist);
741 * With metric_only everything is on a single line.
742 * Without each counter has its own line.
744 for (s = 0; s < config->aggr_map->nr; s++) {
745 if (prefix && metric_only)
746 fprintf(output, "%s", prefix);
749 evlist__for_each_entry(evlist, counter) {
750 print_counter_aggrdata(config, counter, s,
752 &first, (struct perf_cpu){ .cpu = -1 });
759 static int cmp_val(const void *a, const void *b)
761 return ((struct perf_aggr_thread_value *)b)->val -
762 ((struct perf_aggr_thread_value *)a)->val;
765 static struct perf_aggr_thread_value *sort_aggr_thread(
766 struct evsel *counter,
768 struct target *_target)
770 int nthreads = perf_thread_map__nr(counter->core.threads);
773 struct perf_aggr_thread_value *buf;
775 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
779 for (int thread = 0; thread < nthreads; thread++) {
781 u64 ena = 0, run = 0, val = 0;
783 perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
784 struct perf_counts_values *counts =
785 perf_counts(counter->counts, idx, thread);
792 uval = val * counter->scale;
795 * Skip value 0 when enabling --per-thread globally,
796 * otherwise too many 0 output.
798 if (uval == 0.0 && target__has_per_thread(_target))
801 buf[i].counter = counter;
802 buf[i].id = aggr_cpu_id__empty();
803 buf[i].id.thread = thread;
811 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
819 static void print_aggr_thread(struct perf_stat_config *config,
820 struct target *_target,
821 struct evsel *counter, char *prefix)
823 FILE *output = config->output;
824 int thread, sorted_threads;
825 struct aggr_cpu_id id;
826 struct perf_aggr_thread_value *buf;
828 buf = sort_aggr_thread(counter, &sorted_threads, _target);
830 perror("cannot sort aggr thread");
834 for (thread = 0; thread < sorted_threads; thread++) {
836 fprintf(output, "%s", prefix);
840 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
841 prefix, buf[thread].run, buf[thread].ena, 1.0,
842 &config->stats[id.thread]);
844 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
845 prefix, buf[thread].run, buf[thread].ena, 1.0,
854 double avg, avg_enabled, avg_running;
857 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
858 struct evsel *counter, void *data,
859 bool first __maybe_unused)
861 struct caggr_data *cd = data;
862 struct perf_counts_values *aggr = &counter->counts->aggr;
864 cd->avg += aggr->val;
865 cd->avg_enabled += aggr->ena;
866 cd->avg_running += aggr->run;
870 * Print out the results of a single counter:
871 * aggregated counts in system-wide mode
873 static void print_counter_aggr(struct perf_stat_config *config,
874 struct evsel *counter, char *prefix)
876 bool metric_only = config->metric_only;
877 FILE *output = config->output;
879 struct caggr_data cd = { .avg = 0.0 };
881 if (!collect_data(config, counter, counter_aggr_cb, &cd))
884 if (prefix && !metric_only)
885 fprintf(output, "%s", prefix);
887 uval = cd.avg * counter->scale;
888 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
889 cd.avg_enabled, cd.avg, &rt_stat);
891 fprintf(output, "\n");
894 static void counter_cb(struct perf_stat_config *config __maybe_unused,
895 struct evsel *counter, void *data,
896 bool first __maybe_unused)
898 struct aggr_data *ad = data;
900 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
901 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
902 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
906 * Print out the results of a single counter:
907 * does not use aggregated count in system-wide
909 static void print_counter(struct perf_stat_config *config,
910 struct evsel *counter, char *prefix)
912 FILE *output = config->output;
917 struct aggr_cpu_id id;
919 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
920 struct aggr_data ad = { .cpu_map_idx = idx };
922 if (!collect_data(config, counter, counter_cb, &ad))
929 fprintf(output, "%s", prefix);
931 uval = val * counter->scale;
932 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
933 printout(config, id, 0, counter, uval, prefix,
934 run, ena, 1.0, &rt_stat);
940 static void print_no_aggr_metric(struct perf_stat_config *config,
941 struct evlist *evlist,
947 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
948 struct evsel *counter;
951 evlist__for_each_entry(evlist, counter) {
954 struct aggr_cpu_id id;
955 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
960 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
963 fputs(prefix, config->output);
964 aggr_printout(config, counter, id, 0);
967 val = perf_counts(counter->counts, counter_idx, 0)->val;
968 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
969 run = perf_counts(counter->counts, counter_idx, 0)->run;
971 uval = val * counter->scale;
972 printout(config, id, 0, counter, uval, prefix,
973 run, ena, 1.0, &rt_stat);
976 fputc('\n', config->output);
980 static int aggr_header_lens[] = {
989 static const char *aggr_header_csv[] = {
990 [AGGR_CORE] = "core,cpus,",
991 [AGGR_DIE] = "die,cpus",
992 [AGGR_SOCKET] = "socket,cpus",
993 [AGGR_NONE] = "cpu,",
994 [AGGR_THREAD] = "comm-pid,",
998 static void print_metric_headers(struct perf_stat_config *config,
999 struct evlist *evlist,
1000 const char *prefix, bool no_indent)
1002 struct perf_stat_output_ctx out;
1003 struct evsel *counter;
1004 struct outstate os = {
1005 .fh = config->output
1009 fprintf(config->output, "%s", prefix);
1011 if (!config->csv_output && !no_indent)
1012 fprintf(config->output, "%*s",
1013 aggr_header_lens[config->aggr_mode], "");
1014 if (config->csv_output) {
1015 if (config->interval)
1016 fputs("time,", config->output);
1017 if (!config->iostat_run)
1018 fputs(aggr_header_csv[config->aggr_mode], config->output);
1020 if (config->iostat_run)
1021 iostat_print_header_prefix(config);
1023 /* Print metrics headers only */
1024 evlist__for_each_entry(evlist, counter) {
1027 out.print_metric = print_metric_header;
1028 out.new_line = new_line_metric;
1029 out.force_header = true;
1030 perf_stat__print_shadow_stats(config, counter, 0,
1033 &config->metric_events,
1036 fputc('\n', config->output);
1039 static void print_interval(struct perf_stat_config *config,
1040 struct evlist *evlist,
1041 char *prefix, struct timespec *ts)
1043 bool metric_only = config->metric_only;
1044 unsigned int unit_width = config->unit_width;
1045 FILE *output = config->output;
1046 static int num_print_interval;
1048 if (config->interval_clear)
1049 puts(CONSOLE_CLEAR);
1051 if (!config->iostat_run)
1052 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1054 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1055 switch (config->aggr_mode) {
1057 fprintf(output, "# time node cpus");
1059 fprintf(output, " counts %*s events\n", unit_width, "unit");
1062 fprintf(output, "# time socket cpus");
1064 fprintf(output, " counts %*s events\n", unit_width, "unit");
1067 fprintf(output, "# time die cpus");
1069 fprintf(output, " counts %*s events\n", unit_width, "unit");
1072 fprintf(output, "# time core cpus");
1074 fprintf(output, " counts %*s events\n", unit_width, "unit");
1077 fprintf(output, "# time CPU ");
1079 fprintf(output, " counts %*s events\n", unit_width, "unit");
1082 fprintf(output, "# time comm-pid");
1084 fprintf(output, " counts %*s events\n", unit_width, "unit");
1088 if (!config->iostat_run) {
1089 fprintf(output, "# time");
1091 fprintf(output, " counts %*s events\n", unit_width, "unit");
1098 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1099 print_metric_headers(config, evlist, " ", true);
1100 if (++num_print_interval == 25)
1101 num_print_interval = 0;
1104 static void print_header(struct perf_stat_config *config,
1105 struct target *_target,
1106 int argc, const char **argv)
1108 FILE *output = config->output;
1113 if (!config->csv_output) {
1114 fprintf(output, "\n");
1115 fprintf(output, " Performance counter stats for ");
1116 if (_target->bpf_str)
1117 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1118 else if (_target->system_wide)
1119 fprintf(output, "\'system wide");
1120 else if (_target->cpu_list)
1121 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1122 else if (!target__has_task(_target)) {
1123 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1124 for (i = 1; argv && (i < argc); i++)
1125 fprintf(output, " %s", argv[i]);
1126 } else if (_target->pid)
1127 fprintf(output, "process id \'%s", _target->pid);
1129 fprintf(output, "thread id \'%s", _target->tid);
1131 fprintf(output, "\'");
1132 if (config->run_count > 1)
1133 fprintf(output, " (%d runs)", config->run_count);
1134 fprintf(output, ":\n\n");
1138 static int get_precision(double num)
1143 return lround(ceil(-log10(num)));
1146 static void print_table(struct perf_stat_config *config,
1147 FILE *output, int precision, double avg)
1150 int idx, indent = 0;
1152 scnprintf(tmp, 64, " %17.*f", precision, avg);
1153 while (tmp[indent] == ' ')
1156 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1158 for (idx = 0; idx < config->run_count; idx++) {
1159 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1160 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1162 fprintf(output, " %17.*f (%+.*f) ",
1163 precision, run, precision, run - avg);
1165 for (h = 0; h < n; h++)
1166 fprintf(output, "#");
1168 fprintf(output, "\n");
1171 fprintf(output, "\n%*s# Final result:\n", indent, "");
1174 static double timeval2double(struct timeval *t)
1176 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1179 static void print_footer(struct perf_stat_config *config)
1181 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1182 FILE *output = config->output;
1184 if (!config->null_run)
1185 fprintf(output, "\n");
1187 if (config->run_count == 1) {
1188 fprintf(output, " %17.9f seconds time elapsed", avg);
1190 if (config->ru_display) {
1191 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1192 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1194 fprintf(output, "\n\n");
1195 fprintf(output, " %17.9f seconds user\n", ru_utime);
1196 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1199 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1201 * Display at most 2 more significant
1202 * digits than the stddev inaccuracy.
1204 int precision = get_precision(sd) + 2;
1206 if (config->walltime_run_table)
1207 print_table(config, output, precision, avg);
1209 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1210 precision, avg, precision, sd);
1212 print_noise_pct(config, sd, avg);
1214 fprintf(output, "\n\n");
1216 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1218 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1219 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1221 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1223 if (config->print_mixed_hw_group_error)
1225 "The events in group usually have to be from "
1226 "the same PMU. Try reorganizing the group.\n");
1229 static void print_percore_thread(struct perf_stat_config *config,
1230 struct evsel *counter, char *prefix)
1233 struct aggr_cpu_id s2, id;
1234 struct perf_cpu_map *cpus;
1237 struct perf_cpu cpu;
1239 cpus = evsel__cpus(counter);
1240 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1241 s2 = config->aggr_get_id(config, cpu);
1242 for (s = 0; s < config->aggr_map->nr; s++) {
1243 id = config->aggr_map->map[s];
1244 if (aggr_cpu_id__equal(&s2, &id))
1248 print_counter_aggrdata(config, counter, s,
1254 static void print_percore(struct perf_stat_config *config,
1255 struct evsel *counter, char *prefix)
1257 bool metric_only = config->metric_only;
1258 FILE *output = config->output;
1262 if (!config->aggr_map || !config->aggr_get_id)
1265 if (config->percore_show_thread)
1266 return print_percore_thread(config, counter, prefix);
1268 for (s = 0; s < config->aggr_map->nr; s++) {
1269 if (prefix && metric_only)
1270 fprintf(output, "%s", prefix);
1272 print_counter_aggrdata(config, counter, s,
1273 prefix, metric_only,
1274 &first, (struct perf_cpu){ .cpu = -1 });
1278 fputc('\n', output);
1281 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1282 struct target *_target, struct timespec *ts, int argc, const char **argv)
1284 bool metric_only = config->metric_only;
1285 int interval = config->interval;
1286 struct evsel *counter;
1287 char buf[64], *prefix = NULL;
1289 if (config->iostat_run)
1290 evlist->selected = evlist__first(evlist);
1293 print_interval(config, evlist, prefix = buf, ts);
1295 print_header(config, _target, argc, argv);
1298 static int num_print_iv;
1300 if (num_print_iv == 0 && !interval)
1301 print_metric_headers(config, evlist, prefix, false);
1302 if (num_print_iv++ == 25)
1304 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1305 fprintf(config->output, "%s", prefix);
1308 switch (config->aggr_mode) {
1313 print_aggr(config, evlist, prefix);
1316 evlist__for_each_entry(evlist, counter) {
1317 print_aggr_thread(config, _target, counter, prefix);
1321 if (config->iostat_run)
1322 iostat_print_counters(evlist, config, ts, prefix = buf,
1323 print_counter_aggr);
1325 evlist__for_each_entry(evlist, counter) {
1326 print_counter_aggr(config, counter, prefix);
1329 fputc('\n', config->output);
1334 print_no_aggr_metric(config, evlist, prefix);
1336 evlist__for_each_entry(evlist, counter) {
1337 if (counter->percore)
1338 print_percore(config, counter, prefix);
1340 print_counter(config, counter, prefix);
1349 if (!interval && !config->csv_output)
1350 print_footer(config);
1352 fflush(config->output);