1#include <errno.h>
2#include <inttypes.h>
3#include <math.h>
4#include "stat.h"
5#include "evlist.h"
6#include "evsel.h"
7#include "thread_map.h"
8
9void update_stats(struct stats *stats, u64 val)
10{
11 double delta;
12
13 stats->n++;
14 delta = val - stats->mean;
15 stats->mean += delta / stats->n;
16 stats->M2 += delta*(val - stats->mean);
17
18 if (val > stats->max)
19 stats->max = val;
20
21 if (val < stats->min)
22 stats->min = val;
23}
24
25double avg_stats(struct stats *stats)
26{
27 return stats->mean;
28}
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46double stddev_stats(struct stats *stats)
47{
48 double variance, variance_mean;
49
50 if (stats->n < 2)
51 return 0.0;
52
53 variance = stats->M2 / (stats->n - 1);
54 variance_mean = variance / stats->n;
55
56 return sqrt(variance_mean);
57}
58
59double rel_stddev_stats(double stddev, double avg)
60{
61 double pct = 0.0;
62
63 if (avg)
64 pct = 100.0 * stddev/avg;
65
66 return pct;
67}
68
69bool __perf_evsel_stat__is(struct perf_evsel *evsel,
70 enum perf_stat_evsel_id id)
71{
72 struct perf_stat_evsel *ps = evsel->stats;
73
74 return ps->id == id;
75}
76
77#define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
78static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
79 ID(NONE, x),
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/),
91};
92#undef ID
93
94static void perf_stat_evsel_id_init(struct perf_evsel *evsel)
95{
96 struct perf_stat_evsel *ps = evsel->stats;
97 int i;
98
99
100
101 for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
102 if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
103 ps->id = i;
104 break;
105 }
106 }
107}
108
109static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
110{
111 int i;
112 struct perf_stat_evsel *ps = evsel->stats;
113
114 for (i = 0; i < 3; i++)
115 init_stats(&ps->res_stats[i]);
116
117 perf_stat_evsel_id_init(evsel);
118}
119
120static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
121{
122 evsel->stats = zalloc(sizeof(struct perf_stat_evsel));
123 if (evsel->stats == NULL)
124 return -ENOMEM;
125 perf_evsel__reset_stat_priv(evsel);
126 return 0;
127}
128
129static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
130{
131 struct perf_stat_evsel *ps = evsel->stats;
132
133 if (ps)
134 free(ps->group_data);
135 zfree(&evsel->stats);
136}
137
138static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
139 int ncpus, int nthreads)
140{
141 struct perf_counts *counts;
142
143 counts = perf_counts__new(ncpus, nthreads);
144 if (counts)
145 evsel->prev_raw_counts = counts;
146
147 return counts ? 0 : -ENOMEM;
148}
149
150static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
151{
152 perf_counts__delete(evsel->prev_raw_counts);
153 evsel->prev_raw_counts = NULL;
154}
155
156static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
157{
158 int ncpus = perf_evsel__nr_cpus(evsel);
159 int nthreads = thread_map__nr(evsel->threads);
160
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))
164 return -ENOMEM;
165
166 return 0;
167}
168
169int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
170{
171 struct perf_evsel *evsel;
172
173 evlist__for_each_entry(evlist, evsel) {
174 if (perf_evsel__alloc_stats(evsel, alloc_raw))
175 goto out_free;
176 }
177
178 return 0;
179
180out_free:
181 perf_evlist__free_stats(evlist);
182 return -1;
183}
184
185void perf_evlist__free_stats(struct perf_evlist *evlist)
186{
187 struct perf_evsel *evsel;
188
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);
193 }
194}
195
196void perf_evlist__reset_stats(struct perf_evlist *evlist)
197{
198 struct perf_evsel *evsel;
199
200 evlist__for_each_entry(evlist, evsel) {
201 perf_evsel__reset_stat_priv(evsel);
202 perf_evsel__reset_counts(evsel);
203 }
204}
205
206static void zero_per_pkg(struct perf_evsel *counter)
207{
208 if (counter->per_pkg_mask)
209 memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
210}
211
212static int check_per_pkg(struct perf_evsel *counter,
213 struct perf_counts_values *vals, int cpu, bool *skip)
214{
215 unsigned long *mask = counter->per_pkg_mask;
216 struct cpu_map *cpus = perf_evsel__cpus(counter);
217 int s;
218
219 *skip = false;
220
221 if (!counter->per_pkg)
222 return 0;
223
224 if (cpu_map__empty(cpus))
225 return 0;
226
227 if (!mask) {
228 mask = zalloc(MAX_NR_CPUS);
229 if (!mask)
230 return -ENOMEM;
231
232 counter->per_pkg_mask = mask;
233 }
234
235
236
237
238
239
240
241
242
243 if (!(vals->run && vals->ena))
244 return 0;
245
246 s = cpu_map__get_socket(cpus, cpu, NULL);
247 if (s < 0)
248 return -1;
249
250 *skip = test_and_set_bit(s, mask) == 1;
251 return 0;
252}
253
254static int
255process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
256 int cpu, int thread,
257 struct perf_counts_values *count)
258{
259 struct perf_counts_values *aggr = &evsel->counts->aggr;
260 static struct perf_counts_values zero;
261 bool skip = false;
262
263 if (check_per_pkg(evsel, count, cpu, &skip)) {
264 pr_err("failed to read per-pkg counter\n");
265 return -1;
266 }
267
268 if (skip)
269 count = &zero;
270
271 switch (config->aggr_mode) {
272 case AGGR_THREAD:
273 case AGGR_CORE:
274 case AGGR_DIE:
275 case AGGR_SOCKET:
276 case AGGR_NONE:
277 if (!evsel->snapshot)
278 perf_evsel__compute_deltas(evsel, cpu, thread, count);
279 perf_counts_values__scale(count, config->scale, NULL);
280 if ((config->aggr_mode == AGGR_NONE) && (!evsel->percore)) {
281 perf_stat__update_shadow_stats(evsel, count->val,
282 cpu, &rt_stat);
283 }
284
285 if (config->aggr_mode == AGGR_THREAD) {
286 if (config->stats)
287 perf_stat__update_shadow_stats(evsel,
288 count->val, 0, &config->stats[thread]);
289 else
290 perf_stat__update_shadow_stats(evsel,
291 count->val, 0, &rt_stat);
292 }
293 break;
294 case AGGR_GLOBAL:
295 aggr->val += count->val;
296 if (config->scale) {
297 aggr->ena += count->ena;
298 aggr->run += count->run;
299 }
300 case AGGR_UNSET:
301 default:
302 break;
303 }
304
305 return 0;
306}
307
308static 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
329int 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
341
342
343
344
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
373
374 perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
375
376 return 0;
377}
378
379int 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
401size_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
414size_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
425size_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
440int 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 if (config->scale) {
448 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
449 PERF_FORMAT_TOTAL_TIME_RUNNING;
450 }
451
452
453
454
455
456
457 if (leader->nr_members > 1)
458 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
459
460 attr->inherit = !config->no_inherit;
461
462
463
464
465
466 attr->sample_period = 0;
467
468 if (config->identifier)
469 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
470
471
472
473
474
475
476 if (perf_evsel__is_group_leader(evsel)) {
477 attr->disabled = 1;
478
479
480
481
482
483 if (target__none(target) && !config->initial_delay)
484 attr->enable_on_exec = 1;
485 }
486
487 if (target__has_cpu(target) && !target__has_per_thread(target))
488 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
489
490 return perf_evsel__open_per_thread(evsel, evsel->threads);
491}
492
493int perf_stat_synthesize_config(struct perf_stat_config *config,
494 struct perf_tool *tool,
495 struct perf_evlist *evlist,
496 perf_event__handler_t process,
497 bool attrs)
498{
499 int err;
500
501 if (attrs) {
502 err = perf_event__synthesize_attrs(tool, evlist, process);
503 if (err < 0) {
504 pr_err("Couldn't synthesize attrs.\n");
505 return err;
506 }
507 }
508
509 err = perf_event__synthesize_extra_attr(tool, evlist, process,
510 attrs);
511
512 err = perf_event__synthesize_thread_map2(tool, evlist->threads,
513 process, NULL);
514 if (err < 0) {
515 pr_err("Couldn't synthesize thread map.\n");
516 return err;
517 }
518
519 err = perf_event__synthesize_cpu_map(tool, evlist->cpus,
520 process, NULL);
521 if (err < 0) {
522 pr_err("Couldn't synthesize thread map.\n");
523 return err;
524 }
525
526 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
527 if (err < 0) {
528 pr_err("Couldn't synthesize config.\n");
529 return err;
530 }
531
532 return 0;
533}
534