blob: 75ee8c1a6baf2255a8c167f8bb6b35891ba41c33 [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
Xiao Guangrong37860632012-11-15 14:17:01 +0800264static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600265 struct vcpu_event_record *vcpu_record,
266 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400267 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800268{
269 struct kvm_event *event;
270 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600271 int vcpu;
272
273 if (kvm->trace_vcpu == -1)
274 vcpu = -1;
275 else
276 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800277
278 event = vcpu_record->last_event;
279 time_begin = vcpu_record->start_time;
280
281 /* The begin event is not caught. */
282 if (!time_begin)
283 return true;
284
285 /*
286 * In some case, the 'begin event' only records the start timestamp,
287 * the actual event is recognized in the 'end event' (e.g. mmio-event).
288 */
289
290 /* Both begin and end events did not get the key. */
291 if (!event && key->key == INVALID_KEY)
292 return true;
293
294 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600295 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800296
297 if (!event)
298 return false;
299
300 vcpu_record->last_event = NULL;
301 vcpu_record->start_time = 0;
302
David Ahern1afe1d12013-08-05 21:41:34 -0400303 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400304 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400305 pr_debug("End time before begin time; skipping event.\n");
306 return true;
307 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800308
David Ahern70f7b4a2013-08-07 21:56:38 -0400309 time_diff = sample->time - time_begin;
310
311 if (kvm->duration && time_diff > kvm->duration) {
Alexander Yarygin44b38022014-07-03 18:29:04 +0400312 char decode[DECODE_STR_LEN];
David Ahern70f7b4a2013-08-07 21:56:38 -0400313
314 kvm->events_ops->decode_key(kvm, &event->key, decode);
315 if (strcmp(decode, "HLT")) {
316 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
317 sample->time, sample->pid, vcpu_record->vcpu_id,
318 decode, time_diff/1000);
319 }
320 }
321
David Ahern2aa8eab2012-10-08 11:17:35 -0600322 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800323}
324
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300325static
326struct vcpu_event_record *per_vcpu_record(struct thread *thread,
327 struct perf_evsel *evsel,
328 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800329{
330 /* Only kvm_entry records vcpu id. */
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300331 if (!thread->priv && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800332 struct vcpu_event_record *vcpu_record;
333
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300334 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800335 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300336 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800337 return NULL;
338 }
339
Alexander Yarygin44b38022014-07-03 18:29:04 +0400340 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, VCPU_ID);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800341 thread->priv = vcpu_record;
342 }
343
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300344 return thread->priv;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800345}
346
Xiao Guangrong37860632012-11-15 14:17:01 +0800347static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600348 struct thread *thread,
349 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300350 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800351{
352 struct vcpu_event_record *vcpu_record;
353 struct event_key key = {.key = INVALID_KEY};
354
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300355 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800356 if (!vcpu_record)
357 return true;
358
David Ahern2aa8eab2012-10-08 11:17:35 -0600359 /* only process events for vcpus user cares about */
360 if ((kvm->trace_vcpu != -1) &&
361 (kvm->trace_vcpu != vcpu_record->vcpu_id))
362 return true;
363
David Ahernde332ac2012-10-02 22:09:53 -0600364 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
365 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800366
David Ahernde332ac2012-10-02 22:09:53 -0600367 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400368 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800369
370 return true;
371}
372
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800373#define GET_EVENT_KEY(func, field) \
374static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
375{ \
376 if (vcpu == -1) \
377 return event->total.field; \
378 \
379 if (vcpu >= event->max_vcpu) \
380 return 0; \
381 \
382 return event->vcpu[vcpu].field; \
383}
384
385#define COMPARE_EVENT_KEY(func, field) \
386GET_EVENT_KEY(func, field) \
387static int compare_kvm_event_ ## func(struct kvm_event *one, \
388 struct kvm_event *two, int vcpu)\
389{ \
390 return get_event_ ##func(one, vcpu) > \
391 get_event_ ##func(two, vcpu); \
392}
393
394GET_EVENT_KEY(time, time);
395COMPARE_EVENT_KEY(count, stats.n);
396COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400397GET_EVENT_KEY(max, stats.max);
398GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800399
400#define DEF_SORT_NAME_KEY(name, compare_key) \
401 { #name, compare_kvm_event_ ## compare_key }
402
403static struct kvm_event_key keys[] = {
404 DEF_SORT_NAME_KEY(sample, count),
405 DEF_SORT_NAME_KEY(time, mean),
406 { NULL, NULL }
407};
408
Xiao Guangrong37860632012-11-15 14:17:01 +0800409static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800410{
411 int i;
412
413 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600414 if (!strcmp(keys[i].name, kvm->sort_key)) {
415 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800416 return true;
417 }
418 }
419
David Ahernde332ac2012-10-02 22:09:53 -0600420 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800421 return false;
422}
423
David Ahernde332ac2012-10-02 22:09:53 -0600424static void insert_to_result(struct rb_root *result, struct kvm_event *event,
425 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800426{
David Ahernde332ac2012-10-02 22:09:53 -0600427 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800428 struct rb_node *parent = NULL;
429 struct kvm_event *p;
430
431 while (*rb) {
432 p = container_of(*rb, struct kvm_event, rb);
433 parent = *rb;
434
435 if (bigger(event, p, vcpu))
436 rb = &(*rb)->rb_left;
437 else
438 rb = &(*rb)->rb_right;
439 }
440
441 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600442 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800443}
444
Xiao Guangrong37860632012-11-15 14:17:01 +0800445static void
446update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800447{
David Ahernde332ac2012-10-02 22:09:53 -0600448 int vcpu = kvm->trace_vcpu;
449
450 kvm->total_count += get_event_count(event, vcpu);
451 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800452}
453
454static bool event_is_valid(struct kvm_event *event, int vcpu)
455{
456 return !!get_event_count(event, vcpu);
457}
458
Xiao Guangrong37860632012-11-15 14:17:01 +0800459static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800460{
461 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600462 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800463 struct kvm_event *event;
464
David Ahern355afe82012-10-08 11:17:32 -0600465 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
466 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800467 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600468 update_total_count(kvm, event);
469 insert_to_result(&kvm->result, event,
470 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800471 }
David Ahern355afe82012-10-08 11:17:32 -0600472 }
473 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800474}
475
476/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600477static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800478{
David Ahernde332ac2012-10-02 22:09:53 -0600479 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800480
481 if (!node)
482 return NULL;
483
David Ahernde332ac2012-10-02 22:09:53 -0600484 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800485 return container_of(node, struct kvm_event, rb);
486}
487
David Ahern1afe1d12013-08-05 21:41:34 -0400488static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800489{
David Ahern1afe1d12013-08-05 21:41:34 -0400490 int vcpu = kvm->trace_vcpu;
491
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800492 pr_info("Analyze events for ");
493
David Ahern1afe1d12013-08-05 21:41:34 -0400494 if (kvm->live) {
495 if (kvm->opts.target.system_wide)
496 pr_info("all VMs, ");
497 else if (kvm->opts.target.pid)
498 pr_info("pid(s) %s, ", kvm->opts.target.pid);
499 else
500 pr_info("dazed and confused on what is monitored, ");
501 }
502
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800503 if (vcpu == -1)
504 pr_info("all VCPUs:\n\n");
505 else
506 pr_info("VCPU %d:\n\n", vcpu);
507}
508
David Ahern1afe1d12013-08-05 21:41:34 -0400509static void show_timeofday(void)
510{
511 char date[64];
512 struct timeval tv;
513 struct tm ltime;
514
515 gettimeofday(&tv, NULL);
516 if (localtime_r(&tv.tv_sec, &ltime)) {
517 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
518 pr_info("%s.%06ld", date, tv.tv_usec);
519 } else
520 pr_info("00:00:00.000000");
521
522 return;
523}
524
Xiao Guangrong37860632012-11-15 14:17:01 +0800525static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800526{
Alexander Yarygin44b38022014-07-03 18:29:04 +0400527 char decode[DECODE_STR_LEN];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800528 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600529 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800530
David Ahern1afe1d12013-08-05 21:41:34 -0400531 if (kvm->live) {
532 puts(CONSOLE_CLEAR);
533 show_timeofday();
534 }
535
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800536 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400537 print_vcpu_info(kvm);
Alexander Yarygin44b38022014-07-03 18:29:04 +0400538 pr_info("%*s ", DECODE_STR_LEN, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800539 pr_info("%10s ", "Samples");
540 pr_info("%9s ", "Samples%");
541
542 pr_info("%9s ", "Time%");
David Ahern62d04db2013-08-05 21:41:35 -0400543 pr_info("%10s ", "Min Time");
544 pr_info("%10s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800545 pr_info("%16s ", "Avg time");
546 pr_info("\n\n");
547
David Ahernde332ac2012-10-02 22:09:53 -0600548 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400549 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800550
551 ecount = get_event_count(event, vcpu);
552 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400553 max = get_event_max(event, vcpu);
554 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800555
David Ahernde332ac2012-10-02 22:09:53 -0600556 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin44b38022014-07-03 18:29:04 +0400557 pr_info("%*s ", DECODE_STR_LEN, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800558 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600559 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
560 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
David Ahern62d04db2013-08-05 21:41:35 -0400561 pr_info("%8" PRIu64 "us ", min / 1000);
562 pr_info("%8" PRIu64 "us ", max / 1000);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800563 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
564 kvm_event_rel_stddev(vcpu, event));
565 pr_info("\n");
566 }
567
David Aherne4f76372012-10-08 11:17:34 -0600568 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
569 kvm->total_count, kvm->total_time / 1e3);
David Ahern1afe1d12013-08-05 21:41:34 -0400570
571 if (kvm->lost_events)
572 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
573}
574
David Ahern87419c92013-10-29 10:43:16 -0600575#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400576static int process_lost_event(struct perf_tool *tool,
577 union perf_event *event __maybe_unused,
578 struct perf_sample *sample __maybe_unused,
579 struct machine *machine __maybe_unused)
580{
581 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
582
583 kvm->lost_events++;
584 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800585}
David Ahern87419c92013-10-29 10:43:16 -0600586#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800587
David Ahern2e73f002013-08-05 21:41:37 -0400588static bool skip_sample(struct perf_kvm_stat *kvm,
589 struct perf_sample *sample)
590{
591 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
592 return true;
593
594 return false;
595}
596
David Ahernde332ac2012-10-02 22:09:53 -0600597static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800598 union perf_event *event,
599 struct perf_sample *sample,
600 struct perf_evsel *evsel,
601 struct machine *machine)
602{
David Ahern2e73f002013-08-05 21:41:37 -0400603 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800604 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
605 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800606
David Ahern2e73f002013-08-05 21:41:37 -0400607 if (skip_sample(kvm, sample))
608 return 0;
609
Adrian Hunter314add62013-08-27 11:23:03 +0300610 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800611 if (thread == NULL) {
612 pr_debug("problem processing %d event, skipping it.\n",
613 event->header.type);
614 return -1;
615 }
616
David Ahernde332ac2012-10-02 22:09:53 -0600617 if (!handle_kvm_event(kvm, thread, evsel, sample))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800618 return -1;
619
620 return 0;
621}
622
David Ahern1afe1d12013-08-05 21:41:34 -0400623static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800624{
David Ahern1afe1d12013-08-05 21:41:34 -0400625 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400626 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400627
628 if (kvm->live) {
629 err = get_cpuid(buf, sizeof(buf));
630 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400631 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400632 return err;
633 }
634 cpuid = buf;
635 } else
636 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800637
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400638 if (!cpuid) {
639 pr_err("Failed to look up CPU type\n");
640 return -EINVAL;
641 }
642
643 err = cpu_isa_init(kvm, cpuid);
644 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800645 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800646
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400647 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400648}
649
650static bool verify_vcpu(int vcpu)
651{
652 if (vcpu != -1 && vcpu < 0) {
653 pr_err("Invalid vcpu:%d.\n", vcpu);
654 return false;
655 }
656
657 return true;
658}
659
David Ahern87419c92013-10-29 10:43:16 -0600660#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400661/* keeping the max events to a modest level to keep
662 * the processing of samples per mmap smooth.
663 */
664#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
665
666static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
667 u64 *mmap_time)
668{
669 union perf_event *event;
670 struct perf_sample sample;
671 s64 n = 0;
672 int err;
673
674 *mmap_time = ULLONG_MAX;
675 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
676 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
677 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800678 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400679 pr_err("Failed to parse sample\n");
680 return -1;
681 }
682
683 err = perf_session_queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800684 /*
685 * FIXME: Here we can't consume the event, as perf_session_queue_event will
686 * point to it, and it'll get possibly overwritten by the kernel.
687 */
688 perf_evlist__mmap_consume(kvm->evlist, idx);
689
David Ahern1afe1d12013-08-05 21:41:34 -0400690 if (err) {
691 pr_err("Failed to enqueue sample: %d\n", err);
692 return -1;
693 }
694
695 /* save time stamp of our first sample for this mmap */
696 if (n == 0)
697 *mmap_time = sample.time;
698
699 /* limit events per mmap handled all at once */
700 n++;
701 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
702 break;
703 }
704
705 return n;
706}
707
708static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
709{
710 int i, err, throttled = 0;
711 s64 n, ntotal = 0;
712 u64 flush_time = ULLONG_MAX, mmap_time;
713
714 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
715 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
716 if (n < 0)
717 return -1;
718
719 /* flush time is going to be the minimum of all the individual
720 * mmap times. Essentially, we flush all the samples queued up
721 * from the last pass under our minimal start time -- that leaves
722 * a very small race for samples to come in with a lower timestamp.
723 * The ioctl to return the perf_clock timestamp should close the
724 * race entirely.
725 */
726 if (mmap_time < flush_time)
727 flush_time = mmap_time;
728
729 ntotal += n;
730 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
731 throttled = 1;
732 }
733
734 /* flush queue after each round in which we processed events */
735 if (ntotal) {
736 kvm->session->ordered_samples.next_flush = flush_time;
737 err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
738 if (err) {
739 if (kvm->lost_events)
740 pr_info("\nLost events: %" PRIu64 "\n\n",
741 kvm->lost_events);
742 return err;
743 }
744 }
745
746 return throttled;
747}
748
749static volatile int done;
750
751static void sig_handler(int sig __maybe_unused)
752{
753 done = 1;
754}
755
756static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
757{
758 struct itimerspec new_value;
759 int rc = -1;
760
761 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
762 if (kvm->timerfd < 0) {
763 pr_err("timerfd_create failed\n");
764 goto out;
765 }
766
767 new_value.it_value.tv_sec = kvm->display_time;
768 new_value.it_value.tv_nsec = 0;
769 new_value.it_interval.tv_sec = kvm->display_time;
770 new_value.it_interval.tv_nsec = 0;
771
772 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
773 pr_err("timerfd_settime failed: %d\n", errno);
774 close(kvm->timerfd);
775 goto out;
776 }
777
778 rc = 0;
779out:
780 return rc;
781}
782
783static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
784{
785 uint64_t c;
786 int rc;
787
788 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
789 if (rc < 0) {
790 if (errno == EAGAIN)
791 return 0;
792
793 pr_err("Failed to read timer fd: %d\n", errno);
794 return -1;
795 }
796
797 if (rc != sizeof(uint64_t)) {
798 pr_err("Error reading timer fd - invalid size returned\n");
799 return -1;
800 }
801
802 if (c != 1)
803 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
804
805 /* update display */
806 sort_result(kvm);
807 print_result(kvm);
808
809 /* reset counts */
810 clear_events_cache_stats(kvm->kvm_events_cache);
811 kvm->total_count = 0;
812 kvm->total_time = 0;
813 kvm->lost_events = 0;
814
815 return 0;
816}
817
818static int fd_set_nonblock(int fd)
819{
820 long arg = 0;
821
822 arg = fcntl(fd, F_GETFL);
823 if (arg < 0) {
824 pr_err("Failed to get current flags for fd %d\n", fd);
825 return -1;
826 }
827
828 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
829 pr_err("Failed to set non-block option on fd %d\n", fd);
830 return -1;
831 }
832
833 return 0;
834}
835
836static
837int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save)
838{
839 int c;
840
841 tcsetattr(0, TCSANOW, tc_now);
842 c = getc(stdin);
843 tcsetattr(0, TCSAFLUSH, tc_save);
844
845 if (c == 'q')
846 return 1;
847
848 return 0;
849}
850
851static int kvm_events_live_report(struct perf_kvm_stat *kvm)
852{
853 struct pollfd *pollfds = NULL;
854 int nr_fds, nr_stdin, ret, err = -EINVAL;
855 struct termios tc, save;
856
857 /* live flag must be set first */
858 kvm->live = true;
859
860 ret = cpu_isa_config(kvm);
861 if (ret < 0)
862 return ret;
863
864 if (!verify_vcpu(kvm->trace_vcpu) ||
865 !select_key(kvm) ||
866 !register_kvm_events_ops(kvm)) {
867 goto out;
868 }
869
870 init_kvm_event_record(kvm);
871
872 tcgetattr(0, &save);
873 tc = save;
874 tc.c_lflag &= ~(ICANON | ECHO);
875 tc.c_cc[VMIN] = 0;
876 tc.c_cc[VTIME] = 0;
877
878 signal(SIGINT, sig_handler);
879 signal(SIGTERM, sig_handler);
880
881 /* copy pollfds -- need to add timerfd and stdin */
882 nr_fds = kvm->evlist->nr_fds;
883 pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2));
884 if (!pollfds) {
885 err = -ENOMEM;
886 goto out;
887 }
888 memcpy(pollfds, kvm->evlist->pollfd,
889 sizeof(struct pollfd) * kvm->evlist->nr_fds);
890
891 /* add timer fd */
892 if (perf_kvm__timerfd_create(kvm) < 0) {
893 err = -1;
894 goto out;
895 }
896
897 pollfds[nr_fds].fd = kvm->timerfd;
898 pollfds[nr_fds].events = POLLIN;
899 nr_fds++;
900
901 pollfds[nr_fds].fd = fileno(stdin);
902 pollfds[nr_fds].events = POLLIN;
903 nr_stdin = nr_fds;
904 nr_fds++;
905 if (fd_set_nonblock(fileno(stdin)) != 0)
906 goto out;
907
908 /* everything is good - enable the events and process */
909 perf_evlist__enable(kvm->evlist);
910
911 while (!done) {
912 int rc;
913
914 rc = perf_kvm__mmap_read(kvm);
915 if (rc < 0)
916 break;
917
918 err = perf_kvm__handle_timerfd(kvm);
919 if (err)
920 goto out;
921
922 if (pollfds[nr_stdin].revents & POLLIN)
923 done = perf_kvm__handle_stdin(&tc, &save);
924
925 if (!rc && !done)
926 err = poll(pollfds, nr_fds, 100);
927 }
928
929 perf_evlist__disable(kvm->evlist);
930
931 if (err == 0) {
932 sort_result(kvm);
933 print_result(kvm);
934 }
935
936out:
937 if (kvm->timerfd >= 0)
938 close(kvm->timerfd);
939
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300940 free(pollfds);
David Ahern1afe1d12013-08-05 21:41:34 -0400941 return err;
942}
943
944static int kvm_live_open_events(struct perf_kvm_stat *kvm)
945{
946 int err, rc = -1;
947 struct perf_evsel *pos;
948 struct perf_evlist *evlist = kvm->evlist;
949
950 perf_evlist__config(evlist, &kvm->opts);
951
952 /*
953 * Note: exclude_{guest,host} do not apply here.
954 * This command processes KVM tracepoints from host only
955 */
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300956 evlist__for_each(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -0400957 struct perf_event_attr *attr = &pos->attr;
958
959 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +0300960 perf_evsel__set_sample_bit(pos, TID);
961 perf_evsel__set_sample_bit(pos, TIME);
962 perf_evsel__set_sample_bit(pos, CPU);
963 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -0400964 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +0300965 perf_evsel__reset_sample_bit(pos, PERIOD);
966 perf_evsel__reset_sample_bit(pos, IP);
967 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
968 perf_evsel__reset_sample_bit(pos, ADDR);
969 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -0400970 attr->mmap = 0;
971 attr->comm = 0;
972 attr->task = 0;
973
974 attr->sample_period = 1;
975
976 attr->watermark = 0;
977 attr->wakeup_events = 1000;
978
979 /* will enable all once we are ready */
980 attr->disabled = 1;
981 }
982
983 err = perf_evlist__open(evlist);
984 if (err < 0) {
985 printf("Couldn't create the events: %s\n", strerror(errno));
986 goto out;
987 }
988
989 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
990 ui__error("Failed to mmap the events: %s\n", strerror(errno));
991 perf_evlist__close(evlist);
992 goto out;
993 }
994
995 rc = 0;
996
997out:
998 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800999}
David Ahern87419c92013-10-29 10:43:16 -06001000#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001001
Xiao Guangrong37860632012-11-15 14:17:01 +08001002static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001003{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001004 int ret;
1005
David Ahernde332ac2012-10-02 22:09:53 -06001006 struct perf_tool eops = {
1007 .sample = process_sample_event,
1008 .comm = perf_event__process_comm,
1009 .ordered_samples = true,
1010 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001011 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001012 .path = kvm->file_name,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001013 .mode = PERF_DATA_MODE_READ,
1014 };
David Ahernde332ac2012-10-02 22:09:53 -06001015
1016 kvm->tool = eops;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001017 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001018 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001019 pr_err("Initializing perf session failed\n");
1020 return -EINVAL;
1021 }
1022
David Ahernde332ac2012-10-02 22:09:53 -06001023 if (!perf_session__has_traces(kvm->session, "kvm record"))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001024 return -EINVAL;
1025
1026 /*
1027 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1028 * traced in the old kernel.
1029 */
David Ahern1afe1d12013-08-05 21:41:34 -04001030 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001031 if (ret < 0)
1032 return ret;
1033
David Ahernde332ac2012-10-02 22:09:53 -06001034 return perf_session__process_events(kvm->session, &kvm->tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001035}
1036
David Ahern2e73f002013-08-05 21:41:37 -04001037static int parse_target_str(struct perf_kvm_stat *kvm)
1038{
1039 if (kvm->pid_str) {
1040 kvm->pid_list = intlist__new(kvm->pid_str);
1041 if (kvm->pid_list == NULL) {
1042 pr_err("Error parsing process id string\n");
1043 return -EINVAL;
1044 }
1045 }
1046
1047 return 0;
1048}
1049
Xiao Guangrong37860632012-11-15 14:17:01 +08001050static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001051{
1052 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001053 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001054
David Ahern2e73f002013-08-05 21:41:37 -04001055 if (parse_target_str(kvm) != 0)
1056 goto exit;
1057
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001058 if (!verify_vcpu(vcpu))
1059 goto exit;
1060
David Ahernde332ac2012-10-02 22:09:53 -06001061 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001062 goto exit;
1063
David Ahernde332ac2012-10-02 22:09:53 -06001064 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001065 goto exit;
1066
David Ahernde332ac2012-10-02 22:09:53 -06001067 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001068 setup_pager();
1069
David Ahernde332ac2012-10-02 22:09:53 -06001070 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001071 if (ret)
1072 goto exit;
1073
David Ahernde332ac2012-10-02 22:09:53 -06001074 sort_result(kvm);
1075 print_result(kvm);
1076
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001077exit:
1078 return ret;
1079}
1080
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001081#define STRDUP_FAIL_EXIT(s) \
1082 ({ char *_p; \
1083 _p = strdup(s); \
1084 if (!_p) \
1085 return -ENOMEM; \
1086 _p; \
1087 })
1088
Xiao Guangrong37860632012-11-15 14:17:01 +08001089static int
1090kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001091{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001092 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001093 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001094 const char * const record_args[] = {
1095 "record",
1096 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001097 "-m", "1024",
1098 "-c", "1",
1099 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001100 const char * const *events_tp;
1101 events_tp_size = 0;
1102
1103 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1104 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001105
David Ahern8fdd84c2013-08-02 14:05:42 -06001106 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001107 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001108 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1109
1110 if (rec_argv == NULL)
1111 return -ENOMEM;
1112
1113 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1114 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1115
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001116 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001117 rec_argv[i++] = "-e";
1118 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1119 }
1120
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001121 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001122 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001123
1124 for (j = 1; j < (unsigned int)argc; j++, i++)
1125 rec_argv[i] = argv[j];
1126
1127 return cmd_record(i, rec_argv, NULL);
1128}
1129
Xiao Guangrong37860632012-11-15 14:17:01 +08001130static int
1131kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001132{
David Ahernde332ac2012-10-02 22:09:53 -06001133 const struct option kvm_events_report_options[] = {
1134 OPT_STRING(0, "event", &kvm->report_event, "report event",
1135 "event for reporting: vmexit, mmio, ioport"),
1136 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1137 "vcpu id to report"),
1138 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1139 "key for sorting: sample(sort by samples number)"
1140 " time (sort by avg time)"),
David Ahern2e73f002013-08-05 21:41:37 -04001141 OPT_STRING('p', "pid", &kvm->pid_str, "pid",
1142 "analyze events only for given process id(s)"),
David Ahernde332ac2012-10-02 22:09:53 -06001143 OPT_END()
1144 };
1145
1146 const char * const kvm_events_report_usage[] = {
1147 "perf kvm stat report [<options>]",
1148 NULL
1149 };
1150
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001151 symbol__init();
1152
1153 if (argc) {
1154 argc = parse_options(argc, argv,
1155 kvm_events_report_options,
1156 kvm_events_report_usage, 0);
1157 if (argc)
1158 usage_with_options(kvm_events_report_usage,
1159 kvm_events_report_options);
1160 }
1161
David Ahernde332ac2012-10-02 22:09:53 -06001162 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001163}
1164
David Ahern87419c92013-10-29 10:43:16 -06001165#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001166static struct perf_evlist *kvm_live_event_list(void)
1167{
1168 struct perf_evlist *evlist;
1169 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001170 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001171 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001172
1173 evlist = perf_evlist__new();
1174 if (evlist == NULL)
1175 return NULL;
1176
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001177 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001178
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001179 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001180 if (tp == NULL)
1181 goto out;
1182
1183 /* split tracepoint into subsystem and name */
1184 sys = tp;
1185 name = strchr(tp, ':');
1186 if (name == NULL) {
1187 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001188 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001189 free(tp);
1190 goto out;
1191 }
1192 *name = '\0';
1193 name++;
1194
1195 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001196 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001197 free(tp);
1198 goto out;
1199 }
1200
1201 free(tp);
1202 }
1203
1204 err = 0;
1205
1206out:
1207 if (err) {
1208 perf_evlist__delete(evlist);
1209 evlist = NULL;
1210 }
1211
1212 return evlist;
1213}
1214
1215static int kvm_events_live(struct perf_kvm_stat *kvm,
1216 int argc, const char **argv)
1217{
1218 char errbuf[BUFSIZ];
1219 int err;
1220
1221 const struct option live_options[] = {
1222 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1223 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001224 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1225 "number of mmap data pages",
1226 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001227 OPT_INCR('v', "verbose", &verbose,
1228 "be more verbose (show counter open errors, etc)"),
1229 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1230 "system-wide collection from all CPUs"),
1231 OPT_UINTEGER('d', "display", &kvm->display_time,
1232 "time in seconds between display updates"),
1233 OPT_STRING(0, "event", &kvm->report_event, "report event",
1234 "event for reporting: vmexit, mmio, ioport"),
1235 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1236 "vcpu id to report"),
1237 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1238 "key for sorting: sample(sort by samples number)"
1239 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001240 OPT_U64(0, "duration", &kvm->duration,
1241 "show events other than HALT that take longer than duration usecs"),
David Ahern1afe1d12013-08-05 21:41:34 -04001242 OPT_END()
1243 };
1244 const char * const live_usage[] = {
1245 "perf kvm stat live [<options>]",
1246 NULL
1247 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001248 struct perf_data_file file = {
1249 .mode = PERF_DATA_MODE_WRITE,
1250 };
David Ahern1afe1d12013-08-05 21:41:34 -04001251
1252
1253 /* event handling */
1254 kvm->tool.sample = process_sample_event;
1255 kvm->tool.comm = perf_event__process_comm;
1256 kvm->tool.exit = perf_event__process_exit;
1257 kvm->tool.fork = perf_event__process_fork;
1258 kvm->tool.lost = process_lost_event;
1259 kvm->tool.ordered_samples = true;
1260 perf_tool__fill_defaults(&kvm->tool);
1261
1262 /* set defaults */
1263 kvm->display_time = 1;
1264 kvm->opts.user_interval = 1;
1265 kvm->opts.mmap_pages = 512;
1266 kvm->opts.target.uses_mmap = false;
1267 kvm->opts.target.uid_str = NULL;
1268 kvm->opts.target.uid = UINT_MAX;
1269
1270 symbol__init();
1271 disable_buildid_cache();
1272
1273 use_browser = 0;
1274 setup_browser(false);
1275
1276 if (argc) {
1277 argc = parse_options(argc, argv, live_options,
1278 live_usage, 0);
1279 if (argc)
1280 usage_with_options(live_usage, live_options);
1281 }
1282
David Ahern70f7b4a2013-08-07 21:56:38 -04001283 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1284
David Ahern1afe1d12013-08-05 21:41:34 -04001285 /*
1286 * target related setups
1287 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001288 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001289 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001290 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001291 ui__warning("%s", errbuf);
1292 }
1293
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001294 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001295 kvm->opts.target.system_wide = true;
1296
1297
1298 /*
1299 * generate the event list
1300 */
1301 kvm->evlist = kvm_live_event_list();
1302 if (kvm->evlist == NULL) {
1303 err = -1;
1304 goto out;
1305 }
1306
1307 symbol_conf.nr_events = kvm->evlist->nr_entries;
1308
1309 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1310 usage_with_options(live_usage, live_options);
1311
1312 /*
1313 * perf session
1314 */
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001315 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001316 if (kvm->session == NULL) {
1317 err = -ENOMEM;
1318 goto out;
1319 }
1320 kvm->session->evlist = kvm->evlist;
1321 perf_session__set_id_hdr_size(kvm->session);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001322 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
1323 kvm->evlist->threads, false);
David Ahern1afe1d12013-08-05 21:41:34 -04001324 err = kvm_live_open_events(kvm);
1325 if (err)
1326 goto out;
1327
1328 err = kvm_events_live_report(kvm);
1329
1330out:
1331 exit_browser(0);
1332
1333 if (kvm->session)
1334 perf_session__delete(kvm->session);
1335 kvm->session = NULL;
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001336 if (kvm->evlist)
David Ahern1afe1d12013-08-05 21:41:34 -04001337 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001338
1339 return err;
1340}
David Ahern87419c92013-10-29 10:43:16 -06001341#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001342
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001343static void print_kvm_stat_usage(void)
1344{
1345 printf("Usage: perf kvm stat <command>\n\n");
1346
1347 printf("# Available commands:\n");
1348 printf("\trecord: record kvm events\n");
1349 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001350 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001351
1352 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1353}
1354
Xiao Guangrong37860632012-11-15 14:17:01 +08001355static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001356{
Xiao Guangrong37860632012-11-15 14:17:01 +08001357 struct perf_kvm_stat kvm = {
1358 .file_name = file_name,
1359
1360 .trace_vcpu = -1,
1361 .report_event = "vmexit",
1362 .sort_key = "sample",
1363
Xiao Guangrong37860632012-11-15 14:17:01 +08001364 };
1365
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001366 if (argc == 1) {
1367 print_kvm_stat_usage();
1368 goto perf_stat;
1369 }
1370
1371 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001372 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001373
1374 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001375 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001376
David Ahern87419c92013-10-29 10:43:16 -06001377#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001378 if (!strncmp(argv[1], "live", 4))
1379 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001380#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001381
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001382perf_stat:
1383 return cmd_stat(argc, argv, NULL);
1384}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001385#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001386
Xiao Guangrong37860632012-11-15 14:17:01 +08001387static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001388{
1389 int rec_argc, i = 0, j;
1390 const char **rec_argv;
1391
1392 rec_argc = argc + 2;
1393 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1394 rec_argv[i++] = strdup("record");
1395 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001396 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001397 for (j = 1; j < argc; j++, i++)
1398 rec_argv[i] = argv[j];
1399
1400 BUG_ON(i != rec_argc);
1401
1402 return cmd_record(i, rec_argv, NULL);
1403}
1404
Xiao Guangrong37860632012-11-15 14:17:01 +08001405static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001406{
1407 int rec_argc, i = 0, j;
1408 const char **rec_argv;
1409
1410 rec_argc = argc + 2;
1411 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1412 rec_argv[i++] = strdup("report");
1413 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001414 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001415 for (j = 1; j < argc; j++, i++)
1416 rec_argv[i] = argv[j];
1417
1418 BUG_ON(i != rec_argc);
1419
1420 return cmd_report(i, rec_argv, NULL);
1421}
1422
Xiao Guangrong37860632012-11-15 14:17:01 +08001423static int
1424__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001425{
1426 int rec_argc, i = 0, j;
1427 const char **rec_argv;
1428
1429 rec_argc = argc + 2;
1430 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1431 rec_argv[i++] = strdup("buildid-list");
1432 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001433 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001434 for (j = 1; j < argc; j++, i++)
1435 rec_argv[i] = argv[j];
1436
1437 BUG_ON(i != rec_argc);
1438
1439 return cmd_buildid_list(i, rec_argv, NULL);
1440}
1441
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001442int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001443{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001444 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001445 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001446 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001447 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001448 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001449 "Output file name"),
1450 OPT_BOOLEAN(0, "guest", &perf_guest,
1451 "Collect guest os data"),
1452 OPT_BOOLEAN(0, "host", &perf_host,
1453 "Collect host os data"),
1454 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1455 "guest mount directory under which every guest os"
1456 " instance has a subdir"),
1457 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1458 "file", "file saving guest os vmlinux"),
1459 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1460 "file", "file saving guest os /proc/kallsyms"),
1461 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1462 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001463 OPT_INCR('v', "verbose", &verbose,
1464 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001465 OPT_END()
1466 };
1467
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001468 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1469 "buildid-list", "stat", NULL };
1470 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001471
Joerg Roedel1aed2672012-01-04 17:54:20 +01001472 perf_host = 0;
1473 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001474
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001475 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1476 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001477 if (!argc)
1478 usage_with_options(kvm_usage, kvm_options);
1479
1480 if (!perf_host)
1481 perf_guest = 1;
1482
Xiao Guangrong37860632012-11-15 14:17:01 +08001483 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001484 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001485
Xiao Guangrong37860632012-11-15 14:17:01 +08001486 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001487 pr_err("Failed to allocate memory for filename\n");
1488 return -ENOMEM;
1489 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001490 }
1491
1492 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001493 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001494 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001495 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001496 else if (!strncmp(argv[0], "diff", 4))
1497 return cmd_diff(argc, argv, NULL);
1498 else if (!strncmp(argv[0], "top", 3))
1499 return cmd_top(argc, argv, NULL);
1500 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001501 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001502#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001503 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001504 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001505#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001506 else
1507 usage_with_options(kvm_usage, kvm_options);
1508
1509 return 0;
1510}