blob: f309c3773522dee5c5c8a515b1c278757fca58c6 [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"
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -03006#include "util/term.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08007#include "util/util.h"
8#include "util/cache.h"
9#include "util/symbol.h"
10#include "util/thread.h"
11#include "util/header.h"
12#include "util/session.h"
David Ahern2e73f002013-08-05 21:41:37 -040013#include "util/intlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060014#include <subcmd/parse-options.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080016#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080017#include "util/tool.h"
18#include "util/stat.h"
David Ahern1afe1d12013-08-05 21:41:34 -040019#include "util/top.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020020#include "util/data.h"
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030021#include "util/ordered-events.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080022
23#include <sys/prctl.h>
David Ahern87419c92013-10-29 10:43:16 -060024#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -040025#include <sys/timerfd.h>
David Ahern87419c92013-10-29 10:43:16 -060026#endif
Arnaldo Carvalho de Meloc5e40272017-04-19 16:12:39 -030027#include <sys/time.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080028
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030030#include <linux/time64.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030031#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030032#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030033#include <poll.h>
David Ahern1afe1d12013-08-05 21:41:34 -040034#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080035#include <semaphore.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030036#include <signal.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080037#include <pthread.h>
38#include <math.h>
39
Arnaldo Carvalho de Melobb8c16d2017-04-19 16:15:13 -030040static const char *get_filename_for_perf_kvm(void)
41{
42 const char *filename;
43
44 if (perf_host && !perf_guest)
45 filename = strdup("perf.data.host");
46 else if (!perf_host && perf_guest)
47 filename = strdup("perf.data.guest");
48 else
49 filename = strdup("perf.data.kvm");
50
51 return filename;
52}
53
Alexander Yaryginda50ad62014-07-03 17:59:49 +040054#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040055#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080056
Alexander Yarygin9daa8122014-07-03 18:29:05 +040057void exit_event_get_key(struct perf_evsel *evsel,
58 struct perf_sample *sample,
59 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080060{
61 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053062 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080063}
64
Alexander Yarygin9daa8122014-07-03 18:29:05 +040065bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080066{
Hemant Kumar162607e2016-01-28 12:03:04 +053067 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080068}
69
Alexander Yarygin9daa8122014-07-03 18:29:05 +040070bool exit_event_begin(struct perf_evsel *evsel,
71 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080072{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030073 if (kvm_exit_event(evsel)) {
74 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080075 return true;
76 }
77
78 return false;
79}
80
Alexander Yarygin9daa8122014-07-03 18:29:05 +040081bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080082{
Hemant Kumar162607e2016-01-28 12:03:04 +053083 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080084}
85
Alexander Yarygin9daa8122014-07-03 18:29:05 +040086bool exit_event_end(struct perf_evsel *evsel,
87 struct perf_sample *sample __maybe_unused,
88 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080089{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030090 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080091}
92
Alexander Yarygindf74c132014-07-03 17:59:50 +040093static const char *get_exit_reason(struct perf_kvm_stat *kvm,
94 struct exit_reasons_table *tbl,
95 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080096{
Alexander Yarygindf74c132014-07-03 17:59:50 +040097 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060098 if (tbl->exit_code == exit_code)
99 return tbl->reason;
100 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800101 }
102
103 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600104 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800105 return "UNKNOWN";
106}
107
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400108void exit_event_decode_key(struct perf_kvm_stat *kvm,
109 struct event_key *key,
110 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800111{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400112 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +0400113 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800114
Hemant Kumar162607e2016-01-28 12:03:04 +0530115 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800116}
117
Xiao Guangrong37860632012-11-15 14:17:01 +0800118static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800119{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400120 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800121
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400122 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
123 if (!strcmp(events_ops->name, kvm->report_event)) {
124 kvm->events_ops = events_ops->ops;
125 return true;
126 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800127 }
128
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400129 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800130}
131
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800132struct vcpu_event_record {
133 int vcpu_id;
134 u64 start_time;
135 struct kvm_event *last_event;
136};
137
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800138
Xiao Guangrong37860632012-11-15 14:17:01 +0800139static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800140{
David Ahernb880dee2012-10-08 11:17:30 -0600141 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800142
David Ahernb880dee2012-10-08 11:17:30 -0600143 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600144 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800145}
146
David Ahern87419c92013-10-29 10:43:16 -0600147#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400148static void clear_events_cache_stats(struct list_head *kvm_events_cache)
149{
150 struct list_head *head;
151 struct kvm_event *event;
152 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400153 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400154
155 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
156 head = &kvm_events_cache[i];
157 list_for_each_entry(event, head, hash_entry) {
158 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400159 event->total.time = 0;
160 init_stats(&event->total.stats);
161
162 for (j = 0; j < event->max_vcpu; ++j) {
163 event->vcpu[j].time = 0;
164 init_stats(&event->vcpu[j].stats);
165 }
David Ahern1afe1d12013-08-05 21:41:34 -0400166 }
167 }
168}
David Ahern87419c92013-10-29 10:43:16 -0600169#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400170
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800171static int kvm_events_hash_fn(u64 key)
172{
173 return key & (EVENTS_CACHE_SIZE - 1);
174}
175
176static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
177{
178 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600179 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800180
181 if (vcpu_id < event->max_vcpu)
182 return true;
183
184 while (event->max_vcpu <= vcpu_id)
185 event->max_vcpu += DEFAULT_VCPU_NUM;
186
David Ahern6ca5f302013-05-25 18:24:46 -0600187 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800188 event->vcpu = realloc(event->vcpu,
189 event->max_vcpu * sizeof(*event->vcpu));
190 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600191 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800192 pr_err("Not enough memory\n");
193 return false;
194 }
195
196 memset(event->vcpu + old_max_vcpu, 0,
197 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
198 return true;
199}
200
201static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
202{
203 struct kvm_event *event;
204
205 event = zalloc(sizeof(*event));
206 if (!event) {
207 pr_err("Not enough memory\n");
208 return NULL;
209 }
210
211 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200212 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800213 return event;
214}
215
Xiao Guangrong37860632012-11-15 14:17:01 +0800216static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600217 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800218{
219 struct kvm_event *event;
220 struct list_head *head;
221
222 BUG_ON(key->key == INVALID_KEY);
223
David Ahernde332ac2012-10-02 22:09:53 -0600224 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600225 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800226 if (event->key.key == key->key && event->key.info == key->info)
227 return event;
David Ahern355afe82012-10-08 11:17:32 -0600228 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800229
230 event = kvm_alloc_init_event(key);
231 if (!event)
232 return NULL;
233
234 list_add(&event->hash_entry, head);
235 return event;
236}
237
Xiao Guangrong37860632012-11-15 14:17:01 +0800238static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600239 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800240 struct event_key *key, u64 timestamp)
241{
242 struct kvm_event *event = NULL;
243
244 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600245 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800246
247 vcpu_record->last_event = event;
248 vcpu_record->start_time = timestamp;
249 return true;
250}
251
252static void
253kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
254{
255 kvm_stats->time += time_diff;
256 update_stats(&kvm_stats->stats, time_diff);
257}
258
259static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
260{
261 struct kvm_event_stats *kvm_stats = &event->total;
262
263 if (vcpu_id != -1)
264 kvm_stats = &event->vcpu[vcpu_id];
265
266 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
267 avg_stats(&kvm_stats->stats));
268}
269
270static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
271 u64 time_diff)
272{
David Ahern2aa8eab2012-10-08 11:17:35 -0600273 if (vcpu_id == -1) {
274 kvm_update_event_stats(&event->total, time_diff);
275 return true;
276 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800277
278 if (!kvm_event_expand(event, vcpu_id))
279 return false;
280
281 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
282 return true;
283}
284
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400285static bool is_child_event(struct perf_kvm_stat *kvm,
286 struct perf_evsel *evsel,
287 struct perf_sample *sample,
288 struct event_key *key)
289{
290 struct child_event_ops *child_ops;
291
292 child_ops = kvm->events_ops->child_ops;
293
294 if (!child_ops)
295 return false;
296
297 for (; child_ops->name; child_ops++) {
298 if (!strcmp(evsel->name, child_ops->name)) {
299 child_ops->get_key(evsel, sample, key);
300 return true;
301 }
302 }
303
304 return false;
305}
306
307static bool handle_child_event(struct perf_kvm_stat *kvm,
308 struct vcpu_event_record *vcpu_record,
309 struct event_key *key,
310 struct perf_sample *sample __maybe_unused)
311{
312 struct kvm_event *event = NULL;
313
314 if (key->key != INVALID_KEY)
315 event = find_create_kvm_event(kvm, key);
316
317 vcpu_record->last_event = event;
318
319 return true;
320}
321
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400322static bool skip_event(const char *event)
323{
324 const char * const *skip_events;
325
326 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
327 if (!strcmp(event, *skip_events))
328 return true;
329
330 return false;
331}
332
Xiao Guangrong37860632012-11-15 14:17:01 +0800333static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600334 struct vcpu_event_record *vcpu_record,
335 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400336 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800337{
338 struct kvm_event *event;
339 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600340 int vcpu;
341
342 if (kvm->trace_vcpu == -1)
343 vcpu = -1;
344 else
345 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800346
347 event = vcpu_record->last_event;
348 time_begin = vcpu_record->start_time;
349
350 /* The begin event is not caught. */
351 if (!time_begin)
352 return true;
353
354 /*
355 * In some case, the 'begin event' only records the start timestamp,
356 * the actual event is recognized in the 'end event' (e.g. mmio-event).
357 */
358
359 /* Both begin and end events did not get the key. */
360 if (!event && key->key == INVALID_KEY)
361 return true;
362
363 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600364 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800365
366 if (!event)
367 return false;
368
369 vcpu_record->last_event = NULL;
370 vcpu_record->start_time = 0;
371
David Ahern1afe1d12013-08-05 21:41:34 -0400372 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400373 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400374 pr_debug("End time before begin time; skipping event.\n");
375 return true;
376 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800377
David Ahern70f7b4a2013-08-07 21:56:38 -0400378 time_diff = sample->time - time_begin;
379
380 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530381 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400382
383 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400384 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400385 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
386 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300387 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400388 }
389 }
390
David Ahern2aa8eab2012-10-08 11:17:35 -0600391 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800392}
393
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300394static
395struct vcpu_event_record *per_vcpu_record(struct thread *thread,
396 struct perf_evsel *evsel,
397 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800398{
399 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900400 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800401 struct vcpu_event_record *vcpu_record;
402
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300403 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800404 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300405 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800406 return NULL;
407 }
408
Hemant Kumar162607e2016-01-28 12:03:04 +0530409 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
410 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900411 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800412 }
413
Namhyung Kim69e865c2014-10-06 09:46:02 +0900414 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800415}
416
Xiao Guangrong37860632012-11-15 14:17:01 +0800417static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600418 struct thread *thread,
419 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300420 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800421{
422 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400423 struct event_key key = { .key = INVALID_KEY,
424 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800425
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300426 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800427 if (!vcpu_record)
428 return true;
429
David Ahern2aa8eab2012-10-08 11:17:35 -0600430 /* only process events for vcpus user cares about */
431 if ((kvm->trace_vcpu != -1) &&
432 (kvm->trace_vcpu != vcpu_record->vcpu_id))
433 return true;
434
David Ahernde332ac2012-10-02 22:09:53 -0600435 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
436 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800437
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400438 if (is_child_event(kvm, evsel, sample, &key))
439 return handle_child_event(kvm, vcpu_record, &key, sample);
440
David Ahernde332ac2012-10-02 22:09:53 -0600441 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400442 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800443
444 return true;
445}
446
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800447#define GET_EVENT_KEY(func, field) \
448static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
449{ \
450 if (vcpu == -1) \
451 return event->total.field; \
452 \
453 if (vcpu >= event->max_vcpu) \
454 return 0; \
455 \
456 return event->vcpu[vcpu].field; \
457}
458
459#define COMPARE_EVENT_KEY(func, field) \
460GET_EVENT_KEY(func, field) \
461static int compare_kvm_event_ ## func(struct kvm_event *one, \
462 struct kvm_event *two, int vcpu)\
463{ \
464 return get_event_ ##func(one, vcpu) > \
465 get_event_ ##func(two, vcpu); \
466}
467
468GET_EVENT_KEY(time, time);
469COMPARE_EVENT_KEY(count, stats.n);
470COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400471GET_EVENT_KEY(max, stats.max);
472GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800473
474#define DEF_SORT_NAME_KEY(name, compare_key) \
475 { #name, compare_kvm_event_ ## compare_key }
476
477static struct kvm_event_key keys[] = {
478 DEF_SORT_NAME_KEY(sample, count),
479 DEF_SORT_NAME_KEY(time, mean),
480 { NULL, NULL }
481};
482
Xiao Guangrong37860632012-11-15 14:17:01 +0800483static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800484{
485 int i;
486
487 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600488 if (!strcmp(keys[i].name, kvm->sort_key)) {
489 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800490 return true;
491 }
492 }
493
David Ahernde332ac2012-10-02 22:09:53 -0600494 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800495 return false;
496}
497
David Ahernde332ac2012-10-02 22:09:53 -0600498static void insert_to_result(struct rb_root *result, struct kvm_event *event,
499 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800500{
David Ahernde332ac2012-10-02 22:09:53 -0600501 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800502 struct rb_node *parent = NULL;
503 struct kvm_event *p;
504
505 while (*rb) {
506 p = container_of(*rb, struct kvm_event, rb);
507 parent = *rb;
508
509 if (bigger(event, p, vcpu))
510 rb = &(*rb)->rb_left;
511 else
512 rb = &(*rb)->rb_right;
513 }
514
515 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600516 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800517}
518
Xiao Guangrong37860632012-11-15 14:17:01 +0800519static void
520update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800521{
David Ahernde332ac2012-10-02 22:09:53 -0600522 int vcpu = kvm->trace_vcpu;
523
524 kvm->total_count += get_event_count(event, vcpu);
525 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800526}
527
528static bool event_is_valid(struct kvm_event *event, int vcpu)
529{
530 return !!get_event_count(event, vcpu);
531}
532
Xiao Guangrong37860632012-11-15 14:17:01 +0800533static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800534{
535 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600536 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800537 struct kvm_event *event;
538
David Ahern355afe82012-10-08 11:17:32 -0600539 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
540 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800541 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600542 update_total_count(kvm, event);
543 insert_to_result(&kvm->result, event,
544 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800545 }
David Ahern355afe82012-10-08 11:17:32 -0600546 }
547 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800548}
549
550/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600551static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800552{
David Ahernde332ac2012-10-02 22:09:53 -0600553 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800554
555 if (!node)
556 return NULL;
557
David Ahernde332ac2012-10-02 22:09:53 -0600558 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800559 return container_of(node, struct kvm_event, rb);
560}
561
David Ahern1afe1d12013-08-05 21:41:34 -0400562static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800563{
David Ahern1afe1d12013-08-05 21:41:34 -0400564 int vcpu = kvm->trace_vcpu;
565
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800566 pr_info("Analyze events for ");
567
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400568 if (kvm->opts.target.system_wide)
569 pr_info("all VMs, ");
570 else if (kvm->opts.target.pid)
571 pr_info("pid(s) %s, ", kvm->opts.target.pid);
572 else
573 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400574
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800575 if (vcpu == -1)
576 pr_info("all VCPUs:\n\n");
577 else
578 pr_info("VCPU %d:\n\n", vcpu);
579}
580
David Ahern1afe1d12013-08-05 21:41:34 -0400581static void show_timeofday(void)
582{
583 char date[64];
584 struct timeval tv;
585 struct tm ltime;
586
587 gettimeofday(&tv, NULL);
588 if (localtime_r(&tv.tv_sec, &ltime)) {
589 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
590 pr_info("%s.%06ld", date, tv.tv_usec);
591 } else
592 pr_info("00:00:00.000000");
593
594 return;
595}
596
Xiao Guangrong37860632012-11-15 14:17:01 +0800597static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800598{
Hemant Kumar162607e2016-01-28 12:03:04 +0530599 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800600 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600601 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800602
David Ahern1afe1d12013-08-05 21:41:34 -0400603 if (kvm->live) {
604 puts(CONSOLE_CLEAR);
605 show_timeofday();
606 }
607
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800608 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400609 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530610 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800611 pr_info("%10s ", "Samples");
612 pr_info("%9s ", "Samples%");
613
614 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200615 pr_info("%11s ", "Min Time");
616 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800617 pr_info("%16s ", "Avg time");
618 pr_info("\n\n");
619
David Ahernde332ac2012-10-02 22:09:53 -0600620 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400621 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800622
623 ecount = get_event_count(event, vcpu);
624 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400625 max = get_event_max(event, vcpu);
626 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800627
David Ahernde332ac2012-10-02 22:09:53 -0600628 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530629 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800630 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600631 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
632 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300633 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
634 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
635 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800636 kvm_event_rel_stddev(vcpu, event));
637 pr_info("\n");
638 }
639
David Aherne4f76372012-10-08 11:17:34 -0600640 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300641 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400642
643 if (kvm->lost_events)
644 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
645}
646
David Ahern87419c92013-10-29 10:43:16 -0600647#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400648static int process_lost_event(struct perf_tool *tool,
649 union perf_event *event __maybe_unused,
650 struct perf_sample *sample __maybe_unused,
651 struct machine *machine __maybe_unused)
652{
653 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
654
655 kvm->lost_events++;
656 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800657}
David Ahern87419c92013-10-29 10:43:16 -0600658#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800659
David Ahern2e73f002013-08-05 21:41:37 -0400660static bool skip_sample(struct perf_kvm_stat *kvm,
661 struct perf_sample *sample)
662{
663 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
664 return true;
665
666 return false;
667}
668
David Ahernde332ac2012-10-02 22:09:53 -0600669static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800670 union perf_event *event,
671 struct perf_sample *sample,
672 struct perf_evsel *evsel,
673 struct machine *machine)
674{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300675 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400676 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800677 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
678 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800679
David Ahern2e73f002013-08-05 21:41:37 -0400680 if (skip_sample(kvm, sample))
681 return 0;
682
Adrian Hunter314add62013-08-27 11:23:03 +0300683 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800684 if (thread == NULL) {
685 pr_debug("problem processing %d event, skipping it.\n",
686 event->header.type);
687 return -1;
688 }
689
David Ahernde332ac2012-10-02 22:09:53 -0600690 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300691 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800692
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300693 thread__put(thread);
694 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800695}
696
David Ahern1afe1d12013-08-05 21:41:34 -0400697static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800698{
David Ahern1afe1d12013-08-05 21:41:34 -0400699 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400700 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400701
702 if (kvm->live) {
703 err = get_cpuid(buf, sizeof(buf));
704 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400705 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400706 return err;
707 }
708 cpuid = buf;
709 } else
710 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800711
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400712 if (!cpuid) {
713 pr_err("Failed to look up CPU type\n");
714 return -EINVAL;
715 }
716
717 err = cpu_isa_init(kvm, cpuid);
718 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800719 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800720
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400721 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400722}
723
724static bool verify_vcpu(int vcpu)
725{
726 if (vcpu != -1 && vcpu < 0) {
727 pr_err("Invalid vcpu:%d.\n", vcpu);
728 return false;
729 }
730
731 return true;
732}
733
David Ahern87419c92013-10-29 10:43:16 -0600734#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400735/* keeping the max events to a modest level to keep
736 * the processing of samples per mmap smooth.
737 */
738#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
739
740static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
741 u64 *mmap_time)
742{
743 union perf_event *event;
744 struct perf_sample sample;
745 s64 n = 0;
746 int err;
747
748 *mmap_time = ULLONG_MAX;
749 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
750 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
751 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800752 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400753 pr_err("Failed to parse sample\n");
754 return -1;
755 }
756
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300757 err = perf_session__queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800758 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300759 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800760 * point to it, and it'll get possibly overwritten by the kernel.
761 */
762 perf_evlist__mmap_consume(kvm->evlist, idx);
763
David Ahern1afe1d12013-08-05 21:41:34 -0400764 if (err) {
765 pr_err("Failed to enqueue sample: %d\n", err);
766 return -1;
767 }
768
769 /* save time stamp of our first sample for this mmap */
770 if (n == 0)
771 *mmap_time = sample.time;
772
773 /* limit events per mmap handled all at once */
774 n++;
775 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
776 break;
777 }
778
779 return n;
780}
781
782static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
783{
784 int i, err, throttled = 0;
785 s64 n, ntotal = 0;
786 u64 flush_time = ULLONG_MAX, mmap_time;
787
788 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
789 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
790 if (n < 0)
791 return -1;
792
793 /* flush time is going to be the minimum of all the individual
794 * mmap times. Essentially, we flush all the samples queued up
795 * from the last pass under our minimal start time -- that leaves
796 * a very small race for samples to come in with a lower timestamp.
797 * The ioctl to return the perf_clock timestamp should close the
798 * race entirely.
799 */
800 if (mmap_time < flush_time)
801 flush_time = mmap_time;
802
803 ntotal += n;
804 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
805 throttled = 1;
806 }
807
808 /* flush queue after each round in which we processed events */
809 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300810 struct ordered_events *oe = &kvm->session->ordered_events;
811
812 oe->next_flush = flush_time;
813 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400814 if (err) {
815 if (kvm->lost_events)
816 pr_info("\nLost events: %" PRIu64 "\n\n",
817 kvm->lost_events);
818 return err;
819 }
820 }
821
822 return throttled;
823}
824
825static volatile int done;
826
827static void sig_handler(int sig __maybe_unused)
828{
829 done = 1;
830}
831
832static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
833{
834 struct itimerspec new_value;
835 int rc = -1;
836
837 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
838 if (kvm->timerfd < 0) {
839 pr_err("timerfd_create failed\n");
840 goto out;
841 }
842
843 new_value.it_value.tv_sec = kvm->display_time;
844 new_value.it_value.tv_nsec = 0;
845 new_value.it_interval.tv_sec = kvm->display_time;
846 new_value.it_interval.tv_nsec = 0;
847
848 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
849 pr_err("timerfd_settime failed: %d\n", errno);
850 close(kvm->timerfd);
851 goto out;
852 }
853
854 rc = 0;
855out:
856 return rc;
857}
858
859static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
860{
861 uint64_t c;
862 int rc;
863
864 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
865 if (rc < 0) {
866 if (errno == EAGAIN)
867 return 0;
868
869 pr_err("Failed to read timer fd: %d\n", errno);
870 return -1;
871 }
872
873 if (rc != sizeof(uint64_t)) {
874 pr_err("Error reading timer fd - invalid size returned\n");
875 return -1;
876 }
877
878 if (c != 1)
879 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
880
881 /* update display */
882 sort_result(kvm);
883 print_result(kvm);
884
885 /* reset counts */
886 clear_events_cache_stats(kvm->kvm_events_cache);
887 kvm->total_count = 0;
888 kvm->total_time = 0;
889 kvm->lost_events = 0;
890
891 return 0;
892}
893
894static int fd_set_nonblock(int fd)
895{
896 long arg = 0;
897
898 arg = fcntl(fd, F_GETFL);
899 if (arg < 0) {
900 pr_err("Failed to get current flags for fd %d\n", fd);
901 return -1;
902 }
903
904 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
905 pr_err("Failed to set non-block option on fd %d\n", fd);
906 return -1;
907 }
908
909 return 0;
910}
911
Jiri Olsad5b41302014-08-11 10:50:09 +0200912static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400913{
914 int c;
915
David Ahern1afe1d12013-08-05 21:41:34 -0400916 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400917 if (c == 'q')
918 return 1;
919
920 return 0;
921}
922
923static int kvm_events_live_report(struct perf_kvm_stat *kvm)
924{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200925 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200926 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400927
928 /* live flag must be set first */
929 kvm->live = true;
930
931 ret = cpu_isa_config(kvm);
932 if (ret < 0)
933 return ret;
934
935 if (!verify_vcpu(kvm->trace_vcpu) ||
936 !select_key(kvm) ||
937 !register_kvm_events_ops(kvm)) {
938 goto out;
939 }
940
Jiri Olsad5b41302014-08-11 10:50:09 +0200941 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400942 init_kvm_event_record(kvm);
943
David Ahern1afe1d12013-08-05 21:41:34 -0400944 signal(SIGINT, sig_handler);
945 signal(SIGTERM, sig_handler);
946
David Ahern1afe1d12013-08-05 21:41:34 -0400947 /* add timer fd */
948 if (perf_kvm__timerfd_create(kvm) < 0) {
949 err = -1;
950 goto out;
951 }
952
Jiri Olsafe636ad2014-10-01 18:00:27 +0200953 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300954 goto out;
955
Jiri Olsa0cae0132014-10-01 18:00:28 +0200956 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
957 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300958 goto out;
959
David Ahern1afe1d12013-08-05 21:41:34 -0400960 if (fd_set_nonblock(fileno(stdin)) != 0)
961 goto out;
962
963 /* everything is good - enable the events and process */
964 perf_evlist__enable(kvm->evlist);
965
966 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200967 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400968 int rc;
969
970 rc = perf_kvm__mmap_read(kvm);
971 if (rc < 0)
972 break;
973
974 err = perf_kvm__handle_timerfd(kvm);
975 if (err)
976 goto out;
977
Jiri Olsa1ca72262014-10-01 18:00:29 +0200978 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200979 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400980
981 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200982 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400983 }
984
985 perf_evlist__disable(kvm->evlist);
986
987 if (err == 0) {
988 sort_result(kvm);
989 print_result(kvm);
990 }
991
992out:
993 if (kvm->timerfd >= 0)
994 close(kvm->timerfd);
995
Jiri Olsad5b41302014-08-11 10:50:09 +0200996 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -0400997 return err;
998}
999
1000static int kvm_live_open_events(struct perf_kvm_stat *kvm)
1001{
1002 int err, rc = -1;
1003 struct perf_evsel *pos;
1004 struct perf_evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001005 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -04001006
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -03001007 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001008
1009 /*
1010 * Note: exclude_{guest,host} do not apply here.
1011 * This command processes KVM tracepoints from host only
1012 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001013 evlist__for_each_entry(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -04001014 struct perf_event_attr *attr = &pos->attr;
1015
1016 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +03001017 perf_evsel__set_sample_bit(pos, TID);
1018 perf_evsel__set_sample_bit(pos, TIME);
1019 perf_evsel__set_sample_bit(pos, CPU);
1020 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001021 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001022 perf_evsel__reset_sample_bit(pos, PERIOD);
1023 perf_evsel__reset_sample_bit(pos, IP);
1024 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1025 perf_evsel__reset_sample_bit(pos, ADDR);
1026 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001027 attr->mmap = 0;
1028 attr->comm = 0;
1029 attr->task = 0;
1030
1031 attr->sample_period = 1;
1032
1033 attr->watermark = 0;
1034 attr->wakeup_events = 1000;
1035
1036 /* will enable all once we are ready */
1037 attr->disabled = 1;
1038 }
1039
1040 err = perf_evlist__open(evlist);
1041 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001042 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001043 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001044 goto out;
1045 }
1046
1047 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001048 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001049 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001050 perf_evlist__close(evlist);
1051 goto out;
1052 }
1053
1054 rc = 0;
1055
1056out:
1057 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001058}
David Ahern87419c92013-10-29 10:43:16 -06001059#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001060
Xiao Guangrong37860632012-11-15 14:17:01 +08001061static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001062{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001063 int ret;
1064
David Ahernde332ac2012-10-02 22:09:53 -06001065 struct perf_tool eops = {
1066 .sample = process_sample_event,
1067 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301068 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001069 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001070 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001071 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001072 .path = kvm->file_name,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001073 .mode = PERF_DATA_MODE_READ,
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001074 .force = kvm->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001075 };
David Ahernde332ac2012-10-02 22:09:53 -06001076
1077 kvm->tool = eops;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001078 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001079 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001080 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001081 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001082 }
1083
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001084 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001085
Taeung Song41b98362015-07-01 00:18:44 +09001086 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1087 ret = -EINVAL;
1088 goto out_delete;
1089 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001090
1091 /*
1092 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1093 * traced in the old kernel.
1094 */
David Ahern1afe1d12013-08-05 21:41:34 -04001095 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001096 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001097 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001098
Taeung Song41b98362015-07-01 00:18:44 +09001099 ret = perf_session__process_events(kvm->session);
1100
1101out_delete:
1102 perf_session__delete(kvm->session);
1103 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001104}
1105
David Ahern2e73f002013-08-05 21:41:37 -04001106static int parse_target_str(struct perf_kvm_stat *kvm)
1107{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001108 if (kvm->opts.target.pid) {
1109 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001110 if (kvm->pid_list == NULL) {
1111 pr_err("Error parsing process id string\n");
1112 return -EINVAL;
1113 }
1114 }
1115
1116 return 0;
1117}
1118
Xiao Guangrong37860632012-11-15 14:17:01 +08001119static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001120{
1121 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001122 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001123
David Ahern2e73f002013-08-05 21:41:37 -04001124 if (parse_target_str(kvm) != 0)
1125 goto exit;
1126
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001127 if (!verify_vcpu(vcpu))
1128 goto exit;
1129
David Ahernde332ac2012-10-02 22:09:53 -06001130 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001131 goto exit;
1132
David Ahernde332ac2012-10-02 22:09:53 -06001133 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001134 goto exit;
1135
David Ahernde332ac2012-10-02 22:09:53 -06001136 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001137 setup_pager();
1138
David Ahernde332ac2012-10-02 22:09:53 -06001139 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001140 if (ret)
1141 goto exit;
1142
David Ahernde332ac2012-10-02 22:09:53 -06001143 sort_result(kvm);
1144 print_result(kvm);
1145
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001146exit:
1147 return ret;
1148}
1149
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001150#define STRDUP_FAIL_EXIT(s) \
1151 ({ char *_p; \
1152 _p = strdup(s); \
1153 if (!_p) \
1154 return -ENOMEM; \
1155 _p; \
1156 })
1157
Hemant Kumar066d3592016-01-28 12:03:06 +05301158int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1159{
1160 return 0;
1161}
1162
Xiao Guangrong37860632012-11-15 14:17:01 +08001163static int
1164kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001165{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001166 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001167 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001168 const char * const record_args[] = {
1169 "record",
1170 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001171 "-m", "1024",
1172 "-c", "1",
1173 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001174 const char * const kvm_stat_record_usage[] = {
1175 "perf kvm stat record [<options>]",
1176 NULL
1177 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001178 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301179 int ret;
1180
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001181 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301182 ret = setup_kvm_events_tp(kvm);
1183 if (ret < 0) {
1184 pr_err("Unable to setup the kvm tracepoints\n");
1185 return ret;
1186 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001187
1188 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1189 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001190
David Ahern8fdd84c2013-08-02 14:05:42 -06001191 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001192 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001193 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1194
1195 if (rec_argv == NULL)
1196 return -ENOMEM;
1197
1198 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1199 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1200
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001201 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001202 rec_argv[i++] = "-e";
1203 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1204 }
1205
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001206 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001207 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001208
1209 for (j = 1; j < (unsigned int)argc; j++, i++)
1210 rec_argv[i] = argv[j];
1211
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001212 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1213 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1214 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1215
1216 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1217 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1218 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1219 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1220 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1221 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1222 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1223 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1224 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1225 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1226 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1227 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1228 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1229 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1230 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1231
1232 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001233 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001234}
1235
Xiao Guangrong37860632012-11-15 14:17:01 +08001236static int
1237kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001238{
David Ahernde332ac2012-10-02 22:09:53 -06001239 const struct option kvm_events_report_options[] = {
1240 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001241 "event for reporting: vmexit, "
1242 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001243 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1244 "vcpu id to report"),
1245 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1246 "key for sorting: sample(sort by samples number)"
1247 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001248 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001249 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001250 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001251 OPT_END()
1252 };
1253
1254 const char * const kvm_events_report_usage[] = {
1255 "perf kvm stat report [<options>]",
1256 NULL
1257 };
1258
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001259 if (argc) {
1260 argc = parse_options(argc, argv,
1261 kvm_events_report_options,
1262 kvm_events_report_usage, 0);
1263 if (argc)
1264 usage_with_options(kvm_events_report_usage,
1265 kvm_events_report_options);
1266 }
1267
Alexander Yaryginf1819572014-09-01 17:44:54 +04001268 if (!kvm->opts.target.pid)
1269 kvm->opts.target.system_wide = true;
1270
David Ahernde332ac2012-10-02 22:09:53 -06001271 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001272}
1273
David Ahern87419c92013-10-29 10:43:16 -06001274#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001275static struct perf_evlist *kvm_live_event_list(void)
1276{
1277 struct perf_evlist *evlist;
1278 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001279 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001280 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001281
1282 evlist = perf_evlist__new();
1283 if (evlist == NULL)
1284 return NULL;
1285
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001286 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001287
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001288 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001289 if (tp == NULL)
1290 goto out;
1291
1292 /* split tracepoint into subsystem and name */
1293 sys = tp;
1294 name = strchr(tp, ':');
1295 if (name == NULL) {
1296 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001297 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001298 free(tp);
1299 goto out;
1300 }
1301 *name = '\0';
1302 name++;
1303
1304 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001305 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001306 free(tp);
1307 goto out;
1308 }
1309
1310 free(tp);
1311 }
1312
1313 err = 0;
1314
1315out:
1316 if (err) {
1317 perf_evlist__delete(evlist);
1318 evlist = NULL;
1319 }
1320
1321 return evlist;
1322}
1323
1324static int kvm_events_live(struct perf_kvm_stat *kvm,
1325 int argc, const char **argv)
1326{
1327 char errbuf[BUFSIZ];
1328 int err;
1329
1330 const struct option live_options[] = {
1331 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1332 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001333 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1334 "number of mmap data pages",
1335 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001336 OPT_INCR('v', "verbose", &verbose,
1337 "be more verbose (show counter open errors, etc)"),
1338 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1339 "system-wide collection from all CPUs"),
1340 OPT_UINTEGER('d', "display", &kvm->display_time,
1341 "time in seconds between display updates"),
1342 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001343 "event for reporting: "
1344 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001345 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1346 "vcpu id to report"),
1347 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1348 "key for sorting: sample(sort by samples number)"
1349 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001350 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001351 "show events other than"
1352 " HLT (x86 only) or Wait state (s390 only)"
1353 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001354 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1355 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001356 OPT_END()
1357 };
1358 const char * const live_usage[] = {
1359 "perf kvm stat live [<options>]",
1360 NULL
1361 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001362 struct perf_data_file file = {
1363 .mode = PERF_DATA_MODE_WRITE,
1364 };
David Ahern1afe1d12013-08-05 21:41:34 -04001365
1366
1367 /* event handling */
1368 kvm->tool.sample = process_sample_event;
1369 kvm->tool.comm = perf_event__process_comm;
1370 kvm->tool.exit = perf_event__process_exit;
1371 kvm->tool.fork = perf_event__process_fork;
1372 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301373 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001374 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001375 perf_tool__fill_defaults(&kvm->tool);
1376
1377 /* set defaults */
1378 kvm->display_time = 1;
1379 kvm->opts.user_interval = 1;
1380 kvm->opts.mmap_pages = 512;
1381 kvm->opts.target.uses_mmap = false;
1382 kvm->opts.target.uid_str = NULL;
1383 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001384 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001385
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001386 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001387 disable_buildid_cache();
1388
1389 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001390
1391 if (argc) {
1392 argc = parse_options(argc, argv, live_options,
1393 live_usage, 0);
1394 if (argc)
1395 usage_with_options(live_usage, live_options);
1396 }
1397
David Ahern70f7b4a2013-08-07 21:56:38 -04001398 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1399
David Ahern1afe1d12013-08-05 21:41:34 -04001400 /*
1401 * target related setups
1402 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001403 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001404 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001405 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001406 ui__warning("%s", errbuf);
1407 }
1408
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001409 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001410 kvm->opts.target.system_wide = true;
1411
1412
1413 /*
1414 * generate the event list
1415 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301416 err = setup_kvm_events_tp(kvm);
1417 if (err < 0) {
1418 pr_err("Unable to setup the kvm tracepoints\n");
1419 return err;
1420 }
1421
David Ahern1afe1d12013-08-05 21:41:34 -04001422 kvm->evlist = kvm_live_event_list();
1423 if (kvm->evlist == NULL) {
1424 err = -1;
1425 goto out;
1426 }
1427
1428 symbol_conf.nr_events = kvm->evlist->nr_entries;
1429
1430 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1431 usage_with_options(live_usage, live_options);
1432
1433 /*
1434 * perf session
1435 */
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001436 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001437 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001438 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001439 goto out;
1440 }
1441 kvm->session->evlist = kvm->evlist;
1442 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001443 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001444 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001445 kvm->evlist->threads, false, kvm->opts.proc_map_timeout);
David Ahern1afe1d12013-08-05 21:41:34 -04001446 err = kvm_live_open_events(kvm);
1447 if (err)
1448 goto out;
1449
1450 err = kvm_events_live_report(kvm);
1451
1452out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001453 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001454 kvm->session = NULL;
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -03001455 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001456
1457 return err;
1458}
David Ahern87419c92013-10-29 10:43:16 -06001459#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001460
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001461static void print_kvm_stat_usage(void)
1462{
1463 printf("Usage: perf kvm stat <command>\n\n");
1464
1465 printf("# Available commands:\n");
1466 printf("\trecord: record kvm events\n");
1467 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001468 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001469
1470 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1471}
1472
Xiao Guangrong37860632012-11-15 14:17:01 +08001473static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001474{
Xiao Guangrong37860632012-11-15 14:17:01 +08001475 struct perf_kvm_stat kvm = {
1476 .file_name = file_name,
1477
1478 .trace_vcpu = -1,
1479 .report_event = "vmexit",
1480 .sort_key = "sample",
1481
Xiao Guangrong37860632012-11-15 14:17:01 +08001482 };
1483
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001484 if (argc == 1) {
1485 print_kvm_stat_usage();
1486 goto perf_stat;
1487 }
1488
1489 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001490 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001491
1492 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001493 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001494
David Ahern87419c92013-10-29 10:43:16 -06001495#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001496 if (!strncmp(argv[1], "live", 4))
1497 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001498#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001499
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001500perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001501 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001502}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001503#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001504
Xiao Guangrong37860632012-11-15 14:17:01 +08001505static int __cmd_record(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("record");
1513 rec_argv[i++] = strdup("-o");
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_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001521}
1522
Xiao Guangrong37860632012-11-15 14:17:01 +08001523static int __cmd_report(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("report");
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_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001539}
1540
Xiao Guangrong37860632012-11-15 14:17:01 +08001541static int
1542__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001543{
1544 int rec_argc, i = 0, j;
1545 const char **rec_argv;
1546
1547 rec_argc = argc + 2;
1548 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1549 rec_argv[i++] = strdup("buildid-list");
1550 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001551 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001552 for (j = 1; j < argc; j++, i++)
1553 rec_argv[i] = argv[j];
1554
1555 BUG_ON(i != rec_argc);
1556
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001557 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001558}
1559
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001560int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001561{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001562 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001563 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001564 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001565 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001566 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001567 "Output file name"),
1568 OPT_BOOLEAN(0, "guest", &perf_guest,
1569 "Collect guest os data"),
1570 OPT_BOOLEAN(0, "host", &perf_host,
1571 "Collect host os data"),
1572 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1573 "guest mount directory under which every guest os"
1574 " instance has a subdir"),
1575 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1576 "file", "file saving guest os vmlinux"),
1577 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1578 "file", "file saving guest os /proc/kallsyms"),
1579 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1580 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001581 OPT_INCR('v', "verbose", &verbose,
1582 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001583 OPT_END()
1584 };
1585
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001586 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1587 "buildid-list", "stat", NULL };
1588 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001589
Joerg Roedel1aed2672012-01-04 17:54:20 +01001590 perf_host = 0;
1591 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001592
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001593 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1594 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001595 if (!argc)
1596 usage_with_options(kvm_usage, kvm_options);
1597
1598 if (!perf_host)
1599 perf_guest = 1;
1600
Xiao Guangrong37860632012-11-15 14:17:01 +08001601 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001602 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001603
Xiao Guangrong37860632012-11-15 14:17:01 +08001604 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001605 pr_err("Failed to allocate memory for filename\n");
1606 return -ENOMEM;
1607 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001608 }
1609
1610 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001611 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001612 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001613 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001614 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001615 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001616 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001617 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001618 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001619 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001620#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001621 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001622 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001623#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001624 else
1625 usage_with_options(kvm_usage, kvm_options);
1626
1627 return 0;
1628}