blob: 3eb53e33c02b93f29c45f95dae3e3d2aef158c3f [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 Guangrongbcf6edc2012-09-17 16:31:15 +080025#include "../../arch/x86/include/asm/svm.h"
26#include "../../arch/x86/include/asm/vmx.h"
27#include "../../arch/x86/include/asm/kvm.h"
28
29struct event_key {
30 #define INVALID_KEY (~0ULL)
31 u64 key;
32 int info;
33};
34
35struct kvm_events_ops {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030036 bool (*is_begin_event)(struct perf_evsel *evsel,
37 struct perf_sample *sample,
38 struct event_key *key);
39 bool (*is_end_event)(struct perf_evsel *evsel,
40 struct perf_sample *sample, struct event_key *key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080041 void (*decode_key)(struct event_key *key, char decode[20]);
42 const char *name;
43};
44
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030045static void exit_event_get_key(struct perf_evsel *evsel,
46 struct perf_sample *sample,
47 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080048{
49 key->info = 0;
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030050 key->key = perf_evsel__intval(evsel, sample, "exit_reason");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080051}
52
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030053static bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080054{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030055 return !strcmp(evsel->name, "kvm:kvm_exit");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080056}
57
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030058static bool exit_event_begin(struct perf_evsel *evsel,
59 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080060{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030061 if (kvm_exit_event(evsel)) {
62 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080063 return true;
64 }
65
66 return false;
67}
68
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030069static bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080070{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030071 return !strcmp(evsel->name, "kvm:kvm_entry");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080072}
73
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030074static bool exit_event_end(struct perf_evsel *evsel,
75 struct perf_sample *sample __maybe_unused,
76 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080077{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030078 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080079}
80
81struct exit_reasons_table {
82 unsigned long exit_code;
83 const char *reason;
84};
85
86struct exit_reasons_table vmx_exit_reasons[] = {
87 VMX_EXIT_REASONS
88};
89
90struct exit_reasons_table svm_exit_reasons[] = {
91 SVM_EXIT_REASONS
92};
93
94static int cpu_isa;
95
96static const char *get_exit_reason(u64 exit_code)
97{
98 int table_size = ARRAY_SIZE(svm_exit_reasons);
99 struct exit_reasons_table *table = svm_exit_reasons;
100
101 if (cpu_isa == 1) {
102 table = vmx_exit_reasons;
103 table_size = ARRAY_SIZE(vmx_exit_reasons);
104 }
105
106 while (table_size--) {
107 if (table->exit_code == exit_code)
108 return table->reason;
109 table++;
110 }
111
112 pr_err("unknown kvm exit code:%lld on %s\n",
113 (unsigned long long)exit_code, cpu_isa ? "VMX" : "SVM");
114 return "UNKNOWN";
115}
116
117static void exit_event_decode_key(struct event_key *key, char decode[20])
118{
119 const char *exit_reason = get_exit_reason(key->key);
120
121 scnprintf(decode, 20, "%s", exit_reason);
122}
123
124static struct kvm_events_ops exit_events = {
125 .is_begin_event = exit_event_begin,
126 .is_end_event = exit_event_end,
127 .decode_key = exit_event_decode_key,
128 .name = "VM-EXIT"
129};
130
131 /*
132 * For the mmio events, we treat:
133 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
134 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
135 */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300136static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
137 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800138{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300139 key->key = perf_evsel__intval(evsel, sample, "gpa");
140 key->info = perf_evsel__intval(evsel, sample, "type");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800141}
142
143#define KVM_TRACE_MMIO_READ_UNSATISFIED 0
144#define KVM_TRACE_MMIO_READ 1
145#define KVM_TRACE_MMIO_WRITE 2
146
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300147static bool mmio_event_begin(struct perf_evsel *evsel,
148 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800149{
150 /* MMIO read begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300151 if (kvm_exit_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800152 return true;
153
154 /* MMIO write begin event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300155 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
156 perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
157 mmio_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800158 return true;
159 }
160
161 return false;
162}
163
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300164static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
165 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800166{
167 /* MMIO write end event in kernel. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300168 if (kvm_entry_event(evsel))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800169 return true;
170
171 /* MMIO read end event in kernel.*/
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300172 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
173 perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
174 mmio_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800175 return true;
176 }
177
178 return false;
179}
180
181static void mmio_event_decode_key(struct event_key *key, char decode[20])
182{
183 scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
184 key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
185}
186
187static struct kvm_events_ops mmio_events = {
188 .is_begin_event = mmio_event_begin,
189 .is_end_event = mmio_event_end,
190 .decode_key = mmio_event_decode_key,
191 .name = "MMIO Access"
192};
193
194 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300195static void ioport_event_get_key(struct perf_evsel *evsel,
196 struct perf_sample *sample,
197 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800198{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300199 key->key = perf_evsel__intval(evsel, sample, "port");
200 key->info = perf_evsel__intval(evsel, sample, "rw");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800201}
202
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300203static bool ioport_event_begin(struct perf_evsel *evsel,
204 struct perf_sample *sample,
205 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800206{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300207 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
208 ioport_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 ioport_event_end(struct perf_evsel *evsel,
216 struct perf_sample *sample __maybe_unused,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800217 struct event_key *key __maybe_unused)
218{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300219 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800220}
221
222static void ioport_event_decode_key(struct event_key *key, char decode[20])
223{
224 scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
225 key->info ? "POUT" : "PIN");
226}
227
228static struct kvm_events_ops ioport_events = {
229 .is_begin_event = ioport_event_begin,
230 .is_end_event = ioport_event_end,
231 .decode_key = ioport_event_decode_key,
232 .name = "IO Port Access"
233};
234
235static const char *report_event = "vmexit";
236struct kvm_events_ops *events_ops;
237
238static bool register_kvm_events_ops(void)
239{
240 bool ret = true;
241
242 if (!strcmp(report_event, "vmexit"))
243 events_ops = &exit_events;
244 else if (!strcmp(report_event, "mmio"))
245 events_ops = &mmio_events;
246 else if (!strcmp(report_event, "ioport"))
247 events_ops = &ioport_events;
248 else {
249 pr_err("Unknown report event:%s\n", report_event);
250 ret = false;
251 }
252
253 return ret;
254}
255
256struct kvm_event_stats {
257 u64 time;
258 struct stats stats;
259};
260
261struct kvm_event {
262 struct list_head hash_entry;
263 struct rb_node rb;
264
265 struct event_key key;
266
267 struct kvm_event_stats total;
268
269 #define DEFAULT_VCPU_NUM 8
270 int max_vcpu;
271 struct kvm_event_stats *vcpu;
272};
273
274struct vcpu_event_record {
275 int vcpu_id;
276 u64 start_time;
277 struct kvm_event *last_event;
278};
279
280#define EVENTS_BITS 12
281#define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
282
283static u64 total_time;
284static u64 total_count;
285static struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
286
287static void init_kvm_event_record(void)
288{
289 int i;
290
291 for (i = 0; i < (int)EVENTS_CACHE_SIZE; i++)
292 INIT_LIST_HEAD(&kvm_events_cache[i]);
293}
294
295static int kvm_events_hash_fn(u64 key)
296{
297 return key & (EVENTS_CACHE_SIZE - 1);
298}
299
300static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
301{
302 int old_max_vcpu = event->max_vcpu;
303
304 if (vcpu_id < event->max_vcpu)
305 return true;
306
307 while (event->max_vcpu <= vcpu_id)
308 event->max_vcpu += DEFAULT_VCPU_NUM;
309
310 event->vcpu = realloc(event->vcpu,
311 event->max_vcpu * sizeof(*event->vcpu));
312 if (!event->vcpu) {
313 pr_err("Not enough memory\n");
314 return false;
315 }
316
317 memset(event->vcpu + old_max_vcpu, 0,
318 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
319 return true;
320}
321
322static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
323{
324 struct kvm_event *event;
325
326 event = zalloc(sizeof(*event));
327 if (!event) {
328 pr_err("Not enough memory\n");
329 return NULL;
330 }
331
332 event->key = *key;
333 return event;
334}
335
336static struct kvm_event *find_create_kvm_event(struct event_key *key)
337{
338 struct kvm_event *event;
339 struct list_head *head;
340
341 BUG_ON(key->key == INVALID_KEY);
342
343 head = &kvm_events_cache[kvm_events_hash_fn(key->key)];
344 list_for_each_entry(event, head, hash_entry)
345 if (event->key.key == key->key && event->key.info == key->info)
346 return event;
347
348 event = kvm_alloc_init_event(key);
349 if (!event)
350 return NULL;
351
352 list_add(&event->hash_entry, head);
353 return event;
354}
355
356static bool handle_begin_event(struct vcpu_event_record *vcpu_record,
357 struct event_key *key, u64 timestamp)
358{
359 struct kvm_event *event = NULL;
360
361 if (key->key != INVALID_KEY)
362 event = find_create_kvm_event(key);
363
364 vcpu_record->last_event = event;
365 vcpu_record->start_time = timestamp;
366 return true;
367}
368
369static void
370kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
371{
372 kvm_stats->time += time_diff;
373 update_stats(&kvm_stats->stats, time_diff);
374}
375
376static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
377{
378 struct kvm_event_stats *kvm_stats = &event->total;
379
380 if (vcpu_id != -1)
381 kvm_stats = &event->vcpu[vcpu_id];
382
383 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
384 avg_stats(&kvm_stats->stats));
385}
386
387static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
388 u64 time_diff)
389{
390 kvm_update_event_stats(&event->total, time_diff);
391
392 if (!kvm_event_expand(event, vcpu_id))
393 return false;
394
395 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
396 return true;
397}
398
399static bool handle_end_event(struct vcpu_event_record *vcpu_record,
400 struct event_key *key, u64 timestamp)
401{
402 struct kvm_event *event;
403 u64 time_begin, time_diff;
404
405 event = vcpu_record->last_event;
406 time_begin = vcpu_record->start_time;
407
408 /* The begin event is not caught. */
409 if (!time_begin)
410 return true;
411
412 /*
413 * In some case, the 'begin event' only records the start timestamp,
414 * the actual event is recognized in the 'end event' (e.g. mmio-event).
415 */
416
417 /* Both begin and end events did not get the key. */
418 if (!event && key->key == INVALID_KEY)
419 return true;
420
421 if (!event)
422 event = find_create_kvm_event(key);
423
424 if (!event)
425 return false;
426
427 vcpu_record->last_event = NULL;
428 vcpu_record->start_time = 0;
429
430 BUG_ON(timestamp < time_begin);
431
432 time_diff = timestamp - time_begin;
433 return update_kvm_event(event, vcpu_record->vcpu_id, time_diff);
434}
435
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300436static
437struct vcpu_event_record *per_vcpu_record(struct thread *thread,
438 struct perf_evsel *evsel,
439 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800440{
441 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300442 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800443 struct vcpu_event_record *vcpu_record;
444
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300445 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800446 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300447 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800448 return NULL;
449 }
450
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300451 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800452 thread->priv = vcpu_record;
453 }
454
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300455 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800456}
457
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300458static bool handle_kvm_event(struct thread *thread, struct perf_evsel *evsel,
459 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800460{
461 struct vcpu_event_record *vcpu_record;
462 struct event_key key = {.key = INVALID_KEY};
463
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300464 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800465 if (!vcpu_record)
466 return true;
467
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300468 if (events_ops->is_begin_event(evsel, sample, &key))
469 return handle_begin_event(vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800470
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300471 if (events_ops->is_end_event(evsel, sample, &key))
472 return handle_end_event(vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800473
474 return true;
475}
476
477typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
478struct kvm_event_key {
479 const char *name;
480 key_cmp_fun key;
481};
482
483static int trace_vcpu = -1;
484#define GET_EVENT_KEY(func, field) \
485static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
486{ \
487 if (vcpu == -1) \
488 return event->total.field; \
489 \
490 if (vcpu >= event->max_vcpu) \
491 return 0; \
492 \
493 return event->vcpu[vcpu].field; \
494}
495
496#define COMPARE_EVENT_KEY(func, field) \
497GET_EVENT_KEY(func, field) \
498static int compare_kvm_event_ ## func(struct kvm_event *one, \
499 struct kvm_event *two, int vcpu)\
500{ \
501 return get_event_ ##func(one, vcpu) > \
502 get_event_ ##func(two, vcpu); \
503}
504
505GET_EVENT_KEY(time, time);
506COMPARE_EVENT_KEY(count, stats.n);
507COMPARE_EVENT_KEY(mean, stats.mean);
508
509#define DEF_SORT_NAME_KEY(name, compare_key) \
510 { #name, compare_kvm_event_ ## compare_key }
511
512static struct kvm_event_key keys[] = {
513 DEF_SORT_NAME_KEY(sample, count),
514 DEF_SORT_NAME_KEY(time, mean),
515 { NULL, NULL }
516};
517
518static const char *sort_key = "sample";
519static key_cmp_fun compare;
520
521static bool select_key(void)
522{
523 int i;
524
525 for (i = 0; keys[i].name; i++) {
526 if (!strcmp(keys[i].name, sort_key)) {
527 compare = keys[i].key;
528 return true;
529 }
530 }
531
532 pr_err("Unknown compare key:%s\n", sort_key);
533 return false;
534}
535
536static struct rb_root result;
537static void insert_to_result(struct kvm_event *event, key_cmp_fun bigger,
538 int vcpu)
539{
540 struct rb_node **rb = &result.rb_node;
541 struct rb_node *parent = NULL;
542 struct kvm_event *p;
543
544 while (*rb) {
545 p = container_of(*rb, struct kvm_event, rb);
546 parent = *rb;
547
548 if (bigger(event, p, vcpu))
549 rb = &(*rb)->rb_left;
550 else
551 rb = &(*rb)->rb_right;
552 }
553
554 rb_link_node(&event->rb, parent, rb);
555 rb_insert_color(&event->rb, &result);
556}
557
558static void update_total_count(struct kvm_event *event, int vcpu)
559{
560 total_count += get_event_count(event, vcpu);
561 total_time += get_event_time(event, vcpu);
562}
563
564static bool event_is_valid(struct kvm_event *event, int vcpu)
565{
566 return !!get_event_count(event, vcpu);
567}
568
569static void sort_result(int vcpu)
570{
571 unsigned int i;
572 struct kvm_event *event;
573
574 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
575 list_for_each_entry(event, &kvm_events_cache[i], hash_entry)
576 if (event_is_valid(event, vcpu)) {
577 update_total_count(event, vcpu);
578 insert_to_result(event, compare, vcpu);
579 }
580}
581
582/* returns left most element of result, and erase it */
583static struct kvm_event *pop_from_result(void)
584{
585 struct rb_node *node = rb_first(&result);
586
587 if (!node)
588 return NULL;
589
590 rb_erase(node, &result);
591 return container_of(node, struct kvm_event, rb);
592}
593
594static void print_vcpu_info(int vcpu)
595{
596 pr_info("Analyze events for ");
597
598 if (vcpu == -1)
599 pr_info("all VCPUs:\n\n");
600 else
601 pr_info("VCPU %d:\n\n", vcpu);
602}
603
604static void print_result(int vcpu)
605{
606 char decode[20];
607 struct kvm_event *event;
608
609 pr_info("\n\n");
610 print_vcpu_info(vcpu);
611 pr_info("%20s ", events_ops->name);
612 pr_info("%10s ", "Samples");
613 pr_info("%9s ", "Samples%");
614
615 pr_info("%9s ", "Time%");
616 pr_info("%16s ", "Avg time");
617 pr_info("\n\n");
618
619 while ((event = pop_from_result())) {
620 u64 ecount, etime;
621
622 ecount = get_event_count(event, vcpu);
623 etime = get_event_time(event, vcpu);
624
625 events_ops->decode_key(&event->key, decode);
626 pr_info("%20s ", decode);
627 pr_info("%10llu ", (unsigned long long)ecount);
628 pr_info("%8.2f%% ", (double)ecount / total_count * 100);
629 pr_info("%8.2f%% ", (double)etime / total_time * 100);
630 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
631 kvm_event_rel_stddev(vcpu, event));
632 pr_info("\n");
633 }
634
635 pr_info("\nTotal Samples:%lld, Total events handled time:%.2fus.\n\n",
636 (unsigned long long)total_count, total_time / 1e3);
637}
638
639static int process_sample_event(struct perf_tool *tool __maybe_unused,
640 union perf_event *event,
641 struct perf_sample *sample,
642 struct perf_evsel *evsel,
643 struct machine *machine)
644{
645 struct thread *thread = machine__findnew_thread(machine, sample->tid);
646
647 if (thread == NULL) {
648 pr_debug("problem processing %d event, skipping it.\n",
649 event->header.type);
650 return -1;
651 }
652
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300653 if (!handle_kvm_event(thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800654 return -1;
655
656 return 0;
657}
658
659static struct perf_tool eops = {
660 .sample = process_sample_event,
661 .comm = perf_event__process_comm,
662 .ordered_samples = true,
663};
664
665static int get_cpu_isa(struct perf_session *session)
666{
667 char *cpuid;
668 int isa;
669
670 cpuid = perf_header__read_feature(session, HEADER_CPUID);
671
672 if (!cpuid) {
673 pr_err("read HEADER_CPUID failed.\n");
674 return -ENOTSUP;
675 }
676
677 if (strstr(cpuid, "Intel"))
678 isa = 1;
679 else if (strstr(cpuid, "AMD"))
680 isa = 0;
681 else {
682 pr_err("CPU %s is not supported.\n", cpuid);
683 isa = -ENOTSUP;
684 }
685
686 free(cpuid);
687 return isa;
688}
689
690static const char *file_name;
691
692static int read_events(void)
693{
694 struct perf_session *kvm_session;
695 int ret;
696
697 kvm_session = perf_session__new(file_name, O_RDONLY, 0, false, &eops);
698 if (!kvm_session) {
699 pr_err("Initializing perf session failed\n");
700 return -EINVAL;
701 }
702
703 if (!perf_session__has_traces(kvm_session, "kvm record"))
704 return -EINVAL;
705
706 /*
707 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
708 * traced in the old kernel.
709 */
710 ret = get_cpu_isa(kvm_session);
711
712 if (ret < 0)
713 return ret;
714
715 cpu_isa = ret;
716
717 return perf_session__process_events(kvm_session, &eops);
718}
719
720static bool verify_vcpu(int vcpu)
721{
722 if (vcpu != -1 && vcpu < 0) {
723 pr_err("Invalid vcpu:%d.\n", vcpu);
724 return false;
725 }
726
727 return true;
728}
729
730static int kvm_events_report_vcpu(int vcpu)
731{
732 int ret = -EINVAL;
733
734 if (!verify_vcpu(vcpu))
735 goto exit;
736
737 if (!select_key())
738 goto exit;
739
740 if (!register_kvm_events_ops())
741 goto exit;
742
743 init_kvm_event_record();
744 setup_pager();
745
746 ret = read_events();
747 if (ret)
748 goto exit;
749
750 sort_result(vcpu);
751 print_result(vcpu);
752exit:
753 return ret;
754}
755
756static const char * const record_args[] = {
757 "record",
758 "-R",
759 "-f",
760 "-m", "1024",
761 "-c", "1",
762 "-e", "kvm:kvm_entry",
763 "-e", "kvm:kvm_exit",
764 "-e", "kvm:kvm_mmio",
765 "-e", "kvm:kvm_pio",
766};
767
768#define STRDUP_FAIL_EXIT(s) \
769 ({ char *_p; \
770 _p = strdup(s); \
771 if (!_p) \
772 return -ENOMEM; \
773 _p; \
774 })
775
776static int kvm_events_record(int argc, const char **argv)
777{
778 unsigned int rec_argc, i, j;
779 const char **rec_argv;
780
781 rec_argc = ARRAY_SIZE(record_args) + argc + 2;
782 rec_argv = calloc(rec_argc + 1, sizeof(char *));
783
784 if (rec_argv == NULL)
785 return -ENOMEM;
786
787 for (i = 0; i < ARRAY_SIZE(record_args); i++)
788 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
789
790 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
791 rec_argv[i++] = STRDUP_FAIL_EXIT(file_name);
792
793 for (j = 1; j < (unsigned int)argc; j++, i++)
794 rec_argv[i] = argv[j];
795
796 return cmd_record(i, rec_argv, NULL);
797}
798
799static const char * const kvm_events_report_usage[] = {
800 "perf kvm stat report [<options>]",
801 NULL
802};
803
804static const struct option kvm_events_report_options[] = {
805 OPT_STRING(0, "event", &report_event, "report event",
806 "event for reporting: vmexit, mmio, ioport"),
807 OPT_INTEGER(0, "vcpu", &trace_vcpu,
808 "vcpu id to report"),
809 OPT_STRING('k', "key", &sort_key, "sort-key",
810 "key for sorting: sample(sort by samples number)"
811 " time (sort by avg time)"),
812 OPT_END()
813};
814
815static int kvm_events_report(int argc, const char **argv)
816{
817 symbol__init();
818
819 if (argc) {
820 argc = parse_options(argc, argv,
821 kvm_events_report_options,
822 kvm_events_report_usage, 0);
823 if (argc)
824 usage_with_options(kvm_events_report_usage,
825 kvm_events_report_options);
826 }
827
828 return kvm_events_report_vcpu(trace_vcpu);
829}
830
831static void print_kvm_stat_usage(void)
832{
833 printf("Usage: perf kvm stat <command>\n\n");
834
835 printf("# Available commands:\n");
836 printf("\trecord: record kvm events\n");
837 printf("\treport: report statistical data of kvm events\n");
838
839 printf("\nOtherwise, it is the alias of 'perf stat':\n");
840}
841
842static int kvm_cmd_stat(int argc, const char **argv)
843{
844 if (argc == 1) {
845 print_kvm_stat_usage();
846 goto perf_stat;
847 }
848
849 if (!strncmp(argv[1], "rec", 3))
850 return kvm_events_record(argc - 1, argv + 1);
851
852 if (!strncmp(argv[1], "rep", 3))
853 return kvm_events_report(argc - 1 , argv + 1);
854
855perf_stat:
856 return cmd_stat(argc, argv, NULL);
857}
858
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800859static char name_buffer[256];
860
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800861static const char * const kvm_usage[] = {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800862 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800863 NULL
864};
865
866static const struct option kvm_options[] = {
867 OPT_STRING('i', "input", &file_name, "file",
868 "Input file name"),
869 OPT_STRING('o', "output", &file_name, "file",
870 "Output file name"),
871 OPT_BOOLEAN(0, "guest", &perf_guest,
872 "Collect guest os data"),
873 OPT_BOOLEAN(0, "host", &perf_host,
Joerg Roedel9e183422011-10-05 14:01:19 +0200874 "Collect host os data"),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800875 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
876 "guest mount directory under which every guest os"
877 " instance has a subdir"),
878 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
879 "file", "file saving guest os vmlinux"),
880 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
881 "file", "file saving guest os /proc/kallsyms"),
882 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
883 "file", "file saving guest os /proc/modules"),
884 OPT_END()
885};
886
887static int __cmd_record(int argc, const char **argv)
888{
889 int rec_argc, i = 0, j;
890 const char **rec_argv;
891
892 rec_argc = argc + 2;
893 rec_argv = calloc(rec_argc + 1, sizeof(char *));
894 rec_argv[i++] = strdup("record");
895 rec_argv[i++] = strdup("-o");
896 rec_argv[i++] = strdup(file_name);
897 for (j = 1; j < argc; j++, i++)
898 rec_argv[i] = argv[j];
899
900 BUG_ON(i != rec_argc);
901
902 return cmd_record(i, rec_argv, NULL);
903}
904
905static int __cmd_report(int argc, const char **argv)
906{
907 int rec_argc, i = 0, j;
908 const char **rec_argv;
909
910 rec_argc = argc + 2;
911 rec_argv = calloc(rec_argc + 1, sizeof(char *));
912 rec_argv[i++] = strdup("report");
913 rec_argv[i++] = strdup("-i");
914 rec_argv[i++] = strdup(file_name);
915 for (j = 1; j < argc; j++, i++)
916 rec_argv[i] = argv[j];
917
918 BUG_ON(i != rec_argc);
919
920 return cmd_report(i, rec_argv, NULL);
921}
922
923static int __cmd_buildid_list(int argc, const char **argv)
924{
925 int rec_argc, i = 0, j;
926 const char **rec_argv;
927
928 rec_argc = argc + 2;
929 rec_argv = calloc(rec_argc + 1, sizeof(char *));
930 rec_argv[i++] = strdup("buildid-list");
931 rec_argv[i++] = strdup("-i");
932 rec_argv[i++] = strdup(file_name);
933 for (j = 1; j < argc; j++, i++)
934 rec_argv[i] = argv[j];
935
936 BUG_ON(i != rec_argc);
937
938 return cmd_buildid_list(i, rec_argv, NULL);
939}
940
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300941int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800942{
Joerg Roedel1aed2672012-01-04 17:54:20 +0100943 perf_host = 0;
944 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800945
946 argc = parse_options(argc, argv, kvm_options, kvm_usage,
947 PARSE_OPT_STOP_AT_NON_OPTION);
948 if (!argc)
949 usage_with_options(kvm_usage, kvm_options);
950
951 if (!perf_host)
952 perf_guest = 1;
953
954 if (!file_name) {
955 if (perf_host && !perf_guest)
956 sprintf(name_buffer, "perf.data.host");
957 else if (!perf_host && perf_guest)
958 sprintf(name_buffer, "perf.data.guest");
959 else
960 sprintf(name_buffer, "perf.data.kvm");
961 file_name = name_buffer;
962 }
963
964 if (!strncmp(argv[0], "rec", 3))
965 return __cmd_record(argc, argv);
966 else if (!strncmp(argv[0], "rep", 3))
967 return __cmd_report(argc, argv);
968 else if (!strncmp(argv[0], "diff", 4))
969 return cmd_diff(argc, argv, NULL);
970 else if (!strncmp(argv[0], "top", 3))
971 return cmd_top(argc, argv, NULL);
972 else if (!strncmp(argv[0], "buildid-list", 12))
973 return __cmd_buildid_list(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800974 else if (!strncmp(argv[0], "stat", 4))
975 return kvm_cmd_stat(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800976 else
977 usage_with_options(kvm_usage, kvm_options);
978
979 return 0;
980}