1 // SPDX-License-Identifier: GPL-2.0
14 #include "thread_map.h"
15 #include <linux/zalloc.h>
17 void update_stats(struct stats *stats, u64 val)
22 delta = val - stats->mean;
23 stats->mean += delta / stats->n;
24 stats->M2 += delta*(val - stats->mean);
33 double avg_stats(struct stats *stats)
39 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
41 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
42 * s^2 = -------------------------------
45 * http://en.wikipedia.org/wiki/Stddev
47 * The std dev of the mean is related to the std dev by:
54 double stddev_stats(struct stats *stats)
56 double variance, variance_mean;
61 variance = stats->M2 / (stats->n - 1);
62 variance_mean = variance / stats->n;
64 return sqrt(variance_mean);
67 double rel_stddev_stats(double stddev, double avg)
72 pct = 100.0 * stddev/avg;
77 bool __perf_evsel_stat__is(struct evsel *evsel,
78 enum perf_stat_evsel_id id)
80 struct perf_stat_evsel *ps = evsel->stats;
85 #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
86 static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
88 ID(CYCLES_IN_TX, cpu/cycles-t/),
89 ID(TRANSACTION_START, cpu/tx-start/),
90 ID(ELISION_START, cpu/el-start/),
91 ID(CYCLES_IN_TX_CP, cpu/cycles-ct/),
92 ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots),
93 ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued),
94 ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
95 ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
96 ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
97 ID(SMI_NUM, msr/smi/),
98 ID(APERF, msr/aperf/),
102 static void perf_stat_evsel_id_init(struct evsel *evsel)
104 struct perf_stat_evsel *ps = evsel->stats;
107 /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
109 for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
110 if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
117 static void perf_evsel__reset_stat_priv(struct evsel *evsel)
120 struct perf_stat_evsel *ps = evsel->stats;
122 for (i = 0; i < 3; i++)
123 init_stats(&ps->res_stats[i]);
125 perf_stat_evsel_id_init(evsel);
128 static int perf_evsel__alloc_stat_priv(struct evsel *evsel)
130 evsel->stats = zalloc(sizeof(struct perf_stat_evsel));
131 if (evsel->stats == NULL)
133 perf_evsel__reset_stat_priv(evsel);
137 static void perf_evsel__free_stat_priv(struct evsel *evsel)
139 struct perf_stat_evsel *ps = evsel->stats;
142 zfree(&ps->group_data);
143 zfree(&evsel->stats);
146 static int perf_evsel__alloc_prev_raw_counts(struct evsel *evsel,
147 int ncpus, int nthreads)
149 struct perf_counts *counts;
151 counts = perf_counts__new(ncpus, nthreads);
153 evsel->prev_raw_counts = counts;
155 return counts ? 0 : -ENOMEM;
158 static void perf_evsel__free_prev_raw_counts(struct evsel *evsel)
160 perf_counts__delete(evsel->prev_raw_counts);
161 evsel->prev_raw_counts = NULL;
164 static int perf_evsel__alloc_stats(struct evsel *evsel, bool alloc_raw)
166 int ncpus = perf_evsel__nr_cpus(evsel);
167 int nthreads = perf_thread_map__nr(evsel->core.threads);
169 if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
170 perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
171 (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
177 int perf_evlist__alloc_stats(struct evlist *evlist, bool alloc_raw)
181 evlist__for_each_entry(evlist, evsel) {
182 if (perf_evsel__alloc_stats(evsel, alloc_raw))
189 perf_evlist__free_stats(evlist);
193 void perf_evlist__free_stats(struct evlist *evlist)
197 evlist__for_each_entry(evlist, evsel) {
198 perf_evsel__free_stat_priv(evsel);
199 perf_evsel__free_counts(evsel);
200 perf_evsel__free_prev_raw_counts(evsel);
204 void perf_evlist__reset_stats(struct evlist *evlist)
208 evlist__for_each_entry(evlist, evsel) {
209 perf_evsel__reset_stat_priv(evsel);
210 perf_evsel__reset_counts(evsel);
214 static void zero_per_pkg(struct evsel *counter)
216 if (counter->per_pkg_mask)
217 memset(counter->per_pkg_mask, 0, cpu__max_cpu());
220 static int check_per_pkg(struct evsel *counter,
221 struct perf_counts_values *vals, int cpu, bool *skip)
223 unsigned long *mask = counter->per_pkg_mask;
224 struct perf_cpu_map *cpus = evsel__cpus(counter);
229 if (!counter->per_pkg)
232 if (perf_cpu_map__empty(cpus))
236 mask = zalloc(cpu__max_cpu());
240 counter->per_pkg_mask = mask;
244 * we do not consider an event that has not run as a good
245 * instance to mark a package as used (skip=1). Otherwise
246 * we may run into a situation where the first CPU in a package
247 * is not running anything, yet the second is, and this function
248 * would mark the package as used after the first CPU and would
249 * not read the values from the second CPU.
251 if (!(vals->run && vals->ena))
254 s = cpu_map__get_socket(cpus, cpu, NULL);
258 *skip = test_and_set_bit(s, mask) == 1;
263 process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
265 struct perf_counts_values *count)
267 struct perf_counts_values *aggr = &evsel->counts->aggr;
268 static struct perf_counts_values zero;
271 if (check_per_pkg(evsel, count, cpu, &skip)) {
272 pr_err("failed to read per-pkg counter\n");
279 switch (config->aggr_mode) {
285 if (!evsel->snapshot)
286 perf_evsel__compute_deltas(evsel, cpu, thread, count);
287 perf_counts_values__scale(count, config->scale, NULL);
288 if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) {
289 perf_stat__update_shadow_stats(evsel, count->val,
293 if (config->aggr_mode == AGGR_THREAD) {
295 perf_stat__update_shadow_stats(evsel,
296 count->val, 0, &config->stats[thread]);
298 perf_stat__update_shadow_stats(evsel,
299 count->val, 0, &rt_stat);
303 aggr->val += count->val;
304 aggr->ena += count->ena;
305 aggr->run += count->run;
314 static int process_counter_maps(struct perf_stat_config *config,
315 struct evsel *counter)
317 int nthreads = perf_thread_map__nr(counter->core.threads);
318 int ncpus = perf_evsel__nr_cpus(counter);
321 if (counter->system_wide)
324 for (thread = 0; thread < nthreads; thread++) {
325 for (cpu = 0; cpu < ncpus; cpu++) {
326 if (process_counter_values(config, counter, cpu, thread,
327 perf_counts(counter->counts, cpu, thread)))
335 int perf_stat_process_counter(struct perf_stat_config *config,
336 struct evsel *counter)
338 struct perf_counts_values *aggr = &counter->counts->aggr;
339 struct perf_stat_evsel *ps = counter->stats;
340 u64 *count = counter->counts->aggr.values;
343 aggr->val = aggr->ena = aggr->run = 0;
346 * We calculate counter's data every interval,
347 * and the display code shows ps->res_stats
348 * avg value. We need to zero the stats for
349 * interval mode, otherwise overall avg running
350 * averages will be shown for each interval.
352 if (config->interval)
353 init_stats(ps->res_stats);
355 if (counter->per_pkg)
356 zero_per_pkg(counter);
358 ret = process_counter_maps(config, counter);
362 if (config->aggr_mode != AGGR_GLOBAL)
365 if (!counter->snapshot)
366 perf_evsel__compute_deltas(counter, -1, -1, aggr);
367 perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
369 for (i = 0; i < 3; i++)
370 update_stats(&ps->res_stats[i], count[i]);
373 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
374 perf_evsel__name(counter), count[0], count[1], count[2]);
378 * Save the full runtime - to allow normalization during printout:
380 perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
385 int perf_event__process_stat_event(struct perf_session *session,
386 union perf_event *event)
388 struct perf_counts_values count;
389 struct perf_record_stat *st = &event->stat;
390 struct evsel *counter;
396 counter = perf_evlist__id2evsel(session->evlist, st->id);
398 pr_err("Failed to resolve counter for stat event.\n");
402 *perf_counts(counter->counts, st->cpu, st->thread) = count;
403 counter->supported = true;
407 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
409 struct perf_record_stat *st = (struct perf_record_stat *)event;
412 ret = fprintf(fp, "\n... id %" PRI_lu64 ", cpu %d, thread %d\n",
413 st->id, st->cpu, st->thread);
414 ret += fprintf(fp, "... value %" PRI_lu64 ", enabled %" PRI_lu64 ", running %" PRI_lu64 "\n",
415 st->val, st->ena, st->run);
420 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
422 struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event;
425 ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time,
426 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
431 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
433 struct perf_stat_config sc;
436 perf_event__read_stat_config(&sc, &event->stat_config);
438 ret = fprintf(fp, "\n");
439 ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
440 ret += fprintf(fp, "... scale %d\n", sc.scale);
441 ret += fprintf(fp, "... interval %u\n", sc.interval);
446 int create_perf_stat_counter(struct evsel *evsel,
447 struct perf_stat_config *config,
448 struct target *target)
450 struct perf_event_attr *attr = &evsel->core.attr;
451 struct evsel *leader = evsel->leader;
453 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
454 PERF_FORMAT_TOTAL_TIME_RUNNING;
457 * The event is part of non trivial group, let's enable
458 * the group read (for leader) and ID retrieval for all
461 if (leader->core.nr_members > 1)
462 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
464 attr->inherit = !config->no_inherit;
467 * Some events get initialized with sample_(period/type) set,
468 * like tracepoints. Clear it up for counting.
470 attr->sample_period = 0;
472 if (config->identifier)
473 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
476 * Disabling all counters initially, they will be enabled
477 * either manually by us or by kernel via enable_on_exec
480 if (perf_evsel__is_group_leader(evsel)) {
484 * In case of initial_delay we enable tracee
487 if (target__none(target) && !config->initial_delay)
488 attr->enable_on_exec = 1;
491 if (target__has_cpu(target) && !target__has_per_thread(target))
492 return perf_evsel__open_per_cpu(evsel, evsel__cpus(evsel));
494 return perf_evsel__open_per_thread(evsel, evsel->core.threads);
497 int perf_stat_synthesize_config(struct perf_stat_config *config,
498 struct perf_tool *tool,
499 struct evlist *evlist,
500 perf_event__handler_t process,
506 err = perf_event__synthesize_attrs(tool, evlist, process);
508 pr_err("Couldn't synthesize attrs.\n");
513 err = perf_event__synthesize_extra_attr(tool, evlist, process,
516 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads,
519 pr_err("Couldn't synthesize thread map.\n");
523 err = perf_event__synthesize_cpu_map(tool, evlist->core.cpus,
526 pr_err("Couldn't synthesize thread map.\n");
530 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
532 pr_err("Couldn't synthesize config.\n");