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