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