xref: /linux/tools/perf/util/stat.h (revision 473f6c8f437b049f8ec015d57cd59bb983b1d85c)
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			 hide_zero;
73 	bool			 ru_display;
74 	bool			 big_num;
75 	bool			 hybrid_merge;
76 	bool			 walltime_run_table;
77 	bool			 all_kernel;
78 	bool			 all_user;
79 	bool			 percore_show_thread;
80 	bool			 summary;
81 	bool			 no_csv_summary;
82 	bool			 metric_no_group;
83 	bool			 metric_no_merge;
84 	bool			 metric_no_threshold;
85 	bool			 hardware_aware_grouping;
86 	bool			 stop_read_counter;
87 	bool			 iostat_run;
88 	char			 *user_requested_cpu_list;
89 	bool			 system_wide;
90 	FILE			*output;
91 	unsigned int		 interval;
92 	unsigned int		 timeout;
93 	unsigned int		 unit_width;
94 	unsigned int		 metric_only_len;
95 	int			 times;
96 	int			 run_count;
97 	int			 print_free_counters_hint;
98 	const char		*csv_sep;
99 	struct stats		*walltime_nsecs_stats;
100 	struct rusage		 ru_data;
101 	struct cpu_aggr_map	*aggr_map;
102 	aggr_get_id_t		 aggr_get_id;
103 	struct cpu_aggr_map	*cpus_aggr_map;
104 	u64			*walltime_run;
105 	int			 ctl_fd;
106 	int			 ctl_fd_ack;
107 	bool			 ctl_fd_close;
108 	const char		*cgroup_list;
109 	unsigned int		topdown_level;
110 };
111 
112 extern struct perf_stat_config stat_config;
113 
114 void perf_stat__set_big_num(int set);
115 
116 void update_stats(struct stats *stats, u64 val);
117 double avg_stats(struct stats *stats);
118 double stddev_stats(struct stats *stats);
119 double rel_stddev_stats(double stddev, double avg);
120 
121 static inline void init_stats(struct stats *stats)
122 {
123 	stats->n    = 0.0;
124 	stats->mean = 0.0;
125 	stats->M2   = 0.0;
126 	stats->min  = (u64) -1;
127 	stats->max  = 0;
128 }
129 
130 struct evsel;
131 struct evlist;
132 
133 enum metric_threshold_classify {
134 	METRIC_THRESHOLD_UNKNOWN,
135 	METRIC_THRESHOLD_BAD,
136 	METRIC_THRESHOLD_NEARLY_BAD,
137 	METRIC_THRESHOLD_LESS_GOOD,
138 	METRIC_THRESHOLD_GOOD,
139 };
140 const char *metric_threshold_classify__color(enum metric_threshold_classify thresh);
141 
142 typedef void (*print_metric_t)(struct perf_stat_config *config,
143 			       void *ctx,
144 			       enum metric_threshold_classify thresh,
145 			       const char *fmt,
146 			       const char *unit,
147 			       double val);
148 typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
149 
150 /* Used to print the display name of the Default metricgroup for now. */
151 typedef void (*print_metricgroup_header_t)(struct perf_stat_config *config,
152 					   void *ctx, const char *metricgroup_name);
153 
154 void perf_stat__reset_shadow_stats(void);
155 struct perf_stat_output_ctx {
156 	void *ctx;
157 	print_metric_t print_metric;
158 	new_line_t new_line;
159 	print_metricgroup_header_t print_metricgroup_header;
160 	bool force_header;
161 };
162 
163 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
164 				   struct evsel *evsel,
165 				   int aggr_idx,
166 				   struct perf_stat_output_ctx *out);
167 bool perf_stat__skip_metric_event(struct evsel *evsel);
168 void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
169 						struct evsel *evsel,
170 						int aggr_idx,
171 						int *num,
172 						void *from,
173 						struct perf_stat_output_ctx *out);
174 
175 int evlist__alloc_stats(struct perf_stat_config *config,
176 			struct evlist *evlist, bool alloc_raw);
177 void evlist__free_stats(struct evlist *evlist);
178 void evlist__reset_stats(struct evlist *evlist);
179 void evlist__reset_prev_raw_counts(struct evlist *evlist);
180 void evlist__copy_prev_raw_counts(struct evlist *evlist);
181 void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
182 
183 int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr);
184 void evlist__reset_aggr_stats(struct evlist *evlist);
185 void evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist);
186 
187 int perf_stat_process_counter(struct perf_stat_config *config,
188 			      struct evsel *counter);
189 void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist);
190 void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist);
191 
192 struct perf_tool;
193 union perf_event;
194 struct perf_session;
195 struct target;
196 
197 int perf_event__process_stat_event(const struct perf_tool *tool,
198 				   struct perf_session *session,
199 				   union perf_event *event);
200 
201 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
202 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
203 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
204 
205 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
206 			    struct target *_target, struct timespec *ts, int argc, const char **argv);
207 
208 struct metric_expr;
209 double test_generic_metric(struct metric_expr *mexp, int aggr_idx);
210 #endif
211