Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / arch / powerpc / util / kvm-stat.c
blobeed9e5a429358789f3c9414b3b3a655e94d4ec48
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include "util/kvm-stat.h"
4 #include "util/parse-events.h"
5 #include "util/debug.h"
6 #include "util/evsel.h"
7 #include "util/evlist.h"
8 #include "util/pmu.h"
10 #include "book3s_hv_exits.h"
11 #include "book3s_hcalls.h"
12 #include <subcmd/parse-options.h>
14 #define NR_TPS 4
16 const char *vcpu_id_str = "vcpu_id";
17 const int decode_str_len = 40;
18 const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
19 const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
21 define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
22 define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
24 /* Tracepoints specific to ppc_book3s_hv */
25 const char *ppc_book3s_hv_kvm_tp[] = {
26 "kvm_hv:kvm_guest_enter",
27 "kvm_hv:kvm_guest_exit",
28 "kvm_hv:kvm_hcall_enter",
29 "kvm_hv:kvm_hcall_exit",
30 NULL,
33 /* 1 extra placeholder for NULL */
34 const char *kvm_events_tp[NR_TPS + 1];
35 const char *kvm_exit_reason;
37 static void hcall_event_get_key(struct evsel *evsel,
38 struct perf_sample *sample,
39 struct event_key *key)
41 key->info = 0;
42 key->key = evsel__intval(evsel, sample, "req");
45 static const char *get_hcall_exit_reason(u64 exit_code)
47 struct exit_reasons_table *tbl = hcall_reasons;
49 while (tbl->reason != NULL) {
50 if (tbl->exit_code == exit_code)
51 return tbl->reason;
52 tbl++;
55 pr_debug("Unknown hcall code: %lld\n",
56 (unsigned long long)exit_code);
57 return "UNKNOWN";
60 static bool hcall_event_end(struct evsel *evsel,
61 struct perf_sample *sample __maybe_unused,
62 struct event_key *key __maybe_unused)
64 return (!strcmp(evsel->name, kvm_events_tp[3]));
67 static bool hcall_event_begin(struct evsel *evsel,
68 struct perf_sample *sample, struct event_key *key)
70 if (!strcmp(evsel->name, kvm_events_tp[2])) {
71 hcall_event_get_key(evsel, sample, key);
72 return true;
75 return false;
77 static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
78 struct event_key *key,
79 char *decode)
81 const char *hcall_reason = get_hcall_exit_reason(key->key);
83 scnprintf(decode, decode_str_len, "%s", hcall_reason);
86 static struct kvm_events_ops hcall_events = {
87 .is_begin_event = hcall_event_begin,
88 .is_end_event = hcall_event_end,
89 .decode_key = hcall_event_decode_key,
90 .name = "HCALL-EVENT",
93 static struct kvm_events_ops exit_events = {
94 .is_begin_event = exit_event_begin,
95 .is_end_event = exit_event_end,
96 .decode_key = exit_event_decode_key,
97 .name = "VM-EXIT"
100 struct kvm_reg_events_ops kvm_reg_events_ops[] = {
101 { .name = "vmexit", .ops = &exit_events },
102 { .name = "hcall", .ops = &hcall_events },
103 { NULL, NULL },
106 const char * const kvm_skip_events[] = {
107 NULL,
111 static int is_tracepoint_available(const char *str, struct evlist *evlist)
113 struct parse_events_error err;
114 int ret;
116 bzero(&err, sizeof(err));
117 ret = parse_events(evlist, str, &err);
118 if (err.str)
119 parse_events_print_error(&err, "tracepoint");
120 return ret;
123 static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
124 struct evlist *evlist)
126 const char **events_ptr;
127 int i, nr_tp = 0, err = -1;
129 /* Check for book3s_hv tracepoints */
130 for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
131 err = is_tracepoint_available(*events_ptr, evlist);
132 if (err)
133 return -1;
134 nr_tp++;
137 for (i = 0; i < nr_tp; i++)
138 kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];
140 kvm_events_tp[i] = NULL;
141 kvm_exit_reason = "trap";
142 kvm->exit_reasons = hv_exit_reasons;
143 kvm->exit_reasons_isa = "HV";
145 return 0;
148 /* Wrapper to setup kvm tracepoints */
149 static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
151 struct evlist *evlist = evlist__new();
153 if (evlist == NULL)
154 return -ENOMEM;
156 /* Right now, only supported on book3s_hv */
157 return ppc__setup_book3s_hv(kvm, evlist);
160 int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
162 return ppc__setup_kvm_tp(kvm);
165 int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
167 int ret;
169 ret = ppc__setup_kvm_tp(kvm);
170 if (ret) {
171 kvm->exit_reasons = NULL;
172 kvm->exit_reasons_isa = NULL;
175 return ret;
179 * Incase of powerpc architecture, pmu registers are programmable
180 * by guest kernel. So monitoring guest via host may not provide
181 * valid samples with default 'cycles' event. It is better to use
182 * 'trace_imc/trace_cycles' event for guest profiling, since it
183 * can track the guest instruction pointer in the trace-record.
185 * Function to parse the arguments and return appropriate values.
187 int kvm_add_default_arch_event(int *argc, const char **argv)
189 const char **tmp;
190 bool event = false;
191 int i, j = *argc;
193 const struct option event_options[] = {
194 OPT_BOOLEAN('e', "event", &event, NULL),
195 OPT_END()
198 tmp = calloc(j + 1, sizeof(char *));
199 if (!tmp)
200 return -EINVAL;
202 for (i = 0; i < j; i++)
203 tmp[i] = argv[i];
205 parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
206 if (!event) {
207 if (pmu_have_event("trace_imc", "trace_cycles")) {
208 argv[j++] = strdup("-e");
209 argv[j++] = strdup("trace_imc/trace_cycles/");
210 *argc += 2;
211 } else {
212 free(tmp);
213 return -EINVAL;
217 free(tmp);
218 return 0;