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