xref: /linux/tools/perf/util/stat.c (revision 3d689ed6099a1a11c38bb78aff7498e78e287e0b)
1 #include <inttypes.h>
2 #include <math.h>
3 #include "stat.h"
4 #include "evlist.h"
5 #include "evsel.h"
6 #include "thread_map.h"
7 
8 void update_stats(struct stats *stats, u64 val)
9 {
10 	double delta;
11 
12 	stats->n++;
13 	delta = val - stats->mean;
14 	stats->mean += delta / stats->n;
15 	stats->M2 += delta*(val - stats->mean);
16 
17 	if (val > stats->max)
18 		stats->max = val;
19 
20 	if (val < stats->min)
21 		stats->min = val;
22 }
23 
24 double avg_stats(struct stats *stats)
25 {
26 	return stats->mean;
27 }
28 
29 /*
30  * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
31  *
32  *       (\Sum n_i^2) - ((\Sum n_i)^2)/n
33  * s^2 = -------------------------------
34  *                  n - 1
35  *
36  * http://en.wikipedia.org/wiki/Stddev
37  *
38  * The std dev of the mean is related to the std dev by:
39  *
40  *             s
41  * s_mean = -------
42  *          sqrt(n)
43  *
44  */
45 double stddev_stats(struct stats *stats)
46 {
47 	double variance, variance_mean;
48 
49 	if (stats->n < 2)
50 		return 0.0;
51 
52 	variance = stats->M2 / (stats->n - 1);
53 	variance_mean = variance / stats->n;
54 
55 	return sqrt(variance_mean);
56 }
57 
58 double rel_stddev_stats(double stddev, double avg)
59 {
60 	double pct = 0.0;
61 
62 	if (avg)
63 		pct = 100.0 * stddev/avg;
64 
65 	return pct;
66 }
67 
68 bool __perf_evsel_stat__is(struct perf_evsel *evsel,
69 			   enum perf_stat_evsel_id id)
70 {
71 	struct perf_stat_evsel *ps = evsel->priv;
72 
73 	return ps->id == id;
74 }
75 
76 #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
77 static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
78 	ID(NONE,		x),
79 	ID(CYCLES_IN_TX,	cpu/cycles-t/),
80 	ID(TRANSACTION_START,	cpu/tx-start/),
81 	ID(ELISION_START,	cpu/el-start/),
82 	ID(CYCLES_IN_TX_CP,	cpu/cycles-ct/),
83 	ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots),
84 	ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued),
85 	ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
86 	ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
87 	ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
88 };
89 #undef ID
90 
91 void perf_stat_evsel_id_init(struct perf_evsel *evsel)
92 {
93 	struct perf_stat_evsel *ps = evsel->priv;
94 	int i;
95 
96 	/* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
97 
98 	for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
99 		if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
100 			ps->id = i;
101 			break;
102 		}
103 	}
104 }
105 
106 static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
107 {
108 	int i;
109 	struct perf_stat_evsel *ps = evsel->priv;
110 
111 	for (i = 0; i < 3; i++)
112 		init_stats(&ps->res_stats[i]);
113 
114 	perf_stat_evsel_id_init(evsel);
115 }
116 
117 static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
118 {
119 	evsel->priv = zalloc(sizeof(struct perf_stat_evsel));
120 	if (evsel->priv == NULL)
121 		return -ENOMEM;
122 	perf_evsel__reset_stat_priv(evsel);
123 	return 0;
124 }
125 
126 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
127 {
128 	zfree(&evsel->priv);
129 }
130 
131 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
132 					     int ncpus, int nthreads)
133 {
134 	struct perf_counts *counts;
135 
136 	counts = perf_counts__new(ncpus, nthreads);
137 	if (counts)
138 		evsel->prev_raw_counts = counts;
139 
140 	return counts ? 0 : -ENOMEM;
141 }
142 
143 static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
144 {
145 	perf_counts__delete(evsel->prev_raw_counts);
146 	evsel->prev_raw_counts = NULL;
147 }
148 
149 static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
150 {
151 	int ncpus = perf_evsel__nr_cpus(evsel);
152 	int nthreads = thread_map__nr(evsel->threads);
153 
154 	if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
155 	    perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
156 	    (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
157 		return -ENOMEM;
158 
159 	return 0;
160 }
161 
162 int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
163 {
164 	struct perf_evsel *evsel;
165 
166 	evlist__for_each_entry(evlist, evsel) {
167 		if (perf_evsel__alloc_stats(evsel, alloc_raw))
168 			goto out_free;
169 	}
170 
171 	return 0;
172 
173 out_free:
174 	perf_evlist__free_stats(evlist);
175 	return -1;
176 }
177 
178 void perf_evlist__free_stats(struct perf_evlist *evlist)
179 {
180 	struct perf_evsel *evsel;
181 
182 	evlist__for_each_entry(evlist, evsel) {
183 		perf_evsel__free_stat_priv(evsel);
184 		perf_evsel__free_counts(evsel);
185 		perf_evsel__free_prev_raw_counts(evsel);
186 	}
187 }
188 
189 void perf_evlist__reset_stats(struct perf_evlist *evlist)
190 {
191 	struct perf_evsel *evsel;
192 
193 	evlist__for_each_entry(evlist, evsel) {
194 		perf_evsel__reset_stat_priv(evsel);
195 		perf_evsel__reset_counts(evsel);
196 	}
197 }
198 
199 static void zero_per_pkg(struct perf_evsel *counter)
200 {
201 	if (counter->per_pkg_mask)
202 		memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
203 }
204 
205 static int check_per_pkg(struct perf_evsel *counter,
206 			 struct perf_counts_values *vals, int cpu, bool *skip)
207 {
208 	unsigned long *mask = counter->per_pkg_mask;
209 	struct cpu_map *cpus = perf_evsel__cpus(counter);
210 	int s;
211 
212 	*skip = false;
213 
214 	if (!counter->per_pkg)
215 		return 0;
216 
217 	if (cpu_map__empty(cpus))
218 		return 0;
219 
220 	if (!mask) {
221 		mask = zalloc(MAX_NR_CPUS);
222 		if (!mask)
223 			return -ENOMEM;
224 
225 		counter->per_pkg_mask = mask;
226 	}
227 
228 	/*
229 	 * we do not consider an event that has not run as a good
230 	 * instance to mark a package as used (skip=1). Otherwise
231 	 * we may run into a situation where the first CPU in a package
232 	 * is not running anything, yet the second is, and this function
233 	 * would mark the package as used after the first CPU and would
234 	 * not read the values from the second CPU.
235 	 */
236 	if (!(vals->run && vals->ena))
237 		return 0;
238 
239 	s = cpu_map__get_socket(cpus, cpu, NULL);
240 	if (s < 0)
241 		return -1;
242 
243 	*skip = test_and_set_bit(s, mask) == 1;
244 	return 0;
245 }
246 
247 static int
248 process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
249 		       int cpu, int thread,
250 		       struct perf_counts_values *count)
251 {
252 	struct perf_counts_values *aggr = &evsel->counts->aggr;
253 	static struct perf_counts_values zero;
254 	bool skip = false;
255 
256 	if (check_per_pkg(evsel, count, cpu, &skip)) {
257 		pr_err("failed to read per-pkg counter\n");
258 		return -1;
259 	}
260 
261 	if (skip)
262 		count = &zero;
263 
264 	switch (config->aggr_mode) {
265 	case AGGR_THREAD:
266 	case AGGR_CORE:
267 	case AGGR_SOCKET:
268 	case AGGR_NONE:
269 		if (!evsel->snapshot)
270 			perf_evsel__compute_deltas(evsel, cpu, thread, count);
271 		perf_counts_values__scale(count, config->scale, NULL);
272 		if (config->aggr_mode == AGGR_NONE)
273 			perf_stat__update_shadow_stats(evsel, count->values, cpu);
274 		break;
275 	case AGGR_GLOBAL:
276 		aggr->val += count->val;
277 		if (config->scale) {
278 			aggr->ena += count->ena;
279 			aggr->run += count->run;
280 		}
281 	case AGGR_UNSET:
282 	default:
283 		break;
284 	}
285 
286 	return 0;
287 }
288 
289 static int process_counter_maps(struct perf_stat_config *config,
290 				struct perf_evsel *counter)
291 {
292 	int nthreads = thread_map__nr(counter->threads);
293 	int ncpus = perf_evsel__nr_cpus(counter);
294 	int cpu, thread;
295 
296 	if (counter->system_wide)
297 		nthreads = 1;
298 
299 	for (thread = 0; thread < nthreads; thread++) {
300 		for (cpu = 0; cpu < ncpus; cpu++) {
301 			if (process_counter_values(config, counter, cpu, thread,
302 						   perf_counts(counter->counts, cpu, thread)))
303 				return -1;
304 		}
305 	}
306 
307 	return 0;
308 }
309 
310 int perf_stat_process_counter(struct perf_stat_config *config,
311 			      struct perf_evsel *counter)
312 {
313 	struct perf_counts_values *aggr = &counter->counts->aggr;
314 	struct perf_stat_evsel *ps = counter->priv;
315 	u64 *count = counter->counts->aggr.values;
316 	u64 val;
317 	int i, ret;
318 
319 	aggr->val = aggr->ena = aggr->run = 0;
320 
321 	/*
322 	 * We calculate counter's data every interval,
323 	 * and the display code shows ps->res_stats
324 	 * avg value. We need to zero the stats for
325 	 * interval mode, otherwise overall avg running
326 	 * averages will be shown for each interval.
327 	 */
328 	if (config->interval)
329 		init_stats(ps->res_stats);
330 
331 	if (counter->per_pkg)
332 		zero_per_pkg(counter);
333 
334 	ret = process_counter_maps(config, counter);
335 	if (ret)
336 		return ret;
337 
338 	if (config->aggr_mode != AGGR_GLOBAL)
339 		return 0;
340 
341 	if (!counter->snapshot)
342 		perf_evsel__compute_deltas(counter, -1, -1, aggr);
343 	perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
344 
345 	for (i = 0; i < 3; i++)
346 		update_stats(&ps->res_stats[i], count[i]);
347 
348 	if (verbose > 0) {
349 		fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
350 			perf_evsel__name(counter), count[0], count[1], count[2]);
351 	}
352 
353 	/*
354 	 * Save the full runtime - to allow normalization during printout:
355 	 */
356 	val = counter->scale * *count;
357 	perf_stat__update_shadow_stats(counter, &val, 0);
358 
359 	return 0;
360 }
361 
362 int perf_event__process_stat_event(struct perf_tool *tool __maybe_unused,
363 				   union perf_event *event,
364 				   struct perf_session *session)
365 {
366 	struct perf_counts_values count;
367 	struct stat_event *st = &event->stat;
368 	struct perf_evsel *counter;
369 
370 	count.val = st->val;
371 	count.ena = st->ena;
372 	count.run = st->run;
373 
374 	counter = perf_evlist__id2evsel(session->evlist, st->id);
375 	if (!counter) {
376 		pr_err("Failed to resolve counter for stat event.\n");
377 		return -EINVAL;
378 	}
379 
380 	*perf_counts(counter->counts, st->cpu, st->thread) = count;
381 	counter->supported = true;
382 	return 0;
383 }
384 
385 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
386 {
387 	struct stat_event *st = (struct stat_event *) event;
388 	size_t ret;
389 
390 	ret  = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n",
391 		       st->id, st->cpu, st->thread);
392 	ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n",
393 		       st->val, st->ena, st->run);
394 
395 	return ret;
396 }
397 
398 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
399 {
400 	struct stat_round_event *rd = (struct stat_round_event *)event;
401 	size_t ret;
402 
403 	ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time,
404 		      rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
405 
406 	return ret;
407 }
408 
409 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
410 {
411 	struct perf_stat_config sc;
412 	size_t ret;
413 
414 	perf_event__read_stat_config(&sc, &event->stat_config);
415 
416 	ret  = fprintf(fp, "\n");
417 	ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
418 	ret += fprintf(fp, "... scale     %d\n", sc.scale);
419 	ret += fprintf(fp, "... interval  %u\n", sc.interval);
420 
421 	return ret;
422 }
423