Debian Codegraph @

testsuite/systemtap.examples/virtualization/kvm_service_time.stp

raw
1 #!/usr/bin/env stap
2 #
3 # Copyright (C) 2011 Red Hat, Inc.
4 # Written by William Cohen <[email protected]>
5 #
6 # This script tracks the statistics about the amount of time that the
7 # processor left the guest virtual machine for each exit reason (for
8 # example fixing up a page table or handling an io operation). On
9 # Linux 2.6.38 and newer kernel the script can automatically determine
10 # whether it is running on Intel or AMD processors. For older kernel
11 # with a kernel.trace("kvm_exit") tracepoint that does not have the
12 # $isa parameter you can explicitly state the kvm type as below.
13 #
14 # On an Intel machine run with:
15 # ./kvm_service_time.stp -G kvm=intel
16 #
17 # On an AMD machine run with:
18 # ./kvm_service_time.stp -G kvm=amd
19 #
20 global KVM_ISA_VMX=1, KVM_ISA_SVM=2, kvm_isa=0
21 global kvm="unknown" # user select whether "amd" or "intel" flavor kvm
22 global e_time, reason, stats
23
24 # leaving the guest vm for some reason
25 probe kernel.trace("kvm_exit")
26 {
27 e_time[tid()] = gettimeofday_us()
28 reason[tid()] = @choose_defined($exit_reason, $vcpu->run->exit_reason);
29 kvm_isa = @defined($isa) ? $isa : 0
30 }
31
32 # re-entering guest kvm
33 probe kernel.trace("kvm_entry")
34 {
35 new_t = gettimeofday_us();
36 if ( [tid()] in e_time) {
37 elapsed = new_t - e_time[tid()]
38 stats[reason[tid()]] <<< elapsed
39 delete e_time[tid()]
40 delete reason[tid()]
41 }
42 }
43
44 probe end
45 {
46 kvm_flavor()
47 print_header()
48 foreach(r+ in stats) {
49 printf ("%s: %d %d %d %d %d\n", reason_string(r),
50 @count(stats[r]), @sum(stats[r]),
51 @min(stats[r]), @avg(stats[r]), @max(stats[r]))
52 }
53 }
54
55 function kvm_flavor()
56 {
57 /* automatically determine kvm flavor where possible */
58 if (kvm_isa == KVM_ISA_VMX)
59 kvm="intel"
60 else if (kvm_isa == KVM_ISA_SVM)
61 kvm="amd"
62 }
63
64 function reason_string:string(r:long)
65 {
66 if (kvm=="amd")
67 return sprintf ("0x%03x", r)
68 else if (kvm=="intel")
69 return sprintf ("%d", r)
70 else
71 return sprintf ("%d 0x%03x", r, r)
72 }
73
74 function print_header()
75 {
76 if (kvm=="amd") {
77 printf ("\n# AMD reasons are SVM_EXIT_* in %s\n",
78 "linux/arch/x86/include/uapi/asm/svm.h")
79 printf ("# %s: %s %s %s %s %s\n",
80 "reason(hex)", "count", "sum(us)", "min(us)", "avg(us)", "max(us)")
81 } else if (kvm=="intel") {
82 printf("\n# Intel exit reasons are EXIT_REASON_* in %s\n",
83 "linux/arch/x86/include/uapi/asm/vmx.h")
84 printf ("# %s: %s %s %s %s %s\n",
85 "reason", "count", "sum(us)", "min(us)", "avg(us)", "max(us)")
86 } else {
87 printf ("\n# Intel exit reasons are EXIT_REASON_* in %s\n",
88 "linux/arch/x86/include/uapi/asm/vmx.h")
89 printf ("# AMD reasons are SVM_EXIT_* in %s\n",
90 "linux/arch/x86/include/uapi/asm/svm.h")
91 printf ("# %s %s: %s %s %s %s %s\n", "reason", "reason(hex)",
92 "count", "sum(us)", "min(us)", "avg(us)", "max(us)")
93 }
94 }