1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <inttypes.h> 4 #include <linux/string.h> 5 #include <linux/time64.h> 6 #include <math.h> 7 #include <perf/cpumap.h> 8 #include "color.h" 9 #include "counts.h" 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" 17 #include <linux/ctype.h> 18 #include "cgroup.h" 19 #include <api/fs/fs.h> 20 #include "util.h" 21 #include "iostat.h" 22 #include "pmu-hybrid.h" 23 #include "evlist-hybrid.h" 24 25 #define CNTR_NOT_SUPPORTED "<not supported>" 26 #define CNTR_NOT_COUNTED "<not counted>" 27 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, 54 struct evsel *evsel, double avg) 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 65 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel) 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 74 static void aggr_printout(struct perf_stat_config *config, 75 struct evsel *evsel, struct aggr_cpu_id id, int nr) 76 { 77 switch (config->aggr_mode) { 78 case AGGR_CORE: 79 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s", 80 id.socket, 81 id.die, 82 config->csv_output ? 0 : -8, 83 id.core, 84 config->csv_sep, 85 config->csv_output ? 0 : 4, 86 nr, 87 config->csv_sep); 88 break; 89 case AGGR_DIE: 90 fprintf(config->output, "S%d-D%*d%s%*d%s", 91 id.socket, 92 config->csv_output ? 0 : -8, 93 id.die, 94 config->csv_sep, 95 config->csv_output ? 0 : 4, 96 nr, 97 config->csv_sep); 98 break; 99 case AGGR_SOCKET: 100 fprintf(config->output, "S%*d%s%*d%s", 101 config->csv_output ? 0 : -5, 102 id.socket, 103 config->csv_sep, 104 config->csv_output ? 0 : 4, 105 nr, 106 config->csv_sep); 107 break; 108 case AGGR_NODE: 109 fprintf(config->output, "N%*d%s%*d%s", 110 config->csv_output ? 0 : -5, 111 id.node, 112 config->csv_sep, 113 config->csv_output ? 0 : 4, 114 nr, 115 config->csv_sep); 116 break; 117 case AGGR_NONE: 118 if (evsel->percore && !config->percore_show_thread) { 119 fprintf(config->output, "S%d-D%d-C%*d%s", 120 id.socket, 121 id.die, 122 config->csv_output ? 0 : -3, 123 id.core, config->csv_sep); 124 } else if (id.cpu > -1) { 125 fprintf(config->output, "CPU%*d%s", 126 config->csv_output ? 0 : -7, 127 id.cpu, config->csv_sep); 128 } 129 break; 130 case AGGR_THREAD: 131 fprintf(config->output, "%*s-%*d%s", 132 config->csv_output ? 0 : 16, 133 perf_thread_map__comm(evsel->core.threads, id.thread), 134 config->csv_output ? 0 : -8, 135 perf_thread_map__pid(evsel->core.threads, id.thread), 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; 150 int nr; 151 struct aggr_cpu_id id; 152 struct evsel *evsel; 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); 230 ends = vals = skip_spaces(buf); 231 while (isdigit(*ends) || *ends == '.') 232 ends++; 233 *ends = 0; 234 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit)); 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") || 244 strstr(unit, "CPUs utilized")) 245 return false; 246 return true; 247 } 248 249 static const char *fixunit(char *buf, struct evsel *evsel, 250 const char *unit) 251 { 252 if (!strncmp(unit, "of all", 6)) { 253 snprintf(buf, 1024, "%s %s", evsel__name(evsel), 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); 296 ends = vals = skip_spaces(buf); 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 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 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 330 static int first_shadow_cpu(struct perf_stat_config *config, 331 struct evsel *evsel, const struct aggr_cpu_id *id) 332 { 333 struct perf_cpu_map *cpus; 334 int cpu, idx; 335 336 if (config->aggr_mode == AGGR_NONE) 337 return id->cpu; 338 339 if (!config->aggr_get_id) 340 return 0; 341 342 cpus = evsel__cpus(evsel); 343 perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 344 struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu); 345 346 if (aggr_cpu_id__equal(&cpu_id, id)) 347 return cpu; 348 } 349 return 0; 350 } 351 352 static void abs_printout(struct perf_stat_config *config, 353 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg) 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 377 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel)); 378 379 print_cgroup(config, evsel); 380 } 381 382 static bool is_mixed_hw_group(struct evsel *counter) 383 { 384 struct evlist *evlist = counter->evlist; 385 u32 pmu_type = counter->core.attr.type; 386 struct evsel *pos; 387 388 if (counter->core.nr_members < 2) 389 return false; 390 391 evlist__for_each_entry(evlist, pos) { 392 /* software events can be part of any hardware group */ 393 if (pos->core.attr.type == PERF_TYPE_SOFTWARE) 394 continue; 395 if (pmu_type == PERF_TYPE_SOFTWARE) { 396 pmu_type = pos->core.attr.type; 397 continue; 398 } 399 if (pmu_type != pos->core.attr.type) 400 return true; 401 } 402 403 return false; 404 } 405 406 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr, 407 struct evsel *counter, double uval, 408 char *prefix, u64 run, u64 ena, double noise, 409 struct runtime_stat *st) 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, 437 [AGGR_DIE] = 2, 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 } 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 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) { 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 } 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", 479 config->csv_output ? 0 : -25, evsel__name(counter)); 480 481 print_cgroup(config, counter); 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, 506 first_shadow_cpu(config, counter, &id), 507 &out, &config->metric_events, st); 508 if (!config->csv_output && !config->metric_only) { 509 print_noise(config, counter, noise); 510 print_running(config, run, ena); 511 } 512 } 513 514 static void aggr_update_shadow(struct perf_stat_config *config, 515 struct evlist *evlist) 516 { 517 int cpu, idx, s; 518 struct aggr_cpu_id s2, id; 519 u64 val; 520 struct evsel *counter; 521 struct perf_cpu_map *cpus; 522 523 for (s = 0; s < config->aggr_map->nr; s++) { 524 id = config->aggr_map->map[s]; 525 evlist__for_each_entry(evlist, counter) { 526 cpus = evsel__cpus(counter); 527 val = 0; 528 perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 529 s2 = config->aggr_get_id(config, cpu); 530 if (!aggr_cpu_id__equal(&s2, &id)) 531 continue; 532 val += perf_counts(counter->counts, idx, 0)->val; 533 } 534 perf_stat__update_shadow_stats(counter, val, 535 first_shadow_cpu(config, counter, &id), 536 &rt_stat); 537 } 538 } 539 } 540 541 static void uniquify_event_name(struct evsel *counter) 542 { 543 char *new_name; 544 char *config; 545 int ret = 0; 546 547 if (counter->uniquified_name || counter->use_config_name || 548 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name, 549 strlen(counter->pmu_name))) 550 return; 551 552 config = strchr(counter->name, '/'); 553 if (config) { 554 if (asprintf(&new_name, 555 "%s%s", counter->pmu_name, config) > 0) { 556 free(counter->name); 557 counter->name = new_name; 558 } 559 } else { 560 if (perf_pmu__has_hybrid()) { 561 ret = asprintf(&new_name, "%s/%s/", 562 counter->pmu_name, counter->name); 563 } else { 564 ret = asprintf(&new_name, "%s [%s]", 565 counter->name, counter->pmu_name); 566 } 567 568 if (ret) { 569 free(counter->name); 570 counter->name = new_name; 571 } 572 } 573 574 counter->uniquified_name = true; 575 } 576 577 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter, 578 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data, 579 bool first), 580 void *data) 581 { 582 struct evlist *evlist = counter->evlist; 583 struct evsel *alias; 584 585 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node); 586 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) { 587 if (strcmp(evsel__name(alias), evsel__name(counter)) || 588 alias->scale != counter->scale || 589 alias->cgrp != counter->cgrp || 590 strcmp(alias->unit, counter->unit) || 591 evsel__is_clock(alias) != evsel__is_clock(counter) || 592 !strcmp(alias->pmu_name, counter->pmu_name)) 593 break; 594 alias->merged_stat = true; 595 cb(config, alias, data, false); 596 } 597 } 598 599 static bool is_uncore(struct evsel *evsel) 600 { 601 struct perf_pmu *pmu = evsel__find_pmu(evsel); 602 603 return pmu && pmu->is_uncore; 604 } 605 606 static bool hybrid_uniquify(struct evsel *evsel) 607 { 608 return perf_pmu__has_hybrid() && !is_uncore(evsel); 609 } 610 611 static bool collect_data(struct perf_stat_config *config, struct evsel *counter, 612 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data, 613 bool first), 614 void *data) 615 { 616 if (counter->merged_stat) 617 return false; 618 cb(config, counter, data, true); 619 if (config->no_merge || hybrid_uniquify(counter)) 620 uniquify_event_name(counter); 621 else if (counter->auto_merge_stats) 622 collect_all_aliases(config, counter, cb, data); 623 return true; 624 } 625 626 struct aggr_data { 627 u64 ena, run, val; 628 struct aggr_cpu_id id; 629 int nr; 630 int cpu_map_idx; 631 }; 632 633 static void aggr_cb(struct perf_stat_config *config, 634 struct evsel *counter, void *data, bool first) 635 { 636 struct aggr_data *ad = data; 637 int idx, cpu; 638 struct perf_cpu_map *cpus; 639 struct aggr_cpu_id s2; 640 641 cpus = evsel__cpus(counter); 642 perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 643 struct perf_counts_values *counts; 644 645 s2 = config->aggr_get_id(config, cpu); 646 if (!aggr_cpu_id__equal(&s2, &ad->id)) 647 continue; 648 if (first) 649 ad->nr++; 650 counts = perf_counts(counter->counts, idx, 0); 651 /* 652 * When any result is bad, make them all to give 653 * consistent output in interval mode. 654 */ 655 if (counts->ena == 0 || counts->run == 0 || 656 counter->counts->scaled == -1) { 657 ad->ena = 0; 658 ad->run = 0; 659 break; 660 } 661 ad->val += counts->val; 662 ad->ena += counts->ena; 663 ad->run += counts->run; 664 } 665 } 666 667 static void print_counter_aggrdata(struct perf_stat_config *config, 668 struct evsel *counter, int s, 669 char *prefix, bool metric_only, 670 bool *first, int cpu) 671 { 672 struct aggr_data ad; 673 FILE *output = config->output; 674 u64 ena, run, val; 675 int nr; 676 struct aggr_cpu_id id; 677 double uval; 678 679 ad.id = id = config->aggr_map->map[s]; 680 ad.val = ad.ena = ad.run = 0; 681 ad.nr = 0; 682 if (!collect_data(config, counter, aggr_cb, &ad)) 683 return; 684 685 if (perf_pmu__has_hybrid() && ad.ena == 0) 686 return; 687 688 nr = ad.nr; 689 ena = ad.ena; 690 run = ad.run; 691 val = ad.val; 692 if (*first && metric_only) { 693 *first = false; 694 aggr_printout(config, counter, id, nr); 695 } 696 if (prefix && !metric_only) 697 fprintf(output, "%s", prefix); 698 699 uval = val * counter->scale; 700 if (cpu != -1) 701 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); 702 703 printout(config, id, nr, counter, uval, 704 prefix, run, ena, 1.0, &rt_stat); 705 if (!metric_only) 706 fputc('\n', output); 707 } 708 709 static void print_aggr(struct perf_stat_config *config, 710 struct evlist *evlist, 711 char *prefix) 712 { 713 bool metric_only = config->metric_only; 714 FILE *output = config->output; 715 struct evsel *counter; 716 int s; 717 bool first; 718 719 if (!config->aggr_map || !config->aggr_get_id) 720 return; 721 722 aggr_update_shadow(config, evlist); 723 724 /* 725 * With metric_only everything is on a single line. 726 * Without each counter has its own line. 727 */ 728 for (s = 0; s < config->aggr_map->nr; s++) { 729 if (prefix && metric_only) 730 fprintf(output, "%s", prefix); 731 732 first = true; 733 evlist__for_each_entry(evlist, counter) { 734 print_counter_aggrdata(config, counter, s, 735 prefix, metric_only, 736 &first, /*cpu=*/-1); 737 } 738 if (metric_only) 739 fputc('\n', output); 740 } 741 } 742 743 static int cmp_val(const void *a, const void *b) 744 { 745 return ((struct perf_aggr_thread_value *)b)->val - 746 ((struct perf_aggr_thread_value *)a)->val; 747 } 748 749 static struct perf_aggr_thread_value *sort_aggr_thread( 750 struct evsel *counter, 751 int nthreads, int ncpus, 752 int *ret, 753 struct target *_target) 754 { 755 int cpu, thread, i = 0; 756 double uval; 757 struct perf_aggr_thread_value *buf; 758 759 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value)); 760 if (!buf) 761 return NULL; 762 763 for (thread = 0; thread < nthreads; thread++) { 764 u64 ena = 0, run = 0, val = 0; 765 766 for (cpu = 0; cpu < ncpus; cpu++) { 767 val += perf_counts(counter->counts, cpu, thread)->val; 768 ena += perf_counts(counter->counts, cpu, thread)->ena; 769 run += perf_counts(counter->counts, cpu, thread)->run; 770 } 771 772 uval = val * counter->scale; 773 774 /* 775 * Skip value 0 when enabling --per-thread globally, 776 * otherwise too many 0 output. 777 */ 778 if (uval == 0.0 && target__has_per_thread(_target)) 779 continue; 780 781 buf[i].counter = counter; 782 buf[i].id = aggr_cpu_id__empty(); 783 buf[i].id.thread = thread; 784 buf[i].uval = uval; 785 buf[i].val = val; 786 buf[i].run = run; 787 buf[i].ena = ena; 788 i++; 789 } 790 791 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val); 792 793 if (ret) 794 *ret = i; 795 796 return buf; 797 } 798 799 static void print_aggr_thread(struct perf_stat_config *config, 800 struct target *_target, 801 struct evsel *counter, char *prefix) 802 { 803 FILE *output = config->output; 804 int nthreads = perf_thread_map__nr(counter->core.threads); 805 int ncpus = perf_cpu_map__nr(counter->core.cpus); 806 int thread, sorted_threads; 807 struct aggr_cpu_id id; 808 struct perf_aggr_thread_value *buf; 809 810 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target); 811 if (!buf) { 812 perror("cannot sort aggr thread"); 813 return; 814 } 815 816 for (thread = 0; thread < sorted_threads; thread++) { 817 if (prefix) 818 fprintf(output, "%s", prefix); 819 820 id = buf[thread].id; 821 if (config->stats) 822 printout(config, id, 0, buf[thread].counter, buf[thread].uval, 823 prefix, buf[thread].run, buf[thread].ena, 1.0, 824 &config->stats[id.thread]); 825 else 826 printout(config, id, 0, buf[thread].counter, buf[thread].uval, 827 prefix, buf[thread].run, buf[thread].ena, 1.0, 828 &rt_stat); 829 fputc('\n', output); 830 } 831 832 free(buf); 833 } 834 835 struct caggr_data { 836 double avg, avg_enabled, avg_running; 837 }; 838 839 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused, 840 struct evsel *counter, void *data, 841 bool first __maybe_unused) 842 { 843 struct caggr_data *cd = data; 844 struct perf_counts_values *aggr = &counter->counts->aggr; 845 846 cd->avg += aggr->val; 847 cd->avg_enabled += aggr->ena; 848 cd->avg_running += aggr->run; 849 } 850 851 /* 852 * Print out the results of a single counter: 853 * aggregated counts in system-wide mode 854 */ 855 static void print_counter_aggr(struct perf_stat_config *config, 856 struct evsel *counter, char *prefix) 857 { 858 bool metric_only = config->metric_only; 859 FILE *output = config->output; 860 double uval; 861 struct caggr_data cd = { .avg = 0.0 }; 862 863 if (!collect_data(config, counter, counter_aggr_cb, &cd)) 864 return; 865 866 if (prefix && !metric_only) 867 fprintf(output, "%s", prefix); 868 869 uval = cd.avg * counter->scale; 870 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running, 871 cd.avg_enabled, cd.avg, &rt_stat); 872 if (!metric_only) 873 fprintf(output, "\n"); 874 } 875 876 static void counter_cb(struct perf_stat_config *config __maybe_unused, 877 struct evsel *counter, void *data, 878 bool first __maybe_unused) 879 { 880 struct aggr_data *ad = data; 881 882 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val; 883 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena; 884 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run; 885 } 886 887 /* 888 * Print out the results of a single counter: 889 * does not use aggregated count in system-wide 890 */ 891 static void print_counter(struct perf_stat_config *config, 892 struct evsel *counter, char *prefix) 893 { 894 FILE *output = config->output; 895 u64 ena, run, val; 896 double uval; 897 int idx, cpu; 898 struct aggr_cpu_id id; 899 900 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) { 901 struct aggr_data ad = { .cpu_map_idx = idx }; 902 903 if (!collect_data(config, counter, counter_cb, &ad)) 904 return; 905 val = ad.val; 906 ena = ad.ena; 907 run = ad.run; 908 909 if (prefix) 910 fprintf(output, "%s", prefix); 911 912 uval = val * counter->scale; 913 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); 914 printout(config, id, 0, counter, uval, prefix, 915 run, ena, 1.0, &rt_stat); 916 917 fputc('\n', output); 918 } 919 } 920 921 static void print_no_aggr_metric(struct perf_stat_config *config, 922 struct evlist *evlist, 923 char *prefix) 924 { 925 int all_idx, cpu; 926 927 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.cpus) { 928 struct evsel *counter; 929 bool first = true; 930 931 if (prefix) 932 fputs(prefix, config->output); 933 evlist__for_each_entry(evlist, counter) { 934 u64 ena, run, val; 935 double uval; 936 struct aggr_cpu_id id; 937 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu); 938 939 if (counter_idx < 0) 940 continue; 941 942 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); 943 if (first) { 944 aggr_printout(config, counter, id, 0); 945 first = false; 946 } 947 val = perf_counts(counter->counts, counter_idx, 0)->val; 948 ena = perf_counts(counter->counts, counter_idx, 0)->ena; 949 run = perf_counts(counter->counts, counter_idx, 0)->run; 950 951 uval = val * counter->scale; 952 printout(config, id, 0, counter, uval, prefix, 953 run, ena, 1.0, &rt_stat); 954 } 955 fputc('\n', config->output); 956 } 957 } 958 959 static int aggr_header_lens[] = { 960 [AGGR_CORE] = 24, 961 [AGGR_DIE] = 18, 962 [AGGR_SOCKET] = 12, 963 [AGGR_NONE] = 6, 964 [AGGR_THREAD] = 24, 965 [AGGR_GLOBAL] = 0, 966 }; 967 968 static const char *aggr_header_csv[] = { 969 [AGGR_CORE] = "core,cpus,", 970 [AGGR_DIE] = "die,cpus", 971 [AGGR_SOCKET] = "socket,cpus", 972 [AGGR_NONE] = "cpu,", 973 [AGGR_THREAD] = "comm-pid,", 974 [AGGR_GLOBAL] = "" 975 }; 976 977 static void print_metric_headers(struct perf_stat_config *config, 978 struct evlist *evlist, 979 const char *prefix, bool no_indent) 980 { 981 struct perf_stat_output_ctx out; 982 struct evsel *counter; 983 struct outstate os = { 984 .fh = config->output 985 }; 986 987 if (prefix) 988 fprintf(config->output, "%s", prefix); 989 990 if (!config->csv_output && !no_indent) 991 fprintf(config->output, "%*s", 992 aggr_header_lens[config->aggr_mode], ""); 993 if (config->csv_output) { 994 if (config->interval) 995 fputs("time,", config->output); 996 if (!config->iostat_run) 997 fputs(aggr_header_csv[config->aggr_mode], config->output); 998 } 999 if (config->iostat_run) 1000 iostat_print_header_prefix(config); 1001 1002 /* Print metrics headers only */ 1003 evlist__for_each_entry(evlist, counter) { 1004 os.evsel = counter; 1005 out.ctx = &os; 1006 out.print_metric = print_metric_header; 1007 out.new_line = new_line_metric; 1008 out.force_header = true; 1009 perf_stat__print_shadow_stats(config, counter, 0, 1010 0, 1011 &out, 1012 &config->metric_events, 1013 &rt_stat); 1014 } 1015 fputc('\n', config->output); 1016 } 1017 1018 static void print_interval(struct perf_stat_config *config, 1019 struct evlist *evlist, 1020 char *prefix, struct timespec *ts) 1021 { 1022 bool metric_only = config->metric_only; 1023 unsigned int unit_width = config->unit_width; 1024 FILE *output = config->output; 1025 static int num_print_interval; 1026 1027 if (config->interval_clear) 1028 puts(CONSOLE_CLEAR); 1029 1030 if (!config->iostat_run) 1031 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep); 1032 1033 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) { 1034 switch (config->aggr_mode) { 1035 case AGGR_NODE: 1036 fprintf(output, "# time node cpus"); 1037 if (!metric_only) 1038 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1039 break; 1040 case AGGR_SOCKET: 1041 fprintf(output, "# time socket cpus"); 1042 if (!metric_only) 1043 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1044 break; 1045 case AGGR_DIE: 1046 fprintf(output, "# time die cpus"); 1047 if (!metric_only) 1048 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1049 break; 1050 case AGGR_CORE: 1051 fprintf(output, "# time core cpus"); 1052 if (!metric_only) 1053 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1054 break; 1055 case AGGR_NONE: 1056 fprintf(output, "# time CPU "); 1057 if (!metric_only) 1058 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1059 break; 1060 case AGGR_THREAD: 1061 fprintf(output, "# time comm-pid"); 1062 if (!metric_only) 1063 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1064 break; 1065 case AGGR_GLOBAL: 1066 default: 1067 if (!config->iostat_run) { 1068 fprintf(output, "# time"); 1069 if (!metric_only) 1070 fprintf(output, " counts %*s events\n", unit_width, "unit"); 1071 } 1072 case AGGR_UNSET: 1073 break; 1074 } 1075 } 1076 1077 if ((num_print_interval == 0 || config->interval_clear) && metric_only) 1078 print_metric_headers(config, evlist, " ", true); 1079 if (++num_print_interval == 25) 1080 num_print_interval = 0; 1081 } 1082 1083 static void print_header(struct perf_stat_config *config, 1084 struct target *_target, 1085 int argc, const char **argv) 1086 { 1087 FILE *output = config->output; 1088 int i; 1089 1090 fflush(stdout); 1091 1092 if (!config->csv_output) { 1093 fprintf(output, "\n"); 1094 fprintf(output, " Performance counter stats for "); 1095 if (_target->bpf_str) 1096 fprintf(output, "\'BPF program(s) %s", _target->bpf_str); 1097 else if (_target->system_wide) 1098 fprintf(output, "\'system wide"); 1099 else if (_target->cpu_list) 1100 fprintf(output, "\'CPU(s) %s", _target->cpu_list); 1101 else if (!target__has_task(_target)) { 1102 fprintf(output, "\'%s", argv ? argv[0] : "pipe"); 1103 for (i = 1; argv && (i < argc); i++) 1104 fprintf(output, " %s", argv[i]); 1105 } else if (_target->pid) 1106 fprintf(output, "process id \'%s", _target->pid); 1107 else 1108 fprintf(output, "thread id \'%s", _target->tid); 1109 1110 fprintf(output, "\'"); 1111 if (config->run_count > 1) 1112 fprintf(output, " (%d runs)", config->run_count); 1113 fprintf(output, ":\n\n"); 1114 } 1115 } 1116 1117 static int get_precision(double num) 1118 { 1119 if (num > 1) 1120 return 0; 1121 1122 return lround(ceil(-log10(num))); 1123 } 1124 1125 static void print_table(struct perf_stat_config *config, 1126 FILE *output, int precision, double avg) 1127 { 1128 char tmp[64]; 1129 int idx, indent = 0; 1130 1131 scnprintf(tmp, 64, " %17.*f", precision, avg); 1132 while (tmp[indent] == ' ') 1133 indent++; 1134 1135 fprintf(output, "%*s# Table of individual measurements:\n", indent, ""); 1136 1137 for (idx = 0; idx < config->run_count; idx++) { 1138 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC; 1139 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5); 1140 1141 fprintf(output, " %17.*f (%+.*f) ", 1142 precision, run, precision, run - avg); 1143 1144 for (h = 0; h < n; h++) 1145 fprintf(output, "#"); 1146 1147 fprintf(output, "\n"); 1148 } 1149 1150 fprintf(output, "\n%*s# Final result:\n", indent, ""); 1151 } 1152 1153 static double timeval2double(struct timeval *t) 1154 { 1155 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC; 1156 } 1157 1158 static void print_footer(struct perf_stat_config *config) 1159 { 1160 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; 1161 FILE *output = config->output; 1162 1163 if (!config->null_run) 1164 fprintf(output, "\n"); 1165 1166 if (config->run_count == 1) { 1167 fprintf(output, " %17.9f seconds time elapsed", avg); 1168 1169 if (config->ru_display) { 1170 double ru_utime = timeval2double(&config->ru_data.ru_utime); 1171 double ru_stime = timeval2double(&config->ru_data.ru_stime); 1172 1173 fprintf(output, "\n\n"); 1174 fprintf(output, " %17.9f seconds user\n", ru_utime); 1175 fprintf(output, " %17.9f seconds sys\n", ru_stime); 1176 } 1177 } else { 1178 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; 1179 /* 1180 * Display at most 2 more significant 1181 * digits than the stddev inaccuracy. 1182 */ 1183 int precision = get_precision(sd) + 2; 1184 1185 if (config->walltime_run_table) 1186 print_table(config, output, precision, avg); 1187 1188 fprintf(output, " %17.*f +- %.*f seconds time elapsed", 1189 precision, avg, precision, sd); 1190 1191 print_noise_pct(config, sd, avg); 1192 } 1193 fprintf(output, "\n\n"); 1194 1195 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled()) 1196 fprintf(output, 1197 "Some events weren't counted. Try disabling the NMI watchdog:\n" 1198 " echo 0 > /proc/sys/kernel/nmi_watchdog\n" 1199 " perf stat ...\n" 1200 " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); 1201 1202 if (config->print_mixed_hw_group_error) 1203 fprintf(output, 1204 "The events in group usually have to be from " 1205 "the same PMU. Try reorganizing the group.\n"); 1206 } 1207 1208 static void print_percore_thread(struct perf_stat_config *config, 1209 struct evsel *counter, char *prefix) 1210 { 1211 int s; 1212 struct aggr_cpu_id s2, id; 1213 struct perf_cpu_map *cpus; 1214 bool first = true; 1215 int idx, cpu; 1216 1217 cpus = evsel__cpus(counter); 1218 perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 1219 s2 = config->aggr_get_id(config, cpu); 1220 for (s = 0; s < config->aggr_map->nr; s++) { 1221 id = config->aggr_map->map[s]; 1222 if (aggr_cpu_id__equal(&s2, &id)) 1223 break; 1224 } 1225 1226 print_counter_aggrdata(config, counter, s, 1227 prefix, false, 1228 &first, cpu); 1229 } 1230 } 1231 1232 static void print_percore(struct perf_stat_config *config, 1233 struct evsel *counter, char *prefix) 1234 { 1235 bool metric_only = config->metric_only; 1236 FILE *output = config->output; 1237 int s; 1238 bool first = true; 1239 1240 if (!config->aggr_map || !config->aggr_get_id) 1241 return; 1242 1243 if (config->percore_show_thread) 1244 return print_percore_thread(config, counter, prefix); 1245 1246 for (s = 0; s < config->aggr_map->nr; s++) { 1247 if (prefix && metric_only) 1248 fprintf(output, "%s", prefix); 1249 1250 print_counter_aggrdata(config, counter, s, 1251 prefix, metric_only, 1252 &first, /*cpu=*/-1); 1253 } 1254 1255 if (metric_only) 1256 fputc('\n', output); 1257 } 1258 1259 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config, 1260 struct target *_target, struct timespec *ts, int argc, const char **argv) 1261 { 1262 bool metric_only = config->metric_only; 1263 int interval = config->interval; 1264 struct evsel *counter; 1265 char buf[64], *prefix = NULL; 1266 1267 if (config->iostat_run) 1268 evlist->selected = evlist__first(evlist); 1269 1270 if (interval) 1271 print_interval(config, evlist, prefix = buf, ts); 1272 else 1273 print_header(config, _target, argc, argv); 1274 1275 if (metric_only) { 1276 static int num_print_iv; 1277 1278 if (num_print_iv == 0 && !interval) 1279 print_metric_headers(config, evlist, prefix, false); 1280 if (num_print_iv++ == 25) 1281 num_print_iv = 0; 1282 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run) 1283 fprintf(config->output, "%s", prefix); 1284 } 1285 1286 switch (config->aggr_mode) { 1287 case AGGR_CORE: 1288 case AGGR_DIE: 1289 case AGGR_SOCKET: 1290 case AGGR_NODE: 1291 print_aggr(config, evlist, prefix); 1292 break; 1293 case AGGR_THREAD: 1294 evlist__for_each_entry(evlist, counter) { 1295 print_aggr_thread(config, _target, counter, prefix); 1296 } 1297 break; 1298 case AGGR_GLOBAL: 1299 if (config->iostat_run) 1300 iostat_print_counters(evlist, config, ts, prefix = buf, 1301 print_counter_aggr); 1302 else { 1303 evlist__for_each_entry(evlist, counter) { 1304 print_counter_aggr(config, counter, prefix); 1305 } 1306 if (metric_only) 1307 fputc('\n', config->output); 1308 } 1309 break; 1310 case AGGR_NONE: 1311 if (metric_only) 1312 print_no_aggr_metric(config, evlist, prefix); 1313 else { 1314 evlist__for_each_entry(evlist, counter) { 1315 if (counter->percore) 1316 print_percore(config, counter, prefix); 1317 else 1318 print_counter(config, counter, prefix); 1319 } 1320 } 1321 break; 1322 case AGGR_UNSET: 1323 default: 1324 break; 1325 } 1326 1327 if (!interval && !config->csv_output) 1328 print_footer(config); 1329 1330 fflush(config->output); 1331 } 1332