xref: /linux/tools/perf/util/stat.h (revision f4bd0b4a9b21c609ede28cee2dcd16824c0489a8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_STATS_H
3 #define __PERF_STATS_H
4 
5 #include <linux/types.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/resource.h>
9 #include "rblist.h"
10 
11 struct perf_cpu_map;
12 struct perf_stat_config;
13 struct timespec;
14 
15 struct stats {
16 	double n, mean, M2;
17 	u64 max, min;
18 };
19 
20 enum perf_stat_evsel_id {
21 	PERF_STAT_EVSEL_ID__NONE = 0,
22 	PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
23 	PERF_STAT_EVSEL_ID__TRANSACTION_START,
24 	PERF_STAT_EVSEL_ID__ELISION_START,
25 	PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
26 	PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
27 	PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
28 	PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
29 	PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
30 	PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
31 	PERF_STAT_EVSEL_ID__TOPDOWN_RETIRING,
32 	PERF_STAT_EVSEL_ID__TOPDOWN_BAD_SPEC,
33 	PERF_STAT_EVSEL_ID__TOPDOWN_FE_BOUND,
34 	PERF_STAT_EVSEL_ID__TOPDOWN_BE_BOUND,
35 	PERF_STAT_EVSEL_ID__SMI_NUM,
36 	PERF_STAT_EVSEL_ID__APERF,
37 	PERF_STAT_EVSEL_ID__MAX,
38 };
39 
40 struct perf_stat_evsel {
41 	struct stats		 res_stats[3];
42 	enum perf_stat_evsel_id	 id;
43 	u64			*group_data;
44 };
45 
46 enum aggr_mode {
47 	AGGR_NONE,
48 	AGGR_GLOBAL,
49 	AGGR_SOCKET,
50 	AGGR_DIE,
51 	AGGR_CORE,
52 	AGGR_THREAD,
53 	AGGR_UNSET,
54 	AGGR_NODE,
55 };
56 
57 enum {
58 	CTX_BIT_USER	= 1 << 0,
59 	CTX_BIT_KERNEL	= 1 << 1,
60 	CTX_BIT_HV	= 1 << 2,
61 	CTX_BIT_HOST	= 1 << 3,
62 	CTX_BIT_IDLE	= 1 << 4,
63 	CTX_BIT_MAX	= 1 << 5,
64 };
65 
66 #define NUM_CTX CTX_BIT_MAX
67 
68 enum stat_type {
69 	STAT_NONE = 0,
70 	STAT_NSECS,
71 	STAT_CYCLES,
72 	STAT_STALLED_CYCLES_FRONT,
73 	STAT_STALLED_CYCLES_BACK,
74 	STAT_BRANCHES,
75 	STAT_CACHEREFS,
76 	STAT_L1_DCACHE,
77 	STAT_L1_ICACHE,
78 	STAT_LL_CACHE,
79 	STAT_ITLB_CACHE,
80 	STAT_DTLB_CACHE,
81 	STAT_CYCLES_IN_TX,
82 	STAT_TRANSACTION,
83 	STAT_ELISION,
84 	STAT_TOPDOWN_TOTAL_SLOTS,
85 	STAT_TOPDOWN_SLOTS_ISSUED,
86 	STAT_TOPDOWN_SLOTS_RETIRED,
87 	STAT_TOPDOWN_FETCH_BUBBLES,
88 	STAT_TOPDOWN_RECOVERY_BUBBLES,
89 	STAT_TOPDOWN_RETIRING,
90 	STAT_TOPDOWN_BAD_SPEC,
91 	STAT_TOPDOWN_FE_BOUND,
92 	STAT_TOPDOWN_BE_BOUND,
93 	STAT_SMI_NUM,
94 	STAT_APERF,
95 	STAT_MAX
96 };
97 
98 struct runtime_stat {
99 	struct rblist value_list;
100 };
101 
102 typedef int (*aggr_get_id_t)(struct perf_stat_config *config,
103 			     struct perf_cpu_map *m, int cpu);
104 
105 struct perf_stat_config {
106 	enum aggr_mode		 aggr_mode;
107 	bool			 scale;
108 	bool			 no_inherit;
109 	bool			 identifier;
110 	bool			 csv_output;
111 	bool			 interval_clear;
112 	bool			 metric_only;
113 	bool			 null_run;
114 	bool			 ru_display;
115 	bool			 big_num;
116 	bool			 no_merge;
117 	bool			 walltime_run_table;
118 	bool			 all_kernel;
119 	bool			 all_user;
120 	bool			 percore_show_thread;
121 	bool			 summary;
122 	bool			 metric_no_group;
123 	bool			 metric_no_merge;
124 	bool			 stop_read_counter;
125 	bool			 quiet;
126 	FILE			*output;
127 	unsigned int		 interval;
128 	unsigned int		 timeout;
129 	int			 initial_delay;
130 	unsigned int		 unit_width;
131 	unsigned int		 metric_only_len;
132 	int			 times;
133 	int			 run_count;
134 	int			 print_free_counters_hint;
135 	int			 print_mixed_hw_group_error;
136 	struct runtime_stat	*stats;
137 	int			 stats_num;
138 	const char		*csv_sep;
139 	struct stats		*walltime_nsecs_stats;
140 	struct rusage		 ru_data;
141 	struct perf_cpu_map		*aggr_map;
142 	aggr_get_id_t		 aggr_get_id;
143 	struct perf_cpu_map		*cpus_aggr_map;
144 	u64			*walltime_run;
145 	struct rblist		 metric_events;
146 	int			 ctl_fd;
147 	int			 ctl_fd_ack;
148 	bool			 ctl_fd_close;
149 	const char		*cgroup_list;
150 };
151 
152 void perf_stat__set_big_num(int set);
153 
154 void update_stats(struct stats *stats, u64 val);
155 double avg_stats(struct stats *stats);
156 double stddev_stats(struct stats *stats);
157 double rel_stddev_stats(double stddev, double avg);
158 
159 static inline void init_stats(struct stats *stats)
160 {
161 	stats->n    = 0.0;
162 	stats->mean = 0.0;
163 	stats->M2   = 0.0;
164 	stats->min  = (u64) -1;
165 	stats->max  = 0;
166 }
167 
168 struct evsel;
169 struct evlist;
170 
171 struct perf_aggr_thread_value {
172 	struct evsel *counter;
173 	int id;
174 	double uval;
175 	u64 val;
176 	u64 run;
177 	u64 ena;
178 };
179 
180 bool __perf_evsel_stat__is(struct evsel *evsel,
181 			   enum perf_stat_evsel_id id);
182 
183 #define perf_stat_evsel__is(evsel, id) \
184 	__perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
185 
186 extern struct runtime_stat rt_stat;
187 extern struct stats walltime_nsecs_stats;
188 
189 typedef void (*print_metric_t)(struct perf_stat_config *config,
190 			       void *ctx, const char *color, const char *unit,
191 			       const char *fmt, double val);
192 typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
193 
194 void runtime_stat__init(struct runtime_stat *st);
195 void runtime_stat__exit(struct runtime_stat *st);
196 void perf_stat__init_shadow_stats(void);
197 void perf_stat__reset_shadow_stats(void);
198 void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
199 void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
200 				    int cpu, struct runtime_stat *st);
201 struct perf_stat_output_ctx {
202 	void *ctx;
203 	print_metric_t print_metric;
204 	new_line_t new_line;
205 	bool force_header;
206 };
207 
208 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
209 				   struct evsel *evsel,
210 				   double avg, int cpu,
211 				   struct perf_stat_output_ctx *out,
212 				   struct rblist *metric_events,
213 				   struct runtime_stat *st);
214 void perf_stat__collect_metric_expr(struct evlist *);
215 
216 int evlist__alloc_stats(struct evlist *evlist, bool alloc_raw);
217 void evlist__free_stats(struct evlist *evlist);
218 void evlist__reset_stats(struct evlist *evlist);
219 void evlist__reset_prev_raw_counts(struct evlist *evlist);
220 void evlist__copy_prev_raw_counts(struct evlist *evlist);
221 void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
222 
223 int perf_stat_process_counter(struct perf_stat_config *config,
224 			      struct evsel *counter);
225 struct perf_tool;
226 union perf_event;
227 struct perf_session;
228 struct target;
229 
230 int perf_event__process_stat_event(struct perf_session *session,
231 				   union perf_event *event);
232 
233 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
234 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
235 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
236 
237 int create_perf_stat_counter(struct evsel *evsel,
238 			     struct perf_stat_config *config,
239 			     struct target *target,
240 			     int cpu);
241 void
242 perf_evlist__print_counters(struct evlist *evlist,
243 			    struct perf_stat_config *config,
244 			    struct target *_target,
245 			    struct timespec *ts,
246 			    int argc, const char **argv);
247 
248 struct metric_expr;
249 double test_generic_metric(struct metric_expr *mexp, int cpu, struct runtime_stat *st);
250 #endif
251