forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltin-kvm.c
2191 lines (1784 loc) · 50.9 KB
/
builtin-kvm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
#include "builtin.h"
#include "perf.h"
#include "util/build-id.h"
#include "util/evsel.h"
#include "util/evlist.h"
#include "util/mmap.h"
#include "util/term.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
#include "util/session.h"
#include "util/intlist.h"
#include <subcmd/pager.h>
#include <subcmd/parse-options.h>
#include "util/trace-event.h"
#include "util/debug.h"
#include "util/tool.h"
#include "util/stat.h"
#include "util/synthetic-events.h"
#include "util/top.h"
#include "util/data.h"
#include "util/ordered-events.h"
#include "util/kvm-stat.h"
#include "util/util.h"
#include "ui/browsers/hists.h"
#include "ui/progress.h"
#include "ui/ui.h"
#include "util/string2.h"
#include <sys/prctl.h>
#ifdef HAVE_TIMERFD_SUPPORT
#include <sys/timerfd.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/time64.h>
#include <linux/zalloc.h>
#include <errno.h>
#include <inttypes.h>
#include <poll.h>
#include <termios.h>
#include <semaphore.h>
#include <signal.h>
#include <math.h>
#include <perf/mmap.h>
#if defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
#define GET_EVENT_KEY(func, field) \
static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
{ \
if (vcpu == -1) \
return event->total.field; \
\
if (vcpu >= event->max_vcpu) \
return 0; \
\
return event->vcpu[vcpu].field; \
}
#define COMPARE_EVENT_KEY(func, field) \
GET_EVENT_KEY(func, field) \
static int64_t cmp_event_ ## func(struct kvm_event *one, \
struct kvm_event *two, int vcpu) \
{ \
return get_event_ ##func(one, vcpu) - \
get_event_ ##func(two, vcpu); \
}
COMPARE_EVENT_KEY(time, time);
COMPARE_EVENT_KEY(max, stats.max);
COMPARE_EVENT_KEY(min, stats.min);
COMPARE_EVENT_KEY(count, stats.n);
COMPARE_EVENT_KEY(mean, stats.mean);
struct kvm_hists {
struct hists hists;
struct perf_hpp_list list;
};
struct kvm_dimension {
const char *name;
const char *header;
int width;
int64_t (*cmp)(struct perf_hpp_fmt *fmt, struct hist_entry *left,
struct hist_entry *right);
int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
struct hist_entry *he);
};
struct kvm_fmt {
struct perf_hpp_fmt fmt;
struct kvm_dimension *dim;
};
static struct kvm_hists kvm_hists;
static int64_t ev_name_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
struct hist_entry *left,
struct hist_entry *right)
{
/* Return opposite number for sorting in alphabetical order */
return -strcmp(left->kvm_info->name, right->kvm_info->name);
}
static int fmt_width(struct perf_hpp_fmt *fmt,
struct perf_hpp *hpp __maybe_unused,
struct hists *hists __maybe_unused);
static int ev_name_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
struct hist_entry *he)
{
int width = fmt_width(fmt, hpp, he->hists);
return scnprintf(hpp->buf, hpp->size, "%*s", width, he->kvm_info->name);
}
static struct kvm_dimension dim_event = {
.header = "Event name",
.name = "ev_name",
.cmp = ev_name_cmp,
.entry = ev_name_entry,
.width = 40,
};
#define EV_METRIC_CMP(metric) \
static int64_t ev_cmp_##metric(struct perf_hpp_fmt *fmt __maybe_unused, \
struct hist_entry *left, \
struct hist_entry *right) \
{ \
struct kvm_event *event_left; \
struct kvm_event *event_right; \
struct perf_kvm_stat *perf_kvm; \
\
event_left = container_of(left, struct kvm_event, he); \
event_right = container_of(right, struct kvm_event, he); \
\
perf_kvm = event_left->perf_kvm; \
return cmp_event_##metric(event_left, event_right, \
perf_kvm->trace_vcpu); \
}
EV_METRIC_CMP(time)
EV_METRIC_CMP(count)
EV_METRIC_CMP(max)
EV_METRIC_CMP(min)
EV_METRIC_CMP(mean)
#define EV_METRIC_ENTRY(metric) \
static int ev_entry_##metric(struct perf_hpp_fmt *fmt, \
struct perf_hpp *hpp, \
struct hist_entry *he) \
{ \
struct kvm_event *event; \
int width = fmt_width(fmt, hpp, he->hists); \
struct perf_kvm_stat *perf_kvm; \
\
event = container_of(he, struct kvm_event, he); \
perf_kvm = event->perf_kvm; \
return scnprintf(hpp->buf, hpp->size, "%*lu", width, \
get_event_##metric(event, perf_kvm->trace_vcpu)); \
}
EV_METRIC_ENTRY(time)
EV_METRIC_ENTRY(count)
EV_METRIC_ENTRY(max)
EV_METRIC_ENTRY(min)
static struct kvm_dimension dim_time = {
.header = "Time (ns)",
.name = "time",
.cmp = ev_cmp_time,
.entry = ev_entry_time,
.width = 12,
};
static struct kvm_dimension dim_count = {
.header = "Samples",
.name = "sample",
.cmp = ev_cmp_count,
.entry = ev_entry_count,
.width = 12,
};
static struct kvm_dimension dim_max_time = {
.header = "Max Time (ns)",
.name = "max_t",
.cmp = ev_cmp_max,
.entry = ev_entry_max,
.width = 14,
};
static struct kvm_dimension dim_min_time = {
.header = "Min Time (ns)",
.name = "min_t",
.cmp = ev_cmp_min,
.entry = ev_entry_min,
.width = 14,
};
static int ev_entry_mean(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
struct hist_entry *he)
{
struct kvm_event *event;
int width = fmt_width(fmt, hpp, he->hists);
struct perf_kvm_stat *perf_kvm;
event = container_of(he, struct kvm_event, he);
perf_kvm = event->perf_kvm;
return scnprintf(hpp->buf, hpp->size, "%*lu", width,
get_event_mean(event, perf_kvm->trace_vcpu));
}
static struct kvm_dimension dim_mean_time = {
.header = "Mean Time (ns)",
.name = "mean_t",
.cmp = ev_cmp_mean,
.entry = ev_entry_mean,
.width = 14,
};
#define PERC_STR(__s, __v) \
({ \
scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
__s; \
})
static double percent(u64 st, u64 tot)
{
return tot ? 100. * (double) st / (double) tot : 0;
}
#define EV_METRIC_PERCENT(metric) \
static int ev_percent_##metric(struct hist_entry *he) \
{ \
struct kvm_event *event; \
struct perf_kvm_stat *perf_kvm; \
\
event = container_of(he, struct kvm_event, he); \
perf_kvm = event->perf_kvm; \
\
return percent(get_event_##metric(event, perf_kvm->trace_vcpu), \
perf_kvm->total_##metric); \
}
EV_METRIC_PERCENT(time)
EV_METRIC_PERCENT(count)
static int ev_entry_time_precent(struct perf_hpp_fmt *fmt,
struct perf_hpp *hpp,
struct hist_entry *he)
{
int width = fmt_width(fmt, hpp, he->hists);
double per;
char buf[10];
per = ev_percent_time(he);
return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
}
static int64_t
ev_cmp_time_precent(struct perf_hpp_fmt *fmt __maybe_unused,
struct hist_entry *left, struct hist_entry *right)
{
double per_left;
double per_right;
per_left = ev_percent_time(left);
per_right = ev_percent_time(right);
return per_left - per_right;
}
static struct kvm_dimension dim_time_percent = {
.header = "Time%",
.name = "percent_time",
.cmp = ev_cmp_time_precent,
.entry = ev_entry_time_precent,
.width = 12,
};
static int ev_entry_count_precent(struct perf_hpp_fmt *fmt,
struct perf_hpp *hpp,
struct hist_entry *he)
{
int width = fmt_width(fmt, hpp, he->hists);
double per;
char buf[10];
per = ev_percent_count(he);
return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
}
static int64_t
ev_cmp_count_precent(struct perf_hpp_fmt *fmt __maybe_unused,
struct hist_entry *left, struct hist_entry *right)
{
double per_left;
double per_right;
per_left = ev_percent_count(left);
per_right = ev_percent_count(right);
return per_left - per_right;
}
static struct kvm_dimension dim_count_percent = {
.header = "Sample%",
.name = "percent_sample",
.cmp = ev_cmp_count_precent,
.entry = ev_entry_count_precent,
.width = 12,
};
static struct kvm_dimension *dimensions[] = {
&dim_event,
&dim_time,
&dim_time_percent,
&dim_count,
&dim_count_percent,
&dim_max_time,
&dim_min_time,
&dim_mean_time,
NULL,
};
static int fmt_width(struct perf_hpp_fmt *fmt,
struct perf_hpp *hpp __maybe_unused,
struct hists *hists __maybe_unused)
{
struct kvm_fmt *kvm_fmt;
kvm_fmt = container_of(fmt, struct kvm_fmt, fmt);
return kvm_fmt->dim->width;
}
static int fmt_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
struct hists *hists, int line __maybe_unused,
int *span __maybe_unused)
{
struct kvm_fmt *kvm_fmt;
struct kvm_dimension *dim;
int width = fmt_width(fmt, hpp, hists);
kvm_fmt = container_of(fmt, struct kvm_fmt, fmt);
dim = kvm_fmt->dim;
return scnprintf(hpp->buf, hpp->size, "%*s", width, dim->header);
}
static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
{
struct kvm_fmt *kvm_fmt_a = container_of(a, struct kvm_fmt, fmt);
struct kvm_fmt *kvm_fmt_b = container_of(b, struct kvm_fmt, fmt);
return kvm_fmt_a->dim == kvm_fmt_b->dim;
}
static void fmt_free(struct perf_hpp_fmt *fmt)
{
struct kvm_fmt *kvm_fmt;
kvm_fmt = container_of(fmt, struct kvm_fmt, fmt);
free(kvm_fmt);
}
static struct kvm_dimension *get_dimension(const char *name)
{
unsigned int i;
for (i = 0; dimensions[i] != NULL; i++) {
if (!strcmp(dimensions[i]->name, name))
return dimensions[i];
}
return NULL;
}
static struct kvm_fmt *get_format(const char *name)
{
struct kvm_dimension *dim = get_dimension(name);
struct kvm_fmt *kvm_fmt;
struct perf_hpp_fmt *fmt;
if (!dim)
return NULL;
kvm_fmt = zalloc(sizeof(*kvm_fmt));
if (!kvm_fmt)
return NULL;
kvm_fmt->dim = dim;
fmt = &kvm_fmt->fmt;
INIT_LIST_HEAD(&fmt->list);
INIT_LIST_HEAD(&fmt->sort_list);
fmt->cmp = dim->cmp;
fmt->sort = dim->cmp;
fmt->color = NULL;
fmt->entry = dim->entry;
fmt->header = fmt_header;
fmt->width = fmt_width;
fmt->collapse = dim->cmp;
fmt->equal = fmt_equal;
fmt->free = fmt_free;
return kvm_fmt;
}
static int kvm_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
{
struct kvm_fmt *kvm_fmt = get_format(name);
if (!kvm_fmt) {
pr_warning("Fail to find format for output field %s.\n", name);
return -EINVAL;
}
perf_hpp_list__column_register(hpp_list, &kvm_fmt->fmt);
return 0;
}
static int kvm_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
{
struct kvm_fmt *kvm_fmt = get_format(name);
if (!kvm_fmt) {
pr_warning("Fail to find format for sorting %s.\n", name);
return -EINVAL;
}
perf_hpp_list__register_sort_field(hpp_list, &kvm_fmt->fmt);
return 0;
}
static int kvm_hpp_list__init(char *list,
struct perf_hpp_list *hpp_list,
int (*fn)(struct perf_hpp_list *hpp_list,
char *name))
{
char *tmp, *tok;
int ret;
if (!list || !fn)
return 0;
for (tok = strtok_r(list, ", ", &tmp); tok;
tok = strtok_r(NULL, ", ", &tmp)) {
ret = fn(hpp_list, tok);
if (!ret)
continue;
/* Handle errors */
if (ret == -EINVAL)
pr_err("Invalid field key: '%s'", tok);
else if (ret == -ESRCH)
pr_err("Unknown field key: '%s'", tok);
else
pr_err("Fail to initialize for field key: '%s'", tok);
break;
}
return ret;
}
static int kvm_hpp_list__parse(struct perf_hpp_list *hpp_list,
const char *output_, const char *sort_)
{
char *output = output_ ? strdup(output_) : NULL;
char *sort = sort_ ? strdup(sort_) : NULL;
int ret;
ret = kvm_hpp_list__init(output, hpp_list, kvm_hists__init_output);
if (ret)
goto out;
ret = kvm_hpp_list__init(sort, hpp_list, kvm_hists__init_sort);
if (ret)
goto out;
/* Copy sort keys to output fields */
perf_hpp__setup_output_field(hpp_list);
/* and then copy output fields to sort keys */
perf_hpp__append_sort_keys(hpp_list);
out:
free(output);
free(sort);
return ret;
}
static int kvm_hists__init(void)
{
kvm_hists.list.nr_header_lines = 1;
__hists__init(&kvm_hists.hists, &kvm_hists.list);
perf_hpp_list__init(&kvm_hists.list);
return kvm_hpp_list__parse(&kvm_hists.list, NULL, "ev_name");
}
static int kvm_hists__reinit(const char *output, const char *sort)
{
perf_hpp__reset_output_field(&kvm_hists.list);
return kvm_hpp_list__parse(&kvm_hists.list, output, sort);
}
static void print_result(struct perf_kvm_stat *kvm);
#ifdef HAVE_SLANG_SUPPORT
static void kvm_browser__update_nr_entries(struct hist_browser *hb)
{
struct rb_node *nd = rb_first_cached(&hb->hists->entries);
u64 nr_entries = 0;
for (; nd; nd = rb_next(nd)) {
struct hist_entry *he = rb_entry(nd, struct hist_entry,
rb_node);
if (!he->filtered)
nr_entries++;
}
hb->nr_non_filtered_entries = nr_entries;
}
static int kvm_browser__title(struct hist_browser *browser,
char *buf, size_t size)
{
scnprintf(buf, size, "KVM event statistics (%lu entries)",
browser->nr_non_filtered_entries);
return 0;
}
static struct hist_browser*
perf_kvm_browser__new(struct hists *hists)
{
struct hist_browser *browser = hist_browser__new(hists);
if (browser)
browser->title = kvm_browser__title;
return browser;
}
static int kvm__hists_browse(struct hists *hists)
{
struct hist_browser *browser;
int key = -1;
browser = perf_kvm_browser__new(hists);
if (browser == NULL)
return -1;
/* reset abort key so that it can get Ctrl-C as a key */
SLang_reset_tty();
SLang_init_tty(0, 0, 0);
kvm_browser__update_nr_entries(browser);
while (1) {
key = hist_browser__run(browser, "? - help", true, 0);
switch (key) {
case 'q':
goto out;
default:
break;
}
}
out:
hist_browser__delete(browser);
return 0;
}
static void kvm_display(struct perf_kvm_stat *kvm)
{
if (!use_browser)
print_result(kvm);
else
kvm__hists_browse(&kvm_hists.hists);
}
#else
static void kvm_display(struct perf_kvm_stat *kvm)
{
use_browser = 0;
print_result(kvm);
}
#endif /* HAVE_SLANG_SUPPORT */
#endif // defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
static const char *get_filename_for_perf_kvm(void)
{
const char *filename;
if (perf_host && !perf_guest)
filename = strdup("perf.data.host");
else if (!perf_host && perf_guest)
filename = strdup("perf.data.guest");
else
filename = strdup("perf.data.kvm");
return filename;
}
#if defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
void exit_event_get_key(struct evsel *evsel,
struct perf_sample *sample,
struct event_key *key)
{
key->info = 0;
key->key = evsel__intval(evsel, sample, kvm_exit_reason);
}
bool kvm_exit_event(struct evsel *evsel)
{
return evsel__name_is(evsel, kvm_exit_trace);
}
bool exit_event_begin(struct evsel *evsel,
struct perf_sample *sample, struct event_key *key)
{
if (kvm_exit_event(evsel)) {
exit_event_get_key(evsel, sample, key);
return true;
}
return false;
}
bool kvm_entry_event(struct evsel *evsel)
{
return evsel__name_is(evsel, kvm_entry_trace);
}
bool exit_event_end(struct evsel *evsel,
struct perf_sample *sample __maybe_unused,
struct event_key *key __maybe_unused)
{
return kvm_entry_event(evsel);
}
static const char *get_exit_reason(struct perf_kvm_stat *kvm,
struct exit_reasons_table *tbl,
u64 exit_code)
{
while (tbl->reason != NULL) {
if (tbl->exit_code == exit_code)
return tbl->reason;
tbl++;
}
pr_err("unknown kvm exit code:%lld on %s\n",
(unsigned long long)exit_code, kvm->exit_reasons_isa);
return "UNKNOWN";
}
void exit_event_decode_key(struct perf_kvm_stat *kvm,
struct event_key *key,
char *decode)
{
const char *exit_reason = get_exit_reason(kvm, key->exit_reasons,
key->key);
scnprintf(decode, KVM_EVENT_NAME_LEN, "%s", exit_reason);
}
static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
{
struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops;
for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) {
if (!strcmp(events_ops->name, kvm->report_event)) {
kvm->events_ops = events_ops->ops;
return true;
}
}
return false;
}
struct vcpu_event_record {
int vcpu_id;
u64 start_time;
struct kvm_event *last_event;
};
#ifdef HAVE_TIMERFD_SUPPORT
static void clear_events_cache_stats(void)
{
struct rb_root_cached *root;
struct rb_node *nd;
struct kvm_event *event;
int i;
if (hists__has(&kvm_hists.hists, need_collapse))
root = &kvm_hists.hists.entries_collapsed;
else
root = kvm_hists.hists.entries_in;
for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
struct hist_entry *he;
he = rb_entry(nd, struct hist_entry, rb_node_in);
event = container_of(he, struct kvm_event, he);
/* reset stats for event */
event->total.time = 0;
init_stats(&event->total.stats);
for (i = 0; i < event->max_vcpu; ++i) {
event->vcpu[i].time = 0;
init_stats(&event->vcpu[i].stats);
}
}
}
#endif
static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
{
int old_max_vcpu = event->max_vcpu;
void *prev;
if (vcpu_id < event->max_vcpu)
return true;
while (event->max_vcpu <= vcpu_id)
event->max_vcpu += DEFAULT_VCPU_NUM;
prev = event->vcpu;
event->vcpu = realloc(event->vcpu,
event->max_vcpu * sizeof(*event->vcpu));
if (!event->vcpu) {
free(prev);
pr_err("Not enough memory\n");
return false;
}
memset(event->vcpu + old_max_vcpu, 0,
(event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
return true;
}
static void *kvm_he_zalloc(size_t size)
{
struct kvm_event *kvm_ev;
kvm_ev = zalloc(size + sizeof(*kvm_ev));
if (!kvm_ev)
return NULL;
init_stats(&kvm_ev->total.stats);
hists__inc_nr_samples(&kvm_hists.hists, 0);
return &kvm_ev->he;
}
static void kvm_he_free(void *he)
{
struct kvm_event *kvm_ev;
kvm_ev = container_of(he, struct kvm_event, he);
free(kvm_ev);
}
static struct hist_entry_ops kvm_ev_entry_ops = {
.new = kvm_he_zalloc,
.free = kvm_he_free,
};
static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
struct event_key *key,
struct perf_sample *sample)
{
struct kvm_event *event;
struct hist_entry *he;
struct kvm_info *ki;
BUG_ON(key->key == INVALID_KEY);
ki = kvm_info__new();
if (!ki) {
pr_err("Failed to allocate kvm info\n");
return NULL;
}
kvm->events_ops->decode_key(kvm, key, ki->name);
he = hists__add_entry_ops(&kvm_hists.hists, &kvm_ev_entry_ops,
&kvm->al, NULL, NULL, NULL, ki, sample, true);
if (he == NULL) {
pr_err("Failed to allocate hist entry\n");
free(ki);
return NULL;
}
event = container_of(he, struct kvm_event, he);
if (!event->perf_kvm) {
event->perf_kvm = kvm;
event->key = *key;
}
return event;
}
static bool handle_begin_event(struct perf_kvm_stat *kvm,
struct vcpu_event_record *vcpu_record,
struct event_key *key,
struct perf_sample *sample)
{
struct kvm_event *event = NULL;
if (key->key != INVALID_KEY)
event = find_create_kvm_event(kvm, key, sample);
vcpu_record->last_event = event;
vcpu_record->start_time = sample->time;
return true;
}
static void
kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
{
kvm_stats->time += time_diff;
update_stats(&kvm_stats->stats, time_diff);
}
static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
{
struct kvm_event_stats *kvm_stats = &event->total;
if (vcpu_id != -1)
kvm_stats = &event->vcpu[vcpu_id];
return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
avg_stats(&kvm_stats->stats));
}
static bool update_kvm_event(struct perf_kvm_stat *kvm,
struct kvm_event *event, int vcpu_id,
u64 time_diff)
{
/* Update overall statistics */
kvm->total_count++;
kvm->total_time += time_diff;
if (vcpu_id == -1) {
kvm_update_event_stats(&event->total, time_diff);
return true;
}
if (!kvm_event_expand(event, vcpu_id))
return false;
kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
return true;
}
static bool is_child_event(struct perf_kvm_stat *kvm,
struct evsel *evsel,
struct perf_sample *sample,
struct event_key *key)
{
struct child_event_ops *child_ops;
child_ops = kvm->events_ops->child_ops;
if (!child_ops)
return false;
for (; child_ops->name; child_ops++) {
if (evsel__name_is(evsel, child_ops->name)) {
child_ops->get_key(evsel, sample, key);
return true;
}
}
return false;
}
static bool handle_child_event(struct perf_kvm_stat *kvm,
struct vcpu_event_record *vcpu_record,
struct event_key *key,
struct perf_sample *sample)
{
struct kvm_event *event = NULL;
if (key->key != INVALID_KEY)
event = find_create_kvm_event(kvm, key, sample);
vcpu_record->last_event = event;
return true;
}
static bool skip_event(const char *event)
{
const char * const *skip_events;
for (skip_events = kvm_skip_events; *skip_events; skip_events++)
if (!strcmp(event, *skip_events))
return true;
return false;
}
static bool handle_end_event(struct perf_kvm_stat *kvm,
struct vcpu_event_record *vcpu_record,
struct event_key *key,
struct perf_sample *sample)
{
struct kvm_event *event;
u64 time_begin, time_diff;
int vcpu;
if (kvm->trace_vcpu == -1)
vcpu = -1;
else
vcpu = vcpu_record->vcpu_id;
event = vcpu_record->last_event;
time_begin = vcpu_record->start_time;
/* The begin event is not caught. */
if (!time_begin)
return true;
/*
* In some case, the 'begin event' only records the start timestamp,
* the actual event is recognized in the 'end event' (e.g. mmio-event).
*/
/* Both begin and end events did not get the key. */
if (!event && key->key == INVALID_KEY)
return true;
if (!event)
event = find_create_kvm_event(kvm, key, sample);
if (!event)
return false;
vcpu_record->last_event = NULL;
vcpu_record->start_time = 0;
/* seems to happen once in a while during live mode */
if (sample->time < time_begin) {
pr_debug("End time before begin time; skipping event.\n");
return true;
}
time_diff = sample->time - time_begin;
if (kvm->duration && time_diff > kvm->duration) {
char decode[KVM_EVENT_NAME_LEN];
kvm->events_ops->decode_key(kvm, &event->key, decode);
if (!skip_event(decode)) {
pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
sample->time, sample->pid, vcpu_record->vcpu_id,
decode, time_diff / NSEC_PER_USEC);
}
}
return update_kvm_event(kvm, event, vcpu, time_diff);
}
static
struct vcpu_event_record *per_vcpu_record(struct thread *thread,
struct evsel *evsel,
struct perf_sample *sample)
{
/* Only kvm_entry records vcpu id. */
if (!thread__priv(thread) && kvm_entry_event(evsel)) {
struct vcpu_event_record *vcpu_record;
vcpu_record = zalloc(sizeof(*vcpu_record));
if (!vcpu_record) {
pr_err("%s: Not enough memory\n", __func__);
return NULL;
}
vcpu_record->vcpu_id = evsel__intval(evsel, sample, vcpu_id_str);
thread__set_priv(thread, vcpu_record);
}
return thread__priv(thread);
}
static bool handle_kvm_event(struct perf_kvm_stat *kvm,
struct thread *thread,