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