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