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