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