blob: 8ce1479c98f03f6e57c396ac6863b45488775880 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Xiao Guangrong0007ece2012-09-17 16:31:14 +08004#include <math.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03005#include <string.h>
Arnaldo Carvalho de Melobfc49182019-08-21 14:02:05 -03006#include "counts.h"
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +01007#include "cpumap.h"
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -03008#include "debug.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03009#include "header.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080010#include "stat.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030011#include "session.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030012#include "target.h"
Jiri Olsa24e34f62015-06-26 11:29:16 +020013#include "evlist.h"
Jiri Olsae2f56da2015-06-04 15:50:55 +020014#include "evsel.h"
Jiri Olsa24e34f62015-06-26 11:29:16 +020015#include "thread_map.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030016#include <linux/zalloc.h>
Xiao Guangrong0007ece2012-09-17 16:31:14 +080017
18void update_stats(struct stats *stats, u64 val)
19{
20 double delta;
21
22 stats->n++;
23 delta = val - stats->mean;
24 stats->mean += delta / stats->n;
25 stats->M2 += delta*(val - stats->mean);
David Ahernffe4f3c2013-08-02 14:05:40 -060026
27 if (val > stats->max)
28 stats->max = val;
29
30 if (val < stats->min)
31 stats->min = val;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080032}
33
34double avg_stats(struct stats *stats)
35{
36 return stats->mean;
37}
38
39/*
40 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
41 *
42 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
43 * s^2 = -------------------------------
44 * n - 1
45 *
46 * http://en.wikipedia.org/wiki/Stddev
47 *
48 * The std dev of the mean is related to the std dev by:
49 *
50 * s
51 * s_mean = -------
52 * sqrt(n)
53 *
54 */
55double stddev_stats(struct stats *stats)
56{
57 double variance, variance_mean;
58
David Ahern45528f72013-05-25 18:24:48 -060059 if (stats->n < 2)
Xiao Guangrong0007ece2012-09-17 16:31:14 +080060 return 0.0;
61
62 variance = stats->M2 / (stats->n - 1);
63 variance_mean = variance / stats->n;
64
65 return sqrt(variance_mean);
66}
67
68double rel_stddev_stats(double stddev, double avg)
69{
70 double pct = 0.0;
71
72 if (avg)
73 pct = 100.0 * stddev/avg;
74
75 return pct;
76}
Jiri Olsae2f56da2015-06-04 15:50:55 +020077
Jiri Olsa32dcd022019-07-21 13:23:51 +020078bool __perf_evsel_stat__is(struct evsel *evsel,
Jiri Olsae2f56da2015-06-04 15:50:55 +020079 enum perf_stat_evsel_id id)
80{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -030081 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsae2f56da2015-06-04 15:50:55 +020082
83 return ps->id == id;
84}
85
86#define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
87static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
Jiri Olsa4c358d52015-06-03 16:25:52 +020088 ID(NONE, x),
89 ID(CYCLES_IN_TX, cpu/cycles-t/),
90 ID(TRANSACTION_START, cpu/tx-start/),
91 ID(ELISION_START, cpu/el-start/),
92 ID(CYCLES_IN_TX_CP, cpu/cycles-ct/),
Andi Kleen239bd472016-05-24 12:52:37 -070093 ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots),
94 ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued),
95 ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
96 ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
97 ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
Andi Kleen55c36a92020-09-11 07:48:07 -070098 ID(TOPDOWN_RETIRING, topdown-retiring),
99 ID(TOPDOWN_BAD_SPEC, topdown-bad-spec),
100 ID(TOPDOWN_FE_BOUND, topdown-fe-bound),
101 ID(TOPDOWN_BE_BOUND, topdown-be-bound),
Kan Liangdaefd0b2017-05-26 12:05:38 -0700102 ID(SMI_NUM, msr/smi/),
103 ID(APERF, msr/aperf/),
Jiri Olsae2f56da2015-06-04 15:50:55 +0200104};
105#undef ID
106
Jiri Olsa32dcd022019-07-21 13:23:51 +0200107static void perf_stat_evsel_id_init(struct evsel *evsel)
Jiri Olsae2f56da2015-06-04 15:50:55 +0200108{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300109 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsae2f56da2015-06-04 15:50:55 +0200110 int i;
111
112 /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
113
114 for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300115 if (!strcmp(evsel__name(evsel), id_str[i])) {
Jiri Olsae2f56da2015-06-04 15:50:55 +0200116 ps->id = i;
117 break;
118 }
119 }
120}
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +0200121
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300122static void evsel__reset_stat_priv(struct evsel *evsel)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200123{
124 int i;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300125 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsa9689edf2015-06-26 11:29:14 +0200126
127 for (i = 0; i < 3; i++)
128 init_stats(&ps->res_stats[i]);
129
130 perf_stat_evsel_id_init(evsel);
131}
132
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300133static int evsel__alloc_stat_priv(struct evsel *evsel)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200134{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300135 evsel->stats = zalloc(sizeof(struct perf_stat_evsel));
136 if (evsel->stats == NULL)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200137 return -ENOMEM;
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300138 evsel__reset_stat_priv(evsel);
Jiri Olsa9689edf2015-06-26 11:29:14 +0200139 return 0;
140}
141
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300142static void evsel__free_stat_priv(struct evsel *evsel)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200143{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300144 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsaf7794d52017-07-26 14:02:05 +0200145
146 if (ps)
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300147 zfree(&ps->group_data);
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300148 zfree(&evsel->stats);
Jiri Olsa9689edf2015-06-26 11:29:14 +0200149}
Jiri Olsaa9395122015-06-26 11:29:15 +0200150
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300151static int evsel__alloc_prev_raw_counts(struct evsel *evsel, int ncpus, int nthreads)
Jiri Olsaa9395122015-06-26 11:29:15 +0200152{
153 struct perf_counts *counts;
154
155 counts = perf_counts__new(ncpus, nthreads);
156 if (counts)
157 evsel->prev_raw_counts = counts;
158
159 return counts ? 0 : -ENOMEM;
160}
161
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300162static void evsel__free_prev_raw_counts(struct evsel *evsel)
Jiri Olsaa9395122015-06-26 11:29:15 +0200163{
164 perf_counts__delete(evsel->prev_raw_counts);
165 evsel->prev_raw_counts = NULL;
166}
Jiri Olsa24e34f62015-06-26 11:29:16 +0200167
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300168static void evsel__reset_prev_raw_counts(struct evsel *evsel)
Srikar Dronamrajub63fd112019-09-04 15:17:37 +0530169{
Jin Yaocf4d9bd2020-05-20 12:27:34 +0800170 if (evsel->prev_raw_counts)
171 perf_counts__reset(evsel->prev_raw_counts);
Srikar Dronamrajub63fd112019-09-04 15:17:37 +0530172}
173
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300174static int evsel__alloc_stats(struct evsel *evsel, bool alloc_raw)
Jiri Olsaa7d0a102015-06-26 11:29:17 +0200175{
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300176 int ncpus = evsel__nr_cpus(evsel);
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200177 int nthreads = perf_thread_map__nr(evsel->core.threads);
Jiri Olsaa7d0a102015-06-26 11:29:17 +0200178
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300179 if (evsel__alloc_stat_priv(evsel) < 0 ||
180 evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
181 (alloc_raw && evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
Jiri Olsaa7d0a102015-06-26 11:29:17 +0200182 return -ENOMEM;
183
184 return 0;
185}
186
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300187int evlist__alloc_stats(struct evlist *evlist, bool alloc_raw)
Jiri Olsa24e34f62015-06-26 11:29:16 +0200188{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200189 struct evsel *evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200190
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300191 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300192 if (evsel__alloc_stats(evsel, alloc_raw))
Jiri Olsa24e34f62015-06-26 11:29:16 +0200193 goto out_free;
194 }
195
196 return 0;
197
198out_free:
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300199 evlist__free_stats(evlist);
Jiri Olsa24e34f62015-06-26 11:29:16 +0200200 return -1;
201}
202
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300203void evlist__free_stats(struct evlist *evlist)
Jiri Olsa24e34f62015-06-26 11:29:16 +0200204{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200205 struct evsel *evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200206
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300207 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300208 evsel__free_stat_priv(evsel);
209 evsel__free_counts(evsel);
210 evsel__free_prev_raw_counts(evsel);
Jiri Olsa24e34f62015-06-26 11:29:16 +0200211 }
212}
213
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300214void evlist__reset_stats(struct evlist *evlist)
Jiri Olsa24e34f62015-06-26 11:29:16 +0200215{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200216 struct evsel *evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200217
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300218 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300219 evsel__reset_stat_priv(evsel);
220 evsel__reset_counts(evsel);
Jiri Olsa24e34f62015-06-26 11:29:16 +0200221 }
222}
Jiri Olsaf80010e2015-07-21 14:31:27 +0200223
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300224void evlist__reset_prev_raw_counts(struct evlist *evlist)
Srikar Dronamrajub63fd112019-09-04 15:17:37 +0530225{
226 struct evsel *evsel;
227
228 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300229 evsel__reset_prev_raw_counts(evsel);
Srikar Dronamrajub63fd112019-09-04 15:17:37 +0530230}
231
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300232static void evsel__copy_prev_raw_counts(struct evsel *evsel)
Jin Yao297767a2020-05-20 12:27:35 +0800233{
234 int ncpus = evsel__nr_cpus(evsel);
235 int nthreads = perf_thread_map__nr(evsel->core.threads);
236
237 for (int thread = 0; thread < nthreads; thread++) {
238 for (int cpu = 0; cpu < ncpus; cpu++) {
239 *perf_counts(evsel->counts, cpu, thread) =
240 *perf_counts(evsel->prev_raw_counts, cpu,
241 thread);
242 }
243 }
244
245 evsel->counts->aggr = evsel->prev_raw_counts->aggr;
246}
247
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300248void evlist__copy_prev_raw_counts(struct evlist *evlist)
Jin Yao297767a2020-05-20 12:27:35 +0800249{
250 struct evsel *evsel;
251
252 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -0300253 evsel__copy_prev_raw_counts(evsel);
Jin Yao297767a2020-05-20 12:27:35 +0800254}
255
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -0300256void evlist__save_aggr_prev_raw_counts(struct evlist *evlist)
Jin Yao905365f2020-05-20 12:27:36 +0800257{
258 struct evsel *evsel;
259
260 /*
261 * To collect the overall statistics for interval mode,
262 * we copy the counts from evsel->prev_raw_counts to
263 * evsel->counts. The perf_stat_process_counter creates
264 * aggr values from per cpu values, but the per cpu values
265 * are 0 for AGGR_GLOBAL. So we use a trick that saves the
266 * previous aggr value to the first member of perf_counts,
267 * then aggr calculation in process_counter_values can work
268 * correctly.
269 */
270 evlist__for_each_entry(evlist, evsel) {
271 *perf_counts(evsel->prev_raw_counts, 0, 0) =
272 evsel->prev_raw_counts->aggr;
273 }
274}
275
Jiri Olsa32dcd022019-07-21 13:23:51 +0200276static void zero_per_pkg(struct evsel *counter)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200277{
278 if (counter->per_pkg_mask)
Kyle Meyer92b5a152019-08-27 16:43:48 -0500279 memset(counter->per_pkg_mask, 0, cpu__max_cpu());
Jiri Olsaf80010e2015-07-21 14:31:27 +0200280}
281
Jiri Olsa32dcd022019-07-21 13:23:51 +0200282static int check_per_pkg(struct evsel *counter,
Stephane Eranian02d8dab2015-09-03 15:23:40 +0200283 struct perf_counts_values *vals, int cpu, bool *skip)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200284{
285 unsigned long *mask = counter->per_pkg_mask;
Jiri Olsab49aca32019-07-21 13:24:05 +0200286 struct perf_cpu_map *cpus = evsel__cpus(counter);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200287 int s;
288
289 *skip = false;
290
291 if (!counter->per_pkg)
292 return 0;
293
Jiri Olsa315c0a12019-08-22 13:11:39 +0200294 if (perf_cpu_map__empty(cpus))
Jiri Olsaf80010e2015-07-21 14:31:27 +0200295 return 0;
296
297 if (!mask) {
Kyle Meyer92b5a152019-08-27 16:43:48 -0500298 mask = zalloc(cpu__max_cpu());
Jiri Olsaf80010e2015-07-21 14:31:27 +0200299 if (!mask)
300 return -ENOMEM;
301
302 counter->per_pkg_mask = mask;
303 }
304
Stephane Eranian02d8dab2015-09-03 15:23:40 +0200305 /*
306 * we do not consider an event that has not run as a good
307 * instance to mark a package as used (skip=1). Otherwise
308 * we may run into a situation where the first CPU in a package
309 * is not running anything, yet the second is, and this function
310 * would mark the package as used after the first CPU and would
311 * not read the values from the second CPU.
312 */
313 if (!(vals->run && vals->ena))
314 return 0;
315
James Clark1a270cb2020-11-26 16:13:25 +0200316 s = cpu_map__get_socket(cpus, cpu, NULL).socket;
Jiri Olsaf80010e2015-07-21 14:31:27 +0200317 if (s < 0)
318 return -1;
319
320 *skip = test_and_set_bit(s, mask) == 1;
321 return 0;
322}
323
324static int
Jiri Olsa32dcd022019-07-21 13:23:51 +0200325process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
Jiri Olsaf80010e2015-07-21 14:31:27 +0200326 int cpu, int thread,
327 struct perf_counts_values *count)
328{
329 struct perf_counts_values *aggr = &evsel->counts->aggr;
330 static struct perf_counts_values zero;
331 bool skip = false;
332
Stephane Eranian02d8dab2015-09-03 15:23:40 +0200333 if (check_per_pkg(evsel, count, cpu, &skip)) {
Jiri Olsaf80010e2015-07-21 14:31:27 +0200334 pr_err("failed to read per-pkg counter\n");
335 return -1;
336 }
337
338 if (skip)
339 count = &zero;
340
341 switch (config->aggr_mode) {
342 case AGGR_THREAD:
343 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -0700344 case AGGR_DIE:
Jiri Olsaf80010e2015-07-21 14:31:27 +0200345 case AGGR_SOCKET:
Jiri Olsa86895b42019-08-28 10:17:43 +0200346 case AGGR_NODE:
Jiri Olsaf80010e2015-07-21 14:31:27 +0200347 case AGGR_NONE:
348 if (!evsel->snapshot)
Arnaldo Carvalho de Melo12f52612020-04-29 15:47:38 -0300349 evsel__compute_deltas(evsel, cpu, thread, count);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200350 perf_counts_values__scale(count, config->scale, NULL);
Jin Yao4fc4d8d2019-04-12 21:59:49 +0800351 if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) {
352 perf_stat__update_shadow_stats(evsel, count->val,
353 cpu, &rt_stat);
354 }
355
Jin Yao14e72a22017-12-05 22:03:08 +0800356 if (config->aggr_mode == AGGR_THREAD) {
357 if (config->stats)
358 perf_stat__update_shadow_stats(evsel,
359 count->val, 0, &config->stats[thread]);
360 else
361 perf_stat__update_shadow_stats(evsel,
362 count->val, 0, &rt_stat);
363 }
Jiri Olsaf80010e2015-07-21 14:31:27 +0200364 break;
365 case AGGR_GLOBAL:
366 aggr->val += count->val;
Andi Kleen75998bb2019-03-14 15:50:01 -0700367 aggr->ena += count->ena;
368 aggr->run += count->run;
Jiri Olsa208df992015-10-16 12:41:04 +0200369 case AGGR_UNSET:
Jiri Olsaf80010e2015-07-21 14:31:27 +0200370 default:
371 break;
372 }
373
374 return 0;
375}
376
377static int process_counter_maps(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200378 struct evsel *counter)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200379{
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200380 int nthreads = perf_thread_map__nr(counter->core.threads);
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300381 int ncpus = evsel__nr_cpus(counter);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200382 int cpu, thread;
383
Jiri Olsa648b5af2019-08-06 11:35:19 +0200384 if (counter->core.system_wide)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200385 nthreads = 1;
386
387 for (thread = 0; thread < nthreads; thread++) {
388 for (cpu = 0; cpu < ncpus; cpu++) {
389 if (process_counter_values(config, counter, cpu, thread,
390 perf_counts(counter->counts, cpu, thread)))
391 return -1;
392 }
393 }
394
395 return 0;
396}
397
398int perf_stat_process_counter(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200399 struct evsel *counter)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200400{
401 struct perf_counts_values *aggr = &counter->counts->aggr;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300402 struct perf_stat_evsel *ps = counter->stats;
Jiri Olsaf80010e2015-07-21 14:31:27 +0200403 u64 *count = counter->counts->aggr.values;
404 int i, ret;
405
406 aggr->val = aggr->ena = aggr->run = 0;
Jiri Olsaf80010e2015-07-21 14:31:27 +0200407
Jiri Olsa51fd2df2016-02-03 08:43:56 +0100408 /*
409 * We calculate counter's data every interval,
410 * and the display code shows ps->res_stats
411 * avg value. We need to zero the stats for
412 * interval mode, otherwise overall avg running
413 * averages will be shown for each interval.
414 */
Jin Yaoc7e5b322020-05-20 12:27:37 +0800415 if (config->interval || config->summary) {
Jin Yao0e0bf1e2020-04-09 15:07:55 +0800416 for (i = 0; i < 3; i++)
417 init_stats(&ps->res_stats[i]);
418 }
Jiri Olsa51fd2df2016-02-03 08:43:56 +0100419
Jiri Olsaf80010e2015-07-21 14:31:27 +0200420 if (counter->per_pkg)
421 zero_per_pkg(counter);
422
423 ret = process_counter_maps(config, counter);
424 if (ret)
425 return ret;
426
427 if (config->aggr_mode != AGGR_GLOBAL)
428 return 0;
429
430 if (!counter->snapshot)
Arnaldo Carvalho de Melo12f52612020-04-29 15:47:38 -0300431 evsel__compute_deltas(counter, -1, -1, aggr);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200432 perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
433
434 for (i = 0; i < 3; i++)
435 update_stats(&ps->res_stats[i], count[i]);
436
Namhyung Kimbb963e12017-02-17 17:17:38 +0900437 if (verbose > 0) {
Jiri Olsaf80010e2015-07-21 14:31:27 +0200438 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300439 evsel__name(counter), count[0], count[1], count[2]);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200440 }
441
442 /*
443 * Save the full runtime - to allow normalization during printout:
444 */
Jin Yao1fcd0392017-12-05 22:03:04 +0800445 perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200446
447 return 0;
448}
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100449
Jiri Olsa89f16882018-09-13 14:54:03 +0200450int perf_event__process_stat_event(struct perf_session *session,
451 union perf_event *event)
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100452{
453 struct perf_counts_values count;
Jiri Olsa72932372019-08-28 15:57:16 +0200454 struct perf_record_stat *st = &event->stat;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200455 struct evsel *counter;
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100456
457 count.val = st->val;
458 count.ena = st->ena;
459 count.run = st->run;
460
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -0300461 counter = evlist__id2evsel(session->evlist, st->id);
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100462 if (!counter) {
463 pr_err("Failed to resolve counter for stat event.\n");
464 return -EINVAL;
465 }
466
467 *perf_counts(counter->counts, st->cpu, st->thread) = count;
468 counter->supported = true;
469 return 0;
470}
Jiri Olsae08a4562015-10-25 15:51:35 +0100471
472size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
473{
Jiri Olsa72932372019-08-28 15:57:16 +0200474 struct perf_record_stat *st = (struct perf_record_stat *)event;
Jiri Olsae08a4562015-10-25 15:51:35 +0100475 size_t ret;
476
Jiri Olsa18a13a62019-08-28 15:57:10 +0200477 ret = fprintf(fp, "\n... id %" PRI_lu64 ", cpu %d, thread %d\n",
Jiri Olsae08a4562015-10-25 15:51:35 +0100478 st->id, st->cpu, st->thread);
Jiri Olsa18a13a62019-08-28 15:57:10 +0200479 ret += fprintf(fp, "... value %" PRI_lu64 ", enabled %" PRI_lu64 ", running %" PRI_lu64 "\n",
Jiri Olsae08a4562015-10-25 15:51:35 +0100480 st->val, st->ena, st->run);
481
482 return ret;
483}
484
485size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
486{
Jiri Olsa72932372019-08-28 15:57:16 +0200487 struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event;
Jiri Olsae08a4562015-10-25 15:51:35 +0100488 size_t ret;
489
Jiri Olsa782adbe2019-08-28 15:57:11 +0200490 ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time,
Jiri Olsae08a4562015-10-25 15:51:35 +0100491 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
492
493 return ret;
494}
495
496size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
497{
498 struct perf_stat_config sc;
499 size_t ret;
500
501 perf_event__read_stat_config(&sc, &event->stat_config);
502
503 ret = fprintf(fp, "\n");
504 ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
505 ret += fprintf(fp, "... scale %d\n", sc.scale);
506 ret += fprintf(fp, "... interval %u\n", sc.interval);
507
508 return ret;
509}
Jiri Olsad09cefd2018-08-30 08:32:17 +0200510
Jiri Olsa32dcd022019-07-21 13:23:51 +0200511int create_perf_stat_counter(struct evsel *evsel,
Jiri Olsad09cefd2018-08-30 08:32:17 +0200512 struct perf_stat_config *config,
Andi Kleen4804e012019-11-20 16:15:19 -0800513 struct target *target,
514 int cpu)
Jiri Olsad09cefd2018-08-30 08:32:17 +0200515{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200516 struct perf_event_attr *attr = &evsel->core.attr;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200517 struct evsel *leader = evsel->leader;
Jiri Olsad09cefd2018-08-30 08:32:17 +0200518
Andi Kleen75998bb2019-03-14 15:50:01 -0700519 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
520 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsad09cefd2018-08-30 08:32:17 +0200521
522 /*
523 * The event is part of non trivial group, let's enable
524 * the group read (for leader) and ID retrieval for all
525 * members.
526 */
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200527 if (leader->core.nr_members > 1)
Jiri Olsad09cefd2018-08-30 08:32:17 +0200528 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
529
530 attr->inherit = !config->no_inherit;
531
532 /*
533 * Some events get initialized with sample_(period/type) set,
534 * like tracepoints. Clear it up for counting.
535 */
536 attr->sample_period = 0;
537
538 if (config->identifier)
539 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
540
Jin Yaodd071022019-10-11 13:05:45 +0800541 if (config->all_user) {
542 attr->exclude_kernel = 1;
543 attr->exclude_user = 0;
544 }
545
546 if (config->all_kernel) {
547 attr->exclude_kernel = 0;
548 attr->exclude_user = 1;
549 }
550
Jiri Olsad09cefd2018-08-30 08:32:17 +0200551 /*
552 * Disabling all counters initially, they will be enabled
553 * either manually by us or by kernel via enable_on_exec
554 * set later.
555 */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300556 if (evsel__is_group_leader(evsel)) {
Jiri Olsad09cefd2018-08-30 08:32:17 +0200557 attr->disabled = 1;
558
559 /*
560 * In case of initial_delay we enable tracee
561 * events manually.
562 */
563 if (target__none(target) && !config->initial_delay)
564 attr->enable_on_exec = 1;
565 }
566
567 if (target__has_cpu(target) && !target__has_per_thread(target))
Arnaldo Carvalho de Meloaa8c4062020-04-29 16:21:03 -0300568 return evsel__open_per_cpu(evsel, evsel__cpus(evsel), cpu);
Jiri Olsad09cefd2018-08-30 08:32:17 +0200569
Arnaldo Carvalho de Meloaa8c4062020-04-29 16:21:03 -0300570 return evsel__open_per_thread(evsel, evsel->core.threads);
Jiri Olsad09cefd2018-08-30 08:32:17 +0200571}