blob: 7acb9a6730fe99929767004b18b6d210fcfd9c8f [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>
Xiao Guangrong0007ece2012-09-17 16:31:14 +08005#include "stat.h"
Jiri Olsa24e34f62015-06-26 11:29:16 +02006#include "evlist.h"
Jiri Olsae2f56da2015-06-04 15:50:55 +02007#include "evsel.h"
Jiri Olsa24e34f62015-06-26 11:29:16 +02008#include "thread_map.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -03009#include <linux/zalloc.h>
Xiao Guangrong0007ece2012-09-17 16:31:14 +080010
11void 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 Ahernffe4f3c2013-08-02 14:05:40 -060019
20 if (val > stats->max)
21 stats->max = val;
22
23 if (val < stats->min)
24 stats->min = val;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080025}
26
27double 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 */
48double stddev_stats(struct stats *stats)
49{
50 double variance, variance_mean;
51
David Ahern45528f72013-05-25 18:24:48 -060052 if (stats->n < 2)
Xiao Guangrong0007ece2012-09-17 16:31:14 +080053 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
61double 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 Olsae2f56da2015-06-04 15:50:55 +020070
Jiri Olsa32dcd022019-07-21 13:23:51 +020071bool __perf_evsel_stat__is(struct evsel *evsel,
Jiri Olsae2f56da2015-06-04 15:50:55 +020072 enum perf_stat_evsel_id id)
73{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -030074 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsae2f56da2015-06-04 15:50:55 +020075
76 return ps->id == id;
77}
78
79#define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
80static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
Jiri Olsa4c358d52015-06-03 16:25:52 +020081 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 Kleen239bd472016-05-24 12:52:37 -070086 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 Liangdaefd0b2017-05-26 12:05:38 -070091 ID(SMI_NUM, msr/smi/),
92 ID(APERF, msr/aperf/),
Jiri Olsae2f56da2015-06-04 15:50:55 +020093};
94#undef ID
95
Jiri Olsa32dcd022019-07-21 13:23:51 +020096static void perf_stat_evsel_id_init(struct evsel *evsel)
Jiri Olsae2f56da2015-06-04 15:50:55 +020097{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -030098 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsae2f56da2015-06-04 15:50:55 +020099 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 Olsaa9a3a4d2015-06-14 10:19:26 +0200110
Jiri Olsa32dcd022019-07-21 13:23:51 +0200111static void perf_evsel__reset_stat_priv(struct evsel *evsel)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200112{
113 int i;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300114 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsa9689edf2015-06-26 11:29:14 +0200115
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 Olsa32dcd022019-07-21 13:23:51 +0200122static int perf_evsel__alloc_stat_priv(struct evsel *evsel)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200123{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300124 evsel->stats = zalloc(sizeof(struct perf_stat_evsel));
125 if (evsel->stats == NULL)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200126 return -ENOMEM;
127 perf_evsel__reset_stat_priv(evsel);
128 return 0;
129}
130
Jiri Olsa32dcd022019-07-21 13:23:51 +0200131static void perf_evsel__free_stat_priv(struct evsel *evsel)
Jiri Olsa9689edf2015-06-26 11:29:14 +0200132{
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300133 struct perf_stat_evsel *ps = evsel->stats;
Jiri Olsaf7794d52017-07-26 14:02:05 +0200134
135 if (ps)
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300136 zfree(&ps->group_data);
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300137 zfree(&evsel->stats);
Jiri Olsa9689edf2015-06-26 11:29:14 +0200138}
Jiri Olsaa9395122015-06-26 11:29:15 +0200139
Jiri Olsa32dcd022019-07-21 13:23:51 +0200140static int perf_evsel__alloc_prev_raw_counts(struct evsel *evsel,
Jiri Olsa86a2cf32016-01-20 12:56:35 +0100141 int ncpus, int nthreads)
Jiri Olsaa9395122015-06-26 11:29:15 +0200142{
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 Olsa32dcd022019-07-21 13:23:51 +0200152static void perf_evsel__free_prev_raw_counts(struct evsel *evsel)
Jiri Olsaa9395122015-06-26 11:29:15 +0200153{
154 perf_counts__delete(evsel->prev_raw_counts);
155 evsel->prev_raw_counts = NULL;
156}
Jiri Olsa24e34f62015-06-26 11:29:16 +0200157
Jiri Olsa32dcd022019-07-21 13:23:51 +0200158static int perf_evsel__alloc_stats(struct evsel *evsel, bool alloc_raw)
Jiri Olsaa7d0a102015-06-26 11:29:17 +0200159{
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 Olsa24e34f62015-06-26 11:29:16 +0200171int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
172{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200173 struct evsel *evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200174
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300175 evlist__for_each_entry(evlist, evsel) {
Jiri Olsaa7d0a102015-06-26 11:29:17 +0200176 if (perf_evsel__alloc_stats(evsel, alloc_raw))
Jiri Olsa24e34f62015-06-26 11:29:16 +0200177 goto out_free;
178 }
179
180 return 0;
181
182out_free:
183 perf_evlist__free_stats(evlist);
184 return -1;
185}
186
187void perf_evlist__free_stats(struct perf_evlist *evlist)
188{
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) {
Jiri Olsa24e34f62015-06-26 11:29:16 +0200192 perf_evsel__free_stat_priv(evsel);
193 perf_evsel__free_counts(evsel);
194 perf_evsel__free_prev_raw_counts(evsel);
195 }
196}
197
198void perf_evlist__reset_stats(struct perf_evlist *evlist)
199{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200200 struct evsel *evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200201
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300202 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa24e34f62015-06-26 11:29:16 +0200203 perf_evsel__reset_stat_priv(evsel);
204 perf_evsel__reset_counts(evsel);
205 }
206}
Jiri Olsaf80010e2015-07-21 14:31:27 +0200207
Jiri Olsa32dcd022019-07-21 13:23:51 +0200208static void zero_per_pkg(struct evsel *counter)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200209{
210 if (counter->per_pkg_mask)
211 memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
212}
213
Jiri Olsa32dcd022019-07-21 13:23:51 +0200214static int check_per_pkg(struct evsel *counter,
Stephane Eranian02d8dab2015-09-03 15:23:40 +0200215 struct perf_counts_values *vals, int cpu, bool *skip)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200216{
217 unsigned long *mask = counter->per_pkg_mask;
Jiri Olsaf8548392019-07-21 13:23:49 +0200218 struct perf_cpu_map *cpus = perf_evsel__cpus(counter);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200219 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 Eranian02d8dab2015-09-03 15:23:40 +0200237 /*
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 Olsa1fe7a302015-10-16 12:41:15 +0200248 s = cpu_map__get_socket(cpus, cpu, NULL);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200249 if (s < 0)
250 return -1;
251
252 *skip = test_and_set_bit(s, mask) == 1;
253 return 0;
254}
255
256static int
Jiri Olsa32dcd022019-07-21 13:23:51 +0200257process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
Jiri Olsaf80010e2015-07-21 14:31:27 +0200258 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 Eranian02d8dab2015-09-03 15:23:40 +0200265 if (check_per_pkg(evsel, count, cpu, &skip)) {
Jiri Olsaf80010e2015-07-21 14:31:27 +0200266 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 Liangdb5742b2019-06-04 15:50:42 -0700276 case AGGR_DIE:
Jiri Olsaf80010e2015-07-21 14:31:27 +0200277 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 Yao4fc4d8d2019-04-12 21:59:49 +0800282 if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) {
283 perf_stat__update_shadow_stats(evsel, count->val,
284 cpu, &rt_stat);
285 }
286
Jin Yao14e72a22017-12-05 22:03:08 +0800287 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 Olsaf80010e2015-07-21 14:31:27 +0200295 break;
296 case AGGR_GLOBAL:
297 aggr->val += count->val;
Andi Kleen75998bb2019-03-14 15:50:01 -0700298 aggr->ena += count->ena;
299 aggr->run += count->run;
Jiri Olsa208df992015-10-16 12:41:04 +0200300 case AGGR_UNSET:
Jiri Olsaf80010e2015-07-21 14:31:27 +0200301 default:
302 break;
303 }
304
305 return 0;
306}
307
308static int process_counter_maps(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200309 struct evsel *counter)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200310{
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
329int perf_stat_process_counter(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200330 struct evsel *counter)
Jiri Olsaf80010e2015-07-21 14:31:27 +0200331{
332 struct perf_counts_values *aggr = &counter->counts->aggr;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300333 struct perf_stat_evsel *ps = counter->stats;
Jiri Olsaf80010e2015-07-21 14:31:27 +0200334 u64 *count = counter->counts->aggr.values;
335 int i, ret;
336
337 aggr->val = aggr->ena = aggr->run = 0;
Jiri Olsaf80010e2015-07-21 14:31:27 +0200338
Jiri Olsa51fd2df2016-02-03 08:43:56 +0100339 /*
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 Olsaf80010e2015-07-21 14:31:27 +0200349 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 Kimbb963e12017-02-17 17:17:38 +0900366 if (verbose > 0) {
Jiri Olsaf80010e2015-07-21 14:31:27 +0200367 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 Yao1fcd0392017-12-05 22:03:04 +0800374 perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200375
376 return 0;
377}
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100378
Jiri Olsa89f16882018-09-13 14:54:03 +0200379int perf_event__process_stat_event(struct perf_session *session,
380 union perf_event *event)
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100381{
382 struct perf_counts_values count;
383 struct stat_event *st = &event->stat;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200384 struct evsel *counter;
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100385
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 Olsae08a4562015-10-25 15:51:35 +0100400
401size_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
414size_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
425size_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 Olsad09cefd2018-08-30 08:32:17 +0200439
Jiri Olsa32dcd022019-07-21 13:23:51 +0200440int create_perf_stat_counter(struct evsel *evsel,
Jiri Olsad09cefd2018-08-30 08:32:17 +0200441 struct perf_stat_config *config,
442 struct target *target)
443{
444 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200445 struct evsel *leader = evsel->leader;
Jiri Olsad09cefd2018-08-30 08:32:17 +0200446
Andi Kleen75998bb2019-03-14 15:50:01 -0700447 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
448 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsad09cefd2018-08-30 08:32:17 +0200449
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 Olsa0a4e64d2018-08-30 08:32:23 +0200490
491int 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}