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