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" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 5 | #include "util/util.h" |
| 6 | #include "util/cache.h" |
| 7 | #include "util/symbol.h" |
| 8 | #include "util/thread.h" |
| 9 | #include "util/header.h" |
| 10 | #include "util/session.h" |
| 11 | |
| 12 | #include "util/parse-options.h" |
| 13 | #include "util/trace-event.h" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 14 | #include "util/debug.h" |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 15 | #include "util/debugfs.h" |
| 16 | #include "util/tool.h" |
| 17 | #include "util/stat.h" |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 18 | |
| 19 | #include <sys/prctl.h> |
| 20 | |
| 21 | #include <semaphore.h> |
| 22 | #include <pthread.h> |
| 23 | #include <math.h> |
| 24 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 25 | #include "../../arch/x86/include/asm/svm.h" |
| 26 | #include "../../arch/x86/include/asm/vmx.h" |
| 27 | #include "../../arch/x86/include/asm/kvm.h" |
| 28 | |
| 29 | struct event_key { |
| 30 | #define INVALID_KEY (~0ULL) |
| 31 | u64 key; |
| 32 | int info; |
| 33 | }; |
| 34 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 35 | struct kvm_event_stats { |
| 36 | u64 time; |
| 37 | struct stats stats; |
| 38 | }; |
| 39 | |
| 40 | struct kvm_event { |
| 41 | struct list_head hash_entry; |
| 42 | struct rb_node rb; |
| 43 | |
| 44 | struct event_key key; |
| 45 | |
| 46 | struct kvm_event_stats total; |
| 47 | |
| 48 | #define DEFAULT_VCPU_NUM 8 |
| 49 | int max_vcpu; |
| 50 | struct kvm_event_stats *vcpu; |
| 51 | }; |
| 52 | |
| 53 | typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int); |
| 54 | |
| 55 | struct kvm_event_key { |
| 56 | const char *name; |
| 57 | key_cmp_fun key; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | struct perf_kvm; |
| 62 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 63 | struct kvm_events_ops { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 64 | bool (*is_begin_event)(struct perf_evsel *evsel, |
| 65 | struct perf_sample *sample, |
| 66 | struct event_key *key); |
| 67 | bool (*is_end_event)(struct perf_evsel *evsel, |
| 68 | struct perf_sample *sample, struct event_key *key); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 69 | void (*decode_key)(struct perf_kvm *kvm, struct event_key *key, |
| 70 | char decode[20]); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 71 | const char *name; |
| 72 | }; |
| 73 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 74 | struct exit_reasons_table { |
| 75 | unsigned long exit_code; |
| 76 | const char *reason; |
| 77 | }; |
| 78 | |
| 79 | #define EVENTS_BITS 12 |
| 80 | #define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS) |
| 81 | |
| 82 | struct perf_kvm { |
| 83 | struct perf_tool tool; |
| 84 | struct perf_session *session; |
| 85 | |
| 86 | const char *file_name; |
| 87 | const char *report_event; |
| 88 | const char *sort_key; |
| 89 | int trace_vcpu; |
| 90 | |
| 91 | struct exit_reasons_table *exit_reasons; |
| 92 | int exit_reasons_size; |
| 93 | const char *exit_reasons_isa; |
| 94 | |
| 95 | struct kvm_events_ops *events_ops; |
| 96 | key_cmp_fun compare; |
| 97 | struct list_head kvm_events_cache[EVENTS_CACHE_SIZE]; |
| 98 | u64 total_time; |
| 99 | u64 total_count; |
| 100 | |
| 101 | struct rb_root result; |
| 102 | }; |
| 103 | |
| 104 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 105 | static void exit_event_get_key(struct perf_evsel *evsel, |
| 106 | struct perf_sample *sample, |
| 107 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 108 | { |
| 109 | key->info = 0; |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 110 | key->key = perf_evsel__intval(evsel, sample, "exit_reason"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 113 | static bool kvm_exit_event(struct perf_evsel *evsel) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 114 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 115 | return !strcmp(evsel->name, "kvm:kvm_exit"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 118 | static bool exit_event_begin(struct perf_evsel *evsel, |
| 119 | struct perf_sample *sample, struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 120 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 121 | if (kvm_exit_event(evsel)) { |
| 122 | exit_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 129 | static bool kvm_entry_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_entry"); |
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_end(struct perf_evsel *evsel, |
| 135 | struct perf_sample *sample __maybe_unused, |
| 136 | struct event_key *key __maybe_unused) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 137 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 138 | return kvm_entry_event(evsel); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 139 | } |
| 140 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 141 | static struct exit_reasons_table vmx_exit_reasons[] = { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 142 | VMX_EXIT_REASONS |
| 143 | }; |
| 144 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 145 | static struct exit_reasons_table svm_exit_reasons[] = { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 146 | SVM_EXIT_REASONS |
| 147 | }; |
| 148 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 149 | static const char *get_exit_reason(struct perf_kvm *kvm, u64 exit_code) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 150 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 151 | int i = kvm->exit_reasons_size; |
| 152 | struct exit_reasons_table *tbl = kvm->exit_reasons; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 153 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 154 | while (i--) { |
| 155 | if (tbl->exit_code == exit_code) |
| 156 | return tbl->reason; |
| 157 | tbl++; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | pr_err("unknown kvm exit code:%lld on %s\n", |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 161 | (unsigned long long)exit_code, kvm->exit_reasons_isa); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 162 | return "UNKNOWN"; |
| 163 | } |
| 164 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 165 | static void exit_event_decode_key(struct perf_kvm *kvm, |
| 166 | struct event_key *key, |
| 167 | char decode[20]) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 168 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 169 | const char *exit_reason = get_exit_reason(kvm, key->key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 170 | |
| 171 | scnprintf(decode, 20, "%s", exit_reason); |
| 172 | } |
| 173 | |
| 174 | static struct kvm_events_ops exit_events = { |
| 175 | .is_begin_event = exit_event_begin, |
| 176 | .is_end_event = exit_event_end, |
| 177 | .decode_key = exit_event_decode_key, |
| 178 | .name = "VM-EXIT" |
| 179 | }; |
| 180 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 181 | /* |
| 182 | * For the mmio events, we treat: |
| 183 | * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry |
| 184 | * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...). |
| 185 | */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 186 | static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample, |
| 187 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 188 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 189 | key->key = perf_evsel__intval(evsel, sample, "gpa"); |
| 190 | key->info = perf_evsel__intval(evsel, sample, "type"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | #define KVM_TRACE_MMIO_READ_UNSATISFIED 0 |
| 194 | #define KVM_TRACE_MMIO_READ 1 |
| 195 | #define KVM_TRACE_MMIO_WRITE 2 |
| 196 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 197 | static bool mmio_event_begin(struct perf_evsel *evsel, |
| 198 | struct perf_sample *sample, struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 199 | { |
| 200 | /* MMIO read begin event in kernel. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 201 | if (kvm_exit_event(evsel)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 202 | return true; |
| 203 | |
| 204 | /* MMIO write begin event in kernel. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 205 | if (!strcmp(evsel->name, "kvm:kvm_mmio") && |
| 206 | perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) { |
| 207 | mmio_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
| 211 | return false; |
| 212 | } |
| 213 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 214 | static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample, |
| 215 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 216 | { |
| 217 | /* MMIO write end event in kernel. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 218 | if (kvm_entry_event(evsel)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 219 | return true; |
| 220 | |
| 221 | /* MMIO read end event in kernel.*/ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 222 | if (!strcmp(evsel->name, "kvm:kvm_mmio") && |
| 223 | perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) { |
| 224 | mmio_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 231 | static void mmio_event_decode_key(struct perf_kvm *kvm __maybe_unused, |
| 232 | struct event_key *key, |
| 233 | char decode[20]) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 234 | { |
| 235 | scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key, |
| 236 | key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R"); |
| 237 | } |
| 238 | |
| 239 | static struct kvm_events_ops mmio_events = { |
| 240 | .is_begin_event = mmio_event_begin, |
| 241 | .is_end_event = mmio_event_end, |
| 242 | .decode_key = mmio_event_decode_key, |
| 243 | .name = "MMIO Access" |
| 244 | }; |
| 245 | |
| 246 | /* 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] | 247 | static void ioport_event_get_key(struct perf_evsel *evsel, |
| 248 | struct perf_sample *sample, |
| 249 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 250 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 251 | key->key = perf_evsel__intval(evsel, sample, "port"); |
| 252 | key->info = perf_evsel__intval(evsel, sample, "rw"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 253 | } |
| 254 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 255 | static bool ioport_event_begin(struct perf_evsel *evsel, |
| 256 | struct perf_sample *sample, |
| 257 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 258 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 259 | if (!strcmp(evsel->name, "kvm:kvm_pio")) { |
| 260 | ioport_event_get_key(evsel, sample, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 267 | static bool ioport_event_end(struct perf_evsel *evsel, |
| 268 | struct perf_sample *sample __maybe_unused, |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 269 | struct event_key *key __maybe_unused) |
| 270 | { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 271 | return kvm_entry_event(evsel); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 272 | } |
| 273 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 274 | static void ioport_event_decode_key(struct perf_kvm *kvm __maybe_unused, |
| 275 | struct event_key *key, |
| 276 | char decode[20]) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 277 | { |
| 278 | scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key, |
| 279 | key->info ? "POUT" : "PIN"); |
| 280 | } |
| 281 | |
| 282 | static struct kvm_events_ops ioport_events = { |
| 283 | .is_begin_event = ioport_event_begin, |
| 284 | .is_end_event = ioport_event_end, |
| 285 | .decode_key = ioport_event_decode_key, |
| 286 | .name = "IO Port Access" |
| 287 | }; |
| 288 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 289 | static bool register_kvm_events_ops(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 290 | { |
| 291 | bool ret = true; |
| 292 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 293 | if (!strcmp(kvm->report_event, "vmexit")) |
| 294 | kvm->events_ops = &exit_events; |
| 295 | else if (!strcmp(kvm->report_event, "mmio")) |
| 296 | kvm->events_ops = &mmio_events; |
| 297 | else if (!strcmp(kvm->report_event, "ioport")) |
| 298 | kvm->events_ops = &ioport_events; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 299 | else { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 300 | pr_err("Unknown report event:%s\n", kvm->report_event); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 301 | ret = false; |
| 302 | } |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 307 | struct vcpu_event_record { |
| 308 | int vcpu_id; |
| 309 | u64 start_time; |
| 310 | struct kvm_event *last_event; |
| 311 | }; |
| 312 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 313 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 314 | static void init_kvm_event_record(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 315 | { |
David Ahern | b880dee | 2012-10-08 11:17:30 -0600 | [diff] [blame^] | 316 | unsigned int i; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 317 | |
David Ahern | b880dee | 2012-10-08 11:17:30 -0600 | [diff] [blame^] | 318 | for (i = 0; i < EVENTS_CACHE_SIZE; i++) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 319 | INIT_LIST_HEAD(&kvm->kvm_events_cache[i]); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static int kvm_events_hash_fn(u64 key) |
| 323 | { |
| 324 | return key & (EVENTS_CACHE_SIZE - 1); |
| 325 | } |
| 326 | |
| 327 | static bool kvm_event_expand(struct kvm_event *event, int vcpu_id) |
| 328 | { |
| 329 | int old_max_vcpu = event->max_vcpu; |
| 330 | |
| 331 | if (vcpu_id < event->max_vcpu) |
| 332 | return true; |
| 333 | |
| 334 | while (event->max_vcpu <= vcpu_id) |
| 335 | event->max_vcpu += DEFAULT_VCPU_NUM; |
| 336 | |
| 337 | event->vcpu = realloc(event->vcpu, |
| 338 | event->max_vcpu * sizeof(*event->vcpu)); |
| 339 | if (!event->vcpu) { |
| 340 | pr_err("Not enough memory\n"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | memset(event->vcpu + old_max_vcpu, 0, |
| 345 | (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu)); |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | static struct kvm_event *kvm_alloc_init_event(struct event_key *key) |
| 350 | { |
| 351 | struct kvm_event *event; |
| 352 | |
| 353 | event = zalloc(sizeof(*event)); |
| 354 | if (!event) { |
| 355 | pr_err("Not enough memory\n"); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | event->key = *key; |
| 360 | return event; |
| 361 | } |
| 362 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 363 | static struct kvm_event *find_create_kvm_event(struct perf_kvm *kvm, |
| 364 | struct event_key *key) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 365 | { |
| 366 | struct kvm_event *event; |
| 367 | struct list_head *head; |
| 368 | |
| 369 | BUG_ON(key->key == INVALID_KEY); |
| 370 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 371 | head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)]; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 372 | list_for_each_entry(event, head, hash_entry) |
| 373 | if (event->key.key == key->key && event->key.info == key->info) |
| 374 | return event; |
| 375 | |
| 376 | event = kvm_alloc_init_event(key); |
| 377 | if (!event) |
| 378 | return NULL; |
| 379 | |
| 380 | list_add(&event->hash_entry, head); |
| 381 | return event; |
| 382 | } |
| 383 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 384 | static bool handle_begin_event(struct perf_kvm *kvm, |
| 385 | struct vcpu_event_record *vcpu_record, |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 386 | struct event_key *key, u64 timestamp) |
| 387 | { |
| 388 | struct kvm_event *event = NULL; |
| 389 | |
| 390 | if (key->key != INVALID_KEY) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 391 | event = find_create_kvm_event(kvm, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 392 | |
| 393 | vcpu_record->last_event = event; |
| 394 | vcpu_record->start_time = timestamp; |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff) |
| 400 | { |
| 401 | kvm_stats->time += time_diff; |
| 402 | update_stats(&kvm_stats->stats, time_diff); |
| 403 | } |
| 404 | |
| 405 | static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event) |
| 406 | { |
| 407 | struct kvm_event_stats *kvm_stats = &event->total; |
| 408 | |
| 409 | if (vcpu_id != -1) |
| 410 | kvm_stats = &event->vcpu[vcpu_id]; |
| 411 | |
| 412 | return rel_stddev_stats(stddev_stats(&kvm_stats->stats), |
| 413 | avg_stats(&kvm_stats->stats)); |
| 414 | } |
| 415 | |
| 416 | static bool update_kvm_event(struct kvm_event *event, int vcpu_id, |
| 417 | u64 time_diff) |
| 418 | { |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 419 | if (vcpu_id == -1) { |
| 420 | kvm_update_event_stats(&event->total, time_diff); |
| 421 | return true; |
| 422 | } |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 423 | |
| 424 | if (!kvm_event_expand(event, vcpu_id)) |
| 425 | return false; |
| 426 | |
| 427 | kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff); |
| 428 | return true; |
| 429 | } |
| 430 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 431 | static bool handle_end_event(struct perf_kvm *kvm, |
| 432 | struct vcpu_event_record *vcpu_record, |
| 433 | struct event_key *key, |
| 434 | u64 timestamp) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 435 | { |
| 436 | struct kvm_event *event; |
| 437 | u64 time_begin, time_diff; |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 438 | int vcpu; |
| 439 | |
| 440 | if (kvm->trace_vcpu == -1) |
| 441 | vcpu = -1; |
| 442 | else |
| 443 | vcpu = vcpu_record->vcpu_id; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 444 | |
| 445 | event = vcpu_record->last_event; |
| 446 | time_begin = vcpu_record->start_time; |
| 447 | |
| 448 | /* The begin event is not caught. */ |
| 449 | if (!time_begin) |
| 450 | return true; |
| 451 | |
| 452 | /* |
| 453 | * In some case, the 'begin event' only records the start timestamp, |
| 454 | * the actual event is recognized in the 'end event' (e.g. mmio-event). |
| 455 | */ |
| 456 | |
| 457 | /* Both begin and end events did not get the key. */ |
| 458 | if (!event && key->key == INVALID_KEY) |
| 459 | return true; |
| 460 | |
| 461 | if (!event) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 462 | event = find_create_kvm_event(kvm, key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 463 | |
| 464 | if (!event) |
| 465 | return false; |
| 466 | |
| 467 | vcpu_record->last_event = NULL; |
| 468 | vcpu_record->start_time = 0; |
| 469 | |
| 470 | BUG_ON(timestamp < time_begin); |
| 471 | |
| 472 | time_diff = timestamp - time_begin; |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 473 | return update_kvm_event(event, vcpu, time_diff); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 474 | } |
| 475 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 476 | static |
| 477 | struct vcpu_event_record *per_vcpu_record(struct thread *thread, |
| 478 | struct perf_evsel *evsel, |
| 479 | struct perf_sample *sample) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 480 | { |
| 481 | /* Only kvm_entry records vcpu id. */ |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 482 | if (!thread->priv && kvm_entry_event(evsel)) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 483 | struct vcpu_event_record *vcpu_record; |
| 484 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 485 | vcpu_record = zalloc(sizeof(*vcpu_record)); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 486 | if (!vcpu_record) { |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 487 | pr_err("%s: Not enough memory\n", __func__); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 488 | return NULL; |
| 489 | } |
| 490 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 491 | vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id"); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 492 | thread->priv = vcpu_record; |
| 493 | } |
| 494 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 495 | return thread->priv; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 496 | } |
| 497 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 498 | static bool handle_kvm_event(struct perf_kvm *kvm, |
| 499 | struct thread *thread, |
| 500 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 501 | struct perf_sample *sample) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 502 | { |
| 503 | struct vcpu_event_record *vcpu_record; |
| 504 | struct event_key key = {.key = INVALID_KEY}; |
| 505 | |
Arnaldo Carvalho de Melo | 14907e7 | 2012-09-21 16:10:26 -0300 | [diff] [blame] | 506 | vcpu_record = per_vcpu_record(thread, evsel, sample); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 507 | if (!vcpu_record) |
| 508 | return true; |
| 509 | |
David Ahern | 2aa8eab | 2012-10-08 11:17:35 -0600 | [diff] [blame] | 510 | /* only process events for vcpus user cares about */ |
| 511 | if ((kvm->trace_vcpu != -1) && |
| 512 | (kvm->trace_vcpu != vcpu_record->vcpu_id)) |
| 513 | return true; |
| 514 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 515 | if (kvm->events_ops->is_begin_event(evsel, sample, &key)) |
| 516 | return handle_begin_event(kvm, vcpu_record, &key, sample->time); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 517 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 518 | if (kvm->events_ops->is_end_event(evsel, sample, &key)) |
| 519 | return handle_end_event(kvm, vcpu_record, &key, sample->time); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 520 | |
| 521 | return true; |
| 522 | } |
| 523 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 524 | #define GET_EVENT_KEY(func, field) \ |
| 525 | static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \ |
| 526 | { \ |
| 527 | if (vcpu == -1) \ |
| 528 | return event->total.field; \ |
| 529 | \ |
| 530 | if (vcpu >= event->max_vcpu) \ |
| 531 | return 0; \ |
| 532 | \ |
| 533 | return event->vcpu[vcpu].field; \ |
| 534 | } |
| 535 | |
| 536 | #define COMPARE_EVENT_KEY(func, field) \ |
| 537 | GET_EVENT_KEY(func, field) \ |
| 538 | static int compare_kvm_event_ ## func(struct kvm_event *one, \ |
| 539 | struct kvm_event *two, int vcpu)\ |
| 540 | { \ |
| 541 | return get_event_ ##func(one, vcpu) > \ |
| 542 | get_event_ ##func(two, vcpu); \ |
| 543 | } |
| 544 | |
| 545 | GET_EVENT_KEY(time, time); |
| 546 | COMPARE_EVENT_KEY(count, stats.n); |
| 547 | COMPARE_EVENT_KEY(mean, stats.mean); |
| 548 | |
| 549 | #define DEF_SORT_NAME_KEY(name, compare_key) \ |
| 550 | { #name, compare_kvm_event_ ## compare_key } |
| 551 | |
| 552 | static struct kvm_event_key keys[] = { |
| 553 | DEF_SORT_NAME_KEY(sample, count), |
| 554 | DEF_SORT_NAME_KEY(time, mean), |
| 555 | { NULL, NULL } |
| 556 | }; |
| 557 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 558 | static bool select_key(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 559 | { |
| 560 | int i; |
| 561 | |
| 562 | for (i = 0; keys[i].name; i++) { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 563 | if (!strcmp(keys[i].name, kvm->sort_key)) { |
| 564 | kvm->compare = keys[i].key; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | } |
| 568 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 569 | pr_err("Unknown compare key:%s\n", kvm->sort_key); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 570 | return false; |
| 571 | } |
| 572 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 573 | static void insert_to_result(struct rb_root *result, struct kvm_event *event, |
| 574 | key_cmp_fun bigger, int vcpu) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 575 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 576 | struct rb_node **rb = &result->rb_node; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 577 | struct rb_node *parent = NULL; |
| 578 | struct kvm_event *p; |
| 579 | |
| 580 | while (*rb) { |
| 581 | p = container_of(*rb, struct kvm_event, rb); |
| 582 | parent = *rb; |
| 583 | |
| 584 | if (bigger(event, p, vcpu)) |
| 585 | rb = &(*rb)->rb_left; |
| 586 | else |
| 587 | rb = &(*rb)->rb_right; |
| 588 | } |
| 589 | |
| 590 | rb_link_node(&event->rb, parent, rb); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 591 | rb_insert_color(&event->rb, result); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 592 | } |
| 593 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 594 | static void update_total_count(struct perf_kvm *kvm, struct kvm_event *event) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 595 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 596 | int vcpu = kvm->trace_vcpu; |
| 597 | |
| 598 | kvm->total_count += get_event_count(event, vcpu); |
| 599 | kvm->total_time += get_event_time(event, vcpu); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static bool event_is_valid(struct kvm_event *event, int vcpu) |
| 603 | { |
| 604 | return !!get_event_count(event, vcpu); |
| 605 | } |
| 606 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 607 | static void sort_result(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 608 | { |
| 609 | unsigned int i; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 610 | int vcpu = kvm->trace_vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 611 | struct kvm_event *event; |
| 612 | |
| 613 | for (i = 0; i < EVENTS_CACHE_SIZE; i++) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 614 | list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 615 | if (event_is_valid(event, vcpu)) { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 616 | update_total_count(kvm, event); |
| 617 | insert_to_result(&kvm->result, event, |
| 618 | kvm->compare, vcpu); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | /* returns left most element of result, and erase it */ |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 623 | static struct kvm_event *pop_from_result(struct rb_root *result) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 624 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 625 | struct rb_node *node = rb_first(result); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 626 | |
| 627 | if (!node) |
| 628 | return NULL; |
| 629 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 630 | rb_erase(node, result); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 631 | return container_of(node, struct kvm_event, rb); |
| 632 | } |
| 633 | |
| 634 | static void print_vcpu_info(int vcpu) |
| 635 | { |
| 636 | pr_info("Analyze events for "); |
| 637 | |
| 638 | if (vcpu == -1) |
| 639 | pr_info("all VCPUs:\n\n"); |
| 640 | else |
| 641 | pr_info("VCPU %d:\n\n", vcpu); |
| 642 | } |
| 643 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 644 | static void print_result(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 645 | { |
| 646 | char decode[20]; |
| 647 | struct kvm_event *event; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 648 | int vcpu = kvm->trace_vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 649 | |
| 650 | pr_info("\n\n"); |
| 651 | print_vcpu_info(vcpu); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 652 | pr_info("%20s ", kvm->events_ops->name); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 653 | pr_info("%10s ", "Samples"); |
| 654 | pr_info("%9s ", "Samples%"); |
| 655 | |
| 656 | pr_info("%9s ", "Time%"); |
| 657 | pr_info("%16s ", "Avg time"); |
| 658 | pr_info("\n\n"); |
| 659 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 660 | while ((event = pop_from_result(&kvm->result))) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 661 | u64 ecount, etime; |
| 662 | |
| 663 | ecount = get_event_count(event, vcpu); |
| 664 | etime = get_event_time(event, vcpu); |
| 665 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 666 | kvm->events_ops->decode_key(kvm, &event->key, decode); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 667 | pr_info("%20s ", decode); |
| 668 | pr_info("%10llu ", (unsigned long long)ecount); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 669 | pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100); |
| 670 | pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 671 | pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3, |
| 672 | kvm_event_rel_stddev(vcpu, event)); |
| 673 | pr_info("\n"); |
| 674 | } |
| 675 | |
| 676 | pr_info("\nTotal Samples:%lld, Total events handled time:%.2fus.\n\n", |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 677 | (unsigned long long)kvm->total_count, kvm->total_time / 1e3); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 678 | } |
| 679 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 680 | static int process_sample_event(struct perf_tool *tool, |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 681 | union perf_event *event, |
| 682 | struct perf_sample *sample, |
| 683 | struct perf_evsel *evsel, |
| 684 | struct machine *machine) |
| 685 | { |
| 686 | struct thread *thread = machine__findnew_thread(machine, sample->tid); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 687 | struct perf_kvm *kvm = container_of(tool, struct perf_kvm, tool); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 688 | |
| 689 | if (thread == NULL) { |
| 690 | pr_debug("problem processing %d event, skipping it.\n", |
| 691 | event->header.type); |
| 692 | return -1; |
| 693 | } |
| 694 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 695 | if (!handle_kvm_event(kvm, thread, evsel, sample)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 696 | return -1; |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 701 | static int get_cpu_isa(struct perf_session *session) |
| 702 | { |
Namhyung Kim | 2f9e97a | 2012-09-24 17:15:02 +0900 | [diff] [blame] | 703 | char *cpuid = session->header.env.cpuid; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 704 | int isa; |
| 705 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 706 | if (strstr(cpuid, "Intel")) |
| 707 | isa = 1; |
| 708 | else if (strstr(cpuid, "AMD")) |
| 709 | isa = 0; |
| 710 | else { |
| 711 | pr_err("CPU %s is not supported.\n", cpuid); |
| 712 | isa = -ENOTSUP; |
| 713 | } |
| 714 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 715 | return isa; |
| 716 | } |
| 717 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 718 | static int read_events(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 719 | { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 720 | int ret; |
| 721 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 722 | struct perf_tool eops = { |
| 723 | .sample = process_sample_event, |
| 724 | .comm = perf_event__process_comm, |
| 725 | .ordered_samples = true, |
| 726 | }; |
| 727 | |
| 728 | kvm->tool = eops; |
| 729 | kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false, |
| 730 | &kvm->tool); |
| 731 | if (!kvm->session) { |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 732 | pr_err("Initializing perf session failed\n"); |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 736 | if (!perf_session__has_traces(kvm->session, "kvm record")) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 737 | return -EINVAL; |
| 738 | |
| 739 | /* |
| 740 | * Do not use 'isa' recorded in kvm_exit tracepoint since it is not |
| 741 | * traced in the old kernel. |
| 742 | */ |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 743 | ret = get_cpu_isa(kvm->session); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 744 | |
| 745 | if (ret < 0) |
| 746 | return ret; |
| 747 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 748 | if (ret == 1) { |
| 749 | kvm->exit_reasons = vmx_exit_reasons; |
| 750 | kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons); |
| 751 | kvm->exit_reasons_isa = "VMX"; |
| 752 | } |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 753 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 754 | return perf_session__process_events(kvm->session, &kvm->tool); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static bool verify_vcpu(int vcpu) |
| 758 | { |
| 759 | if (vcpu != -1 && vcpu < 0) { |
| 760 | pr_err("Invalid vcpu:%d.\n", vcpu); |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | return true; |
| 765 | } |
| 766 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 767 | static int kvm_events_report_vcpu(struct perf_kvm *kvm) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 768 | { |
| 769 | int ret = -EINVAL; |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 770 | int vcpu = kvm->trace_vcpu; |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 771 | |
| 772 | if (!verify_vcpu(vcpu)) |
| 773 | goto exit; |
| 774 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 775 | if (!select_key(kvm)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 776 | goto exit; |
| 777 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 778 | if (!register_kvm_events_ops(kvm)) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 779 | goto exit; |
| 780 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 781 | init_kvm_event_record(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 782 | setup_pager(); |
| 783 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 784 | ret = read_events(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 785 | if (ret) |
| 786 | goto exit; |
| 787 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 788 | sort_result(kvm); |
| 789 | print_result(kvm); |
| 790 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 791 | exit: |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | static const char * const record_args[] = { |
| 796 | "record", |
| 797 | "-R", |
| 798 | "-f", |
| 799 | "-m", "1024", |
| 800 | "-c", "1", |
| 801 | "-e", "kvm:kvm_entry", |
| 802 | "-e", "kvm:kvm_exit", |
| 803 | "-e", "kvm:kvm_mmio", |
| 804 | "-e", "kvm:kvm_pio", |
| 805 | }; |
| 806 | |
| 807 | #define STRDUP_FAIL_EXIT(s) \ |
| 808 | ({ char *_p; \ |
| 809 | _p = strdup(s); \ |
| 810 | if (!_p) \ |
| 811 | return -ENOMEM; \ |
| 812 | _p; \ |
| 813 | }) |
| 814 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 815 | static int kvm_events_record(struct perf_kvm *kvm, int argc, const char **argv) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 816 | { |
| 817 | unsigned int rec_argc, i, j; |
| 818 | const char **rec_argv; |
| 819 | |
| 820 | rec_argc = ARRAY_SIZE(record_args) + argc + 2; |
| 821 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 822 | |
| 823 | if (rec_argv == NULL) |
| 824 | return -ENOMEM; |
| 825 | |
| 826 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 827 | rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]); |
| 828 | |
| 829 | rec_argv[i++] = STRDUP_FAIL_EXIT("-o"); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 830 | rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 831 | |
| 832 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 833 | rec_argv[i] = argv[j]; |
| 834 | |
| 835 | return cmd_record(i, rec_argv, NULL); |
| 836 | } |
| 837 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 838 | static int kvm_events_report(struct perf_kvm *kvm, int argc, const char **argv) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 839 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 840 | const struct option kvm_events_report_options[] = { |
| 841 | OPT_STRING(0, "event", &kvm->report_event, "report event", |
| 842 | "event for reporting: vmexit, mmio, ioport"), |
| 843 | OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu, |
| 844 | "vcpu id to report"), |
| 845 | OPT_STRING('k', "key", &kvm->sort_key, "sort-key", |
| 846 | "key for sorting: sample(sort by samples number)" |
| 847 | " time (sort by avg time)"), |
| 848 | OPT_END() |
| 849 | }; |
| 850 | |
| 851 | const char * const kvm_events_report_usage[] = { |
| 852 | "perf kvm stat report [<options>]", |
| 853 | NULL |
| 854 | }; |
| 855 | |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 856 | symbol__init(); |
| 857 | |
| 858 | if (argc) { |
| 859 | argc = parse_options(argc, argv, |
| 860 | kvm_events_report_options, |
| 861 | kvm_events_report_usage, 0); |
| 862 | if (argc) |
| 863 | usage_with_options(kvm_events_report_usage, |
| 864 | kvm_events_report_options); |
| 865 | } |
| 866 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 867 | return kvm_events_report_vcpu(kvm); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static void print_kvm_stat_usage(void) |
| 871 | { |
| 872 | printf("Usage: perf kvm stat <command>\n\n"); |
| 873 | |
| 874 | printf("# Available commands:\n"); |
| 875 | printf("\trecord: record kvm events\n"); |
| 876 | printf("\treport: report statistical data of kvm events\n"); |
| 877 | |
| 878 | printf("\nOtherwise, it is the alias of 'perf stat':\n"); |
| 879 | } |
| 880 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 881 | static int kvm_cmd_stat(struct perf_kvm *kvm, int argc, const char **argv) |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 882 | { |
| 883 | if (argc == 1) { |
| 884 | print_kvm_stat_usage(); |
| 885 | goto perf_stat; |
| 886 | } |
| 887 | |
| 888 | if (!strncmp(argv[1], "rec", 3)) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 889 | return kvm_events_record(kvm, argc - 1, argv + 1); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 890 | |
| 891 | if (!strncmp(argv[1], "rep", 3)) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 892 | return kvm_events_report(kvm, argc - 1 , argv + 1); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 893 | |
| 894 | perf_stat: |
| 895 | return cmd_stat(argc, argv, NULL); |
| 896 | } |
| 897 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 898 | static int __cmd_record(struct perf_kvm *kvm, int argc, const char **argv) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 899 | { |
| 900 | int rec_argc, i = 0, j; |
| 901 | const char **rec_argv; |
| 902 | |
| 903 | rec_argc = argc + 2; |
| 904 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 905 | rec_argv[i++] = strdup("record"); |
| 906 | rec_argv[i++] = strdup("-o"); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 907 | rec_argv[i++] = strdup(kvm->file_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 908 | for (j = 1; j < argc; j++, i++) |
| 909 | rec_argv[i] = argv[j]; |
| 910 | |
| 911 | BUG_ON(i != rec_argc); |
| 912 | |
| 913 | return cmd_record(i, rec_argv, NULL); |
| 914 | } |
| 915 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 916 | static int __cmd_report(struct perf_kvm *kvm, int argc, const char **argv) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 917 | { |
| 918 | int rec_argc, i = 0, j; |
| 919 | const char **rec_argv; |
| 920 | |
| 921 | rec_argc = argc + 2; |
| 922 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 923 | rec_argv[i++] = strdup("report"); |
| 924 | rec_argv[i++] = strdup("-i"); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 925 | rec_argv[i++] = strdup(kvm->file_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 926 | for (j = 1; j < argc; j++, i++) |
| 927 | rec_argv[i] = argv[j]; |
| 928 | |
| 929 | BUG_ON(i != rec_argc); |
| 930 | |
| 931 | return cmd_report(i, rec_argv, NULL); |
| 932 | } |
| 933 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 934 | static int __cmd_buildid_list(struct perf_kvm *kvm, int argc, const char **argv) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 935 | { |
| 936 | int rec_argc, i = 0, j; |
| 937 | const char **rec_argv; |
| 938 | |
| 939 | rec_argc = argc + 2; |
| 940 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 941 | rec_argv[i++] = strdup("buildid-list"); |
| 942 | rec_argv[i++] = strdup("-i"); |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 943 | rec_argv[i++] = strdup(kvm->file_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 944 | for (j = 1; j < argc; j++, i++) |
| 945 | rec_argv[i] = argv[j]; |
| 946 | |
| 947 | BUG_ON(i != rec_argc); |
| 948 | |
| 949 | return cmd_buildid_list(i, rec_argv, NULL); |
| 950 | } |
| 951 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 952 | 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] | 953 | { |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 954 | struct perf_kvm kvm = { |
| 955 | .trace_vcpu = -1, |
| 956 | .report_event = "vmexit", |
| 957 | .sort_key = "sample", |
| 958 | |
| 959 | .exit_reasons = svm_exit_reasons, |
| 960 | .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons), |
| 961 | .exit_reasons_isa = "SVM", |
| 962 | }; |
| 963 | |
| 964 | const struct option kvm_options[] = { |
| 965 | OPT_STRING('i', "input", &kvm.file_name, "file", |
| 966 | "Input file name"), |
| 967 | OPT_STRING('o', "output", &kvm.file_name, "file", |
| 968 | "Output file name"), |
| 969 | OPT_BOOLEAN(0, "guest", &perf_guest, |
| 970 | "Collect guest os data"), |
| 971 | OPT_BOOLEAN(0, "host", &perf_host, |
| 972 | "Collect host os data"), |
| 973 | OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory", |
| 974 | "guest mount directory under which every guest os" |
| 975 | " instance has a subdir"), |
| 976 | OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name, |
| 977 | "file", "file saving guest os vmlinux"), |
| 978 | OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms, |
| 979 | "file", "file saving guest os /proc/kallsyms"), |
| 980 | OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules, |
| 981 | "file", "file saving guest os /proc/modules"), |
| 982 | OPT_END() |
| 983 | }; |
| 984 | |
| 985 | |
| 986 | const char * const kvm_usage[] = { |
| 987 | "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}", |
| 988 | NULL |
| 989 | }; |
| 990 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 991 | perf_host = 0; |
| 992 | perf_guest = 1; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 993 | |
| 994 | argc = parse_options(argc, argv, kvm_options, kvm_usage, |
| 995 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 996 | if (!argc) |
| 997 | usage_with_options(kvm_usage, kvm_options); |
| 998 | |
| 999 | if (!perf_host) |
| 1000 | perf_guest = 1; |
| 1001 | |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1002 | if (!kvm.file_name) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1003 | if (perf_host && !perf_guest) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1004 | kvm.file_name = strdup("perf.data.host"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1005 | else if (!perf_host && perf_guest) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1006 | kvm.file_name = strdup("perf.data.guest"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1007 | else |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1008 | kvm.file_name = strdup("perf.data.kvm"); |
| 1009 | |
| 1010 | if (!kvm.file_name) { |
| 1011 | pr_err("Failed to allocate memory for filename\n"); |
| 1012 | return -ENOMEM; |
| 1013 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if (!strncmp(argv[0], "rec", 3)) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1017 | return __cmd_record(&kvm, argc, argv); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1018 | else if (!strncmp(argv[0], "rep", 3)) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1019 | return __cmd_report(&kvm, argc, argv); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1020 | else if (!strncmp(argv[0], "diff", 4)) |
| 1021 | return cmd_diff(argc, argv, NULL); |
| 1022 | else if (!strncmp(argv[0], "top", 3)) |
| 1023 | return cmd_top(argc, argv, NULL); |
| 1024 | else if (!strncmp(argv[0], "buildid-list", 12)) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1025 | return __cmd_buildid_list(&kvm, argc, argv); |
Xiao Guangrong | bcf6edc | 2012-09-17 16:31:15 +0800 | [diff] [blame] | 1026 | else if (!strncmp(argv[0], "stat", 4)) |
David Ahern | de332ac | 2012-10-02 22:09:53 -0600 | [diff] [blame] | 1027 | return kvm_cmd_stat(&kvm, argc, argv); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1028 | else |
| 1029 | usage_with_options(kvm_usage, kvm_options); |
| 1030 | |
| 1031 | return 0; |
| 1032 | } |