builtin-kvm.c 24.2 KB
Newer Older
1 2 3
#include "builtin.h"
#include "perf.h"

X
Xiao Guangrong 已提交
4
#include "util/evsel.h"
5 6 7 8 9 10 11 12 13 14
#include "util/util.h"
#include "util/cache.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
#include "util/session.h"

#include "util/parse-options.h"
#include "util/trace-event.h"
#include "util/debug.h"
15
#include <lk/debugfs.h>
X
Xiao Guangrong 已提交
16 17
#include "util/tool.h"
#include "util/stat.h"
18 19 20 21 22 23 24

#include <sys/prctl.h>

#include <semaphore.h>
#include <pthread.h>
#include <math.h>

25
#if defined(__i386__) || defined(__x86_64__)
26 27 28
#include <asm/svm.h>
#include <asm/vmx.h>
#include <asm/kvm.h>
X
Xiao Guangrong 已提交
29 30 31 32 33 34 35

struct event_key {
	#define INVALID_KEY     (~0ULL)
	u64 key;
	int info;
};

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
struct kvm_event_stats {
	u64 time;
	struct stats stats;
};

struct kvm_event {
	struct list_head hash_entry;
	struct rb_node rb;

	struct event_key key;

	struct kvm_event_stats total;

	#define DEFAULT_VCPU_NUM 8
	int max_vcpu;
	struct kvm_event_stats *vcpu;
};

typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);

struct kvm_event_key {
	const char *name;
	key_cmp_fun key;
};


62
struct perf_kvm_stat;
63

X
Xiao Guangrong 已提交
64
struct kvm_events_ops {
65 66 67 68 69
	bool (*is_begin_event)(struct perf_evsel *evsel,
			       struct perf_sample *sample,
			       struct event_key *key);
	bool (*is_end_event)(struct perf_evsel *evsel,
			     struct perf_sample *sample, struct event_key *key);
70
	void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
71
			   char decode[20]);
X
Xiao Guangrong 已提交
72 73 74
	const char *name;
};

75 76 77 78 79 80 81 82
struct exit_reasons_table {
	unsigned long exit_code;
	const char *reason;
};

#define EVENTS_BITS		12
#define EVENTS_CACHE_SIZE	(1UL << EVENTS_BITS)

83
struct perf_kvm_stat {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	struct perf_tool    tool;
	struct perf_session *session;

	const char *file_name;
	const char *report_event;
	const char *sort_key;
	int trace_vcpu;

	struct exit_reasons_table *exit_reasons;
	int exit_reasons_size;
	const char *exit_reasons_isa;

	struct kvm_events_ops *events_ops;
	key_cmp_fun compare;
	struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
	u64 total_time;
	u64 total_count;

	struct rb_root result;
};


106 107 108
static void exit_event_get_key(struct perf_evsel *evsel,
			       struct perf_sample *sample,
			       struct event_key *key)
X
Xiao Guangrong 已提交
109 110
{
	key->info = 0;
111
	key->key = perf_evsel__intval(evsel, sample, "exit_reason");
X
Xiao Guangrong 已提交
112 113
}

114
static bool kvm_exit_event(struct perf_evsel *evsel)
X
Xiao Guangrong 已提交
115
{
116
	return !strcmp(evsel->name, "kvm:kvm_exit");
X
Xiao Guangrong 已提交
117 118
}

119 120
static bool exit_event_begin(struct perf_evsel *evsel,
			     struct perf_sample *sample, struct event_key *key)
X
Xiao Guangrong 已提交
121
{
122 123
	if (kvm_exit_event(evsel)) {
		exit_event_get_key(evsel, sample, key);
X
Xiao Guangrong 已提交
124 125 126 127 128 129
		return true;
	}

	return false;
}

130
static bool kvm_entry_event(struct perf_evsel *evsel)
X
Xiao Guangrong 已提交
131
{
132
	return !strcmp(evsel->name, "kvm:kvm_entry");
X
Xiao Guangrong 已提交
133 134
}

135 136 137
static bool exit_event_end(struct perf_evsel *evsel,
			   struct perf_sample *sample __maybe_unused,
			   struct event_key *key __maybe_unused)
X
Xiao Guangrong 已提交
138
{
139
	return kvm_entry_event(evsel);
X
Xiao Guangrong 已提交
140 141
}

142
static struct exit_reasons_table vmx_exit_reasons[] = {
X
Xiao Guangrong 已提交
143 144 145
	VMX_EXIT_REASONS
};

146
static struct exit_reasons_table svm_exit_reasons[] = {
X
Xiao Guangrong 已提交
147 148 149
	SVM_EXIT_REASONS
};

150
static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
X
Xiao Guangrong 已提交
151
{
152 153
	int i = kvm->exit_reasons_size;
	struct exit_reasons_table *tbl = kvm->exit_reasons;
X
Xiao Guangrong 已提交
154

155 156 157 158
	while (i--) {
		if (tbl->exit_code == exit_code)
			return tbl->reason;
		tbl++;
X
Xiao Guangrong 已提交
159 160 161
	}

	pr_err("unknown kvm exit code:%lld on %s\n",
162
		(unsigned long long)exit_code, kvm->exit_reasons_isa);
X
Xiao Guangrong 已提交
163 164 165
	return "UNKNOWN";
}

166
static void exit_event_decode_key(struct perf_kvm_stat *kvm,
167 168
				  struct event_key *key,
				  char decode[20])
X
Xiao Guangrong 已提交
169
{
170
	const char *exit_reason = get_exit_reason(kvm, key->key);
X
Xiao Guangrong 已提交
171 172 173 174 175 176 177 178 179 180 181

	scnprintf(decode, 20, "%s", exit_reason);
}

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"
};

182 183 184 185 186
/*
 * For the mmio events, we treat:
 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
 */
187 188
static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
			       struct event_key *key)
X
Xiao Guangrong 已提交
189
{
190 191
	key->key  = perf_evsel__intval(evsel, sample, "gpa");
	key->info = perf_evsel__intval(evsel, sample, "type");
X
Xiao Guangrong 已提交
192 193 194 195 196 197
}

#define KVM_TRACE_MMIO_READ_UNSATISFIED 0
#define KVM_TRACE_MMIO_READ 1
#define KVM_TRACE_MMIO_WRITE 2

198 199
static bool mmio_event_begin(struct perf_evsel *evsel,
			     struct perf_sample *sample, struct event_key *key)
X
Xiao Guangrong 已提交
200 201
{
	/* MMIO read begin event in kernel. */
202
	if (kvm_exit_event(evsel))
X
Xiao Guangrong 已提交
203 204 205
		return true;

	/* MMIO write begin event in kernel. */
206 207 208
	if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
	    perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
		mmio_event_get_key(evsel, sample, key);
X
Xiao Guangrong 已提交
209 210 211 212 213 214
		return true;
	}

	return false;
}

215 216
static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
			   struct event_key *key)
X
Xiao Guangrong 已提交
217 218
{
	/* MMIO write end event in kernel. */
219
	if (kvm_entry_event(evsel))
X
Xiao Guangrong 已提交
220 221 222
		return true;

	/* MMIO read end event in kernel.*/
223 224 225
	if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
	    perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
		mmio_event_get_key(evsel, sample, key);
X
Xiao Guangrong 已提交
226 227 228 229 230 231
		return true;
	}

	return false;
}

232
static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
233 234
				  struct event_key *key,
				  char decode[20])
X
Xiao Guangrong 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247
{
	scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
				key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
}

static struct kvm_events_ops mmio_events = {
	.is_begin_event = mmio_event_begin,
	.is_end_event = mmio_event_end,
	.decode_key = mmio_event_decode_key,
	.name = "MMIO Access"
};

 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
248 249 250
static void ioport_event_get_key(struct perf_evsel *evsel,
				 struct perf_sample *sample,
				 struct event_key *key)
X
Xiao Guangrong 已提交
251
{
252 253
	key->key  = perf_evsel__intval(evsel, sample, "port");
	key->info = perf_evsel__intval(evsel, sample, "rw");
X
Xiao Guangrong 已提交
254 255
}

256 257 258
static bool ioport_event_begin(struct perf_evsel *evsel,
			       struct perf_sample *sample,
			       struct event_key *key)
X
Xiao Guangrong 已提交
259
{
260 261
	if (!strcmp(evsel->name, "kvm:kvm_pio")) {
		ioport_event_get_key(evsel, sample, key);
X
Xiao Guangrong 已提交
262 263 264 265 266 267
		return true;
	}

	return false;
}

268 269
static bool ioport_event_end(struct perf_evsel *evsel,
			     struct perf_sample *sample __maybe_unused,
X
Xiao Guangrong 已提交
270 271
			     struct event_key *key __maybe_unused)
{
272
	return kvm_entry_event(evsel);
X
Xiao Guangrong 已提交
273 274
}

275
static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
276 277
				    struct event_key *key,
				    char decode[20])
X
Xiao Guangrong 已提交
278 279 280 281 282 283 284 285 286 287 288 289
{
	scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
				key->info ? "POUT" : "PIN");
}

static struct kvm_events_ops ioport_events = {
	.is_begin_event = ioport_event_begin,
	.is_end_event = ioport_event_end,
	.decode_key = ioport_event_decode_key,
	.name = "IO Port Access"
};

290
static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
291 292 293
{
	bool ret = true;

294 295 296 297 298 299
	if (!strcmp(kvm->report_event, "vmexit"))
		kvm->events_ops = &exit_events;
	else if (!strcmp(kvm->report_event, "mmio"))
		kvm->events_ops = &mmio_events;
	else if (!strcmp(kvm->report_event, "ioport"))
		kvm->events_ops = &ioport_events;
X
Xiao Guangrong 已提交
300
	else {
301
		pr_err("Unknown report event:%s\n", kvm->report_event);
X
Xiao Guangrong 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314
		ret = false;
	}

	return ret;
}

struct vcpu_event_record {
	int vcpu_id;
	u64 start_time;
	struct kvm_event *last_event;
};


315
static void init_kvm_event_record(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
316
{
317
	unsigned int i;
X
Xiao Guangrong 已提交
318

319
	for (i = 0; i < EVENTS_CACHE_SIZE; i++)
320
		INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
X
Xiao Guangrong 已提交
321 322 323 324 325 326 327 328 329 330
}

static int kvm_events_hash_fn(u64 key)
{
	return key & (EVENTS_CACHE_SIZE - 1);
}

static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
{
	int old_max_vcpu = event->max_vcpu;
D
David Ahern 已提交
331
	void *prev;
X
Xiao Guangrong 已提交
332 333 334 335 336 337 338

	if (vcpu_id < event->max_vcpu)
		return true;

	while (event->max_vcpu <= vcpu_id)
		event->max_vcpu += DEFAULT_VCPU_NUM;

D
David Ahern 已提交
339
	prev = event->vcpu;
X
Xiao Guangrong 已提交
340 341 342
	event->vcpu = realloc(event->vcpu,
			      event->max_vcpu * sizeof(*event->vcpu));
	if (!event->vcpu) {
D
David Ahern 已提交
343
		free(prev);
X
Xiao Guangrong 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		pr_err("Not enough memory\n");
		return false;
	}

	memset(event->vcpu + old_max_vcpu, 0,
	       (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
	return true;
}

static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
{
	struct kvm_event *event;

	event = zalloc(sizeof(*event));
	if (!event) {
		pr_err("Not enough memory\n");
		return NULL;
	}

	event->key = *key;
	return event;
}

367
static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
368
					       struct event_key *key)
X
Xiao Guangrong 已提交
369 370 371 372 373 374
{
	struct kvm_event *event;
	struct list_head *head;

	BUG_ON(key->key == INVALID_KEY);

375
	head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
376
	list_for_each_entry(event, head, hash_entry) {
X
Xiao Guangrong 已提交
377 378
		if (event->key.key == key->key && event->key.info == key->info)
			return event;
379
	}
X
Xiao Guangrong 已提交
380 381 382 383 384 385 386 387 388

	event = kvm_alloc_init_event(key);
	if (!event)
		return NULL;

	list_add(&event->hash_entry, head);
	return event;
}

389
static bool handle_begin_event(struct perf_kvm_stat *kvm,
390
			       struct vcpu_event_record *vcpu_record,
X
Xiao Guangrong 已提交
391 392 393 394 395
			       struct event_key *key, u64 timestamp)
{
	struct kvm_event *event = NULL;

	if (key->key != INVALID_KEY)
396
		event = find_create_kvm_event(kvm, key);
X
Xiao Guangrong 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

	vcpu_record->last_event = event;
	vcpu_record->start_time = timestamp;
	return true;
}

static void
kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
{
	kvm_stats->time += time_diff;
	update_stats(&kvm_stats->stats, time_diff);
}

static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
{
	struct kvm_event_stats *kvm_stats = &event->total;

	if (vcpu_id != -1)
		kvm_stats = &event->vcpu[vcpu_id];

	return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
				avg_stats(&kvm_stats->stats));
}

static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
			     u64 time_diff)
{
424 425 426 427
	if (vcpu_id == -1) {
		kvm_update_event_stats(&event->total, time_diff);
		return true;
	}
X
Xiao Guangrong 已提交
428 429 430 431 432 433 434 435

	if (!kvm_event_expand(event, vcpu_id))
		return false;

	kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
	return true;
}

436
static bool handle_end_event(struct perf_kvm_stat *kvm,
437 438 439
			     struct vcpu_event_record *vcpu_record,
			     struct event_key *key,
			     u64 timestamp)
X
Xiao Guangrong 已提交
440 441 442
{
	struct kvm_event *event;
	u64 time_begin, time_diff;
443 444 445 446 447 448
	int vcpu;

	if (kvm->trace_vcpu == -1)
		vcpu = -1;
	else
		vcpu = vcpu_record->vcpu_id;
X
Xiao Guangrong 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

	event = vcpu_record->last_event;
	time_begin = vcpu_record->start_time;

	/* The begin event is not caught. */
	if (!time_begin)
		return true;

	/*
	 * In some case, the 'begin event' only records the start timestamp,
	 * the actual event is recognized in the 'end event' (e.g. mmio-event).
	 */

	/* Both begin and end events did not get the key. */
	if (!event && key->key == INVALID_KEY)
		return true;

	if (!event)
467
		event = find_create_kvm_event(kvm, key);
X
Xiao Guangrong 已提交
468 469 470 471 472 473 474 475 476 477

	if (!event)
		return false;

	vcpu_record->last_event = NULL;
	vcpu_record->start_time = 0;

	BUG_ON(timestamp < time_begin);

	time_diff = timestamp - time_begin;
478
	return update_kvm_event(event, vcpu, time_diff);
X
Xiao Guangrong 已提交
479 480
}

481 482 483 484
static
struct vcpu_event_record *per_vcpu_record(struct thread *thread,
					  struct perf_evsel *evsel,
					  struct perf_sample *sample)
X
Xiao Guangrong 已提交
485 486
{
	/* Only kvm_entry records vcpu id. */
487
	if (!thread->priv && kvm_entry_event(evsel)) {
X
Xiao Guangrong 已提交
488 489
		struct vcpu_event_record *vcpu_record;

490
		vcpu_record = zalloc(sizeof(*vcpu_record));
X
Xiao Guangrong 已提交
491
		if (!vcpu_record) {
492
			pr_err("%s: Not enough memory\n", __func__);
X
Xiao Guangrong 已提交
493 494 495
			return NULL;
		}

496
		vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
X
Xiao Guangrong 已提交
497 498 499
		thread->priv = vcpu_record;
	}

500
	return thread->priv;
X
Xiao Guangrong 已提交
501 502
}

503
static bool handle_kvm_event(struct perf_kvm_stat *kvm,
504 505
			     struct thread *thread,
			     struct perf_evsel *evsel,
506
			     struct perf_sample *sample)
X
Xiao Guangrong 已提交
507 508 509 510
{
	struct vcpu_event_record *vcpu_record;
	struct event_key key = {.key = INVALID_KEY};

511
	vcpu_record = per_vcpu_record(thread, evsel, sample);
X
Xiao Guangrong 已提交
512 513 514
	if (!vcpu_record)
		return true;

515 516 517 518 519
	/* only process events for vcpus user cares about */
	if ((kvm->trace_vcpu != -1) &&
	    (kvm->trace_vcpu != vcpu_record->vcpu_id))
		return true;

520 521
	if (kvm->events_ops->is_begin_event(evsel, sample, &key))
		return handle_begin_event(kvm, vcpu_record, &key, sample->time);
X
Xiao Guangrong 已提交
522

523 524
	if (kvm->events_ops->is_end_event(evsel, sample, &key))
		return handle_end_event(kvm, vcpu_record, &key, sample->time);
X
Xiao Guangrong 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

	return true;
}

#define GET_EVENT_KEY(func, field)					\
static u64 get_event_ ##func(struct kvm_event *event, int vcpu)		\
{									\
	if (vcpu == -1)							\
		return event->total.field;				\
									\
	if (vcpu >= event->max_vcpu)					\
		return 0;						\
									\
	return event->vcpu[vcpu].field;					\
}

#define COMPARE_EVENT_KEY(func, field)					\
GET_EVENT_KEY(func, field)						\
static int compare_kvm_event_ ## func(struct kvm_event *one,		\
					struct kvm_event *two, int vcpu)\
{									\
	return get_event_ ##func(one, vcpu) >				\
				get_event_ ##func(two, vcpu);		\
}

GET_EVENT_KEY(time, time);
COMPARE_EVENT_KEY(count, stats.n);
COMPARE_EVENT_KEY(mean, stats.mean);

#define DEF_SORT_NAME_KEY(name, compare_key)				\
	{ #name, compare_kvm_event_ ## compare_key }

static struct kvm_event_key keys[] = {
	DEF_SORT_NAME_KEY(sample, count),
	DEF_SORT_NAME_KEY(time, mean),
	{ NULL, NULL }
};

563
static bool select_key(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
564 565 566 567
{
	int i;

	for (i = 0; keys[i].name; i++) {
568 569
		if (!strcmp(keys[i].name, kvm->sort_key)) {
			kvm->compare = keys[i].key;
X
Xiao Guangrong 已提交
570 571 572 573
			return true;
		}
	}

574
	pr_err("Unknown compare key:%s\n", kvm->sort_key);
X
Xiao Guangrong 已提交
575 576 577
	return false;
}

578 579
static void insert_to_result(struct rb_root *result, struct kvm_event *event,
			     key_cmp_fun bigger, int vcpu)
X
Xiao Guangrong 已提交
580
{
581
	struct rb_node **rb = &result->rb_node;
X
Xiao Guangrong 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595
	struct rb_node *parent = NULL;
	struct kvm_event *p;

	while (*rb) {
		p = container_of(*rb, struct kvm_event, rb);
		parent = *rb;

		if (bigger(event, p, vcpu))
			rb = &(*rb)->rb_left;
		else
			rb = &(*rb)->rb_right;
	}

	rb_link_node(&event->rb, parent, rb);
596
	rb_insert_color(&event->rb, result);
X
Xiao Guangrong 已提交
597 598
}

599 600
static void
update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
X
Xiao Guangrong 已提交
601
{
602 603 604 605
	int vcpu = kvm->trace_vcpu;

	kvm->total_count += get_event_count(event, vcpu);
	kvm->total_time += get_event_time(event, vcpu);
X
Xiao Guangrong 已提交
606 607 608 609 610 611 612
}

static bool event_is_valid(struct kvm_event *event, int vcpu)
{
	return !!get_event_count(event, vcpu);
}

613
static void sort_result(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
614 615
{
	unsigned int i;
616
	int vcpu = kvm->trace_vcpu;
X
Xiao Guangrong 已提交
617 618
	struct kvm_event *event;

619 620
	for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
		list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
X
Xiao Guangrong 已提交
621
			if (event_is_valid(event, vcpu)) {
622 623 624
				update_total_count(kvm, event);
				insert_to_result(&kvm->result, event,
						 kvm->compare, vcpu);
X
Xiao Guangrong 已提交
625
			}
626 627
		}
	}
X
Xiao Guangrong 已提交
628 629 630
}

/* returns left most element of result, and erase it */
631
static struct kvm_event *pop_from_result(struct rb_root *result)
X
Xiao Guangrong 已提交
632
{
633
	struct rb_node *node = rb_first(result);
X
Xiao Guangrong 已提交
634 635 636 637

	if (!node)
		return NULL;

638
	rb_erase(node, result);
X
Xiao Guangrong 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651
	return container_of(node, struct kvm_event, rb);
}

static void print_vcpu_info(int vcpu)
{
	pr_info("Analyze events for ");

	if (vcpu == -1)
		pr_info("all VCPUs:\n\n");
	else
		pr_info("VCPU %d:\n\n", vcpu);
}

652
static void print_result(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
653 654 655
{
	char decode[20];
	struct kvm_event *event;
656
	int vcpu = kvm->trace_vcpu;
X
Xiao Guangrong 已提交
657 658 659

	pr_info("\n\n");
	print_vcpu_info(vcpu);
660
	pr_info("%20s ", kvm->events_ops->name);
X
Xiao Guangrong 已提交
661 662 663 664 665 666 667
	pr_info("%10s ", "Samples");
	pr_info("%9s ", "Samples%");

	pr_info("%9s ", "Time%");
	pr_info("%16s ", "Avg time");
	pr_info("\n\n");

668
	while ((event = pop_from_result(&kvm->result))) {
X
Xiao Guangrong 已提交
669 670 671 672 673
		u64 ecount, etime;

		ecount = get_event_count(event, vcpu);
		etime = get_event_time(event, vcpu);

674
		kvm->events_ops->decode_key(kvm, &event->key, decode);
X
Xiao Guangrong 已提交
675 676
		pr_info("%20s ", decode);
		pr_info("%10llu ", (unsigned long long)ecount);
677 678
		pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
		pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
X
Xiao Guangrong 已提交
679 680 681 682 683
		pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
			kvm_event_rel_stddev(vcpu, event));
		pr_info("\n");
	}

684 685
	pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
		kvm->total_count, kvm->total_time / 1e3);
X
Xiao Guangrong 已提交
686 687
}

688
static int process_sample_event(struct perf_tool *tool,
X
Xiao Guangrong 已提交
689 690 691 692 693 694
				union perf_event *event,
				struct perf_sample *sample,
				struct perf_evsel *evsel,
				struct machine *machine)
{
	struct thread *thread = machine__findnew_thread(machine, sample->tid);
695 696
	struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
						 tool);
X
Xiao Guangrong 已提交
697 698 699 700 701 702 703

	if (thread == NULL) {
		pr_debug("problem processing %d event, skipping it.\n",
			event->header.type);
		return -1;
	}

704
	if (!handle_kvm_event(kvm, thread, evsel, sample))
X
Xiao Guangrong 已提交
705 706 707 708 709 710 711
		return -1;

	return 0;
}

static int get_cpu_isa(struct perf_session *session)
{
712
	char *cpuid = session->header.env.cpuid;
X
Xiao Guangrong 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726
	int isa;

	if (strstr(cpuid, "Intel"))
		isa = 1;
	else if (strstr(cpuid, "AMD"))
		isa = 0;
	else {
		pr_err("CPU %s is not supported.\n", cpuid);
		isa = -ENOTSUP;
	}

	return isa;
}

727
static int read_events(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
728 729 730
{
	int ret;

731 732 733 734 735 736 737 738 739 740
	struct perf_tool eops = {
		.sample			= process_sample_event,
		.comm			= perf_event__process_comm,
		.ordered_samples	= true,
	};

	kvm->tool = eops;
	kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false,
					 &kvm->tool);
	if (!kvm->session) {
X
Xiao Guangrong 已提交
741 742 743 744
		pr_err("Initializing perf session failed\n");
		return -EINVAL;
	}

745
	if (!perf_session__has_traces(kvm->session, "kvm record"))
X
Xiao Guangrong 已提交
746 747 748 749 750 751
		return -EINVAL;

	/*
	 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
	 * traced in the old kernel.
	 */
752
	ret = get_cpu_isa(kvm->session);
X
Xiao Guangrong 已提交
753 754 755 756

	if (ret < 0)
		return ret;

757 758 759 760 761
	if (ret == 1) {
		kvm->exit_reasons = vmx_exit_reasons;
		kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
		kvm->exit_reasons_isa = "VMX";
	}
X
Xiao Guangrong 已提交
762

763
	return perf_session__process_events(kvm->session, &kvm->tool);
X
Xiao Guangrong 已提交
764 765 766 767 768 769 770 771 772 773 774 775
}

static bool verify_vcpu(int vcpu)
{
	if (vcpu != -1 && vcpu < 0) {
		pr_err("Invalid vcpu:%d.\n", vcpu);
		return false;
	}

	return true;
}

776
static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
X
Xiao Guangrong 已提交
777 778
{
	int ret = -EINVAL;
779
	int vcpu = kvm->trace_vcpu;
X
Xiao Guangrong 已提交
780 781 782 783

	if (!verify_vcpu(vcpu))
		goto exit;

784
	if (!select_key(kvm))
X
Xiao Guangrong 已提交
785 786
		goto exit;

787
	if (!register_kvm_events_ops(kvm))
X
Xiao Guangrong 已提交
788 789
		goto exit;

790
	init_kvm_event_record(kvm);
X
Xiao Guangrong 已提交
791 792
	setup_pager();

793
	ret = read_events(kvm);
X
Xiao Guangrong 已提交
794 795 796
	if (ret)
		goto exit;

797 798 799
	sort_result(kvm);
	print_result(kvm);

X
Xiao Guangrong 已提交
800 801 802 803
exit:
	return ret;
}

804 805 806 807 808
static const char * const kvm_events_tp[] = {
	"kvm:kvm_entry",
	"kvm:kvm_exit",
	"kvm:kvm_mmio",
	"kvm:kvm_pio",
X
Xiao Guangrong 已提交
809 810 811 812 813 814 815 816 817 818
};

#define STRDUP_FAIL_EXIT(s)		\
	({	char *_p;		\
	_p = strdup(s);		\
		if (!_p)		\
			return -ENOMEM;	\
		_p;			\
	})

819 820
static int
kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
X
Xiao Guangrong 已提交
821 822 823
{
	unsigned int rec_argc, i, j;
	const char **rec_argv;
824 825 826 827 828 829 830
	const char * const record_args[] = {
		"record",
		"-R",
		"-f",
		"-m", "1024",
		"-c", "1",
	};
X
Xiao Guangrong 已提交
831

832 833
	rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
		   2 * ARRAY_SIZE(kvm_events_tp);
X
Xiao Guangrong 已提交
834 835 836 837 838 839 840 841
	rec_argv = calloc(rec_argc + 1, sizeof(char *));

	if (rec_argv == NULL)
		return -ENOMEM;

	for (i = 0; i < ARRAY_SIZE(record_args); i++)
		rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);

842 843 844 845 846
	for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) {
		rec_argv[i++] = "-e";
		rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
	}

X
Xiao Guangrong 已提交
847
	rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
848
	rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
X
Xiao Guangrong 已提交
849 850 851 852 853 854 855

	for (j = 1; j < (unsigned int)argc; j++, i++)
		rec_argv[i] = argv[j];

	return cmd_record(i, rec_argv, NULL);
}

856 857
static int
kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
858 859 860 861 862 863 864 865 866 867 868
{
	const struct option kvm_events_report_options[] = {
		OPT_STRING(0, "event", &kvm->report_event, "report event",
			    "event for reporting: vmexit, mmio, ioport"),
		OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
			    "vcpu id to report"),
		OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
			    "key for sorting: sample(sort by samples number)"
			    " time (sort by avg time)"),
		OPT_END()
	};
X
Xiao Guangrong 已提交
869

870 871 872 873
	const char * const kvm_events_report_usage[] = {
		"perf kvm stat report [<options>]",
		NULL
	};
X
Xiao Guangrong 已提交
874 875 876 877 878 879 880 881 882 883 884 885

	symbol__init();

	if (argc) {
		argc = parse_options(argc, argv,
				     kvm_events_report_options,
				     kvm_events_report_usage, 0);
		if (argc)
			usage_with_options(kvm_events_report_usage,
					   kvm_events_report_options);
	}

886
	return kvm_events_report_vcpu(kvm);
X
Xiao Guangrong 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899
}

static void print_kvm_stat_usage(void)
{
	printf("Usage: perf kvm stat <command>\n\n");

	printf("# Available commands:\n");
	printf("\trecord: record kvm events\n");
	printf("\treport: report statistical data of kvm events\n");

	printf("\nOtherwise, it is the alias of 'perf stat':\n");
}

900
static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
X
Xiao Guangrong 已提交
901
{
902 903 904 905 906 907 908 909 910 911 912 913
	struct perf_kvm_stat kvm = {
		.file_name = file_name,

		.trace_vcpu	= -1,
		.report_event	= "vmexit",
		.sort_key	= "sample",

		.exit_reasons = svm_exit_reasons,
		.exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
		.exit_reasons_isa = "SVM",
	};

X
Xiao Guangrong 已提交
914 915 916 917 918 919
	if (argc == 1) {
		print_kvm_stat_usage();
		goto perf_stat;
	}

	if (!strncmp(argv[1], "rec", 3))
920
		return kvm_events_record(&kvm, argc - 1, argv + 1);
X
Xiao Guangrong 已提交
921 922

	if (!strncmp(argv[1], "rep", 3))
923
		return kvm_events_report(&kvm, argc - 1 , argv + 1);
X
Xiao Guangrong 已提交
924 925 926 927

perf_stat:
	return cmd_stat(argc, argv, NULL);
}
928
#endif
X
Xiao Guangrong 已提交
929

930
static int __cmd_record(const char *file_name, int argc, const char **argv)
931 932 933 934 935 936 937 938
{
	int rec_argc, i = 0, j;
	const char **rec_argv;

	rec_argc = argc + 2;
	rec_argv = calloc(rec_argc + 1, sizeof(char *));
	rec_argv[i++] = strdup("record");
	rec_argv[i++] = strdup("-o");
939
	rec_argv[i++] = strdup(file_name);
940 941 942 943 944 945 946 947
	for (j = 1; j < argc; j++, i++)
		rec_argv[i] = argv[j];

	BUG_ON(i != rec_argc);

	return cmd_record(i, rec_argv, NULL);
}

948
static int __cmd_report(const char *file_name, int argc, const char **argv)
949 950 951 952 953 954 955 956
{
	int rec_argc, i = 0, j;
	const char **rec_argv;

	rec_argc = argc + 2;
	rec_argv = calloc(rec_argc + 1, sizeof(char *));
	rec_argv[i++] = strdup("report");
	rec_argv[i++] = strdup("-i");
957
	rec_argv[i++] = strdup(file_name);
958 959 960 961 962 963 964 965
	for (j = 1; j < argc; j++, i++)
		rec_argv[i] = argv[j];

	BUG_ON(i != rec_argc);

	return cmd_report(i, rec_argv, NULL);
}

966 967
static int
__cmd_buildid_list(const char *file_name, int argc, const char **argv)
968 969 970 971 972 973 974 975
{
	int rec_argc, i = 0, j;
	const char **rec_argv;

	rec_argc = argc + 2;
	rec_argv = calloc(rec_argc + 1, sizeof(char *));
	rec_argv[i++] = strdup("buildid-list");
	rec_argv[i++] = strdup("-i");
976
	rec_argv[i++] = strdup(file_name);
977 978 979 980 981 982 983 984
	for (j = 1; j < argc; j++, i++)
		rec_argv[i] = argv[j];

	BUG_ON(i != rec_argc);

	return cmd_buildid_list(i, rec_argv, NULL);
}

985
int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
986
{
987
	const char *file_name = NULL;
988
	const struct option kvm_options[] = {
989
		OPT_STRING('i', "input", &file_name, "file",
990
			   "Input file name"),
991
		OPT_STRING('o', "output", &file_name, "file",
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
			   "Output file name"),
		OPT_BOOLEAN(0, "guest", &perf_guest,
			    "Collect guest os data"),
		OPT_BOOLEAN(0, "host", &perf_host,
			    "Collect host os data"),
		OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
			   "guest mount directory under which every guest os"
			   " instance has a subdir"),
		OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
			   "file", "file saving guest os vmlinux"),
		OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
			   "file", "file saving guest os /proc/kallsyms"),
		OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
			   "file", "file saving guest os /proc/modules"),
		OPT_END()
	};


	const char * const kvm_usage[] = {
		"perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
		NULL
	};

1015 1016
	perf_host  = 0;
	perf_guest = 1;
1017 1018 1019 1020 1021 1022 1023 1024 1025

	argc = parse_options(argc, argv, kvm_options, kvm_usage,
			PARSE_OPT_STOP_AT_NON_OPTION);
	if (!argc)
		usage_with_options(kvm_usage, kvm_options);

	if (!perf_host)
		perf_guest = 1;

1026
	if (!file_name) {
1027
		if (perf_host && !perf_guest)
1028
			file_name = strdup("perf.data.host");
1029
		else if (!perf_host && perf_guest)
1030
			file_name = strdup("perf.data.guest");
1031
		else
1032
			file_name = strdup("perf.data.kvm");
1033

1034
		if (!file_name) {
1035 1036 1037
			pr_err("Failed to allocate memory for filename\n");
			return -ENOMEM;
		}
1038 1039 1040
	}

	if (!strncmp(argv[0], "rec", 3))
1041
		return __cmd_record(file_name, argc, argv);
1042
	else if (!strncmp(argv[0], "rep", 3))
1043
		return __cmd_report(file_name, argc, argv);
1044 1045 1046 1047 1048
	else if (!strncmp(argv[0], "diff", 4))
		return cmd_diff(argc, argv, NULL);
	else if (!strncmp(argv[0], "top", 3))
		return cmd_top(argc, argv, NULL);
	else if (!strncmp(argv[0], "buildid-list", 12))
1049
		return __cmd_buildid_list(file_name, argc, argv);
1050
#if defined(__i386__) || defined(__x86_64__)
X
Xiao Guangrong 已提交
1051
	else if (!strncmp(argv[0], "stat", 4))
1052
		return kvm_cmd_stat(file_name, argc, argv);
1053
#endif
1054 1055 1056 1057 1058
	else
		usage_with_options(kvm_usage, kvm_options);

	return 0;
}