7 #include "thread_map.h"
9 void update_stats(struct stats *stats, u64 val)
14 delta = val - stats->mean;
15 stats->mean += delta / stats->n;
16 stats->M2 += delta*(val - stats->mean);
25 double avg_stats(struct stats *stats)
31 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
33 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
34 * s^2 = -------------------------------
37 * http://en.wikipedia.org/wiki/Stddev
39 * The std dev of the mean is related to the std dev by:
46 double stddev_stats(struct stats *stats)
48 double variance, variance_mean;
53 variance = stats->M2 / (stats->n - 1);
54 variance_mean = variance / stats->n;
56 return sqrt(variance_mean);
59 double rel_stddev_stats(double stddev, double avg)
64 pct = 100.0 * stddev/avg;
69 bool __perf_evsel_stat__is(struct perf_evsel *evsel,
70 enum perf_stat_evsel_id id)
72 struct perf_stat_evsel *ps = evsel->priv;
77 #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
78 static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
80 ID(CYCLES_IN_TX, cpu/cycles-t/),
81 ID(TRANSACTION_START, cpu/tx-start/),
82 ID(ELISION_START, cpu/el-start/),
83 ID(CYCLES_IN_TX_CP, cpu/cycles-ct/),
84 ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots),
85 ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued),
86 ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
87 ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
88 ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
89 ID(SMI_NUM, msr/smi/),
90 ID(APERF, msr/aperf/),
94 void perf_stat_evsel_id_init(struct perf_evsel *evsel)
96 struct perf_stat_evsel *ps = evsel->priv;
99 /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
101 for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
102 if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
109 static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
112 struct perf_stat_evsel *ps = evsel->priv;
114 for (i = 0; i < 3; i++)
115 init_stats(&ps->res_stats[i]);
117 perf_stat_evsel_id_init(evsel);
120 static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
122 evsel->priv = zalloc(sizeof(struct perf_stat_evsel));
123 if (evsel->priv == NULL)
125 perf_evsel__reset_stat_priv(evsel);
129 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
134 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
135 int ncpus, int nthreads)
137 struct perf_counts *counts;
139 counts = perf_counts__new(ncpus, nthreads);
141 evsel->prev_raw_counts = counts;
143 return counts ? 0 : -ENOMEM;
146 static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
148 perf_counts__delete(evsel->prev_raw_counts);
149 evsel->prev_raw_counts = NULL;
152 static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
154 int ncpus = perf_evsel__nr_cpus(evsel);
155 int nthreads = thread_map__nr(evsel->threads);
157 if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
158 perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
159 (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
165 int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
167 struct perf_evsel *evsel;
169 evlist__for_each_entry(evlist, evsel) {
170 if (perf_evsel__alloc_stats(evsel, alloc_raw))
177 perf_evlist__free_stats(evlist);
181 void perf_evlist__free_stats(struct perf_evlist *evlist)
183 struct perf_evsel *evsel;
185 evlist__for_each_entry(evlist, evsel) {
186 perf_evsel__free_stat_priv(evsel);
187 perf_evsel__free_counts(evsel);
188 perf_evsel__free_prev_raw_counts(evsel);
192 void perf_evlist__reset_stats(struct perf_evlist *evlist)
194 struct perf_evsel *evsel;
196 evlist__for_each_entry(evlist, evsel) {
197 perf_evsel__reset_stat_priv(evsel);
198 perf_evsel__reset_counts(evsel);
202 static void zero_per_pkg(struct perf_evsel *counter)
204 if (counter->per_pkg_mask)
205 memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
208 static int check_per_pkg(struct perf_evsel *counter,
209 struct perf_counts_values *vals, int cpu, bool *skip)
211 unsigned long *mask = counter->per_pkg_mask;
212 struct cpu_map *cpus = perf_evsel__cpus(counter);
217 if (!counter->per_pkg)
220 if (cpu_map__empty(cpus))
224 mask = zalloc(MAX_NR_CPUS);
228 counter->per_pkg_mask = mask;
232 * we do not consider an event that has not run as a good
233 * instance to mark a package as used (skip=1). Otherwise
234 * we may run into a situation where the first CPU in a package
235 * is not running anything, yet the second is, and this function
236 * would mark the package as used after the first CPU and would
237 * not read the values from the second CPU.
239 if (!(vals->run && vals->ena))
242 s = cpu_map__get_socket(cpus, cpu, NULL);
246 *skip = test_and_set_bit(s, mask) == 1;
251 process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
253 struct perf_counts_values *count)
255 struct perf_counts_values *aggr = &evsel->counts->aggr;
256 static struct perf_counts_values zero;
259 if (check_per_pkg(evsel, count, cpu, &skip)) {
260 pr_err("failed to read per-pkg counter\n");
267 switch (config->aggr_mode) {
272 if (!evsel->snapshot)
273 perf_evsel__compute_deltas(evsel, cpu, thread, count);
274 perf_counts_values__scale(count, config->scale, NULL);
275 if (config->aggr_mode == AGGR_NONE)
276 perf_stat__update_shadow_stats(evsel, count->values, cpu);
279 aggr->val += count->val;
281 aggr->ena += count->ena;
282 aggr->run += count->run;
292 static int process_counter_maps(struct perf_stat_config *config,
293 struct perf_evsel *counter)
295 int nthreads = thread_map__nr(counter->threads);
296 int ncpus = perf_evsel__nr_cpus(counter);
299 if (counter->system_wide)
302 for (thread = 0; thread < nthreads; thread++) {
303 for (cpu = 0; cpu < ncpus; cpu++) {
304 if (process_counter_values(config, counter, cpu, thread,
305 perf_counts(counter->counts, cpu, thread)))
313 int perf_stat_process_counter(struct perf_stat_config *config,
314 struct perf_evsel *counter)
316 struct perf_counts_values *aggr = &counter->counts->aggr;
317 struct perf_stat_evsel *ps = counter->priv;
318 u64 *count = counter->counts->aggr.values;
322 aggr->val = aggr->ena = aggr->run = 0;
325 * We calculate counter's data every interval,
326 * and the display code shows ps->res_stats
327 * avg value. We need to zero the stats for
328 * interval mode, otherwise overall avg running
329 * averages will be shown for each interval.
331 if (config->interval)
332 init_stats(ps->res_stats);
334 if (counter->per_pkg)
335 zero_per_pkg(counter);
337 ret = process_counter_maps(config, counter);
341 if (config->aggr_mode != AGGR_GLOBAL)
344 if (!counter->snapshot)
345 perf_evsel__compute_deltas(counter, -1, -1, aggr);
346 perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
348 for (i = 0; i < 3; i++)
349 update_stats(&ps->res_stats[i], count[i]);
352 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
353 perf_evsel__name(counter), count[0], count[1], count[2]);
357 * Save the full runtime - to allow normalization during printout:
359 val = counter->scale * *count;
360 perf_stat__update_shadow_stats(counter, &val, 0);
365 int perf_event__process_stat_event(struct perf_tool *tool __maybe_unused,
366 union perf_event *event,
367 struct perf_session *session)
369 struct perf_counts_values count;
370 struct stat_event *st = &event->stat;
371 struct perf_evsel *counter;
377 counter = perf_evlist__id2evsel(session->evlist, st->id);
379 pr_err("Failed to resolve counter for stat event.\n");
383 *perf_counts(counter->counts, st->cpu, st->thread) = count;
384 counter->supported = true;
388 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
390 struct stat_event *st = (struct stat_event *) event;
393 ret = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n",
394 st->id, st->cpu, st->thread);
395 ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n",
396 st->val, st->ena, st->run);
401 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
403 struct stat_round_event *rd = (struct stat_round_event *)event;
406 ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time,
407 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
412 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
414 struct perf_stat_config sc;
417 perf_event__read_stat_config(&sc, &event->stat_config);
419 ret = fprintf(fp, "\n");
420 ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
421 ret += fprintf(fp, "... scale %d\n", sc.scale);
422 ret += fprintf(fp, "... interval %u\n", sc.interval);