xref: /openbmc/linux/tools/perf/util/stat.h (revision dea54fba)
1 #ifndef __PERF_STATS_H
2 #define __PERF_STATS_H
3 
4 #include <linux/types.h>
5 #include <stdio.h>
6 #include "xyarray.h"
7 
8 struct stats
9 {
10 	double n, mean, M2;
11 	u64 max, min;
12 };
13 
14 enum perf_stat_evsel_id {
15 	PERF_STAT_EVSEL_ID__NONE = 0,
16 	PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
17 	PERF_STAT_EVSEL_ID__TRANSACTION_START,
18 	PERF_STAT_EVSEL_ID__ELISION_START,
19 	PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
20 	PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
21 	PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
22 	PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
23 	PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
24 	PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
25 	PERF_STAT_EVSEL_ID__SMI_NUM,
26 	PERF_STAT_EVSEL_ID__APERF,
27 	PERF_STAT_EVSEL_ID__MAX,
28 };
29 
30 struct perf_stat_evsel {
31 	struct stats		res_stats[3];
32 	enum perf_stat_evsel_id	id;
33 };
34 
35 enum aggr_mode {
36 	AGGR_NONE,
37 	AGGR_GLOBAL,
38 	AGGR_SOCKET,
39 	AGGR_CORE,
40 	AGGR_THREAD,
41 	AGGR_UNSET,
42 };
43 
44 struct perf_stat_config {
45 	enum aggr_mode	aggr_mode;
46 	bool		scale;
47 	FILE		*output;
48 	unsigned int	interval;
49 };
50 
51 void update_stats(struct stats *stats, u64 val);
52 double avg_stats(struct stats *stats);
53 double stddev_stats(struct stats *stats);
54 double rel_stddev_stats(double stddev, double avg);
55 
56 static inline void init_stats(struct stats *stats)
57 {
58 	stats->n    = 0.0;
59 	stats->mean = 0.0;
60 	stats->M2   = 0.0;
61 	stats->min  = (u64) -1;
62 	stats->max  = 0;
63 }
64 
65 struct perf_evsel;
66 struct perf_evlist;
67 
68 bool __perf_evsel_stat__is(struct perf_evsel *evsel,
69 			   enum perf_stat_evsel_id id);
70 
71 #define perf_stat_evsel__is(evsel, id) \
72 	__perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
73 
74 void perf_stat_evsel_id_init(struct perf_evsel *evsel);
75 
76 extern struct stats walltime_nsecs_stats;
77 
78 typedef void (*print_metric_t)(void *ctx, const char *color, const char *unit,
79 			       const char *fmt, double val);
80 typedef void (*new_line_t )(void *ctx);
81 
82 void perf_stat__init_shadow_stats(void);
83 void perf_stat__reset_shadow_stats(void);
84 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,
85 				    int cpu);
86 struct perf_stat_output_ctx {
87 	void *ctx;
88 	print_metric_t print_metric;
89 	new_line_t new_line;
90 	bool force_header;
91 };
92 
93 void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
94 				   double avg, int cpu,
95 				   struct perf_stat_output_ctx *out);
96 void perf_stat__collect_metric_expr(struct perf_evlist *);
97 
98 int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw);
99 void perf_evlist__free_stats(struct perf_evlist *evlist);
100 void perf_evlist__reset_stats(struct perf_evlist *evlist);
101 
102 int perf_stat_process_counter(struct perf_stat_config *config,
103 			      struct perf_evsel *counter);
104 struct perf_tool;
105 union perf_event;
106 struct perf_session;
107 int perf_event__process_stat_event(struct perf_tool *tool,
108 				   union perf_event *event,
109 				   struct perf_session *session);
110 
111 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
112 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
113 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
114 #endif
115