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