Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 1 | #include <stdlib.h> |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 810826a | 2019-06-25 21:28:49 -0300 | [diff] [blame] | 4 | #include <linux/string.h> |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 5 | #include <linux/time64.h> |
| 6 | #include <math.h> |
Ian Rogers | 7ea82fb | 2022-01-04 22:13:34 -0800 | [diff] [blame] | 7 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | 8a249c7 | 2019-01-22 10:47:38 -0200 | [diff] [blame] | 8 | #include "color.h" |
Arnaldo Carvalho de Melo | bfc4918 | 2019-08-21 14:02:05 -0300 | [diff] [blame] | 9 | #include "counts.h" |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 10 | #include "evlist.h" |
| 11 | #include "evsel.h" |
| 12 | #include "stat.h" |
| 13 | #include "top.h" |
| 14 | #include "thread_map.h" |
| 15 | #include "cpumap.h" |
| 16 | #include "string2.h" |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 17 | #include <linux/ctype.h> |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 18 | #include "cgroup.h" |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 19 | #include <api/fs/fs.h> |
Kan Liang | 2a14c1b | 2020-02-24 13:59:22 -0800 | [diff] [blame] | 20 | #include "util.h" |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 21 | #include "iostat.h" |
Jin Yao | 1227942 | 2021-04-27 15:01:20 +0800 | [diff] [blame] | 22 | #include "pmu-hybrid.h" |
Jin Yao | 493be70 | 2021-06-10 11:45:57 +0800 | [diff] [blame] | 23 | #include "evlist-hybrid.h" |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 24 | |
| 25 | #define CNTR_NOT_SUPPORTED "<not supported>" |
| 26 | #define CNTR_NOT_COUNTED "<not counted>" |
| 27 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 28 | static void print_running(struct perf_stat_config *config, |
| 29 | u64 run, u64 ena) |
| 30 | { |
| 31 | if (config->csv_output) { |
| 32 | fprintf(config->output, "%s%" PRIu64 "%s%.2f", |
| 33 | config->csv_sep, |
| 34 | run, |
| 35 | config->csv_sep, |
| 36 | ena ? 100.0 * run / ena : 100.0); |
| 37 | } else if (run != ena) { |
| 38 | fprintf(config->output, " (%.2f%%)", 100.0 * run / ena); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static void print_noise_pct(struct perf_stat_config *config, |
| 43 | double total, double avg) |
| 44 | { |
| 45 | double pct = rel_stddev_stats(total, avg); |
| 46 | |
| 47 | if (config->csv_output) |
| 48 | fprintf(config->output, "%s%.2f%%", config->csv_sep, pct); |
| 49 | else if (pct) |
| 50 | fprintf(config->output, " ( +-%6.2f%% )", pct); |
| 51 | } |
| 52 | |
| 53 | static void print_noise(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 54 | struct evsel *evsel, double avg) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 55 | { |
| 56 | struct perf_stat_evsel *ps; |
| 57 | |
| 58 | if (config->run_count == 1) |
| 59 | return; |
| 60 | |
| 61 | ps = evsel->stats; |
| 62 | print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg); |
| 63 | } |
| 64 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 65 | static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel) |
Stephane Eranian | bc4da38 | 2018-11-07 02:50:45 -0800 | [diff] [blame] | 66 | { |
| 67 | if (nr_cgroups) { |
| 68 | const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : ""; |
| 69 | fprintf(config->output, "%s%s", config->csv_sep, cgrp_name); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 74 | static void aggr_printout(struct perf_stat_config *config, |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 75 | struct evsel *evsel, struct aggr_cpu_id id, int nr) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 76 | { |
| 77 | switch (config->aggr_mode) { |
| 78 | case AGGR_CORE: |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 79 | fprintf(config->output, "S%d-D%d-C%*d%s%*d%s", |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 80 | id.socket, |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 81 | id.die, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 82 | config->csv_output ? 0 : -8, |
James Clark | b993381 | 2020-11-26 16:13:27 +0200 | [diff] [blame] | 83 | id.core, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 84 | config->csv_sep, |
| 85 | config->csv_output ? 0 : 4, |
| 86 | nr, |
| 87 | config->csv_sep); |
| 88 | break; |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 89 | case AGGR_DIE: |
| 90 | fprintf(config->output, "S%d-D%*d%s%*d%s", |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 91 | id.socket, |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 92 | config->csv_output ? 0 : -8, |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 93 | id.die, |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 94 | config->csv_sep, |
| 95 | config->csv_output ? 0 : 4, |
| 96 | nr, |
| 97 | config->csv_sep); |
| 98 | break; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 99 | case AGGR_SOCKET: |
| 100 | fprintf(config->output, "S%*d%s%*d%s", |
| 101 | config->csv_output ? 0 : -5, |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 102 | id.socket, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 103 | config->csv_sep, |
| 104 | config->csv_output ? 0 : 4, |
| 105 | nr, |
| 106 | config->csv_sep); |
| 107 | break; |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 108 | case AGGR_NODE: |
| 109 | fprintf(config->output, "N%*d%s%*d%s", |
| 110 | config->csv_output ? 0 : -5, |
James Clark | fcd83a3 | 2020-11-26 16:13:24 +0200 | [diff] [blame] | 111 | id.node, |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 112 | config->csv_sep, |
| 113 | config->csv_output ? 0 : 4, |
| 114 | nr, |
| 115 | config->csv_sep); |
| 116 | break; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 117 | case AGGR_NONE: |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 118 | if (evsel->percore && !config->percore_show_thread) { |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 119 | fprintf(config->output, "S%d-D%d-C%*d%s", |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 120 | id.socket, |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 121 | id.die, |
Jin Yao | d13e9e4 | 2020-02-18 15:16:14 +0800 | [diff] [blame] | 122 | config->csv_output ? 0 : -3, |
James Clark | b993381 | 2020-11-26 16:13:27 +0200 | [diff] [blame] | 123 | id.core, config->csv_sep); |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 124 | } else if (id.cpu.cpu > -1) { |
Jin Yao | d13e9e4 | 2020-02-18 15:16:14 +0800 | [diff] [blame] | 125 | fprintf(config->output, "CPU%*d%s", |
| 126 | config->csv_output ? 0 : -7, |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 127 | id.cpu.cpu, config->csv_sep); |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 128 | } |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 129 | break; |
| 130 | case AGGR_THREAD: |
| 131 | fprintf(config->output, "%*s-%*d%s", |
| 132 | config->csv_output ? 0 : 16, |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 133 | perf_thread_map__comm(evsel->core.threads, id.thread), |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 134 | config->csv_output ? 0 : -8, |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 135 | perf_thread_map__pid(evsel->core.threads, id.thread), |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 136 | config->csv_sep); |
| 137 | break; |
| 138 | case AGGR_GLOBAL: |
| 139 | case AGGR_UNSET: |
| 140 | default: |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | struct outstate { |
| 146 | FILE *fh; |
| 147 | bool newline; |
| 148 | const char *prefix; |
| 149 | int nfields; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 150 | int nr; |
| 151 | struct aggr_cpu_id id; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 152 | struct evsel *evsel; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | #define METRIC_LEN 35 |
| 156 | |
| 157 | static void new_line_std(struct perf_stat_config *config __maybe_unused, |
| 158 | void *ctx) |
| 159 | { |
| 160 | struct outstate *os = ctx; |
| 161 | |
| 162 | os->newline = true; |
| 163 | } |
| 164 | |
| 165 | static void do_new_line_std(struct perf_stat_config *config, |
| 166 | struct outstate *os) |
| 167 | { |
| 168 | fputc('\n', os->fh); |
| 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, " "); |
| 174 | } |
| 175 | |
| 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) |
| 179 | { |
| 180 | struct outstate *os = ctx; |
| 181 | FILE *out = os->fh; |
| 182 | int n; |
| 183 | bool newline = os->newline; |
| 184 | |
| 185 | os->newline = false; |
| 186 | |
| 187 | if (unit == NULL || fmt == NULL) { |
| 188 | fprintf(out, "%-*s", METRIC_LEN, ""); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | if (newline) |
| 193 | do_new_line_std(config, os); |
| 194 | |
| 195 | n = fprintf(out, " # "); |
| 196 | if (color) |
| 197 | n += color_fprintf(out, color, fmt, val); |
| 198 | else |
| 199 | n += fprintf(out, fmt, val); |
| 200 | fprintf(out, " %-*s", METRIC_LEN - n - 1, unit); |
| 201 | } |
| 202 | |
| 203 | static void new_line_csv(struct perf_stat_config *config, void *ctx) |
| 204 | { |
| 205 | struct outstate *os = ctx; |
| 206 | int i; |
| 207 | |
| 208 | fputc('\n', os->fh); |
| 209 | if (os->prefix) |
| 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); |
| 214 | } |
| 215 | |
| 216 | static void print_metric_csv(struct perf_stat_config *config __maybe_unused, |
| 217 | void *ctx, |
| 218 | const char *color __maybe_unused, |
| 219 | const char *fmt, const char *unit, double val) |
| 220 | { |
| 221 | struct outstate *os = ctx; |
| 222 | FILE *out = os->fh; |
| 223 | char buf[64], *vals, *ends; |
| 224 | |
| 225 | if (unit == NULL || fmt == NULL) { |
| 226 | fprintf(out, "%s%s", config->csv_sep, config->csv_sep); |
| 227 | return; |
| 228 | } |
| 229 | snprintf(buf, sizeof(buf), fmt, val); |
Arnaldo Carvalho de Melo | 3285848 | 2019-06-26 11:42:03 -0300 | [diff] [blame] | 230 | ends = vals = skip_spaces(buf); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 231 | while (isdigit(*ends) || *ends == '.') |
| 232 | ends++; |
| 233 | *ends = 0; |
Arnaldo Carvalho de Melo | 810826a | 2019-06-25 21:28:49 -0300 | [diff] [blame] | 234 | fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit)); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* Filter out some columns that don't work well in metrics only mode */ |
| 238 | |
| 239 | static bool valid_only_metric(const char *unit) |
| 240 | { |
| 241 | if (!unit) |
| 242 | return false; |
| 243 | if (strstr(unit, "/sec") || |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 244 | strstr(unit, "CPUs utilized")) |
| 245 | return false; |
| 246 | return true; |
| 247 | } |
| 248 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 249 | static const char *fixunit(char *buf, struct evsel *evsel, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 250 | const char *unit) |
| 251 | { |
| 252 | if (!strncmp(unit, "of all", 6)) { |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 253 | snprintf(buf, 1024, "%s %s", evsel__name(evsel), |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 254 | unit); |
| 255 | return buf; |
| 256 | } |
| 257 | return unit; |
| 258 | } |
| 259 | |
| 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) |
| 263 | { |
| 264 | struct outstate *os = ctx; |
| 265 | FILE *out = os->fh; |
| 266 | char buf[1024], str[1024]; |
| 267 | unsigned mlen = config->metric_only_len; |
| 268 | |
| 269 | if (!valid_only_metric(unit)) |
| 270 | return; |
| 271 | unit = fixunit(buf, os->evsel, unit); |
| 272 | if (mlen < strlen(unit)) |
| 273 | mlen = strlen(unit) + 1; |
| 274 | |
| 275 | if (color) |
| 276 | mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1; |
| 277 | |
| 278 | color_snprintf(str, sizeof(str), color ?: "", fmt, val); |
| 279 | fprintf(out, "%*s ", mlen, str); |
| 280 | } |
| 281 | |
| 282 | static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused, |
| 283 | void *ctx, const char *color __maybe_unused, |
| 284 | const char *fmt, |
| 285 | const char *unit, double val) |
| 286 | { |
| 287 | struct outstate *os = ctx; |
| 288 | FILE *out = os->fh; |
| 289 | char buf[64], *vals, *ends; |
| 290 | char tbuf[1024]; |
| 291 | |
| 292 | if (!valid_only_metric(unit)) |
| 293 | return; |
| 294 | unit = fixunit(tbuf, os->evsel, unit); |
| 295 | snprintf(buf, sizeof buf, fmt, val); |
Arnaldo Carvalho de Melo | 3285848 | 2019-06-26 11:42:03 -0300 | [diff] [blame] | 296 | ends = vals = skip_spaces(buf); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 297 | while (isdigit(*ends) || *ends == '.') |
| 298 | ends++; |
| 299 | *ends = 0; |
| 300 | fprintf(out, "%s%s", vals, config->csv_sep); |
| 301 | } |
| 302 | |
| 303 | static void new_line_metric(struct perf_stat_config *config __maybe_unused, |
| 304 | void *ctx __maybe_unused) |
| 305 | { |
| 306 | } |
| 307 | |
| 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) |
| 312 | { |
| 313 | struct outstate *os = ctx; |
| 314 | char tbuf[1024]; |
| 315 | |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 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) |
| 319 | return; |
| 320 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 321 | if (!valid_only_metric(unit)) |
| 322 | return; |
| 323 | unit = fixunit(tbuf, os->evsel, unit); |
| 324 | if (config->csv_output) |
| 325 | fprintf(os->fh, "%s%s", unit, config->csv_sep); |
| 326 | else |
| 327 | fprintf(os->fh, "%*s ", config->metric_only_len, unit); |
| 328 | } |
| 329 | |
Ian Rogers | ce37ab3 | 2022-01-04 22:13:50 -0800 | [diff] [blame] | 330 | static int first_shadow_cpu_map_idx(struct perf_stat_config *config, |
| 331 | struct evsel *evsel, const struct aggr_cpu_id *id) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 332 | { |
Ian Rogers | ce37ab3 | 2022-01-04 22:13:50 -0800 | [diff] [blame] | 333 | struct perf_cpu_map *cpus = evsel__cpus(evsel); |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 334 | struct perf_cpu cpu; |
| 335 | int idx; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 336 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 337 | if (config->aggr_mode == AGGR_NONE) |
Ian Rogers | ce37ab3 | 2022-01-04 22:13:50 -0800 | [diff] [blame] | 338 | return perf_cpu_map__idx(cpus, id->cpu); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 339 | |
Namhyung Kim | c0ee1d5a | 2020-11-27 13:14:03 +0900 | [diff] [blame] | 340 | if (!config->aggr_get_id) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 341 | return 0; |
| 342 | |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 343 | perf_cpu_map__for_each_cpu(cpu, idx, cpus) { |
Ian Rogers | 3ac23d1 | 2022-01-04 22:13:16 -0800 | [diff] [blame] | 344 | struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu); |
| 345 | |
| 346 | if (aggr_cpu_id__equal(&cpu_id, id)) |
Ian Rogers | ce37ab3 | 2022-01-04 22:13:50 -0800 | [diff] [blame] | 347 | return idx; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static void abs_printout(struct perf_stat_config *config, |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 353 | struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 354 | { |
| 355 | FILE *output = config->output; |
| 356 | double sc = evsel->scale; |
| 357 | const char *fmt; |
| 358 | |
| 359 | if (config->csv_output) { |
| 360 | fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s"; |
| 361 | } else { |
| 362 | if (config->big_num) |
| 363 | fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s"; |
| 364 | else |
| 365 | fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s"; |
| 366 | } |
| 367 | |
| 368 | aggr_printout(config, evsel, id, nr); |
| 369 | |
| 370 | fprintf(output, fmt, avg, config->csv_sep); |
| 371 | |
| 372 | if (evsel->unit) |
| 373 | fprintf(output, "%-*s%s", |
| 374 | config->csv_output ? 0 : config->unit_width, |
| 375 | evsel->unit, config->csv_sep); |
| 376 | |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 377 | fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel)); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 378 | |
Stephane Eranian | bc4da38 | 2018-11-07 02:50:45 -0800 | [diff] [blame] | 379 | print_cgroup(config, evsel); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 382 | static bool is_mixed_hw_group(struct evsel *counter) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 383 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 384 | struct evlist *evlist = counter->evlist; |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 385 | u32 pmu_type = counter->core.attr.type; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 386 | struct evsel *pos; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 387 | |
Jiri Olsa | 5643b1a | 2019-07-21 13:24:46 +0200 | [diff] [blame] | 388 | if (counter->core.nr_members < 2) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 389 | return false; |
| 390 | |
| 391 | evlist__for_each_entry(evlist, pos) { |
| 392 | /* software events can be part of any hardware group */ |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 393 | if (pos->core.attr.type == PERF_TYPE_SOFTWARE) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 394 | continue; |
| 395 | if (pmu_type == PERF_TYPE_SOFTWARE) { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 396 | pmu_type = pos->core.attr.type; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 397 | continue; |
| 398 | } |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 399 | if (pmu_type != pos->core.attr.type) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
| 403 | return false; |
| 404 | } |
| 405 | |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 406 | static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 407 | struct evsel *counter, double uval, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 408 | char *prefix, u64 run, u64 ena, double noise, |
| 409 | struct runtime_stat *st) |
| 410 | { |
| 411 | struct perf_stat_output_ctx out; |
| 412 | struct outstate os = { |
| 413 | .fh = config->output, |
| 414 | .prefix = prefix ? prefix : "", |
| 415 | .id = id, |
| 416 | .nr = nr, |
| 417 | .evsel = counter, |
| 418 | }; |
| 419 | print_metric_t pm = print_metric_std; |
| 420 | new_line_t nl; |
| 421 | |
| 422 | if (config->metric_only) { |
| 423 | nl = new_line_metric; |
| 424 | if (config->csv_output) |
| 425 | pm = print_metric_only_csv; |
| 426 | else |
| 427 | pm = print_metric_only; |
| 428 | } else |
| 429 | nl = new_line_std; |
| 430 | |
| 431 | if (config->csv_output && !config->metric_only) { |
| 432 | static int aggr_fields[] = { |
| 433 | [AGGR_GLOBAL] = 0, |
| 434 | [AGGR_THREAD] = 1, |
| 435 | [AGGR_NONE] = 1, |
| 436 | [AGGR_SOCKET] = 2, |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 437 | [AGGR_DIE] = 2, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 438 | [AGGR_CORE] = 2, |
| 439 | }; |
| 440 | |
| 441 | pm = print_metric_csv; |
| 442 | nl = new_line_csv; |
| 443 | os.nfields = 3; |
| 444 | os.nfields += aggr_fields[config->aggr_mode]; |
| 445 | if (counter->cgrp) |
| 446 | os.nfields++; |
| 447 | } |
Jin Yao | 0bdad97 | 2021-03-19 15:01:55 +0800 | [diff] [blame] | 448 | |
| 449 | if (!config->no_csv_summary && config->csv_output && |
| 450 | config->summary && !config->interval) { |
| 451 | fprintf(config->output, "%16s%s", "summary", config->csv_sep); |
| 452 | } |
| 453 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 454 | if (run == 0 || ena == 0 || counter->counts->scaled == -1) { |
| 455 | if (config->metric_only) { |
| 456 | pm(config, &os, NULL, "", "", 0); |
| 457 | return; |
| 458 | } |
| 459 | aggr_printout(config, counter, id, nr); |
| 460 | |
| 461 | fprintf(config->output, "%*s%s", |
| 462 | config->csv_output ? 0 : 18, |
| 463 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
| 464 | config->csv_sep); |
| 465 | |
| 466 | if (counter->supported) { |
Jin Yao | 493be70 | 2021-06-10 11:45:57 +0800 | [diff] [blame] | 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; |
| 471 | } |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | fprintf(config->output, "%-*s%s", |
| 475 | config->csv_output ? 0 : config->unit_width, |
| 476 | counter->unit, config->csv_sep); |
| 477 | |
| 478 | fprintf(config->output, "%*s", |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 479 | config->csv_output ? 0 : -25, evsel__name(counter)); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 480 | |
Stephane Eranian | bc4da38 | 2018-11-07 02:50:45 -0800 | [diff] [blame] | 481 | print_cgroup(config, counter); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 482 | |
| 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); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | if (!config->metric_only) |
| 493 | abs_printout(config, id, nr, counter, uval); |
| 494 | |
| 495 | out.print_metric = pm; |
| 496 | out.new_line = nl; |
| 497 | out.ctx = &os; |
| 498 | out.force_header = false; |
| 499 | |
| 500 | if (config->csv_output && !config->metric_only) { |
| 501 | print_noise(config, counter, noise); |
| 502 | print_running(config, run, ena); |
| 503 | } |
| 504 | |
| 505 | perf_stat__print_shadow_stats(config, counter, uval, |
Ian Rogers | ce37ab3 | 2022-01-04 22:13:50 -0800 | [diff] [blame] | 506 | first_shadow_cpu_map_idx(config, counter, &id), |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 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); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static void aggr_update_shadow(struct perf_stat_config *config, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 515 | struct evlist *evlist) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 516 | { |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 517 | int idx, s; |
| 518 | struct perf_cpu cpu; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 519 | struct aggr_cpu_id s2, id; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 520 | u64 val; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 521 | struct evsel *counter; |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 522 | struct perf_cpu_map *cpus; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 523 | |
| 524 | for (s = 0; s < config->aggr_map->nr; s++) { |
James Clark | ff52329 | 2020-11-26 16:13:23 +0200 | [diff] [blame] | 525 | id = config->aggr_map->map[s]; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 526 | evlist__for_each_entry(evlist, counter) { |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 527 | cpus = evsel__cpus(counter); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 528 | val = 0; |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 529 | perf_cpu_map__for_each_cpu(cpu, idx, cpus) { |
Ian Rogers | 88031a0 | 2022-01-04 22:13:08 -0800 | [diff] [blame] | 530 | s2 = config->aggr_get_id(config, cpu); |
Ian Rogers | 3ac23d1 | 2022-01-04 22:13:16 -0800 | [diff] [blame] | 531 | if (!aggr_cpu_id__equal(&s2, &id)) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 532 | continue; |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 533 | val += perf_counts(counter->counts, idx, 0)->val; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 534 | } |
| 535 | perf_stat__update_shadow_stats(counter, val, |
Ian Rogers | ce37ab3 | 2022-01-04 22:13:50 -0800 | [diff] [blame] | 536 | first_shadow_cpu_map_idx(config, counter, &id), |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 537 | &rt_stat); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 542 | static void uniquify_event_name(struct evsel *counter) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 543 | { |
| 544 | char *new_name; |
| 545 | char *config; |
Jin Yao | 1227942 | 2021-04-27 15:01:20 +0800 | [diff] [blame] | 546 | int ret = 0; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 547 | |
Namhyung Kim | 3cc8439 | 2021-06-02 14:22:41 -0700 | [diff] [blame] | 548 | if (counter->uniquified_name || counter->use_config_name || |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 549 | !counter->pmu_name || !strncmp(counter->name, counter->pmu_name, |
| 550 | strlen(counter->pmu_name))) |
| 551 | return; |
| 552 | |
| 553 | config = strchr(counter->name, '/'); |
| 554 | if (config) { |
| 555 | if (asprintf(&new_name, |
| 556 | "%s%s", counter->pmu_name, config) > 0) { |
| 557 | free(counter->name); |
| 558 | counter->name = new_name; |
| 559 | } |
| 560 | } else { |
Jin Yao | 1227942 | 2021-04-27 15:01:20 +0800 | [diff] [blame] | 561 | if (perf_pmu__has_hybrid()) { |
Namhyung Kim | 3cc8439 | 2021-06-02 14:22:41 -0700 | [diff] [blame] | 562 | ret = asprintf(&new_name, "%s/%s/", |
| 563 | counter->pmu_name, counter->name); |
Jin Yao | 1227942 | 2021-04-27 15:01:20 +0800 | [diff] [blame] | 564 | } else { |
| 565 | ret = asprintf(&new_name, "%s [%s]", |
| 566 | counter->name, counter->pmu_name); |
| 567 | } |
| 568 | |
| 569 | if (ret) { |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 570 | free(counter->name); |
| 571 | counter->name = new_name; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | counter->uniquified_name = true; |
| 576 | } |
| 577 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 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, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 580 | bool first), |
| 581 | void *data) |
| 582 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 583 | struct evlist *evlist = counter->evlist; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 584 | struct evsel *alias; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 585 | |
Jiri Olsa | ce9036a | 2019-07-21 13:24:23 +0200 | [diff] [blame] | 586 | alias = list_prepare_entry(counter, &(evlist->core.entries), core.node); |
| 587 | list_for_each_entry_continue (alias, &evlist->core.entries, core.node) { |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 588 | if (strcmp(evsel__name(alias), evsel__name(counter)) || |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 589 | alias->scale != counter->scale || |
| 590 | alias->cgrp != counter->cgrp || |
| 591 | strcmp(alias->unit, counter->unit) || |
Arnaldo Carvalho de Melo | c754c38 | 2020-04-30 10:51:16 -0300 | [diff] [blame] | 592 | evsel__is_clock(alias) != evsel__is_clock(counter) || |
Andi Kleen | 6c5f4e5 | 2019-06-24 12:37:09 -0700 | [diff] [blame] | 593 | !strcmp(alias->pmu_name, counter->pmu_name)) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 594 | break; |
| 595 | alias->merged_stat = true; |
| 596 | cb(config, alias, data, false); |
| 597 | } |
| 598 | } |
| 599 | |
Jin Yao | e0a7ef2 | 2021-07-07 13:56:52 +0800 | [diff] [blame] | 600 | static bool is_uncore(struct evsel *evsel) |
| 601 | { |
| 602 | struct perf_pmu *pmu = evsel__find_pmu(evsel); |
| 603 | |
| 604 | return pmu && pmu->is_uncore; |
| 605 | } |
| 606 | |
| 607 | static bool hybrid_uniquify(struct evsel *evsel) |
| 608 | { |
| 609 | return perf_pmu__has_hybrid() && !is_uncore(evsel); |
| 610 | } |
| 611 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 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, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 614 | bool first), |
| 615 | void *data) |
| 616 | { |
| 617 | if (counter->merged_stat) |
| 618 | return false; |
| 619 | cb(config, counter, data, true); |
Jin Yao | e0a7ef2 | 2021-07-07 13:56:52 +0800 | [diff] [blame] | 620 | if (config->no_merge || hybrid_uniquify(counter)) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 621 | uniquify_event_name(counter); |
| 622 | else if (counter->auto_merge_stats) |
| 623 | collect_all_aliases(config, counter, cb, data); |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | struct aggr_data { |
| 628 | u64 ena, run, val; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 629 | struct aggr_cpu_id id; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 630 | int nr; |
Ian Rogers | ab90caa7 | 2022-01-04 22:13:33 -0800 | [diff] [blame] | 631 | int cpu_map_idx; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | static void aggr_cb(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 635 | struct evsel *counter, void *data, bool first) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 636 | { |
| 637 | struct aggr_data *ad = data; |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 638 | int idx; |
| 639 | struct perf_cpu cpu; |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 640 | struct perf_cpu_map *cpus; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 641 | struct aggr_cpu_id s2; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 642 | |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 643 | cpus = evsel__cpus(counter); |
| 644 | perf_cpu_map__for_each_cpu(cpu, idx, cpus) { |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 645 | struct perf_counts_values *counts; |
| 646 | |
Ian Rogers | 88031a0 | 2022-01-04 22:13:08 -0800 | [diff] [blame] | 647 | s2 = config->aggr_get_id(config, cpu); |
Ian Rogers | 3ac23d1 | 2022-01-04 22:13:16 -0800 | [diff] [blame] | 648 | if (!aggr_cpu_id__equal(&s2, &ad->id)) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 649 | continue; |
| 650 | if (first) |
| 651 | ad->nr++; |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 652 | counts = perf_counts(counter->counts, idx, 0); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 653 | /* |
| 654 | * When any result is bad, make them all to give |
| 655 | * consistent output in interval mode. |
| 656 | */ |
| 657 | if (counts->ena == 0 || counts->run == 0 || |
| 658 | counter->counts->scaled == -1) { |
| 659 | ad->ena = 0; |
| 660 | ad->run = 0; |
| 661 | break; |
| 662 | } |
| 663 | ad->val += counts->val; |
| 664 | ad->ena += counts->ena; |
| 665 | ad->run += counts->run; |
| 666 | } |
| 667 | } |
| 668 | |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 669 | static void print_counter_aggrdata(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 670 | struct evsel *counter, int s, |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 671 | char *prefix, bool metric_only, |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 672 | bool *first, struct perf_cpu cpu) |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 673 | { |
| 674 | struct aggr_data ad; |
| 675 | FILE *output = config->output; |
| 676 | u64 ena, run, val; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 677 | int nr; |
| 678 | struct aggr_cpu_id id; |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 679 | double uval; |
| 680 | |
James Clark | ff52329 | 2020-11-26 16:13:23 +0200 | [diff] [blame] | 681 | ad.id = id = config->aggr_map->map[s]; |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 682 | ad.val = ad.ena = ad.run = 0; |
| 683 | ad.nr = 0; |
| 684 | if (!collect_data(config, counter, aggr_cb, &ad)) |
| 685 | return; |
| 686 | |
Jin Yao | 92637cc | 2021-04-27 15:01:28 +0800 | [diff] [blame] | 687 | if (perf_pmu__has_hybrid() && ad.ena == 0) |
| 688 | return; |
| 689 | |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 690 | nr = ad.nr; |
| 691 | ena = ad.ena; |
| 692 | run = ad.run; |
| 693 | val = ad.val; |
| 694 | if (*first && metric_only) { |
| 695 | *first = false; |
| 696 | aggr_printout(config, counter, id, nr); |
| 697 | } |
| 698 | if (prefix && !metric_only) |
| 699 | fprintf(output, "%s", prefix); |
| 700 | |
| 701 | uval = val * counter->scale; |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 702 | if (cpu.cpu != -1) |
Ian Rogers | 7365f10 | 2022-01-04 22:13:28 -0800 | [diff] [blame] | 703 | id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); |
| 704 | |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 705 | printout(config, id, nr, counter, uval, |
| 706 | prefix, run, ena, 1.0, &rt_stat); |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 707 | if (!metric_only) |
| 708 | fputc('\n', output); |
| 709 | } |
| 710 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 711 | static void print_aggr(struct perf_stat_config *config, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 712 | struct evlist *evlist, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 713 | char *prefix) |
| 714 | { |
| 715 | bool metric_only = config->metric_only; |
| 716 | FILE *output = config->output; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 717 | struct evsel *counter; |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 718 | int s; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 719 | bool first; |
| 720 | |
Hongbo Yao | c0c652f | 2020-06-05 17:17:40 +0800 | [diff] [blame] | 721 | if (!config->aggr_map || !config->aggr_get_id) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 722 | return; |
| 723 | |
| 724 | aggr_update_shadow(config, evlist); |
| 725 | |
| 726 | /* |
| 727 | * With metric_only everything is on a single line. |
| 728 | * Without each counter has its own line. |
| 729 | */ |
| 730 | for (s = 0; s < config->aggr_map->nr; s++) { |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 731 | if (prefix && metric_only) |
| 732 | fprintf(output, "%s", prefix); |
| 733 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 734 | first = true; |
| 735 | evlist__for_each_entry(evlist, counter) { |
Jin Yao | 40480a8 | 2019-04-12 21:59:48 +0800 | [diff] [blame] | 736 | print_counter_aggrdata(config, counter, s, |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 737 | prefix, metric_only, |
| 738 | &first, (struct perf_cpu){ .cpu = -1 }); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 739 | } |
| 740 | if (metric_only) |
| 741 | fputc('\n', output); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | static int cmp_val(const void *a, const void *b) |
| 746 | { |
| 747 | return ((struct perf_aggr_thread_value *)b)->val - |
| 748 | ((struct perf_aggr_thread_value *)a)->val; |
| 749 | } |
| 750 | |
| 751 | static struct perf_aggr_thread_value *sort_aggr_thread( |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 752 | struct evsel *counter, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 753 | int nthreads, int ncpus, |
| 754 | int *ret, |
| 755 | struct target *_target) |
| 756 | { |
| 757 | int cpu, thread, i = 0; |
| 758 | double uval; |
| 759 | struct perf_aggr_thread_value *buf; |
| 760 | |
| 761 | buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value)); |
| 762 | if (!buf) |
| 763 | return NULL; |
| 764 | |
| 765 | for (thread = 0; thread < nthreads; thread++) { |
| 766 | u64 ena = 0, run = 0, val = 0; |
| 767 | |
| 768 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 769 | val += perf_counts(counter->counts, cpu, thread)->val; |
| 770 | ena += perf_counts(counter->counts, cpu, thread)->ena; |
| 771 | run += perf_counts(counter->counts, cpu, thread)->run; |
| 772 | } |
| 773 | |
| 774 | uval = val * counter->scale; |
| 775 | |
| 776 | /* |
| 777 | * Skip value 0 when enabling --per-thread globally, |
| 778 | * otherwise too many 0 output. |
| 779 | */ |
| 780 | if (uval == 0.0 && target__has_per_thread(_target)) |
| 781 | continue; |
| 782 | |
| 783 | buf[i].counter = counter; |
Ian Rogers | 51b826f | 2022-01-04 22:13:17 -0800 | [diff] [blame] | 784 | buf[i].id = aggr_cpu_id__empty(); |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 785 | buf[i].id.thread = thread; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 786 | buf[i].uval = uval; |
| 787 | buf[i].val = val; |
| 788 | buf[i].run = run; |
| 789 | buf[i].ena = ena; |
| 790 | i++; |
| 791 | } |
| 792 | |
| 793 | qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val); |
| 794 | |
| 795 | if (ret) |
| 796 | *ret = i; |
| 797 | |
| 798 | return buf; |
| 799 | } |
| 800 | |
| 801 | static void print_aggr_thread(struct perf_stat_config *config, |
| 802 | struct target *_target, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 803 | struct evsel *counter, char *prefix) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 804 | { |
| 805 | FILE *output = config->output; |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 806 | int nthreads = perf_thread_map__nr(counter->core.threads); |
Jiri Olsa | 6549cd8 | 2019-08-22 13:11:38 +0200 | [diff] [blame] | 807 | int ncpus = perf_cpu_map__nr(counter->core.cpus); |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 808 | int thread, sorted_threads; |
| 809 | struct aggr_cpu_id id; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 810 | struct perf_aggr_thread_value *buf; |
| 811 | |
| 812 | buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target); |
| 813 | if (!buf) { |
| 814 | perror("cannot sort aggr thread"); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | for (thread = 0; thread < sorted_threads; thread++) { |
| 819 | if (prefix) |
| 820 | fprintf(output, "%s", prefix); |
| 821 | |
| 822 | id = buf[thread].id; |
| 823 | if (config->stats) |
| 824 | printout(config, id, 0, buf[thread].counter, buf[thread].uval, |
| 825 | prefix, buf[thread].run, buf[thread].ena, 1.0, |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 826 | &config->stats[id.thread]); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 827 | else |
| 828 | printout(config, id, 0, buf[thread].counter, buf[thread].uval, |
| 829 | prefix, buf[thread].run, buf[thread].ena, 1.0, |
| 830 | &rt_stat); |
| 831 | fputc('\n', output); |
| 832 | } |
| 833 | |
| 834 | free(buf); |
| 835 | } |
| 836 | |
| 837 | struct caggr_data { |
| 838 | double avg, avg_enabled, avg_running; |
| 839 | }; |
| 840 | |
| 841 | static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 842 | struct evsel *counter, void *data, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 843 | bool first __maybe_unused) |
| 844 | { |
| 845 | struct caggr_data *cd = data; |
Namhyung Kim | 07b747f | 2021-04-22 19:38:33 -0700 | [diff] [blame] | 846 | struct perf_counts_values *aggr = &counter->counts->aggr; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 847 | |
Namhyung Kim | 07b747f | 2021-04-22 19:38:33 -0700 | [diff] [blame] | 848 | cd->avg += aggr->val; |
| 849 | cd->avg_enabled += aggr->ena; |
| 850 | cd->avg_running += aggr->run; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Print out the results of a single counter: |
| 855 | * aggregated counts in system-wide mode |
| 856 | */ |
| 857 | static void print_counter_aggr(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 858 | struct evsel *counter, char *prefix) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 859 | { |
| 860 | bool metric_only = config->metric_only; |
| 861 | FILE *output = config->output; |
| 862 | double uval; |
| 863 | struct caggr_data cd = { .avg = 0.0 }; |
| 864 | |
| 865 | if (!collect_data(config, counter, counter_aggr_cb, &cd)) |
| 866 | return; |
| 867 | |
| 868 | if (prefix && !metric_only) |
| 869 | fprintf(output, "%s", prefix); |
| 870 | |
| 871 | uval = cd.avg * counter->scale; |
Ian Rogers | 51b826f | 2022-01-04 22:13:17 -0800 | [diff] [blame] | 872 | printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running, |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 873 | cd.avg_enabled, cd.avg, &rt_stat); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 874 | if (!metric_only) |
| 875 | fprintf(output, "\n"); |
| 876 | } |
| 877 | |
| 878 | static void counter_cb(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 879 | struct evsel *counter, void *data, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 880 | bool first __maybe_unused) |
| 881 | { |
| 882 | struct aggr_data *ad = data; |
| 883 | |
Ian Rogers | ab90caa7 | 2022-01-04 22:13:33 -0800 | [diff] [blame] | 884 | ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val; |
| 885 | ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena; |
| 886 | ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* |
| 890 | * Print out the results of a single counter: |
| 891 | * does not use aggregated count in system-wide |
| 892 | */ |
| 893 | static void print_counter(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 894 | struct evsel *counter, char *prefix) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 895 | { |
| 896 | FILE *output = config->output; |
| 897 | u64 ena, run, val; |
| 898 | double uval; |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 899 | int idx; |
| 900 | struct perf_cpu cpu; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 901 | struct aggr_cpu_id id; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 902 | |
Ian Rogers | 7ea82fb | 2022-01-04 22:13:34 -0800 | [diff] [blame] | 903 | perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) { |
| 904 | struct aggr_data ad = { .cpu_map_idx = idx }; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 905 | |
| 906 | if (!collect_data(config, counter, counter_cb, &ad)) |
| 907 | return; |
| 908 | val = ad.val; |
| 909 | ena = ad.ena; |
| 910 | run = ad.run; |
| 911 | |
| 912 | if (prefix) |
| 913 | fprintf(output, "%s", prefix); |
| 914 | |
| 915 | uval = val * counter->scale; |
Ian Rogers | 7365f10 | 2022-01-04 22:13:28 -0800 | [diff] [blame] | 916 | id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 917 | printout(config, id, 0, counter, uval, prefix, |
| 918 | run, ena, 1.0, &rt_stat); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 919 | |
| 920 | fputc('\n', output); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | static void print_no_aggr_metric(struct perf_stat_config *config, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 925 | struct evlist *evlist, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 926 | char *prefix) |
| 927 | { |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 928 | int all_idx; |
| 929 | struct perf_cpu cpu; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 930 | |
Ian Rogers | 7365f10 | 2022-01-04 22:13:28 -0800 | [diff] [blame] | 931 | perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.cpus) { |
| 932 | struct evsel *counter; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 933 | bool first = true; |
| 934 | |
| 935 | if (prefix) |
| 936 | fputs(prefix, config->output); |
| 937 | evlist__for_each_entry(evlist, counter) { |
Ian Rogers | 7365f10 | 2022-01-04 22:13:28 -0800 | [diff] [blame] | 938 | u64 ena, run, val; |
| 939 | double uval; |
| 940 | struct aggr_cpu_id id; |
| 941 | int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu); |
| 942 | |
| 943 | if (counter_idx < 0) |
| 944 | continue; |
| 945 | |
| 946 | id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 947 | if (first) { |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 948 | aggr_printout(config, counter, id, 0); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 949 | first = false; |
| 950 | } |
Ian Rogers | 7365f10 | 2022-01-04 22:13:28 -0800 | [diff] [blame] | 951 | val = perf_counts(counter->counts, counter_idx, 0)->val; |
| 952 | ena = perf_counts(counter->counts, counter_idx, 0)->ena; |
| 953 | run = perf_counts(counter->counts, counter_idx, 0)->run; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 954 | |
| 955 | uval = val * counter->scale; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 956 | printout(config, id, 0, counter, uval, prefix, |
| 957 | run, ena, 1.0, &rt_stat); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 958 | } |
| 959 | fputc('\n', config->output); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | static int aggr_header_lens[] = { |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 964 | [AGGR_CORE] = 24, |
| 965 | [AGGR_DIE] = 18, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 966 | [AGGR_SOCKET] = 12, |
| 967 | [AGGR_NONE] = 6, |
| 968 | [AGGR_THREAD] = 24, |
| 969 | [AGGR_GLOBAL] = 0, |
| 970 | }; |
| 971 | |
| 972 | static const char *aggr_header_csv[] = { |
| 973 | [AGGR_CORE] = "core,cpus,", |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 974 | [AGGR_DIE] = "die,cpus", |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 975 | [AGGR_SOCKET] = "socket,cpus", |
| 976 | [AGGR_NONE] = "cpu,", |
| 977 | [AGGR_THREAD] = "comm-pid,", |
| 978 | [AGGR_GLOBAL] = "" |
| 979 | }; |
| 980 | |
| 981 | static void print_metric_headers(struct perf_stat_config *config, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 982 | struct evlist *evlist, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 983 | const char *prefix, bool no_indent) |
| 984 | { |
| 985 | struct perf_stat_output_ctx out; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 986 | struct evsel *counter; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 987 | struct outstate os = { |
| 988 | .fh = config->output |
| 989 | }; |
| 990 | |
| 991 | if (prefix) |
| 992 | fprintf(config->output, "%s", prefix); |
| 993 | |
| 994 | if (!config->csv_output && !no_indent) |
| 995 | fprintf(config->output, "%*s", |
| 996 | aggr_header_lens[config->aggr_mode], ""); |
| 997 | if (config->csv_output) { |
| 998 | if (config->interval) |
| 999 | fputs("time,", config->output); |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1000 | if (!config->iostat_run) |
| 1001 | fputs(aggr_header_csv[config->aggr_mode], config->output); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1002 | } |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1003 | if (config->iostat_run) |
| 1004 | iostat_print_header_prefix(config); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1005 | |
| 1006 | /* Print metrics headers only */ |
| 1007 | evlist__for_each_entry(evlist, counter) { |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1008 | os.evsel = counter; |
| 1009 | out.ctx = &os; |
| 1010 | out.print_metric = print_metric_header; |
| 1011 | out.new_line = new_line_metric; |
| 1012 | out.force_header = true; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1013 | perf_stat__print_shadow_stats(config, counter, 0, |
| 1014 | 0, |
| 1015 | &out, |
| 1016 | &config->metric_events, |
| 1017 | &rt_stat); |
| 1018 | } |
| 1019 | fputc('\n', config->output); |
| 1020 | } |
| 1021 | |
| 1022 | static void print_interval(struct perf_stat_config *config, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1023 | struct evlist *evlist, |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1024 | char *prefix, struct timespec *ts) |
| 1025 | { |
| 1026 | bool metric_only = config->metric_only; |
| 1027 | unsigned int unit_width = config->unit_width; |
| 1028 | FILE *output = config->output; |
| 1029 | static int num_print_interval; |
| 1030 | |
| 1031 | if (config->interval_clear) |
| 1032 | puts(CONSOLE_CLEAR); |
| 1033 | |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1034 | if (!config->iostat_run) |
| 1035 | sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1036 | |
| 1037 | if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) { |
| 1038 | switch (config->aggr_mode) { |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1039 | case AGGR_NODE: |
| 1040 | fprintf(output, "# time node cpus"); |
| 1041 | if (!metric_only) |
| 1042 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1043 | break; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1044 | case AGGR_SOCKET: |
| 1045 | fprintf(output, "# time socket cpus"); |
| 1046 | if (!metric_only) |
| 1047 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1048 | break; |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1049 | case AGGR_DIE: |
| 1050 | fprintf(output, "# time die cpus"); |
| 1051 | if (!metric_only) |
| 1052 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1053 | break; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1054 | case AGGR_CORE: |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1055 | fprintf(output, "# time core cpus"); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1056 | if (!metric_only) |
| 1057 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1058 | break; |
| 1059 | case AGGR_NONE: |
| 1060 | fprintf(output, "# time CPU "); |
| 1061 | if (!metric_only) |
| 1062 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1063 | break; |
| 1064 | case AGGR_THREAD: |
| 1065 | fprintf(output, "# time comm-pid"); |
| 1066 | if (!metric_only) |
| 1067 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1068 | break; |
| 1069 | case AGGR_GLOBAL: |
| 1070 | default: |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1071 | if (!config->iostat_run) { |
| 1072 | fprintf(output, "# time"); |
| 1073 | if (!metric_only) |
| 1074 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
| 1075 | } |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1076 | case AGGR_UNSET: |
| 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | if ((num_print_interval == 0 || config->interval_clear) && metric_only) |
| 1082 | print_metric_headers(config, evlist, " ", true); |
| 1083 | if (++num_print_interval == 25) |
| 1084 | num_print_interval = 0; |
| 1085 | } |
| 1086 | |
| 1087 | static void print_header(struct perf_stat_config *config, |
| 1088 | struct target *_target, |
| 1089 | int argc, const char **argv) |
| 1090 | { |
| 1091 | FILE *output = config->output; |
| 1092 | int i; |
| 1093 | |
| 1094 | fflush(stdout); |
| 1095 | |
| 1096 | if (!config->csv_output) { |
| 1097 | fprintf(output, "\n"); |
| 1098 | fprintf(output, " Performance counter stats for "); |
Song Liu | fa853c4 | 2020-12-29 13:42:14 -0800 | [diff] [blame] | 1099 | if (_target->bpf_str) |
| 1100 | fprintf(output, "\'BPF program(s) %s", _target->bpf_str); |
| 1101 | else if (_target->system_wide) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1102 | fprintf(output, "\'system wide"); |
| 1103 | else if (_target->cpu_list) |
| 1104 | fprintf(output, "\'CPU(s) %s", _target->cpu_list); |
| 1105 | else if (!target__has_task(_target)) { |
| 1106 | fprintf(output, "\'%s", argv ? argv[0] : "pipe"); |
| 1107 | for (i = 1; argv && (i < argc); i++) |
| 1108 | fprintf(output, " %s", argv[i]); |
| 1109 | } else if (_target->pid) |
| 1110 | fprintf(output, "process id \'%s", _target->pid); |
| 1111 | else |
| 1112 | fprintf(output, "thread id \'%s", _target->tid); |
| 1113 | |
| 1114 | fprintf(output, "\'"); |
| 1115 | if (config->run_count > 1) |
| 1116 | fprintf(output, " (%d runs)", config->run_count); |
| 1117 | fprintf(output, ":\n\n"); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | static int get_precision(double num) |
| 1122 | { |
| 1123 | if (num > 1) |
| 1124 | return 0; |
| 1125 | |
| 1126 | return lround(ceil(-log10(num))); |
| 1127 | } |
| 1128 | |
| 1129 | static void print_table(struct perf_stat_config *config, |
| 1130 | FILE *output, int precision, double avg) |
| 1131 | { |
| 1132 | char tmp[64]; |
| 1133 | int idx, indent = 0; |
| 1134 | |
| 1135 | scnprintf(tmp, 64, " %17.*f", precision, avg); |
| 1136 | while (tmp[indent] == ' ') |
| 1137 | indent++; |
| 1138 | |
| 1139 | fprintf(output, "%*s# Table of individual measurements:\n", indent, ""); |
| 1140 | |
| 1141 | for (idx = 0; idx < config->run_count; idx++) { |
| 1142 | double run = (double) config->walltime_run[idx] / NSEC_PER_SEC; |
| 1143 | int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5); |
| 1144 | |
| 1145 | fprintf(output, " %17.*f (%+.*f) ", |
| 1146 | precision, run, precision, run - avg); |
| 1147 | |
| 1148 | for (h = 0; h < n; h++) |
| 1149 | fprintf(output, "#"); |
| 1150 | |
| 1151 | fprintf(output, "\n"); |
| 1152 | } |
| 1153 | |
| 1154 | fprintf(output, "\n%*s# Final result:\n", indent, ""); |
| 1155 | } |
| 1156 | |
| 1157 | static double timeval2double(struct timeval *t) |
| 1158 | { |
| 1159 | return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC; |
| 1160 | } |
| 1161 | |
| 1162 | static void print_footer(struct perf_stat_config *config) |
| 1163 | { |
| 1164 | double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; |
| 1165 | FILE *output = config->output; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1166 | |
| 1167 | if (!config->null_run) |
| 1168 | fprintf(output, "\n"); |
| 1169 | |
| 1170 | if (config->run_count == 1) { |
| 1171 | fprintf(output, " %17.9f seconds time elapsed", avg); |
| 1172 | |
| 1173 | if (config->ru_display) { |
| 1174 | double ru_utime = timeval2double(&config->ru_data.ru_utime); |
| 1175 | double ru_stime = timeval2double(&config->ru_data.ru_stime); |
| 1176 | |
| 1177 | fprintf(output, "\n\n"); |
| 1178 | fprintf(output, " %17.9f seconds user\n", ru_utime); |
| 1179 | fprintf(output, " %17.9f seconds sys\n", ru_stime); |
| 1180 | } |
| 1181 | } else { |
| 1182 | double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; |
| 1183 | /* |
| 1184 | * Display at most 2 more significant |
| 1185 | * digits than the stddev inaccuracy. |
| 1186 | */ |
| 1187 | int precision = get_precision(sd) + 2; |
| 1188 | |
| 1189 | if (config->walltime_run_table) |
| 1190 | print_table(config, output, precision, avg); |
| 1191 | |
| 1192 | fprintf(output, " %17.*f +- %.*f seconds time elapsed", |
| 1193 | precision, avg, precision, sd); |
| 1194 | |
| 1195 | print_noise_pct(config, sd, avg); |
| 1196 | } |
| 1197 | fprintf(output, "\n\n"); |
| 1198 | |
Kan Liang | 2a14c1b | 2020-02-24 13:59:22 -0800 | [diff] [blame] | 1199 | if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled()) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1200 | fprintf(output, |
| 1201 | "Some events weren't counted. Try disabling the NMI watchdog:\n" |
| 1202 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" |
| 1203 | " perf stat ...\n" |
| 1204 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); |
| 1205 | |
| 1206 | if (config->print_mixed_hw_group_error) |
| 1207 | fprintf(output, |
| 1208 | "The events in group usually have to be from " |
| 1209 | "the same PMU. Try reorganizing the group.\n"); |
| 1210 | } |
| 1211 | |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1212 | static void print_percore_thread(struct perf_stat_config *config, |
| 1213 | struct evsel *counter, char *prefix) |
| 1214 | { |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 1215 | int s; |
| 1216 | struct aggr_cpu_id s2, id; |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 1217 | struct perf_cpu_map *cpus; |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1218 | bool first = true; |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 1219 | int idx; |
| 1220 | struct perf_cpu cpu; |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1221 | |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 1222 | cpus = evsel__cpus(counter); |
| 1223 | perf_cpu_map__for_each_cpu(cpu, idx, cpus) { |
Ian Rogers | 88031a0 | 2022-01-04 22:13:08 -0800 | [diff] [blame] | 1224 | s2 = config->aggr_get_id(config, cpu); |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1225 | for (s = 0; s < config->aggr_map->nr; s++) { |
James Clark | ff52329 | 2020-11-26 16:13:23 +0200 | [diff] [blame] | 1226 | id = config->aggr_map->map[s]; |
Ian Rogers | 3ac23d1 | 2022-01-04 22:13:16 -0800 | [diff] [blame] | 1227 | if (aggr_cpu_id__equal(&s2, &id)) |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | |
| 1231 | print_counter_aggrdata(config, counter, s, |
| 1232 | prefix, false, |
Ian Rogers | a023283 | 2022-01-04 22:13:07 -0800 | [diff] [blame] | 1233 | &first, cpu); |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1237 | static void print_percore(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1238 | struct evsel *counter, char *prefix) |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1239 | { |
| 1240 | bool metric_only = config->metric_only; |
| 1241 | FILE *output = config->output; |
| 1242 | int s; |
| 1243 | bool first = true; |
| 1244 | |
Hongbo Yao | c0c652f | 2020-06-05 17:17:40 +0800 | [diff] [blame] | 1245 | if (!config->aggr_map || !config->aggr_get_id) |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1246 | return; |
| 1247 | |
Jin Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1248 | if (config->percore_show_thread) |
| 1249 | return print_percore_thread(config, counter, prefix); |
| 1250 | |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1251 | for (s = 0; s < config->aggr_map->nr; s++) { |
| 1252 | if (prefix && metric_only) |
| 1253 | fprintf(output, "%s", prefix); |
| 1254 | |
| 1255 | print_counter_aggrdata(config, counter, s, |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 1256 | prefix, metric_only, |
| 1257 | &first, (struct perf_cpu){ .cpu = -1 }); |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | if (metric_only) |
| 1261 | fputc('\n', output); |
| 1262 | } |
| 1263 | |
Arnaldo Carvalho de Melo | 7127372 | 2020-11-30 14:55:12 -0300 | [diff] [blame] | 1264 | void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config, |
| 1265 | struct target *_target, struct timespec *ts, int argc, const char **argv) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1266 | { |
| 1267 | bool metric_only = config->metric_only; |
| 1268 | int interval = config->interval; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1269 | struct evsel *counter; |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1270 | char buf[64], *prefix = NULL; |
| 1271 | |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1272 | if (config->iostat_run) |
| 1273 | evlist->selected = evlist__first(evlist); |
| 1274 | |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1275 | if (interval) |
| 1276 | print_interval(config, evlist, prefix = buf, ts); |
| 1277 | else |
| 1278 | print_header(config, _target, argc, argv); |
| 1279 | |
| 1280 | if (metric_only) { |
| 1281 | static int num_print_iv; |
| 1282 | |
| 1283 | if (num_print_iv == 0 && !interval) |
| 1284 | print_metric_headers(config, evlist, prefix, false); |
| 1285 | if (num_print_iv++ == 25) |
| 1286 | num_print_iv = 0; |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1287 | if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run) |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1288 | fprintf(config->output, "%s", prefix); |
| 1289 | } |
| 1290 | |
| 1291 | switch (config->aggr_mode) { |
| 1292 | case AGGR_CORE: |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1293 | case AGGR_DIE: |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1294 | case AGGR_SOCKET: |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1295 | case AGGR_NODE: |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1296 | print_aggr(config, evlist, prefix); |
| 1297 | break; |
| 1298 | case AGGR_THREAD: |
| 1299 | evlist__for_each_entry(evlist, counter) { |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1300 | print_aggr_thread(config, _target, counter, prefix); |
| 1301 | } |
| 1302 | break; |
| 1303 | case AGGR_GLOBAL: |
Alexander Antonov | f07952b | 2021-04-19 12:41:44 +0300 | [diff] [blame] | 1304 | if (config->iostat_run) |
| 1305 | iostat_print_counters(evlist, config, ts, prefix = buf, |
| 1306 | print_counter_aggr); |
| 1307 | else { |
| 1308 | evlist__for_each_entry(evlist, counter) { |
| 1309 | print_counter_aggr(config, counter, prefix); |
| 1310 | } |
| 1311 | if (metric_only) |
| 1312 | fputc('\n', config->output); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1313 | } |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1314 | break; |
| 1315 | case AGGR_NONE: |
| 1316 | if (metric_only) |
| 1317 | print_no_aggr_metric(config, evlist, prefix); |
| 1318 | else { |
| 1319 | evlist__for_each_entry(evlist, counter) { |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1320 | if (counter->percore) |
| 1321 | print_percore(config, counter, prefix); |
| 1322 | else |
| 1323 | print_counter(config, counter, prefix); |
Jiri Olsa | 088519f | 2018-08-30 08:32:52 +0200 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | break; |
| 1327 | case AGGR_UNSET: |
| 1328 | default: |
| 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | if (!interval && !config->csv_output) |
| 1333 | print_footer(config); |
| 1334 | |
| 1335 | fflush(config->output); |
| 1336 | } |