blob: 6fb4dac26ccca8026f0209c39dd5c33617f31d15 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Xiao Guangrong0007ece2012-09-17 16:31:14 +08002#ifndef __PERF_STATS_H
3#define __PERF_STATS_H
4
Borislav Petkovd944c4e2014-04-25 21:31:02 +02005#include <linux/types.h>
Jiri Olsaf87027b2015-06-03 16:25:59 +02006#include <stdio.h>
Jiri Olsaa8e02322015-06-26 11:29:10 +02007#include "xyarray.h"
Jin Yaoe5fcc2a2017-12-05 22:03:01 +08008#include "rblist.h"
Jiri Olsa728c0ee2018-08-30 08:32:11 +02009#include "perf.h"
Jiri Olsa0a4e64d2018-08-30 08:32:23 +020010#include "event.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080011
Jiri Olsae55c14a2018-04-23 11:08:21 +020012struct stats {
Xiao Guangrong0007ece2012-09-17 16:31:14 +080013 double n, mean, M2;
David Ahernffe4f3c2013-08-02 14:05:40 -060014 u64 max, min;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080015};
16
Jiri Olsae2f56da2015-06-04 15:50:55 +020017enum perf_stat_evsel_id {
18 PERF_STAT_EVSEL_ID__NONE = 0,
Jiri Olsa4c358d52015-06-03 16:25:52 +020019 PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
20 PERF_STAT_EVSEL_ID__TRANSACTION_START,
21 PERF_STAT_EVSEL_ID__ELISION_START,
22 PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
Andi Kleen239bd472016-05-24 12:52:37 -070023 PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
24 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
25 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
26 PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
27 PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
Kan Liangdaefd0b2017-05-26 12:05:38 -070028 PERF_STAT_EVSEL_ID__SMI_NUM,
29 PERF_STAT_EVSEL_ID__APERF,
Jiri Olsae2f56da2015-06-04 15:50:55 +020030 PERF_STAT_EVSEL_ID__MAX,
31};
32
Jiri Olsa581cc8a2015-10-16 12:41:03 +020033struct perf_stat_evsel {
Jiri Olsaf7794d52017-07-26 14:02:05 +020034 struct stats res_stats[3];
35 enum perf_stat_evsel_id id;
36 u64 *group_data;
Jiri Olsae2f56da2015-06-04 15:50:55 +020037};
38
Jiri Olsaf87027b2015-06-03 16:25:59 +020039enum aggr_mode {
40 AGGR_NONE,
41 AGGR_GLOBAL,
42 AGGR_SOCKET,
43 AGGR_CORE,
Jiri Olsa32b8af82015-06-26 11:29:27 +020044 AGGR_THREAD,
Jiri Olsa208df992015-10-16 12:41:04 +020045 AGGR_UNSET,
Jiri Olsaf87027b2015-06-03 16:25:59 +020046};
47
Jin Yaoe5fcc2a2017-12-05 22:03:01 +080048enum {
49 CTX_BIT_USER = 1 << 0,
50 CTX_BIT_KERNEL = 1 << 1,
51 CTX_BIT_HV = 1 << 2,
52 CTX_BIT_HOST = 1 << 3,
53 CTX_BIT_IDLE = 1 << 4,
54 CTX_BIT_MAX = 1 << 5,
55};
56
57#define NUM_CTX CTX_BIT_MAX
58
59enum stat_type {
60 STAT_NONE = 0,
61 STAT_NSECS,
62 STAT_CYCLES,
63 STAT_STALLED_CYCLES_FRONT,
64 STAT_STALLED_CYCLES_BACK,
65 STAT_BRANCHES,
66 STAT_CACHEREFS,
67 STAT_L1_DCACHE,
68 STAT_L1_ICACHE,
69 STAT_LL_CACHE,
70 STAT_ITLB_CACHE,
71 STAT_DTLB_CACHE,
72 STAT_CYCLES_IN_TX,
73 STAT_TRANSACTION,
74 STAT_ELISION,
75 STAT_TOPDOWN_TOTAL_SLOTS,
76 STAT_TOPDOWN_SLOTS_ISSUED,
77 STAT_TOPDOWN_SLOTS_RETIRED,
78 STAT_TOPDOWN_FETCH_BUBBLES,
79 STAT_TOPDOWN_RECOVERY_BUBBLES,
80 STAT_SMI_NUM,
81 STAT_APERF,
82 STAT_MAX
83};
84
85struct runtime_stat {
86 struct rblist value_list;
87};
88
Jiri Olsa421a50f2015-07-21 14:31:22 +020089struct perf_stat_config {
Jiri Olsa728c0ee2018-08-30 08:32:11 +020090 enum aggr_mode aggr_mode;
91 bool scale;
Jiri Olsa5698f262018-08-30 08:32:12 +020092 bool no_inherit;
Jiri Olsa7d9ad162018-08-30 08:32:14 +020093 bool identifier;
Jiri Olsafa7070a2018-08-30 08:32:29 +020094 bool csv_output;
Jiri Olsa132c6ba2018-08-30 08:32:30 +020095 bool interval_clear;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +020096 bool metric_only;
Jiri Olsaaea0dca2018-08-30 08:32:41 +020097 bool null_run;
Jiri Olsa728c0ee2018-08-30 08:32:11 +020098 FILE *output;
99 unsigned int interval;
100 unsigned int timeout;
101 unsigned int initial_delay;
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200102 unsigned int unit_width;
Jiri Olsaee1760e2018-08-30 08:32:37 +0200103 unsigned int metric_only_len;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200104 int times;
Jiri Olsad97ae042018-08-30 08:32:36 +0200105 int run_count;
Jiri Olsa31084122018-08-30 08:32:42 +0200106 int print_free_counters_hint;
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +0200107 int print_mixed_hw_group_error;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200108 struct runtime_stat *stats;
109 int stats_num;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200110 const char *csv_sep;
Jiri Olsa26893a62018-08-30 08:32:40 +0200111 struct stats *walltime_nsecs_stats;
Jiri Olsa421a50f2015-07-21 14:31:22 +0200112};
113
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800114void update_stats(struct stats *stats, u64 val);
115double avg_stats(struct stats *stats);
116double stddev_stats(struct stats *stats);
117double rel_stddev_stats(double stddev, double avg);
118
David Ahernffe4f3c2013-08-02 14:05:40 -0600119static inline void init_stats(struct stats *stats)
120{
121 stats->n = 0.0;
122 stats->mean = 0.0;
123 stats->M2 = 0.0;
124 stats->min = (u64) -1;
125 stats->max = 0;
126}
Jiri Olsae2f56da2015-06-04 15:50:55 +0200127
128struct perf_evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200129struct perf_evlist;
130
Jin Yao29734552017-12-05 22:03:11 +0800131struct perf_aggr_thread_value {
132 struct perf_evsel *counter;
133 int id;
134 double uval;
135 u64 val;
136 u64 run;
137 u64 ena;
138};
139
Jiri Olsae2f56da2015-06-04 15:50:55 +0200140bool __perf_evsel_stat__is(struct perf_evsel *evsel,
141 enum perf_stat_evsel_id id);
142
143#define perf_stat_evsel__is(evsel, id) \
144 __perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
145
Jin Yao8efb2df2017-12-05 22:03:03 +0800146extern struct runtime_stat rt_stat;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200147extern struct stats walltime_nsecs_stats;
148
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200149typedef void (*print_metric_t)(struct perf_stat_config *config,
150 void *ctx, const char *color, const char *unit,
Andi Kleen140aead2016-01-30 09:06:49 -0800151 const char *fmt, double val);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200152typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
Andi Kleen140aead2016-01-30 09:06:49 -0800153
Jin Yao8efb2df2017-12-05 22:03:03 +0800154void runtime_stat__init(struct runtime_stat *st);
155void runtime_stat__exit(struct runtime_stat *st);
Andi Kleenfb4605b2016-03-01 10:57:52 -0800156void perf_stat__init_shadow_stats(void);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200157void perf_stat__reset_shadow_stats(void);
Jin Yao6a1e2c52017-12-05 22:03:06 +0800158void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
Jiri Olsa54830dd2017-01-23 22:42:56 +0100159void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
Jin Yao1fcd0392017-12-05 22:03:04 +0800160 int cpu, struct runtime_stat *st);
Andi Kleen140aead2016-01-30 09:06:49 -0800161struct perf_stat_output_ctx {
162 void *ctx;
163 print_metric_t print_metric;
164 new_line_t new_line;
Andi Kleen37932c12017-03-20 13:17:08 -0700165 bool force_header;
Andi Kleen140aead2016-01-30 09:06:49 -0800166};
167
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200168void perf_stat__print_shadow_stats(struct perf_stat_config *config,
169 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800170 double avg, int cpu,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700171 struct perf_stat_output_ctx *out,
Jin Yaoe0128b32017-12-05 22:03:05 +0800172 struct rblist *metric_events,
173 struct runtime_stat *st);
Andi Kleen37932c12017-03-20 13:17:08 -0700174void perf_stat__collect_metric_expr(struct perf_evlist *);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200175
Jiri Olsa24e34f62015-06-26 11:29:16 +0200176int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw);
177void perf_evlist__free_stats(struct perf_evlist *evlist);
178void perf_evlist__reset_stats(struct perf_evlist *evlist);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200179
180int perf_stat_process_counter(struct perf_stat_config *config,
181 struct perf_evsel *counter);
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100182struct perf_tool;
183union perf_event;
184struct perf_session;
185int perf_event__process_stat_event(struct perf_tool *tool,
186 union perf_event *event,
187 struct perf_session *session);
Jiri Olsae08a4562015-10-25 15:51:35 +0100188
189size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
190size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
191size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
Jiri Olsad09cefd2018-08-30 08:32:17 +0200192
193int create_perf_stat_counter(struct perf_evsel *evsel,
194 struct perf_stat_config *config,
195 struct target *target);
Jiri Olsa0a4e64d2018-08-30 08:32:23 +0200196int perf_stat_synthesize_config(struct perf_stat_config *config,
197 struct perf_tool *tool,
198 struct perf_evlist *evlist,
199 perf_event__handler_t process,
200 bool attrs);
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800201#endif