blob: e82a6979327f1de64148954b499829a4ebdd275d [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 Melofd20e812017-04-17 15:23:08 -030029#include <inttypes.h>
David Ahern1afe1d12013-08-05 21:41:34 -040030#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080031#include <semaphore.h>
32#include <pthread.h>
33#include <math.h>
34
Alexander Yaryginda50ad62014-07-03 17:59:49 +040035#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040036#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080037
Alexander Yarygin9daa8122014-07-03 18:29:05 +040038void exit_event_get_key(struct perf_evsel *evsel,
39 struct perf_sample *sample,
40 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080041{
42 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053043 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080044}
45
Alexander Yarygin9daa8122014-07-03 18:29:05 +040046bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080047{
Hemant Kumar162607e2016-01-28 12:03:04 +053048 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080049}
50
Alexander Yarygin9daa8122014-07-03 18:29:05 +040051bool exit_event_begin(struct perf_evsel *evsel,
52 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080053{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030054 if (kvm_exit_event(evsel)) {
55 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080056 return true;
57 }
58
59 return false;
60}
61
Alexander Yarygin9daa8122014-07-03 18:29:05 +040062bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080063{
Hemant Kumar162607e2016-01-28 12:03:04 +053064 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080065}
66
Alexander Yarygin9daa8122014-07-03 18:29:05 +040067bool exit_event_end(struct perf_evsel *evsel,
68 struct perf_sample *sample __maybe_unused,
69 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080070{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030071 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080072}
73
Alexander Yarygindf74c132014-07-03 17:59:50 +040074static const char *get_exit_reason(struct perf_kvm_stat *kvm,
75 struct exit_reasons_table *tbl,
76 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080077{
Alexander Yarygindf74c132014-07-03 17:59:50 +040078 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060079 if (tbl->exit_code == exit_code)
80 return tbl->reason;
81 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080082 }
83
84 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -060085 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080086 return "UNKNOWN";
87}
88
Alexander Yarygin9daa8122014-07-03 18:29:05 +040089void exit_event_decode_key(struct perf_kvm_stat *kvm,
90 struct event_key *key,
91 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080092{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +040093 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +040094 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080095
Hemant Kumar162607e2016-01-28 12:03:04 +053096 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080097}
98
Xiao Guangrong37860632012-11-15 14:17:01 +080099static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800100{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400101 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800102
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400103 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
104 if (!strcmp(events_ops->name, kvm->report_event)) {
105 kvm->events_ops = events_ops->ops;
106 return true;
107 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800108 }
109
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400110 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800111}
112
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800113struct vcpu_event_record {
114 int vcpu_id;
115 u64 start_time;
116 struct kvm_event *last_event;
117};
118
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800119
Xiao Guangrong37860632012-11-15 14:17:01 +0800120static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800121{
David Ahernb880dee2012-10-08 11:17:30 -0600122 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800123
David Ahernb880dee2012-10-08 11:17:30 -0600124 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600125 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800126}
127
David Ahern87419c92013-10-29 10:43:16 -0600128#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400129static void clear_events_cache_stats(struct list_head *kvm_events_cache)
130{
131 struct list_head *head;
132 struct kvm_event *event;
133 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400134 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400135
136 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
137 head = &kvm_events_cache[i];
138 list_for_each_entry(event, head, hash_entry) {
139 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400140 event->total.time = 0;
141 init_stats(&event->total.stats);
142
143 for (j = 0; j < event->max_vcpu; ++j) {
144 event->vcpu[j].time = 0;
145 init_stats(&event->vcpu[j].stats);
146 }
David Ahern1afe1d12013-08-05 21:41:34 -0400147 }
148 }
149}
David Ahern87419c92013-10-29 10:43:16 -0600150#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400151
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800152static int kvm_events_hash_fn(u64 key)
153{
154 return key & (EVENTS_CACHE_SIZE - 1);
155}
156
157static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
158{
159 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600160 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800161
162 if (vcpu_id < event->max_vcpu)
163 return true;
164
165 while (event->max_vcpu <= vcpu_id)
166 event->max_vcpu += DEFAULT_VCPU_NUM;
167
David Ahern6ca5f302013-05-25 18:24:46 -0600168 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800169 event->vcpu = realloc(event->vcpu,
170 event->max_vcpu * sizeof(*event->vcpu));
171 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600172 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800173 pr_err("Not enough memory\n");
174 return false;
175 }
176
177 memset(event->vcpu + old_max_vcpu, 0,
178 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
179 return true;
180}
181
182static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
183{
184 struct kvm_event *event;
185
186 event = zalloc(sizeof(*event));
187 if (!event) {
188 pr_err("Not enough memory\n");
189 return NULL;
190 }
191
192 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200193 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800194 return event;
195}
196
Xiao Guangrong37860632012-11-15 14:17:01 +0800197static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600198 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800199{
200 struct kvm_event *event;
201 struct list_head *head;
202
203 BUG_ON(key->key == INVALID_KEY);
204
David Ahernde332ac2012-10-02 22:09:53 -0600205 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600206 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800207 if (event->key.key == key->key && event->key.info == key->info)
208 return event;
David Ahern355afe82012-10-08 11:17:32 -0600209 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800210
211 event = kvm_alloc_init_event(key);
212 if (!event)
213 return NULL;
214
215 list_add(&event->hash_entry, head);
216 return event;
217}
218
Xiao Guangrong37860632012-11-15 14:17:01 +0800219static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600220 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800221 struct event_key *key, u64 timestamp)
222{
223 struct kvm_event *event = NULL;
224
225 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600226 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800227
228 vcpu_record->last_event = event;
229 vcpu_record->start_time = timestamp;
230 return true;
231}
232
233static void
234kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
235{
236 kvm_stats->time += time_diff;
237 update_stats(&kvm_stats->stats, time_diff);
238}
239
240static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
241{
242 struct kvm_event_stats *kvm_stats = &event->total;
243
244 if (vcpu_id != -1)
245 kvm_stats = &event->vcpu[vcpu_id];
246
247 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
248 avg_stats(&kvm_stats->stats));
249}
250
251static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
252 u64 time_diff)
253{
David Ahern2aa8eab2012-10-08 11:17:35 -0600254 if (vcpu_id == -1) {
255 kvm_update_event_stats(&event->total, time_diff);
256 return true;
257 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800258
259 if (!kvm_event_expand(event, vcpu_id))
260 return false;
261
262 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
263 return true;
264}
265
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400266static bool is_child_event(struct perf_kvm_stat *kvm,
267 struct perf_evsel *evsel,
268 struct perf_sample *sample,
269 struct event_key *key)
270{
271 struct child_event_ops *child_ops;
272
273 child_ops = kvm->events_ops->child_ops;
274
275 if (!child_ops)
276 return false;
277
278 for (; child_ops->name; child_ops++) {
279 if (!strcmp(evsel->name, child_ops->name)) {
280 child_ops->get_key(evsel, sample, key);
281 return true;
282 }
283 }
284
285 return false;
286}
287
288static bool handle_child_event(struct perf_kvm_stat *kvm,
289 struct vcpu_event_record *vcpu_record,
290 struct event_key *key,
291 struct perf_sample *sample __maybe_unused)
292{
293 struct kvm_event *event = NULL;
294
295 if (key->key != INVALID_KEY)
296 event = find_create_kvm_event(kvm, key);
297
298 vcpu_record->last_event = event;
299
300 return true;
301}
302
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400303static bool skip_event(const char *event)
304{
305 const char * const *skip_events;
306
307 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
308 if (!strcmp(event, *skip_events))
309 return true;
310
311 return false;
312}
313
Xiao Guangrong37860632012-11-15 14:17:01 +0800314static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600315 struct vcpu_event_record *vcpu_record,
316 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400317 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800318{
319 struct kvm_event *event;
320 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600321 int vcpu;
322
323 if (kvm->trace_vcpu == -1)
324 vcpu = -1;
325 else
326 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800327
328 event = vcpu_record->last_event;
329 time_begin = vcpu_record->start_time;
330
331 /* The begin event is not caught. */
332 if (!time_begin)
333 return true;
334
335 /*
336 * In some case, the 'begin event' only records the start timestamp,
337 * the actual event is recognized in the 'end event' (e.g. mmio-event).
338 */
339
340 /* Both begin and end events did not get the key. */
341 if (!event && key->key == INVALID_KEY)
342 return true;
343
344 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600345 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800346
347 if (!event)
348 return false;
349
350 vcpu_record->last_event = NULL;
351 vcpu_record->start_time = 0;
352
David Ahern1afe1d12013-08-05 21:41:34 -0400353 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400354 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400355 pr_debug("End time before begin time; skipping event.\n");
356 return true;
357 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800358
David Ahern70f7b4a2013-08-07 21:56:38 -0400359 time_diff = sample->time - time_begin;
360
361 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530362 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400363
364 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400365 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400366 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
367 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300368 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400369 }
370 }
371
David Ahern2aa8eab2012-10-08 11:17:35 -0600372 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800373}
374
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300375static
376struct vcpu_event_record *per_vcpu_record(struct thread *thread,
377 struct perf_evsel *evsel,
378 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800379{
380 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900381 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800382 struct vcpu_event_record *vcpu_record;
383
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300384 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800385 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300386 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800387 return NULL;
388 }
389
Hemant Kumar162607e2016-01-28 12:03:04 +0530390 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
391 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900392 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800393 }
394
Namhyung Kim69e865c2014-10-06 09:46:02 +0900395 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800396}
397
Xiao Guangrong37860632012-11-15 14:17:01 +0800398static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600399 struct thread *thread,
400 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300401 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800402{
403 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400404 struct event_key key = { .key = INVALID_KEY,
405 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800406
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300407 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800408 if (!vcpu_record)
409 return true;
410
David Ahern2aa8eab2012-10-08 11:17:35 -0600411 /* only process events for vcpus user cares about */
412 if ((kvm->trace_vcpu != -1) &&
413 (kvm->trace_vcpu != vcpu_record->vcpu_id))
414 return true;
415
David Ahernde332ac2012-10-02 22:09:53 -0600416 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
417 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800418
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400419 if (is_child_event(kvm, evsel, sample, &key))
420 return handle_child_event(kvm, vcpu_record, &key, sample);
421
David Ahernde332ac2012-10-02 22:09:53 -0600422 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400423 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800424
425 return true;
426}
427
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800428#define GET_EVENT_KEY(func, field) \
429static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
430{ \
431 if (vcpu == -1) \
432 return event->total.field; \
433 \
434 if (vcpu >= event->max_vcpu) \
435 return 0; \
436 \
437 return event->vcpu[vcpu].field; \
438}
439
440#define COMPARE_EVENT_KEY(func, field) \
441GET_EVENT_KEY(func, field) \
442static int compare_kvm_event_ ## func(struct kvm_event *one, \
443 struct kvm_event *two, int vcpu)\
444{ \
445 return get_event_ ##func(one, vcpu) > \
446 get_event_ ##func(two, vcpu); \
447}
448
449GET_EVENT_KEY(time, time);
450COMPARE_EVENT_KEY(count, stats.n);
451COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400452GET_EVENT_KEY(max, stats.max);
453GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800454
455#define DEF_SORT_NAME_KEY(name, compare_key) \
456 { #name, compare_kvm_event_ ## compare_key }
457
458static struct kvm_event_key keys[] = {
459 DEF_SORT_NAME_KEY(sample, count),
460 DEF_SORT_NAME_KEY(time, mean),
461 { NULL, NULL }
462};
463
Xiao Guangrong37860632012-11-15 14:17:01 +0800464static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800465{
466 int i;
467
468 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600469 if (!strcmp(keys[i].name, kvm->sort_key)) {
470 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800471 return true;
472 }
473 }
474
David Ahernde332ac2012-10-02 22:09:53 -0600475 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800476 return false;
477}
478
David Ahernde332ac2012-10-02 22:09:53 -0600479static void insert_to_result(struct rb_root *result, struct kvm_event *event,
480 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800481{
David Ahernde332ac2012-10-02 22:09:53 -0600482 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800483 struct rb_node *parent = NULL;
484 struct kvm_event *p;
485
486 while (*rb) {
487 p = container_of(*rb, struct kvm_event, rb);
488 parent = *rb;
489
490 if (bigger(event, p, vcpu))
491 rb = &(*rb)->rb_left;
492 else
493 rb = &(*rb)->rb_right;
494 }
495
496 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600497 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800498}
499
Xiao Guangrong37860632012-11-15 14:17:01 +0800500static void
501update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800502{
David Ahernde332ac2012-10-02 22:09:53 -0600503 int vcpu = kvm->trace_vcpu;
504
505 kvm->total_count += get_event_count(event, vcpu);
506 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800507}
508
509static bool event_is_valid(struct kvm_event *event, int vcpu)
510{
511 return !!get_event_count(event, vcpu);
512}
513
Xiao Guangrong37860632012-11-15 14:17:01 +0800514static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800515{
516 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600517 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800518 struct kvm_event *event;
519
David Ahern355afe82012-10-08 11:17:32 -0600520 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
521 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800522 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600523 update_total_count(kvm, event);
524 insert_to_result(&kvm->result, event,
525 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800526 }
David Ahern355afe82012-10-08 11:17:32 -0600527 }
528 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800529}
530
531/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600532static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800533{
David Ahernde332ac2012-10-02 22:09:53 -0600534 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800535
536 if (!node)
537 return NULL;
538
David Ahernde332ac2012-10-02 22:09:53 -0600539 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800540 return container_of(node, struct kvm_event, rb);
541}
542
David Ahern1afe1d12013-08-05 21:41:34 -0400543static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800544{
David Ahern1afe1d12013-08-05 21:41:34 -0400545 int vcpu = kvm->trace_vcpu;
546
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800547 pr_info("Analyze events for ");
548
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400549 if (kvm->opts.target.system_wide)
550 pr_info("all VMs, ");
551 else if (kvm->opts.target.pid)
552 pr_info("pid(s) %s, ", kvm->opts.target.pid);
553 else
554 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400555
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800556 if (vcpu == -1)
557 pr_info("all VCPUs:\n\n");
558 else
559 pr_info("VCPU %d:\n\n", vcpu);
560}
561
David Ahern1afe1d12013-08-05 21:41:34 -0400562static void show_timeofday(void)
563{
564 char date[64];
565 struct timeval tv;
566 struct tm ltime;
567
568 gettimeofday(&tv, NULL);
569 if (localtime_r(&tv.tv_sec, &ltime)) {
570 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
571 pr_info("%s.%06ld", date, tv.tv_usec);
572 } else
573 pr_info("00:00:00.000000");
574
575 return;
576}
577
Xiao Guangrong37860632012-11-15 14:17:01 +0800578static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800579{
Hemant Kumar162607e2016-01-28 12:03:04 +0530580 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800581 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600582 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800583
David Ahern1afe1d12013-08-05 21:41:34 -0400584 if (kvm->live) {
585 puts(CONSOLE_CLEAR);
586 show_timeofday();
587 }
588
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800589 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400590 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530591 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800592 pr_info("%10s ", "Samples");
593 pr_info("%9s ", "Samples%");
594
595 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200596 pr_info("%11s ", "Min Time");
597 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800598 pr_info("%16s ", "Avg time");
599 pr_info("\n\n");
600
David Ahernde332ac2012-10-02 22:09:53 -0600601 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400602 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800603
604 ecount = get_event_count(event, vcpu);
605 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400606 max = get_event_max(event, vcpu);
607 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800608
David Ahernde332ac2012-10-02 22:09:53 -0600609 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530610 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800611 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600612 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
613 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300614 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
615 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
616 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800617 kvm_event_rel_stddev(vcpu, event));
618 pr_info("\n");
619 }
620
David Aherne4f76372012-10-08 11:17:34 -0600621 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300622 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400623
624 if (kvm->lost_events)
625 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
626}
627
David Ahern87419c92013-10-29 10:43:16 -0600628#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400629static int process_lost_event(struct perf_tool *tool,
630 union perf_event *event __maybe_unused,
631 struct perf_sample *sample __maybe_unused,
632 struct machine *machine __maybe_unused)
633{
634 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
635
636 kvm->lost_events++;
637 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800638}
David Ahern87419c92013-10-29 10:43:16 -0600639#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800640
David Ahern2e73f002013-08-05 21:41:37 -0400641static bool skip_sample(struct perf_kvm_stat *kvm,
642 struct perf_sample *sample)
643{
644 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
645 return true;
646
647 return false;
648}
649
David Ahernde332ac2012-10-02 22:09:53 -0600650static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800651 union perf_event *event,
652 struct perf_sample *sample,
653 struct perf_evsel *evsel,
654 struct machine *machine)
655{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300656 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400657 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800658 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
659 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800660
David Ahern2e73f002013-08-05 21:41:37 -0400661 if (skip_sample(kvm, sample))
662 return 0;
663
Adrian Hunter314add62013-08-27 11:23:03 +0300664 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800665 if (thread == NULL) {
666 pr_debug("problem processing %d event, skipping it.\n",
667 event->header.type);
668 return -1;
669 }
670
David Ahernde332ac2012-10-02 22:09:53 -0600671 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300672 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800673
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300674 thread__put(thread);
675 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800676}
677
David Ahern1afe1d12013-08-05 21:41:34 -0400678static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800679{
David Ahern1afe1d12013-08-05 21:41:34 -0400680 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400681 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400682
683 if (kvm->live) {
684 err = get_cpuid(buf, sizeof(buf));
685 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400686 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400687 return err;
688 }
689 cpuid = buf;
690 } else
691 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800692
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400693 if (!cpuid) {
694 pr_err("Failed to look up CPU type\n");
695 return -EINVAL;
696 }
697
698 err = cpu_isa_init(kvm, cpuid);
699 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800700 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800701
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400702 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400703}
704
705static bool verify_vcpu(int vcpu)
706{
707 if (vcpu != -1 && vcpu < 0) {
708 pr_err("Invalid vcpu:%d.\n", vcpu);
709 return false;
710 }
711
712 return true;
713}
714
David Ahern87419c92013-10-29 10:43:16 -0600715#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400716/* keeping the max events to a modest level to keep
717 * the processing of samples per mmap smooth.
718 */
719#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
720
721static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
722 u64 *mmap_time)
723{
724 union perf_event *event;
725 struct perf_sample sample;
726 s64 n = 0;
727 int err;
728
729 *mmap_time = ULLONG_MAX;
730 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
731 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
732 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800733 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400734 pr_err("Failed to parse sample\n");
735 return -1;
736 }
737
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300738 err = perf_session__queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800739 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300740 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800741 * point to it, and it'll get possibly overwritten by the kernel.
742 */
743 perf_evlist__mmap_consume(kvm->evlist, idx);
744
David Ahern1afe1d12013-08-05 21:41:34 -0400745 if (err) {
746 pr_err("Failed to enqueue sample: %d\n", err);
747 return -1;
748 }
749
750 /* save time stamp of our first sample for this mmap */
751 if (n == 0)
752 *mmap_time = sample.time;
753
754 /* limit events per mmap handled all at once */
755 n++;
756 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
757 break;
758 }
759
760 return n;
761}
762
763static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
764{
765 int i, err, throttled = 0;
766 s64 n, ntotal = 0;
767 u64 flush_time = ULLONG_MAX, mmap_time;
768
769 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
770 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
771 if (n < 0)
772 return -1;
773
774 /* flush time is going to be the minimum of all the individual
775 * mmap times. Essentially, we flush all the samples queued up
776 * from the last pass under our minimal start time -- that leaves
777 * a very small race for samples to come in with a lower timestamp.
778 * The ioctl to return the perf_clock timestamp should close the
779 * race entirely.
780 */
781 if (mmap_time < flush_time)
782 flush_time = mmap_time;
783
784 ntotal += n;
785 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
786 throttled = 1;
787 }
788
789 /* flush queue after each round in which we processed events */
790 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300791 struct ordered_events *oe = &kvm->session->ordered_events;
792
793 oe->next_flush = flush_time;
794 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400795 if (err) {
796 if (kvm->lost_events)
797 pr_info("\nLost events: %" PRIu64 "\n\n",
798 kvm->lost_events);
799 return err;
800 }
801 }
802
803 return throttled;
804}
805
806static volatile int done;
807
808static void sig_handler(int sig __maybe_unused)
809{
810 done = 1;
811}
812
813static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
814{
815 struct itimerspec new_value;
816 int rc = -1;
817
818 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
819 if (kvm->timerfd < 0) {
820 pr_err("timerfd_create failed\n");
821 goto out;
822 }
823
824 new_value.it_value.tv_sec = kvm->display_time;
825 new_value.it_value.tv_nsec = 0;
826 new_value.it_interval.tv_sec = kvm->display_time;
827 new_value.it_interval.tv_nsec = 0;
828
829 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
830 pr_err("timerfd_settime failed: %d\n", errno);
831 close(kvm->timerfd);
832 goto out;
833 }
834
835 rc = 0;
836out:
837 return rc;
838}
839
840static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
841{
842 uint64_t c;
843 int rc;
844
845 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
846 if (rc < 0) {
847 if (errno == EAGAIN)
848 return 0;
849
850 pr_err("Failed to read timer fd: %d\n", errno);
851 return -1;
852 }
853
854 if (rc != sizeof(uint64_t)) {
855 pr_err("Error reading timer fd - invalid size returned\n");
856 return -1;
857 }
858
859 if (c != 1)
860 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
861
862 /* update display */
863 sort_result(kvm);
864 print_result(kvm);
865
866 /* reset counts */
867 clear_events_cache_stats(kvm->kvm_events_cache);
868 kvm->total_count = 0;
869 kvm->total_time = 0;
870 kvm->lost_events = 0;
871
872 return 0;
873}
874
875static int fd_set_nonblock(int fd)
876{
877 long arg = 0;
878
879 arg = fcntl(fd, F_GETFL);
880 if (arg < 0) {
881 pr_err("Failed to get current flags for fd %d\n", fd);
882 return -1;
883 }
884
885 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
886 pr_err("Failed to set non-block option on fd %d\n", fd);
887 return -1;
888 }
889
890 return 0;
891}
892
Jiri Olsad5b41302014-08-11 10:50:09 +0200893static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400894{
895 int c;
896
David Ahern1afe1d12013-08-05 21:41:34 -0400897 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400898 if (c == 'q')
899 return 1;
900
901 return 0;
902}
903
904static int kvm_events_live_report(struct perf_kvm_stat *kvm)
905{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200906 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200907 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400908
909 /* live flag must be set first */
910 kvm->live = true;
911
912 ret = cpu_isa_config(kvm);
913 if (ret < 0)
914 return ret;
915
916 if (!verify_vcpu(kvm->trace_vcpu) ||
917 !select_key(kvm) ||
918 !register_kvm_events_ops(kvm)) {
919 goto out;
920 }
921
Jiri Olsad5b41302014-08-11 10:50:09 +0200922 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400923 init_kvm_event_record(kvm);
924
David Ahern1afe1d12013-08-05 21:41:34 -0400925 signal(SIGINT, sig_handler);
926 signal(SIGTERM, sig_handler);
927
David Ahern1afe1d12013-08-05 21:41:34 -0400928 /* add timer fd */
929 if (perf_kvm__timerfd_create(kvm) < 0) {
930 err = -1;
931 goto out;
932 }
933
Jiri Olsafe636ad2014-10-01 18:00:27 +0200934 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300935 goto out;
936
Jiri Olsa0cae0132014-10-01 18:00:28 +0200937 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
938 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300939 goto out;
940
David Ahern1afe1d12013-08-05 21:41:34 -0400941 if (fd_set_nonblock(fileno(stdin)) != 0)
942 goto out;
943
944 /* everything is good - enable the events and process */
945 perf_evlist__enable(kvm->evlist);
946
947 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200948 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400949 int rc;
950
951 rc = perf_kvm__mmap_read(kvm);
952 if (rc < 0)
953 break;
954
955 err = perf_kvm__handle_timerfd(kvm);
956 if (err)
957 goto out;
958
Jiri Olsa1ca72262014-10-01 18:00:29 +0200959 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200960 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400961
962 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200963 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400964 }
965
966 perf_evlist__disable(kvm->evlist);
967
968 if (err == 0) {
969 sort_result(kvm);
970 print_result(kvm);
971 }
972
973out:
974 if (kvm->timerfd >= 0)
975 close(kvm->timerfd);
976
Jiri Olsad5b41302014-08-11 10:50:09 +0200977 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -0400978 return err;
979}
980
981static int kvm_live_open_events(struct perf_kvm_stat *kvm)
982{
983 int err, rc = -1;
984 struct perf_evsel *pos;
985 struct perf_evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +0000986 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -0400987
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300988 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -0400989
990 /*
991 * Note: exclude_{guest,host} do not apply here.
992 * This command processes KVM tracepoints from host only
993 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300994 evlist__for_each_entry(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -0400995 struct perf_event_attr *attr = &pos->attr;
996
997 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +0300998 perf_evsel__set_sample_bit(pos, TID);
999 perf_evsel__set_sample_bit(pos, TIME);
1000 perf_evsel__set_sample_bit(pos, CPU);
1001 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001002 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001003 perf_evsel__reset_sample_bit(pos, PERIOD);
1004 perf_evsel__reset_sample_bit(pos, IP);
1005 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1006 perf_evsel__reset_sample_bit(pos, ADDR);
1007 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001008 attr->mmap = 0;
1009 attr->comm = 0;
1010 attr->task = 0;
1011
1012 attr->sample_period = 1;
1013
1014 attr->watermark = 0;
1015 attr->wakeup_events = 1000;
1016
1017 /* will enable all once we are ready */
1018 attr->disabled = 1;
1019 }
1020
1021 err = perf_evlist__open(evlist);
1022 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001023 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001024 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001025 goto out;
1026 }
1027
1028 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001029 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001030 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001031 perf_evlist__close(evlist);
1032 goto out;
1033 }
1034
1035 rc = 0;
1036
1037out:
1038 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001039}
David Ahern87419c92013-10-29 10:43:16 -06001040#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001041
Xiao Guangrong37860632012-11-15 14:17:01 +08001042static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001043{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001044 int ret;
1045
David Ahernde332ac2012-10-02 22:09:53 -06001046 struct perf_tool eops = {
1047 .sample = process_sample_event,
1048 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301049 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001050 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001051 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001052 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001053 .path = kvm->file_name,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001054 .mode = PERF_DATA_MODE_READ,
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001055 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001056 };
David Ahernde332ac2012-10-02 22:09:53 -06001057
1058 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001059 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001060 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001061 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001062 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001063 }
1064
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001065 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001066
Taeung Song41b98362015-07-01 00:18:44 +09001067 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1068 ret = -EINVAL;
1069 goto out_delete;
1070 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001071
1072 /*
1073 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1074 * traced in the old kernel.
1075 */
David Ahern1afe1d12013-08-05 21:41:34 -04001076 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001077 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001078 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001079
Taeung Song41b98362015-07-01 00:18:44 +09001080 ret = perf_session__process_events(kvm->session);
1081
1082out_delete:
1083 perf_session__delete(kvm->session);
1084 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001085}
1086
David Ahern2e73f002013-08-05 21:41:37 -04001087static int parse_target_str(struct perf_kvm_stat *kvm)
1088{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001089 if (kvm->opts.target.pid) {
1090 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001091 if (kvm->pid_list == NULL) {
1092 pr_err("Error parsing process id string\n");
1093 return -EINVAL;
1094 }
1095 }
1096
1097 return 0;
1098}
1099
Xiao Guangrong37860632012-11-15 14:17:01 +08001100static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001101{
1102 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001103 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001104
David Ahern2e73f002013-08-05 21:41:37 -04001105 if (parse_target_str(kvm) != 0)
1106 goto exit;
1107
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001108 if (!verify_vcpu(vcpu))
1109 goto exit;
1110
David Ahernde332ac2012-10-02 22:09:53 -06001111 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001112 goto exit;
1113
David Ahernde332ac2012-10-02 22:09:53 -06001114 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001115 goto exit;
1116
David Ahernde332ac2012-10-02 22:09:53 -06001117 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001118 setup_pager();
1119
David Ahernde332ac2012-10-02 22:09:53 -06001120 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001121 if (ret)
1122 goto exit;
1123
David Ahernde332ac2012-10-02 22:09:53 -06001124 sort_result(kvm);
1125 print_result(kvm);
1126
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001127exit:
1128 return ret;
1129}
1130
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001131#define STRDUP_FAIL_EXIT(s) \
1132 ({ char *_p; \
1133 _p = strdup(s); \
1134 if (!_p) \
1135 return -ENOMEM; \
1136 _p; \
1137 })
1138
Hemant Kumar066d3592016-01-28 12:03:06 +05301139int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1140{
1141 return 0;
1142}
1143
Xiao Guangrong37860632012-11-15 14:17:01 +08001144static int
1145kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001146{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001147 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001148 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001149 const char * const record_args[] = {
1150 "record",
1151 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001152 "-m", "1024",
1153 "-c", "1",
1154 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001155 const char * const kvm_stat_record_usage[] = {
1156 "perf kvm stat record [<options>]",
1157 NULL
1158 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001159 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301160 int ret;
1161
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001162 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301163 ret = setup_kvm_events_tp(kvm);
1164 if (ret < 0) {
1165 pr_err("Unable to setup the kvm tracepoints\n");
1166 return ret;
1167 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001168
1169 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1170 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001171
David Ahern8fdd84c2013-08-02 14:05:42 -06001172 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001173 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001174 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1175
1176 if (rec_argv == NULL)
1177 return -ENOMEM;
1178
1179 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1180 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1181
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001182 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001183 rec_argv[i++] = "-e";
1184 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1185 }
1186
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001187 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001188 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001189
1190 for (j = 1; j < (unsigned int)argc; j++, i++)
1191 rec_argv[i] = argv[j];
1192
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001193 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1194 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1195 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1196
1197 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1198 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1199 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1200 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1201 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1202 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1203 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1204 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1205 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1206 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1207 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1208 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1209 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1210 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1211 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1212
1213 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001214 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001215}
1216
Xiao Guangrong37860632012-11-15 14:17:01 +08001217static int
1218kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001219{
David Ahernde332ac2012-10-02 22:09:53 -06001220 const struct option kvm_events_report_options[] = {
1221 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001222 "event for reporting: vmexit, "
1223 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001224 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1225 "vcpu id to report"),
1226 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1227 "key for sorting: sample(sort by samples number)"
1228 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001229 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001230 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001231 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001232 OPT_END()
1233 };
1234
1235 const char * const kvm_events_report_usage[] = {
1236 "perf kvm stat report [<options>]",
1237 NULL
1238 };
1239
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001240 if (argc) {
1241 argc = parse_options(argc, argv,
1242 kvm_events_report_options,
1243 kvm_events_report_usage, 0);
1244 if (argc)
1245 usage_with_options(kvm_events_report_usage,
1246 kvm_events_report_options);
1247 }
1248
Alexander Yaryginf1819572014-09-01 17:44:54 +04001249 if (!kvm->opts.target.pid)
1250 kvm->opts.target.system_wide = true;
1251
David Ahernde332ac2012-10-02 22:09:53 -06001252 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001253}
1254
David Ahern87419c92013-10-29 10:43:16 -06001255#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001256static struct perf_evlist *kvm_live_event_list(void)
1257{
1258 struct perf_evlist *evlist;
1259 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001260 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001261 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001262
1263 evlist = perf_evlist__new();
1264 if (evlist == NULL)
1265 return NULL;
1266
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001267 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001268
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001269 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001270 if (tp == NULL)
1271 goto out;
1272
1273 /* split tracepoint into subsystem and name */
1274 sys = tp;
1275 name = strchr(tp, ':');
1276 if (name == NULL) {
1277 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001278 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001279 free(tp);
1280 goto out;
1281 }
1282 *name = '\0';
1283 name++;
1284
1285 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001286 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001287 free(tp);
1288 goto out;
1289 }
1290
1291 free(tp);
1292 }
1293
1294 err = 0;
1295
1296out:
1297 if (err) {
1298 perf_evlist__delete(evlist);
1299 evlist = NULL;
1300 }
1301
1302 return evlist;
1303}
1304
1305static int kvm_events_live(struct perf_kvm_stat *kvm,
1306 int argc, const char **argv)
1307{
1308 char errbuf[BUFSIZ];
1309 int err;
1310
1311 const struct option live_options[] = {
1312 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1313 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001314 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1315 "number of mmap data pages",
1316 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001317 OPT_INCR('v', "verbose", &verbose,
1318 "be more verbose (show counter open errors, etc)"),
1319 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1320 "system-wide collection from all CPUs"),
1321 OPT_UINTEGER('d', "display", &kvm->display_time,
1322 "time in seconds between display updates"),
1323 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001324 "event for reporting: "
1325 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001326 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1327 "vcpu id to report"),
1328 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1329 "key for sorting: sample(sort by samples number)"
1330 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001331 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001332 "show events other than"
1333 " HLT (x86 only) or Wait state (s390 only)"
1334 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001335 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1336 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001337 OPT_END()
1338 };
1339 const char * const live_usage[] = {
1340 "perf kvm stat live [<options>]",
1341 NULL
1342 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001343 struct perf_data_file file = {
1344 .mode = PERF_DATA_MODE_WRITE,
1345 };
David Ahern1afe1d12013-08-05 21:41:34 -04001346
1347
1348 /* event handling */
1349 kvm->tool.sample = process_sample_event;
1350 kvm->tool.comm = perf_event__process_comm;
1351 kvm->tool.exit = perf_event__process_exit;
1352 kvm->tool.fork = perf_event__process_fork;
1353 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301354 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001355 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001356 perf_tool__fill_defaults(&kvm->tool);
1357
1358 /* set defaults */
1359 kvm->display_time = 1;
1360 kvm->opts.user_interval = 1;
1361 kvm->opts.mmap_pages = 512;
1362 kvm->opts.target.uses_mmap = false;
1363 kvm->opts.target.uid_str = NULL;
1364 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001365 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001366
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001367 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001368 disable_buildid_cache();
1369
1370 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001371
1372 if (argc) {
1373 argc = parse_options(argc, argv, live_options,
1374 live_usage, 0);
1375 if (argc)
1376 usage_with_options(live_usage, live_options);
1377 }
1378
David Ahern70f7b4a2013-08-07 21:56:38 -04001379 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1380
David Ahern1afe1d12013-08-05 21:41:34 -04001381 /*
1382 * target related setups
1383 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001384 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001385 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001386 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001387 ui__warning("%s", errbuf);
1388 }
1389
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001390 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001391 kvm->opts.target.system_wide = true;
1392
1393
1394 /*
1395 * generate the event list
1396 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301397 err = setup_kvm_events_tp(kvm);
1398 if (err < 0) {
1399 pr_err("Unable to setup the kvm tracepoints\n");
1400 return err;
1401 }
1402
David Ahern1afe1d12013-08-05 21:41:34 -04001403 kvm->evlist = kvm_live_event_list();
1404 if (kvm->evlist == NULL) {
1405 err = -1;
1406 goto out;
1407 }
1408
1409 symbol_conf.nr_events = kvm->evlist->nr_entries;
1410
1411 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1412 usage_with_options(live_usage, live_options);
1413
1414 /*
1415 * perf session
1416 */
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001417 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001418 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001419 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001420 goto out;
1421 }
1422 kvm->session->evlist = kvm->evlist;
1423 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001424 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001425 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001426 kvm->evlist->threads, false, kvm->opts.proc_map_timeout);
David Ahern1afe1d12013-08-05 21:41:34 -04001427 err = kvm_live_open_events(kvm);
1428 if (err)
1429 goto out;
1430
1431 err = kvm_events_live_report(kvm);
1432
1433out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001434 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001435 kvm->session = NULL;
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -03001436 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001437
1438 return err;
1439}
David Ahern87419c92013-10-29 10:43:16 -06001440#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001441
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001442static void print_kvm_stat_usage(void)
1443{
1444 printf("Usage: perf kvm stat <command>\n\n");
1445
1446 printf("# Available commands:\n");
1447 printf("\trecord: record kvm events\n");
1448 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001449 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001450
1451 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1452}
1453
Xiao Guangrong37860632012-11-15 14:17:01 +08001454static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001455{
Xiao Guangrong37860632012-11-15 14:17:01 +08001456 struct perf_kvm_stat kvm = {
1457 .file_name = file_name,
1458
1459 .trace_vcpu = -1,
1460 .report_event = "vmexit",
1461 .sort_key = "sample",
1462
Xiao Guangrong37860632012-11-15 14:17:01 +08001463 };
1464
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001465 if (argc == 1) {
1466 print_kvm_stat_usage();
1467 goto perf_stat;
1468 }
1469
1470 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001471 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001472
1473 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001474 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001475
David Ahern87419c92013-10-29 10:43:16 -06001476#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001477 if (!strncmp(argv[1], "live", 4))
1478 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001479#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001480
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001481perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001482 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001483}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001484#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001485
Xiao Guangrong37860632012-11-15 14:17:01 +08001486static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001487{
1488 int rec_argc, i = 0, j;
1489 const char **rec_argv;
1490
1491 rec_argc = argc + 2;
1492 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1493 rec_argv[i++] = strdup("record");
1494 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001495 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001496 for (j = 1; j < argc; j++, i++)
1497 rec_argv[i] = argv[j];
1498
1499 BUG_ON(i != rec_argc);
1500
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001501 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001502}
1503
Xiao Guangrong37860632012-11-15 14:17:01 +08001504static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001505{
1506 int rec_argc, i = 0, j;
1507 const char **rec_argv;
1508
1509 rec_argc = argc + 2;
1510 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1511 rec_argv[i++] = strdup("report");
1512 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001513 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001514 for (j = 1; j < argc; j++, i++)
1515 rec_argv[i] = argv[j];
1516
1517 BUG_ON(i != rec_argc);
1518
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001519 return cmd_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001520}
1521
Xiao Guangrong37860632012-11-15 14:17:01 +08001522static int
1523__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001524{
1525 int rec_argc, i = 0, j;
1526 const char **rec_argv;
1527
1528 rec_argc = argc + 2;
1529 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1530 rec_argv[i++] = strdup("buildid-list");
1531 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001532 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001533 for (j = 1; j < argc; j++, i++)
1534 rec_argv[i] = argv[j];
1535
1536 BUG_ON(i != rec_argc);
1537
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001538 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001539}
1540
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001541int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001542{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001543 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001544 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001545 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001546 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001547 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001548 "Output file name"),
1549 OPT_BOOLEAN(0, "guest", &perf_guest,
1550 "Collect guest os data"),
1551 OPT_BOOLEAN(0, "host", &perf_host,
1552 "Collect host os data"),
1553 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1554 "guest mount directory under which every guest os"
1555 " instance has a subdir"),
1556 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1557 "file", "file saving guest os vmlinux"),
1558 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1559 "file", "file saving guest os /proc/kallsyms"),
1560 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1561 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001562 OPT_INCR('v', "verbose", &verbose,
1563 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001564 OPT_END()
1565 };
1566
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001567 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1568 "buildid-list", "stat", NULL };
1569 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001570
Joerg Roedel1aed2672012-01-04 17:54:20 +01001571 perf_host = 0;
1572 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001573
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001574 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1575 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001576 if (!argc)
1577 usage_with_options(kvm_usage, kvm_options);
1578
1579 if (!perf_host)
1580 perf_guest = 1;
1581
Xiao Guangrong37860632012-11-15 14:17:01 +08001582 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001583 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001584
Xiao Guangrong37860632012-11-15 14:17:01 +08001585 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001586 pr_err("Failed to allocate memory for filename\n");
1587 return -ENOMEM;
1588 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001589 }
1590
1591 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001592 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001593 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001594 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001595 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001596 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001597 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001598 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001599 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001600 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001601#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001602 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001603 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001604#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001605 else
1606 usage_with_options(kvm_usage, kvm_options);
1607
1608 return 0;
1609}