blob: fc2d63d3e79141a73408d64f6de48eeb31a45422 [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 Petkov553873e2013-12-09 17:14:23 +010016#include <api/fs/debugfs.h>
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080017#include "util/tool.h"
18#include "util/stat.h"
David Ahern1afe1d12013-08-05 21:41:34 -040019#include "util/top.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020020#include "util/data.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080021
22#include <sys/prctl.h>
David Ahern87419c92013-10-29 10:43:16 -060023#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -040024#include <sys/timerfd.h>
David Ahern87419c92013-10-29 10:43:16 -060025#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080026
David Ahern1afe1d12013-08-05 21:41:34 -040027#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080028#include <semaphore.h>
29#include <pthread.h>
30#include <math.h>
31
Alexander Yaryginda50ad62014-07-03 17:59:49 +040032#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin44b38022014-07-03 18:29:04 +040033#include <asm/kvm_perf.h>
Alexander Yarygin9daa8122014-07-03 18:29:05 +040034#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080035
Alexander Yarygin9daa8122014-07-03 18:29:05 +040036void exit_event_get_key(struct perf_evsel *evsel,
37 struct perf_sample *sample,
38 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080039{
40 key->info = 0;
Alexander Yarygin44b38022014-07-03 18:29:04 +040041 key->key = perf_evsel__intval(evsel, sample, KVM_EXIT_REASON);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080042}
43
Alexander Yarygin9daa8122014-07-03 18:29:05 +040044bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080045{
Alexander Yarygin44b38022014-07-03 18:29:04 +040046 return !strcmp(evsel->name, KVM_EXIT_TRACE);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080047}
48
Alexander Yarygin9daa8122014-07-03 18:29:05 +040049bool exit_event_begin(struct perf_evsel *evsel,
50 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080051{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030052 if (kvm_exit_event(evsel)) {
53 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080054 return true;
55 }
56
57 return false;
58}
59
Alexander Yarygin9daa8122014-07-03 18:29:05 +040060bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080061{
Alexander Yarygin44b38022014-07-03 18:29:04 +040062 return !strcmp(evsel->name, KVM_ENTRY_TRACE);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080063}
64
Alexander Yarygin9daa8122014-07-03 18:29:05 +040065bool exit_event_end(struct perf_evsel *evsel,
66 struct perf_sample *sample __maybe_unused,
67 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080068{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030069 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080070}
71
Alexander Yarygindf74c132014-07-03 17:59:50 +040072static const char *get_exit_reason(struct perf_kvm_stat *kvm,
73 struct exit_reasons_table *tbl,
74 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080075{
Alexander Yarygindf74c132014-07-03 17:59:50 +040076 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060077 if (tbl->exit_code == exit_code)
78 return tbl->reason;
79 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080080 }
81
82 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -060083 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080084 return "UNKNOWN";
85}
86
Alexander Yarygin9daa8122014-07-03 18:29:05 +040087void exit_event_decode_key(struct perf_kvm_stat *kvm,
88 struct event_key *key,
89 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080090{
Alexander Yarygindf74c132014-07-03 17:59:50 +040091 const char *exit_reason = get_exit_reason(kvm, kvm->exit_reasons,
92 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080093
Alexander Yarygin44b38022014-07-03 18:29:04 +040094 scnprintf(decode, DECODE_STR_LEN, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080095}
96
Xiao Guangrong37860632012-11-15 14:17:01 +080097static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080098{
Alexander Yarygin9daa8122014-07-03 18:29:05 +040099 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800100
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400101 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
102 if (!strcmp(events_ops->name, kvm->report_event)) {
103 kvm->events_ops = events_ops->ops;
104 return true;
105 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800106 }
107
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400108 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800109}
110
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800111struct vcpu_event_record {
112 int vcpu_id;
113 u64 start_time;
114 struct kvm_event *last_event;
115};
116
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800117
Xiao Guangrong37860632012-11-15 14:17:01 +0800118static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800119{
David Ahernb880dee2012-10-08 11:17:30 -0600120 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800121
David Ahernb880dee2012-10-08 11:17:30 -0600122 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600123 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800124}
125
David Ahern87419c92013-10-29 10:43:16 -0600126#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400127static void clear_events_cache_stats(struct list_head *kvm_events_cache)
128{
129 struct list_head *head;
130 struct kvm_event *event;
131 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400132 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400133
134 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
135 head = &kvm_events_cache[i];
136 list_for_each_entry(event, head, hash_entry) {
137 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400138 event->total.time = 0;
139 init_stats(&event->total.stats);
140
141 for (j = 0; j < event->max_vcpu; ++j) {
142 event->vcpu[j].time = 0;
143 init_stats(&event->vcpu[j].stats);
144 }
David Ahern1afe1d12013-08-05 21:41:34 -0400145 }
146 }
147}
David Ahern87419c92013-10-29 10:43:16 -0600148#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400149
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800150static int kvm_events_hash_fn(u64 key)
151{
152 return key & (EVENTS_CACHE_SIZE - 1);
153}
154
155static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
156{
157 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600158 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800159
160 if (vcpu_id < event->max_vcpu)
161 return true;
162
163 while (event->max_vcpu <= vcpu_id)
164 event->max_vcpu += DEFAULT_VCPU_NUM;
165
David Ahern6ca5f302013-05-25 18:24:46 -0600166 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800167 event->vcpu = realloc(event->vcpu,
168 event->max_vcpu * sizeof(*event->vcpu));
169 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600170 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800171 pr_err("Not enough memory\n");
172 return false;
173 }
174
175 memset(event->vcpu + old_max_vcpu, 0,
176 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
177 return true;
178}
179
180static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
181{
182 struct kvm_event *event;
183
184 event = zalloc(sizeof(*event));
185 if (!event) {
186 pr_err("Not enough memory\n");
187 return NULL;
188 }
189
190 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200191 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800192 return event;
193}
194
Xiao Guangrong37860632012-11-15 14:17:01 +0800195static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600196 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800197{
198 struct kvm_event *event;
199 struct list_head *head;
200
201 BUG_ON(key->key == INVALID_KEY);
202
David Ahernde332ac2012-10-02 22:09:53 -0600203 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600204 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800205 if (event->key.key == key->key && event->key.info == key->info)
206 return event;
David Ahern355afe82012-10-08 11:17:32 -0600207 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800208
209 event = kvm_alloc_init_event(key);
210 if (!event)
211 return NULL;
212
213 list_add(&event->hash_entry, head);
214 return event;
215}
216
Xiao Guangrong37860632012-11-15 14:17:01 +0800217static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600218 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800219 struct event_key *key, u64 timestamp)
220{
221 struct kvm_event *event = NULL;
222
223 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600224 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800225
226 vcpu_record->last_event = event;
227 vcpu_record->start_time = timestamp;
228 return true;
229}
230
231static void
232kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
233{
234 kvm_stats->time += time_diff;
235 update_stats(&kvm_stats->stats, time_diff);
236}
237
238static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
239{
240 struct kvm_event_stats *kvm_stats = &event->total;
241
242 if (vcpu_id != -1)
243 kvm_stats = &event->vcpu[vcpu_id];
244
245 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
246 avg_stats(&kvm_stats->stats));
247}
248
249static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
250 u64 time_diff)
251{
David Ahern2aa8eab2012-10-08 11:17:35 -0600252 if (vcpu_id == -1) {
253 kvm_update_event_stats(&event->total, time_diff);
254 return true;
255 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800256
257 if (!kvm_event_expand(event, vcpu_id))
258 return false;
259
260 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
261 return true;
262}
263
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400264static bool skip_event(const char *event)
265{
266 const char * const *skip_events;
267
268 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
269 if (!strcmp(event, *skip_events))
270 return true;
271
272 return false;
273}
274
Xiao Guangrong37860632012-11-15 14:17:01 +0800275static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600276 struct vcpu_event_record *vcpu_record,
277 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400278 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800279{
280 struct kvm_event *event;
281 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600282 int vcpu;
283
284 if (kvm->trace_vcpu == -1)
285 vcpu = -1;
286 else
287 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800288
289 event = vcpu_record->last_event;
290 time_begin = vcpu_record->start_time;
291
292 /* The begin event is not caught. */
293 if (!time_begin)
294 return true;
295
296 /*
297 * In some case, the 'begin event' only records the start timestamp,
298 * the actual event is recognized in the 'end event' (e.g. mmio-event).
299 */
300
301 /* Both begin and end events did not get the key. */
302 if (!event && key->key == INVALID_KEY)
303 return true;
304
305 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600306 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800307
308 if (!event)
309 return false;
310
311 vcpu_record->last_event = NULL;
312 vcpu_record->start_time = 0;
313
David Ahern1afe1d12013-08-05 21:41:34 -0400314 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400315 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400316 pr_debug("End time before begin time; skipping event.\n");
317 return true;
318 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800319
David Ahern70f7b4a2013-08-07 21:56:38 -0400320 time_diff = sample->time - time_begin;
321
322 if (kvm->duration && time_diff > kvm->duration) {
Alexander Yarygin44b38022014-07-03 18:29:04 +0400323 char decode[DECODE_STR_LEN];
David Ahern70f7b4a2013-08-07 21:56:38 -0400324
325 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400326 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400327 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
328 sample->time, sample->pid, vcpu_record->vcpu_id,
329 decode, time_diff/1000);
330 }
331 }
332
David Ahern2aa8eab2012-10-08 11:17:35 -0600333 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800334}
335
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300336static
337struct vcpu_event_record *per_vcpu_record(struct thread *thread,
338 struct perf_evsel *evsel,
339 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800340{
341 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300342 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800343 struct vcpu_event_record *vcpu_record;
344
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300345 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800346 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300347 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800348 return NULL;
349 }
350
Alexander Yarygin44b38022014-07-03 18:29:04 +0400351 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, VCPU_ID);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800352 thread->priv = vcpu_record;
353 }
354
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300355 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800356}
357
Xiao Guangrong37860632012-11-15 14:17:01 +0800358static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600359 struct thread *thread,
360 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300361 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800362{
363 struct vcpu_event_record *vcpu_record;
364 struct event_key key = {.key = INVALID_KEY};
365
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300366 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800367 if (!vcpu_record)
368 return true;
369
David Ahern2aa8eab2012-10-08 11:17:35 -0600370 /* only process events for vcpus user cares about */
371 if ((kvm->trace_vcpu != -1) &&
372 (kvm->trace_vcpu != vcpu_record->vcpu_id))
373 return true;
374
David Ahernde332ac2012-10-02 22:09:53 -0600375 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
376 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800377
David Ahernde332ac2012-10-02 22:09:53 -0600378 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400379 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800380
381 return true;
382}
383
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800384#define GET_EVENT_KEY(func, field) \
385static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
386{ \
387 if (vcpu == -1) \
388 return event->total.field; \
389 \
390 if (vcpu >= event->max_vcpu) \
391 return 0; \
392 \
393 return event->vcpu[vcpu].field; \
394}
395
396#define COMPARE_EVENT_KEY(func, field) \
397GET_EVENT_KEY(func, field) \
398static int compare_kvm_event_ ## func(struct kvm_event *one, \
399 struct kvm_event *two, int vcpu)\
400{ \
401 return get_event_ ##func(one, vcpu) > \
402 get_event_ ##func(two, vcpu); \
403}
404
405GET_EVENT_KEY(time, time);
406COMPARE_EVENT_KEY(count, stats.n);
407COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400408GET_EVENT_KEY(max, stats.max);
409GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800410
411#define DEF_SORT_NAME_KEY(name, compare_key) \
412 { #name, compare_kvm_event_ ## compare_key }
413
414static struct kvm_event_key keys[] = {
415 DEF_SORT_NAME_KEY(sample, count),
416 DEF_SORT_NAME_KEY(time, mean),
417 { NULL, NULL }
418};
419
Xiao Guangrong37860632012-11-15 14:17:01 +0800420static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800421{
422 int i;
423
424 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600425 if (!strcmp(keys[i].name, kvm->sort_key)) {
426 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800427 return true;
428 }
429 }
430
David Ahernde332ac2012-10-02 22:09:53 -0600431 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800432 return false;
433}
434
David Ahernde332ac2012-10-02 22:09:53 -0600435static void insert_to_result(struct rb_root *result, struct kvm_event *event,
436 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800437{
David Ahernde332ac2012-10-02 22:09:53 -0600438 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800439 struct rb_node *parent = NULL;
440 struct kvm_event *p;
441
442 while (*rb) {
443 p = container_of(*rb, struct kvm_event, rb);
444 parent = *rb;
445
446 if (bigger(event, p, vcpu))
447 rb = &(*rb)->rb_left;
448 else
449 rb = &(*rb)->rb_right;
450 }
451
452 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600453 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800454}
455
Xiao Guangrong37860632012-11-15 14:17:01 +0800456static void
457update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800458{
David Ahernde332ac2012-10-02 22:09:53 -0600459 int vcpu = kvm->trace_vcpu;
460
461 kvm->total_count += get_event_count(event, vcpu);
462 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800463}
464
465static bool event_is_valid(struct kvm_event *event, int vcpu)
466{
467 return !!get_event_count(event, vcpu);
468}
469
Xiao Guangrong37860632012-11-15 14:17:01 +0800470static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800471{
472 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600473 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800474 struct kvm_event *event;
475
David Ahern355afe82012-10-08 11:17:32 -0600476 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
477 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800478 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600479 update_total_count(kvm, event);
480 insert_to_result(&kvm->result, event,
481 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800482 }
David Ahern355afe82012-10-08 11:17:32 -0600483 }
484 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800485}
486
487/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600488static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800489{
David Ahernde332ac2012-10-02 22:09:53 -0600490 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800491
492 if (!node)
493 return NULL;
494
David Ahernde332ac2012-10-02 22:09:53 -0600495 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800496 return container_of(node, struct kvm_event, rb);
497}
498
David Ahern1afe1d12013-08-05 21:41:34 -0400499static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800500{
David Ahern1afe1d12013-08-05 21:41:34 -0400501 int vcpu = kvm->trace_vcpu;
502
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800503 pr_info("Analyze events for ");
504
David Ahern1afe1d12013-08-05 21:41:34 -0400505 if (kvm->live) {
506 if (kvm->opts.target.system_wide)
507 pr_info("all VMs, ");
508 else if (kvm->opts.target.pid)
509 pr_info("pid(s) %s, ", kvm->opts.target.pid);
510 else
511 pr_info("dazed and confused on what is monitored, ");
512 }
513
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800514 if (vcpu == -1)
515 pr_info("all VCPUs:\n\n");
516 else
517 pr_info("VCPU %d:\n\n", vcpu);
518}
519
David Ahern1afe1d12013-08-05 21:41:34 -0400520static void show_timeofday(void)
521{
522 char date[64];
523 struct timeval tv;
524 struct tm ltime;
525
526 gettimeofday(&tv, NULL);
527 if (localtime_r(&tv.tv_sec, &ltime)) {
528 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
529 pr_info("%s.%06ld", date, tv.tv_usec);
530 } else
531 pr_info("00:00:00.000000");
532
533 return;
534}
535
Xiao Guangrong37860632012-11-15 14:17:01 +0800536static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800537{
Alexander Yarygin44b38022014-07-03 18:29:04 +0400538 char decode[DECODE_STR_LEN];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800539 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600540 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800541
David Ahern1afe1d12013-08-05 21:41:34 -0400542 if (kvm->live) {
543 puts(CONSOLE_CLEAR);
544 show_timeofday();
545 }
546
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800547 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400548 print_vcpu_info(kvm);
Alexander Yarygin44b38022014-07-03 18:29:04 +0400549 pr_info("%*s ", DECODE_STR_LEN, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800550 pr_info("%10s ", "Samples");
551 pr_info("%9s ", "Samples%");
552
553 pr_info("%9s ", "Time%");
David Ahern62d04db2013-08-05 21:41:35 -0400554 pr_info("%10s ", "Min Time");
555 pr_info("%10s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800556 pr_info("%16s ", "Avg time");
557 pr_info("\n\n");
558
David Ahernde332ac2012-10-02 22:09:53 -0600559 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400560 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800561
562 ecount = get_event_count(event, vcpu);
563 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400564 max = get_event_max(event, vcpu);
565 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800566
David Ahernde332ac2012-10-02 22:09:53 -0600567 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin44b38022014-07-03 18:29:04 +0400568 pr_info("%*s ", DECODE_STR_LEN, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800569 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600570 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
571 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
David Ahern62d04db2013-08-05 21:41:35 -0400572 pr_info("%8" PRIu64 "us ", min / 1000);
573 pr_info("%8" PRIu64 "us ", max / 1000);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800574 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
575 kvm_event_rel_stddev(vcpu, event));
576 pr_info("\n");
577 }
578
David Aherne4f76372012-10-08 11:17:34 -0600579 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
580 kvm->total_count, kvm->total_time / 1e3);
David Ahern1afe1d12013-08-05 21:41:34 -0400581
582 if (kvm->lost_events)
583 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
584}
585
David Ahern87419c92013-10-29 10:43:16 -0600586#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400587static int process_lost_event(struct perf_tool *tool,
588 union perf_event *event __maybe_unused,
589 struct perf_sample *sample __maybe_unused,
590 struct machine *machine __maybe_unused)
591{
592 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
593
594 kvm->lost_events++;
595 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800596}
David Ahern87419c92013-10-29 10:43:16 -0600597#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800598
David Ahern2e73f002013-08-05 21:41:37 -0400599static bool skip_sample(struct perf_kvm_stat *kvm,
600 struct perf_sample *sample)
601{
602 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
603 return true;
604
605 return false;
606}
607
David Ahernde332ac2012-10-02 22:09:53 -0600608static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800609 union perf_event *event,
610 struct perf_sample *sample,
611 struct perf_evsel *evsel,
612 struct machine *machine)
613{
David Ahern2e73f002013-08-05 21:41:37 -0400614 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800615 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
616 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800617
David Ahern2e73f002013-08-05 21:41:37 -0400618 if (skip_sample(kvm, sample))
619 return 0;
620
Adrian Hunter314add62013-08-27 11:23:03 +0300621 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800622 if (thread == NULL) {
623 pr_debug("problem processing %d event, skipping it.\n",
624 event->header.type);
625 return -1;
626 }
627
David Ahernde332ac2012-10-02 22:09:53 -0600628 if (!handle_kvm_event(kvm, thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800629 return -1;
630
631 return 0;
632}
633
David Ahern1afe1d12013-08-05 21:41:34 -0400634static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800635{
David Ahern1afe1d12013-08-05 21:41:34 -0400636 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400637 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400638
639 if (kvm->live) {
640 err = get_cpuid(buf, sizeof(buf));
641 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400642 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400643 return err;
644 }
645 cpuid = buf;
646 } else
647 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800648
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400649 if (!cpuid) {
650 pr_err("Failed to look up CPU type\n");
651 return -EINVAL;
652 }
653
654 err = cpu_isa_init(kvm, cpuid);
655 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800656 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800657
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400658 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400659}
660
661static bool verify_vcpu(int vcpu)
662{
663 if (vcpu != -1 && vcpu < 0) {
664 pr_err("Invalid vcpu:%d.\n", vcpu);
665 return false;
666 }
667
668 return true;
669}
670
David Ahern87419c92013-10-29 10:43:16 -0600671#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400672/* keeping the max events to a modest level to keep
673 * the processing of samples per mmap smooth.
674 */
675#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
676
677static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
678 u64 *mmap_time)
679{
680 union perf_event *event;
681 struct perf_sample sample;
682 s64 n = 0;
683 int err;
684
685 *mmap_time = ULLONG_MAX;
686 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
687 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
688 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800689 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400690 pr_err("Failed to parse sample\n");
691 return -1;
692 }
693
694 err = perf_session_queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800695 /*
696 * FIXME: Here we can't consume the event, as perf_session_queue_event will
697 * point to it, and it'll get possibly overwritten by the kernel.
698 */
699 perf_evlist__mmap_consume(kvm->evlist, idx);
700
David Ahern1afe1d12013-08-05 21:41:34 -0400701 if (err) {
702 pr_err("Failed to enqueue sample: %d\n", err);
703 return -1;
704 }
705
706 /* save time stamp of our first sample for this mmap */
707 if (n == 0)
708 *mmap_time = sample.time;
709
710 /* limit events per mmap handled all at once */
711 n++;
712 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
713 break;
714 }
715
716 return n;
717}
718
719static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
720{
721 int i, err, throttled = 0;
722 s64 n, ntotal = 0;
723 u64 flush_time = ULLONG_MAX, mmap_time;
724
725 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
726 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
727 if (n < 0)
728 return -1;
729
730 /* flush time is going to be the minimum of all the individual
731 * mmap times. Essentially, we flush all the samples queued up
732 * from the last pass under our minimal start time -- that leaves
733 * a very small race for samples to come in with a lower timestamp.
734 * The ioctl to return the perf_clock timestamp should close the
735 * race entirely.
736 */
737 if (mmap_time < flush_time)
738 flush_time = mmap_time;
739
740 ntotal += n;
741 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
742 throttled = 1;
743 }
744
745 /* flush queue after each round in which we processed events */
746 if (ntotal) {
747 kvm->session->ordered_samples.next_flush = flush_time;
748 err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
749 if (err) {
750 if (kvm->lost_events)
751 pr_info("\nLost events: %" PRIu64 "\n\n",
752 kvm->lost_events);
753 return err;
754 }
755 }
756
757 return throttled;
758}
759
760static volatile int done;
761
762static void sig_handler(int sig __maybe_unused)
763{
764 done = 1;
765}
766
767static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
768{
769 struct itimerspec new_value;
770 int rc = -1;
771
772 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
773 if (kvm->timerfd < 0) {
774 pr_err("timerfd_create failed\n");
775 goto out;
776 }
777
778 new_value.it_value.tv_sec = kvm->display_time;
779 new_value.it_value.tv_nsec = 0;
780 new_value.it_interval.tv_sec = kvm->display_time;
781 new_value.it_interval.tv_nsec = 0;
782
783 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
784 pr_err("timerfd_settime failed: %d\n", errno);
785 close(kvm->timerfd);
786 goto out;
787 }
788
789 rc = 0;
790out:
791 return rc;
792}
793
794static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
795{
796 uint64_t c;
797 int rc;
798
799 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
800 if (rc < 0) {
801 if (errno == EAGAIN)
802 return 0;
803
804 pr_err("Failed to read timer fd: %d\n", errno);
805 return -1;
806 }
807
808 if (rc != sizeof(uint64_t)) {
809 pr_err("Error reading timer fd - invalid size returned\n");
810 return -1;
811 }
812
813 if (c != 1)
814 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
815
816 /* update display */
817 sort_result(kvm);
818 print_result(kvm);
819
820 /* reset counts */
821 clear_events_cache_stats(kvm->kvm_events_cache);
822 kvm->total_count = 0;
823 kvm->total_time = 0;
824 kvm->lost_events = 0;
825
826 return 0;
827}
828
829static int fd_set_nonblock(int fd)
830{
831 long arg = 0;
832
833 arg = fcntl(fd, F_GETFL);
834 if (arg < 0) {
835 pr_err("Failed to get current flags for fd %d\n", fd);
836 return -1;
837 }
838
839 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
840 pr_err("Failed to set non-block option on fd %d\n", fd);
841 return -1;
842 }
843
844 return 0;
845}
846
847static
848int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save)
849{
850 int c;
851
852 tcsetattr(0, TCSANOW, tc_now);
853 c = getc(stdin);
854 tcsetattr(0, TCSAFLUSH, tc_save);
855
856 if (c == 'q')
857 return 1;
858
859 return 0;
860}
861
862static int kvm_events_live_report(struct perf_kvm_stat *kvm)
863{
864 struct pollfd *pollfds = NULL;
865 int nr_fds, nr_stdin, ret, err = -EINVAL;
866 struct termios tc, save;
867
868 /* live flag must be set first */
869 kvm->live = true;
870
871 ret = cpu_isa_config(kvm);
872 if (ret < 0)
873 return ret;
874
875 if (!verify_vcpu(kvm->trace_vcpu) ||
876 !select_key(kvm) ||
877 !register_kvm_events_ops(kvm)) {
878 goto out;
879 }
880
881 init_kvm_event_record(kvm);
882
883 tcgetattr(0, &save);
884 tc = save;
885 tc.c_lflag &= ~(ICANON | ECHO);
886 tc.c_cc[VMIN] = 0;
887 tc.c_cc[VTIME] = 0;
888
889 signal(SIGINT, sig_handler);
890 signal(SIGTERM, sig_handler);
891
892 /* copy pollfds -- need to add timerfd and stdin */
893 nr_fds = kvm->evlist->nr_fds;
894 pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2));
895 if (!pollfds) {
896 err = -ENOMEM;
897 goto out;
898 }
899 memcpy(pollfds, kvm->evlist->pollfd,
900 sizeof(struct pollfd) * kvm->evlist->nr_fds);
901
902 /* add timer fd */
903 if (perf_kvm__timerfd_create(kvm) < 0) {
904 err = -1;
905 goto out;
906 }
907
908 pollfds[nr_fds].fd = kvm->timerfd;
909 pollfds[nr_fds].events = POLLIN;
910 nr_fds++;
911
912 pollfds[nr_fds].fd = fileno(stdin);
913 pollfds[nr_fds].events = POLLIN;
914 nr_stdin = nr_fds;
915 nr_fds++;
916 if (fd_set_nonblock(fileno(stdin)) != 0)
917 goto out;
918
919 /* everything is good - enable the events and process */
920 perf_evlist__enable(kvm->evlist);
921
922 while (!done) {
923 int rc;
924
925 rc = perf_kvm__mmap_read(kvm);
926 if (rc < 0)
927 break;
928
929 err = perf_kvm__handle_timerfd(kvm);
930 if (err)
931 goto out;
932
933 if (pollfds[nr_stdin].revents & POLLIN)
934 done = perf_kvm__handle_stdin(&tc, &save);
935
936 if (!rc && !done)
937 err = poll(pollfds, nr_fds, 100);
938 }
939
940 perf_evlist__disable(kvm->evlist);
941
942 if (err == 0) {
943 sort_result(kvm);
944 print_result(kvm);
945 }
946
947out:
948 if (kvm->timerfd >= 0)
949 close(kvm->timerfd);
950
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300951 free(pollfds);
David Ahern1afe1d12013-08-05 21:41:34 -0400952 return err;
953}
954
955static int kvm_live_open_events(struct perf_kvm_stat *kvm)
956{
957 int err, rc = -1;
958 struct perf_evsel *pos;
959 struct perf_evlist *evlist = kvm->evlist;
960
961 perf_evlist__config(evlist, &kvm->opts);
962
963 /*
964 * Note: exclude_{guest,host} do not apply here.
965 * This command processes KVM tracepoints from host only
966 */
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300967 evlist__for_each(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -0400968 struct perf_event_attr *attr = &pos->attr;
969
970 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +0300971 perf_evsel__set_sample_bit(pos, TID);
972 perf_evsel__set_sample_bit(pos, TIME);
973 perf_evsel__set_sample_bit(pos, CPU);
974 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -0400975 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +0300976 perf_evsel__reset_sample_bit(pos, PERIOD);
977 perf_evsel__reset_sample_bit(pos, IP);
978 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
979 perf_evsel__reset_sample_bit(pos, ADDR);
980 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -0400981 attr->mmap = 0;
982 attr->comm = 0;
983 attr->task = 0;
984
985 attr->sample_period = 1;
986
987 attr->watermark = 0;
988 attr->wakeup_events = 1000;
989
990 /* will enable all once we are ready */
991 attr->disabled = 1;
992 }
993
994 err = perf_evlist__open(evlist);
995 if (err < 0) {
996 printf("Couldn't create the events: %s\n", strerror(errno));
997 goto out;
998 }
999
1000 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
1001 ui__error("Failed to mmap the events: %s\n", strerror(errno));
1002 perf_evlist__close(evlist);
1003 goto out;
1004 }
1005
1006 rc = 0;
1007
1008out:
1009 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001010}
David Ahern87419c92013-10-29 10:43:16 -06001011#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001012
Xiao Guangrong37860632012-11-15 14:17:01 +08001013static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001014{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001015 int ret;
1016
David Ahernde332ac2012-10-02 22:09:53 -06001017 struct perf_tool eops = {
1018 .sample = process_sample_event,
1019 .comm = perf_event__process_comm,
1020 .ordered_samples = true,
1021 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001022 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001023 .path = kvm->file_name,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001024 .mode = PERF_DATA_MODE_READ,
1025 };
David Ahernde332ac2012-10-02 22:09:53 -06001026
1027 kvm->tool = eops;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001028 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001029 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001030 pr_err("Initializing perf session failed\n");
1031 return -EINVAL;
1032 }
1033
David Ahernde332ac2012-10-02 22:09:53 -06001034 if (!perf_session__has_traces(kvm->session, "kvm record"))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001035 return -EINVAL;
1036
1037 /*
1038 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1039 * traced in the old kernel.
1040 */
David Ahern1afe1d12013-08-05 21:41:34 -04001041 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001042 if (ret < 0)
1043 return ret;
1044
David Ahernde332ac2012-10-02 22:09:53 -06001045 return perf_session__process_events(kvm->session, &kvm->tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001046}
1047
David Ahern2e73f002013-08-05 21:41:37 -04001048static int parse_target_str(struct perf_kvm_stat *kvm)
1049{
1050 if (kvm->pid_str) {
1051 kvm->pid_list = intlist__new(kvm->pid_str);
1052 if (kvm->pid_list == NULL) {
1053 pr_err("Error parsing process id string\n");
1054 return -EINVAL;
1055 }
1056 }
1057
1058 return 0;
1059}
1060
Xiao Guangrong37860632012-11-15 14:17:01 +08001061static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001062{
1063 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001064 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001065
David Ahern2e73f002013-08-05 21:41:37 -04001066 if (parse_target_str(kvm) != 0)
1067 goto exit;
1068
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001069 if (!verify_vcpu(vcpu))
1070 goto exit;
1071
David Ahernde332ac2012-10-02 22:09:53 -06001072 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001073 goto exit;
1074
David Ahernde332ac2012-10-02 22:09:53 -06001075 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001076 goto exit;
1077
David Ahernde332ac2012-10-02 22:09:53 -06001078 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001079 setup_pager();
1080
David Ahernde332ac2012-10-02 22:09:53 -06001081 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001082 if (ret)
1083 goto exit;
1084
David Ahernde332ac2012-10-02 22:09:53 -06001085 sort_result(kvm);
1086 print_result(kvm);
1087
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001088exit:
1089 return ret;
1090}
1091
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001092#define STRDUP_FAIL_EXIT(s) \
1093 ({ char *_p; \
1094 _p = strdup(s); \
1095 if (!_p) \
1096 return -ENOMEM; \
1097 _p; \
1098 })
1099
Xiao Guangrong37860632012-11-15 14:17:01 +08001100static int
1101kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001102{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001103 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001104 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001105 const char * const record_args[] = {
1106 "record",
1107 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001108 "-m", "1024",
1109 "-c", "1",
1110 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001111 const char * const *events_tp;
1112 events_tp_size = 0;
1113
1114 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1115 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001116
David Ahern8fdd84c2013-08-02 14:05:42 -06001117 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001118 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001119 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1120
1121 if (rec_argv == NULL)
1122 return -ENOMEM;
1123
1124 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1125 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1126
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001127 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001128 rec_argv[i++] = "-e";
1129 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1130 }
1131
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001132 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001133 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001134
1135 for (j = 1; j < (unsigned int)argc; j++, i++)
1136 rec_argv[i] = argv[j];
1137
1138 return cmd_record(i, rec_argv, NULL);
1139}
1140
Xiao Guangrong37860632012-11-15 14:17:01 +08001141static int
1142kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001143{
David Ahernde332ac2012-10-02 22:09:53 -06001144 const struct option kvm_events_report_options[] = {
1145 OPT_STRING(0, "event", &kvm->report_event, "report event",
1146 "event for reporting: vmexit, mmio, ioport"),
1147 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1148 "vcpu id to report"),
1149 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1150 "key for sorting: sample(sort by samples number)"
1151 " time (sort by avg time)"),
David Ahern2e73f002013-08-05 21:41:37 -04001152 OPT_STRING('p', "pid", &kvm->pid_str, "pid",
1153 "analyze events only for given process id(s)"),
David Ahernde332ac2012-10-02 22:09:53 -06001154 OPT_END()
1155 };
1156
1157 const char * const kvm_events_report_usage[] = {
1158 "perf kvm stat report [<options>]",
1159 NULL
1160 };
1161
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001162 symbol__init();
1163
1164 if (argc) {
1165 argc = parse_options(argc, argv,
1166 kvm_events_report_options,
1167 kvm_events_report_usage, 0);
1168 if (argc)
1169 usage_with_options(kvm_events_report_usage,
1170 kvm_events_report_options);
1171 }
1172
David Ahernde332ac2012-10-02 22:09:53 -06001173 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001174}
1175
David Ahern87419c92013-10-29 10:43:16 -06001176#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001177static struct perf_evlist *kvm_live_event_list(void)
1178{
1179 struct perf_evlist *evlist;
1180 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001181 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001182 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001183
1184 evlist = perf_evlist__new();
1185 if (evlist == NULL)
1186 return NULL;
1187
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001188 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001189
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001190 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001191 if (tp == NULL)
1192 goto out;
1193
1194 /* split tracepoint into subsystem and name */
1195 sys = tp;
1196 name = strchr(tp, ':');
1197 if (name == NULL) {
1198 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001199 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001200 free(tp);
1201 goto out;
1202 }
1203 *name = '\0';
1204 name++;
1205
1206 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001207 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001208 free(tp);
1209 goto out;
1210 }
1211
1212 free(tp);
1213 }
1214
1215 err = 0;
1216
1217out:
1218 if (err) {
1219 perf_evlist__delete(evlist);
1220 evlist = NULL;
1221 }
1222
1223 return evlist;
1224}
1225
1226static int kvm_events_live(struct perf_kvm_stat *kvm,
1227 int argc, const char **argv)
1228{
1229 char errbuf[BUFSIZ];
1230 int err;
1231
1232 const struct option live_options[] = {
1233 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1234 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001235 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1236 "number of mmap data pages",
1237 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001238 OPT_INCR('v', "verbose", &verbose,
1239 "be more verbose (show counter open errors, etc)"),
1240 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1241 "system-wide collection from all CPUs"),
1242 OPT_UINTEGER('d', "display", &kvm->display_time,
1243 "time in seconds between display updates"),
1244 OPT_STRING(0, "event", &kvm->report_event, "report event",
1245 "event for reporting: vmexit, mmio, ioport"),
1246 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1247 "vcpu id to report"),
1248 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1249 "key for sorting: sample(sort by samples number)"
1250 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001251 OPT_U64(0, "duration", &kvm->duration,
1252 "show events other than HALT that take longer than duration usecs"),
David Ahern1afe1d12013-08-05 21:41:34 -04001253 OPT_END()
1254 };
1255 const char * const live_usage[] = {
1256 "perf kvm stat live [<options>]",
1257 NULL
1258 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001259 struct perf_data_file file = {
1260 .mode = PERF_DATA_MODE_WRITE,
1261 };
David Ahern1afe1d12013-08-05 21:41:34 -04001262
1263
1264 /* event handling */
1265 kvm->tool.sample = process_sample_event;
1266 kvm->tool.comm = perf_event__process_comm;
1267 kvm->tool.exit = perf_event__process_exit;
1268 kvm->tool.fork = perf_event__process_fork;
1269 kvm->tool.lost = process_lost_event;
1270 kvm->tool.ordered_samples = true;
1271 perf_tool__fill_defaults(&kvm->tool);
1272
1273 /* set defaults */
1274 kvm->display_time = 1;
1275 kvm->opts.user_interval = 1;
1276 kvm->opts.mmap_pages = 512;
1277 kvm->opts.target.uses_mmap = false;
1278 kvm->opts.target.uid_str = NULL;
1279 kvm->opts.target.uid = UINT_MAX;
1280
1281 symbol__init();
1282 disable_buildid_cache();
1283
1284 use_browser = 0;
1285 setup_browser(false);
1286
1287 if (argc) {
1288 argc = parse_options(argc, argv, live_options,
1289 live_usage, 0);
1290 if (argc)
1291 usage_with_options(live_usage, live_options);
1292 }
1293
David Ahern70f7b4a2013-08-07 21:56:38 -04001294 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1295
David Ahern1afe1d12013-08-05 21:41:34 -04001296 /*
1297 * target related setups
1298 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001299 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001300 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001301 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001302 ui__warning("%s", errbuf);
1303 }
1304
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001305 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001306 kvm->opts.target.system_wide = true;
1307
1308
1309 /*
1310 * generate the event list
1311 */
1312 kvm->evlist = kvm_live_event_list();
1313 if (kvm->evlist == NULL) {
1314 err = -1;
1315 goto out;
1316 }
1317
1318 symbol_conf.nr_events = kvm->evlist->nr_entries;
1319
1320 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1321 usage_with_options(live_usage, live_options);
1322
1323 /*
1324 * perf session
1325 */
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001326 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001327 if (kvm->session == NULL) {
1328 err = -ENOMEM;
1329 goto out;
1330 }
1331 kvm->session->evlist = kvm->evlist;
1332 perf_session__set_id_hdr_size(kvm->session);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001333 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
1334 kvm->evlist->threads, false);
David Ahern1afe1d12013-08-05 21:41:34 -04001335 err = kvm_live_open_events(kvm);
1336 if (err)
1337 goto out;
1338
1339 err = kvm_events_live_report(kvm);
1340
1341out:
1342 exit_browser(0);
1343
1344 if (kvm->session)
1345 perf_session__delete(kvm->session);
1346 kvm->session = NULL;
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001347 if (kvm->evlist)
David Ahern1afe1d12013-08-05 21:41:34 -04001348 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001349
1350 return err;
1351}
David Ahern87419c92013-10-29 10:43:16 -06001352#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001353
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001354static void print_kvm_stat_usage(void)
1355{
1356 printf("Usage: perf kvm stat <command>\n\n");
1357
1358 printf("# Available commands:\n");
1359 printf("\trecord: record kvm events\n");
1360 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001361 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001362
1363 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1364}
1365
Xiao Guangrong37860632012-11-15 14:17:01 +08001366static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001367{
Xiao Guangrong37860632012-11-15 14:17:01 +08001368 struct perf_kvm_stat kvm = {
1369 .file_name = file_name,
1370
1371 .trace_vcpu = -1,
1372 .report_event = "vmexit",
1373 .sort_key = "sample",
1374
Xiao Guangrong37860632012-11-15 14:17:01 +08001375 };
1376
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001377 if (argc == 1) {
1378 print_kvm_stat_usage();
1379 goto perf_stat;
1380 }
1381
1382 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001383 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001384
1385 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001386 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001387
David Ahern87419c92013-10-29 10:43:16 -06001388#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001389 if (!strncmp(argv[1], "live", 4))
1390 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001391#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001392
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001393perf_stat:
1394 return cmd_stat(argc, argv, NULL);
1395}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001396#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001397
Xiao Guangrong37860632012-11-15 14:17:01 +08001398static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001399{
1400 int rec_argc, i = 0, j;
1401 const char **rec_argv;
1402
1403 rec_argc = argc + 2;
1404 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1405 rec_argv[i++] = strdup("record");
1406 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001407 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001408 for (j = 1; j < argc; j++, i++)
1409 rec_argv[i] = argv[j];
1410
1411 BUG_ON(i != rec_argc);
1412
1413 return cmd_record(i, rec_argv, NULL);
1414}
1415
Xiao Guangrong37860632012-11-15 14:17:01 +08001416static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001417{
1418 int rec_argc, i = 0, j;
1419 const char **rec_argv;
1420
1421 rec_argc = argc + 2;
1422 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1423 rec_argv[i++] = strdup("report");
1424 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001425 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001426 for (j = 1; j < argc; j++, i++)
1427 rec_argv[i] = argv[j];
1428
1429 BUG_ON(i != rec_argc);
1430
1431 return cmd_report(i, rec_argv, NULL);
1432}
1433
Xiao Guangrong37860632012-11-15 14:17:01 +08001434static int
1435__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001436{
1437 int rec_argc, i = 0, j;
1438 const char **rec_argv;
1439
1440 rec_argc = argc + 2;
1441 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1442 rec_argv[i++] = strdup("buildid-list");
1443 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001444 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001445 for (j = 1; j < argc; j++, i++)
1446 rec_argv[i] = argv[j];
1447
1448 BUG_ON(i != rec_argc);
1449
1450 return cmd_buildid_list(i, rec_argv, NULL);
1451}
1452
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001453int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001454{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001455 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001456 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001457 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001458 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001459 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001460 "Output file name"),
1461 OPT_BOOLEAN(0, "guest", &perf_guest,
1462 "Collect guest os data"),
1463 OPT_BOOLEAN(0, "host", &perf_host,
1464 "Collect host os data"),
1465 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1466 "guest mount directory under which every guest os"
1467 " instance has a subdir"),
1468 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1469 "file", "file saving guest os vmlinux"),
1470 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1471 "file", "file saving guest os /proc/kallsyms"),
1472 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1473 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001474 OPT_INCR('v', "verbose", &verbose,
1475 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001476 OPT_END()
1477 };
1478
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001479 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1480 "buildid-list", "stat", NULL };
1481 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001482
Joerg Roedel1aed2672012-01-04 17:54:20 +01001483 perf_host = 0;
1484 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001485
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001486 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1487 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001488 if (!argc)
1489 usage_with_options(kvm_usage, kvm_options);
1490
1491 if (!perf_host)
1492 perf_guest = 1;
1493
Xiao Guangrong37860632012-11-15 14:17:01 +08001494 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001495 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001496
Xiao Guangrong37860632012-11-15 14:17:01 +08001497 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001498 pr_err("Failed to allocate memory for filename\n");
1499 return -ENOMEM;
1500 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001501 }
1502
1503 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001504 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001505 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001506 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001507 else if (!strncmp(argv[0], "diff", 4))
1508 return cmd_diff(argc, argv, NULL);
1509 else if (!strncmp(argv[0], "top", 3))
1510 return cmd_top(argc, argv, NULL);
1511 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001512 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001513#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001514 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001515 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001516#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001517 else
1518 usage_with_options(kvm_usage, kvm_options);
1519
1520 return 0;
1521}