19 #include <sys/prctl.h>
25 #if defined(__i386__) || defined(__x86_64__)
31 #define INVALID_KEY (~0ULL)
36 struct kvm_event_stats {
47 struct kvm_event_stats total;
49 #define DEFAULT_VCPU_NUM 8
51 struct kvm_event_stats *vcpu;
54 typedef int (*key_cmp_fun)(
struct kvm_event*,
struct kvm_event*,
int);
56 struct kvm_event_key {
64 struct kvm_events_ops {
67 struct event_key *
key);
70 void (*decode_key)(
struct perf_kvm_stat *
kvm,
struct event_key *
key,
75 struct exit_reasons_table {
76 unsigned long exit_code;
80 #define EVENTS_BITS 12
81 #define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
83 struct perf_kvm_stat {
88 const char *report_event;
92 struct exit_reasons_table *exit_reasons;
93 int exit_reasons_size;
94 const char *exit_reasons_isa;
96 struct kvm_events_ops *events_ops;
98 struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
106 static void exit_event_get_key(
struct perf_evsel *evsel,
108 struct event_key *
key)
114 static bool kvm_exit_event(
struct perf_evsel *evsel)
119 static bool exit_event_begin(
struct perf_evsel *evsel,
122 if (kvm_exit_event(evsel)) {
123 exit_event_get_key(evsel, sample, key);
130 static bool kvm_entry_event(
struct perf_evsel *evsel)
135 static bool exit_event_end(
struct perf_evsel *evsel,
137 struct event_key *key __maybe_unused)
139 return kvm_entry_event(evsel);
142 static struct exit_reasons_table vmx_exit_reasons[] = {
146 static struct exit_reasons_table svm_exit_reasons[] = {
150 static const char *get_exit_reason(
struct perf_kvm_stat *
kvm,
u64 exit_code)
152 int i = kvm->exit_reasons_size;
153 struct exit_reasons_table *tbl = kvm->exit_reasons;
156 if (tbl->exit_code == exit_code)
161 pr_err(
"unknown kvm exit code:%lld on %s\n",
162 (
unsigned long long)exit_code, kvm->exit_reasons_isa);
166 static void exit_event_decode_key(
struct perf_kvm_stat *kvm,
167 struct event_key *key,
170 const char *exit_reason = get_exit_reason(kvm, key->key);
172 scnprintf(decode, 20,
"%s", exit_reason);
175 static struct kvm_events_ops exit_events = {
176 .is_begin_event = exit_event_begin,
177 .is_end_event = exit_event_end,
178 .decode_key = exit_event_decode_key,
188 struct event_key *key)
194 #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
195 #define KVM_TRACE_MMIO_READ 1
196 #define KVM_TRACE_MMIO_WRITE 2
198 static bool mmio_event_begin(
struct perf_evsel *evsel,
202 if (kvm_exit_event(evsel))
208 mmio_event_get_key(evsel, sample, key);
216 struct event_key *key)
219 if (kvm_entry_event(evsel))
225 mmio_event_get_key(evsel, sample, key);
232 static void mmio_event_decode_key(
struct perf_kvm_stat *kvm
__maybe_unused,
233 struct event_key *key,
236 scnprintf(decode, 20,
"%#lx:%s", (
unsigned long)key->key,
240 static struct kvm_events_ops mmio_events = {
241 .is_begin_event = mmio_event_begin,
242 .is_end_event = mmio_event_end,
243 .decode_key = mmio_event_decode_key,
244 .name =
"MMIO Access"
248 static void ioport_event_get_key(
struct perf_evsel *evsel,
250 struct event_key *key)
256 static bool ioport_event_begin(
struct perf_evsel *evsel,
258 struct event_key *key)
261 ioport_event_get_key(evsel, sample, key);
268 static bool ioport_event_end(
struct perf_evsel *evsel,
270 struct event_key *key __maybe_unused)
272 return kvm_entry_event(evsel);
275 static void ioport_event_decode_key(
struct perf_kvm_stat *kvm __maybe_unused,
276 struct event_key *key,
279 scnprintf(decode, 20,
"%#llx:%s", (
unsigned long long)key->key,
280 key->info ?
"POUT" :
"PIN");
283 static struct kvm_events_ops ioport_events = {
284 .is_begin_event = ioport_event_begin,
285 .is_end_event = ioport_event_end,
286 .decode_key = ioport_event_decode_key,
287 .name =
"IO Port Access"
290 static bool register_kvm_events_ops(
struct perf_kvm_stat *kvm)
294 if (!
strcmp(kvm->report_event,
"vmexit"))
295 kvm->events_ops = &exit_events;
296 else if (!
strcmp(kvm->report_event,
"mmio"))
297 kvm->events_ops = &mmio_events;
298 else if (!
strcmp(kvm->report_event,
"ioport"))
299 kvm->events_ops = &ioport_events;
301 pr_err(
"Unknown report event:%s\n", kvm->report_event);
308 struct vcpu_event_record {
311 struct kvm_event *last_event;
315 static void init_kvm_event_record(
struct perf_kvm_stat *kvm)
319 for (i = 0; i < (
int)EVENTS_CACHE_SIZE; i++)
320 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
323 static int kvm_events_hash_fn(
u64 key)
325 return key & (EVENTS_CACHE_SIZE - 1);
328 static bool kvm_event_expand(
struct kvm_event *
event,
int vcpu_id)
330 int old_max_vcpu =
event->max_vcpu;
332 if (vcpu_id < event->max_vcpu)
335 while (event->max_vcpu <= vcpu_id)
336 event->max_vcpu += DEFAULT_VCPU_NUM;
338 event->vcpu = realloc(event->vcpu,
339 event->max_vcpu *
sizeof(*event->vcpu));
341 pr_err(
"Not enough memory\n");
345 memset(event->vcpu + old_max_vcpu, 0,
346 (event->max_vcpu - old_max_vcpu) *
sizeof(*event->vcpu));
350 static struct kvm_event *kvm_alloc_init_event(
struct event_key *key)
352 struct kvm_event *
event;
354 event =
zalloc(
sizeof(*event));
356 pr_err(
"Not enough memory\n");
364 static struct kvm_event *find_create_kvm_event(
struct perf_kvm_stat *kvm,
365 struct event_key *key)
367 struct kvm_event *
event;
370 BUG_ON(key->key == INVALID_KEY);
372 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
374 if (event->key.key == key->key && event->key.
info == key->
info)
377 event = kvm_alloc_init_event(key);
381 list_add(&event->hash_entry, head);
385 static
bool handle_begin_event(
struct perf_kvm_stat *kvm,
386 struct vcpu_event_record *vcpu_record,
389 struct kvm_event *
event =
NULL;
391 if (key->key != INVALID_KEY)
392 event = find_create_kvm_event(kvm, key);
394 vcpu_record->last_event =
event;
400 kvm_update_event_stats(
struct kvm_event_stats *kvm_stats,
u64 time_diff)
402 kvm_stats->time += time_diff;
406 static double kvm_event_rel_stddev(
int vcpu_id,
struct kvm_event *event)
408 struct kvm_event_stats *kvm_stats = &
event->total;
411 kvm_stats = &
event->vcpu[vcpu_id];
417 static bool update_kvm_event(
struct kvm_event *event,
int vcpu_id,
420 kvm_update_event_stats(&event->total, time_diff);
422 if (!kvm_event_expand(event, vcpu_id))
425 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
429 static bool handle_end_event(
struct perf_kvm_stat *kvm,
430 struct vcpu_event_record *vcpu_record,
431 struct event_key *key,
434 struct kvm_event *
event;
435 u64 time_begin, time_diff;
437 event = vcpu_record->last_event;
438 time_begin = vcpu_record->start_time;
450 if (!event && key->key == INVALID_KEY)
454 event = find_create_kvm_event(kvm, key);
459 vcpu_record->last_event =
NULL;
460 vcpu_record->start_time = 0;
462 BUG_ON(timestamp < time_begin);
464 time_diff = timestamp - time_begin;
465 return update_kvm_event(event, vcpu_record->vcpu_id, time_diff);
469 struct vcpu_event_record *per_vcpu_record(
struct thread *
thread,
474 if (!thread->
priv && kvm_entry_event(evsel)) {
475 struct vcpu_event_record *vcpu_record;
477 vcpu_record =
zalloc(
sizeof(*vcpu_record));
479 pr_err(
"%s: Not enough memory\n", __func__);
484 thread->
priv = vcpu_record;
490 static bool handle_kvm_event(
struct perf_kvm_stat *kvm,
491 struct thread *thread,
495 struct vcpu_event_record *vcpu_record;
496 struct event_key key = {.key = INVALID_KEY};
498 vcpu_record = per_vcpu_record(thread, evsel, sample);
502 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
503 return handle_begin_event(kvm, vcpu_record, &key, sample->
time);
505 if (kvm->events_ops->is_end_event(evsel, sample, &key))
506 return handle_end_event(kvm, vcpu_record, &key, sample->
time);
511 #define GET_EVENT_KEY(func, field) \
512 static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
515 return event->total.field; \
517 if (vcpu >= event->max_vcpu) \
520 return event->vcpu[vcpu].field; \
523 #define COMPARE_EVENT_KEY(func, field) \
524 GET_EVENT_KEY(func, field) \
525 static int compare_kvm_event_ ## func(struct kvm_event *one, \
526 struct kvm_event *two, int vcpu)\
528 return get_event_ ##func(one, vcpu) > \
529 get_event_ ##func(two, vcpu); \
536 #define DEF_SORT_NAME_KEY(name, compare_key) \
537 { #name, compare_kvm_event_ ## compare_key }
539 static struct kvm_event_key
keys[] = {
540 DEF_SORT_NAME_KEY(sample,
count),
541 DEF_SORT_NAME_KEY(
time, mean),
545 static bool select_key(
struct perf_kvm_stat *kvm)
549 for (i = 0; keys[
i].name; i++) {
551 kvm->compare = keys[
i].key;
556 pr_err(
"Unknown compare key:%s\n", kvm->sort_key);
560 static void insert_to_result(
struct rb_root *
result,
struct kvm_event *event,
561 key_cmp_fun bigger,
int vcpu)
571 if (bigger(event, p, vcpu))
577 rb_link_node(&event->rb, parent, rb);
582 update_total_count(
struct perf_kvm_stat *kvm,
struct kvm_event *event)
584 int vcpu = kvm->trace_vcpu;
586 kvm->total_count += get_event_count(event, vcpu);
587 kvm->total_time += get_event_time(event, vcpu);
590 static bool event_is_valid(
struct kvm_event *event,
int vcpu)
592 return !!get_event_count(event, vcpu);
595 static void sort_result(
struct perf_kvm_stat *kvm)
598 int vcpu = kvm->trace_vcpu;
599 struct kvm_event *
event;
601 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
603 if (event_is_valid(event, vcpu)) {
604 update_total_count(kvm, event);
605 insert_to_result(&kvm->result, event,
611 static struct kvm_event *pop_from_result(
struct rb_root *result)
622 static void print_vcpu_info(
int vcpu)
624 pr_info(
"Analyze events for ");
632 static void print_result(
struct perf_kvm_stat *kvm)
635 struct kvm_event *
event;
636 int vcpu = kvm->trace_vcpu;
639 print_vcpu_info(vcpu);
640 pr_info(
"%20s ", kvm->events_ops->name);
648 while ((event = pop_from_result(&kvm->result))) {
651 ecount = get_event_count(event, vcpu);
652 etime = get_event_time(event, vcpu);
654 kvm->events_ops->decode_key(kvm, &event->key, decode);
656 pr_info(
"%10llu ", (
unsigned long long)ecount);
657 pr_info(
"%8.2f%% ", (
double)ecount / kvm->total_count * 100);
658 pr_info(
"%8.2f%% ", (
double)etime / kvm->total_time * 100);
659 pr_info(
"%9.2fus ( +-%7.2f%% )", (
double)etime / ecount/1
e3,
660 kvm_event_rel_stddev(vcpu, event));
664 pr_info(
"\nTotal Samples:%lld, Total events handled time:%.2fus.\n\n",
665 (
unsigned long long)kvm->total_count, kvm->total_time / 1
e3);
668 static int process_sample_event(
struct perf_tool *tool,
675 struct perf_kvm_stat *kvm =
container_of(tool,
struct perf_kvm_stat,
678 if (thread == NULL) {
679 pr_debug(
"problem processing %d event, skipping it.\n",
684 if (!handle_kvm_event(kvm, thread, evsel, sample))
695 if (
strstr(cpuid,
"Intel"))
697 else if (
strstr(cpuid,
"AMD"))
700 pr_err(
"CPU %s is not supported.\n", cpuid);
707 static int read_events(
struct perf_kvm_stat *kvm)
712 .
sample = process_sample_event,
714 .ordered_samples =
true,
721 pr_err(
"Initializing perf session failed\n");
732 ret = get_cpu_isa(kvm->session);
738 kvm->exit_reasons = vmx_exit_reasons;
739 kvm->exit_reasons_size =
ARRAY_SIZE(vmx_exit_reasons);
740 kvm->exit_reasons_isa =
"VMX";
746 static bool verify_vcpu(
int vcpu)
748 if (vcpu != -1 && vcpu < 0) {
749 pr_err(
"Invalid vcpu:%d.\n", vcpu);
756 static int kvm_events_report_vcpu(
struct perf_kvm_stat *kvm)
759 int vcpu = kvm->trace_vcpu;
761 if (!verify_vcpu(vcpu))
764 if (!select_key(kvm))
767 if (!register_kvm_events_ops(kvm))
770 init_kvm_event_record(kvm);
773 ret = read_events(kvm);
784 static const char *
const record_args[] = {
790 "-e",
"kvm:kvm_entry",
791 "-e",
"kvm:kvm_exit",
792 "-e",
"kvm:kvm_mmio",
796 #define STRDUP_FAIL_EXIT(s) \
805 kvm_events_record(
struct perf_kvm_stat *kvm,
int argc,
const char **argv)
807 unsigned int rec_argc,
i,
j;
808 const char **rec_argv;
810 rec_argc =
ARRAY_SIZE(record_args) + argc + 2;
811 rec_argv = calloc(rec_argc + 1,
sizeof(
char *));
813 if (rec_argv == NULL)
817 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
819 rec_argv[i++] = STRDUP_FAIL_EXIT(
"-o");
820 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
822 for (j = 1; j < (
unsigned int)argc; j++, i++)
823 rec_argv[i] = argv[j];
829 kvm_events_report(
struct perf_kvm_stat *kvm,
int argc,
const char **argv)
831 const struct option kvm_events_report_options[] = {
832 OPT_STRING(0,
"event", &kvm->report_event,
"report event",
833 "event for reporting: vmexit, mmio, ioport"),
835 "vcpu id to report"),
836 OPT_STRING(
'k',
"key", &kvm->sort_key,
"sort-key",
837 "key for sorting: sample(sort by samples number)"
838 " time (sort by avg time)"),
842 const char *
const kvm_events_report_usage[] = {
843 "perf kvm stat report [<options>]",
851 kvm_events_report_options,
852 kvm_events_report_usage, 0);
855 kvm_events_report_options);
858 return kvm_events_report_vcpu(kvm);
861 static void print_kvm_stat_usage(
void)
863 printf(
"Usage: perf kvm stat <command>\n\n");
865 printf(
"# Available commands:\n");
866 printf(
"\trecord: record kvm events\n");
867 printf(
"\treport: report statistical data of kvm events\n");
869 printf(
"\nOtherwise, it is the alias of 'perf stat':\n");
872 static int kvm_cmd_stat(
const char *
file_name,
int argc,
const char **argv)
874 struct perf_kvm_stat kvm = {
878 .report_event =
"vmexit",
879 .sort_key =
"sample",
881 .exit_reasons = svm_exit_reasons,
882 .exit_reasons_size =
ARRAY_SIZE(svm_exit_reasons),
883 .exit_reasons_isa =
"SVM",
887 print_kvm_stat_usage();
891 if (!
strncmp(argv[1],
"rec", 3))
892 return kvm_events_record(&kvm, argc - 1, argv + 1);
894 if (!
strncmp(argv[1],
"rep", 3))
895 return kvm_events_report(&kvm, argc - 1 , argv + 1);
902 static int __cmd_record(
const char *file_name,
int argc,
const char **argv)
904 int rec_argc, i = 0,
j;
905 const char **rec_argv;
908 rec_argv = calloc(rec_argc + 1,
sizeof(
char *));
909 rec_argv[i++] = strdup(
"record");
910 rec_argv[i++] = strdup(
"-o");
911 rec_argv[i++] = strdup(file_name);
912 for (j = 1; j <
argc; j++, i++)
913 rec_argv[i] = argv[j];
920 static int __cmd_report(
const char *file_name,
int argc,
const char **argv)
922 int rec_argc, i = 0,
j;
923 const char **rec_argv;
926 rec_argv = calloc(rec_argc + 1,
sizeof(
char *));
927 rec_argv[i++] = strdup(
"report");
928 rec_argv[i++] = strdup(
"-i");
929 rec_argv[i++] = strdup(file_name);
930 for (j = 1; j <
argc; j++, i++)
931 rec_argv[i] = argv[j];
939 __cmd_buildid_list(
const char *file_name,
int argc,
const char **argv)
941 int rec_argc, i = 0,
j;
942 const char **rec_argv;
945 rec_argv = calloc(rec_argc + 1,
sizeof(
char *));
946 rec_argv[i++] = strdup(
"buildid-list");
947 rec_argv[i++] = strdup(
"-i");
948 rec_argv[i++] = strdup(file_name);
949 for (j = 1; j <
argc; j++, i++)
950 rec_argv[i] = argv[j];
961 const struct option kvm_options[] = {
967 "Collect guest os data"),
969 "Collect host os data"),
971 "guest mount directory under which every guest os"
972 " instance has a subdir"),
974 "file",
"file saving guest os vmlinux"),
976 "file",
"file saving guest os /proc/kallsyms"),
978 "file",
"file saving guest os /proc/modules"),
983 const char *
const kvm_usage[] = {
984 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
1001 file_name = strdup(
"perf.data.host");
1003 file_name = strdup(
"perf.data.guest");
1005 file_name = strdup(
"perf.data.kvm");
1008 pr_err(
"Failed to allocate memory for filename\n");
1013 if (!
strncmp(argv[0],
"rec", 3))
1014 return __cmd_record(file_name, argc, argv);
1015 else if (!
strncmp(argv[0],
"rep", 3))
1016 return __cmd_report(file_name, argc, argv);
1017 else if (!
strncmp(argv[0],
"diff", 4))
1019 else if (!
strncmp(argv[0],
"top", 3))
1020 return cmd_top(argc, argv, NULL);
1021 else if (!
strncmp(argv[0],
"buildid-list", 12))
1022 return __cmd_buildid_list(file_name, argc, argv);
1023 #if defined(__i386__) || defined(__x86_64__)
1024 else if (!
strncmp(argv[0],
"stat", 4))
1025 return kvm_cmd_stat(file_name, argc, argv);