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