kvm-stat.c 5.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <errno.h>
3 4
#include "util/kvm-stat.h"
#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"
9 10

#include "book3s_hv_exits.h"
11
#include "book3s_hcalls.h"
12
#include <subcmd/parse-options.h>
13

14
#define NR_TPS 4
15 16 17 18 19 20 21

const char *vcpu_id_str = "vcpu_id";
const int decode_str_len = 40;
const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";

define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
22
define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
23 24 25 26 27

/* Tracepoints specific to ppc_book3s_hv */
const char *ppc_book3s_hv_kvm_tp[] = {
	"kvm_hv:kvm_guest_enter",
	"kvm_hv:kvm_guest_exit",
28 29 30
	"kvm_hv:kvm_hcall_enter",
	"kvm_hv:kvm_hcall_exit",
	NULL,
31 32 33 34 35 36
};

/* 1 extra placeholder for NULL */
const char *kvm_events_tp[NR_TPS + 1];
const char *kvm_exit_reason;

37
static void hcall_event_get_key(struct evsel *evsel,
38 39 40 41
				struct perf_sample *sample,
				struct event_key *key)
{
	key->info = 0;
42
	key->key = evsel__intval(evsel, sample, "req");
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
}

static const char *get_hcall_exit_reason(u64 exit_code)
{
	struct exit_reasons_table *tbl = hcall_reasons;

	while (tbl->reason != NULL) {
		if (tbl->exit_code == exit_code)
			return tbl->reason;
		tbl++;
	}

	pr_debug("Unknown hcall code: %lld\n",
	       (unsigned long long)exit_code);
	return "UNKNOWN";
}

60
static bool hcall_event_end(struct evsel *evsel,
61 62 63 64 65 66
			    struct perf_sample *sample __maybe_unused,
			    struct event_key *key __maybe_unused)
{
	return (!strcmp(evsel->name, kvm_events_tp[3]));
}

67
static bool hcall_event_begin(struct evsel *evsel,
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
			      struct perf_sample *sample, struct event_key *key)
{
	if (!strcmp(evsel->name, kvm_events_tp[2])) {
		hcall_event_get_key(evsel, sample, key);
		return true;
	}

	return false;
}
static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
				   struct event_key *key,
				   char *decode)
{
	const char *hcall_reason = get_hcall_exit_reason(key->key);

	scnprintf(decode, decode_str_len, "%s", hcall_reason);
}

static struct kvm_events_ops hcall_events = {
	.is_begin_event = hcall_event_begin,
	.is_end_event = hcall_event_end,
	.decode_key = hcall_event_decode_key,
	.name = "HCALL-EVENT",
};

93 94 95 96 97 98 99 100 101
static struct kvm_events_ops exit_events = {
	.is_begin_event = exit_event_begin,
	.is_end_event = exit_event_end,
	.decode_key = exit_event_decode_key,
	.name = "VM-EXIT"
};

struct kvm_reg_events_ops kvm_reg_events_ops[] = {
	{ .name = "vmexit", .ops = &exit_events },
102
	{ .name = "hcall", .ops = &hcall_events },
103 104 105 106 107 108 109 110
	{ NULL, NULL },
};

const char * const kvm_skip_events[] = {
	NULL,
};


111
static int is_tracepoint_available(const char *str, struct evlist *evlist)
112 113 114 115
{
	struct parse_events_error err;
	int ret;

116
	parse_events_error__init(&err);
117 118
	ret = parse_events(evlist, str, &err);
	if (err.str)
119
		parse_events_error__print(&err, "tracepoint");
120
	parse_events_error__exit(&err);
121 122 123 124
	return ret;
}

static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
125
				struct evlist *evlist)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
{
	const char **events_ptr;
	int i, nr_tp = 0, err = -1;

	/* Check for book3s_hv tracepoints */
	for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
		err = is_tracepoint_available(*events_ptr, evlist);
		if (err)
			return -1;
		nr_tp++;
	}

	for (i = 0; i < nr_tp; i++)
		kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];

	kvm_events_tp[i] = NULL;
	kvm_exit_reason = "trap";
	kvm->exit_reasons = hv_exit_reasons;
	kvm->exit_reasons_isa = "HV";

	return 0;
}

/* Wrapper to setup kvm tracepoints */
static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
{
152
	struct evlist *evlist = evlist__new();
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

	if (evlist == NULL)
		return -ENOMEM;

	/* Right now, only supported on book3s_hv */
	return ppc__setup_book3s_hv(kvm, evlist);
}

int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
{
	return ppc__setup_kvm_tp(kvm);
}

int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
{
	int ret;

	ret = ppc__setup_kvm_tp(kvm);
	if (ret) {
		kvm->exit_reasons = NULL;
		kvm->exit_reasons_isa = NULL;
	}

	return ret;
}
178 179

/*
180
 * In case of powerpc architecture, pmu registers are programmable
181
 * by guest kernel. So monitoring guest via host may not provide
182 183 184
 * valid samples with default 'cycles' event. It is better to use
 * 'trace_imc/trace_cycles' event for guest profiling, since it
 * can track the guest instruction pointer in the trace-record.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
 *
 * Function to parse the arguments and return appropriate values.
 */
int kvm_add_default_arch_event(int *argc, const char **argv)
{
	const char **tmp;
	bool event = false;
	int i, j = *argc;

	const struct option event_options[] = {
		OPT_BOOLEAN('e', "event", &event, NULL),
		OPT_END()
	};

	tmp = calloc(j + 1, sizeof(char *));
	if (!tmp)
		return -EINVAL;

	for (i = 0; i < j; i++)
		tmp[i] = argv[i];

	parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
	if (!event) {
208 209 210 211 212 213 214 215
		if (pmu_have_event("trace_imc", "trace_cycles")) {
			argv[j++] = strdup("-e");
			argv[j++] = strdup("trace_imc/trace_cycles/");
			*argc += 2;
		} else {
			free(tmp);
			return -EINVAL;
		}
216 217 218 219 220
	}

	free(tmp);
	return 0;
}