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