aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-kvm.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/builtin-kvm.c')
-rw-r--r--tools/perf/builtin-kvm.c121
1 files changed, 67 insertions, 54 deletions
diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index 260abc535b5b..283b4397e397 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -22,9 +22,10 @@
22#include <pthread.h> 22#include <pthread.h>
23#include <math.h> 23#include <math.h>
24 24
25#include "../../arch/x86/include/asm/svm.h" 25#if defined(__i386__) || defined(__x86_64__)
26#include "../../arch/x86/include/asm/vmx.h" 26#include <asm/svm.h>
27#include "../../arch/x86/include/asm/kvm.h" 27#include <asm/vmx.h>
28#include <asm/kvm.h>
28 29
29struct event_key { 30struct event_key {
30 #define INVALID_KEY (~0ULL) 31 #define INVALID_KEY (~0ULL)
@@ -58,7 +59,7 @@ struct kvm_event_key {
58}; 59};
59 60
60 61
61struct perf_kvm; 62struct perf_kvm_stat;
62 63
63struct kvm_events_ops { 64struct kvm_events_ops {
64 bool (*is_begin_event)(struct perf_evsel *evsel, 65 bool (*is_begin_event)(struct perf_evsel *evsel,
@@ -66,7 +67,7 @@ struct kvm_events_ops {
66 struct event_key *key); 67 struct event_key *key);
67 bool (*is_end_event)(struct perf_evsel *evsel, 68 bool (*is_end_event)(struct perf_evsel *evsel,
68 struct perf_sample *sample, struct event_key *key); 69 struct perf_sample *sample, struct event_key *key);
69 void (*decode_key)(struct perf_kvm *kvm, struct event_key *key, 70 void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
70 char decode[20]); 71 char decode[20]);
71 const char *name; 72 const char *name;
72}; 73};
@@ -79,7 +80,7 @@ struct exit_reasons_table {
79#define EVENTS_BITS 12 80#define EVENTS_BITS 12
80#define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS) 81#define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
81 82
82struct perf_kvm { 83struct perf_kvm_stat {
83 struct perf_tool tool; 84 struct perf_tool tool;
84 struct perf_session *session; 85 struct perf_session *session;
85 86
@@ -146,7 +147,7 @@ static struct exit_reasons_table svm_exit_reasons[] = {
146 SVM_EXIT_REASONS 147 SVM_EXIT_REASONS
147}; 148};
148 149
149static const char *get_exit_reason(struct perf_kvm *kvm, u64 exit_code) 150static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
150{ 151{
151 int i = kvm->exit_reasons_size; 152 int i = kvm->exit_reasons_size;
152 struct exit_reasons_table *tbl = kvm->exit_reasons; 153 struct exit_reasons_table *tbl = kvm->exit_reasons;
@@ -162,7 +163,7 @@ static const char *get_exit_reason(struct perf_kvm *kvm, u64 exit_code)
162 return "UNKNOWN"; 163 return "UNKNOWN";
163} 164}
164 165
165static void exit_event_decode_key(struct perf_kvm *kvm, 166static void exit_event_decode_key(struct perf_kvm_stat *kvm,
166 struct event_key *key, 167 struct event_key *key,
167 char decode[20]) 168 char decode[20])
168{ 169{
@@ -228,7 +229,7 @@ static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
228 return false; 229 return false;
229} 230}
230 231
231static void mmio_event_decode_key(struct perf_kvm *kvm __maybe_unused, 232static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
232 struct event_key *key, 233 struct event_key *key,
233 char decode[20]) 234 char decode[20])
234{ 235{
@@ -271,7 +272,7 @@ static bool ioport_event_end(struct perf_evsel *evsel,
271 return kvm_entry_event(evsel); 272 return kvm_entry_event(evsel);
272} 273}
273 274
274static void ioport_event_decode_key(struct perf_kvm *kvm __maybe_unused, 275static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
275 struct event_key *key, 276 struct event_key *key,
276 char decode[20]) 277 char decode[20])
277{ 278{
@@ -286,7 +287,7 @@ static struct kvm_events_ops ioport_events = {
286 .name = "IO Port Access" 287 .name = "IO Port Access"
287}; 288};
288 289
289static bool register_kvm_events_ops(struct perf_kvm *kvm) 290static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
290{ 291{
291 bool ret = true; 292 bool ret = true;
292 293
@@ -311,7 +312,7 @@ struct vcpu_event_record {
311}; 312};
312 313
313 314
314static void init_kvm_event_record(struct perf_kvm *kvm) 315static void init_kvm_event_record(struct perf_kvm_stat *kvm)
315{ 316{
316 int i; 317 int i;
317 318
@@ -360,7 +361,7 @@ static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
360 return event; 361 return event;
361} 362}
362 363
363static struct kvm_event *find_create_kvm_event(struct perf_kvm *kvm, 364static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
364 struct event_key *key) 365 struct event_key *key)
365{ 366{
366 struct kvm_event *event; 367 struct kvm_event *event;
@@ -381,7 +382,7 @@ static struct kvm_event *find_create_kvm_event(struct perf_kvm *kvm,
381 return event; 382 return event;
382} 383}
383 384
384static bool handle_begin_event(struct perf_kvm *kvm, 385static bool handle_begin_event(struct perf_kvm_stat *kvm,
385 struct vcpu_event_record *vcpu_record, 386 struct vcpu_event_record *vcpu_record,
386 struct event_key *key, u64 timestamp) 387 struct event_key *key, u64 timestamp)
387{ 388{
@@ -425,7 +426,7 @@ static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
425 return true; 426 return true;
426} 427}
427 428
428static bool handle_end_event(struct perf_kvm *kvm, 429static bool handle_end_event(struct perf_kvm_stat *kvm,
429 struct vcpu_event_record *vcpu_record, 430 struct vcpu_event_record *vcpu_record,
430 struct event_key *key, 431 struct event_key *key,
431 u64 timestamp) 432 u64 timestamp)
@@ -486,7 +487,7 @@ struct vcpu_event_record *per_vcpu_record(struct thread *thread,
486 return thread->priv; 487 return thread->priv;
487} 488}
488 489
489static bool handle_kvm_event(struct perf_kvm *kvm, 490static bool handle_kvm_event(struct perf_kvm_stat *kvm,
490 struct thread *thread, 491 struct thread *thread,
491 struct perf_evsel *evsel, 492 struct perf_evsel *evsel,
492 struct perf_sample *sample) 493 struct perf_sample *sample)
@@ -541,7 +542,7 @@ static struct kvm_event_key keys[] = {
541 { NULL, NULL } 542 { NULL, NULL }
542}; 543};
543 544
544static bool select_key(struct perf_kvm *kvm) 545static bool select_key(struct perf_kvm_stat *kvm)
545{ 546{
546 int i; 547 int i;
547 548
@@ -577,7 +578,8 @@ static void insert_to_result(struct rb_root *result, struct kvm_event *event,
577 rb_insert_color(&event->rb, result); 578 rb_insert_color(&event->rb, result);
578} 579}
579 580
580static void update_total_count(struct perf_kvm *kvm, struct kvm_event *event) 581static void
582update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
581{ 583{
582 int vcpu = kvm->trace_vcpu; 584 int vcpu = kvm->trace_vcpu;
583 585
@@ -590,7 +592,7 @@ static bool event_is_valid(struct kvm_event *event, int vcpu)
590 return !!get_event_count(event, vcpu); 592 return !!get_event_count(event, vcpu);
591} 593}
592 594
593static void sort_result(struct perf_kvm *kvm) 595static void sort_result(struct perf_kvm_stat *kvm)
594{ 596{
595 unsigned int i; 597 unsigned int i;
596 int vcpu = kvm->trace_vcpu; 598 int vcpu = kvm->trace_vcpu;
@@ -627,7 +629,7 @@ static void print_vcpu_info(int vcpu)
627 pr_info("VCPU %d:\n\n", vcpu); 629 pr_info("VCPU %d:\n\n", vcpu);
628} 630}
629 631
630static void print_result(struct perf_kvm *kvm) 632static void print_result(struct perf_kvm_stat *kvm)
631{ 633{
632 char decode[20]; 634 char decode[20];
633 struct kvm_event *event; 635 struct kvm_event *event;
@@ -670,7 +672,8 @@ static int process_sample_event(struct perf_tool *tool,
670 struct machine *machine) 672 struct machine *machine)
671{ 673{
672 struct thread *thread = machine__findnew_thread(machine, sample->tid); 674 struct thread *thread = machine__findnew_thread(machine, sample->tid);
673 struct perf_kvm *kvm = container_of(tool, struct perf_kvm, tool); 675 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
676 tool);
674 677
675 if (thread == NULL) { 678 if (thread == NULL) {
676 pr_debug("problem processing %d event, skipping it.\n", 679 pr_debug("problem processing %d event, skipping it.\n",
@@ -701,7 +704,7 @@ static int get_cpu_isa(struct perf_session *session)
701 return isa; 704 return isa;
702} 705}
703 706
704static int read_events(struct perf_kvm *kvm) 707static int read_events(struct perf_kvm_stat *kvm)
705{ 708{
706 int ret; 709 int ret;
707 710
@@ -750,7 +753,7 @@ static bool verify_vcpu(int vcpu)
750 return true; 753 return true;
751} 754}
752 755
753static int kvm_events_report_vcpu(struct perf_kvm *kvm) 756static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
754{ 757{
755 int ret = -EINVAL; 758 int ret = -EINVAL;
756 int vcpu = kvm->trace_vcpu; 759 int vcpu = kvm->trace_vcpu;
@@ -798,7 +801,8 @@ static const char * const record_args[] = {
798 _p; \ 801 _p; \
799 }) 802 })
800 803
801static int kvm_events_record(struct perf_kvm *kvm, int argc, const char **argv) 804static int
805kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
802{ 806{
803 unsigned int rec_argc, i, j; 807 unsigned int rec_argc, i, j;
804 const char **rec_argv; 808 const char **rec_argv;
@@ -821,7 +825,8 @@ static int kvm_events_record(struct perf_kvm *kvm, int argc, const char **argv)
821 return cmd_record(i, rec_argv, NULL); 825 return cmd_record(i, rec_argv, NULL);
822} 826}
823 827
824static int kvm_events_report(struct perf_kvm *kvm, int argc, const char **argv) 828static int
829kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
825{ 830{
826 const struct option kvm_events_report_options[] = { 831 const struct option kvm_events_report_options[] = {
827 OPT_STRING(0, "event", &kvm->report_event, "report event", 832 OPT_STRING(0, "event", &kvm->report_event, "report event",
@@ -864,24 +869,37 @@ static void print_kvm_stat_usage(void)
864 printf("\nOtherwise, it is the alias of 'perf stat':\n"); 869 printf("\nOtherwise, it is the alias of 'perf stat':\n");
865} 870}
866 871
867static int kvm_cmd_stat(struct perf_kvm *kvm, int argc, const char **argv) 872static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
868{ 873{
874 struct perf_kvm_stat kvm = {
875 .file_name = file_name,
876
877 .trace_vcpu = -1,
878 .report_event = "vmexit",
879 .sort_key = "sample",
880
881 .exit_reasons = svm_exit_reasons,
882 .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
883 .exit_reasons_isa = "SVM",
884 };
885
869 if (argc == 1) { 886 if (argc == 1) {
870 print_kvm_stat_usage(); 887 print_kvm_stat_usage();
871 goto perf_stat; 888 goto perf_stat;
872 } 889 }
873 890
874 if (!strncmp(argv[1], "rec", 3)) 891 if (!strncmp(argv[1], "rec", 3))
875 return kvm_events_record(kvm, argc - 1, argv + 1); 892 return kvm_events_record(&kvm, argc - 1, argv + 1);
876 893
877 if (!strncmp(argv[1], "rep", 3)) 894 if (!strncmp(argv[1], "rep", 3))
878 return kvm_events_report(kvm, argc - 1 , argv + 1); 895 return kvm_events_report(&kvm, argc - 1 , argv + 1);
879 896
880perf_stat: 897perf_stat:
881 return cmd_stat(argc, argv, NULL); 898 return cmd_stat(argc, argv, NULL);
882} 899}
900#endif
883 901
884static int __cmd_record(struct perf_kvm *kvm, int argc, const char **argv) 902static int __cmd_record(const char *file_name, int argc, const char **argv)
885{ 903{
886 int rec_argc, i = 0, j; 904 int rec_argc, i = 0, j;
887 const char **rec_argv; 905 const char **rec_argv;
@@ -890,7 +908,7 @@ static int __cmd_record(struct perf_kvm *kvm, int argc, const char **argv)
890 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 908 rec_argv = calloc(rec_argc + 1, sizeof(char *));
891 rec_argv[i++] = strdup("record"); 909 rec_argv[i++] = strdup("record");
892 rec_argv[i++] = strdup("-o"); 910 rec_argv[i++] = strdup("-o");
893 rec_argv[i++] = strdup(kvm->file_name); 911 rec_argv[i++] = strdup(file_name);
894 for (j = 1; j < argc; j++, i++) 912 for (j = 1; j < argc; j++, i++)
895 rec_argv[i] = argv[j]; 913 rec_argv[i] = argv[j];
896 914
@@ -899,7 +917,7 @@ static int __cmd_record(struct perf_kvm *kvm, int argc, const char **argv)
899 return cmd_record(i, rec_argv, NULL); 917 return cmd_record(i, rec_argv, NULL);
900} 918}
901 919
902static int __cmd_report(struct perf_kvm *kvm, int argc, const char **argv) 920static int __cmd_report(const char *file_name, int argc, const char **argv)
903{ 921{
904 int rec_argc, i = 0, j; 922 int rec_argc, i = 0, j;
905 const char **rec_argv; 923 const char **rec_argv;
@@ -908,7 +926,7 @@ static int __cmd_report(struct perf_kvm *kvm, int argc, const char **argv)
908 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 926 rec_argv = calloc(rec_argc + 1, sizeof(char *));
909 rec_argv[i++] = strdup("report"); 927 rec_argv[i++] = strdup("report");
910 rec_argv[i++] = strdup("-i"); 928 rec_argv[i++] = strdup("-i");
911 rec_argv[i++] = strdup(kvm->file_name); 929 rec_argv[i++] = strdup(file_name);
912 for (j = 1; j < argc; j++, i++) 930 for (j = 1; j < argc; j++, i++)
913 rec_argv[i] = argv[j]; 931 rec_argv[i] = argv[j];
914 932
@@ -917,7 +935,8 @@ static int __cmd_report(struct perf_kvm *kvm, int argc, const char **argv)
917 return cmd_report(i, rec_argv, NULL); 935 return cmd_report(i, rec_argv, NULL);
918} 936}
919 937
920static int __cmd_buildid_list(struct perf_kvm *kvm, int argc, const char **argv) 938static int
939__cmd_buildid_list(const char *file_name, int argc, const char **argv)
921{ 940{
922 int rec_argc, i = 0, j; 941 int rec_argc, i = 0, j;
923 const char **rec_argv; 942 const char **rec_argv;
@@ -926,7 +945,7 @@ static int __cmd_buildid_list(struct perf_kvm *kvm, int argc, const char **argv)
926 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 945 rec_argv = calloc(rec_argc + 1, sizeof(char *));
927 rec_argv[i++] = strdup("buildid-list"); 946 rec_argv[i++] = strdup("buildid-list");
928 rec_argv[i++] = strdup("-i"); 947 rec_argv[i++] = strdup("-i");
929 rec_argv[i++] = strdup(kvm->file_name); 948 rec_argv[i++] = strdup(file_name);
930 for (j = 1; j < argc; j++, i++) 949 for (j = 1; j < argc; j++, i++)
931 rec_argv[i] = argv[j]; 950 rec_argv[i] = argv[j];
932 951
@@ -937,20 +956,12 @@ static int __cmd_buildid_list(struct perf_kvm *kvm, int argc, const char **argv)
937 956
938int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused) 957int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
939{ 958{
940 struct perf_kvm kvm = { 959 const char *file_name;
941 .trace_vcpu = -1,
942 .report_event = "vmexit",
943 .sort_key = "sample",
944
945 .exit_reasons = svm_exit_reasons,
946 .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
947 .exit_reasons_isa = "SVM",
948 };
949 960
950 const struct option kvm_options[] = { 961 const struct option kvm_options[] = {
951 OPT_STRING('i', "input", &kvm.file_name, "file", 962 OPT_STRING('i', "input", &file_name, "file",
952 "Input file name"), 963 "Input file name"),
953 OPT_STRING('o', "output", &kvm.file_name, "file", 964 OPT_STRING('o', "output", &file_name, "file",
954 "Output file name"), 965 "Output file name"),
955 OPT_BOOLEAN(0, "guest", &perf_guest, 966 OPT_BOOLEAN(0, "guest", &perf_guest,
956 "Collect guest os data"), 967 "Collect guest os data"),
@@ -985,32 +996,34 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
985 if (!perf_host) 996 if (!perf_host)
986 perf_guest = 1; 997 perf_guest = 1;
987 998
988 if (!kvm.file_name) { 999 if (!file_name) {
989 if (perf_host && !perf_guest) 1000 if (perf_host && !perf_guest)
990 kvm.file_name = strdup("perf.data.host"); 1001 file_name = strdup("perf.data.host");
991 else if (!perf_host && perf_guest) 1002 else if (!perf_host && perf_guest)
992 kvm.file_name = strdup("perf.data.guest"); 1003 file_name = strdup("perf.data.guest");
993 else 1004 else
994 kvm.file_name = strdup("perf.data.kvm"); 1005 file_name = strdup("perf.data.kvm");
995 1006
996 if (!kvm.file_name) { 1007 if (!file_name) {
997 pr_err("Failed to allocate memory for filename\n"); 1008 pr_err("Failed to allocate memory for filename\n");
998 return -ENOMEM; 1009 return -ENOMEM;
999 } 1010 }
1000 } 1011 }
1001 1012
1002 if (!strncmp(argv[0], "rec", 3)) 1013 if (!strncmp(argv[0], "rec", 3))
1003 return __cmd_record(&kvm, argc, argv); 1014 return __cmd_record(file_name, argc, argv);
1004 else if (!strncmp(argv[0], "rep", 3)) 1015 else if (!strncmp(argv[0], "rep", 3))
1005 return __cmd_report(&kvm, argc, argv); 1016 return __cmd_report(file_name, argc, argv);
1006 else if (!strncmp(argv[0], "diff", 4)) 1017 else if (!strncmp(argv[0], "diff", 4))
1007 return cmd_diff(argc, argv, NULL); 1018 return cmd_diff(argc, argv, NULL);
1008 else if (!strncmp(argv[0], "top", 3)) 1019 else if (!strncmp(argv[0], "top", 3))
1009 return cmd_top(argc, argv, NULL); 1020 return cmd_top(argc, argv, NULL);
1010 else if (!strncmp(argv[0], "buildid-list", 12)) 1021 else if (!strncmp(argv[0], "buildid-list", 12))
1011 return __cmd_buildid_list(&kvm, argc, argv); 1022 return __cmd_buildid_list(file_name, argc, argv);
1023#if defined(__i386__) || defined(__x86_64__)
1012 else if (!strncmp(argv[0], "stat", 4)) 1024 else if (!strncmp(argv[0], "stat", 4))
1013 return kvm_cmd_stat(&kvm, argc, argv); 1025 return kvm_cmd_stat(file_name, argc, argv);
1026#endif
1014 else 1027 else
1015 usage_with_options(kvm_usage, kvm_options); 1028 usage_with_options(kvm_usage, kvm_options);
1016 1029