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