0001 #include <stdlib.h>
0002 #include <stdio.h>
0003 #include <inttypes.h>
0004 #include <linux/string.h>
0005 #include <linux/time64.h>
0006 #include <math.h>
0007 #include <perf/cpumap.h>
0008 #include "color.h"
0009 #include "counts.h"
0010 #include "evlist.h"
0011 #include "evsel.h"
0012 #include "stat.h"
0013 #include "top.h"
0014 #include "thread_map.h"
0015 #include "cpumap.h"
0016 #include "string2.h"
0017 #include <linux/ctype.h>
0018 #include "cgroup.h"
0019 #include <api/fs/fs.h>
0020 #include "util.h"
0021 #include "iostat.h"
0022 #include "pmu-hybrid.h"
0023 #include "evlist-hybrid.h"
0024
0025 #define CNTR_NOT_SUPPORTED "<not supported>"
0026 #define CNTR_NOT_COUNTED "<not counted>"
0027
0028 static void print_running(struct perf_stat_config *config,
0029 u64 run, u64 ena)
0030 {
0031
0032 double enabled_percent = 100;
0033
0034 if (run != ena)
0035 enabled_percent = 100 * run / ena;
0036 if (config->json_output)
0037 fprintf(config->output,
0038 "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
0039 run, enabled_percent);
0040 else if (config->csv_output)
0041 fprintf(config->output,
0042 "%s%" PRIu64 "%s%.2f", config->csv_sep,
0043 run, config->csv_sep, enabled_percent);
0044 else if (run != ena)
0045 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
0046 }
0047
0048 static void print_noise_pct(struct perf_stat_config *config,
0049 double total, double avg)
0050 {
0051 double pct = rel_stddev_stats(total, avg);
0052
0053 if (config->json_output)
0054 fprintf(config->output, "\"variance\" : %.2f, ", pct);
0055 else if (config->csv_output)
0056 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
0057 else if (pct)
0058 fprintf(config->output, " ( +-%6.2f%% )", pct);
0059 }
0060
0061 static void print_noise(struct perf_stat_config *config,
0062 struct evsel *evsel, double avg)
0063 {
0064 struct perf_stat_evsel *ps;
0065
0066 if (config->run_count == 1)
0067 return;
0068
0069 ps = evsel->stats;
0070 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
0071 }
0072
0073 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
0074 {
0075 if (nr_cgroups) {
0076 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
0077
0078 if (config->json_output)
0079 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
0080 else
0081 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
0082 }
0083 }
0084
0085
0086 static void aggr_printout(struct perf_stat_config *config,
0087 struct evsel *evsel, struct aggr_cpu_id id, int nr)
0088 {
0089
0090
0091 if (config->json_output && !config->interval)
0092 fprintf(config->output, "{");
0093
0094 switch (config->aggr_mode) {
0095 case AGGR_CORE:
0096 if (config->json_output) {
0097 fprintf(config->output,
0098 "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
0099 id.socket,
0100 id.die,
0101 id.core,
0102 nr);
0103 } else {
0104 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
0105 id.socket,
0106 id.die,
0107 config->csv_output ? 0 : -8,
0108 id.core,
0109 config->csv_sep,
0110 config->csv_output ? 0 : 4,
0111 nr,
0112 config->csv_sep);
0113 }
0114 break;
0115 case AGGR_DIE:
0116 if (config->json_output) {
0117 fprintf(config->output,
0118 "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
0119 id.socket,
0120 id.die,
0121 nr);
0122 } else {
0123 fprintf(config->output, "S%d-D%*d%s%*d%s",
0124 id.socket,
0125 config->csv_output ? 0 : -8,
0126 id.die,
0127 config->csv_sep,
0128 config->csv_output ? 0 : 4,
0129 nr,
0130 config->csv_sep);
0131 }
0132 break;
0133 case AGGR_SOCKET:
0134 if (config->json_output) {
0135 fprintf(config->output,
0136 "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
0137 id.socket,
0138 nr);
0139 } else {
0140 fprintf(config->output, "S%*d%s%*d%s",
0141 config->csv_output ? 0 : -5,
0142 id.socket,
0143 config->csv_sep,
0144 config->csv_output ? 0 : 4,
0145 nr,
0146 config->csv_sep);
0147 }
0148 break;
0149 case AGGR_NODE:
0150 if (config->json_output) {
0151 fprintf(config->output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
0152 id.node,
0153 nr);
0154 } else {
0155 fprintf(config->output, "N%*d%s%*d%s",
0156 config->csv_output ? 0 : -5,
0157 id.node,
0158 config->csv_sep,
0159 config->csv_output ? 0 : 4,
0160 nr,
0161 config->csv_sep);
0162 }
0163 break;
0164 case AGGR_NONE:
0165 if (config->json_output) {
0166 if (evsel->percore && !config->percore_show_thread) {
0167 fprintf(config->output, "\"core\" : \"S%d-D%d-C%d\"",
0168 id.socket,
0169 id.die,
0170 id.core);
0171 } else if (id.core > -1) {
0172 fprintf(config->output, "\"cpu\" : \"%d\", ",
0173 id.cpu.cpu);
0174 }
0175 } else {
0176 if (evsel->percore && !config->percore_show_thread) {
0177 fprintf(config->output, "S%d-D%d-C%*d%s",
0178 id.socket,
0179 id.die,
0180 config->csv_output ? 0 : -3,
0181 id.core, config->csv_sep);
0182 } else if (id.core > -1) {
0183 fprintf(config->output, "CPU%*d%s",
0184 config->csv_output ? 0 : -7,
0185 id.cpu.cpu, config->csv_sep);
0186 }
0187 }
0188 break;
0189 case AGGR_THREAD:
0190 if (config->json_output) {
0191 fprintf(config->output, "\"thread\" : \"%s-%d\", ",
0192 perf_thread_map__comm(evsel->core.threads, id.thread),
0193 perf_thread_map__pid(evsel->core.threads, id.thread));
0194 } else {
0195 fprintf(config->output, "%*s-%*d%s",
0196 config->csv_output ? 0 : 16,
0197 perf_thread_map__comm(evsel->core.threads, id.thread),
0198 config->csv_output ? 0 : -8,
0199 perf_thread_map__pid(evsel->core.threads, id.thread),
0200 config->csv_sep);
0201 }
0202 break;
0203 case AGGR_GLOBAL:
0204 case AGGR_UNSET:
0205 case AGGR_MAX:
0206 default:
0207 break;
0208 }
0209 }
0210
0211 struct outstate {
0212 FILE *fh;
0213 bool newline;
0214 const char *prefix;
0215 int nfields;
0216 int nr;
0217 struct aggr_cpu_id id;
0218 struct evsel *evsel;
0219 };
0220
0221 #define METRIC_LEN 35
0222
0223 static void new_line_std(struct perf_stat_config *config __maybe_unused,
0224 void *ctx)
0225 {
0226 struct outstate *os = ctx;
0227
0228 os->newline = true;
0229 }
0230
0231 static void do_new_line_std(struct perf_stat_config *config,
0232 struct outstate *os)
0233 {
0234 fputc('\n', os->fh);
0235 fputs(os->prefix, os->fh);
0236 aggr_printout(config, os->evsel, os->id, os->nr);
0237 if (config->aggr_mode == AGGR_NONE)
0238 fprintf(os->fh, " ");
0239 fprintf(os->fh, " ");
0240 }
0241
0242 static void print_metric_std(struct perf_stat_config *config,
0243 void *ctx, const char *color, const char *fmt,
0244 const char *unit, double val)
0245 {
0246 struct outstate *os = ctx;
0247 FILE *out = os->fh;
0248 int n;
0249 bool newline = os->newline;
0250
0251 os->newline = false;
0252
0253 if (unit == NULL || fmt == NULL) {
0254 fprintf(out, "%-*s", METRIC_LEN, "");
0255 return;
0256 }
0257
0258 if (newline)
0259 do_new_line_std(config, os);
0260
0261 n = fprintf(out, " # ");
0262 if (color)
0263 n += color_fprintf(out, color, fmt, val);
0264 else
0265 n += fprintf(out, fmt, val);
0266 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
0267 }
0268
0269 static void new_line_csv(struct perf_stat_config *config, void *ctx)
0270 {
0271 struct outstate *os = ctx;
0272 int i;
0273
0274 fputc('\n', os->fh);
0275 if (os->prefix)
0276 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
0277 aggr_printout(config, os->evsel, os->id, os->nr);
0278 for (i = 0; i < os->nfields; i++)
0279 fputs(config->csv_sep, os->fh);
0280 }
0281
0282 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
0283 void *ctx,
0284 const char *color __maybe_unused,
0285 const char *fmt, const char *unit, double val)
0286 {
0287 struct outstate *os = ctx;
0288 FILE *out = os->fh;
0289 char buf[64], *vals, *ends;
0290
0291 if (unit == NULL || fmt == NULL) {
0292 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
0293 return;
0294 }
0295 snprintf(buf, sizeof(buf), fmt, val);
0296 ends = vals = skip_spaces(buf);
0297 while (isdigit(*ends) || *ends == '.')
0298 ends++;
0299 *ends = 0;
0300 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
0301 }
0302
0303 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
0304 void *ctx,
0305 const char *color __maybe_unused,
0306 const char *fmt __maybe_unused,
0307 const char *unit, double val)
0308 {
0309 struct outstate *os = ctx;
0310 FILE *out = os->fh;
0311
0312 fprintf(out, "\"metric-value\" : %f, ", val);
0313 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
0314 if (!config->metric_only)
0315 fprintf(out, "}");
0316 }
0317
0318 static void new_line_json(struct perf_stat_config *config, void *ctx)
0319 {
0320 struct outstate *os = ctx;
0321
0322 fputc('\n', os->fh);
0323 if (os->prefix)
0324 fprintf(os->fh, "%s", os->prefix);
0325 aggr_printout(config, os->evsel, os->id, os->nr);
0326 }
0327
0328
0329
0330 static bool valid_only_metric(const char *unit)
0331 {
0332 if (!unit)
0333 return false;
0334 if (strstr(unit, "/sec") ||
0335 strstr(unit, "CPUs utilized"))
0336 return false;
0337 return true;
0338 }
0339
0340 static const char *fixunit(char *buf, struct evsel *evsel,
0341 const char *unit)
0342 {
0343 if (!strncmp(unit, "of all", 6)) {
0344 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
0345 unit);
0346 return buf;
0347 }
0348 return unit;
0349 }
0350
0351 static void print_metric_only(struct perf_stat_config *config,
0352 void *ctx, const char *color, const char *fmt,
0353 const char *unit, double val)
0354 {
0355 struct outstate *os = ctx;
0356 FILE *out = os->fh;
0357 char buf[1024], str[1024];
0358 unsigned mlen = config->metric_only_len;
0359
0360 if (!valid_only_metric(unit))
0361 return;
0362 unit = fixunit(buf, os->evsel, unit);
0363 if (mlen < strlen(unit))
0364 mlen = strlen(unit) + 1;
0365
0366 if (color)
0367 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
0368
0369 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
0370 fprintf(out, "%*s ", mlen, str);
0371 }
0372
0373 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
0374 void *ctx, const char *color __maybe_unused,
0375 const char *fmt,
0376 const char *unit, double val)
0377 {
0378 struct outstate *os = ctx;
0379 FILE *out = os->fh;
0380 char buf[64], *vals, *ends;
0381 char tbuf[1024];
0382
0383 if (!valid_only_metric(unit))
0384 return;
0385 unit = fixunit(tbuf, os->evsel, unit);
0386 snprintf(buf, sizeof buf, fmt, val);
0387 ends = vals = skip_spaces(buf);
0388 while (isdigit(*ends) || *ends == '.')
0389 ends++;
0390 *ends = 0;
0391 fprintf(out, "%s%s", vals, config->csv_sep);
0392 }
0393
0394 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
0395 void *ctx, const char *color __maybe_unused,
0396 const char *fmt,
0397 const char *unit, double val)
0398 {
0399 struct outstate *os = ctx;
0400 FILE *out = os->fh;
0401 char buf[64], *vals, *ends;
0402 char tbuf[1024];
0403
0404 if (!valid_only_metric(unit))
0405 return;
0406 unit = fixunit(tbuf, os->evsel, unit);
0407 snprintf(buf, sizeof(buf), fmt, val);
0408 ends = vals = skip_spaces(buf);
0409 while (isdigit(*ends) || *ends == '.')
0410 ends++;
0411 *ends = 0;
0412 fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
0413 }
0414
0415 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
0416 void *ctx __maybe_unused)
0417 {
0418 }
0419
0420 static void print_metric_header(struct perf_stat_config *config,
0421 void *ctx, const char *color __maybe_unused,
0422 const char *fmt __maybe_unused,
0423 const char *unit, double val __maybe_unused)
0424 {
0425 struct outstate *os = ctx;
0426 char tbuf[1024];
0427
0428
0429 if (config->iostat_run &&
0430 os->evsel->priv != os->evsel->evlist->selected->priv)
0431 return;
0432
0433 if (!valid_only_metric(unit) && !config->json_output)
0434 return;
0435 unit = fixunit(tbuf, os->evsel, unit);
0436
0437 if (config->json_output)
0438 fprintf(os->fh, "\"unit\" : \"%s\"", unit);
0439 else if (config->csv_output)
0440 fprintf(os->fh, "%s%s", unit, config->csv_sep);
0441 else
0442 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
0443 }
0444
0445 static int first_shadow_cpu_map_idx(struct perf_stat_config *config,
0446 struct evsel *evsel, const struct aggr_cpu_id *id)
0447 {
0448 struct perf_cpu_map *cpus = evsel__cpus(evsel);
0449 struct perf_cpu cpu;
0450 int idx;
0451
0452 if (config->aggr_mode == AGGR_NONE)
0453 return perf_cpu_map__idx(cpus, id->cpu);
0454
0455 if (!config->aggr_get_id)
0456 return 0;
0457
0458 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
0459 struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
0460
0461 if (aggr_cpu_id__equal(&cpu_id, id))
0462 return idx;
0463 }
0464 return 0;
0465 }
0466
0467 static void abs_printout(struct perf_stat_config *config,
0468 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
0469 {
0470 FILE *output = config->output;
0471 double sc = evsel->scale;
0472 const char *fmt;
0473
0474 if (config->csv_output) {
0475 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
0476 } else {
0477 if (config->big_num)
0478 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
0479 else
0480 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
0481 }
0482
0483 aggr_printout(config, evsel, id, nr);
0484
0485 if (config->json_output)
0486 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
0487 else
0488 fprintf(output, fmt, avg, config->csv_sep);
0489
0490 if (config->json_output) {
0491 if (evsel->unit) {
0492 fprintf(output, "\"unit\" : \"%s\", ",
0493 evsel->unit);
0494 }
0495 } else {
0496 if (evsel->unit)
0497 fprintf(output, "%-*s%s",
0498 config->csv_output ? 0 : config->unit_width,
0499 evsel->unit, config->csv_sep);
0500 }
0501
0502 if (config->json_output)
0503 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
0504 else
0505 fprintf(output, "%-*s", config->csv_output ? 0 : 32, evsel__name(evsel));
0506
0507 print_cgroup(config, evsel);
0508 }
0509
0510 static bool is_mixed_hw_group(struct evsel *counter)
0511 {
0512 struct evlist *evlist = counter->evlist;
0513 u32 pmu_type = counter->core.attr.type;
0514 struct evsel *pos;
0515
0516 if (counter->core.nr_members < 2)
0517 return false;
0518
0519 evlist__for_each_entry(evlist, pos) {
0520
0521 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
0522 continue;
0523 if (pmu_type == PERF_TYPE_SOFTWARE) {
0524 pmu_type = pos->core.attr.type;
0525 continue;
0526 }
0527 if (pmu_type != pos->core.attr.type)
0528 return true;
0529 }
0530
0531 return false;
0532 }
0533
0534 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
0535 struct evsel *counter, double uval,
0536 char *prefix, u64 run, u64 ena, double noise,
0537 struct runtime_stat *st)
0538 {
0539 struct perf_stat_output_ctx out;
0540 struct outstate os = {
0541 .fh = config->output,
0542 .prefix = prefix ? prefix : "",
0543 .id = id,
0544 .nr = nr,
0545 .evsel = counter,
0546 };
0547 print_metric_t pm;
0548 new_line_t nl;
0549
0550 if (config->csv_output) {
0551 static const int aggr_fields[AGGR_MAX] = {
0552 [AGGR_NONE] = 1,
0553 [AGGR_GLOBAL] = 0,
0554 [AGGR_SOCKET] = 2,
0555 [AGGR_DIE] = 2,
0556 [AGGR_CORE] = 2,
0557 [AGGR_THREAD] = 1,
0558 [AGGR_UNSET] = 0,
0559 [AGGR_NODE] = 0,
0560 };
0561
0562 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
0563 nl = config->metric_only ? new_line_metric : new_line_csv;
0564 os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
0565 } else if (config->json_output) {
0566 pm = config->metric_only ? print_metric_only_json : print_metric_json;
0567 nl = config->metric_only ? new_line_metric : new_line_json;
0568 } else {
0569 pm = config->metric_only ? print_metric_only : print_metric_std;
0570 nl = config->metric_only ? new_line_metric : new_line_std;
0571 }
0572
0573 if (!config->no_csv_summary && config->csv_output &&
0574 config->summary && !config->interval) {
0575 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
0576 }
0577
0578 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
0579 if (config->metric_only) {
0580 pm(config, &os, NULL, "", "", 0);
0581 return;
0582 }
0583 aggr_printout(config, counter, id, nr);
0584
0585 if (config->json_output) {
0586 fprintf(config->output, "\"counter-value\" : \"%s\", ",
0587 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED);
0588 } else {
0589 fprintf(config->output, "%*s%s",
0590 config->csv_output ? 0 : 18,
0591 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
0592 config->csv_sep);
0593 }
0594
0595 if (counter->supported) {
0596 if (!evlist__has_hybrid(counter->evlist)) {
0597 config->print_free_counters_hint = 1;
0598 if (is_mixed_hw_group(counter))
0599 config->print_mixed_hw_group_error = 1;
0600 }
0601 }
0602
0603 if (config->json_output) {
0604 fprintf(config->output, "\"unit\" : \"%s\", ", counter->unit);
0605 } else {
0606 fprintf(config->output, "%-*s%s",
0607 config->csv_output ? 0 : config->unit_width,
0608 counter->unit, config->csv_sep);
0609 }
0610
0611 if (config->json_output) {
0612 fprintf(config->output, "\"event\" : \"%s\", ",
0613 evsel__name(counter));
0614 } else {
0615 fprintf(config->output, "%*s",
0616 config->csv_output ? 0 : -25, evsel__name(counter));
0617 }
0618
0619 print_cgroup(config, counter);
0620
0621 if (!config->csv_output && !config->json_output)
0622 pm(config, &os, NULL, NULL, "", 0);
0623 print_noise(config, counter, noise);
0624 print_running(config, run, ena);
0625 if (config->csv_output)
0626 pm(config, &os, NULL, NULL, "", 0);
0627 else if (config->json_output)
0628 pm(config, &os, NULL, NULL, "", 0);
0629 return;
0630 }
0631
0632 if (!config->metric_only)
0633 abs_printout(config, id, nr, counter, uval);
0634
0635 out.print_metric = pm;
0636 out.new_line = nl;
0637 out.ctx = &os;
0638 out.force_header = false;
0639
0640 if (config->csv_output && !config->metric_only) {
0641 print_noise(config, counter, noise);
0642 print_running(config, run, ena);
0643 } else if (config->json_output && !config->metric_only) {
0644 print_noise(config, counter, noise);
0645 print_running(config, run, ena);
0646 }
0647
0648 perf_stat__print_shadow_stats(config, counter, uval,
0649 first_shadow_cpu_map_idx(config, counter, &id),
0650 &out, &config->metric_events, st);
0651 if (!config->csv_output && !config->metric_only && !config->json_output) {
0652 print_noise(config, counter, noise);
0653 print_running(config, run, ena);
0654 }
0655 }
0656
0657 static void aggr_update_shadow(struct perf_stat_config *config,
0658 struct evlist *evlist)
0659 {
0660 int idx, s;
0661 struct perf_cpu cpu;
0662 struct aggr_cpu_id s2, id;
0663 u64 val;
0664 struct evsel *counter;
0665 struct perf_cpu_map *cpus;
0666
0667 for (s = 0; s < config->aggr_map->nr; s++) {
0668 id = config->aggr_map->map[s];
0669 evlist__for_each_entry(evlist, counter) {
0670 cpus = evsel__cpus(counter);
0671 val = 0;
0672 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
0673 s2 = config->aggr_get_id(config, cpu);
0674 if (!aggr_cpu_id__equal(&s2, &id))
0675 continue;
0676 val += perf_counts(counter->counts, idx, 0)->val;
0677 }
0678 perf_stat__update_shadow_stats(counter, val,
0679 first_shadow_cpu_map_idx(config, counter, &id),
0680 &rt_stat);
0681 }
0682 }
0683 }
0684
0685 static void uniquify_event_name(struct evsel *counter)
0686 {
0687 char *new_name;
0688 char *config;
0689 int ret = 0;
0690
0691 if (counter->uniquified_name || counter->use_config_name ||
0692 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
0693 strlen(counter->pmu_name)))
0694 return;
0695
0696 config = strchr(counter->name, '/');
0697 if (config) {
0698 if (asprintf(&new_name,
0699 "%s%s", counter->pmu_name, config) > 0) {
0700 free(counter->name);
0701 counter->name = new_name;
0702 }
0703 } else {
0704 if (perf_pmu__has_hybrid()) {
0705 ret = asprintf(&new_name, "%s/%s/",
0706 counter->pmu_name, counter->name);
0707 } else {
0708 ret = asprintf(&new_name, "%s [%s]",
0709 counter->name, counter->pmu_name);
0710 }
0711
0712 if (ret) {
0713 free(counter->name);
0714 counter->name = new_name;
0715 }
0716 }
0717
0718 counter->uniquified_name = true;
0719 }
0720
0721 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
0722 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
0723 bool first),
0724 void *data)
0725 {
0726 struct evlist *evlist = counter->evlist;
0727 struct evsel *alias;
0728
0729 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
0730 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
0731
0732 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
0733 alias->scale == counter->scale &&
0734 alias->cgrp == counter->cgrp &&
0735 !strcmp(alias->unit, counter->unit) &&
0736 evsel__is_clock(alias) == evsel__is_clock(counter) &&
0737 strcmp(alias->pmu_name, counter->pmu_name)) {
0738 alias->merged_stat = true;
0739 cb(config, alias, data, false);
0740 }
0741 }
0742 }
0743
0744 static bool is_uncore(struct evsel *evsel)
0745 {
0746 struct perf_pmu *pmu = evsel__find_pmu(evsel);
0747
0748 return pmu && pmu->is_uncore;
0749 }
0750
0751 static bool hybrid_uniquify(struct evsel *evsel)
0752 {
0753 return perf_pmu__has_hybrid() && !is_uncore(evsel);
0754 }
0755
0756 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
0757 bool check)
0758 {
0759 if (hybrid_uniquify(counter)) {
0760 if (check)
0761 return config && config->hybrid_merge;
0762 else
0763 return config && !config->hybrid_merge;
0764 }
0765
0766 return false;
0767 }
0768
0769 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
0770 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
0771 bool first),
0772 void *data)
0773 {
0774 if (counter->merged_stat)
0775 return false;
0776 cb(config, counter, data, true);
0777 if (config->no_merge || hybrid_merge(counter, config, false))
0778 uniquify_event_name(counter);
0779 else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
0780 collect_all_aliases(config, counter, cb, data);
0781 return true;
0782 }
0783
0784 struct aggr_data {
0785 u64 ena, run, val;
0786 struct aggr_cpu_id id;
0787 int nr;
0788 int cpu_map_idx;
0789 };
0790
0791 static void aggr_cb(struct perf_stat_config *config,
0792 struct evsel *counter, void *data, bool first)
0793 {
0794 struct aggr_data *ad = data;
0795 int idx;
0796 struct perf_cpu cpu;
0797 struct perf_cpu_map *cpus;
0798 struct aggr_cpu_id s2;
0799
0800 cpus = evsel__cpus(counter);
0801 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
0802 struct perf_counts_values *counts;
0803
0804 s2 = config->aggr_get_id(config, cpu);
0805 if (!aggr_cpu_id__equal(&s2, &ad->id))
0806 continue;
0807 if (first)
0808 ad->nr++;
0809 counts = perf_counts(counter->counts, idx, 0);
0810
0811
0812
0813
0814 if (counts->ena == 0 || counts->run == 0 ||
0815 counter->counts->scaled == -1) {
0816 ad->ena = 0;
0817 ad->run = 0;
0818 break;
0819 }
0820 ad->val += counts->val;
0821 ad->ena += counts->ena;
0822 ad->run += counts->run;
0823 }
0824 }
0825
0826 static void print_counter_aggrdata(struct perf_stat_config *config,
0827 struct evsel *counter, int s,
0828 char *prefix, bool metric_only,
0829 bool *first, struct perf_cpu cpu)
0830 {
0831 struct aggr_data ad;
0832 FILE *output = config->output;
0833 u64 ena, run, val;
0834 int nr;
0835 struct aggr_cpu_id id;
0836 double uval;
0837
0838 ad.id = id = config->aggr_map->map[s];
0839 ad.val = ad.ena = ad.run = 0;
0840 ad.nr = 0;
0841 if (!collect_data(config, counter, aggr_cb, &ad))
0842 return;
0843
0844 if (perf_pmu__has_hybrid() && ad.ena == 0)
0845 return;
0846
0847 nr = ad.nr;
0848 ena = ad.ena;
0849 run = ad.run;
0850 val = ad.val;
0851 if (*first && metric_only) {
0852 *first = false;
0853 aggr_printout(config, counter, id, nr);
0854 }
0855 if (prefix && !metric_only)
0856 fprintf(output, "%s", prefix);
0857
0858 uval = val * counter->scale;
0859 if (cpu.cpu != -1)
0860 id = aggr_cpu_id__cpu(cpu, NULL);
0861
0862 printout(config, id, nr, counter, uval,
0863 prefix, run, ena, 1.0, &rt_stat);
0864 if (!metric_only)
0865 fputc('\n', output);
0866 }
0867
0868 static void print_aggr(struct perf_stat_config *config,
0869 struct evlist *evlist,
0870 char *prefix)
0871 {
0872 bool metric_only = config->metric_only;
0873 FILE *output = config->output;
0874 struct evsel *counter;
0875 int s;
0876 bool first;
0877
0878 if (!config->aggr_map || !config->aggr_get_id)
0879 return;
0880
0881 aggr_update_shadow(config, evlist);
0882
0883
0884
0885
0886
0887 for (s = 0; s < config->aggr_map->nr; s++) {
0888 if (prefix && metric_only)
0889 fprintf(output, "%s", prefix);
0890
0891 first = true;
0892 evlist__for_each_entry(evlist, counter) {
0893 print_counter_aggrdata(config, counter, s,
0894 prefix, metric_only,
0895 &first, (struct perf_cpu){ .cpu = -1 });
0896 }
0897 if (metric_only)
0898 fputc('\n', output);
0899 }
0900 }
0901
0902 static int cmp_val(const void *a, const void *b)
0903 {
0904 return ((struct perf_aggr_thread_value *)b)->val -
0905 ((struct perf_aggr_thread_value *)a)->val;
0906 }
0907
0908 static struct perf_aggr_thread_value *sort_aggr_thread(
0909 struct evsel *counter,
0910 int *ret,
0911 struct target *_target)
0912 {
0913 int nthreads = perf_thread_map__nr(counter->core.threads);
0914 int i = 0;
0915 double uval;
0916 struct perf_aggr_thread_value *buf;
0917
0918 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
0919 if (!buf)
0920 return NULL;
0921
0922 for (int thread = 0; thread < nthreads; thread++) {
0923 int idx;
0924 u64 ena = 0, run = 0, val = 0;
0925
0926 perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
0927 struct perf_counts_values *counts =
0928 perf_counts(counter->counts, idx, thread);
0929
0930 val += counts->val;
0931 ena += counts->ena;
0932 run += counts->run;
0933 }
0934
0935 uval = val * counter->scale;
0936
0937
0938
0939
0940
0941 if (uval == 0.0 && target__has_per_thread(_target))
0942 continue;
0943
0944 buf[i].counter = counter;
0945 buf[i].id = aggr_cpu_id__empty();
0946 buf[i].id.thread = thread;
0947 buf[i].uval = uval;
0948 buf[i].val = val;
0949 buf[i].run = run;
0950 buf[i].ena = ena;
0951 i++;
0952 }
0953
0954 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
0955
0956 if (ret)
0957 *ret = i;
0958
0959 return buf;
0960 }
0961
0962 static void print_aggr_thread(struct perf_stat_config *config,
0963 struct target *_target,
0964 struct evsel *counter, char *prefix)
0965 {
0966 FILE *output = config->output;
0967 int thread, sorted_threads;
0968 struct aggr_cpu_id id;
0969 struct perf_aggr_thread_value *buf;
0970
0971 buf = sort_aggr_thread(counter, &sorted_threads, _target);
0972 if (!buf) {
0973 perror("cannot sort aggr thread");
0974 return;
0975 }
0976
0977 for (thread = 0; thread < sorted_threads; thread++) {
0978 if (prefix)
0979 fprintf(output, "%s", prefix);
0980
0981 id = buf[thread].id;
0982 if (config->stats)
0983 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
0984 prefix, buf[thread].run, buf[thread].ena, 1.0,
0985 &config->stats[id.thread]);
0986 else
0987 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
0988 prefix, buf[thread].run, buf[thread].ena, 1.0,
0989 &rt_stat);
0990 fputc('\n', output);
0991 }
0992
0993 free(buf);
0994 }
0995
0996 struct caggr_data {
0997 double avg, avg_enabled, avg_running;
0998 };
0999
1000 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
1001 struct evsel *counter, void *data,
1002 bool first __maybe_unused)
1003 {
1004 struct caggr_data *cd = data;
1005 struct perf_counts_values *aggr = &counter->counts->aggr;
1006
1007 cd->avg += aggr->val;
1008 cd->avg_enabled += aggr->ena;
1009 cd->avg_running += aggr->run;
1010 }
1011
1012
1013
1014
1015
1016 static void print_counter_aggr(struct perf_stat_config *config,
1017 struct evsel *counter, char *prefix)
1018 {
1019 bool metric_only = config->metric_only;
1020 FILE *output = config->output;
1021 double uval;
1022 struct caggr_data cd = { .avg = 0.0 };
1023
1024 if (!collect_data(config, counter, counter_aggr_cb, &cd))
1025 return;
1026
1027 if (prefix && !metric_only)
1028 fprintf(output, "%s", prefix);
1029
1030 uval = cd.avg * counter->scale;
1031 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
1032 cd.avg_enabled, cd.avg, &rt_stat);
1033 if (!metric_only)
1034 fprintf(output, "\n");
1035 }
1036
1037 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1038 struct evsel *counter, void *data,
1039 bool first __maybe_unused)
1040 {
1041 struct aggr_data *ad = data;
1042
1043 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
1044 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
1045 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
1046 }
1047
1048
1049
1050
1051
1052 static void print_counter(struct perf_stat_config *config,
1053 struct evsel *counter, char *prefix)
1054 {
1055 FILE *output = config->output;
1056 u64 ena, run, val;
1057 double uval;
1058 int idx;
1059 struct perf_cpu cpu;
1060 struct aggr_cpu_id id;
1061
1062 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
1063 struct aggr_data ad = { .cpu_map_idx = idx };
1064
1065 if (!collect_data(config, counter, counter_cb, &ad))
1066 return;
1067 val = ad.val;
1068 ena = ad.ena;
1069 run = ad.run;
1070
1071 if (prefix)
1072 fprintf(output, "%s", prefix);
1073
1074 uval = val * counter->scale;
1075 id = aggr_cpu_id__cpu(cpu, NULL);
1076 printout(config, id, 0, counter, uval, prefix,
1077 run, ena, 1.0, &rt_stat);
1078
1079 fputc('\n', output);
1080 }
1081 }
1082
1083 static void print_no_aggr_metric(struct perf_stat_config *config,
1084 struct evlist *evlist,
1085 char *prefix)
1086 {
1087 int all_idx;
1088 struct perf_cpu cpu;
1089
1090 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1091 struct evsel *counter;
1092 bool first = true;
1093
1094 evlist__for_each_entry(evlist, counter) {
1095 u64 ena, run, val;
1096 double uval;
1097 struct aggr_cpu_id id;
1098 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1099
1100 if (counter_idx < 0)
1101 continue;
1102
1103 id = aggr_cpu_id__cpu(cpu, NULL);
1104 if (first) {
1105 if (prefix)
1106 fputs(prefix, config->output);
1107 aggr_printout(config, counter, id, 0);
1108 first = false;
1109 }
1110 val = perf_counts(counter->counts, counter_idx, 0)->val;
1111 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
1112 run = perf_counts(counter->counts, counter_idx, 0)->run;
1113
1114 uval = val * counter->scale;
1115 printout(config, id, 0, counter, uval, prefix,
1116 run, ena, 1.0, &rt_stat);
1117 }
1118 if (!first)
1119 fputc('\n', config->output);
1120 }
1121 }
1122
1123 static int aggr_header_lens[] = {
1124 [AGGR_CORE] = 24,
1125 [AGGR_DIE] = 18,
1126 [AGGR_SOCKET] = 12,
1127 [AGGR_NONE] = 6,
1128 [AGGR_THREAD] = 24,
1129 [AGGR_GLOBAL] = 0,
1130 };
1131
1132 static const char *aggr_header_csv[] = {
1133 [AGGR_CORE] = "core,cpus,",
1134 [AGGR_DIE] = "die,cpus",
1135 [AGGR_SOCKET] = "socket,cpus",
1136 [AGGR_NONE] = "cpu,",
1137 [AGGR_THREAD] = "comm-pid,",
1138 [AGGR_GLOBAL] = ""
1139 };
1140
1141 static void print_metric_headers(struct perf_stat_config *config,
1142 struct evlist *evlist,
1143 const char *prefix, bool no_indent)
1144 {
1145 struct perf_stat_output_ctx out;
1146 struct evsel *counter;
1147 struct outstate os = {
1148 .fh = config->output
1149 };
1150 bool first = true;
1151
1152 if (config->json_output && !config->interval)
1153 fprintf(config->output, "{");
1154
1155 if (prefix && !config->json_output)
1156 fprintf(config->output, "%s", prefix);
1157
1158 if (!config->csv_output && !no_indent)
1159 fprintf(config->output, "%*s",
1160 aggr_header_lens[config->aggr_mode], "");
1161 if (config->csv_output) {
1162 if (config->interval)
1163 fputs("time,", config->output);
1164 if (!config->iostat_run)
1165 fputs(aggr_header_csv[config->aggr_mode], config->output);
1166 }
1167 if (config->iostat_run)
1168 iostat_print_header_prefix(config);
1169
1170
1171 evlist__for_each_entry(evlist, counter) {
1172 os.evsel = counter;
1173 out.ctx = &os;
1174 out.print_metric = print_metric_header;
1175 if (!first && config->json_output)
1176 fprintf(config->output, ", ");
1177 first = false;
1178 out.new_line = new_line_metric;
1179 out.force_header = true;
1180 perf_stat__print_shadow_stats(config, counter, 0,
1181 0,
1182 &out,
1183 &config->metric_events,
1184 &rt_stat);
1185 }
1186 if (config->json_output)
1187 fprintf(config->output, "}");
1188 fputc('\n', config->output);
1189 }
1190
1191 static void print_interval(struct perf_stat_config *config,
1192 struct evlist *evlist,
1193 char *prefix, struct timespec *ts)
1194 {
1195 bool metric_only = config->metric_only;
1196 unsigned int unit_width = config->unit_width;
1197 FILE *output = config->output;
1198 static int num_print_interval;
1199
1200 if (config->interval_clear)
1201 puts(CONSOLE_CLEAR);
1202
1203 if (!config->iostat_run && !config->json_output)
1204 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1205 ts->tv_nsec, config->csv_sep);
1206 if (!config->iostat_run && config->json_output && !config->metric_only)
1207 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1208 ts->tv_sec, ts->tv_nsec);
1209 if (!config->iostat_run && config->json_output && config->metric_only)
1210 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1211 ts->tv_sec, ts->tv_nsec);
1212
1213 if ((num_print_interval == 0 && !config->csv_output && !config->json_output)
1214 || config->interval_clear) {
1215 switch (config->aggr_mode) {
1216 case AGGR_NODE:
1217 fprintf(output, "# time node cpus");
1218 if (!metric_only)
1219 fprintf(output, " counts %*s events\n", unit_width, "unit");
1220 break;
1221 case AGGR_SOCKET:
1222 fprintf(output, "# time socket cpus");
1223 if (!metric_only)
1224 fprintf(output, " counts %*s events\n", unit_width, "unit");
1225 break;
1226 case AGGR_DIE:
1227 fprintf(output, "# time die cpus");
1228 if (!metric_only)
1229 fprintf(output, " counts %*s events\n", unit_width, "unit");
1230 break;
1231 case AGGR_CORE:
1232 fprintf(output, "# time core cpus");
1233 if (!metric_only)
1234 fprintf(output, " counts %*s events\n", unit_width, "unit");
1235 break;
1236 case AGGR_NONE:
1237 fprintf(output, "# time CPU ");
1238 if (!metric_only)
1239 fprintf(output, " counts %*s events\n", unit_width, "unit");
1240 break;
1241 case AGGR_THREAD:
1242 fprintf(output, "# time comm-pid");
1243 if (!metric_only)
1244 fprintf(output, " counts %*s events\n", unit_width, "unit");
1245 break;
1246 case AGGR_GLOBAL:
1247 default:
1248 if (!config->iostat_run) {
1249 fprintf(output, "# time");
1250 if (!metric_only)
1251 fprintf(output, " counts %*s events\n", unit_width, "unit");
1252 }
1253 case AGGR_UNSET:
1254 case AGGR_MAX:
1255 break;
1256 }
1257 }
1258
1259 if ((num_print_interval == 0 || config->interval_clear)
1260 && metric_only && !config->json_output)
1261 print_metric_headers(config, evlist, " ", true);
1262 if ((num_print_interval == 0 || config->interval_clear)
1263 && metric_only && config->json_output) {
1264 fprintf(output, "{");
1265 print_metric_headers(config, evlist, " ", true);
1266 }
1267 if (++num_print_interval == 25)
1268 num_print_interval = 0;
1269 }
1270
1271 static void print_header(struct perf_stat_config *config,
1272 struct target *_target,
1273 int argc, const char **argv)
1274 {
1275 FILE *output = config->output;
1276 int i;
1277
1278 fflush(stdout);
1279
1280 if (!config->csv_output && !config->json_output) {
1281 fprintf(output, "\n");
1282 fprintf(output, " Performance counter stats for ");
1283 if (_target->bpf_str)
1284 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1285 else if (_target->system_wide)
1286 fprintf(output, "\'system wide");
1287 else if (_target->cpu_list)
1288 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1289 else if (!target__has_task(_target)) {
1290 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1291 for (i = 1; argv && (i < argc); i++)
1292 fprintf(output, " %s", argv[i]);
1293 } else if (_target->pid)
1294 fprintf(output, "process id \'%s", _target->pid);
1295 else
1296 fprintf(output, "thread id \'%s", _target->tid);
1297
1298 fprintf(output, "\'");
1299 if (config->run_count > 1)
1300 fprintf(output, " (%d runs)", config->run_count);
1301 fprintf(output, ":\n\n");
1302 }
1303 }
1304
1305 static int get_precision(double num)
1306 {
1307 if (num > 1)
1308 return 0;
1309
1310 return lround(ceil(-log10(num)));
1311 }
1312
1313 static void print_table(struct perf_stat_config *config,
1314 FILE *output, int precision, double avg)
1315 {
1316 char tmp[64];
1317 int idx, indent = 0;
1318
1319 scnprintf(tmp, 64, " %17.*f", precision, avg);
1320 while (tmp[indent] == ' ')
1321 indent++;
1322
1323 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1324
1325 for (idx = 0; idx < config->run_count; idx++) {
1326 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1327 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1328
1329 fprintf(output, " %17.*f (%+.*f) ",
1330 precision, run, precision, run - avg);
1331
1332 for (h = 0; h < n; h++)
1333 fprintf(output, "#");
1334
1335 fprintf(output, "\n");
1336 }
1337
1338 fprintf(output, "\n%*s# Final result:\n", indent, "");
1339 }
1340
1341 static double timeval2double(struct timeval *t)
1342 {
1343 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1344 }
1345
1346 static void print_footer(struct perf_stat_config *config)
1347 {
1348 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1349 FILE *output = config->output;
1350
1351 if (!config->null_run)
1352 fprintf(output, "\n");
1353
1354 if (config->run_count == 1) {
1355 fprintf(output, " %17.9f seconds time elapsed", avg);
1356
1357 if (config->ru_display) {
1358 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1359 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1360
1361 fprintf(output, "\n\n");
1362 fprintf(output, " %17.9f seconds user\n", ru_utime);
1363 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1364 }
1365 } else {
1366 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1367
1368
1369
1370
1371 int precision = get_precision(sd) + 2;
1372
1373 if (config->walltime_run_table)
1374 print_table(config, output, precision, avg);
1375
1376 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1377 precision, avg, precision, sd);
1378
1379 print_noise_pct(config, sd, avg);
1380 }
1381 fprintf(output, "\n\n");
1382
1383 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1384 fprintf(output,
1385 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1386 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1387 " perf stat ...\n"
1388 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1389
1390 if (config->print_mixed_hw_group_error)
1391 fprintf(output,
1392 "The events in group usually have to be from "
1393 "the same PMU. Try reorganizing the group.\n");
1394 }
1395
1396 static void print_percore_thread(struct perf_stat_config *config,
1397 struct evsel *counter, char *prefix)
1398 {
1399 int s;
1400 struct aggr_cpu_id s2, id;
1401 struct perf_cpu_map *cpus;
1402 bool first = true;
1403 int idx;
1404 struct perf_cpu cpu;
1405
1406 cpus = evsel__cpus(counter);
1407 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1408 s2 = config->aggr_get_id(config, cpu);
1409 for (s = 0; s < config->aggr_map->nr; s++) {
1410 id = config->aggr_map->map[s];
1411 if (aggr_cpu_id__equal(&s2, &id))
1412 break;
1413 }
1414
1415 print_counter_aggrdata(config, counter, s,
1416 prefix, false,
1417 &first, cpu);
1418 }
1419 }
1420
1421 static void print_percore(struct perf_stat_config *config,
1422 struct evsel *counter, char *prefix)
1423 {
1424 bool metric_only = config->metric_only;
1425 FILE *output = config->output;
1426 int s;
1427 bool first = true;
1428
1429 if (!config->aggr_map || !config->aggr_get_id)
1430 return;
1431
1432 if (config->percore_show_thread)
1433 return print_percore_thread(config, counter, prefix);
1434
1435 for (s = 0; s < config->aggr_map->nr; s++) {
1436 if (prefix && metric_only)
1437 fprintf(output, "%s", prefix);
1438
1439 print_counter_aggrdata(config, counter, s,
1440 prefix, metric_only,
1441 &first, (struct perf_cpu){ .cpu = -1 });
1442 }
1443
1444 if (metric_only)
1445 fputc('\n', output);
1446 }
1447
1448 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1449 struct target *_target, struct timespec *ts, int argc, const char **argv)
1450 {
1451 bool metric_only = config->metric_only;
1452 int interval = config->interval;
1453 struct evsel *counter;
1454 char buf[64], *prefix = NULL;
1455
1456 if (config->iostat_run)
1457 evlist->selected = evlist__first(evlist);
1458
1459 if (interval)
1460 print_interval(config, evlist, prefix = buf, ts);
1461 else
1462 print_header(config, _target, argc, argv);
1463
1464 if (metric_only) {
1465 static int num_print_iv;
1466
1467 if (num_print_iv == 0 && !interval)
1468 print_metric_headers(config, evlist, prefix, false);
1469 if (num_print_iv++ == 25)
1470 num_print_iv = 0;
1471 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1472 fprintf(config->output, "%s", prefix);
1473
1474 if (config->json_output && !config->metric_only)
1475 fprintf(config->output, "}");
1476 }
1477
1478 switch (config->aggr_mode) {
1479 case AGGR_CORE:
1480 case AGGR_DIE:
1481 case AGGR_SOCKET:
1482 case AGGR_NODE:
1483 print_aggr(config, evlist, prefix);
1484 break;
1485 case AGGR_THREAD:
1486 evlist__for_each_entry(evlist, counter) {
1487 print_aggr_thread(config, _target, counter, prefix);
1488 }
1489 break;
1490 case AGGR_GLOBAL:
1491 if (config->iostat_run)
1492 iostat_print_counters(evlist, config, ts, prefix = buf,
1493 print_counter_aggr);
1494 else {
1495 evlist__for_each_entry(evlist, counter) {
1496 print_counter_aggr(config, counter, prefix);
1497 }
1498 if (metric_only)
1499 fputc('\n', config->output);
1500 }
1501 break;
1502 case AGGR_NONE:
1503 if (metric_only)
1504 print_no_aggr_metric(config, evlist, prefix);
1505 else {
1506 evlist__for_each_entry(evlist, counter) {
1507 if (counter->percore)
1508 print_percore(config, counter, prefix);
1509 else
1510 print_counter(config, counter, prefix);
1511 }
1512 }
1513 break;
1514 case AGGR_MAX:
1515 case AGGR_UNSET:
1516 default:
1517 break;
1518 }
1519
1520 if (!interval && !config->csv_output && !config->json_output)
1521 print_footer(config);
1522
1523 fflush(config->output);
1524 }