blob: 283b4397e397fdd3155c4bcf11b23caeeb2bb139 [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
Xiao Guangrong73210902012-11-19 16:19:21 +080025#if defined(__i386__) || defined(__x86_64__)
David Howellsd2709c72012-11-19 22:21:03 +000026#include <asm/svm.h>
27#include <asm/vmx.h>
28#include <asm/kvm.h>
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080029
30struct event_key {
31 #define INVALID_KEY (~0ULL)
32 u64 key;
33 int info;
34};
35
David Ahernde332ac2012-10-02 22:09:53 -060036struct kvm_event_stats {
37 u64 time;
38 struct stats stats;
39};
40
41struct kvm_event {
42 struct list_head hash_entry;
43 struct rb_node rb;
44
45 struct event_key key;
46
47 struct kvm_event_stats total;
48
49 #define DEFAULT_VCPU_NUM 8
50 int max_vcpu;
51 struct kvm_event_stats *vcpu;
52};
53
54typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
55
56struct kvm_event_key {
57 const char *name;
58 key_cmp_fun key;
59};
60
61
Xiao Guangrong37860632012-11-15 14:17:01 +080062struct perf_kvm_stat;
David Ahernde332ac2012-10-02 22:09:53 -060063
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080064struct kvm_events_ops {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030065 bool (*is_begin_event)(struct perf_evsel *evsel,
66 struct perf_sample *sample,
67 struct event_key *key);
68 bool (*is_end_event)(struct perf_evsel *evsel,
69 struct perf_sample *sample, struct event_key *key);
Xiao Guangrong37860632012-11-15 14:17:01 +080070 void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
David Ahernde332ac2012-10-02 22:09:53 -060071 char decode[20]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080072 const char *name;
73};
74
David Ahernde332ac2012-10-02 22:09:53 -060075struct exit_reasons_table {
76 unsigned long exit_code;
77 const char *reason;
78};
79
80#define EVENTS_BITS 12
81#define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
82
Xiao Guangrong37860632012-11-15 14:17:01 +080083struct perf_kvm_stat {
David Ahernde332ac2012-10-02 22:09:53 -060084 struct perf_tool tool;
85 struct perf_session *session;
86
87 const char *file_name;
88 const char *report_event;
89 const char *sort_key;
90 int trace_vcpu;
91
92 struct exit_reasons_table *exit_reasons;
93 int exit_reasons_size;
94 const char *exit_reasons_isa;
95
96 struct kvm_events_ops *events_ops;
97 key_cmp_fun compare;
98 struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
99 u64 total_time;
100 u64 total_count;
101
102 struct rb_root result;
103};
104
105
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300106static void exit_event_get_key(struct perf_evsel *evsel,
107 struct perf_sample *sample,
108 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800109{
110 key->info = 0;
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300111 key->key = perf_evsel__intval(evsel, sample, "exit_reason");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800112}
113
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300114static bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800115{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300116 return !strcmp(evsel->name, "kvm:kvm_exit");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800117}
118
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300119static bool exit_event_begin(struct perf_evsel *evsel,
120 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800121{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300122 if (kvm_exit_event(evsel)) {
123 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800124 return true;
125 }
126
127 return false;
128}
129
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300130static bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800131{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300132 return !strcmp(evsel->name, "kvm:kvm_entry");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800133}
134
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300135static bool exit_event_end(struct perf_evsel *evsel,
136 struct perf_sample *sample __maybe_unused,
137 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800138{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300139 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800140}
141
David Ahernde332ac2012-10-02 22:09:53 -0600142static struct exit_reasons_table vmx_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800143 VMX_EXIT_REASONS
144};
145
David Ahernde332ac2012-10-02 22:09:53 -0600146static struct exit_reasons_table svm_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800147 SVM_EXIT_REASONS
148};
149
Xiao Guangrong37860632012-11-15 14:17:01 +0800150static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800151{
David Ahernde332ac2012-10-02 22:09:53 -0600152 int i = kvm->exit_reasons_size;
153 struct exit_reasons_table *tbl = kvm->exit_reasons;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800154
David Ahernde332ac2012-10-02 22:09:53 -0600155 while (i--) {
156 if (tbl->exit_code == exit_code)
157 return tbl->reason;
158 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800159 }
160
161 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600162 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800163 return "UNKNOWN";
164}
165
Xiao Guangrong37860632012-11-15 14:17:01 +0800166static void exit_event_decode_key(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600167 struct event_key *key,
168 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800169{
David Ahernde332ac2012-10-02 22:09:53 -0600170 const char *exit_reason = get_exit_reason(kvm, key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800171
172 scnprintf(decode, 20, "%s", exit_reason);
173}
174
175static struct kvm_events_ops exit_events = {
176 .is_begin_event = exit_event_begin,
177 .is_end_event = exit_event_end,
178 .decode_key = exit_event_decode_key,
179 .name = "VM-EXIT"
180};
181
David Ahernde332ac2012-10-02 22:09:53 -0600182/*
183 * For the mmio events, we treat:
184 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
185 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
186 */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300187static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
188 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800189{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300190 key->key = perf_evsel__intval(evsel, sample, "gpa");
191 key->info = perf_evsel__intval(evsel, sample, "type");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800192}
193
194#define KVM_TRACE_MMIO_READ_UNSATISFIED 0
195#define KVM_TRACE_MMIO_READ 1
196#define KVM_TRACE_MMIO_WRITE 2
197
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300198static bool mmio_event_begin(struct perf_evsel *evsel,
199 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800200{
201 /* MMIO read begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300202 if (kvm_exit_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800203 return true;
204
205 /* MMIO write begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300206 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
207 perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
208 mmio_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800209 return true;
210 }
211
212 return false;
213}
214
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300215static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
216 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800217{
218 /* MMIO write end event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300219 if (kvm_entry_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800220 return true;
221
222 /* MMIO read end 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_READ) {
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
Xiao Guangrong37860632012-11-15 14:17:01 +0800232static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600233 struct event_key *key,
234 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800235{
236 scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
237 key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
238}
239
240static struct kvm_events_ops mmio_events = {
241 .is_begin_event = mmio_event_begin,
242 .is_end_event = mmio_event_end,
243 .decode_key = mmio_event_decode_key,
244 .name = "MMIO Access"
245};
246
247 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300248static void ioport_event_get_key(struct perf_evsel *evsel,
249 struct perf_sample *sample,
250 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800251{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300252 key->key = perf_evsel__intval(evsel, sample, "port");
253 key->info = perf_evsel__intval(evsel, sample, "rw");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800254}
255
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300256static bool ioport_event_begin(struct perf_evsel *evsel,
257 struct perf_sample *sample,
258 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800259{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300260 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
261 ioport_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800262 return true;
263 }
264
265 return false;
266}
267
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300268static bool ioport_event_end(struct perf_evsel *evsel,
269 struct perf_sample *sample __maybe_unused,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800270 struct event_key *key __maybe_unused)
271{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300272 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800273}
274
Xiao Guangrong37860632012-11-15 14:17:01 +0800275static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600276 struct event_key *key,
277 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800278{
279 scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
280 key->info ? "POUT" : "PIN");
281}
282
283static struct kvm_events_ops ioport_events = {
284 .is_begin_event = ioport_event_begin,
285 .is_end_event = ioport_event_end,
286 .decode_key = ioport_event_decode_key,
287 .name = "IO Port Access"
288};
289
Xiao Guangrong37860632012-11-15 14:17:01 +0800290static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800291{
292 bool ret = true;
293
David Ahernde332ac2012-10-02 22:09:53 -0600294 if (!strcmp(kvm->report_event, "vmexit"))
295 kvm->events_ops = &exit_events;
296 else if (!strcmp(kvm->report_event, "mmio"))
297 kvm->events_ops = &mmio_events;
298 else if (!strcmp(kvm->report_event, "ioport"))
299 kvm->events_ops = &ioport_events;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800300 else {
David Ahernde332ac2012-10-02 22:09:53 -0600301 pr_err("Unknown report event:%s\n", kvm->report_event);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800302 ret = false;
303 }
304
305 return ret;
306}
307
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800308struct vcpu_event_record {
309 int vcpu_id;
310 u64 start_time;
311 struct kvm_event *last_event;
312};
313
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800314
Xiao Guangrong37860632012-11-15 14:17:01 +0800315static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800316{
317 int i;
318
319 for (i = 0; i < (int)EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600320 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800321}
322
323static int kvm_events_hash_fn(u64 key)
324{
325 return key & (EVENTS_CACHE_SIZE - 1);
326}
327
328static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
329{
330 int old_max_vcpu = event->max_vcpu;
331
332 if (vcpu_id < event->max_vcpu)
333 return true;
334
335 while (event->max_vcpu <= vcpu_id)
336 event->max_vcpu += DEFAULT_VCPU_NUM;
337
338 event->vcpu = realloc(event->vcpu,
339 event->max_vcpu * sizeof(*event->vcpu));
340 if (!event->vcpu) {
341 pr_err("Not enough memory\n");
342 return false;
343 }
344
345 memset(event->vcpu + old_max_vcpu, 0,
346 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
347 return true;
348}
349
350static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
351{
352 struct kvm_event *event;
353
354 event = zalloc(sizeof(*event));
355 if (!event) {
356 pr_err("Not enough memory\n");
357 return NULL;
358 }
359
360 event->key = *key;
361 return event;
362}
363
Xiao Guangrong37860632012-11-15 14:17:01 +0800364static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600365 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800366{
367 struct kvm_event *event;
368 struct list_head *head;
369
370 BUG_ON(key->key == INVALID_KEY);
371
David Ahernde332ac2012-10-02 22:09:53 -0600372 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800373 list_for_each_entry(event, head, hash_entry)
374 if (event->key.key == key->key && event->key.info == key->info)
375 return event;
376
377 event = kvm_alloc_init_event(key);
378 if (!event)
379 return NULL;
380
381 list_add(&event->hash_entry, head);
382 return event;
383}
384
Xiao Guangrong37860632012-11-15 14:17:01 +0800385static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600386 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800387 struct event_key *key, u64 timestamp)
388{
389 struct kvm_event *event = NULL;
390
391 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600392 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800393
394 vcpu_record->last_event = event;
395 vcpu_record->start_time = timestamp;
396 return true;
397}
398
399static void
400kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
401{
402 kvm_stats->time += time_diff;
403 update_stats(&kvm_stats->stats, time_diff);
404}
405
406static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
407{
408 struct kvm_event_stats *kvm_stats = &event->total;
409
410 if (vcpu_id != -1)
411 kvm_stats = &event->vcpu[vcpu_id];
412
413 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
414 avg_stats(&kvm_stats->stats));
415}
416
417static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
418 u64 time_diff)
419{
420 kvm_update_event_stats(&event->total, time_diff);
421
422 if (!kvm_event_expand(event, vcpu_id))
423 return false;
424
425 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
426 return true;
427}
428
Xiao Guangrong37860632012-11-15 14:17:01 +0800429static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600430 struct vcpu_event_record *vcpu_record,
431 struct event_key *key,
432 u64 timestamp)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800433{
434 struct kvm_event *event;
435 u64 time_begin, time_diff;
436
437 event = vcpu_record->last_event;
438 time_begin = vcpu_record->start_time;
439
440 /* The begin event is not caught. */
441 if (!time_begin)
442 return true;
443
444 /*
445 * In some case, the 'begin event' only records the start timestamp,
446 * the actual event is recognized in the 'end event' (e.g. mmio-event).
447 */
448
449 /* Both begin and end events did not get the key. */
450 if (!event && key->key == INVALID_KEY)
451 return true;
452
453 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600454 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800455
456 if (!event)
457 return false;
458
459 vcpu_record->last_event = NULL;
460 vcpu_record->start_time = 0;
461
462 BUG_ON(timestamp < time_begin);
463
464 time_diff = timestamp - time_begin;
465 return update_kvm_event(event, vcpu_record->vcpu_id, time_diff);
466}
467
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300468static
469struct vcpu_event_record *per_vcpu_record(struct thread *thread,
470 struct perf_evsel *evsel,
471 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800472{
473 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300474 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800475 struct vcpu_event_record *vcpu_record;
476
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300477 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800478 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300479 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800480 return NULL;
481 }
482
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300483 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800484 thread->priv = vcpu_record;
485 }
486
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300487 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800488}
489
Xiao Guangrong37860632012-11-15 14:17:01 +0800490static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600491 struct thread *thread,
492 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300493 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800494{
495 struct vcpu_event_record *vcpu_record;
496 struct event_key key = {.key = INVALID_KEY};
497
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300498 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800499 if (!vcpu_record)
500 return true;
501
David Ahernde332ac2012-10-02 22:09:53 -0600502 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
503 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800504
David Ahernde332ac2012-10-02 22:09:53 -0600505 if (kvm->events_ops->is_end_event(evsel, sample, &key))
506 return handle_end_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800507
508 return true;
509}
510
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800511#define GET_EVENT_KEY(func, field) \
512static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
513{ \
514 if (vcpu == -1) \
515 return event->total.field; \
516 \
517 if (vcpu >= event->max_vcpu) \
518 return 0; \
519 \
520 return event->vcpu[vcpu].field; \
521}
522
523#define COMPARE_EVENT_KEY(func, field) \
524GET_EVENT_KEY(func, field) \
525static int compare_kvm_event_ ## func(struct kvm_event *one, \
526 struct kvm_event *two, int vcpu)\
527{ \
528 return get_event_ ##func(one, vcpu) > \
529 get_event_ ##func(two, vcpu); \
530}
531
532GET_EVENT_KEY(time, time);
533COMPARE_EVENT_KEY(count, stats.n);
534COMPARE_EVENT_KEY(mean, stats.mean);
535
536#define DEF_SORT_NAME_KEY(name, compare_key) \
537 { #name, compare_kvm_event_ ## compare_key }
538
539static struct kvm_event_key keys[] = {
540 DEF_SORT_NAME_KEY(sample, count),
541 DEF_SORT_NAME_KEY(time, mean),
542 { NULL, NULL }
543};
544
Xiao Guangrong37860632012-11-15 14:17:01 +0800545static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800546{
547 int i;
548
549 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600550 if (!strcmp(keys[i].name, kvm->sort_key)) {
551 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800552 return true;
553 }
554 }
555
David Ahernde332ac2012-10-02 22:09:53 -0600556 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800557 return false;
558}
559
David Ahernde332ac2012-10-02 22:09:53 -0600560static void insert_to_result(struct rb_root *result, struct kvm_event *event,
561 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800562{
David Ahernde332ac2012-10-02 22:09:53 -0600563 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800564 struct rb_node *parent = NULL;
565 struct kvm_event *p;
566
567 while (*rb) {
568 p = container_of(*rb, struct kvm_event, rb);
569 parent = *rb;
570
571 if (bigger(event, p, vcpu))
572 rb = &(*rb)->rb_left;
573 else
574 rb = &(*rb)->rb_right;
575 }
576
577 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600578 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800579}
580
Xiao Guangrong37860632012-11-15 14:17:01 +0800581static void
582update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800583{
David Ahernde332ac2012-10-02 22:09:53 -0600584 int vcpu = kvm->trace_vcpu;
585
586 kvm->total_count += get_event_count(event, vcpu);
587 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800588}
589
590static bool event_is_valid(struct kvm_event *event, int vcpu)
591{
592 return !!get_event_count(event, vcpu);
593}
594
Xiao Guangrong37860632012-11-15 14:17:01 +0800595static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800596{
597 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600598 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800599 struct kvm_event *event;
600
601 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600602 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800603 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600604 update_total_count(kvm, event);
605 insert_to_result(&kvm->result, event,
606 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800607 }
608}
609
610/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600611static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800612{
David Ahernde332ac2012-10-02 22:09:53 -0600613 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800614
615 if (!node)
616 return NULL;
617
David Ahernde332ac2012-10-02 22:09:53 -0600618 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800619 return container_of(node, struct kvm_event, rb);
620}
621
622static void print_vcpu_info(int vcpu)
623{
624 pr_info("Analyze events for ");
625
626 if (vcpu == -1)
627 pr_info("all VCPUs:\n\n");
628 else
629 pr_info("VCPU %d:\n\n", vcpu);
630}
631
Xiao Guangrong37860632012-11-15 14:17:01 +0800632static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800633{
634 char decode[20];
635 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600636 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800637
638 pr_info("\n\n");
639 print_vcpu_info(vcpu);
David Ahernde332ac2012-10-02 22:09:53 -0600640 pr_info("%20s ", kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800641 pr_info("%10s ", "Samples");
642 pr_info("%9s ", "Samples%");
643
644 pr_info("%9s ", "Time%");
645 pr_info("%16s ", "Avg time");
646 pr_info("\n\n");
647
David Ahernde332ac2012-10-02 22:09:53 -0600648 while ((event = pop_from_result(&kvm->result))) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800649 u64 ecount, etime;
650
651 ecount = get_event_count(event, vcpu);
652 etime = get_event_time(event, vcpu);
653
David Ahernde332ac2012-10-02 22:09:53 -0600654 kvm->events_ops->decode_key(kvm, &event->key, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800655 pr_info("%20s ", decode);
656 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600657 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
658 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800659 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
660 kvm_event_rel_stddev(vcpu, event));
661 pr_info("\n");
662 }
663
664 pr_info("\nTotal Samples:%lld, Total events handled time:%.2fus.\n\n",
David Ahernde332ac2012-10-02 22:09:53 -0600665 (unsigned long long)kvm->total_count, kvm->total_time / 1e3);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800666}
667
David Ahernde332ac2012-10-02 22:09:53 -0600668static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800669 union perf_event *event,
670 struct perf_sample *sample,
671 struct perf_evsel *evsel,
672 struct machine *machine)
673{
674 struct thread *thread = machine__findnew_thread(machine, sample->tid);
Xiao Guangrong37860632012-11-15 14:17:01 +0800675 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
676 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800677
678 if (thread == NULL) {
679 pr_debug("problem processing %d event, skipping it.\n",
680 event->header.type);
681 return -1;
682 }
683
David Ahernde332ac2012-10-02 22:09:53 -0600684 if (!handle_kvm_event(kvm, thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800685 return -1;
686
687 return 0;
688}
689
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800690static int get_cpu_isa(struct perf_session *session)
691{
Namhyung Kim2f9e97a2012-09-24 17:15:02 +0900692 char *cpuid = session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800693 int isa;
694
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800695 if (strstr(cpuid, "Intel"))
696 isa = 1;
697 else if (strstr(cpuid, "AMD"))
698 isa = 0;
699 else {
700 pr_err("CPU %s is not supported.\n", cpuid);
701 isa = -ENOTSUP;
702 }
703
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800704 return isa;
705}
706
Xiao Guangrong37860632012-11-15 14:17:01 +0800707static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800708{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800709 int ret;
710
David Ahernde332ac2012-10-02 22:09:53 -0600711 struct perf_tool eops = {
712 .sample = process_sample_event,
713 .comm = perf_event__process_comm,
714 .ordered_samples = true,
715 };
716
717 kvm->tool = eops;
718 kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false,
719 &kvm->tool);
720 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800721 pr_err("Initializing perf session failed\n");
722 return -EINVAL;
723 }
724
David Ahernde332ac2012-10-02 22:09:53 -0600725 if (!perf_session__has_traces(kvm->session, "kvm record"))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800726 return -EINVAL;
727
728 /*
729 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
730 * traced in the old kernel.
731 */
David Ahernde332ac2012-10-02 22:09:53 -0600732 ret = get_cpu_isa(kvm->session);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800733
734 if (ret < 0)
735 return ret;
736
David Ahernde332ac2012-10-02 22:09:53 -0600737 if (ret == 1) {
738 kvm->exit_reasons = vmx_exit_reasons;
739 kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
740 kvm->exit_reasons_isa = "VMX";
741 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800742
David Ahernde332ac2012-10-02 22:09:53 -0600743 return perf_session__process_events(kvm->session, &kvm->tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800744}
745
746static bool verify_vcpu(int vcpu)
747{
748 if (vcpu != -1 && vcpu < 0) {
749 pr_err("Invalid vcpu:%d.\n", vcpu);
750 return false;
751 }
752
753 return true;
754}
755
Xiao Guangrong37860632012-11-15 14:17:01 +0800756static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800757{
758 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -0600759 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800760
761 if (!verify_vcpu(vcpu))
762 goto exit;
763
David Ahernde332ac2012-10-02 22:09:53 -0600764 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800765 goto exit;
766
David Ahernde332ac2012-10-02 22:09:53 -0600767 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800768 goto exit;
769
David Ahernde332ac2012-10-02 22:09:53 -0600770 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800771 setup_pager();
772
David Ahernde332ac2012-10-02 22:09:53 -0600773 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800774 if (ret)
775 goto exit;
776
David Ahernde332ac2012-10-02 22:09:53 -0600777 sort_result(kvm);
778 print_result(kvm);
779
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800780exit:
781 return ret;
782}
783
784static const char * const record_args[] = {
785 "record",
786 "-R",
787 "-f",
788 "-m", "1024",
789 "-c", "1",
790 "-e", "kvm:kvm_entry",
791 "-e", "kvm:kvm_exit",
792 "-e", "kvm:kvm_mmio",
793 "-e", "kvm:kvm_pio",
794};
795
796#define STRDUP_FAIL_EXIT(s) \
797 ({ char *_p; \
798 _p = strdup(s); \
799 if (!_p) \
800 return -ENOMEM; \
801 _p; \
802 })
803
Xiao Guangrong37860632012-11-15 14:17:01 +0800804static int
805kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800806{
807 unsigned int rec_argc, i, j;
808 const char **rec_argv;
809
810 rec_argc = ARRAY_SIZE(record_args) + argc + 2;
811 rec_argv = calloc(rec_argc + 1, sizeof(char *));
812
813 if (rec_argv == NULL)
814 return -ENOMEM;
815
816 for (i = 0; i < ARRAY_SIZE(record_args); i++)
817 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
818
819 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -0600820 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800821
822 for (j = 1; j < (unsigned int)argc; j++, i++)
823 rec_argv[i] = argv[j];
824
825 return cmd_record(i, rec_argv, NULL);
826}
827
Xiao Guangrong37860632012-11-15 14:17:01 +0800828static int
829kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800830{
David Ahernde332ac2012-10-02 22:09:53 -0600831 const struct option kvm_events_report_options[] = {
832 OPT_STRING(0, "event", &kvm->report_event, "report event",
833 "event for reporting: vmexit, mmio, ioport"),
834 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
835 "vcpu id to report"),
836 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
837 "key for sorting: sample(sort by samples number)"
838 " time (sort by avg time)"),
839 OPT_END()
840 };
841
842 const char * const kvm_events_report_usage[] = {
843 "perf kvm stat report [<options>]",
844 NULL
845 };
846
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800847 symbol__init();
848
849 if (argc) {
850 argc = parse_options(argc, argv,
851 kvm_events_report_options,
852 kvm_events_report_usage, 0);
853 if (argc)
854 usage_with_options(kvm_events_report_usage,
855 kvm_events_report_options);
856 }
857
David Ahernde332ac2012-10-02 22:09:53 -0600858 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800859}
860
861static void print_kvm_stat_usage(void)
862{
863 printf("Usage: perf kvm stat <command>\n\n");
864
865 printf("# Available commands:\n");
866 printf("\trecord: record kvm events\n");
867 printf("\treport: report statistical data of kvm events\n");
868
869 printf("\nOtherwise, it is the alias of 'perf stat':\n");
870}
871
Xiao Guangrong37860632012-11-15 14:17:01 +0800872static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800873{
Xiao Guangrong37860632012-11-15 14:17:01 +0800874 struct perf_kvm_stat kvm = {
875 .file_name = file_name,
876
877 .trace_vcpu = -1,
878 .report_event = "vmexit",
879 .sort_key = "sample",
880
881 .exit_reasons = svm_exit_reasons,
882 .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
883 .exit_reasons_isa = "SVM",
884 };
885
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800886 if (argc == 1) {
887 print_kvm_stat_usage();
888 goto perf_stat;
889 }
890
891 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +0800892 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800893
894 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +0800895 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800896
897perf_stat:
898 return cmd_stat(argc, argv, NULL);
899}
Xiao Guangrong73210902012-11-19 16:19:21 +0800900#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800901
Xiao Guangrong37860632012-11-15 14:17:01 +0800902static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800903{
904 int rec_argc, i = 0, j;
905 const char **rec_argv;
906
907 rec_argc = argc + 2;
908 rec_argv = calloc(rec_argc + 1, sizeof(char *));
909 rec_argv[i++] = strdup("record");
910 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +0800911 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800912 for (j = 1; j < argc; j++, i++)
913 rec_argv[i] = argv[j];
914
915 BUG_ON(i != rec_argc);
916
917 return cmd_record(i, rec_argv, NULL);
918}
919
Xiao Guangrong37860632012-11-15 14:17:01 +0800920static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800921{
922 int rec_argc, i = 0, j;
923 const char **rec_argv;
924
925 rec_argc = argc + 2;
926 rec_argv = calloc(rec_argc + 1, sizeof(char *));
927 rec_argv[i++] = strdup("report");
928 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +0800929 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800930 for (j = 1; j < argc; j++, i++)
931 rec_argv[i] = argv[j];
932
933 BUG_ON(i != rec_argc);
934
935 return cmd_report(i, rec_argv, NULL);
936}
937
Xiao Guangrong37860632012-11-15 14:17:01 +0800938static int
939__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800940{
941 int rec_argc, i = 0, j;
942 const char **rec_argv;
943
944 rec_argc = argc + 2;
945 rec_argv = calloc(rec_argc + 1, sizeof(char *));
946 rec_argv[i++] = strdup("buildid-list");
947 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +0800948 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800949 for (j = 1; j < argc; j++, i++)
950 rec_argv[i] = argv[j];
951
952 BUG_ON(i != rec_argc);
953
954 return cmd_buildid_list(i, rec_argv, NULL);
955}
956
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300957int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800958{
Xiao Guangrong37860632012-11-15 14:17:01 +0800959 const char *file_name;
David Ahernde332ac2012-10-02 22:09:53 -0600960
961 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +0800962 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -0600963 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +0800964 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -0600965 "Output file name"),
966 OPT_BOOLEAN(0, "guest", &perf_guest,
967 "Collect guest os data"),
968 OPT_BOOLEAN(0, "host", &perf_host,
969 "Collect host os data"),
970 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
971 "guest mount directory under which every guest os"
972 " instance has a subdir"),
973 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
974 "file", "file saving guest os vmlinux"),
975 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
976 "file", "file saving guest os /proc/kallsyms"),
977 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
978 "file", "file saving guest os /proc/modules"),
979 OPT_END()
980 };
981
982
983 const char * const kvm_usage[] = {
984 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
985 NULL
986 };
987
Joerg Roedel1aed2672012-01-04 17:54:20 +0100988 perf_host = 0;
989 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800990
991 argc = parse_options(argc, argv, kvm_options, kvm_usage,
992 PARSE_OPT_STOP_AT_NON_OPTION);
993 if (!argc)
994 usage_with_options(kvm_usage, kvm_options);
995
996 if (!perf_host)
997 perf_guest = 1;
998
Xiao Guangrong37860632012-11-15 14:17:01 +0800999 if (!file_name) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001000 if (perf_host && !perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001001 file_name = strdup("perf.data.host");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001002 else if (!perf_host && perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001003 file_name = strdup("perf.data.guest");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001004 else
Xiao Guangrong37860632012-11-15 14:17:01 +08001005 file_name = strdup("perf.data.kvm");
David Ahernde332ac2012-10-02 22:09:53 -06001006
Xiao Guangrong37860632012-11-15 14:17:01 +08001007 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001008 pr_err("Failed to allocate memory for filename\n");
1009 return -ENOMEM;
1010 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001011 }
1012
1013 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001014 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001015 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001016 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001017 else if (!strncmp(argv[0], "diff", 4))
1018 return cmd_diff(argc, argv, NULL);
1019 else if (!strncmp(argv[0], "top", 3))
1020 return cmd_top(argc, argv, NULL);
1021 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001022 return __cmd_buildid_list(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001023#if defined(__i386__) || defined(__x86_64__)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001024 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001025 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001026#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001027 else
1028 usage_with_options(kvm_usage, kvm_options);
1029
1030 return 0;
1031}