blob: a7d7f4c6052e2ff89ff492cf395807189eaeeb05 [file] [log] [blame]
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001#include "builtin.h"
2#include "perf.h"
3
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08004#include "util/evsel.h"
David Ahern1afe1d12013-08-05 21:41:34 -04005#include "util/evlist.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08006#include "util/util.h"
7#include "util/cache.h"
8#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
11#include "util/session.h"
David Ahern2e73f002013-08-05 21:41:37 -040012#include "util/intlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060013#include <subcmd/parse-options.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080014#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080016#include "util/tool.h"
17#include "util/stat.h"
David Ahern1afe1d12013-08-05 21:41:34 -040018#include "util/top.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030020#include "util/ordered-events.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080021
22#include <sys/prctl.h>
David Ahern87419c92013-10-29 10:43:16 -060023#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -040024#include <sys/timerfd.h>
David Ahern87419c92013-10-29 10:43:16 -060025#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080026
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030027#include <linux/kernel.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030028#include <linux/time64.h>
David Ahern1afe1d12013-08-05 21:41:34 -040029#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080030#include <semaphore.h>
31#include <pthread.h>
32#include <math.h>
33
Alexander Yaryginda50ad62014-07-03 17:59:49 +040034#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040035#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080036
Alexander Yarygin9daa8122014-07-03 18:29:05 +040037void exit_event_get_key(struct perf_evsel *evsel,
38 struct perf_sample *sample,
39 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080040{
41 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053042 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080043}
44
Alexander Yarygin9daa8122014-07-03 18:29:05 +040045bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080046{
Hemant Kumar162607e2016-01-28 12:03:04 +053047 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080048}
49
Alexander Yarygin9daa8122014-07-03 18:29:05 +040050bool exit_event_begin(struct perf_evsel *evsel,
51 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080052{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030053 if (kvm_exit_event(evsel)) {
54 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080055 return true;
56 }
57
58 return false;
59}
60
Alexander Yarygin9daa8122014-07-03 18:29:05 +040061bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080062{
Hemant Kumar162607e2016-01-28 12:03:04 +053063 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080064}
65
Alexander Yarygin9daa8122014-07-03 18:29:05 +040066bool exit_event_end(struct perf_evsel *evsel,
67 struct perf_sample *sample __maybe_unused,
68 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080069{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030070 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080071}
72
Alexander Yarygindf74c132014-07-03 17:59:50 +040073static const char *get_exit_reason(struct perf_kvm_stat *kvm,
74 struct exit_reasons_table *tbl,
75 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080076{
Alexander Yarygindf74c132014-07-03 17:59:50 +040077 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060078 if (tbl->exit_code == exit_code)
79 return tbl->reason;
80 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080081 }
82
83 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -060084 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080085 return "UNKNOWN";
86}
87
Alexander Yarygin9daa8122014-07-03 18:29:05 +040088void exit_event_decode_key(struct perf_kvm_stat *kvm,
89 struct event_key *key,
90 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080091{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +040092 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +040093 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080094
Hemant Kumar162607e2016-01-28 12:03:04 +053095 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080096}
97
Xiao Guangrong37860632012-11-15 14:17:01 +080098static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080099{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400100 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800101
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400102 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
103 if (!strcmp(events_ops->name, kvm->report_event)) {
104 kvm->events_ops = events_ops->ops;
105 return true;
106 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800107 }
108
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400109 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800110}
111
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800112struct vcpu_event_record {
113 int vcpu_id;
114 u64 start_time;
115 struct kvm_event *last_event;
116};
117
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800118
Xiao Guangrong37860632012-11-15 14:17:01 +0800119static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800120{
David Ahernb880dee2012-10-08 11:17:30 -0600121 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800122
David Ahernb880dee2012-10-08 11:17:30 -0600123 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600124 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800125}
126
David Ahern87419c92013-10-29 10:43:16 -0600127#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400128static void clear_events_cache_stats(struct list_head *kvm_events_cache)
129{
130 struct list_head *head;
131 struct kvm_event *event;
132 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400133 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400134
135 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
136 head = &kvm_events_cache[i];
137 list_for_each_entry(event, head, hash_entry) {
138 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400139 event->total.time = 0;
140 init_stats(&event->total.stats);
141
142 for (j = 0; j < event->max_vcpu; ++j) {
143 event->vcpu[j].time = 0;
144 init_stats(&event->vcpu[j].stats);
145 }
David Ahern1afe1d12013-08-05 21:41:34 -0400146 }
147 }
148}
David Ahern87419c92013-10-29 10:43:16 -0600149#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400150
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800151static int kvm_events_hash_fn(u64 key)
152{
153 return key & (EVENTS_CACHE_SIZE - 1);
154}
155
156static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
157{
158 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600159 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800160
161 if (vcpu_id < event->max_vcpu)
162 return true;
163
164 while (event->max_vcpu <= vcpu_id)
165 event->max_vcpu += DEFAULT_VCPU_NUM;
166
David Ahern6ca5f302013-05-25 18:24:46 -0600167 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800168 event->vcpu = realloc(event->vcpu,
169 event->max_vcpu * sizeof(*event->vcpu));
170 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600171 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800172 pr_err("Not enough memory\n");
173 return false;
174 }
175
176 memset(event->vcpu + old_max_vcpu, 0,
177 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
178 return true;
179}
180
181static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
182{
183 struct kvm_event *event;
184
185 event = zalloc(sizeof(*event));
186 if (!event) {
187 pr_err("Not enough memory\n");
188 return NULL;
189 }
190
191 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200192 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800193 return event;
194}
195
Xiao Guangrong37860632012-11-15 14:17:01 +0800196static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600197 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800198{
199 struct kvm_event *event;
200 struct list_head *head;
201
202 BUG_ON(key->key == INVALID_KEY);
203
David Ahernde332ac2012-10-02 22:09:53 -0600204 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600205 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800206 if (event->key.key == key->key && event->key.info == key->info)
207 return event;
David Ahern355afe82012-10-08 11:17:32 -0600208 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800209
210 event = kvm_alloc_init_event(key);
211 if (!event)
212 return NULL;
213
214 list_add(&event->hash_entry, head);
215 return event;
216}
217
Xiao Guangrong37860632012-11-15 14:17:01 +0800218static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600219 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800220 struct event_key *key, u64 timestamp)
221{
222 struct kvm_event *event = NULL;
223
224 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600225 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800226
227 vcpu_record->last_event = event;
228 vcpu_record->start_time = timestamp;
229 return true;
230}
231
232static void
233kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
234{
235 kvm_stats->time += time_diff;
236 update_stats(&kvm_stats->stats, time_diff);
237}
238
239static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
240{
241 struct kvm_event_stats *kvm_stats = &event->total;
242
243 if (vcpu_id != -1)
244 kvm_stats = &event->vcpu[vcpu_id];
245
246 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
247 avg_stats(&kvm_stats->stats));
248}
249
250static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
251 u64 time_diff)
252{
David Ahern2aa8eab2012-10-08 11:17:35 -0600253 if (vcpu_id == -1) {
254 kvm_update_event_stats(&event->total, time_diff);
255 return true;
256 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800257
258 if (!kvm_event_expand(event, vcpu_id))
259 return false;
260
261 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
262 return true;
263}
264
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400265static bool is_child_event(struct perf_kvm_stat *kvm,
266 struct perf_evsel *evsel,
267 struct perf_sample *sample,
268 struct event_key *key)
269{
270 struct child_event_ops *child_ops;
271
272 child_ops = kvm->events_ops->child_ops;
273
274 if (!child_ops)
275 return false;
276
277 for (; child_ops->name; child_ops++) {
278 if (!strcmp(evsel->name, child_ops->name)) {
279 child_ops->get_key(evsel, sample, key);
280 return true;
281 }
282 }
283
284 return false;
285}
286
287static bool handle_child_event(struct perf_kvm_stat *kvm,
288 struct vcpu_event_record *vcpu_record,
289 struct event_key *key,
290 struct perf_sample *sample __maybe_unused)
291{
292 struct kvm_event *event = NULL;
293
294 if (key->key != INVALID_KEY)
295 event = find_create_kvm_event(kvm, key);
296
297 vcpu_record->last_event = event;
298
299 return true;
300}
301
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400302static bool skip_event(const char *event)
303{
304 const char * const *skip_events;
305
306 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
307 if (!strcmp(event, *skip_events))
308 return true;
309
310 return false;
311}
312
Xiao Guangrong37860632012-11-15 14:17:01 +0800313static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600314 struct vcpu_event_record *vcpu_record,
315 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400316 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800317{
318 struct kvm_event *event;
319 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600320 int vcpu;
321
322 if (kvm->trace_vcpu == -1)
323 vcpu = -1;
324 else
325 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800326
327 event = vcpu_record->last_event;
328 time_begin = vcpu_record->start_time;
329
330 /* The begin event is not caught. */
331 if (!time_begin)
332 return true;
333
334 /*
335 * In some case, the 'begin event' only records the start timestamp,
336 * the actual event is recognized in the 'end event' (e.g. mmio-event).
337 */
338
339 /* Both begin and end events did not get the key. */
340 if (!event && key->key == INVALID_KEY)
341 return true;
342
343 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600344 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800345
346 if (!event)
347 return false;
348
349 vcpu_record->last_event = NULL;
350 vcpu_record->start_time = 0;
351
David Ahern1afe1d12013-08-05 21:41:34 -0400352 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400353 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400354 pr_debug("End time before begin time; skipping event.\n");
355 return true;
356 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800357
David Ahern70f7b4a2013-08-07 21:56:38 -0400358 time_diff = sample->time - time_begin;
359
360 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530361 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400362
363 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400364 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400365 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
366 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300367 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400368 }
369 }
370
David Ahern2aa8eab2012-10-08 11:17:35 -0600371 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800372}
373
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300374static
375struct vcpu_event_record *per_vcpu_record(struct thread *thread,
376 struct perf_evsel *evsel,
377 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800378{
379 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900380 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800381 struct vcpu_event_record *vcpu_record;
382
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300383 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800384 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300385 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800386 return NULL;
387 }
388
Hemant Kumar162607e2016-01-28 12:03:04 +0530389 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
390 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900391 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800392 }
393
Namhyung Kim69e865c2014-10-06 09:46:02 +0900394 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800395}
396
Xiao Guangrong37860632012-11-15 14:17:01 +0800397static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600398 struct thread *thread,
399 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300400 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800401{
402 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400403 struct event_key key = { .key = INVALID_KEY,
404 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800405
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300406 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800407 if (!vcpu_record)
408 return true;
409
David Ahern2aa8eab2012-10-08 11:17:35 -0600410 /* only process events for vcpus user cares about */
411 if ((kvm->trace_vcpu != -1) &&
412 (kvm->trace_vcpu != vcpu_record->vcpu_id))
413 return true;
414
David Ahernde332ac2012-10-02 22:09:53 -0600415 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
416 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800417
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400418 if (is_child_event(kvm, evsel, sample, &key))
419 return handle_child_event(kvm, vcpu_record, &key, sample);
420
David Ahernde332ac2012-10-02 22:09:53 -0600421 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400422 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800423
424 return true;
425}
426
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800427#define GET_EVENT_KEY(func, field) \
428static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
429{ \
430 if (vcpu == -1) \
431 return event->total.field; \
432 \
433 if (vcpu >= event->max_vcpu) \
434 return 0; \
435 \
436 return event->vcpu[vcpu].field; \
437}
438
439#define COMPARE_EVENT_KEY(func, field) \
440GET_EVENT_KEY(func, field) \
441static int compare_kvm_event_ ## func(struct kvm_event *one, \
442 struct kvm_event *two, int vcpu)\
443{ \
444 return get_event_ ##func(one, vcpu) > \
445 get_event_ ##func(two, vcpu); \
446}
447
448GET_EVENT_KEY(time, time);
449COMPARE_EVENT_KEY(count, stats.n);
450COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400451GET_EVENT_KEY(max, stats.max);
452GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800453
454#define DEF_SORT_NAME_KEY(name, compare_key) \
455 { #name, compare_kvm_event_ ## compare_key }
456
457static struct kvm_event_key keys[] = {
458 DEF_SORT_NAME_KEY(sample, count),
459 DEF_SORT_NAME_KEY(time, mean),
460 { NULL, NULL }
461};
462
Xiao Guangrong37860632012-11-15 14:17:01 +0800463static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800464{
465 int i;
466
467 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600468 if (!strcmp(keys[i].name, kvm->sort_key)) {
469 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800470 return true;
471 }
472 }
473
David Ahernde332ac2012-10-02 22:09:53 -0600474 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800475 return false;
476}
477
David Ahernde332ac2012-10-02 22:09:53 -0600478static void insert_to_result(struct rb_root *result, struct kvm_event *event,
479 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800480{
David Ahernde332ac2012-10-02 22:09:53 -0600481 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800482 struct rb_node *parent = NULL;
483 struct kvm_event *p;
484
485 while (*rb) {
486 p = container_of(*rb, struct kvm_event, rb);
487 parent = *rb;
488
489 if (bigger(event, p, vcpu))
490 rb = &(*rb)->rb_left;
491 else
492 rb = &(*rb)->rb_right;
493 }
494
495 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600496 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800497}
498
Xiao Guangrong37860632012-11-15 14:17:01 +0800499static void
500update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800501{
David Ahernde332ac2012-10-02 22:09:53 -0600502 int vcpu = kvm->trace_vcpu;
503
504 kvm->total_count += get_event_count(event, vcpu);
505 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800506}
507
508static bool event_is_valid(struct kvm_event *event, int vcpu)
509{
510 return !!get_event_count(event, vcpu);
511}
512
Xiao Guangrong37860632012-11-15 14:17:01 +0800513static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800514{
515 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600516 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800517 struct kvm_event *event;
518
David Ahern355afe82012-10-08 11:17:32 -0600519 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
520 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800521 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600522 update_total_count(kvm, event);
523 insert_to_result(&kvm->result, event,
524 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800525 }
David Ahern355afe82012-10-08 11:17:32 -0600526 }
527 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800528}
529
530/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600531static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800532{
David Ahernde332ac2012-10-02 22:09:53 -0600533 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800534
535 if (!node)
536 return NULL;
537
David Ahernde332ac2012-10-02 22:09:53 -0600538 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800539 return container_of(node, struct kvm_event, rb);
540}
541
David Ahern1afe1d12013-08-05 21:41:34 -0400542static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800543{
David Ahern1afe1d12013-08-05 21:41:34 -0400544 int vcpu = kvm->trace_vcpu;
545
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800546 pr_info("Analyze events for ");
547
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400548 if (kvm->opts.target.system_wide)
549 pr_info("all VMs, ");
550 else if (kvm->opts.target.pid)
551 pr_info("pid(s) %s, ", kvm->opts.target.pid);
552 else
553 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400554
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800555 if (vcpu == -1)
556 pr_info("all VCPUs:\n\n");
557 else
558 pr_info("VCPU %d:\n\n", vcpu);
559}
560
David Ahern1afe1d12013-08-05 21:41:34 -0400561static void show_timeofday(void)
562{
563 char date[64];
564 struct timeval tv;
565 struct tm ltime;
566
567 gettimeofday(&tv, NULL);
568 if (localtime_r(&tv.tv_sec, &ltime)) {
569 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
570 pr_info("%s.%06ld", date, tv.tv_usec);
571 } else
572 pr_info("00:00:00.000000");
573
574 return;
575}
576
Xiao Guangrong37860632012-11-15 14:17:01 +0800577static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800578{
Hemant Kumar162607e2016-01-28 12:03:04 +0530579 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800580 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600581 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800582
David Ahern1afe1d12013-08-05 21:41:34 -0400583 if (kvm->live) {
584 puts(CONSOLE_CLEAR);
585 show_timeofday();
586 }
587
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800588 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400589 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530590 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800591 pr_info("%10s ", "Samples");
592 pr_info("%9s ", "Samples%");
593
594 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200595 pr_info("%11s ", "Min Time");
596 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800597 pr_info("%16s ", "Avg time");
598 pr_info("\n\n");
599
David Ahernde332ac2012-10-02 22:09:53 -0600600 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400601 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800602
603 ecount = get_event_count(event, vcpu);
604 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400605 max = get_event_max(event, vcpu);
606 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800607
David Ahernde332ac2012-10-02 22:09:53 -0600608 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530609 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800610 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600611 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
612 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300613 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
614 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
615 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800616 kvm_event_rel_stddev(vcpu, event));
617 pr_info("\n");
618 }
619
David Aherne4f76372012-10-08 11:17:34 -0600620 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300621 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400622
623 if (kvm->lost_events)
624 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
625}
626
David Ahern87419c92013-10-29 10:43:16 -0600627#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400628static int process_lost_event(struct perf_tool *tool,
629 union perf_event *event __maybe_unused,
630 struct perf_sample *sample __maybe_unused,
631 struct machine *machine __maybe_unused)
632{
633 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
634
635 kvm->lost_events++;
636 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800637}
David Ahern87419c92013-10-29 10:43:16 -0600638#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800639
David Ahern2e73f002013-08-05 21:41:37 -0400640static bool skip_sample(struct perf_kvm_stat *kvm,
641 struct perf_sample *sample)
642{
643 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
644 return true;
645
646 return false;
647}
648
David Ahernde332ac2012-10-02 22:09:53 -0600649static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800650 union perf_event *event,
651 struct perf_sample *sample,
652 struct perf_evsel *evsel,
653 struct machine *machine)
654{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300655 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400656 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800657 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
658 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800659
David Ahern2e73f002013-08-05 21:41:37 -0400660 if (skip_sample(kvm, sample))
661 return 0;
662
Adrian Hunter314add62013-08-27 11:23:03 +0300663 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800664 if (thread == NULL) {
665 pr_debug("problem processing %d event, skipping it.\n",
666 event->header.type);
667 return -1;
668 }
669
David Ahernde332ac2012-10-02 22:09:53 -0600670 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300671 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800672
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300673 thread__put(thread);
674 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800675}
676
David Ahern1afe1d12013-08-05 21:41:34 -0400677static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800678{
David Ahern1afe1d12013-08-05 21:41:34 -0400679 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400680 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400681
682 if (kvm->live) {
683 err = get_cpuid(buf, sizeof(buf));
684 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400685 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400686 return err;
687 }
688 cpuid = buf;
689 } else
690 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800691
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400692 if (!cpuid) {
693 pr_err("Failed to look up CPU type\n");
694 return -EINVAL;
695 }
696
697 err = cpu_isa_init(kvm, cpuid);
698 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800699 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800700
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400701 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400702}
703
704static bool verify_vcpu(int vcpu)
705{
706 if (vcpu != -1 && vcpu < 0) {
707 pr_err("Invalid vcpu:%d.\n", vcpu);
708 return false;
709 }
710
711 return true;
712}
713
David Ahern87419c92013-10-29 10:43:16 -0600714#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400715/* keeping the max events to a modest level to keep
716 * the processing of samples per mmap smooth.
717 */
718#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
719
720static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
721 u64 *mmap_time)
722{
723 union perf_event *event;
724 struct perf_sample sample;
725 s64 n = 0;
726 int err;
727
728 *mmap_time = ULLONG_MAX;
729 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
730 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
731 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800732 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400733 pr_err("Failed to parse sample\n");
734 return -1;
735 }
736
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300737 err = perf_session__queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800738 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300739 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800740 * point to it, and it'll get possibly overwritten by the kernel.
741 */
742 perf_evlist__mmap_consume(kvm->evlist, idx);
743
David Ahern1afe1d12013-08-05 21:41:34 -0400744 if (err) {
745 pr_err("Failed to enqueue sample: %d\n", err);
746 return -1;
747 }
748
749 /* save time stamp of our first sample for this mmap */
750 if (n == 0)
751 *mmap_time = sample.time;
752
753 /* limit events per mmap handled all at once */
754 n++;
755 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
756 break;
757 }
758
759 return n;
760}
761
762static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
763{
764 int i, err, throttled = 0;
765 s64 n, ntotal = 0;
766 u64 flush_time = ULLONG_MAX, mmap_time;
767
768 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
769 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
770 if (n < 0)
771 return -1;
772
773 /* flush time is going to be the minimum of all the individual
774 * mmap times. Essentially, we flush all the samples queued up
775 * from the last pass under our minimal start time -- that leaves
776 * a very small race for samples to come in with a lower timestamp.
777 * The ioctl to return the perf_clock timestamp should close the
778 * race entirely.
779 */
780 if (mmap_time < flush_time)
781 flush_time = mmap_time;
782
783 ntotal += n;
784 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
785 throttled = 1;
786 }
787
788 /* flush queue after each round in which we processed events */
789 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300790 struct ordered_events *oe = &kvm->session->ordered_events;
791
792 oe->next_flush = flush_time;
793 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400794 if (err) {
795 if (kvm->lost_events)
796 pr_info("\nLost events: %" PRIu64 "\n\n",
797 kvm->lost_events);
798 return err;
799 }
800 }
801
802 return throttled;
803}
804
805static volatile int done;
806
807static void sig_handler(int sig __maybe_unused)
808{
809 done = 1;
810}
811
812static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
813{
814 struct itimerspec new_value;
815 int rc = -1;
816
817 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
818 if (kvm->timerfd < 0) {
819 pr_err("timerfd_create failed\n");
820 goto out;
821 }
822
823 new_value.it_value.tv_sec = kvm->display_time;
824 new_value.it_value.tv_nsec = 0;
825 new_value.it_interval.tv_sec = kvm->display_time;
826 new_value.it_interval.tv_nsec = 0;
827
828 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
829 pr_err("timerfd_settime failed: %d\n", errno);
830 close(kvm->timerfd);
831 goto out;
832 }
833
834 rc = 0;
835out:
836 return rc;
837}
838
839static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
840{
841 uint64_t c;
842 int rc;
843
844 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
845 if (rc < 0) {
846 if (errno == EAGAIN)
847 return 0;
848
849 pr_err("Failed to read timer fd: %d\n", errno);
850 return -1;
851 }
852
853 if (rc != sizeof(uint64_t)) {
854 pr_err("Error reading timer fd - invalid size returned\n");
855 return -1;
856 }
857
858 if (c != 1)
859 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
860
861 /* update display */
862 sort_result(kvm);
863 print_result(kvm);
864
865 /* reset counts */
866 clear_events_cache_stats(kvm->kvm_events_cache);
867 kvm->total_count = 0;
868 kvm->total_time = 0;
869 kvm->lost_events = 0;
870
871 return 0;
872}
873
874static int fd_set_nonblock(int fd)
875{
876 long arg = 0;
877
878 arg = fcntl(fd, F_GETFL);
879 if (arg < 0) {
880 pr_err("Failed to get current flags for fd %d\n", fd);
881 return -1;
882 }
883
884 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
885 pr_err("Failed to set non-block option on fd %d\n", fd);
886 return -1;
887 }
888
889 return 0;
890}
891
Jiri Olsad5b41302014-08-11 10:50:09 +0200892static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400893{
894 int c;
895
David Ahern1afe1d12013-08-05 21:41:34 -0400896 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400897 if (c == 'q')
898 return 1;
899
900 return 0;
901}
902
903static int kvm_events_live_report(struct perf_kvm_stat *kvm)
904{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200905 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200906 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400907
908 /* live flag must be set first */
909 kvm->live = true;
910
911 ret = cpu_isa_config(kvm);
912 if (ret < 0)
913 return ret;
914
915 if (!verify_vcpu(kvm->trace_vcpu) ||
916 !select_key(kvm) ||
917 !register_kvm_events_ops(kvm)) {
918 goto out;
919 }
920
Jiri Olsad5b41302014-08-11 10:50:09 +0200921 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400922 init_kvm_event_record(kvm);
923
David Ahern1afe1d12013-08-05 21:41:34 -0400924 signal(SIGINT, sig_handler);
925 signal(SIGTERM, sig_handler);
926
David Ahern1afe1d12013-08-05 21:41:34 -0400927 /* add timer fd */
928 if (perf_kvm__timerfd_create(kvm) < 0) {
929 err = -1;
930 goto out;
931 }
932
Jiri Olsafe636ad2014-10-01 18:00:27 +0200933 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300934 goto out;
935
Jiri Olsa0cae0132014-10-01 18:00:28 +0200936 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
937 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300938 goto out;
939
David Ahern1afe1d12013-08-05 21:41:34 -0400940 if (fd_set_nonblock(fileno(stdin)) != 0)
941 goto out;
942
943 /* everything is good - enable the events and process */
944 perf_evlist__enable(kvm->evlist);
945
946 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200947 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400948 int rc;
949
950 rc = perf_kvm__mmap_read(kvm);
951 if (rc < 0)
952 break;
953
954 err = perf_kvm__handle_timerfd(kvm);
955 if (err)
956 goto out;
957
Jiri Olsa1ca72262014-10-01 18:00:29 +0200958 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200959 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400960
961 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200962 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400963 }
964
965 perf_evlist__disable(kvm->evlist);
966
967 if (err == 0) {
968 sort_result(kvm);
969 print_result(kvm);
970 }
971
972out:
973 if (kvm->timerfd >= 0)
974 close(kvm->timerfd);
975
Jiri Olsad5b41302014-08-11 10:50:09 +0200976 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -0400977 return err;
978}
979
980static int kvm_live_open_events(struct perf_kvm_stat *kvm)
981{
982 int err, rc = -1;
983 struct perf_evsel *pos;
984 struct perf_evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +0000985 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -0400986
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300987 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -0400988
989 /*
990 * Note: exclude_{guest,host} do not apply here.
991 * This command processes KVM tracepoints from host only
992 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300993 evlist__for_each_entry(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -0400994 struct perf_event_attr *attr = &pos->attr;
995
996 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +0300997 perf_evsel__set_sample_bit(pos, TID);
998 perf_evsel__set_sample_bit(pos, TIME);
999 perf_evsel__set_sample_bit(pos, CPU);
1000 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001001 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001002 perf_evsel__reset_sample_bit(pos, PERIOD);
1003 perf_evsel__reset_sample_bit(pos, IP);
1004 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1005 perf_evsel__reset_sample_bit(pos, ADDR);
1006 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001007 attr->mmap = 0;
1008 attr->comm = 0;
1009 attr->task = 0;
1010
1011 attr->sample_period = 1;
1012
1013 attr->watermark = 0;
1014 attr->wakeup_events = 1000;
1015
1016 /* will enable all once we are ready */
1017 attr->disabled = 1;
1018 }
1019
1020 err = perf_evlist__open(evlist);
1021 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001022 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001023 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001024 goto out;
1025 }
1026
1027 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001028 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001029 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001030 perf_evlist__close(evlist);
1031 goto out;
1032 }
1033
1034 rc = 0;
1035
1036out:
1037 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001038}
David Ahern87419c92013-10-29 10:43:16 -06001039#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001040
Xiao Guangrong37860632012-11-15 14:17:01 +08001041static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001042{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001043 int ret;
1044
David Ahernde332ac2012-10-02 22:09:53 -06001045 struct perf_tool eops = {
1046 .sample = process_sample_event,
1047 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301048 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001049 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001050 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001051 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001052 .path = kvm->file_name,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001053 .mode = PERF_DATA_MODE_READ,
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001054 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001055 };
David Ahernde332ac2012-10-02 22:09:53 -06001056
1057 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001058 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001059 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001060 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001061 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001062 }
1063
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001064 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001065
Taeung Song41b98362015-07-01 00:18:44 +09001066 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1067 ret = -EINVAL;
1068 goto out_delete;
1069 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001070
1071 /*
1072 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1073 * traced in the old kernel.
1074 */
David Ahern1afe1d12013-08-05 21:41:34 -04001075 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001076 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001077 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001078
Taeung Song41b98362015-07-01 00:18:44 +09001079 ret = perf_session__process_events(kvm->session);
1080
1081out_delete:
1082 perf_session__delete(kvm->session);
1083 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001084}
1085
David Ahern2e73f002013-08-05 21:41:37 -04001086static int parse_target_str(struct perf_kvm_stat *kvm)
1087{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001088 if (kvm->opts.target.pid) {
1089 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001090 if (kvm->pid_list == NULL) {
1091 pr_err("Error parsing process id string\n");
1092 return -EINVAL;
1093 }
1094 }
1095
1096 return 0;
1097}
1098
Xiao Guangrong37860632012-11-15 14:17:01 +08001099static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001100{
1101 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001102 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001103
David Ahern2e73f002013-08-05 21:41:37 -04001104 if (parse_target_str(kvm) != 0)
1105 goto exit;
1106
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001107 if (!verify_vcpu(vcpu))
1108 goto exit;
1109
David Ahernde332ac2012-10-02 22:09:53 -06001110 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001111 goto exit;
1112
David Ahernde332ac2012-10-02 22:09:53 -06001113 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001114 goto exit;
1115
David Ahernde332ac2012-10-02 22:09:53 -06001116 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001117 setup_pager();
1118
David Ahernde332ac2012-10-02 22:09:53 -06001119 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001120 if (ret)
1121 goto exit;
1122
David Ahernde332ac2012-10-02 22:09:53 -06001123 sort_result(kvm);
1124 print_result(kvm);
1125
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001126exit:
1127 return ret;
1128}
1129
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001130#define STRDUP_FAIL_EXIT(s) \
1131 ({ char *_p; \
1132 _p = strdup(s); \
1133 if (!_p) \
1134 return -ENOMEM; \
1135 _p; \
1136 })
1137
Hemant Kumar066d3592016-01-28 12:03:06 +05301138int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1139{
1140 return 0;
1141}
1142
Xiao Guangrong37860632012-11-15 14:17:01 +08001143static int
1144kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001145{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001146 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001147 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001148 const char * const record_args[] = {
1149 "record",
1150 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001151 "-m", "1024",
1152 "-c", "1",
1153 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001154 const char * const kvm_stat_record_usage[] = {
1155 "perf kvm stat record [<options>]",
1156 NULL
1157 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001158 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301159 int ret;
1160
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001161 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301162 ret = setup_kvm_events_tp(kvm);
1163 if (ret < 0) {
1164 pr_err("Unable to setup the kvm tracepoints\n");
1165 return ret;
1166 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001167
1168 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1169 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001170
David Ahern8fdd84c2013-08-02 14:05:42 -06001171 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001172 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001173 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1174
1175 if (rec_argv == NULL)
1176 return -ENOMEM;
1177
1178 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1179 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1180
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001181 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001182 rec_argv[i++] = "-e";
1183 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1184 }
1185
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001186 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001187 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001188
1189 for (j = 1; j < (unsigned int)argc; j++, i++)
1190 rec_argv[i] = argv[j];
1191
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001192 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1193 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1194 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1195
1196 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1197 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1198 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1199 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1200 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1201 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1202 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1203 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1204 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1205 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1206 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1207 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1208 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1209 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1210 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1211
1212 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001213 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001214}
1215
Xiao Guangrong37860632012-11-15 14:17:01 +08001216static int
1217kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001218{
David Ahernde332ac2012-10-02 22:09:53 -06001219 const struct option kvm_events_report_options[] = {
1220 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001221 "event for reporting: vmexit, "
1222 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001223 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1224 "vcpu id to report"),
1225 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1226 "key for sorting: sample(sort by samples number)"
1227 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001228 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001229 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001230 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001231 OPT_END()
1232 };
1233
1234 const char * const kvm_events_report_usage[] = {
1235 "perf kvm stat report [<options>]",
1236 NULL
1237 };
1238
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001239 if (argc) {
1240 argc = parse_options(argc, argv,
1241 kvm_events_report_options,
1242 kvm_events_report_usage, 0);
1243 if (argc)
1244 usage_with_options(kvm_events_report_usage,
1245 kvm_events_report_options);
1246 }
1247
Alexander Yaryginf1819572014-09-01 17:44:54 +04001248 if (!kvm->opts.target.pid)
1249 kvm->opts.target.system_wide = true;
1250
David Ahernde332ac2012-10-02 22:09:53 -06001251 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001252}
1253
David Ahern87419c92013-10-29 10:43:16 -06001254#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001255static struct perf_evlist *kvm_live_event_list(void)
1256{
1257 struct perf_evlist *evlist;
1258 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001259 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001260 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001261
1262 evlist = perf_evlist__new();
1263 if (evlist == NULL)
1264 return NULL;
1265
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001266 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001267
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001268 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001269 if (tp == NULL)
1270 goto out;
1271
1272 /* split tracepoint into subsystem and name */
1273 sys = tp;
1274 name = strchr(tp, ':');
1275 if (name == NULL) {
1276 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001277 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001278 free(tp);
1279 goto out;
1280 }
1281 *name = '\0';
1282 name++;
1283
1284 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001285 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001286 free(tp);
1287 goto out;
1288 }
1289
1290 free(tp);
1291 }
1292
1293 err = 0;
1294
1295out:
1296 if (err) {
1297 perf_evlist__delete(evlist);
1298 evlist = NULL;
1299 }
1300
1301 return evlist;
1302}
1303
1304static int kvm_events_live(struct perf_kvm_stat *kvm,
1305 int argc, const char **argv)
1306{
1307 char errbuf[BUFSIZ];
1308 int err;
1309
1310 const struct option live_options[] = {
1311 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1312 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001313 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1314 "number of mmap data pages",
1315 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001316 OPT_INCR('v', "verbose", &verbose,
1317 "be more verbose (show counter open errors, etc)"),
1318 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1319 "system-wide collection from all CPUs"),
1320 OPT_UINTEGER('d', "display", &kvm->display_time,
1321 "time in seconds between display updates"),
1322 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001323 "event for reporting: "
1324 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001325 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1326 "vcpu id to report"),
1327 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1328 "key for sorting: sample(sort by samples number)"
1329 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001330 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001331 "show events other than"
1332 " HLT (x86 only) or Wait state (s390 only)"
1333 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001334 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1335 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001336 OPT_END()
1337 };
1338 const char * const live_usage[] = {
1339 "perf kvm stat live [<options>]",
1340 NULL
1341 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001342 struct perf_data_file file = {
1343 .mode = PERF_DATA_MODE_WRITE,
1344 };
David Ahern1afe1d12013-08-05 21:41:34 -04001345
1346
1347 /* event handling */
1348 kvm->tool.sample = process_sample_event;
1349 kvm->tool.comm = perf_event__process_comm;
1350 kvm->tool.exit = perf_event__process_exit;
1351 kvm->tool.fork = perf_event__process_fork;
1352 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301353 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001354 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001355 perf_tool__fill_defaults(&kvm->tool);
1356
1357 /* set defaults */
1358 kvm->display_time = 1;
1359 kvm->opts.user_interval = 1;
1360 kvm->opts.mmap_pages = 512;
1361 kvm->opts.target.uses_mmap = false;
1362 kvm->opts.target.uid_str = NULL;
1363 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001364 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001365
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001366 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001367 disable_buildid_cache();
1368
1369 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001370
1371 if (argc) {
1372 argc = parse_options(argc, argv, live_options,
1373 live_usage, 0);
1374 if (argc)
1375 usage_with_options(live_usage, live_options);
1376 }
1377
David Ahern70f7b4a2013-08-07 21:56:38 -04001378 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1379
David Ahern1afe1d12013-08-05 21:41:34 -04001380 /*
1381 * target related setups
1382 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001383 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001384 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001385 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001386 ui__warning("%s", errbuf);
1387 }
1388
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001389 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001390 kvm->opts.target.system_wide = true;
1391
1392
1393 /*
1394 * generate the event list
1395 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301396 err = setup_kvm_events_tp(kvm);
1397 if (err < 0) {
1398 pr_err("Unable to setup the kvm tracepoints\n");
1399 return err;
1400 }
1401
David Ahern1afe1d12013-08-05 21:41:34 -04001402 kvm->evlist = kvm_live_event_list();
1403 if (kvm->evlist == NULL) {
1404 err = -1;
1405 goto out;
1406 }
1407
1408 symbol_conf.nr_events = kvm->evlist->nr_entries;
1409
1410 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1411 usage_with_options(live_usage, live_options);
1412
1413 /*
1414 * perf session
1415 */
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001416 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001417 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001418 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001419 goto out;
1420 }
1421 kvm->session->evlist = kvm->evlist;
1422 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001423 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001424 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001425 kvm->evlist->threads, false, kvm->opts.proc_map_timeout);
David Ahern1afe1d12013-08-05 21:41:34 -04001426 err = kvm_live_open_events(kvm);
1427 if (err)
1428 goto out;
1429
1430 err = kvm_events_live_report(kvm);
1431
1432out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001433 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001434 kvm->session = NULL;
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -03001435 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001436
1437 return err;
1438}
David Ahern87419c92013-10-29 10:43:16 -06001439#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001440
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001441static void print_kvm_stat_usage(void)
1442{
1443 printf("Usage: perf kvm stat <command>\n\n");
1444
1445 printf("# Available commands:\n");
1446 printf("\trecord: record kvm events\n");
1447 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001448 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001449
1450 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1451}
1452
Xiao Guangrong37860632012-11-15 14:17:01 +08001453static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001454{
Xiao Guangrong37860632012-11-15 14:17:01 +08001455 struct perf_kvm_stat kvm = {
1456 .file_name = file_name,
1457
1458 .trace_vcpu = -1,
1459 .report_event = "vmexit",
1460 .sort_key = "sample",
1461
Xiao Guangrong37860632012-11-15 14:17:01 +08001462 };
1463
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001464 if (argc == 1) {
1465 print_kvm_stat_usage();
1466 goto perf_stat;
1467 }
1468
1469 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001470 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001471
1472 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001473 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001474
David Ahern87419c92013-10-29 10:43:16 -06001475#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001476 if (!strncmp(argv[1], "live", 4))
1477 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001478#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001479
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001480perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001481 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001482}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001483#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001484
Xiao Guangrong37860632012-11-15 14:17:01 +08001485static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001486{
1487 int rec_argc, i = 0, j;
1488 const char **rec_argv;
1489
1490 rec_argc = argc + 2;
1491 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1492 rec_argv[i++] = strdup("record");
1493 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001494 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001495 for (j = 1; j < argc; j++, i++)
1496 rec_argv[i] = argv[j];
1497
1498 BUG_ON(i != rec_argc);
1499
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001500 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001501}
1502
Xiao Guangrong37860632012-11-15 14:17:01 +08001503static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001504{
1505 int rec_argc, i = 0, j;
1506 const char **rec_argv;
1507
1508 rec_argc = argc + 2;
1509 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1510 rec_argv[i++] = strdup("report");
1511 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001512 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001513 for (j = 1; j < argc; j++, i++)
1514 rec_argv[i] = argv[j];
1515
1516 BUG_ON(i != rec_argc);
1517
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001518 return cmd_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001519}
1520
Xiao Guangrong37860632012-11-15 14:17:01 +08001521static int
1522__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001523{
1524 int rec_argc, i = 0, j;
1525 const char **rec_argv;
1526
1527 rec_argc = argc + 2;
1528 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1529 rec_argv[i++] = strdup("buildid-list");
1530 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001531 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001532 for (j = 1; j < argc; j++, i++)
1533 rec_argv[i] = argv[j];
1534
1535 BUG_ON(i != rec_argc);
1536
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001537 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001538}
1539
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001540int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001541{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001542 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001543 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001544 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001545 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001546 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001547 "Output file name"),
1548 OPT_BOOLEAN(0, "guest", &perf_guest,
1549 "Collect guest os data"),
1550 OPT_BOOLEAN(0, "host", &perf_host,
1551 "Collect host os data"),
1552 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1553 "guest mount directory under which every guest os"
1554 " instance has a subdir"),
1555 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1556 "file", "file saving guest os vmlinux"),
1557 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1558 "file", "file saving guest os /proc/kallsyms"),
1559 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1560 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001561 OPT_INCR('v', "verbose", &verbose,
1562 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001563 OPT_END()
1564 };
1565
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001566 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1567 "buildid-list", "stat", NULL };
1568 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001569
Joerg Roedel1aed2672012-01-04 17:54:20 +01001570 perf_host = 0;
1571 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001572
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001573 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1574 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001575 if (!argc)
1576 usage_with_options(kvm_usage, kvm_options);
1577
1578 if (!perf_host)
1579 perf_guest = 1;
1580
Xiao Guangrong37860632012-11-15 14:17:01 +08001581 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001582 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001583
Xiao Guangrong37860632012-11-15 14:17:01 +08001584 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001585 pr_err("Failed to allocate memory for filename\n");
1586 return -ENOMEM;
1587 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001588 }
1589
1590 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001591 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001592 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001593 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001594 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001595 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001596 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001597 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001598 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001599 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001600#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001601 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001602 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001603#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001604 else
1605 usage_with_options(kvm_usage, kvm_options);
1606
1607 return 0;
1608}