xref: /linux/tools/perf/util/kvm-stat.h (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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 perf_sample *sample,
57 			struct event_key *key);
58 	const char *name;
59 };
60 
61 struct kvm_events_ops {
62 	bool (*is_begin_event)(struct perf_sample *sample,
63 			       struct event_key *key);
64 	bool (*is_end_event)(struct perf_sample *sample, struct event_key *key);
65 	const struct child_event_ops *child_ops;
66 	void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
67 			   char *decode);
68 	const char *name;
69 };
70 
71 struct exit_reasons_table {
72 	u64 exit_code;
73 	const char *reason;
74 };
75 
76 struct perf_kvm_stat {
77 	struct perf_tool    tool;
78 	struct record_opts  opts;
79 	struct evlist  *evlist;
80 	struct perf_session *session;
81 
82 	const char *file_name;
83 	const char *report_event;
84 	const char *sort_key;
85 	int trace_vcpu;
86 
87 	/* Used when process events */
88 	struct addr_location al;
89 
90 	struct exit_reasons_table *exit_reasons;
91 	const char *exit_reasons_isa;
92 
93 	const struct kvm_events_ops *events_ops;
94 
95 	u64 total_time;
96 	u64 total_count;
97 	u64 lost_events;
98 	u64 duration;
99 
100 	struct intlist *pid_list;
101 
102 	int timerfd;
103 	unsigned int display_time;
104 	bool live;
105 	bool force;
106 	bool use_stdio;
107 };
108 
109 struct kvm_reg_events_ops {
110 	const char *name;
111 	const struct kvm_events_ops *ops;
112 };
113 
114 #ifdef HAVE_LIBTRACEEVENT
115 
116 void exit_event_get_key(struct perf_sample *sample,
117 			struct event_key *key);
118 bool exit_event_begin(struct perf_sample *sample,
119 		      struct event_key *key);
120 bool exit_event_end(struct perf_sample *sample,
121 		    struct event_key *key);
122 void exit_event_decode_key(struct perf_kvm_stat *kvm,
123 			   struct event_key *key,
124 			   char *decode);
125 
126 bool kvm_exit_event(struct evsel *evsel);
127 bool kvm_entry_event(struct evsel *evsel);
128 
129 #define define_exit_reasons_table(name, symbols)	\
130 	static struct exit_reasons_table name[] = {	\
131 		symbols, { -1, NULL }			\
132 	}
133 
134 /*
135  * arch specific callbacks and data structures
136  */
137 int setup_kvm_events_tp(struct perf_kvm_stat *kvm, uint16_t e_machine);
138 int __setup_kvm_events_tp_powerpc(struct perf_kvm_stat *kvm);
139 
140 int cpu_isa_init(struct perf_kvm_stat *kvm, uint16_t e_machine, const char *cpuid);
141 int __cpu_isa_init_arm64(struct perf_kvm_stat *kvm);
142 int __cpu_isa_init_loongarch(struct perf_kvm_stat *kvm);
143 int __cpu_isa_init_powerpc(struct perf_kvm_stat *kvm);
144 int __cpu_isa_init_riscv(struct perf_kvm_stat *kvm);
145 int __cpu_isa_init_s390(struct perf_kvm_stat *kvm, const char *cpuid);
146 int __cpu_isa_init_x86(struct perf_kvm_stat *kvm, const char *cpuid);
147 
148 const char *vcpu_id_str(uint16_t e_machine);
149 const char *kvm_exit_reason(uint16_t e_machine);
150 const char *kvm_entry_trace(uint16_t e_machine);
151 const char *kvm_exit_trace(uint16_t e_machine);
152 
153 const char * const *kvm_events_tp(uint16_t e_machine);
154 const char * const *__kvm_events_tp_arm64(void);
155 const char * const *__kvm_events_tp_loongarch(void);
156 const char * const *__kvm_events_tp_powerpc(void);
157 const char * const *__kvm_events_tp_riscv(void);
158 const char * const *__kvm_events_tp_s390(void);
159 const char * const *__kvm_events_tp_x86(void);
160 
161 const struct kvm_reg_events_ops *kvm_reg_events_ops(uint16_t e_machine);
162 const struct kvm_reg_events_ops *__kvm_reg_events_ops_arm64(void);
163 const struct kvm_reg_events_ops *__kvm_reg_events_ops_loongarch(void);
164 const struct kvm_reg_events_ops *__kvm_reg_events_ops_powerpc(void);
165 const struct kvm_reg_events_ops *__kvm_reg_events_ops_riscv(void);
166 const struct kvm_reg_events_ops *__kvm_reg_events_ops_s390(void);
167 const struct kvm_reg_events_ops *__kvm_reg_events_ops_x86(void);
168 
169 const char * const *kvm_skip_events(uint16_t e_machine);
170 const char * const *__kvm_skip_events_arm64(void);
171 const char * const *__kvm_skip_events_loongarch(void);
172 const char * const *__kvm_skip_events_powerpc(void);
173 const char * const *__kvm_skip_events_riscv(void);
174 const char * const *__kvm_skip_events_s390(void);
175 const char * const *__kvm_skip_events_x86(void);
176 
177 bool kvm_need_default_arch_event(uint16_t e_machine, int argc, const char **argv);
178 int kvm_add_default_arch_event(uint16_t e_machine, int *argc, const char **argv);
179 int __kvm_add_default_arch_event_powerpc(int *argc, const char **argv);
180 int __kvm_add_default_arch_event_x86(int *argc, const char **argv);
181 
182 #else /* !HAVE_LIBTRACEEVENT */
183 
184 static inline bool kvm_need_default_arch_event(uint16_t e_machine __maybe_unused,
185 					       int argc __maybe_unused,
186 					       const char **argv __maybe_unused)
187 {
188 	return false;
189 }
190 
191 static inline int kvm_add_default_arch_event(uint16_t e_machine __maybe_unused,
192 					     int *argc __maybe_unused,
193 					     const char **argv __maybe_unused)
194 {
195 	return 0;
196 }
197 
198 #endif /* HAVE_LIBTRACEEVENT */
199 
200 static inline struct kvm_info *kvm_info__get(struct kvm_info *ki)
201 {
202 	if (ki)
203 		refcount_inc(&ki->refcnt);
204 	return ki;
205 }
206 
207 static inline void kvm_info__put(struct kvm_info *ki)
208 {
209 	if (ki && refcount_dec_and_test(&ki->refcnt))
210 		free(ki);
211 }
212 
213 static inline void __kvm_info__zput(struct kvm_info **ki)
214 {
215 	kvm_info__put(*ki);
216 	*ki = NULL;
217 }
218 
219 #define kvm_info__zput(ki) __kvm_info__zput(&ki)
220 
221 static inline struct kvm_info *kvm_info__new(void)
222 {
223 	struct kvm_info *ki;
224 
225 	ki = zalloc(sizeof(*ki));
226 	if (ki)
227 		refcount_set(&ki->refcnt, 1);
228 
229 	return ki;
230 }
231 
232 #endif /* __PERF_KVM_STAT_H */
233