1168054Sflz// SPDX-License-Identifier: GPL-2.0 2168054Sflz#include <errno.h> 3168266Sgabor#include <linux/err.h> 4168266Sgabor#include <inttypes.h> 5168266Sgabor#include <math.h> 6168266Sgabor#include <string.h> 7168266Sgabor#include "counts.h" 8168266Sgabor#include "cpumap.h" 9168266Sgabor#include "debug.h" 10168266Sgabor#include "header.h" 11168054Sflz#include "stat.h" 12168054Sflz#include "session.h" 13168064Sflz#include "target.h" 14168064Sflz#include "evlist.h" 15168064Sflz#include "evsel.h" 16168064Sflz#include "thread_map.h" 17168064Sflz#include "util/hashmap.h" 18168064Sflz#include <linux/zalloc.h> 19168064Sflz 20168064Sflzvoid update_stats(struct stats *stats, u64 val) 21168064Sflz{ 22168064Sflz double delta; 23168064Sflz 24168064Sflz stats->n++; 25168064Sflz delta = val - stats->mean; 26168064Sflz stats->mean += delta / stats->n; 27168054Sflz stats->M2 += delta*(val - stats->mean); 28168054Sflz 29168064Sflz if (val > stats->max) 30168054Sflz stats->max = val; 31168064Sflz 32171129Sobrien if (val < stats->min) 33168939Stmclaugh stats->min = val; 34168131Sbmah} 35168113Smarcus 36180225Smarceldouble avg_stats(struct stats *stats) 37168123Snetchild{ 38168939Stmclaugh return stats->mean; 39168064Sflz} 40168054Sflz 41168054Sflz/* 42168054Sflz * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 43168054Sflz * 44168261Sache * (\Sum n_i^2) - ((\Sum n_i)^2)/n 45168077Sflz * s^2 = ------------------------------- 46168077Sflz * n - 1 47168126Sale * 48168069Sgarga * http://en.wikipedia.org/wiki/Stddev 49168472Snovel * 50179877Samdmi3 * The std dev of the mean is related to the std dev by: 51168274Ssem * 52169073Saraujo * s 53168667Sstefan * s_mean = ------- 54209039Sashish * sqrt(n) 55203044Savilla * 56193190Savl */ 57168274Ssemdouble stddev_stats(struct stats *stats) 58210573Sbapt{ 59188692Sbeat double variance, variance_mean; 60170471Sbeech 61168113Smarcus if (stats->n < 2) 62173254Sbrix return 0.0; 63168098Skrion 64168123Snetchild variance = stats->M2 / (stats->n - 1); 65170601Schinsan variance_mean = variance / stats->n; 66168082Sgarga 67168116Sclsung return sqrt(variance_mean); 68168937Scperciva} 69168177Sgabor 70168354Sdanfedouble rel_stddev_stats(double stddev, double avg) 71168072Sehaupt{ 72206034Sdecke double pct = 0.0; 73168108Srafan 74168186Smat if (avg) 75168210Sitetcu pct = 100.0 * stddev/avg; 76168068Serwin 77168072Sehaupt return pct; 78168113Smarcus} 79168059Sgabor 80168542Smiwistatic void evsel__reset_aggr_stats(struct evsel *evsel) 81168098Skrion{ 82206495Sfluffy struct perf_stat_evsel *ps = evsel->stats; 83168054Sflz struct perf_stat_aggr *aggr = ps->aggr; 84168059Sgabor 85176595Sgahr if (aggr) 86168054Sflz memset(aggr, 0, sizeof(*aggr) * ps->nr_aggr); 87210773Sglarkin} 88210773Sglarkin 89172158Sjkimstatic void evsel__reset_stat_priv(struct evsel *evsel) 90172158Sjkim{ 91168256Sijliao struct perf_stat_evsel *ps = evsel->stats; 92168209Sitetcu 93206184Sjacula init_stats(&ps->res_stats); 94176768Sjadawin evsel__reset_aggr_stats(evsel); 95172158Sjkim} 96168076Sjmelo 97168123Snetchildstatic int evsel__alloc_aggr_stats(struct evsel *evsel, int nr_aggr) 98168054Sflz{ 99168055Spav struct perf_stat_evsel *ps = evsel->stats; 100182814Sjpaetzel 101210169Sjsa if (ps == NULL) 102168055Spav return 0; 103168536Skevlo 104168177Sgabor ps->nr_aggr = nr_aggr; 105168098Skrion ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr)); 106168055Spav if (ps->aggr == NULL) 107168161Sphilip return -ENOMEM; 108168054Sflz 109168208Sitetcu return 0; 110168295Sleeym} 111168295Sleeym 112172158Sjkimint evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr) 113175205Sedwin{ 114168939Stmclaugh struct evsel *evsel; 115176979Slippe 116168068Serwin evlist__for_each_entry(evlist, evsel) { 117168337Slwhsu if (evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) 118175205Sedwin return -1; 119168177Sgabor } 120199480Smandree return 0; 121199479Smandree} 122168113Smarcus 123168918Sbruefferstatic int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr) 124168186Smat{ 125168055Spav struct perf_stat_evsel *ps; 126168098Skrion 127168359Smm ps = zalloc(sizeof(*ps)); 128168100Smnag if (ps == NULL) 129169036Snemoliu return -ENOMEM; 130168123Snetchild 131168177Sgabor evsel->stats = ps; 132168134Snork 133168084Sehaupt if (nr_aggr && evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) { 134168542Smiwi evsel->stats = NULL; 135171129Sobrien free(ps); 136171129Sobrien return -ENOMEM; 137169253Sfjoe } 138168939Stmclaugh 139168054Sflz evsel__reset_stat_priv(evsel); 140190977Spgj return 0; 141168098Skrion} 142180729Spgollucci 143168108Srafanstatic void evsel__free_stat_priv(struct evsel *evsel) 144206489Srene{ 145180983Srnoland struct perf_stat_evsel *ps = evsel->stats; 146203046Sromain 147206532Ssahil if (ps) { 148168098Skrion zfree(&ps->aggr); 149168098Skrion zfree(&ps->group_data); 150168098Skrion } 151168055Spav zfree(&evsel->stats); 152168068Serwin} 153191885Sskreuzer 154168939Stmclaughstatic int evsel__alloc_prev_raw_counts(struct evsel *evsel) 155168290Ssem{ 156168667Sstefan int cpu_map_nr = evsel__nr_cpus(evsel); 157212219Sswills int nthreads = perf_thread_map__nr(evsel->core.threads); 158172276Stabthorpe struct perf_counts *counts; 159168125Stdb 160170675Stimur counts = perf_counts__new(cpu_map_nr, nthreads); 161192568Stota if (counts) 162169018Strasz evsel->prev_raw_counts = counts; 163168225Strhodes 164168186Smat return counts ? 0 : -ENOMEM; 165168061Sahze} 166168069Sgarga 167180728Swxsstatic void evsel__free_prev_raw_counts(struct evsel *evsel) 168168935Stmclaugh{ 169195800Syzlin perf_counts__delete(evsel->prev_raw_counts); 170172158Sjkim evsel->prev_raw_counts = NULL; 171168054Sflz} 172168054Sflz 173168064Sflzstatic void evsel__reset_prev_raw_counts(struct evsel *evsel) 174168064Sflz{ 175168054Sflz if (evsel->prev_raw_counts) 176168055Spav perf_counts__reset(evsel->prev_raw_counts); 177168055Spav} 178168055Spav 179168055Spavstatic int evsel__alloc_stats(struct evsel *evsel, int nr_aggr, bool alloc_raw) 180168055Spav{ 181182814Sjpaetzel if (evsel__alloc_stat_priv(evsel, nr_aggr) < 0 || 182182814Sjpaetzel evsel__alloc_counts(evsel) < 0 || 183168055Spav (alloc_raw && evsel__alloc_prev_raw_counts(evsel) < 0)) 184168057Sahze return -ENOMEM; 185168055Spav 186176979Slippe return 0; 187180729Spgollucci} 188176979Slippe 189168919Sbruefferint evlist__alloc_stats(struct perf_stat_config *config, 190168667Sstefan struct evlist *evlist, bool alloc_raw) 191168667Sstefan{ 192171129Sobrien struct evsel *evsel; 193171129Sobrien int nr_aggr = 0; 194206034Sdecke 195206034Sdecke if (config && config->aggr_map) 196188837Sbeech nr_aggr = config->aggr_map->nr; 197188837Sbeech 198188837Sbeech evlist__for_each_entry(evlist, evsel) { 199168939Stmclaugh if (evsel__alloc_stats(evsel, nr_aggr, alloc_raw)) 200168939Stmclaugh goto out_free; 201168939Stmclaugh } 202168125Stdb 203168208Sitetcu return 0; 204168125Stdb 205168337Slwhsuout_free: 206172275Stabthorpe evlist__free_stats(evlist); 207168337Slwhsu return -1; 208169036Snemoliu} 209168108Srafan 210168108Srafanvoid evlist__free_stats(struct evlist *evlist) 211168186Smat{ 212168186Smat struct evsel *evsel; 213168938Scperciva 214168068Serwin evlist__for_each_entry(evlist, evsel) { 215175205Sedwin evsel__free_stat_priv(evsel); 216175205Sedwin evsel__free_counts(evsel); 217168068Serwin evsel__free_prev_raw_counts(evsel); 218168072Sehaupt } 219168072Sehaupt} 220168274Ssem 221168225Strhodesvoid evlist__reset_stats(struct evlist *evlist) 222168225Strhodes{ 223173254Sbrix struct evsel *evsel; 224168068Serwin 225168059Sgabor evlist__for_each_entry(evlist, evsel) { 226168068Serwin evsel__reset_stat_priv(evsel); 227168068Serwin evsel__reset_counts(evsel); 228168068Serwin } 229168059Sgabor} 230168354Sdanfe 231168098Skrionvoid evlist__reset_aggr_stats(struct evlist *evlist) 232169251Sfjoe{ 233168098Skrion struct evsel *evsel; 234168054Sflz 235168054Sflz evlist__for_each_entry(evlist, evsel) 236168054Sflz evsel__reset_aggr_stats(evsel); 237203046Sromain} 238168054Sflz 239176979Slippevoid evlist__reset_prev_raw_counts(struct evlist *evlist) 240190977Spgj{ 241176979Slippe struct evsel *evsel; 242168069Sgarga 243168069Sgarga evlist__for_each_entry(evlist, evsel) 244199479Smandree evsel__reset_prev_raw_counts(evsel); 245168359Smm} 246180983Srnoland 247168069Sgargastatic void evsel__copy_prev_raw_counts(struct evsel *evsel) 248180728Swxs{ 249168935Stmclaugh int idx, nthreads = perf_thread_map__nr(evsel->core.threads); 250168069Sgarga 251193190Savl for (int thread = 0; thread < nthreads; thread++) { 252193190Savl perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { 253172158Sjkim *perf_counts(evsel->counts, idx, thread) = 254172158Sjkim *perf_counts(evsel->prev_raw_counts, idx, thread); 255172158Sjkim } 256168295Sleeym } 257168295Sleeym} 258169073Saraujo 259168210Sitetcuvoid evlist__copy_prev_raw_counts(struct evlist *evlist) 260206532Ssahil{ 261168210Sitetcu struct evsel *evsel; 262168123Snetchild 263168123Snetchild evlist__for_each_entry(evlist, evsel) 264168134Snork evsel__copy_prev_raw_counts(evsel); 265168134Snork} 266168134Snork 267168134Snorkstatic void evsel__copy_res_stats(struct evsel *evsel) 268168134Snork{ 269168098Skrion struct perf_stat_evsel *ps = evsel->stats; 270168098Skrion 271168098Skrion /* 272168098Skrion * For GLOBAL aggregation mode, it updates the counts for each run 273168098Skrion * in the evsel->stats.res_stats. See perf_stat_process_counter(). 274168098Skrion */ 275168098Skrion *ps->aggr[0].counts.values = avg_stats(&ps->res_stats); 276168098Skrion} 277210169Sjsa 278210169Sjsavoid evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist) 279168209Sitetcu{ 280168209Sitetcu struct evsel *evsel; 281168295Sleeym 282168295Sleeym if (config->aggr_mode != AGGR_GLOBAL) 283168939Stmclaugh return; 284168939Stmclaugh 285195800Syzlin evlist__for_each_entry(evlist, evsel) 286195800Syzlin evsel__copy_res_stats(evsel); 287192568Stota} 288192568Stota 289168687Sahzestatic size_t pkg_id_hash(long __key, void *ctx __maybe_unused) 290168113Smarcus{ 291168113Smarcus uint64_t *key = (uint64_t *) __key; 292168113Smarcus 293168113Smarcus return *key & 0xffffffff; 294168186Smat} 295168186Smat 296168936Stmclaughstatic bool pkg_id_equal(long __key1, long __key2, void *ctx __maybe_unused) 297168936Stmclaugh{ 298179877Samdmi3 uint64_t *key1 = (uint64_t *) __key1; 299203044Savilla uint64_t *key2 = (uint64_t *) __key2; 300188692Sbeat 301206034Sdecke return *key1 == *key2; 302168542Smiwi} 303206495Sfluffy 304176595Sgahrstatic int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals, 305188818Smakc int cpu_map_idx, bool *skip) 306199479Smandree{ 307188837Sbeech struct hashmap *mask = counter->per_pkg_mask; 308168542Smiwi struct perf_cpu_map *cpus = evsel__cpus(counter); 309172275Stabthorpe struct perf_cpu cpu = perf_cpu_map__cpu(cpus, cpu_map_idx); 310169018Strasz int s, d, ret = 0; 311168542Smiwi uint64_t *key; 312168076Sjmelo 313168076Sjmelo *skip = false; 314168123Snetchild 315168123Snetchild if (!counter->per_pkg) 316168126Sale return 0; 317168126Sale 318168472Snovel if (perf_cpu_map__has_any_cpu_or_is_empty(cpus)) 319168084Sehaupt return 0; 320168084Sehaupt 321171129Sobrien if (!mask) { 322171129Sobrien mask = hashmap__new(pkg_id_hash, pkg_id_equal, NULL); 323171129Sobrien if (IS_ERR(mask)) 324168939Stmclaugh return -ENOMEM; 325168939Stmclaugh 326168687Sahze counter->per_pkg_mask = mask; 327168054Sflz } 328168055Spav 329168055Spav /* 330168055Spav * we do not consider an event that has not run as a good 331168054Sflz * instance to mark a package as used (skip=1). Otherwise 332209039Sashish * we may run into a situation where the first CPU in a package 333206184Sjacula * is not running anything, yet the second is, and this function 334206184Sjacula * would mark the package as used after the first CPU and would 335212219Sswills * not read the values from the second CPU. 336212219Sswills */ 337168161Sphilip if (!(vals->run && vals->ena)) 338168161Sphilip return 0; 339170601Schinsan 340170601Schinsan s = cpu__get_socket_id(cpu); 341170471Sbeech if (s < 0) 342170471Sbeech return -1; 343168274Ssem 344168274Ssem /* 345168108Srafan * On multi-die system, die_id > 0. On no-die system, die_id = 0. 346168274Ssem * We use hashmap(socket, die) to check the used socket+die pair. 347168108Srafan */ 348170675Stimur d = cpu__get_die_id(cpu); 349170675Stimur if (d < 0) 350172158Sjkim return -1; 351172158Sjkim 352172158Sjkim key = malloc(sizeof(*key)); 353168939Stmclaugh if (!key) 354172158Sjkim return -ENOMEM; 355168939Stmclaugh 356169073Saraujo *key = (uint64_t)d << 32 | s; 357169073Saraujo if (hashmap__find(mask, key, NULL)) { 358168123Snetchild *skip = true; 359168123Snetchild free(key); 360209039Sashish } else 361203044Savilla ret = hashmap__add(mask, key, 1); 362193190Savl 363206495Sfluffy return ret; 364206184Sjacula} 365176768Sjadawin 366190977Spgjstatic bool evsel__count_has_error(struct evsel *evsel, 367206489Srene struct perf_counts_values *count, 368176768Sjadawin struct perf_stat_config *config) 369176768Sjadawin{ 370176768Sjadawin /* the evsel was failed already */ 371168209Sitetcu if (evsel->err || evsel->counts->scaled == -1) 372168935Stmclaugh return true; 373168209Sitetcu 374168939Stmclaugh /* this is meaningful for CPU aggregation modes only */ 375168939Stmclaugh if (config->aggr_mode == AGGR_GLOBAL) 376210169Sjsa return false; 377206532Ssahil 378191885Sskreuzer /* it's considered ok when it actually ran */ 379212219Sswills if (count->ena != 0 && count->run != 0) 380191885Sskreuzer return false; 381168054Sflz 382 return true; 383} 384 385static int 386process_counter_values(struct perf_stat_config *config, struct evsel *evsel, 387 int cpu_map_idx, int thread, 388 struct perf_counts_values *count) 389{ 390 struct perf_stat_evsel *ps = evsel->stats; 391 static struct perf_counts_values zero; 392 bool skip = false; 393 394 if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) { 395 pr_err("failed to read per-pkg counter\n"); 396 return -1; 397 } 398 399 if (skip) 400 count = &zero; 401 402 if (!evsel->snapshot) 403 evsel__compute_deltas(evsel, cpu_map_idx, thread, count); 404 perf_counts_values__scale(count, config->scale, NULL); 405 406 if (config->aggr_mode == AGGR_THREAD) { 407 struct perf_counts_values *aggr_counts = &ps->aggr[thread].counts; 408 409 /* 410 * Skip value 0 when enabling --per-thread globally, 411 * otherwise too many 0 output. 412 */ 413 if (count->val == 0 && config->system_wide) 414 return 0; 415 416 ps->aggr[thread].nr++; 417 418 aggr_counts->val += count->val; 419 aggr_counts->ena += count->ena; 420 aggr_counts->run += count->run; 421 return 0; 422 } 423 424 if (ps->aggr) { 425 struct perf_cpu cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx); 426 struct aggr_cpu_id aggr_id = config->aggr_get_id(config, cpu); 427 struct perf_stat_aggr *ps_aggr; 428 int i; 429 430 for (i = 0; i < ps->nr_aggr; i++) { 431 if (!aggr_cpu_id__equal(&aggr_id, &config->aggr_map->map[i])) 432 continue; 433 434 ps_aggr = &ps->aggr[i]; 435 ps_aggr->nr++; 436 437 /* 438 * When any result is bad, make them all to give consistent output 439 * in interval mode. But per-task counters can have 0 enabled time 440 * when some tasks are idle. 441 */ 442 if (evsel__count_has_error(evsel, count, config) && !ps_aggr->failed) { 443 ps_aggr->counts.val = 0; 444 ps_aggr->counts.ena = 0; 445 ps_aggr->counts.run = 0; 446 ps_aggr->failed = true; 447 } 448 449 if (!ps_aggr->failed) { 450 ps_aggr->counts.val += count->val; 451 ps_aggr->counts.ena += count->ena; 452 ps_aggr->counts.run += count->run; 453 } 454 break; 455 } 456 } 457 458 return 0; 459} 460 461static int process_counter_maps(struct perf_stat_config *config, 462 struct evsel *counter) 463{ 464 int nthreads = perf_thread_map__nr(counter->core.threads); 465 int ncpus = evsel__nr_cpus(counter); 466 int idx, thread; 467 468 for (thread = 0; thread < nthreads; thread++) { 469 for (idx = 0; idx < ncpus; idx++) { 470 if (process_counter_values(config, counter, idx, thread, 471 perf_counts(counter->counts, idx, thread))) 472 return -1; 473 } 474 } 475 476 return 0; 477} 478 479int perf_stat_process_counter(struct perf_stat_config *config, 480 struct evsel *counter) 481{ 482 struct perf_stat_evsel *ps = counter->stats; 483 u64 *count; 484 int ret; 485 486 if (counter->per_pkg) 487 evsel__zero_per_pkg(counter); 488 489 ret = process_counter_maps(config, counter); 490 if (ret) 491 return ret; 492 493 if (config->aggr_mode != AGGR_GLOBAL) 494 return 0; 495 496 /* 497 * GLOBAL aggregation mode only has a single aggr counts, 498 * so we can use ps->aggr[0] as the actual output. 499 */ 500 count = ps->aggr[0].counts.values; 501 update_stats(&ps->res_stats, *count); 502 503 if (verbose > 0) { 504 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", 505 evsel__name(counter), count[0], count[1], count[2]); 506 } 507 508 return 0; 509} 510 511static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias) 512{ 513 struct perf_stat_evsel *ps_a = evsel->stats; 514 struct perf_stat_evsel *ps_b = alias->stats; 515 int i; 516 517 if (ps_a->aggr == NULL && ps_b->aggr == NULL) 518 return 0; 519 520 if (ps_a->nr_aggr != ps_b->nr_aggr) { 521 pr_err("Unmatched aggregation mode between aliases\n"); 522 return -1; 523 } 524 525 for (i = 0; i < ps_a->nr_aggr; i++) { 526 struct perf_counts_values *aggr_counts_a = &ps_a->aggr[i].counts; 527 struct perf_counts_values *aggr_counts_b = &ps_b->aggr[i].counts; 528 529 /* NB: don't increase aggr.nr for aliases */ 530 531 aggr_counts_a->val += aggr_counts_b->val; 532 aggr_counts_a->ena += aggr_counts_b->ena; 533 aggr_counts_a->run += aggr_counts_b->run; 534 } 535 536 return 0; 537} 538/* events should have the same name, scale, unit, cgroup but on different PMUs */ 539static bool evsel__is_alias(struct evsel *evsel_a, struct evsel *evsel_b) 540{ 541 if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b))) 542 return false; 543 544 if (evsel_a->scale != evsel_b->scale) 545 return false; 546 547 if (evsel_a->cgrp != evsel_b->cgrp) 548 return false; 549 550 if (strcmp(evsel_a->unit, evsel_b->unit)) 551 return false; 552 553 if (evsel__is_clock(evsel_a) != evsel__is_clock(evsel_b)) 554 return false; 555 556 return !!strcmp(evsel_a->pmu_name, evsel_b->pmu_name); 557} 558 559static void evsel__merge_aliases(struct evsel *evsel) 560{ 561 struct evlist *evlist = evsel->evlist; 562 struct evsel *alias; 563 564 alias = list_prepare_entry(evsel, &(evlist->core.entries), core.node); 565 list_for_each_entry_continue(alias, &evlist->core.entries, core.node) { 566 /* Merge the same events on different PMUs. */ 567 if (evsel__is_alias(evsel, alias)) { 568 evsel__merge_aggr_counters(evsel, alias); 569 alias->merged_stat = true; 570 } 571 } 572} 573 574static bool evsel__should_merge_hybrid(const struct evsel *evsel, 575 const struct perf_stat_config *config) 576{ 577 return config->hybrid_merge && evsel__is_hybrid(evsel); 578} 579 580static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_config *config) 581{ 582 /* this evsel is already merged */ 583 if (evsel->merged_stat) 584 return; 585 586 if (evsel->auto_merge_stats || evsel__should_merge_hybrid(evsel, config)) 587 evsel__merge_aliases(evsel); 588} 589 590/* merge the same uncore and hybrid events if requested */ 591void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist) 592{ 593 struct evsel *evsel; 594 595 if (config->aggr_mode == AGGR_NONE) 596 return; 597 598 evlist__for_each_entry(evlist, evsel) 599 evsel__merge_stats(evsel, config); 600} 601 602static void evsel__update_percore_stats(struct evsel *evsel, struct aggr_cpu_id *core_id) 603{ 604 struct perf_stat_evsel *ps = evsel->stats; 605 struct perf_counts_values counts = { 0, }; 606 struct aggr_cpu_id id; 607 struct perf_cpu cpu; 608 int idx; 609 610 /* collect per-core counts */ 611 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) { 612 struct perf_stat_aggr *aggr = &ps->aggr[idx]; 613 614 id = aggr_cpu_id__core(cpu, NULL); 615 if (!aggr_cpu_id__equal(core_id, &id)) 616 continue; 617 618 counts.val += aggr->counts.val; 619 counts.ena += aggr->counts.ena; 620 counts.run += aggr->counts.run; 621 } 622 623 /* update aggregated per-core counts for each CPU */ 624 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) { 625 struct perf_stat_aggr *aggr = &ps->aggr[idx]; 626 627 id = aggr_cpu_id__core(cpu, NULL); 628 if (!aggr_cpu_id__equal(core_id, &id)) 629 continue; 630 631 aggr->counts.val = counts.val; 632 aggr->counts.ena = counts.ena; 633 aggr->counts.run = counts.run; 634 635 aggr->used = true; 636 } 637} 638 639/* we have an aggr_map for cpu, but want to aggregate the counters per-core */ 640static void evsel__process_percore(struct evsel *evsel) 641{ 642 struct perf_stat_evsel *ps = evsel->stats; 643 struct aggr_cpu_id core_id; 644 struct perf_cpu cpu; 645 int idx; 646 647 if (!evsel->percore) 648 return; 649 650 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) { 651 struct perf_stat_aggr *aggr = &ps->aggr[idx]; 652 653 if (aggr->used) 654 continue; 655 656 core_id = aggr_cpu_id__core(cpu, NULL); 657 evsel__update_percore_stats(evsel, &core_id); 658 } 659} 660 661/* process cpu stats on per-core events */ 662void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist) 663{ 664 struct evsel *evsel; 665 666 if (config->aggr_mode != AGGR_NONE) 667 return; 668 669 evlist__for_each_entry(evlist, evsel) 670 evsel__process_percore(evsel); 671} 672 673int perf_event__process_stat_event(struct perf_session *session, 674 union perf_event *event) 675{ 676 struct perf_counts_values count, *ptr; 677 struct perf_record_stat *st = &event->stat; 678 struct evsel *counter; 679 int cpu_map_idx; 680 681 count.val = st->val; 682 count.ena = st->ena; 683 count.run = st->run; 684 685 counter = evlist__id2evsel(session->evlist, st->id); 686 if (!counter) { 687 pr_err("Failed to resolve counter for stat event.\n"); 688 return -EINVAL; 689 } 690 cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu}); 691 if (cpu_map_idx == -1) { 692 pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter)); 693 return -EINVAL; 694 } 695 ptr = perf_counts(counter->counts, cpu_map_idx, st->thread); 696 if (ptr == NULL) { 697 pr_err("Failed to find perf count for CPU %d thread %d on event %s.\n", 698 st->cpu, st->thread, evsel__name(counter)); 699 return -EINVAL; 700 } 701 *ptr = count; 702 counter->supported = true; 703 return 0; 704} 705 706size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp) 707{ 708 struct perf_record_stat *st = (struct perf_record_stat *)event; 709 size_t ret; 710 711 ret = fprintf(fp, "\n... id %" PRI_lu64 ", cpu %d, thread %d\n", 712 st->id, st->cpu, st->thread); 713 ret += fprintf(fp, "... value %" PRI_lu64 ", enabled %" PRI_lu64 ", running %" PRI_lu64 "\n", 714 st->val, st->ena, st->run); 715 716 return ret; 717} 718 719size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp) 720{ 721 struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event; 722 size_t ret; 723 724 ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time, 725 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL"); 726 727 return ret; 728} 729 730size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp) 731{ 732 struct perf_stat_config sc = {}; 733 size_t ret; 734 735 perf_event__read_stat_config(&sc, &event->stat_config); 736 737 ret = fprintf(fp, "\n"); 738 ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode); 739 ret += fprintf(fp, "... scale %d\n", sc.scale); 740 ret += fprintf(fp, "... interval %u\n", sc.interval); 741 742 return ret; 743} 744 745int create_perf_stat_counter(struct evsel *evsel, 746 struct perf_stat_config *config, 747 struct target *target, 748 int cpu_map_idx) 749{ 750 struct perf_event_attr *attr = &evsel->core.attr; 751 struct evsel *leader = evsel__leader(evsel); 752 753 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 754 PERF_FORMAT_TOTAL_TIME_RUNNING; 755 756 /* 757 * The event is part of non trivial group, let's enable 758 * the group read (for leader) and ID retrieval for all 759 * members. 760 */ 761 if (leader->core.nr_members > 1) 762 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP; 763 764 attr->inherit = !config->no_inherit && list_empty(&evsel->bpf_counter_list); 765 766 /* 767 * Some events get initialized with sample_(period/type) set, 768 * like tracepoints. Clear it up for counting. 769 */ 770 attr->sample_period = 0; 771 772 if (config->identifier) 773 attr->sample_type = PERF_SAMPLE_IDENTIFIER; 774 775 if (config->all_user) { 776 attr->exclude_kernel = 1; 777 attr->exclude_user = 0; 778 } 779 780 if (config->all_kernel) { 781 attr->exclude_kernel = 0; 782 attr->exclude_user = 1; 783 } 784 785 /* 786 * Disabling all counters initially, they will be enabled 787 * either manually by us or by kernel via enable_on_exec 788 * set later. 789 */ 790 if (evsel__is_group_leader(evsel)) { 791 attr->disabled = 1; 792 793 if (target__enable_on_exec(target)) 794 attr->enable_on_exec = 1; 795 } 796 797 if (target__has_cpu(target) && !target__has_per_thread(target)) 798 return evsel__open_per_cpu(evsel, evsel__cpus(evsel), cpu_map_idx); 799 800 return evsel__open_per_thread(evsel, evsel->core.threads); 801} 802