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 "cpumap.h"
10#include "rblist.h"
11#include "counts.h"
12
13struct perf_cpu_map;
14struct perf_stat_config;
15struct timespec;
16
17struct stats {
18	double n, mean, M2;
19	u64 max, min;
20};
21
22/* hold aggregated event info */
23struct perf_stat_aggr {
24	/* aggregated values */
25	struct perf_counts_values	counts;
26	/* number of entries (CPUs) aggregated */
27	int				nr;
28	/* whether any entry has failed to read/process event */
29	bool				failed;
30	/* to mark this data is processed already */
31	bool				used;
32};
33
34/* per-evsel event stats */
35struct perf_stat_evsel {
36	/* used for repeated runs */
37	struct stats		 res_stats;
38	/* number of allocated 'aggr' */
39	int			 nr_aggr;
40	/* aggregated event values */
41	struct perf_stat_aggr	*aggr;
42	/* used for group read */
43	u64			*group_data;
44};
45
46enum aggr_mode {
47	AGGR_NONE,
48	AGGR_GLOBAL,
49	AGGR_SOCKET,
50	AGGR_DIE,
51	AGGR_CLUSTER,
52	AGGR_CACHE,
53	AGGR_CORE,
54	AGGR_THREAD,
55	AGGR_UNSET,
56	AGGR_NODE,
57	AGGR_MAX
58};
59
60struct rusage_stats {
61	struct stats ru_utime_usec_stat;
62	struct stats ru_stime_usec_stat;
63};
64
65typedef struct aggr_cpu_id (*aggr_get_id_t)(struct perf_stat_config *config, struct perf_cpu cpu);
66
67struct perf_stat_config {
68	enum aggr_mode		 aggr_mode;
69	u32			 aggr_level;
70	bool			 scale;
71	bool			 no_inherit;
72	bool			 identifier;
73	bool			 csv_output;
74	bool			 json_output;
75	bool			 interval_clear;
76	bool			 metric_only;
77	bool			 null_run;
78	bool			 ru_display;
79	bool			 big_num;
80	bool			 hybrid_merge;
81	bool			 walltime_run_table;
82	bool			 all_kernel;
83	bool			 all_user;
84	bool			 percore_show_thread;
85	bool			 summary;
86	bool			 no_csv_summary;
87	bool			 metric_no_group;
88	bool			 metric_no_merge;
89	bool			 metric_no_threshold;
90	bool			 stop_read_counter;
91	bool			 iostat_run;
92	char			 *user_requested_cpu_list;
93	bool			 system_wide;
94	FILE			*output;
95	unsigned int		 interval;
96	unsigned int		 timeout;
97	unsigned int		 unit_width;
98	unsigned int		 metric_only_len;
99	int			 times;
100	int			 run_count;
101	int			 print_free_counters_hint;
102	int			 print_mixed_hw_group_error;
103	const char		*csv_sep;
104	struct stats		*walltime_nsecs_stats;
105	struct rusage		 ru_data;
106	struct rusage_stats		 *ru_stats;
107	struct cpu_aggr_map	*aggr_map;
108	aggr_get_id_t		 aggr_get_id;
109	struct cpu_aggr_map	*cpus_aggr_map;
110	u64			*walltime_run;
111	struct rblist		 metric_events;
112	int			 ctl_fd;
113	int			 ctl_fd_ack;
114	bool			 ctl_fd_close;
115	const char		*cgroup_list;
116	unsigned int		topdown_level;
117};
118
119void perf_stat__set_big_num(int set);
120void perf_stat__set_no_csv_summary(int set);
121
122void update_stats(struct stats *stats, u64 val);
123double avg_stats(struct stats *stats);
124double stddev_stats(struct stats *stats);
125double rel_stddev_stats(double stddev, double avg);
126
127static inline void init_stats(struct stats *stats)
128{
129	stats->n    = 0.0;
130	stats->mean = 0.0;
131	stats->M2   = 0.0;
132	stats->min  = (u64) -1;
133	stats->max  = 0;
134}
135
136static inline void init_rusage_stats(struct rusage_stats *ru_stats) {
137	init_stats(&ru_stats->ru_utime_usec_stat);
138	init_stats(&ru_stats->ru_stime_usec_stat);
139}
140
141static inline void update_rusage_stats(struct rusage_stats *ru_stats, struct rusage* rusage) {
142	const u64 us_to_ns = 1000;
143	const u64 s_to_ns = 1000000000;
144	update_stats(&ru_stats->ru_utime_usec_stat,
145	             (rusage->ru_utime.tv_usec * us_to_ns + rusage->ru_utime.tv_sec * s_to_ns));
146	update_stats(&ru_stats->ru_stime_usec_stat,
147	             (rusage->ru_stime.tv_usec * us_to_ns + rusage->ru_stime.tv_sec * s_to_ns));
148}
149
150struct evsel;
151struct evlist;
152
153extern struct stats walltime_nsecs_stats;
154extern struct rusage_stats ru_stats;
155
156typedef void (*print_metric_t)(struct perf_stat_config *config,
157			       void *ctx, const char *color, const char *unit,
158			       const char *fmt, double val);
159typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
160
161/* Used to print the display name of the Default metricgroup for now. */
162typedef void (*print_metricgroup_header_t)(struct perf_stat_config *config,
163					   void *ctx, const char *metricgroup_name);
164
165void perf_stat__reset_shadow_stats(void);
166struct perf_stat_output_ctx {
167	void *ctx;
168	print_metric_t print_metric;
169	new_line_t new_line;
170	print_metricgroup_header_t print_metricgroup_header;
171	bool force_header;
172};
173
174void perf_stat__print_shadow_stats(struct perf_stat_config *config,
175				   struct evsel *evsel,
176				   double avg, int aggr_idx,
177				   struct perf_stat_output_ctx *out,
178				   struct rblist *metric_events);
179bool perf_stat__skip_metric_event(struct evsel *evsel,
180				  struct rblist *metric_events,
181				  u64 ena, u64 run);
182void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
183						struct evsel *evsel,
184						int aggr_idx,
185						int *num,
186						void *from,
187						struct perf_stat_output_ctx *out,
188						struct rblist *metric_events);
189
190int evlist__alloc_stats(struct perf_stat_config *config,
191			struct evlist *evlist, bool alloc_raw);
192void evlist__free_stats(struct evlist *evlist);
193void evlist__reset_stats(struct evlist *evlist);
194void evlist__reset_prev_raw_counts(struct evlist *evlist);
195void evlist__copy_prev_raw_counts(struct evlist *evlist);
196void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
197
198int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr);
199void evlist__reset_aggr_stats(struct evlist *evlist);
200void evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist);
201
202int perf_stat_process_counter(struct perf_stat_config *config,
203			      struct evsel *counter);
204void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist);
205void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist);
206
207struct perf_tool;
208union perf_event;
209struct perf_session;
210struct target;
211
212int perf_event__process_stat_event(struct perf_session *session,
213				   union perf_event *event);
214
215size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
216size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
217size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
218
219int create_perf_stat_counter(struct evsel *evsel,
220			     struct perf_stat_config *config,
221			     struct target *target,
222			     int cpu_map_idx);
223void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
224			    struct target *_target, struct timespec *ts, int argc, const char **argv);
225
226struct metric_expr;
227double test_generic_metric(struct metric_expr *mexp, int aggr_idx);
228#endif
229