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->stats;
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->stats;
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->stats;
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->stats = zalloc(sizeof(struct perf_stat_evsel));
123 if (evsel->stats == NULL)
125 perf_evsel__reset_stat_priv(evsel);
129 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
131 struct perf_stat_evsel *ps = evsel->stats;
134 free(ps->group_data);
135 zfree(&evsel->stats);
138 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
139 int ncpus, int nthreads)
141 struct perf_counts *counts;
143 counts = perf_counts__new(ncpus, nthreads);
145 evsel->prev_raw_counts = counts;
147 return counts ? 0 : -ENOMEM;
150 static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
152 perf_counts__delete(evsel->prev_raw_counts);
153 evsel->prev_raw_counts = NULL;
156 static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
158 int ncpus = perf_evsel__nr_cpus(evsel);
159 int nthreads = thread_map__nr(evsel->threads);
161 if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
162 perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
163 (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
169 int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
171 struct perf_evsel *evsel;
173 evlist__for_each_entry(evlist, evsel) {
174 if (perf_evsel__alloc_stats(evsel, alloc_raw))
181 perf_evlist__free_stats(evlist);
185 void perf_evlist__free_stats(struct perf_evlist *evlist)
187 struct perf_evsel *evsel;
189 evlist__for_each_entry(evlist, evsel) {
190 perf_evsel__free_stat_priv(evsel);
191 perf_evsel__free_counts(evsel);
192 perf_evsel__free_prev_raw_counts(evsel);
196 void perf_evlist__reset_stats(struct perf_evlist *evlist)
198 struct perf_evsel *evsel;
200 evlist__for_each_entry(evlist, evsel) {
201 perf_evsel__reset_stat_priv(evsel);
202 perf_evsel__reset_counts(evsel);
206 static void zero_per_pkg(struct perf_evsel *counter)
208 if (counter->per_pkg_mask)
209 memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
212 static int check_per_pkg(struct perf_evsel *counter,
213 struct perf_counts_values *vals, int cpu, bool *skip)
215 unsigned long *mask = counter->per_pkg_mask;
216 struct cpu_map *cpus = perf_evsel__cpus(counter);
221 if (!counter->per_pkg)
224 if (cpu_map__empty(cpus))
228 mask = zalloc(MAX_NR_CPUS);
232 counter->per_pkg_mask = mask;
236 * we do not consider an event that has not run as a good
237 * instance to mark a package as used (skip=1). Otherwise
238 * we may run into a situation where the first CPU in a package
239 * is not running anything, yet the second is, and this function
240 * would mark the package as used after the first CPU and would
241 * not read the values from the second CPU.
243 if (!(vals->run && vals->ena))
246 s = cpu_map__get_socket(cpus, cpu, NULL);
250 *skip = test_and_set_bit(s, mask) == 1;
255 process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
257 struct perf_counts_values *count)
259 struct perf_counts_values *aggr = &evsel->counts->aggr;
260 static struct perf_counts_values zero;
263 if (check_per_pkg(evsel, count, cpu, &skip)) {
264 pr_err("failed to read per-pkg counter\n");
271 switch (config->aggr_mode) {
276 if (!evsel->snapshot)
277 perf_evsel__compute_deltas(evsel, cpu, thread, count);
278 perf_counts_values__scale(count, config->scale, NULL);
279 if (config->aggr_mode == AGGR_NONE)
280 perf_stat__update_shadow_stats(evsel, count->val, cpu);
281 if (config->aggr_mode == AGGR_THREAD)
282 perf_stat__update_shadow_stats(evsel, count->val, 0);
285 aggr->val += count->val;
287 aggr->ena += count->ena;
288 aggr->run += count->run;
298 static int process_counter_maps(struct perf_stat_config *config,
299 struct perf_evsel *counter)
301 int nthreads = thread_map__nr(counter->threads);
302 int ncpus = perf_evsel__nr_cpus(counter);
305 if (counter->system_wide)
308 for (thread = 0; thread < nthreads; thread++) {
309 for (cpu = 0; cpu < ncpus; cpu++) {
310 if (process_counter_values(config, counter, cpu, thread,
311 perf_counts(counter->counts, cpu, thread)))
319 int perf_stat_process_counter(struct perf_stat_config *config,
320 struct perf_evsel *counter)
322 struct perf_counts_values *aggr = &counter->counts->aggr;
323 struct perf_stat_evsel *ps = counter->stats;
324 u64 *count = counter->counts->aggr.values;
327 aggr->val = aggr->ena = aggr->run = 0;
330 * We calculate counter's data every interval,
331 * and the display code shows ps->res_stats
332 * avg value. We need to zero the stats for
333 * interval mode, otherwise overall avg running
334 * averages will be shown for each interval.
336 if (config->interval)
337 init_stats(ps->res_stats);
339 if (counter->per_pkg)
340 zero_per_pkg(counter);
342 ret = process_counter_maps(config, counter);
346 if (config->aggr_mode != AGGR_GLOBAL)
349 if (!counter->snapshot)
350 perf_evsel__compute_deltas(counter, -1, -1, aggr);
351 perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
353 for (i = 0; i < 3; i++)
354 update_stats(&ps->res_stats[i], count[i]);
357 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
358 perf_evsel__name(counter), count[0], count[1], count[2]);
362 * Save the full runtime - to allow normalization during printout:
364 perf_stat__update_shadow_stats(counter, *count, 0);
369 int perf_event__process_stat_event(struct perf_tool *tool __maybe_unused,
370 union perf_event *event,
371 struct perf_session *session)
373 struct perf_counts_values count;
374 struct stat_event *st = &event->stat;
375 struct perf_evsel *counter;
381 counter = perf_evlist__id2evsel(session->evlist, st->id);
383 pr_err("Failed to resolve counter for stat event.\n");
387 *perf_counts(counter->counts, st->cpu, st->thread) = count;
388 counter->supported = true;
392 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
394 struct stat_event *st = (struct stat_event *) event;
397 ret = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n",
398 st->id, st->cpu, st->thread);
399 ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n",
400 st->val, st->ena, st->run);
405 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
407 struct stat_round_event *rd = (struct stat_round_event *)event;
410 ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time,
411 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
416 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
418 struct perf_stat_config sc;
421 perf_event__read_stat_config(&sc, &event->stat_config);
423 ret = fprintf(fp, "\n");
424 ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
425 ret += fprintf(fp, "... scale %d\n", sc.scale);
426 ret += fprintf(fp, "... interval %u\n", sc.interval);