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