blob: 95a77058023e66c0a3ce80cf1f42f8cbfd706147 [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 Meloe0fcfb02019-09-23 12:20:38 -03008#include "util/mmap.h"
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -03009#include "util/term.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080010#include "util/symbol.h"
11#include "util/thread.h"
12#include "util/header.h"
13#include "util/session.h"
David Ahern2e73f002013-08-05 21:41:37 -040014#include "util/intlist.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030015#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060016#include <subcmd/parse-options.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080017#include "util/trace-event.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080018#include "util/debug.h"
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080019#include "util/tool.h"
20#include "util/stat.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030021#include "util/synthetic-events.h"
David Ahern1afe1d12013-08-05 21:41:34 -040022#include "util/top.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020023#include "util/data.h"
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030024#include "util/ordered-events.h"
Anju T Sudhakar8067b3d2019-07-18 23:47:47 +053025#include "util/kvm-stat.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030026#include "ui/ui.h"
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080027
28#include <sys/prctl.h>
David Ahern87419c92013-10-29 10:43:16 -060029#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -040030#include <sys/timerfd.h>
David Ahern87419c92013-10-29 10:43:16 -060031#endif
Arnaldo Carvalho de Meloc5e40272017-04-19 16:12:39 -030032#include <sys/time.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030033#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080036
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053037#include <linux/err.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030038#include <linux/kernel.h>
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030039#include <linux/string.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030040#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030041#include <linux/zalloc.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030042#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030043#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030044#include <poll.h>
David Ahern1afe1d12013-08-05 21:41:34 -040045#include <termios.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080046#include <semaphore.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030047#include <signal.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080048#include <math.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +020049#include <perf/mmap.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080050
Arnaldo Carvalho de Melobb8c16d2017-04-19 16:15:13 -030051static const char *get_filename_for_perf_kvm(void)
52{
53 const char *filename;
54
55 if (perf_host && !perf_guest)
56 filename = strdup("perf.data.host");
57 else if (!perf_host && perf_guest)
58 filename = strdup("perf.data.guest");
59 else
60 filename = strdup("perf.data.kvm");
61
62 return filename;
63}
64
Alexander Yaryginda50ad62014-07-03 17:59:49 +040065#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080066
Jiri Olsa32dcd022019-07-21 13:23:51 +020067void exit_event_get_key(struct evsel *evsel,
Alexander Yarygin9daa8122014-07-03 18:29:05 +040068 struct perf_sample *sample,
69 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080070{
71 key->info = 0;
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -030072 key->key = evsel__intval(evsel, sample, kvm_exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080073}
74
Jiri Olsa32dcd022019-07-21 13:23:51 +020075bool kvm_exit_event(struct evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080076{
Hemant Kumar162607e2016-01-28 12:03:04 +053077 return !strcmp(evsel->name, kvm_exit_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080078}
79
Jiri Olsa32dcd022019-07-21 13:23:51 +020080bool exit_event_begin(struct evsel *evsel,
Alexander Yarygin9daa8122014-07-03 18:29:05 +040081 struct perf_sample *sample, struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080082{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -030083 if (kvm_exit_event(evsel)) {
84 exit_event_get_key(evsel, sample, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080085 return true;
86 }
87
88 return false;
89}
90
Jiri Olsa32dcd022019-07-21 13:23:51 +020091bool kvm_entry_event(struct evsel *evsel)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080092{
Hemant Kumar162607e2016-01-28 12:03:04 +053093 return !strcmp(evsel->name, kvm_entry_trace);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080094}
95
Jiri Olsa32dcd022019-07-21 13:23:51 +020096bool exit_event_end(struct evsel *evsel,
Alexander Yarygin9daa8122014-07-03 18:29:05 +040097 struct perf_sample *sample __maybe_unused,
98 struct event_key *key __maybe_unused)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +080099{
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300100 return kvm_entry_event(evsel);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800101}
102
Alexander Yarygindf74c132014-07-03 17:59:50 +0400103static const char *get_exit_reason(struct perf_kvm_stat *kvm,
104 struct exit_reasons_table *tbl,
105 u64 exit_code)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800106{
Alexander Yarygindf74c132014-07-03 17:59:50 +0400107 while (tbl->reason != NULL) {
David Ahernde332ac2012-10-02 22:09:53 -0600108 if (tbl->exit_code == exit_code)
109 return tbl->reason;
110 tbl++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800111 }
112
113 pr_err("unknown kvm exit code:%lld on %s\n",
David Ahernde332ac2012-10-02 22:09:53 -0600114 (unsigned long long)exit_code, kvm->exit_reasons_isa);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800115 return "UNKNOWN";
116}
117
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400118void exit_event_decode_key(struct perf_kvm_stat *kvm,
119 struct event_key *key,
120 char *decode)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800121{
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400122 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
Alexander Yarygindf74c132014-07-03 17:59:50 +0400123 key->key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800124
Hemant Kumar162607e2016-01-28 12:03:04 +0530125 scnprintf(decode, decode_str_len, "%s", exit_reason);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800126}
127
Xiao Guangrong37860632012-11-15 14:17:01 +0800128static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800129{
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400130 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800131
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400132 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
133 if (!strcmp(events_ops->name, kvm->report_event)) {
134 kvm->events_ops = events_ops->ops;
135 return true;
136 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800137 }
138
Alexander Yarygin9daa8122014-07-03 18:29:05 +0400139 return false;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800140}
141
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800142struct vcpu_event_record {
143 int vcpu_id;
144 u64 start_time;
145 struct kvm_event *last_event;
146};
147
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800148
Xiao Guangrong37860632012-11-15 14:17:01 +0800149static void init_kvm_event_record(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800150{
David Ahernb880dee2012-10-08 11:17:30 -0600151 unsigned int i;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800152
David Ahernb880dee2012-10-08 11:17:30 -0600153 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
David Ahernde332ac2012-10-02 22:09:53 -0600154 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800155}
156
David Ahern87419c92013-10-29 10:43:16 -0600157#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400158static void clear_events_cache_stats(struct list_head *kvm_events_cache)
159{
160 struct list_head *head;
161 struct kvm_event *event;
162 unsigned int i;
David Ahern62d04db2013-08-05 21:41:35 -0400163 int j;
David Ahern1afe1d12013-08-05 21:41:34 -0400164
165 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
166 head = &kvm_events_cache[i];
167 list_for_each_entry(event, head, hash_entry) {
168 /* reset stats for event */
David Ahern62d04db2013-08-05 21:41:35 -0400169 event->total.time = 0;
170 init_stats(&event->total.stats);
171
172 for (j = 0; j < event->max_vcpu; ++j) {
173 event->vcpu[j].time = 0;
174 init_stats(&event->vcpu[j].stats);
175 }
David Ahern1afe1d12013-08-05 21:41:34 -0400176 }
177 }
178}
David Ahern87419c92013-10-29 10:43:16 -0600179#endif
David Ahern1afe1d12013-08-05 21:41:34 -0400180
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800181static int kvm_events_hash_fn(u64 key)
182{
183 return key & (EVENTS_CACHE_SIZE - 1);
184}
185
186static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
187{
188 int old_max_vcpu = event->max_vcpu;
David Ahern6ca5f302013-05-25 18:24:46 -0600189 void *prev;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800190
191 if (vcpu_id < event->max_vcpu)
192 return true;
193
194 while (event->max_vcpu <= vcpu_id)
195 event->max_vcpu += DEFAULT_VCPU_NUM;
196
David Ahern6ca5f302013-05-25 18:24:46 -0600197 prev = event->vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800198 event->vcpu = realloc(event->vcpu,
199 event->max_vcpu * sizeof(*event->vcpu));
200 if (!event->vcpu) {
David Ahern6ca5f302013-05-25 18:24:46 -0600201 free(prev);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800202 pr_err("Not enough memory\n");
203 return false;
204 }
205
206 memset(event->vcpu + old_max_vcpu, 0,
207 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
208 return true;
209}
210
211static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
212{
213 struct kvm_event *event;
214
215 event = zalloc(sizeof(*event));
216 if (!event) {
217 pr_err("Not enough memory\n");
218 return NULL;
219 }
220
221 event->key = *key;
Alexander Yaryginacb61fc2014-04-09 16:21:59 +0200222 init_stats(&event->total.stats);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800223 return event;
224}
225
Xiao Guangrong37860632012-11-15 14:17:01 +0800226static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600227 struct event_key *key)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800228{
229 struct kvm_event *event;
230 struct list_head *head;
231
232 BUG_ON(key->key == INVALID_KEY);
233
David Ahernde332ac2012-10-02 22:09:53 -0600234 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
David Ahern355afe82012-10-08 11:17:32 -0600235 list_for_each_entry(event, head, hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800236 if (event->key.key == key->key && event->key.info == key->info)
237 return event;
David Ahern355afe82012-10-08 11:17:32 -0600238 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800239
240 event = kvm_alloc_init_event(key);
241 if (!event)
242 return NULL;
243
244 list_add(&event->hash_entry, head);
245 return event;
246}
247
Xiao Guangrong37860632012-11-15 14:17:01 +0800248static bool handle_begin_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600249 struct vcpu_event_record *vcpu_record,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800250 struct event_key *key, u64 timestamp)
251{
252 struct kvm_event *event = NULL;
253
254 if (key->key != INVALID_KEY)
David Ahernde332ac2012-10-02 22:09:53 -0600255 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800256
257 vcpu_record->last_event = event;
258 vcpu_record->start_time = timestamp;
259 return true;
260}
261
262static void
263kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
264{
265 kvm_stats->time += time_diff;
266 update_stats(&kvm_stats->stats, time_diff);
267}
268
269static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
270{
271 struct kvm_event_stats *kvm_stats = &event->total;
272
273 if (vcpu_id != -1)
274 kvm_stats = &event->vcpu[vcpu_id];
275
276 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
277 avg_stats(&kvm_stats->stats));
278}
279
280static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
281 u64 time_diff)
282{
David Ahern2aa8eab2012-10-08 11:17:35 -0600283 if (vcpu_id == -1) {
284 kvm_update_event_stats(&event->total, time_diff);
285 return true;
286 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800287
288 if (!kvm_event_expand(event, vcpu_id))
289 return false;
290
291 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
292 return true;
293}
294
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400295static bool is_child_event(struct perf_kvm_stat *kvm,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200296 struct evsel *evsel,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400297 struct perf_sample *sample,
298 struct event_key *key)
299{
300 struct child_event_ops *child_ops;
301
302 child_ops = kvm->events_ops->child_ops;
303
304 if (!child_ops)
305 return false;
306
307 for (; child_ops->name; child_ops++) {
308 if (!strcmp(evsel->name, child_ops->name)) {
309 child_ops->get_key(evsel, sample, key);
310 return true;
311 }
312 }
313
314 return false;
315}
316
317static bool handle_child_event(struct perf_kvm_stat *kvm,
318 struct vcpu_event_record *vcpu_record,
319 struct event_key *key,
320 struct perf_sample *sample __maybe_unused)
321{
322 struct kvm_event *event = NULL;
323
324 if (key->key != INVALID_KEY)
325 event = find_create_kvm_event(kvm, key);
326
327 vcpu_record->last_event = event;
328
329 return true;
330}
331
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400332static bool skip_event(const char *event)
333{
334 const char * const *skip_events;
335
336 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
337 if (!strcmp(event, *skip_events))
338 return true;
339
340 return false;
341}
342
Xiao Guangrong37860632012-11-15 14:17:01 +0800343static bool handle_end_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600344 struct vcpu_event_record *vcpu_record,
345 struct event_key *key,
David Ahern70f7b4a2013-08-07 21:56:38 -0400346 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800347{
348 struct kvm_event *event;
349 u64 time_begin, time_diff;
David Ahern2aa8eab2012-10-08 11:17:35 -0600350 int vcpu;
351
352 if (kvm->trace_vcpu == -1)
353 vcpu = -1;
354 else
355 vcpu = vcpu_record->vcpu_id;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800356
357 event = vcpu_record->last_event;
358 time_begin = vcpu_record->start_time;
359
360 /* The begin event is not caught. */
361 if (!time_begin)
362 return true;
363
364 /*
365 * In some case, the 'begin event' only records the start timestamp,
366 * the actual event is recognized in the 'end event' (e.g. mmio-event).
367 */
368
369 /* Both begin and end events did not get the key. */
370 if (!event && key->key == INVALID_KEY)
371 return true;
372
373 if (!event)
David Ahernde332ac2012-10-02 22:09:53 -0600374 event = find_create_kvm_event(kvm, key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800375
376 if (!event)
377 return false;
378
379 vcpu_record->last_event = NULL;
380 vcpu_record->start_time = 0;
381
David Ahern1afe1d12013-08-05 21:41:34 -0400382 /* seems to happen once in a while during live mode */
David Ahern70f7b4a2013-08-07 21:56:38 -0400383 if (sample->time < time_begin) {
David Ahern1afe1d12013-08-05 21:41:34 -0400384 pr_debug("End time before begin time; skipping event.\n");
385 return true;
386 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800387
David Ahern70f7b4a2013-08-07 21:56:38 -0400388 time_diff = sample->time - time_begin;
389
390 if (kvm->duration && time_diff > kvm->duration) {
Hemant Kumar162607e2016-01-28 12:03:04 +0530391 char decode[decode_str_len];
David Ahern70f7b4a2013-08-07 21:56:38 -0400392
393 kvm->events_ops->decode_key(kvm, &event->key, decode);
Alexander Yarygin54c801ff72014-07-03 18:29:06 +0400394 if (!skip_event(decode)) {
David Ahern70f7b4a2013-08-07 21:56:38 -0400395 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
396 sample->time, sample->pid, vcpu_record->vcpu_id,
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300397 decode, time_diff / NSEC_PER_USEC);
David Ahern70f7b4a2013-08-07 21:56:38 -0400398 }
399 }
400
David Ahern2aa8eab2012-10-08 11:17:35 -0600401 return update_kvm_event(event, vcpu, time_diff);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800402}
403
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300404static
405struct vcpu_event_record *per_vcpu_record(struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200406 struct evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300407 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800408{
409 /* Only kvm_entry records vcpu id. */
Namhyung Kim69e865c2014-10-06 09:46:02 +0900410 if (!thread__priv(thread) && kvm_entry_event(evsel)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800411 struct vcpu_event_record *vcpu_record;
412
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300413 vcpu_record = zalloc(sizeof(*vcpu_record));
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800414 if (!vcpu_record) {
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300415 pr_err("%s: Not enough memory\n", __func__);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800416 return NULL;
417 }
418
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300419 vcpu_record->vcpu_id = evsel__intval(evsel, sample, vcpu_id_str);
Namhyung Kim69e865c2014-10-06 09:46:02 +0900420 thread__set_priv(thread, vcpu_record);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800421 }
422
Namhyung Kim69e865c2014-10-06 09:46:02 +0900423 return thread__priv(thread);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800424}
425
Xiao Guangrong37860632012-11-15 14:17:01 +0800426static bool handle_kvm_event(struct perf_kvm_stat *kvm,
David Ahernde332ac2012-10-02 22:09:53 -0600427 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200428 struct evsel *evsel,
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300429 struct perf_sample *sample)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800430{
431 struct vcpu_event_record *vcpu_record;
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400432 struct event_key key = { .key = INVALID_KEY,
433 .exit_reasons = kvm->exit_reasons };
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800434
Arnaldo Carvalho de Melo14907e72012-09-21 16:10:26 -0300435 vcpu_record = per_vcpu_record(thread, evsel, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800436 if (!vcpu_record)
437 return true;
438
David Ahern2aa8eab2012-10-08 11:17:35 -0600439 /* only process events for vcpus user cares about */
440 if ((kvm->trace_vcpu != -1) &&
441 (kvm->trace_vcpu != vcpu_record->vcpu_id))
442 return true;
443
David Ahernde332ac2012-10-02 22:09:53 -0600444 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
445 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800446
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +0400447 if (is_child_event(kvm, evsel, sample, &key))
448 return handle_child_event(kvm, vcpu_record, &key, sample);
449
David Ahernde332ac2012-10-02 22:09:53 -0600450 if (kvm->events_ops->is_end_event(evsel, sample, &key))
David Ahern70f7b4a2013-08-07 21:56:38 -0400451 return handle_end_event(kvm, vcpu_record, &key, sample);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800452
453 return true;
454}
455
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800456#define GET_EVENT_KEY(func, field) \
457static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
458{ \
459 if (vcpu == -1) \
460 return event->total.field; \
461 \
462 if (vcpu >= event->max_vcpu) \
463 return 0; \
464 \
465 return event->vcpu[vcpu].field; \
466}
467
468#define COMPARE_EVENT_KEY(func, field) \
469GET_EVENT_KEY(func, field) \
470static int compare_kvm_event_ ## func(struct kvm_event *one, \
471 struct kvm_event *two, int vcpu)\
472{ \
473 return get_event_ ##func(one, vcpu) > \
474 get_event_ ##func(two, vcpu); \
475}
476
477GET_EVENT_KEY(time, time);
478COMPARE_EVENT_KEY(count, stats.n);
479COMPARE_EVENT_KEY(mean, stats.mean);
David Ahern62d04db2013-08-05 21:41:35 -0400480GET_EVENT_KEY(max, stats.max);
481GET_EVENT_KEY(min, stats.min);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800482
483#define DEF_SORT_NAME_KEY(name, compare_key) \
484 { #name, compare_kvm_event_ ## compare_key }
485
486static struct kvm_event_key keys[] = {
487 DEF_SORT_NAME_KEY(sample, count),
488 DEF_SORT_NAME_KEY(time, mean),
489 { NULL, NULL }
490};
491
Xiao Guangrong37860632012-11-15 14:17:01 +0800492static bool select_key(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800493{
494 int i;
495
496 for (i = 0; keys[i].name; i++) {
David Ahernde332ac2012-10-02 22:09:53 -0600497 if (!strcmp(keys[i].name, kvm->sort_key)) {
498 kvm->compare = keys[i].key;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800499 return true;
500 }
501 }
502
David Ahernde332ac2012-10-02 22:09:53 -0600503 pr_err("Unknown compare key:%s\n", kvm->sort_key);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800504 return false;
505}
506
David Ahernde332ac2012-10-02 22:09:53 -0600507static void insert_to_result(struct rb_root *result, struct kvm_event *event,
508 key_cmp_fun bigger, int vcpu)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800509{
David Ahernde332ac2012-10-02 22:09:53 -0600510 struct rb_node **rb = &result->rb_node;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800511 struct rb_node *parent = NULL;
512 struct kvm_event *p;
513
514 while (*rb) {
515 p = container_of(*rb, struct kvm_event, rb);
516 parent = *rb;
517
518 if (bigger(event, p, vcpu))
519 rb = &(*rb)->rb_left;
520 else
521 rb = &(*rb)->rb_right;
522 }
523
524 rb_link_node(&event->rb, parent, rb);
David Ahernde332ac2012-10-02 22:09:53 -0600525 rb_insert_color(&event->rb, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800526}
527
Xiao Guangrong37860632012-11-15 14:17:01 +0800528static void
529update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800530{
David Ahernde332ac2012-10-02 22:09:53 -0600531 int vcpu = kvm->trace_vcpu;
532
533 kvm->total_count += get_event_count(event, vcpu);
534 kvm->total_time += get_event_time(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800535}
536
537static bool event_is_valid(struct kvm_event *event, int vcpu)
538{
539 return !!get_event_count(event, vcpu);
540}
541
Xiao Guangrong37860632012-11-15 14:17:01 +0800542static void sort_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800543{
544 unsigned int i;
David Ahernde332ac2012-10-02 22:09:53 -0600545 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800546 struct kvm_event *event;
547
David Ahern355afe82012-10-08 11:17:32 -0600548 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
549 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800550 if (event_is_valid(event, vcpu)) {
David Ahernde332ac2012-10-02 22:09:53 -0600551 update_total_count(kvm, event);
552 insert_to_result(&kvm->result, event,
553 kvm->compare, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800554 }
David Ahern355afe82012-10-08 11:17:32 -0600555 }
556 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800557}
558
559/* returns left most element of result, and erase it */
David Ahernde332ac2012-10-02 22:09:53 -0600560static struct kvm_event *pop_from_result(struct rb_root *result)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800561{
David Ahernde332ac2012-10-02 22:09:53 -0600562 struct rb_node *node = rb_first(result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800563
564 if (!node)
565 return NULL;
566
David Ahernde332ac2012-10-02 22:09:53 -0600567 rb_erase(node, result);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800568 return container_of(node, struct kvm_event, rb);
569}
570
David Ahern1afe1d12013-08-05 21:41:34 -0400571static void print_vcpu_info(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800572{
David Ahern1afe1d12013-08-05 21:41:34 -0400573 int vcpu = kvm->trace_vcpu;
574
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800575 pr_info("Analyze events for ");
576
Alexander Yarygin1f3e5b52014-09-01 17:44:55 +0400577 if (kvm->opts.target.system_wide)
578 pr_info("all VMs, ");
579 else if (kvm->opts.target.pid)
580 pr_info("pid(s) %s, ", kvm->opts.target.pid);
581 else
582 pr_info("dazed and confused on what is monitored, ");
David Ahern1afe1d12013-08-05 21:41:34 -0400583
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800584 if (vcpu == -1)
585 pr_info("all VCPUs:\n\n");
586 else
587 pr_info("VCPU %d:\n\n", vcpu);
588}
589
David Ahern1afe1d12013-08-05 21:41:34 -0400590static void show_timeofday(void)
591{
592 char date[64];
593 struct timeval tv;
594 struct tm ltime;
595
596 gettimeofday(&tv, NULL);
597 if (localtime_r(&tv.tv_sec, &ltime)) {
598 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
599 pr_info("%s.%06ld", date, tv.tv_usec);
600 } else
601 pr_info("00:00:00.000000");
602
603 return;
604}
605
Xiao Guangrong37860632012-11-15 14:17:01 +0800606static void print_result(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800607{
Hemant Kumar162607e2016-01-28 12:03:04 +0530608 char decode[decode_str_len];
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800609 struct kvm_event *event;
David Ahernde332ac2012-10-02 22:09:53 -0600610 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800611
David Ahern1afe1d12013-08-05 21:41:34 -0400612 if (kvm->live) {
613 puts(CONSOLE_CLEAR);
614 show_timeofday();
615 }
616
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800617 pr_info("\n\n");
David Ahern1afe1d12013-08-05 21:41:34 -0400618 print_vcpu_info(kvm);
Hemant Kumar162607e2016-01-28 12:03:04 +0530619 pr_info("%*s ", decode_str_len, kvm->events_ops->name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800620 pr_info("%10s ", "Samples");
621 pr_info("%9s ", "Samples%");
622
623 pr_info("%9s ", "Time%");
Christian Borntraegerb048a242014-07-31 13:13:51 +0200624 pr_info("%11s ", "Min Time");
625 pr_info("%11s ", "Max Time");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800626 pr_info("%16s ", "Avg time");
627 pr_info("\n\n");
628
David Ahernde332ac2012-10-02 22:09:53 -0600629 while ((event = pop_from_result(&kvm->result))) {
David Ahern62d04db2013-08-05 21:41:35 -0400630 u64 ecount, etime, max, min;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800631
632 ecount = get_event_count(event, vcpu);
633 etime = get_event_time(event, vcpu);
David Ahern62d04db2013-08-05 21:41:35 -0400634 max = get_event_max(event, vcpu);
635 min = get_event_min(event, vcpu);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800636
David Ahernde332ac2012-10-02 22:09:53 -0600637 kvm->events_ops->decode_key(kvm, &event->key, decode);
Hemant Kumar162607e2016-01-28 12:03:04 +0530638 pr_info("%*s ", decode_str_len, decode);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800639 pr_info("%10llu ", (unsigned long long)ecount);
David Ahernde332ac2012-10-02 22:09:53 -0600640 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
641 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300642 pr_info("%9.2fus ", (double)min / NSEC_PER_USEC);
643 pr_info("%9.2fus ", (double)max / NSEC_PER_USEC);
644 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800645 kvm_event_rel_stddev(vcpu, event));
646 pr_info("\n");
647 }
648
David Aherne4f76372012-10-08 11:17:34 -0600649 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
Arnaldo Carvalho de Meloc05a6e12016-08-08 15:10:44 -0300650 kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC);
David Ahern1afe1d12013-08-05 21:41:34 -0400651
652 if (kvm->lost_events)
653 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
654}
655
David Ahern87419c92013-10-29 10:43:16 -0600656#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400657static int process_lost_event(struct perf_tool *tool,
658 union perf_event *event __maybe_unused,
659 struct perf_sample *sample __maybe_unused,
660 struct machine *machine __maybe_unused)
661{
662 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
663
664 kvm->lost_events++;
665 return 0;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800666}
David Ahern87419c92013-10-29 10:43:16 -0600667#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800668
David Ahern2e73f002013-08-05 21:41:37 -0400669static bool skip_sample(struct perf_kvm_stat *kvm,
670 struct perf_sample *sample)
671{
672 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
673 return true;
674
675 return false;
676}
677
David Ahernde332ac2012-10-02 22:09:53 -0600678static int process_sample_event(struct perf_tool *tool,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800679 union perf_event *event,
680 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200681 struct evsel *evsel,
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800682 struct machine *machine)
683{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300684 int err = 0;
David Ahern2e73f002013-08-05 21:41:37 -0400685 struct thread *thread;
Xiao Guangrong37860632012-11-15 14:17:01 +0800686 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
687 tool);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800688
David Ahern2e73f002013-08-05 21:41:37 -0400689 if (skip_sample(kvm, sample))
690 return 0;
691
Adrian Hunter314add62013-08-27 11:23:03 +0300692 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800693 if (thread == NULL) {
694 pr_debug("problem processing %d event, skipping it.\n",
695 event->header.type);
696 return -1;
697 }
698
David Ahernde332ac2012-10-02 22:09:53 -0600699 if (!handle_kvm_event(kvm, thread, evsel, sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300700 err = -1;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800701
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300702 thread__put(thread);
703 return err;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800704}
705
David Ahern1afe1d12013-08-05 21:41:34 -0400706static int cpu_isa_config(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800707{
Arnaldo Carvalho de Melof67001a2019-09-30 10:55:34 -0300708 char buf[128], *cpuid;
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400709 int err;
David Ahern1afe1d12013-08-05 21:41:34 -0400710
711 if (kvm->live) {
712 err = get_cpuid(buf, sizeof(buf));
713 if (err != 0) {
Arnaldo Carvalho de Melof67001a2019-09-30 10:55:34 -0300714 pr_err("Failed to look up CPU type: %s\n",
715 str_error_r(err, buf, sizeof(buf)));
716 return -err;
David Ahern1afe1d12013-08-05 21:41:34 -0400717 }
718 cpuid = buf;
719 } else
720 cpuid = kvm->session->header.env.cpuid;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800721
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400722 if (!cpuid) {
723 pr_err("Failed to look up CPU type\n");
724 return -EINVAL;
725 }
726
727 err = cpu_isa_init(kvm, cpuid);
728 if (err == -ENOTSUP)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800729 pr_err("CPU %s is not supported.\n", cpuid);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +0800730
Alexander Yarygin65c647a2014-07-03 17:59:51 +0400731 return err;
David Ahern1afe1d12013-08-05 21:41:34 -0400732}
733
734static bool verify_vcpu(int vcpu)
735{
736 if (vcpu != -1 && vcpu < 0) {
737 pr_err("Invalid vcpu:%d.\n", vcpu);
738 return false;
739 }
740
741 return true;
742}
743
David Ahern87419c92013-10-29 10:43:16 -0600744#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -0400745/* keeping the max events to a modest level to keep
746 * the processing of samples per mmap smooth.
747 */
748#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
749
750static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
751 u64 *mmap_time)
752{
Jiri Olsa63503db2019-07-21 13:23:52 +0200753 struct evlist *evlist = kvm->evlist;
David Ahern1afe1d12013-08-05 21:41:34 -0400754 union perf_event *event;
Jiri Olsaa5830532019-07-27 20:30:53 +0200755 struct mmap *md;
Jiri Olsa93d10af2017-08-03 13:21:14 +0200756 u64 timestamp;
David Ahern1afe1d12013-08-05 21:41:34 -0400757 s64 n = 0;
758 int err;
759
760 *mmap_time = ULLONG_MAX;
Kan Liang53172f92018-03-01 18:08:58 -0500761 md = &evlist->mmap[idx];
Jiri Olsa7c4d4182019-10-07 14:53:18 +0200762 err = perf_mmap__read_init(&md->core);
Kan Liang53172f92018-03-01 18:08:58 -0500763 if (err < 0)
764 return (err == -EAGAIN) ? 0 : -1;
765
Jiri Olsa151ed5d2019-10-07 14:53:20 +0200766 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Kan Liang53172f92018-03-01 18:08:58 -0500767 err = perf_evlist__parse_sample_timestamp(evlist, event, &timestamp);
David Ahern1afe1d12013-08-05 21:41:34 -0400768 if (err) {
Jiri Olsa7728fa02019-10-07 14:53:17 +0200769 perf_mmap__consume(&md->core);
David Ahern1afe1d12013-08-05 21:41:34 -0400770 pr_err("Failed to parse sample\n");
771 return -1;
772 }
773
Jiri Olsa93d10af2017-08-03 13:21:14 +0200774 err = perf_session__queue_event(kvm->session, event, timestamp, 0);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800775 /*
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300776 * FIXME: Here we can't consume the event, as perf_session__queue_event will
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800777 * point to it, and it'll get possibly overwritten by the kernel.
778 */
Jiri Olsa7728fa02019-10-07 14:53:17 +0200779 perf_mmap__consume(&md->core);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800780
David Ahern1afe1d12013-08-05 21:41:34 -0400781 if (err) {
782 pr_err("Failed to enqueue sample: %d\n", err);
783 return -1;
784 }
785
786 /* save time stamp of our first sample for this mmap */
787 if (n == 0)
Jiri Olsa93d10af2017-08-03 13:21:14 +0200788 *mmap_time = timestamp;
David Ahern1afe1d12013-08-05 21:41:34 -0400789
790 /* limit events per mmap handled all at once */
791 n++;
792 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
793 break;
794 }
795
Jiri Olsa32fdc2c2019-10-07 14:53:19 +0200796 perf_mmap__read_done(&md->core);
David Ahern1afe1d12013-08-05 21:41:34 -0400797 return n;
798}
799
800static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
801{
802 int i, err, throttled = 0;
803 s64 n, ntotal = 0;
804 u64 flush_time = ULLONG_MAX, mmap_time;
805
Jiri Olsac976ee12019-07-30 13:04:59 +0200806 for (i = 0; i < kvm->evlist->core.nr_mmaps; i++) {
David Ahern1afe1d12013-08-05 21:41:34 -0400807 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
808 if (n < 0)
809 return -1;
810
811 /* flush time is going to be the minimum of all the individual
812 * mmap times. Essentially, we flush all the samples queued up
813 * from the last pass under our minimal start time -- that leaves
814 * a very small race for samples to come in with a lower timestamp.
815 * The ioctl to return the perf_clock timestamp should close the
816 * race entirely.
817 */
818 if (mmap_time < flush_time)
819 flush_time = mmap_time;
820
821 ntotal += n;
822 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
823 throttled = 1;
824 }
825
826 /* flush queue after each round in which we processed events */
827 if (ntotal) {
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300828 struct ordered_events *oe = &kvm->session->ordered_events;
829
830 oe->next_flush = flush_time;
831 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
David Ahern1afe1d12013-08-05 21:41:34 -0400832 if (err) {
833 if (kvm->lost_events)
834 pr_info("\nLost events: %" PRIu64 "\n\n",
835 kvm->lost_events);
836 return err;
837 }
838 }
839
840 return throttled;
841}
842
843static volatile int done;
844
845static void sig_handler(int sig __maybe_unused)
846{
847 done = 1;
848}
849
850static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
851{
852 struct itimerspec new_value;
853 int rc = -1;
854
855 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
856 if (kvm->timerfd < 0) {
857 pr_err("timerfd_create failed\n");
858 goto out;
859 }
860
861 new_value.it_value.tv_sec = kvm->display_time;
862 new_value.it_value.tv_nsec = 0;
863 new_value.it_interval.tv_sec = kvm->display_time;
864 new_value.it_interval.tv_nsec = 0;
865
866 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
867 pr_err("timerfd_settime failed: %d\n", errno);
868 close(kvm->timerfd);
869 goto out;
870 }
871
872 rc = 0;
873out:
874 return rc;
875}
876
877static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
878{
879 uint64_t c;
880 int rc;
881
882 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
883 if (rc < 0) {
884 if (errno == EAGAIN)
885 return 0;
886
887 pr_err("Failed to read timer fd: %d\n", errno);
888 return -1;
889 }
890
891 if (rc != sizeof(uint64_t)) {
892 pr_err("Error reading timer fd - invalid size returned\n");
893 return -1;
894 }
895
896 if (c != 1)
897 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
898
899 /* update display */
900 sort_result(kvm);
901 print_result(kvm);
902
903 /* reset counts */
904 clear_events_cache_stats(kvm->kvm_events_cache);
905 kvm->total_count = 0;
906 kvm->total_time = 0;
907 kvm->lost_events = 0;
908
909 return 0;
910}
911
912static int fd_set_nonblock(int fd)
913{
914 long arg = 0;
915
916 arg = fcntl(fd, F_GETFL);
917 if (arg < 0) {
918 pr_err("Failed to get current flags for fd %d\n", fd);
919 return -1;
920 }
921
922 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
923 pr_err("Failed to set non-block option on fd %d\n", fd);
924 return -1;
925 }
926
927 return 0;
928}
929
Jiri Olsad5b41302014-08-11 10:50:09 +0200930static int perf_kvm__handle_stdin(void)
David Ahern1afe1d12013-08-05 21:41:34 -0400931{
932 int c;
933
David Ahern1afe1d12013-08-05 21:41:34 -0400934 c = getc(stdin);
David Ahern1afe1d12013-08-05 21:41:34 -0400935 if (c == 'q')
936 return 1;
937
938 return 0;
939}
940
941static int kvm_events_live_report(struct perf_kvm_stat *kvm)
942{
Jiri Olsa1ca72262014-10-01 18:00:29 +0200943 int nr_stdin, ret, err = -EINVAL;
Jiri Olsad5b41302014-08-11 10:50:09 +0200944 struct termios save;
David Ahern1afe1d12013-08-05 21:41:34 -0400945
946 /* live flag must be set first */
947 kvm->live = true;
948
949 ret = cpu_isa_config(kvm);
950 if (ret < 0)
951 return ret;
952
953 if (!verify_vcpu(kvm->trace_vcpu) ||
954 !select_key(kvm) ||
955 !register_kvm_events_ops(kvm)) {
956 goto out;
957 }
958
Jiri Olsad5b41302014-08-11 10:50:09 +0200959 set_term_quiet_input(&save);
David Ahern1afe1d12013-08-05 21:41:34 -0400960 init_kvm_event_record(kvm);
961
David Ahern1afe1d12013-08-05 21:41:34 -0400962 signal(SIGINT, sig_handler);
963 signal(SIGTERM, sig_handler);
964
David Ahern1afe1d12013-08-05 21:41:34 -0400965 /* add timer fd */
966 if (perf_kvm__timerfd_create(kvm) < 0) {
967 err = -1;
968 goto out;
969 }
970
Jiri Olsaf4009e72019-08-16 16:00:45 +0200971 if (evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300972 goto out;
973
Jiri Olsaf4009e72019-08-16 16:00:45 +0200974 nr_stdin = evlist__add_pollfd(kvm->evlist, fileno(stdin));
Jiri Olsa0cae0132014-10-01 18:00:28 +0200975 if (nr_stdin < 0)
Arnaldo Carvalho de Melo0a04c9e2014-08-18 17:12:30 -0300976 goto out;
977
David Ahern1afe1d12013-08-05 21:41:34 -0400978 if (fd_set_nonblock(fileno(stdin)) != 0)
979 goto out;
980
981 /* everything is good - enable the events and process */
Jiri Olsa1c87f162019-07-21 13:24:08 +0200982 evlist__enable(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -0400983
984 while (!done) {
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200985 struct fdarray *fda = &kvm->evlist->core.pollfd;
David Ahern1afe1d12013-08-05 21:41:34 -0400986 int rc;
987
988 rc = perf_kvm__mmap_read(kvm);
989 if (rc < 0)
990 break;
991
992 err = perf_kvm__handle_timerfd(kvm);
993 if (err)
994 goto out;
995
Jiri Olsa1ca72262014-10-01 18:00:29 +0200996 if (fda->entries[nr_stdin].revents & POLLIN)
Jiri Olsad5b41302014-08-11 10:50:09 +0200997 done = perf_kvm__handle_stdin();
David Ahern1afe1d12013-08-05 21:41:34 -0400998
999 if (!rc && !done)
Igor Lubashev4bfbcf32019-10-22 21:54:53 -04001000 err = evlist__poll(kvm->evlist, 100);
David Ahern1afe1d12013-08-05 21:41:34 -04001001 }
1002
Jiri Olsae74676d2019-07-21 13:24:09 +02001003 evlist__disable(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001004
1005 if (err == 0) {
1006 sort_result(kvm);
1007 print_result(kvm);
1008 }
1009
1010out:
1011 if (kvm->timerfd >= 0)
1012 close(kvm->timerfd);
1013
Jiri Olsad5b41302014-08-11 10:50:09 +02001014 tcsetattr(0, TCSAFLUSH, &save);
David Ahern1afe1d12013-08-05 21:41:34 -04001015 return err;
1016}
1017
1018static int kvm_live_open_events(struct perf_kvm_stat *kvm)
1019{
1020 int err, rc = -1;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001021 struct evsel *pos;
Jiri Olsa63503db2019-07-21 13:23:52 +02001022 struct evlist *evlist = kvm->evlist;
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001023 char sbuf[STRERR_BUFSIZE];
David Ahern1afe1d12013-08-05 21:41:34 -04001024
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -03001025 perf_evlist__config(evlist, &kvm->opts, NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001026
1027 /*
1028 * Note: exclude_{guest,host} do not apply here.
1029 * This command processes KVM tracepoints from host only
1030 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001031 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001032 struct perf_event_attr *attr = &pos->core.attr;
David Ahern1afe1d12013-08-05 21:41:34 -04001033
1034 /* make sure these *are* set */
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001035 evsel__set_sample_bit(pos, TID);
1036 evsel__set_sample_bit(pos, TIME);
1037 evsel__set_sample_bit(pos, CPU);
1038 evsel__set_sample_bit(pos, RAW);
David Ahern1afe1d12013-08-05 21:41:34 -04001039 /* make sure these are *not*; want as small a sample as possible */
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -03001040 evsel__reset_sample_bit(pos, PERIOD);
1041 evsel__reset_sample_bit(pos, IP);
1042 evsel__reset_sample_bit(pos, CALLCHAIN);
1043 evsel__reset_sample_bit(pos, ADDR);
1044 evsel__reset_sample_bit(pos, READ);
David Ahern1afe1d12013-08-05 21:41:34 -04001045 attr->mmap = 0;
1046 attr->comm = 0;
1047 attr->task = 0;
1048
1049 attr->sample_period = 1;
1050
1051 attr->watermark = 0;
1052 attr->wakeup_events = 1000;
1053
1054 /* will enable all once we are ready */
1055 attr->disabled = 1;
1056 }
1057
Jiri Olsa474ddc42019-07-21 13:24:06 +02001058 err = evlist__open(evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001059 if (err < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001060 printf("Couldn't create the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001061 str_error_r(errno, sbuf, sizeof(sbuf)));
David Ahern1afe1d12013-08-05 21:41:34 -04001062 goto out;
1063 }
1064
Jiri Olsa9521b5f2019-07-28 12:45:35 +02001065 if (evlist__mmap(evlist, kvm->opts.mmap_pages) < 0) {
Masami Hiramatsuf9f33fd2014-08-14 02:22:51 +00001066 ui__error("Failed to mmap the events: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001067 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsa750b4ed2019-07-21 13:24:07 +02001068 evlist__close(evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001069 goto out;
1070 }
1071
1072 rc = 0;
1073
1074out:
1075 return rc;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001076}
David Ahern87419c92013-10-29 10:43:16 -06001077#endif
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001078
Xiao Guangrong37860632012-11-15 14:17:01 +08001079static int read_events(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001080{
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001081 int ret;
1082
David Ahernde332ac2012-10-02 22:09:53 -06001083 struct perf_tool eops = {
1084 .sample = process_sample_event,
1085 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05301086 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001087 .ordered_events = true,
David Ahernde332ac2012-10-02 22:09:53 -06001088 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001089 struct perf_data file = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001090 .path = kvm->file_name,
1091 .mode = PERF_DATA_MODE_READ,
1092 .force = kvm->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001093 };
David Ahernde332ac2012-10-02 22:09:53 -06001094
1095 kvm->tool = eops;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001096 kvm->session = perf_session__new(&file, false, &kvm->tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05301097 if (IS_ERR(kvm->session)) {
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001098 pr_err("Initializing perf session failed\n");
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05301099 return PTR_ERR(kvm->session);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001100 }
1101
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001102 symbol__init(&kvm->session->header.env);
Namhyung Kim14d37f32014-08-12 15:40:39 +09001103
Taeung Song41b98362015-07-01 00:18:44 +09001104 if (!perf_session__has_traces(kvm->session, "kvm record")) {
1105 ret = -EINVAL;
1106 goto out_delete;
1107 }
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001108
1109 /*
1110 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1111 * traced in the old kernel.
1112 */
David Ahern1afe1d12013-08-05 21:41:34 -04001113 ret = cpu_isa_config(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001114 if (ret < 0)
Taeung Song41b98362015-07-01 00:18:44 +09001115 goto out_delete;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001116
Taeung Song41b98362015-07-01 00:18:44 +09001117 ret = perf_session__process_events(kvm->session);
1118
1119out_delete:
1120 perf_session__delete(kvm->session);
1121 return ret;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001122}
1123
David Ahern2e73f002013-08-05 21:41:37 -04001124static int parse_target_str(struct perf_kvm_stat *kvm)
1125{
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001126 if (kvm->opts.target.pid) {
1127 kvm->pid_list = intlist__new(kvm->opts.target.pid);
David Ahern2e73f002013-08-05 21:41:37 -04001128 if (kvm->pid_list == NULL) {
1129 pr_err("Error parsing process id string\n");
1130 return -EINVAL;
1131 }
1132 }
1133
1134 return 0;
1135}
1136
Xiao Guangrong37860632012-11-15 14:17:01 +08001137static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001138{
1139 int ret = -EINVAL;
David Ahernde332ac2012-10-02 22:09:53 -06001140 int vcpu = kvm->trace_vcpu;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001141
David Ahern2e73f002013-08-05 21:41:37 -04001142 if (parse_target_str(kvm) != 0)
1143 goto exit;
1144
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001145 if (!verify_vcpu(vcpu))
1146 goto exit;
1147
David Ahernde332ac2012-10-02 22:09:53 -06001148 if (!select_key(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001149 goto exit;
1150
David Ahernde332ac2012-10-02 22:09:53 -06001151 if (!register_kvm_events_ops(kvm))
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001152 goto exit;
1153
David Ahernde332ac2012-10-02 22:09:53 -06001154 init_kvm_event_record(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001155 setup_pager();
1156
David Ahernde332ac2012-10-02 22:09:53 -06001157 ret = read_events(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001158 if (ret)
1159 goto exit;
1160
David Ahernde332ac2012-10-02 22:09:53 -06001161 sort_result(kvm);
1162 print_result(kvm);
1163
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001164exit:
1165 return ret;
1166}
1167
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001168#define STRDUP_FAIL_EXIT(s) \
1169 ({ char *_p; \
1170 _p = strdup(s); \
1171 if (!_p) \
1172 return -ENOMEM; \
1173 _p; \
1174 })
1175
Hemant Kumar066d3592016-01-28 12:03:06 +05301176int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
1177{
1178 return 0;
1179}
1180
Xiao Guangrong37860632012-11-15 14:17:01 +08001181static int
1182kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001183{
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001184 unsigned int rec_argc, i, j, events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001185 const char **rec_argv;
David Ahern8fdd84c2013-08-02 14:05:42 -06001186 const char * const record_args[] = {
1187 "record",
1188 "-R",
David Ahern8fdd84c2013-08-02 14:05:42 -06001189 "-m", "1024",
1190 "-c", "1",
1191 };
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001192 const char * const kvm_stat_record_usage[] = {
1193 "perf kvm stat record [<options>]",
1194 NULL
1195 };
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001196 const char * const *events_tp;
Hemant Kumar066d3592016-01-28 12:03:06 +05301197 int ret;
1198
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001199 events_tp_size = 0;
Hemant Kumar066d3592016-01-28 12:03:06 +05301200 ret = setup_kvm_events_tp(kvm);
1201 if (ret < 0) {
1202 pr_err("Unable to setup the kvm tracepoints\n");
1203 return ret;
1204 }
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001205
1206 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1207 events_tp_size++;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001208
David Ahern8fdd84c2013-08-02 14:05:42 -06001209 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001210 2 * events_tp_size;
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001211 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1212
1213 if (rec_argv == NULL)
1214 return -ENOMEM;
1215
1216 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1217 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1218
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001219 for (j = 0; j < events_tp_size; j++) {
David Ahern8fdd84c2013-08-02 14:05:42 -06001220 rec_argv[i++] = "-e";
1221 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1222 }
1223
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001224 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
David Ahernde332ac2012-10-02 22:09:53 -06001225 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001226
1227 for (j = 1; j < (unsigned int)argc; j++, i++)
1228 rec_argv[i] = argv[j];
1229
Namhyung Kimf45d20f2014-10-23 00:15:47 +09001230 set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
1231 set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
1232 set_option_flag(record_options, 'R', "raw-samples", PARSE_OPT_HIDDEN);
1233
1234 set_option_flag(record_options, 'F', "freq", PARSE_OPT_DISABLED);
1235 set_option_flag(record_options, 0, "group", PARSE_OPT_DISABLED);
1236 set_option_flag(record_options, 'g', NULL, PARSE_OPT_DISABLED);
1237 set_option_flag(record_options, 0, "call-graph", PARSE_OPT_DISABLED);
1238 set_option_flag(record_options, 'd', "data", PARSE_OPT_DISABLED);
1239 set_option_flag(record_options, 'T', "timestamp", PARSE_OPT_DISABLED);
1240 set_option_flag(record_options, 'P', "period", PARSE_OPT_DISABLED);
1241 set_option_flag(record_options, 'n', "no-samples", PARSE_OPT_DISABLED);
1242 set_option_flag(record_options, 'N', "no-buildid-cache", PARSE_OPT_DISABLED);
1243 set_option_flag(record_options, 'B', "no-buildid", PARSE_OPT_DISABLED);
1244 set_option_flag(record_options, 'G', "cgroup", PARSE_OPT_DISABLED);
1245 set_option_flag(record_options, 'b', "branch-any", PARSE_OPT_DISABLED);
1246 set_option_flag(record_options, 'j', "branch-filter", PARSE_OPT_DISABLED);
1247 set_option_flag(record_options, 'W', "weight", PARSE_OPT_DISABLED);
1248 set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
1249
1250 record_usage = kvm_stat_record_usage;
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001251 return cmd_record(i, rec_argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001252}
1253
Xiao Guangrong37860632012-11-15 14:17:01 +08001254static int
1255kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001256{
David Ahernde332ac2012-10-02 22:09:53 -06001257 const struct option kvm_events_report_options[] = {
1258 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001259 "event for reporting: vmexit, "
1260 "mmio (x86 only), ioport (x86 only)"),
David Ahernde332ac2012-10-02 22:09:53 -06001261 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1262 "vcpu id to report"),
1263 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1264 "key for sorting: sample(sort by samples number)"
1265 " time (sort by avg time)"),
Alexander Yarygin3ae4a762014-09-01 17:44:53 +04001266 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
David Ahern2e73f002013-08-05 21:41:37 -04001267 "analyze events only for given process id(s)"),
Yunlong Song8cc5ec1f2015-04-02 21:47:13 +08001268 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
David Ahernde332ac2012-10-02 22:09:53 -06001269 OPT_END()
1270 };
1271
1272 const char * const kvm_events_report_usage[] = {
1273 "perf kvm stat report [<options>]",
1274 NULL
1275 };
1276
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001277 if (argc) {
1278 argc = parse_options(argc, argv,
1279 kvm_events_report_options,
1280 kvm_events_report_usage, 0);
1281 if (argc)
1282 usage_with_options(kvm_events_report_usage,
1283 kvm_events_report_options);
1284 }
1285
Alexander Yaryginf1819572014-09-01 17:44:54 +04001286 if (!kvm->opts.target.pid)
1287 kvm->opts.target.system_wide = true;
1288
David Ahernde332ac2012-10-02 22:09:53 -06001289 return kvm_events_report_vcpu(kvm);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001290}
1291
David Ahern87419c92013-10-29 10:43:16 -06001292#ifdef HAVE_TIMERFD_SUPPORT
Jiri Olsa63503db2019-07-21 13:23:52 +02001293static struct evlist *kvm_live_event_list(void)
David Ahern1afe1d12013-08-05 21:41:34 -04001294{
Jiri Olsa63503db2019-07-21 13:23:52 +02001295 struct evlist *evlist;
David Ahern1afe1d12013-08-05 21:41:34 -04001296 char *tp, *name, *sys;
David Ahern1afe1d12013-08-05 21:41:34 -04001297 int err = -1;
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001298 const char * const *events_tp;
David Ahern1afe1d12013-08-05 21:41:34 -04001299
Jiri Olsa0f98b112019-07-21 13:23:55 +02001300 evlist = evlist__new();
David Ahern1afe1d12013-08-05 21:41:34 -04001301 if (evlist == NULL)
1302 return NULL;
1303
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001304 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
David Ahern1afe1d12013-08-05 21:41:34 -04001305
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001306 tp = strdup(*events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001307 if (tp == NULL)
1308 goto out;
1309
1310 /* split tracepoint into subsystem and name */
1311 sys = tp;
1312 name = strchr(tp, ':');
1313 if (name == NULL) {
1314 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001315 *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001316 free(tp);
1317 goto out;
1318 }
1319 *name = '\0';
1320 name++;
1321
1322 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
Alexander Yarygin9daa8122014-07-03 18:29:05 +04001323 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
David Ahern1afe1d12013-08-05 21:41:34 -04001324 free(tp);
1325 goto out;
1326 }
1327
1328 free(tp);
1329 }
1330
1331 err = 0;
1332
1333out:
1334 if (err) {
Jiri Olsac12995a2019-07-21 13:23:56 +02001335 evlist__delete(evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001336 evlist = NULL;
1337 }
1338
1339 return evlist;
1340}
1341
1342static int kvm_events_live(struct perf_kvm_stat *kvm,
1343 int argc, const char **argv)
1344{
1345 char errbuf[BUFSIZ];
1346 int err;
1347
1348 const struct option live_options[] = {
1349 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1350 "record events on existing process id"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001351 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1352 "number of mmap data pages",
1353 perf_evlist__parse_mmap_pages),
David Ahern1afe1d12013-08-05 21:41:34 -04001354 OPT_INCR('v', "verbose", &verbose,
1355 "be more verbose (show counter open errors, etc)"),
1356 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1357 "system-wide collection from all CPUs"),
1358 OPT_UINTEGER('d', "display", &kvm->display_time,
1359 "time in seconds between display updates"),
1360 OPT_STRING(0, "event", &kvm->report_event, "report event",
Alexander Yarygin99d348a2014-12-10 14:28:10 +01001361 "event for reporting: "
1362 "vmexit, mmio (x86 only), ioport (x86 only)"),
David Ahern1afe1d12013-08-05 21:41:34 -04001363 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1364 "vcpu id to report"),
1365 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1366 "key for sorting: sample(sort by samples number)"
1367 " time (sort by avg time)"),
David Ahern70f7b4a2013-08-07 21:56:38 -04001368 OPT_U64(0, "duration", &kvm->duration,
Alexander Yarygin3be8e2a2014-07-03 18:29:07 +04001369 "show events other than"
1370 " HLT (x86 only) or Wait state (s390 only)"
1371 " that take longer than duration usecs"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001372 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04001373 "per thread proc mmap processing timeout in ms"),
David Ahern1afe1d12013-08-05 21:41:34 -04001374 OPT_END()
1375 };
1376 const char * const live_usage[] = {
1377 "perf kvm stat live [<options>]",
1378 NULL
1379 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001380 struct perf_data data = {
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001381 .mode = PERF_DATA_MODE_WRITE,
1382 };
David Ahern1afe1d12013-08-05 21:41:34 -04001383
1384
1385 /* event handling */
1386 kvm->tool.sample = process_sample_event;
1387 kvm->tool.comm = perf_event__process_comm;
1388 kvm->tool.exit = perf_event__process_exit;
1389 kvm->tool.fork = perf_event__process_fork;
1390 kvm->tool.lost = process_lost_event;
Hari Bathinif3b36142017-03-08 02:11:43 +05301391 kvm->tool.namespaces = perf_event__process_namespaces;
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001392 kvm->tool.ordered_events = true;
David Ahern1afe1d12013-08-05 21:41:34 -04001393 perf_tool__fill_defaults(&kvm->tool);
1394
1395 /* set defaults */
1396 kvm->display_time = 1;
1397 kvm->opts.user_interval = 1;
1398 kvm->opts.mmap_pages = 512;
1399 kvm->opts.target.uses_mmap = false;
1400 kvm->opts.target.uid_str = NULL;
1401 kvm->opts.target.uid = UINT_MAX;
1402
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001403 symbol__init(NULL);
David Ahern1afe1d12013-08-05 21:41:34 -04001404 disable_buildid_cache();
1405
1406 use_browser = 0;
David Ahern1afe1d12013-08-05 21:41:34 -04001407
1408 if (argc) {
1409 argc = parse_options(argc, argv, live_options,
1410 live_usage, 0);
1411 if (argc)
1412 usage_with_options(live_usage, live_options);
1413 }
1414
David Ahern70f7b4a2013-08-07 21:56:38 -04001415 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1416
David Ahern1afe1d12013-08-05 21:41:34 -04001417 /*
1418 * target related setups
1419 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001420 err = target__validate(&kvm->opts.target);
David Ahern1afe1d12013-08-05 21:41:34 -04001421 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001422 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
David Ahern1afe1d12013-08-05 21:41:34 -04001423 ui__warning("%s", errbuf);
1424 }
1425
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001426 if (target__none(&kvm->opts.target))
David Ahern1afe1d12013-08-05 21:41:34 -04001427 kvm->opts.target.system_wide = true;
1428
1429
1430 /*
1431 * generate the event list
1432 */
Hemant Kumar066d3592016-01-28 12:03:06 +05301433 err = setup_kvm_events_tp(kvm);
1434 if (err < 0) {
1435 pr_err("Unable to setup the kvm tracepoints\n");
1436 return err;
1437 }
1438
David Ahern1afe1d12013-08-05 21:41:34 -04001439 kvm->evlist = kvm_live_event_list();
1440 if (kvm->evlist == NULL) {
1441 err = -1;
1442 goto out;
1443 }
1444
David Ahern1afe1d12013-08-05 21:41:34 -04001445 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1446 usage_with_options(live_usage, live_options);
1447
1448 /*
1449 * perf session
1450 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001451 kvm->session = perf_session__new(&data, false, &kvm->tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05301452 if (IS_ERR(kvm->session)) {
1453 err = PTR_ERR(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001454 goto out;
1455 }
1456 kvm->session->evlist = kvm->evlist;
1457 perf_session__set_id_hdr_size(kvm->session);
Alexander Yarygin673d6592014-10-03 18:40:12 +04001458 ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001459 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
Jiri Olsa03617c22019-07-21 13:24:42 +02001460 kvm->evlist->core.threads, false, 1);
David Ahern1afe1d12013-08-05 21:41:34 -04001461 err = kvm_live_open_events(kvm);
1462 if (err)
1463 goto out;
1464
1465 err = kvm_events_live_report(kvm);
1466
1467out:
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001468 perf_session__delete(kvm->session);
David Ahern1afe1d12013-08-05 21:41:34 -04001469 kvm->session = NULL;
Jiri Olsac12995a2019-07-21 13:23:56 +02001470 evlist__delete(kvm->evlist);
David Ahern1afe1d12013-08-05 21:41:34 -04001471
1472 return err;
1473}
David Ahern87419c92013-10-29 10:43:16 -06001474#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001475
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001476static void print_kvm_stat_usage(void)
1477{
1478 printf("Usage: perf kvm stat <command>\n\n");
1479
1480 printf("# Available commands:\n");
1481 printf("\trecord: record kvm events\n");
1482 printf("\treport: report statistical data of kvm events\n");
David Ahern1afe1d12013-08-05 21:41:34 -04001483 printf("\tlive: live reporting of statistical data of kvm events\n");
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001484
1485 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1486}
1487
Xiao Guangrong37860632012-11-15 14:17:01 +08001488static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001489{
Xiao Guangrong37860632012-11-15 14:17:01 +08001490 struct perf_kvm_stat kvm = {
1491 .file_name = file_name,
1492
1493 .trace_vcpu = -1,
1494 .report_event = "vmexit",
1495 .sort_key = "sample",
1496
Xiao Guangrong37860632012-11-15 14:17:01 +08001497 };
1498
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001499 if (argc == 1) {
1500 print_kvm_stat_usage();
1501 goto perf_stat;
1502 }
1503
1504 if (!strncmp(argv[1], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001505 return kvm_events_record(&kvm, argc - 1, argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001506
1507 if (!strncmp(argv[1], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001508 return kvm_events_report(&kvm, argc - 1 , argv + 1);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001509
David Ahern87419c92013-10-29 10:43:16 -06001510#ifdef HAVE_TIMERFD_SUPPORT
David Ahern1afe1d12013-08-05 21:41:34 -04001511 if (!strncmp(argv[1], "live", 4))
1512 return kvm_events_live(&kvm, argc - 1 , argv + 1);
David Ahern87419c92013-10-29 10:43:16 -06001513#endif
David Ahern1afe1d12013-08-05 21:41:34 -04001514
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001515perf_stat:
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001516 return cmd_stat(argc, argv);
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001517}
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001518#endif /* HAVE_KVM_STAT_SUPPORT */
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001519
Anju T Sudhakar124eb5f2019-07-18 23:47:48 +05301520int __weak kvm_add_default_arch_event(int *argc __maybe_unused,
1521 const char **argv __maybe_unused)
1522{
1523 return 0;
1524}
1525
Xiao Guangrong37860632012-11-15 14:17:01 +08001526static int __cmd_record(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001527{
Anju T Sudhakar124eb5f2019-07-18 23:47:48 +05301528 int rec_argc, i = 0, j, ret;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001529 const char **rec_argv;
1530
Anju T Sudhakar124eb5f2019-07-18 23:47:48 +05301531 ret = kvm_add_default_arch_event(&argc, argv);
1532 if (ret)
1533 return -EINVAL;
1534
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001535 rec_argc = argc + 2;
1536 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1537 rec_argv[i++] = strdup("record");
1538 rec_argv[i++] = strdup("-o");
Xiao Guangrong37860632012-11-15 14:17:01 +08001539 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001540 for (j = 1; j < argc; j++, i++)
1541 rec_argv[i] = argv[j];
1542
1543 BUG_ON(i != rec_argc);
1544
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001545 return cmd_record(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001546}
1547
Xiao Guangrong37860632012-11-15 14:17:01 +08001548static int __cmd_report(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001549{
1550 int rec_argc, i = 0, j;
1551 const char **rec_argv;
1552
1553 rec_argc = argc + 2;
1554 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1555 rec_argv[i++] = strdup("report");
1556 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001557 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001558 for (j = 1; j < argc; j++, i++)
1559 rec_argv[i] = argv[j];
1560
1561 BUG_ON(i != rec_argc);
1562
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001563 return cmd_report(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001564}
1565
Xiao Guangrong37860632012-11-15 14:17:01 +08001566static int
1567__cmd_buildid_list(const char *file_name, int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001568{
1569 int rec_argc, i = 0, j;
1570 const char **rec_argv;
1571
1572 rec_argc = argc + 2;
1573 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1574 rec_argv[i++] = strdup("buildid-list");
1575 rec_argv[i++] = strdup("-i");
Xiao Guangrong37860632012-11-15 14:17:01 +08001576 rec_argv[i++] = strdup(file_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001577 for (j = 1; j < argc; j++, i++)
1578 rec_argv[i] = argv[j];
1579
1580 BUG_ON(i != rec_argc);
1581
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001582 return cmd_buildid_list(i, rec_argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001583}
1584
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001585int cmd_kvm(int argc, const char **argv)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001586{
Arnaldo Carvalho de Melo20914ce52012-12-19 10:59:29 -03001587 const char *file_name = NULL;
David Ahernde332ac2012-10-02 22:09:53 -06001588 const struct option kvm_options[] = {
Xiao Guangrong37860632012-11-15 14:17:01 +08001589 OPT_STRING('i', "input", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001590 "Input file name"),
Xiao Guangrong37860632012-11-15 14:17:01 +08001591 OPT_STRING('o', "output", &file_name, "file",
David Ahernde332ac2012-10-02 22:09:53 -06001592 "Output file name"),
1593 OPT_BOOLEAN(0, "guest", &perf_guest,
1594 "Collect guest os data"),
1595 OPT_BOOLEAN(0, "host", &perf_host,
1596 "Collect host os data"),
1597 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1598 "guest mount directory under which every guest os"
1599 " instance has a subdir"),
1600 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1601 "file", "file saving guest os vmlinux"),
1602 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1603 "file", "file saving guest os /proc/kallsyms"),
1604 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1605 "file", "file saving guest os /proc/modules"),
Dongsheng Yang100b9072013-12-09 12:15:11 -05001606 OPT_INCR('v', "verbose", &verbose,
1607 "be more verbose (show counter open errors, etc)"),
David Ahernde332ac2012-10-02 22:09:53 -06001608 OPT_END()
1609 };
1610
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001611 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1612 "buildid-list", "stat", NULL };
1613 const char *kvm_usage[] = { NULL, NULL };
David Ahernde332ac2012-10-02 22:09:53 -06001614
Joerg Roedel1aed2672012-01-04 17:54:20 +01001615 perf_host = 0;
1616 perf_guest = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001617
Ramkumar Ramachandra09a71b92014-03-03 20:26:36 -05001618 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1619 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001620 if (!argc)
1621 usage_with_options(kvm_usage, kvm_options);
1622
1623 if (!perf_host)
1624 perf_guest = 1;
1625
Xiao Guangrong37860632012-11-15 14:17:01 +08001626 if (!file_name) {
Dongsheng Yange1a2b172013-12-06 17:25:51 -05001627 file_name = get_filename_for_perf_kvm();
David Ahernde332ac2012-10-02 22:09:53 -06001628
Xiao Guangrong37860632012-11-15 14:17:01 +08001629 if (!file_name) {
David Ahernde332ac2012-10-02 22:09:53 -06001630 pr_err("Failed to allocate memory for filename\n");
1631 return -ENOMEM;
1632 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001633 }
1634
1635 if (!strncmp(argv[0], "rec", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001636 return __cmd_record(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001637 else if (!strncmp(argv[0], "rep", 3))
Xiao Guangrong37860632012-11-15 14:17:01 +08001638 return __cmd_report(file_name, argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001639 else if (!strncmp(argv[0], "diff", 4))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001640 return cmd_diff(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001641 else if (!strncmp(argv[0], "top", 3))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001642 return cmd_top(argc, argv);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001643 else if (!strncmp(argv[0], "buildid-list", 12))
Xiao Guangrong37860632012-11-15 14:17:01 +08001644 return __cmd_buildid_list(file_name, argc, argv);
Alexander Yaryginda50ad62014-07-03 17:59:49 +04001645#ifdef HAVE_KVM_STAT_SUPPORT
Xiao Guangrongbcf6edc2012-09-17 16:31:15 +08001646 else if (!strncmp(argv[0], "stat", 4))
Xiao Guangrong37860632012-11-15 14:17:01 +08001647 return kvm_cmd_stat(file_name, argc, argv);
Xiao Guangrong73210902012-11-19 16:19:21 +08001648#endif
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001649 else
1650 usage_with_options(kvm_usage, kvm_options);
1651
1652 return 0;
1653}