Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / tools / perf / builtin-kvm.c
1 #include "builtin.h"
2 #include "perf.h"
3
4 #include "util/evsel.h"
5 #include "util/evlist.h"
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"
12 #include "util/intlist.h"
13 #include "util/parse-options.h"
14 #include "util/trace-event.h"
15 #include "util/debug.h"
16 #include <lk/debugfs.h>
17 #include "util/tool.h"
18 #include "util/stat.h"
19 #include "util/top.h"
20
21 #include <sys/prctl.h>
22 #include <sys/timerfd.h>
23
24 #include <termios.h>
25 #include <semaphore.h>
26 #include <pthread.h>
27 #include <math.h>
28
29 #if defined(__i386__) || defined(__x86_64__)
30 #include <asm/svm.h>
31 #include <asm/vmx.h>
32 #include <asm/kvm.h>
33
34 struct event_key {
35 #define INVALID_KEY (~0ULL)
36 u64 key;
37 int info;
38 };
39
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
66 struct perf_kvm_stat;
67
68 struct kvm_events_ops {
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);
74 void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
75 char decode[20]);
76 const char *name;
77 };
78
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
87 struct perf_kvm_stat {
88 struct perf_tool tool;
89 struct perf_record_opts opts;
90 struct perf_evlist *evlist;
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];
105
106 u64 total_time;
107 u64 total_count;
108 u64 lost_events;
109
110 const char *pid_str;
111 struct intlist *pid_list;
112
113 struct rb_root result;
114
115 int timerfd;
116 unsigned int display_time;
117 bool live;
118 };
119
120
121 static void exit_event_get_key(struct perf_evsel *evsel,
122 struct perf_sample *sample,
123 struct event_key *key)
124 {
125 key->info = 0;
126 key->key = perf_evsel__intval(evsel, sample, "exit_reason");
127 }
128
129 static bool kvm_exit_event(struct perf_evsel *evsel)
130 {
131 return !strcmp(evsel->name, "kvm:kvm_exit");
132 }
133
134 static bool exit_event_begin(struct perf_evsel *evsel,
135 struct perf_sample *sample, struct event_key *key)
136 {
137 if (kvm_exit_event(evsel)) {
138 exit_event_get_key(evsel, sample, key);
139 return true;
140 }
141
142 return false;
143 }
144
145 static bool kvm_entry_event(struct perf_evsel *evsel)
146 {
147 return !strcmp(evsel->name, "kvm:kvm_entry");
148 }
149
150 static bool exit_event_end(struct perf_evsel *evsel,
151 struct perf_sample *sample __maybe_unused,
152 struct event_key *key __maybe_unused)
153 {
154 return kvm_entry_event(evsel);
155 }
156
157 static struct exit_reasons_table vmx_exit_reasons[] = {
158 VMX_EXIT_REASONS
159 };
160
161 static struct exit_reasons_table svm_exit_reasons[] = {
162 SVM_EXIT_REASONS
163 };
164
165 static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
166 {
167 int i = kvm->exit_reasons_size;
168 struct exit_reasons_table *tbl = kvm->exit_reasons;
169
170 while (i--) {
171 if (tbl->exit_code == exit_code)
172 return tbl->reason;
173 tbl++;
174 }
175
176 pr_err("unknown kvm exit code:%lld on %s\n",
177 (unsigned long long)exit_code, kvm->exit_reasons_isa);
178 return "UNKNOWN";
179 }
180
181 static void exit_event_decode_key(struct perf_kvm_stat *kvm,
182 struct event_key *key,
183 char decode[20])
184 {
185 const char *exit_reason = get_exit_reason(kvm, key->key);
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
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 */
202 static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
203 struct event_key *key)
204 {
205 key->key = perf_evsel__intval(evsel, sample, "gpa");
206 key->info = perf_evsel__intval(evsel, sample, "type");
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
213 static bool mmio_event_begin(struct perf_evsel *evsel,
214 struct perf_sample *sample, struct event_key *key)
215 {
216 /* MMIO read begin event in kernel. */
217 if (kvm_exit_event(evsel))
218 return true;
219
220 /* MMIO write begin event in kernel. */
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);
224 return true;
225 }
226
227 return false;
228 }
229
230 static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
231 struct event_key *key)
232 {
233 /* MMIO write end event in kernel. */
234 if (kvm_entry_event(evsel))
235 return true;
236
237 /* MMIO read end event in kernel.*/
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);
241 return true;
242 }
243
244 return false;
245 }
246
247 static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
248 struct event_key *key,
249 char decode[20])
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. */
263 static void ioport_event_get_key(struct perf_evsel *evsel,
264 struct perf_sample *sample,
265 struct event_key *key)
266 {
267 key->key = perf_evsel__intval(evsel, sample, "port");
268 key->info = perf_evsel__intval(evsel, sample, "rw");
269 }
270
271 static bool ioport_event_begin(struct perf_evsel *evsel,
272 struct perf_sample *sample,
273 struct event_key *key)
274 {
275 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
276 ioport_event_get_key(evsel, sample, key);
277 return true;
278 }
279
280 return false;
281 }
282
283 static bool ioport_event_end(struct perf_evsel *evsel,
284 struct perf_sample *sample __maybe_unused,
285 struct event_key *key __maybe_unused)
286 {
287 return kvm_entry_event(evsel);
288 }
289
290 static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
291 struct event_key *key,
292 char decode[20])
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
305 static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
306 {
307 bool ret = true;
308
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;
315 else {
316 pr_err("Unknown report event:%s\n", kvm->report_event);
317 ret = false;
318 }
319
320 return ret;
321 }
322
323 struct vcpu_event_record {
324 int vcpu_id;
325 u64 start_time;
326 struct kvm_event *last_event;
327 };
328
329
330 static void init_kvm_event_record(struct perf_kvm_stat *kvm)
331 {
332 unsigned int i;
333
334 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
335 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
336 }
337
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;
343 int j;
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 */
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 }
356 }
357 }
358 }
359
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;
368 void *prev;
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
376 prev = event->vcpu;
377 event->vcpu = realloc(event->vcpu,
378 event->max_vcpu * sizeof(*event->vcpu));
379 if (!event->vcpu) {
380 free(prev);
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
404 static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
405 struct event_key *key)
406 {
407 struct kvm_event *event;
408 struct list_head *head;
409
410 BUG_ON(key->key == INVALID_KEY);
411
412 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
413 list_for_each_entry(event, head, hash_entry) {
414 if (event->key.key == key->key && event->key.info == key->info)
415 return event;
416 }
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
426 static bool handle_begin_event(struct perf_kvm_stat *kvm,
427 struct vcpu_event_record *vcpu_record,
428 struct event_key *key, u64 timestamp)
429 {
430 struct kvm_event *event = NULL;
431
432 if (key->key != INVALID_KEY)
433 event = find_create_kvm_event(kvm, key);
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 {
461 if (vcpu_id == -1) {
462 kvm_update_event_stats(&event->total, time_diff);
463 return true;
464 }
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
473 static bool handle_end_event(struct perf_kvm_stat *kvm,
474 struct vcpu_event_record *vcpu_record,
475 struct event_key *key,
476 u64 timestamp)
477 {
478 struct kvm_event *event;
479 u64 time_begin, time_diff;
480 int vcpu;
481
482 if (kvm->trace_vcpu == -1)
483 vcpu = -1;
484 else
485 vcpu = vcpu_record->vcpu_id;
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)
504 event = find_create_kvm_event(kvm, key);
505
506 if (!event)
507 return false;
508
509 vcpu_record->last_event = NULL;
510 vcpu_record->start_time = 0;
511
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 }
517
518 time_diff = timestamp - time_begin;
519 return update_kvm_event(event, vcpu, time_diff);
520 }
521
522 static
523 struct vcpu_event_record *per_vcpu_record(struct thread *thread,
524 struct perf_evsel *evsel,
525 struct perf_sample *sample)
526 {
527 /* Only kvm_entry records vcpu id. */
528 if (!thread->priv && kvm_entry_event(evsel)) {
529 struct vcpu_event_record *vcpu_record;
530
531 vcpu_record = zalloc(sizeof(*vcpu_record));
532 if (!vcpu_record) {
533 pr_err("%s: Not enough memory\n", __func__);
534 return NULL;
535 }
536
537 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
538 thread->priv = vcpu_record;
539 }
540
541 return thread->priv;
542 }
543
544 static bool handle_kvm_event(struct perf_kvm_stat *kvm,
545 struct thread *thread,
546 struct perf_evsel *evsel,
547 struct perf_sample *sample)
548 {
549 struct vcpu_event_record *vcpu_record;
550 struct event_key key = {.key = INVALID_KEY};
551
552 vcpu_record = per_vcpu_record(thread, evsel, sample);
553 if (!vcpu_record)
554 return true;
555
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
561 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
562 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
563
564 if (kvm->events_ops->is_end_event(evsel, sample, &key))
565 return handle_end_event(kvm, vcpu_record, &key, sample->time);
566
567 return true;
568 }
569
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);
594 GET_EVENT_KEY(max, stats.max);
595 GET_EVENT_KEY(min, stats.min);
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
606 static bool select_key(struct perf_kvm_stat *kvm)
607 {
608 int i;
609
610 for (i = 0; keys[i].name; i++) {
611 if (!strcmp(keys[i].name, kvm->sort_key)) {
612 kvm->compare = keys[i].key;
613 return true;
614 }
615 }
616
617 pr_err("Unknown compare key:%s\n", kvm->sort_key);
618 return false;
619 }
620
621 static void insert_to_result(struct rb_root *result, struct kvm_event *event,
622 key_cmp_fun bigger, int vcpu)
623 {
624 struct rb_node **rb = &result->rb_node;
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);
639 rb_insert_color(&event->rb, result);
640 }
641
642 static void
643 update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
644 {
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);
649 }
650
651 static bool event_is_valid(struct kvm_event *event, int vcpu)
652 {
653 return !!get_event_count(event, vcpu);
654 }
655
656 static void sort_result(struct perf_kvm_stat *kvm)
657 {
658 unsigned int i;
659 int vcpu = kvm->trace_vcpu;
660 struct kvm_event *event;
661
662 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
663 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
664 if (event_is_valid(event, vcpu)) {
665 update_total_count(kvm, event);
666 insert_to_result(&kvm->result, event,
667 kvm->compare, vcpu);
668 }
669 }
670 }
671 }
672
673 /* returns left most element of result, and erase it */
674 static struct kvm_event *pop_from_result(struct rb_root *result)
675 {
676 struct rb_node *node = rb_first(result);
677
678 if (!node)
679 return NULL;
680
681 rb_erase(node, result);
682 return container_of(node, struct kvm_event, rb);
683 }
684
685 static void print_vcpu_info(struct perf_kvm_stat *kvm)
686 {
687 int vcpu = kvm->trace_vcpu;
688
689 pr_info("Analyze events for ");
690
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
700 if (vcpu == -1)
701 pr_info("all VCPUs:\n\n");
702 else
703 pr_info("VCPU %d:\n\n", vcpu);
704 }
705
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, &ltime)) {
714 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
715 pr_info("%s.%06ld", date, tv.tv_usec);
716 } else
717 pr_info("00:00:00.000000");
718
719 return;
720 }
721
722 static void print_result(struct perf_kvm_stat *kvm)
723 {
724 char decode[20];
725 struct kvm_event *event;
726 int vcpu = kvm->trace_vcpu;
727
728 if (kvm->live) {
729 puts(CONSOLE_CLEAR);
730 show_timeofday();
731 }
732
733 pr_info("\n\n");
734 print_vcpu_info(kvm);
735 pr_info("%20s ", kvm->events_ops->name);
736 pr_info("%10s ", "Samples");
737 pr_info("%9s ", "Samples%");
738
739 pr_info("%9s ", "Time%");
740 pr_info("%10s ", "Min Time");
741 pr_info("%10s ", "Max Time");
742 pr_info("%16s ", "Avg time");
743 pr_info("\n\n");
744
745 while ((event = pop_from_result(&kvm->result))) {
746 u64 ecount, etime, max, min;
747
748 ecount = get_event_count(event, vcpu);
749 etime = get_event_time(event, vcpu);
750 max = get_event_max(event, vcpu);
751 min = get_event_min(event, vcpu);
752
753 kvm->events_ops->decode_key(kvm, &event->key, decode);
754 pr_info("%20s ", decode);
755 pr_info("%10llu ", (unsigned long long)ecount);
756 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
757 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
758 pr_info("%8" PRIu64 "us ", min / 1000);
759 pr_info("%8" PRIu64 "us ", max / 1000);
760 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
761 kvm_event_rel_stddev(vcpu, event));
762 pr_info("\n");
763 }
764
765 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
766 kvm->total_count, kvm->total_time / 1e3);
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;
781 }
782
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
792 static int process_sample_event(struct perf_tool *tool,
793 union perf_event *event,
794 struct perf_sample *sample,
795 struct perf_evsel *evsel,
796 struct machine *machine)
797 {
798 struct thread *thread;
799 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
800 tool);
801
802 if (skip_sample(kvm, sample))
803 return 0;
804
805 thread = machine__findnew_thread(machine, sample->tid);
806 if (thread == NULL) {
807 pr_debug("problem processing %d event, skipping it.\n",
808 event->header.type);
809 return -1;
810 }
811
812 if (!handle_kvm_event(kvm, thread, evsel, sample))
813 return -1;
814
815 return 0;
816 }
817
818 static int cpu_isa_config(struct perf_kvm_stat *kvm)
819 {
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;
832
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);
839 return -ENOTSUP;
840 }
841
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;
1194 }
1195
1196 static int read_events(struct perf_kvm_stat *kvm)
1197 {
1198 int ret;
1199
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) {
1210 pr_err("Initializing perf session failed\n");
1211 return -EINVAL;
1212 }
1213
1214 if (!perf_session__has_traces(kvm->session, "kvm record"))
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 */
1221 ret = cpu_isa_config(kvm);
1222 if (ret < 0)
1223 return ret;
1224
1225 return perf_session__process_events(kvm->session, &kvm->tool);
1226 }
1227
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
1241 static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
1242 {
1243 int ret = -EINVAL;
1244 int vcpu = kvm->trace_vcpu;
1245
1246 if (parse_target_str(kvm) != 0)
1247 goto exit;
1248
1249 if (!verify_vcpu(vcpu))
1250 goto exit;
1251
1252 if (!select_key(kvm))
1253 goto exit;
1254
1255 if (!register_kvm_events_ops(kvm))
1256 goto exit;
1257
1258 init_kvm_event_record(kvm);
1259 setup_pager();
1260
1261 ret = read_events(kvm);
1262 if (ret)
1263 goto exit;
1264
1265 sort_result(kvm);
1266 print_result(kvm);
1267
1268 exit:
1269 return ret;
1270 }
1271
1272 static const char * const kvm_events_tp[] = {
1273 "kvm:kvm_entry",
1274 "kvm:kvm_exit",
1275 "kvm:kvm_mmio",
1276 "kvm:kvm_pio",
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
1287 static int
1288 kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
1289 {
1290 unsigned int rec_argc, i, j;
1291 const char **rec_argv;
1292 const char * const record_args[] = {
1293 "record",
1294 "-R",
1295 "-f",
1296 "-m", "1024",
1297 "-c", "1",
1298 };
1299
1300 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
1301 2 * ARRAY_SIZE(kvm_events_tp);
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
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
1315 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
1316 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
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
1324 static int
1325 kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
1326 {
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)"),
1335 OPT_STRING('p', "pid", &kvm->pid_str, "pid",
1336 "analyze events only for given process id(s)"),
1337 OPT_END()
1338 };
1339
1340 const char * const kvm_events_report_usage[] = {
1341 "perf kvm stat report [<options>]",
1342 NULL
1343 };
1344
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
1356 return kvm_events_report_vcpu(kvm);
1357 }
1358
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
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");
1546 printf("\tlive: live reporting of statistical data of kvm events\n");
1547
1548 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1549 }
1550
1551 static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
1552 {
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
1565 if (argc == 1) {
1566 print_kvm_stat_usage();
1567 goto perf_stat;
1568 }
1569
1570 if (!strncmp(argv[1], "rec", 3))
1571 return kvm_events_record(&kvm, argc - 1, argv + 1);
1572
1573 if (!strncmp(argv[1], "rep", 3))
1574 return kvm_events_report(&kvm, argc - 1 , argv + 1);
1575
1576 if (!strncmp(argv[1], "live", 4))
1577 return kvm_events_live(&kvm, argc - 1 , argv + 1);
1578
1579 perf_stat:
1580 return cmd_stat(argc, argv, NULL);
1581 }
1582 #endif
1583
1584 static int __cmd_record(const char *file_name, int argc, const char **argv)
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");
1593 rec_argv[i++] = strdup(file_name);
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
1602 static int __cmd_report(const char *file_name, int argc, const char **argv)
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");
1611 rec_argv[i++] = strdup(file_name);
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
1620 static int
1621 __cmd_buildid_list(const char *file_name, int argc, const char **argv)
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");
1630 rec_argv[i++] = strdup(file_name);
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
1639 int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
1640 {
1641 const char *file_name = NULL;
1642 const struct option kvm_options[] = {
1643 OPT_STRING('i', "input", &file_name, "file",
1644 "Input file name"),
1645 OPT_STRING('o', "output", &file_name, "file",
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
1669 perf_host = 0;
1670 perf_guest = 1;
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
1680 if (!file_name) {
1681 if (perf_host && !perf_guest)
1682 file_name = strdup("perf.data.host");
1683 else if (!perf_host && perf_guest)
1684 file_name = strdup("perf.data.guest");
1685 else
1686 file_name = strdup("perf.data.kvm");
1687
1688 if (!file_name) {
1689 pr_err("Failed to allocate memory for filename\n");
1690 return -ENOMEM;
1691 }
1692 }
1693
1694 if (!strncmp(argv[0], "rec", 3))
1695 return __cmd_record(file_name, argc, argv);
1696 else if (!strncmp(argv[0], "rep", 3))
1697 return __cmd_report(file_name, argc, argv);
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))
1703 return __cmd_buildid_list(file_name, argc, argv);
1704 #if defined(__i386__) || defined(__x86_64__)
1705 else if (!strncmp(argv[0], "stat", 4))
1706 return kvm_cmd_stat(file_name, argc, argv);
1707 #endif
1708 else
1709 usage_with_options(kvm_usage, kvm_options);
1710
1711 return 0;
1712 }
This page took 0.065919 seconds and 6 git commands to generate.