blob: ac6d6e061dc57e34dc278b1e19bde9f573e9486f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002#include "builtin.h"
3#include "perf.h"
4
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03005#include "util/build-id.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08006#include "util/evsel.h"
David Ahern1afe1d12013-08-05 21:41:34 -04007#include "util/evlist.h"
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -03008#include "util/term.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08009#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"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030014#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060015#include <subcmd/parse-options.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080016#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080017#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080018#include "util/tool.h"
19#include "util/stat.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030020#include "util/synthetic-events.h"
David Ahern1afe1d12013-08-05 21:41:34 -040021#include "util/top.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020022#include "util/data.h"
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030023#include "util/ordered-events.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030024#include "ui/ui.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080025
26#include <sys/prctl.h>
David Ahern87419c92013-10-29 10:43:16 -060027#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -040028#include <sys/timerfd.h>
David Ahern87419c92013-10-29 10:43:16 -060029#endif
Arnaldo Carvalho de Meloc5e40272017-04-19 16:12:39 -030030#include <sys/time.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030031#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080034
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030035#include <linux/kernel.h>
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030036#include <linux/string.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030037#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030038#include <linux/zalloc.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030039#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030040#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030041#include <poll.h>
David Ahern1afe1d12013-08-05 21:41:34 -040042#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080043#include <semaphore.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030044#include <signal.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080045#include <math.h>
46
Arnaldo Carvalho de Melobb8c16d2017-04-19 16:15:13 -030047static const char *get_filename_for_perf_kvm(void)
48{
49 const char *filename;
50
51 if (perf_host && !perf_guest)
52 filename = strdup("perf.data.host");
53 else if (!perf_host && perf_guest)
54 filename = strdup("perf.data.guest");
55 else
56 filename = strdup("perf.data.kvm");
57
58 return filename;
59}
60
Alexander Yaryginda50ad62014-07-03 17:59:49 +040061#ifdef HAVE_KVM_STAT_SUPPORT
Alexander Yarygin9daa8122014-07-03 18:29:05 +040062#include "util/kvm-stat.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080063
Jiri Olsa32dcd022019-07-21 13:23:51 +020064void exit_event_get_key(struct evsel *evsel,
Alexander Yarygin9daa8122014-07-03 18:29:05 +040065 struct perf_sample *sample,
66 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080067{
68 key->info = 0;
Hemant Kumar162607e2016-01-28 12:03:04 +053069 key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080070}
71
Jiri Olsa32dcd022019-07-21 13:23:51 +020072bool kvm_exit_event(struct evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080073{
Hemant Kumar162607e2016-01-28 12:03:04 +053074 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080075}
76
Jiri Olsa32dcd022019-07-21 13:23:51 +020077bool exit_event_begin(struct evsel *evsel,
Alexander Yarygin9daa8122014-07-03 18:29:05 +040078 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080079{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030080 if (kvm_exit_event(evsel)) {
81 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080082 return true;
83 }
84
85 return false;
86}
87
Jiri Olsa32dcd022019-07-21 13:23:51 +020088bool kvm_entry_event(struct evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080089{
Hemant Kumar162607e2016-01-28 12:03:04 +053090 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080091}
92
Jiri Olsa32dcd022019-07-21 13:23:51 +020093bool exit_event_end(struct evsel *evsel,
Alexander Yarygin9daa8122014-07-03 18:29:05 +040094 struct perf_sample *sample __maybe_unused,
95 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080096{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030097 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080098}
99
Alexander Yarygindf74c132014-07-03 17:59:50 +0400100static const char *get_exit_reason(struct perf_kvm_stat *kvm,
101 struct exit_reasons_table *tbl,
102 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800103{
Alexander Yarygindf74c132014-07-03 17:59:50 +0400104 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -0600105 if (tbl->exit_code == exit_code)
106 return tbl->reason;
107 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800108 }
109
110 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600111 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800112 return "UNKNOWN";
113}
114
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400115void exit_event_decode_key(struct perf_kvm_stat *kvm,
116 struct event_key *key,
117 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800118{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400119 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +0400120 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800121
Hemant Kumar162607e2016-01-28 12:03:04 +0530122 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800123}
124
Xiao Guangrong37860632012-11-15 14:17:01 +0800125static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800126{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400127 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800128
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400129 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
130 if (!strcmp(events_ops->name, kvm->report_event)) {
131 kvm->events_ops = events_ops->ops;
132 return true;
133 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800134 }
135
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400136 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800137}
138
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800139struct vcpu_event_record {
140 int vcpu_id;
141 u64 start_time;
142 struct kvm_event *last_event;
143};
144
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800145
Xiao Guangrong37860632012-11-15 14:17:01 +0800146static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800147{
David Ahernb880dee2012-10-08 11:17:30 -0600148 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800149
David Ahernb880dee2012-10-08 11:17:30 -0600150 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600151 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800152}
153
David Ahern87419c92013-10-29 10:43:16 -0600154#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400155static void clear_events_cache_stats(struct list_head *kvm_events_cache)
156{
157 struct list_head *head;
158 struct kvm_event *event;
159 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400160 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400161
162 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
163 head = &kvm_events_cache[i];
164 list_for_each_entry(event, head, hash_entry) {
165 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400166 event->total.time = 0;
167 init_stats(&event->total.stats);
168
169 for (j = 0; j < event->max_vcpu; ++j) {
170 event->vcpu[j].time = 0;
171 init_stats(&event->vcpu[j].stats);
172 }
David Ahern1afe1d12013-08-05 21:41:34 -0400173 }
174 }
175}
David Ahern87419c92013-10-29 10:43:16 -0600176#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400177
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800178static int kvm_events_hash_fn(u64 key)
179{
180 return key & (EVENTS_CACHE_SIZE - 1);
181}
182
183static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
184{
185 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600186 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800187
188 if (vcpu_id < event->max_vcpu)
189 return true;
190
191 while (event->max_vcpu <= vcpu_id)
192 event->max_vcpu += DEFAULT_VCPU_NUM;
193
David Ahern6ca5f302013-05-25 18:24:46 -0600194 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800195 event->vcpu = realloc(event->vcpu,
196 event->max_vcpu * sizeof(*event->vcpu));
197 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600198 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800199 pr_err("Not enough memory\n");
200 return false;
201 }
202
203 memset(event->vcpu + old_max_vcpu, 0,
204 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
205 return true;
206}
207
208static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
209{
210 struct kvm_event *event;
211
212 event = zalloc(sizeof(*event));
213 if (!event) {
214 pr_err("Not enough memory\n");
215 return NULL;
216 }
217
218 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200219 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800220 return event;
221}
222
Xiao Guangrong37860632012-11-15 14:17:01 +0800223static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600224 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800225{
226 struct kvm_event *event;
227 struct list_head *head;
228
229 BUG_ON(key->key == INVALID_KEY);
230
David Ahernde332ac2012-10-02 22:09:53 -0600231 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600232 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800233 if (event->key.key == key->key && event->key.info == key->info)
234 return event;
David Ahern355afe82012-10-08 11:17:32 -0600235 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800236
237 event = kvm_alloc_init_event(key);
238 if (!event)
239 return NULL;
240
241 list_add(&event->hash_entry, head);
242 return event;
243}
244
Xiao Guangrong37860632012-11-15 14:17:01 +0800245static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600246 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800247 struct event_key *key, u64 timestamp)
248{
249 struct kvm_event *event = NULL;
250
251 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600252 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800253
254 vcpu_record->last_event = event;
255 vcpu_record->start_time = timestamp;
256 return true;
257}
258
259static void
260kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
261{
262 kvm_stats->time += time_diff;
263 update_stats(&kvm_stats->stats, time_diff);
264}
265
266static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
267{
268 struct kvm_event_stats *kvm_stats = &event->total;
269
270 if (vcpu_id != -1)
271 kvm_stats = &event->vcpu[vcpu_id];
272
273 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
274 avg_stats(&kvm_stats->stats));
275}
276
277static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
278 u64 time_diff)
279{
David Ahern2aa8eab2012-10-08 11:17:35 -0600280 if (vcpu_id == -1) {
281 kvm_update_event_stats(&event->total, time_diff);
282 return true;
283 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800284
285 if (!kvm_event_expand(event, vcpu_id))
286 return false;
287
288 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
289 return true;
290}
291
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400292static bool is_child_event(struct perf_kvm_stat *kvm,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200293 struct evsel *evsel,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400294 struct perf_sample *sample,
295 struct event_key *key)
296{
297 struct child_event_ops *child_ops;
298
299 child_ops = kvm->events_ops->child_ops;
300
301 if (!child_ops)
302 return false;
303
304 for (; child_ops->name; child_ops++) {
305 if (!strcmp(evsel->name, child_ops->name)) {
306 child_ops->get_key(evsel, sample, key);
307 return true;
308 }
309 }
310
311 return false;
312}
313
314static bool handle_child_event(struct perf_kvm_stat *kvm,
315 struct vcpu_event_record *vcpu_record,
316 struct event_key *key,
317 struct perf_sample *sample __maybe_unused)
318{
319 struct kvm_event *event = NULL;
320
321 if (key->key != INVALID_KEY)
322 event = find_create_kvm_event(kvm, key);
323
324 vcpu_record->last_event = event;
325
326 return true;
327}
328
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400329static bool skip_event(const char *event)
330{
331 const char * const *skip_events;
332
333 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
334 if (!strcmp(event, *skip_events))
335 return true;
336
337 return false;
338}
339
Xiao Guangrong37860632012-11-15 14:17:01 +0800340static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600341 struct vcpu_event_record *vcpu_record,
342 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400343 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800344{
345 struct kvm_event *event;
346 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600347 int vcpu;
348
349 if (kvm->trace_vcpu == -1)
350 vcpu = -1;
351 else
352 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800353
354 event = vcpu_record->last_event;
355 time_begin = vcpu_record->start_time;
356
357 /* The begin event is not caught. */
358 if (!time_begin)
359 return true;
360
361 /*
362 * In some case, the 'begin event' only records the start timestamp,
363 * the actual event is recognized in the 'end event' (e.g. mmio-event).
364 */
365
366 /* Both begin and end events did not get the key. */
367 if (!event && key->key == INVALID_KEY)
368 return true;
369
370 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600371 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800372
373 if (!event)
374 return false;
375
376 vcpu_record->last_event = NULL;
377 vcpu_record->start_time = 0;
378
David Ahern1afe1d12013-08-05 21:41:34 -0400379 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400380 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400381 pr_debug("End time before begin time; skipping event.\n");
382 return true;
383 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800384
David Ahern70f7b4a2013-08-07 21:56:38 -0400385 time_diff = sample->time - time_begin;
386
387 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530388 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400389
390 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400391 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400392 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
393 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300394 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400395 }
396 }
397
David Ahern2aa8eab2012-10-08 11:17:35 -0600398 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800399}
400
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300401static
402struct vcpu_event_record *per_vcpu_record(struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200403 struct 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 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900407 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800408 struct vcpu_event_record *vcpu_record;
409
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300410 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800411 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300412 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800413 return NULL;
414 }
415
Hemant Kumar162607e2016-01-28 12:03:04 +0530416 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample,
417 vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900418 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800419 }
420
Namhyung Kim69e865c2014-10-06 09:46:02 +0900421 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800422}
423
Xiao Guangrong37860632012-11-15 14:17:01 +0800424static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600425 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200426 struct evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300427 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800428{
429 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400430 struct event_key key = { .key = INVALID_KEY,
431 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800432
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300433 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800434 if (!vcpu_record)
435 return true;
436
David Ahern2aa8eab2012-10-08 11:17:35 -0600437 /* only process events for vcpus user cares about */
438 if ((kvm->trace_vcpu != -1) &&
439 (kvm->trace_vcpu != vcpu_record->vcpu_id))
440 return true;
441
David Ahernde332ac2012-10-02 22:09:53 -0600442 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
443 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800444
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400445 if (is_child_event(kvm, evsel, sample, &key))
446 return handle_child_event(kvm, vcpu_record, &key, sample);
447
David Ahernde332ac2012-10-02 22:09:53 -0600448 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400449 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800450
451 return true;
452}
453
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800454#define GET_EVENT_KEY(func, field) \
455static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
456{ \
457 if (vcpu == -1) \
458 return event->total.field; \
459 \
460 if (vcpu >= event->max_vcpu) \
461 return 0; \
462 \
463 return event->vcpu[vcpu].field; \
464}
465
466#define COMPARE_EVENT_KEY(func, field) \
467GET_EVENT_KEY(func, field) \
468static int compare_kvm_event_ ## func(struct kvm_event *one, \
469 struct kvm_event *two, int vcpu)\
470{ \
471 return get_event_ ##func(one, vcpu) > \
472 get_event_ ##func(two, vcpu); \
473}
474
475GET_EVENT_KEY(time, time);
476COMPARE_EVENT_KEY(count, stats.n);
477COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400478GET_EVENT_KEY(max, stats.max);
479GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800480
481#define DEF_SORT_NAME_KEY(name, compare_key) \
482 { #name, compare_kvm_event_ ## compare_key }
483
484static struct kvm_event_key keys[] = {
485 DEF_SORT_NAME_KEY(sample, count),
486 DEF_SORT_NAME_KEY(time, mean),
487 { NULL, NULL }
488};
489
Xiao Guangrong37860632012-11-15 14:17:01 +0800490static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800491{
492 int i;
493
494 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600495 if (!strcmp(keys[i].name, kvm->sort_key)) {
496 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800497 return true;
498 }
499 }
500
David Ahernde332ac2012-10-02 22:09:53 -0600501 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800502 return false;
503}
504
David Ahernde332ac2012-10-02 22:09:53 -0600505static void insert_to_result(struct rb_root *result, struct kvm_event *event,
506 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800507{
David Ahernde332ac2012-10-02 22:09:53 -0600508 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800509 struct rb_node *parent = NULL;
510 struct kvm_event *p;
511
512 while (*rb) {
513 p = container_of(*rb, struct kvm_event, rb);
514 parent = *rb;
515
516 if (bigger(event, p, vcpu))
517 rb = &(*rb)->rb_left;
518 else
519 rb = &(*rb)->rb_right;
520 }
521
522 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600523 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800524}
525
Xiao Guangrong37860632012-11-15 14:17:01 +0800526static void
527update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800528{
David Ahernde332ac2012-10-02 22:09:53 -0600529 int vcpu = kvm->trace_vcpu;
530
531 kvm->total_count += get_event_count(event, vcpu);
532 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800533}
534
535static bool event_is_valid(struct kvm_event *event, int vcpu)
536{
537 return !!get_event_count(event, vcpu);
538}
539
Xiao Guangrong37860632012-11-15 14:17:01 +0800540static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800541{
542 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600543 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800544 struct kvm_event *event;
545
David Ahern355afe82012-10-08 11:17:32 -0600546 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
547 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800548 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600549 update_total_count(kvm, event);
550 insert_to_result(&kvm->result, event,
551 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800552 }
David Ahern355afe82012-10-08 11:17:32 -0600553 }
554 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800555}
556
557/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600558static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800559{
David Ahernde332ac2012-10-02 22:09:53 -0600560 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800561
562 if (!node)
563 return NULL;
564
David Ahernde332ac2012-10-02 22:09:53 -0600565 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800566 return container_of(node, struct kvm_event, rb);
567}
568
David Ahern1afe1d12013-08-05 21:41:34 -0400569static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800570{
David Ahern1afe1d12013-08-05 21:41:34 -0400571 int vcpu = kvm->trace_vcpu;
572
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800573 pr_info("Analyze events for ");
574
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400575 if (kvm->opts.target.system_wide)
576 pr_info("all VMs, ");
577 else if (kvm->opts.target.pid)
578 pr_info("pid(s) %s, ", kvm->opts.target.pid);
579 else
580 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400581
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800582 if (vcpu == -1)
583 pr_info("all VCPUs:\n\n");
584 else
585 pr_info("VCPU %d:\n\n", vcpu);
586}
587
David Ahern1afe1d12013-08-05 21:41:34 -0400588static void show_timeofday(void)
589{
590 char date[64];
591 struct timeval tv;
592 struct tm ltime;
593
594 gettimeofday(&tv, NULL);
595 if (localtime_r(&tv.tv_sec, &ltime)) {
596 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
597 pr_info("%s.%06ld", date, tv.tv_usec);
598 } else
599 pr_info("00:00:00.000000");
600
601 return;
602}
603
Xiao Guangrong37860632012-11-15 14:17:01 +0800604static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800605{
Hemant Kumar162607e2016-01-28 12:03:04 +0530606 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800607 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600608 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800609
David Ahern1afe1d12013-08-05 21:41:34 -0400610 if (kvm->live) {
611 puts(CONSOLE_CLEAR);
612 show_timeofday();
613 }
614
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800615 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400616 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530617 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800618 pr_info("%10s ", "Samples");
619 pr_info("%9s ", "Samples%");
620
621 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200622 pr_info("%11s ", "Min Time");
623 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800624 pr_info("%16s ", "Avg time");
625 pr_info("\n\n");
626
David Ahernde332ac2012-10-02 22:09:53 -0600627 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400628 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800629
630 ecount = get_event_count(event, vcpu);
631 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400632 max = get_event_max(event, vcpu);
633 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800634
David Ahernde332ac2012-10-02 22:09:53 -0600635 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530636 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800637 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600638 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
639 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300640 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
641 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
642 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800643 kvm_event_rel_stddev(vcpu, event));
644 pr_info("\n");
645 }
646
David Aherne4f76372012-10-08 11:17:34 -0600647 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300648 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400649
650 if (kvm->lost_events)
651 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
652}
653
David Ahern87419c92013-10-29 10:43:16 -0600654#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400655static int process_lost_event(struct perf_tool *tool,
656 union perf_event *event __maybe_unused,
657 struct perf_sample *sample __maybe_unused,
658 struct machine *machine __maybe_unused)
659{
660 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
661
662 kvm->lost_events++;
663 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800664}
David Ahern87419c92013-10-29 10:43:16 -0600665#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800666
David Ahern2e73f002013-08-05 21:41:37 -0400667static bool skip_sample(struct perf_kvm_stat *kvm,
668 struct perf_sample *sample)
669{
670 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
671 return true;
672
673 return false;
674}
675
David Ahernde332ac2012-10-02 22:09:53 -0600676static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800677 union perf_event *event,
678 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200679 struct evsel *evsel,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800680 struct machine *machine)
681{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300682 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400683 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800684 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
685 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800686
David Ahern2e73f002013-08-05 21:41:37 -0400687 if (skip_sample(kvm, sample))
688 return 0;
689
Adrian Hunter314add62013-08-27 11:23:03 +0300690 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800691 if (thread == NULL) {
692 pr_debug("problem processing %d event, skipping it.\n",
693 event->header.type);
694 return -1;
695 }
696
David Ahernde332ac2012-10-02 22:09:53 -0600697 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300698 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800699
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300700 thread__put(thread);
701 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800702}
703
David Ahern1afe1d12013-08-05 21:41:34 -0400704static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800705{
David Ahern1afe1d12013-08-05 21:41:34 -0400706 char buf[64], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400707 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400708
709 if (kvm->live) {
710 err = get_cpuid(buf, sizeof(buf));
711 if (err != 0) {
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400712 pr_err("Failed to look up CPU type\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400713 return err;
714 }
715 cpuid = buf;
716 } else
717 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800718
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400719 if (!cpuid) {
720 pr_err("Failed to look up CPU type\n");
721 return -EINVAL;
722 }
723
724 err = cpu_isa_init(kvm, cpuid);
725 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800726 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800727
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400728 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400729}
730
731static bool verify_vcpu(int vcpu)
732{
733 if (vcpu != -1 && vcpu < 0) {
734 pr_err("Invalid vcpu:%d.\n", vcpu);
735 return false;
736 }
737
738 return true;
739}
740
David Ahern87419c92013-10-29 10:43:16 -0600741#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400742/* keeping the max events to a modest level to keep
743 * the processing of samples per mmap smooth.
744 */
745#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
746
747static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
748 u64 *mmap_time)
749{
Jiri Olsa63503db2019-07-21 13:23:52 +0200750 struct evlist *evlist = kvm->evlist;
David Ahern1afe1d12013-08-05 21:41:34 -0400751 union perf_event *event;
Kan Liang53172f92018-03-01 18:08:58 -0500752 struct perf_mmap *md;
Jiri Olsa93d10af2017-08-03 13:21:14 +0200753 u64 timestamp;
David Ahern1afe1d12013-08-05 21:41:34 -0400754 s64 n = 0;
755 int err;
756
757 *mmap_time = ULLONG_MAX;
Kan Liang53172f92018-03-01 18:08:58 -0500758 md = &evlist->mmap[idx];
Kan Liangb9bae2c2018-03-06 10:36:07 -0500759 err = perf_mmap__read_init(md);
Kan Liang53172f92018-03-01 18:08:58 -0500760 if (err < 0)
761 return (err == -EAGAIN) ? 0 : -1;
762
Kan Liang0019dc872018-03-06 10:36:06 -0500763 while ((event = perf_mmap__read_event(md)) != NULL) {
Kan Liang53172f92018-03-01 18:08:58 -0500764 err = perf_evlist__parse_sample_timestamp(evlist, event, &timestamp);
David Ahern1afe1d12013-08-05 21:41:34 -0400765 if (err) {
Kan Liangd6ace3d2018-03-06 10:36:05 -0500766 perf_mmap__consume(md);
David Ahern1afe1d12013-08-05 21:41:34 -0400767 pr_err("Failed to parse sample\n");
768 return -1;
769 }
770
Jiri Olsa93d10af2017-08-03 13:21:14 +0200771 err = perf_session__queue_event(kvm->session, event, timestamp, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800772 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300773 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800774 * point to it, and it'll get possibly overwritten by the kernel.
775 */
Kan Liangd6ace3d2018-03-06 10:36:05 -0500776 perf_mmap__consume(md);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800777
David Ahern1afe1d12013-08-05 21:41:34 -0400778 if (err) {
779 pr_err("Failed to enqueue sample: %d\n", err);
780 return -1;
781 }
782
783 /* save time stamp of our first sample for this mmap */
784 if (n == 0)
Jiri Olsa93d10af2017-08-03 13:21:14 +0200785 *mmap_time = timestamp;
David Ahern1afe1d12013-08-05 21:41:34 -0400786
787 /* limit events per mmap handled all at once */
788 n++;
789 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
790 break;
791 }
792
Kan Liang53172f92018-03-01 18:08:58 -0500793 perf_mmap__read_done(md);
David Ahern1afe1d12013-08-05 21:41:34 -0400794 return n;
795}
796
797static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
798{
799 int i, err, throttled = 0;
800 s64 n, ntotal = 0;
801 u64 flush_time = ULLONG_MAX, mmap_time;
802
803 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
804 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
805 if (n < 0)
806 return -1;
807
808 /* flush time is going to be the minimum of all the individual
809 * mmap times. Essentially, we flush all the samples queued up
810 * from the last pass under our minimal start time -- that leaves
811 * a very small race for samples to come in with a lower timestamp.
812 * The ioctl to return the perf_clock timestamp should close the
813 * race entirely.
814 */
815 if (mmap_time < flush_time)
816 flush_time = mmap_time;
817
818 ntotal += n;
819 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
820 throttled = 1;
821 }
822
823 /* flush queue after each round in which we processed events */
824 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300825 struct ordered_events *oe = &kvm->session->ordered_events;
826
827 oe->next_flush = flush_time;
828 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400829 if (err) {
830 if (kvm->lost_events)
831 pr_info("\nLost events: %" PRIu64 "\n\n",
832 kvm->lost_events);
833 return err;
834 }
835 }
836
837 return throttled;
838}
839
840static volatile int done;
841
842static void sig_handler(int sig __maybe_unused)
843{
844 done = 1;
845}
846
847static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
848{
849 struct itimerspec new_value;
850 int rc = -1;
851
852 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
853 if (kvm->timerfd < 0) {
854 pr_err("timerfd_create failed\n");
855 goto out;
856 }
857
858 new_value.it_value.tv_sec = kvm->display_time;
859 new_value.it_value.tv_nsec = 0;
860 new_value.it_interval.tv_sec = kvm->display_time;
861 new_value.it_interval.tv_nsec = 0;
862
863 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
864 pr_err("timerfd_settime failed: %d\n", errno);
865 close(kvm->timerfd);
866 goto out;
867 }
868
869 rc = 0;
870out:
871 return rc;
872}
873
874static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
875{
876 uint64_t c;
877 int rc;
878
879 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
880 if (rc < 0) {
881 if (errno == EAGAIN)
882 return 0;
883
884 pr_err("Failed to read timer fd: %d\n", errno);
885 return -1;
886 }
887
888 if (rc != sizeof(uint64_t)) {
889 pr_err("Error reading timer fd - invalid size returned\n");
890 return -1;
891 }
892
893 if (c != 1)
894 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
895
896 /* update display */
897 sort_result(kvm);
898 print_result(kvm);
899
900 /* reset counts */
901 clear_events_cache_stats(kvm->kvm_events_cache);
902 kvm->total_count = 0;
903 kvm->total_time = 0;
904 kvm->lost_events = 0;
905
906 return 0;
907}
908
909static int fd_set_nonblock(int fd)
910{
911 long arg = 0;
912
913 arg = fcntl(fd, F_GETFL);
914 if (arg < 0) {
915 pr_err("Failed to get current flags for fd %d\n", fd);
916 return -1;
917 }
918
919 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
920 pr_err("Failed to set non-block option on fd %d\n", fd);
921 return -1;
922 }
923
924 return 0;
925}
926
Jiri Olsad5b41302014-08-11 10:50:09 +0200927static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400928{
929 int c;
930
David Ahern1afe1d12013-08-05 21:41:34 -0400931 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400932 if (c == 'q')
933 return 1;
934
935 return 0;
936}
937
938static int kvm_events_live_report(struct perf_kvm_stat *kvm)
939{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200940 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200941 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400942
943 /* live flag must be set first */
944 kvm->live = true;
945
946 ret = cpu_isa_config(kvm);
947 if (ret < 0)
948 return ret;
949
950 if (!verify_vcpu(kvm->trace_vcpu) ||
951 !select_key(kvm) ||
952 !register_kvm_events_ops(kvm)) {
953 goto out;
954 }
955
Jiri Olsad5b41302014-08-11 10:50:09 +0200956 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400957 init_kvm_event_record(kvm);
958
David Ahern1afe1d12013-08-05 21:41:34 -0400959 signal(SIGINT, sig_handler);
960 signal(SIGTERM, sig_handler);
961
David Ahern1afe1d12013-08-05 21:41:34 -0400962 /* add timer fd */
963 if (perf_kvm__timerfd_create(kvm) < 0) {
964 err = -1;
965 goto out;
966 }
967
Jiri Olsafe636ad2014-10-01 18:00:27 +0200968 if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300969 goto out;
970
Jiri Olsa0cae0132014-10-01 18:00:28 +0200971 nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
972 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300973 goto out;
974
David Ahern1afe1d12013-08-05 21:41:34 -0400975 if (fd_set_nonblock(fileno(stdin)) != 0)
976 goto out;
977
978 /* everything is good - enable the events and process */
Jiri Olsa1c87f162019-07-21 13:24:08 +0200979 evlist__enable(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -0400980
981 while (!done) {
Jiri Olsa1ca72262014-10-01 18:00:29 +0200982 struct fdarray *fda = &kvm->evlist->pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400983 int rc;
984
985 rc = perf_kvm__mmap_read(kvm);
986 if (rc < 0)
987 break;
988
989 err = perf_kvm__handle_timerfd(kvm);
990 if (err)
991 goto out;
992
Jiri Olsa1ca72262014-10-01 18:00:29 +0200993 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200994 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400995
996 if (!rc && !done)
Jiri Olsa1ca72262014-10-01 18:00:29 +0200997 err = fdarray__poll(fda, 100);
David Ahern1afe1d12013-08-05 21:41:34 -0400998 }
999
Jiri Olsae74676d2019-07-21 13:24:09 +02001000 evlist__disable(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001001
1002 if (err == 0) {
1003 sort_result(kvm);
1004 print_result(kvm);
1005 }
1006
1007out:
1008 if (kvm->timerfd >= 0)
1009 close(kvm->timerfd);
1010
Jiri Olsad5b41302014-08-11 10:50:09 +02001011 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -04001012 return err;
1013}
1014
1015static int kvm_live_open_events(struct perf_kvm_stat *kvm)
1016{
1017 int err, rc = -1;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001018 struct evsel *pos;
Jiri Olsa63503db2019-07-21 13:23:52 +02001019 struct evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001020 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -04001021
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -03001022 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001023
1024 /*
1025 * Note: exclude_{guest,host} do not apply here.
1026 * This command processes KVM tracepoints from host only
1027 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001028 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001029 struct perf_event_attr *attr = &pos->core.attr;
David Ahern1afe1d12013-08-05 21:41:34 -04001030
1031 /* make sure these *are* set */
Adrian Huntere71aa282013-09-06 22:40:12 +03001032 perf_evsel__set_sample_bit(pos, TID);
1033 perf_evsel__set_sample_bit(pos, TIME);
1034 perf_evsel__set_sample_bit(pos, CPU);
1035 perf_evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001036 /* make sure these are *not*; want as small a sample as possible */
Adrian Huntere71aa282013-09-06 22:40:12 +03001037 perf_evsel__reset_sample_bit(pos, PERIOD);
1038 perf_evsel__reset_sample_bit(pos, IP);
1039 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1040 perf_evsel__reset_sample_bit(pos, ADDR);
1041 perf_evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001042 attr->mmap = 0;
1043 attr->comm = 0;
1044 attr->task = 0;
1045
1046 attr->sample_period = 1;
1047
1048 attr->watermark = 0;
1049 attr->wakeup_events = 1000;
1050
1051 /* will enable all once we are ready */
1052 attr->disabled = 1;
1053 }
1054
Jiri Olsa474ddc42019-07-21 13:24:06 +02001055 err = evlist__open(evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001056 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001057 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001058 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001059 goto out;
1060 }
1061
Wang Nanf74b9d3a2017-12-03 02:00:37 +00001062 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001063 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001064 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsa750b4ed2019-07-21 13:24:07 +02001065 evlist__close(evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001066 goto out;
1067 }
1068
1069 rc = 0;
1070
1071out:
1072 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001073}
David Ahern87419c92013-10-29 10:43:16 -06001074#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001075
Xiao Guangrong37860632012-11-15 14:17:01 +08001076static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001077{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001078 int ret;
1079
David Ahernde332ac2012-10-02 22:09:53 -06001080 struct perf_tool eops = {
1081 .sample = process_sample_event,
1082 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301083 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001084 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001085 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001086 struct perf_data file = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001087 .path = kvm->file_name,
1088 .mode = PERF_DATA_MODE_READ,
1089 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001090 };
David Ahernde332ac2012-10-02 22:09:53 -06001091
1092 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001093 kvm->session = perf_session__new(&file, false, &kvm->tool);
David Ahernde332ac2012-10-02 22:09:53 -06001094 if (!kvm->session) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001095 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +09001096 return -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001097 }
1098
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001099 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001100
Taeung Song41b98362015-07-01 00:18:44 +09001101 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1102 ret = -EINVAL;
1103 goto out_delete;
1104 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001105
1106 /*
1107 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1108 * traced in the old kernel.
1109 */
David Ahern1afe1d12013-08-05 21:41:34 -04001110 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001111 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001112 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001113
Taeung Song41b98362015-07-01 00:18:44 +09001114 ret = perf_session__process_events(kvm->session);
1115
1116out_delete:
1117 perf_session__delete(kvm->session);
1118 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001119}
1120
David Ahern2e73f002013-08-05 21:41:37 -04001121static int parse_target_str(struct perf_kvm_stat *kvm)
1122{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001123 if (kvm->opts.target.pid) {
1124 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001125 if (kvm->pid_list == NULL) {
1126 pr_err("Error parsing process id string\n");
1127 return -EINVAL;
1128 }
1129 }
1130
1131 return 0;
1132}
1133
Xiao Guangrong37860632012-11-15 14:17:01 +08001134static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001135{
1136 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001137 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001138
David Ahern2e73f002013-08-05 21:41:37 -04001139 if (parse_target_str(kvm) != 0)
1140 goto exit;
1141
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001142 if (!verify_vcpu(vcpu))
1143 goto exit;
1144
David Ahernde332ac2012-10-02 22:09:53 -06001145 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001146 goto exit;
1147
David Ahernde332ac2012-10-02 22:09:53 -06001148 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001149 goto exit;
1150
David Ahernde332ac2012-10-02 22:09:53 -06001151 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001152 setup_pager();
1153
David Ahernde332ac2012-10-02 22:09:53 -06001154 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001155 if (ret)
1156 goto exit;
1157
David Ahernde332ac2012-10-02 22:09:53 -06001158 sort_result(kvm);
1159 print_result(kvm);
1160
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001161exit:
1162 return ret;
1163}
1164
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001165#define STRDUP_FAIL_EXIT(s) \
1166 ({ char *_p; \
1167 _p = strdup(s); \
1168 if (!_p) \
1169 return -ENOMEM; \
1170 _p; \
1171 })
1172
Hemant Kumar066d3592016-01-28 12:03:06 +05301173int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1174{
1175 return 0;
1176}
1177
Xiao Guangrong37860632012-11-15 14:17:01 +08001178static int
1179kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001180{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001181 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001182 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001183 const char * const record_args[] = {
1184 "record",
1185 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001186 "-m", "1024",
1187 "-c", "1",
1188 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001189 const char * const kvm_stat_record_usage[] = {
1190 "perf kvm stat record [<options>]",
1191 NULL
1192 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001193 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301194 int ret;
1195
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001196 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301197 ret = setup_kvm_events_tp(kvm);
1198 if (ret < 0) {
1199 pr_err("Unable to setup the kvm tracepoints\n");
1200 return ret;
1201 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001202
1203 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1204 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001205
David Ahern8fdd84c2013-08-02 14:05:42 -06001206 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001207 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001208 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1209
1210 if (rec_argv == NULL)
1211 return -ENOMEM;
1212
1213 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1214 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1215
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001216 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001217 rec_argv[i++] = "-e";
1218 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1219 }
1220
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001221 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001222 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001223
1224 for (j = 1; j < (unsigned int)argc; j++, i++)
1225 rec_argv[i] = argv[j];
1226
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001227 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1228 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1229 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1230
1231 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1232 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1233 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1234 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1235 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1236 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1237 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1238 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1239 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1240 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1241 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1242 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1243 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1244 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1245 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1246
1247 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001248 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001249}
1250
Xiao Guangrong37860632012-11-15 14:17:01 +08001251static int
1252kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001253{
David Ahernde332ac2012-10-02 22:09:53 -06001254 const struct option kvm_events_report_options[] = {
1255 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001256 "event for reporting: vmexit, "
1257 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001258 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1259 "vcpu id to report"),
1260 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1261 "key for sorting: sample(sort by samples number)"
1262 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001263 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001264 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001265 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001266 OPT_END()
1267 };
1268
1269 const char * const kvm_events_report_usage[] = {
1270 "perf kvm stat report [<options>]",
1271 NULL
1272 };
1273
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001274 if (argc) {
1275 argc = parse_options(argc, argv,
1276 kvm_events_report_options,
1277 kvm_events_report_usage, 0);
1278 if (argc)
1279 usage_with_options(kvm_events_report_usage,
1280 kvm_events_report_options);
1281 }
1282
Alexander Yaryginf1819572014-09-01 17:44:54 +04001283 if (!kvm->opts.target.pid)
1284 kvm->opts.target.system_wide = true;
1285
David Ahernde332ac2012-10-02 22:09:53 -06001286 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001287}
1288
David Ahern87419c92013-10-29 10:43:16 -06001289#ifdef HAVE_TIMERFD_SUPPORT
Jiri Olsa63503db2019-07-21 13:23:52 +02001290static struct evlist *kvm_live_event_list(void)
David Ahern1afe1d12013-08-05 21:41:34 -04001291{
Jiri Olsa63503db2019-07-21 13:23:52 +02001292 struct evlist *evlist;
David Ahern1afe1d12013-08-05 21:41:34 -04001293 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001294 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001295 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001296
Jiri Olsa0f98b112019-07-21 13:23:55 +02001297 evlist = evlist__new();
David Ahern1afe1d12013-08-05 21:41:34 -04001298 if (evlist == NULL)
1299 return NULL;
1300
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001301 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001302
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001303 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001304 if (tp == NULL)
1305 goto out;
1306
1307 /* split tracepoint into subsystem and name */
1308 sys = tp;
1309 name = strchr(tp, ':');
1310 if (name == NULL) {
1311 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001312 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001313 free(tp);
1314 goto out;
1315 }
1316 *name = '\0';
1317 name++;
1318
1319 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001320 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001321 free(tp);
1322 goto out;
1323 }
1324
1325 free(tp);
1326 }
1327
1328 err = 0;
1329
1330out:
1331 if (err) {
Jiri Olsac12995a2019-07-21 13:23:56 +02001332 evlist__delete(evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001333 evlist = NULL;
1334 }
1335
1336 return evlist;
1337}
1338
1339static int kvm_events_live(struct perf_kvm_stat *kvm,
1340 int argc, const char **argv)
1341{
1342 char errbuf[BUFSIZ];
1343 int err;
1344
1345 const struct option live_options[] = {
1346 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1347 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001348 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1349 "number of mmap data pages",
1350 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001351 OPT_INCR('v', "verbose", &verbose,
1352 "be more verbose (show counter open errors, etc)"),
1353 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1354 "system-wide collection from all CPUs"),
1355 OPT_UINTEGER('d', "display", &kvm->display_time,
1356 "time in seconds between display updates"),
1357 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001358 "event for reporting: "
1359 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001360 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1361 "vcpu id to report"),
1362 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1363 "key for sorting: sample(sort by samples number)"
1364 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001365 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001366 "show events other than"
1367 " HLT (x86 only) or Wait state (s390 only)"
1368 " that take longer than duration usecs"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001369 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04001370 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001371 OPT_END()
1372 };
1373 const char * const live_usage[] = {
1374 "perf kvm stat live [<options>]",
1375 NULL
1376 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001377 struct perf_data data = {
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001378 .mode = PERF_DATA_MODE_WRITE,
1379 };
David Ahern1afe1d12013-08-05 21:41:34 -04001380
1381
1382 /* event handling */
1383 kvm->tool.sample = process_sample_event;
1384 kvm->tool.comm = perf_event__process_comm;
1385 kvm->tool.exit = perf_event__process_exit;
1386 kvm->tool.fork = perf_event__process_fork;
1387 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301388 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001389 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001390 perf_tool__fill_defaults(&kvm->tool);
1391
1392 /* set defaults */
1393 kvm->display_time = 1;
1394 kvm->opts.user_interval = 1;
1395 kvm->opts.mmap_pages = 512;
1396 kvm->opts.target.uses_mmap = false;
1397 kvm->opts.target.uid_str = NULL;
1398 kvm->opts.target.uid = UINT_MAX;
1399
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001400 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001401 disable_buildid_cache();
1402
1403 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001404
1405 if (argc) {
1406 argc = parse_options(argc, argv, live_options,
1407 live_usage, 0);
1408 if (argc)
1409 usage_with_options(live_usage, live_options);
1410 }
1411
David Ahern70f7b4a2013-08-07 21:56:38 -04001412 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1413
David Ahern1afe1d12013-08-05 21:41:34 -04001414 /*
1415 * target related setups
1416 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001417 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001418 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001419 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001420 ui__warning("%s", errbuf);
1421 }
1422
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001423 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001424 kvm->opts.target.system_wide = true;
1425
1426
1427 /*
1428 * generate the event list
1429 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301430 err = setup_kvm_events_tp(kvm);
1431 if (err < 0) {
1432 pr_err("Unable to setup the kvm tracepoints\n");
1433 return err;
1434 }
1435
David Ahern1afe1d12013-08-05 21:41:34 -04001436 kvm->evlist = kvm_live_event_list();
1437 if (kvm->evlist == NULL) {
1438 err = -1;
1439 goto out;
1440 }
1441
David Ahern1afe1d12013-08-05 21:41:34 -04001442 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1443 usage_with_options(live_usage, live_options);
1444
1445 /*
1446 * perf session
1447 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001448 kvm->session = perf_session__new(&data, false, &kvm->tool);
David Ahern1afe1d12013-08-05 21:41:34 -04001449 if (kvm->session == NULL) {
Taeung Song52e028342014-09-24 10:33:37 +09001450 err = -1;
David Ahern1afe1d12013-08-05 21:41:34 -04001451 goto out;
1452 }
1453 kvm->session->evlist = kvm->evlist;
1454 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001455 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001456 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Jiri Olsa03617c22019-07-21 13:24:42 +02001457 kvm->evlist->core.threads, false, 1);
David Ahern1afe1d12013-08-05 21:41:34 -04001458 err = kvm_live_open_events(kvm);
1459 if (err)
1460 goto out;
1461
1462 err = kvm_events_live_report(kvm);
1463
1464out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001465 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001466 kvm->session = NULL;
Jiri Olsac12995a2019-07-21 13:23:56 +02001467 evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001468
1469 return err;
1470}
David Ahern87419c92013-10-29 10:43:16 -06001471#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001472
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001473static void print_kvm_stat_usage(void)
1474{
1475 printf("Usage: perf kvm stat <command>\n\n");
1476
1477 printf("# Available commands:\n");
1478 printf("\trecord: record kvm events\n");
1479 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001480 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001481
1482 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1483}
1484
Xiao Guangrong37860632012-11-15 14:17:01 +08001485static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001486{
Xiao Guangrong37860632012-11-15 14:17:01 +08001487 struct perf_kvm_stat kvm = {
1488 .file_name = file_name,
1489
1490 .trace_vcpu = -1,
1491 .report_event = "vmexit",
1492 .sort_key = "sample",
1493
Xiao Guangrong37860632012-11-15 14:17:01 +08001494 };
1495
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001496 if (argc == 1) {
1497 print_kvm_stat_usage();
1498 goto perf_stat;
1499 }
1500
1501 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001502 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001503
1504 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001505 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001506
David Ahern87419c92013-10-29 10:43:16 -06001507#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001508 if (!strncmp(argv[1], "live", 4))
1509 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001510#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001511
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001512perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001513 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001514}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001515#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001516
Xiao Guangrong37860632012-11-15 14:17:01 +08001517static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001518{
1519 int rec_argc, i = 0, j;
1520 const char **rec_argv;
1521
1522 rec_argc = argc + 2;
1523 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1524 rec_argv[i++] = strdup("record");
1525 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001526 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001527 for (j = 1; j < argc; j++, i++)
1528 rec_argv[i] = argv[j];
1529
1530 BUG_ON(i != rec_argc);
1531
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001532 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001533}
1534
Xiao Guangrong37860632012-11-15 14:17:01 +08001535static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001536{
1537 int rec_argc, i = 0, j;
1538 const char **rec_argv;
1539
1540 rec_argc = argc + 2;
1541 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1542 rec_argv[i++] = strdup("report");
1543 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001544 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001545 for (j = 1; j < argc; j++, i++)
1546 rec_argv[i] = argv[j];
1547
1548 BUG_ON(i != rec_argc);
1549
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001550 return cmd_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001551}
1552
Xiao Guangrong37860632012-11-15 14:17:01 +08001553static int
1554__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001555{
1556 int rec_argc, i = 0, j;
1557 const char **rec_argv;
1558
1559 rec_argc = argc + 2;
1560 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1561 rec_argv[i++] = strdup("buildid-list");
1562 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001563 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001564 for (j = 1; j < argc; j++, i++)
1565 rec_argv[i] = argv[j];
1566
1567 BUG_ON(i != rec_argc);
1568
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001569 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001570}
1571
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001572int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001573{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001574 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001575 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001576 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001577 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001578 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001579 "Output file name"),
1580 OPT_BOOLEAN(0, "guest", &perf_guest,
1581 "Collect guest os data"),
1582 OPT_BOOLEAN(0, "host", &perf_host,
1583 "Collect host os data"),
1584 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1585 "guest mount directory under which every guest os"
1586 " instance has a subdir"),
1587 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1588 "file", "file saving guest os vmlinux"),
1589 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1590 "file", "file saving guest os /proc/kallsyms"),
1591 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1592 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001593 OPT_INCR('v', "verbose", &verbose,
1594 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001595 OPT_END()
1596 };
1597
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001598 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1599 "buildid-list", "stat", NULL };
1600 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001601
Joerg Roedel1aed2672012-01-04 17:54:20 +01001602 perf_host = 0;
1603 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001604
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001605 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1606 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001607 if (!argc)
1608 usage_with_options(kvm_usage, kvm_options);
1609
1610 if (!perf_host)
1611 perf_guest = 1;
1612
Xiao Guangrong37860632012-11-15 14:17:01 +08001613 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001614 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001615
Xiao Guangrong37860632012-11-15 14:17:01 +08001616 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001617 pr_err("Failed to allocate memory for filename\n");
1618 return -ENOMEM;
1619 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001620 }
1621
1622 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001623 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001624 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001625 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001626 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001627 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001628 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001629 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001630 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001631 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001632#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001633 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001634 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001635#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001636 else
1637 usage_with_options(kvm_usage, kvm_options);
1638
1639 return 0;
1640}