1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2a43783aeSArnaldo Carvalho de Melo #include <errno.h> 3fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 40007eceaSXiao Guangrong #include <math.h> 50007eceaSXiao Guangrong #include "stat.h" 624e34f68SJiri Olsa #include "evlist.h" 7e2f56da1SJiri Olsa #include "evsel.h" 824e34f68SJiri Olsa #include "thread_map.h" 97f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 100007eceaSXiao Guangrong 110007eceaSXiao Guangrong void update_stats(struct stats *stats, u64 val) 120007eceaSXiao Guangrong { 130007eceaSXiao Guangrong double delta; 140007eceaSXiao Guangrong 150007eceaSXiao Guangrong stats->n++; 160007eceaSXiao Guangrong delta = val - stats->mean; 170007eceaSXiao Guangrong stats->mean += delta / stats->n; 180007eceaSXiao Guangrong stats->M2 += delta*(val - stats->mean); 19ffe4f3c0SDavid Ahern 20ffe4f3c0SDavid Ahern if (val > stats->max) 21ffe4f3c0SDavid Ahern stats->max = val; 22ffe4f3c0SDavid Ahern 23ffe4f3c0SDavid Ahern if (val < stats->min) 24ffe4f3c0SDavid Ahern stats->min = val; 250007eceaSXiao Guangrong } 260007eceaSXiao Guangrong 270007eceaSXiao Guangrong double avg_stats(struct stats *stats) 280007eceaSXiao Guangrong { 290007eceaSXiao Guangrong return stats->mean; 300007eceaSXiao Guangrong } 310007eceaSXiao Guangrong 320007eceaSXiao Guangrong /* 330007eceaSXiao Guangrong * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 340007eceaSXiao Guangrong * 350007eceaSXiao Guangrong * (\Sum n_i^2) - ((\Sum n_i)^2)/n 360007eceaSXiao Guangrong * s^2 = ------------------------------- 370007eceaSXiao Guangrong * n - 1 380007eceaSXiao Guangrong * 390007eceaSXiao Guangrong * http://en.wikipedia.org/wiki/Stddev 400007eceaSXiao Guangrong * 410007eceaSXiao Guangrong * The std dev of the mean is related to the std dev by: 420007eceaSXiao Guangrong * 430007eceaSXiao Guangrong * s 440007eceaSXiao Guangrong * s_mean = ------- 450007eceaSXiao Guangrong * sqrt(n) 460007eceaSXiao Guangrong * 470007eceaSXiao Guangrong */ 480007eceaSXiao Guangrong double stddev_stats(struct stats *stats) 490007eceaSXiao Guangrong { 500007eceaSXiao Guangrong double variance, variance_mean; 510007eceaSXiao Guangrong 5245528f7cSDavid Ahern if (stats->n < 2) 530007eceaSXiao Guangrong return 0.0; 540007eceaSXiao Guangrong 550007eceaSXiao Guangrong variance = stats->M2 / (stats->n - 1); 560007eceaSXiao Guangrong variance_mean = variance / stats->n; 570007eceaSXiao Guangrong 580007eceaSXiao Guangrong return sqrt(variance_mean); 590007eceaSXiao Guangrong } 600007eceaSXiao Guangrong 610007eceaSXiao Guangrong double rel_stddev_stats(double stddev, double avg) 620007eceaSXiao Guangrong { 630007eceaSXiao Guangrong double pct = 0.0; 640007eceaSXiao Guangrong 650007eceaSXiao Guangrong if (avg) 660007eceaSXiao Guangrong pct = 100.0 * stddev/avg; 670007eceaSXiao Guangrong 680007eceaSXiao Guangrong return pct; 690007eceaSXiao Guangrong } 70e2f56da1SJiri Olsa 71e2f56da1SJiri Olsa bool __perf_evsel_stat__is(struct perf_evsel *evsel, 72e2f56da1SJiri Olsa enum perf_stat_evsel_id id) 73e2f56da1SJiri Olsa { 74e669e833SArnaldo Carvalho de Melo struct perf_stat_evsel *ps = evsel->stats; 75e2f56da1SJiri Olsa 76e2f56da1SJiri Olsa return ps->id == id; 77e2f56da1SJiri Olsa } 78e2f56da1SJiri Olsa 79e2f56da1SJiri Olsa #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name 80e2f56da1SJiri Olsa static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = { 81e2f56da1SJiri Olsa ID(NONE, x), 824c358d5cSJiri Olsa ID(CYCLES_IN_TX, cpu/cycles-t/), 834c358d5cSJiri Olsa ID(TRANSACTION_START, cpu/tx-start/), 844c358d5cSJiri Olsa ID(ELISION_START, cpu/el-start/), 854c358d5cSJiri Olsa ID(CYCLES_IN_TX_CP, cpu/cycles-ct/), 86239bd47fSAndi Kleen ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots), 87239bd47fSAndi Kleen ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued), 88239bd47fSAndi Kleen ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired), 89239bd47fSAndi Kleen ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles), 90239bd47fSAndi Kleen ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles), 91daefd0bcSKan Liang ID(SMI_NUM, msr/smi/), 92daefd0bcSKan Liang ID(APERF, msr/aperf/), 93e2f56da1SJiri Olsa }; 94e2f56da1SJiri Olsa #undef ID 95e2f56da1SJiri Olsa 9626e4711fSThomas Richter static void perf_stat_evsel_id_init(struct perf_evsel *evsel) 97e2f56da1SJiri Olsa { 98e669e833SArnaldo Carvalho de Melo struct perf_stat_evsel *ps = evsel->stats; 99e2f56da1SJiri Olsa int i; 100e2f56da1SJiri Olsa 101e2f56da1SJiri Olsa /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */ 102e2f56da1SJiri Olsa 103e2f56da1SJiri Olsa for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) { 104e2f56da1SJiri Olsa if (!strcmp(perf_evsel__name(evsel), id_str[i])) { 105e2f56da1SJiri Olsa ps->id = i; 106e2f56da1SJiri Olsa break; 107e2f56da1SJiri Olsa } 108e2f56da1SJiri Olsa } 109e2f56da1SJiri Olsa } 110a9a3a4d9SJiri Olsa 11186a2cf31SJiri Olsa static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel) 1129689edfaSJiri Olsa { 1139689edfaSJiri Olsa int i; 114e669e833SArnaldo Carvalho de Melo struct perf_stat_evsel *ps = evsel->stats; 1159689edfaSJiri Olsa 1169689edfaSJiri Olsa for (i = 0; i < 3; i++) 1179689edfaSJiri Olsa init_stats(&ps->res_stats[i]); 1189689edfaSJiri Olsa 1199689edfaSJiri Olsa perf_stat_evsel_id_init(evsel); 1209689edfaSJiri Olsa } 1219689edfaSJiri Olsa 12286a2cf31SJiri Olsa static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel) 1239689edfaSJiri Olsa { 124e669e833SArnaldo Carvalho de Melo evsel->stats = zalloc(sizeof(struct perf_stat_evsel)); 125e669e833SArnaldo Carvalho de Melo if (evsel->stats == NULL) 1269689edfaSJiri Olsa return -ENOMEM; 1279689edfaSJiri Olsa perf_evsel__reset_stat_priv(evsel); 1289689edfaSJiri Olsa return 0; 1299689edfaSJiri Olsa } 1309689edfaSJiri Olsa 13186a2cf31SJiri Olsa static void perf_evsel__free_stat_priv(struct perf_evsel *evsel) 1329689edfaSJiri Olsa { 133e669e833SArnaldo Carvalho de Melo struct perf_stat_evsel *ps = evsel->stats; 134f7794d52SJiri Olsa 135f7794d52SJiri Olsa if (ps) 136d8f9da24SArnaldo Carvalho de Melo zfree(&ps->group_data); 137e669e833SArnaldo Carvalho de Melo zfree(&evsel->stats); 1389689edfaSJiri Olsa } 139a939512dSJiri Olsa 14086a2cf31SJiri Olsa static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel, 141a939512dSJiri Olsa int ncpus, int nthreads) 142a939512dSJiri Olsa { 143a939512dSJiri Olsa struct perf_counts *counts; 144a939512dSJiri Olsa 145a939512dSJiri Olsa counts = perf_counts__new(ncpus, nthreads); 146a939512dSJiri Olsa if (counts) 147a939512dSJiri Olsa evsel->prev_raw_counts = counts; 148a939512dSJiri Olsa 149a939512dSJiri Olsa return counts ? 0 : -ENOMEM; 150a939512dSJiri Olsa } 151a939512dSJiri Olsa 15286a2cf31SJiri Olsa static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel) 153a939512dSJiri Olsa { 154a939512dSJiri Olsa perf_counts__delete(evsel->prev_raw_counts); 155a939512dSJiri Olsa evsel->prev_raw_counts = NULL; 156a939512dSJiri Olsa } 15724e34f68SJiri Olsa 15886a2cf31SJiri Olsa static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw) 15924e34f68SJiri Olsa { 16024e34f68SJiri Olsa int ncpus = perf_evsel__nr_cpus(evsel); 161a7d0a102SJiri Olsa int nthreads = thread_map__nr(evsel->threads); 16224e34f68SJiri Olsa 16324e34f68SJiri Olsa if (perf_evsel__alloc_stat_priv(evsel) < 0 || 16424e34f68SJiri Olsa perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 || 16524e34f68SJiri Olsa (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0)) 166a7d0a102SJiri Olsa return -ENOMEM; 167a7d0a102SJiri Olsa 168a7d0a102SJiri Olsa return 0; 169a7d0a102SJiri Olsa } 170a7d0a102SJiri Olsa 171a7d0a102SJiri Olsa int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw) 172a7d0a102SJiri Olsa { 173a7d0a102SJiri Olsa struct perf_evsel *evsel; 174a7d0a102SJiri Olsa 175e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 176a7d0a102SJiri Olsa if (perf_evsel__alloc_stats(evsel, alloc_raw)) 17724e34f68SJiri Olsa goto out_free; 17824e34f68SJiri Olsa } 17924e34f68SJiri Olsa 18024e34f68SJiri Olsa return 0; 18124e34f68SJiri Olsa 18224e34f68SJiri Olsa out_free: 18324e34f68SJiri Olsa perf_evlist__free_stats(evlist); 18424e34f68SJiri Olsa return -1; 18524e34f68SJiri Olsa } 18624e34f68SJiri Olsa 18724e34f68SJiri Olsa void perf_evlist__free_stats(struct perf_evlist *evlist) 18824e34f68SJiri Olsa { 18924e34f68SJiri Olsa struct perf_evsel *evsel; 19024e34f68SJiri Olsa 191e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 19224e34f68SJiri Olsa perf_evsel__free_stat_priv(evsel); 19324e34f68SJiri Olsa perf_evsel__free_counts(evsel); 19424e34f68SJiri Olsa perf_evsel__free_prev_raw_counts(evsel); 19524e34f68SJiri Olsa } 19624e34f68SJiri Olsa } 19724e34f68SJiri Olsa 19824e34f68SJiri Olsa void perf_evlist__reset_stats(struct perf_evlist *evlist) 19924e34f68SJiri Olsa { 20024e34f68SJiri Olsa struct perf_evsel *evsel; 20124e34f68SJiri Olsa 202e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 20324e34f68SJiri Olsa perf_evsel__reset_stat_priv(evsel); 20424e34f68SJiri Olsa perf_evsel__reset_counts(evsel); 20524e34f68SJiri Olsa } 20624e34f68SJiri Olsa } 207f80010ebSJiri Olsa 208f80010ebSJiri Olsa static void zero_per_pkg(struct perf_evsel *counter) 209f80010ebSJiri Olsa { 210f80010ebSJiri Olsa if (counter->per_pkg_mask) 211f80010ebSJiri Olsa memset(counter->per_pkg_mask, 0, MAX_NR_CPUS); 212f80010ebSJiri Olsa } 213f80010ebSJiri Olsa 21402d8dabcSStephane Eranian static int check_per_pkg(struct perf_evsel *counter, 21502d8dabcSStephane Eranian struct perf_counts_values *vals, int cpu, bool *skip) 216f80010ebSJiri Olsa { 217f80010ebSJiri Olsa unsigned long *mask = counter->per_pkg_mask; 218f854839bSJiri Olsa struct perf_cpu_map *cpus = perf_evsel__cpus(counter); 219f80010ebSJiri Olsa int s; 220f80010ebSJiri Olsa 221f80010ebSJiri Olsa *skip = false; 222f80010ebSJiri Olsa 223f80010ebSJiri Olsa if (!counter->per_pkg) 224f80010ebSJiri Olsa return 0; 225f80010ebSJiri Olsa 226f80010ebSJiri Olsa if (cpu_map__empty(cpus)) 227f80010ebSJiri Olsa return 0; 228f80010ebSJiri Olsa 229f80010ebSJiri Olsa if (!mask) { 230f80010ebSJiri Olsa mask = zalloc(MAX_NR_CPUS); 231f80010ebSJiri Olsa if (!mask) 232f80010ebSJiri Olsa return -ENOMEM; 233f80010ebSJiri Olsa 234f80010ebSJiri Olsa counter->per_pkg_mask = mask; 235f80010ebSJiri Olsa } 236f80010ebSJiri Olsa 23702d8dabcSStephane Eranian /* 23802d8dabcSStephane Eranian * we do not consider an event that has not run as a good 23902d8dabcSStephane Eranian * instance to mark a package as used (skip=1). Otherwise 24002d8dabcSStephane Eranian * we may run into a situation where the first CPU in a package 24102d8dabcSStephane Eranian * is not running anything, yet the second is, and this function 24202d8dabcSStephane Eranian * would mark the package as used after the first CPU and would 24302d8dabcSStephane Eranian * not read the values from the second CPU. 24402d8dabcSStephane Eranian */ 24502d8dabcSStephane Eranian if (!(vals->run && vals->ena)) 24602d8dabcSStephane Eranian return 0; 24702d8dabcSStephane Eranian 2481fe7a300SJiri Olsa s = cpu_map__get_socket(cpus, cpu, NULL); 249f80010ebSJiri Olsa if (s < 0) 250f80010ebSJiri Olsa return -1; 251f80010ebSJiri Olsa 252f80010ebSJiri Olsa *skip = test_and_set_bit(s, mask) == 1; 253f80010ebSJiri Olsa return 0; 254f80010ebSJiri Olsa } 255f80010ebSJiri Olsa 256f80010ebSJiri Olsa static int 257f80010ebSJiri Olsa process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel, 258f80010ebSJiri Olsa int cpu, int thread, 259f80010ebSJiri Olsa struct perf_counts_values *count) 260f80010ebSJiri Olsa { 261f80010ebSJiri Olsa struct perf_counts_values *aggr = &evsel->counts->aggr; 262f80010ebSJiri Olsa static struct perf_counts_values zero; 263f80010ebSJiri Olsa bool skip = false; 264f80010ebSJiri Olsa 26502d8dabcSStephane Eranian if (check_per_pkg(evsel, count, cpu, &skip)) { 266f80010ebSJiri Olsa pr_err("failed to read per-pkg counter\n"); 267f80010ebSJiri Olsa return -1; 268f80010ebSJiri Olsa } 269f80010ebSJiri Olsa 270f80010ebSJiri Olsa if (skip) 271f80010ebSJiri Olsa count = &zero; 272f80010ebSJiri Olsa 273f80010ebSJiri Olsa switch (config->aggr_mode) { 274f80010ebSJiri Olsa case AGGR_THREAD: 275f80010ebSJiri Olsa case AGGR_CORE: 276db5742b6SKan Liang case AGGR_DIE: 277f80010ebSJiri Olsa case AGGR_SOCKET: 278f80010ebSJiri Olsa case AGGR_NONE: 279f80010ebSJiri Olsa if (!evsel->snapshot) 280f80010ebSJiri Olsa perf_evsel__compute_deltas(evsel, cpu, thread, count); 281f80010ebSJiri Olsa perf_counts_values__scale(count, config->scale, NULL); 2824fc4d8dfSJin Yao if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) { 2834fc4d8dfSJin Yao perf_stat__update_shadow_stats(evsel, count->val, 2844fc4d8dfSJin Yao cpu, &rt_stat); 2854fc4d8dfSJin Yao } 2864fc4d8dfSJin Yao 28714e72a21SJin Yao if (config->aggr_mode == AGGR_THREAD) { 28814e72a21SJin Yao if (config->stats) 28914e72a21SJin Yao perf_stat__update_shadow_stats(evsel, 29014e72a21SJin Yao count->val, 0, &config->stats[thread]); 29114e72a21SJin Yao else 29214e72a21SJin Yao perf_stat__update_shadow_stats(evsel, 29314e72a21SJin Yao count->val, 0, &rt_stat); 29414e72a21SJin Yao } 295f80010ebSJiri Olsa break; 296f80010ebSJiri Olsa case AGGR_GLOBAL: 297f80010ebSJiri Olsa aggr->val += count->val; 298f80010ebSJiri Olsa aggr->ena += count->ena; 299f80010ebSJiri Olsa aggr->run += count->run; 300208df99eSJiri Olsa case AGGR_UNSET: 301f80010ebSJiri Olsa default: 302f80010ebSJiri Olsa break; 303f80010ebSJiri Olsa } 304f80010ebSJiri Olsa 305f80010ebSJiri Olsa return 0; 306f80010ebSJiri Olsa } 307f80010ebSJiri Olsa 308f80010ebSJiri Olsa static int process_counter_maps(struct perf_stat_config *config, 309f80010ebSJiri Olsa struct perf_evsel *counter) 310f80010ebSJiri Olsa { 311f80010ebSJiri Olsa int nthreads = thread_map__nr(counter->threads); 312f80010ebSJiri Olsa int ncpus = perf_evsel__nr_cpus(counter); 313f80010ebSJiri Olsa int cpu, thread; 314f80010ebSJiri Olsa 315f80010ebSJiri Olsa if (counter->system_wide) 316f80010ebSJiri Olsa nthreads = 1; 317f80010ebSJiri Olsa 318f80010ebSJiri Olsa for (thread = 0; thread < nthreads; thread++) { 319f80010ebSJiri Olsa for (cpu = 0; cpu < ncpus; cpu++) { 320f80010ebSJiri Olsa if (process_counter_values(config, counter, cpu, thread, 321f80010ebSJiri Olsa perf_counts(counter->counts, cpu, thread))) 322f80010ebSJiri Olsa return -1; 323f80010ebSJiri Olsa } 324f80010ebSJiri Olsa } 325f80010ebSJiri Olsa 326f80010ebSJiri Olsa return 0; 327f80010ebSJiri Olsa } 328f80010ebSJiri Olsa 329f80010ebSJiri Olsa int perf_stat_process_counter(struct perf_stat_config *config, 330f80010ebSJiri Olsa struct perf_evsel *counter) 331f80010ebSJiri Olsa { 332f80010ebSJiri Olsa struct perf_counts_values *aggr = &counter->counts->aggr; 333e669e833SArnaldo Carvalho de Melo struct perf_stat_evsel *ps = counter->stats; 334f80010ebSJiri Olsa u64 *count = counter->counts->aggr.values; 335f80010ebSJiri Olsa int i, ret; 336f80010ebSJiri Olsa 337f80010ebSJiri Olsa aggr->val = aggr->ena = aggr->run = 0; 338f80010ebSJiri Olsa 33951fd2df1SJiri Olsa /* 34051fd2df1SJiri Olsa * We calculate counter's data every interval, 34151fd2df1SJiri Olsa * and the display code shows ps->res_stats 34251fd2df1SJiri Olsa * avg value. We need to zero the stats for 34351fd2df1SJiri Olsa * interval mode, otherwise overall avg running 34451fd2df1SJiri Olsa * averages will be shown for each interval. 34551fd2df1SJiri Olsa */ 34651fd2df1SJiri Olsa if (config->interval) 34751fd2df1SJiri Olsa init_stats(ps->res_stats); 34851fd2df1SJiri Olsa 349f80010ebSJiri Olsa if (counter->per_pkg) 350f80010ebSJiri Olsa zero_per_pkg(counter); 351f80010ebSJiri Olsa 352f80010ebSJiri Olsa ret = process_counter_maps(config, counter); 353f80010ebSJiri Olsa if (ret) 354f80010ebSJiri Olsa return ret; 355f80010ebSJiri Olsa 356f80010ebSJiri Olsa if (config->aggr_mode != AGGR_GLOBAL) 357f80010ebSJiri Olsa return 0; 358f80010ebSJiri Olsa 359f80010ebSJiri Olsa if (!counter->snapshot) 360f80010ebSJiri Olsa perf_evsel__compute_deltas(counter, -1, -1, aggr); 361f80010ebSJiri Olsa perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled); 362f80010ebSJiri Olsa 363f80010ebSJiri Olsa for (i = 0; i < 3; i++) 364f80010ebSJiri Olsa update_stats(&ps->res_stats[i], count[i]); 365f80010ebSJiri Olsa 366bb963e16SNamhyung Kim if (verbose > 0) { 367f80010ebSJiri Olsa fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", 368f80010ebSJiri Olsa perf_evsel__name(counter), count[0], count[1], count[2]); 369f80010ebSJiri Olsa } 370f80010ebSJiri Olsa 371f80010ebSJiri Olsa /* 372f80010ebSJiri Olsa * Save the full runtime - to allow normalization during printout: 373f80010ebSJiri Olsa */ 3741fcd0394SJin Yao perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat); 375f80010ebSJiri Olsa 376f80010ebSJiri Olsa return 0; 377f80010ebSJiri Olsa } 3780ea0e355SJiri Olsa 37989f1688aSJiri Olsa int perf_event__process_stat_event(struct perf_session *session, 38089f1688aSJiri Olsa union perf_event *event) 3810ea0e355SJiri Olsa { 3820ea0e355SJiri Olsa struct perf_counts_values count; 3830ea0e355SJiri Olsa struct stat_event *st = &event->stat; 3840ea0e355SJiri Olsa struct perf_evsel *counter; 3850ea0e355SJiri Olsa 3860ea0e355SJiri Olsa count.val = st->val; 3870ea0e355SJiri Olsa count.ena = st->ena; 3880ea0e355SJiri Olsa count.run = st->run; 3890ea0e355SJiri Olsa 3900ea0e355SJiri Olsa counter = perf_evlist__id2evsel(session->evlist, st->id); 3910ea0e355SJiri Olsa if (!counter) { 3920ea0e355SJiri Olsa pr_err("Failed to resolve counter for stat event.\n"); 3930ea0e355SJiri Olsa return -EINVAL; 3940ea0e355SJiri Olsa } 3950ea0e355SJiri Olsa 3960ea0e355SJiri Olsa *perf_counts(counter->counts, st->cpu, st->thread) = count; 3970ea0e355SJiri Olsa counter->supported = true; 3980ea0e355SJiri Olsa return 0; 3990ea0e355SJiri Olsa } 400e08a4564SJiri Olsa 401e08a4564SJiri Olsa size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp) 402e08a4564SJiri Olsa { 403e08a4564SJiri Olsa struct stat_event *st = (struct stat_event *) event; 404e08a4564SJiri Olsa size_t ret; 405e08a4564SJiri Olsa 406e08a4564SJiri Olsa ret = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n", 407e08a4564SJiri Olsa st->id, st->cpu, st->thread); 408e08a4564SJiri Olsa ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n", 409e08a4564SJiri Olsa st->val, st->ena, st->run); 410e08a4564SJiri Olsa 411e08a4564SJiri Olsa return ret; 412e08a4564SJiri Olsa } 413e08a4564SJiri Olsa 414e08a4564SJiri Olsa size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp) 415e08a4564SJiri Olsa { 416e08a4564SJiri Olsa struct stat_round_event *rd = (struct stat_round_event *)event; 417e08a4564SJiri Olsa size_t ret; 418e08a4564SJiri Olsa 419e08a4564SJiri Olsa ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time, 420e08a4564SJiri Olsa rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL"); 421e08a4564SJiri Olsa 422e08a4564SJiri Olsa return ret; 423e08a4564SJiri Olsa } 424e08a4564SJiri Olsa 425e08a4564SJiri Olsa size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp) 426e08a4564SJiri Olsa { 427e08a4564SJiri Olsa struct perf_stat_config sc; 428e08a4564SJiri Olsa size_t ret; 429e08a4564SJiri Olsa 430e08a4564SJiri Olsa perf_event__read_stat_config(&sc, &event->stat_config); 431e08a4564SJiri Olsa 432e08a4564SJiri Olsa ret = fprintf(fp, "\n"); 433e08a4564SJiri Olsa ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode); 434e08a4564SJiri Olsa ret += fprintf(fp, "... scale %d\n", sc.scale); 435e08a4564SJiri Olsa ret += fprintf(fp, "... interval %u\n", sc.interval); 436e08a4564SJiri Olsa 437e08a4564SJiri Olsa return ret; 438e08a4564SJiri Olsa } 439d09cefd2SJiri Olsa 440d09cefd2SJiri Olsa int create_perf_stat_counter(struct perf_evsel *evsel, 441d09cefd2SJiri Olsa struct perf_stat_config *config, 442d09cefd2SJiri Olsa struct target *target) 443d09cefd2SJiri Olsa { 444d09cefd2SJiri Olsa struct perf_event_attr *attr = &evsel->attr; 445d09cefd2SJiri Olsa struct perf_evsel *leader = evsel->leader; 446d09cefd2SJiri Olsa 447d09cefd2SJiri Olsa attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 448d09cefd2SJiri Olsa PERF_FORMAT_TOTAL_TIME_RUNNING; 449d09cefd2SJiri Olsa 450d09cefd2SJiri Olsa /* 451d09cefd2SJiri Olsa * The event is part of non trivial group, let's enable 452d09cefd2SJiri Olsa * the group read (for leader) and ID retrieval for all 453d09cefd2SJiri Olsa * members. 454d09cefd2SJiri Olsa */ 455d09cefd2SJiri Olsa if (leader->nr_members > 1) 456d09cefd2SJiri Olsa attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP; 457d09cefd2SJiri Olsa 458d09cefd2SJiri Olsa attr->inherit = !config->no_inherit; 459d09cefd2SJiri Olsa 460d09cefd2SJiri Olsa /* 461d09cefd2SJiri Olsa * Some events get initialized with sample_(period/type) set, 462d09cefd2SJiri Olsa * like tracepoints. Clear it up for counting. 463d09cefd2SJiri Olsa */ 464d09cefd2SJiri Olsa attr->sample_period = 0; 465d09cefd2SJiri Olsa 466d09cefd2SJiri Olsa if (config->identifier) 467d09cefd2SJiri Olsa attr->sample_type = PERF_SAMPLE_IDENTIFIER; 468d09cefd2SJiri Olsa 469d09cefd2SJiri Olsa /* 470d09cefd2SJiri Olsa * Disabling all counters initially, they will be enabled 471d09cefd2SJiri Olsa * either manually by us or by kernel via enable_on_exec 472d09cefd2SJiri Olsa * set later. 473d09cefd2SJiri Olsa */ 474d09cefd2SJiri Olsa if (perf_evsel__is_group_leader(evsel)) { 475d09cefd2SJiri Olsa attr->disabled = 1; 476d09cefd2SJiri Olsa 477d09cefd2SJiri Olsa /* 478d09cefd2SJiri Olsa * In case of initial_delay we enable tracee 479d09cefd2SJiri Olsa * events manually. 480d09cefd2SJiri Olsa */ 481d09cefd2SJiri Olsa if (target__none(target) && !config->initial_delay) 482d09cefd2SJiri Olsa attr->enable_on_exec = 1; 483d09cefd2SJiri Olsa } 484d09cefd2SJiri Olsa 485d09cefd2SJiri Olsa if (target__has_cpu(target) && !target__has_per_thread(target)) 486d09cefd2SJiri Olsa return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); 487d09cefd2SJiri Olsa 488d09cefd2SJiri Olsa return perf_evsel__open_per_thread(evsel, evsel->threads); 489d09cefd2SJiri Olsa } 4900a4e64d3SJiri Olsa 4910a4e64d3SJiri Olsa int perf_stat_synthesize_config(struct perf_stat_config *config, 4920a4e64d3SJiri Olsa struct perf_tool *tool, 4930a4e64d3SJiri Olsa struct perf_evlist *evlist, 4940a4e64d3SJiri Olsa perf_event__handler_t process, 4950a4e64d3SJiri Olsa bool attrs) 4960a4e64d3SJiri Olsa { 4970a4e64d3SJiri Olsa int err; 4980a4e64d3SJiri Olsa 4990a4e64d3SJiri Olsa if (attrs) { 5000a4e64d3SJiri Olsa err = perf_event__synthesize_attrs(tool, evlist, process); 5010a4e64d3SJiri Olsa if (err < 0) { 5020a4e64d3SJiri Olsa pr_err("Couldn't synthesize attrs.\n"); 5030a4e64d3SJiri Olsa return err; 5040a4e64d3SJiri Olsa } 5050a4e64d3SJiri Olsa } 5060a4e64d3SJiri Olsa 5070a4e64d3SJiri Olsa err = perf_event__synthesize_extra_attr(tool, evlist, process, 5080a4e64d3SJiri Olsa attrs); 5090a4e64d3SJiri Olsa 5100a4e64d3SJiri Olsa err = perf_event__synthesize_thread_map2(tool, evlist->threads, 5110a4e64d3SJiri Olsa process, NULL); 5120a4e64d3SJiri Olsa if (err < 0) { 5130a4e64d3SJiri Olsa pr_err("Couldn't synthesize thread map.\n"); 5140a4e64d3SJiri Olsa return err; 5150a4e64d3SJiri Olsa } 5160a4e64d3SJiri Olsa 5170a4e64d3SJiri Olsa err = perf_event__synthesize_cpu_map(tool, evlist->cpus, 5180a4e64d3SJiri Olsa process, NULL); 5190a4e64d3SJiri Olsa if (err < 0) { 5200a4e64d3SJiri Olsa pr_err("Couldn't synthesize thread map.\n"); 5210a4e64d3SJiri Olsa return err; 5220a4e64d3SJiri Olsa } 5230a4e64d3SJiri Olsa 5240a4e64d3SJiri Olsa err = perf_event__synthesize_stat_config(tool, config, process, NULL); 5250a4e64d3SJiri Olsa if (err < 0) { 5260a4e64d3SJiri Olsa pr_err("Couldn't synthesize config.\n"); 5270a4e64d3SJiri Olsa return err; 5280a4e64d3SJiri Olsa } 5290a4e64d3SJiri Olsa 5300a4e64d3SJiri Olsa return 0; 5310a4e64d3SJiri Olsa } 532