4 #include "util/evsel.h"
5 #include "util/evlist.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"
12 #include "util/intlist.h"
13 #include "util/parse-options.h"
14 #include "util/trace-event.h"
15 #include "util/debug.h"
16 #include <lk/debugfs.h>
17 #include "util/tool.h"
18 #include "util/stat.h"
21 #include <sys/prctl.h>
22 #include <sys/timerfd.h>
25 #include <semaphore.h>
29 #if defined(__i386__) || defined(__x86_64__)
35 #define INVALID_KEY (~0ULL)
40 struct kvm_event_stats
{
46 struct list_head hash_entry
;
51 struct kvm_event_stats total
;
53 #define DEFAULT_VCPU_NUM 8
55 struct kvm_event_stats
*vcpu
;
58 typedef int (*key_cmp_fun
)(struct kvm_event
*, struct kvm_event
*, int);
60 struct kvm_event_key
{
68 struct kvm_events_ops
{
69 bool (*is_begin_event
)(struct perf_evsel
*evsel
,
70 struct perf_sample
*sample
,
71 struct event_key
*key
);
72 bool (*is_end_event
)(struct perf_evsel
*evsel
,
73 struct perf_sample
*sample
, struct event_key
*key
);
74 void (*decode_key
)(struct perf_kvm_stat
*kvm
, struct event_key
*key
,
79 struct exit_reasons_table
{
80 unsigned long exit_code
;
84 #define EVENTS_BITS 12
85 #define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
87 struct perf_kvm_stat
{
88 struct perf_tool tool
;
89 struct perf_record_opts opts
;
90 struct perf_evlist
*evlist
;
91 struct perf_session
*session
;
93 const char *file_name
;
94 const char *report_event
;
98 struct exit_reasons_table
*exit_reasons
;
99 int exit_reasons_size
;
100 const char *exit_reasons_isa
;
102 struct kvm_events_ops
*events_ops
;
104 struct list_head kvm_events_cache
[EVENTS_CACHE_SIZE
];
111 struct intlist
*pid_list
;
113 struct rb_root result
;
116 unsigned int display_time
;
121 static void exit_event_get_key(struct perf_evsel
*evsel
,
122 struct perf_sample
*sample
,
123 struct event_key
*key
)
126 key
->key
= perf_evsel__intval(evsel
, sample
, "exit_reason");
129 static bool kvm_exit_event(struct perf_evsel
*evsel
)
131 return !strcmp(evsel
->name
, "kvm:kvm_exit");
134 static bool exit_event_begin(struct perf_evsel
*evsel
,
135 struct perf_sample
*sample
, struct event_key
*key
)
137 if (kvm_exit_event(evsel
)) {
138 exit_event_get_key(evsel
, sample
, key
);
145 static bool kvm_entry_event(struct perf_evsel
*evsel
)
147 return !strcmp(evsel
->name
, "kvm:kvm_entry");
150 static bool exit_event_end(struct perf_evsel
*evsel
,
151 struct perf_sample
*sample __maybe_unused
,
152 struct event_key
*key __maybe_unused
)
154 return kvm_entry_event(evsel
);
157 static struct exit_reasons_table vmx_exit_reasons
[] = {
161 static struct exit_reasons_table svm_exit_reasons
[] = {
165 static const char *get_exit_reason(struct perf_kvm_stat
*kvm
, u64 exit_code
)
167 int i
= kvm
->exit_reasons_size
;
168 struct exit_reasons_table
*tbl
= kvm
->exit_reasons
;
171 if (tbl
->exit_code
== exit_code
)
176 pr_err("unknown kvm exit code:%lld on %s\n",
177 (unsigned long long)exit_code
, kvm
->exit_reasons_isa
);
181 static void exit_event_decode_key(struct perf_kvm_stat
*kvm
,
182 struct event_key
*key
,
185 const char *exit_reason
= get_exit_reason(kvm
, key
->key
);
187 scnprintf(decode
, 20, "%s", exit_reason
);
190 static struct kvm_events_ops exit_events
= {
191 .is_begin_event
= exit_event_begin
,
192 .is_end_event
= exit_event_end
,
193 .decode_key
= exit_event_decode_key
,
198 * For the mmio events, we treat:
199 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
200 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
202 static void mmio_event_get_key(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
203 struct event_key
*key
)
205 key
->key
= perf_evsel__intval(evsel
, sample
, "gpa");
206 key
->info
= perf_evsel__intval(evsel
, sample
, "type");
209 #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
210 #define KVM_TRACE_MMIO_READ 1
211 #define KVM_TRACE_MMIO_WRITE 2
213 static bool mmio_event_begin(struct perf_evsel
*evsel
,
214 struct perf_sample
*sample
, struct event_key
*key
)
216 /* MMIO read begin event in kernel. */
217 if (kvm_exit_event(evsel
))
220 /* MMIO write begin event in kernel. */
221 if (!strcmp(evsel
->name
, "kvm:kvm_mmio") &&
222 perf_evsel__intval(evsel
, sample
, "type") == KVM_TRACE_MMIO_WRITE
) {
223 mmio_event_get_key(evsel
, sample
, key
);
230 static bool mmio_event_end(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
231 struct event_key
*key
)
233 /* MMIO write end event in kernel. */
234 if (kvm_entry_event(evsel
))
237 /* MMIO read end event in kernel.*/
238 if (!strcmp(evsel
->name
, "kvm:kvm_mmio") &&
239 perf_evsel__intval(evsel
, sample
, "type") == KVM_TRACE_MMIO_READ
) {
240 mmio_event_get_key(evsel
, sample
, key
);
247 static void mmio_event_decode_key(struct perf_kvm_stat
*kvm __maybe_unused
,
248 struct event_key
*key
,
251 scnprintf(decode
, 20, "%#lx:%s", (unsigned long)key
->key
,
252 key
->info
== KVM_TRACE_MMIO_WRITE
? "W" : "R");
255 static struct kvm_events_ops mmio_events
= {
256 .is_begin_event
= mmio_event_begin
,
257 .is_end_event
= mmio_event_end
,
258 .decode_key
= mmio_event_decode_key
,
259 .name
= "MMIO Access"
262 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
263 static void ioport_event_get_key(struct perf_evsel
*evsel
,
264 struct perf_sample
*sample
,
265 struct event_key
*key
)
267 key
->key
= perf_evsel__intval(evsel
, sample
, "port");
268 key
->info
= perf_evsel__intval(evsel
, sample
, "rw");
271 static bool ioport_event_begin(struct perf_evsel
*evsel
,
272 struct perf_sample
*sample
,
273 struct event_key
*key
)
275 if (!strcmp(evsel
->name
, "kvm:kvm_pio")) {
276 ioport_event_get_key(evsel
, sample
, key
);
283 static bool ioport_event_end(struct perf_evsel
*evsel
,
284 struct perf_sample
*sample __maybe_unused
,
285 struct event_key
*key __maybe_unused
)
287 return kvm_entry_event(evsel
);
290 static void ioport_event_decode_key(struct perf_kvm_stat
*kvm __maybe_unused
,
291 struct event_key
*key
,
294 scnprintf(decode
, 20, "%#llx:%s", (unsigned long long)key
->key
,
295 key
->info
? "POUT" : "PIN");
298 static struct kvm_events_ops ioport_events
= {
299 .is_begin_event
= ioport_event_begin
,
300 .is_end_event
= ioport_event_end
,
301 .decode_key
= ioport_event_decode_key
,
302 .name
= "IO Port Access"
305 static bool register_kvm_events_ops(struct perf_kvm_stat
*kvm
)
309 if (!strcmp(kvm
->report_event
, "vmexit"))
310 kvm
->events_ops
= &exit_events
;
311 else if (!strcmp(kvm
->report_event
, "mmio"))
312 kvm
->events_ops
= &mmio_events
;
313 else if (!strcmp(kvm
->report_event
, "ioport"))
314 kvm
->events_ops
= &ioport_events
;
316 pr_err("Unknown report event:%s\n", kvm
->report_event
);
323 struct vcpu_event_record
{
326 struct kvm_event
*last_event
;
330 static void init_kvm_event_record(struct perf_kvm_stat
*kvm
)
334 for (i
= 0; i
< EVENTS_CACHE_SIZE
; i
++)
335 INIT_LIST_HEAD(&kvm
->kvm_events_cache
[i
]);
338 static void clear_events_cache_stats(struct list_head
*kvm_events_cache
)
340 struct list_head
*head
;
341 struct kvm_event
*event
;
345 for (i
= 0; i
< EVENTS_CACHE_SIZE
; i
++) {
346 head
= &kvm_events_cache
[i
];
347 list_for_each_entry(event
, head
, hash_entry
) {
348 /* reset stats for event */
349 event
->total
.time
= 0;
350 init_stats(&event
->total
.stats
);
352 for (j
= 0; j
< event
->max_vcpu
; ++j
) {
353 event
->vcpu
[j
].time
= 0;
354 init_stats(&event
->vcpu
[j
].stats
);
360 static int kvm_events_hash_fn(u64 key
)
362 return key
& (EVENTS_CACHE_SIZE
- 1);
365 static bool kvm_event_expand(struct kvm_event
*event
, int vcpu_id
)
367 int old_max_vcpu
= event
->max_vcpu
;
370 if (vcpu_id
< event
->max_vcpu
)
373 while (event
->max_vcpu
<= vcpu_id
)
374 event
->max_vcpu
+= DEFAULT_VCPU_NUM
;
377 event
->vcpu
= realloc(event
->vcpu
,
378 event
->max_vcpu
* sizeof(*event
->vcpu
));
381 pr_err("Not enough memory\n");
385 memset(event
->vcpu
+ old_max_vcpu
, 0,
386 (event
->max_vcpu
- old_max_vcpu
) * sizeof(*event
->vcpu
));
390 static struct kvm_event
*kvm_alloc_init_event(struct event_key
*key
)
392 struct kvm_event
*event
;
394 event
= zalloc(sizeof(*event
));
396 pr_err("Not enough memory\n");
404 static struct kvm_event
*find_create_kvm_event(struct perf_kvm_stat
*kvm
,
405 struct event_key
*key
)
407 struct kvm_event
*event
;
408 struct list_head
*head
;
410 BUG_ON(key
->key
== INVALID_KEY
);
412 head
= &kvm
->kvm_events_cache
[kvm_events_hash_fn(key
->key
)];
413 list_for_each_entry(event
, head
, hash_entry
) {
414 if (event
->key
.key
== key
->key
&& event
->key
.info
== key
->info
)
418 event
= kvm_alloc_init_event(key
);
422 list_add(&event
->hash_entry
, head
);
426 static bool handle_begin_event(struct perf_kvm_stat
*kvm
,
427 struct vcpu_event_record
*vcpu_record
,
428 struct event_key
*key
, u64 timestamp
)
430 struct kvm_event
*event
= NULL
;
432 if (key
->key
!= INVALID_KEY
)
433 event
= find_create_kvm_event(kvm
, key
);
435 vcpu_record
->last_event
= event
;
436 vcpu_record
->start_time
= timestamp
;
441 kvm_update_event_stats(struct kvm_event_stats
*kvm_stats
, u64 time_diff
)
443 kvm_stats
->time
+= time_diff
;
444 update_stats(&kvm_stats
->stats
, time_diff
);
447 static double kvm_event_rel_stddev(int vcpu_id
, struct kvm_event
*event
)
449 struct kvm_event_stats
*kvm_stats
= &event
->total
;
452 kvm_stats
= &event
->vcpu
[vcpu_id
];
454 return rel_stddev_stats(stddev_stats(&kvm_stats
->stats
),
455 avg_stats(&kvm_stats
->stats
));
458 static bool update_kvm_event(struct kvm_event
*event
, int vcpu_id
,
462 kvm_update_event_stats(&event
->total
, time_diff
);
466 if (!kvm_event_expand(event
, vcpu_id
))
469 kvm_update_event_stats(&event
->vcpu
[vcpu_id
], time_diff
);
473 static bool handle_end_event(struct perf_kvm_stat
*kvm
,
474 struct vcpu_event_record
*vcpu_record
,
475 struct event_key
*key
,
478 struct kvm_event
*event
;
479 u64 time_begin
, time_diff
;
482 if (kvm
->trace_vcpu
== -1)
485 vcpu
= vcpu_record
->vcpu_id
;
487 event
= vcpu_record
->last_event
;
488 time_begin
= vcpu_record
->start_time
;
490 /* The begin event is not caught. */
495 * In some case, the 'begin event' only records the start timestamp,
496 * the actual event is recognized in the 'end event' (e.g. mmio-event).
499 /* Both begin and end events did not get the key. */
500 if (!event
&& key
->key
== INVALID_KEY
)
504 event
= find_create_kvm_event(kvm
, key
);
509 vcpu_record
->last_event
= NULL
;
510 vcpu_record
->start_time
= 0;
512 /* seems to happen once in a while during live mode */
513 if (timestamp
< time_begin
) {
514 pr_debug("End time before begin time; skipping event.\n");
518 time_diff
= timestamp
- time_begin
;
519 return update_kvm_event(event
, vcpu
, time_diff
);
523 struct vcpu_event_record
*per_vcpu_record(struct thread
*thread
,
524 struct perf_evsel
*evsel
,
525 struct perf_sample
*sample
)
527 /* Only kvm_entry records vcpu id. */
528 if (!thread
->priv
&& kvm_entry_event(evsel
)) {
529 struct vcpu_event_record
*vcpu_record
;
531 vcpu_record
= zalloc(sizeof(*vcpu_record
));
533 pr_err("%s: Not enough memory\n", __func__
);
537 vcpu_record
->vcpu_id
= perf_evsel__intval(evsel
, sample
, "vcpu_id");
538 thread
->priv
= vcpu_record
;
544 static bool handle_kvm_event(struct perf_kvm_stat
*kvm
,
545 struct thread
*thread
,
546 struct perf_evsel
*evsel
,
547 struct perf_sample
*sample
)
549 struct vcpu_event_record
*vcpu_record
;
550 struct event_key key
= {.key
= INVALID_KEY
};
552 vcpu_record
= per_vcpu_record(thread
, evsel
, sample
);
556 /* only process events for vcpus user cares about */
557 if ((kvm
->trace_vcpu
!= -1) &&
558 (kvm
->trace_vcpu
!= vcpu_record
->vcpu_id
))
561 if (kvm
->events_ops
->is_begin_event(evsel
, sample
, &key
))
562 return handle_begin_event(kvm
, vcpu_record
, &key
, sample
->time
);
564 if (kvm
->events_ops
->is_end_event(evsel
, sample
, &key
))
565 return handle_end_event(kvm
, vcpu_record
, &key
, sample
->time
);
570 #define GET_EVENT_KEY(func, field) \
571 static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
574 return event->total.field; \
576 if (vcpu >= event->max_vcpu) \
579 return event->vcpu[vcpu].field; \
582 #define COMPARE_EVENT_KEY(func, field) \
583 GET_EVENT_KEY(func, field) \
584 static int compare_kvm_event_ ## func(struct kvm_event *one, \
585 struct kvm_event *two, int vcpu)\
587 return get_event_ ##func(one, vcpu) > \
588 get_event_ ##func(two, vcpu); \
591 GET_EVENT_KEY(time
, time
);
592 COMPARE_EVENT_KEY(count
, stats
.n
);
593 COMPARE_EVENT_KEY(mean
, stats
.mean
);
594 GET_EVENT_KEY(max
, stats
.max
);
595 GET_EVENT_KEY(min
, stats
.min
);
597 #define DEF_SORT_NAME_KEY(name, compare_key) \
598 { #name, compare_kvm_event_ ## compare_key }
600 static struct kvm_event_key keys
[] = {
601 DEF_SORT_NAME_KEY(sample
, count
),
602 DEF_SORT_NAME_KEY(time
, mean
),
606 static bool select_key(struct perf_kvm_stat
*kvm
)
610 for (i
= 0; keys
[i
].name
; i
++) {
611 if (!strcmp(keys
[i
].name
, kvm
->sort_key
)) {
612 kvm
->compare
= keys
[i
].key
;
617 pr_err("Unknown compare key:%s\n", kvm
->sort_key
);
621 static void insert_to_result(struct rb_root
*result
, struct kvm_event
*event
,
622 key_cmp_fun bigger
, int vcpu
)
624 struct rb_node
**rb
= &result
->rb_node
;
625 struct rb_node
*parent
= NULL
;
629 p
= container_of(*rb
, struct kvm_event
, rb
);
632 if (bigger(event
, p
, vcpu
))
633 rb
= &(*rb
)->rb_left
;
635 rb
= &(*rb
)->rb_right
;
638 rb_link_node(&event
->rb
, parent
, rb
);
639 rb_insert_color(&event
->rb
, result
);
643 update_total_count(struct perf_kvm_stat
*kvm
, struct kvm_event
*event
)
645 int vcpu
= kvm
->trace_vcpu
;
647 kvm
->total_count
+= get_event_count(event
, vcpu
);
648 kvm
->total_time
+= get_event_time(event
, vcpu
);
651 static bool event_is_valid(struct kvm_event
*event
, int vcpu
)
653 return !!get_event_count(event
, vcpu
);
656 static void sort_result(struct perf_kvm_stat
*kvm
)
659 int vcpu
= kvm
->trace_vcpu
;
660 struct kvm_event
*event
;
662 for (i
= 0; i
< EVENTS_CACHE_SIZE
; i
++) {
663 list_for_each_entry(event
, &kvm
->kvm_events_cache
[i
], hash_entry
) {
664 if (event_is_valid(event
, vcpu
)) {
665 update_total_count(kvm
, event
);
666 insert_to_result(&kvm
->result
, event
,
673 /* returns left most element of result, and erase it */
674 static struct kvm_event
*pop_from_result(struct rb_root
*result
)
676 struct rb_node
*node
= rb_first(result
);
681 rb_erase(node
, result
);
682 return container_of(node
, struct kvm_event
, rb
);
685 static void print_vcpu_info(struct perf_kvm_stat
*kvm
)
687 int vcpu
= kvm
->trace_vcpu
;
689 pr_info("Analyze events for ");
692 if (kvm
->opts
.target
.system_wide
)
693 pr_info("all VMs, ");
694 else if (kvm
->opts
.target
.pid
)
695 pr_info("pid(s) %s, ", kvm
->opts
.target
.pid
);
697 pr_info("dazed and confused on what is monitored, ");
701 pr_info("all VCPUs:\n\n");
703 pr_info("VCPU %d:\n\n", vcpu
);
706 static void show_timeofday(void)
712 gettimeofday(&tv
, NULL
);
713 if (localtime_r(&tv
.tv_sec
, <ime
)) {
714 strftime(date
, sizeof(date
), "%H:%M:%S", <ime
);
715 pr_info("%s.%06ld", date
, tv
.tv_usec
);
717 pr_info("00:00:00.000000");
722 static void print_result(struct perf_kvm_stat
*kvm
)
725 struct kvm_event
*event
;
726 int vcpu
= kvm
->trace_vcpu
;
734 print_vcpu_info(kvm
);
735 pr_info("%20s ", kvm
->events_ops
->name
);
736 pr_info("%10s ", "Samples");
737 pr_info("%9s ", "Samples%");
739 pr_info("%9s ", "Time%");
740 pr_info("%10s ", "Min Time");
741 pr_info("%10s ", "Max Time");
742 pr_info("%16s ", "Avg time");
745 while ((event
= pop_from_result(&kvm
->result
))) {
746 u64 ecount
, etime
, max
, min
;
748 ecount
= get_event_count(event
, vcpu
);
749 etime
= get_event_time(event
, vcpu
);
750 max
= get_event_max(event
, vcpu
);
751 min
= get_event_min(event
, vcpu
);
753 kvm
->events_ops
->decode_key(kvm
, &event
->key
, decode
);
754 pr_info("%20s ", decode
);
755 pr_info("%10llu ", (unsigned long long)ecount
);
756 pr_info("%8.2f%% ", (double)ecount
/ kvm
->total_count
* 100);
757 pr_info("%8.2f%% ", (double)etime
/ kvm
->total_time
* 100);
758 pr_info("%8" PRIu64
"us ", min
/ 1000);
759 pr_info("%8" PRIu64
"us ", max
/ 1000);
760 pr_info("%9.2fus ( +-%7.2f%% )", (double)etime
/ ecount
/1e3
,
761 kvm_event_rel_stddev(vcpu
, event
));
765 pr_info("\nTotal Samples:%" PRIu64
", Total events handled time:%.2fus.\n\n",
766 kvm
->total_count
, kvm
->total_time
/ 1e3
);
768 if (kvm
->lost_events
)
769 pr_info("\nLost events: %" PRIu64
"\n\n", kvm
->lost_events
);
772 static int process_lost_event(struct perf_tool
*tool
,
773 union perf_event
*event __maybe_unused
,
774 struct perf_sample
*sample __maybe_unused
,
775 struct machine
*machine __maybe_unused
)
777 struct perf_kvm_stat
*kvm
= container_of(tool
, struct perf_kvm_stat
, tool
);
783 static bool skip_sample(struct perf_kvm_stat
*kvm
,
784 struct perf_sample
*sample
)
786 if (kvm
->pid_list
&& intlist__find(kvm
->pid_list
, sample
->pid
) == NULL
)
792 static int process_sample_event(struct perf_tool
*tool
,
793 union perf_event
*event
,
794 struct perf_sample
*sample
,
795 struct perf_evsel
*evsel
,
796 struct machine
*machine
)
798 struct thread
*thread
;
799 struct perf_kvm_stat
*kvm
= container_of(tool
, struct perf_kvm_stat
,
802 if (skip_sample(kvm
, sample
))
805 thread
= machine__findnew_thread(machine
, sample
->tid
);
806 if (thread
== NULL
) {
807 pr_debug("problem processing %d event, skipping it.\n",
812 if (!handle_kvm_event(kvm
, thread
, evsel
, sample
))
818 static int cpu_isa_config(struct perf_kvm_stat
*kvm
)
820 char buf
[64], *cpuid
;
824 err
= get_cpuid(buf
, sizeof(buf
));
826 pr_err("Failed to look up CPU type (Intel or AMD)\n");
831 cpuid
= kvm
->session
->header
.env
.cpuid
;
833 if (strstr(cpuid
, "Intel"))
835 else if (strstr(cpuid
, "AMD"))
838 pr_err("CPU %s is not supported.\n", cpuid
);
843 kvm
->exit_reasons
= vmx_exit_reasons
;
844 kvm
->exit_reasons_size
= ARRAY_SIZE(vmx_exit_reasons
);
845 kvm
->exit_reasons_isa
= "VMX";
851 static bool verify_vcpu(int vcpu
)
853 if (vcpu
!= -1 && vcpu
< 0) {
854 pr_err("Invalid vcpu:%d.\n", vcpu
);
861 /* keeping the max events to a modest level to keep
862 * the processing of samples per mmap smooth.
864 #define PERF_KVM__MAX_EVENTS_PER_MMAP 25
866 static s64
perf_kvm__mmap_read_idx(struct perf_kvm_stat
*kvm
, int idx
,
869 union perf_event
*event
;
870 struct perf_sample sample
;
874 *mmap_time
= ULLONG_MAX
;
875 while ((event
= perf_evlist__mmap_read(kvm
->evlist
, idx
)) != NULL
) {
876 err
= perf_evlist__parse_sample(kvm
->evlist
, event
, &sample
);
878 pr_err("Failed to parse sample\n");
882 err
= perf_session_queue_event(kvm
->session
, event
, &sample
, 0);
884 pr_err("Failed to enqueue sample: %d\n", err
);
888 /* save time stamp of our first sample for this mmap */
890 *mmap_time
= sample
.time
;
892 /* limit events per mmap handled all at once */
894 if (n
== PERF_KVM__MAX_EVENTS_PER_MMAP
)
901 static int perf_kvm__mmap_read(struct perf_kvm_stat
*kvm
)
903 int i
, err
, throttled
= 0;
905 u64 flush_time
= ULLONG_MAX
, mmap_time
;
907 for (i
= 0; i
< kvm
->evlist
->nr_mmaps
; i
++) {
908 n
= perf_kvm__mmap_read_idx(kvm
, i
, &mmap_time
);
912 /* flush time is going to be the minimum of all the individual
913 * mmap times. Essentially, we flush all the samples queued up
914 * from the last pass under our minimal start time -- that leaves
915 * a very small race for samples to come in with a lower timestamp.
916 * The ioctl to return the perf_clock timestamp should close the
919 if (mmap_time
< flush_time
)
920 flush_time
= mmap_time
;
923 if (n
== PERF_KVM__MAX_EVENTS_PER_MMAP
)
927 /* flush queue after each round in which we processed events */
929 kvm
->session
->ordered_samples
.next_flush
= flush_time
;
930 err
= kvm
->tool
.finished_round(&kvm
->tool
, NULL
, kvm
->session
);
932 if (kvm
->lost_events
)
933 pr_info("\nLost events: %" PRIu64
"\n\n",
942 static volatile int done
;
944 static void sig_handler(int sig __maybe_unused
)
949 static int perf_kvm__timerfd_create(struct perf_kvm_stat
*kvm
)
951 struct itimerspec new_value
;
954 kvm
->timerfd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
);
955 if (kvm
->timerfd
< 0) {
956 pr_err("timerfd_create failed\n");
960 new_value
.it_value
.tv_sec
= kvm
->display_time
;
961 new_value
.it_value
.tv_nsec
= 0;
962 new_value
.it_interval
.tv_sec
= kvm
->display_time
;
963 new_value
.it_interval
.tv_nsec
= 0;
965 if (timerfd_settime(kvm
->timerfd
, 0, &new_value
, NULL
) != 0) {
966 pr_err("timerfd_settime failed: %d\n", errno
);
976 static int perf_kvm__handle_timerfd(struct perf_kvm_stat
*kvm
)
981 rc
= read(kvm
->timerfd
, &c
, sizeof(uint64_t));
986 pr_err("Failed to read timer fd: %d\n", errno
);
990 if (rc
!= sizeof(uint64_t)) {
991 pr_err("Error reading timer fd - invalid size returned\n");
996 pr_debug("Missed timer beats: %" PRIu64
"\n", c
-1);
1003 clear_events_cache_stats(kvm
->kvm_events_cache
);
1004 kvm
->total_count
= 0;
1005 kvm
->total_time
= 0;
1006 kvm
->lost_events
= 0;
1011 static int fd_set_nonblock(int fd
)
1015 arg
= fcntl(fd
, F_GETFL
);
1017 pr_err("Failed to get current flags for fd %d\n", fd
);
1021 if (fcntl(fd
, F_SETFL
, arg
| O_NONBLOCK
) < 0) {
1022 pr_err("Failed to set non-block option on fd %d\n", fd
);
1030 int perf_kvm__handle_stdin(struct termios
*tc_now
, struct termios
*tc_save
)
1034 tcsetattr(0, TCSANOW
, tc_now
);
1036 tcsetattr(0, TCSAFLUSH
, tc_save
);
1044 static int kvm_events_live_report(struct perf_kvm_stat
*kvm
)
1046 struct pollfd
*pollfds
= NULL
;
1047 int nr_fds
, nr_stdin
, ret
, err
= -EINVAL
;
1048 struct termios tc
, save
;
1050 /* live flag must be set first */
1053 ret
= cpu_isa_config(kvm
);
1057 if (!verify_vcpu(kvm
->trace_vcpu
) ||
1059 !register_kvm_events_ops(kvm
)) {
1063 init_kvm_event_record(kvm
);
1065 tcgetattr(0, &save
);
1067 tc
.c_lflag
&= ~(ICANON
| ECHO
);
1071 signal(SIGINT
, sig_handler
);
1072 signal(SIGTERM
, sig_handler
);
1074 /* copy pollfds -- need to add timerfd and stdin */
1075 nr_fds
= kvm
->evlist
->nr_fds
;
1076 pollfds
= zalloc(sizeof(struct pollfd
) * (nr_fds
+ 2));
1081 memcpy(pollfds
, kvm
->evlist
->pollfd
,
1082 sizeof(struct pollfd
) * kvm
->evlist
->nr_fds
);
1085 if (perf_kvm__timerfd_create(kvm
) < 0) {
1090 pollfds
[nr_fds
].fd
= kvm
->timerfd
;
1091 pollfds
[nr_fds
].events
= POLLIN
;
1094 pollfds
[nr_fds
].fd
= fileno(stdin
);
1095 pollfds
[nr_fds
].events
= POLLIN
;
1098 if (fd_set_nonblock(fileno(stdin
)) != 0)
1101 /* everything is good - enable the events and process */
1102 perf_evlist__enable(kvm
->evlist
);
1107 rc
= perf_kvm__mmap_read(kvm
);
1111 err
= perf_kvm__handle_timerfd(kvm
);
1115 if (pollfds
[nr_stdin
].revents
& POLLIN
)
1116 done
= perf_kvm__handle_stdin(&tc
, &save
);
1119 err
= poll(pollfds
, nr_fds
, 100);
1122 perf_evlist__disable(kvm
->evlist
);
1130 if (kvm
->timerfd
>= 0)
1131 close(kvm
->timerfd
);
1139 static int kvm_live_open_events(struct perf_kvm_stat
*kvm
)
1142 struct perf_evsel
*pos
;
1143 struct perf_evlist
*evlist
= kvm
->evlist
;
1145 perf_evlist__config(evlist
, &kvm
->opts
);
1148 * Note: exclude_{guest,host} do not apply here.
1149 * This command processes KVM tracepoints from host only
1151 list_for_each_entry(pos
, &evlist
->entries
, node
) {
1152 struct perf_event_attr
*attr
= &pos
->attr
;
1154 /* make sure these *are* set */
1155 attr
->sample_type
|= PERF_SAMPLE_TID
;
1156 attr
->sample_type
|= PERF_SAMPLE_TIME
;
1157 attr
->sample_type
|= PERF_SAMPLE_CPU
;
1158 attr
->sample_type
|= PERF_SAMPLE_RAW
;
1159 /* make sure these are *not*; want as small a sample as possible */
1160 attr
->sample_type
&= ~PERF_SAMPLE_PERIOD
;
1161 attr
->sample_type
&= ~PERF_SAMPLE_IP
;
1162 attr
->sample_type
&= ~PERF_SAMPLE_CALLCHAIN
;
1163 attr
->sample_type
&= ~PERF_SAMPLE_ADDR
;
1164 attr
->sample_type
&= ~PERF_SAMPLE_READ
;
1169 attr
->sample_period
= 1;
1171 attr
->watermark
= 0;
1172 attr
->wakeup_events
= 1000;
1174 /* will enable all once we are ready */
1178 err
= perf_evlist__open(evlist
);
1180 printf("Couldn't create the events: %s\n", strerror(errno
));
1184 if (perf_evlist__mmap(evlist
, kvm
->opts
.mmap_pages
, false) < 0) {
1185 ui__error("Failed to mmap the events: %s\n", strerror(errno
));
1186 perf_evlist__close(evlist
);
1196 static int read_events(struct perf_kvm_stat
*kvm
)
1200 struct perf_tool eops
= {
1201 .sample
= process_sample_event
,
1202 .comm
= perf_event__process_comm
,
1203 .ordered_samples
= true,
1207 kvm
->session
= perf_session__new(kvm
->file_name
, O_RDONLY
, 0, false,
1209 if (!kvm
->session
) {
1210 pr_err("Initializing perf session failed\n");
1214 if (!perf_session__has_traces(kvm
->session
, "kvm record"))
1218 * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
1219 * traced in the old kernel.
1221 ret
= cpu_isa_config(kvm
);
1225 return perf_session__process_events(kvm
->session
, &kvm
->tool
);
1228 static int parse_target_str(struct perf_kvm_stat
*kvm
)
1231 kvm
->pid_list
= intlist__new(kvm
->pid_str
);
1232 if (kvm
->pid_list
== NULL
) {
1233 pr_err("Error parsing process id string\n");
1241 static int kvm_events_report_vcpu(struct perf_kvm_stat
*kvm
)
1244 int vcpu
= kvm
->trace_vcpu
;
1246 if (parse_target_str(kvm
) != 0)
1249 if (!verify_vcpu(vcpu
))
1252 if (!select_key(kvm
))
1255 if (!register_kvm_events_ops(kvm
))
1258 init_kvm_event_record(kvm
);
1261 ret
= read_events(kvm
);
1272 static const char * const kvm_events_tp
[] = {
1279 #define STRDUP_FAIL_EXIT(s) \
1288 kvm_events_record(struct perf_kvm_stat
*kvm
, int argc
, const char **argv
)
1290 unsigned int rec_argc
, i
, j
;
1291 const char **rec_argv
;
1292 const char * const record_args
[] = {
1300 rec_argc
= ARRAY_SIZE(record_args
) + argc
+ 2 +
1301 2 * ARRAY_SIZE(kvm_events_tp
);
1302 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1304 if (rec_argv
== NULL
)
1307 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
1308 rec_argv
[i
] = STRDUP_FAIL_EXIT(record_args
[i
]);
1310 for (j
= 0; j
< ARRAY_SIZE(kvm_events_tp
); j
++) {
1311 rec_argv
[i
++] = "-e";
1312 rec_argv
[i
++] = STRDUP_FAIL_EXIT(kvm_events_tp
[j
]);
1315 rec_argv
[i
++] = STRDUP_FAIL_EXIT("-o");
1316 rec_argv
[i
++] = STRDUP_FAIL_EXIT(kvm
->file_name
);
1318 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
1319 rec_argv
[i
] = argv
[j
];
1321 return cmd_record(i
, rec_argv
, NULL
);
1325 kvm_events_report(struct perf_kvm_stat
*kvm
, int argc
, const char **argv
)
1327 const struct option kvm_events_report_options
[] = {
1328 OPT_STRING(0, "event", &kvm
->report_event
, "report event",
1329 "event for reporting: vmexit, mmio, ioport"),
1330 OPT_INTEGER(0, "vcpu", &kvm
->trace_vcpu
,
1331 "vcpu id to report"),
1332 OPT_STRING('k', "key", &kvm
->sort_key
, "sort-key",
1333 "key for sorting: sample(sort by samples number)"
1334 " time (sort by avg time)"),
1335 OPT_STRING('p', "pid", &kvm
->pid_str
, "pid",
1336 "analyze events only for given process id(s)"),
1340 const char * const kvm_events_report_usage
[] = {
1341 "perf kvm stat report [<options>]",
1348 argc
= parse_options(argc
, argv
,
1349 kvm_events_report_options
,
1350 kvm_events_report_usage
, 0);
1352 usage_with_options(kvm_events_report_usage
,
1353 kvm_events_report_options
);
1356 return kvm_events_report_vcpu(kvm
);
1359 static struct perf_evlist
*kvm_live_event_list(void)
1361 struct perf_evlist
*evlist
;
1362 char *tp
, *name
, *sys
;
1366 evlist
= perf_evlist__new();
1370 for (j
= 0; j
< ARRAY_SIZE(kvm_events_tp
); j
++) {
1372 tp
= strdup(kvm_events_tp
[j
]);
1376 /* split tracepoint into subsystem and name */
1378 name
= strchr(tp
, ':');
1380 pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
1388 if (perf_evlist__add_newtp(evlist
, sys
, name
, NULL
)) {
1389 pr_err("Failed to add %s tracepoint to the list\n", kvm_events_tp
[j
]);
1401 perf_evlist__delete(evlist
);
1408 static int kvm_events_live(struct perf_kvm_stat
*kvm
,
1409 int argc
, const char **argv
)
1411 char errbuf
[BUFSIZ
];
1414 const struct option live_options
[] = {
1415 OPT_STRING('p', "pid", &kvm
->opts
.target
.pid
, "pid",
1416 "record events on existing process id"),
1417 OPT_UINTEGER('m', "mmap-pages", &kvm
->opts
.mmap_pages
,
1418 "number of mmap data pages"),
1419 OPT_INCR('v', "verbose", &verbose
,
1420 "be more verbose (show counter open errors, etc)"),
1421 OPT_BOOLEAN('a', "all-cpus", &kvm
->opts
.target
.system_wide
,
1422 "system-wide collection from all CPUs"),
1423 OPT_UINTEGER('d', "display", &kvm
->display_time
,
1424 "time in seconds between display updates"),
1425 OPT_STRING(0, "event", &kvm
->report_event
, "report event",
1426 "event for reporting: vmexit, mmio, ioport"),
1427 OPT_INTEGER(0, "vcpu", &kvm
->trace_vcpu
,
1428 "vcpu id to report"),
1429 OPT_STRING('k', "key", &kvm
->sort_key
, "sort-key",
1430 "key for sorting: sample(sort by samples number)"
1431 " time (sort by avg time)"),
1434 const char * const live_usage
[] = {
1435 "perf kvm stat live [<options>]",
1440 /* event handling */
1441 kvm
->tool
.sample
= process_sample_event
;
1442 kvm
->tool
.comm
= perf_event__process_comm
;
1443 kvm
->tool
.exit
= perf_event__process_exit
;
1444 kvm
->tool
.fork
= perf_event__process_fork
;
1445 kvm
->tool
.lost
= process_lost_event
;
1446 kvm
->tool
.ordered_samples
= true;
1447 perf_tool__fill_defaults(&kvm
->tool
);
1450 kvm
->display_time
= 1;
1451 kvm
->opts
.user_interval
= 1;
1452 kvm
->opts
.mmap_pages
= 512;
1453 kvm
->opts
.target
.uses_mmap
= false;
1454 kvm
->opts
.target
.uid_str
= NULL
;
1455 kvm
->opts
.target
.uid
= UINT_MAX
;
1458 disable_buildid_cache();
1461 setup_browser(false);
1464 argc
= parse_options(argc
, argv
, live_options
,
1467 usage_with_options(live_usage
, live_options
);
1471 * target related setups
1473 err
= perf_target__validate(&kvm
->opts
.target
);
1475 perf_target__strerror(&kvm
->opts
.target
, err
, errbuf
, BUFSIZ
);
1476 ui__warning("%s", errbuf
);
1479 if (perf_target__none(&kvm
->opts
.target
))
1480 kvm
->opts
.target
.system_wide
= true;
1484 * generate the event list
1486 kvm
->evlist
= kvm_live_event_list();
1487 if (kvm
->evlist
== NULL
) {
1492 symbol_conf
.nr_events
= kvm
->evlist
->nr_entries
;
1494 if (perf_evlist__create_maps(kvm
->evlist
, &kvm
->opts
.target
) < 0)
1495 usage_with_options(live_usage
, live_options
);
1500 kvm
->session
= perf_session__new(NULL
, O_WRONLY
, false, false, &kvm
->tool
);
1501 if (kvm
->session
== NULL
) {
1505 kvm
->session
->evlist
= kvm
->evlist
;
1506 perf_session__set_id_hdr_size(kvm
->session
);
1509 if (perf_target__has_task(&kvm
->opts
.target
))
1510 perf_event__synthesize_thread_map(&kvm
->tool
,
1511 kvm
->evlist
->threads
,
1512 perf_event__process
,
1513 &kvm
->session
->machines
.host
);
1515 perf_event__synthesize_threads(&kvm
->tool
, perf_event__process
,
1516 &kvm
->session
->machines
.host
);
1519 err
= kvm_live_open_events(kvm
);
1523 err
= kvm_events_live_report(kvm
);
1529 perf_session__delete(kvm
->session
);
1530 kvm
->session
= NULL
;
1532 perf_evlist__delete_maps(kvm
->evlist
);
1533 perf_evlist__delete(kvm
->evlist
);
1539 static void print_kvm_stat_usage(void)
1541 printf("Usage: perf kvm stat <command>\n\n");
1543 printf("# Available commands:\n");
1544 printf("\trecord: record kvm events\n");
1545 printf("\treport: report statistical data of kvm events\n");
1546 printf("\tlive: live reporting of statistical data of kvm events\n");
1548 printf("\nOtherwise, it is the alias of 'perf stat':\n");
1551 static int kvm_cmd_stat(const char *file_name
, int argc
, const char **argv
)
1553 struct perf_kvm_stat kvm
= {
1554 .file_name
= file_name
,
1557 .report_event
= "vmexit",
1558 .sort_key
= "sample",
1560 .exit_reasons
= svm_exit_reasons
,
1561 .exit_reasons_size
= ARRAY_SIZE(svm_exit_reasons
),
1562 .exit_reasons_isa
= "SVM",
1566 print_kvm_stat_usage();
1570 if (!strncmp(argv
[1], "rec", 3))
1571 return kvm_events_record(&kvm
, argc
- 1, argv
+ 1);
1573 if (!strncmp(argv
[1], "rep", 3))
1574 return kvm_events_report(&kvm
, argc
- 1 , argv
+ 1);
1576 if (!strncmp(argv
[1], "live", 4))
1577 return kvm_events_live(&kvm
, argc
- 1 , argv
+ 1);
1580 return cmd_stat(argc
, argv
, NULL
);
1584 static int __cmd_record(const char *file_name
, int argc
, const char **argv
)
1586 int rec_argc
, i
= 0, j
;
1587 const char **rec_argv
;
1589 rec_argc
= argc
+ 2;
1590 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1591 rec_argv
[i
++] = strdup("record");
1592 rec_argv
[i
++] = strdup("-o");
1593 rec_argv
[i
++] = strdup(file_name
);
1594 for (j
= 1; j
< argc
; j
++, i
++)
1595 rec_argv
[i
] = argv
[j
];
1597 BUG_ON(i
!= rec_argc
);
1599 return cmd_record(i
, rec_argv
, NULL
);
1602 static int __cmd_report(const char *file_name
, int argc
, const char **argv
)
1604 int rec_argc
, i
= 0, j
;
1605 const char **rec_argv
;
1607 rec_argc
= argc
+ 2;
1608 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1609 rec_argv
[i
++] = strdup("report");
1610 rec_argv
[i
++] = strdup("-i");
1611 rec_argv
[i
++] = strdup(file_name
);
1612 for (j
= 1; j
< argc
; j
++, i
++)
1613 rec_argv
[i
] = argv
[j
];
1615 BUG_ON(i
!= rec_argc
);
1617 return cmd_report(i
, rec_argv
, NULL
);
1621 __cmd_buildid_list(const char *file_name
, int argc
, const char **argv
)
1623 int rec_argc
, i
= 0, j
;
1624 const char **rec_argv
;
1626 rec_argc
= argc
+ 2;
1627 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1628 rec_argv
[i
++] = strdup("buildid-list");
1629 rec_argv
[i
++] = strdup("-i");
1630 rec_argv
[i
++] = strdup(file_name
);
1631 for (j
= 1; j
< argc
; j
++, i
++)
1632 rec_argv
[i
] = argv
[j
];
1634 BUG_ON(i
!= rec_argc
);
1636 return cmd_buildid_list(i
, rec_argv
, NULL
);
1639 int cmd_kvm(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1641 const char *file_name
= NULL
;
1642 const struct option kvm_options
[] = {
1643 OPT_STRING('i', "input", &file_name
, "file",
1645 OPT_STRING('o', "output", &file_name
, "file",
1646 "Output file name"),
1647 OPT_BOOLEAN(0, "guest", &perf_guest
,
1648 "Collect guest os data"),
1649 OPT_BOOLEAN(0, "host", &perf_host
,
1650 "Collect host os data"),
1651 OPT_STRING(0, "guestmount", &symbol_conf
.guestmount
, "directory",
1652 "guest mount directory under which every guest os"
1653 " instance has a subdir"),
1654 OPT_STRING(0, "guestvmlinux", &symbol_conf
.default_guest_vmlinux_name
,
1655 "file", "file saving guest os vmlinux"),
1656 OPT_STRING(0, "guestkallsyms", &symbol_conf
.default_guest_kallsyms
,
1657 "file", "file saving guest os /proc/kallsyms"),
1658 OPT_STRING(0, "guestmodules", &symbol_conf
.default_guest_modules
,
1659 "file", "file saving guest os /proc/modules"),
1664 const char * const kvm_usage
[] = {
1665 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
1672 argc
= parse_options(argc
, argv
, kvm_options
, kvm_usage
,
1673 PARSE_OPT_STOP_AT_NON_OPTION
);
1675 usage_with_options(kvm_usage
, kvm_options
);
1681 if (perf_host
&& !perf_guest
)
1682 file_name
= strdup("perf.data.host");
1683 else if (!perf_host
&& perf_guest
)
1684 file_name
= strdup("perf.data.guest");
1686 file_name
= strdup("perf.data.kvm");
1689 pr_err("Failed to allocate memory for filename\n");
1694 if (!strncmp(argv
[0], "rec", 3))
1695 return __cmd_record(file_name
, argc
, argv
);
1696 else if (!strncmp(argv
[0], "rep", 3))
1697 return __cmd_report(file_name
, argc
, argv
);
1698 else if (!strncmp(argv
[0], "diff", 4))
1699 return cmd_diff(argc
, argv
, NULL
);
1700 else if (!strncmp(argv
[0], "top", 3))
1701 return cmd_top(argc
, argv
, NULL
);
1702 else if (!strncmp(argv
[0], "buildid-list", 12))
1703 return __cmd_buildid_list(file_name
, argc
, argv
);
1704 #if defined(__i386__) || defined(__x86_64__)
1705 else if (!strncmp(argv
[0], "stat", 4))
1706 return kvm_cmd_stat(file_name
, argc
, argv
);
1709 usage_with_options(kvm_usage
, kvm_options
);