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 35 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%s", os->prefix, config->csv_sep); 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) && !config->json_output) 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] = 0, 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) { 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) 604 pm(config, &os, NULL, NULL, "", 0); 605 else if (config->json_output) 606 pm(config, &os, NULL, NULL, "", 0); 607 return; 608 } 609 610 if (!config->metric_only) 611 abs_printout(config, id, nr, counter, uval); 612 613 out.print_metric = pm; 614 out.new_line = nl; 615 out.ctx = &os; 616 out.force_header = false; 617 618 if (config->csv_output && !config->metric_only) { 619 print_noise(config, counter, noise); 620 print_running(config, run, ena); 621 } else if (config->json_output && !config->metric_only) { 622 print_noise(config, counter, noise); 623 print_running(config, run, ena); 624 } 625 626 perf_stat__print_shadow_stats(config, counter, uval, map_idx, 627 &out, &config->metric_events, st); 628 if (!config->csv_output && !config->metric_only && !config->json_output) { 629 print_noise(config, counter, noise); 630 print_running(config, run, ena); 631 } 632 } 633 634 static void uniquify_event_name(struct evsel *counter) 635 { 636 char *new_name; 637 char *config; 638 int ret = 0; 639 640 if (counter->uniquified_name || counter->use_config_name || 641 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name, 642 strlen(counter->pmu_name))) 643 return; 644 645 config = strchr(counter->name, '/'); 646 if (config) { 647 if (asprintf(&new_name, 648 "%s%s", counter->pmu_name, config) > 0) { 649 free(counter->name); 650 counter->name = new_name; 651 } 652 } else { 653 if (evsel__is_hybrid(counter)) { 654 ret = asprintf(&new_name, "%s/%s/", 655 counter->pmu_name, counter->name); 656 } else { 657 ret = asprintf(&new_name, "%s [%s]", 658 counter->name, counter->pmu_name); 659 } 660 661 if (ret) { 662 free(counter->name); 663 counter->name = new_name; 664 } 665 } 666 667 counter->uniquified_name = true; 668 } 669 670 static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config) 671 { 672 return evsel__is_hybrid(evsel) && !config->hybrid_merge; 673 } 674 675 static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter) 676 { 677 if (config->no_merge || hybrid_uniquify(counter, config)) 678 uniquify_event_name(counter); 679 } 680 681 static void print_counter_aggrdata(struct perf_stat_config *config, 682 struct evsel *counter, int s, 683 char *prefix, bool metric_only, 684 bool *first) 685 { 686 FILE *output = config->output; 687 u64 ena, run, val; 688 double uval; 689 struct perf_stat_evsel *ps = counter->stats; 690 struct perf_stat_aggr *aggr = &ps->aggr[s]; 691 struct aggr_cpu_id id = config->aggr_map->map[s]; 692 double avg = aggr->counts.val; 693 694 if (counter->supported && aggr->nr == 0) 695 return; 696 697 uniquify_counter(config, counter); 698 699 val = aggr->counts.val; 700 ena = aggr->counts.ena; 701 run = aggr->counts.run; 702 703 if (*first && metric_only) { 704 *first = false; 705 aggr_printout(config, counter, id, aggr->nr); 706 } 707 if (prefix && !metric_only) 708 fprintf(output, "%s", prefix); 709 710 uval = val * counter->scale; 711 712 printout(config, id, aggr->nr, counter, uval, 713 prefix, run, ena, avg, &rt_stat, s); 714 715 if (!metric_only) 716 fputc('\n', output); 717 } 718 719 static void print_aggr(struct perf_stat_config *config, 720 struct evlist *evlist, 721 char *prefix) 722 { 723 bool metric_only = config->metric_only; 724 FILE *output = config->output; 725 struct evsel *counter; 726 int s; 727 bool first; 728 729 if (!config->aggr_map || !config->aggr_get_id) 730 return; 731 732 /* 733 * With metric_only everything is on a single line. 734 * Without each counter has its own line. 735 */ 736 for (s = 0; s < config->aggr_map->nr; s++) { 737 if (prefix && metric_only) 738 fprintf(output, "%s", prefix); 739 740 first = true; 741 evlist__for_each_entry(evlist, counter) { 742 if (counter->merged_stat) 743 continue; 744 745 print_counter_aggrdata(config, counter, s, 746 prefix, metric_only, 747 &first); 748 } 749 if (metric_only) 750 fputc('\n', output); 751 } 752 } 753 754 static void print_counter(struct perf_stat_config *config, 755 struct evsel *counter, char *prefix) 756 { 757 bool metric_only = config->metric_only; 758 bool first = false; 759 int s; 760 761 /* AGGR_THREAD doesn't have config->aggr_get_id */ 762 if (!config->aggr_map) 763 return; 764 765 if (counter->merged_stat) 766 return; 767 768 for (s = 0; s < config->aggr_map->nr; s++) { 769 print_counter_aggrdata(config, counter, s, 770 prefix, metric_only, 771 &first); 772 } 773 } 774 775 static void print_no_aggr_metric(struct perf_stat_config *config, 776 struct evlist *evlist, 777 char *prefix) 778 { 779 int all_idx; 780 struct perf_cpu cpu; 781 782 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) { 783 struct evsel *counter; 784 bool first = true; 785 786 evlist__for_each_entry(evlist, counter) { 787 u64 ena, run, val; 788 double uval; 789 struct aggr_cpu_id id; 790 struct perf_stat_evsel *ps = counter->stats; 791 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu); 792 793 if (counter_idx < 0) 794 continue; 795 796 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL); 797 if (first) { 798 if (prefix) 799 fputs(prefix, config->output); 800 aggr_printout(config, counter, id, 0); 801 first = false; 802 } 803 val = ps->aggr[counter_idx].counts.val; 804 ena = ps->aggr[counter_idx].counts.ena; 805 run = ps->aggr[counter_idx].counts.run; 806 807 uval = val * counter->scale; 808 printout(config, id, 0, counter, uval, prefix, 809 run, ena, 1.0, &rt_stat, counter_idx); 810 } 811 if (!first) 812 fputc('\n', config->output); 813 } 814 } 815 816 static int aggr_header_lens[] = { 817 [AGGR_CORE] = 24, 818 [AGGR_DIE] = 18, 819 [AGGR_SOCKET] = 12, 820 [AGGR_NONE] = 6, 821 [AGGR_THREAD] = 24, 822 [AGGR_GLOBAL] = 0, 823 }; 824 825 static const char *aggr_header_csv[] = { 826 [AGGR_CORE] = "core,cpus,", 827 [AGGR_DIE] = "die,cpus", 828 [AGGR_SOCKET] = "socket,cpus", 829 [AGGR_NONE] = "cpu,", 830 [AGGR_THREAD] = "comm-pid,", 831 [AGGR_GLOBAL] = "" 832 }; 833 834 static void print_metric_headers(struct perf_stat_config *config, 835 struct evlist *evlist, 836 const char *prefix, bool no_indent) 837 { 838 struct perf_stat_output_ctx out; 839 struct evsel *counter; 840 struct outstate os = { 841 .fh = config->output 842 }; 843 bool first = true; 844 845 if (config->json_output && !config->interval) 846 fprintf(config->output, "{"); 847 848 if (prefix && !config->json_output) 849 fprintf(config->output, "%s", prefix); 850 851 if (!config->csv_output && !no_indent) 852 fprintf(config->output, "%*s", 853 aggr_header_lens[config->aggr_mode], ""); 854 if (config->csv_output) { 855 if (config->interval) 856 fputs("time,", config->output); 857 if (!config->iostat_run) 858 fputs(aggr_header_csv[config->aggr_mode], config->output); 859 } 860 if (config->iostat_run) 861 iostat_print_header_prefix(config); 862 863 /* Print metrics headers only */ 864 evlist__for_each_entry(evlist, counter) { 865 os.evsel = counter; 866 out.ctx = &os; 867 out.print_metric = print_metric_header; 868 if (!first && config->json_output) 869 fprintf(config->output, ", "); 870 first = false; 871 out.new_line = new_line_metric; 872 out.force_header = true; 873 perf_stat__print_shadow_stats(config, counter, 0, 874 0, 875 &out, 876 &config->metric_events, 877 &rt_stat); 878 } 879 if (config->json_output) 880 fprintf(config->output, "}"); 881 fputc('\n', config->output); 882 } 883 884 static void print_interval(struct perf_stat_config *config, 885 struct evlist *evlist, 886 char *prefix, struct timespec *ts) 887 { 888 bool metric_only = config->metric_only; 889 unsigned int unit_width = config->unit_width; 890 FILE *output = config->output; 891 static int num_print_interval; 892 893 if (config->interval_clear) 894 puts(CONSOLE_CLEAR); 895 896 if (!config->iostat_run && !config->json_output) 897 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, 898 ts->tv_nsec, config->csv_sep); 899 if (!config->iostat_run && config->json_output && !config->metric_only) 900 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long) 901 ts->tv_sec, ts->tv_nsec); 902 if (!config->iostat_run && config->json_output && config->metric_only) 903 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long) 904 ts->tv_sec, ts->tv_nsec); 905 906 if ((num_print_interval == 0 && !config->csv_output && !config->json_output) 907 || config->interval_clear) { 908 switch (config->aggr_mode) { 909 case AGGR_NODE: 910 fprintf(output, "# time node cpus"); 911 if (!metric_only) 912 fprintf(output, " counts %*s events\n", unit_width, "unit"); 913 break; 914 case AGGR_SOCKET: 915 fprintf(output, "# time socket cpus"); 916 if (!metric_only) 917 fprintf(output, " counts %*s events\n", unit_width, "unit"); 918 break; 919 case AGGR_DIE: 920 fprintf(output, "# time die cpus"); 921 if (!metric_only) 922 fprintf(output, " counts %*s events\n", unit_width, "unit"); 923 break; 924 case AGGR_CORE: 925 fprintf(output, "# time core cpus"); 926 if (!metric_only) 927 fprintf(output, " counts %*s events\n", unit_width, "unit"); 928 break; 929 case AGGR_NONE: 930 fprintf(output, "# time CPU "); 931 if (!metric_only) 932 fprintf(output, " counts %*s events\n", unit_width, "unit"); 933 break; 934 case AGGR_THREAD: 935 fprintf(output, "# time comm-pid"); 936 if (!metric_only) 937 fprintf(output, " counts %*s events\n", unit_width, "unit"); 938 break; 939 case AGGR_GLOBAL: 940 default: 941 if (!config->iostat_run) { 942 fprintf(output, "# time"); 943 if (!metric_only) 944 fprintf(output, " counts %*s events\n", unit_width, "unit"); 945 } 946 case AGGR_UNSET: 947 case AGGR_MAX: 948 break; 949 } 950 } 951 952 if ((num_print_interval == 0 || config->interval_clear) 953 && metric_only && !config->json_output) 954 print_metric_headers(config, evlist, " ", true); 955 if ((num_print_interval == 0 || config->interval_clear) 956 && metric_only && config->json_output) { 957 fprintf(output, "{"); 958 print_metric_headers(config, evlist, " ", true); 959 } 960 if (++num_print_interval == 25) 961 num_print_interval = 0; 962 } 963 964 static void print_header(struct perf_stat_config *config, 965 struct target *_target, 966 int argc, const char **argv) 967 { 968 FILE *output = config->output; 969 int i; 970 971 fflush(stdout); 972 973 if (!config->csv_output && !config->json_output) { 974 fprintf(output, "\n"); 975 fprintf(output, " Performance counter stats for "); 976 if (_target->bpf_str) 977 fprintf(output, "\'BPF program(s) %s", _target->bpf_str); 978 else 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 1044 if (!config->null_run) 1045 fprintf(output, "\n"); 1046 1047 if (config->run_count == 1) { 1048 fprintf(output, " %17.9f seconds time elapsed", avg); 1049 1050 if (config->ru_display) { 1051 double ru_utime = timeval2double(&config->ru_data.ru_utime); 1052 double ru_stime = timeval2double(&config->ru_data.ru_stime); 1053 1054 fprintf(output, "\n\n"); 1055 fprintf(output, " %17.9f seconds user\n", ru_utime); 1056 fprintf(output, " %17.9f seconds sys\n", ru_stime); 1057 } 1058 } else { 1059 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC; 1060 /* 1061 * Display at most 2 more significant 1062 * digits than the stddev inaccuracy. 1063 */ 1064 int precision = get_precision(sd) + 2; 1065 1066 if (config->walltime_run_table) 1067 print_table(config, output, precision, avg); 1068 1069 fprintf(output, " %17.*f +- %.*f seconds time elapsed", 1070 precision, avg, precision, sd); 1071 1072 print_noise_pct(config, sd, avg); 1073 } 1074 fprintf(output, "\n\n"); 1075 1076 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled()) 1077 fprintf(output, 1078 "Some events weren't counted. Try disabling the NMI watchdog:\n" 1079 " echo 0 > /proc/sys/kernel/nmi_watchdog\n" 1080 " perf stat ...\n" 1081 " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); 1082 1083 if (config->print_mixed_hw_group_error) 1084 fprintf(output, 1085 "The events in group usually have to be from " 1086 "the same PMU. Try reorganizing the group.\n"); 1087 } 1088 1089 static void print_percore(struct perf_stat_config *config, 1090 struct evsel *counter, char *prefix) 1091 { 1092 bool metric_only = config->metric_only; 1093 FILE *output = config->output; 1094 struct cpu_aggr_map *core_map; 1095 int s, c, i; 1096 bool first = true; 1097 1098 if (!config->aggr_map || !config->aggr_get_id) 1099 return; 1100 1101 if (config->percore_show_thread) 1102 return print_counter(config, counter, prefix); 1103 1104 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr); 1105 if (core_map == NULL) { 1106 fprintf(output, "Cannot allocate per-core aggr map for display\n"); 1107 return; 1108 } 1109 1110 for (s = 0, c = 0; s < config->aggr_map->nr; s++) { 1111 struct perf_cpu curr_cpu = config->aggr_map->map[s].cpu; 1112 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL); 1113 bool found = false; 1114 1115 for (i = 0; i < c; i++) { 1116 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) { 1117 found = true; 1118 break; 1119 } 1120 } 1121 if (found) 1122 continue; 1123 1124 if (prefix && metric_only) 1125 fprintf(output, "%s", prefix); 1126 1127 print_counter_aggrdata(config, counter, s, 1128 prefix, metric_only, &first); 1129 1130 core_map->map[c++] = core_id; 1131 } 1132 free(core_map); 1133 1134 if (metric_only) 1135 fputc('\n', output); 1136 } 1137 1138 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config, 1139 struct target *_target, struct timespec *ts, int argc, const char **argv) 1140 { 1141 bool metric_only = config->metric_only; 1142 int interval = config->interval; 1143 struct evsel *counter; 1144 char buf[64], *prefix = NULL; 1145 1146 if (config->iostat_run) 1147 evlist->selected = evlist__first(evlist); 1148 1149 if (interval) 1150 print_interval(config, evlist, prefix = buf, ts); 1151 else 1152 print_header(config, _target, argc, argv); 1153 1154 if (metric_only) { 1155 static int num_print_iv; 1156 1157 if (num_print_iv == 0 && !interval) 1158 print_metric_headers(config, evlist, prefix, false); 1159 if (num_print_iv++ == 25) 1160 num_print_iv = 0; 1161 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run) 1162 fprintf(config->output, "%s", prefix); 1163 1164 if (config->json_output && !config->metric_only) 1165 fprintf(config->output, "}"); 1166 } 1167 1168 switch (config->aggr_mode) { 1169 case AGGR_CORE: 1170 case AGGR_DIE: 1171 case AGGR_SOCKET: 1172 case AGGR_NODE: 1173 print_aggr(config, evlist, prefix); 1174 break; 1175 case AGGR_THREAD: 1176 case AGGR_GLOBAL: 1177 if (config->iostat_run) 1178 iostat_print_counters(evlist, config, ts, prefix = buf, 1179 print_counter); 1180 else { 1181 evlist__for_each_entry(evlist, counter) { 1182 print_counter(config, counter, prefix); 1183 } 1184 if (metric_only) 1185 fputc('\n', config->output); 1186 } 1187 break; 1188 case AGGR_NONE: 1189 if (metric_only) 1190 print_no_aggr_metric(config, evlist, prefix); 1191 else { 1192 evlist__for_each_entry(evlist, counter) { 1193 if (counter->percore) 1194 print_percore(config, counter, prefix); 1195 else 1196 print_counter(config, counter, prefix); 1197 } 1198 } 1199 break; 1200 case AGGR_MAX: 1201 case AGGR_UNSET: 1202 default: 1203 break; 1204 } 1205 1206 if (!interval && !config->csv_output && !config->json_output) 1207 print_footer(config); 1208 1209 fflush(config->output); 1210 } 1211