blob: 2b1732cfc0beb0b8f4d3a99865f16a7d8a3212fe [file] [log] [blame]
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001#include "builtin.h"
2#include "perf.h"
3
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08004#include "util/evsel.h"
David Ahern1afe1d12013-08-05 21:41:34 -04005#include "util/evlist.h"
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -03006#include "util/term.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08007#include "util/util.h"
8#include "util/cache.h"
9#include "util/symbol.h"
10#include "util/thread.h"
11#include "util/header.h"
12#include "util/session.h"
David Ahern2e73f002013-08-05 21:41:37 -040013#include "util/intlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060014#include <subcmd/parse-options.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080016#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080017#include "util/tool.h"
18#include "util/stat.h"
David Ahern1afe1d12013-08-05 21:41:34 -040019#include "util/top.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030021#include "util/ordered-events.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080022
23#include <sys/prctl.h>
David Ahern87419c92013-10-29 10:43:16 -060024#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -040025#include <sys/timerfd.h>
David Ahern87419c92013-10-29 10:43:16 -060026#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080027
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030028#include <linux/kernel.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030029#include <linux/time64.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030030#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030031#include <inttypes.h>
David Ahern1afe1d12013-08-05 21:41:34 -040032#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080033#include <semaphore.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030034#include <signal.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080035#include <pthread.h>
36#include <math.h>
37
Alexander Yaryginda50ad62014-07-03 17:59:49 +040038#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040039#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080040
Alexander Yarygin9daa8122014-07-03 18:29:05 +040041void exit_event_get_key(struct perf_evsel *evsel,
42 struct perf_sample *sample,
43 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080044{
45 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053046 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080047}
48
Alexander Yarygin9daa8122014-07-03 18:29:05 +040049bool kvm_exit_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080050{
Hemant Kumar162607e2016-01-28 12:03:04 +053051 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080052}
53
Alexander Yarygin9daa8122014-07-03 18:29:05 +040054bool exit_event_begin(struct perf_evsel *evsel,
55 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080056{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030057 if (kvm_exit_event(evsel)) {
58 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080059 return true;
60 }
61
62 return false;
63}
64
Alexander Yarygin9daa8122014-07-03 18:29:05 +040065bool kvm_entry_event(struct perf_evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080066{
Hemant Kumar162607e2016-01-28 12:03:04 +053067 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080068}
69
Alexander Yarygin9daa8122014-07-03 18:29:05 +040070bool exit_event_end(struct perf_evsel *evsel,
71 struct perf_sample *sample __maybe_unused,
72 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080073{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030074 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080075}
76
Alexander Yarygindf74c132014-07-03 17:59:50 +040077static const char *get_exit_reason(struct perf_kvm_stat *kvm,
78 struct exit_reasons_table *tbl,
79 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080080{
Alexander Yarygindf74c132014-07-03 17:59:50 +040081 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -060082 if (tbl->exit_code == exit_code)
83 return tbl->reason;
84 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080085 }
86
87 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -060088 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080089 return "UNKNOWN";
90}
91
Alexander Yarygin9daa8122014-07-03 18:29:05 +040092void exit_event_decode_key(struct perf_kvm_stat *kvm,
93 struct event_key *key,
94 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080095{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +040096 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +040097 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080098
Hemant Kumar162607e2016-01-28 12:03:04 +053099 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800100}
101
Xiao Guangrong37860632012-11-15 14:17:01 +0800102static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800103{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400104 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800105
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400106 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
107 if (!strcmp(events_ops->name, kvm->report_event)) {
108 kvm->events_ops = events_ops->ops;
109 return true;
110 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800111 }
112
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400113 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800114}
115
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800116struct vcpu_event_record {
117 int vcpu_id;
118 u64 start_time;
119 struct kvm_event *last_event;
120};
121
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800122
Xiao Guangrong37860632012-11-15 14:17:01 +0800123static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800124{
David Ahernb880dee2012-10-08 11:17:30 -0600125 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800126
David Ahernb880dee2012-10-08 11:17:30 -0600127 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600128 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800129}
130
David Ahern87419c92013-10-29 10:43:16 -0600131#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400132static void clear_events_cache_stats(struct list_head *kvm_events_cache)
133{
134 struct list_head *head;
135 struct kvm_event *event;
136 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400137 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400138
139 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
140 head = &kvm_events_cache[i];
141 list_for_each_entry(event, head, hash_entry) {
142 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400143 event->total.time = 0;
144 init_stats(&event->total.stats);
145
146 for (j = 0; j < event->max_vcpu; ++j) {
147 event->vcpu[j].time = 0;
148 init_stats(&event->vcpu[j].stats);
149 }
David Ahern1afe1d12013-08-05 21:41:34 -0400150 }
151 }
152}
David Ahern87419c92013-10-29 10:43:16 -0600153#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400154
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800155static int kvm_events_hash_fn(u64 key)
156{
157 return key & (EVENTS_CACHE_SIZE - 1);
158}
159
160static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
161{
162 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600163 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800164
165 if (vcpu_id < event->max_vcpu)
166 return true;
167
168 while (event->max_vcpu <= vcpu_id)
169 event->max_vcpu += DEFAULT_VCPU_NUM;
170
David Ahern6ca5f302013-05-25 18:24:46 -0600171 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800172 event->vcpu = realloc(event->vcpu,
173 event->max_vcpu * sizeof(*event->vcpu));
174 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600175 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800176 pr_err("Not enough memory\n");
177 return false;
178 }
179
180 memset(event->vcpu + old_max_vcpu, 0,
181 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
182 return true;
183}
184
185static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
186{
187 struct kvm_event *event;
188
189 event = zalloc(sizeof(*event));
190 if (!event) {
191 pr_err("Not enough memory\n");
192 return NULL;
193 }
194
195 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200196 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800197 return event;
198}
199
Xiao Guangrong37860632012-11-15 14:17:01 +0800200static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600201 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800202{
203 struct kvm_event *event;
204 struct list_head *head;
205
206 BUG_ON(key->key == INVALID_KEY);
207
David Ahernde332ac2012-10-02 22:09:53 -0600208 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600209 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800210 if (event->key.key == key->key && event->key.info == key->info)
211 return event;
David Ahern355afe82012-10-08 11:17:32 -0600212 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800213
214 event = kvm_alloc_init_event(key);
215 if (!event)
216 return NULL;
217
218 list_add(&event->hash_entry, head);
219 return event;
220}
221
Xiao Guangrong37860632012-11-15 14:17:01 +0800222static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600223 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800224 struct event_key *key, u64 timestamp)
225{
226 struct kvm_event *event = NULL;
227
228 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600229 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800230
231 vcpu_record->last_event = event;
232 vcpu_record->start_time = timestamp;
233 return true;
234}
235
236static void
237kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
238{
239 kvm_stats->time += time_diff;
240 update_stats(&kvm_stats->stats, time_diff);
241}
242
243static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
244{
245 struct kvm_event_stats *kvm_stats = &event->total;
246
247 if (vcpu_id != -1)
248 kvm_stats = &event->vcpu[vcpu_id];
249
250 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
251 avg_stats(&kvm_stats->stats));
252}
253
254static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
255 u64 time_diff)
256{
David Ahern2aa8eab2012-10-08 11:17:35 -0600257 if (vcpu_id == -1) {
258 kvm_update_event_stats(&event->total, time_diff);
259 return true;
260 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800261
262 if (!kvm_event_expand(event, vcpu_id))
263 return false;
264
265 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
266 return true;
267}
268
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400269static bool is_child_event(struct perf_kvm_stat *kvm,
270 struct perf_evsel *evsel,
271 struct perf_sample *sample,
272 struct event_key *key)
273{
274 struct child_event_ops *child_ops;
275
276 child_ops = kvm->events_ops->child_ops;
277
278 if (!child_ops)
279 return false;
280
281 for (; child_ops->name; child_ops++) {
282 if (!strcmp(evsel->name, child_ops->name)) {
283 child_ops->get_key(evsel, sample, key);
284 return true;
285 }
286 }
287
288 return false;
289}
290
291static bool handle_child_event(struct perf_kvm_stat *kvm,
292 struct vcpu_event_record *vcpu_record,
293 struct event_key *key,
294 struct perf_sample *sample __maybe_unused)
295{
296 struct kvm_event *event = NULL;
297
298 if (key->key != INVALID_KEY)
299 event = find_create_kvm_event(kvm, key);
300
301 vcpu_record->last_event = event;
302
303 return true;
304}
305
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400306static bool skip_event(const char *event)
307{
308 const char * const *skip_events;
309
310 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
311 if (!strcmp(event, *skip_events))
312 return true;
313
314 return false;
315}
316
Xiao Guangrong37860632012-11-15 14:17:01 +0800317static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600318 struct vcpu_event_record *vcpu_record,
319 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400320 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800321{
322 struct kvm_event *event;
323 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600324 int vcpu;
325
326 if (kvm->trace_vcpu == -1)
327 vcpu = -1;
328 else
329 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800330
331 event = vcpu_record->last_event;
332 time_begin = vcpu_record->start_time;
333
334 /* The begin event is not caught. */
335 if (!time_begin)
336 return true;
337
338 /*
339 * In some case, the 'begin event' only records the start timestamp,
340 * the actual event is recognized in the 'end event' (e.g. mmio-event).
341 */
342
343 /* Both begin and end events did not get the key. */
344 if (!event && key->key == INVALID_KEY)
345 return true;
346
347 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600348 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800349
350 if (!event)
351 return false;
352
353 vcpu_record->last_event = NULL;
354 vcpu_record->start_time = 0;
355
David Ahern1afe1d12013-08-05 21:41:34 -0400356 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400357 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400358 pr_debug("End time before begin time; skipping event.\n");
359 return true;
360 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800361
David Ahern70f7b4a2013-08-07 21:56:38 -0400362 time_diff = sample->time - time_begin;
363
364 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530365 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400366
367 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801f2014-07-03 18:29:06 +0400368 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400369 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
370 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300371 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400372 }
373 }
374
David Ahern2aa8eab2012-10-08 11:17:35 -0600375 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800376}
377
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300378static
379struct vcpu_event_record *per_vcpu_record(struct thread *thread,
380 struct perf_evsel *evsel,
381 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800382{
383 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900384 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800385 struct vcpu_event_record *vcpu_record;
386
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300387 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800388 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300389 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800390 return NULL;
391 }
392
Hemant Kumar162607e2016-01-28 12:03:04 +0530393 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
394 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900395 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800396 }
397
Namhyung Kim69e865c2014-10-06 09:46:02 +0900398 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800399}
400
Xiao Guangrong37860632012-11-15 14:17:01 +0800401static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600402 struct thread *thread,
403 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300404 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800405{
406 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400407 struct event_key key = { .key = INVALID_KEY,
408 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800409
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300410 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800411 if (!vcpu_record)
412 return true;
413
David Ahern2aa8eab2012-10-08 11:17:35 -0600414 /* only process events for vcpus user cares about */
415 if ((kvm->trace_vcpu != -1) &&
416 (kvm->trace_vcpu != vcpu_record->vcpu_id))
417 return true;
418
David Ahernde332ac2012-10-02 22:09:53 -0600419 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
420 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800421
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400422 if (is_child_event(kvm, evsel, sample, &key))
423 return handle_child_event(kvm, vcpu_record, &key, sample);
424
David Ahernde332ac2012-10-02 22:09:53 -0600425 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400426 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800427
428 return true;
429}
430
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800431#define GET_EVENT_KEY(func, field) \
432static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
433{ \
434 if (vcpu == -1) \
435 return event->total.field; \
436 \
437 if (vcpu >= event->max_vcpu) \
438 return 0; \
439 \
440 return event->vcpu[vcpu].field; \
441}
442
443#define COMPARE_EVENT_KEY(func, field) \
444GET_EVENT_KEY(func, field) \
445static int compare_kvm_event_ ## func(struct kvm_event *one, \
446 struct kvm_event *two, int vcpu)\
447{ \
448 return get_event_ ##func(one, vcpu) > \
449 get_event_ ##func(two, vcpu); \
450}
451
452GET_EVENT_KEY(time, time);
453COMPARE_EVENT_KEY(count, stats.n);
454COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400455GET_EVENT_KEY(max, stats.max);
456GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800457
458#define DEF_SORT_NAME_KEY(name, compare_key) \
459 { #name, compare_kvm_event_ ## compare_key }
460
461static struct kvm_event_key keys[] = {
462 DEF_SORT_NAME_KEY(sample, count),
463 DEF_SORT_NAME_KEY(time, mean),
464 { NULL, NULL }
465};
466
Xiao Guangrong37860632012-11-15 14:17:01 +0800467static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800468{
469 int i;
470
471 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600472 if (!strcmp(keys[i].name, kvm->sort_key)) {
473 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800474 return true;
475 }
476 }
477
David Ahernde332ac2012-10-02 22:09:53 -0600478 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800479 return false;
480}
481
David Ahernde332ac2012-10-02 22:09:53 -0600482static void insert_to_result(struct rb_root *result, struct kvm_event *event,
483 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800484{
David Ahernde332ac2012-10-02 22:09:53 -0600485 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800486 struct rb_node *parent = NULL;
487 struct kvm_event *p;
488
489 while (*rb) {
490 p = container_of(*rb, struct kvm_event, rb);
491 parent = *rb;
492
493 if (bigger(event, p, vcpu))
494 rb = &(*rb)->rb_left;
495 else
496 rb = &(*rb)->rb_right;
497 }
498
499 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600500 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800501}
502
Xiao Guangrong37860632012-11-15 14:17:01 +0800503static void
504update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800505{
David Ahernde332ac2012-10-02 22:09:53 -0600506 int vcpu = kvm->trace_vcpu;
507
508 kvm->total_count += get_event_count(event, vcpu);
509 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800510}
511
512static bool event_is_valid(struct kvm_event *event, int vcpu)
513{
514 return !!get_event_count(event, vcpu);
515}
516
Xiao Guangrong37860632012-11-15 14:17:01 +0800517static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800518{
519 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600520 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800521 struct kvm_event *event;
522
David Ahern355afe82012-10-08 11:17:32 -0600523 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
524 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800525 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600526 update_total_count(kvm, event);
527 insert_to_result(&kvm->result, event,
528 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800529 }
David Ahern355afe82012-10-08 11:17:32 -0600530 }
531 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800532}
533
534/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600535static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800536{
David Ahernde332ac2012-10-02 22:09:53 -0600537 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800538
539 if (!node)
540 return NULL;
541
David Ahernde332ac2012-10-02 22:09:53 -0600542 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800543 return container_of(node, struct kvm_event, rb);
544}
545
David Ahern1afe1d12013-08-05 21:41:34 -0400546static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800547{
David Ahern1afe1d12013-08-05 21:41:34 -0400548 int vcpu = kvm->trace_vcpu;
549
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800550 pr_info("Analyze events for ");
551
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400552 if (kvm->opts.target.system_wide)
553 pr_info("all VMs, ");
554 else if (kvm->opts.target.pid)
555 pr_info("pid(s) %s, ", kvm->opts.target.pid);
556 else
557 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400558
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800559 if (vcpu == -1)
560 pr_info("all VCPUs:\n\n");
561 else
562 pr_info("VCPU %d:\n\n", vcpu);
563}
564
David Ahern1afe1d12013-08-05 21:41:34 -0400565static void show_timeofday(void)
566{
567 char date[64];
568 struct timeval tv;
569 struct tm ltime;
570
571 gettimeofday(&tv, NULL);
572 if (localtime_r(&tv.tv_sec, &ltime)) {
573 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
574 pr_info("%s.%06ld", date, tv.tv_usec);
575 } else
576 pr_info("00:00:00.000000");
577
578 return;
579}
580
Xiao Guangrong37860632012-11-15 14:17:01 +0800581static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800582{
Hemant Kumar162607e2016-01-28 12:03:04 +0530583 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800584 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600585 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800586
David Ahern1afe1d12013-08-05 21:41:34 -0400587 if (kvm->live) {
588 puts(CONSOLE_CLEAR);
589 show_timeofday();
590 }
591
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800592 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400593 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530594 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800595 pr_info("%10s ", "Samples");
596 pr_info("%9s ", "Samples%");
597
598 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200599 pr_info("%11s ", "Min Time");
600 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800601 pr_info("%16s ", "Avg time");
602 pr_info("\n\n");
603
David Ahernde332ac2012-10-02 22:09:53 -0600604 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400605 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800606
607 ecount = get_event_count(event, vcpu);
608 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400609 max = get_event_max(event, vcpu);
610 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800611
David Ahernde332ac2012-10-02 22:09:53 -0600612 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530613 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800614 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600615 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
616 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300617 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
618 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
619 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800620 kvm_event_rel_stddev(vcpu, event));
621 pr_info("\n");
622 }
623
David Aherne4f76372012-10-08 11:17:34 -0600624 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300625 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400626
627 if (kvm->lost_events)
628 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
629}
630
David Ahern87419c92013-10-29 10:43:16 -0600631#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400632static int process_lost_event(struct perf_tool *tool,
633 union perf_event *event __maybe_unused,
634 struct perf_sample *sample __maybe_unused,
635 struct machine *machine __maybe_unused)
636{
637 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
638
639 kvm->lost_events++;
640 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800641}
David Ahern87419c92013-10-29 10:43:16 -0600642#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800643
David Ahern2e73f002013-08-05 21:41:37 -0400644static bool skip_sample(struct perf_kvm_stat *kvm,
645 struct perf_sample *sample)
646{
647 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
648 return true;
649
650 return false;
651}
652
David Ahernde332ac2012-10-02 22:09:53 -0600653static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800654 union perf_event *event,
655 struct perf_sample *sample,
656 struct perf_evsel *evsel,
657 struct machine *machine)
658{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300659 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400660 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800661 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
662 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800663
David Ahern2e73f002013-08-05 21:41:37 -0400664 if (skip_sample(kvm, sample))
665 return 0;
666
Adrian Hunter314add62013-08-27 11:23:03 +0300667 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800668 if (thread == NULL) {
669 pr_debug("problem processing %d event, skipping it.\n",
670 event->header.type);
671 return -1;
672 }
673
David Ahernde332ac2012-10-02 22:09:53 -0600674 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300675 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800676
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300677 thread__put(thread);
678 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800679}
680
David Ahern1afe1d12013-08-05 21:41:34 -0400681static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800682{
David Ahern1afe1d12013-08-05 21:41:34 -0400683 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400684 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400685
686 if (kvm->live) {
687 err = get_cpuid(buf, sizeof(buf));
688 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400689 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400690 return err;
691 }
692 cpuid = buf;
693 } else
694 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800695
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400696 if (!cpuid) {
697 pr_err("Failed to look up CPU type\n");
698 return -EINVAL;
699 }
700
701 err = cpu_isa_init(kvm, cpuid);
702 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800703 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800704
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400705 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400706}
707
708static bool verify_vcpu(int vcpu)
709{
710 if (vcpu != -1 && vcpu < 0) {
711 pr_err("Invalid vcpu:%d.\n", vcpu);
712 return false;
713 }
714
715 return true;
716}
717
David Ahern87419c92013-10-29 10:43:16 -0600718#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400719/* keeping the max events to a modest level to keep
720 * the processing of samples per mmap smooth.
721 */
722#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
723
724static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
725 u64 *mmap_time)
726{
727 union perf_event *event;
728 struct perf_sample sample;
729 s64 n = 0;
730 int err;
731
732 *mmap_time = ULLONG_MAX;
733 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
734 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
735 if (err) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800736 perf_evlist__mmap_consume(kvm->evlist, idx);
David Ahern1afe1d12013-08-05 21:41:34 -0400737 pr_err("Failed to parse sample\n");
738 return -1;
739 }
740
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300741 err = perf_session__queue_event(kvm->session, event, &sample, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800742 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300743 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800744 * point to it, and it'll get possibly overwritten by the kernel.
745 */
746 perf_evlist__mmap_consume(kvm->evlist, idx);
747
David Ahern1afe1d12013-08-05 21:41:34 -0400748 if (err) {
749 pr_err("Failed to enqueue sample: %d\n", err);
750 return -1;
751 }
752
753 /* save time stamp of our first sample for this mmap */
754 if (n == 0)
755 *mmap_time = sample.time;
756
757 /* limit events per mmap handled all at once */
758 n++;
759 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
760 break;
761 }
762
763 return n;
764}
765
766static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
767{
768 int i, err, throttled = 0;
769 s64 n, ntotal = 0;
770 u64 flush_time = ULLONG_MAX, mmap_time;
771
772 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
773 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
774 if (n < 0)
775 return -1;
776
777 /* flush time is going to be the minimum of all the individual
778 * mmap times. Essentially, we flush all the samples queued up
779 * from the last pass under our minimal start time -- that leaves
780 * a very small race for samples to come in with a lower timestamp.
781 * The ioctl to return the perf_clock timestamp should close the
782 * race entirely.
783 */
784 if (mmap_time < flush_time)
785 flush_time = mmap_time;
786
787 ntotal += n;
788 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
789 throttled = 1;
790 }
791
792 /* flush queue after each round in which we processed events */
793 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300794 struct ordered_events *oe = &kvm->session->ordered_events;
795
796 oe->next_flush = flush_time;
797 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400798 if (err) {
799 if (kvm->lost_events)
800 pr_info("\nLost events: %" PRIu64 "\n\n",
801 kvm->lost_events);
802 return err;
803 }
804 }
805
806 return throttled;
807}
808
809static volatile int done;
810
811static void sig_handler(int sig __maybe_unused)
812{
813 done = 1;
814}
815
816static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
817{
818 struct itimerspec new_value;
819 int rc = -1;
820
821 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
822 if (kvm->timerfd < 0) {
823 pr_err("timerfd_create failed\n");
824 goto out;
825 }
826
827 new_value.it_value.tv_sec = kvm->display_time;
828 new_value.it_value.tv_nsec = 0;
829 new_value.it_interval.tv_sec = kvm->display_time;
830 new_value.it_interval.tv_nsec = 0;
831
832 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
833 pr_err("timerfd_settime failed: %d\n", errno);
834 close(kvm->timerfd);
835 goto out;
836 }
837
838 rc = 0;
839out:
840 return rc;
841}
842
843static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
844{
845 uint64_t c;
846 int rc;
847
848 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
849 if (rc < 0) {
850 if (errno == EAGAIN)
851 return 0;
852
853 pr_err("Failed to read timer fd: %d\n", errno);
854 return -1;
855 }
856
857 if (rc != sizeof(uint64_t)) {
858 pr_err("Error reading timer fd - invalid size returned\n");
859 return -1;
860 }
861
862 if (c != 1)
863 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
864
865 /* update display */
866 sort_result(kvm);
867 print_result(kvm);
868
869 /* reset counts */
870 clear_events_cache_stats(kvm->kvm_events_cache);
871 kvm->total_count = 0;
872 kvm->total_time = 0;
873 kvm->lost_events = 0;
874
875 return 0;
876}
877
878static int fd_set_nonblock(int fd)
879{
880 long arg = 0;
881
882 arg = fcntl(fd, F_GETFL);
883 if (arg < 0) {
884 pr_err("Failed to get current flags for fd %d\n", fd);
885 return -1;
886 }
887
888 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
889 pr_err("Failed to set non-block option on fd %d\n", fd);
890 return -1;
891 }
892
893 return 0;
894}
895
Jiri Olsad5b41302014-08-11 10:50:09 +0200896static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400897{
898 int c;
899
David Ahern1afe1d12013-08-05 21:41:34 -0400900 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400901 if (c == 'q')
902 return 1;
903
904 return 0;
905}
906
907static int kvm_events_live_report(struct perf_kvm_stat *kvm)
908{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200909 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200910 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400911
912 /* live flag must be set first */
913 kvm->live = true;
914
915 ret = cpu_isa_config(kvm);
916 if (ret < 0)
917 return ret;
918
919 if (!verify_vcpu(kvm->trace_vcpu) ||
920 !select_key(kvm) ||
921 !register_kvm_events_ops(kvm)) {
922 goto out;
923 }
924
Jiri Olsad5b41302014-08-11 10:50:09 +0200925 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400926 init_kvm_event_record(kvm);
927
David Ahern1afe1d12013-08-05 21:41:34 -0400928 signal(SIGINT, sig_handler);
929 signal(SIGTERM, sig_handler);
930
David Ahern1afe1d12013-08-05 21:41:34 -0400931 /* add timer fd */
932 if (perf_kvm__timerfd_create(kvm) < 0) {
933 err = -1;
934 goto out;
935 }
936
Jiri Olsafe636ad2014-10-01 18:00:27 +0200937 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300938 goto out;
939
Jiri Olsa0cae0132014-10-01 18:00:28 +0200940 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
941 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300942 goto out;
943
David Ahern1afe1d12013-08-05 21:41:34 -0400944 if (fd_set_nonblock(fileno(stdin)) != 0)
945 goto out;
946
947 /* everything is good - enable the events and process */
948 perf_evlist__enable(kvm->evlist);
949
950 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200951 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400952 int rc;
953
954 rc = perf_kvm__mmap_read(kvm);
955 if (rc < 0)
956 break;
957
958 err = perf_kvm__handle_timerfd(kvm);
959 if (err)
960 goto out;
961
Jiri Olsa1ca72262014-10-01 18:00:29 +0200962 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200963 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400964
965 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200966 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400967 }
968
969 perf_evlist__disable(kvm->evlist);
970
971 if (err == 0) {
972 sort_result(kvm);
973 print_result(kvm);
974 }
975
976out:
977 if (kvm->timerfd >= 0)
978 close(kvm->timerfd);
979
Jiri Olsad5b41302014-08-11 10:50:09 +0200980 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -0400981 return err;
982}
983
984static int kvm_live_open_events(struct perf_kvm_stat *kvm)
985{
986 int err, rc = -1;
987 struct perf_evsel *pos;
988 struct perf_evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +0000989 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -0400990
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300991 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -0400992
993 /*
994 * Note: exclude_{guest,host} do not apply here.
995 * This command processes KVM tracepoints from host only
996 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300997 evlist__for_each_entry(evlist, pos) {
David Ahern1afe1d12013-08-05 21:41:34 -0400998 struct perf_event_attr *attr = &pos->attr;
999
1000 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +03001001 perf_evsel__set_sample_bit(pos, TID);
1002 perf_evsel__set_sample_bit(pos, TIME);
1003 perf_evsel__set_sample_bit(pos, CPU);
1004 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001005 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001006 perf_evsel__reset_sample_bit(pos, PERIOD);
1007 perf_evsel__reset_sample_bit(pos, IP);
1008 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1009 perf_evsel__reset_sample_bit(pos, ADDR);
1010 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001011 attr->mmap = 0;
1012 attr->comm = 0;
1013 attr->task = 0;
1014
1015 attr->sample_period = 1;
1016
1017 attr->watermark = 0;
1018 attr->wakeup_events = 1000;
1019
1020 /* will enable all once we are ready */
1021 attr->disabled = 1;
1022 }
1023
1024 err = perf_evlist__open(evlist);
1025 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001026 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001027 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001028 goto out;
1029 }
1030
1031 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001032 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001033 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001034 perf_evlist__close(evlist);
1035 goto out;
1036 }
1037
1038 rc = 0;
1039
1040out:
1041 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001042}
David Ahern87419c92013-10-29 10:43:16 -06001043#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001044
Xiao Guangrong37860632012-11-15 14:17:01 +08001045static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001046{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001047 int ret;
1048
David Ahernde332ac2012-10-02 22:09:53 -06001049 struct perf_tool eops = {
1050 .sample = process_sample_event,
1051 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301052 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001053 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001054 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001055 struct perf_data_file file = {
Dongsheng Yang476b3a82013-12-09 18:47:03 -05001056 .path = kvm->file_name,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001057 .mode = PERF_DATA_MODE_READ,
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001058 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001059 };
David Ahernde332ac2012-10-02 22:09:53 -06001060
1061 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001062 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001063 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001064 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001065 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001066 }
1067
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001068 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001069
Taeung Song41b98362015-07-01 00:18:44 +09001070 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1071 ret = -EINVAL;
1072 goto out_delete;
1073 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001074
1075 /*
1076 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1077 * traced in the old kernel.
1078 */
David Ahern1afe1d12013-08-05 21:41:34 -04001079 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001080 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001081 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001082
Taeung Song41b98362015-07-01 00:18:44 +09001083 ret = perf_session__process_events(kvm->session);
1084
1085out_delete:
1086 perf_session__delete(kvm->session);
1087 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001088}
1089
David Ahern2e73f002013-08-05 21:41:37 -04001090static int parse_target_str(struct perf_kvm_stat *kvm)
1091{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001092 if (kvm->opts.target.pid) {
1093 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001094 if (kvm->pid_list == NULL) {
1095 pr_err("Error parsing process id string\n");
1096 return -EINVAL;
1097 }
1098 }
1099
1100 return 0;
1101}
1102
Xiao Guangrong37860632012-11-15 14:17:01 +08001103static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001104{
1105 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001106 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001107
David Ahern2e73f002013-08-05 21:41:37 -04001108 if (parse_target_str(kvm) != 0)
1109 goto exit;
1110
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001111 if (!verify_vcpu(vcpu))
1112 goto exit;
1113
David Ahernde332ac2012-10-02 22:09:53 -06001114 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001115 goto exit;
1116
David Ahernde332ac2012-10-02 22:09:53 -06001117 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001118 goto exit;
1119
David Ahernde332ac2012-10-02 22:09:53 -06001120 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001121 setup_pager();
1122
David Ahernde332ac2012-10-02 22:09:53 -06001123 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001124 if (ret)
1125 goto exit;
1126
David Ahernde332ac2012-10-02 22:09:53 -06001127 sort_result(kvm);
1128 print_result(kvm);
1129
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001130exit:
1131 return ret;
1132}
1133
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001134#define STRDUP_FAIL_EXIT(s) \
1135 ({ char *_p; \
1136 _p = strdup(s); \
1137 if (!_p) \
1138 return -ENOMEM; \
1139 _p; \
1140 })
1141
Hemant Kumar066d3592016-01-28 12:03:06 +05301142int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1143{
1144 return 0;
1145}
1146
Xiao Guangrong37860632012-11-15 14:17:01 +08001147static int
1148kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001149{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001150 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001151 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001152 const char * const record_args[] = {
1153 "record",
1154 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001155 "-m", "1024",
1156 "-c", "1",
1157 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001158 const char * const kvm_stat_record_usage[] = {
1159 "perf kvm stat record [<options>]",
1160 NULL
1161 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001162 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301163 int ret;
1164
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001165 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301166 ret = setup_kvm_events_tp(kvm);
1167 if (ret < 0) {
1168 pr_err("Unable to setup the kvm tracepoints\n");
1169 return ret;
1170 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001171
1172 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1173 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001174
David Ahern8fdd84c2013-08-02 14:05:42 -06001175 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001176 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001177 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1178
1179 if (rec_argv == NULL)
1180 return -ENOMEM;
1181
1182 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1183 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1184
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001185 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001186 rec_argv[i++] = "-e";
1187 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1188 }
1189
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001190 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001191 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001192
1193 for (j = 1; j < (unsigned int)argc; j++, i++)
1194 rec_argv[i] = argv[j];
1195
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001196 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1197 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1198 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1199
1200 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1201 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1202 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1203 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1204 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1205 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1206 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1207 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1208 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1209 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1210 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1211 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1212 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1213 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1214 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1215
1216 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001217 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001218}
1219
Xiao Guangrong37860632012-11-15 14:17:01 +08001220static int
1221kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001222{
David Ahernde332ac2012-10-02 22:09:53 -06001223 const struct option kvm_events_report_options[] = {
1224 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001225 "event for reporting: vmexit, "
1226 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001227 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1228 "vcpu id to report"),
1229 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1230 "key for sorting: sample(sort by samples number)"
1231 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001232 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001233 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001234 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001235 OPT_END()
1236 };
1237
1238 const char * const kvm_events_report_usage[] = {
1239 "perf kvm stat report [<options>]",
1240 NULL
1241 };
1242
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001243 if (argc) {
1244 argc = parse_options(argc, argv,
1245 kvm_events_report_options,
1246 kvm_events_report_usage, 0);
1247 if (argc)
1248 usage_with_options(kvm_events_report_usage,
1249 kvm_events_report_options);
1250 }
1251
Alexander Yaryginf1819572014-09-01 17:44:54 +04001252 if (!kvm->opts.target.pid)
1253 kvm->opts.target.system_wide = true;
1254
David Ahernde332ac2012-10-02 22:09:53 -06001255 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001256}
1257
David Ahern87419c92013-10-29 10:43:16 -06001258#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001259static struct perf_evlist *kvm_live_event_list(void)
1260{
1261 struct perf_evlist *evlist;
1262 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001263 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001264 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001265
1266 evlist = perf_evlist__new();
1267 if (evlist == NULL)
1268 return NULL;
1269
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001270 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001271
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001272 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001273 if (tp == NULL)
1274 goto out;
1275
1276 /* split tracepoint into subsystem and name */
1277 sys = tp;
1278 name = strchr(tp, ':');
1279 if (name == NULL) {
1280 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001281 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001282 free(tp);
1283 goto out;
1284 }
1285 *name = '\0';
1286 name++;
1287
1288 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001289 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001290 free(tp);
1291 goto out;
1292 }
1293
1294 free(tp);
1295 }
1296
1297 err = 0;
1298
1299out:
1300 if (err) {
1301 perf_evlist__delete(evlist);
1302 evlist = NULL;
1303 }
1304
1305 return evlist;
1306}
1307
1308static int kvm_events_live(struct perf_kvm_stat *kvm,
1309 int argc, const char **argv)
1310{
1311 char errbuf[BUFSIZ];
1312 int err;
1313
1314 const struct option live_options[] = {
1315 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1316 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001317 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1318 "number of mmap data pages",
1319 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001320 OPT_INCR('v', "verbose", &verbose,
1321 "be more verbose (show counter open errors, etc)"),
1322 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1323 "system-wide collection from all CPUs"),
1324 OPT_UINTEGER('d', "display", &kvm->display_time,
1325 "time in seconds between display updates"),
1326 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001327 "event for reporting: "
1328 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001329 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1330 "vcpu id to report"),
1331 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1332 "key for sorting: sample(sort by samples number)"
1333 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001334 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001335 "show events other than"
1336 " HLT (x86 only) or Wait state (s390 only)"
1337 " that take longer than duration usecs"),
Kan Liang9d9cad72015-06-17 09:51:11 -04001338 OPT_UINTEGER(0, "proc-map-timeout", &kvm->opts.proc_map_timeout,
1339 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001340 OPT_END()
1341 };
1342 const char * const live_usage[] = {
1343 "perf kvm stat live [<options>]",
1344 NULL
1345 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001346 struct perf_data_file file = {
1347 .mode = PERF_DATA_MODE_WRITE,
1348 };
David Ahern1afe1d12013-08-05 21:41:34 -04001349
1350
1351 /* event handling */
1352 kvm->tool.sample = process_sample_event;
1353 kvm->tool.comm = perf_event__process_comm;
1354 kvm->tool.exit = perf_event__process_exit;
1355 kvm->tool.fork = perf_event__process_fork;
1356 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301357 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001358 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001359 perf_tool__fill_defaults(&kvm->tool);
1360
1361 /* set defaults */
1362 kvm->display_time = 1;
1363 kvm->opts.user_interval = 1;
1364 kvm->opts.mmap_pages = 512;
1365 kvm->opts.target.uses_mmap = false;
1366 kvm->opts.target.uid_str = NULL;
1367 kvm->opts.target.uid = UINT_MAX;
Kan Liang9d9cad72015-06-17 09:51:11 -04001368 kvm->opts.proc_map_timeout = 500;
David Ahern1afe1d12013-08-05 21:41:34 -04001369
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001370 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001371 disable_buildid_cache();
1372
1373 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001374
1375 if (argc) {
1376 argc = parse_options(argc, argv, live_options,
1377 live_usage, 0);
1378 if (argc)
1379 usage_with_options(live_usage, live_options);
1380 }
1381
David Ahern70f7b4a2013-08-07 21:56:38 -04001382 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1383
David Ahern1afe1d12013-08-05 21:41:34 -04001384 /*
1385 * target related setups
1386 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001387 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001388 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001389 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001390 ui__warning("%s", errbuf);
1391 }
1392
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001393 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001394 kvm->opts.target.system_wide = true;
1395
1396
1397 /*
1398 * generate the event list
1399 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301400 err = setup_kvm_events_tp(kvm);
1401 if (err < 0) {
1402 pr_err("Unable to setup the kvm tracepoints\n");
1403 return err;
1404 }
1405
David Ahern1afe1d12013-08-05 21:41:34 -04001406 kvm->evlist = kvm_live_event_list();
1407 if (kvm->evlist == NULL) {
1408 err = -1;
1409 goto out;
1410 }
1411
1412 symbol_conf.nr_events = kvm->evlist->nr_entries;
1413
1414 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1415 usage_with_options(live_usage, live_options);
1416
1417 /*
1418 * perf session
1419 */
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001420 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001421 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001422 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001423 goto out;
1424 }
1425 kvm->session->evlist = kvm->evlist;
1426 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001427 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001428 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001429 kvm->evlist->threads, false, kvm->opts.proc_map_timeout);
David Ahern1afe1d12013-08-05 21:41:34 -04001430 err = kvm_live_open_events(kvm);
1431 if (err)
1432 goto out;
1433
1434 err = kvm_events_live_report(kvm);
1435
1436out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001437 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001438 kvm->session = NULL;
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -03001439 perf_evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001440
1441 return err;
1442}
David Ahern87419c92013-10-29 10:43:16 -06001443#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001444
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001445static void print_kvm_stat_usage(void)
1446{
1447 printf("Usage: perf kvm stat <command>\n\n");
1448
1449 printf("# Available commands:\n");
1450 printf("\trecord: record kvm events\n");
1451 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001452 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001453
1454 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1455}
1456
Xiao Guangrong37860632012-11-15 14:17:01 +08001457static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001458{
Xiao Guangrong37860632012-11-15 14:17:01 +08001459 struct perf_kvm_stat kvm = {
1460 .file_name = file_name,
1461
1462 .trace_vcpu = -1,
1463 .report_event = "vmexit",
1464 .sort_key = "sample",
1465
Xiao Guangrong37860632012-11-15 14:17:01 +08001466 };
1467
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001468 if (argc == 1) {
1469 print_kvm_stat_usage();
1470 goto perf_stat;
1471 }
1472
1473 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001474 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001475
1476 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001477 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001478
David Ahern87419c92013-10-29 10:43:16 -06001479#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001480 if (!strncmp(argv[1], "live", 4))
1481 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001482#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001483
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001484perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001485 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001486}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001487#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001488
Xiao Guangrong37860632012-11-15 14:17:01 +08001489static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001490{
1491 int rec_argc, i = 0, j;
1492 const char **rec_argv;
1493
1494 rec_argc = argc + 2;
1495 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1496 rec_argv[i++] = strdup("record");
1497 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001498 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001499 for (j = 1; j < argc; j++, i++)
1500 rec_argv[i] = argv[j];
1501
1502 BUG_ON(i != rec_argc);
1503
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001504 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001505}
1506
Xiao Guangrong37860632012-11-15 14:17:01 +08001507static int __cmd_report(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("report");
1515 rec_argv[i++] = strdup("-i");
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_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001523}
1524
Xiao Guangrong37860632012-11-15 14:17:01 +08001525static int
1526__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001527{
1528 int rec_argc, i = 0, j;
1529 const char **rec_argv;
1530
1531 rec_argc = argc + 2;
1532 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1533 rec_argv[i++] = strdup("buildid-list");
1534 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001535 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001536 for (j = 1; j < argc; j++, i++)
1537 rec_argv[i] = argv[j];
1538
1539 BUG_ON(i != rec_argc);
1540
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001541 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001542}
1543
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001544int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001545{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001546 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001547 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001548 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001549 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001550 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001551 "Output file name"),
1552 OPT_BOOLEAN(0, "guest", &perf_guest,
1553 "Collect guest os data"),
1554 OPT_BOOLEAN(0, "host", &perf_host,
1555 "Collect host os data"),
1556 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1557 "guest mount directory under which every guest os"
1558 " instance has a subdir"),
1559 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1560 "file", "file saving guest os vmlinux"),
1561 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1562 "file", "file saving guest os /proc/kallsyms"),
1563 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1564 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001565 OPT_INCR('v', "verbose", &verbose,
1566 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001567 OPT_END()
1568 };
1569
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001570 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1571 "buildid-list", "stat", NULL };
1572 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001573
Joerg Roedel1aed2672012-01-04 17:54:20 +01001574 perf_host = 0;
1575 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001576
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001577 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1578 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001579 if (!argc)
1580 usage_with_options(kvm_usage, kvm_options);
1581
1582 if (!perf_host)
1583 perf_guest = 1;
1584
Xiao Guangrong37860632012-11-15 14:17:01 +08001585 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001586 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001587
Xiao Guangrong37860632012-11-15 14:17:01 +08001588 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001589 pr_err("Failed to allocate memory for filename\n");
1590 return -ENOMEM;
1591 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001592 }
1593
1594 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001595 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001596 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001597 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001598 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001599 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001600 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001601 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001602 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001603 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001604#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001605 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001606 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001607#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001608 else
1609 usage_with_options(kvm_usage, kvm_options);
1610
1611 return 0;
1612}