xref: /openbmc/linux/tools/perf/util/stat.c (revision ec2da07c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <math.h>
5 #include "stat.h"
6 #include "evlist.h"
7 #include "evsel.h"
8 #include "thread_map.h"
9 #include <linux/zalloc.h>
10 
11 void update_stats(struct stats *stats, u64 val)
12 {
13 	double delta;
14 
15 	stats->n++;
16 	delta = val - stats->mean;
17 	stats->mean += delta / stats->n;
18 	stats->M2 += delta*(val - stats->mean);
19 
20 	if (val > stats->max)
21 		stats->max = val;
22 
23 	if (val < stats->min)
24 		stats->min = val;
25 }
26 
27 double avg_stats(struct stats *stats)
28 {
29 	return stats->mean;
30 }
31 
32 /*
33  * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
34  *
35  *       (\Sum n_i^2) - ((\Sum n_i)^2)/n
36  * s^2 = -------------------------------
37  *                  n - 1
38  *
39  * http://en.wikipedia.org/wiki/Stddev
40  *
41  * The std dev of the mean is related to the std dev by:
42  *
43  *             s
44  * s_mean = -------
45  *          sqrt(n)
46  *
47  */
48 double stddev_stats(struct stats *stats)
49 {
50 	double variance, variance_mean;
51 
52 	if (stats->n < 2)
53 		return 0.0;
54 
55 	variance = stats->M2 / (stats->n - 1);
56 	variance_mean = variance / stats->n;
57 
58 	return sqrt(variance_mean);
59 }
60 
61 double rel_stddev_stats(double stddev, double avg)
62 {
63 	double pct = 0.0;
64 
65 	if (avg)
66 		pct = 100.0 * stddev/avg;
67 
68 	return pct;
69 }
70 
71 bool __perf_evsel_stat__is(struct perf_evsel *evsel,
72 			   enum perf_stat_evsel_id id)
73 {
74 	struct perf_stat_evsel *ps = evsel->stats;
75 
76 	return ps->id == id;
77 }
78 
79 #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
80 static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
81 	ID(NONE,		x),
82 	ID(CYCLES_IN_TX,	cpu/cycles-t/),
83 	ID(TRANSACTION_START,	cpu/tx-start/),
84 	ID(ELISION_START,	cpu/el-start/),
85 	ID(CYCLES_IN_TX_CP,	cpu/cycles-ct/),
86 	ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots),
87 	ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued),
88 	ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
89 	ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
90 	ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
91 	ID(SMI_NUM, msr/smi/),
92 	ID(APERF, msr/aperf/),
93 };
94 #undef ID
95 
96 static void perf_stat_evsel_id_init(struct perf_evsel *evsel)
97 {
98 	struct perf_stat_evsel *ps = evsel->stats;
99 	int i;
100 
101 	/* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
102 
103 	for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
104 		if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
105 			ps->id = i;
106 			break;
107 		}
108 	}
109 }
110 
111 static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
112 {
113 	int i;
114 	struct perf_stat_evsel *ps = evsel->stats;
115 
116 	for (i = 0; i < 3; i++)
117 		init_stats(&ps->res_stats[i]);
118 
119 	perf_stat_evsel_id_init(evsel);
120 }
121 
122 static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
123 {
124 	evsel->stats = zalloc(sizeof(struct perf_stat_evsel));
125 	if (evsel->stats == NULL)
126 		return -ENOMEM;
127 	perf_evsel__reset_stat_priv(evsel);
128 	return 0;
129 }
130 
131 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
132 {
133 	struct perf_stat_evsel *ps = evsel->stats;
134 
135 	if (ps)
136 		zfree(&ps->group_data);
137 	zfree(&evsel->stats);
138 }
139 
140 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
141 					     int ncpus, int nthreads)
142 {
143 	struct perf_counts *counts;
144 
145 	counts = perf_counts__new(ncpus, nthreads);
146 	if (counts)
147 		evsel->prev_raw_counts = counts;
148 
149 	return counts ? 0 : -ENOMEM;
150 }
151 
152 static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
153 {
154 	perf_counts__delete(evsel->prev_raw_counts);
155 	evsel->prev_raw_counts = NULL;
156 }
157 
158 static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
159 {
160 	int ncpus = perf_evsel__nr_cpus(evsel);
161 	int nthreads = thread_map__nr(evsel->threads);
162 
163 	if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
164 	    perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
165 	    (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
166 		return -ENOMEM;
167 
168 	return 0;
169 }
170 
171 int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
172 {
173 	struct perf_evsel *evsel;
174 
175 	evlist__for_each_entry(evlist, evsel) {
176 		if (perf_evsel__alloc_stats(evsel, alloc_raw))
177 			goto out_free;
178 	}
179 
180 	return 0;
181 
182 out_free:
183 	perf_evlist__free_stats(evlist);
184 	return -1;
185 }
186 
187 void perf_evlist__free_stats(struct perf_evlist *evlist)
188 {
189 	struct perf_evsel *evsel;
190 
191 	evlist__for_each_entry(evlist, evsel) {
192 		perf_evsel__free_stat_priv(evsel);
193 		perf_evsel__free_counts(evsel);
194 		perf_evsel__free_prev_raw_counts(evsel);
195 	}
196 }
197 
198 void perf_evlist__reset_stats(struct perf_evlist *evlist)
199 {
200 	struct perf_evsel *evsel;
201 
202 	evlist__for_each_entry(evlist, evsel) {
203 		perf_evsel__reset_stat_priv(evsel);
204 		perf_evsel__reset_counts(evsel);
205 	}
206 }
207 
208 static void zero_per_pkg(struct perf_evsel *counter)
209 {
210 	if (counter->per_pkg_mask)
211 		memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
212 }
213 
214 static int check_per_pkg(struct perf_evsel *counter,
215 			 struct perf_counts_values *vals, int cpu, bool *skip)
216 {
217 	unsigned long *mask = counter->per_pkg_mask;
218 	struct cpu_map *cpus = perf_evsel__cpus(counter);
219 	int s;
220 
221 	*skip = false;
222 
223 	if (!counter->per_pkg)
224 		return 0;
225 
226 	if (cpu_map__empty(cpus))
227 		return 0;
228 
229 	if (!mask) {
230 		mask = zalloc(MAX_NR_CPUS);
231 		if (!mask)
232 			return -ENOMEM;
233 
234 		counter->per_pkg_mask = mask;
235 	}
236 
237 	/*
238 	 * we do not consider an event that has not run as a good
239 	 * instance to mark a package as used (skip=1). Otherwise
240 	 * we may run into a situation where the first CPU in a package
241 	 * is not running anything, yet the second is, and this function
242 	 * would mark the package as used after the first CPU and would
243 	 * not read the values from the second CPU.
244 	 */
245 	if (!(vals->run && vals->ena))
246 		return 0;
247 
248 	s = cpu_map__get_socket(cpus, cpu, NULL);
249 	if (s < 0)
250 		return -1;
251 
252 	*skip = test_and_set_bit(s, mask) == 1;
253 	return 0;
254 }
255 
256 static int
257 process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
258 		       int cpu, int thread,
259 		       struct perf_counts_values *count)
260 {
261 	struct perf_counts_values *aggr = &evsel->counts->aggr;
262 	static struct perf_counts_values zero;
263 	bool skip = false;
264 
265 	if (check_per_pkg(evsel, count, cpu, &skip)) {
266 		pr_err("failed to read per-pkg counter\n");
267 		return -1;
268 	}
269 
270 	if (skip)
271 		count = &zero;
272 
273 	switch (config->aggr_mode) {
274 	case AGGR_THREAD:
275 	case AGGR_CORE:
276 	case AGGR_DIE:
277 	case AGGR_SOCKET:
278 	case AGGR_NONE:
279 		if (!evsel->snapshot)
280 			perf_evsel__compute_deltas(evsel, cpu, thread, count);
281 		perf_counts_values__scale(count, config->scale, NULL);
282 		if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) {
283 			perf_stat__update_shadow_stats(evsel, count->val,
284 						       cpu, &rt_stat);
285 		}
286 
287 		if (config->aggr_mode == AGGR_THREAD) {
288 			if (config->stats)
289 				perf_stat__update_shadow_stats(evsel,
290 					count->val, 0, &config->stats[thread]);
291 			else
292 				perf_stat__update_shadow_stats(evsel,
293 					count->val, 0, &rt_stat);
294 		}
295 		break;
296 	case AGGR_GLOBAL:
297 		aggr->val += count->val;
298 		aggr->ena += count->ena;
299 		aggr->run += count->run;
300 	case AGGR_UNSET:
301 	default:
302 		break;
303 	}
304 
305 	return 0;
306 }
307 
308 static int process_counter_maps(struct perf_stat_config *config,
309 				struct perf_evsel *counter)
310 {
311 	int nthreads = thread_map__nr(counter->threads);
312 	int ncpus = perf_evsel__nr_cpus(counter);
313 	int cpu, thread;
314 
315 	if (counter->system_wide)
316 		nthreads = 1;
317 
318 	for (thread = 0; thread < nthreads; thread++) {
319 		for (cpu = 0; cpu < ncpus; cpu++) {
320 			if (process_counter_values(config, counter, cpu, thread,
321 						   perf_counts(counter->counts, cpu, thread)))
322 				return -1;
323 		}
324 	}
325 
326 	return 0;
327 }
328 
329 int perf_stat_process_counter(struct perf_stat_config *config,
330 			      struct perf_evsel *counter)
331 {
332 	struct perf_counts_values *aggr = &counter->counts->aggr;
333 	struct perf_stat_evsel *ps = counter->stats;
334 	u64 *count = counter->counts->aggr.values;
335 	int i, ret;
336 
337 	aggr->val = aggr->ena = aggr->run = 0;
338 
339 	/*
340 	 * We calculate counter's data every interval,
341 	 * and the display code shows ps->res_stats
342 	 * avg value. We need to zero the stats for
343 	 * interval mode, otherwise overall avg running
344 	 * averages will be shown for each interval.
345 	 */
346 	if (config->interval)
347 		init_stats(ps->res_stats);
348 
349 	if (counter->per_pkg)
350 		zero_per_pkg(counter);
351 
352 	ret = process_counter_maps(config, counter);
353 	if (ret)
354 		return ret;
355 
356 	if (config->aggr_mode != AGGR_GLOBAL)
357 		return 0;
358 
359 	if (!counter->snapshot)
360 		perf_evsel__compute_deltas(counter, -1, -1, aggr);
361 	perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
362 
363 	for (i = 0; i < 3; i++)
364 		update_stats(&ps->res_stats[i], count[i]);
365 
366 	if (verbose > 0) {
367 		fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
368 			perf_evsel__name(counter), count[0], count[1], count[2]);
369 	}
370 
371 	/*
372 	 * Save the full runtime - to allow normalization during printout:
373 	 */
374 	perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
375 
376 	return 0;
377 }
378 
379 int perf_event__process_stat_event(struct perf_session *session,
380 				   union perf_event *event)
381 {
382 	struct perf_counts_values count;
383 	struct stat_event *st = &event->stat;
384 	struct perf_evsel *counter;
385 
386 	count.val = st->val;
387 	count.ena = st->ena;
388 	count.run = st->run;
389 
390 	counter = perf_evlist__id2evsel(session->evlist, st->id);
391 	if (!counter) {
392 		pr_err("Failed to resolve counter for stat event.\n");
393 		return -EINVAL;
394 	}
395 
396 	*perf_counts(counter->counts, st->cpu, st->thread) = count;
397 	counter->supported = true;
398 	return 0;
399 }
400 
401 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
402 {
403 	struct stat_event *st = (struct stat_event *) event;
404 	size_t ret;
405 
406 	ret  = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n",
407 		       st->id, st->cpu, st->thread);
408 	ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n",
409 		       st->val, st->ena, st->run);
410 
411 	return ret;
412 }
413 
414 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
415 {
416 	struct stat_round_event *rd = (struct stat_round_event *)event;
417 	size_t ret;
418 
419 	ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time,
420 		      rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
421 
422 	return ret;
423 }
424 
425 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
426 {
427 	struct perf_stat_config sc;
428 	size_t ret;
429 
430 	perf_event__read_stat_config(&sc, &event->stat_config);
431 
432 	ret  = fprintf(fp, "\n");
433 	ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
434 	ret += fprintf(fp, "... scale     %d\n", sc.scale);
435 	ret += fprintf(fp, "... interval  %u\n", sc.interval);
436 
437 	return ret;
438 }
439 
440 int create_perf_stat_counter(struct perf_evsel *evsel,
441 			     struct perf_stat_config *config,
442 			     struct target *target)
443 {
444 	struct perf_event_attr *attr = &evsel->attr;
445 	struct perf_evsel *leader = evsel->leader;
446 
447 	attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
448 			    PERF_FORMAT_TOTAL_TIME_RUNNING;
449 
450 	/*
451 	 * The event is part of non trivial group, let's enable
452 	 * the group read (for leader) and ID retrieval for all
453 	 * members.
454 	 */
455 	if (leader->nr_members > 1)
456 		attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
457 
458 	attr->inherit = !config->no_inherit;
459 
460 	/*
461 	 * Some events get initialized with sample_(period/type) set,
462 	 * like tracepoints. Clear it up for counting.
463 	 */
464 	attr->sample_period = 0;
465 
466 	if (config->identifier)
467 		attr->sample_type = PERF_SAMPLE_IDENTIFIER;
468 
469 	/*
470 	 * Disabling all counters initially, they will be enabled
471 	 * either manually by us or by kernel via enable_on_exec
472 	 * set later.
473 	 */
474 	if (perf_evsel__is_group_leader(evsel)) {
475 		attr->disabled = 1;
476 
477 		/*
478 		 * In case of initial_delay we enable tracee
479 		 * events manually.
480 		 */
481 		if (target__none(target) && !config->initial_delay)
482 			attr->enable_on_exec = 1;
483 	}
484 
485 	if (target__has_cpu(target) && !target__has_per_thread(target))
486 		return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
487 
488 	return perf_evsel__open_per_thread(evsel, evsel->threads);
489 }
490 
491 int perf_stat_synthesize_config(struct perf_stat_config *config,
492 				struct perf_tool *tool,
493 				struct perf_evlist *evlist,
494 				perf_event__handler_t process,
495 				bool attrs)
496 {
497 	int err;
498 
499 	if (attrs) {
500 		err = perf_event__synthesize_attrs(tool, evlist, process);
501 		if (err < 0) {
502 			pr_err("Couldn't synthesize attrs.\n");
503 			return err;
504 		}
505 	}
506 
507 	err = perf_event__synthesize_extra_attr(tool, evlist, process,
508 						attrs);
509 
510 	err = perf_event__synthesize_thread_map2(tool, evlist->threads,
511 						 process, NULL);
512 	if (err < 0) {
513 		pr_err("Couldn't synthesize thread map.\n");
514 		return err;
515 	}
516 
517 	err = perf_event__synthesize_cpu_map(tool, evlist->cpus,
518 					     process, NULL);
519 	if (err < 0) {
520 		pr_err("Couldn't synthesize thread map.\n");
521 		return err;
522 	}
523 
524 	err = perf_event__synthesize_stat_config(tool, config, process, NULL);
525 	if (err < 0) {
526 		pr_err("Couldn't synthesize config.\n");
527 		return err;
528 	}
529 
530 	return 0;
531 }
532