xref: /linux/tools/perf/util/kvm-stat.h (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_KVM_STAT_H
3 #define __PERF_KVM_STAT_H
4 
5 #include "tool.h"
6 #include "sort.h"
7 #include "stat.h"
8 #include "symbol.h"
9 #include "record.h"
10 
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <linux/zalloc.h>
14 
15 #define KVM_EVENT_NAME_LEN	40
16 
17 struct evsel;
18 struct evlist;
19 struct perf_session;
20 
21 struct event_key {
22 	#define INVALID_KEY     (~0ULL)
23 	u64 key;
24 	int info;
25 	struct exit_reasons_table *exit_reasons;
26 };
27 
28 struct kvm_info {
29 	char name[KVM_EVENT_NAME_LEN];
30 	refcount_t refcnt;
31 };
32 
33 struct kvm_event_stats {
34 	u64 time;
35 	struct stats stats;
36 };
37 
38 struct perf_kvm_stat;
39 
40 struct kvm_event {
41 	struct list_head hash_entry;
42 
43 	struct perf_kvm_stat *perf_kvm;
44 	struct event_key key;
45 
46 	struct kvm_event_stats total;
47 
48 	#define DEFAULT_VCPU_NUM 8
49 	int max_vcpu;
50 	struct kvm_event_stats *vcpu;
51 
52 	struct hist_entry he;
53 };
54 
55 struct child_event_ops {
56 	void (*get_key)(struct evsel *evsel,
57 			struct perf_sample *sample,
58 			struct event_key *key);
59 	const char *name;
60 };
61 
62 struct kvm_events_ops {
63 	bool (*is_begin_event)(struct evsel *evsel,
64 			       struct perf_sample *sample,
65 			       struct event_key *key);
66 	bool (*is_end_event)(struct evsel *evsel,
67 			     struct perf_sample *sample, struct event_key *key);
68 	const struct child_event_ops *child_ops;
69 	void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
70 			   char *decode);
71 	const char *name;
72 };
73 
74 struct exit_reasons_table {
75 	unsigned long exit_code;
76 	const char *reason;
77 };
78 
79 struct perf_kvm_stat {
80 	struct perf_tool    tool;
81 	struct record_opts  opts;
82 	struct evlist  *evlist;
83 	struct perf_session *session;
84 
85 	const char *file_name;
86 	const char *report_event;
87 	const char *sort_key;
88 	int trace_vcpu;
89 
90 	/* Used when process events */
91 	struct addr_location al;
92 
93 	struct exit_reasons_table *exit_reasons;
94 	const char *exit_reasons_isa;
95 
96 	const struct kvm_events_ops *events_ops;
97 
98 	u64 total_time;
99 	u64 total_count;
100 	u64 lost_events;
101 	u64 duration;
102 
103 	struct intlist *pid_list;
104 
105 	int timerfd;
106 	unsigned int display_time;
107 	bool live;
108 	bool force;
109 	bool use_stdio;
110 };
111 
112 struct kvm_reg_events_ops {
113 	const char *name;
114 	const struct kvm_events_ops *ops;
115 };
116 
117 #ifdef HAVE_LIBTRACEEVENT
118 
119 void exit_event_get_key(struct evsel *evsel,
120 			struct perf_sample *sample,
121 			struct event_key *key);
122 bool exit_event_begin(struct evsel *evsel,
123 		      struct perf_sample *sample,
124 		      struct event_key *key);
125 bool exit_event_end(struct evsel *evsel,
126 		    struct perf_sample *sample,
127 		    struct event_key *key);
128 void exit_event_decode_key(struct perf_kvm_stat *kvm,
129 			   struct event_key *key,
130 			   char *decode);
131 
132 bool kvm_exit_event(struct evsel *evsel);
133 bool kvm_entry_event(struct evsel *evsel);
134 
135 #define define_exit_reasons_table(name, symbols)	\
136 	static struct exit_reasons_table name[] = {	\
137 		symbols, { -1, NULL }			\
138 	}
139 
140 /*
141  * arch specific callbacks and data structures
142  */
143 int setup_kvm_events_tp(struct perf_kvm_stat *kvm, uint16_t e_machine);
144 int __setup_kvm_events_tp_powerpc(struct perf_kvm_stat *kvm);
145 
146 int cpu_isa_init(struct perf_kvm_stat *kvm, uint16_t e_machine, const char *cpuid);
147 int __cpu_isa_init_arm64(struct perf_kvm_stat *kvm);
148 int __cpu_isa_init_loongarch(struct perf_kvm_stat *kvm);
149 int __cpu_isa_init_powerpc(struct perf_kvm_stat *kvm);
150 int __cpu_isa_init_riscv(struct perf_kvm_stat *kvm);
151 int __cpu_isa_init_s390(struct perf_kvm_stat *kvm, const char *cpuid);
152 int __cpu_isa_init_x86(struct perf_kvm_stat *kvm, const char *cpuid);
153 
154 const char *vcpu_id_str(uint16_t e_machine);
155 const char *kvm_exit_reason(uint16_t e_machine);
156 const char *kvm_entry_trace(uint16_t e_machine);
157 const char *kvm_exit_trace(uint16_t e_machine);
158 
159 const char * const *kvm_events_tp(uint16_t e_machine);
160 const char * const *__kvm_events_tp_arm64(void);
161 const char * const *__kvm_events_tp_loongarch(void);
162 const char * const *__kvm_events_tp_powerpc(void);
163 const char * const *__kvm_events_tp_riscv(void);
164 const char * const *__kvm_events_tp_s390(void);
165 const char * const *__kvm_events_tp_x86(void);
166 
167 const struct kvm_reg_events_ops *kvm_reg_events_ops(uint16_t e_machine);
168 const struct kvm_reg_events_ops *__kvm_reg_events_ops_arm64(void);
169 const struct kvm_reg_events_ops *__kvm_reg_events_ops_loongarch(void);
170 const struct kvm_reg_events_ops *__kvm_reg_events_ops_powerpc(void);
171 const struct kvm_reg_events_ops *__kvm_reg_events_ops_riscv(void);
172 const struct kvm_reg_events_ops *__kvm_reg_events_ops_s390(void);
173 const struct kvm_reg_events_ops *__kvm_reg_events_ops_x86(void);
174 
175 const char * const *kvm_skip_events(uint16_t e_machine);
176 const char * const *__kvm_skip_events_arm64(void);
177 const char * const *__kvm_skip_events_loongarch(void);
178 const char * const *__kvm_skip_events_powerpc(void);
179 const char * const *__kvm_skip_events_riscv(void);
180 const char * const *__kvm_skip_events_s390(void);
181 const char * const *__kvm_skip_events_x86(void);
182 
183 int kvm_add_default_arch_event(uint16_t e_machine, int *argc, const char **argv);
184 int __kvm_add_default_arch_event_powerpc(int *argc, const char **argv);
185 int __kvm_add_default_arch_event_x86(int *argc, const char **argv);
186 
187 #else /* !HAVE_LIBTRACEEVENT */
188 
kvm_add_default_arch_event(uint16_t e_machine __maybe_unused,int * argc __maybe_unused,const char ** argv __maybe_unused)189 static inline int kvm_add_default_arch_event(uint16_t e_machine __maybe_unused,
190 					     int *argc __maybe_unused,
191 					     const char **argv __maybe_unused)
192 {
193 	return 0;
194 }
195 
196 #endif /* HAVE_LIBTRACEEVENT */
197 
kvm_info__get(struct kvm_info * ki)198 static inline struct kvm_info *kvm_info__get(struct kvm_info *ki)
199 {
200 	if (ki)
201 		refcount_inc(&ki->refcnt);
202 	return ki;
203 }
204 
kvm_info__put(struct kvm_info * ki)205 static inline void kvm_info__put(struct kvm_info *ki)
206 {
207 	if (ki && refcount_dec_and_test(&ki->refcnt))
208 		free(ki);
209 }
210 
__kvm_info__zput(struct kvm_info ** ki)211 static inline void __kvm_info__zput(struct kvm_info **ki)
212 {
213 	kvm_info__put(*ki);
214 	*ki = NULL;
215 }
216 
217 #define kvm_info__zput(ki) __kvm_info__zput(&ki)
218 
kvm_info__new(void)219 static inline struct kvm_info *kvm_info__new(void)
220 {
221 	struct kvm_info *ki;
222 
223 	ki = zalloc(sizeof(*ki));
224 	if (ki)
225 		refcount_set(&ki->refcnt, 1);
226 
227 	return ki;
228 }
229 
230 #define STRDUP_FAIL_EXIT(s)		\
231 	({	char *_p;		\
232 		_p = strdup(s);		\
233 		if (!_p) {		\
234 			ret = -ENOMEM;	\
235 			goto EXIT;	\
236 		}			\
237 		_p;			\
238 	})
239 
240 #endif /* __PERF_KVM_STAT_H */
241