blob: 935d52216c899eb42774ae9f6e63767ff77fc8ba [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"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080020
21#include <sys/prctl.h>
David Ahern1afe1d12013-08-05 21:41:34 -040022#include <sys/timerfd.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080023
David Ahern1afe1d12013-08-05 21:41:34 -040024#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080025#include <semaphore.h>
26#include <pthread.h>
27#include <math.h>
28
Xiao Guangrong73210902012-11-19 16:19:21 +080029#if defined(__i386__) || defined(__x86_64__)
David Howellsd2709c72012-11-19 22:21:03 +000030#include <asm/svm.h>
31#include <asm/vmx.h>
32#include <asm/kvm.h>
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080033
34struct event_key {
35 #define INVALID_KEY (~0ULL)
36 u64 key;
37 int info;
38};
39
David Ahernde332ac2012-10-02 22:09:53 -060040struct kvm_event_stats {
41 u64 time;
42 struct stats stats;
43};
44
45struct kvm_event {
46 struct list_head hash_entry;
47 struct rb_node rb;
48
49 struct event_key key;
50
51 struct kvm_event_stats total;
52
53 #define DEFAULT_VCPU_NUM 8
54 int max_vcpu;
55 struct kvm_event_stats *vcpu;
56};
57
58typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
59
60struct kvm_event_key {
61 const char *name;
62 key_cmp_fun key;
63};
64
65
Xiao Guangrong37860632012-11-15 14:17:01 +080066struct perf_kvm_stat;
David Ahernde332ac2012-10-02 22:09:53 -060067
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080068struct kvm_events_ops {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030069 bool (*is_begin_event)(struct perf_evsel *evsel,
70 struct perf_sample *sample,
71 struct event_key *key);
72 bool (*is_end_event)(struct perf_evsel *evsel,
73 struct perf_sample *sample, struct event_key *key);
Xiao Guangrong37860632012-11-15 14:17:01 +080074 void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
David Ahernde332ac2012-10-02 22:09:53 -060075 char decode[20]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080076 const char *name;
77};
78
David Ahernde332ac2012-10-02 22:09:53 -060079struct exit_reasons_table {
80 unsigned long exit_code;
81 const char *reason;
82};
83
84#define EVENTS_BITS 12
85#define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
86
Xiao Guangrong37860632012-11-15 14:17:01 +080087struct perf_kvm_stat {
David Ahernde332ac2012-10-02 22:09:53 -060088 struct perf_tool tool;
David Ahern1afe1d12013-08-05 21:41:34 -040089 struct perf_record_opts opts;
90 struct perf_evlist *evlist;
David Ahernde332ac2012-10-02 22:09:53 -060091 struct perf_session *session;
92
93 const char *file_name;
94 const char *report_event;
95 const char *sort_key;
96 int trace_vcpu;
97
98 struct exit_reasons_table *exit_reasons;
99 int exit_reasons_size;
100 const char *exit_reasons_isa;
101
102 struct kvm_events_ops *events_ops;
103 key_cmp_fun compare;
104 struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
David Ahern1afe1d12013-08-05 21:41:34 -0400105
David Ahernde332ac2012-10-02 22:09:53 -0600106 u64 total_time;
107 u64 total_count;
David Ahern1afe1d12013-08-05 21:41:34 -0400108 u64 lost_events;
David Ahern70f7b4a2013-08-07 21:56:38 -0400109 u64 duration;
David Ahernde332ac2012-10-02 22:09:53 -0600110
David Ahern2e73f002013-08-05 21:41:37 -0400111 const char *pid_str;
112 struct intlist *pid_list;
113
David Ahernde332ac2012-10-02 22:09:53 -0600114 struct rb_root result;
David Ahern1afe1d12013-08-05 21:41:34 -0400115
116 int timerfd;
117 unsigned int display_time;
118 bool live;
David Ahernde332ac2012-10-02 22:09:53 -0600119};
120
121
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300122static void exit_event_get_key(struct perf_evsel *evsel,
123 struct perf_sample *sample,
124 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800125{
126 key->info = 0;
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300127 key->key = perf_evsel__intval(evsel, sample, "exit_reason");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800128}
129
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300130static bool kvm_exit_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_exit");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800133}
134
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300135static bool exit_event_begin(struct perf_evsel *evsel,
136 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800137{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300138 if (kvm_exit_event(evsel)) {
139 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800140 return true;
141 }
142
143 return false;
144}
145
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300146static bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800147{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300148 return !strcmp(evsel->name, "kvm:kvm_entry");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800149}
150
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300151static bool exit_event_end(struct perf_evsel *evsel,
152 struct perf_sample *sample __maybe_unused,
153 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800154{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300155 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800156}
157
David Ahernde332ac2012-10-02 22:09:53 -0600158static struct exit_reasons_table vmx_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800159 VMX_EXIT_REASONS
160};
161
David Ahernde332ac2012-10-02 22:09:53 -0600162static struct exit_reasons_table svm_exit_reasons[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800163 SVM_EXIT_REASONS
164};
165
Xiao Guangrong37860632012-11-15 14:17:01 +0800166static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800167{
David Ahernde332ac2012-10-02 22:09:53 -0600168 int i = kvm->exit_reasons_size;
169 struct exit_reasons_table *tbl = kvm->exit_reasons;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800170
David Ahernde332ac2012-10-02 22:09:53 -0600171 while (i--) {
172 if (tbl->exit_code == exit_code)
173 return tbl->reason;
174 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800175 }
176
177 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600178 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800179 return "UNKNOWN";
180}
181
Xiao Guangrong37860632012-11-15 14:17:01 +0800182static void exit_event_decode_key(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600183 struct event_key *key,
184 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800185{
David Ahernde332ac2012-10-02 22:09:53 -0600186 const char *exit_reason = get_exit_reason(kvm, key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800187
188 scnprintf(decode, 20, "%s", exit_reason);
189}
190
191static struct kvm_events_ops exit_events = {
192 .is_begin_event = exit_event_begin,
193 .is_end_event = exit_event_end,
194 .decode_key = exit_event_decode_key,
195 .name = "VM-EXIT"
196};
197
David Ahernde332ac2012-10-02 22:09:53 -0600198/*
199 * For the mmio events, we treat:
200 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
201 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
202 */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300203static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
204 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800205{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300206 key->key = perf_evsel__intval(evsel, sample, "gpa");
207 key->info = perf_evsel__intval(evsel, sample, "type");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800208}
209
210#define KVM_TRACE_MMIO_READ_UNSATISFIED 0
211#define KVM_TRACE_MMIO_READ 1
212#define KVM_TRACE_MMIO_WRITE 2
213
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300214static bool mmio_event_begin(struct perf_evsel *evsel,
215 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800216{
217 /* MMIO read begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300218 if (kvm_exit_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800219 return true;
220
221 /* MMIO write begin 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_WRITE) {
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
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300231static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
232 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800233{
234 /* MMIO write end event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300235 if (kvm_entry_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800236 return true;
237
238 /* MMIO read end event in kernel.*/
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300239 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
240 perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
241 mmio_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800242 return true;
243 }
244
245 return false;
246}
247
Xiao Guangrong37860632012-11-15 14:17:01 +0800248static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600249 struct event_key *key,
250 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800251{
252 scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
253 key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
254}
255
256static struct kvm_events_ops mmio_events = {
257 .is_begin_event = mmio_event_begin,
258 .is_end_event = mmio_event_end,
259 .decode_key = mmio_event_decode_key,
260 .name = "MMIO Access"
261};
262
263 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300264static void ioport_event_get_key(struct perf_evsel *evsel,
265 struct perf_sample *sample,
266 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800267{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300268 key->key = perf_evsel__intval(evsel, sample, "port");
269 key->info = perf_evsel__intval(evsel, sample, "rw");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800270}
271
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300272static bool ioport_event_begin(struct perf_evsel *evsel,
273 struct perf_sample *sample,
274 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800275{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300276 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
277 ioport_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800278 return true;
279 }
280
281 return false;
282}
283
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300284static bool ioport_event_end(struct perf_evsel *evsel,
285 struct perf_sample *sample __maybe_unused,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800286 struct event_key *key __maybe_unused)
287{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300288 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800289}
290
Xiao Guangrong37860632012-11-15 14:17:01 +0800291static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
David Ahernde332ac2012-10-02 22:09:53 -0600292 struct event_key *key,
293 char decode[20])
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800294{
295 scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
296 key->info ? "POUT" : "PIN");
297}
298
299static struct kvm_events_ops ioport_events = {
300 .is_begin_event = ioport_event_begin,
301 .is_end_event = ioport_event_end,
302 .decode_key = ioport_event_decode_key,
303 .name = "IO Port Access"
304};
305
Xiao Guangrong37860632012-11-15 14:17:01 +0800306static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800307{
308 bool ret = true;
309
David Ahernde332ac2012-10-02 22:09:53 -0600310 if (!strcmp(kvm->report_event, "vmexit"))
311 kvm->events_ops = &exit_events;
312 else if (!strcmp(kvm->report_event, "mmio"))
313 kvm->events_ops = &mmio_events;
314 else if (!strcmp(kvm->report_event, "ioport"))
315 kvm->events_ops = &ioport_events;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800316 else {
David Ahernde332ac2012-10-02 22:09:53 -0600317 pr_err("Unknown report event:%s\n", kvm->report_event);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800318 ret = false;
319 }
320
321 return ret;
322}
323
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800324struct vcpu_event_record {
325 int vcpu_id;
326 u64 start_time;
327 struct kvm_event *last_event;
328};
329
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800330
Xiao Guangrong37860632012-11-15 14:17:01 +0800331static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800332{
David Ahernb880dee2012-10-08 11:17:30 -0600333 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800334
David Ahernb880dee2012-10-08 11:17:30 -0600335 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600336 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800337}
338
David Ahern1afe1d12013-08-05 21:41:34 -0400339static void clear_events_cache_stats(struct list_head *kvm_events_cache)
340{
341 struct list_head *head;
342 struct kvm_event *event;
343 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400344 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400345
346 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
347 head = &kvm_events_cache[i];
348 list_for_each_entry(event, head, hash_entry) {
349 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400350 event->total.time = 0;
351 init_stats(&event->total.stats);
352
353 for (j = 0; j < event->max_vcpu; ++j) {
354 event->vcpu[j].time = 0;
355 init_stats(&event->vcpu[j].stats);
356 }
David Ahern1afe1d12013-08-05 21:41:34 -0400357 }
358 }
359}
360
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800361static int kvm_events_hash_fn(u64 key)
362{
363 return key & (EVENTS_CACHE_SIZE - 1);
364}
365
366static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
367{
368 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600369 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800370
371 if (vcpu_id < event->max_vcpu)
372 return true;
373
374 while (event->max_vcpu <= vcpu_id)
375 event->max_vcpu += DEFAULT_VCPU_NUM;
376
David Ahern6ca5f302013-05-25 18:24:46 -0600377 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800378 event->vcpu = realloc(event->vcpu,
379 event->max_vcpu * sizeof(*event->vcpu));
380 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600381 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800382 pr_err("Not enough memory\n");
383 return false;
384 }
385
386 memset(event->vcpu + old_max_vcpu, 0,
387 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
388 return true;
389}
390
391static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
392{
393 struct kvm_event *event;
394
395 event = zalloc(sizeof(*event));
396 if (!event) {
397 pr_err("Not enough memory\n");
398 return NULL;
399 }
400
401 event->key = *key;
402 return event;
403}
404
Xiao Guangrong37860632012-11-15 14:17:01 +0800405static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600406 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800407{
408 struct kvm_event *event;
409 struct list_head *head;
410
411 BUG_ON(key->key == INVALID_KEY);
412
David Ahernde332ac2012-10-02 22:09:53 -0600413 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600414 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800415 if (event->key.key == key->key && event->key.info == key->info)
416 return event;
David Ahern355afe82012-10-08 11:17:32 -0600417 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800418
419 event = kvm_alloc_init_event(key);
420 if (!event)
421 return NULL;
422
423 list_add(&event->hash_entry, head);
424 return event;
425}
426
Xiao Guangrong37860632012-11-15 14:17:01 +0800427static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600428 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800429 struct event_key *key, u64 timestamp)
430{
431 struct kvm_event *event = NULL;
432
433 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600434 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800435
436 vcpu_record->last_event = event;
437 vcpu_record->start_time = timestamp;
438 return true;
439}
440
441static void
442kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
443{
444 kvm_stats->time += time_diff;
445 update_stats(&kvm_stats->stats, time_diff);
446}
447
448static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
449{
450 struct kvm_event_stats *kvm_stats = &event->total;
451
452 if (vcpu_id != -1)
453 kvm_stats = &event->vcpu[vcpu_id];
454
455 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
456 avg_stats(&kvm_stats->stats));
457}
458
459static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
460 u64 time_diff)
461{
David Ahern2aa8eab2012-10-08 11:17:35 -0600462 if (vcpu_id == -1) {
463 kvm_update_event_stats(&event->total, time_diff);
464 return true;
465 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800466
467 if (!kvm_event_expand(event, vcpu_id))
468 return false;
469
470 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
471 return true;
472}
473
Xiao Guangrong37860632012-11-15 14:17:01 +0800474static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600475 struct vcpu_event_record *vcpu_record,
476 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400477 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800478{
479 struct kvm_event *event;
480 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600481 int vcpu;
482
483 if (kvm->trace_vcpu == -1)
484 vcpu = -1;
485 else
486 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800487
488 event = vcpu_record->last_event;
489 time_begin = vcpu_record->start_time;
490
491 /* The begin event is not caught. */
492 if (!time_begin)
493 return true;
494
495 /*
496 * In some case, the 'begin event' only records the start timestamp,
497 * the actual event is recognized in the 'end event' (e.g. mmio-event).
498 */
499
500 /* Both begin and end events did not get the key. */
501 if (!event && key->key == INVALID_KEY)
502 return true;
503
504 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600505 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800506
507 if (!event)
508 return false;
509
510 vcpu_record->last_event = NULL;
511 vcpu_record->start_time = 0;
512
David Ahern1afe1d12013-08-05 21:41:34 -0400513 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400514 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400515 pr_debug("End time before begin time; skipping event.\n");
516 return true;
517 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800518
David Ahern70f7b4a2013-08-07 21:56:38 -0400519 time_diff = sample->time - time_begin;
520
521 if (kvm->duration && time_diff > kvm->duration) {
522 char decode[32];
523
524 kvm->events_ops->decode_key(kvm, &event->key, decode);
525 if (strcmp(decode, "HLT")) {
526 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
527 sample->time, sample->pid, vcpu_record->vcpu_id,
528 decode, time_diff/1000);
529 }
530 }
531
David Ahern2aa8eab2012-10-08 11:17:35 -0600532 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800533}
534
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300535static
536struct vcpu_event_record *per_vcpu_record(struct thread *thread,
537 struct perf_evsel *evsel,
538 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800539{
540 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300541 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800542 struct vcpu_event_record *vcpu_record;
543
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300544 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800545 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300546 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800547 return NULL;
548 }
549
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300550 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800551 thread->priv = vcpu_record;
552 }
553
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300554 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800555}
556
Xiao Guangrong37860632012-11-15 14:17:01 +0800557static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600558 struct thread *thread,
559 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300560 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800561{
562 struct vcpu_event_record *vcpu_record;
563 struct event_key key = {.key = INVALID_KEY};
564
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300565 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800566 if (!vcpu_record)
567 return true;
568
David Ahern2aa8eab2012-10-08 11:17:35 -0600569 /* only process events for vcpus user cares about */
570 if ((kvm->trace_vcpu != -1) &&
571 (kvm->trace_vcpu != vcpu_record->vcpu_id))
572 return true;
573
David Ahernde332ac2012-10-02 22:09:53 -0600574 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
575 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800576
David Ahernde332ac2012-10-02 22:09:53 -0600577 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400578 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800579
580 return true;
581}
582
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800583#define GET_EVENT_KEY(func, field) \
584static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
585{ \
586 if (vcpu == -1) \
587 return event->total.field; \
588 \
589 if (vcpu >= event->max_vcpu) \
590 return 0; \
591 \
592 return event->vcpu[vcpu].field; \
593}
594
595#define COMPARE_EVENT_KEY(func, field) \
596GET_EVENT_KEY(func, field) \
597static int compare_kvm_event_ ## func(struct kvm_event *one, \
598 struct kvm_event *two, int vcpu)\
599{ \
600 return get_event_ ##func(one, vcpu) > \
601 get_event_ ##func(two, vcpu); \
602}
603
604GET_EVENT_KEY(time, time);
605COMPARE_EVENT_KEY(count, stats.n);
606COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400607GET_EVENT_KEY(max, stats.max);
608GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800609
610#define DEF_SORT_NAME_KEY(name, compare_key) \
611 { #name, compare_kvm_event_ ## compare_key }
612
613static struct kvm_event_key keys[] = {
614 DEF_SORT_NAME_KEY(sample, count),
615 DEF_SORT_NAME_KEY(time, mean),
616 { NULL, NULL }
617};
618
Xiao Guangrong37860632012-11-15 14:17:01 +0800619static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800620{
621 int i;
622
623 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600624 if (!strcmp(keys[i].name, kvm->sort_key)) {
625 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800626 return true;
627 }
628 }
629
David Ahernde332ac2012-10-02 22:09:53 -0600630 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800631 return false;
632}
633
David Ahernde332ac2012-10-02 22:09:53 -0600634static void insert_to_result(struct rb_root *result, struct kvm_event *event,
635 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800636{
David Ahernde332ac2012-10-02 22:09:53 -0600637 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800638 struct rb_node *parent = NULL;
639 struct kvm_event *p;
640
641 while (*rb) {
642 p = container_of(*rb, struct kvm_event, rb);
643 parent = *rb;
644
645 if (bigger(event, p, vcpu))
646 rb = &(*rb)->rb_left;
647 else
648 rb = &(*rb)->rb_right;
649 }
650
651 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600652 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800653}
654
Xiao Guangrong37860632012-11-15 14:17:01 +0800655static void
656update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800657{
David Ahernde332ac2012-10-02 22:09:53 -0600658 int vcpu = kvm->trace_vcpu;
659
660 kvm->total_count += get_event_count(event, vcpu);
661 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800662}
663
664static bool event_is_valid(struct kvm_event *event, int vcpu)
665{
666 return !!get_event_count(event, vcpu);
667}
668
Xiao Guangrong37860632012-11-15 14:17:01 +0800669static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800670{
671 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600672 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800673 struct kvm_event *event;
674
David Ahern355afe82012-10-08 11:17:32 -0600675 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
676 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800677 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600678 update_total_count(kvm, event);
679 insert_to_result(&kvm->result, event,
680 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800681 }
David Ahern355afe82012-10-08 11:17:32 -0600682 }
683 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800684}
685
686/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600687static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800688{
David Ahernde332ac2012-10-02 22:09:53 -0600689 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800690
691 if (!node)
692 return NULL;
693
David Ahernde332ac2012-10-02 22:09:53 -0600694 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800695 return container_of(node, struct kvm_event, rb);
696}
697
David Ahern1afe1d12013-08-05 21:41:34 -0400698static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800699{
David Ahern1afe1d12013-08-05 21:41:34 -0400700 int vcpu = kvm->trace_vcpu;
701
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800702 pr_info("Analyze events for ");
703
David Ahern1afe1d12013-08-05 21:41:34 -0400704 if (kvm->live) {
705 if (kvm->opts.target.system_wide)
706 pr_info("all VMs, ");
707 else if (kvm->opts.target.pid)
708 pr_info("pid(s) %s, ", kvm->opts.target.pid);
709 else
710 pr_info("dazed and confused on what is monitored, ");
711 }
712
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800713 if (vcpu == -1)
714 pr_info("all VCPUs:\n\n");
715 else
716 pr_info("VCPU %d:\n\n", vcpu);
717}
718
David Ahern1afe1d12013-08-05 21:41:34 -0400719static void show_timeofday(void)
720{
721 char date[64];
722 struct timeval tv;
723 struct tm ltime;
724
725 gettimeofday(&tv, NULL);
726 if (localtime_r(&tv.tv_sec, &ltime)) {
727 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
728 pr_info("%s.%06ld", date, tv.tv_usec);
729 } else
730 pr_info("00:00:00.000000");
731
732 return;
733}
734
Xiao Guangrong37860632012-11-15 14:17:01 +0800735static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800736{
737 char decode[20];
738 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600739 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800740
David Ahern1afe1d12013-08-05 21:41:34 -0400741 if (kvm->live) {
742 puts(CONSOLE_CLEAR);
743 show_timeofday();
744 }
745
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800746 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400747 print_vcpu_info(kvm);
David Ahernde332ac2012-10-02 22:09:53 -0600748 pr_info("%20s ", kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800749 pr_info("%10s ", "Samples");
750 pr_info("%9s ", "Samples%");
751
752 pr_info("%9s ", "Time%");
David Ahern62d04db2013-08-05 21:41:35 -0400753 pr_info("%10s ", "Min Time");
754 pr_info("%10s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800755 pr_info("%16s ", "Avg time");
756 pr_info("\n\n");
757
David Ahernde332ac2012-10-02 22:09:53 -0600758 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400759 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800760
761 ecount = get_event_count(event, vcpu);
762 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400763 max = get_event_max(event, vcpu);
764 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800765
David Ahernde332ac2012-10-02 22:09:53 -0600766 kvm->events_ops->decode_key(kvm, &event->key, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800767 pr_info("%20s ", decode);
768 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600769 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
770 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
David Ahern62d04db2013-08-05 21:41:35 -0400771 pr_info("%8" PRIu64 "us ", min / 1000);
772 pr_info("%8" PRIu64 "us ", max / 1000);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800773 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
774 kvm_event_rel_stddev(vcpu, event));
775 pr_info("\n");
776 }
777
David Aherne4f76372012-10-08 11:17:34 -0600778 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
779 kvm->total_count, kvm->total_time / 1e3);
David Ahern1afe1d12013-08-05 21:41:34 -0400780
781 if (kvm->lost_events)
782 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
783}
784
785static int process_lost_event(struct perf_tool *tool,
786 union perf_event *event __maybe_unused,
787 struct perf_sample *sample __maybe_unused,
788 struct machine *machine __maybe_unused)
789{
790 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
791
792 kvm->lost_events++;
793 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800794}
795
David Ahern2e73f002013-08-05 21:41:37 -0400796static bool skip_sample(struct perf_kvm_stat *kvm,
797 struct perf_sample *sample)
798{
799 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
800 return true;
801
802 return false;
803}
804
David Ahernde332ac2012-10-02 22:09:53 -0600805static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800806 union perf_event *event,
807 struct perf_sample *sample,
808 struct perf_evsel *evsel,
809 struct machine *machine)
810{
David Ahern2e73f002013-08-05 21:41:37 -0400811 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800812 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
813 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800814
David Ahern2e73f002013-08-05 21:41:37 -0400815 if (skip_sample(kvm, sample))
816 return 0;
817
Adrian Hunter314add62013-08-27 11:23:03 +0300818 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800819 if (thread == NULL) {
820 pr_debug("problem processing %d event, skipping it.\n",
821 event->header.type);
822 return -1;
823 }
824
David Ahernde332ac2012-10-02 22:09:53 -0600825 if (!handle_kvm_event(kvm, thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800826 return -1;
827
828 return 0;
829}
830
David Ahern1afe1d12013-08-05 21:41:34 -0400831static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800832{
David Ahern1afe1d12013-08-05 21:41:34 -0400833 char buf[64], *cpuid;
834 int err, isa;
835
836 if (kvm->live) {
837 err = get_cpuid(buf, sizeof(buf));
838 if (err != 0) {
839 pr_err("Failed to look up CPU type (Intel or AMD)\n");
840 return err;
841 }
842 cpuid = buf;
843 } else
844 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800845
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800846 if (strstr(cpuid, "Intel"))
847 isa = 1;
848 else if (strstr(cpuid, "AMD"))
849 isa = 0;
850 else {
851 pr_err("CPU %s is not supported.\n", cpuid);
David Ahern1afe1d12013-08-05 21:41:34 -0400852 return -ENOTSUP;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800853 }
854
David Ahern1afe1d12013-08-05 21:41:34 -0400855 if (isa == 1) {
856 kvm->exit_reasons = vmx_exit_reasons;
857 kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
858 kvm->exit_reasons_isa = "VMX";
859 }
860
861 return 0;
862}
863
864static bool verify_vcpu(int vcpu)
865{
866 if (vcpu != -1 && vcpu < 0) {
867 pr_err("Invalid vcpu:%d.\n", vcpu);
868 return false;
869 }
870
871 return true;
872}
873
874/* keeping the max events to a modest level to keep
875 * the processing of samples per mmap smooth.
876 */
877#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
878
879static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
880 u64 *mmap_time)
881{
882 union perf_event *event;
883 struct perf_sample sample;
884 s64 n = 0;
885 int err;
886
887 *mmap_time = ULLONG_MAX;
888 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
889 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
890 if (err) {
891 pr_err("Failed to parse sample\n");
892 return -1;
893 }
894
895 err = perf_session_queue_event(kvm->session, event, &sample, 0);
896 if (err) {
897 pr_err("Failed to enqueue sample: %d\n", err);
898 return -1;
899 }
900
901 /* save time stamp of our first sample for this mmap */
902 if (n == 0)
903 *mmap_time = sample.time;
904
905 /* limit events per mmap handled all at once */
906 n++;
907 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
908 break;
909 }
910
911 return n;
912}
913
914static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
915{
916 int i, err, throttled = 0;
917 s64 n, ntotal = 0;
918 u64 flush_time = ULLONG_MAX, mmap_time;
919
920 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
921 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
922 if (n < 0)
923 return -1;
924
925 /* flush time is going to be the minimum of all the individual
926 * mmap times. Essentially, we flush all the samples queued up
927 * from the last pass under our minimal start time -- that leaves
928 * a very small race for samples to come in with a lower timestamp.
929 * The ioctl to return the perf_clock timestamp should close the
930 * race entirely.
931 */
932 if (mmap_time < flush_time)
933 flush_time = mmap_time;
934
935 ntotal += n;
936 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
937 throttled = 1;
938 }
939
940 /* flush queue after each round in which we processed events */
941 if (ntotal) {
942 kvm->session->ordered_samples.next_flush = flush_time;
943 err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
944 if (err) {
945 if (kvm->lost_events)
946 pr_info("\nLost events: %" PRIu64 "\n\n",
947 kvm->lost_events);
948 return err;
949 }
950 }
951
952 return throttled;
953}
954
955static volatile int done;
956
957static void sig_handler(int sig __maybe_unused)
958{
959 done = 1;
960}
961
962static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
963{
964 struct itimerspec new_value;
965 int rc = -1;
966
967 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
968 if (kvm->timerfd < 0) {
969 pr_err("timerfd_create failed\n");
970 goto out;
971 }
972
973 new_value.it_value.tv_sec = kvm->display_time;
974 new_value.it_value.tv_nsec = 0;
975 new_value.it_interval.tv_sec = kvm->display_time;
976 new_value.it_interval.tv_nsec = 0;
977
978 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
979 pr_err("timerfd_settime failed: %d\n", errno);
980 close(kvm->timerfd);
981 goto out;
982 }
983
984 rc = 0;
985out:
986 return rc;
987}
988
989static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
990{
991 uint64_t c;
992 int rc;
993
994 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
995 if (rc < 0) {
996 if (errno == EAGAIN)
997 return 0;
998
999 pr_err("Failed to read timer fd: %d\n", errno);
1000 return -1;
1001 }
1002
1003 if (rc != sizeof(uint64_t)) {
1004 pr_err("Error reading timer fd - invalid size returned\n");
1005 return -1;
1006 }
1007
1008 if (c != 1)
1009 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
1010
1011 /* update display */
1012 sort_result(kvm);
1013 print_result(kvm);
1014
1015 /* reset counts */
1016 clear_events_cache_stats(kvm->kvm_events_cache);
1017 kvm->total_count = 0;
1018 kvm->total_time = 0;
1019 kvm->lost_events = 0;
1020
1021 return 0;
1022}
1023
1024static int fd_set_nonblock(int fd)
1025{
1026 long arg = 0;
1027
1028 arg = fcntl(fd, F_GETFL);
1029 if (arg < 0) {
1030 pr_err("Failed to get current flags for fd %d\n", fd);
1031 return -1;
1032 }
1033
1034 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
1035 pr_err("Failed to set non-block option on fd %d\n", fd);
1036 return -1;
1037 }
1038
1039 return 0;
1040}
1041
1042static
1043int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save)
1044{
1045 int c;
1046
1047 tcsetattr(0, TCSANOW, tc_now);
1048 c = getc(stdin);
1049 tcsetattr(0, TCSAFLUSH, tc_save);
1050
1051 if (c == 'q')
1052 return 1;
1053
1054 return 0;
1055}
1056
1057static int kvm_events_live_report(struct perf_kvm_stat *kvm)
1058{
1059 struct pollfd *pollfds = NULL;
1060 int nr_fds, nr_stdin, ret, err = -EINVAL;
1061 struct termios tc, save;
1062
1063 /* live flag must be set first */
1064 kvm->live = true;
1065
1066 ret = cpu_isa_config(kvm);
1067 if (ret < 0)
1068 return ret;
1069
1070 if (!verify_vcpu(kvm->trace_vcpu) ||
1071 !select_key(kvm) ||
1072 !register_kvm_events_ops(kvm)) {
1073 goto out;
1074 }
1075
1076 init_kvm_event_record(kvm);
1077
1078 tcgetattr(0, &save);
1079 tc = save;
1080 tc.c_lflag &= ~(ICANON | ECHO);
1081 tc.c_cc[VMIN] = 0;
1082 tc.c_cc[VTIME] = 0;
1083
1084 signal(SIGINT, sig_handler);
1085 signal(SIGTERM, sig_handler);
1086
1087 /* copy pollfds -- need to add timerfd and stdin */
1088 nr_fds = kvm->evlist->nr_fds;
1089 pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2));
1090 if (!pollfds) {
1091 err = -ENOMEM;
1092 goto out;
1093 }
1094 memcpy(pollfds, kvm->evlist->pollfd,
1095 sizeof(struct pollfd) * kvm->evlist->nr_fds);
1096
1097 /* add timer fd */
1098 if (perf_kvm__timerfd_create(kvm) < 0) {
1099 err = -1;
1100 goto out;
1101 }
1102
1103 pollfds[nr_fds].fd = kvm->timerfd;
1104 pollfds[nr_fds].events = POLLIN;
1105 nr_fds++;
1106
1107 pollfds[nr_fds].fd = fileno(stdin);
1108 pollfds[nr_fds].events = POLLIN;
1109 nr_stdin = nr_fds;
1110 nr_fds++;
1111 if (fd_set_nonblock(fileno(stdin)) != 0)
1112 goto out;
1113
1114 /* everything is good - enable the events and process */
1115 perf_evlist__enable(kvm->evlist);
1116
1117 while (!done) {
1118 int rc;
1119
1120 rc = perf_kvm__mmap_read(kvm);
1121 if (rc < 0)
1122 break;
1123
1124 err = perf_kvm__handle_timerfd(kvm);
1125 if (err)
1126 goto out;
1127
1128 if (pollfds[nr_stdin].revents & POLLIN)
1129 done = perf_kvm__handle_stdin(&tc, &save);
1130
1131 if (!rc && !done)
1132 err = poll(pollfds, nr_fds, 100);
1133 }
1134
1135 perf_evlist__disable(kvm->evlist);
1136
1137 if (err == 0) {
1138 sort_result(kvm);
1139 print_result(kvm);
1140 }
1141
1142out:
1143 if (kvm->timerfd >= 0)
1144 close(kvm->timerfd);
1145
1146 if (pollfds)
1147 free(pollfds);
1148
1149 return err;
1150}
1151
1152static int kvm_live_open_events(struct perf_kvm_stat *kvm)
1153{
1154 int err, rc = -1;
1155 struct perf_evsel *pos;
1156 struct perf_evlist *evlist = kvm->evlist;
1157
1158 perf_evlist__config(evlist, &kvm->opts);
1159
1160 /*
1161 * Note: exclude_{guest,host} do not apply here.
1162 * This command processes KVM tracepoints from host only
1163 */
1164 list_for_each_entry(pos, &evlist->entries, node) {
1165 struct perf_event_attr *attr = &pos->attr;
1166
1167 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +03001168 perf_evsel__set_sample_bit(pos, TID);
1169 perf_evsel__set_sample_bit(pos, TIME);
1170 perf_evsel__set_sample_bit(pos, CPU);
1171 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001172 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001173 perf_evsel__reset_sample_bit(pos, PERIOD);
1174 perf_evsel__reset_sample_bit(pos, IP);
1175 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1176 perf_evsel__reset_sample_bit(pos, ADDR);
1177 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001178 attr->mmap = 0;
1179 attr->comm = 0;
1180 attr->task = 0;
1181
1182 attr->sample_period = 1;
1183
1184 attr->watermark = 0;
1185 attr->wakeup_events = 1000;
1186
1187 /* will enable all once we are ready */
1188 attr->disabled = 1;
1189 }
1190
1191 err = perf_evlist__open(evlist);
1192 if (err < 0) {
1193 printf("Couldn't create the events: %s\n", strerror(errno));
1194 goto out;
1195 }
1196
1197 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
1198 ui__error("Failed to mmap the events: %s\n", strerror(errno));
1199 perf_evlist__close(evlist);
1200 goto out;
1201 }
1202
1203 rc = 0;
1204
1205out:
1206 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001207}
1208
Xiao Guangrong37860632012-11-15 14:17:01 +08001209static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001210{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001211 int ret;
1212
David Ahernde332ac2012-10-02 22:09:53 -06001213 struct perf_tool eops = {
1214 .sample = process_sample_event,
1215 .comm = perf_event__process_comm,
1216 .ordered_samples = true,
1217 };
1218
1219 kvm->tool = eops;
1220 kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false,
1221 &kvm->tool);
1222 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001223 pr_err("Initializing perf session failed\n");
1224 return -EINVAL;
1225 }
1226
David Ahernde332ac2012-10-02 22:09:53 -06001227 if (!perf_session__has_traces(kvm->session, "kvm record"))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001228 return -EINVAL;
1229
1230 /*
1231 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1232 * traced in the old kernel.
1233 */
David Ahern1afe1d12013-08-05 21:41:34 -04001234 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001235 if (ret < 0)
1236 return ret;
1237
David Ahernde332ac2012-10-02 22:09:53 -06001238 return perf_session__process_events(kvm->session, &kvm->tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001239}
1240
David Ahern2e73f002013-08-05 21:41:37 -04001241static int parse_target_str(struct perf_kvm_stat *kvm)
1242{
1243 if (kvm->pid_str) {
1244 kvm->pid_list = intlist__new(kvm->pid_str);
1245 if (kvm->pid_list == NULL) {
1246 pr_err("Error parsing process id string\n");
1247 return -EINVAL;
1248 }
1249 }
1250
1251 return 0;
1252}
1253
Xiao Guangrong37860632012-11-15 14:17:01 +08001254static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001255{
1256 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001257 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001258
David Ahern2e73f002013-08-05 21:41:37 -04001259 if (parse_target_str(kvm) != 0)
1260 goto exit;
1261
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001262 if (!verify_vcpu(vcpu))
1263 goto exit;
1264
David Ahernde332ac2012-10-02 22:09:53 -06001265 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001266 goto exit;
1267
David Ahernde332ac2012-10-02 22:09:53 -06001268 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001269 goto exit;
1270
David Ahernde332ac2012-10-02 22:09:53 -06001271 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001272 setup_pager();
1273
David Ahernde332ac2012-10-02 22:09:53 -06001274 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001275 if (ret)
1276 goto exit;
1277
David Ahernde332ac2012-10-02 22:09:53 -06001278 sort_result(kvm);
1279 print_result(kvm);
1280
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001281exit:
1282 return ret;
1283}
1284
David Ahern8fdd84c2013-08-02 14:05:42 -06001285static const char * const kvm_events_tp[] = {
1286 "kvm:kvm_entry",
1287 "kvm:kvm_exit",
1288 "kvm:kvm_mmio",
1289 "kvm:kvm_pio",
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001290};
1291
1292#define STRDUP_FAIL_EXIT(s) \
1293 ({ char *_p; \
1294 _p = strdup(s); \
1295 if (!_p) \
1296 return -ENOMEM; \
1297 _p; \
1298 })
1299
Xiao Guangrong37860632012-11-15 14:17:01 +08001300static int
1301kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001302{
1303 unsigned int rec_argc, i, j;
1304 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001305 const char * const record_args[] = {
1306 "record",
1307 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001308 "-m", "1024",
1309 "-c", "1",
1310 };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001311
David Ahern8fdd84c2013-08-02 14:05:42 -06001312 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
1313 2 * ARRAY_SIZE(kvm_events_tp);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001314 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1315
1316 if (rec_argv == NULL)
1317 return -ENOMEM;
1318
1319 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1320 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1321
David Ahern8fdd84c2013-08-02 14:05:42 -06001322 for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) {
1323 rec_argv[i++] = "-e";
1324 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1325 }
1326
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001327 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001328 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001329
1330 for (j = 1; j < (unsigned int)argc; j++, i++)
1331 rec_argv[i] = argv[j];
1332
1333 return cmd_record(i, rec_argv, NULL);
1334}
1335
Xiao Guangrong37860632012-11-15 14:17:01 +08001336static int
1337kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001338{
David Ahernde332ac2012-10-02 22:09:53 -06001339 const struct option kvm_events_report_options[] = {
1340 OPT_STRING(0, "event", &kvm->report_event, "report event",
1341 "event for reporting: vmexit, mmio, ioport"),
1342 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1343 "vcpu id to report"),
1344 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1345 "key for sorting: sample(sort by samples number)"
1346 " time (sort by avg time)"),
David Ahern2e73f002013-08-05 21:41:37 -04001347 OPT_STRING('p', "pid", &kvm->pid_str, "pid",
1348 "analyze events only for given process id(s)"),
David Ahernde332ac2012-10-02 22:09:53 -06001349 OPT_END()
1350 };
1351
1352 const char * const kvm_events_report_usage[] = {
1353 "perf kvm stat report [<options>]",
1354 NULL
1355 };
1356
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001357 symbol__init();
1358
1359 if (argc) {
1360 argc = parse_options(argc, argv,
1361 kvm_events_report_options,
1362 kvm_events_report_usage, 0);
1363 if (argc)
1364 usage_with_options(kvm_events_report_usage,
1365 kvm_events_report_options);
1366 }
1367
David Ahernde332ac2012-10-02 22:09:53 -06001368 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001369}
1370
David Ahern1afe1d12013-08-05 21:41:34 -04001371static struct perf_evlist *kvm_live_event_list(void)
1372{
1373 struct perf_evlist *evlist;
1374 char *tp, *name, *sys;
1375 unsigned int j;
1376 int err = -1;
1377
1378 evlist = perf_evlist__new();
1379 if (evlist == NULL)
1380 return NULL;
1381
1382 for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) {
1383
1384 tp = strdup(kvm_events_tp[j]);
1385 if (tp == NULL)
1386 goto out;
1387
1388 /* split tracepoint into subsystem and name */
1389 sys = tp;
1390 name = strchr(tp, ':');
1391 if (name == NULL) {
1392 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
1393 kvm_events_tp[j]);
1394 free(tp);
1395 goto out;
1396 }
1397 *name = '\0';
1398 name++;
1399
1400 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
1401 pr_err("Failed to add %s tracepoint to the list\n", kvm_events_tp[j]);
1402 free(tp);
1403 goto out;
1404 }
1405
1406 free(tp);
1407 }
1408
1409 err = 0;
1410
1411out:
1412 if (err) {
1413 perf_evlist__delete(evlist);
1414 evlist = NULL;
1415 }
1416
1417 return evlist;
1418}
1419
1420static int kvm_events_live(struct perf_kvm_stat *kvm,
1421 int argc, const char **argv)
1422{
1423 char errbuf[BUFSIZ];
1424 int err;
1425
1426 const struct option live_options[] = {
1427 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1428 "record events on existing process id"),
1429 OPT_UINTEGER('m', "mmap-pages", &kvm->opts.mmap_pages,
1430 "number of mmap data pages"),
1431 OPT_INCR('v', "verbose", &verbose,
1432 "be more verbose (show counter open errors, etc)"),
1433 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1434 "system-wide collection from all CPUs"),
1435 OPT_UINTEGER('d', "display", &kvm->display_time,
1436 "time in seconds between display updates"),
1437 OPT_STRING(0, "event", &kvm->report_event, "report event",
1438 "event for reporting: vmexit, mmio, ioport"),
1439 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1440 "vcpu id to report"),
1441 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1442 "key for sorting: sample(sort by samples number)"
1443 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001444 OPT_U64(0, "duration", &kvm->duration,
1445 "show events other than HALT that take longer than duration usecs"),
David Ahern1afe1d12013-08-05 21:41:34 -04001446 OPT_END()
1447 };
1448 const char * const live_usage[] = {
1449 "perf kvm stat live [<options>]",
1450 NULL
1451 };
1452
1453
1454 /* event handling */
1455 kvm->tool.sample = process_sample_event;
1456 kvm->tool.comm = perf_event__process_comm;
1457 kvm->tool.exit = perf_event__process_exit;
1458 kvm->tool.fork = perf_event__process_fork;
1459 kvm->tool.lost = process_lost_event;
1460 kvm->tool.ordered_samples = true;
1461 perf_tool__fill_defaults(&kvm->tool);
1462
1463 /* set defaults */
1464 kvm->display_time = 1;
1465 kvm->opts.user_interval = 1;
1466 kvm->opts.mmap_pages = 512;
1467 kvm->opts.target.uses_mmap = false;
1468 kvm->opts.target.uid_str = NULL;
1469 kvm->opts.target.uid = UINT_MAX;
1470
1471 symbol__init();
1472 disable_buildid_cache();
1473
1474 use_browser = 0;
1475 setup_browser(false);
1476
1477 if (argc) {
1478 argc = parse_options(argc, argv, live_options,
1479 live_usage, 0);
1480 if (argc)
1481 usage_with_options(live_usage, live_options);
1482 }
1483
David Ahern70f7b4a2013-08-07 21:56:38 -04001484 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1485
David Ahern1afe1d12013-08-05 21:41:34 -04001486 /*
1487 * target related setups
1488 */
1489 err = perf_target__validate(&kvm->opts.target);
1490 if (err) {
1491 perf_target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
1492 ui__warning("%s", errbuf);
1493 }
1494
1495 if (perf_target__none(&kvm->opts.target))
1496 kvm->opts.target.system_wide = true;
1497
1498
1499 /*
1500 * generate the event list
1501 */
1502 kvm->evlist = kvm_live_event_list();
1503 if (kvm->evlist == NULL) {
1504 err = -1;
1505 goto out;
1506 }
1507
1508 symbol_conf.nr_events = kvm->evlist->nr_entries;
1509
1510 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1511 usage_with_options(live_usage, live_options);
1512
1513 /*
1514 * perf session
1515 */
1516 kvm->session = perf_session__new(NULL, O_WRONLY, false, false, &kvm->tool);
1517 if (kvm->session == NULL) {
1518 err = -ENOMEM;
1519 goto out;
1520 }
1521 kvm->session->evlist = kvm->evlist;
1522 perf_session__set_id_hdr_size(kvm->session);
1523
1524
1525 if (perf_target__has_task(&kvm->opts.target))
1526 perf_event__synthesize_thread_map(&kvm->tool,
1527 kvm->evlist->threads,
1528 perf_event__process,
1529 &kvm->session->machines.host);
1530 else
1531 perf_event__synthesize_threads(&kvm->tool, perf_event__process,
1532 &kvm->session->machines.host);
1533
1534
1535 err = kvm_live_open_events(kvm);
1536 if (err)
1537 goto out;
1538
1539 err = kvm_events_live_report(kvm);
1540
1541out:
1542 exit_browser(0);
1543
1544 if (kvm->session)
1545 perf_session__delete(kvm->session);
1546 kvm->session = NULL;
1547 if (kvm->evlist) {
1548 perf_evlist__delete_maps(kvm->evlist);
1549 perf_evlist__delete(kvm->evlist);
1550 }
1551
1552 return err;
1553}
1554
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001555static void print_kvm_stat_usage(void)
1556{
1557 printf("Usage: perf kvm stat <command>\n\n");
1558
1559 printf("# Available commands:\n");
1560 printf("\trecord: record kvm events\n");
1561 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001562 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001563
1564 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1565}
1566
Xiao Guangrong37860632012-11-15 14:17:01 +08001567static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001568{
Xiao Guangrong37860632012-11-15 14:17:01 +08001569 struct perf_kvm_stat kvm = {
1570 .file_name = file_name,
1571
1572 .trace_vcpu = -1,
1573 .report_event = "vmexit",
1574 .sort_key = "sample",
1575
1576 .exit_reasons = svm_exit_reasons,
1577 .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
1578 .exit_reasons_isa = "SVM",
1579 };
1580
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001581 if (argc == 1) {
1582 print_kvm_stat_usage();
1583 goto perf_stat;
1584 }
1585
1586 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001587 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001588
1589 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001590 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001591
David Ahern1afe1d12013-08-05 21:41:34 -04001592 if (!strncmp(argv[1], "live", 4))
1593 return kvm_events_live(&kvm, argc - 1 , argv + 1);
1594
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001595perf_stat:
1596 return cmd_stat(argc, argv, NULL);
1597}
Xiao Guangrong73210902012-11-19 16:19:21 +08001598#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001599
Xiao Guangrong37860632012-11-15 14:17:01 +08001600static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001601{
1602 int rec_argc, i = 0, j;
1603 const char **rec_argv;
1604
1605 rec_argc = argc + 2;
1606 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1607 rec_argv[i++] = strdup("record");
1608 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001609 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001610 for (j = 1; j < argc; j++, i++)
1611 rec_argv[i] = argv[j];
1612
1613 BUG_ON(i != rec_argc);
1614
1615 return cmd_record(i, rec_argv, NULL);
1616}
1617
Xiao Guangrong37860632012-11-15 14:17:01 +08001618static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001619{
1620 int rec_argc, i = 0, j;
1621 const char **rec_argv;
1622
1623 rec_argc = argc + 2;
1624 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1625 rec_argv[i++] = strdup("report");
1626 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001627 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001628 for (j = 1; j < argc; j++, i++)
1629 rec_argv[i] = argv[j];
1630
1631 BUG_ON(i != rec_argc);
1632
1633 return cmd_report(i, rec_argv, NULL);
1634}
1635
Xiao Guangrong37860632012-11-15 14:17:01 +08001636static int
1637__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001638{
1639 int rec_argc, i = 0, j;
1640 const char **rec_argv;
1641
1642 rec_argc = argc + 2;
1643 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1644 rec_argv[i++] = strdup("buildid-list");
1645 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001646 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001647 for (j = 1; j < argc; j++, i++)
1648 rec_argv[i] = argv[j];
1649
1650 BUG_ON(i != rec_argc);
1651
1652 return cmd_buildid_list(i, rec_argv, NULL);
1653}
1654
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001655int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001656{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001657 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001658 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001659 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001660 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001661 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001662 "Output file name"),
1663 OPT_BOOLEAN(0, "guest", &perf_guest,
1664 "Collect guest os data"),
1665 OPT_BOOLEAN(0, "host", &perf_host,
1666 "Collect host os data"),
1667 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1668 "guest mount directory under which every guest os"
1669 " instance has a subdir"),
1670 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1671 "file", "file saving guest os vmlinux"),
1672 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1673 "file", "file saving guest os /proc/kallsyms"),
1674 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1675 "file", "file saving guest os /proc/modules"),
1676 OPT_END()
1677 };
1678
1679
1680 const char * const kvm_usage[] = {
1681 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
1682 NULL
1683 };
1684
Joerg Roedel1aed2672012-01-04 17:54:20 +01001685 perf_host = 0;
1686 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001687
1688 argc = parse_options(argc, argv, kvm_options, kvm_usage,
1689 PARSE_OPT_STOP_AT_NON_OPTION);
1690 if (!argc)
1691 usage_with_options(kvm_usage, kvm_options);
1692
1693 if (!perf_host)
1694 perf_guest = 1;
1695
Xiao Guangrong37860632012-11-15 14:17:01 +08001696 if (!file_name) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001697 if (perf_host && !perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001698 file_name = strdup("perf.data.host");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001699 else if (!perf_host && perf_guest)
Xiao Guangrong37860632012-11-15 14:17:01 +08001700 file_name = strdup("perf.data.guest");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001701 else
Xiao Guangrong37860632012-11-15 14:17:01 +08001702 file_name = strdup("perf.data.kvm");
David Ahernde332ac2012-10-02 22:09:53 -06001703
Xiao Guangrong37860632012-11-15 14:17:01 +08001704 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001705 pr_err("Failed to allocate memory for filename\n");
1706 return -ENOMEM;
1707 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001708 }
1709
1710 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001711 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001712 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001713 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001714 else if (!strncmp(argv[0], "diff", 4))
1715 return cmd_diff(argc, argv, NULL);
1716 else if (!strncmp(argv[0], "top", 3))
1717 return cmd_top(argc, argv, NULL);
1718 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001719 return __cmd_buildid_list(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001720#if defined(__i386__) || defined(__x86_64__)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001721 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001722 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001723#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001724 else
1725 usage_with_options(kvm_usage, kvm_options);
1726
1727 return 0;
1728}