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 collect_data(struct perf_stat_config *config, struct evsel *counter,
614 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
618 if (counter->merged_stat)
620 cb(config, counter, data, true);
621 if (config->no_merge || hybrid_uniquify(counter))
622 uniquify_event_name(counter);
623 else if (counter->auto_merge_stats)
624 collect_all_aliases(config, counter, cb, data);
630 struct aggr_cpu_id id;
635 static void aggr_cb(struct perf_stat_config *config,
636 struct evsel *counter, void *data, bool first)
638 struct aggr_data *ad = data;
641 struct perf_cpu_map *cpus;
642 struct aggr_cpu_id s2;
644 cpus = evsel__cpus(counter);
645 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
646 struct perf_counts_values *counts;
648 s2 = config->aggr_get_id(config, cpu);
649 if (!aggr_cpu_id__equal(&s2, &ad->id))
653 counts = perf_counts(counter->counts, idx, 0);
655 * When any result is bad, make them all to give
656 * consistent output in interval mode.
658 if (counts->ena == 0 || counts->run == 0 ||
659 counter->counts->scaled == -1) {
664 ad->val += counts->val;
665 ad->ena += counts->ena;
666 ad->run += counts->run;
670 static void print_counter_aggrdata(struct perf_stat_config *config,
671 struct evsel *counter, int s,
672 char *prefix, bool metric_only,
673 bool *first, struct perf_cpu cpu)
676 FILE *output = config->output;
679 struct aggr_cpu_id id;
682 ad.id = id = config->aggr_map->map[s];
683 ad.val = ad.ena = ad.run = 0;
685 if (!collect_data(config, counter, aggr_cb, &ad))
688 if (perf_pmu__has_hybrid() && ad.ena == 0)
695 if (*first && metric_only) {
697 aggr_printout(config, counter, id, nr);
699 if (prefix && !metric_only)
700 fprintf(output, "%s", prefix);
702 uval = val * counter->scale;
704 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
706 printout(config, id, nr, counter, uval,
707 prefix, run, ena, 1.0, &rt_stat);
712 static void print_aggr(struct perf_stat_config *config,
713 struct evlist *evlist,
716 bool metric_only = config->metric_only;
717 FILE *output = config->output;
718 struct evsel *counter;
722 if (!config->aggr_map || !config->aggr_get_id)
725 aggr_update_shadow(config, evlist);
728 * With metric_only everything is on a single line.
729 * Without each counter has its own line.
731 for (s = 0; s < config->aggr_map->nr; s++) {
732 if (prefix && metric_only)
733 fprintf(output, "%s", prefix);
736 evlist__for_each_entry(evlist, counter) {
737 print_counter_aggrdata(config, counter, s,
739 &first, (struct perf_cpu){ .cpu = -1 });
746 static int cmp_val(const void *a, const void *b)
748 return ((struct perf_aggr_thread_value *)b)->val -
749 ((struct perf_aggr_thread_value *)a)->val;
752 static struct perf_aggr_thread_value *sort_aggr_thread(
753 struct evsel *counter,
754 int nthreads, int ncpus,
756 struct target *_target)
758 int cpu, thread, i = 0;
760 struct perf_aggr_thread_value *buf;
762 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
766 for (thread = 0; thread < nthreads; thread++) {
767 u64 ena = 0, run = 0, val = 0;
769 for (cpu = 0; cpu < ncpus; cpu++) {
770 val += perf_counts(counter->counts, cpu, thread)->val;
771 ena += perf_counts(counter->counts, cpu, thread)->ena;
772 run += perf_counts(counter->counts, cpu, thread)->run;
775 uval = val * counter->scale;
778 * Skip value 0 when enabling --per-thread globally,
779 * otherwise too many 0 output.
781 if (uval == 0.0 && target__has_per_thread(_target))
784 buf[i].counter = counter;
785 buf[i].id = aggr_cpu_id__empty();
786 buf[i].id.thread = thread;
794 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
802 static void print_aggr_thread(struct perf_stat_config *config,
803 struct target *_target,
804 struct evsel *counter, char *prefix)
806 FILE *output = config->output;
807 int nthreads = perf_thread_map__nr(counter->core.threads);
808 int ncpus = perf_cpu_map__nr(counter->core.cpus);
809 int thread, sorted_threads;
810 struct aggr_cpu_id id;
811 struct perf_aggr_thread_value *buf;
813 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
815 perror("cannot sort aggr thread");
819 for (thread = 0; thread < sorted_threads; thread++) {
821 fprintf(output, "%s", prefix);
825 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
826 prefix, buf[thread].run, buf[thread].ena, 1.0,
827 &config->stats[id.thread]);
829 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
830 prefix, buf[thread].run, buf[thread].ena, 1.0,
839 double avg, avg_enabled, avg_running;
842 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
843 struct evsel *counter, void *data,
844 bool first __maybe_unused)
846 struct caggr_data *cd = data;
847 struct perf_counts_values *aggr = &counter->counts->aggr;
849 cd->avg += aggr->val;
850 cd->avg_enabled += aggr->ena;
851 cd->avg_running += aggr->run;
855 * Print out the results of a single counter:
856 * aggregated counts in system-wide mode
858 static void print_counter_aggr(struct perf_stat_config *config,
859 struct evsel *counter, char *prefix)
861 bool metric_only = config->metric_only;
862 FILE *output = config->output;
864 struct caggr_data cd = { .avg = 0.0 };
866 if (!collect_data(config, counter, counter_aggr_cb, &cd))
869 if (prefix && !metric_only)
870 fprintf(output, "%s", prefix);
872 uval = cd.avg * counter->scale;
873 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
874 cd.avg_enabled, cd.avg, &rt_stat);
876 fprintf(output, "\n");
879 static void counter_cb(struct perf_stat_config *config __maybe_unused,
880 struct evsel *counter, void *data,
881 bool first __maybe_unused)
883 struct aggr_data *ad = data;
885 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
886 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
887 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
891 * Print out the results of a single counter:
892 * does not use aggregated count in system-wide
894 static void print_counter(struct perf_stat_config *config,
895 struct evsel *counter, char *prefix)
897 FILE *output = config->output;
902 struct aggr_cpu_id id;
904 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
905 struct aggr_data ad = { .cpu_map_idx = idx };
907 if (!collect_data(config, counter, counter_cb, &ad))
914 fprintf(output, "%s", prefix);
916 uval = val * counter->scale;
917 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
918 printout(config, id, 0, counter, uval, prefix,
919 run, ena, 1.0, &rt_stat);
925 static void print_no_aggr_metric(struct perf_stat_config *config,
926 struct evlist *evlist,
932 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.cpus) {
933 struct evsel *counter;
937 fputs(prefix, config->output);
938 evlist__for_each_entry(evlist, counter) {
941 struct aggr_cpu_id id;
942 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
947 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
949 aggr_printout(config, counter, id, 0);
952 val = perf_counts(counter->counts, counter_idx, 0)->val;
953 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
954 run = perf_counts(counter->counts, counter_idx, 0)->run;
956 uval = val * counter->scale;
957 printout(config, id, 0, counter, uval, prefix,
958 run, ena, 1.0, &rt_stat);
960 fputc('\n', config->output);
964 static int aggr_header_lens[] = {
973 static const char *aggr_header_csv[] = {
974 [AGGR_CORE] = "core,cpus,",
975 [AGGR_DIE] = "die,cpus",
976 [AGGR_SOCKET] = "socket,cpus",
977 [AGGR_NONE] = "cpu,",
978 [AGGR_THREAD] = "comm-pid,",
982 static void print_metric_headers(struct perf_stat_config *config,
983 struct evlist *evlist,
984 const char *prefix, bool no_indent)
986 struct perf_stat_output_ctx out;
987 struct evsel *counter;
988 struct outstate os = {
993 fprintf(config->output, "%s", prefix);
995 if (!config->csv_output && !no_indent)
996 fprintf(config->output, "%*s",
997 aggr_header_lens[config->aggr_mode], "");
998 if (config->csv_output) {
999 if (config->interval)
1000 fputs("time,", config->output);
1001 if (!config->iostat_run)
1002 fputs(aggr_header_csv[config->aggr_mode], config->output);
1004 if (config->iostat_run)
1005 iostat_print_header_prefix(config);
1007 /* Print metrics headers only */
1008 evlist__for_each_entry(evlist, counter) {
1011 out.print_metric = print_metric_header;
1012 out.new_line = new_line_metric;
1013 out.force_header = true;
1014 perf_stat__print_shadow_stats(config, counter, 0,
1017 &config->metric_events,
1020 fputc('\n', config->output);
1023 static void print_interval(struct perf_stat_config *config,
1024 struct evlist *evlist,
1025 char *prefix, struct timespec *ts)
1027 bool metric_only = config->metric_only;
1028 unsigned int unit_width = config->unit_width;
1029 FILE *output = config->output;
1030 static int num_print_interval;
1032 if (config->interval_clear)
1033 puts(CONSOLE_CLEAR);
1035 if (!config->iostat_run)
1036 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1038 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1039 switch (config->aggr_mode) {
1041 fprintf(output, "# time node cpus");
1043 fprintf(output, " counts %*s events\n", unit_width, "unit");
1046 fprintf(output, "# time socket cpus");
1048 fprintf(output, " counts %*s events\n", unit_width, "unit");
1051 fprintf(output, "# time die cpus");
1053 fprintf(output, " counts %*s events\n", unit_width, "unit");
1056 fprintf(output, "# time core cpus");
1058 fprintf(output, " counts %*s events\n", unit_width, "unit");
1061 fprintf(output, "# time CPU ");
1063 fprintf(output, " counts %*s events\n", unit_width, "unit");
1066 fprintf(output, "# time comm-pid");
1068 fprintf(output, " counts %*s events\n", unit_width, "unit");
1072 if (!config->iostat_run) {
1073 fprintf(output, "# time");
1075 fprintf(output, " counts %*s events\n", unit_width, "unit");
1082 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1083 print_metric_headers(config, evlist, " ", true);
1084 if (++num_print_interval == 25)
1085 num_print_interval = 0;
1088 static void print_header(struct perf_stat_config *config,
1089 struct target *_target,
1090 int argc, const char **argv)
1092 FILE *output = config->output;
1097 if (!config->csv_output) {
1098 fprintf(output, "\n");
1099 fprintf(output, " Performance counter stats for ");
1100 if (_target->bpf_str)
1101 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1102 else if (_target->system_wide)
1103 fprintf(output, "\'system wide");
1104 else if (_target->cpu_list)
1105 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1106 else if (!target__has_task(_target)) {
1107 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1108 for (i = 1; argv && (i < argc); i++)
1109 fprintf(output, " %s", argv[i]);
1110 } else if (_target->pid)
1111 fprintf(output, "process id \'%s", _target->pid);
1113 fprintf(output, "thread id \'%s", _target->tid);
1115 fprintf(output, "\'");
1116 if (config->run_count > 1)
1117 fprintf(output, " (%d runs)", config->run_count);
1118 fprintf(output, ":\n\n");
1122 static int get_precision(double num)
1127 return lround(ceil(-log10(num)));
1130 static void print_table(struct perf_stat_config *config,
1131 FILE *output, int precision, double avg)
1134 int idx, indent = 0;
1136 scnprintf(tmp, 64, " %17.*f", precision, avg);
1137 while (tmp[indent] == ' ')
1140 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1142 for (idx = 0; idx < config->run_count; idx++) {
1143 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1144 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1146 fprintf(output, " %17.*f (%+.*f) ",
1147 precision, run, precision, run - avg);
1149 for (h = 0; h < n; h++)
1150 fprintf(output, "#");
1152 fprintf(output, "\n");
1155 fprintf(output, "\n%*s# Final result:\n", indent, "");
1158 static double timeval2double(struct timeval *t)
1160 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1163 static void print_footer(struct perf_stat_config *config)
1165 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1166 FILE *output = config->output;
1168 if (!config->null_run)
1169 fprintf(output, "\n");
1171 if (config->run_count == 1) {
1172 fprintf(output, " %17.9f seconds time elapsed", avg);
1174 if (config->ru_display) {
1175 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1176 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1178 fprintf(output, "\n\n");
1179 fprintf(output, " %17.9f seconds user\n", ru_utime);
1180 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1183 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1185 * Display at most 2 more significant
1186 * digits than the stddev inaccuracy.
1188 int precision = get_precision(sd) + 2;
1190 if (config->walltime_run_table)
1191 print_table(config, output, precision, avg);
1193 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1194 precision, avg, precision, sd);
1196 print_noise_pct(config, sd, avg);
1198 fprintf(output, "\n\n");
1200 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1202 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1203 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1205 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1207 if (config->print_mixed_hw_group_error)
1209 "The events in group usually have to be from "
1210 "the same PMU. Try reorganizing the group.\n");
1213 static void print_percore_thread(struct perf_stat_config *config,
1214 struct evsel *counter, char *prefix)
1217 struct aggr_cpu_id s2, id;
1218 struct perf_cpu_map *cpus;
1221 struct perf_cpu cpu;
1223 cpus = evsel__cpus(counter);
1224 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1225 s2 = config->aggr_get_id(config, cpu);
1226 for (s = 0; s < config->aggr_map->nr; s++) {
1227 id = config->aggr_map->map[s];
1228 if (aggr_cpu_id__equal(&s2, &id))
1232 print_counter_aggrdata(config, counter, s,
1238 static void print_percore(struct perf_stat_config *config,
1239 struct evsel *counter, char *prefix)
1241 bool metric_only = config->metric_only;
1242 FILE *output = config->output;
1246 if (!config->aggr_map || !config->aggr_get_id)
1249 if (config->percore_show_thread)
1250 return print_percore_thread(config, counter, prefix);
1252 for (s = 0; s < config->aggr_map->nr; s++) {
1253 if (prefix && metric_only)
1254 fprintf(output, "%s", prefix);
1256 print_counter_aggrdata(config, counter, s,
1257 prefix, metric_only,
1258 &first, (struct perf_cpu){ .cpu = -1 });
1262 fputc('\n', output);
1265 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1266 struct target *_target, struct timespec *ts, int argc, const char **argv)
1268 bool metric_only = config->metric_only;
1269 int interval = config->interval;
1270 struct evsel *counter;
1271 char buf[64], *prefix = NULL;
1273 if (config->iostat_run)
1274 evlist->selected = evlist__first(evlist);
1277 print_interval(config, evlist, prefix = buf, ts);
1279 print_header(config, _target, argc, argv);
1282 static int num_print_iv;
1284 if (num_print_iv == 0 && !interval)
1285 print_metric_headers(config, evlist, prefix, false);
1286 if (num_print_iv++ == 25)
1288 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1289 fprintf(config->output, "%s", prefix);
1292 switch (config->aggr_mode) {
1297 print_aggr(config, evlist, prefix);
1300 evlist__for_each_entry(evlist, counter) {
1301 print_aggr_thread(config, _target, counter, prefix);
1305 if (config->iostat_run)
1306 iostat_print_counters(evlist, config, ts, prefix = buf,
1307 print_counter_aggr);
1309 evlist__for_each_entry(evlist, counter) {
1310 print_counter_aggr(config, counter, prefix);
1313 fputc('\n', config->output);
1318 print_no_aggr_metric(config, evlist, prefix);
1320 evlist__for_each_entry(evlist, counter) {
1321 if (counter->percore)
1322 print_percore(config, counter, prefix);
1324 print_counter(config, counter, prefix);
1333 if (!interval && !config->csv_output)
1334 print_footer(config);
1336 fflush(config->output);