blob: 9fa45fa13bd6f8d774473b791aaf8873d043969d [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"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08005#include "util/util.h"
6#include "util/cache.h"
7#include "util/symbol.h"
8#include "util/thread.h"
9#include "util/header.h"
10#include "util/session.h"
11
12#include "util/parse-options.h"
13#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080014#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080015#include "util/debugfs.h"
16#include "util/tool.h"
17#include "util/stat.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080018
19#include <sys/prctl.h>
20
21#include <semaphore.h>
22#include <pthread.h>
23#include <math.h>
24
David Howellsd2709c72012-11-19 22:21:03 +000025#include <asm/svm.h>
26#include <asm/vmx.h>
27#include <asm/kvm.h>
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080028
29struct event_key {
30 #define INVALID_KEY (~0ULL)
31 u64 key;
32 int info;
33};
34
David Ahernde332ac2012-10-02 22:09:53 -060035struct kvm_event_stats {
36 u64 time;
37 struct stats stats;
38};
39
40struct kvm_event {
41 struct list_head hash_entry;
42 struct rb_node rb;
43
44 struct event_key key;
45
46 struct kvm_event_stats total;
47
48 #define DEFAULT_VCPU_NUM 8
49 int max_vcpu;
50 struct kvm_event_stats *vcpu;
51};
52
53typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
54
55struct kvm_event_key {
56 const char *name;
57 key_cmp_fun key;
58};
59
60
Xiao Guangrong37860632012-11-15 14:17:01 +080061struct perf_kvm_stat;
David Ahernde332ac2012-10-02 22:09:53 -060062
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080063struct kvm_events_ops {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030064 bool (*is_begin_event)(struct perf_evsel *evsel,
65 struct perf_sample *sample,
66 struct event_key *key);
67 bool (*is_end_event)(struct perf_evsel *evsel,
68 struct perf_sample *sample, struct event_key *key);
Xiao Guangrong37860632012-11-15 14:17:01 +080069 void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
David Ahernde332ac2012-10-02 22:09:53 -060070 char decode[20]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080071 const char *name;
72};
73
David Ahernde332ac2012-10-02 22:09:53 -060074struct exit_reasons_table {
75 unsigned long exit_code;
76 const char *reason;
77};
78
79#define EVENTS_BITS 12
80#define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
81
Xiao Guangrong37860632012-11-15 14:17:01 +080082struct perf_kvm_stat {
David Ahernde332ac2012-10-02 22:09:53 -060083 struct perf_tool tool;
84 struct perf_session *session;
85
86 const char *file_name;
87 const char *report_event;
88 const char *sort_key;
89 int trace_vcpu;
90
91 struct exit_reasons_table *exit_reasons;
92 int exit_reasons_size;
93 const char *exit_reasons_isa;
94
95 struct kvm_events_ops *events_ops;
96 key_cmp_fun compare;
97 struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
98 u64 total_time;
99 u64 total_count;
100
101 struct rb_root result;
102};
103
104
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300105static void exit_event_get_key(struct perf_evsel *evsel,
106 struct perf_sample *sample,
107 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800108{
109 key->info = 0;
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300110 key->key = perf_evsel__intval(evsel, sample, "exit_reason");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800111}
112
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300113static bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800114{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300115 return !strcmp(evsel->name, "kvm:kvm_exit");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800116}
117
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300118static bool exit_event_begin(struct perf_evsel *evsel,
119 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800120{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300121 if (kvm_exit_event(evsel)) {
122 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800123 return true;
124 }
125
126 return false;
127}
128
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300129static bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800130{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300131 return !strcmp(evsel->name, "kvm:kvm_entry");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800132}
133
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300134static bool exit_event_end(struct perf_evsel *evsel,
135 struct perf_sample *sample __maybe_unused,
136 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800137{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300138 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800139}
140
David Ahernde332ac2012-10-02 22:09:53 -0600141static struct exit_reasons_table vmx_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800142 VMX_EXIT_REASONS
143};
144
David Ahernde332ac2012-10-02 22:09:53 -0600145static struct exit_reasons_table svm_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800146 SVM_EXIT_REASONS
147};
148
Xiao Guangrong37860632012-11-15 14:17:01 +0800149static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800150{
David Ahernde332ac2012-10-02 22:09:53 -0600151 int i = kvm->exit_reasons_size;
152 struct exit_reasons_table *tbl = kvm->exit_reasons;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800153
David Ahernde332ac2012-10-02 22:09:53 -0600154 while (i--) {
155 if (tbl->exit_code == exit_code)
156 return tbl->reason;
157 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800158 }
159
160 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600161 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800162 return "UNKNOWN";
163}
164
Xiao Guangrong37860632012-11-15 14:17:01 +0800165static void exit_event_decode_key(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600166 struct event_key *key,
167 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800168{
David Ahernde332ac2012-10-02 22:09:53 -0600169 const char *exit_reason = get_exit_reason(kvm, key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800170
171 scnprintf(decode, 20, "%s", exit_reason);
172}
173
174static struct kvm_events_ops exit_events = {
175 .is_begin_event = exit_event_begin,
176 .is_end_event = exit_event_end,
177 .decode_key = exit_event_decode_key,
178 .name = "VM-EXIT"
179};
180
David Ahernde332ac2012-10-02 22:09:53 -0600181/*
182 * For the mmio events, we treat:
183 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
184 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
185 */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300186static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
187 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800188{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300189 key->key = perf_evsel__intval(evsel, sample, "gpa");
190 key->info = perf_evsel__intval(evsel, sample, "type");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800191}
192
193#define KVM_TRACE_MMIO_READ_UNSATISFIED 0
194#define KVM_TRACE_MMIO_READ 1
195#define KVM_TRACE_MMIO_WRITE 2
196
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300197static bool mmio_event_begin(struct perf_evsel *evsel,
198 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800199{
200 /* MMIO read begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300201 if (kvm_exit_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800202 return true;
203
204 /* MMIO write begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300205 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
206 perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
207 mmio_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800208 return true;
209 }
210
211 return false;
212}
213
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300214static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
215 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800216{
217 /* MMIO write end event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300218 if (kvm_entry_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800219 return true;
220
221 /* MMIO read end event in kernel.*/
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300222 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
223 perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
224 mmio_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800225 return true;
226 }
227
228 return false;
229}
230
Xiao Guangrong37860632012-11-15 14:17:01 +0800231static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600232 struct event_key *key,
233 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800234{
235 scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
236 key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
237}
238
239static struct kvm_events_ops mmio_events = {
240 .is_begin_event = mmio_event_begin,
241 .is_end_event = mmio_event_end,
242 .decode_key = mmio_event_decode_key,
243 .name = "MMIO Access"
244};
245
246 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300247static void ioport_event_get_key(struct perf_evsel *evsel,
248 struct perf_sample *sample,
249 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800250{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300251 key->key = perf_evsel__intval(evsel, sample, "port");
252 key->info = perf_evsel__intval(evsel, sample, "rw");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800253}
254
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300255static bool ioport_event_begin(struct perf_evsel *evsel,
256 struct perf_sample *sample,
257 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800258{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300259 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
260 ioport_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800261 return true;
262 }
263
264 return false;
265}
266
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300267static bool ioport_event_end(struct perf_evsel *evsel,
268 struct perf_sample *sample __maybe_unused,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800269 struct event_key *key __maybe_unused)
270{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300271 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800272}
273
Xiao Guangrong37860632012-11-15 14:17:01 +0800274static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600275 struct event_key *key,
276 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800277{
278 scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
279 key->info ? "POUT" : "PIN");
280}
281
282static struct kvm_events_ops ioport_events = {
283 .is_begin_event = ioport_event_begin,
284 .is_end_event = ioport_event_end,
285 .decode_key = ioport_event_decode_key,
286 .name = "IO Port Access"
287};
288
Xiao Guangrong37860632012-11-15 14:17:01 +0800289static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800290{
291 bool ret = true;
292
David Ahernde332ac2012-10-02 22:09:53 -0600293 if (!strcmp(kvm->report_event, "vmexit"))
294 kvm->events_ops = &exit_events;
295 else if (!strcmp(kvm->report_event, "mmio"))
296 kvm->events_ops = &mmio_events;
297 else if (!strcmp(kvm->report_event, "ioport"))
298 kvm->events_ops = &ioport_events;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800299 else {
David Ahernde332ac2012-10-02 22:09:53 -0600300 pr_err("Unknown report event:%s\n", kvm->report_event);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800301 ret = false;
302 }
303
304 return ret;
305}
306
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800307struct vcpu_event_record {
308 int vcpu_id;
309 u64 start_time;
310 struct kvm_event *last_event;
311};
312
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800313
Xiao Guangrong37860632012-11-15 14:17:01 +0800314static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800315{
316 int i;
317
318 for (i = 0; i < (int)EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600319 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800320}
321
322static int kvm_events_hash_fn(u64 key)
323{
324 return key & (EVENTS_CACHE_SIZE - 1);
325}
326
327static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
328{
329 int old_max_vcpu = event->max_vcpu;
330
331 if (vcpu_id < event->max_vcpu)
332 return true;
333
334 while (event->max_vcpu <= vcpu_id)
335 event->max_vcpu += DEFAULT_VCPU_NUM;
336
337 event->vcpu = realloc(event->vcpu,
338 event->max_vcpu * sizeof(*event->vcpu));
339 if (!event->vcpu) {
340 pr_err("Not enough memory\n");
341 return false;
342 }
343
344 memset(event->vcpu + old_max_vcpu, 0,
345 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
346 return true;
347}
348
349static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
350{
351 struct kvm_event *event;
352
353 event = zalloc(sizeof(*event));
354 if (!event) {
355 pr_err("Not enough memory\n");
356 return NULL;
357 }
358
359 event->key = *key;
360 return event;
361}
362
Xiao Guangrong37860632012-11-15 14:17:01 +0800363static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600364 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800365{
366 struct kvm_event *event;
367 struct list_head *head;
368
369 BUG_ON(key->key == INVALID_KEY);
370
David Ahernde332ac2012-10-02 22:09:53 -0600371 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800372 list_for_each_entry(event, head, hash_entry)
373 if (event->key.key == key->key && event->key.info == key->info)
374 return event;
375
376 event = kvm_alloc_init_event(key);
377 if (!event)
378 return NULL;
379
380 list_add(&event->hash_entry, head);
381 return event;
382}
383
Xiao Guangrong37860632012-11-15 14:17:01 +0800384static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600385 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800386 struct event_key *key, u64 timestamp)
387{
388 struct kvm_event *event = NULL;
389
390 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600391 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800392
393 vcpu_record->last_event = event;
394 vcpu_record->start_time = timestamp;
395 return true;
396}
397
398static void
399kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
400{
401 kvm_stats->time += time_diff;
402 update_stats(&kvm_stats->stats, time_diff);
403}
404
405static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
406{
407 struct kvm_event_stats *kvm_stats = &event->total;
408
409 if (vcpu_id != -1)
410 kvm_stats = &event->vcpu[vcpu_id];
411
412 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
413 avg_stats(&kvm_stats->stats));
414}
415
416static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
417 u64 time_diff)
418{
419 kvm_update_event_stats(&event->total, time_diff);
420
421 if (!kvm_event_expand(event, vcpu_id))
422 return false;
423
424 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
425 return true;
426}
427
Xiao Guangrong37860632012-11-15 14:17:01 +0800428static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600429 struct vcpu_event_record *vcpu_record,
430 struct event_key *key,
431 u64 timestamp)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800432{
433 struct kvm_event *event;
434 u64 time_begin, time_diff;
435
436 event = vcpu_record->last_event;
437 time_begin = vcpu_record->start_time;
438
439 /* The begin event is not caught. */
440 if (!time_begin)
441 return true;
442
443 /*
444 * In some case, the 'begin event' only records the start timestamp,
445 * the actual event is recognized in the 'end event' (e.g. mmio-event).
446 */
447
448 /* Both begin and end events did not get the key. */
449 if (!event && key->key == INVALID_KEY)
450 return true;
451
452 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600453 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800454
455 if (!event)
456 return false;
457
458 vcpu_record->last_event = NULL;
459 vcpu_record->start_time = 0;
460
461 BUG_ON(timestamp < time_begin);
462
463 time_diff = timestamp - time_begin;
464 return update_kvm_event(event, vcpu_record->vcpu_id, time_diff);
465}
466
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300467static
468struct vcpu_event_record *per_vcpu_record(struct thread *thread,
469 struct perf_evsel *evsel,
470 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800471{
472 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300473 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800474 struct vcpu_event_record *vcpu_record;
475
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300476 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800477 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300478 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800479 return NULL;
480 }
481
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300482 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800483 thread->priv = vcpu_record;
484 }
485
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300486 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800487}
488
Xiao Guangrong37860632012-11-15 14:17:01 +0800489static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600490 struct thread *thread,
491 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300492 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800493{
494 struct vcpu_event_record *vcpu_record;
495 struct event_key key = {.key = INVALID_KEY};
496
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300497 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800498 if (!vcpu_record)
499 return true;
500
David Ahernde332ac2012-10-02 22:09:53 -0600501 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
502 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800503
David Ahernde332ac2012-10-02 22:09:53 -0600504 if (kvm->events_ops->is_end_event(evsel, sample, &key))
505 return handle_end_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800506
507 return true;
508}
509
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800510#define GET_EVENT_KEY(func, field) \
511static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
512{ \
513 if (vcpu == -1) \
514 return event->total.field; \
515 \
516 if (vcpu >= event->max_vcpu) \
517 return 0; \
518 \
519 return event->vcpu[vcpu].field; \
520}
521
522#define COMPARE_EVENT_KEY(func, field) \
523GET_EVENT_KEY(func, field) \
524static int compare_kvm_event_ ## func(struct kvm_event *one, \
525 struct kvm_event *two, int vcpu)\
526{ \
527 return get_event_ ##func(one, vcpu) > \
528 get_event_ ##func(two, vcpu); \
529}
530
531GET_EVENT_KEY(time, time);
532COMPARE_EVENT_KEY(count, stats.n);
533COMPARE_EVENT_KEY(mean, stats.mean);
534
535#define DEF_SORT_NAME_KEY(name, compare_key) \
536 { #name, compare_kvm_event_ ## compare_key }
537
538static struct kvm_event_key keys[] = {
539 DEF_SORT_NAME_KEY(sample, count),
540 DEF_SORT_NAME_KEY(time, mean),
541 { NULL, NULL }
542};
543
Xiao Guangrong37860632012-11-15 14:17:01 +0800544static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800545{
546 int i;
547
548 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600549 if (!strcmp(keys[i].name, kvm->sort_key)) {
550 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800551 return true;
552 }
553 }
554
David Ahernde332ac2012-10-02 22:09:53 -0600555 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800556 return false;
557}
558
David Ahernde332ac2012-10-02 22:09:53 -0600559static void insert_to_result(struct rb_root *result, struct kvm_event *event,
560 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800561{
David Ahernde332ac2012-10-02 22:09:53 -0600562 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800563 struct rb_node *parent = NULL;
564 struct kvm_event *p;
565
566 while (*rb) {
567 p = container_of(*rb, struct kvm_event, rb);
568 parent = *rb;
569
570 if (bigger(event, p, vcpu))
571 rb = &(*rb)->rb_left;
572 else
573 rb = &(*rb)->rb_right;
574 }
575
576 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600577 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800578}
579
Xiao Guangrong37860632012-11-15 14:17:01 +0800580static void
581update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800582{
David Ahernde332ac2012-10-02 22:09:53 -0600583 int vcpu = kvm->trace_vcpu;
584
585 kvm->total_count += get_event_count(event, vcpu);
586 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800587}
588
589static bool event_is_valid(struct kvm_event *event, int vcpu)
590{
591 return !!get_event_count(event, vcpu);
592}
593
Xiao Guangrong37860632012-11-15 14:17:01 +0800594static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800595{
596 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600597 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800598 struct kvm_event *event;
599
600 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600601 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800602 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600603 update_total_count(kvm, event);
604 insert_to_result(&kvm->result, event,
605 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800606 }
607}
608
609/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600610static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800611{
David Ahernde332ac2012-10-02 22:09:53 -0600612 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800613
614 if (!node)
615 return NULL;
616
David Ahernde332ac2012-10-02 22:09:53 -0600617 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800618 return container_of(node, struct kvm_event, rb);
619}
620
621static void print_vcpu_info(int vcpu)
622{
623 pr_info("Analyze events for ");
624
625 if (vcpu == -1)
626 pr_info("all VCPUs:\n\n");
627 else
628 pr_info("VCPU %d:\n\n", vcpu);
629}
630
Xiao Guangrong37860632012-11-15 14:17:01 +0800631static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800632{
633 char decode[20];
634 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600635 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800636
637 pr_info("\n\n");
638 print_vcpu_info(vcpu);
David Ahernde332ac2012-10-02 22:09:53 -0600639 pr_info("%20s ", kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800640 pr_info("%10s ", "Samples");
641 pr_info("%9s ", "Samples%");
642
643 pr_info("%9s ", "Time%");
644 pr_info("%16s ", "Avg time");
645 pr_info("\n\n");
646
David Ahernde332ac2012-10-02 22:09:53 -0600647 while ((event = pop_from_result(&kvm->result))) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800648 u64 ecount, etime;
649
650 ecount = get_event_count(event, vcpu);
651 etime = get_event_time(event, vcpu);
652
David Ahernde332ac2012-10-02 22:09:53 -0600653 kvm->events_ops->decode_key(kvm, &event->key, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800654 pr_info("%20s ", decode);
655 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600656 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
657 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800658 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
659 kvm_event_rel_stddev(vcpu, event));
660 pr_info("\n");
661 }
662
663 pr_info("\nTotal Samples:%lld, Total events handled time:%.2fus.\n\n",
David Ahernde332ac2012-10-02 22:09:53 -0600664 (unsigned long long)kvm->total_count, kvm->total_time / 1e3);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800665}
666
David Ahernde332ac2012-10-02 22:09:53 -0600667static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800668 union perf_event *event,
669 struct perf_sample *sample,
670 struct perf_evsel *evsel,
671 struct machine *machine)
672{
673 struct thread *thread = machine__findnew_thread(machine, sample->tid);
Xiao Guangrong37860632012-11-15 14:17:01 +0800674 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
675 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800676
677 if (thread == NULL) {
678 pr_debug("problem processing %d event, skipping it.\n",
679 event->header.type);
680 return -1;
681 }
682
David Ahernde332ac2012-10-02 22:09:53 -0600683 if (!handle_kvm_event(kvm, thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800684 return -1;
685
686 return 0;
687}
688
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800689static int get_cpu_isa(struct perf_session *session)
690{
Namhyung Kim2f9e97a2012-09-24 17:15:02 +0900691 char *cpuid = session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800692 int isa;
693
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800694 if (strstr(cpuid, "Intel"))
695 isa = 1;
696 else if (strstr(cpuid, "AMD"))
697 isa = 0;
698 else {
699 pr_err("CPU %s is not supported.\n", cpuid);
700 isa = -ENOTSUP;
701 }
702
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800703 return isa;
704}
705
Xiao Guangrong37860632012-11-15 14:17:01 +0800706static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800707{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800708 int ret;
709
David Ahernde332ac2012-10-02 22:09:53 -0600710 struct perf_tool eops = {
711 .sample = process_sample_event,
712 .comm = perf_event__process_comm,
713 .ordered_samples = true,
714 };
715
716 kvm->tool = eops;
717 kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false,
718 &kvm->tool);
719 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800720 pr_err("Initializing perf session failed\n");
721 return -EINVAL;
722 }
723
David Ahernde332ac2012-10-02 22:09:53 -0600724 if (!perf_session__has_traces(kvm->session, "kvm record"))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800725 return -EINVAL;
726
727 /*
728 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
729 * traced in the old kernel.
730 */
David Ahernde332ac2012-10-02 22:09:53 -0600731 ret = get_cpu_isa(kvm->session);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800732
733 if (ret < 0)
734 return ret;
735
David Ahernde332ac2012-10-02 22:09:53 -0600736 if (ret == 1) {
737 kvm->exit_reasons = vmx_exit_reasons;
738 kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
739 kvm->exit_reasons_isa = "VMX";
740 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800741
David Ahernde332ac2012-10-02 22:09:53 -0600742 return perf_session__process_events(kvm->session, &kvm->tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800743}
744
745static bool verify_vcpu(int vcpu)
746{
747 if (vcpu != -1 && vcpu < 0) {
748 pr_err("Invalid vcpu:%d.\n", vcpu);
749 return false;
750 }
751
752 return true;
753}
754
Xiao Guangrong37860632012-11-15 14:17:01 +0800755static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800756{
757 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -0600758 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800759
760 if (!verify_vcpu(vcpu))
761 goto exit;
762
David Ahernde332ac2012-10-02 22:09:53 -0600763 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800764 goto exit;
765
David Ahernde332ac2012-10-02 22:09:53 -0600766 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800767 goto exit;
768
David Ahernde332ac2012-10-02 22:09:53 -0600769 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800770 setup_pager();
771
David Ahernde332ac2012-10-02 22:09:53 -0600772 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800773 if (ret)
774 goto exit;
775
David Ahernde332ac2012-10-02 22:09:53 -0600776 sort_result(kvm);
777 print_result(kvm);
778
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800779exit:
780 return ret;
781}
782
783static const char * const record_args[] = {
784 "record",
785 "-R",
786 "-f",
787 "-m", "1024",
788 "-c", "1",
789 "-e", "kvm:kvm_entry",
790 "-e", "kvm:kvm_exit",
791 "-e", "kvm:kvm_mmio",
792 "-e", "kvm:kvm_pio",
793};
794
795#define STRDUP_FAIL_EXIT(s) \
796 ({ char *_p; \
797 _p = strdup(s); \
798 if (!_p) \
799 return -ENOMEM; \
800 _p; \
801 })
802
Xiao Guangrong37860632012-11-15 14:17:01 +0800803static int
804kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800805{
806 unsigned int rec_argc, i, j;
807 const char **rec_argv;
808
809 rec_argc = ARRAY_SIZE(record_args) + argc + 2;
810 rec_argv = calloc(rec_argc + 1, sizeof(char *));
811
812 if (rec_argv == NULL)
813 return -ENOMEM;
814
815 for (i = 0; i < ARRAY_SIZE(record_args); i++)
816 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
817
818 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -0600819 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800820
821 for (j = 1; j < (unsigned int)argc; j++, i++)
822 rec_argv[i] = argv[j];
823
824 return cmd_record(i, rec_argv, NULL);
825}
826
Xiao Guangrong37860632012-11-15 14:17:01 +0800827static int
828kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800829{
David Ahernde332ac2012-10-02 22:09:53 -0600830 const struct option kvm_events_report_options[] = {
831 OPT_STRING(0, "event", &kvm->report_event, "report event",
832 "event for reporting: vmexit, mmio, ioport"),
833 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
834 "vcpu id to report"),
835 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
836 "key for sorting: sample(sort by samples number)"
837 " time (sort by avg time)"),
838 OPT_END()
839 };
840
841 const char * const kvm_events_report_usage[] = {
842 "perf kvm stat report [<options>]",
843 NULL
844 };
845
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800846 symbol__init();
847
848 if (argc) {
849 argc = parse_options(argc, argv,
850 kvm_events_report_options,
851 kvm_events_report_usage, 0);
852 if (argc)
853 usage_with_options(kvm_events_report_usage,
854 kvm_events_report_options);
855 }
856
David Ahernde332ac2012-10-02 22:09:53 -0600857 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800858}
859
860static void print_kvm_stat_usage(void)
861{
862 printf("Usage: perf kvm stat <command>\n\n");
863
864 printf("# Available commands:\n");
865 printf("\trecord: record kvm events\n");
866 printf("\treport: report statistical data of kvm events\n");
867
868 printf("\nOtherwise, it is the alias of 'perf stat':\n");
869}
870
Xiao Guangrong37860632012-11-15 14:17:01 +0800871static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800872{
Xiao Guangrong37860632012-11-15 14:17:01 +0800873 struct perf_kvm_stat kvm = {
874 .file_name = file_name,
875
876 .trace_vcpu = -1,
877 .report_event = "vmexit",
878 .sort_key = "sample",
879
880 .exit_reasons = svm_exit_reasons,
881 .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
882 .exit_reasons_isa = "SVM",
883 };
884
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800885 if (argc == 1) {
886 print_kvm_stat_usage();
887 goto perf_stat;
888 }
889
890 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +0800891 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800892
893 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +0800894 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800895
896perf_stat:
897 return cmd_stat(argc, argv, NULL);
898}
899
Xiao Guangrong37860632012-11-15 14:17:01 +0800900static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800901{
902 int rec_argc, i = 0, j;
903 const char **rec_argv;
904
905 rec_argc = argc + 2;
906 rec_argv = calloc(rec_argc + 1, sizeof(char *));
907 rec_argv[i++] = strdup("record");
908 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +0800909 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800910 for (j = 1; j < argc; j++, i++)
911 rec_argv[i] = argv[j];
912
913 BUG_ON(i != rec_argc);
914
915 return cmd_record(i, rec_argv, NULL);
916}
917
Xiao Guangrong37860632012-11-15 14:17:01 +0800918static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800919{
920 int rec_argc, i = 0, j;
921 const char **rec_argv;
922
923 rec_argc = argc + 2;
924 rec_argv = calloc(rec_argc + 1, sizeof(char *));
925 rec_argv[i++] = strdup("report");
926 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +0800927 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800928 for (j = 1; j < argc; j++, i++)
929 rec_argv[i] = argv[j];
930
931 BUG_ON(i != rec_argc);
932
933 return cmd_report(i, rec_argv, NULL);
934}
935
Xiao Guangrong37860632012-11-15 14:17:01 +0800936static int
937__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800938{
939 int rec_argc, i = 0, j;
940 const char **rec_argv;
941
942 rec_argc = argc + 2;
943 rec_argv = calloc(rec_argc + 1, sizeof(char *));
944 rec_argv[i++] = strdup("buildid-list");
945 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +0800946 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800947 for (j = 1; j < argc; j++, i++)
948 rec_argv[i] = argv[j];
949
950 BUG_ON(i != rec_argc);
951
952 return cmd_buildid_list(i, rec_argv, NULL);
953}
954
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300955int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800956{
Xiao Guangrong37860632012-11-15 14:17:01 +0800957 const char *file_name;
David Ahernde332ac2012-10-02 22:09:53 -0600958
959 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +0800960 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -0600961 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +0800962 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -0600963 "Output file name"),
964 OPT_BOOLEAN(0, "guest", &perf_guest,
965 "Collect guest os data"),
966 OPT_BOOLEAN(0, "host", &perf_host,
967 "Collect host os data"),
968 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
969 "guest mount directory under which every guest os"
970 " instance has a subdir"),
971 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
972 "file", "file saving guest os vmlinux"),
973 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
974 "file", "file saving guest os /proc/kallsyms"),
975 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
976 "file", "file saving guest os /proc/modules"),
977 OPT_END()
978 };
979
980
981 const char * const kvm_usage[] = {
982 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
983 NULL
984 };
985
Joerg Roedel1aed2672012-01-04 17:54:20 +0100986 perf_host = 0;
987 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800988
989 argc = parse_options(argc, argv, kvm_options, kvm_usage,
990 PARSE_OPT_STOP_AT_NON_OPTION);
991 if (!argc)
992 usage_with_options(kvm_usage, kvm_options);
993
994 if (!perf_host)
995 perf_guest = 1;
996
Xiao Guangrong37860632012-11-15 14:17:01 +0800997 if (!file_name) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800998 if (perf_host && !perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +0800999 file_name = strdup("perf.data.host");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001000 else if (!perf_host && perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001001 file_name = strdup("perf.data.guest");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001002 else
Xiao Guangrong37860632012-11-15 14:17:01 +08001003 file_name = strdup("perf.data.kvm");
David Ahernde332ac2012-10-02 22:09:53 -06001004
Xiao Guangrong37860632012-11-15 14:17:01 +08001005 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001006 pr_err("Failed to allocate memory for filename\n");
1007 return -ENOMEM;
1008 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001009 }
1010
1011 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001012 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001013 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001014 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001015 else if (!strncmp(argv[0], "diff", 4))
1016 return cmd_diff(argc, argv, NULL);
1017 else if (!strncmp(argv[0], "top", 3))
1018 return cmd_top(argc, argv, NULL);
1019 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001020 return __cmd_buildid_list(file_name, argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001021 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001022 return kvm_cmd_stat(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001023 else
1024 usage_with_options(kvm_usage, kvm_options);
1025
1026 return 0;
1027}