1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <linux/err.h> 4 #include <inttypes.h> 5 #include <math.h> 6 #include <string.h> 7 #include "counts.h" 8 #include "cpumap.h" 9 #include "debug.h" 10 #include "header.h" 11 #include "stat.h" 12 #include "session.h" 13 #include "target.h" 14 #include "evlist.h" 15 #include "evsel.h" 16 #include "thread_map.h" 17 #include "util/hashmap.h" 18 #include <linux/zalloc.h> 19 20 void update_stats(struct stats *stats, u64 val) 21 { 22 double delta; 23 24 stats->n++; 25 delta = val - stats->mean; 26 stats->mean += delta / stats->n; 27 stats->M2 += delta*(val - stats->mean); 28 29 if (val > stats->max) 30 stats->max = val; 31 32 if (val < stats->min) 33 stats->min = val; 34 } 35 36 double avg_stats(struct stats *stats) 37 { 38 return stats->mean; 39 } 40 41 /* 42 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 43 * 44 * (\Sum n_i^2) - ((\Sum n_i)^2)/n 45 * s^2 = ------------------------------- 46 * n - 1 47 * 48 * http://en.wikipedia.org/wiki/Stddev 49 * 50 * The std dev of the mean is related to the std dev by: 51 * 52 * s 53 * s_mean = ------- 54 * sqrt(n) 55 * 56 */ 57 double stddev_stats(struct stats *stats) 58 { 59 double variance, variance_mean; 60 61 if (stats->n < 2) 62 return 0.0; 63 64 variance = stats->M2 / (stats->n - 1); 65 variance_mean = variance / stats->n; 66 67 return sqrt(variance_mean); 68 } 69 70 double rel_stddev_stats(double stddev, double avg) 71 { 72 double pct = 0.0; 73 74 if (avg) 75 pct = 100.0 * stddev/avg; 76 77 return pct; 78 } 79 80 static void evsel__reset_aggr_stats(struct evsel *evsel) 81 { 82 struct perf_stat_evsel *ps = evsel->stats; 83 struct perf_stat_aggr *aggr = ps->aggr; 84 85 if (aggr) 86 memset(aggr, 0, sizeof(*aggr) * ps->nr_aggr); 87 } 88 89 static void evsel__reset_stat_priv(struct evsel *evsel) 90 { 91 struct perf_stat_evsel *ps = evsel->stats; 92 93 init_stats(&ps->res_stats); 94 evsel__reset_aggr_stats(evsel); 95 } 96 97 static int evsel__alloc_aggr_stats(struct evsel *evsel, int nr_aggr) 98 { 99 struct perf_stat_evsel *ps = evsel->stats; 100 101 if (ps == NULL) 102 return 0; 103 104 ps->nr_aggr = nr_aggr; 105 ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr)); 106 if (ps->aggr == NULL) 107 return -ENOMEM; 108 109 return 0; 110 } 111 112 int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr) 113 { 114 struct evsel *evsel; 115 116 evlist__for_each_entry(evlist, evsel) { 117 if (evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) 118 return -1; 119 } 120 return 0; 121 } 122 123 static int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr) 124 { 125 struct perf_stat_evsel *ps; 126 127 ps = zalloc(sizeof(*ps)); 128 if (ps == NULL) 129 return -ENOMEM; 130 131 evsel->stats = ps; 132 133 if (nr_aggr && evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) { 134 evsel->stats = NULL; 135 free(ps); 136 return -ENOMEM; 137 } 138 139 evsel__reset_stat_priv(evsel); 140 return 0; 141 } 142 143 static void evsel__free_stat_priv(struct evsel *evsel) 144 { 145 struct perf_stat_evsel *ps = evsel->stats; 146 147 if (ps) { 148 zfree(&ps->aggr); 149 zfree(&ps->group_data); 150 } 151 zfree(&evsel->stats); 152 } 153 154 static int evsel__alloc_prev_raw_counts(struct evsel *evsel) 155 { 156 int cpu_map_nr = evsel__nr_cpus(evsel); 157 int nthreads = perf_thread_map__nr(evsel->core.threads); 158 struct perf_counts *counts; 159 160 counts = perf_counts__new(cpu_map_nr, nthreads); 161 if (counts) 162 evsel->prev_raw_counts = counts; 163 164 return counts ? 0 : -ENOMEM; 165 } 166 167 static void evsel__free_prev_raw_counts(struct evsel *evsel) 168 { 169 perf_counts__delete(evsel->prev_raw_counts); 170 evsel->prev_raw_counts = NULL; 171 } 172 173 static void evsel__reset_prev_raw_counts(struct evsel *evsel) 174 { 175 if (evsel->prev_raw_counts) 176 perf_counts__reset(evsel->prev_raw_counts); 177 } 178 179 static int evsel__alloc_stats(struct evsel *evsel, int nr_aggr, bool alloc_raw) 180 { 181 if (evsel__alloc_stat_priv(evsel, nr_aggr) < 0 || 182 evsel__alloc_counts(evsel) < 0 || 183 (alloc_raw && evsel__alloc_prev_raw_counts(evsel) < 0)) 184 return -ENOMEM; 185 186 return 0; 187 } 188 189 int evlist__alloc_stats(struct perf_stat_config *config, 190 struct evlist *evlist, bool alloc_raw) 191 { 192 struct evsel *evsel; 193 int nr_aggr = 0; 194 195 if (config && config->aggr_map) 196 nr_aggr = config->aggr_map->nr; 197 198 evlist__for_each_entry(evlist, evsel) { 199 if (evsel__alloc_stats(evsel, nr_aggr, alloc_raw)) 200 goto out_free; 201 } 202 203 return 0; 204 205 out_free: 206 evlist__free_stats(evlist); 207 return -1; 208 } 209 210 void evlist__free_stats(struct evlist *evlist) 211 { 212 struct evsel *evsel; 213 214 evlist__for_each_entry(evlist, evsel) { 215 evsel__free_stat_priv(evsel); 216 evsel__free_counts(evsel); 217 evsel__free_prev_raw_counts(evsel); 218 } 219 } 220 221 void evlist__reset_stats(struct evlist *evlist) 222 { 223 struct evsel *evsel; 224 225 evlist__for_each_entry(evlist, evsel) { 226 evsel__reset_stat_priv(evsel); 227 evsel__reset_counts(evsel); 228 } 229 } 230 231 void evlist__reset_aggr_stats(struct evlist *evlist) 232 { 233 struct evsel *evsel; 234 235 evlist__for_each_entry(evlist, evsel) 236 evsel__reset_aggr_stats(evsel); 237 } 238 239 void evlist__reset_prev_raw_counts(struct evlist *evlist) 240 { 241 struct evsel *evsel; 242 243 evlist__for_each_entry(evlist, evsel) 244 evsel__reset_prev_raw_counts(evsel); 245 } 246 247 static void evsel__copy_prev_raw_counts(struct evsel *evsel) 248 { 249 int nthreads = perf_thread_map__nr(evsel->core.threads); 250 251 for (int thread = 0; thread < nthreads; thread++) { 252 unsigned int idx; 253 254 perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { 255 *perf_counts(evsel->counts, idx, thread) = 256 *perf_counts(evsel->prev_raw_counts, idx, thread); 257 } 258 } 259 } 260 261 void evlist__copy_prev_raw_counts(struct evlist *evlist) 262 { 263 struct evsel *evsel; 264 265 evlist__for_each_entry(evlist, evsel) 266 evsel__copy_prev_raw_counts(evsel); 267 } 268 269 static void evsel__copy_res_stats(struct evsel *evsel) 270 { 271 struct perf_stat_evsel *ps = evsel->stats; 272 273 /* 274 * For GLOBAL aggregation mode, it updates the counts for each run 275 * in the evsel->stats.res_stats. See perf_stat_process_counter(). 276 */ 277 *ps->aggr[0].counts.values = avg_stats(&ps->res_stats); 278 } 279 280 void evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist) 281 { 282 struct evsel *evsel; 283 284 if (config->aggr_mode != AGGR_GLOBAL) 285 return; 286 287 evlist__for_each_entry(evlist, evsel) 288 evsel__copy_res_stats(evsel); 289 } 290 291 static size_t pkg_id_hash(long __key, void *ctx __maybe_unused) 292 { 293 uint64_t *key = (uint64_t *) __key; 294 295 return *key & 0xffffffff; 296 } 297 298 static bool pkg_id_equal(long __key1, long __key2, void *ctx __maybe_unused) 299 { 300 uint64_t *key1 = (uint64_t *) __key1; 301 uint64_t *key2 = (uint64_t *) __key2; 302 303 return *key1 == *key2; 304 } 305 306 static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals, 307 int cpu_map_idx, bool *skip) 308 { 309 struct hashmap *mask = counter->per_pkg_mask; 310 struct perf_cpu_map *cpus = evsel__cpus(counter); 311 struct perf_cpu cpu = perf_cpu_map__cpu(cpus, cpu_map_idx); 312 int s, d, ret = 0; 313 uint64_t *key; 314 315 *skip = false; 316 317 if (!counter->per_pkg) 318 return 0; 319 320 if (perf_cpu_map__is_any_cpu_or_is_empty(cpus)) 321 return 0; 322 323 if (!mask) { 324 mask = hashmap__new(pkg_id_hash, pkg_id_equal, NULL); 325 if (IS_ERR(mask)) 326 return -ENOMEM; 327 328 counter->per_pkg_mask = mask; 329 } 330 331 /* 332 * we do not consider an event that has not run as a good 333 * instance to mark a package as used (skip=1). Otherwise 334 * we may run into a situation where the first CPU in a package 335 * is not running anything, yet the second is, and this function 336 * would mark the package as used after the first CPU and would 337 * not read the values from the second CPU. 338 */ 339 if (!(vals->run && vals->ena)) 340 return 0; 341 342 s = cpu__get_socket_id(cpu); 343 if (s < 0) 344 return -1; 345 346 /* 347 * On multi-die system, die_id > 0. On no-die system, die_id = 0. 348 * We use hashmap(socket, die) to check the used socket+die pair. 349 */ 350 d = cpu__get_die_id(cpu); 351 if (d < 0) 352 return -1; 353 354 key = malloc(sizeof(*key)); 355 if (!key) 356 return -ENOMEM; 357 358 *key = (uint64_t)d << 32 | s; 359 if (hashmap__find(mask, key, NULL)) { 360 *skip = true; 361 free(key); 362 } else 363 ret = hashmap__add(mask, key, 1); 364 365 return ret; 366 } 367 368 static bool evsel__count_has_error(struct evsel *evsel, 369 struct perf_counts_values *count, 370 struct perf_stat_config *config) 371 { 372 /* the evsel was failed already */ 373 if (evsel->err || evsel->counts->scaled == -1) 374 return true; 375 376 /* this is meaningful for CPU aggregation modes only */ 377 if (config->aggr_mode == AGGR_GLOBAL) 378 return false; 379 380 /* it's considered ok when it actually ran */ 381 if (count->ena != 0 && count->run != 0) 382 return false; 383 384 return true; 385 } 386 387 static int 388 process_counter_values(struct perf_stat_config *config, struct evsel *evsel, 389 int cpu_map_idx, int thread, 390 struct perf_counts_values *count) 391 { 392 struct perf_stat_evsel *ps = evsel->stats; 393 static struct perf_counts_values zero; 394 bool skip = false; 395 396 if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) { 397 pr_err("failed to read per-pkg counter\n"); 398 return -1; 399 } 400 401 if (skip) 402 count = &zero; 403 404 if (!evsel->snapshot) 405 evsel__compute_deltas(evsel, cpu_map_idx, thread, count); 406 perf_counts_values__scale(count, config->scale, NULL); 407 408 if (config->aggr_mode == AGGR_THREAD) { 409 struct perf_counts_values *aggr_counts = &ps->aggr[thread].counts; 410 411 /* 412 * Skip value 0 when enabling --per-thread globally, 413 * otherwise too many 0 output. 414 */ 415 if (count->val == 0 && config->system_wide) 416 return 0; 417 418 ps->aggr[thread].nr++; 419 420 aggr_counts->val += count->val; 421 aggr_counts->ena += count->ena; 422 aggr_counts->run += count->run; 423 return 0; 424 } 425 426 if (ps->aggr) { 427 struct perf_cpu cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx); 428 struct aggr_cpu_id aggr_id = config->aggr_get_id(config, cpu); 429 struct perf_stat_aggr *ps_aggr; 430 int i; 431 432 for (i = 0; i < ps->nr_aggr; i++) { 433 if (!aggr_cpu_id__equal(&aggr_id, &config->aggr_map->map[i])) 434 continue; 435 436 ps_aggr = &ps->aggr[i]; 437 ps_aggr->nr++; 438 439 /* 440 * When any result is bad, make them all to give consistent output 441 * in interval mode. But per-task counters can have 0 enabled time 442 * when some tasks are idle. 443 */ 444 if (evsel__count_has_error(evsel, count, config) && !ps_aggr->failed) { 445 ps_aggr->counts.val = 0; 446 ps_aggr->counts.ena = 0; 447 ps_aggr->counts.run = 0; 448 ps_aggr->failed = true; 449 } 450 451 if (!ps_aggr->failed) { 452 ps_aggr->counts.val += count->val; 453 ps_aggr->counts.ena += count->ena; 454 ps_aggr->counts.run += count->run; 455 } 456 break; 457 } 458 } 459 460 return 0; 461 } 462 463 static int process_counter_maps(struct perf_stat_config *config, 464 struct evsel *counter) 465 { 466 int nthreads = perf_thread_map__nr(counter->core.threads); 467 int ncpus = evsel__nr_cpus(counter); 468 int idx, thread; 469 470 for (thread = 0; thread < nthreads; thread++) { 471 for (idx = 0; idx < ncpus; idx++) { 472 if (process_counter_values(config, counter, idx, thread, 473 perf_counts(counter->counts, idx, thread))) 474 return -1; 475 } 476 } 477 478 return 0; 479 } 480 481 int perf_stat_process_counter(struct perf_stat_config *config, 482 struct evsel *counter) 483 { 484 struct perf_stat_evsel *ps = counter->stats; 485 u64 *count; 486 int ret; 487 488 if (counter->per_pkg) 489 evsel__zero_per_pkg(counter); 490 491 ret = process_counter_maps(config, counter); 492 if (ret) 493 return ret; 494 495 if (config->aggr_mode != AGGR_GLOBAL) 496 return 0; 497 498 /* 499 * GLOBAL aggregation mode only has a single aggr counts, 500 * so we can use ps->aggr[0] as the actual output. 501 */ 502 count = ps->aggr[0].counts.values; 503 update_stats(&ps->res_stats, *count); 504 505 if (verbose > 0) { 506 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", 507 evsel__name(counter), count[0], count[1], count[2]); 508 } 509 510 return 0; 511 } 512 513 static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias) 514 { 515 struct perf_stat_evsel *ps_a = evsel->stats; 516 struct perf_stat_evsel *ps_b = alias->stats; 517 int i; 518 519 if (ps_a->aggr == NULL && ps_b->aggr == NULL) 520 return 0; 521 522 if (ps_a->nr_aggr != ps_b->nr_aggr) { 523 pr_err("Unmatched aggregation mode between aliases\n"); 524 return -1; 525 } 526 527 for (i = 0; i < ps_a->nr_aggr; i++) { 528 struct perf_counts_values *aggr_counts_a = &ps_a->aggr[i].counts; 529 struct perf_counts_values *aggr_counts_b = &ps_b->aggr[i].counts; 530 531 ps_a->aggr[i].nr += ps_b->aggr[i].nr; 532 533 aggr_counts_a->val += aggr_counts_b->val; 534 aggr_counts_a->ena += aggr_counts_b->ena; 535 aggr_counts_a->run += aggr_counts_b->run; 536 } 537 538 return 0; 539 } 540 541 static void evsel__merge_aliases(struct evsel *evsel) 542 { 543 struct evlist *evlist = evsel->evlist; 544 struct evsel *alias; 545 546 alias = list_prepare_entry(evsel, &(evlist->core.entries), core.node); 547 list_for_each_entry_continue(alias, &evlist->core.entries, core.node) { 548 if (alias->first_wildcard_match == evsel) { 549 /* Merge the same events on different PMUs. */ 550 evsel__merge_aggr_counters(evsel, alias); 551 } 552 } 553 } 554 555 static bool evsel__should_merge_hybrid(const struct evsel *evsel, 556 const struct perf_stat_config *config) 557 { 558 return config->hybrid_merge && evsel__is_hybrid(evsel); 559 } 560 561 static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_config *config) 562 { 563 if (!evsel->pmu || !evsel->pmu->is_core || evsel__should_merge_hybrid(evsel, config)) 564 evsel__merge_aliases(evsel); 565 } 566 567 /* merge the same uncore and hybrid events if requested */ 568 void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist) 569 { 570 struct evsel *evsel; 571 572 if (config->aggr_mode == AGGR_NONE) 573 return; 574 575 evlist__for_each_entry(evlist, evsel) 576 evsel__merge_stats(evsel, config); 577 } 578 579 static void evsel__update_percore_stats(struct evsel *evsel, struct aggr_cpu_id *core_id) 580 { 581 struct perf_stat_evsel *ps = evsel->stats; 582 struct perf_counts_values counts = { 0, }; 583 struct aggr_cpu_id id; 584 struct perf_cpu cpu; 585 unsigned int idx; 586 587 /* collect per-core counts */ 588 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) { 589 struct perf_stat_aggr *aggr = &ps->aggr[idx]; 590 591 id = aggr_cpu_id__core(cpu, NULL); 592 if (!aggr_cpu_id__equal(core_id, &id)) 593 continue; 594 595 counts.val += aggr->counts.val; 596 counts.ena += aggr->counts.ena; 597 counts.run += aggr->counts.run; 598 } 599 600 /* update aggregated per-core counts for each CPU */ 601 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) { 602 struct perf_stat_aggr *aggr = &ps->aggr[idx]; 603 604 id = aggr_cpu_id__core(cpu, NULL); 605 if (!aggr_cpu_id__equal(core_id, &id)) 606 continue; 607 608 aggr->counts.val = counts.val; 609 aggr->counts.ena = counts.ena; 610 aggr->counts.run = counts.run; 611 612 aggr->used = true; 613 } 614 } 615 616 /* we have an aggr_map for cpu, but want to aggregate the counters per-core */ 617 static void evsel__process_percore(struct evsel *evsel) 618 { 619 struct perf_stat_evsel *ps = evsel->stats; 620 struct aggr_cpu_id core_id; 621 struct perf_cpu cpu; 622 unsigned int idx; 623 624 if (!evsel->percore) 625 return; 626 627 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) { 628 struct perf_stat_aggr *aggr = &ps->aggr[idx]; 629 630 if (aggr->used) 631 continue; 632 633 core_id = aggr_cpu_id__core(cpu, NULL); 634 evsel__update_percore_stats(evsel, &core_id); 635 } 636 } 637 638 /* process cpu stats on per-core events */ 639 void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist) 640 { 641 struct evsel *evsel; 642 643 if (config->aggr_mode != AGGR_NONE) 644 return; 645 646 evlist__for_each_entry(evlist, evsel) 647 evsel__process_percore(evsel); 648 } 649 650 int perf_event__process_stat_event(const struct perf_tool *tool __maybe_unused, 651 struct perf_session *session, 652 union perf_event *event) 653 { 654 struct perf_counts_values count, *ptr; 655 struct perf_record_stat *st = &event->stat; 656 struct evsel *counter; 657 int cpu_map_idx; 658 659 count.val = st->val; 660 count.ena = st->ena; 661 count.run = st->run; 662 663 counter = evlist__id2evsel(session->evlist, st->id); 664 if (!counter) { 665 pr_err("Failed to resolve counter for stat event.\n"); 666 return -EINVAL; 667 } 668 cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu}); 669 if (cpu_map_idx == -1) { 670 pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter)); 671 return -EINVAL; 672 } 673 ptr = perf_counts(counter->counts, cpu_map_idx, st->thread); 674 if (ptr == NULL) { 675 pr_err("Failed to find perf count for CPU %d thread %d on event %s.\n", 676 st->cpu, st->thread, evsel__name(counter)); 677 return -EINVAL; 678 } 679 *ptr = count; 680 counter->supported = true; 681 return 0; 682 } 683 684 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp) 685 { 686 struct perf_record_stat *st = (struct perf_record_stat *)event; 687 size_t ret; 688 689 ret = fprintf(fp, "\n... id %" PRI_lu64 ", cpu %d, thread %d\n", 690 st->id, st->cpu, st->thread); 691 ret += fprintf(fp, "... value %" PRI_lu64 ", enabled %" PRI_lu64 ", running %" PRI_lu64 "\n", 692 st->val, st->ena, st->run); 693 694 return ret; 695 } 696 697 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp) 698 { 699 struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event; 700 size_t ret; 701 702 ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time, 703 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL"); 704 705 return ret; 706 } 707 708 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp) 709 { 710 struct perf_stat_config sc = {}; 711 size_t ret; 712 713 perf_event__read_stat_config(&sc, &event->stat_config); 714 715 ret = fprintf(fp, "\n"); 716 ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode); 717 ret += fprintf(fp, "... scale %d\n", sc.scale); 718 ret += fprintf(fp, "... interval %u\n", sc.interval); 719 720 return ret; 721 } 722