4 #include <linux/string.h>
5 #include <linux/time64.h>
13 #include "thread_map.h"
16 #include <linux/ctype.h>
18 #include <api/fs/fs.h>
21 #include "pmu-hybrid.h"
22 #include "evlist-hybrid.h"
24 #define CNTR_NOT_SUPPORTED "<not supported>"
25 #define CNTR_NOT_COUNTED "<not counted>"
27 static void print_running(struct perf_stat_config *config,
30 if (config->csv_output) {
31 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
35 ena ? 100.0 * run / ena : 100.0);
36 } else if (run != ena) {
37 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
41 static void print_noise_pct(struct perf_stat_config *config,
42 double total, double avg)
44 double pct = rel_stddev_stats(total, avg);
46 if (config->csv_output)
47 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
49 fprintf(config->output, " ( +-%6.2f%% )", pct);
52 static void print_noise(struct perf_stat_config *config,
53 struct evsel *evsel, double avg)
55 struct perf_stat_evsel *ps;
57 if (config->run_count == 1)
61 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
64 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
67 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
68 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
73 static void aggr_printout(struct perf_stat_config *config,
74 struct evsel *evsel, struct aggr_cpu_id id, int nr)
76 switch (config->aggr_mode) {
78 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
81 config->csv_output ? 0 : -8,
84 config->csv_output ? 0 : 4,
89 fprintf(config->output, "S%d-D%*d%s%*d%s",
91 config->csv_output ? 0 : -8,
94 config->csv_output ? 0 : 4,
99 fprintf(config->output, "S%*d%s%*d%s",
100 config->csv_output ? 0 : -5,
103 config->csv_output ? 0 : 4,
108 fprintf(config->output, "N%*d%s%*d%s",
109 config->csv_output ? 0 : -5,
112 config->csv_output ? 0 : 4,
117 if (evsel->percore && !config->percore_show_thread) {
118 fprintf(config->output, "S%d-D%d-C%*d%s",
121 config->csv_output ? 0 : -3,
122 id.core, config->csv_sep);
123 } else if (id.core > -1) {
124 fprintf(config->output, "CPU%*d%s",
125 config->csv_output ? 0 : -7,
126 evsel__cpus(evsel)->map[id.core],
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", os->prefix);
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(struct perf_stat_config *config,
331 struct evsel *evsel, struct aggr_cpu_id id)
333 struct evlist *evlist = evsel->evlist;
336 if (config->aggr_mode == AGGR_NONE)
339 if (!config->aggr_get_id)
342 for (i = 0; i < evsel__nr_cpus(evsel); i++) {
343 int cpu2 = evsel__cpus(evsel)->map[i];
345 if (cpu_map__compare_aggr_cpu_id(
346 config->aggr_get_id(config, evlist->core.cpus, cpu2),
354 static void abs_printout(struct perf_stat_config *config,
355 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
357 FILE *output = config->output;
358 double sc = evsel->scale;
361 if (config->csv_output) {
362 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
365 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
367 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
370 aggr_printout(config, evsel, id, nr);
372 fprintf(output, fmt, avg, config->csv_sep);
375 fprintf(output, "%-*s%s",
376 config->csv_output ? 0 : config->unit_width,
377 evsel->unit, config->csv_sep);
379 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
381 print_cgroup(config, evsel);
384 static bool is_mixed_hw_group(struct evsel *counter)
386 struct evlist *evlist = counter->evlist;
387 u32 pmu_type = counter->core.attr.type;
390 if (counter->core.nr_members < 2)
393 evlist__for_each_entry(evlist, pos) {
394 /* software events can be part of any hardware group */
395 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
397 if (pmu_type == PERF_TYPE_SOFTWARE) {
398 pmu_type = pos->core.attr.type;
401 if (pmu_type != pos->core.attr.type)
408 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
409 struct evsel *counter, double uval,
410 char *prefix, u64 run, u64 ena, double noise,
411 struct runtime_stat *st)
413 struct perf_stat_output_ctx out;
414 struct outstate os = {
415 .fh = config->output,
416 .prefix = prefix ? prefix : "",
421 print_metric_t pm = print_metric_std;
424 if (config->metric_only) {
425 nl = new_line_metric;
426 if (config->csv_output)
427 pm = print_metric_only_csv;
429 pm = print_metric_only;
433 if (config->csv_output && !config->metric_only) {
434 static int aggr_fields[] = {
443 pm = print_metric_csv;
446 os.nfields += aggr_fields[config->aggr_mode];
451 if (!config->no_csv_summary && config->csv_output &&
452 config->summary && !config->interval) {
453 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
456 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
457 if (config->metric_only) {
458 pm(config, &os, NULL, "", "", 0);
461 aggr_printout(config, counter, id, nr);
463 fprintf(config->output, "%*s%s",
464 config->csv_output ? 0 : 18,
465 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
468 if (counter->supported) {
469 if (!evlist__has_hybrid(counter->evlist)) {
470 config->print_free_counters_hint = 1;
471 if (is_mixed_hw_group(counter))
472 config->print_mixed_hw_group_error = 1;
476 fprintf(config->output, "%-*s%s",
477 config->csv_output ? 0 : config->unit_width,
478 counter->unit, config->csv_sep);
480 fprintf(config->output, "%*s",
481 config->csv_output ? 0 : -25, evsel__name(counter));
483 print_cgroup(config, counter);
485 if (!config->csv_output)
486 pm(config, &os, NULL, NULL, "", 0);
487 print_noise(config, counter, noise);
488 print_running(config, run, ena);
489 if (config->csv_output)
490 pm(config, &os, NULL, NULL, "", 0);
494 if (!config->metric_only)
495 abs_printout(config, id, nr, counter, uval);
497 out.print_metric = pm;
500 out.force_header = false;
502 if (config->csv_output && !config->metric_only) {
503 print_noise(config, counter, noise);
504 print_running(config, run, ena);
507 perf_stat__print_shadow_stats(config, counter, uval,
508 first_shadow_cpu(config, counter, id),
509 &out, &config->metric_events, st);
510 if (!config->csv_output && !config->metric_only) {
511 print_noise(config, counter, noise);
512 print_running(config, run, ena);
516 static void aggr_update_shadow(struct perf_stat_config *config,
517 struct evlist *evlist)
520 struct aggr_cpu_id s2, id;
522 struct evsel *counter;
524 for (s = 0; s < config->aggr_map->nr; s++) {
525 id = config->aggr_map->map[s];
526 evlist__for_each_entry(evlist, counter) {
528 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
529 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
530 if (!cpu_map__compare_aggr_cpu_id(s2, id))
532 val += perf_counts(counter->counts, cpu, 0)->val;
534 perf_stat__update_shadow_stats(counter, val,
535 first_shadow_cpu(config, counter, id),
541 static void uniquify_event_name(struct evsel *counter)
547 if (counter->uniquified_name || counter->use_config_name ||
548 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
549 strlen(counter->pmu_name)))
552 config = strchr(counter->name, '/');
554 if (asprintf(&new_name,
555 "%s%s", counter->pmu_name, config) > 0) {
557 counter->name = new_name;
560 if (perf_pmu__has_hybrid()) {
561 ret = asprintf(&new_name, "%s/%s/",
562 counter->pmu_name, counter->name);
564 ret = asprintf(&new_name, "%s [%s]",
565 counter->name, counter->pmu_name);
570 counter->name = new_name;
574 counter->uniquified_name = true;
577 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
578 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
582 struct evlist *evlist = counter->evlist;
585 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
586 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
587 /* Merge events with the same name, etc. but on different PMUs. */
588 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
589 alias->scale == counter->scale &&
590 alias->cgrp == counter->cgrp &&
591 !strcmp(alias->unit, counter->unit) &&
592 evsel__is_clock(alias) == evsel__is_clock(counter) &&
593 strcmp(alias->pmu_name, counter->pmu_name)) {
594 alias->merged_stat = true;
595 cb(config, alias, data, false);
600 static bool is_uncore(struct evsel *evsel)
602 struct perf_pmu *pmu = evsel__find_pmu(evsel);
604 return pmu && pmu->is_uncore;
607 static bool hybrid_uniquify(struct evsel *evsel)
609 return perf_pmu__has_hybrid() && !is_uncore(evsel);
612 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
613 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
617 if (counter->merged_stat)
619 cb(config, counter, data, true);
620 if (config->no_merge || hybrid_uniquify(counter))
621 uniquify_event_name(counter);
622 else if (counter->auto_merge_stats)
623 collect_all_aliases(config, counter, cb, data);
629 struct aggr_cpu_id id;
634 static void aggr_cb(struct perf_stat_config *config,
635 struct evsel *counter, void *data, bool first)
637 struct aggr_data *ad = data;
639 struct aggr_cpu_id s2;
641 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
642 struct perf_counts_values *counts;
644 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
645 if (!cpu_map__compare_aggr_cpu_id(s2, ad->id))
649 counts = perf_counts(counter->counts, cpu, 0);
651 * When any result is bad, make them all to give
652 * consistent output in interval mode.
654 if (counts->ena == 0 || counts->run == 0 ||
655 counter->counts->scaled == -1) {
660 ad->val += counts->val;
661 ad->ena += counts->ena;
662 ad->run += counts->run;
666 static void print_counter_aggrdata(struct perf_stat_config *config,
667 struct evsel *counter, int s,
668 char *prefix, bool metric_only,
669 bool *first, int cpu)
672 FILE *output = config->output;
675 struct aggr_cpu_id id;
678 ad.id = id = config->aggr_map->map[s];
679 ad.val = ad.ena = ad.run = 0;
681 if (!collect_data(config, counter, aggr_cb, &ad))
684 if (perf_pmu__has_hybrid() && ad.ena == 0)
691 if (*first && metric_only) {
693 aggr_printout(config, counter, id, nr);
695 if (prefix && !metric_only)
696 fprintf(output, "%s", prefix);
698 uval = val * counter->scale;
700 id = cpu_map__empty_aggr_cpu_id();
703 printout(config, id, nr, counter, uval,
704 prefix, run, ena, 1.0, &rt_stat);
709 static void print_aggr(struct perf_stat_config *config,
710 struct evlist *evlist,
713 bool metric_only = config->metric_only;
714 FILE *output = config->output;
715 struct evsel *counter;
719 if (!config->aggr_map || !config->aggr_get_id)
722 aggr_update_shadow(config, evlist);
725 * With metric_only everything is on a single line.
726 * Without each counter has its own line.
728 for (s = 0; s < config->aggr_map->nr; s++) {
729 if (prefix && metric_only)
730 fprintf(output, "%s", prefix);
733 evlist__for_each_entry(evlist, counter) {
734 print_counter_aggrdata(config, counter, s,
743 static int cmp_val(const void *a, const void *b)
745 return ((struct perf_aggr_thread_value *)b)->val -
746 ((struct perf_aggr_thread_value *)a)->val;
749 static struct perf_aggr_thread_value *sort_aggr_thread(
750 struct evsel *counter,
751 int nthreads, int ncpus,
753 struct target *_target)
755 int cpu, thread, i = 0;
757 struct perf_aggr_thread_value *buf;
759 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
763 for (thread = 0; thread < nthreads; thread++) {
764 u64 ena = 0, run = 0, val = 0;
766 for (cpu = 0; cpu < ncpus; cpu++) {
767 val += perf_counts(counter->counts, cpu, thread)->val;
768 ena += perf_counts(counter->counts, cpu, thread)->ena;
769 run += perf_counts(counter->counts, cpu, thread)->run;
772 uval = val * counter->scale;
775 * Skip value 0 when enabling --per-thread globally,
776 * otherwise too many 0 output.
778 if (uval == 0.0 && target__has_per_thread(_target))
781 buf[i].counter = counter;
782 buf[i].id = cpu_map__empty_aggr_cpu_id();
783 buf[i].id.thread = thread;
791 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
799 static void print_aggr_thread(struct perf_stat_config *config,
800 struct target *_target,
801 struct evsel *counter, char *prefix)
803 FILE *output = config->output;
804 int nthreads = perf_thread_map__nr(counter->core.threads);
805 int ncpus = perf_cpu_map__nr(counter->core.cpus);
806 int thread, sorted_threads;
807 struct aggr_cpu_id id;
808 struct perf_aggr_thread_value *buf;
810 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
812 perror("cannot sort aggr thread");
816 for (thread = 0; thread < sorted_threads; thread++) {
818 fprintf(output, "%s", prefix);
822 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
823 prefix, buf[thread].run, buf[thread].ena, 1.0,
824 &config->stats[id.thread]);
826 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
827 prefix, buf[thread].run, buf[thread].ena, 1.0,
836 double avg, avg_enabled, avg_running;
839 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
840 struct evsel *counter, void *data,
841 bool first __maybe_unused)
843 struct caggr_data *cd = data;
844 struct perf_counts_values *aggr = &counter->counts->aggr;
846 cd->avg += aggr->val;
847 cd->avg_enabled += aggr->ena;
848 cd->avg_running += aggr->run;
852 * Print out the results of a single counter:
853 * aggregated counts in system-wide mode
855 static void print_counter_aggr(struct perf_stat_config *config,
856 struct evsel *counter, char *prefix)
858 bool metric_only = config->metric_only;
859 FILE *output = config->output;
861 struct caggr_data cd = { .avg = 0.0 };
863 if (!collect_data(config, counter, counter_aggr_cb, &cd))
866 if (prefix && !metric_only)
867 fprintf(output, "%s", prefix);
869 uval = cd.avg * counter->scale;
870 printout(config, cpu_map__empty_aggr_cpu_id(), 0, counter, uval, prefix, cd.avg_running,
871 cd.avg_enabled, cd.avg, &rt_stat);
873 fprintf(output, "\n");
876 static void counter_cb(struct perf_stat_config *config __maybe_unused,
877 struct evsel *counter, void *data,
878 bool first __maybe_unused)
880 struct aggr_data *ad = data;
882 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
883 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
884 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
888 * Print out the results of a single counter:
889 * does not use aggregated count in system-wide
891 static void print_counter(struct perf_stat_config *config,
892 struct evsel *counter, char *prefix)
894 FILE *output = config->output;
898 struct aggr_cpu_id id;
900 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
901 struct aggr_data ad = { .cpu = cpu };
903 if (!collect_data(config, counter, counter_cb, &ad))
910 fprintf(output, "%s", prefix);
912 uval = val * counter->scale;
913 id = cpu_map__empty_aggr_cpu_id();
915 printout(config, id, 0, counter, uval, prefix,
916 run, ena, 1.0, &rt_stat);
922 static void print_no_aggr_metric(struct perf_stat_config *config,
923 struct evlist *evlist,
928 struct evsel *counter;
931 struct aggr_cpu_id id;
933 nrcpus = evlist->core.cpus->nr;
934 for (cpu = 0; cpu < nrcpus; cpu++) {
938 fputs(prefix, config->output);
939 evlist__for_each_entry(evlist, counter) {
940 id = cpu_map__empty_aggr_cpu_id();
943 aggr_printout(config, counter, id, 0);
946 val = perf_counts(counter->counts, cpu, 0)->val;
947 ena = perf_counts(counter->counts, cpu, 0)->ena;
948 run = perf_counts(counter->counts, cpu, 0)->run;
950 uval = val * counter->scale;
951 printout(config, id, 0, counter, uval, prefix,
952 run, ena, 1.0, &rt_stat);
954 fputc('\n', config->output);
958 static int aggr_header_lens[] = {
967 static const char *aggr_header_csv[] = {
968 [AGGR_CORE] = "core,cpus,",
969 [AGGR_DIE] = "die,cpus",
970 [AGGR_SOCKET] = "socket,cpus",
971 [AGGR_NONE] = "cpu,",
972 [AGGR_THREAD] = "comm-pid,",
976 static void print_metric_headers(struct perf_stat_config *config,
977 struct evlist *evlist,
978 const char *prefix, bool no_indent)
980 struct perf_stat_output_ctx out;
981 struct evsel *counter;
982 struct outstate os = {
987 fprintf(config->output, "%s", prefix);
989 if (!config->csv_output && !no_indent)
990 fprintf(config->output, "%*s",
991 aggr_header_lens[config->aggr_mode], "");
992 if (config->csv_output) {
993 if (config->interval)
994 fputs("time,", config->output);
995 if (!config->iostat_run)
996 fputs(aggr_header_csv[config->aggr_mode], config->output);
998 if (config->iostat_run)
999 iostat_print_header_prefix(config);
1001 /* Print metrics headers only */
1002 evlist__for_each_entry(evlist, counter) {
1005 out.print_metric = print_metric_header;
1006 out.new_line = new_line_metric;
1007 out.force_header = true;
1008 perf_stat__print_shadow_stats(config, counter, 0,
1011 &config->metric_events,
1014 fputc('\n', config->output);
1017 static void print_interval(struct perf_stat_config *config,
1018 struct evlist *evlist,
1019 char *prefix, struct timespec *ts)
1021 bool metric_only = config->metric_only;
1022 unsigned int unit_width = config->unit_width;
1023 FILE *output = config->output;
1024 static int num_print_interval;
1026 if (config->interval_clear)
1027 puts(CONSOLE_CLEAR);
1029 if (!config->iostat_run)
1030 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1032 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1033 switch (config->aggr_mode) {
1035 fprintf(output, "# time node cpus");
1037 fprintf(output, " counts %*s events\n", unit_width, "unit");
1040 fprintf(output, "# time socket cpus");
1042 fprintf(output, " counts %*s events\n", unit_width, "unit");
1045 fprintf(output, "# time die cpus");
1047 fprintf(output, " counts %*s events\n", unit_width, "unit");
1050 fprintf(output, "# time core cpus");
1052 fprintf(output, " counts %*s events\n", unit_width, "unit");
1055 fprintf(output, "# time CPU ");
1057 fprintf(output, " counts %*s events\n", unit_width, "unit");
1060 fprintf(output, "# time comm-pid");
1062 fprintf(output, " counts %*s events\n", unit_width, "unit");
1066 if (!config->iostat_run) {
1067 fprintf(output, "# time");
1069 fprintf(output, " counts %*s events\n", unit_width, "unit");
1076 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1077 print_metric_headers(config, evlist, " ", true);
1078 if (++num_print_interval == 25)
1079 num_print_interval = 0;
1082 static void print_header(struct perf_stat_config *config,
1083 struct target *_target,
1084 int argc, const char **argv)
1086 FILE *output = config->output;
1091 if (!config->csv_output) {
1092 fprintf(output, "\n");
1093 fprintf(output, " Performance counter stats for ");
1094 if (_target->bpf_str)
1095 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1096 else if (_target->system_wide)
1097 fprintf(output, "\'system wide");
1098 else if (_target->cpu_list)
1099 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1100 else if (!target__has_task(_target)) {
1101 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1102 for (i = 1; argv && (i < argc); i++)
1103 fprintf(output, " %s", argv[i]);
1104 } else if (_target->pid)
1105 fprintf(output, "process id \'%s", _target->pid);
1107 fprintf(output, "thread id \'%s", _target->tid);
1109 fprintf(output, "\'");
1110 if (config->run_count > 1)
1111 fprintf(output, " (%d runs)", config->run_count);
1112 fprintf(output, ":\n\n");
1116 static int get_precision(double num)
1121 return lround(ceil(-log10(num)));
1124 static void print_table(struct perf_stat_config *config,
1125 FILE *output, int precision, double avg)
1128 int idx, indent = 0;
1130 scnprintf(tmp, 64, " %17.*f", precision, avg);
1131 while (tmp[indent] == ' ')
1134 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1136 for (idx = 0; idx < config->run_count; idx++) {
1137 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1138 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1140 fprintf(output, " %17.*f (%+.*f) ",
1141 precision, run, precision, run - avg);
1143 for (h = 0; h < n; h++)
1144 fprintf(output, "#");
1146 fprintf(output, "\n");
1149 fprintf(output, "\n%*s# Final result:\n", indent, "");
1152 static double timeval2double(struct timeval *t)
1154 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1157 static void print_footer(struct perf_stat_config *config)
1159 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1160 FILE *output = config->output;
1162 if (!config->null_run)
1163 fprintf(output, "\n");
1165 if (config->run_count == 1) {
1166 fprintf(output, " %17.9f seconds time elapsed", avg);
1168 if (config->ru_display) {
1169 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1170 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1172 fprintf(output, "\n\n");
1173 fprintf(output, " %17.9f seconds user\n", ru_utime);
1174 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1177 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1179 * Display at most 2 more significant
1180 * digits than the stddev inaccuracy.
1182 int precision = get_precision(sd) + 2;
1184 if (config->walltime_run_table)
1185 print_table(config, output, precision, avg);
1187 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1188 precision, avg, precision, sd);
1190 print_noise_pct(config, sd, avg);
1192 fprintf(output, "\n\n");
1194 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1196 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1197 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1199 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1201 if (config->print_mixed_hw_group_error)
1203 "The events in group usually have to be from "
1204 "the same PMU. Try reorganizing the group.\n");
1207 static void print_percore_thread(struct perf_stat_config *config,
1208 struct evsel *counter, char *prefix)
1211 struct aggr_cpu_id s2, id;
1214 for (int i = 0; i < evsel__nr_cpus(counter); i++) {
1215 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1216 for (s = 0; s < config->aggr_map->nr; s++) {
1217 id = config->aggr_map->map[s];
1218 if (cpu_map__compare_aggr_cpu_id(s2, id))
1222 print_counter_aggrdata(config, counter, s,
1228 static void print_percore(struct perf_stat_config *config,
1229 struct evsel *counter, char *prefix)
1231 bool metric_only = config->metric_only;
1232 FILE *output = config->output;
1236 if (!config->aggr_map || !config->aggr_get_id)
1239 if (config->percore_show_thread)
1240 return print_percore_thread(config, counter, prefix);
1242 for (s = 0; s < config->aggr_map->nr; s++) {
1243 if (prefix && metric_only)
1244 fprintf(output, "%s", prefix);
1246 print_counter_aggrdata(config, counter, s,
1247 prefix, metric_only,
1252 fputc('\n', output);
1255 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1256 struct target *_target, struct timespec *ts, int argc, const char **argv)
1258 bool metric_only = config->metric_only;
1259 int interval = config->interval;
1260 struct evsel *counter;
1261 char buf[64], *prefix = NULL;
1263 if (config->iostat_run)
1264 evlist->selected = evlist__first(evlist);
1267 print_interval(config, evlist, prefix = buf, ts);
1269 print_header(config, _target, argc, argv);
1272 static int num_print_iv;
1274 if (num_print_iv == 0 && !interval)
1275 print_metric_headers(config, evlist, prefix, false);
1276 if (num_print_iv++ == 25)
1278 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1279 fprintf(config->output, "%s", prefix);
1282 switch (config->aggr_mode) {
1287 print_aggr(config, evlist, prefix);
1290 evlist__for_each_entry(evlist, counter) {
1291 print_aggr_thread(config, _target, counter, prefix);
1295 if (config->iostat_run)
1296 iostat_print_counters(evlist, config, ts, prefix = buf,
1297 print_counter_aggr);
1299 evlist__for_each_entry(evlist, counter) {
1300 print_counter_aggr(config, counter, prefix);
1303 fputc('\n', config->output);
1308 print_no_aggr_metric(config, evlist, prefix);
1310 evlist__for_each_entry(evlist, counter) {
1311 if (counter->percore)
1312 print_percore(config, counter, prefix);
1314 print_counter(config, counter, prefix);
1323 if (!interval && !config->csv_output)
1324 print_footer(config);
1326 fflush(config->output);