Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "perf.h" |
| 3 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 4 | #include "util/evsel.h" |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 5 | #include "util/evlist.h" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 6 | #include "util/util.h" |
| 7 | #include "util/cache.h" |
| 8 | #include "util/symbol.h" |
| 9 | #include "util/thread.h" |
| 10 | #include "util/header.h" |
| 11 | #include "util/session.h" |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 12 | #include "util/intlist.h" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 13 | #include "util/parse-options.h" |
| 14 | #include "util/trace-event.h" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 15 | #include "util/debug.h" |
Borislav Petkov | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 16 | #include <lk/debugfs.h> |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 17 | #include "util/tool.h" |
| 18 | #include "util/stat.h" |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 19 | #include "util/top.h" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 20 | |
| 21 | #include <sys/prctl.h> |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 22 | #include <sys/timerfd.h> |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 23 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 24 | #include <termios.h> |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 25 | #include <semaphore.h> |
| 26 | #include <pthread.h> |
| 27 | #include <math.h> |
| 28 | |
Xiao Guangrong | 7321090 | 2012-11-19 16:19:21 +0800 | [diff] [blame] | 29 | #if defined(__i386__) || defined(__x86_64__) |
David Howells | d2709c7 | 2012-11-19 22:21:03 +0000 | [diff] [blame] | 30 | #include <asm/svm.h> |
| 31 | #include <asm/vmx.h> |
| 32 | #include <asm/kvm.h> |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 33 | |
| 34 | struct event_key { |
| 35 | #define INVALID_KEY (~0ULL) |
| 36 | u64 key; |
| 37 | int info; |
| 38 | }; |
| 39 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 40 | struct kvm_event_stats { |
| 41 | u64 time; |
| 42 | struct stats stats; |
| 43 | }; |
| 44 | |
| 45 | struct kvm_event { |
| 46 | struct list_head hash_entry; |
| 47 | struct rb_node rb; |
| 48 | |
| 49 | struct event_key key; |
| 50 | |
| 51 | struct kvm_event_stats total; |
| 52 | |
| 53 | #define DEFAULT_VCPU_NUM 8 |
| 54 | int max_vcpu; |
| 55 | struct kvm_event_stats *vcpu; |
| 56 | }; |
| 57 | |
| 58 | typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int); |
| 59 | |
| 60 | struct kvm_event_key { |
| 61 | const char *name; |
| 62 | key_cmp_fun key; |
| 63 | }; |
| 64 | |
| 65 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 66 | struct perf_kvm_stat; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 67 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 68 | struct kvm_events_ops { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 69 | bool (*is_begin_event)(struct perf_evsel *evsel, |
| 70 | struct perf_sample *sample, |
| 71 | struct event_key *key); |
| 72 | bool (*is_end_event)(struct perf_evsel *evsel, |
| 73 | struct perf_sample *sample, struct event_key *key); |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 74 | void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 75 | char decode[20]); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 76 | const char *name; |
| 77 | }; |
| 78 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 79 | struct exit_reasons_table { |
| 80 | unsigned long exit_code; |
| 81 | const char *reason; |
| 82 | }; |
| 83 | |
| 84 | #define EVENTS_BITS 12 |
| 85 | #define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS) |
| 86 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 87 | struct perf_kvm_stat { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 88 | struct perf_tool tool; |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 89 | struct perf_record_opts opts; |
| 90 | struct perf_evlist *evlist; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 91 | struct perf_session *session; |
| 92 | |
| 93 | const char *file_name; |
| 94 | const char *report_event; |
| 95 | const char *sort_key; |
| 96 | int trace_vcpu; |
| 97 | |
| 98 | struct exit_reasons_table *exit_reasons; |
| 99 | int exit_reasons_size; |
| 100 | const char *exit_reasons_isa; |
| 101 | |
| 102 | struct kvm_events_ops *events_ops; |
| 103 | key_cmp_fun compare; |
| 104 | struct list_head kvm_events_cache[EVENTS_CACHE_SIZE]; |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 105 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 106 | u64 total_time; |
| 107 | u64 total_count; |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 108 | u64 lost_events; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 109 | |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 110 | const char *pid_str; |
| 111 | struct intlist *pid_list; |
| 112 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 113 | struct rb_root result; |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 114 | |
| 115 | int timerfd; |
| 116 | unsigned int display_time; |
| 117 | bool live; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 121 | static void exit_event_get_key(struct perf_evsel *evsel, |
| 122 | struct perf_sample *sample, |
| 123 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 124 | { |
| 125 | key->info = 0; |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 126 | key->key = perf_evsel__intval(evsel, sample, "exit_reason"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 129 | static bool kvm_exit_event(struct perf_evsel *evsel) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 130 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 131 | return !strcmp(evsel->name, "kvm:kvm_exit"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 134 | static bool exit_event_begin(struct perf_evsel *evsel, |
| 135 | struct perf_sample *sample, struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 136 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 137 | if (kvm_exit_event(evsel)) { |
| 138 | exit_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 145 | static bool kvm_entry_event(struct perf_evsel *evsel) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 146 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 147 | return !strcmp(evsel->name, "kvm:kvm_entry"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 148 | } |
| 149 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 150 | static bool exit_event_end(struct perf_evsel *evsel, |
| 151 | struct perf_sample *sample __maybe_unused, |
| 152 | struct event_key *key __maybe_unused) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 153 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 154 | return kvm_entry_event(evsel); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 155 | } |
| 156 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 157 | static struct exit_reasons_table vmx_exit_reasons[] = { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 158 | VMX_EXIT_REASONS |
| 159 | }; |
| 160 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 161 | static struct exit_reasons_table svm_exit_reasons[] = { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 162 | SVM_EXIT_REASONS |
| 163 | }; |
| 164 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 165 | static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 166 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 167 | int i = kvm->exit_reasons_size; |
| 168 | struct exit_reasons_table *tbl = kvm->exit_reasons; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 169 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 170 | while (i--) { |
| 171 | if (tbl->exit_code == exit_code) |
| 172 | return tbl->reason; |
| 173 | tbl++; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | pr_err("unknown kvm exit code:%lld on %s\n", |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 177 | (unsigned long long)exit_code, kvm->exit_reasons_isa); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 178 | return "UNKNOWN"; |
| 179 | } |
| 180 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 181 | static void exit_event_decode_key(struct perf_kvm_stat *kvm, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 182 | struct event_key *key, |
| 183 | char decode[20]) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 184 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 185 | const char *exit_reason = get_exit_reason(kvm, key->key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 186 | |
| 187 | scnprintf(decode, 20, "%s", exit_reason); |
| 188 | } |
| 189 | |
| 190 | static struct kvm_events_ops exit_events = { |
| 191 | .is_begin_event = exit_event_begin, |
| 192 | .is_end_event = exit_event_end, |
| 193 | .decode_key = exit_event_decode_key, |
| 194 | .name = "VM-EXIT" |
| 195 | }; |
| 196 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 197 | /* |
| 198 | * For the mmio events, we treat: |
| 199 | * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry |
| 200 | * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...). |
| 201 | */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 202 | static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample, |
| 203 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 204 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 205 | key->key = perf_evsel__intval(evsel, sample, "gpa"); |
| 206 | key->info = perf_evsel__intval(evsel, sample, "type"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | #define KVM_TRACE_MMIO_READ_UNSATISFIED 0 |
| 210 | #define KVM_TRACE_MMIO_READ 1 |
| 211 | #define KVM_TRACE_MMIO_WRITE 2 |
| 212 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 213 | static bool mmio_event_begin(struct perf_evsel *evsel, |
| 214 | struct perf_sample *sample, struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 215 | { |
| 216 | /* MMIO read begin event in kernel. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 217 | if (kvm_exit_event(evsel)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 218 | return true; |
| 219 | |
| 220 | /* MMIO write begin event in kernel. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 221 | if (!strcmp(evsel->name, "kvm:kvm_mmio") && |
| 222 | perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) { |
| 223 | mmio_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 230 | static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample, |
| 231 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 232 | { |
| 233 | /* MMIO write end event in kernel. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 234 | if (kvm_entry_event(evsel)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 235 | return true; |
| 236 | |
| 237 | /* MMIO read end event in kernel.*/ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 238 | if (!strcmp(evsel->name, "kvm:kvm_mmio") && |
| 239 | perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) { |
| 240 | mmio_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 247 | static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 248 | struct event_key *key, |
| 249 | char decode[20]) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 250 | { |
| 251 | scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key, |
| 252 | key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R"); |
| 253 | } |
| 254 | |
| 255 | static struct kvm_events_ops mmio_events = { |
| 256 | .is_begin_event = mmio_event_begin, |
| 257 | .is_end_event = mmio_event_end, |
| 258 | .decode_key = mmio_event_decode_key, |
| 259 | .name = "MMIO Access" |
| 260 | }; |
| 261 | |
| 262 | /* The time of emulation pio access is from kvm_pio to kvm_entry. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 263 | static void ioport_event_get_key(struct perf_evsel *evsel, |
| 264 | struct perf_sample *sample, |
| 265 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 266 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 267 | key->key = perf_evsel__intval(evsel, sample, "port"); |
| 268 | key->info = perf_evsel__intval(evsel, sample, "rw"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 269 | } |
| 270 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 271 | static bool ioport_event_begin(struct perf_evsel *evsel, |
| 272 | struct perf_sample *sample, |
| 273 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 274 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 275 | if (!strcmp(evsel->name, "kvm:kvm_pio")) { |
| 276 | ioport_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 283 | static bool ioport_event_end(struct perf_evsel *evsel, |
| 284 | struct perf_sample *sample __maybe_unused, |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 285 | struct event_key *key __maybe_unused) |
| 286 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 287 | return kvm_entry_event(evsel); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 290 | static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 291 | struct event_key *key, |
| 292 | char decode[20]) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 293 | { |
| 294 | scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key, |
| 295 | key->info ? "POUT" : "PIN"); |
| 296 | } |
| 297 | |
| 298 | static struct kvm_events_ops ioport_events = { |
| 299 | .is_begin_event = ioport_event_begin, |
| 300 | .is_end_event = ioport_event_end, |
| 301 | .decode_key = ioport_event_decode_key, |
| 302 | .name = "IO Port Access" |
| 303 | }; |
| 304 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 305 | static bool register_kvm_events_ops(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 306 | { |
| 307 | bool ret = true; |
| 308 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 309 | if (!strcmp(kvm->report_event, "vmexit")) |
| 310 | kvm->events_ops = &exit_events; |
| 311 | else if (!strcmp(kvm->report_event, "mmio")) |
| 312 | kvm->events_ops = &mmio_events; |
| 313 | else if (!strcmp(kvm->report_event, "ioport")) |
| 314 | kvm->events_ops = &ioport_events; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 315 | else { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 316 | pr_err("Unknown report event:%s\n", kvm->report_event); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 317 | ret = false; |
| 318 | } |
| 319 | |
| 320 | return ret; |
| 321 | } |
| 322 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 323 | struct vcpu_event_record { |
| 324 | int vcpu_id; |
| 325 | u64 start_time; |
| 326 | struct kvm_event *last_event; |
| 327 | }; |
| 328 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 329 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 330 | static void init_kvm_event_record(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 331 | { |
David Ahern | b880dee | 2012-10-08 11:17:30 -0600 | [diff] [blame] | 332 | unsigned int i; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 333 | |
David Ahern | b880dee | 2012-10-08 11:17:30 -0600 | [diff] [blame] | 334 | for (i = 0; i < EVENTS_CACHE_SIZE; i++) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 335 | INIT_LIST_HEAD(&kvm->kvm_events_cache[i]); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 336 | } |
| 337 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 338 | static void clear_events_cache_stats(struct list_head *kvm_events_cache) |
| 339 | { |
| 340 | struct list_head *head; |
| 341 | struct kvm_event *event; |
| 342 | unsigned int i; |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 343 | int j; |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 344 | |
| 345 | for (i = 0; i < EVENTS_CACHE_SIZE; i++) { |
| 346 | head = &kvm_events_cache[i]; |
| 347 | list_for_each_entry(event, head, hash_entry) { |
| 348 | /* reset stats for event */ |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 349 | event->total.time = 0; |
| 350 | init_stats(&event->total.stats); |
| 351 | |
| 352 | for (j = 0; j < event->max_vcpu; ++j) { |
| 353 | event->vcpu[j].time = 0; |
| 354 | init_stats(&event->vcpu[j].stats); |
| 355 | } |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 360 | static int kvm_events_hash_fn(u64 key) |
| 361 | { |
| 362 | return key & (EVENTS_CACHE_SIZE - 1); |
| 363 | } |
| 364 | |
| 365 | static bool kvm_event_expand(struct kvm_event *event, int vcpu_id) |
| 366 | { |
| 367 | int old_max_vcpu = event->max_vcpu; |
David Ahern | 6ca5f30 | 2013-05-25 18:24:46 -0600 | [diff] [blame] | 368 | void *prev; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 369 | |
| 370 | if (vcpu_id < event->max_vcpu) |
| 371 | return true; |
| 372 | |
| 373 | while (event->max_vcpu <= vcpu_id) |
| 374 | event->max_vcpu += DEFAULT_VCPU_NUM; |
| 375 | |
David Ahern | 6ca5f30 | 2013-05-25 18:24:46 -0600 | [diff] [blame] | 376 | prev = event->vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 377 | event->vcpu = realloc(event->vcpu, |
| 378 | event->max_vcpu * sizeof(*event->vcpu)); |
| 379 | if (!event->vcpu) { |
David Ahern | 6ca5f30 | 2013-05-25 18:24:46 -0600 | [diff] [blame] | 380 | free(prev); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 381 | pr_err("Not enough memory\n"); |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | memset(event->vcpu + old_max_vcpu, 0, |
| 386 | (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu)); |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | static struct kvm_event *kvm_alloc_init_event(struct event_key *key) |
| 391 | { |
| 392 | struct kvm_event *event; |
| 393 | |
| 394 | event = zalloc(sizeof(*event)); |
| 395 | if (!event) { |
| 396 | pr_err("Not enough memory\n"); |
| 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | event->key = *key; |
| 401 | return event; |
| 402 | } |
| 403 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 404 | static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 405 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 406 | { |
| 407 | struct kvm_event *event; |
| 408 | struct list_head *head; |
| 409 | |
| 410 | BUG_ON(key->key == INVALID_KEY); |
| 411 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 412 | head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)]; |
David Ahern | 355afe8 | 2012-10-08 11:17:32 -0600 | [diff] [blame] | 413 | list_for_each_entry(event, head, hash_entry) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 414 | if (event->key.key == key->key && event->key.info == key->info) |
| 415 | return event; |
David Ahern | 355afe8 | 2012-10-08 11:17:32 -0600 | [diff] [blame] | 416 | } |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 417 | |
| 418 | event = kvm_alloc_init_event(key); |
| 419 | if (!event) |
| 420 | return NULL; |
| 421 | |
| 422 | list_add(&event->hash_entry, head); |
| 423 | return event; |
| 424 | } |
| 425 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 426 | static bool handle_begin_event(struct perf_kvm_stat *kvm, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 427 | struct vcpu_event_record *vcpu_record, |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 428 | struct event_key *key, u64 timestamp) |
| 429 | { |
| 430 | struct kvm_event *event = NULL; |
| 431 | |
| 432 | if (key->key != INVALID_KEY) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 433 | event = find_create_kvm_event(kvm, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 434 | |
| 435 | vcpu_record->last_event = event; |
| 436 | vcpu_record->start_time = timestamp; |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | static void |
| 441 | kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff) |
| 442 | { |
| 443 | kvm_stats->time += time_diff; |
| 444 | update_stats(&kvm_stats->stats, time_diff); |
| 445 | } |
| 446 | |
| 447 | static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event) |
| 448 | { |
| 449 | struct kvm_event_stats *kvm_stats = &event->total; |
| 450 | |
| 451 | if (vcpu_id != -1) |
| 452 | kvm_stats = &event->vcpu[vcpu_id]; |
| 453 | |
| 454 | return rel_stddev_stats(stddev_stats(&kvm_stats->stats), |
| 455 | avg_stats(&kvm_stats->stats)); |
| 456 | } |
| 457 | |
| 458 | static bool update_kvm_event(struct kvm_event *event, int vcpu_id, |
| 459 | u64 time_diff) |
| 460 | { |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 461 | if (vcpu_id == -1) { |
| 462 | kvm_update_event_stats(&event->total, time_diff); |
| 463 | return true; |
| 464 | } |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 465 | |
| 466 | if (!kvm_event_expand(event, vcpu_id)) |
| 467 | return false; |
| 468 | |
| 469 | kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff); |
| 470 | return true; |
| 471 | } |
| 472 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 473 | static bool handle_end_event(struct perf_kvm_stat *kvm, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 474 | struct vcpu_event_record *vcpu_record, |
| 475 | struct event_key *key, |
| 476 | u64 timestamp) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 477 | { |
| 478 | struct kvm_event *event; |
| 479 | u64 time_begin, time_diff; |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 480 | int vcpu; |
| 481 | |
| 482 | if (kvm->trace_vcpu == -1) |
| 483 | vcpu = -1; |
| 484 | else |
| 485 | vcpu = vcpu_record->vcpu_id; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 486 | |
| 487 | event = vcpu_record->last_event; |
| 488 | time_begin = vcpu_record->start_time; |
| 489 | |
| 490 | /* The begin event is not caught. */ |
| 491 | if (!time_begin) |
| 492 | return true; |
| 493 | |
| 494 | /* |
| 495 | * In some case, the 'begin event' only records the start timestamp, |
| 496 | * the actual event is recognized in the 'end event' (e.g. mmio-event). |
| 497 | */ |
| 498 | |
| 499 | /* Both begin and end events did not get the key. */ |
| 500 | if (!event && key->key == INVALID_KEY) |
| 501 | return true; |
| 502 | |
| 503 | if (!event) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 504 | event = find_create_kvm_event(kvm, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 505 | |
| 506 | if (!event) |
| 507 | return false; |
| 508 | |
| 509 | vcpu_record->last_event = NULL; |
| 510 | vcpu_record->start_time = 0; |
| 511 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 512 | /* seems to happen once in a while during live mode */ |
| 513 | if (timestamp < time_begin) { |
| 514 | pr_debug("End time before begin time; skipping event.\n"); |
| 515 | return true; |
| 516 | } |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 517 | |
| 518 | time_diff = timestamp - time_begin; |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 519 | return update_kvm_event(event, vcpu, time_diff); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 520 | } |
| 521 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 522 | static |
| 523 | struct vcpu_event_record *per_vcpu_record(struct thread *thread, |
| 524 | struct perf_evsel *evsel, |
| 525 | struct perf_sample *sample) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 526 | { |
| 527 | /* Only kvm_entry records vcpu id. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 528 | if (!thread->priv && kvm_entry_event(evsel)) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 529 | struct vcpu_event_record *vcpu_record; |
| 530 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 531 | vcpu_record = zalloc(sizeof(*vcpu_record)); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 532 | if (!vcpu_record) { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 533 | pr_err("%s: Not enough memory\n", __func__); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
| 536 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 537 | vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 538 | thread->priv = vcpu_record; |
| 539 | } |
| 540 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 541 | return thread->priv; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 542 | } |
| 543 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 544 | static bool handle_kvm_event(struct perf_kvm_stat *kvm, |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 545 | struct thread *thread, |
| 546 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 547 | struct perf_sample *sample) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 548 | { |
| 549 | struct vcpu_event_record *vcpu_record; |
| 550 | struct event_key key = {.key = INVALID_KEY}; |
| 551 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 552 | vcpu_record = per_vcpu_record(thread, evsel, sample); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 553 | if (!vcpu_record) |
| 554 | return true; |
| 555 | |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 556 | /* only process events for vcpus user cares about */ |
| 557 | if ((kvm->trace_vcpu != -1) && |
| 558 | (kvm->trace_vcpu != vcpu_record->vcpu_id)) |
| 559 | return true; |
| 560 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 561 | if (kvm->events_ops->is_begin_event(evsel, sample, &key)) |
| 562 | return handle_begin_event(kvm, vcpu_record, &key, sample->time); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 563 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 564 | if (kvm->events_ops->is_end_event(evsel, sample, &key)) |
| 565 | return handle_end_event(kvm, vcpu_record, &key, sample->time); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 566 | |
| 567 | return true; |
| 568 | } |
| 569 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 570 | #define GET_EVENT_KEY(func, field) \ |
| 571 | static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \ |
| 572 | { \ |
| 573 | if (vcpu == -1) \ |
| 574 | return event->total.field; \ |
| 575 | \ |
| 576 | if (vcpu >= event->max_vcpu) \ |
| 577 | return 0; \ |
| 578 | \ |
| 579 | return event->vcpu[vcpu].field; \ |
| 580 | } |
| 581 | |
| 582 | #define COMPARE_EVENT_KEY(func, field) \ |
| 583 | GET_EVENT_KEY(func, field) \ |
| 584 | static int compare_kvm_event_ ## func(struct kvm_event *one, \ |
| 585 | struct kvm_event *two, int vcpu)\ |
| 586 | { \ |
| 587 | return get_event_ ##func(one, vcpu) > \ |
| 588 | get_event_ ##func(two, vcpu); \ |
| 589 | } |
| 590 | |
| 591 | GET_EVENT_KEY(time, time); |
| 592 | COMPARE_EVENT_KEY(count, stats.n); |
| 593 | COMPARE_EVENT_KEY(mean, stats.mean); |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 594 | GET_EVENT_KEY(max, stats.max); |
| 595 | GET_EVENT_KEY(min, stats.min); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 596 | |
| 597 | #define DEF_SORT_NAME_KEY(name, compare_key) \ |
| 598 | { #name, compare_kvm_event_ ## compare_key } |
| 599 | |
| 600 | static struct kvm_event_key keys[] = { |
| 601 | DEF_SORT_NAME_KEY(sample, count), |
| 602 | DEF_SORT_NAME_KEY(time, mean), |
| 603 | { NULL, NULL } |
| 604 | }; |
| 605 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 606 | static bool select_key(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 607 | { |
| 608 | int i; |
| 609 | |
| 610 | for (i = 0; keys[i].name; i++) { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 611 | if (!strcmp(keys[i].name, kvm->sort_key)) { |
| 612 | kvm->compare = keys[i].key; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 613 | return true; |
| 614 | } |
| 615 | } |
| 616 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 617 | pr_err("Unknown compare key:%s\n", kvm->sort_key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 618 | return false; |
| 619 | } |
| 620 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 621 | static void insert_to_result(struct rb_root *result, struct kvm_event *event, |
| 622 | key_cmp_fun bigger, int vcpu) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 623 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 624 | struct rb_node **rb = &result->rb_node; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 625 | struct rb_node *parent = NULL; |
| 626 | struct kvm_event *p; |
| 627 | |
| 628 | while (*rb) { |
| 629 | p = container_of(*rb, struct kvm_event, rb); |
| 630 | parent = *rb; |
| 631 | |
| 632 | if (bigger(event, p, vcpu)) |
| 633 | rb = &(*rb)->rb_left; |
| 634 | else |
| 635 | rb = &(*rb)->rb_right; |
| 636 | } |
| 637 | |
| 638 | rb_link_node(&event->rb, parent, rb); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 639 | rb_insert_color(&event->rb, result); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 640 | } |
| 641 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 642 | static void |
| 643 | update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 644 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 645 | int vcpu = kvm->trace_vcpu; |
| 646 | |
| 647 | kvm->total_count += get_event_count(event, vcpu); |
| 648 | kvm->total_time += get_event_time(event, vcpu); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static bool event_is_valid(struct kvm_event *event, int vcpu) |
| 652 | { |
| 653 | return !!get_event_count(event, vcpu); |
| 654 | } |
| 655 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 656 | static void sort_result(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 657 | { |
| 658 | unsigned int i; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 659 | int vcpu = kvm->trace_vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 660 | struct kvm_event *event; |
| 661 | |
David Ahern | 355afe8 | 2012-10-08 11:17:32 -0600 | [diff] [blame] | 662 | for (i = 0; i < EVENTS_CACHE_SIZE; i++) { |
| 663 | list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 664 | if (event_is_valid(event, vcpu)) { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 665 | update_total_count(kvm, event); |
| 666 | insert_to_result(&kvm->result, event, |
| 667 | kvm->compare, vcpu); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 668 | } |
David Ahern | 355afe8 | 2012-10-08 11:17:32 -0600 | [diff] [blame] | 669 | } |
| 670 | } |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* returns left most element of result, and erase it */ |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 674 | static struct kvm_event *pop_from_result(struct rb_root *result) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 675 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 676 | struct rb_node *node = rb_first(result); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 677 | |
| 678 | if (!node) |
| 679 | return NULL; |
| 680 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 681 | rb_erase(node, result); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 682 | return container_of(node, struct kvm_event, rb); |
| 683 | } |
| 684 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 685 | static void print_vcpu_info(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 686 | { |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 687 | int vcpu = kvm->trace_vcpu; |
| 688 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 689 | pr_info("Analyze events for "); |
| 690 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 691 | if (kvm->live) { |
| 692 | if (kvm->opts.target.system_wide) |
| 693 | pr_info("all VMs, "); |
| 694 | else if (kvm->opts.target.pid) |
| 695 | pr_info("pid(s) %s, ", kvm->opts.target.pid); |
| 696 | else |
| 697 | pr_info("dazed and confused on what is monitored, "); |
| 698 | } |
| 699 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 700 | if (vcpu == -1) |
| 701 | pr_info("all VCPUs:\n\n"); |
| 702 | else |
| 703 | pr_info("VCPU %d:\n\n", vcpu); |
| 704 | } |
| 705 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 706 | static void show_timeofday(void) |
| 707 | { |
| 708 | char date[64]; |
| 709 | struct timeval tv; |
| 710 | struct tm ltime; |
| 711 | |
| 712 | gettimeofday(&tv, NULL); |
| 713 | if (localtime_r(&tv.tv_sec, <ime)) { |
| 714 | strftime(date, sizeof(date), "%H:%M:%S", <ime); |
| 715 | pr_info("%s.%06ld", date, tv.tv_usec); |
| 716 | } else |
| 717 | pr_info("00:00:00.000000"); |
| 718 | |
| 719 | return; |
| 720 | } |
| 721 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 722 | static void print_result(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 723 | { |
| 724 | char decode[20]; |
| 725 | struct kvm_event *event; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 726 | int vcpu = kvm->trace_vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 727 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 728 | if (kvm->live) { |
| 729 | puts(CONSOLE_CLEAR); |
| 730 | show_timeofday(); |
| 731 | } |
| 732 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 733 | pr_info("\n\n"); |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 734 | print_vcpu_info(kvm); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 735 | pr_info("%20s ", kvm->events_ops->name); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 736 | pr_info("%10s ", "Samples"); |
| 737 | pr_info("%9s ", "Samples%"); |
| 738 | |
| 739 | pr_info("%9s ", "Time%"); |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 740 | pr_info("%10s ", "Min Time"); |
| 741 | pr_info("%10s ", "Max Time"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 742 | pr_info("%16s ", "Avg time"); |
| 743 | pr_info("\n\n"); |
| 744 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 745 | while ((event = pop_from_result(&kvm->result))) { |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 746 | u64 ecount, etime, max, min; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 747 | |
| 748 | ecount = get_event_count(event, vcpu); |
| 749 | etime = get_event_time(event, vcpu); |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 750 | max = get_event_max(event, vcpu); |
| 751 | min = get_event_min(event, vcpu); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 752 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 753 | kvm->events_ops->decode_key(kvm, &event->key, decode); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 754 | pr_info("%20s ", decode); |
| 755 | pr_info("%10llu ", (unsigned long long)ecount); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 756 | pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100); |
| 757 | pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100); |
David Ahern | 62d04db | 2013-08-05 21:41:35 -0400 | [diff] [blame] | 758 | pr_info("%8" PRIu64 "us ", min / 1000); |
| 759 | pr_info("%8" PRIu64 "us ", max / 1000); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 760 | pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3, |
| 761 | kvm_event_rel_stddev(vcpu, event)); |
| 762 | pr_info("\n"); |
| 763 | } |
| 764 | |
David Ahern | e4f7637 | 2012-10-08 11:17:34 -0600 | [diff] [blame] | 765 | pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n", |
| 766 | kvm->total_count, kvm->total_time / 1e3); |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 767 | |
| 768 | if (kvm->lost_events) |
| 769 | pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events); |
| 770 | } |
| 771 | |
| 772 | static int process_lost_event(struct perf_tool *tool, |
| 773 | union perf_event *event __maybe_unused, |
| 774 | struct perf_sample *sample __maybe_unused, |
| 775 | struct machine *machine __maybe_unused) |
| 776 | { |
| 777 | struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool); |
| 778 | |
| 779 | kvm->lost_events++; |
| 780 | return 0; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 781 | } |
| 782 | |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 783 | static bool skip_sample(struct perf_kvm_stat *kvm, |
| 784 | struct perf_sample *sample) |
| 785 | { |
| 786 | if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL) |
| 787 | return true; |
| 788 | |
| 789 | return false; |
| 790 | } |
| 791 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 792 | static int process_sample_event(struct perf_tool *tool, |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 793 | union perf_event *event, |
| 794 | struct perf_sample *sample, |
| 795 | struct perf_evsel *evsel, |
| 796 | struct machine *machine) |
| 797 | { |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 798 | struct thread *thread; |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 799 | struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, |
| 800 | tool); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 801 | |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 802 | if (skip_sample(kvm, sample)) |
| 803 | return 0; |
| 804 | |
| 805 | thread = machine__findnew_thread(machine, sample->tid); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 806 | if (thread == NULL) { |
| 807 | pr_debug("problem processing %d event, skipping it.\n", |
| 808 | event->header.type); |
| 809 | return -1; |
| 810 | } |
| 811 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 812 | if (!handle_kvm_event(kvm, thread, evsel, sample)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 813 | return -1; |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 818 | static int cpu_isa_config(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 819 | { |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 820 | char buf[64], *cpuid; |
| 821 | int err, isa; |
| 822 | |
| 823 | if (kvm->live) { |
| 824 | err = get_cpuid(buf, sizeof(buf)); |
| 825 | if (err != 0) { |
| 826 | pr_err("Failed to look up CPU type (Intel or AMD)\n"); |
| 827 | return err; |
| 828 | } |
| 829 | cpuid = buf; |
| 830 | } else |
| 831 | cpuid = kvm->session->header.env.cpuid; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 832 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 833 | if (strstr(cpuid, "Intel")) |
| 834 | isa = 1; |
| 835 | else if (strstr(cpuid, "AMD")) |
| 836 | isa = 0; |
| 837 | else { |
| 838 | pr_err("CPU %s is not supported.\n", cpuid); |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 839 | return -ENOTSUP; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 840 | } |
| 841 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 842 | if (isa == 1) { |
| 843 | kvm->exit_reasons = vmx_exit_reasons; |
| 844 | kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons); |
| 845 | kvm->exit_reasons_isa = "VMX"; |
| 846 | } |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static bool verify_vcpu(int vcpu) |
| 852 | { |
| 853 | if (vcpu != -1 && vcpu < 0) { |
| 854 | pr_err("Invalid vcpu:%d.\n", vcpu); |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | /* keeping the max events to a modest level to keep |
| 862 | * the processing of samples per mmap smooth. |
| 863 | */ |
| 864 | #define PERF_KVM__MAX_EVENTS_PER_MMAP 25 |
| 865 | |
| 866 | static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx, |
| 867 | u64 *mmap_time) |
| 868 | { |
| 869 | union perf_event *event; |
| 870 | struct perf_sample sample; |
| 871 | s64 n = 0; |
| 872 | int err; |
| 873 | |
| 874 | *mmap_time = ULLONG_MAX; |
| 875 | while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) { |
| 876 | err = perf_evlist__parse_sample(kvm->evlist, event, &sample); |
| 877 | if (err) { |
| 878 | pr_err("Failed to parse sample\n"); |
| 879 | return -1; |
| 880 | } |
| 881 | |
| 882 | err = perf_session_queue_event(kvm->session, event, &sample, 0); |
| 883 | if (err) { |
| 884 | pr_err("Failed to enqueue sample: %d\n", err); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | /* save time stamp of our first sample for this mmap */ |
| 889 | if (n == 0) |
| 890 | *mmap_time = sample.time; |
| 891 | |
| 892 | /* limit events per mmap handled all at once */ |
| 893 | n++; |
| 894 | if (n == PERF_KVM__MAX_EVENTS_PER_MMAP) |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | return n; |
| 899 | } |
| 900 | |
| 901 | static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm) |
| 902 | { |
| 903 | int i, err, throttled = 0; |
| 904 | s64 n, ntotal = 0; |
| 905 | u64 flush_time = ULLONG_MAX, mmap_time; |
| 906 | |
| 907 | for (i = 0; i < kvm->evlist->nr_mmaps; i++) { |
| 908 | n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time); |
| 909 | if (n < 0) |
| 910 | return -1; |
| 911 | |
| 912 | /* flush time is going to be the minimum of all the individual |
| 913 | * mmap times. Essentially, we flush all the samples queued up |
| 914 | * from the last pass under our minimal start time -- that leaves |
| 915 | * a very small race for samples to come in with a lower timestamp. |
| 916 | * The ioctl to return the perf_clock timestamp should close the |
| 917 | * race entirely. |
| 918 | */ |
| 919 | if (mmap_time < flush_time) |
| 920 | flush_time = mmap_time; |
| 921 | |
| 922 | ntotal += n; |
| 923 | if (n == PERF_KVM__MAX_EVENTS_PER_MMAP) |
| 924 | throttled = 1; |
| 925 | } |
| 926 | |
| 927 | /* flush queue after each round in which we processed events */ |
| 928 | if (ntotal) { |
| 929 | kvm->session->ordered_samples.next_flush = flush_time; |
| 930 | err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session); |
| 931 | if (err) { |
| 932 | if (kvm->lost_events) |
| 933 | pr_info("\nLost events: %" PRIu64 "\n\n", |
| 934 | kvm->lost_events); |
| 935 | return err; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return throttled; |
| 940 | } |
| 941 | |
| 942 | static volatile int done; |
| 943 | |
| 944 | static void sig_handler(int sig __maybe_unused) |
| 945 | { |
| 946 | done = 1; |
| 947 | } |
| 948 | |
| 949 | static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm) |
| 950 | { |
| 951 | struct itimerspec new_value; |
| 952 | int rc = -1; |
| 953 | |
| 954 | kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); |
| 955 | if (kvm->timerfd < 0) { |
| 956 | pr_err("timerfd_create failed\n"); |
| 957 | goto out; |
| 958 | } |
| 959 | |
| 960 | new_value.it_value.tv_sec = kvm->display_time; |
| 961 | new_value.it_value.tv_nsec = 0; |
| 962 | new_value.it_interval.tv_sec = kvm->display_time; |
| 963 | new_value.it_interval.tv_nsec = 0; |
| 964 | |
| 965 | if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) { |
| 966 | pr_err("timerfd_settime failed: %d\n", errno); |
| 967 | close(kvm->timerfd); |
| 968 | goto out; |
| 969 | } |
| 970 | |
| 971 | rc = 0; |
| 972 | out: |
| 973 | return rc; |
| 974 | } |
| 975 | |
| 976 | static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm) |
| 977 | { |
| 978 | uint64_t c; |
| 979 | int rc; |
| 980 | |
| 981 | rc = read(kvm->timerfd, &c, sizeof(uint64_t)); |
| 982 | if (rc < 0) { |
| 983 | if (errno == EAGAIN) |
| 984 | return 0; |
| 985 | |
| 986 | pr_err("Failed to read timer fd: %d\n", errno); |
| 987 | return -1; |
| 988 | } |
| 989 | |
| 990 | if (rc != sizeof(uint64_t)) { |
| 991 | pr_err("Error reading timer fd - invalid size returned\n"); |
| 992 | return -1; |
| 993 | } |
| 994 | |
| 995 | if (c != 1) |
| 996 | pr_debug("Missed timer beats: %" PRIu64 "\n", c-1); |
| 997 | |
| 998 | /* update display */ |
| 999 | sort_result(kvm); |
| 1000 | print_result(kvm); |
| 1001 | |
| 1002 | /* reset counts */ |
| 1003 | clear_events_cache_stats(kvm->kvm_events_cache); |
| 1004 | kvm->total_count = 0; |
| 1005 | kvm->total_time = 0; |
| 1006 | kvm->lost_events = 0; |
| 1007 | |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static int fd_set_nonblock(int fd) |
| 1012 | { |
| 1013 | long arg = 0; |
| 1014 | |
| 1015 | arg = fcntl(fd, F_GETFL); |
| 1016 | if (arg < 0) { |
| 1017 | pr_err("Failed to get current flags for fd %d\n", fd); |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
| 1021 | if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) { |
| 1022 | pr_err("Failed to set non-block option on fd %d\n", fd); |
| 1023 | return -1; |
| 1024 | } |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | static |
| 1030 | int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save) |
| 1031 | { |
| 1032 | int c; |
| 1033 | |
| 1034 | tcsetattr(0, TCSANOW, tc_now); |
| 1035 | c = getc(stdin); |
| 1036 | tcsetattr(0, TCSAFLUSH, tc_save); |
| 1037 | |
| 1038 | if (c == 'q') |
| 1039 | return 1; |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | static int kvm_events_live_report(struct perf_kvm_stat *kvm) |
| 1045 | { |
| 1046 | struct pollfd *pollfds = NULL; |
| 1047 | int nr_fds, nr_stdin, ret, err = -EINVAL; |
| 1048 | struct termios tc, save; |
| 1049 | |
| 1050 | /* live flag must be set first */ |
| 1051 | kvm->live = true; |
| 1052 | |
| 1053 | ret = cpu_isa_config(kvm); |
| 1054 | if (ret < 0) |
| 1055 | return ret; |
| 1056 | |
| 1057 | if (!verify_vcpu(kvm->trace_vcpu) || |
| 1058 | !select_key(kvm) || |
| 1059 | !register_kvm_events_ops(kvm)) { |
| 1060 | goto out; |
| 1061 | } |
| 1062 | |
| 1063 | init_kvm_event_record(kvm); |
| 1064 | |
| 1065 | tcgetattr(0, &save); |
| 1066 | tc = save; |
| 1067 | tc.c_lflag &= ~(ICANON | ECHO); |
| 1068 | tc.c_cc[VMIN] = 0; |
| 1069 | tc.c_cc[VTIME] = 0; |
| 1070 | |
| 1071 | signal(SIGINT, sig_handler); |
| 1072 | signal(SIGTERM, sig_handler); |
| 1073 | |
| 1074 | /* copy pollfds -- need to add timerfd and stdin */ |
| 1075 | nr_fds = kvm->evlist->nr_fds; |
| 1076 | pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2)); |
| 1077 | if (!pollfds) { |
| 1078 | err = -ENOMEM; |
| 1079 | goto out; |
| 1080 | } |
| 1081 | memcpy(pollfds, kvm->evlist->pollfd, |
| 1082 | sizeof(struct pollfd) * kvm->evlist->nr_fds); |
| 1083 | |
| 1084 | /* add timer fd */ |
| 1085 | if (perf_kvm__timerfd_create(kvm) < 0) { |
| 1086 | err = -1; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | |
| 1090 | pollfds[nr_fds].fd = kvm->timerfd; |
| 1091 | pollfds[nr_fds].events = POLLIN; |
| 1092 | nr_fds++; |
| 1093 | |
| 1094 | pollfds[nr_fds].fd = fileno(stdin); |
| 1095 | pollfds[nr_fds].events = POLLIN; |
| 1096 | nr_stdin = nr_fds; |
| 1097 | nr_fds++; |
| 1098 | if (fd_set_nonblock(fileno(stdin)) != 0) |
| 1099 | goto out; |
| 1100 | |
| 1101 | /* everything is good - enable the events and process */ |
| 1102 | perf_evlist__enable(kvm->evlist); |
| 1103 | |
| 1104 | while (!done) { |
| 1105 | int rc; |
| 1106 | |
| 1107 | rc = perf_kvm__mmap_read(kvm); |
| 1108 | if (rc < 0) |
| 1109 | break; |
| 1110 | |
| 1111 | err = perf_kvm__handle_timerfd(kvm); |
| 1112 | if (err) |
| 1113 | goto out; |
| 1114 | |
| 1115 | if (pollfds[nr_stdin].revents & POLLIN) |
| 1116 | done = perf_kvm__handle_stdin(&tc, &save); |
| 1117 | |
| 1118 | if (!rc && !done) |
| 1119 | err = poll(pollfds, nr_fds, 100); |
| 1120 | } |
| 1121 | |
| 1122 | perf_evlist__disable(kvm->evlist); |
| 1123 | |
| 1124 | if (err == 0) { |
| 1125 | sort_result(kvm); |
| 1126 | print_result(kvm); |
| 1127 | } |
| 1128 | |
| 1129 | out: |
| 1130 | if (kvm->timerfd >= 0) |
| 1131 | close(kvm->timerfd); |
| 1132 | |
| 1133 | if (pollfds) |
| 1134 | free(pollfds); |
| 1135 | |
| 1136 | return err; |
| 1137 | } |
| 1138 | |
| 1139 | static int kvm_live_open_events(struct perf_kvm_stat *kvm) |
| 1140 | { |
| 1141 | int err, rc = -1; |
| 1142 | struct perf_evsel *pos; |
| 1143 | struct perf_evlist *evlist = kvm->evlist; |
| 1144 | |
| 1145 | perf_evlist__config(evlist, &kvm->opts); |
| 1146 | |
| 1147 | /* |
| 1148 | * Note: exclude_{guest,host} do not apply here. |
| 1149 | * This command processes KVM tracepoints from host only |
| 1150 | */ |
| 1151 | list_for_each_entry(pos, &evlist->entries, node) { |
| 1152 | struct perf_event_attr *attr = &pos->attr; |
| 1153 | |
| 1154 | /* make sure these *are* set */ |
| 1155 | attr->sample_type |= PERF_SAMPLE_TID; |
| 1156 | attr->sample_type |= PERF_SAMPLE_TIME; |
| 1157 | attr->sample_type |= PERF_SAMPLE_CPU; |
| 1158 | attr->sample_type |= PERF_SAMPLE_RAW; |
| 1159 | /* make sure these are *not*; want as small a sample as possible */ |
| 1160 | attr->sample_type &= ~PERF_SAMPLE_PERIOD; |
| 1161 | attr->sample_type &= ~PERF_SAMPLE_IP; |
| 1162 | attr->sample_type &= ~PERF_SAMPLE_CALLCHAIN; |
| 1163 | attr->sample_type &= ~PERF_SAMPLE_ADDR; |
| 1164 | attr->sample_type &= ~PERF_SAMPLE_READ; |
| 1165 | attr->mmap = 0; |
| 1166 | attr->comm = 0; |
| 1167 | attr->task = 0; |
| 1168 | |
| 1169 | attr->sample_period = 1; |
| 1170 | |
| 1171 | attr->watermark = 0; |
| 1172 | attr->wakeup_events = 1000; |
| 1173 | |
| 1174 | /* will enable all once we are ready */ |
| 1175 | attr->disabled = 1; |
| 1176 | } |
| 1177 | |
| 1178 | err = perf_evlist__open(evlist); |
| 1179 | if (err < 0) { |
| 1180 | printf("Couldn't create the events: %s\n", strerror(errno)); |
| 1181 | goto out; |
| 1182 | } |
| 1183 | |
| 1184 | if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) { |
| 1185 | ui__error("Failed to mmap the events: %s\n", strerror(errno)); |
| 1186 | perf_evlist__close(evlist); |
| 1187 | goto out; |
| 1188 | } |
| 1189 | |
| 1190 | rc = 0; |
| 1191 | |
| 1192 | out: |
| 1193 | return rc; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1196 | static int read_events(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1197 | { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1198 | int ret; |
| 1199 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1200 | struct perf_tool eops = { |
| 1201 | .sample = process_sample_event, |
| 1202 | .comm = perf_event__process_comm, |
| 1203 | .ordered_samples = true, |
| 1204 | }; |
| 1205 | |
| 1206 | kvm->tool = eops; |
| 1207 | kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false, |
| 1208 | &kvm->tool); |
| 1209 | if (!kvm->session) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1210 | pr_err("Initializing perf session failed\n"); |
| 1211 | return -EINVAL; |
| 1212 | } |
| 1213 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1214 | if (!perf_session__has_traces(kvm->session, "kvm record")) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1215 | return -EINVAL; |
| 1216 | |
| 1217 | /* |
| 1218 | * Do not use 'isa' recorded in kvm_exit tracepoint since it is not |
| 1219 | * traced in the old kernel. |
| 1220 | */ |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 1221 | ret = cpu_isa_config(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1222 | if (ret < 0) |
| 1223 | return ret; |
| 1224 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1225 | return perf_session__process_events(kvm->session, &kvm->tool); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1226 | } |
| 1227 | |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 1228 | static int parse_target_str(struct perf_kvm_stat *kvm) |
| 1229 | { |
| 1230 | if (kvm->pid_str) { |
| 1231 | kvm->pid_list = intlist__new(kvm->pid_str); |
| 1232 | if (kvm->pid_list == NULL) { |
| 1233 | pr_err("Error parsing process id string\n"); |
| 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1241 | static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1242 | { |
| 1243 | int ret = -EINVAL; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1244 | int vcpu = kvm->trace_vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1245 | |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 1246 | if (parse_target_str(kvm) != 0) |
| 1247 | goto exit; |
| 1248 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1249 | if (!verify_vcpu(vcpu)) |
| 1250 | goto exit; |
| 1251 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1252 | if (!select_key(kvm)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1253 | goto exit; |
| 1254 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1255 | if (!register_kvm_events_ops(kvm)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1256 | goto exit; |
| 1257 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1258 | init_kvm_event_record(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1259 | setup_pager(); |
| 1260 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1261 | ret = read_events(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1262 | if (ret) |
| 1263 | goto exit; |
| 1264 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1265 | sort_result(kvm); |
| 1266 | print_result(kvm); |
| 1267 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1268 | exit: |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
David Ahern | 8fdd84c | 2013-08-02 14:05:42 -0600 | [diff] [blame] | 1272 | static const char * const kvm_events_tp[] = { |
| 1273 | "kvm:kvm_entry", |
| 1274 | "kvm:kvm_exit", |
| 1275 | "kvm:kvm_mmio", |
| 1276 | "kvm:kvm_pio", |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1277 | }; |
| 1278 | |
| 1279 | #define STRDUP_FAIL_EXIT(s) \ |
| 1280 | ({ char *_p; \ |
| 1281 | _p = strdup(s); \ |
| 1282 | if (!_p) \ |
| 1283 | return -ENOMEM; \ |
| 1284 | _p; \ |
| 1285 | }) |
| 1286 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1287 | static int |
| 1288 | kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1289 | { |
| 1290 | unsigned int rec_argc, i, j; |
| 1291 | const char **rec_argv; |
David Ahern | 8fdd84c | 2013-08-02 14:05:42 -0600 | [diff] [blame] | 1292 | const char * const record_args[] = { |
| 1293 | "record", |
| 1294 | "-R", |
| 1295 | "-f", |
| 1296 | "-m", "1024", |
| 1297 | "-c", "1", |
| 1298 | }; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1299 | |
David Ahern | 8fdd84c | 2013-08-02 14:05:42 -0600 | [diff] [blame] | 1300 | rec_argc = ARRAY_SIZE(record_args) + argc + 2 + |
| 1301 | 2 * ARRAY_SIZE(kvm_events_tp); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1302 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1303 | |
| 1304 | if (rec_argv == NULL) |
| 1305 | return -ENOMEM; |
| 1306 | |
| 1307 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 1308 | rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]); |
| 1309 | |
David Ahern | 8fdd84c | 2013-08-02 14:05:42 -0600 | [diff] [blame] | 1310 | for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) { |
| 1311 | rec_argv[i++] = "-e"; |
| 1312 | rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]); |
| 1313 | } |
| 1314 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1315 | rec_argv[i++] = STRDUP_FAIL_EXIT("-o"); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1316 | rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1317 | |
| 1318 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 1319 | rec_argv[i] = argv[j]; |
| 1320 | |
| 1321 | return cmd_record(i, rec_argv, NULL); |
| 1322 | } |
| 1323 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1324 | static int |
| 1325 | kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1326 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1327 | const struct option kvm_events_report_options[] = { |
| 1328 | OPT_STRING(0, "event", &kvm->report_event, "report event", |
| 1329 | "event for reporting: vmexit, mmio, ioport"), |
| 1330 | OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu, |
| 1331 | "vcpu id to report"), |
| 1332 | OPT_STRING('k', "key", &kvm->sort_key, "sort-key", |
| 1333 | "key for sorting: sample(sort by samples number)" |
| 1334 | " time (sort by avg time)"), |
David Ahern | 2e73f00 | 2013-08-05 21:41:37 -0400 | [diff] [blame^] | 1335 | OPT_STRING('p', "pid", &kvm->pid_str, "pid", |
| 1336 | "analyze events only for given process id(s)"), |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1337 | OPT_END() |
| 1338 | }; |
| 1339 | |
| 1340 | const char * const kvm_events_report_usage[] = { |
| 1341 | "perf kvm stat report [<options>]", |
| 1342 | NULL |
| 1343 | }; |
| 1344 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1345 | symbol__init(); |
| 1346 | |
| 1347 | if (argc) { |
| 1348 | argc = parse_options(argc, argv, |
| 1349 | kvm_events_report_options, |
| 1350 | kvm_events_report_usage, 0); |
| 1351 | if (argc) |
| 1352 | usage_with_options(kvm_events_report_usage, |
| 1353 | kvm_events_report_options); |
| 1354 | } |
| 1355 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1356 | return kvm_events_report_vcpu(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1357 | } |
| 1358 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 1359 | static struct perf_evlist *kvm_live_event_list(void) |
| 1360 | { |
| 1361 | struct perf_evlist *evlist; |
| 1362 | char *tp, *name, *sys; |
| 1363 | unsigned int j; |
| 1364 | int err = -1; |
| 1365 | |
| 1366 | evlist = perf_evlist__new(); |
| 1367 | if (evlist == NULL) |
| 1368 | return NULL; |
| 1369 | |
| 1370 | for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) { |
| 1371 | |
| 1372 | tp = strdup(kvm_events_tp[j]); |
| 1373 | if (tp == NULL) |
| 1374 | goto out; |
| 1375 | |
| 1376 | /* split tracepoint into subsystem and name */ |
| 1377 | sys = tp; |
| 1378 | name = strchr(tp, ':'); |
| 1379 | if (name == NULL) { |
| 1380 | pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n", |
| 1381 | kvm_events_tp[j]); |
| 1382 | free(tp); |
| 1383 | goto out; |
| 1384 | } |
| 1385 | *name = '\0'; |
| 1386 | name++; |
| 1387 | |
| 1388 | if (perf_evlist__add_newtp(evlist, sys, name, NULL)) { |
| 1389 | pr_err("Failed to add %s tracepoint to the list\n", kvm_events_tp[j]); |
| 1390 | free(tp); |
| 1391 | goto out; |
| 1392 | } |
| 1393 | |
| 1394 | free(tp); |
| 1395 | } |
| 1396 | |
| 1397 | err = 0; |
| 1398 | |
| 1399 | out: |
| 1400 | if (err) { |
| 1401 | perf_evlist__delete(evlist); |
| 1402 | evlist = NULL; |
| 1403 | } |
| 1404 | |
| 1405 | return evlist; |
| 1406 | } |
| 1407 | |
| 1408 | static int kvm_events_live(struct perf_kvm_stat *kvm, |
| 1409 | int argc, const char **argv) |
| 1410 | { |
| 1411 | char errbuf[BUFSIZ]; |
| 1412 | int err; |
| 1413 | |
| 1414 | const struct option live_options[] = { |
| 1415 | OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid", |
| 1416 | "record events on existing process id"), |
| 1417 | OPT_UINTEGER('m', "mmap-pages", &kvm->opts.mmap_pages, |
| 1418 | "number of mmap data pages"), |
| 1419 | OPT_INCR('v', "verbose", &verbose, |
| 1420 | "be more verbose (show counter open errors, etc)"), |
| 1421 | OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide, |
| 1422 | "system-wide collection from all CPUs"), |
| 1423 | OPT_UINTEGER('d', "display", &kvm->display_time, |
| 1424 | "time in seconds between display updates"), |
| 1425 | OPT_STRING(0, "event", &kvm->report_event, "report event", |
| 1426 | "event for reporting: vmexit, mmio, ioport"), |
| 1427 | OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu, |
| 1428 | "vcpu id to report"), |
| 1429 | OPT_STRING('k', "key", &kvm->sort_key, "sort-key", |
| 1430 | "key for sorting: sample(sort by samples number)" |
| 1431 | " time (sort by avg time)"), |
| 1432 | OPT_END() |
| 1433 | }; |
| 1434 | const char * const live_usage[] = { |
| 1435 | "perf kvm stat live [<options>]", |
| 1436 | NULL |
| 1437 | }; |
| 1438 | |
| 1439 | |
| 1440 | /* event handling */ |
| 1441 | kvm->tool.sample = process_sample_event; |
| 1442 | kvm->tool.comm = perf_event__process_comm; |
| 1443 | kvm->tool.exit = perf_event__process_exit; |
| 1444 | kvm->tool.fork = perf_event__process_fork; |
| 1445 | kvm->tool.lost = process_lost_event; |
| 1446 | kvm->tool.ordered_samples = true; |
| 1447 | perf_tool__fill_defaults(&kvm->tool); |
| 1448 | |
| 1449 | /* set defaults */ |
| 1450 | kvm->display_time = 1; |
| 1451 | kvm->opts.user_interval = 1; |
| 1452 | kvm->opts.mmap_pages = 512; |
| 1453 | kvm->opts.target.uses_mmap = false; |
| 1454 | kvm->opts.target.uid_str = NULL; |
| 1455 | kvm->opts.target.uid = UINT_MAX; |
| 1456 | |
| 1457 | symbol__init(); |
| 1458 | disable_buildid_cache(); |
| 1459 | |
| 1460 | use_browser = 0; |
| 1461 | setup_browser(false); |
| 1462 | |
| 1463 | if (argc) { |
| 1464 | argc = parse_options(argc, argv, live_options, |
| 1465 | live_usage, 0); |
| 1466 | if (argc) |
| 1467 | usage_with_options(live_usage, live_options); |
| 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * target related setups |
| 1472 | */ |
| 1473 | err = perf_target__validate(&kvm->opts.target); |
| 1474 | if (err) { |
| 1475 | perf_target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ); |
| 1476 | ui__warning("%s", errbuf); |
| 1477 | } |
| 1478 | |
| 1479 | if (perf_target__none(&kvm->opts.target)) |
| 1480 | kvm->opts.target.system_wide = true; |
| 1481 | |
| 1482 | |
| 1483 | /* |
| 1484 | * generate the event list |
| 1485 | */ |
| 1486 | kvm->evlist = kvm_live_event_list(); |
| 1487 | if (kvm->evlist == NULL) { |
| 1488 | err = -1; |
| 1489 | goto out; |
| 1490 | } |
| 1491 | |
| 1492 | symbol_conf.nr_events = kvm->evlist->nr_entries; |
| 1493 | |
| 1494 | if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0) |
| 1495 | usage_with_options(live_usage, live_options); |
| 1496 | |
| 1497 | /* |
| 1498 | * perf session |
| 1499 | */ |
| 1500 | kvm->session = perf_session__new(NULL, O_WRONLY, false, false, &kvm->tool); |
| 1501 | if (kvm->session == NULL) { |
| 1502 | err = -ENOMEM; |
| 1503 | goto out; |
| 1504 | } |
| 1505 | kvm->session->evlist = kvm->evlist; |
| 1506 | perf_session__set_id_hdr_size(kvm->session); |
| 1507 | |
| 1508 | |
| 1509 | if (perf_target__has_task(&kvm->opts.target)) |
| 1510 | perf_event__synthesize_thread_map(&kvm->tool, |
| 1511 | kvm->evlist->threads, |
| 1512 | perf_event__process, |
| 1513 | &kvm->session->machines.host); |
| 1514 | else |
| 1515 | perf_event__synthesize_threads(&kvm->tool, perf_event__process, |
| 1516 | &kvm->session->machines.host); |
| 1517 | |
| 1518 | |
| 1519 | err = kvm_live_open_events(kvm); |
| 1520 | if (err) |
| 1521 | goto out; |
| 1522 | |
| 1523 | err = kvm_events_live_report(kvm); |
| 1524 | |
| 1525 | out: |
| 1526 | exit_browser(0); |
| 1527 | |
| 1528 | if (kvm->session) |
| 1529 | perf_session__delete(kvm->session); |
| 1530 | kvm->session = NULL; |
| 1531 | if (kvm->evlist) { |
| 1532 | perf_evlist__delete_maps(kvm->evlist); |
| 1533 | perf_evlist__delete(kvm->evlist); |
| 1534 | } |
| 1535 | |
| 1536 | return err; |
| 1537 | } |
| 1538 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1539 | static void print_kvm_stat_usage(void) |
| 1540 | { |
| 1541 | printf("Usage: perf kvm stat <command>\n\n"); |
| 1542 | |
| 1543 | printf("# Available commands:\n"); |
| 1544 | printf("\trecord: record kvm events\n"); |
| 1545 | printf("\treport: report statistical data of kvm events\n"); |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 1546 | printf("\tlive: live reporting of statistical data of kvm events\n"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1547 | |
| 1548 | printf("\nOtherwise, it is the alias of 'perf stat':\n"); |
| 1549 | } |
| 1550 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1551 | static int kvm_cmd_stat(const char *file_name, int argc, const char **argv) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1552 | { |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1553 | struct perf_kvm_stat kvm = { |
| 1554 | .file_name = file_name, |
| 1555 | |
| 1556 | .trace_vcpu = -1, |
| 1557 | .report_event = "vmexit", |
| 1558 | .sort_key = "sample", |
| 1559 | |
| 1560 | .exit_reasons = svm_exit_reasons, |
| 1561 | .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons), |
| 1562 | .exit_reasons_isa = "SVM", |
| 1563 | }; |
| 1564 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1565 | if (argc == 1) { |
| 1566 | print_kvm_stat_usage(); |
| 1567 | goto perf_stat; |
| 1568 | } |
| 1569 | |
| 1570 | if (!strncmp(argv[1], "rec", 3)) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1571 | return kvm_events_record(&kvm, argc - 1, argv + 1); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1572 | |
| 1573 | if (!strncmp(argv[1], "rep", 3)) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1574 | return kvm_events_report(&kvm, argc - 1 , argv + 1); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1575 | |
David Ahern | 1afe1d1 | 2013-08-05 21:41:34 -0400 | [diff] [blame] | 1576 | if (!strncmp(argv[1], "live", 4)) |
| 1577 | return kvm_events_live(&kvm, argc - 1 , argv + 1); |
| 1578 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1579 | perf_stat: |
| 1580 | return cmd_stat(argc, argv, NULL); |
| 1581 | } |
Xiao Guangrong | 7321090 | 2012-11-19 16:19:21 +0800 | [diff] [blame] | 1582 | #endif |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1583 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1584 | static int __cmd_record(const char *file_name, int argc, const char **argv) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1585 | { |
| 1586 | int rec_argc, i = 0, j; |
| 1587 | const char **rec_argv; |
| 1588 | |
| 1589 | rec_argc = argc + 2; |
| 1590 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1591 | rec_argv[i++] = strdup("record"); |
| 1592 | rec_argv[i++] = strdup("-o"); |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1593 | rec_argv[i++] = strdup(file_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1594 | for (j = 1; j < argc; j++, i++) |
| 1595 | rec_argv[i] = argv[j]; |
| 1596 | |
| 1597 | BUG_ON(i != rec_argc); |
| 1598 | |
| 1599 | return cmd_record(i, rec_argv, NULL); |
| 1600 | } |
| 1601 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1602 | static int __cmd_report(const char *file_name, int argc, const char **argv) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1603 | { |
| 1604 | int rec_argc, i = 0, j; |
| 1605 | const char **rec_argv; |
| 1606 | |
| 1607 | rec_argc = argc + 2; |
| 1608 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1609 | rec_argv[i++] = strdup("report"); |
| 1610 | rec_argv[i++] = strdup("-i"); |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1611 | rec_argv[i++] = strdup(file_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1612 | for (j = 1; j < argc; j++, i++) |
| 1613 | rec_argv[i] = argv[j]; |
| 1614 | |
| 1615 | BUG_ON(i != rec_argc); |
| 1616 | |
| 1617 | return cmd_report(i, rec_argv, NULL); |
| 1618 | } |
| 1619 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1620 | static int |
| 1621 | __cmd_buildid_list(const char *file_name, int argc, const char **argv) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1622 | { |
| 1623 | int rec_argc, i = 0, j; |
| 1624 | const char **rec_argv; |
| 1625 | |
| 1626 | rec_argc = argc + 2; |
| 1627 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1628 | rec_argv[i++] = strdup("buildid-list"); |
| 1629 | rec_argv[i++] = strdup("-i"); |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1630 | rec_argv[i++] = strdup(file_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1631 | for (j = 1; j < argc; j++, i++) |
| 1632 | rec_argv[i] = argv[j]; |
| 1633 | |
| 1634 | BUG_ON(i != rec_argc); |
| 1635 | |
| 1636 | return cmd_buildid_list(i, rec_argv, NULL); |
| 1637 | } |
| 1638 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1639 | int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1640 | { |
Arnaldo Carvalho de Melo | 20914ce5 | 2012-12-19 10:59:29 -0300 | [diff] [blame] | 1641 | const char *file_name = NULL; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1642 | const struct option kvm_options[] = { |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1643 | OPT_STRING('i', "input", &file_name, "file", |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1644 | "Input file name"), |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1645 | OPT_STRING('o', "output", &file_name, "file", |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1646 | "Output file name"), |
| 1647 | OPT_BOOLEAN(0, "guest", &perf_guest, |
| 1648 | "Collect guest os data"), |
| 1649 | OPT_BOOLEAN(0, "host", &perf_host, |
| 1650 | "Collect host os data"), |
| 1651 | OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory", |
| 1652 | "guest mount directory under which every guest os" |
| 1653 | " instance has a subdir"), |
| 1654 | OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name, |
| 1655 | "file", "file saving guest os vmlinux"), |
| 1656 | OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms, |
| 1657 | "file", "file saving guest os /proc/kallsyms"), |
| 1658 | OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules, |
| 1659 | "file", "file saving guest os /proc/modules"), |
| 1660 | OPT_END() |
| 1661 | }; |
| 1662 | |
| 1663 | |
| 1664 | const char * const kvm_usage[] = { |
| 1665 | "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}", |
| 1666 | NULL |
| 1667 | }; |
| 1668 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 1669 | perf_host = 0; |
| 1670 | perf_guest = 1; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1671 | |
| 1672 | argc = parse_options(argc, argv, kvm_options, kvm_usage, |
| 1673 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1674 | if (!argc) |
| 1675 | usage_with_options(kvm_usage, kvm_options); |
| 1676 | |
| 1677 | if (!perf_host) |
| 1678 | perf_guest = 1; |
| 1679 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1680 | if (!file_name) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1681 | if (perf_host && !perf_guest) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1682 | file_name = strdup("perf.data.host"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1683 | else if (!perf_host && perf_guest) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1684 | file_name = strdup("perf.data.guest"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1685 | else |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1686 | file_name = strdup("perf.data.kvm"); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1687 | |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1688 | if (!file_name) { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1689 | pr_err("Failed to allocate memory for filename\n"); |
| 1690 | return -ENOMEM; |
| 1691 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | if (!strncmp(argv[0], "rec", 3)) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1695 | return __cmd_record(file_name, argc, argv); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1696 | else if (!strncmp(argv[0], "rep", 3)) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1697 | return __cmd_report(file_name, argc, argv); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1698 | else if (!strncmp(argv[0], "diff", 4)) |
| 1699 | return cmd_diff(argc, argv, NULL); |
| 1700 | else if (!strncmp(argv[0], "top", 3)) |
| 1701 | return cmd_top(argc, argv, NULL); |
| 1702 | else if (!strncmp(argv[0], "buildid-list", 12)) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1703 | return __cmd_buildid_list(file_name, argc, argv); |
Xiao Guangrong | 7321090 | 2012-11-19 16:19:21 +0800 | [diff] [blame] | 1704 | #if defined(__i386__) || defined(__x86_64__) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1705 | else if (!strncmp(argv[0], "stat", 4)) |
Xiao Guangrong | 3786063 | 2012-11-15 14:17:01 +0800 | [diff] [blame] | 1706 | return kvm_cmd_stat(file_name, argc, argv); |
Xiao Guangrong | 7321090 | 2012-11-19 16:19:21 +0800 | [diff] [blame] | 1707 | #endif |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1708 | else |
| 1709 | usage_with_options(kvm_usage, kvm_options); |
| 1710 | |
| 1711 | return 0; |
| 1712 | } |