blob: 40660428fa72f58694b9a5c0309a81de7a14e8c4 [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"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060013#include <subcmd/parse-options.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080014#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080016#include "util/tool.h"
17#include "util/stat.h"
David Ahern1afe1d12013-08-05 21:41:34 -040018#include "util/top.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030020#include "util/ordered-events.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
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030027#include <linux/kernel.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030028#include <linux/time64.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030029#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030030#include <inttypes.h>
David Ahern1afe1d12013-08-05 21:41:34 -040031#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080032#include <semaphore.h>
33#include <pthread.h>
34#include <math.h>
35
Alexander Yaryginda50ad62014-07-03 17:59:49 +040036#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040037#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080038
Alexander Yarygin9daa8122014-07-03 18:29:05 +040039void exit_event_get_key(struct perf_evsel *evsel,
40 struct perf_sample *sample,
41 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080042{
43 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053044 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080045}
46
Alexander Yarygin9daa8122014-07-03 18:29:05 +040047bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080048{
Hemant Kumar162607e2016-01-28 12:03:04 +053049 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080050}
51
Alexander Yarygin9daa8122014-07-03 18:29:05 +040052bool exit_event_begin(struct perf_evsel *evsel,
53 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080054{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030055 if (kvm_exit_event(evsel)) {
56 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080057 return true;
58 }
59
60 return false;
61}
62
Alexander Yarygin9daa8122014-07-03 18:29:05 +040063bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080064{
Hemant Kumar162607e2016-01-28 12:03:04 +053065 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080066}
67
Alexander Yarygin9daa8122014-07-03 18:29:05 +040068bool exit_event_end(struct perf_evsel *evsel,
69 struct perf_sample *sample __maybe_unused,
70 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080071{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030072 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080073}
74
Alexander Yarygindf74c132014-07-03 17:59:50 +040075static const char *get_exit_reason(struct perf_kvm_stat *kvm,
76 struct exit_reasons_table *tbl,
77 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080078{
Alexander Yarygindf74c132014-07-03 17:59:50 +040079 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060080 if (tbl->exit_code == exit_code)
81 return tbl->reason;
82 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080083 }
84
85 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -060086 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080087 return "UNKNOWN";
88}
89
Alexander Yarygin9daa8122014-07-03 18:29:05 +040090void exit_event_decode_key(struct perf_kvm_stat *kvm,
91 struct event_key *key,
92 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080093{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +040094 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +040095 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080096
Hemant Kumar162607e2016-01-28 12:03:04 +053097 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080098}
99
Xiao Guangrong37860632012-11-15 14:17:01 +0800100static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800101{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400102 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800103
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400104 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
105 if (!strcmp(events_ops->name, kvm->report_event)) {
106 kvm->events_ops = events_ops->ops;
107 return true;
108 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800109 }
110
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400111 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800112}
113
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800114struct vcpu_event_record {
115 int vcpu_id;
116 u64 start_time;
117 struct kvm_event *last_event;
118};
119
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800120
Xiao Guangrong37860632012-11-15 14:17:01 +0800121static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800122{
David Ahernb880dee2012-10-08 11:17:30 -0600123 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800124
David Ahernb880dee2012-10-08 11:17:30 -0600125 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600126 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800127}
128
David Ahern87419c92013-10-29 10:43:16 -0600129#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400130static void clear_events_cache_stats(struct list_head *kvm_events_cache)
131{
132 struct list_head *head;
133 struct kvm_event *event;
134 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400135 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400136
137 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
138 head = &kvm_events_cache[i];
139 list_for_each_entry(event, head, hash_entry) {
140 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400141 event->total.time = 0;
142 init_stats(&event->total.stats);
143
144 for (j = 0; j < event->max_vcpu; ++j) {
145 event->vcpu[j].time = 0;
146 init_stats(&event->vcpu[j].stats);
147 }
David Ahern1afe1d12013-08-05 21:41:34 -0400148 }
149 }
150}
David Ahern87419c92013-10-29 10:43:16 -0600151#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400152
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800153static int kvm_events_hash_fn(u64 key)
154{
155 return key & (EVENTS_CACHE_SIZE - 1);
156}
157
158static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
159{
160 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600161 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800162
163 if (vcpu_id < event->max_vcpu)
164 return true;
165
166 while (event->max_vcpu <= vcpu_id)
167 event->max_vcpu += DEFAULT_VCPU_NUM;
168
David Ahern6ca5f302013-05-25 18:24:46 -0600169 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800170 event->vcpu = realloc(event->vcpu,
171 event->max_vcpu * sizeof(*event->vcpu));
172 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600173 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800174 pr_err("Not enough memory\n");
175 return false;
176 }
177
178 memset(event->vcpu + old_max_vcpu, 0,
179 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
180 return true;
181}
182
183static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
184{
185 struct kvm_event *event;
186
187 event = zalloc(sizeof(*event));
188 if (!event) {
189 pr_err("Not enough memory\n");
190 return NULL;
191 }
192
193 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200194 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800195 return event;
196}
197
Xiao Guangrong37860632012-11-15 14:17:01 +0800198static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600199 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800200{
201 struct kvm_event *event;
202 struct list_head *head;
203
204 BUG_ON(key->key == INVALID_KEY);
205
David Ahernde332ac2012-10-02 22:09:53 -0600206 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600207 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800208 if (event->key.key == key->key && event->key.info == key->info)
209 return event;
David Ahern355afe82012-10-08 11:17:32 -0600210 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800211
212 event = kvm_alloc_init_event(key);
213 if (!event)
214 return NULL;
215
216 list_add(&event->hash_entry, head);
217 return event;
218}
219
Xiao Guangrong37860632012-11-15 14:17:01 +0800220static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600221 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800222 struct event_key *key, u64 timestamp)
223{
224 struct kvm_event *event = NULL;
225
226 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600227 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800228
229 vcpu_record->last_event = event;
230 vcpu_record->start_time = timestamp;
231 return true;
232}
233
234static void
235kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
236{
237 kvm_stats->time += time_diff;
238 update_stats(&kvm_stats->stats, time_diff);
239}
240
241static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
242{
243 struct kvm_event_stats *kvm_stats = &event->total;
244
245 if (vcpu_id != -1)
246 kvm_stats = &event->vcpu[vcpu_id];
247
248 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
249 avg_stats(&kvm_stats->stats));
250}
251
252static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
253 u64 time_diff)
254{
David Ahern2aa8eab2012-10-08 11:17:35 -0600255 if (vcpu_id == -1) {
256 kvm_update_event_stats(&event->total, time_diff);
257 return true;
258 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800259
260 if (!kvm_event_expand(event, vcpu_id))
261 return false;
262
263 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
264 return true;
265}
266
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400267static bool is_child_event(struct perf_kvm_stat *kvm,
268 struct perf_evsel *evsel,
269 struct perf_sample *sample,
270 struct event_key *key)
271{
272 struct child_event_ops *child_ops;
273
274 child_ops = kvm->events_ops->child_ops;
275
276 if (!child_ops)
277 return false;
278
279 for (; child_ops->name; child_ops++) {
280 if (!strcmp(evsel->name, child_ops->name)) {
281 child_ops->get_key(evsel, sample, key);
282 return true;
283 }
284 }
285
286 return false;
287}
288
289static bool handle_child_event(struct perf_kvm_stat *kvm,
290 struct vcpu_event_record *vcpu_record,
291 struct event_key *key,
292 struct perf_sample *sample __maybe_unused)
293{
294 struct kvm_event *event = NULL;
295
296 if (key->key != INVALID_KEY)
297 event = find_create_kvm_event(kvm, key);
298
299 vcpu_record->last_event = event;
300
301 return true;
302}
303
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400304static bool skip_event(const char *event)
305{
306 const char * const *skip_events;
307
308 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
309 if (!strcmp(event, *skip_events))
310 return true;
311
312 return false;
313}
314
Xiao Guangrong37860632012-11-15 14:17:01 +0800315static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600316 struct vcpu_event_record *vcpu_record,
317 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400318 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800319{
320 struct kvm_event *event;
321 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600322 int vcpu;
323
324 if (kvm->trace_vcpu == -1)
325 vcpu = -1;
326 else
327 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800328
329 event = vcpu_record->last_event;
330 time_begin = vcpu_record->start_time;
331
332 /* The begin event is not caught. */
333 if (!time_begin)
334 return true;
335
336 /*
337 * In some case, the 'begin event' only records the start timestamp,
338 * the actual event is recognized in the 'end event' (e.g. mmio-event).
339 */
340
341 /* Both begin and end events did not get the key. */
342 if (!event && key->key == INVALID_KEY)
343 return true;
344
345 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600346 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800347
348 if (!event)
349 return false;
350
351 vcpu_record->last_event = NULL;
352 vcpu_record->start_time = 0;
353
David Ahern1afe1d12013-08-05 21:41:34 -0400354 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400355 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400356 pr_debug("End time before begin time; skipping event.\n");
357 return true;
358 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800359
David Ahern70f7b4a2013-08-07 21:56:38 -0400360 time_diff = sample->time - time_begin;
361
362 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530363 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400364
365 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400366 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400367 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
368 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300369 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400370 }
371 }
372
David Ahern2aa8eab2012-10-08 11:17:35 -0600373 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800374}
375
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300376static
377struct vcpu_event_record *per_vcpu_record(struct thread *thread,
378 struct perf_evsel *evsel,
379 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800380{
381 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900382 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800383 struct vcpu_event_record *vcpu_record;
384
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300385 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800386 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300387 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800388 return NULL;
389 }
390
Hemant Kumar162607e2016-01-28 12:03:04 +0530391 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
392 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900393 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800394 }
395
Namhyung Kim69e865c2014-10-06 09:46:02 +0900396 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800397}
398
Xiao Guangrong37860632012-11-15 14:17:01 +0800399static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600400 struct thread *thread,
401 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300402 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800403{
404 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400405 struct event_key key = { .key = INVALID_KEY,
406 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800407
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300408 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800409 if (!vcpu_record)
410 return true;
411
David Ahern2aa8eab2012-10-08 11:17:35 -0600412 /* only process events for vcpus user cares about */
413 if ((kvm->trace_vcpu != -1) &&
414 (kvm->trace_vcpu != vcpu_record->vcpu_id))
415 return true;
416
David Ahernde332ac2012-10-02 22:09:53 -0600417 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
418 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800419
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400420 if (is_child_event(kvm, evsel, sample, &key))
421 return handle_child_event(kvm, vcpu_record, &key, sample);
422
David Ahernde332ac2012-10-02 22:09:53 -0600423 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400424 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800425
426 return true;
427}
428
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800429#define GET_EVENT_KEY(func, field) \
430static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
431{ \
432 if (vcpu == -1) \
433 return event->total.field; \
434 \
435 if (vcpu >= event->max_vcpu) \
436 return 0; \
437 \
438 return event->vcpu[vcpu].field; \
439}
440
441#define COMPARE_EVENT_KEY(func, field) \
442GET_EVENT_KEY(func, field) \
443static int compare_kvm_event_ ## func(struct kvm_event *one, \
444 struct kvm_event *two, int vcpu)\
445{ \
446 return get_event_ ##func(one, vcpu) > \
447 get_event_ ##func(two, vcpu); \
448}
449
450GET_EVENT_KEY(time, time);
451COMPARE_EVENT_KEY(count, stats.n);
452COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400453GET_EVENT_KEY(max, stats.max);
454GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800455
456#define DEF_SORT_NAME_KEY(name, compare_key) \
457 { #name, compare_kvm_event_ ## compare_key }
458
459static struct kvm_event_key keys[] = {
460 DEF_SORT_NAME_KEY(sample, count),
461 DEF_SORT_NAME_KEY(time, mean),
462 { NULL, NULL }
463};
464
Xiao Guangrong37860632012-11-15 14:17:01 +0800465static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800466{
467 int i;
468
469 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600470 if (!strcmp(keys[i].name, kvm->sort_key)) {
471 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800472 return true;
473 }
474 }
475
David Ahernde332ac2012-10-02 22:09:53 -0600476 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800477 return false;
478}
479
David Ahernde332ac2012-10-02 22:09:53 -0600480static void insert_to_result(struct rb_root *result, struct kvm_event *event,
481 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800482{
David Ahernde332ac2012-10-02 22:09:53 -0600483 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800484 struct rb_node *parent = NULL;
485 struct kvm_event *p;
486
487 while (*rb) {
488 p = container_of(*rb, struct kvm_event, rb);
489 parent = *rb;
490
491 if (bigger(event, p, vcpu))
492 rb = &(*rb)->rb_left;
493 else
494 rb = &(*rb)->rb_right;
495 }
496
497 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600498 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800499}
500
Xiao Guangrong37860632012-11-15 14:17:01 +0800501static void
502update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800503{
David Ahernde332ac2012-10-02 22:09:53 -0600504 int vcpu = kvm->trace_vcpu;
505
506 kvm->total_count += get_event_count(event, vcpu);
507 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800508}
509
510static bool event_is_valid(struct kvm_event *event, int vcpu)
511{
512 return !!get_event_count(event, vcpu);
513}
514
Xiao Guangrong37860632012-11-15 14:17:01 +0800515static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800516{
517 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600518 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800519 struct kvm_event *event;
520
David Ahern355afe82012-10-08 11:17:32 -0600521 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
522 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800523 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600524 update_total_count(kvm, event);
525 insert_to_result(&kvm->result, event,
526 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800527 }
David Ahern355afe82012-10-08 11:17:32 -0600528 }
529 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800530}
531
532/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600533static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800534{
David Ahernde332ac2012-10-02 22:09:53 -0600535 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800536
537 if (!node)
538 return NULL;
539
David Ahernde332ac2012-10-02 22:09:53 -0600540 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800541 return container_of(node, struct kvm_event, rb);
542}
543
David Ahern1afe1d12013-08-05 21:41:34 -0400544static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800545{
David Ahern1afe1d12013-08-05 21:41:34 -0400546 int vcpu = kvm->trace_vcpu;
547
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800548 pr_info("Analyze events for ");
549
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400550 if (kvm->opts.target.system_wide)
551 pr_info("all VMs, ");
552 else if (kvm->opts.target.pid)
553 pr_info("pid(s) %s, ", kvm->opts.target.pid);
554 else
555 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400556
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800557 if (vcpu == -1)
558 pr_info("all VCPUs:\n\n");
559 else
560 pr_info("VCPU %d:\n\n", vcpu);
561}
562
David Ahern1afe1d12013-08-05 21:41:34 -0400563static void show_timeofday(void)
564{
565 char date[64];
566 struct timeval tv;
567 struct tm ltime;
568
569 gettimeofday(&tv, NULL);
570 if (localtime_r(&tv.tv_sec, &ltime)) {
571 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
572 pr_info("%s.%06ld", date, tv.tv_usec);
573 } else
574 pr_info("00:00:00.000000");
575
576 return;
577}
578
Xiao Guangrong37860632012-11-15 14:17:01 +0800579static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800580{
Hemant Kumar162607e2016-01-28 12:03:04 +0530581 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800582 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600583 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800584
David Ahern1afe1d12013-08-05 21:41:34 -0400585 if (kvm->live) {
586 puts(CONSOLE_CLEAR);
587 show_timeofday();
588 }
589
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800590 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400591 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530592 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800593 pr_info("%10s ", "Samples");
594 pr_info("%9s ", "Samples%");
595
596 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200597 pr_info("%11s ", "Min Time");
598 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800599 pr_info("%16s ", "Avg time");
600 pr_info("\n\n");
601
David Ahernde332ac2012-10-02 22:09:53 -0600602 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400603 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800604
605 ecount = get_event_count(event, vcpu);
606 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400607 max = get_event_max(event, vcpu);
608 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800609
David Ahernde332ac2012-10-02 22:09:53 -0600610 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530611 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800612 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600613 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
614 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300615 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
616 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
617 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800618 kvm_event_rel_stddev(vcpu, event));
619 pr_info("\n");
620 }
621
David Aherne4f76372012-10-08 11:17:34 -0600622 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300623 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400624
625 if (kvm->lost_events)
626 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
627}
628
David Ahern87419c92013-10-29 10:43:16 -0600629#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400630static int process_lost_event(struct perf_tool *tool,
631 union perf_event *event __maybe_unused,
632 struct perf_sample *sample __maybe_unused,
633 struct machine *machine __maybe_unused)
634{
635 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
636
637 kvm->lost_events++;
638 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800639}
David Ahern87419c92013-10-29 10:43:16 -0600640#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800641
David Ahern2e73f002013-08-05 21:41:37 -0400642static bool skip_sample(struct perf_kvm_stat *kvm,
643 struct perf_sample *sample)
644{
645 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
646 return true;
647
648 return false;
649}
650
David Ahernde332ac2012-10-02 22:09:53 -0600651static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800652 union perf_event *event,
653 struct perf_sample *sample,
654 struct perf_evsel *evsel,
655 struct machine *machine)
656{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300657 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400658 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800659 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
660 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800661
David Ahern2e73f002013-08-05 21:41:37 -0400662 if (skip_sample(kvm, sample))
663 return 0;
664
Adrian Hunter314add62013-08-27 11:23:03 +0300665 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800666 if (thread == NULL) {
667 pr_debug("problem processing %d event, skipping it.\n",
668 event->header.type);
669 return -1;
670 }
671
David Ahernde332ac2012-10-02 22:09:53 -0600672 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300673 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800674
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300675 thread__put(thread);
676 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800677}
678
David Ahern1afe1d12013-08-05 21:41:34 -0400679static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800680{
David Ahern1afe1d12013-08-05 21:41:34 -0400681 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400682 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400683
684 if (kvm->live) {
685 err = get_cpuid(buf, sizeof(buf));
686 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400687 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400688 return err;
689 }
690 cpuid = buf;
691 } else
692 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800693
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400694 if (!cpuid) {
695 pr_err("Failed to look up CPU type\n");
696 return -EINVAL;
697 }
698
699 err = cpu_isa_init(kvm, cpuid);
700 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800701 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800702
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400703 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400704}
705
706static bool verify_vcpu(int vcpu)
707{
708 if (vcpu != -1 && vcpu < 0) {
709 pr_err("Invalid vcpu:%d.\n", vcpu);
710 return false;
711 }
712
713 return true;
714}
715
David Ahern87419c92013-10-29 10:43:16 -0600716#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400717/* keeping the max events to a modest level to keep
718 * the processing of samples per mmap smooth.
719 */
720#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
721
722static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
723 u64 *mmap_time)
724{
725 union perf_event *event;
726 struct perf_sample sample;
727 s64 n = 0;
728 int err;
729
730 *mmap_time = ULLONG_MAX;
731 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
732 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
733 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800734 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400735 pr_err("Failed to parse sample\n");
736 return -1;
737 }
738
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300739 err = perf_session__queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800740 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300741 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800742 * point to it, and it'll get possibly overwritten by the kernel.
743 */
744 perf_evlist__mmap_consume(kvm->evlist, idx);
745
David Ahern1afe1d12013-08-05 21:41:34 -0400746 if (err) {
747 pr_err("Failed to enqueue sample: %d\n", err);
748 return -1;
749 }
750
751 /* save time stamp of our first sample for this mmap */
752 if (n == 0)
753 *mmap_time = sample.time;
754
755 /* limit events per mmap handled all at once */
756 n++;
757 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
758 break;
759 }
760
761 return n;
762}
763
764static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
765{
766 int i, err, throttled = 0;
767 s64 n, ntotal = 0;
768 u64 flush_time = ULLONG_MAX, mmap_time;
769
770 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
771 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
772 if (n < 0)
773 return -1;
774
775 /* flush time is going to be the minimum of all the individual
776 * mmap times. Essentially, we flush all the samples queued up
777 * from the last pass under our minimal start time -- that leaves
778 * a very small race for samples to come in with a lower timestamp.
779 * The ioctl to return the perf_clock timestamp should close the
780 * race entirely.
781 */
782 if (mmap_time < flush_time)
783 flush_time = mmap_time;
784
785 ntotal += n;
786 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
787 throttled = 1;
788 }
789
790 /* flush queue after each round in which we processed events */
791 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300792 struct ordered_events *oe = &kvm->session->ordered_events;
793
794 oe->next_flush = flush_time;
795 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400796 if (err) {
797 if (kvm->lost_events)
798 pr_info("\nLost events: %" PRIu64 "\n\n",
799 kvm->lost_events);
800 return err;
801 }
802 }
803
804 return throttled;
805}
806
807static volatile int done;
808
809static void sig_handler(int sig __maybe_unused)
810{
811 done = 1;
812}
813
814static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
815{
816 struct itimerspec new_value;
817 int rc = -1;
818
819 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
820 if (kvm->timerfd < 0) {
821 pr_err("timerfd_create failed\n");
822 goto out;
823 }
824
825 new_value.it_value.tv_sec = kvm->display_time;
826 new_value.it_value.tv_nsec = 0;
827 new_value.it_interval.tv_sec = kvm->display_time;
828 new_value.it_interval.tv_nsec = 0;
829
830 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
831 pr_err("timerfd_settime failed: %d\n", errno);
832 close(kvm->timerfd);
833 goto out;
834 }
835
836 rc = 0;
837out:
838 return rc;
839}
840
841static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
842{
843 uint64_t c;
844 int rc;
845
846 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
847 if (rc < 0) {
848 if (errno == EAGAIN)
849 return 0;
850
851 pr_err("Failed to read timer fd: %d\n", errno);
852 return -1;
853 }
854
855 if (rc != sizeof(uint64_t)) {
856 pr_err("Error reading timer fd - invalid size returned\n");
857 return -1;
858 }
859
860 if (c != 1)
861 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
862
863 /* update display */
864 sort_result(kvm);
865 print_result(kvm);
866
867 /* reset counts */
868 clear_events_cache_stats(kvm->kvm_events_cache);
869 kvm->total_count = 0;
870 kvm->total_time = 0;
871 kvm->lost_events = 0;
872
873 return 0;
874}
875
876static int fd_set_nonblock(int fd)
877{
878 long arg = 0;
879
880 arg = fcntl(fd, F_GETFL);
881 if (arg < 0) {
882 pr_err("Failed to get current flags for fd %d\n", fd);
883 return -1;
884 }
885
886 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
887 pr_err("Failed to set non-block option on fd %d\n", fd);
888 return -1;
889 }
890
891 return 0;
892}
893
Jiri Olsad5b41302014-08-11 10:50:09 +0200894static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400895{
896 int c;
897
David Ahern1afe1d12013-08-05 21:41:34 -0400898 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400899 if (c == 'q')
900 return 1;
901
902 return 0;
903}
904
905static int kvm_events_live_report(struct perf_kvm_stat *kvm)
906{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200907 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200908 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400909
910 /* live flag must be set first */
911 kvm->live = true;
912
913 ret = cpu_isa_config(kvm);
914 if (ret < 0)
915 return ret;
916
917 if (!verify_vcpu(kvm->trace_vcpu) ||
918 !select_key(kvm) ||
919 !register_kvm_events_ops(kvm)) {
920 goto out;
921 }
922
Jiri Olsad5b41302014-08-11 10:50:09 +0200923 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400924 init_kvm_event_record(kvm);
925
David Ahern1afe1d12013-08-05 21:41:34 -0400926 signal(SIGINT, sig_handler);
927 signal(SIGTERM, sig_handler);
928
David Ahern1afe1d12013-08-05 21:41:34 -0400929 /* add timer fd */
930 if (perf_kvm__timerfd_create(kvm) < 0) {
931 err = -1;
932 goto out;
933 }
934
Jiri Olsafe636ad2014-10-01 18:00:27 +0200935 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300936 goto out;
937
Jiri Olsa0cae0132014-10-01 18:00:28 +0200938 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
939 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300940 goto out;
941
David Ahern1afe1d12013-08-05 21:41:34 -0400942 if (fd_set_nonblock(fileno(stdin)) != 0)
943 goto out;
944
945 /* everything is good - enable the events and process */
946 perf_evlist__enable(kvm->evlist);
947
948 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200949 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400950 int rc;
951
952 rc = perf_kvm__mmap_read(kvm);
953 if (rc < 0)
954 break;
955
956 err = perf_kvm__handle_timerfd(kvm);
957 if (err)
958 goto out;
959
Jiri Olsa1ca72262014-10-01 18:00:29 +0200960 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200961 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400962
963 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200964 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400965 }
966
967 perf_evlist__disable(kvm->evlist);
968
969 if (err == 0) {
970 sort_result(kvm);
971 print_result(kvm);
972 }
973
974out:
975 if (kvm->timerfd >= 0)
976 close(kvm->timerfd);
977
Jiri Olsad5b41302014-08-11 10:50:09 +0200978 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -0400979 return err;
980}
981
982static int kvm_live_open_events(struct perf_kvm_stat *kvm)
983{
984 int err, rc = -1;
985 struct perf_evsel *pos;
986 struct perf_evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +0000987 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -0400988
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300989 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -0400990
991 /*
992 * Note: exclude_{guest,host} do not apply here.
993 * This command processes KVM tracepoints from host only
994 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300995 evlist__for_each_entry(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -0400996 struct perf_event_attr *attr = &pos->attr;
997
998 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +0300999 perf_evsel__set_sample_bit(pos, TID);
1000 perf_evsel__set_sample_bit(pos, TIME);
1001 perf_evsel__set_sample_bit(pos, CPU);
1002 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001003 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001004 perf_evsel__reset_sample_bit(pos, PERIOD);
1005 perf_evsel__reset_sample_bit(pos, IP);
1006 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1007 perf_evsel__reset_sample_bit(pos, ADDR);
1008 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001009 attr->mmap = 0;
1010 attr->comm = 0;
1011 attr->task = 0;
1012
1013 attr->sample_period = 1;
1014
1015 attr->watermark = 0;
1016 attr->wakeup_events = 1000;
1017
1018 /* will enable all once we are ready */
1019 attr->disabled = 1;
1020 }
1021
1022 err = perf_evlist__open(evlist);
1023 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001024 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001025 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001026 goto out;
1027 }
1028
1029 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001030 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001031 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001032 perf_evlist__close(evlist);
1033 goto out;
1034 }
1035
1036 rc = 0;
1037
1038out:
1039 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001040}
David Ahern87419c92013-10-29 10:43:16 -06001041#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001042
Xiao Guangrong37860632012-11-15 14:17:01 +08001043static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001044{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001045 int ret;
1046
David Ahernde332ac2012-10-02 22:09:53 -06001047 struct perf_tool eops = {
1048 .sample = process_sample_event,
1049 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301050 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001051 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001052 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001053 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001054 .path = kvm->file_name,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001055 .mode = PERF_DATA_MODE_READ,
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001056 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001057 };
David Ahernde332ac2012-10-02 22:09:53 -06001058
1059 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001060 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001061 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001062 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001063 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001064 }
1065
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001066 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001067
Taeung Song41b98362015-07-01 00:18:44 +09001068 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1069 ret = -EINVAL;
1070 goto out_delete;
1071 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001072
1073 /*
1074 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1075 * traced in the old kernel.
1076 */
David Ahern1afe1d12013-08-05 21:41:34 -04001077 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001078 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001079 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001080
Taeung Song41b98362015-07-01 00:18:44 +09001081 ret = perf_session__process_events(kvm->session);
1082
1083out_delete:
1084 perf_session__delete(kvm->session);
1085 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001086}
1087
David Ahern2e73f002013-08-05 21:41:37 -04001088static int parse_target_str(struct perf_kvm_stat *kvm)
1089{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001090 if (kvm->opts.target.pid) {
1091 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001092 if (kvm->pid_list == NULL) {
1093 pr_err("Error parsing process id string\n");
1094 return -EINVAL;
1095 }
1096 }
1097
1098 return 0;
1099}
1100
Xiao Guangrong37860632012-11-15 14:17:01 +08001101static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001102{
1103 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001104 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001105
David Ahern2e73f002013-08-05 21:41:37 -04001106 if (parse_target_str(kvm) != 0)
1107 goto exit;
1108
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001109 if (!verify_vcpu(vcpu))
1110 goto exit;
1111
David Ahernde332ac2012-10-02 22:09:53 -06001112 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001113 goto exit;
1114
David Ahernde332ac2012-10-02 22:09:53 -06001115 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001116 goto exit;
1117
David Ahernde332ac2012-10-02 22:09:53 -06001118 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001119 setup_pager();
1120
David Ahernde332ac2012-10-02 22:09:53 -06001121 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001122 if (ret)
1123 goto exit;
1124
David Ahernde332ac2012-10-02 22:09:53 -06001125 sort_result(kvm);
1126 print_result(kvm);
1127
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001128exit:
1129 return ret;
1130}
1131
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001132#define STRDUP_FAIL_EXIT(s) \
1133 ({ char *_p; \
1134 _p = strdup(s); \
1135 if (!_p) \
1136 return -ENOMEM; \
1137 _p; \
1138 })
1139
Hemant Kumar066d3592016-01-28 12:03:06 +05301140int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1141{
1142 return 0;
1143}
1144
Xiao Guangrong37860632012-11-15 14:17:01 +08001145static int
1146kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001147{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001148 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001149 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001150 const char * const record_args[] = {
1151 "record",
1152 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001153 "-m", "1024",
1154 "-c", "1",
1155 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001156 const char * const kvm_stat_record_usage[] = {
1157 "perf kvm stat record [<options>]",
1158 NULL
1159 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001160 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301161 int ret;
1162
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001163 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301164 ret = setup_kvm_events_tp(kvm);
1165 if (ret < 0) {
1166 pr_err("Unable to setup the kvm tracepoints\n");
1167 return ret;
1168 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001169
1170 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1171 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001172
David Ahern8fdd84c2013-08-02 14:05:42 -06001173 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001174 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001175 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1176
1177 if (rec_argv == NULL)
1178 return -ENOMEM;
1179
1180 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1181 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1182
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001183 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001184 rec_argv[i++] = "-e";
1185 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1186 }
1187
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001188 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001189 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001190
1191 for (j = 1; j < (unsigned int)argc; j++, i++)
1192 rec_argv[i] = argv[j];
1193
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001194 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1195 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1196 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1197
1198 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1199 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1200 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1201 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1202 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1203 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1204 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1205 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1206 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1207 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1208 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1209 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1210 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1211 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1212 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1213
1214 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001215 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001216}
1217
Xiao Guangrong37860632012-11-15 14:17:01 +08001218static int
1219kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001220{
David Ahernde332ac2012-10-02 22:09:53 -06001221 const struct option kvm_events_report_options[] = {
1222 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001223 "event for reporting: vmexit, "
1224 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001225 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1226 "vcpu id to report"),
1227 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1228 "key for sorting: sample(sort by samples number)"
1229 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001230 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001231 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001232 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001233 OPT_END()
1234 };
1235
1236 const char * const kvm_events_report_usage[] = {
1237 "perf kvm stat report [<options>]",
1238 NULL
1239 };
1240
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001241 if (argc) {
1242 argc = parse_options(argc, argv,
1243 kvm_events_report_options,
1244 kvm_events_report_usage, 0);
1245 if (argc)
1246 usage_with_options(kvm_events_report_usage,
1247 kvm_events_report_options);
1248 }
1249
Alexander Yaryginf1819572014-09-01 17:44:54 +04001250 if (!kvm->opts.target.pid)
1251 kvm->opts.target.system_wide = true;
1252
David Ahernde332ac2012-10-02 22:09:53 -06001253 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001254}
1255
David Ahern87419c92013-10-29 10:43:16 -06001256#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001257static struct perf_evlist *kvm_live_event_list(void)
1258{
1259 struct perf_evlist *evlist;
1260 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001261 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001262 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001263
1264 evlist = perf_evlist__new();
1265 if (evlist == NULL)
1266 return NULL;
1267
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001268 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001269
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001270 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001271 if (tp == NULL)
1272 goto out;
1273
1274 /* split tracepoint into subsystem and name */
1275 sys = tp;
1276 name = strchr(tp, ':');
1277 if (name == NULL) {
1278 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001279 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001280 free(tp);
1281 goto out;
1282 }
1283 *name = '\0';
1284 name++;
1285
1286 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001287 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001288 free(tp);
1289 goto out;
1290 }
1291
1292 free(tp);
1293 }
1294
1295 err = 0;
1296
1297out:
1298 if (err) {
1299 perf_evlist__delete(evlist);
1300 evlist = NULL;
1301 }
1302
1303 return evlist;
1304}
1305
1306static int kvm_events_live(struct perf_kvm_stat *kvm,
1307 int argc, const char **argv)
1308{
1309 char errbuf[BUFSIZ];
1310 int err;
1311
1312 const struct option live_options[] = {
1313 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1314 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001315 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1316 "number of mmap data pages",
1317 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001318 OPT_INCR('v', "verbose", &verbose,
1319 "be more verbose (show counter open errors, etc)"),
1320 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1321 "system-wide collection from all CPUs"),
1322 OPT_UINTEGER('d', "display", &kvm->display_time,
1323 "time in seconds between display updates"),
1324 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001325 "event for reporting: "
1326 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001327 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1328 "vcpu id to report"),
1329 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1330 "key for sorting: sample(sort by samples number)"
1331 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001332 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001333 "show events other than"
1334 " HLT (x86 only) or Wait state (s390 only)"
1335 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001336 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1337 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001338 OPT_END()
1339 };
1340 const char * const live_usage[] = {
1341 "perf kvm stat live [<options>]",
1342 NULL
1343 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001344 struct perf_data_file file = {
1345 .mode = PERF_DATA_MODE_WRITE,
1346 };
David Ahern1afe1d12013-08-05 21:41:34 -04001347
1348
1349 /* event handling */
1350 kvm->tool.sample = process_sample_event;
1351 kvm->tool.comm = perf_event__process_comm;
1352 kvm->tool.exit = perf_event__process_exit;
1353 kvm->tool.fork = perf_event__process_fork;
1354 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301355 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001356 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001357 perf_tool__fill_defaults(&kvm->tool);
1358
1359 /* set defaults */
1360 kvm->display_time = 1;
1361 kvm->opts.user_interval = 1;
1362 kvm->opts.mmap_pages = 512;
1363 kvm->opts.target.uses_mmap = false;
1364 kvm->opts.target.uid_str = NULL;
1365 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001366 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001367
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001368 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001369 disable_buildid_cache();
1370
1371 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001372
1373 if (argc) {
1374 argc = parse_options(argc, argv, live_options,
1375 live_usage, 0);
1376 if (argc)
1377 usage_with_options(live_usage, live_options);
1378 }
1379
David Ahern70f7b4a2013-08-07 21:56:38 -04001380 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1381
David Ahern1afe1d12013-08-05 21:41:34 -04001382 /*
1383 * target related setups
1384 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001385 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001386 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001387 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001388 ui__warning("%s", errbuf);
1389 }
1390
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001391 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001392 kvm->opts.target.system_wide = true;
1393
1394
1395 /*
1396 * generate the event list
1397 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301398 err = setup_kvm_events_tp(kvm);
1399 if (err < 0) {
1400 pr_err("Unable to setup the kvm tracepoints\n");
1401 return err;
1402 }
1403
David Ahern1afe1d12013-08-05 21:41:34 -04001404 kvm->evlist = kvm_live_event_list();
1405 if (kvm->evlist == NULL) {
1406 err = -1;
1407 goto out;
1408 }
1409
1410 symbol_conf.nr_events = kvm->evlist->nr_entries;
1411
1412 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1413 usage_with_options(live_usage, live_options);
1414
1415 /*
1416 * perf session
1417 */
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001418 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001419 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001420 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001421 goto out;
1422 }
1423 kvm->session->evlist = kvm->evlist;
1424 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001425 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001426 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001427 kvm->evlist->threads, false, kvm->opts.proc_map_timeout);
David Ahern1afe1d12013-08-05 21:41:34 -04001428 err = kvm_live_open_events(kvm);
1429 if (err)
1430 goto out;
1431
1432 err = kvm_events_live_report(kvm);
1433
1434out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001435 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001436 kvm->session = NULL;
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -03001437 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001438
1439 return err;
1440}
David Ahern87419c92013-10-29 10:43:16 -06001441#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001442
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001443static void print_kvm_stat_usage(void)
1444{
1445 printf("Usage: perf kvm stat <command>\n\n");
1446
1447 printf("# Available commands:\n");
1448 printf("\trecord: record kvm events\n");
1449 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001450 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001451
1452 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1453}
1454
Xiao Guangrong37860632012-11-15 14:17:01 +08001455static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001456{
Xiao Guangrong37860632012-11-15 14:17:01 +08001457 struct perf_kvm_stat kvm = {
1458 .file_name = file_name,
1459
1460 .trace_vcpu = -1,
1461 .report_event = "vmexit",
1462 .sort_key = "sample",
1463
Xiao Guangrong37860632012-11-15 14:17:01 +08001464 };
1465
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001466 if (argc == 1) {
1467 print_kvm_stat_usage();
1468 goto perf_stat;
1469 }
1470
1471 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001472 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001473
1474 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001475 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001476
David Ahern87419c92013-10-29 10:43:16 -06001477#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001478 if (!strncmp(argv[1], "live", 4))
1479 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001480#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001481
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001482perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001483 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001484}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001485#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001486
Xiao Guangrong37860632012-11-15 14:17:01 +08001487static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001488{
1489 int rec_argc, i = 0, j;
1490 const char **rec_argv;
1491
1492 rec_argc = argc + 2;
1493 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1494 rec_argv[i++] = strdup("record");
1495 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001496 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001497 for (j = 1; j < argc; j++, i++)
1498 rec_argv[i] = argv[j];
1499
1500 BUG_ON(i != rec_argc);
1501
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001502 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001503}
1504
Xiao Guangrong37860632012-11-15 14:17:01 +08001505static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001506{
1507 int rec_argc, i = 0, j;
1508 const char **rec_argv;
1509
1510 rec_argc = argc + 2;
1511 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1512 rec_argv[i++] = strdup("report");
1513 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001514 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001515 for (j = 1; j < argc; j++, i++)
1516 rec_argv[i] = argv[j];
1517
1518 BUG_ON(i != rec_argc);
1519
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001520 return cmd_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001521}
1522
Xiao Guangrong37860632012-11-15 14:17:01 +08001523static int
1524__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001525{
1526 int rec_argc, i = 0, j;
1527 const char **rec_argv;
1528
1529 rec_argc = argc + 2;
1530 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1531 rec_argv[i++] = strdup("buildid-list");
1532 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001533 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001534 for (j = 1; j < argc; j++, i++)
1535 rec_argv[i] = argv[j];
1536
1537 BUG_ON(i != rec_argc);
1538
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001539 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001540}
1541
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001542int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001543{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001544 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001545 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001546 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001547 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001548 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001549 "Output file name"),
1550 OPT_BOOLEAN(0, "guest", &perf_guest,
1551 "Collect guest os data"),
1552 OPT_BOOLEAN(0, "host", &perf_host,
1553 "Collect host os data"),
1554 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1555 "guest mount directory under which every guest os"
1556 " instance has a subdir"),
1557 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1558 "file", "file saving guest os vmlinux"),
1559 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1560 "file", "file saving guest os /proc/kallsyms"),
1561 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1562 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001563 OPT_INCR('v', "verbose", &verbose,
1564 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001565 OPT_END()
1566 };
1567
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001568 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1569 "buildid-list", "stat", NULL };
1570 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001571
Joerg Roedel1aed2672012-01-04 17:54:20 +01001572 perf_host = 0;
1573 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001574
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001575 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1576 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001577 if (!argc)
1578 usage_with_options(kvm_usage, kvm_options);
1579
1580 if (!perf_host)
1581 perf_guest = 1;
1582
Xiao Guangrong37860632012-11-15 14:17:01 +08001583 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001584 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001585
Xiao Guangrong37860632012-11-15 14:17:01 +08001586 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001587 pr_err("Failed to allocate memory for filename\n");
1588 return -ENOMEM;
1589 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001590 }
1591
1592 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001593 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001594 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001595 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001596 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001597 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001598 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001599 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001600 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001601 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001602#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001603 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001604 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001605#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001606 else
1607 usage_with_options(kvm_usage, kvm_options);
1608
1609 return 0;
1610}