blob: 0af4c092b4711f0d6f7e2312ebc1ab3b73d7f301 [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 <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 Yarygin54c801f2014-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 Yarygin54c801f2014-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 = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01001071 .file = {
1072 .path = kvm->file_name,
1073 },
1074 .mode = PERF_DATA_MODE_READ,
1075 .force = kvm->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001076 };
David Ahernde332ac2012-10-02 22:09:53 -06001077
1078 kvm->tool = eops;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001079 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001080 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001081 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001082 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001083 }
1084
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001085 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001086
Taeung Song41b98362015-07-01 00:18:44 +09001087 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1088 ret = -EINVAL;
1089 goto out_delete;
1090 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001091
1092 /*
1093 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1094 * traced in the old kernel.
1095 */
David Ahern1afe1d12013-08-05 21:41:34 -04001096 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001097 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001098 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001099
Taeung Song41b98362015-07-01 00:18:44 +09001100 ret = perf_session__process_events(kvm->session);
1101
1102out_delete:
1103 perf_session__delete(kvm->session);
1104 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001105}
1106
David Ahern2e73f002013-08-05 21:41:37 -04001107static int parse_target_str(struct perf_kvm_stat *kvm)
1108{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001109 if (kvm->opts.target.pid) {
1110 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001111 if (kvm->pid_list == NULL) {
1112 pr_err("Error parsing process id string\n");
1113 return -EINVAL;
1114 }
1115 }
1116
1117 return 0;
1118}
1119
Xiao Guangrong37860632012-11-15 14:17:01 +08001120static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001121{
1122 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001123 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001124
David Ahern2e73f002013-08-05 21:41:37 -04001125 if (parse_target_str(kvm) != 0)
1126 goto exit;
1127
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001128 if (!verify_vcpu(vcpu))
1129 goto exit;
1130
David Ahernde332ac2012-10-02 22:09:53 -06001131 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001132 goto exit;
1133
David Ahernde332ac2012-10-02 22:09:53 -06001134 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001135 goto exit;
1136
David Ahernde332ac2012-10-02 22:09:53 -06001137 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001138 setup_pager();
1139
David Ahernde332ac2012-10-02 22:09:53 -06001140 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001141 if (ret)
1142 goto exit;
1143
David Ahernde332ac2012-10-02 22:09:53 -06001144 sort_result(kvm);
1145 print_result(kvm);
1146
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001147exit:
1148 return ret;
1149}
1150
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001151#define STRDUP_FAIL_EXIT(s) \
1152 ({ char *_p; \
1153 _p = strdup(s); \
1154 if (!_p) \
1155 return -ENOMEM; \
1156 _p; \
1157 })
1158
Hemant Kumar066d3592016-01-28 12:03:06 +05301159int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1160{
1161 return 0;
1162}
1163
Xiao Guangrong37860632012-11-15 14:17:01 +08001164static int
1165kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001166{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001167 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001168 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001169 const char * const record_args[] = {
1170 "record",
1171 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001172 "-m", "1024",
1173 "-c", "1",
1174 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001175 const char * const kvm_stat_record_usage[] = {
1176 "perf kvm stat record [<options>]",
1177 NULL
1178 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001179 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301180 int ret;
1181
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001182 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301183 ret = setup_kvm_events_tp(kvm);
1184 if (ret < 0) {
1185 pr_err("Unable to setup the kvm tracepoints\n");
1186 return ret;
1187 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001188
1189 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1190 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001191
David Ahern8fdd84c2013-08-02 14:05:42 -06001192 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001193 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001194 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1195
1196 if (rec_argv == NULL)
1197 return -ENOMEM;
1198
1199 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1200 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1201
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001202 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001203 rec_argv[i++] = "-e";
1204 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1205 }
1206
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001207 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001208 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001209
1210 for (j = 1; j < (unsigned int)argc; j++, i++)
1211 rec_argv[i] = argv[j];
1212
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001213 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1214 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1215 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1216
1217 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1218 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1219 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1220 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1221 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1222 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1223 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1224 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1225 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1226 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1227 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1228 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1229 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1230 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1231 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1232
1233 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001234 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001235}
1236
Xiao Guangrong37860632012-11-15 14:17:01 +08001237static int
1238kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001239{
David Ahernde332ac2012-10-02 22:09:53 -06001240 const struct option kvm_events_report_options[] = {
1241 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001242 "event for reporting: vmexit, "
1243 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001244 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1245 "vcpu id to report"),
1246 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1247 "key for sorting: sample(sort by samples number)"
1248 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001249 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001250 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001251 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001252 OPT_END()
1253 };
1254
1255 const char * const kvm_events_report_usage[] = {
1256 "perf kvm stat report [<options>]",
1257 NULL
1258 };
1259
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001260 if (argc) {
1261 argc = parse_options(argc, argv,
1262 kvm_events_report_options,
1263 kvm_events_report_usage, 0);
1264 if (argc)
1265 usage_with_options(kvm_events_report_usage,
1266 kvm_events_report_options);
1267 }
1268
Alexander Yaryginf1819572014-09-01 17:44:54 +04001269 if (!kvm->opts.target.pid)
1270 kvm->opts.target.system_wide = true;
1271
David Ahernde332ac2012-10-02 22:09:53 -06001272 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001273}
1274
David Ahern87419c92013-10-29 10:43:16 -06001275#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001276static struct perf_evlist *kvm_live_event_list(void)
1277{
1278 struct perf_evlist *evlist;
1279 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001280 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001281 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001282
1283 evlist = perf_evlist__new();
1284 if (evlist == NULL)
1285 return NULL;
1286
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001287 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001288
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001289 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001290 if (tp == NULL)
1291 goto out;
1292
1293 /* split tracepoint into subsystem and name */
1294 sys = tp;
1295 name = strchr(tp, ':');
1296 if (name == NULL) {
1297 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001298 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001299 free(tp);
1300 goto out;
1301 }
1302 *name = '\0';
1303 name++;
1304
1305 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001306 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001307 free(tp);
1308 goto out;
1309 }
1310
1311 free(tp);
1312 }
1313
1314 err = 0;
1315
1316out:
1317 if (err) {
1318 perf_evlist__delete(evlist);
1319 evlist = NULL;
1320 }
1321
1322 return evlist;
1323}
1324
1325static int kvm_events_live(struct perf_kvm_stat *kvm,
1326 int argc, const char **argv)
1327{
1328 char errbuf[BUFSIZ];
1329 int err;
1330
1331 const struct option live_options[] = {
1332 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1333 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001334 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1335 "number of mmap data pages",
1336 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001337 OPT_INCR('v', "verbose", &verbose,
1338 "be more verbose (show counter open errors, etc)"),
1339 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1340 "system-wide collection from all CPUs"),
1341 OPT_UINTEGER('d', "display", &kvm->display_time,
1342 "time in seconds between display updates"),
1343 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001344 "event for reporting: "
1345 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001346 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1347 "vcpu id to report"),
1348 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1349 "key for sorting: sample(sort by samples number)"
1350 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001351 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001352 "show events other than"
1353 " HLT (x86 only) or Wait state (s390 only)"
1354 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001355 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1356 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001357 OPT_END()
1358 };
1359 const char * const live_usage[] = {
1360 "perf kvm stat live [<options>]",
1361 NULL
1362 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001363 struct perf_data data = {
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001364 .mode = PERF_DATA_MODE_WRITE,
1365 };
David Ahern1afe1d12013-08-05 21:41:34 -04001366
1367
1368 /* event handling */
1369 kvm->tool.sample = process_sample_event;
1370 kvm->tool.comm = perf_event__process_comm;
1371 kvm->tool.exit = perf_event__process_exit;
1372 kvm->tool.fork = perf_event__process_fork;
1373 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301374 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001375 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001376 perf_tool__fill_defaults(&kvm->tool);
1377
1378 /* set defaults */
1379 kvm->display_time = 1;
1380 kvm->opts.user_interval = 1;
1381 kvm->opts.mmap_pages = 512;
1382 kvm->opts.target.uses_mmap = false;
1383 kvm->opts.target.uid_str = NULL;
1384 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001385 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001386
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001387 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001388 disable_buildid_cache();
1389
1390 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001391
1392 if (argc) {
1393 argc = parse_options(argc, argv, live_options,
1394 live_usage, 0);
1395 if (argc)
1396 usage_with_options(live_usage, live_options);
1397 }
1398
David Ahern70f7b4a2013-08-07 21:56:38 -04001399 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1400
David Ahern1afe1d12013-08-05 21:41:34 -04001401 /*
1402 * target related setups
1403 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001404 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001405 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001406 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001407 ui__warning("%s", errbuf);
1408 }
1409
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001410 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001411 kvm->opts.target.system_wide = true;
1412
1413
1414 /*
1415 * generate the event list
1416 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301417 err = setup_kvm_events_tp(kvm);
1418 if (err < 0) {
1419 pr_err("Unable to setup the kvm tracepoints\n");
1420 return err;
1421 }
1422
David Ahern1afe1d12013-08-05 21:41:34 -04001423 kvm->evlist = kvm_live_event_list();
1424 if (kvm->evlist == NULL) {
1425 err = -1;
1426 goto out;
1427 }
1428
1429 symbol_conf.nr_events = kvm->evlist->nr_entries;
1430
1431 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1432 usage_with_options(live_usage, live_options);
1433
1434 /*
1435 * perf session
1436 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001437 kvm->session = perf_session__new(&data, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001438 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001439 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001440 goto out;
1441 }
1442 kvm->session->evlist = kvm->evlist;
1443 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001444 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001445 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang340b47f2017-09-29 07:47:54 -07001446 kvm->evlist->threads, false,
1447 kvm->opts.proc_map_timeout, 1);
David Ahern1afe1d12013-08-05 21:41:34 -04001448 err = kvm_live_open_events(kvm);
1449 if (err)
1450 goto out;
1451
1452 err = kvm_events_live_report(kvm);
1453
1454out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001455 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001456 kvm->session = NULL;
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -03001457 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001458
1459 return err;
1460}
David Ahern87419c92013-10-29 10:43:16 -06001461#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001462
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001463static void print_kvm_stat_usage(void)
1464{
1465 printf("Usage: perf kvm stat <command>\n\n");
1466
1467 printf("# Available commands:\n");
1468 printf("\trecord: record kvm events\n");
1469 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001470 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001471
1472 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1473}
1474
Xiao Guangrong37860632012-11-15 14:17:01 +08001475static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001476{
Xiao Guangrong37860632012-11-15 14:17:01 +08001477 struct perf_kvm_stat kvm = {
1478 .file_name = file_name,
1479
1480 .trace_vcpu = -1,
1481 .report_event = "vmexit",
1482 .sort_key = "sample",
1483
Xiao Guangrong37860632012-11-15 14:17:01 +08001484 };
1485
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001486 if (argc == 1) {
1487 print_kvm_stat_usage();
1488 goto perf_stat;
1489 }
1490
1491 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001492 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001493
1494 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001495 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001496
David Ahern87419c92013-10-29 10:43:16 -06001497#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001498 if (!strncmp(argv[1], "live", 4))
1499 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001500#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001501
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001502perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001503 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001504}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001505#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001506
Xiao Guangrong37860632012-11-15 14:17:01 +08001507static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001508{
1509 int rec_argc, i = 0, j;
1510 const char **rec_argv;
1511
1512 rec_argc = argc + 2;
1513 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1514 rec_argv[i++] = strdup("record");
1515 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001516 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001517 for (j = 1; j < argc; j++, i++)
1518 rec_argv[i] = argv[j];
1519
1520 BUG_ON(i != rec_argc);
1521
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001522 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001523}
1524
Xiao Guangrong37860632012-11-15 14:17:01 +08001525static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001526{
1527 int rec_argc, i = 0, j;
1528 const char **rec_argv;
1529
1530 rec_argc = argc + 2;
1531 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1532 rec_argv[i++] = strdup("report");
1533 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001534 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001535 for (j = 1; j < argc; j++, i++)
1536 rec_argv[i] = argv[j];
1537
1538 BUG_ON(i != rec_argc);
1539
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001540 return cmd_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001541}
1542
Xiao Guangrong37860632012-11-15 14:17:01 +08001543static int
1544__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001545{
1546 int rec_argc, i = 0, j;
1547 const char **rec_argv;
1548
1549 rec_argc = argc + 2;
1550 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1551 rec_argv[i++] = strdup("buildid-list");
1552 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001553 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001554 for (j = 1; j < argc; j++, i++)
1555 rec_argv[i] = argv[j];
1556
1557 BUG_ON(i != rec_argc);
1558
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001559 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001560}
1561
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001562int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001563{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001564 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001565 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001566 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001567 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001568 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001569 "Output file name"),
1570 OPT_BOOLEAN(0, "guest", &perf_guest,
1571 "Collect guest os data"),
1572 OPT_BOOLEAN(0, "host", &perf_host,
1573 "Collect host os data"),
1574 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1575 "guest mount directory under which every guest os"
1576 " instance has a subdir"),
1577 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1578 "file", "file saving guest os vmlinux"),
1579 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1580 "file", "file saving guest os /proc/kallsyms"),
1581 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1582 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001583 OPT_INCR('v', "verbose", &verbose,
1584 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001585 OPT_END()
1586 };
1587
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001588 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1589 "buildid-list", "stat", NULL };
1590 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001591
Joerg Roedel1aed2672012-01-04 17:54:20 +01001592 perf_host = 0;
1593 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001594
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001595 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1596 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001597 if (!argc)
1598 usage_with_options(kvm_usage, kvm_options);
1599
1600 if (!perf_host)
1601 perf_guest = 1;
1602
Xiao Guangrong37860632012-11-15 14:17:01 +08001603 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001604 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001605
Xiao Guangrong37860632012-11-15 14:17:01 +08001606 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001607 pr_err("Failed to allocate memory for filename\n");
1608 return -ENOMEM;
1609 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001610 }
1611
1612 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001613 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001614 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001615 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001616 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001617 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001618 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001619 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001620 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001621 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001622#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001623 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001624 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001625#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001626 else
1627 usage_with_options(kvm_usage, kvm_options);
1628
1629 return 0;
1630}