5 #include "thread_map.h"
7 void update_stats(struct stats *stats, u64 val)
12 delta = val - stats->mean;
13 stats->mean += delta / stats->n;
14 stats->M2 += delta*(val - stats->mean);
23 double avg_stats(struct stats *stats)
29 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
31 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
32 * s^2 = -------------------------------
35 * http://en.wikipedia.org/wiki/Stddev
37 * The std dev of the mean is related to the std dev by:
44 double stddev_stats(struct stats *stats)
46 double variance, variance_mean;
51 variance = stats->M2 / (stats->n - 1);
52 variance_mean = variance / stats->n;
54 return sqrt(variance_mean);
57 double rel_stddev_stats(double stddev, double avg)
62 pct = 100.0 * stddev/avg;
67 bool __perf_evsel_stat__is(struct perf_evsel *evsel,
68 enum perf_stat_evsel_id id)
70 struct perf_stat *ps = evsel->priv;
75 #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
76 static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
78 ID(CYCLES_IN_TX, cpu/cycles-t/),
79 ID(TRANSACTION_START, cpu/tx-start/),
80 ID(ELISION_START, cpu/el-start/),
81 ID(CYCLES_IN_TX_CP, cpu/cycles-ct/),
85 void perf_stat_evsel_id_init(struct perf_evsel *evsel)
87 struct perf_stat *ps = evsel->priv;
90 /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
92 for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
93 if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
100 struct perf_counts *perf_counts__new(int ncpus, int nthreads)
102 struct perf_counts *counts = zalloc(sizeof(*counts));
105 struct xyarray *values;
107 values = xyarray__new(ncpus, nthreads, sizeof(struct perf_counts_values));
113 counts->values = values;
119 void perf_counts__delete(struct perf_counts *counts)
122 xyarray__delete(counts->values);
127 static void perf_counts__reset(struct perf_counts *counts)
129 xyarray__reset(counts->values);
132 void perf_evsel__reset_counts(struct perf_evsel *evsel)
134 perf_counts__reset(evsel->counts);
137 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus, int nthreads)
139 evsel->counts = perf_counts__new(ncpus, nthreads);
140 return evsel->counts != NULL ? 0 : -ENOMEM;
143 void perf_evsel__free_counts(struct perf_evsel *evsel)
145 perf_counts__delete(evsel->counts);
146 evsel->counts = NULL;
149 void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
152 struct perf_stat *ps = evsel->priv;
154 for (i = 0; i < 3; i++)
155 init_stats(&ps->res_stats[i]);
157 perf_stat_evsel_id_init(evsel);
160 int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
162 evsel->priv = zalloc(sizeof(struct perf_stat));
163 if (evsel->priv == NULL)
165 perf_evsel__reset_stat_priv(evsel);
169 void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
174 int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
175 int ncpus, int nthreads)
177 struct perf_counts *counts;
179 counts = perf_counts__new(ncpus, nthreads);
181 evsel->prev_raw_counts = counts;
183 return counts ? 0 : -ENOMEM;
186 void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
188 perf_counts__delete(evsel->prev_raw_counts);
189 evsel->prev_raw_counts = NULL;
192 int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
194 int ncpus = perf_evsel__nr_cpus(evsel);
195 int nthreads = thread_map__nr(evsel->threads);
197 if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
198 perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
199 (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
205 int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
207 struct perf_evsel *evsel;
209 evlist__for_each(evlist, evsel) {
210 if (perf_evsel__alloc_stats(evsel, alloc_raw))
217 perf_evlist__free_stats(evlist);
221 void perf_evlist__free_stats(struct perf_evlist *evlist)
223 struct perf_evsel *evsel;
225 evlist__for_each(evlist, evsel) {
226 perf_evsel__free_stat_priv(evsel);
227 perf_evsel__free_counts(evsel);
228 perf_evsel__free_prev_raw_counts(evsel);
232 void perf_evlist__reset_stats(struct perf_evlist *evlist)
234 struct perf_evsel *evsel;
236 evlist__for_each(evlist, evsel) {
237 perf_evsel__reset_stat_priv(evsel);
238 perf_evsel__reset_counts(evsel);