Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.h> |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 4 | #include <math.h> |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 5 | #include "stat.h" |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 6 | #include "evlist.h" |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 7 | #include "evsel.h" |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 8 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 9 | #include <linux/zalloc.h> |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 10 | |
| 11 | void update_stats(struct stats *stats, u64 val) |
| 12 | { |
| 13 | double delta; |
| 14 | |
| 15 | stats->n++; |
| 16 | delta = val - stats->mean; |
| 17 | stats->mean += delta / stats->n; |
| 18 | stats->M2 += delta*(val - stats->mean); |
David Ahern | ffe4f3c | 2013-08-02 14:05:40 -0600 | [diff] [blame] | 19 | |
| 20 | if (val > stats->max) |
| 21 | stats->max = val; |
| 22 | |
| 23 | if (val < stats->min) |
| 24 | stats->min = val; |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | double avg_stats(struct stats *stats) |
| 28 | { |
| 29 | return stats->mean; |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance |
| 34 | * |
| 35 | * (\Sum n_i^2) - ((\Sum n_i)^2)/n |
| 36 | * s^2 = ------------------------------- |
| 37 | * n - 1 |
| 38 | * |
| 39 | * http://en.wikipedia.org/wiki/Stddev |
| 40 | * |
| 41 | * The std dev of the mean is related to the std dev by: |
| 42 | * |
| 43 | * s |
| 44 | * s_mean = ------- |
| 45 | * sqrt(n) |
| 46 | * |
| 47 | */ |
| 48 | double stddev_stats(struct stats *stats) |
| 49 | { |
| 50 | double variance, variance_mean; |
| 51 | |
David Ahern | 45528f7 | 2013-05-25 18:24:48 -0600 | [diff] [blame] | 52 | if (stats->n < 2) |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 53 | return 0.0; |
| 54 | |
| 55 | variance = stats->M2 / (stats->n - 1); |
| 56 | variance_mean = variance / stats->n; |
| 57 | |
| 58 | return sqrt(variance_mean); |
| 59 | } |
| 60 | |
| 61 | double rel_stddev_stats(double stddev, double avg) |
| 62 | { |
| 63 | double pct = 0.0; |
| 64 | |
| 65 | if (avg) |
| 66 | pct = 100.0 * stddev/avg; |
| 67 | |
| 68 | return pct; |
| 69 | } |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 70 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 71 | bool __perf_evsel_stat__is(struct evsel *evsel, |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 72 | enum perf_stat_evsel_id id) |
| 73 | { |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 74 | struct perf_stat_evsel *ps = evsel->stats; |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 75 | |
| 76 | return ps->id == id; |
| 77 | } |
| 78 | |
| 79 | #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name |
| 80 | static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = { |
Jiri Olsa | 4c358d5 | 2015-06-03 16:25:52 +0200 | [diff] [blame] | 81 | ID(NONE, x), |
| 82 | ID(CYCLES_IN_TX, cpu/cycles-t/), |
| 83 | ID(TRANSACTION_START, cpu/tx-start/), |
| 84 | ID(ELISION_START, cpu/el-start/), |
| 85 | ID(CYCLES_IN_TX_CP, cpu/cycles-ct/), |
Andi Kleen | 239bd47 | 2016-05-24 12:52:37 -0700 | [diff] [blame] | 86 | ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots), |
| 87 | ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued), |
| 88 | ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired), |
| 89 | ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles), |
| 90 | ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles), |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 91 | ID(SMI_NUM, msr/smi/), |
| 92 | ID(APERF, msr/aperf/), |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 93 | }; |
| 94 | #undef ID |
| 95 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 96 | static void perf_stat_evsel_id_init(struct evsel *evsel) |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 97 | { |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 98 | struct perf_stat_evsel *ps = evsel->stats; |
Jiri Olsa | e2f56da | 2015-06-04 15:50:55 +0200 | [diff] [blame] | 99 | int i; |
| 100 | |
| 101 | /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */ |
| 102 | |
| 103 | for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) { |
| 104 | if (!strcmp(perf_evsel__name(evsel), id_str[i])) { |
| 105 | ps->id = i; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
Jiri Olsa | a9a3a4d | 2015-06-14 10:19:26 +0200 | [diff] [blame] | 110 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 111 | static void perf_evsel__reset_stat_priv(struct evsel *evsel) |
Jiri Olsa | 9689edf | 2015-06-26 11:29:14 +0200 | [diff] [blame] | 112 | { |
| 113 | int i; |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 114 | struct perf_stat_evsel *ps = evsel->stats; |
Jiri Olsa | 9689edf | 2015-06-26 11:29:14 +0200 | [diff] [blame] | 115 | |
| 116 | for (i = 0; i < 3; i++) |
| 117 | init_stats(&ps->res_stats[i]); |
| 118 | |
| 119 | perf_stat_evsel_id_init(evsel); |
| 120 | } |
| 121 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 122 | static int perf_evsel__alloc_stat_priv(struct evsel *evsel) |
Jiri Olsa | 9689edf | 2015-06-26 11:29:14 +0200 | [diff] [blame] | 123 | { |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 124 | evsel->stats = zalloc(sizeof(struct perf_stat_evsel)); |
| 125 | if (evsel->stats == NULL) |
Jiri Olsa | 9689edf | 2015-06-26 11:29:14 +0200 | [diff] [blame] | 126 | return -ENOMEM; |
| 127 | perf_evsel__reset_stat_priv(evsel); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 131 | static void perf_evsel__free_stat_priv(struct evsel *evsel) |
Jiri Olsa | 9689edf | 2015-06-26 11:29:14 +0200 | [diff] [blame] | 132 | { |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 133 | struct perf_stat_evsel *ps = evsel->stats; |
Jiri Olsa | f7794d5 | 2017-07-26 14:02:05 +0200 | [diff] [blame] | 134 | |
| 135 | if (ps) |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 136 | zfree(&ps->group_data); |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 137 | zfree(&evsel->stats); |
Jiri Olsa | 9689edf | 2015-06-26 11:29:14 +0200 | [diff] [blame] | 138 | } |
Jiri Olsa | a939512 | 2015-06-26 11:29:15 +0200 | [diff] [blame] | 139 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 140 | static int perf_evsel__alloc_prev_raw_counts(struct evsel *evsel, |
Jiri Olsa | 86a2cf3 | 2016-01-20 12:56:35 +0100 | [diff] [blame] | 141 | int ncpus, int nthreads) |
Jiri Olsa | a939512 | 2015-06-26 11:29:15 +0200 | [diff] [blame] | 142 | { |
| 143 | struct perf_counts *counts; |
| 144 | |
| 145 | counts = perf_counts__new(ncpus, nthreads); |
| 146 | if (counts) |
| 147 | evsel->prev_raw_counts = counts; |
| 148 | |
| 149 | return counts ? 0 : -ENOMEM; |
| 150 | } |
| 151 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 152 | static void perf_evsel__free_prev_raw_counts(struct evsel *evsel) |
Jiri Olsa | a939512 | 2015-06-26 11:29:15 +0200 | [diff] [blame] | 153 | { |
| 154 | perf_counts__delete(evsel->prev_raw_counts); |
| 155 | evsel->prev_raw_counts = NULL; |
| 156 | } |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 157 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 158 | static int perf_evsel__alloc_stats(struct evsel *evsel, bool alloc_raw) |
Jiri Olsa | a7d0a10 | 2015-06-26 11:29:17 +0200 | [diff] [blame] | 159 | { |
| 160 | int ncpus = perf_evsel__nr_cpus(evsel); |
| 161 | int nthreads = thread_map__nr(evsel->threads); |
| 162 | |
| 163 | if (perf_evsel__alloc_stat_priv(evsel) < 0 || |
| 164 | perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 || |
| 165 | (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0)) |
| 166 | return -ENOMEM; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 171 | int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw) |
| 172 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 173 | struct evsel *evsel; |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 174 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 175 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | a7d0a10 | 2015-06-26 11:29:17 +0200 | [diff] [blame] | 176 | if (perf_evsel__alloc_stats(evsel, alloc_raw)) |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 177 | goto out_free; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | |
| 182 | out_free: |
| 183 | perf_evlist__free_stats(evlist); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | void perf_evlist__free_stats(struct perf_evlist *evlist) |
| 188 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 189 | struct evsel *evsel; |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 190 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 191 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 192 | perf_evsel__free_stat_priv(evsel); |
| 193 | perf_evsel__free_counts(evsel); |
| 194 | perf_evsel__free_prev_raw_counts(evsel); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void perf_evlist__reset_stats(struct perf_evlist *evlist) |
| 199 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 200 | struct evsel *evsel; |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 201 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 202 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | 24e34f6 | 2015-06-26 11:29:16 +0200 | [diff] [blame] | 203 | perf_evsel__reset_stat_priv(evsel); |
| 204 | perf_evsel__reset_counts(evsel); |
| 205 | } |
| 206 | } |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 207 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 208 | static void zero_per_pkg(struct evsel *counter) |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 209 | { |
| 210 | if (counter->per_pkg_mask) |
| 211 | memset(counter->per_pkg_mask, 0, MAX_NR_CPUS); |
| 212 | } |
| 213 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 214 | static int check_per_pkg(struct evsel *counter, |
Stephane Eranian | 02d8dab | 2015-09-03 15:23:40 +0200 | [diff] [blame] | 215 | struct perf_counts_values *vals, int cpu, bool *skip) |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 216 | { |
| 217 | unsigned long *mask = counter->per_pkg_mask; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 218 | struct perf_cpu_map *cpus = perf_evsel__cpus(counter); |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 219 | int s; |
| 220 | |
| 221 | *skip = false; |
| 222 | |
| 223 | if (!counter->per_pkg) |
| 224 | return 0; |
| 225 | |
| 226 | if (cpu_map__empty(cpus)) |
| 227 | return 0; |
| 228 | |
| 229 | if (!mask) { |
| 230 | mask = zalloc(MAX_NR_CPUS); |
| 231 | if (!mask) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | counter->per_pkg_mask = mask; |
| 235 | } |
| 236 | |
Stephane Eranian | 02d8dab | 2015-09-03 15:23:40 +0200 | [diff] [blame] | 237 | /* |
| 238 | * we do not consider an event that has not run as a good |
| 239 | * instance to mark a package as used (skip=1). Otherwise |
| 240 | * we may run into a situation where the first CPU in a package |
| 241 | * is not running anything, yet the second is, and this function |
| 242 | * would mark the package as used after the first CPU and would |
| 243 | * not read the values from the second CPU. |
| 244 | */ |
| 245 | if (!(vals->run && vals->ena)) |
| 246 | return 0; |
| 247 | |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 248 | s = cpu_map__get_socket(cpus, cpu, NULL); |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 249 | if (s < 0) |
| 250 | return -1; |
| 251 | |
| 252 | *skip = test_and_set_bit(s, mask) == 1; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 257 | process_counter_values(struct perf_stat_config *config, struct evsel *evsel, |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 258 | int cpu, int thread, |
| 259 | struct perf_counts_values *count) |
| 260 | { |
| 261 | struct perf_counts_values *aggr = &evsel->counts->aggr; |
| 262 | static struct perf_counts_values zero; |
| 263 | bool skip = false; |
| 264 | |
Stephane Eranian | 02d8dab | 2015-09-03 15:23:40 +0200 | [diff] [blame] | 265 | if (check_per_pkg(evsel, count, cpu, &skip)) { |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 266 | pr_err("failed to read per-pkg counter\n"); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | if (skip) |
| 271 | count = &zero; |
| 272 | |
| 273 | switch (config->aggr_mode) { |
| 274 | case AGGR_THREAD: |
| 275 | case AGGR_CORE: |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 276 | case AGGR_DIE: |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 277 | case AGGR_SOCKET: |
| 278 | case AGGR_NONE: |
| 279 | if (!evsel->snapshot) |
| 280 | perf_evsel__compute_deltas(evsel, cpu, thread, count); |
| 281 | perf_counts_values__scale(count, config->scale, NULL); |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 282 | if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) { |
| 283 | perf_stat__update_shadow_stats(evsel, count->val, |
| 284 | cpu, &rt_stat); |
| 285 | } |
| 286 | |
Jin Yao | 14e72a2 | 2017-12-05 22:03:08 +0800 | [diff] [blame] | 287 | if (config->aggr_mode == AGGR_THREAD) { |
| 288 | if (config->stats) |
| 289 | perf_stat__update_shadow_stats(evsel, |
| 290 | count->val, 0, &config->stats[thread]); |
| 291 | else |
| 292 | perf_stat__update_shadow_stats(evsel, |
| 293 | count->val, 0, &rt_stat); |
| 294 | } |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 295 | break; |
| 296 | case AGGR_GLOBAL: |
| 297 | aggr->val += count->val; |
Andi Kleen | 75998bb | 2019-03-14 15:50:01 -0700 | [diff] [blame] | 298 | aggr->ena += count->ena; |
| 299 | aggr->run += count->run; |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 300 | case AGGR_UNSET: |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 301 | default: |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int process_counter_maps(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 309 | struct evsel *counter) |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 310 | { |
| 311 | int nthreads = thread_map__nr(counter->threads); |
| 312 | int ncpus = perf_evsel__nr_cpus(counter); |
| 313 | int cpu, thread; |
| 314 | |
| 315 | if (counter->system_wide) |
| 316 | nthreads = 1; |
| 317 | |
| 318 | for (thread = 0; thread < nthreads; thread++) { |
| 319 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 320 | if (process_counter_values(config, counter, cpu, thread, |
| 321 | perf_counts(counter->counts, cpu, thread))) |
| 322 | return -1; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | int perf_stat_process_counter(struct perf_stat_config *config, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 330 | struct evsel *counter) |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 331 | { |
| 332 | struct perf_counts_values *aggr = &counter->counts->aggr; |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 333 | struct perf_stat_evsel *ps = counter->stats; |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 334 | u64 *count = counter->counts->aggr.values; |
| 335 | int i, ret; |
| 336 | |
| 337 | aggr->val = aggr->ena = aggr->run = 0; |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 338 | |
Jiri Olsa | 51fd2df | 2016-02-03 08:43:56 +0100 | [diff] [blame] | 339 | /* |
| 340 | * We calculate counter's data every interval, |
| 341 | * and the display code shows ps->res_stats |
| 342 | * avg value. We need to zero the stats for |
| 343 | * interval mode, otherwise overall avg running |
| 344 | * averages will be shown for each interval. |
| 345 | */ |
| 346 | if (config->interval) |
| 347 | init_stats(ps->res_stats); |
| 348 | |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 349 | if (counter->per_pkg) |
| 350 | zero_per_pkg(counter); |
| 351 | |
| 352 | ret = process_counter_maps(config, counter); |
| 353 | if (ret) |
| 354 | return ret; |
| 355 | |
| 356 | if (config->aggr_mode != AGGR_GLOBAL) |
| 357 | return 0; |
| 358 | |
| 359 | if (!counter->snapshot) |
| 360 | perf_evsel__compute_deltas(counter, -1, -1, aggr); |
| 361 | perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled); |
| 362 | |
| 363 | for (i = 0; i < 3; i++) |
| 364 | update_stats(&ps->res_stats[i], count[i]); |
| 365 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 366 | if (verbose > 0) { |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 367 | fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 368 | perf_evsel__name(counter), count[0], count[1], count[2]); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Save the full runtime - to allow normalization during printout: |
| 373 | */ |
Jin Yao | 1fcd039 | 2017-12-05 22:03:04 +0800 | [diff] [blame] | 374 | perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat); |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
Jiri Olsa | 0ea0e35 | 2015-10-25 15:51:32 +0100 | [diff] [blame] | 378 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 379 | int perf_event__process_stat_event(struct perf_session *session, |
| 380 | union perf_event *event) |
Jiri Olsa | 0ea0e35 | 2015-10-25 15:51:32 +0100 | [diff] [blame] | 381 | { |
| 382 | struct perf_counts_values count; |
| 383 | struct stat_event *st = &event->stat; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 384 | struct evsel *counter; |
Jiri Olsa | 0ea0e35 | 2015-10-25 15:51:32 +0100 | [diff] [blame] | 385 | |
| 386 | count.val = st->val; |
| 387 | count.ena = st->ena; |
| 388 | count.run = st->run; |
| 389 | |
| 390 | counter = perf_evlist__id2evsel(session->evlist, st->id); |
| 391 | if (!counter) { |
| 392 | pr_err("Failed to resolve counter for stat event.\n"); |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | *perf_counts(counter->counts, st->cpu, st->thread) = count; |
| 397 | counter->supported = true; |
| 398 | return 0; |
| 399 | } |
Jiri Olsa | e08a456 | 2015-10-25 15:51:35 +0100 | [diff] [blame] | 400 | |
| 401 | size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp) |
| 402 | { |
| 403 | struct stat_event *st = (struct stat_event *) event; |
| 404 | size_t ret; |
| 405 | |
| 406 | ret = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n", |
| 407 | st->id, st->cpu, st->thread); |
| 408 | ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n", |
| 409 | st->val, st->ena, st->run); |
| 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp) |
| 415 | { |
| 416 | struct stat_round_event *rd = (struct stat_round_event *)event; |
| 417 | size_t ret; |
| 418 | |
| 419 | ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time, |
| 420 | rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL"); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp) |
| 426 | { |
| 427 | struct perf_stat_config sc; |
| 428 | size_t ret; |
| 429 | |
| 430 | perf_event__read_stat_config(&sc, &event->stat_config); |
| 431 | |
| 432 | ret = fprintf(fp, "\n"); |
| 433 | ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode); |
| 434 | ret += fprintf(fp, "... scale %d\n", sc.scale); |
| 435 | ret += fprintf(fp, "... interval %u\n", sc.interval); |
| 436 | |
| 437 | return ret; |
| 438 | } |
Jiri Olsa | d09cefd | 2018-08-30 08:32:17 +0200 | [diff] [blame] | 439 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 440 | int create_perf_stat_counter(struct evsel *evsel, |
Jiri Olsa | d09cefd | 2018-08-30 08:32:17 +0200 | [diff] [blame] | 441 | struct perf_stat_config *config, |
| 442 | struct target *target) |
| 443 | { |
| 444 | struct perf_event_attr *attr = &evsel->attr; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame^] | 445 | struct evsel *leader = evsel->leader; |
Jiri Olsa | d09cefd | 2018-08-30 08:32:17 +0200 | [diff] [blame] | 446 | |
Andi Kleen | 75998bb | 2019-03-14 15:50:01 -0700 | [diff] [blame] | 447 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 448 | PERF_FORMAT_TOTAL_TIME_RUNNING; |
Jiri Olsa | d09cefd | 2018-08-30 08:32:17 +0200 | [diff] [blame] | 449 | |
| 450 | /* |
| 451 | * The event is part of non trivial group, let's enable |
| 452 | * the group read (for leader) and ID retrieval for all |
| 453 | * members. |
| 454 | */ |
| 455 | if (leader->nr_members > 1) |
| 456 | attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP; |
| 457 | |
| 458 | attr->inherit = !config->no_inherit; |
| 459 | |
| 460 | /* |
| 461 | * Some events get initialized with sample_(period/type) set, |
| 462 | * like tracepoints. Clear it up for counting. |
| 463 | */ |
| 464 | attr->sample_period = 0; |
| 465 | |
| 466 | if (config->identifier) |
| 467 | attr->sample_type = PERF_SAMPLE_IDENTIFIER; |
| 468 | |
| 469 | /* |
| 470 | * Disabling all counters initially, they will be enabled |
| 471 | * either manually by us or by kernel via enable_on_exec |
| 472 | * set later. |
| 473 | */ |
| 474 | if (perf_evsel__is_group_leader(evsel)) { |
| 475 | attr->disabled = 1; |
| 476 | |
| 477 | /* |
| 478 | * In case of initial_delay we enable tracee |
| 479 | * events manually. |
| 480 | */ |
| 481 | if (target__none(target) && !config->initial_delay) |
| 482 | attr->enable_on_exec = 1; |
| 483 | } |
| 484 | |
| 485 | if (target__has_cpu(target) && !target__has_per_thread(target)) |
| 486 | return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); |
| 487 | |
| 488 | return perf_evsel__open_per_thread(evsel, evsel->threads); |
| 489 | } |
Jiri Olsa | 0a4e64d | 2018-08-30 08:32:23 +0200 | [diff] [blame] | 490 | |
| 491 | int perf_stat_synthesize_config(struct perf_stat_config *config, |
| 492 | struct perf_tool *tool, |
| 493 | struct perf_evlist *evlist, |
| 494 | perf_event__handler_t process, |
| 495 | bool attrs) |
| 496 | { |
| 497 | int err; |
| 498 | |
| 499 | if (attrs) { |
| 500 | err = perf_event__synthesize_attrs(tool, evlist, process); |
| 501 | if (err < 0) { |
| 502 | pr_err("Couldn't synthesize attrs.\n"); |
| 503 | return err; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | err = perf_event__synthesize_extra_attr(tool, evlist, process, |
| 508 | attrs); |
| 509 | |
| 510 | err = perf_event__synthesize_thread_map2(tool, evlist->threads, |
| 511 | process, NULL); |
| 512 | if (err < 0) { |
| 513 | pr_err("Couldn't synthesize thread map.\n"); |
| 514 | return err; |
| 515 | } |
| 516 | |
| 517 | err = perf_event__synthesize_cpu_map(tool, evlist->cpus, |
| 518 | process, NULL); |
| 519 | if (err < 0) { |
| 520 | pr_err("Couldn't synthesize thread map.\n"); |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | err = perf_event__synthesize_stat_config(tool, config, process, NULL); |
| 525 | if (err < 0) { |
| 526 | pr_err("Couldn't synthesize config.\n"); |
| 527 | return err; |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |