blob: 188bb29b373f9ee7280f25f28c127a00e83d195a [file] [log] [blame]
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001#include "builtin.h"
2#include "perf.h"
3
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08004#include "util/evsel.h"
David Ahern1afe1d12013-08-05 21:41:34 -04005#include "util/evlist.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08006#include "util/util.h"
7#include "util/cache.h"
8#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
11#include "util/session.h"
David Ahern2e73f002013-08-05 21:41:37 -040012#include "util/intlist.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080013#include "util/parse-options.h"
14#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015#include "util/debug.h"
Borislav Petkov85c66be2013-02-20 16:32:30 +010016#include <lk/debugfs.h>
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080017#include "util/tool.h"
18#include "util/stat.h"
David Ahern1afe1d12013-08-05 21:41:34 -040019#include "util/top.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020020#include "util/data.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080021
22#include <sys/prctl.h>
David Ahern1afe1d12013-08-05 21:41:34 -040023#include <sys/timerfd.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080024
David Ahern1afe1d12013-08-05 21:41:34 -040025#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080026#include <semaphore.h>
27#include <pthread.h>
28#include <math.h>
29
Xiao Guangrong73210902012-11-19 16:19:21 +080030#if defined(__i386__) || defined(__x86_64__)
David Howellsd2709c72012-11-19 22:21:03 +000031#include <asm/svm.h>
32#include <asm/vmx.h>
33#include <asm/kvm.h>
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080034
35struct event_key {
36 #define INVALID_KEY (~0ULL)
37 u64 key;
38 int info;
39};
40
David Ahernde332ac2012-10-02 22:09:53 -060041struct kvm_event_stats {
42 u64 time;
43 struct stats stats;
44};
45
46struct 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
59typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
60
61struct kvm_event_key {
62 const char *name;
63 key_cmp_fun key;
64};
65
66
Xiao Guangrong37860632012-11-15 14:17:01 +080067struct perf_kvm_stat;
David Ahernde332ac2012-10-02 22:09:53 -060068
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080069struct kvm_events_ops {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030070 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);
Xiao Guangrong37860632012-11-15 14:17:01 +080075 void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
David Ahernde332ac2012-10-02 22:09:53 -060076 char decode[20]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080077 const char *name;
78};
79
David Ahernde332ac2012-10-02 22:09:53 -060080struct 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
Xiao Guangrong37860632012-11-15 14:17:01 +080088struct perf_kvm_stat {
David Ahernde332ac2012-10-02 22:09:53 -060089 struct perf_tool tool;
David Ahern1afe1d12013-08-05 21:41:34 -040090 struct perf_record_opts opts;
91 struct perf_evlist *evlist;
David Ahernde332ac2012-10-02 22:09:53 -060092 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];
David Ahern1afe1d12013-08-05 21:41:34 -0400106
David Ahernde332ac2012-10-02 22:09:53 -0600107 u64 total_time;
108 u64 total_count;
David Ahern1afe1d12013-08-05 21:41:34 -0400109 u64 lost_events;
David Ahern70f7b4a2013-08-07 21:56:38 -0400110 u64 duration;
David Ahernde332ac2012-10-02 22:09:53 -0600111
David Ahern2e73f002013-08-05 21:41:37 -0400112 const char *pid_str;
113 struct intlist *pid_list;
114
David Ahernde332ac2012-10-02 22:09:53 -0600115 struct rb_root result;
David Ahern1afe1d12013-08-05 21:41:34 -0400116
117 int timerfd;
118 unsigned int display_time;
119 bool live;
David Ahernde332ac2012-10-02 22:09:53 -0600120};
121
122
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300123static void exit_event_get_key(struct perf_evsel *evsel,
124 struct perf_sample *sample,
125 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800126{
127 key->info = 0;
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300128 key->key = perf_evsel__intval(evsel, sample, "exit_reason");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800129}
130
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300131static bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800132{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300133 return !strcmp(evsel->name, "kvm:kvm_exit");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800134}
135
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300136static bool exit_event_begin(struct perf_evsel *evsel,
137 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800138{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300139 if (kvm_exit_event(evsel)) {
140 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800141 return true;
142 }
143
144 return false;
145}
146
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300147static bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800148{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300149 return !strcmp(evsel->name, "kvm:kvm_entry");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800150}
151
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300152static bool exit_event_end(struct perf_evsel *evsel,
153 struct perf_sample *sample __maybe_unused,
154 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800155{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300156 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800157}
158
David Ahernde332ac2012-10-02 22:09:53 -0600159static struct exit_reasons_table vmx_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800160 VMX_EXIT_REASONS
161};
162
David Ahernde332ac2012-10-02 22:09:53 -0600163static struct exit_reasons_table svm_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800164 SVM_EXIT_REASONS
165};
166
Xiao Guangrong37860632012-11-15 14:17:01 +0800167static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800168{
David Ahernde332ac2012-10-02 22:09:53 -0600169 int i = kvm->exit_reasons_size;
170 struct exit_reasons_table *tbl = kvm->exit_reasons;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800171
David Ahernde332ac2012-10-02 22:09:53 -0600172 while (i--) {
173 if (tbl->exit_code == exit_code)
174 return tbl->reason;
175 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800176 }
177
178 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600179 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800180 return "UNKNOWN";
181}
182
Xiao Guangrong37860632012-11-15 14:17:01 +0800183static void exit_event_decode_key(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600184 struct event_key *key,
185 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800186{
David Ahernde332ac2012-10-02 22:09:53 -0600187 const char *exit_reason = get_exit_reason(kvm, key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800188
189 scnprintf(decode, 20, "%s", exit_reason);
190}
191
192static 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
David Ahernde332ac2012-10-02 22:09:53 -0600199/*
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 */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300204static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
205 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800206{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300207 key->key = perf_evsel__intval(evsel, sample, "gpa");
208 key->info = perf_evsel__intval(evsel, sample, "type");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800209}
210
211#define KVM_TRACE_MMIO_READ_UNSATISFIED 0
212#define KVM_TRACE_MMIO_READ 1
213#define KVM_TRACE_MMIO_WRITE 2
214
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300215static bool mmio_event_begin(struct perf_evsel *evsel,
216 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800217{
218 /* MMIO read begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300219 if (kvm_exit_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800220 return true;
221
222 /* MMIO write begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300223 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);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800226 return true;
227 }
228
229 return false;
230}
231
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300232static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
233 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800234{
235 /* MMIO write end event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300236 if (kvm_entry_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800237 return true;
238
239 /* MMIO read end event in kernel.*/
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300240 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);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800243 return true;
244 }
245
246 return false;
247}
248
Xiao Guangrong37860632012-11-15 14:17:01 +0800249static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600250 struct event_key *key,
251 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800252{
253 scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
254 key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
255}
256
257static 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. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300265static void ioport_event_get_key(struct perf_evsel *evsel,
266 struct perf_sample *sample,
267 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800268{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300269 key->key = perf_evsel__intval(evsel, sample, "port");
270 key->info = perf_evsel__intval(evsel, sample, "rw");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800271}
272
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300273static bool ioport_event_begin(struct perf_evsel *evsel,
274 struct perf_sample *sample,
275 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800276{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300277 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
278 ioport_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800279 return true;
280 }
281
282 return false;
283}
284
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300285static bool ioport_event_end(struct perf_evsel *evsel,
286 struct perf_sample *sample __maybe_unused,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800287 struct event_key *key __maybe_unused)
288{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300289 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800290}
291
Xiao Guangrong37860632012-11-15 14:17:01 +0800292static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600293 struct event_key *key,
294 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800295{
296 scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
297 key->info ? "POUT" : "PIN");
298}
299
300static 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
Xiao Guangrong37860632012-11-15 14:17:01 +0800307static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800308{
309 bool ret = true;
310
David Ahernde332ac2012-10-02 22:09:53 -0600311 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;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800317 else {
David Ahernde332ac2012-10-02 22:09:53 -0600318 pr_err("Unknown report event:%s\n", kvm->report_event);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800319 ret = false;
320 }
321
322 return ret;
323}
324
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800325struct vcpu_event_record {
326 int vcpu_id;
327 u64 start_time;
328 struct kvm_event *last_event;
329};
330
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800331
Xiao Guangrong37860632012-11-15 14:17:01 +0800332static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800333{
David Ahernb880dee2012-10-08 11:17:30 -0600334 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800335
David Ahernb880dee2012-10-08 11:17:30 -0600336 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600337 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800338}
339
David Ahern1afe1d12013-08-05 21:41:34 -0400340static 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;
David Ahern62d04db2013-08-05 21:41:35 -0400345 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400346
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 */
David Ahern62d04db2013-08-05 21:41:35 -0400351 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 }
David Ahern1afe1d12013-08-05 21:41:34 -0400358 }
359 }
360}
361
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800362static int kvm_events_hash_fn(u64 key)
363{
364 return key & (EVENTS_CACHE_SIZE - 1);
365}
366
367static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
368{
369 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600370 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800371
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
David Ahern6ca5f302013-05-25 18:24:46 -0600378 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800379 event->vcpu = realloc(event->vcpu,
380 event->max_vcpu * sizeof(*event->vcpu));
381 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600382 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800383 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
392static 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
Xiao Guangrong37860632012-11-15 14:17:01 +0800406static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600407 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800408{
409 struct kvm_event *event;
410 struct list_head *head;
411
412 BUG_ON(key->key == INVALID_KEY);
413
David Ahernde332ac2012-10-02 22:09:53 -0600414 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600415 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800416 if (event->key.key == key->key && event->key.info == key->info)
417 return event;
David Ahern355afe82012-10-08 11:17:32 -0600418 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800419
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
Xiao Guangrong37860632012-11-15 14:17:01 +0800428static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600429 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800430 struct event_key *key, u64 timestamp)
431{
432 struct kvm_event *event = NULL;
433
434 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600435 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800436
437 vcpu_record->last_event = event;
438 vcpu_record->start_time = timestamp;
439 return true;
440}
441
442static void
443kvm_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
449static 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
460static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
461 u64 time_diff)
462{
David Ahern2aa8eab2012-10-08 11:17:35 -0600463 if (vcpu_id == -1) {
464 kvm_update_event_stats(&event->total, time_diff);
465 return true;
466 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800467
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
Xiao Guangrong37860632012-11-15 14:17:01 +0800475static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600476 struct vcpu_event_record *vcpu_record,
477 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400478 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800479{
480 struct kvm_event *event;
481 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600482 int vcpu;
483
484 if (kvm->trace_vcpu == -1)
485 vcpu = -1;
486 else
487 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800488
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)
David Ahernde332ac2012-10-02 22:09:53 -0600506 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800507
508 if (!event)
509 return false;
510
511 vcpu_record->last_event = NULL;
512 vcpu_record->start_time = 0;
513
David Ahern1afe1d12013-08-05 21:41:34 -0400514 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400515 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400516 pr_debug("End time before begin time; skipping event.\n");
517 return true;
518 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800519
David Ahern70f7b4a2013-08-07 21:56:38 -0400520 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
David Ahern2aa8eab2012-10-08 11:17:35 -0600533 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800534}
535
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300536static
537struct vcpu_event_record *per_vcpu_record(struct thread *thread,
538 struct perf_evsel *evsel,
539 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800540{
541 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300542 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800543 struct vcpu_event_record *vcpu_record;
544
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300545 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800546 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300547 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800548 return NULL;
549 }
550
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300551 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800552 thread->priv = vcpu_record;
553 }
554
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300555 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800556}
557
Xiao Guangrong37860632012-11-15 14:17:01 +0800558static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600559 struct thread *thread,
560 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300561 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800562{
563 struct vcpu_event_record *vcpu_record;
564 struct event_key key = {.key = INVALID_KEY};
565
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300566 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800567 if (!vcpu_record)
568 return true;
569
David Ahern2aa8eab2012-10-08 11:17:35 -0600570 /* 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
David Ahernde332ac2012-10-02 22:09:53 -0600575 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
576 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800577
David Ahernde332ac2012-10-02 22:09:53 -0600578 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400579 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800580
581 return true;
582}
583
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800584#define GET_EVENT_KEY(func, field) \
585static 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) \
597GET_EVENT_KEY(func, field) \
598static 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
605GET_EVENT_KEY(time, time);
606COMPARE_EVENT_KEY(count, stats.n);
607COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400608GET_EVENT_KEY(max, stats.max);
609GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800610
611#define DEF_SORT_NAME_KEY(name, compare_key) \
612 { #name, compare_kvm_event_ ## compare_key }
613
614static struct kvm_event_key keys[] = {
615 DEF_SORT_NAME_KEY(sample, count),
616 DEF_SORT_NAME_KEY(time, mean),
617 { NULL, NULL }
618};
619
Xiao Guangrong37860632012-11-15 14:17:01 +0800620static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800621{
622 int i;
623
624 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600625 if (!strcmp(keys[i].name, kvm->sort_key)) {
626 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800627 return true;
628 }
629 }
630
David Ahernde332ac2012-10-02 22:09:53 -0600631 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800632 return false;
633}
634
David Ahernde332ac2012-10-02 22:09:53 -0600635static void insert_to_result(struct rb_root *result, struct kvm_event *event,
636 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800637{
David Ahernde332ac2012-10-02 22:09:53 -0600638 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800639 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);
David Ahernde332ac2012-10-02 22:09:53 -0600653 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800654}
655
Xiao Guangrong37860632012-11-15 14:17:01 +0800656static void
657update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800658{
David Ahernde332ac2012-10-02 22:09:53 -0600659 int vcpu = kvm->trace_vcpu;
660
661 kvm->total_count += get_event_count(event, vcpu);
662 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800663}
664
665static bool event_is_valid(struct kvm_event *event, int vcpu)
666{
667 return !!get_event_count(event, vcpu);
668}
669
Xiao Guangrong37860632012-11-15 14:17:01 +0800670static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800671{
672 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600673 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800674 struct kvm_event *event;
675
David Ahern355afe82012-10-08 11:17:32 -0600676 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
677 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800678 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600679 update_total_count(kvm, event);
680 insert_to_result(&kvm->result, event,
681 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800682 }
David Ahern355afe82012-10-08 11:17:32 -0600683 }
684 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800685}
686
687/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600688static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800689{
David Ahernde332ac2012-10-02 22:09:53 -0600690 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800691
692 if (!node)
693 return NULL;
694
David Ahernde332ac2012-10-02 22:09:53 -0600695 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800696 return container_of(node, struct kvm_event, rb);
697}
698
David Ahern1afe1d12013-08-05 21:41:34 -0400699static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800700{
David Ahern1afe1d12013-08-05 21:41:34 -0400701 int vcpu = kvm->trace_vcpu;
702
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800703 pr_info("Analyze events for ");
704
David Ahern1afe1d12013-08-05 21:41:34 -0400705 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
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800714 if (vcpu == -1)
715 pr_info("all VCPUs:\n\n");
716 else
717 pr_info("VCPU %d:\n\n", vcpu);
718}
719
David Ahern1afe1d12013-08-05 21:41:34 -0400720static 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
Xiao Guangrong37860632012-11-15 14:17:01 +0800736static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800737{
738 char decode[20];
739 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600740 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800741
David Ahern1afe1d12013-08-05 21:41:34 -0400742 if (kvm->live) {
743 puts(CONSOLE_CLEAR);
744 show_timeofday();
745 }
746
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800747 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400748 print_vcpu_info(kvm);
David Ahernde332ac2012-10-02 22:09:53 -0600749 pr_info("%20s ", kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800750 pr_info("%10s ", "Samples");
751 pr_info("%9s ", "Samples%");
752
753 pr_info("%9s ", "Time%");
David Ahern62d04db2013-08-05 21:41:35 -0400754 pr_info("%10s ", "Min Time");
755 pr_info("%10s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800756 pr_info("%16s ", "Avg time");
757 pr_info("\n\n");
758
David Ahernde332ac2012-10-02 22:09:53 -0600759 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400760 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800761
762 ecount = get_event_count(event, vcpu);
763 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400764 max = get_event_max(event, vcpu);
765 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800766
David Ahernde332ac2012-10-02 22:09:53 -0600767 kvm->events_ops->decode_key(kvm, &event->key, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800768 pr_info("%20s ", decode);
769 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600770 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
771 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
David Ahern62d04db2013-08-05 21:41:35 -0400772 pr_info("%8" PRIu64 "us ", min / 1000);
773 pr_info("%8" PRIu64 "us ", max / 1000);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800774 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
775 kvm_event_rel_stddev(vcpu, event));
776 pr_info("\n");
777 }
778
David Aherne4f76372012-10-08 11:17:34 -0600779 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
780 kvm->total_count, kvm->total_time / 1e3);
David Ahern1afe1d12013-08-05 21:41:34 -0400781
782 if (kvm->lost_events)
783 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
784}
785
786static 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;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800795}
796
David Ahern2e73f002013-08-05 21:41:37 -0400797static 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
David Ahernde332ac2012-10-02 22:09:53 -0600806static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800807 union perf_event *event,
808 struct perf_sample *sample,
809 struct perf_evsel *evsel,
810 struct machine *machine)
811{
David Ahern2e73f002013-08-05 21:41:37 -0400812 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800813 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
814 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800815
David Ahern2e73f002013-08-05 21:41:37 -0400816 if (skip_sample(kvm, sample))
817 return 0;
818
Adrian Hunter314add62013-08-27 11:23:03 +0300819 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800820 if (thread == NULL) {
821 pr_debug("problem processing %d event, skipping it.\n",
822 event->header.type);
823 return -1;
824 }
825
David Ahernde332ac2012-10-02 22:09:53 -0600826 if (!handle_kvm_event(kvm, thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800827 return -1;
828
829 return 0;
830}
831
David Ahern1afe1d12013-08-05 21:41:34 -0400832static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800833{
David Ahern1afe1d12013-08-05 21:41:34 -0400834 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;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800846
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800847 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);
David Ahern1afe1d12013-08-05 21:41:34 -0400853 return -ENOTSUP;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800854 }
855
David Ahern1afe1d12013-08-05 21:41:34 -0400856 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
865static 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
880static 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
915static 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
956static volatile int done;
957
958static void sig_handler(int sig __maybe_unused)
959{
960 done = 1;
961}
962
963static 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;
986out:
987 return rc;
988}
989
990static 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
1025static 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
1043static
1044int 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
1058static 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
1143out:
1144 if (kvm->timerfd >= 0)
1145 close(kvm->timerfd);
1146
1147 if (pollfds)
1148 free(pollfds);
1149
1150 return err;
1151}
1152
1153static 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 */
Adrian Huntere71aa282013-09-06 22:40:12 +03001169 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);
David Ahern1afe1d12013-08-05 21:41:34 -04001173 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001174 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);
David Ahern1afe1d12013-08-05 21:41:34 -04001179 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
1206out:
1207 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001208}
1209
Xiao Guangrong37860632012-11-15 14:17:01 +08001210static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001211{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001212 int ret;
1213
David Ahernde332ac2012-10-02 22:09:53 -06001214 struct perf_tool eops = {
1215 .sample = process_sample_event,
1216 .comm = perf_event__process_comm,
1217 .ordered_samples = true,
1218 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001219 struct perf_data_file file = {
1220 .path = input_name,
1221 .mode = PERF_DATA_MODE_READ,
1222 };
David Ahernde332ac2012-10-02 22:09:53 -06001223
1224 kvm->tool = eops;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001225 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001226 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001227 pr_err("Initializing perf session failed\n");
1228 return -EINVAL;
1229 }
1230
David Ahernde332ac2012-10-02 22:09:53 -06001231 if (!perf_session__has_traces(kvm->session, "kvm record"))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001232 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 */
David Ahern1afe1d12013-08-05 21:41:34 -04001238 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001239 if (ret < 0)
1240 return ret;
1241
David Ahernde332ac2012-10-02 22:09:53 -06001242 return perf_session__process_events(kvm->session, &kvm->tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001243}
1244
David Ahern2e73f002013-08-05 21:41:37 -04001245static 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
Xiao Guangrong37860632012-11-15 14:17:01 +08001258static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001259{
1260 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001261 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001262
David Ahern2e73f002013-08-05 21:41:37 -04001263 if (parse_target_str(kvm) != 0)
1264 goto exit;
1265
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001266 if (!verify_vcpu(vcpu))
1267 goto exit;
1268
David Ahernde332ac2012-10-02 22:09:53 -06001269 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001270 goto exit;
1271
David Ahernde332ac2012-10-02 22:09:53 -06001272 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001273 goto exit;
1274
David Ahernde332ac2012-10-02 22:09:53 -06001275 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001276 setup_pager();
1277
David Ahernde332ac2012-10-02 22:09:53 -06001278 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001279 if (ret)
1280 goto exit;
1281
David Ahernde332ac2012-10-02 22:09:53 -06001282 sort_result(kvm);
1283 print_result(kvm);
1284
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001285exit:
1286 return ret;
1287}
1288
David Ahern8fdd84c2013-08-02 14:05:42 -06001289static const char * const kvm_events_tp[] = {
1290 "kvm:kvm_entry",
1291 "kvm:kvm_exit",
1292 "kvm:kvm_mmio",
1293 "kvm:kvm_pio",
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001294};
1295
1296#define STRDUP_FAIL_EXIT(s) \
1297 ({ char *_p; \
1298 _p = strdup(s); \
1299 if (!_p) \
1300 return -ENOMEM; \
1301 _p; \
1302 })
1303
Xiao Guangrong37860632012-11-15 14:17:01 +08001304static int
1305kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001306{
1307 unsigned int rec_argc, i, j;
1308 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001309 const char * const record_args[] = {
1310 "record",
1311 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001312 "-m", "1024",
1313 "-c", "1",
1314 };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001315
David Ahern8fdd84c2013-08-02 14:05:42 -06001316 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
1317 2 * ARRAY_SIZE(kvm_events_tp);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001318 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
David Ahern8fdd84c2013-08-02 14:05:42 -06001326 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
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001331 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001332 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001333
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
Xiao Guangrong37860632012-11-15 14:17:01 +08001340static int
1341kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001342{
David Ahernde332ac2012-10-02 22:09:53 -06001343 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)"),
David Ahern2e73f002013-08-05 21:41:37 -04001351 OPT_STRING('p', "pid", &kvm->pid_str, "pid",
1352 "analyze events only for given process id(s)"),
David Ahernde332ac2012-10-02 22:09:53 -06001353 OPT_END()
1354 };
1355
1356 const char * const kvm_events_report_usage[] = {
1357 "perf kvm stat report [<options>]",
1358 NULL
1359 };
1360
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001361 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
David Ahernde332ac2012-10-02 22:09:53 -06001372 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001373}
1374
David Ahern1afe1d12013-08-05 21:41:34 -04001375static 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
1415out:
1416 if (err) {
1417 perf_evlist__delete(evlist);
1418 evlist = NULL;
1419 }
1420
1421 return evlist;
1422}
1423
1424static 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"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001433 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1434 "number of mmap data pages",
1435 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001436 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)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001449 OPT_U64(0, "duration", &kvm->duration,
1450 "show events other than HALT that take longer than duration usecs"),
David Ahern1afe1d12013-08-05 21:41:34 -04001451 OPT_END()
1452 };
1453 const char * const live_usage[] = {
1454 "perf kvm stat live [<options>]",
1455 NULL
1456 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001457 struct perf_data_file file = {
1458 .mode = PERF_DATA_MODE_WRITE,
1459 };
David Ahern1afe1d12013-08-05 21:41:34 -04001460
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
David Ahern70f7b4a2013-08-07 21:56:38 -04001492 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1493
David Ahern1afe1d12013-08-05 21:41:34 -04001494 /*
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 */
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001524 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001525 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
1549out:
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
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001563static 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");
David Ahern1afe1d12013-08-05 21:41:34 -04001570 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001571
1572 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1573}
1574
Xiao Guangrong37860632012-11-15 14:17:01 +08001575static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001576{
Xiao Guangrong37860632012-11-15 14:17:01 +08001577 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
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001589 if (argc == 1) {
1590 print_kvm_stat_usage();
1591 goto perf_stat;
1592 }
1593
1594 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001595 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001596
1597 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001598 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001599
David Ahern1afe1d12013-08-05 21:41:34 -04001600 if (!strncmp(argv[1], "live", 4))
1601 return kvm_events_live(&kvm, argc - 1 , argv + 1);
1602
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001603perf_stat:
1604 return cmd_stat(argc, argv, NULL);
1605}
Xiao Guangrong73210902012-11-19 16:19:21 +08001606#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001607
Xiao Guangrong37860632012-11-15 14:17:01 +08001608static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001609{
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");
Xiao Guangrong37860632012-11-15 14:17:01 +08001617 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001618 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
Xiao Guangrong37860632012-11-15 14:17:01 +08001626static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001627{
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");
Xiao Guangrong37860632012-11-15 14:17:01 +08001635 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001636 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
Xiao Guangrong37860632012-11-15 14:17:01 +08001644static int
1645__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001646{
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");
Xiao Guangrong37860632012-11-15 14:17:01 +08001654 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001655 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
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001663int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001664{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001665 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001666 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001667 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001668 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001669 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001670 "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
Joerg Roedel1aed2672012-01-04 17:54:20 +01001693 perf_host = 0;
1694 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001695
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
Xiao Guangrong37860632012-11-15 14:17:01 +08001704 if (!file_name) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001705 if (perf_host && !perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001706 file_name = strdup("perf.data.host");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001707 else if (!perf_host && perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001708 file_name = strdup("perf.data.guest");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001709 else
Xiao Guangrong37860632012-11-15 14:17:01 +08001710 file_name = strdup("perf.data.kvm");
David Ahernde332ac2012-10-02 22:09:53 -06001711
Xiao Guangrong37860632012-11-15 14:17:01 +08001712 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001713 pr_err("Failed to allocate memory for filename\n");
1714 return -ENOMEM;
1715 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001716 }
1717
1718 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001719 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001720 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001721 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001722 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))
Xiao Guangrong37860632012-11-15 14:17:01 +08001727 return __cmd_buildid_list(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001728#if defined(__i386__) || defined(__x86_64__)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001729 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001730 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001731#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001732 else
1733 usage_with_options(kvm_usage, kvm_options);
1734
1735 return 0;
1736}