blob: 4301fc34f23cb299a51eaec5cbf597cd6fa0e144 [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 Olsaf5fc14122013-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 <math.h>
38
Arnaldo Carvalho de Melobb8c16d2017-04-19 16:15:13 -030039static const char *get_filename_for_perf_kvm(void)
40{
41 const char *filename;
42
43 if (perf_host && !perf_guest)
44 filename = strdup("perf.data.host");
45 else if (!perf_host && perf_guest)
46 filename = strdup("perf.data.guest");
47 else
48 filename = strdup("perf.data.kvm");
49
50 return filename;
51}
52
Alexander Yaryginda50ad62014-07-03 17:59:49 +040053#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040054#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080055
Alexander Yarygin9daa8122014-07-03 18:29:05 +040056void exit_event_get_key(struct perf_evsel *evsel,
57 struct perf_sample *sample,
58 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080059{
60 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053061 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080062}
63
Alexander Yarygin9daa8122014-07-03 18:29:05 +040064bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080065{
Hemant Kumar162607e2016-01-28 12:03:04 +053066 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080067}
68
Alexander Yarygin9daa8122014-07-03 18:29:05 +040069bool exit_event_begin(struct perf_evsel *evsel,
70 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080071{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030072 if (kvm_exit_event(evsel)) {
73 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080074 return true;
75 }
76
77 return false;
78}
79
Alexander Yarygin9daa8122014-07-03 18:29:05 +040080bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080081{
Hemant Kumar162607e2016-01-28 12:03:04 +053082 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080083}
84
Alexander Yarygin9daa8122014-07-03 18:29:05 +040085bool exit_event_end(struct perf_evsel *evsel,
86 struct perf_sample *sample __maybe_unused,
87 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080088{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030089 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080090}
91
Alexander Yarygindf74c132014-07-03 17:59:50 +040092static const char *get_exit_reason(struct perf_kvm_stat *kvm,
93 struct exit_reasons_table *tbl,
94 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080095{
Alexander Yarygindf74c132014-07-03 17:59:50 +040096 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060097 if (tbl->exit_code == exit_code)
98 return tbl->reason;
99 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800100 }
101
102 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600103 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800104 return "UNKNOWN";
105}
106
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400107void exit_event_decode_key(struct perf_kvm_stat *kvm,
108 struct event_key *key,
109 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800110{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400111 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +0400112 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800113
Hemant Kumar162607e2016-01-28 12:03:04 +0530114 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800115}
116
Xiao Guangrong37860632012-11-15 14:17:01 +0800117static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800118{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400119 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800120
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400121 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
122 if (!strcmp(events_ops->name, kvm->report_event)) {
123 kvm->events_ops = events_ops->ops;
124 return true;
125 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800126 }
127
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400128 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800129}
130
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800131struct vcpu_event_record {
132 int vcpu_id;
133 u64 start_time;
134 struct kvm_event *last_event;
135};
136
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800137
Xiao Guangrong37860632012-11-15 14:17:01 +0800138static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800139{
David Ahernb880dee2012-10-08 11:17:30 -0600140 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800141
David Ahernb880dee2012-10-08 11:17:30 -0600142 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600143 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800144}
145
David Ahern87419c92013-10-29 10:43:16 -0600146#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400147static void clear_events_cache_stats(struct list_head *kvm_events_cache)
148{
149 struct list_head *head;
150 struct kvm_event *event;
151 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400152 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400153
154 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
155 head = &kvm_events_cache[i];
156 list_for_each_entry(event, head, hash_entry) {
157 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400158 event->total.time = 0;
159 init_stats(&event->total.stats);
160
161 for (j = 0; j < event->max_vcpu; ++j) {
162 event->vcpu[j].time = 0;
163 init_stats(&event->vcpu[j].stats);
164 }
David Ahern1afe1d12013-08-05 21:41:34 -0400165 }
166 }
167}
David Ahern87419c92013-10-29 10:43:16 -0600168#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400169
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800170static int kvm_events_hash_fn(u64 key)
171{
172 return key & (EVENTS_CACHE_SIZE - 1);
173}
174
175static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
176{
177 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600178 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800179
180 if (vcpu_id < event->max_vcpu)
181 return true;
182
183 while (event->max_vcpu <= vcpu_id)
184 event->max_vcpu += DEFAULT_VCPU_NUM;
185
David Ahern6ca5f302013-05-25 18:24:46 -0600186 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800187 event->vcpu = realloc(event->vcpu,
188 event->max_vcpu * sizeof(*event->vcpu));
189 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600190 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800191 pr_err("Not enough memory\n");
192 return false;
193 }
194
195 memset(event->vcpu + old_max_vcpu, 0,
196 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
197 return true;
198}
199
200static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
201{
202 struct kvm_event *event;
203
204 event = zalloc(sizeof(*event));
205 if (!event) {
206 pr_err("Not enough memory\n");
207 return NULL;
208 }
209
210 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200211 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800212 return event;
213}
214
Xiao Guangrong37860632012-11-15 14:17:01 +0800215static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600216 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800217{
218 struct kvm_event *event;
219 struct list_head *head;
220
221 BUG_ON(key->key == INVALID_KEY);
222
David Ahernde332ac2012-10-02 22:09:53 -0600223 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600224 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800225 if (event->key.key == key->key && event->key.info == key->info)
226 return event;
David Ahern355afe82012-10-08 11:17:32 -0600227 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800228
229 event = kvm_alloc_init_event(key);
230 if (!event)
231 return NULL;
232
233 list_add(&event->hash_entry, head);
234 return event;
235}
236
Xiao Guangrong37860632012-11-15 14:17:01 +0800237static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600238 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800239 struct event_key *key, u64 timestamp)
240{
241 struct kvm_event *event = NULL;
242
243 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600244 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800245
246 vcpu_record->last_event = event;
247 vcpu_record->start_time = timestamp;
248 return true;
249}
250
251static void
252kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
253{
254 kvm_stats->time += time_diff;
255 update_stats(&kvm_stats->stats, time_diff);
256}
257
258static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
259{
260 struct kvm_event_stats *kvm_stats = &event->total;
261
262 if (vcpu_id != -1)
263 kvm_stats = &event->vcpu[vcpu_id];
264
265 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
266 avg_stats(&kvm_stats->stats));
267}
268
269static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
270 u64 time_diff)
271{
David Ahern2aa8eab2012-10-08 11:17:35 -0600272 if (vcpu_id == -1) {
273 kvm_update_event_stats(&event->total, time_diff);
274 return true;
275 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800276
277 if (!kvm_event_expand(event, vcpu_id))
278 return false;
279
280 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
281 return true;
282}
283
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400284static bool is_child_event(struct perf_kvm_stat *kvm,
285 struct perf_evsel *evsel,
286 struct perf_sample *sample,
287 struct event_key *key)
288{
289 struct child_event_ops *child_ops;
290
291 child_ops = kvm->events_ops->child_ops;
292
293 if (!child_ops)
294 return false;
295
296 for (; child_ops->name; child_ops++) {
297 if (!strcmp(evsel->name, child_ops->name)) {
298 child_ops->get_key(evsel, sample, key);
299 return true;
300 }
301 }
302
303 return false;
304}
305
306static bool handle_child_event(struct perf_kvm_stat *kvm,
307 struct vcpu_event_record *vcpu_record,
308 struct event_key *key,
309 struct perf_sample *sample __maybe_unused)
310{
311 struct kvm_event *event = NULL;
312
313 if (key->key != INVALID_KEY)
314 event = find_create_kvm_event(kvm, key);
315
316 vcpu_record->last_event = event;
317
318 return true;
319}
320
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400321static bool skip_event(const char *event)
322{
323 const char * const *skip_events;
324
325 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
326 if (!strcmp(event, *skip_events))
327 return true;
328
329 return false;
330}
331
Xiao Guangrong37860632012-11-15 14:17:01 +0800332static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600333 struct vcpu_event_record *vcpu_record,
334 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400335 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800336{
337 struct kvm_event *event;
338 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600339 int vcpu;
340
341 if (kvm->trace_vcpu == -1)
342 vcpu = -1;
343 else
344 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800345
346 event = vcpu_record->last_event;
347 time_begin = vcpu_record->start_time;
348
349 /* The begin event is not caught. */
350 if (!time_begin)
351 return true;
352
353 /*
354 * In some case, the 'begin event' only records the start timestamp,
355 * the actual event is recognized in the 'end event' (e.g. mmio-event).
356 */
357
358 /* Both begin and end events did not get the key. */
359 if (!event && key->key == INVALID_KEY)
360 return true;
361
362 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600363 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800364
365 if (!event)
366 return false;
367
368 vcpu_record->last_event = NULL;
369 vcpu_record->start_time = 0;
370
David Ahern1afe1d12013-08-05 21:41:34 -0400371 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400372 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400373 pr_debug("End time before begin time; skipping event.\n");
374 return true;
375 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800376
David Ahern70f7b4a2013-08-07 21:56:38 -0400377 time_diff = sample->time - time_begin;
378
379 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530380 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400381
382 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400383 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400384 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
385 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300386 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400387 }
388 }
389
David Ahern2aa8eab2012-10-08 11:17:35 -0600390 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800391}
392
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300393static
394struct vcpu_event_record *per_vcpu_record(struct thread *thread,
395 struct perf_evsel *evsel,
396 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800397{
398 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900399 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800400 struct vcpu_event_record *vcpu_record;
401
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300402 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800403 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300404 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800405 return NULL;
406 }
407
Hemant Kumar162607e2016-01-28 12:03:04 +0530408 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
409 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900410 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800411 }
412
Namhyung Kim69e865c2014-10-06 09:46:02 +0900413 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800414}
415
Xiao Guangrong37860632012-11-15 14:17:01 +0800416static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600417 struct thread *thread,
418 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300419 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800420{
421 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400422 struct event_key key = { .key = INVALID_KEY,
423 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800424
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300425 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800426 if (!vcpu_record)
427 return true;
428
David Ahern2aa8eab2012-10-08 11:17:35 -0600429 /* only process events for vcpus user cares about */
430 if ((kvm->trace_vcpu != -1) &&
431 (kvm->trace_vcpu != vcpu_record->vcpu_id))
432 return true;
433
David Ahernde332ac2012-10-02 22:09:53 -0600434 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
435 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800436
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400437 if (is_child_event(kvm, evsel, sample, &key))
438 return handle_child_event(kvm, vcpu_record, &key, sample);
439
David Ahernde332ac2012-10-02 22:09:53 -0600440 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400441 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800442
443 return true;
444}
445
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800446#define GET_EVENT_KEY(func, field) \
447static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
448{ \
449 if (vcpu == -1) \
450 return event->total.field; \
451 \
452 if (vcpu >= event->max_vcpu) \
453 return 0; \
454 \
455 return event->vcpu[vcpu].field; \
456}
457
458#define COMPARE_EVENT_KEY(func, field) \
459GET_EVENT_KEY(func, field) \
460static int compare_kvm_event_ ## func(struct kvm_event *one, \
461 struct kvm_event *two, int vcpu)\
462{ \
463 return get_event_ ##func(one, vcpu) > \
464 get_event_ ##func(two, vcpu); \
465}
466
467GET_EVENT_KEY(time, time);
468COMPARE_EVENT_KEY(count, stats.n);
469COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400470GET_EVENT_KEY(max, stats.max);
471GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800472
473#define DEF_SORT_NAME_KEY(name, compare_key) \
474 { #name, compare_kvm_event_ ## compare_key }
475
476static struct kvm_event_key keys[] = {
477 DEF_SORT_NAME_KEY(sample, count),
478 DEF_SORT_NAME_KEY(time, mean),
479 { NULL, NULL }
480};
481
Xiao Guangrong37860632012-11-15 14:17:01 +0800482static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800483{
484 int i;
485
486 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600487 if (!strcmp(keys[i].name, kvm->sort_key)) {
488 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800489 return true;
490 }
491 }
492
David Ahernde332ac2012-10-02 22:09:53 -0600493 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800494 return false;
495}
496
David Ahernde332ac2012-10-02 22:09:53 -0600497static void insert_to_result(struct rb_root *result, struct kvm_event *event,
498 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800499{
David Ahernde332ac2012-10-02 22:09:53 -0600500 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800501 struct rb_node *parent = NULL;
502 struct kvm_event *p;
503
504 while (*rb) {
505 p = container_of(*rb, struct kvm_event, rb);
506 parent = *rb;
507
508 if (bigger(event, p, vcpu))
509 rb = &(*rb)->rb_left;
510 else
511 rb = &(*rb)->rb_right;
512 }
513
514 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600515 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800516}
517
Xiao Guangrong37860632012-11-15 14:17:01 +0800518static void
519update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800520{
David Ahernde332ac2012-10-02 22:09:53 -0600521 int vcpu = kvm->trace_vcpu;
522
523 kvm->total_count += get_event_count(event, vcpu);
524 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800525}
526
527static bool event_is_valid(struct kvm_event *event, int vcpu)
528{
529 return !!get_event_count(event, vcpu);
530}
531
Xiao Guangrong37860632012-11-15 14:17:01 +0800532static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800533{
534 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600535 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800536 struct kvm_event *event;
537
David Ahern355afe82012-10-08 11:17:32 -0600538 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
539 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800540 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600541 update_total_count(kvm, event);
542 insert_to_result(&kvm->result, event,
543 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800544 }
David Ahern355afe82012-10-08 11:17:32 -0600545 }
546 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800547}
548
549/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600550static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800551{
David Ahernde332ac2012-10-02 22:09:53 -0600552 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800553
554 if (!node)
555 return NULL;
556
David Ahernde332ac2012-10-02 22:09:53 -0600557 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800558 return container_of(node, struct kvm_event, rb);
559}
560
David Ahern1afe1d12013-08-05 21:41:34 -0400561static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800562{
David Ahern1afe1d12013-08-05 21:41:34 -0400563 int vcpu = kvm->trace_vcpu;
564
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800565 pr_info("Analyze events for ");
566
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400567 if (kvm->opts.target.system_wide)
568 pr_info("all VMs, ");
569 else if (kvm->opts.target.pid)
570 pr_info("pid(s) %s, ", kvm->opts.target.pid);
571 else
572 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400573
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800574 if (vcpu == -1)
575 pr_info("all VCPUs:\n\n");
576 else
577 pr_info("VCPU %d:\n\n", vcpu);
578}
579
David Ahern1afe1d12013-08-05 21:41:34 -0400580static void show_timeofday(void)
581{
582 char date[64];
583 struct timeval tv;
584 struct tm ltime;
585
586 gettimeofday(&tv, NULL);
587 if (localtime_r(&tv.tv_sec, &ltime)) {
588 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
589 pr_info("%s.%06ld", date, tv.tv_usec);
590 } else
591 pr_info("00:00:00.000000");
592
593 return;
594}
595
Xiao Guangrong37860632012-11-15 14:17:01 +0800596static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800597{
Hemant Kumar162607e2016-01-28 12:03:04 +0530598 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800599 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600600 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800601
David Ahern1afe1d12013-08-05 21:41:34 -0400602 if (kvm->live) {
603 puts(CONSOLE_CLEAR);
604 show_timeofday();
605 }
606
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800607 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400608 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530609 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800610 pr_info("%10s ", "Samples");
611 pr_info("%9s ", "Samples%");
612
613 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200614 pr_info("%11s ", "Min Time");
615 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800616 pr_info("%16s ", "Avg time");
617 pr_info("\n\n");
618
David Ahernde332ac2012-10-02 22:09:53 -0600619 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400620 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800621
622 ecount = get_event_count(event, vcpu);
623 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400624 max = get_event_max(event, vcpu);
625 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800626
David Ahernde332ac2012-10-02 22:09:53 -0600627 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530628 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800629 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600630 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
631 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300632 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
633 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
634 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800635 kvm_event_rel_stddev(vcpu, event));
636 pr_info("\n");
637 }
638
David Aherne4f76372012-10-08 11:17:34 -0600639 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300640 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400641
642 if (kvm->lost_events)
643 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
644}
645
David Ahern87419c92013-10-29 10:43:16 -0600646#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400647static int process_lost_event(struct perf_tool *tool,
648 union perf_event *event __maybe_unused,
649 struct perf_sample *sample __maybe_unused,
650 struct machine *machine __maybe_unused)
651{
652 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
653
654 kvm->lost_events++;
655 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800656}
David Ahern87419c92013-10-29 10:43:16 -0600657#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800658
David Ahern2e73f002013-08-05 21:41:37 -0400659static bool skip_sample(struct perf_kvm_stat *kvm,
660 struct perf_sample *sample)
661{
662 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
663 return true;
664
665 return false;
666}
667
David Ahernde332ac2012-10-02 22:09:53 -0600668static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800669 union perf_event *event,
670 struct perf_sample *sample,
671 struct perf_evsel *evsel,
672 struct machine *machine)
673{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300674 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400675 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800676 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
677 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800678
David Ahern2e73f002013-08-05 21:41:37 -0400679 if (skip_sample(kvm, sample))
680 return 0;
681
Adrian Hunter314add62013-08-27 11:23:03 +0300682 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800683 if (thread == NULL) {
684 pr_debug("problem processing %d event, skipping it.\n",
685 event->header.type);
686 return -1;
687 }
688
David Ahernde332ac2012-10-02 22:09:53 -0600689 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300690 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800691
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300692 thread__put(thread);
693 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800694}
695
David Ahern1afe1d12013-08-05 21:41:34 -0400696static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800697{
David Ahern1afe1d12013-08-05 21:41:34 -0400698 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400699 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400700
701 if (kvm->live) {
702 err = get_cpuid(buf, sizeof(buf));
703 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400704 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400705 return err;
706 }
707 cpuid = buf;
708 } else
709 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800710
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400711 if (!cpuid) {
712 pr_err("Failed to look up CPU type\n");
713 return -EINVAL;
714 }
715
716 err = cpu_isa_init(kvm, cpuid);
717 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800718 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800719
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400720 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400721}
722
723static bool verify_vcpu(int vcpu)
724{
725 if (vcpu != -1 && vcpu < 0) {
726 pr_err("Invalid vcpu:%d.\n", vcpu);
727 return false;
728 }
729
730 return true;
731}
732
David Ahern87419c92013-10-29 10:43:16 -0600733#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400734/* keeping the max events to a modest level to keep
735 * the processing of samples per mmap smooth.
736 */
737#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
738
739static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
740 u64 *mmap_time)
741{
742 union perf_event *event;
743 struct perf_sample sample;
744 s64 n = 0;
745 int err;
746
747 *mmap_time = ULLONG_MAX;
748 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
749 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
750 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800751 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400752 pr_err("Failed to parse sample\n");
753 return -1;
754 }
755
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300756 err = perf_session__queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800757 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300758 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800759 * point to it, and it'll get possibly overwritten by the kernel.
760 */
761 perf_evlist__mmap_consume(kvm->evlist, idx);
762
David Ahern1afe1d12013-08-05 21:41:34 -0400763 if (err) {
764 pr_err("Failed to enqueue sample: %d\n", err);
765 return -1;
766 }
767
768 /* save time stamp of our first sample for this mmap */
769 if (n == 0)
770 *mmap_time = sample.time;
771
772 /* limit events per mmap handled all at once */
773 n++;
774 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
775 break;
776 }
777
778 return n;
779}
780
781static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
782{
783 int i, err, throttled = 0;
784 s64 n, ntotal = 0;
785 u64 flush_time = ULLONG_MAX, mmap_time;
786
787 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
788 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
789 if (n < 0)
790 return -1;
791
792 /* flush time is going to be the minimum of all the individual
793 * mmap times. Essentially, we flush all the samples queued up
794 * from the last pass under our minimal start time -- that leaves
795 * a very small race for samples to come in with a lower timestamp.
796 * The ioctl to return the perf_clock timestamp should close the
797 * race entirely.
798 */
799 if (mmap_time < flush_time)
800 flush_time = mmap_time;
801
802 ntotal += n;
803 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
804 throttled = 1;
805 }
806
807 /* flush queue after each round in which we processed events */
808 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300809 struct ordered_events *oe = &kvm->session->ordered_events;
810
811 oe->next_flush = flush_time;
812 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400813 if (err) {
814 if (kvm->lost_events)
815 pr_info("\nLost events: %" PRIu64 "\n\n",
816 kvm->lost_events);
817 return err;
818 }
819 }
820
821 return throttled;
822}
823
824static volatile int done;
825
826static void sig_handler(int sig __maybe_unused)
827{
828 done = 1;
829}
830
831static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
832{
833 struct itimerspec new_value;
834 int rc = -1;
835
836 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
837 if (kvm->timerfd < 0) {
838 pr_err("timerfd_create failed\n");
839 goto out;
840 }
841
842 new_value.it_value.tv_sec = kvm->display_time;
843 new_value.it_value.tv_nsec = 0;
844 new_value.it_interval.tv_sec = kvm->display_time;
845 new_value.it_interval.tv_nsec = 0;
846
847 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
848 pr_err("timerfd_settime failed: %d\n", errno);
849 close(kvm->timerfd);
850 goto out;
851 }
852
853 rc = 0;
854out:
855 return rc;
856}
857
858static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
859{
860 uint64_t c;
861 int rc;
862
863 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
864 if (rc < 0) {
865 if (errno == EAGAIN)
866 return 0;
867
868 pr_err("Failed to read timer fd: %d\n", errno);
869 return -1;
870 }
871
872 if (rc != sizeof(uint64_t)) {
873 pr_err("Error reading timer fd - invalid size returned\n");
874 return -1;
875 }
876
877 if (c != 1)
878 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
879
880 /* update display */
881 sort_result(kvm);
882 print_result(kvm);
883
884 /* reset counts */
885 clear_events_cache_stats(kvm->kvm_events_cache);
886 kvm->total_count = 0;
887 kvm->total_time = 0;
888 kvm->lost_events = 0;
889
890 return 0;
891}
892
893static int fd_set_nonblock(int fd)
894{
895 long arg = 0;
896
897 arg = fcntl(fd, F_GETFL);
898 if (arg < 0) {
899 pr_err("Failed to get current flags for fd %d\n", fd);
900 return -1;
901 }
902
903 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
904 pr_err("Failed to set non-block option on fd %d\n", fd);
905 return -1;
906 }
907
908 return 0;
909}
910
Jiri Olsad5b41302014-08-11 10:50:09 +0200911static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400912{
913 int c;
914
David Ahern1afe1d12013-08-05 21:41:34 -0400915 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400916 if (c == 'q')
917 return 1;
918
919 return 0;
920}
921
922static int kvm_events_live_report(struct perf_kvm_stat *kvm)
923{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200924 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200925 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400926
927 /* live flag must be set first */
928 kvm->live = true;
929
930 ret = cpu_isa_config(kvm);
931 if (ret < 0)
932 return ret;
933
934 if (!verify_vcpu(kvm->trace_vcpu) ||
935 !select_key(kvm) ||
936 !register_kvm_events_ops(kvm)) {
937 goto out;
938 }
939
Jiri Olsad5b41302014-08-11 10:50:09 +0200940 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400941 init_kvm_event_record(kvm);
942
David Ahern1afe1d12013-08-05 21:41:34 -0400943 signal(SIGINT, sig_handler);
944 signal(SIGTERM, sig_handler);
945
David Ahern1afe1d12013-08-05 21:41:34 -0400946 /* add timer fd */
947 if (perf_kvm__timerfd_create(kvm) < 0) {
948 err = -1;
949 goto out;
950 }
951
Jiri Olsafe636ad2014-10-01 18:00:27 +0200952 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300953 goto out;
954
Jiri Olsa0cae0132014-10-01 18:00:28 +0200955 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
956 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300957 goto out;
958
David Ahern1afe1d12013-08-05 21:41:34 -0400959 if (fd_set_nonblock(fileno(stdin)) != 0)
960 goto out;
961
962 /* everything is good - enable the events and process */
963 perf_evlist__enable(kvm->evlist);
964
965 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200966 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400967 int rc;
968
969 rc = perf_kvm__mmap_read(kvm);
970 if (rc < 0)
971 break;
972
973 err = perf_kvm__handle_timerfd(kvm);
974 if (err)
975 goto out;
976
Jiri Olsa1ca72262014-10-01 18:00:29 +0200977 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200978 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400979
980 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200981 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400982 }
983
984 perf_evlist__disable(kvm->evlist);
985
986 if (err == 0) {
987 sort_result(kvm);
988 print_result(kvm);
989 }
990
991out:
992 if (kvm->timerfd >= 0)
993 close(kvm->timerfd);
994
Jiri Olsad5b41302014-08-11 10:50:09 +0200995 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -0400996 return err;
997}
998
999static int kvm_live_open_events(struct perf_kvm_stat *kvm)
1000{
1001 int err, rc = -1;
1002 struct perf_evsel *pos;
1003 struct perf_evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001004 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -04001005
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -03001006 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001007
1008 /*
1009 * Note: exclude_{guest,host} do not apply here.
1010 * This command processes KVM tracepoints from host only
1011 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001012 evlist__for_each_entry(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -04001013 struct perf_event_attr *attr = &pos->attr;
1014
1015 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +03001016 perf_evsel__set_sample_bit(pos, TID);
1017 perf_evsel__set_sample_bit(pos, TIME);
1018 perf_evsel__set_sample_bit(pos, CPU);
1019 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001020 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001021 perf_evsel__reset_sample_bit(pos, PERIOD);
1022 perf_evsel__reset_sample_bit(pos, IP);
1023 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1024 perf_evsel__reset_sample_bit(pos, ADDR);
1025 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001026 attr->mmap = 0;
1027 attr->comm = 0;
1028 attr->task = 0;
1029
1030 attr->sample_period = 1;
1031
1032 attr->watermark = 0;
1033 attr->wakeup_events = 1000;
1034
1035 /* will enable all once we are ready */
1036 attr->disabled = 1;
1037 }
1038
1039 err = perf_evlist__open(evlist);
1040 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001041 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001042 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001043 goto out;
1044 }
1045
1046 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001047 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001048 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001049 perf_evlist__close(evlist);
1050 goto out;
1051 }
1052
1053 rc = 0;
1054
1055out:
1056 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001057}
David Ahern87419c92013-10-29 10:43:16 -06001058#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001059
Xiao Guangrong37860632012-11-15 14:17:01 +08001060static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001061{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001062 int ret;
1063
David Ahernde332ac2012-10-02 22:09:53 -06001064 struct perf_tool eops = {
1065 .sample = process_sample_event,
1066 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301067 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001068 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001069 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001070 struct perf_data file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001071 .path = kvm->file_name,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001072 .mode = PERF_DATA_MODE_READ,
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001073 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001074 };
David Ahernde332ac2012-10-02 22:09:53 -06001075
1076 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001077 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001078 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001079 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001080 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001081 }
1082
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001083 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001084
Taeung Song41b98362015-07-01 00:18:44 +09001085 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1086 ret = -EINVAL;
1087 goto out_delete;
1088 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001089
1090 /*
1091 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1092 * traced in the old kernel.
1093 */
David Ahern1afe1d12013-08-05 21:41:34 -04001094 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001095 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001096 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001097
Taeung Song41b98362015-07-01 00:18:44 +09001098 ret = perf_session__process_events(kvm->session);
1099
1100out_delete:
1101 perf_session__delete(kvm->session);
1102 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001103}
1104
David Ahern2e73f002013-08-05 21:41:37 -04001105static int parse_target_str(struct perf_kvm_stat *kvm)
1106{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001107 if (kvm->opts.target.pid) {
1108 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001109 if (kvm->pid_list == NULL) {
1110 pr_err("Error parsing process id string\n");
1111 return -EINVAL;
1112 }
1113 }
1114
1115 return 0;
1116}
1117
Xiao Guangrong37860632012-11-15 14:17:01 +08001118static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001119{
1120 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001121 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001122
David Ahern2e73f002013-08-05 21:41:37 -04001123 if (parse_target_str(kvm) != 0)
1124 goto exit;
1125
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001126 if (!verify_vcpu(vcpu))
1127 goto exit;
1128
David Ahernde332ac2012-10-02 22:09:53 -06001129 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001130 goto exit;
1131
David Ahernde332ac2012-10-02 22:09:53 -06001132 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001133 goto exit;
1134
David Ahernde332ac2012-10-02 22:09:53 -06001135 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001136 setup_pager();
1137
David Ahernde332ac2012-10-02 22:09:53 -06001138 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001139 if (ret)
1140 goto exit;
1141
David Ahernde332ac2012-10-02 22:09:53 -06001142 sort_result(kvm);
1143 print_result(kvm);
1144
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001145exit:
1146 return ret;
1147}
1148
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001149#define STRDUP_FAIL_EXIT(s) \
1150 ({ char *_p; \
1151 _p = strdup(s); \
1152 if (!_p) \
1153 return -ENOMEM; \
1154 _p; \
1155 })
1156
Hemant Kumar066d3592016-01-28 12:03:06 +05301157int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1158{
1159 return 0;
1160}
1161
Xiao Guangrong37860632012-11-15 14:17:01 +08001162static int
1163kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001164{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001165 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001166 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001167 const char * const record_args[] = {
1168 "record",
1169 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001170 "-m", "1024",
1171 "-c", "1",
1172 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001173 const char * const kvm_stat_record_usage[] = {
1174 "perf kvm stat record [<options>]",
1175 NULL
1176 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001177 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301178 int ret;
1179
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001180 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301181 ret = setup_kvm_events_tp(kvm);
1182 if (ret < 0) {
1183 pr_err("Unable to setup the kvm tracepoints\n");
1184 return ret;
1185 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001186
1187 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1188 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001189
David Ahern8fdd84c2013-08-02 14:05:42 -06001190 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001191 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001192 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1193
1194 if (rec_argv == NULL)
1195 return -ENOMEM;
1196
1197 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1198 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1199
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001200 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001201 rec_argv[i++] = "-e";
1202 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1203 }
1204
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001205 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001206 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001207
1208 for (j = 1; j < (unsigned int)argc; j++, i++)
1209 rec_argv[i] = argv[j];
1210
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001211 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1212 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1213 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1214
1215 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1216 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1217 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1218 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1219 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1220 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1221 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1222 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1223 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1224 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1225 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1226 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1227 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1228 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1229 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1230
1231 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001232 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001233}
1234
Xiao Guangrong37860632012-11-15 14:17:01 +08001235static int
1236kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001237{
David Ahernde332ac2012-10-02 22:09:53 -06001238 const struct option kvm_events_report_options[] = {
1239 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001240 "event for reporting: vmexit, "
1241 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001242 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1243 "vcpu id to report"),
1244 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1245 "key for sorting: sample(sort by samples number)"
1246 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001247 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001248 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001249 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001250 OPT_END()
1251 };
1252
1253 const char * const kvm_events_report_usage[] = {
1254 "perf kvm stat report [<options>]",
1255 NULL
1256 };
1257
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001258 if (argc) {
1259 argc = parse_options(argc, argv,
1260 kvm_events_report_options,
1261 kvm_events_report_usage, 0);
1262 if (argc)
1263 usage_with_options(kvm_events_report_usage,
1264 kvm_events_report_options);
1265 }
1266
Alexander Yaryginf1819572014-09-01 17:44:54 +04001267 if (!kvm->opts.target.pid)
1268 kvm->opts.target.system_wide = true;
1269
David Ahernde332ac2012-10-02 22:09:53 -06001270 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001271}
1272
David Ahern87419c92013-10-29 10:43:16 -06001273#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001274static struct perf_evlist *kvm_live_event_list(void)
1275{
1276 struct perf_evlist *evlist;
1277 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001278 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001279 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001280
1281 evlist = perf_evlist__new();
1282 if (evlist == NULL)
1283 return NULL;
1284
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001285 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001286
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001287 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001288 if (tp == NULL)
1289 goto out;
1290
1291 /* split tracepoint into subsystem and name */
1292 sys = tp;
1293 name = strchr(tp, ':');
1294 if (name == NULL) {
1295 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001296 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001297 free(tp);
1298 goto out;
1299 }
1300 *name = '\0';
1301 name++;
1302
1303 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001304 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001305 free(tp);
1306 goto out;
1307 }
1308
1309 free(tp);
1310 }
1311
1312 err = 0;
1313
1314out:
1315 if (err) {
1316 perf_evlist__delete(evlist);
1317 evlist = NULL;
1318 }
1319
1320 return evlist;
1321}
1322
1323static int kvm_events_live(struct perf_kvm_stat *kvm,
1324 int argc, const char **argv)
1325{
1326 char errbuf[BUFSIZ];
1327 int err;
1328
1329 const struct option live_options[] = {
1330 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1331 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001332 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1333 "number of mmap data pages",
1334 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001335 OPT_INCR('v', "verbose", &verbose,
1336 "be more verbose (show counter open errors, etc)"),
1337 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1338 "system-wide collection from all CPUs"),
1339 OPT_UINTEGER('d', "display", &kvm->display_time,
1340 "time in seconds between display updates"),
1341 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001342 "event for reporting: "
1343 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001344 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1345 "vcpu id to report"),
1346 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1347 "key for sorting: sample(sort by samples number)"
1348 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001349 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001350 "show events other than"
1351 " HLT (x86 only) or Wait state (s390 only)"
1352 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001353 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1354 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001355 OPT_END()
1356 };
1357 const char * const live_usage[] = {
1358 "perf kvm stat live [<options>]",
1359 NULL
1360 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001361 struct perf_data data = {
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001362 .mode = PERF_DATA_MODE_WRITE,
1363 };
David Ahern1afe1d12013-08-05 21:41:34 -04001364
1365
1366 /* event handling */
1367 kvm->tool.sample = process_sample_event;
1368 kvm->tool.comm = perf_event__process_comm;
1369 kvm->tool.exit = perf_event__process_exit;
1370 kvm->tool.fork = perf_event__process_fork;
1371 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301372 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001373 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001374 perf_tool__fill_defaults(&kvm->tool);
1375
1376 /* set defaults */
1377 kvm->display_time = 1;
1378 kvm->opts.user_interval = 1;
1379 kvm->opts.mmap_pages = 512;
1380 kvm->opts.target.uses_mmap = false;
1381 kvm->opts.target.uid_str = NULL;
1382 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001383 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001384
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001385 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001386 disable_buildid_cache();
1387
1388 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001389
1390 if (argc) {
1391 argc = parse_options(argc, argv, live_options,
1392 live_usage, 0);
1393 if (argc)
1394 usage_with_options(live_usage, live_options);
1395 }
1396
David Ahern70f7b4a2013-08-07 21:56:38 -04001397 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1398
David Ahern1afe1d12013-08-05 21:41:34 -04001399 /*
1400 * target related setups
1401 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001402 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001403 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001404 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001405 ui__warning("%s", errbuf);
1406 }
1407
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001408 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001409 kvm->opts.target.system_wide = true;
1410
1411
1412 /*
1413 * generate the event list
1414 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301415 err = setup_kvm_events_tp(kvm);
1416 if (err < 0) {
1417 pr_err("Unable to setup the kvm tracepoints\n");
1418 return err;
1419 }
1420
David Ahern1afe1d12013-08-05 21:41:34 -04001421 kvm->evlist = kvm_live_event_list();
1422 if (kvm->evlist == NULL) {
1423 err = -1;
1424 goto out;
1425 }
1426
1427 symbol_conf.nr_events = kvm->evlist->nr_entries;
1428
1429 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1430 usage_with_options(live_usage, live_options);
1431
1432 /*
1433 * perf session
1434 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001435 kvm->session = perf_session__new(&data, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001436 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001437 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001438 goto out;
1439 }
1440 kvm->session->evlist = kvm->evlist;
1441 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001442 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001443 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang340b47f2017-09-29 07:47:54 -07001444 kvm->evlist->threads, false,
1445 kvm->opts.proc_map_timeout, 1);
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}