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; 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 != b->cpu) 51 return a->cpu - b->cpu; 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, 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 = cpu, 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, u64 count, 217 struct runtime_stat_data *rsd) 218 { 219 struct saved_value *v = saved_value_lookup(NULL, cpu, 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, 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, count_ns, &rsd); 245 else if (evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) 246 update_runtime_stat(st, STAT_CYCLES, cpu, count, &rsd); 247 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX)) 248 update_runtime_stat(st, STAT_CYCLES_IN_TX, cpu, count, &rsd); 249 else if (perf_stat_evsel__is(counter, TRANSACTION_START)) 250 update_runtime_stat(st, STAT_TRANSACTION, cpu, count, &rsd); 251 else if (perf_stat_evsel__is(counter, ELISION_START)) 252 update_runtime_stat(st, STAT_ELISION, cpu, count, &rsd); 253 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS)) 254 update_runtime_stat(st, STAT_TOPDOWN_TOTAL_SLOTS, 255 cpu, count, &rsd); 256 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED)) 257 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_ISSUED, 258 cpu, count, &rsd); 259 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED)) 260 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_RETIRED, 261 cpu, count, &rsd); 262 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES)) 263 update_runtime_stat(st, STAT_TOPDOWN_FETCH_BUBBLES, 264 cpu, count, &rsd); 265 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES)) 266 update_runtime_stat(st, STAT_TOPDOWN_RECOVERY_BUBBLES, 267 cpu, count, &rsd); 268 else if (perf_stat_evsel__is(counter, TOPDOWN_RETIRING)) 269 update_runtime_stat(st, STAT_TOPDOWN_RETIRING, 270 cpu, count, &rsd); 271 else if (perf_stat_evsel__is(counter, TOPDOWN_BAD_SPEC)) 272 update_runtime_stat(st, STAT_TOPDOWN_BAD_SPEC, 273 cpu, count, &rsd); 274 else if (perf_stat_evsel__is(counter, TOPDOWN_FE_BOUND)) 275 update_runtime_stat(st, STAT_TOPDOWN_FE_BOUND, 276 cpu, count, &rsd); 277 else if (perf_stat_evsel__is(counter, TOPDOWN_BE_BOUND)) 278 update_runtime_stat(st, STAT_TOPDOWN_BE_BOUND, 279 cpu, count, &rsd); 280 else if (perf_stat_evsel__is(counter, TOPDOWN_HEAVY_OPS)) 281 update_runtime_stat(st, STAT_TOPDOWN_HEAVY_OPS, 282 cpu, count, &rsd); 283 else if (perf_stat_evsel__is(counter, TOPDOWN_BR_MISPREDICT)) 284 update_runtime_stat(st, STAT_TOPDOWN_BR_MISPREDICT, 285 cpu, count, &rsd); 286 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_LAT)) 287 update_runtime_stat(st, STAT_TOPDOWN_FETCH_LAT, 288 cpu, count, &rsd); 289 else if (perf_stat_evsel__is(counter, TOPDOWN_MEM_BOUND)) 290 update_runtime_stat(st, STAT_TOPDOWN_MEM_BOUND, 291 cpu, count, &rsd); 292 else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) 293 update_runtime_stat(st, STAT_STALLED_CYCLES_FRONT, 294 cpu, count, &rsd); 295 else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND)) 296 update_runtime_stat(st, STAT_STALLED_CYCLES_BACK, 297 cpu, count, &rsd); 298 else if (evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) 299 update_runtime_stat(st, STAT_BRANCHES, cpu, count, &rsd); 300 else if (evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES)) 301 update_runtime_stat(st, STAT_CACHEREFS, cpu, count, &rsd); 302 else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1D)) 303 update_runtime_stat(st, STAT_L1_DCACHE, cpu, count, &rsd); 304 else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1I)) 305 update_runtime_stat(st, STAT_L1_ICACHE, cpu, count, &rsd); 306 else if (evsel__match(counter, HW_CACHE, HW_CACHE_LL)) 307 update_runtime_stat(st, STAT_LL_CACHE, cpu, count, &rsd); 308 else if (evsel__match(counter, HW_CACHE, HW_CACHE_DTLB)) 309 update_runtime_stat(st, STAT_DTLB_CACHE, cpu, count, &rsd); 310 else if (evsel__match(counter, HW_CACHE, HW_CACHE_ITLB)) 311 update_runtime_stat(st, STAT_ITLB_CACHE, cpu, count, &rsd); 312 else if (perf_stat_evsel__is(counter, SMI_NUM)) 313 update_runtime_stat(st, STAT_SMI_NUM, cpu, count, &rsd); 314 else if (perf_stat_evsel__is(counter, APERF)) 315 update_runtime_stat(st, STAT_APERF, cpu, count, &rsd); 316 317 if (counter->collect_stat) { 318 v = saved_value_lookup(counter, cpu, 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, 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, 468 struct runtime_stat_data *rsd) 469 { 470 struct saved_value *v; 471 472 v = saved_value_lookup(NULL, cpu, 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, 481 struct runtime_stat_data *rsd) 482 { 483 struct saved_value *v; 484 485 v = saved_value_lookup(NULL, cpu, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, struct runtime_stat *st, 694 struct runtime_stat_data *rsd) 695 { 696 return runtime_stat_avg(st, STAT_TOPDOWN_TOTAL_SLOTS, cpu, rsd); 697 } 698 699 static double td_bad_spec(int cpu, 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, rsd) - 707 runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, cpu, rsd) + 708 runtime_stat_avg(st, STAT_TOPDOWN_RECOVERY_BUBBLES, cpu, rsd); 709 710 total_slots = td_total_slots(cpu, 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, struct runtime_stat *st, 717 struct runtime_stat_data *rsd) 718 { 719 double retiring = 0; 720 double total_slots = td_total_slots(cpu, st, rsd); 721 double ret_slots = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, 722 cpu, 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, struct runtime_stat *st, 730 struct runtime_stat_data *rsd) 731 { 732 double fe_bound = 0; 733 double total_slots = td_total_slots(cpu, st, rsd); 734 double fetch_bub = runtime_stat_avg(st, STAT_TOPDOWN_FETCH_BUBBLES, 735 cpu, 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, struct runtime_stat *st, 743 struct runtime_stat_data *rsd) 744 { 745 double sum = (td_fe_bound(cpu, st, rsd) + 746 td_bad_spec(cpu, st, rsd) + 747 td_retiring(cpu, 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, 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, rsd) + 763 runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, cpu, rsd) + 764 runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, cpu, rsd) + 765 runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, cpu, rsd); 766 double d = runtime_stat_avg(stat, type, cpu, 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, 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, rsd) > 0) 784 c++; 785 if (runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, cpu, rsd) > 0) 786 c++; 787 if (runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, cpu, rsd) > 0) 788 c++; 789 if (runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, cpu, rsd) > 0) 790 c++; 791 return c >= 2; 792 } 793 794 static void print_smi_cost(struct perf_stat_config *config, int cpu, 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, rsd); 803 aperf = runtime_stat_avg(st, STAT_APERF, cpu, rsd); 804 cycles = runtime_stat_avg(st, STAT_CYCLES, cpu, 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, 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 833 if (!strcmp(metric_events[i]->name, "duration_time")) { 834 stats = &walltime_nsecs_stats; 835 scale = 1e-9; 836 } else { 837 v = saved_value_lookup(metric_events[i], cpu, false, 838 STAT_NONE, 0, st, 839 metric_events[i]->cgrp); 840 if (!v) 841 break; 842 stats = &v->stats; 843 scale = 1.0; 844 845 if (v->metric_other) 846 metric_total = v->metric_total; 847 } 848 n = strdup(evsel__metric_id(metric_events[i])); 849 if (!n) 850 return -ENOMEM; 851 852 expr__add_id_val(pctx, n, metric_total ? : avg_stats(stats) * scale); 853 } 854 855 for (j = 0; metric_refs && metric_refs[j].metric_name; j++) { 856 ret = expr__add_ref(pctx, &metric_refs[j]); 857 if (ret) 858 return ret; 859 } 860 861 return i; 862 } 863 864 static void generic_metric(struct perf_stat_config *config, 865 const char *metric_expr, 866 struct evsel **metric_events, 867 struct metric_ref *metric_refs, 868 char *name, 869 const char *metric_name, 870 const char *metric_unit, 871 int runtime, 872 int cpu, 873 struct perf_stat_output_ctx *out, 874 struct runtime_stat *st) 875 { 876 print_metric_t print_metric = out->print_metric; 877 struct expr_parse_ctx *pctx; 878 double ratio, scale; 879 int i; 880 void *ctxp = out->ctx; 881 882 pctx = expr__ctx_new(); 883 if (!pctx) 884 return; 885 886 pctx->runtime = runtime; 887 i = prepare_metric(metric_events, metric_refs, pctx, cpu, st); 888 if (i < 0) { 889 expr__ctx_free(pctx); 890 return; 891 } 892 if (!metric_events[i]) { 893 if (expr__parse(&ratio, pctx, metric_expr) == 0) { 894 char *unit; 895 char metric_bf[64]; 896 897 if (metric_unit && metric_name) { 898 if (perf_pmu__convert_scale(metric_unit, 899 &unit, &scale) >= 0) { 900 ratio *= scale; 901 } 902 if (strstr(metric_expr, "?")) 903 scnprintf(metric_bf, sizeof(metric_bf), 904 "%s %s_%d", unit, metric_name, runtime); 905 else 906 scnprintf(metric_bf, sizeof(metric_bf), 907 "%s %s", unit, metric_name); 908 909 print_metric(config, ctxp, NULL, "%8.1f", 910 metric_bf, ratio); 911 } else { 912 print_metric(config, ctxp, NULL, "%8.2f", 913 metric_name ? 914 metric_name : 915 out->force_header ? name : "", 916 ratio); 917 } 918 } else { 919 print_metric(config, ctxp, NULL, NULL, 920 out->force_header ? 921 (metric_name ? metric_name : name) : "", 0); 922 } 923 } else { 924 print_metric(config, ctxp, NULL, NULL, 925 out->force_header ? 926 (metric_name ? metric_name : name) : "", 0); 927 } 928 929 expr__ctx_free(pctx); 930 } 931 932 double test_generic_metric(struct metric_expr *mexp, int cpu, struct runtime_stat *st) 933 { 934 struct expr_parse_ctx *pctx; 935 double ratio = 0.0; 936 937 pctx = expr__ctx_new(); 938 if (!pctx) 939 return NAN; 940 941 if (prepare_metric(mexp->metric_events, mexp->metric_refs, pctx, cpu, st) < 0) 942 goto out; 943 944 if (expr__parse(&ratio, pctx, mexp->metric_expr)) 945 ratio = 0.0; 946 947 out: 948 expr__ctx_free(pctx); 949 return ratio; 950 } 951 952 void perf_stat__print_shadow_stats(struct perf_stat_config *config, 953 struct evsel *evsel, 954 double avg, int cpu, 955 struct perf_stat_output_ctx *out, 956 struct rblist *metric_events, 957 struct runtime_stat *st) 958 { 959 void *ctxp = out->ctx; 960 print_metric_t print_metric = out->print_metric; 961 double total, ratio = 0.0, total2; 962 const char *color = NULL; 963 struct runtime_stat_data rsd = { 964 .ctx = evsel_context(evsel), 965 .cgrp = evsel->cgrp, 966 }; 967 struct metric_event *me; 968 int num = 1; 969 970 if (config->iostat_run) { 971 iostat_print_metric(config, evsel, out); 972 } else if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) { 973 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd); 974 975 if (total) { 976 ratio = avg / total; 977 print_metric(config, ctxp, NULL, "%7.2f ", 978 "insn per cycle", ratio); 979 } else { 980 print_metric(config, ctxp, NULL, NULL, "insn per cycle", 0); 981 } 982 983 total = runtime_stat_avg(st, STAT_STALLED_CYCLES_FRONT, cpu, &rsd); 984 985 total = max(total, runtime_stat_avg(st, 986 STAT_STALLED_CYCLES_BACK, 987 cpu, &rsd)); 988 989 if (total && avg) { 990 out->new_line(config, ctxp); 991 ratio = total / avg; 992 print_metric(config, ctxp, NULL, "%7.2f ", 993 "stalled cycles per insn", 994 ratio); 995 } 996 } else if (evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) { 997 if (runtime_stat_n(st, STAT_BRANCHES, cpu, &rsd) != 0) 998 print_branch_misses(config, cpu, avg, out, st, &rsd); 999 else 1000 print_metric(config, ctxp, NULL, NULL, "of all branches", 0); 1001 } else if ( 1002 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1003 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_L1D | 1004 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1005 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1006 1007 if (runtime_stat_n(st, STAT_L1_DCACHE, cpu, &rsd) != 0) 1008 print_l1_dcache_misses(config, cpu, avg, out, st, &rsd); 1009 else 1010 print_metric(config, ctxp, NULL, NULL, "of all L1-dcache accesses", 0); 1011 } else if ( 1012 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1013 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_L1I | 1014 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1015 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1016 1017 if (runtime_stat_n(st, STAT_L1_ICACHE, cpu, &rsd) != 0) 1018 print_l1_icache_misses(config, cpu, avg, out, st, &rsd); 1019 else 1020 print_metric(config, ctxp, NULL, NULL, "of all L1-icache accesses", 0); 1021 } else if ( 1022 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1023 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_DTLB | 1024 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1025 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1026 1027 if (runtime_stat_n(st, STAT_DTLB_CACHE, cpu, &rsd) != 0) 1028 print_dtlb_cache_misses(config, cpu, avg, out, st, &rsd); 1029 else 1030 print_metric(config, ctxp, NULL, NULL, "of all dTLB cache accesses", 0); 1031 } else if ( 1032 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1033 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_ITLB | 1034 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1035 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1036 1037 if (runtime_stat_n(st, STAT_ITLB_CACHE, cpu, &rsd) != 0) 1038 print_itlb_cache_misses(config, cpu, avg, out, st, &rsd); 1039 else 1040 print_metric(config, ctxp, NULL, NULL, "of all iTLB cache accesses", 0); 1041 } else if ( 1042 evsel->core.attr.type == PERF_TYPE_HW_CACHE && 1043 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_LL | 1044 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) | 1045 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) { 1046 1047 if (runtime_stat_n(st, STAT_LL_CACHE, cpu, &rsd) != 0) 1048 print_ll_cache_misses(config, cpu, avg, out, st, &rsd); 1049 else 1050 print_metric(config, ctxp, NULL, NULL, "of all LL-cache accesses", 0); 1051 } else if (evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) { 1052 total = runtime_stat_avg(st, STAT_CACHEREFS, cpu, &rsd); 1053 1054 if (total) 1055 ratio = avg * 100 / total; 1056 1057 if (runtime_stat_n(st, STAT_CACHEREFS, cpu, &rsd) != 0) 1058 print_metric(config, ctxp, NULL, "%8.3f %%", 1059 "of all cache refs", ratio); 1060 else 1061 print_metric(config, ctxp, NULL, NULL, "of all cache refs", 0); 1062 } else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) { 1063 print_stalled_cycles_frontend(config, cpu, avg, out, st, &rsd); 1064 } else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) { 1065 print_stalled_cycles_backend(config, cpu, avg, out, st, &rsd); 1066 } else if (evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) { 1067 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd); 1068 1069 if (total) { 1070 ratio = avg / total; 1071 print_metric(config, ctxp, NULL, "%8.3f", "GHz", ratio); 1072 } else { 1073 print_metric(config, ctxp, NULL, NULL, "Ghz", 0); 1074 } 1075 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) { 1076 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd); 1077 1078 if (total) 1079 print_metric(config, ctxp, NULL, 1080 "%7.2f%%", "transactional cycles", 1081 100.0 * (avg / total)); 1082 else 1083 print_metric(config, ctxp, NULL, NULL, "transactional cycles", 1084 0); 1085 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) { 1086 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd); 1087 total2 = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd); 1088 1089 if (total2 < avg) 1090 total2 = avg; 1091 if (total) 1092 print_metric(config, ctxp, NULL, "%7.2f%%", "aborted cycles", 1093 100.0 * ((total2-avg) / total)); 1094 else 1095 print_metric(config, ctxp, NULL, NULL, "aborted cycles", 0); 1096 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) { 1097 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd); 1098 1099 if (avg) 1100 ratio = total / avg; 1101 1102 if (runtime_stat_n(st, STAT_CYCLES_IN_TX, cpu, &rsd) != 0) 1103 print_metric(config, ctxp, NULL, "%8.0f", 1104 "cycles / transaction", ratio); 1105 else 1106 print_metric(config, ctxp, NULL, NULL, "cycles / transaction", 1107 0); 1108 } else if (perf_stat_evsel__is(evsel, ELISION_START)) { 1109 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd); 1110 1111 if (avg) 1112 ratio = total / avg; 1113 1114 print_metric(config, ctxp, NULL, "%8.0f", "cycles / elision", ratio); 1115 } else if (evsel__is_clock(evsel)) { 1116 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0) 1117 print_metric(config, ctxp, NULL, "%8.3f", "CPUs utilized", 1118 avg / (ratio * evsel->scale)); 1119 else 1120 print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0); 1121 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) { 1122 double fe_bound = td_fe_bound(cpu, st, &rsd); 1123 1124 if (fe_bound > 0.2) 1125 color = PERF_COLOR_RED; 1126 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound", 1127 fe_bound * 100.); 1128 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) { 1129 double retiring = td_retiring(cpu, st, &rsd); 1130 1131 if (retiring > 0.7) 1132 color = PERF_COLOR_GREEN; 1133 print_metric(config, ctxp, color, "%8.1f%%", "retiring", 1134 retiring * 100.); 1135 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) { 1136 double bad_spec = td_bad_spec(cpu, st, &rsd); 1137 1138 if (bad_spec > 0.1) 1139 color = PERF_COLOR_RED; 1140 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation", 1141 bad_spec * 100.); 1142 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) { 1143 double be_bound = td_be_bound(cpu, st, &rsd); 1144 const char *name = "backend bound"; 1145 static int have_recovery_bubbles = -1; 1146 1147 /* In case the CPU does not support topdown-recovery-bubbles */ 1148 if (have_recovery_bubbles < 0) 1149 have_recovery_bubbles = pmu_have_event("cpu", 1150 "topdown-recovery-bubbles"); 1151 if (!have_recovery_bubbles) 1152 name = "backend bound/bad spec"; 1153 1154 if (be_bound > 0.2) 1155 color = PERF_COLOR_RED; 1156 if (td_total_slots(cpu, st, &rsd) > 0) 1157 print_metric(config, ctxp, color, "%8.1f%%", name, 1158 be_bound * 100.); 1159 else 1160 print_metric(config, ctxp, NULL, NULL, name, 0); 1161 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RETIRING) && 1162 full_td(cpu, st, &rsd)) { 1163 double retiring = td_metric_ratio(cpu, 1164 STAT_TOPDOWN_RETIRING, st, 1165 &rsd); 1166 if (retiring > 0.7) 1167 color = PERF_COLOR_GREEN; 1168 print_metric(config, ctxp, color, "%8.1f%%", "retiring", 1169 retiring * 100.); 1170 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FE_BOUND) && 1171 full_td(cpu, st, &rsd)) { 1172 double fe_bound = td_metric_ratio(cpu, 1173 STAT_TOPDOWN_FE_BOUND, st, 1174 &rsd); 1175 if (fe_bound > 0.2) 1176 color = PERF_COLOR_RED; 1177 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound", 1178 fe_bound * 100.); 1179 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BE_BOUND) && 1180 full_td(cpu, st, &rsd)) { 1181 double be_bound = td_metric_ratio(cpu, 1182 STAT_TOPDOWN_BE_BOUND, st, 1183 &rsd); 1184 if (be_bound > 0.2) 1185 color = PERF_COLOR_RED; 1186 print_metric(config, ctxp, color, "%8.1f%%", "backend bound", 1187 be_bound * 100.); 1188 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BAD_SPEC) && 1189 full_td(cpu, st, &rsd)) { 1190 double bad_spec = td_metric_ratio(cpu, 1191 STAT_TOPDOWN_BAD_SPEC, st, 1192 &rsd); 1193 if (bad_spec > 0.1) 1194 color = PERF_COLOR_RED; 1195 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation", 1196 bad_spec * 100.); 1197 } else if (perf_stat_evsel__is(evsel, TOPDOWN_HEAVY_OPS) && 1198 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1199 double retiring = td_metric_ratio(cpu, 1200 STAT_TOPDOWN_RETIRING, st, 1201 &rsd); 1202 double heavy_ops = td_metric_ratio(cpu, 1203 STAT_TOPDOWN_HEAVY_OPS, st, 1204 &rsd); 1205 double light_ops = retiring - heavy_ops; 1206 1207 if (retiring > 0.7 && heavy_ops > 0.1) 1208 color = PERF_COLOR_GREEN; 1209 print_metric(config, ctxp, color, "%8.1f%%", "heavy operations", 1210 heavy_ops * 100.); 1211 if (retiring > 0.7 && light_ops > 0.6) 1212 color = PERF_COLOR_GREEN; 1213 else 1214 color = NULL; 1215 print_metric(config, ctxp, color, "%8.1f%%", "light operations", 1216 light_ops * 100.); 1217 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BR_MISPREDICT) && 1218 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1219 double bad_spec = td_metric_ratio(cpu, 1220 STAT_TOPDOWN_BAD_SPEC, st, 1221 &rsd); 1222 double br_mis = td_metric_ratio(cpu, 1223 STAT_TOPDOWN_BR_MISPREDICT, st, 1224 &rsd); 1225 double m_clears = bad_spec - br_mis; 1226 1227 if (bad_spec > 0.1 && br_mis > 0.05) 1228 color = PERF_COLOR_RED; 1229 print_metric(config, ctxp, color, "%8.1f%%", "branch mispredict", 1230 br_mis * 100.); 1231 if (bad_spec > 0.1 && m_clears > 0.05) 1232 color = PERF_COLOR_RED; 1233 else 1234 color = NULL; 1235 print_metric(config, ctxp, color, "%8.1f%%", "machine clears", 1236 m_clears * 100.); 1237 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_LAT) && 1238 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1239 double fe_bound = td_metric_ratio(cpu, 1240 STAT_TOPDOWN_FE_BOUND, st, 1241 &rsd); 1242 double fetch_lat = td_metric_ratio(cpu, 1243 STAT_TOPDOWN_FETCH_LAT, st, 1244 &rsd); 1245 double fetch_bw = fe_bound - fetch_lat; 1246 1247 if (fe_bound > 0.2 && fetch_lat > 0.15) 1248 color = PERF_COLOR_RED; 1249 print_metric(config, ctxp, color, "%8.1f%%", "fetch latency", 1250 fetch_lat * 100.); 1251 if (fe_bound > 0.2 && fetch_bw > 0.1) 1252 color = PERF_COLOR_RED; 1253 else 1254 color = NULL; 1255 print_metric(config, ctxp, color, "%8.1f%%", "fetch bandwidth", 1256 fetch_bw * 100.); 1257 } else if (perf_stat_evsel__is(evsel, TOPDOWN_MEM_BOUND) && 1258 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) { 1259 double be_bound = td_metric_ratio(cpu, 1260 STAT_TOPDOWN_BE_BOUND, st, 1261 &rsd); 1262 double mem_bound = td_metric_ratio(cpu, 1263 STAT_TOPDOWN_MEM_BOUND, st, 1264 &rsd); 1265 double core_bound = be_bound - mem_bound; 1266 1267 if (be_bound > 0.2 && mem_bound > 0.2) 1268 color = PERF_COLOR_RED; 1269 print_metric(config, ctxp, color, "%8.1f%%", "memory bound", 1270 mem_bound * 100.); 1271 if (be_bound > 0.2 && core_bound > 0.1) 1272 color = PERF_COLOR_RED; 1273 else 1274 color = NULL; 1275 print_metric(config, ctxp, color, "%8.1f%%", "Core bound", 1276 core_bound * 100.); 1277 } else if (evsel->metric_expr) { 1278 generic_metric(config, evsel->metric_expr, evsel->metric_events, NULL, 1279 evsel->name, evsel->metric_name, NULL, 1, cpu, out, st); 1280 } else if (runtime_stat_n(st, STAT_NSECS, cpu, &rsd) != 0) { 1281 char unit = ' '; 1282 char unit_buf[10] = "/sec"; 1283 1284 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd); 1285 if (total) 1286 ratio = convert_unit_double(1000000000.0 * avg / total, &unit); 1287 1288 if (unit != ' ') 1289 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit); 1290 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio); 1291 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) { 1292 print_smi_cost(config, cpu, out, st, &rsd); 1293 } else { 1294 num = 0; 1295 } 1296 1297 if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) { 1298 struct metric_expr *mexp; 1299 1300 list_for_each_entry (mexp, &me->head, nd) { 1301 if (num++ > 0) 1302 out->new_line(config, ctxp); 1303 generic_metric(config, mexp->metric_expr, mexp->metric_events, 1304 mexp->metric_refs, evsel->name, mexp->metric_name, 1305 mexp->metric_unit, mexp->runtime, cpu, out, st); 1306 } 1307 } 1308 if (num == 0) 1309 print_metric(config, ctxp, NULL, NULL, NULL, 0); 1310 } 1311