1 // SPDX-License-Identifier: GPL-2.0 2 #include <stdio.h> 3 #include "evsel.h" 4 #include "stat.h" 5 #include "color.h" 6 #include "pmu.h" 7 #include "rblist.h" 8 #include "evlist.h" 9 #include "expr.h" 10 #include "metricgroup.h" 11 #include "cgroup.h" 12 #include "units.h" 13 #include <linux/zalloc.h> 14 15 /* 16 * AGGR_GLOBAL: Use CPU 0 17 * AGGR_SOCKET: Use first CPU of socket 18 * AGGR_DIE: Use first CPU of die 19 * AGGR_CORE: Use first CPU of core 20 * AGGR_NONE: Use matching CPU 21 * AGGR_THREAD: Not supported? 22 */ 23 24 struct runtime_stat rt_stat; 25 struct stats walltime_nsecs_stats; 26 27 struct saved_value { 28 struct rb_node rb_node; 29 struct evsel *evsel; 30 enum stat_type type; 31 int ctx; 32 int cpu; 33 struct cgroup *cgrp; 34 struct runtime_stat *stat; 35 struct stats stats; 36 u64 metric_total; 37 int metric_other; 38 }; 39 40 static int saved_value_cmp(struct rb_node *rb_node, const void *entry) 41 { 42 struct saved_value *a = container_of(rb_node, 43 struct saved_value, 44 rb_node); 45 const struct saved_value *b = entry; 46 47 if (a->cpu != b->cpu) 48 return a->cpu - b->cpu; 49 50 /* 51 * Previously the rbtree was used to link generic metrics. 52 * The keys were evsel/cpu. Now the rbtree is extended to support 53 * per-thread shadow stats. For shadow stats case, the keys 54 * are cpu/type/ctx/stat (evsel is NULL). For generic metrics 55 * case, the keys are still evsel/cpu (type/ctx/stat are 0 or NULL). 56 */ 57 if (a->type != b->type) 58 return a->type - b->type; 59 60 if (a->ctx != b->ctx) 61 return a->ctx - b->ctx; 62 63 if (a->cgrp != b->cgrp) 64 return (char *)a->cgrp < (char *)b->cgrp ? -1 : +1; 65 66 if (a->evsel == NULL && b->evsel == NULL) { 67 if (a->stat == b->stat) 68 return 0; 69 70 if ((char *)a->stat < (char *)b->stat) 71 return -1; 72 73 return 1; 74 } 75 76 if (a->evsel == b->evsel) 77 return 0; 78 if ((char *)a->evsel < (char *)b->evsel) 79 return -1; 80 return +1; 81 } 82 83 static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused, 84 const void *entry) 85 { 86 struct saved_value *nd = malloc(sizeof(struct saved_value)); 87 88 if (!nd) 89 return NULL; 90 memcpy(nd, entry, sizeof(struct saved_value)); 91 return &nd->rb_node; 92 } 93 94 static void saved_value_delete(struct rblist *rblist __maybe_unused, 95 struct rb_node *rb_node) 96 { 97 struct saved_value *v; 98 99 BUG_ON(!rb_node); 100 v = container_of(rb_node, struct saved_value, rb_node); 101 free(v); 102 } 103 104 static struct saved_value *saved_value_lookup(struct evsel *evsel, 105 int cpu, 106 bool create, 107 enum stat_type type, 108 int ctx, 109 struct runtime_stat *st, 110 struct cgroup *cgrp) 111 { 112 struct rblist *rblist; 113 struct rb_node *nd; 114 struct saved_value dm = { 115 .cpu = cpu, 116 .evsel = evsel, 117 .type = type, 118 .ctx = ctx, 119 .stat = st, 120 .cgrp = cgrp, 121 }; 122 123 rblist = &st->value_list; 124 125 /* don't use context info for clock events */ 126 if (type == STAT_NSECS) 127 dm.ctx = 0; 128 129 nd = rblist__find(rblist, &dm); 130 if (nd) 131 return container_of(nd, struct saved_value, rb_node); 132 if (create) { 133 rblist__add_node(rblist, &dm); 134 nd = rblist__find(rblist, &dm); 135 if (nd) 136 return container_of(nd, struct saved_value, rb_node); 137 } 138 return NULL; 139 } 140 141 void runtime_stat__init(struct runtime_stat *st) 142 { 143 struct rblist *rblist = &st->value_list; 144 145 rblist__init(rblist); 146 rblist->node_cmp = saved_value_cmp; 147 rblist->node_new = saved_value_new; 148 rblist->node_delete = saved_value_delete; 149 } 150 151 void runtime_stat__exit(struct runtime_stat *st) 152 { 153 rblist__exit(&st->value_list); 154 } 155 156 void perf_stat__init_shadow_stats(void) 157 { 158 runtime_stat__init(&rt_stat); 159 } 160 161 static int evsel_context(struct evsel *evsel) 162 { 163 int ctx = 0; 164 165 if (evsel->core.attr.exclude_kernel) 166 ctx |= CTX_BIT_KERNEL; 167 if (evsel->core.attr.exclude_user) 168 ctx |= CTX_BIT_USER; 169 if (evsel->core.attr.exclude_hv) 170 ctx |= CTX_BIT_HV; 171 if (evsel->core.attr.exclude_host) 172 ctx |= CTX_BIT_HOST; 173 if (evsel->core.attr.exclude_idle) 174 ctx |= CTX_BIT_IDLE; 175 176 return ctx; 177 } 178 179 static void reset_stat(struct runtime_stat *st) 180 { 181 struct rblist *rblist; 182 struct rb_node *pos, *next; 183 184 rblist = &st->value_list; 185 next = rb_first_cached(&rblist->entries); 186 while (next) { 187 pos = next; 188 next = rb_next(pos); 189 memset(&container_of(pos, struct saved_value, rb_node)->stats, 190 0, 191 sizeof(struct stats)); 192 } 193 } 194 195 void perf_stat__reset_shadow_stats(void) 196 { 197 reset_stat(&rt_stat); 198 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats)); 199 } 200 201 void perf_stat__reset_shadow_per_stat(struct runtime_stat *st) 202 { 203 reset_stat(st); 204 } 205 206 struct runtime_stat_data { 207 int ctx; 208 struct cgroup *cgrp; 209 }; 210 211 static void update_runtime_stat(struct runtime_stat *st, 212 enum stat_type type, 213 int cpu, u64 count, 214 struct runtime_stat_data *rsd) 215 { 216 struct saved_value *v = saved_value_lookup(NULL, cpu, true, type, 217 rsd->ctx, st, rsd->cgrp); 218 219 if (v) 220 update_stats(&v->stats, count); 221 } 222 223 /* 224 * Update various tracking values we maintain to print 225 * more semantic information such as miss/hit ratios, 226 * instruction rates, etc: 227 */ 228 void perf_stat__update_shadow_stats(struct evsel *counter, u64 count, 229 int cpu, struct runtime_stat *st) 230 { 231 u64 count_ns = count; 232 struct saved_value *v; 233 struct runtime_stat_data rsd = { 234 .ctx = evsel_context(counter), 235 .cgrp = counter->cgrp, 236 }; 237 238 count *= counter->scale; 239 240 if (evsel__is_clock(counter)) 241 update_runtime_stat(st, STAT_NSECS, cpu, count_ns, &rsd); 242 else if (evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) 243 update_runtime_stat(st, STAT_CYCLES, cpu, count, &rsd); 244 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX)) 245 update_runtime_stat(st, STAT_CYCLES_IN_TX, cpu, count, &rsd); 246 else if (perf_stat_evsel__is(counter, TRANSACTION_START)) 247 update_runtime_stat(st, STAT_TRANSACTION, cpu, count, &rsd); 248 else if (perf_stat_evsel__is(counter, ELISION_START)) 249 update_runtime_stat(st, STAT_ELISION, cpu, count, &rsd); 250 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS)) 251 update_runtime_stat(st, STAT_TOPDOWN_TOTAL_SLOTS, 252 cpu, count, &rsd); 253 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED)) 254 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_ISSUED, 255 cpu, count, &rsd); 256 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED)) 257 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_RETIRED, 258 cpu, count, &rsd); 259 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES)) 260 update_runtime_stat(st, STAT_TOPDOWN_FETCH_BUBBLES, 261 cpu, count, &rsd); 262 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES)) 263 update_runtime_stat(st, STAT_TOPDOWN_RECOVERY_BUBBLES, 264 cpu, count, &rsd); 265 else if (perf_stat_evsel__is(counter, TOPDOWN_RETIRING)) 266 update_runtime_stat(st, STAT_TOPDOWN_RETIRING, 267 cpu, count, &rsd); 268 else if (perf_stat_evsel__is(counter, TOPDOWN_BAD_SPEC)) 269 update_runtime_stat(st, STAT_TOPDOWN_BAD_SPEC, 270 cpu, count, &rsd); 271 else if (perf_stat_evsel__is(counter, TOPDOWN_FE_BOUND)) 272 update_runtime_stat(st, STAT_TOPDOWN_FE_BOUND, 273 cpu, count, &rsd); 274 else if (perf_stat_evsel__is(counter, TOPDOWN_BE_BOUND)) 275 update_runtime_stat(st, STAT_TOPDOWN_BE_BOUND, 276 cpu, count, &rsd); 277 else if (perf_stat_evsel__is(counter, TOPDOWN_HEAVY_OPS)) 278 update_runtime_stat(st, STAT_TOPDOWN_HEAVY_OPS, 279 cpu, count, &rsd); 280 else if (perf_stat_evsel__is(counter, TOPDOWN_BR_MISPREDICT)) 281 update_runtime_stat(st, STAT_TOPDOWN_BR_MISPREDICT, 282 cpu, count, &rsd); 283 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_LAT)) 284 update_runtime_stat(st, STAT_TOPDOWN_FETCH_LAT, 285 cpu, count, &rsd); 286 else if (perf_stat_evsel__is(counter, TOPDOWN_MEM_BOUND)) 287 update_runtime_stat(st, STAT_TOPDOWN_MEM_BOUND, 288 cpu, count, &rsd); 289 else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) 290 update_runtime_stat(st, STAT_STALLED_CYCLES_FRONT, 291 cpu, count, &rsd); 292 else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND)) 293 update_runtime_stat(st, STAT_STALLED_CYCLES_BACK, 294 cpu, count, &rsd); 295 else if (evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) 296 update_runtime_stat(st, STAT_BRANCHES, cpu, count, &rsd); 297 else if (evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES)) 298 update_runtime_stat(st, STAT_CACHEREFS, cpu, count, &rsd); 299 else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1D)) 300 update_runtime_stat(st, STAT_L1_DCACHE, cpu, count, &rsd); 301 else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1I)) 302 update_runtime_stat(st, STAT_L1_ICACHE, cpu, count, &rsd); 303 else if (evsel__match(counter, HW_CACHE, HW_CACHE_LL)) 304 update_runtime_stat(st, STAT_LL_CACHE, cpu, count, &rsd); 305 else if (evsel__match(counter, HW_CACHE, HW_CACHE_DTLB)) 306 update_runtime_stat(st, STAT_DTLB_CACHE, cpu, count, &rsd); 307 else if (evsel__match(counter, HW_CACHE, HW_CACHE_ITLB)) 308 update_runtime_stat(st, STAT_ITLB_CACHE, cpu, count, &rsd); 309 else if (perf_stat_evsel__is(counter, SMI_NUM)) 310 update_runtime_stat(st, STAT_SMI_NUM, cpu, count, &rsd); 311 else if (perf_stat_evsel__is(counter, APERF)) 312 update_runtime_stat(st, STAT_APERF, cpu, count, &rsd); 313 314 if (counter->collect_stat) { 315 v = saved_value_lookup(counter, cpu, true, STAT_NONE, 0, st, 316 rsd.cgrp); 317 update_stats(&v->stats, count); 318 if (counter->metric_leader) 319 v->metric_total += count; 320 } else if (counter->metric_leader) { 321 v = saved_value_lookup(counter->metric_leader, 322 cpu, true, STAT_NONE, 0, st, rsd.cgrp); 323 v->metric_total += count; 324 v->metric_other++; 325 } 326 } 327 328 /* used for get_ratio_color() */ 329 enum grc_type { 330 GRC_STALLED_CYCLES_FE, 331 GRC_STALLED_CYCLES_BE, 332 GRC_CACHE_MISSES, 333 GRC_MAX_NR 334 }; 335 336 static const char *get_ratio_color(enum grc_type type, double ratio) 337 { 338 static const double grc_table[GRC_MAX_NR][3] = { 339 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 }, 340 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 }, 341 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 }, 342 }; 343 const char *color = PERF_COLOR_NORMAL; 344 345 if (ratio > grc_table[type][0]) 346 color = PERF_COLOR_RED; 347 else if (ratio > grc_table[type][1]) 348 color = PERF_COLOR_MAGENTA; 349 else if (ratio > grc_table[type][2]) 350 color = PERF_COLOR_YELLOW; 351 352 return color; 353 } 354 355 static struct evsel *perf_stat__find_event(struct evlist *evsel_list, 356 const char *name) 357 { 358 struct evsel *c2; 359 360 evlist__for_each_entry (evsel_list, c2) { 361 if (!strcasecmp(c2->name, name) && !c2->collect_stat) 362 return c2; 363 } 364 return NULL; 365 } 366 367 /* Mark MetricExpr target events and link events using them to them. */ 368 void perf_stat__collect_metric_expr(struct evlist *evsel_list) 369 { 370 struct evsel *counter, *leader, **metric_events, *oc; 371 bool found; 372 struct expr_parse_ctx ctx; 373 struct hashmap_entry *cur; 374 size_t bkt; 375 int i; 376 377 expr__ctx_init(&ctx); 378 evlist__for_each_entry(evsel_list, counter) { 379 bool invalid = false; 380 381 leader = counter->leader; 382 if (!counter->metric_expr) 383 continue; 384 385 expr__ctx_clear(&ctx); 386 metric_events = counter->metric_events; 387 if (!metric_events) { 388 if (expr__find_other(counter->metric_expr, 389 counter->name, 390 &ctx, 1) < 0) 391 continue; 392 393 metric_events = calloc(sizeof(struct evsel *), 394 hashmap__size(&ctx.ids) + 1); 395 if (!metric_events) { 396 expr__ctx_clear(&ctx); 397 return; 398 } 399 counter->metric_events = metric_events; 400 } 401 402 i = 0; 403 hashmap__for_each_entry((&ctx.ids), cur, bkt) { 404 const char *metric_name = (const char *)cur->key; 405 406 found = false; 407 if (leader) { 408 /* Search in group */ 409 for_each_group_member (oc, leader) { 410 if (!strcasecmp(oc->name, 411 metric_name) && 412 !oc->collect_stat) { 413 found = true; 414 break; 415 } 416 } 417 } 418 if (!found) { 419 /* Search ignoring groups */ 420 oc = perf_stat__find_event(evsel_list, 421 metric_name); 422 } 423 if (!oc) { 424 /* Deduping one is good enough to handle duplicated PMUs. */ 425 static char *printed; 426 427 /* 428 * Adding events automatically would be difficult, because 429 * it would risk creating groups that are not schedulable. 430 * perf stat doesn't understand all the scheduling constraints 431 * of events. So we ask the user instead to add the missing 432 * events. 433 */ 434 if (!printed || 435 strcasecmp(printed, metric_name)) { 436 fprintf(stderr, 437 "Add %s event to groups to get metric expression for %s\n", 438 metric_name, 439 counter->name); 440 printed = strdup(metric_name); 441 } 442 invalid = true; 443 continue; 444 } 445 metric_events[i++] = oc; 446 oc->collect_stat = true; 447 } 448 metric_events[i] = NULL; 449 if (invalid) { 450 free(metric_events); 451 counter->metric_events = NULL; 452 counter->metric_expr = NULL; 453 } 454 } 455 expr__ctx_clear(&ctx); 456 } 457 458 static double runtime_stat_avg(struct runtime_stat *st, 459 enum stat_type type, int cpu, 460 struct runtime_stat_data *rsd) 461 { 462 struct saved_value *v; 463 464 v = saved_value_lookup(NULL, cpu, false, type, rsd->ctx, st, rsd->cgrp); 465 if (!v) 466 return 0.0; 467 468 return avg_stats(&v->stats); 469 } 470 471 static double runtime_stat_n(struct runtime_stat *st, 472 enum stat_type type, int cpu, 473 struct runtime_stat_data *rsd) 474 { 475 struct saved_value *v; 476 477 v = saved_value_lookup(NULL, cpu, false, type, rsd->ctx, st, rsd->cgrp); 478 if (!v) 479 return 0.0; 480 481 return v->stats.n; 482 } 483 484 static void print_stalled_cycles_frontend(struct perf_stat_config *config, 485 int cpu, double avg, 486 struct perf_stat_output_ctx *out, 487 struct runtime_stat *st, 488 struct runtime_stat_data *rsd) 489 { 490 double total, ratio = 0.0; 491 const char *color; 492 493 total = runtime_stat_avg(st, STAT_CYCLES, cpu, rsd); 494 495 if (total) 496 ratio = avg / total * 100.0; 497 498 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio); 499 500 if (ratio) 501 out->print_metric(config, out->ctx, color, "%7.2f%%", "frontend cycles idle", 502 ratio); 503 else 504 out->print_metric(config, out->ctx, NULL, NULL, "frontend cycles idle", 0); 505 } 506 507 static void print_stalled_cycles_backend(struct perf_stat_config *config, 508 int cpu, double avg, 509 struct perf_stat_output_ctx *out, 510 struct runtime_stat *st, 511 struct runtime_stat_data *rsd) 512 { 513 double total, ratio = 0.0; 514 const char *color; 515 516 total = runtime_stat_avg(st, STAT_CYCLES, cpu, rsd); 517 518 if (total) 519 ratio = avg / total * 100.0; 520 521 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio); 522 523 out->print_metric(config, out->ctx, color, "%7.2f%%", "backend cycles idle", ratio); 524 } 525 526 static void print_branch_misses(struct perf_stat_config *config, 527 int cpu, double avg, 528 struct perf_stat_output_ctx *out, 529 struct runtime_stat *st, 530 struct runtime_stat_data *rsd) 531 { 532 double total, ratio = 0.0; 533 const char *color; 534 535 total = runtime_stat_avg(st, STAT_BRANCHES, cpu, rsd); 536 537 if (total) 538 ratio = avg / total * 100.0; 539 540 color = get_ratio_color(GRC_CACHE_MISSES, ratio); 541 542 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all branches", ratio); 543 } 544 545 static void print_l1_dcache_misses(struct perf_stat_config *config, 546 int cpu, double avg, 547 struct perf_stat_output_ctx *out, 548 struct runtime_stat *st, 549 struct runtime_stat_data *rsd) 550 { 551 double total, ratio = 0.0; 552 const char *color; 553 554 total = runtime_stat_avg(st, STAT_L1_DCACHE, cpu, rsd); 555 556 if (total) 557 ratio = avg / total * 100.0; 558 559 color = get_ratio_color(GRC_CACHE_MISSES, ratio); 560 561 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-dcache accesses", ratio); 562 } 563 564 static void print_l1_icache_misses(struct perf_stat_config *config, 565 int cpu, double avg, 566 struct perf_stat_output_ctx *out, 567 struct runtime_stat *st, 568 struct runtime_stat_data *rsd) 569 { 570 double total, ratio = 0.0; 571 const char *color; 572 573 total = runtime_stat_avg(st, STAT_L1_ICACHE, cpu, rsd); 574 575 if (total) 576 ratio = avg / total * 100.0; 577 578 color = get_ratio_color(GRC_CACHE_MISSES, ratio); 579 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-icache accesses", ratio); 580 } 581 582 static void print_dtlb_cache_misses(struct perf_stat_config *config, 583 int cpu, double avg, 584 struct perf_stat_output_ctx *out, 585 struct runtime_stat *st, 586 struct runtime_stat_data *rsd) 587 { 588 double total, ratio = 0.0; 589 const char *color; 590 591 total = runtime_stat_avg(st, STAT_DTLB_CACHE, cpu, rsd); 592 593 if (total) 594 ratio = avg / total * 100.0; 595 596 color = get_ratio_color(GRC_CACHE_MISSES, ratio); 597 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all dTLB cache accesses", ratio); 598 } 599 600 static void print_itlb_cache_misses(struct perf_stat_config *config, 601 int cpu, double avg, 602 struct perf_stat_output_ctx *out, 603 struct runtime_stat *st, 604 struct runtime_stat_data *rsd) 605 { 606 double total, ratio = 0.0; 607 const char *color; 608 609 total = runtime_stat_avg(st, STAT_ITLB_CACHE, cpu, rsd); 610 611 if (total) 612 ratio = avg / total * 100.0; 613 614 color = get_ratio_color(GRC_CACHE_MISSES, ratio); 615 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all iTLB cache accesses", ratio); 616 } 617 618 static void print_ll_cache_misses(struct perf_stat_config *config, 619 int cpu, double avg, 620 struct perf_stat_output_ctx *out, 621 struct runtime_stat *st, 622 struct runtime_stat_data *rsd) 623 { 624 double total, ratio = 0.0; 625 const char *color; 626 627 total = runtime_stat_avg(st, STAT_LL_CACHE, cpu, rsd); 628 629 if (total) 630 ratio = avg / total * 100.0; 631 632 color = get_ratio_color(GRC_CACHE_MISSES, ratio); 633 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all LL-cache accesses", ratio); 634 } 635 636 /* 637 * High level "TopDown" CPU core pipe line bottleneck break down. 638 * 639 * Basic concept following 640 * Yasin, A Top Down Method for Performance analysis and Counter architecture 641 * ISPASS14 642 * 643 * The CPU pipeline is divided into 4 areas that can be bottlenecks: 644 * 645 * Frontend -> Backend -> Retiring 646 * BadSpeculation in addition means out of order execution that is thrown away 647 * (for example branch mispredictions) 648 * Frontend is instruction decoding. 649 * Backend is execution, like computation and accessing data in memory 650 * Retiring is good execution that is not directly bottlenecked 651 * 652 * The formulas are computed in slots. 653 * A slot is an entry in the pipeline each for the pipeline width 654 * (for example a 4-wide pipeline has 4 slots for each cycle) 655 * 656 * Formulas: 657 * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) / 658 * TotalSlots 659 * Retiring = SlotsRetired / TotalSlots 660 * FrontendBound = FetchBubbles / TotalSlots 661 * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound 662 * 663 * The kernel provides the mapping to the low level CPU events and any scaling 664 * needed for the CPU pipeline width, for example: 665 * 666 * TotalSlots = Cycles * 4 667 * 668 * The scaling factor is communicated in the sysfs unit. 669 * 670 * In some cases the CPU may not be able to measure all the formulas due to 671 * missing events. In this case multiple formulas are combined, as possible. 672 * 673 * Full TopDown supports more levels to sub-divide each area: for example 674 * BackendBound into computing bound and memory bound. For now we only 675 * support Level 1 TopDown. 676 */ 677 678 static double sanitize_val(double x) 679 { 680 if (x < 0 && x >= -0.02) 681 return 0.0; 682 return x; 683 } 684 685 static double td_total_slots(int cpu, struct runtime_stat *st, 686 struct runtime_stat_data *rsd) 687 { 688 return runtime_stat_avg(st, STAT_TOPDOWN_TOTAL_SLOTS, cpu, rsd); 689 } 690 691 static double td_bad_spec(int cpu, struct runtime_stat *st, 692 struct runtime_stat_data *rsd) 693 { 694 double bad_spec = 0; 695 double total_slots; 696 double total; 697 698 total = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_ISSUED, cpu, rsd) - 699 runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, cpu, rsd) + 700 runtime_stat_avg(st, STAT_TOPDOWN_RECOVERY_BUBBLES, cpu, rsd); 701 702 total_slots = td_total_slots(cpu, st, rsd); 703 if (total_slots) 704 bad_spec = total / total_slots; 705 return sanitize_val(bad_spec); 706 } 707 708 static double td_retiring(int cpu, struct runtime_stat *st, 709 struct runtime_stat_data *rsd) 710 { 711 double retiring = 0; 712 double total_slots = td_total_slots(cpu, st, rsd); 713 double ret_slots = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, 714 cpu, rsd); 715 716 if (total_slots) 717 retiring = ret_slots / total_slots; 718 return retiring; 719 } 720 721 static double td_fe_bound(int cpu, struct runtime_stat *st, 722 struct runtime_stat_data *rsd) 723 { 724 double fe_bound = 0; 725 double total_slots = td_total_slots(cpu, st, rsd); 726 double fetch_bub = runtime_stat_avg(st, STAT_TOPDOWN_FETCH_BUBBLES, 727 cpu, rsd); 728 729 if (total_slots) 730 fe_bound = fetch_bub / total_slots; 731 return fe_bound; 732 } 733 734 static double td_be_bound(int cpu, struct runtime_stat *st, 735 struct runtime_stat_data *rsd) 736 { 737 double sum = (td_fe_bound(cpu, st, rsd) + 738 td_bad_spec(cpu, st, rsd) + 739 td_retiring(cpu, st, rsd)); 740 if (sum == 0) 741 return 0; 742 return sanitize_val(1.0 - sum); 743 } 744 745 /* 746 * Kernel reports metrics multiplied with slots. To get back 747 * the ratios we need to recreate the sum. 748 */ 749 750 static double td_metric_ratio(int cpu, enum stat_type type, 751 struct runtime_stat *stat, 752 struct runtime_stat_data *rsd) 753 { 754 double sum = runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, cpu, rsd) + 755 runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, cpu, rsd) + 756 runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, cpu, rsd) + 757 runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, cpu, rsd); 758 double d = runtime_stat_avg(stat, type, cpu, rsd); 759 760 if (sum) 761 return d / sum; 762 return 0; 763 } 764 765 /* 766 * ... but only if most of the values are actually available. 767 * We allow two missing. 768 */ 769 770 static bool full_td(int cpu, struct runtime_stat *stat, 771 struct runtime_stat_data *rsd) 772 { 773 int c = 0; 774 775 if (runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, cpu, rsd) > 0) 776 c++; 777 if (runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, cpu, rsd) > 0) 778 c++; 779 if (runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, cpu, rsd) > 0) 780 c++; 781 if (runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, cpu, rsd) > 0) 782 c++; 783 return c >= 2; 784 } 785 786 static void print_smi_cost(struct perf_stat_config *config, int cpu, 787 struct perf_stat_output_ctx *out, 788 struct runtime_stat *st, 789 struct runtime_stat_data *rsd) 790 { 791 double smi_num, aperf, cycles, cost = 0.0; 792 const char *color = NULL; 793 794 smi_num = runtime_stat_avg(st, STAT_SMI_NUM, cpu, rsd); 795 aperf = runtime_stat_avg(st, STAT_APERF, cpu, rsd); 796 cycles = runtime_stat_avg(st, STAT_CYCLES, cpu, rsd); 797 798 if ((cycles == 0) || (aperf == 0)) 799 return; 800 801 if (smi_num) 802 cost = (aperf - cycles) / aperf * 100.00; 803 804 if (cost > 10) 805 color = PERF_COLOR_RED; 806 out->print_metric(config, out->ctx, color, "%8.1f%%", "SMI cycles%", cost); 807 out->print_metric(config, out->ctx, NULL, "%4.0f", "SMI#", smi_num); 808 } 809 810 static int prepare_metric(struct evsel **metric_events, 811 struct metric_ref *metric_refs, 812 struct expr_parse_ctx *pctx, 813 int cpu, 814 struct runtime_stat *st) 815 { 816 double scale; 817 char *n, *pn; 818 int i, j, ret; 819 820 expr__ctx_init(pctx); 821 for (i = 0; metric_events[i]; i++) { 822 struct saved_value *v; 823 struct stats *stats; 824 u64 metric_total = 0; 825 826 if (!strcmp(metric_events[i]->name, "duration_time")) { 827 stats = &walltime_nsecs_stats; 828 scale = 1e-9; 829 } else { 830 v = saved_value_lookup(metric_events[i], cpu, false, 831 STAT_NONE, 0, st, 832 metric_events[i]->cgrp); 833 if (!v) 834 break; 835 stats = &v->stats; 836 scale = 1.0; 837 838 if (v->metric_other) 839 metric_total = v->metric_total; 840 } 841 842 n = strdup(metric_events[i]->name); 843 if (!n) 844 return -ENOMEM; 845 /* 846 * This display code with --no-merge adds [cpu] postfixes. 847 * These are not supported by the parser. Remove everything 848 * after the space. 849 */ 850 pn = strchr(n, ' '); 851 if (pn) 852 *pn = 0; 853 854 if (metric_total) 855 expr__add_id_val(pctx, n, metric_total); 856 else 857 expr__add_id_val(pctx, n, avg_stats(stats)*scale); 858 } 859 860 for (j = 0; metric_refs && metric_refs[j].metric_name; j++) { 861 ret = expr__add_ref(pctx, &metric_refs[j]); 862 if (ret) 863 return ret; 864 } 865 866 return i; 867 } 868 869 static void generic_metric(struct perf_stat_config *config, 870 const char *metric_expr, 871 struct evsel **metric_events, 872 struct metric_ref *metric_refs, 873 char *name, 874 const char *metric_name, 875 const char *metric_unit, 876 int runtime, 877 int cpu, 878 struct perf_stat_output_ctx *out, 879 struct runtime_stat *st) 880 { 881 print_metric_t print_metric = out->print_metric; 882 struct expr_parse_ctx pctx; 883 double ratio, scale; 884 int i; 885 void *ctxp = out->ctx; 886 887 i = prepare_metric(metric_events, metric_refs, &pctx, cpu, st); 888 if (i < 0) 889 return; 890 891 if (!metric_events[i]) { 892 if (expr__parse(&ratio, &pctx, metric_expr, runtime) == 0) { 893 char *unit; 894 char metric_bf[64]; 895 896 if (metric_unit && metric_name) { 897 if (perf_pmu__convert_scale(metric_unit, 898 &unit, &scale) >= 0) { 899 ratio *= scale; 900 } 901 if (strstr(metric_expr, "?")) 902 scnprintf(metric_bf, sizeof(metric_bf), 903 "%s %s_%d", unit, metric_name, runtime); 904 else 905 scnprintf(metric_bf, sizeof(metric_bf), 906 "%s %s", unit, metric_name); 907 908 print_metric(config, ctxp, NULL, "%8.1f", 909 metric_bf, ratio); 910 } else { 911 print_metric(config, ctxp, NULL, "%8.2f", 912 metric_name ? 913 metric_name : 914 out->force_header ? name : "", 915 ratio); 916 } 917 } else { 918 print_metric(config, ctxp, NULL, NULL, 919 out->force_header ? 920 (metric_name ? metric_name : name) : "", 0); 921 } 922 } else { 923 print_metric(config, ctxp, NULL, NULL, 924 out->force_header ? 925 (metric_name ? metric_name : name) : "", 0); 926 } 927 928 expr__ctx_clear(&pctx); 929 } 930 931 double test_generic_metric(struct metric_expr *mexp, int cpu, struct runtime_stat *st) 932 { 933 struct expr_parse_ctx pctx; 934 double ratio = 0.0; 935 936 if (prepare_metric(mexp->metric_events, mexp->metric_refs, &pctx, cpu, st) < 0) 937 goto out; 938 939 if (expr__parse(&ratio, &pctx, mexp->metric_expr, 1)) 940 ratio = 0.0; 941 942 out: 943 expr__ctx_clear(&pctx); 944 return ratio; 945 } 946 947 void perf_stat__print_shadow_stats(struct perf_stat_config *config, 948 struct evsel *evsel, 949 double avg, int cpu, 950 struct perf_stat_output_ctx *out, 951 struct rblist *metric_events, 952 struct runtime_stat *st) 953 { 954 void *ctxp = out->ctx; 955 print_metric_t print_metric = out->print_metric; 956 double total, ratio = 0.0, total2; 957 const char *color = NULL; 958 struct runtime_stat_data rsd = { 959 .ctx = evsel_context(evsel), 960 .cgrp = evsel->cgrp, 961 }; 962 struct metric_event *me; 963 int num = 1; 964 965 if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) { 966 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd); 967 968 if (total) { 969 ratio = avg / total; 970 print_metric(config, ctxp, NULL, "%7.2f ", 971 "insn per cycle", ratio); 972 } else { 973 print_metric(config, ctxp, NULL, NULL, "insn per cycle", 0); 974 } 975 976 total = runtime_stat_avg(st, STAT_STALLED_CYCLES_FRONT, cpu, &rsd); 977 978 total = max(total, runtime_stat_avg(st, 979 STAT_STALLED_CYCLES_BACK, 980 cpu, &rsd)); 981 982 if (total && avg) { 983 out->new_line(config, ctxp); 984 ratio = total / avg; 985 print_metric(config, ctxp, NULL, "%7.2f ", 986 "stalled cycles per insn", 987 ratio); 988 } 989 } else if (evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) { 990 if (runtime_stat_n(st, STAT_BRANCHES, cpu, &rsd) != 0) 991 print_branch_misses(config, cpu, avg, out, st, &rsd); 992 else 993 print_metric(config, ctxp, NULL, NULL, "of all branches", 0); 994 } else if ( 995 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 996 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_L1D | 997 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 998 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 999 1000 if (runtime_stat_n(st, STAT_L1_DCACHE, cpu, &rsd) != 0) 1001 print_l1_dcache_misses(config, cpu, avg, out, st, &rsd); 1002 else 1003 print_metric(config, ctxp, NULL, NULL, "of all L1-dcache accesses", 0); 1004 } else if ( 1005 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1006 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_L1I | 1007 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1008 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1009 1010 if (runtime_stat_n(st, STAT_L1_ICACHE, cpu, &rsd) != 0) 1011 print_l1_icache_misses(config, cpu, avg, out, st, &rsd); 1012 else 1013 print_metric(config, ctxp, NULL, NULL, "of all L1-icache accesses", 0); 1014 } else if ( 1015 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1016 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_DTLB | 1017 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1018 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1019 1020 if (runtime_stat_n(st, STAT_DTLB_CACHE, cpu, &rsd) != 0) 1021 print_dtlb_cache_misses(config, cpu, avg, out, st, &rsd); 1022 else 1023 print_metric(config, ctxp, NULL, NULL, "of all dTLB cache accesses", 0); 1024 } else if ( 1025 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1026 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_ITLB | 1027 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1028 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1029 1030 if (runtime_stat_n(st, STAT_ITLB_CACHE, cpu, &rsd) != 0) 1031 print_itlb_cache_misses(config, cpu, avg, out, st, &rsd); 1032 else 1033 print_metric(config, ctxp, NULL, NULL, "of all iTLB cache accesses", 0); 1034 } else if ( 1035 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1036 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_LL | 1037 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1038 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1039 1040 if (runtime_stat_n(st, STAT_LL_CACHE, cpu, &rsd) != 0) 1041 print_ll_cache_misses(config, cpu, avg, out, st, &rsd); 1042 else 1043 print_metric(config, ctxp, NULL, NULL, "of all LL-cache accesses", 0); 1044 } else if (evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) { 1045 total = runtime_stat_avg(st, STAT_CACHEREFS, cpu, &rsd); 1046 1047 if (total) 1048 ratio = avg * 100 / total; 1049 1050 if (runtime_stat_n(st, STAT_CACHEREFS, cpu, &rsd) != 0) 1051 print_metric(config, ctxp, NULL, "%8.3f %%", 1052 "of all cache refs", ratio); 1053 else 1054 print_metric(config, ctxp, NULL, NULL, "of all cache refs", 0); 1055 } else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) { 1056 print_stalled_cycles_frontend(config, cpu, avg, out, st, &rsd); 1057 } else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) { 1058 print_stalled_cycles_backend(config, cpu, avg, out, st, &rsd); 1059 } else if (evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) { 1060 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd); 1061 1062 if (total) { 1063 ratio = avg / total; 1064 print_metric(config, ctxp, NULL, "%8.3f", "GHz", ratio); 1065 } else { 1066 print_metric(config, ctxp, NULL, NULL, "Ghz", 0); 1067 } 1068 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) { 1069 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd); 1070 1071 if (total) 1072 print_metric(config, ctxp, NULL, 1073 "%7.2f%%", "transactional cycles", 1074 100.0 * (avg / total)); 1075 else 1076 print_metric(config, ctxp, NULL, NULL, "transactional cycles", 1077 0); 1078 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) { 1079 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd); 1080 total2 = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd); 1081 1082 if (total2 < avg) 1083 total2 = avg; 1084 if (total) 1085 print_metric(config, ctxp, NULL, "%7.2f%%", "aborted cycles", 1086 100.0 * ((total2-avg) / total)); 1087 else 1088 print_metric(config, ctxp, NULL, NULL, "aborted cycles", 0); 1089 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) { 1090 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd); 1091 1092 if (avg) 1093 ratio = total / avg; 1094 1095 if (runtime_stat_n(st, STAT_CYCLES_IN_TX, cpu, &rsd) != 0) 1096 print_metric(config, ctxp, NULL, "%8.0f", 1097 "cycles / transaction", ratio); 1098 else 1099 print_metric(config, ctxp, NULL, NULL, "cycles / transaction", 1100 0); 1101 } else if (perf_stat_evsel__is(evsel, ELISION_START)) { 1102 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd); 1103 1104 if (avg) 1105 ratio = total / avg; 1106 1107 print_metric(config, ctxp, NULL, "%8.0f", "cycles / elision", ratio); 1108 } else if (evsel__is_clock(evsel)) { 1109 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0) 1110 print_metric(config, ctxp, NULL, "%8.3f", "CPUs utilized", 1111 avg / (ratio * evsel->scale)); 1112 else 1113 print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0); 1114 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) { 1115 double fe_bound = td_fe_bound(cpu, st, &rsd); 1116 1117 if (fe_bound > 0.2) 1118 color = PERF_COLOR_RED; 1119 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound", 1120 fe_bound * 100.); 1121 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) { 1122 double retiring = td_retiring(cpu, st, &rsd); 1123 1124 if (retiring > 0.7) 1125 color = PERF_COLOR_GREEN; 1126 print_metric(config, ctxp, color, "%8.1f%%", "retiring", 1127 retiring * 100.); 1128 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) { 1129 double bad_spec = td_bad_spec(cpu, st, &rsd); 1130 1131 if (bad_spec > 0.1) 1132 color = PERF_COLOR_RED; 1133 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation", 1134 bad_spec * 100.); 1135 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) { 1136 double be_bound = td_be_bound(cpu, st, &rsd); 1137 const char *name = "backend bound"; 1138 static int have_recovery_bubbles = -1; 1139 1140 /* In case the CPU does not support topdown-recovery-bubbles */ 1141 if (have_recovery_bubbles < 0) 1142 have_recovery_bubbles = pmu_have_event("cpu", 1143 "topdown-recovery-bubbles"); 1144 if (!have_recovery_bubbles) 1145 name = "backend bound/bad spec"; 1146 1147 if (be_bound > 0.2) 1148 color = PERF_COLOR_RED; 1149 if (td_total_slots(cpu, st, &rsd) > 0) 1150 print_metric(config, ctxp, color, "%8.1f%%", name, 1151 be_bound * 100.); 1152 else 1153 print_metric(config, ctxp, NULL, NULL, name, 0); 1154 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RETIRING) && 1155 full_td(cpu, st, &rsd)) { 1156 double retiring = td_metric_ratio(cpu, 1157 STAT_TOPDOWN_RETIRING, st, 1158 &rsd); 1159 if (retiring > 0.7) 1160 color = PERF_COLOR_GREEN; 1161 print_metric(config, ctxp, color, "%8.1f%%", "retiring", 1162 retiring * 100.); 1163 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FE_BOUND) && 1164 full_td(cpu, st, &rsd)) { 1165 double fe_bound = td_metric_ratio(cpu, 1166 STAT_TOPDOWN_FE_BOUND, st, 1167 &rsd); 1168 if (fe_bound > 0.2) 1169 color = PERF_COLOR_RED; 1170 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound", 1171 fe_bound * 100.); 1172 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BE_BOUND) && 1173 full_td(cpu, st, &rsd)) { 1174 double be_bound = td_metric_ratio(cpu, 1175 STAT_TOPDOWN_BE_BOUND, st, 1176 &rsd); 1177 if (be_bound > 0.2) 1178 color = PERF_COLOR_RED; 1179 print_metric(config, ctxp, color, "%8.1f%%", "backend bound", 1180 be_bound * 100.); 1181 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BAD_SPEC) && 1182 full_td(cpu, st, &rsd)) { 1183 double bad_spec = td_metric_ratio(cpu, 1184 STAT_TOPDOWN_BAD_SPEC, st, 1185 &rsd); 1186 if (bad_spec > 0.1) 1187 color = PERF_COLOR_RED; 1188 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation", 1189 bad_spec * 100.); 1190 } else if (perf_stat_evsel__is(evsel, TOPDOWN_HEAVY_OPS) && 1191 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1192 double retiring = td_metric_ratio(cpu, 1193 STAT_TOPDOWN_RETIRING, st, 1194 &rsd); 1195 double heavy_ops = td_metric_ratio(cpu, 1196 STAT_TOPDOWN_HEAVY_OPS, st, 1197 &rsd); 1198 double light_ops = retiring - heavy_ops; 1199 1200 if (retiring > 0.7 && heavy_ops > 0.1) 1201 color = PERF_COLOR_GREEN; 1202 print_metric(config, ctxp, color, "%8.1f%%", "heavy operations", 1203 heavy_ops * 100.); 1204 if (retiring > 0.7 && light_ops > 0.6) 1205 color = PERF_COLOR_GREEN; 1206 else 1207 color = NULL; 1208 print_metric(config, ctxp, color, "%8.1f%%", "light operations", 1209 light_ops * 100.); 1210 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BR_MISPREDICT) && 1211 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1212 double bad_spec = td_metric_ratio(cpu, 1213 STAT_TOPDOWN_BAD_SPEC, st, 1214 &rsd); 1215 double br_mis = td_metric_ratio(cpu, 1216 STAT_TOPDOWN_BR_MISPREDICT, st, 1217 &rsd); 1218 double m_clears = bad_spec - br_mis; 1219 1220 if (bad_spec > 0.1 && br_mis > 0.05) 1221 color = PERF_COLOR_RED; 1222 print_metric(config, ctxp, color, "%8.1f%%", "branch mispredict", 1223 br_mis * 100.); 1224 if (bad_spec > 0.1 && m_clears > 0.05) 1225 color = PERF_COLOR_RED; 1226 else 1227 color = NULL; 1228 print_metric(config, ctxp, color, "%8.1f%%", "machine clears", 1229 m_clears * 100.); 1230 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_LAT) && 1231 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1232 double fe_bound = td_metric_ratio(cpu, 1233 STAT_TOPDOWN_FE_BOUND, st, 1234 &rsd); 1235 double fetch_lat = td_metric_ratio(cpu, 1236 STAT_TOPDOWN_FETCH_LAT, st, 1237 &rsd); 1238 double fetch_bw = fe_bound - fetch_lat; 1239 1240 if (fe_bound > 0.2 && fetch_lat > 0.15) 1241 color = PERF_COLOR_RED; 1242 print_metric(config, ctxp, color, "%8.1f%%", "fetch latency", 1243 fetch_lat * 100.); 1244 if (fe_bound > 0.2 && fetch_bw > 0.1) 1245 color = PERF_COLOR_RED; 1246 else 1247 color = NULL; 1248 print_metric(config, ctxp, color, "%8.1f%%", "fetch bandwidth", 1249 fetch_bw * 100.); 1250 } else if (perf_stat_evsel__is(evsel, TOPDOWN_MEM_BOUND) && 1251 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1252 double be_bound = td_metric_ratio(cpu, 1253 STAT_TOPDOWN_BE_BOUND, st, 1254 &rsd); 1255 double mem_bound = td_metric_ratio(cpu, 1256 STAT_TOPDOWN_MEM_BOUND, st, 1257 &rsd); 1258 double core_bound = be_bound - mem_bound; 1259 1260 if (be_bound > 0.2 && mem_bound > 0.2) 1261 color = PERF_COLOR_RED; 1262 print_metric(config, ctxp, color, "%8.1f%%", "memory bound", 1263 mem_bound * 100.); 1264 if (be_bound > 0.2 && core_bound > 0.1) 1265 color = PERF_COLOR_RED; 1266 else 1267 color = NULL; 1268 print_metric(config, ctxp, color, "%8.1f%%", "Core bound", 1269 core_bound * 100.); 1270 } else if (evsel->metric_expr) { 1271 generic_metric(config, evsel->metric_expr, evsel->metric_events, NULL, 1272 evsel->name, evsel->metric_name, NULL, 1, cpu, out, st); 1273 } else if (runtime_stat_n(st, STAT_NSECS, cpu, &rsd) != 0) { 1274 char unit = ' '; 1275 char unit_buf[10] = "/sec"; 1276 1277 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd); 1278 if (total) 1279 ratio = convert_unit_double(1000000000.0 * avg / total, &unit); 1280 1281 if (unit != ' ') 1282 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit); 1283 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio); 1284 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) { 1285 print_smi_cost(config, cpu, out, st, &rsd); 1286 } else { 1287 num = 0; 1288 } 1289 1290 if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) { 1291 struct metric_expr *mexp; 1292 1293 list_for_each_entry (mexp, &me->head, nd) { 1294 if (num++ > 0) 1295 out->new_line(config, ctxp); 1296 generic_metric(config, mexp->metric_expr, mexp->metric_events, 1297 mexp->metric_refs, evsel->name, mexp->metric_name, 1298 mexp->metric_unit, mexp->runtime, cpu, out, st); 1299 } 1300 } 1301 if (num == 0) 1302 print_metric(config, ctxp, NULL, NULL, NULL, 0); 1303 } 1304