perf symbols: Don't try to find DSOs in SYSV maps
[deliverable/linux.git] / tools / perf / builtin-kvm.c
CommitLineData
a1645ce1
ZY
1#include "builtin.h"
2#include "perf.h"
3
bcf6edcd 4#include "util/evsel.h"
1afe1d14 5#include "util/evlist.h"
a1645ce1
ZY
6#include "util/util.h"
7#include "util/cache.h"
8#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
11#include "util/session.h"
2e73f00f 12#include "util/intlist.h"
a1645ce1
ZY
13#include "util/parse-options.h"
14#include "util/trace-event.h"
a1645ce1 15#include "util/debug.h"
553873e1 16#include <api/fs/debugfs.h>
bcf6edcd
XG
17#include "util/tool.h"
18#include "util/stat.h"
1afe1d14 19#include "util/top.h"
f5fc1412 20#include "util/data.h"
a1645ce1
ZY
21
22#include <sys/prctl.h>
87419c9a 23#ifdef HAVE_TIMERFD_SUPPORT
1afe1d14 24#include <sys/timerfd.h>
87419c9a 25#endif
a1645ce1 26
1afe1d14 27#include <termios.h>
a1645ce1
ZY
28#include <semaphore.h>
29#include <pthread.h>
30#include <math.h>
31
da50ad69 32#ifdef HAVE_KVM_STAT_SUPPORT
44b38021 33#include <asm/kvm_perf.h>
9daa8123 34#include "util/kvm-stat.h"
bcf6edcd 35
9daa8123
AY
36void exit_event_get_key(struct perf_evsel *evsel,
37 struct perf_sample *sample,
38 struct event_key *key)
bcf6edcd
XG
39{
40 key->info = 0;
44b38021 41 key->key = perf_evsel__intval(evsel, sample, KVM_EXIT_REASON);
bcf6edcd
XG
42}
43
9daa8123 44bool kvm_exit_event(struct perf_evsel *evsel)
bcf6edcd 45{
44b38021 46 return !strcmp(evsel->name, KVM_EXIT_TRACE);
bcf6edcd
XG
47}
48
9daa8123
AY
49bool exit_event_begin(struct perf_evsel *evsel,
50 struct perf_sample *sample, struct event_key *key)
bcf6edcd 51{
14907e73
ACM
52 if (kvm_exit_event(evsel)) {
53 exit_event_get_key(evsel, sample, key);
bcf6edcd
XG
54 return true;
55 }
56
57 return false;
58}
59
9daa8123 60bool kvm_entry_event(struct perf_evsel *evsel)
bcf6edcd 61{
44b38021 62 return !strcmp(evsel->name, KVM_ENTRY_TRACE);
bcf6edcd
XG
63}
64
9daa8123
AY
65bool exit_event_end(struct perf_evsel *evsel,
66 struct perf_sample *sample __maybe_unused,
67 struct event_key *key __maybe_unused)
bcf6edcd 68{
14907e73 69 return kvm_entry_event(evsel);
bcf6edcd
XG
70}
71
df74c13b
AY
72static const char *get_exit_reason(struct perf_kvm_stat *kvm,
73 struct exit_reasons_table *tbl,
74 u64 exit_code)
bcf6edcd 75{
df74c13b 76 while (tbl->reason != NULL) {
de332ac4
DA
77 if (tbl->exit_code == exit_code)
78 return tbl->reason;
79 tbl++;
bcf6edcd
XG
80 }
81
82 pr_err("unknown kvm exit code:%lld on %s\n",
de332ac4 83 (unsigned long long)exit_code, kvm->exit_reasons_isa);
bcf6edcd
XG
84 return "UNKNOWN";
85}
86
9daa8123
AY
87void exit_event_decode_key(struct perf_kvm_stat *kvm,
88 struct event_key *key,
89 char *decode)
bcf6edcd 90{
3be8e2a0 91 const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
df74c13b 92 key->key);
bcf6edcd 93
44b38021 94 scnprintf(decode, DECODE_STR_LEN, "%s", exit_reason);
bcf6edcd
XG
95}
96
9daa8123 97static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
bcf6edcd 98{
9daa8123 99 struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
bcf6edcd 100
9daa8123
AY
101 for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
102 if (!strcmp(events_ops->name, kvm->report_event)) {
103 kvm->events_ops = events_ops->ops;
104 return true;
105 }
bcf6edcd
XG
106 }
107
108 return false;
109}
110
bcf6edcd
XG
111struct vcpu_event_record {
112 int vcpu_id;
113 u64 start_time;
114 struct kvm_event *last_event;
115};
116
bcf6edcd 117
3786063a 118static void init_kvm_event_record(struct perf_kvm_stat *kvm)
bcf6edcd 119{
b880deea 120 unsigned int i;
bcf6edcd 121
b880deea 122 for (i = 0; i < EVENTS_CACHE_SIZE; i++)
de332ac4 123 INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
bcf6edcd
XG
124}
125
87419c9a 126#ifdef HAVE_TIMERFD_SUPPORT
1afe1d14
DA
127static void clear_events_cache_stats(struct list_head *kvm_events_cache)
128{
129 struct list_head *head;
130 struct kvm_event *event;
131 unsigned int i;
62d04dbf 132 int j;
1afe1d14
DA
133
134 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
135 head = &kvm_events_cache[i];
136 list_for_each_entry(event, head, hash_entry) {
137 /* reset stats for event */
62d04dbf
DA
138 event->total.time = 0;
139 init_stats(&event->total.stats);
140
141 for (j = 0; j < event->max_vcpu; ++j) {
142 event->vcpu[j].time = 0;
143 init_stats(&event->vcpu[j].stats);
144 }
1afe1d14
DA
145 }
146 }
147}
87419c9a 148#endif
1afe1d14 149
bcf6edcd
XG
150static int kvm_events_hash_fn(u64 key)
151{
152 return key & (EVENTS_CACHE_SIZE - 1);
153}
154
155static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
156{
157 int old_max_vcpu = event->max_vcpu;
6ca5f308 158 void *prev;
bcf6edcd
XG
159
160 if (vcpu_id < event->max_vcpu)
161 return true;
162
163 while (event->max_vcpu <= vcpu_id)
164 event->max_vcpu += DEFAULT_VCPU_NUM;
165
6ca5f308 166 prev = event->vcpu;
bcf6edcd
XG
167 event->vcpu = realloc(event->vcpu,
168 event->max_vcpu * sizeof(*event->vcpu));
169 if (!event->vcpu) {
6ca5f308 170 free(prev);
bcf6edcd
XG
171 pr_err("Not enough memory\n");
172 return false;
173 }
174
175 memset(event->vcpu + old_max_vcpu, 0,
176 (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
177 return true;
178}
179
180static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
181{
182 struct kvm_event *event;
183
184 event = zalloc(sizeof(*event));
185 if (!event) {
186 pr_err("Not enough memory\n");
187 return NULL;
188 }
189
190 event->key = *key;
acb61fc8 191 init_stats(&event->total.stats);
bcf6edcd
XG
192 return event;
193}
194
3786063a 195static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
de332ac4 196 struct event_key *key)
bcf6edcd
XG
197{
198 struct kvm_event *event;
199 struct list_head *head;
200
201 BUG_ON(key->key == INVALID_KEY);
202
de332ac4 203 head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
355afe81 204 list_for_each_entry(event, head, hash_entry) {
bcf6edcd
XG
205 if (event->key.key == key->key && event->key.info == key->info)
206 return event;
355afe81 207 }
bcf6edcd
XG
208
209 event = kvm_alloc_init_event(key);
210 if (!event)
211 return NULL;
212
213 list_add(&event->hash_entry, head);
214 return event;
215}
216
3786063a 217static bool handle_begin_event(struct perf_kvm_stat *kvm,
de332ac4 218 struct vcpu_event_record *vcpu_record,
bcf6edcd
XG
219 struct event_key *key, u64 timestamp)
220{
221 struct kvm_event *event = NULL;
222
223 if (key->key != INVALID_KEY)
de332ac4 224 event = find_create_kvm_event(kvm, key);
bcf6edcd
XG
225
226 vcpu_record->last_event = event;
227 vcpu_record->start_time = timestamp;
228 return true;
229}
230
231static void
232kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
233{
234 kvm_stats->time += time_diff;
235 update_stats(&kvm_stats->stats, time_diff);
236}
237
238static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
239{
240 struct kvm_event_stats *kvm_stats = &event->total;
241
242 if (vcpu_id != -1)
243 kvm_stats = &event->vcpu[vcpu_id];
244
245 return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
246 avg_stats(&kvm_stats->stats));
247}
248
249static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
250 u64 time_diff)
251{
2aa8eab0
DA
252 if (vcpu_id == -1) {
253 kvm_update_event_stats(&event->total, time_diff);
254 return true;
255 }
bcf6edcd
XG
256
257 if (!kvm_event_expand(event, vcpu_id))
258 return false;
259
260 kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
261 return true;
262}
263
3be8e2a0
AY
264static bool is_child_event(struct perf_kvm_stat *kvm,
265 struct perf_evsel *evsel,
266 struct perf_sample *sample,
267 struct event_key *key)
268{
269 struct child_event_ops *child_ops;
270
271 child_ops = kvm->events_ops->child_ops;
272
273 if (!child_ops)
274 return false;
275
276 for (; child_ops->name; child_ops++) {
277 if (!strcmp(evsel->name, child_ops->name)) {
278 child_ops->get_key(evsel, sample, key);
279 return true;
280 }
281 }
282
283 return false;
284}
285
286static bool handle_child_event(struct perf_kvm_stat *kvm,
287 struct vcpu_event_record *vcpu_record,
288 struct event_key *key,
289 struct perf_sample *sample __maybe_unused)
290{
291 struct kvm_event *event = NULL;
292
293 if (key->key != INVALID_KEY)
294 event = find_create_kvm_event(kvm, key);
295
296 vcpu_record->last_event = event;
297
298 return true;
299}
300
54c801ff
AY
301static bool skip_event(const char *event)
302{
303 const char * const *skip_events;
304
305 for (skip_events = kvm_skip_events; *skip_events; skip_events++)
306 if (!strcmp(event, *skip_events))
307 return true;
308
309 return false;
310}
311
3786063a 312static bool handle_end_event(struct perf_kvm_stat *kvm,
de332ac4
DA
313 struct vcpu_event_record *vcpu_record,
314 struct event_key *key,
70f7b4a7 315 struct perf_sample *sample)
bcf6edcd
XG
316{
317 struct kvm_event *event;
318 u64 time_begin, time_diff;
2aa8eab0
DA
319 int vcpu;
320
321 if (kvm->trace_vcpu == -1)
322 vcpu = -1;
323 else
324 vcpu = vcpu_record->vcpu_id;
bcf6edcd
XG
325
326 event = vcpu_record->last_event;
327 time_begin = vcpu_record->start_time;
328
329 /* The begin event is not caught. */
330 if (!time_begin)
331 return true;
332
333 /*
334 * In some case, the 'begin event' only records the start timestamp,
335 * the actual event is recognized in the 'end event' (e.g. mmio-event).
336 */
337
338 /* Both begin and end events did not get the key. */
339 if (!event && key->key == INVALID_KEY)
340 return true;
341
342 if (!event)
de332ac4 343 event = find_create_kvm_event(kvm, key);
bcf6edcd
XG
344
345 if (!event)
346 return false;
347
348 vcpu_record->last_event = NULL;
349 vcpu_record->start_time = 0;
350
1afe1d14 351 /* seems to happen once in a while during live mode */
70f7b4a7 352 if (sample->time < time_begin) {
1afe1d14
DA
353 pr_debug("End time before begin time; skipping event.\n");
354 return true;
355 }
bcf6edcd 356
70f7b4a7
DA
357 time_diff = sample->time - time_begin;
358
359 if (kvm->duration && time_diff > kvm->duration) {
44b38021 360 char decode[DECODE_STR_LEN];
70f7b4a7
DA
361
362 kvm->events_ops->decode_key(kvm, &event->key, decode);
54c801ff 363 if (!skip_event(decode)) {
70f7b4a7
DA
364 pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
365 sample->time, sample->pid, vcpu_record->vcpu_id,
366 decode, time_diff/1000);
367 }
368 }
369
2aa8eab0 370 return update_kvm_event(event, vcpu, time_diff);
bcf6edcd
XG
371}
372
14907e73
ACM
373static
374struct vcpu_event_record *per_vcpu_record(struct thread *thread,
375 struct perf_evsel *evsel,
376 struct perf_sample *sample)
bcf6edcd
XG
377{
378 /* Only kvm_entry records vcpu id. */
14907e73 379 if (!thread->priv && kvm_entry_event(evsel)) {
bcf6edcd
XG
380 struct vcpu_event_record *vcpu_record;
381
14907e73 382 vcpu_record = zalloc(sizeof(*vcpu_record));
bcf6edcd 383 if (!vcpu_record) {
14907e73 384 pr_err("%s: Not enough memory\n", __func__);
bcf6edcd
XG
385 return NULL;
386 }
387
44b38021 388 vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, VCPU_ID);
bcf6edcd
XG
389 thread->priv = vcpu_record;
390 }
391
14907e73 392 return thread->priv;
bcf6edcd
XG
393}
394
3786063a 395static bool handle_kvm_event(struct perf_kvm_stat *kvm,
de332ac4
DA
396 struct thread *thread,
397 struct perf_evsel *evsel,
14907e73 398 struct perf_sample *sample)
bcf6edcd
XG
399{
400 struct vcpu_event_record *vcpu_record;
3be8e2a0
AY
401 struct event_key key = { .key = INVALID_KEY,
402 .exit_reasons = kvm->exit_reasons };
bcf6edcd 403
14907e73 404 vcpu_record = per_vcpu_record(thread, evsel, sample);
bcf6edcd
XG
405 if (!vcpu_record)
406 return true;
407
2aa8eab0
DA
408 /* only process events for vcpus user cares about */
409 if ((kvm->trace_vcpu != -1) &&
410 (kvm->trace_vcpu != vcpu_record->vcpu_id))
411 return true;
412
de332ac4
DA
413 if (kvm->events_ops->is_begin_event(evsel, sample, &key))
414 return handle_begin_event(kvm, vcpu_record, &key, sample->time);
bcf6edcd 415
3be8e2a0
AY
416 if (is_child_event(kvm, evsel, sample, &key))
417 return handle_child_event(kvm, vcpu_record, &key, sample);
418
de332ac4 419 if (kvm->events_ops->is_end_event(evsel, sample, &key))
70f7b4a7 420 return handle_end_event(kvm, vcpu_record, &key, sample);
bcf6edcd
XG
421
422 return true;
423}
424
bcf6edcd
XG
425#define GET_EVENT_KEY(func, field) \
426static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
427{ \
428 if (vcpu == -1) \
429 return event->total.field; \
430 \
431 if (vcpu >= event->max_vcpu) \
432 return 0; \
433 \
434 return event->vcpu[vcpu].field; \
435}
436
437#define COMPARE_EVENT_KEY(func, field) \
438GET_EVENT_KEY(func, field) \
439static int compare_kvm_event_ ## func(struct kvm_event *one, \
440 struct kvm_event *two, int vcpu)\
441{ \
442 return get_event_ ##func(one, vcpu) > \
443 get_event_ ##func(two, vcpu); \
444}
445
446GET_EVENT_KEY(time, time);
447COMPARE_EVENT_KEY(count, stats.n);
448COMPARE_EVENT_KEY(mean, stats.mean);
62d04dbf
DA
449GET_EVENT_KEY(max, stats.max);
450GET_EVENT_KEY(min, stats.min);
bcf6edcd
XG
451
452#define DEF_SORT_NAME_KEY(name, compare_key) \
453 { #name, compare_kvm_event_ ## compare_key }
454
455static struct kvm_event_key keys[] = {
456 DEF_SORT_NAME_KEY(sample, count),
457 DEF_SORT_NAME_KEY(time, mean),
458 { NULL, NULL }
459};
460
3786063a 461static bool select_key(struct perf_kvm_stat *kvm)
bcf6edcd
XG
462{
463 int i;
464
465 for (i = 0; keys[i].name; i++) {
de332ac4
DA
466 if (!strcmp(keys[i].name, kvm->sort_key)) {
467 kvm->compare = keys[i].key;
bcf6edcd
XG
468 return true;
469 }
470 }
471
de332ac4 472 pr_err("Unknown compare key:%s\n", kvm->sort_key);
bcf6edcd
XG
473 return false;
474}
475
de332ac4
DA
476static void insert_to_result(struct rb_root *result, struct kvm_event *event,
477 key_cmp_fun bigger, int vcpu)
bcf6edcd 478{
de332ac4 479 struct rb_node **rb = &result->rb_node;
bcf6edcd
XG
480 struct rb_node *parent = NULL;
481 struct kvm_event *p;
482
483 while (*rb) {
484 p = container_of(*rb, struct kvm_event, rb);
485 parent = *rb;
486
487 if (bigger(event, p, vcpu))
488 rb = &(*rb)->rb_left;
489 else
490 rb = &(*rb)->rb_right;
491 }
492
493 rb_link_node(&event->rb, parent, rb);
de332ac4 494 rb_insert_color(&event->rb, result);
bcf6edcd
XG
495}
496
3786063a
XG
497static void
498update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
bcf6edcd 499{
de332ac4
DA
500 int vcpu = kvm->trace_vcpu;
501
502 kvm->total_count += get_event_count(event, vcpu);
503 kvm->total_time += get_event_time(event, vcpu);
bcf6edcd
XG
504}
505
506static bool event_is_valid(struct kvm_event *event, int vcpu)
507{
508 return !!get_event_count(event, vcpu);
509}
510
3786063a 511static void sort_result(struct perf_kvm_stat *kvm)
bcf6edcd
XG
512{
513 unsigned int i;
de332ac4 514 int vcpu = kvm->trace_vcpu;
bcf6edcd
XG
515 struct kvm_event *event;
516
355afe81
DA
517 for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
518 list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
bcf6edcd 519 if (event_is_valid(event, vcpu)) {
de332ac4
DA
520 update_total_count(kvm, event);
521 insert_to_result(&kvm->result, event,
522 kvm->compare, vcpu);
bcf6edcd 523 }
355afe81
DA
524 }
525 }
bcf6edcd
XG
526}
527
528/* returns left most element of result, and erase it */
de332ac4 529static struct kvm_event *pop_from_result(struct rb_root *result)
bcf6edcd 530{
de332ac4 531 struct rb_node *node = rb_first(result);
bcf6edcd
XG
532
533 if (!node)
534 return NULL;
535
de332ac4 536 rb_erase(node, result);
bcf6edcd
XG
537 return container_of(node, struct kvm_event, rb);
538}
539
1afe1d14 540static void print_vcpu_info(struct perf_kvm_stat *kvm)
bcf6edcd 541{
1afe1d14
DA
542 int vcpu = kvm->trace_vcpu;
543
bcf6edcd
XG
544 pr_info("Analyze events for ");
545
1afe1d14
DA
546 if (kvm->live) {
547 if (kvm->opts.target.system_wide)
548 pr_info("all VMs, ");
549 else if (kvm->opts.target.pid)
550 pr_info("pid(s) %s, ", kvm->opts.target.pid);
551 else
552 pr_info("dazed and confused on what is monitored, ");
553 }
554
bcf6edcd
XG
555 if (vcpu == -1)
556 pr_info("all VCPUs:\n\n");
557 else
558 pr_info("VCPU %d:\n\n", vcpu);
559}
560
1afe1d14
DA
561static void show_timeofday(void)
562{
563 char date[64];
564 struct timeval tv;
565 struct tm ltime;
566
567 gettimeofday(&tv, NULL);
568 if (localtime_r(&tv.tv_sec, &ltime)) {
569 strftime(date, sizeof(date), "%H:%M:%S", &ltime);
570 pr_info("%s.%06ld", date, tv.tv_usec);
571 } else
572 pr_info("00:00:00.000000");
573
574 return;
575}
576
3786063a 577static void print_result(struct perf_kvm_stat *kvm)
bcf6edcd 578{
44b38021 579 char decode[DECODE_STR_LEN];
bcf6edcd 580 struct kvm_event *event;
de332ac4 581 int vcpu = kvm->trace_vcpu;
bcf6edcd 582
1afe1d14
DA
583 if (kvm->live) {
584 puts(CONSOLE_CLEAR);
585 show_timeofday();
586 }
587
bcf6edcd 588 pr_info("\n\n");
1afe1d14 589 print_vcpu_info(kvm);
44b38021 590 pr_info("%*s ", DECODE_STR_LEN, kvm->events_ops->name);
bcf6edcd
XG
591 pr_info("%10s ", "Samples");
592 pr_info("%9s ", "Samples%");
593
594 pr_info("%9s ", "Time%");
b048a24c
CB
595 pr_info("%11s ", "Min Time");
596 pr_info("%11s ", "Max Time");
bcf6edcd
XG
597 pr_info("%16s ", "Avg time");
598 pr_info("\n\n");
599
de332ac4 600 while ((event = pop_from_result(&kvm->result))) {
62d04dbf 601 u64 ecount, etime, max, min;
bcf6edcd
XG
602
603 ecount = get_event_count(event, vcpu);
604 etime = get_event_time(event, vcpu);
62d04dbf
DA
605 max = get_event_max(event, vcpu);
606 min = get_event_min(event, vcpu);
bcf6edcd 607
de332ac4 608 kvm->events_ops->decode_key(kvm, &event->key, decode);
44b38021 609 pr_info("%*s ", DECODE_STR_LEN, decode);
bcf6edcd 610 pr_info("%10llu ", (unsigned long long)ecount);
de332ac4
DA
611 pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
612 pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
b048a24c
CB
613 pr_info("%9.2fus ", (double)min / 1e3);
614 pr_info("%9.2fus ", (double)max / 1e3);
bcf6edcd
XG
615 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
616 kvm_event_rel_stddev(vcpu, event));
617 pr_info("\n");
618 }
619
e4f7637f
DA
620 pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
621 kvm->total_count, kvm->total_time / 1e3);
1afe1d14
DA
622
623 if (kvm->lost_events)
624 pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
625}
626
87419c9a 627#ifdef HAVE_TIMERFD_SUPPORT
1afe1d14
DA
628static int process_lost_event(struct perf_tool *tool,
629 union perf_event *event __maybe_unused,
630 struct perf_sample *sample __maybe_unused,
631 struct machine *machine __maybe_unused)
632{
633 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
634
635 kvm->lost_events++;
636 return 0;
bcf6edcd 637}
87419c9a 638#endif
bcf6edcd 639
2e73f00f
DA
640static bool skip_sample(struct perf_kvm_stat *kvm,
641 struct perf_sample *sample)
642{
643 if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
644 return true;
645
646 return false;
647}
648
de332ac4 649static int process_sample_event(struct perf_tool *tool,
bcf6edcd
XG
650 union perf_event *event,
651 struct perf_sample *sample,
652 struct perf_evsel *evsel,
653 struct machine *machine)
654{
2e73f00f 655 struct thread *thread;
3786063a
XG
656 struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
657 tool);
bcf6edcd 658
2e73f00f
DA
659 if (skip_sample(kvm, sample))
660 return 0;
661
314add6b 662 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
bcf6edcd
XG
663 if (thread == NULL) {
664 pr_debug("problem processing %d event, skipping it.\n",
665 event->header.type);
666 return -1;
667 }
668
de332ac4 669 if (!handle_kvm_event(kvm, thread, evsel, sample))
bcf6edcd
XG
670 return -1;
671
672 return 0;
673}
674
1afe1d14 675static int cpu_isa_config(struct perf_kvm_stat *kvm)
bcf6edcd 676{
1afe1d14 677 char buf[64], *cpuid;
65c647a6 678 int err;
1afe1d14
DA
679
680 if (kvm->live) {
681 err = get_cpuid(buf, sizeof(buf));
682 if (err != 0) {
65c647a6 683 pr_err("Failed to look up CPU type\n");
1afe1d14
DA
684 return err;
685 }
686 cpuid = buf;
687 } else
688 cpuid = kvm->session->header.env.cpuid;
bcf6edcd 689
65c647a6
AY
690 if (!cpuid) {
691 pr_err("Failed to look up CPU type\n");
692 return -EINVAL;
1afe1d14
DA
693 }
694
65c647a6
AY
695 err = cpu_isa_init(kvm, cpuid);
696 if (err == -ENOTSUP)
697 pr_err("CPU %s is not supported.\n", cpuid);
1afe1d14 698
65c647a6 699 return err;
1afe1d14
DA
700}
701
702static bool verify_vcpu(int vcpu)
703{
704 if (vcpu != -1 && vcpu < 0) {
705 pr_err("Invalid vcpu:%d.\n", vcpu);
706 return false;
707 }
708
709 return true;
710}
711
87419c9a 712#ifdef HAVE_TIMERFD_SUPPORT
1afe1d14
DA
713/* keeping the max events to a modest level to keep
714 * the processing of samples per mmap smooth.
715 */
716#define PERF_KVM__MAX_EVENTS_PER_MMAP 25
717
718static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
719 u64 *mmap_time)
720{
721 union perf_event *event;
722 struct perf_sample sample;
723 s64 n = 0;
724 int err;
725
726 *mmap_time = ULLONG_MAX;
727 while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
728 err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
729 if (err) {
8e50d384 730 perf_evlist__mmap_consume(kvm->evlist, idx);
1afe1d14
DA
731 pr_err("Failed to parse sample\n");
732 return -1;
733 }
734
d40b4a15 735 err = perf_session_queue_event(kvm->session, event, &kvm->tool, &sample, 0);
8e50d384
ZZ
736 /*
737 * FIXME: Here we can't consume the event, as perf_session_queue_event will
738 * point to it, and it'll get possibly overwritten by the kernel.
739 */
740 perf_evlist__mmap_consume(kvm->evlist, idx);
741
1afe1d14
DA
742 if (err) {
743 pr_err("Failed to enqueue sample: %d\n", err);
744 return -1;
745 }
746
747 /* save time stamp of our first sample for this mmap */
748 if (n == 0)
749 *mmap_time = sample.time;
750
751 /* limit events per mmap handled all at once */
752 n++;
753 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
754 break;
755 }
756
757 return n;
758}
759
760static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
761{
762 int i, err, throttled = 0;
763 s64 n, ntotal = 0;
764 u64 flush_time = ULLONG_MAX, mmap_time;
765
766 for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
767 n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
768 if (n < 0)
769 return -1;
770
771 /* flush time is going to be the minimum of all the individual
772 * mmap times. Essentially, we flush all the samples queued up
773 * from the last pass under our minimal start time -- that leaves
774 * a very small race for samples to come in with a lower timestamp.
775 * The ioctl to return the perf_clock timestamp should close the
776 * race entirely.
777 */
778 if (mmap_time < flush_time)
779 flush_time = mmap_time;
780
781 ntotal += n;
782 if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
783 throttled = 1;
784 }
785
786 /* flush queue after each round in which we processed events */
787 if (ntotal) {
37e39aa8 788 kvm->session->ordered_events.next_flush = flush_time;
1afe1d14
DA
789 err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
790 if (err) {
791 if (kvm->lost_events)
792 pr_info("\nLost events: %" PRIu64 "\n\n",
793 kvm->lost_events);
794 return err;
795 }
796 }
797
798 return throttled;
799}
800
801static volatile int done;
802
803static void sig_handler(int sig __maybe_unused)
804{
805 done = 1;
806}
807
808static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
809{
810 struct itimerspec new_value;
811 int rc = -1;
812
813 kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
814 if (kvm->timerfd < 0) {
815 pr_err("timerfd_create failed\n");
816 goto out;
817 }
818
819 new_value.it_value.tv_sec = kvm->display_time;
820 new_value.it_value.tv_nsec = 0;
821 new_value.it_interval.tv_sec = kvm->display_time;
822 new_value.it_interval.tv_nsec = 0;
823
824 if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
825 pr_err("timerfd_settime failed: %d\n", errno);
826 close(kvm->timerfd);
827 goto out;
828 }
829
830 rc = 0;
831out:
832 return rc;
833}
834
835static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
836{
837 uint64_t c;
838 int rc;
839
840 rc = read(kvm->timerfd, &c, sizeof(uint64_t));
841 if (rc < 0) {
842 if (errno == EAGAIN)
843 return 0;
844
845 pr_err("Failed to read timer fd: %d\n", errno);
846 return -1;
847 }
848
849 if (rc != sizeof(uint64_t)) {
850 pr_err("Error reading timer fd - invalid size returned\n");
851 return -1;
852 }
853
854 if (c != 1)
855 pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
856
857 /* update display */
858 sort_result(kvm);
859 print_result(kvm);
860
861 /* reset counts */
862 clear_events_cache_stats(kvm->kvm_events_cache);
863 kvm->total_count = 0;
864 kvm->total_time = 0;
865 kvm->lost_events = 0;
866
867 return 0;
868}
869
870static int fd_set_nonblock(int fd)
871{
872 long arg = 0;
873
874 arg = fcntl(fd, F_GETFL);
875 if (arg < 0) {
876 pr_err("Failed to get current flags for fd %d\n", fd);
877 return -1;
878 }
879
880 if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
881 pr_err("Failed to set non-block option on fd %d\n", fd);
882 return -1;
883 }
884
885 return 0;
886}
887
d5b4130a 888static int perf_kvm__handle_stdin(void)
1afe1d14
DA
889{
890 int c;
891
1afe1d14 892 c = getc(stdin);
1afe1d14
DA
893 if (c == 'q')
894 return 1;
895
896 return 0;
897}
898
899static int kvm_events_live_report(struct perf_kvm_stat *kvm)
900{
901 struct pollfd *pollfds = NULL;
902 int nr_fds, nr_stdin, ret, err = -EINVAL;
d5b4130a 903 struct termios save;
1afe1d14
DA
904
905 /* live flag must be set first */
906 kvm->live = true;
907
908 ret = cpu_isa_config(kvm);
909 if (ret < 0)
910 return ret;
911
912 if (!verify_vcpu(kvm->trace_vcpu) ||
913 !select_key(kvm) ||
914 !register_kvm_events_ops(kvm)) {
915 goto out;
916 }
917
d5b4130a 918 set_term_quiet_input(&save);
1afe1d14
DA
919 init_kvm_event_record(kvm);
920
1afe1d14
DA
921 signal(SIGINT, sig_handler);
922 signal(SIGTERM, sig_handler);
923
924 /* copy pollfds -- need to add timerfd and stdin */
925 nr_fds = kvm->evlist->nr_fds;
926 pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2));
927 if (!pollfds) {
928 err = -ENOMEM;
929 goto out;
930 }
931 memcpy(pollfds, kvm->evlist->pollfd,
932 sizeof(struct pollfd) * kvm->evlist->nr_fds);
933
934 /* add timer fd */
935 if (perf_kvm__timerfd_create(kvm) < 0) {
936 err = -1;
937 goto out;
938 }
939
940 pollfds[nr_fds].fd = kvm->timerfd;
941 pollfds[nr_fds].events = POLLIN;
942 nr_fds++;
943
944 pollfds[nr_fds].fd = fileno(stdin);
945 pollfds[nr_fds].events = POLLIN;
946 nr_stdin = nr_fds;
947 nr_fds++;
948 if (fd_set_nonblock(fileno(stdin)) != 0)
949 goto out;
950
951 /* everything is good - enable the events and process */
952 perf_evlist__enable(kvm->evlist);
953
954 while (!done) {
955 int rc;
956
957 rc = perf_kvm__mmap_read(kvm);
958 if (rc < 0)
959 break;
960
961 err = perf_kvm__handle_timerfd(kvm);
962 if (err)
963 goto out;
964
965 if (pollfds[nr_stdin].revents & POLLIN)
d5b4130a 966 done = perf_kvm__handle_stdin();
1afe1d14
DA
967
968 if (!rc && !done)
969 err = poll(pollfds, nr_fds, 100);
970 }
971
972 perf_evlist__disable(kvm->evlist);
973
974 if (err == 0) {
975 sort_result(kvm);
976 print_result(kvm);
977 }
978
979out:
980 if (kvm->timerfd >= 0)
981 close(kvm->timerfd);
982
d5b4130a 983 tcsetattr(0, TCSAFLUSH, &save);
f5385650 984 free(pollfds);
1afe1d14
DA
985 return err;
986}
987
988static int kvm_live_open_events(struct perf_kvm_stat *kvm)
989{
990 int err, rc = -1;
991 struct perf_evsel *pos;
992 struct perf_evlist *evlist = kvm->evlist;
f9f33fdb 993 char sbuf[STRERR_BUFSIZE];
1afe1d14
DA
994
995 perf_evlist__config(evlist, &kvm->opts);
996
997 /*
998 * Note: exclude_{guest,host} do not apply here.
999 * This command processes KVM tracepoints from host only
1000 */
0050f7aa 1001 evlist__for_each(evlist, pos) {
1afe1d14
DA
1002 struct perf_event_attr *attr = &pos->attr;
1003
1004 /* make sure these *are* set */
e71aa283
AH
1005 perf_evsel__set_sample_bit(pos, TID);
1006 perf_evsel__set_sample_bit(pos, TIME);
1007 perf_evsel__set_sample_bit(pos, CPU);
1008 perf_evsel__set_sample_bit(pos, RAW);
1afe1d14 1009 /* make sure these are *not*; want as small a sample as possible */
e71aa283
AH
1010 perf_evsel__reset_sample_bit(pos, PERIOD);
1011 perf_evsel__reset_sample_bit(pos, IP);
1012 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
1013 perf_evsel__reset_sample_bit(pos, ADDR);
1014 perf_evsel__reset_sample_bit(pos, READ);
1afe1d14
DA
1015 attr->mmap = 0;
1016 attr->comm = 0;
1017 attr->task = 0;
1018
1019 attr->sample_period = 1;
1020
1021 attr->watermark = 0;
1022 attr->wakeup_events = 1000;
1023
1024 /* will enable all once we are ready */
1025 attr->disabled = 1;
1026 }
1027
1028 err = perf_evlist__open(evlist);
1029 if (err < 0) {
f9f33fdb
MH
1030 printf("Couldn't create the events: %s\n",
1031 strerror_r(errno, sbuf, sizeof(sbuf)));
1afe1d14 1032 goto out;
bcf6edcd
XG
1033 }
1034
1afe1d14 1035 if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
f9f33fdb
MH
1036 ui__error("Failed to mmap the events: %s\n",
1037 strerror_r(errno, sbuf, sizeof(sbuf)));
1afe1d14
DA
1038 perf_evlist__close(evlist);
1039 goto out;
1040 }
1041
1042 rc = 0;
1043
1044out:
1045 return rc;
bcf6edcd 1046}
87419c9a 1047#endif
bcf6edcd 1048
3786063a 1049static int read_events(struct perf_kvm_stat *kvm)
bcf6edcd 1050{
bcf6edcd
XG
1051 int ret;
1052
de332ac4
DA
1053 struct perf_tool eops = {
1054 .sample = process_sample_event,
1055 .comm = perf_event__process_comm,
0a8cb85c 1056 .ordered_events = true,
de332ac4 1057 };
f5fc1412 1058 struct perf_data_file file = {
476b3a86 1059 .path = kvm->file_name,
f5fc1412
JO
1060 .mode = PERF_DATA_MODE_READ,
1061 };
de332ac4
DA
1062
1063 kvm->tool = eops;
f5fc1412 1064 kvm->session = perf_session__new(&file, false, &kvm->tool);
de332ac4 1065 if (!kvm->session) {
bcf6edcd
XG
1066 pr_err("Initializing perf session failed\n");
1067 return -EINVAL;
1068 }
1069
0a7e6d1b 1070 symbol__init(&kvm->session->header.env);
14d37f38 1071
de332ac4 1072 if (!perf_session__has_traces(kvm->session, "kvm record"))
bcf6edcd
XG
1073 return -EINVAL;
1074
1075 /*
1076 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1077 * traced in the old kernel.
1078 */
1afe1d14 1079 ret = cpu_isa_config(kvm);
bcf6edcd
XG
1080 if (ret < 0)
1081 return ret;
1082
de332ac4 1083 return perf_session__process_events(kvm->session, &kvm->tool);
bcf6edcd
XG
1084}
1085
2e73f00f
DA
1086static int parse_target_str(struct perf_kvm_stat *kvm)
1087{
1088 if (kvm->pid_str) {
1089 kvm->pid_list = intlist__new(kvm->pid_str);
1090 if (kvm->pid_list == NULL) {
1091 pr_err("Error parsing process id string\n");
1092 return -EINVAL;
1093 }
1094 }
1095
1096 return 0;
1097}
1098
3786063a 1099static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
bcf6edcd
XG
1100{
1101 int ret = -EINVAL;
de332ac4 1102 int vcpu = kvm->trace_vcpu;
bcf6edcd 1103
2e73f00f
DA
1104 if (parse_target_str(kvm) != 0)
1105 goto exit;
1106
bcf6edcd
XG
1107 if (!verify_vcpu(vcpu))
1108 goto exit;
1109
de332ac4 1110 if (!select_key(kvm))
bcf6edcd
XG
1111 goto exit;
1112
de332ac4 1113 if (!register_kvm_events_ops(kvm))
bcf6edcd
XG
1114 goto exit;
1115
de332ac4 1116 init_kvm_event_record(kvm);
bcf6edcd
XG
1117 setup_pager();
1118
de332ac4 1119 ret = read_events(kvm);
bcf6edcd
XG
1120 if (ret)
1121 goto exit;
1122
de332ac4
DA
1123 sort_result(kvm);
1124 print_result(kvm);
1125
bcf6edcd
XG
1126exit:
1127 return ret;
1128}
1129
bcf6edcd
XG
1130#define STRDUP_FAIL_EXIT(s) \
1131 ({ char *_p; \
1132 _p = strdup(s); \
1133 if (!_p) \
1134 return -ENOMEM; \
1135 _p; \
1136 })
1137
3786063a
XG
1138static int
1139kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
bcf6edcd 1140{
9daa8123 1141 unsigned int rec_argc, i, j, events_tp_size;
bcf6edcd 1142 const char **rec_argv;
8fdd84c4
DA
1143 const char * const record_args[] = {
1144 "record",
1145 "-R",
8fdd84c4
DA
1146 "-m", "1024",
1147 "-c", "1",
1148 };
9daa8123
AY
1149 const char * const *events_tp;
1150 events_tp_size = 0;
1151
1152 for (events_tp = kvm_events_tp; *events_tp; events_tp++)
1153 events_tp_size++;
bcf6edcd 1154
8fdd84c4 1155 rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
9daa8123 1156 2 * events_tp_size;
bcf6edcd
XG
1157 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1158
1159 if (rec_argv == NULL)
1160 return -ENOMEM;
1161
1162 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1163 rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
1164
9daa8123 1165 for (j = 0; j < events_tp_size; j++) {
8fdd84c4
DA
1166 rec_argv[i++] = "-e";
1167 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
1168 }
1169
bcf6edcd 1170 rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
de332ac4 1171 rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
bcf6edcd
XG
1172
1173 for (j = 1; j < (unsigned int)argc; j++, i++)
1174 rec_argv[i] = argv[j];
1175
1176 return cmd_record(i, rec_argv, NULL);
1177}
1178
3786063a
XG
1179static int
1180kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
de332ac4
DA
1181{
1182 const struct option kvm_events_report_options[] = {
1183 OPT_STRING(0, "event", &kvm->report_event, "report event",
3be8e2a0
AY
1184 "event for reporting: vmexit, "
1185 "mmio (x86 only), ioport (x86 only)"),
de332ac4
DA
1186 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1187 "vcpu id to report"),
1188 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1189 "key for sorting: sample(sort by samples number)"
1190 " time (sort by avg time)"),
2e73f00f
DA
1191 OPT_STRING('p', "pid", &kvm->pid_str, "pid",
1192 "analyze events only for given process id(s)"),
de332ac4
DA
1193 OPT_END()
1194 };
bcf6edcd 1195
de332ac4
DA
1196 const char * const kvm_events_report_usage[] = {
1197 "perf kvm stat report [<options>]",
1198 NULL
1199 };
bcf6edcd 1200
bcf6edcd
XG
1201 if (argc) {
1202 argc = parse_options(argc, argv,
1203 kvm_events_report_options,
1204 kvm_events_report_usage, 0);
1205 if (argc)
1206 usage_with_options(kvm_events_report_usage,
1207 kvm_events_report_options);
1208 }
1209
de332ac4 1210 return kvm_events_report_vcpu(kvm);
bcf6edcd
XG
1211}
1212
87419c9a 1213#ifdef HAVE_TIMERFD_SUPPORT
1afe1d14
DA
1214static struct perf_evlist *kvm_live_event_list(void)
1215{
1216 struct perf_evlist *evlist;
1217 char *tp, *name, *sys;
1afe1d14 1218 int err = -1;
9daa8123 1219 const char * const *events_tp;
1afe1d14
DA
1220
1221 evlist = perf_evlist__new();
1222 if (evlist == NULL)
1223 return NULL;
1224
9daa8123 1225 for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
1afe1d14 1226
9daa8123 1227 tp = strdup(*events_tp);
1afe1d14
DA
1228 if (tp == NULL)
1229 goto out;
1230
1231 /* split tracepoint into subsystem and name */
1232 sys = tp;
1233 name = strchr(tp, ':');
1234 if (name == NULL) {
1235 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
9daa8123 1236 *events_tp);
1afe1d14
DA
1237 free(tp);
1238 goto out;
1239 }
1240 *name = '\0';
1241 name++;
1242
1243 if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
9daa8123 1244 pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
1afe1d14
DA
1245 free(tp);
1246 goto out;
1247 }
1248
1249 free(tp);
1250 }
1251
1252 err = 0;
1253
1254out:
1255 if (err) {
1256 perf_evlist__delete(evlist);
1257 evlist = NULL;
1258 }
1259
1260 return evlist;
1261}
1262
1263static int kvm_events_live(struct perf_kvm_stat *kvm,
1264 int argc, const char **argv)
1265{
1266 char errbuf[BUFSIZ];
1267 int err;
1268
1269 const struct option live_options[] = {
1270 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1271 "record events on existing process id"),
994a1f78
JO
1272 OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
1273 "number of mmap data pages",
1274 perf_evlist__parse_mmap_pages),
1afe1d14
DA
1275 OPT_INCR('v', "verbose", &verbose,
1276 "be more verbose (show counter open errors, etc)"),
1277 OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
1278 "system-wide collection from all CPUs"),
1279 OPT_UINTEGER('d', "display", &kvm->display_time,
1280 "time in seconds between display updates"),
1281 OPT_STRING(0, "event", &kvm->report_event, "report event",
1282 "event for reporting: vmexit, mmio, ioport"),
1283 OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
1284 "vcpu id to report"),
1285 OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
1286 "key for sorting: sample(sort by samples number)"
1287 " time (sort by avg time)"),
70f7b4a7 1288 OPT_U64(0, "duration", &kvm->duration,
3be8e2a0
AY
1289 "show events other than"
1290 " HLT (x86 only) or Wait state (s390 only)"
1291 " that take longer than duration usecs"),
1afe1d14
DA
1292 OPT_END()
1293 };
1294 const char * const live_usage[] = {
1295 "perf kvm stat live [<options>]",
1296 NULL
1297 };
f5fc1412
JO
1298 struct perf_data_file file = {
1299 .mode = PERF_DATA_MODE_WRITE,
1300 };
1afe1d14
DA
1301
1302
1303 /* event handling */
1304 kvm->tool.sample = process_sample_event;
1305 kvm->tool.comm = perf_event__process_comm;
1306 kvm->tool.exit = perf_event__process_exit;
1307 kvm->tool.fork = perf_event__process_fork;
1308 kvm->tool.lost = process_lost_event;
0a8cb85c 1309 kvm->tool.ordered_events = true;
1afe1d14
DA
1310 perf_tool__fill_defaults(&kvm->tool);
1311
1312 /* set defaults */
1313 kvm->display_time = 1;
1314 kvm->opts.user_interval = 1;
1315 kvm->opts.mmap_pages = 512;
1316 kvm->opts.target.uses_mmap = false;
1317 kvm->opts.target.uid_str = NULL;
1318 kvm->opts.target.uid = UINT_MAX;
1319
0a7e6d1b 1320 symbol__init(NULL);
1afe1d14
DA
1321 disable_buildid_cache();
1322
1323 use_browser = 0;
1324 setup_browser(false);
1325
1326 if (argc) {
1327 argc = parse_options(argc, argv, live_options,
1328 live_usage, 0);
1329 if (argc)
1330 usage_with_options(live_usage, live_options);
1331 }
1332
70f7b4a7
DA
1333 kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
1334
1afe1d14
DA
1335 /*
1336 * target related setups
1337 */
602ad878 1338 err = target__validate(&kvm->opts.target);
1afe1d14 1339 if (err) {
602ad878 1340 target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
1afe1d14
DA
1341 ui__warning("%s", errbuf);
1342 }
1343
602ad878 1344 if (target__none(&kvm->opts.target))
1afe1d14
DA
1345 kvm->opts.target.system_wide = true;
1346
1347
1348 /*
1349 * generate the event list
1350 */
1351 kvm->evlist = kvm_live_event_list();
1352 if (kvm->evlist == NULL) {
1353 err = -1;
1354 goto out;
1355 }
1356
1357 symbol_conf.nr_events = kvm->evlist->nr_entries;
1358
1359 if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
1360 usage_with_options(live_usage, live_options);
1361
1362 /*
1363 * perf session
1364 */
f5fc1412 1365 kvm->session = perf_session__new(&file, false, &kvm->tool);
1afe1d14
DA
1366 if (kvm->session == NULL) {
1367 err = -ENOMEM;
1368 goto out;
1369 }
1370 kvm->session->evlist = kvm->evlist;
1371 perf_session__set_id_hdr_size(kvm->session);
a33fbd56
ACM
1372 machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
1373 kvm->evlist->threads, false);
1afe1d14
DA
1374 err = kvm_live_open_events(kvm);
1375 if (err)
1376 goto out;
1377
1378 err = kvm_events_live_report(kvm);
1379
1380out:
1381 exit_browser(0);
1382
1383 if (kvm->session)
1384 perf_session__delete(kvm->session);
1385 kvm->session = NULL;
03ad9747 1386 if (kvm->evlist)
1afe1d14 1387 perf_evlist__delete(kvm->evlist);
1afe1d14
DA
1388
1389 return err;
1390}
87419c9a 1391#endif
1afe1d14 1392
bcf6edcd
XG
1393static void print_kvm_stat_usage(void)
1394{
1395 printf("Usage: perf kvm stat <command>\n\n");
1396
1397 printf("# Available commands:\n");
1398 printf("\trecord: record kvm events\n");
1399 printf("\treport: report statistical data of kvm events\n");
1afe1d14 1400 printf("\tlive: live reporting of statistical data of kvm events\n");
bcf6edcd
XG
1401
1402 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1403}
1404
3786063a 1405static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
bcf6edcd 1406{
3786063a
XG
1407 struct perf_kvm_stat kvm = {
1408 .file_name = file_name,
1409
1410 .trace_vcpu = -1,
1411 .report_event = "vmexit",
1412 .sort_key = "sample",
1413
3786063a
XG
1414 };
1415
bcf6edcd
XG
1416 if (argc == 1) {
1417 print_kvm_stat_usage();
1418 goto perf_stat;
1419 }
1420
1421 if (!strncmp(argv[1], "rec", 3))
3786063a 1422 return kvm_events_record(&kvm, argc - 1, argv + 1);
bcf6edcd
XG
1423
1424 if (!strncmp(argv[1], "rep", 3))
3786063a 1425 return kvm_events_report(&kvm, argc - 1 , argv + 1);
bcf6edcd 1426
87419c9a 1427#ifdef HAVE_TIMERFD_SUPPORT
1afe1d14
DA
1428 if (!strncmp(argv[1], "live", 4))
1429 return kvm_events_live(&kvm, argc - 1 , argv + 1);
87419c9a 1430#endif
1afe1d14 1431
bcf6edcd
XG
1432perf_stat:
1433 return cmd_stat(argc, argv, NULL);
1434}
da50ad69 1435#endif /* HAVE_KVM_STAT_SUPPORT */
bcf6edcd 1436
3786063a 1437static int __cmd_record(const char *file_name, int argc, const char **argv)
a1645ce1
ZY
1438{
1439 int rec_argc, i = 0, j;
1440 const char **rec_argv;
1441
1442 rec_argc = argc + 2;
1443 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1444 rec_argv[i++] = strdup("record");
1445 rec_argv[i++] = strdup("-o");
3786063a 1446 rec_argv[i++] = strdup(file_name);
a1645ce1
ZY
1447 for (j = 1; j < argc; j++, i++)
1448 rec_argv[i] = argv[j];
1449
1450 BUG_ON(i != rec_argc);
1451
1452 return cmd_record(i, rec_argv, NULL);
1453}
1454
3786063a 1455static int __cmd_report(const char *file_name, int argc, const char **argv)
a1645ce1
ZY
1456{
1457 int rec_argc, i = 0, j;
1458 const char **rec_argv;
1459
1460 rec_argc = argc + 2;
1461 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1462 rec_argv[i++] = strdup("report");
1463 rec_argv[i++] = strdup("-i");
3786063a 1464 rec_argv[i++] = strdup(file_name);
a1645ce1
ZY
1465 for (j = 1; j < argc; j++, i++)
1466 rec_argv[i] = argv[j];
1467
1468 BUG_ON(i != rec_argc);
1469
1470 return cmd_report(i, rec_argv, NULL);
1471}
1472
3786063a
XG
1473static int
1474__cmd_buildid_list(const char *file_name, int argc, const char **argv)
a1645ce1
ZY
1475{
1476 int rec_argc, i = 0, j;
1477 const char **rec_argv;
1478
1479 rec_argc = argc + 2;
1480 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1481 rec_argv[i++] = strdup("buildid-list");
1482 rec_argv[i++] = strdup("-i");
3786063a 1483 rec_argv[i++] = strdup(file_name);
a1645ce1
ZY
1484 for (j = 1; j < argc; j++, i++)
1485 rec_argv[i] = argv[j];
1486
1487 BUG_ON(i != rec_argc);
1488
1489 return cmd_buildid_list(i, rec_argv, NULL);
1490}
1491
1d037ca1 1492int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
a1645ce1 1493{
20914ce5 1494 const char *file_name = NULL;
de332ac4 1495 const struct option kvm_options[] = {
3786063a 1496 OPT_STRING('i', "input", &file_name, "file",
de332ac4 1497 "Input file name"),
3786063a 1498 OPT_STRING('o', "output", &file_name, "file",
de332ac4
DA
1499 "Output file name"),
1500 OPT_BOOLEAN(0, "guest", &perf_guest,
1501 "Collect guest os data"),
1502 OPT_BOOLEAN(0, "host", &perf_host,
1503 "Collect host os data"),
1504 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
1505 "guest mount directory under which every guest os"
1506 " instance has a subdir"),
1507 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
1508 "file", "file saving guest os vmlinux"),
1509 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
1510 "file", "file saving guest os /proc/kallsyms"),
1511 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
1512 "file", "file saving guest os /proc/modules"),
100b9073
DY
1513 OPT_INCR('v', "verbose", &verbose,
1514 "be more verbose (show counter open errors, etc)"),
de332ac4
DA
1515 OPT_END()
1516 };
1517
09a71b97
RR
1518 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1519 "buildid-list", "stat", NULL };
1520 const char *kvm_usage[] = { NULL, NULL };
de332ac4 1521
1aed2671
JR
1522 perf_host = 0;
1523 perf_guest = 1;
a1645ce1 1524
09a71b97
RR
1525 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1526 PARSE_OPT_STOP_AT_NON_OPTION);
a1645ce1
ZY
1527 if (!argc)
1528 usage_with_options(kvm_usage, kvm_options);
1529
1530 if (!perf_host)
1531 perf_guest = 1;
1532
3786063a 1533 if (!file_name) {
e1a2b174 1534 file_name = get_filename_for_perf_kvm();
de332ac4 1535
3786063a 1536 if (!file_name) {
de332ac4
DA
1537 pr_err("Failed to allocate memory for filename\n");
1538 return -ENOMEM;
1539 }
a1645ce1
ZY
1540 }
1541
1542 if (!strncmp(argv[0], "rec", 3))
3786063a 1543 return __cmd_record(file_name, argc, argv);
a1645ce1 1544 else if (!strncmp(argv[0], "rep", 3))
3786063a 1545 return __cmd_report(file_name, argc, argv);
a1645ce1
ZY
1546 else if (!strncmp(argv[0], "diff", 4))
1547 return cmd_diff(argc, argv, NULL);
1548 else if (!strncmp(argv[0], "top", 3))
1549 return cmd_top(argc, argv, NULL);
1550 else if (!strncmp(argv[0], "buildid-list", 12))
3786063a 1551 return __cmd_buildid_list(file_name, argc, argv);
da50ad69 1552#ifdef HAVE_KVM_STAT_SUPPORT
bcf6edcd 1553 else if (!strncmp(argv[0], "stat", 4))
3786063a 1554 return kvm_cmd_stat(file_name, argc, argv);
7321090f 1555#endif
a1645ce1
ZY
1556 else
1557 usage_with_options(kvm_usage, kvm_options);
1558
1559 return 0;
1560}
This page took 0.266206 seconds and 5 git commands to generate.