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 	FILE			*output;
126 	unsigned int		 interval;
127 	unsigned int		 timeout;
128 	int			 initial_delay;
129 	unsigned int		 unit_width;
130 	unsigned int		 metric_only_len;
131 	int			 times;
132 	int			 run_count;
133 	int			 print_free_counters_hint;
134 	int			 print_mixed_hw_group_error;
135 	struct runtime_stat	*stats;
136 	int			 stats_num;
137 	const char		*csv_sep;
138 	struct stats		*walltime_nsecs_stats;
139 	struct rusage		 ru_data;
140 	struct perf_cpu_map		*aggr_map;
141 	aggr_get_id_t		 aggr_get_id;
142 	struct perf_cpu_map		*cpus_aggr_map;
143 	u64			*walltime_run;
144 	struct rblist		 metric_events;
145 	int			 ctl_fd;
146 	int			 ctl_fd_ack;
147 	bool			 ctl_fd_close;
148 	const char		*cgroup_list;
149 };
150 
151 void perf_stat__set_big_num(int set);
152 
153 void update_stats(struct stats *stats, u64 val);
154 double avg_stats(struct stats *stats);
155 double stddev_stats(struct stats *stats);
156 double rel_stddev_stats(double stddev, double avg);
157 
init_stats(struct stats * stats)158 static inline void init_stats(struct stats *stats)
159 {
160 	stats->n    = 0.0;
161 	stats->mean = 0.0;
162 	stats->M2   = 0.0;
163 	stats->min  = (u64) -1;
164 	stats->max  = 0;
165 }
166 
167 struct evsel;
168 struct evlist;
169 
170 struct perf_aggr_thread_value {
171 	struct evsel *counter;
172 	int id;
173 	double uval;
174 	u64 val;
175 	u64 run;
176 	u64 ena;
177 };
178 
179 bool __perf_evsel_stat__is(struct evsel *evsel,
180 			   enum perf_stat_evsel_id id);
181 
182 #define perf_stat_evsel__is(evsel, id) \
183 	__perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
184 
185 extern struct runtime_stat rt_stat;
186 extern struct stats walltime_nsecs_stats;
187 
188 typedef void (*print_metric_t)(struct perf_stat_config *config,
189 			       void *ctx, const char *color, const char *unit,
190 			       const char *fmt, double val);
191 typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
192 
193 void runtime_stat__init(struct runtime_stat *st);
194 void runtime_stat__exit(struct runtime_stat *st);
195 void perf_stat__init_shadow_stats(void);
196 void perf_stat__reset_shadow_stats(void);
197 void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
198 void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
199 				    int cpu, struct runtime_stat *st);
200 struct perf_stat_output_ctx {
201 	void *ctx;
202 	print_metric_t print_metric;
203 	new_line_t new_line;
204 	bool force_header;
205 };
206 
207 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
208 				   struct evsel *evsel,
209 				   double avg, int cpu,
210 				   struct perf_stat_output_ctx *out,
211 				   struct rblist *metric_events,
212 				   struct runtime_stat *st);
213 void perf_stat__collect_metric_expr(struct evlist *);
214 
215 int perf_evlist__alloc_stats(struct evlist *evlist, bool alloc_raw);
216 void perf_evlist__free_stats(struct evlist *evlist);
217 void perf_evlist__reset_stats(struct evlist *evlist);
218 void perf_evlist__reset_prev_raw_counts(struct evlist *evlist);
219 void perf_evlist__copy_prev_raw_counts(struct evlist *evlist);
220 void perf_evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
221 
222 int perf_stat_process_counter(struct perf_stat_config *config,
223 			      struct evsel *counter);
224 struct perf_tool;
225 union perf_event;
226 struct perf_session;
227 struct target;
228 
229 int perf_event__process_stat_event(struct perf_session *session,
230 				   union perf_event *event);
231 
232 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
233 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
234 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
235 
236 int create_perf_stat_counter(struct evsel *evsel,
237 			     struct perf_stat_config *config,
238 			     struct target *target,
239 			     int cpu);
240 void
241 perf_evlist__print_counters(struct evlist *evlist,
242 			    struct perf_stat_config *config,
243 			    struct target *_target,
244 			    struct timespec *ts,
245 			    int argc, const char **argv);
246 
247 struct metric_expr;
248 double test_generic_metric(struct metric_expr *mexp, int cpu, struct runtime_stat *st);
249 #endif
250