2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright IBM Corp. 2008
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
21 #include <linux/kvm_host.h>
23 #include <linux/seq_file.h>
24 #include <linux/debugfs.h>
25 #include <linux/uaccess.h>
26 #include <linux/module.h>
29 #include <asm-generic/div64.h>
33 void kvmppc_init_timing_stats(struct kvm_vcpu
*vcpu
)
37 /* pause guest execution to avoid concurrent updates */
39 mutex_lock(&vcpu
->mutex
);
41 vcpu
->arch
.last_exit_type
= 0xDEAD;
42 for (i
= 0; i
< __NUMBER_OF_KVM_EXIT_TYPES
; i
++) {
43 vcpu
->arch
.timing_count_type
[i
] = 0;
44 vcpu
->arch
.timing_max_duration
[i
] = 0;
45 vcpu
->arch
.timing_min_duration
[i
] = 0xFFFFFFFF;
46 vcpu
->arch
.timing_sum_duration
[i
] = 0;
47 vcpu
->arch
.timing_sum_quad_duration
[i
] = 0;
49 vcpu
->arch
.timing_last_exit
= 0;
50 vcpu
->arch
.timing_exit
.tv64
= 0;
51 vcpu
->arch
.timing_last_enter
.tv64
= 0;
53 mutex_unlock(&vcpu
->mutex
);
57 static void add_exit_timing(struct kvm_vcpu
*vcpu
, u64 duration
, int type
)
61 do_div(duration
, tb_ticks_per_usec
);
62 if (unlikely(duration
> 0xFFFFFFFF)) {
63 printk(KERN_ERR
"%s - duration too big -> overflow"
64 " duration %lld type %d exit #%d\n",
65 __func__
, duration
, type
,
66 vcpu
->arch
.timing_count_type
[type
]);
70 vcpu
->arch
.timing_count_type
[type
]++;
73 old
= vcpu
->arch
.timing_sum_duration
[type
];
74 vcpu
->arch
.timing_sum_duration
[type
] += duration
;
75 if (unlikely(old
> vcpu
->arch
.timing_sum_duration
[type
])) {
76 printk(KERN_ERR
"%s - wrap adding sum of durations"
77 " old %lld new %lld type %d exit # of type %d\n",
78 __func__
, old
, vcpu
->arch
.timing_sum_duration
[type
],
79 type
, vcpu
->arch
.timing_count_type
[type
]);
83 old
= vcpu
->arch
.timing_sum_quad_duration
[type
];
84 vcpu
->arch
.timing_sum_quad_duration
[type
] += (duration
*duration
);
85 if (unlikely(old
> vcpu
->arch
.timing_sum_quad_duration
[type
])) {
86 printk(KERN_ERR
"%s - wrap adding sum of squared durations"
87 " old %lld new %lld type %d exit # of type %d\n",
89 vcpu
->arch
.timing_sum_quad_duration
[type
],
90 type
, vcpu
->arch
.timing_count_type
[type
]);
94 if (unlikely(duration
< vcpu
->arch
.timing_min_duration
[type
]))
95 vcpu
->arch
.timing_min_duration
[type
] = duration
;
96 if (unlikely(duration
> vcpu
->arch
.timing_max_duration
[type
]))
97 vcpu
->arch
.timing_max_duration
[type
] = duration
;
100 void kvmppc_update_timing_stats(struct kvm_vcpu
*vcpu
)
102 u64 exit
= vcpu
->arch
.timing_last_exit
;
103 u64 enter
= vcpu
->arch
.timing_last_enter
.tv64
;
105 /* save exit time, used next exit when the reenter time is known */
106 vcpu
->arch
.timing_last_exit
= vcpu
->arch
.timing_exit
.tv64
;
108 if (unlikely(vcpu
->arch
.last_exit_type
== 0xDEAD || exit
== 0))
109 return; /* skip incomplete cycle (e.g. after reset) */
111 /* update statistics for average and standard deviation */
112 add_exit_timing(vcpu
, (enter
- exit
), vcpu
->arch
.last_exit_type
);
113 /* enter -> timing_last_exit is time spent in guest - log this too */
114 add_exit_timing(vcpu
, (vcpu
->arch
.timing_last_exit
- enter
),
118 static const char *kvm_exit_names
[__NUMBER_OF_KVM_EXIT_TYPES
] = {
119 [MMIO_EXITS
] = "MMIO",
121 [SIGNAL_EXITS
] = "SIGNAL",
122 [ITLB_REAL_MISS_EXITS
] = "ITLBREAL",
123 [ITLB_VIRT_MISS_EXITS
] = "ITLBVIRT",
124 [DTLB_REAL_MISS_EXITS
] = "DTLBREAL",
125 [DTLB_VIRT_MISS_EXITS
] = "DTLBVIRT",
126 [SYSCALL_EXITS
] = "SYSCALL",
129 [EMULATED_INST_EXITS
] = "EMULINST",
130 [EMULATED_MTMSRWE_EXITS
] = "EMUL_WAIT",
131 [EMULATED_WRTEE_EXITS
] = "EMUL_WRTEE",
132 [EMULATED_MTSPR_EXITS
] = "EMUL_MTSPR",
133 [EMULATED_MFSPR_EXITS
] = "EMUL_MFSPR",
134 [EMULATED_MTMSR_EXITS
] = "EMUL_MTMSR",
135 [EMULATED_MFMSR_EXITS
] = "EMUL_MFMSR",
136 [EMULATED_TLBSX_EXITS
] = "EMUL_TLBSX",
137 [EMULATED_TLBWE_EXITS
] = "EMUL_TLBWE",
138 [EMULATED_RFI_EXITS
] = "EMUL_RFI",
140 [EXT_INTR_EXITS
] = "EXTINT",
141 [HALT_WAKEUP
] = "HALT",
142 [USR_PR_INST
] = "USR_PR_INST",
143 [FP_UNAVAIL
] = "FP_UNAVAIL",
144 [DEBUG_EXITS
] = "DEBUG",
145 [TIMEINGUEST
] = "TIMEINGUEST"
148 static int kvmppc_exit_timing_show(struct seq_file
*m
, void *private)
150 struct kvm_vcpu
*vcpu
= m
->private;
153 seq_printf(m
, "%s", "type count min max sum sum_squared\n");
155 for (i
= 0; i
< __NUMBER_OF_KVM_EXIT_TYPES
; i
++) {
156 seq_printf(m
, "%12s %10d %10lld %10lld %20lld %20lld\n",
158 vcpu
->arch
.timing_count_type
[i
],
159 vcpu
->arch
.timing_min_duration
[i
],
160 vcpu
->arch
.timing_max_duration
[i
],
161 vcpu
->arch
.timing_sum_duration
[i
],
162 vcpu
->arch
.timing_sum_quad_duration
[i
]);
167 /* Write 'c' to clear the timing statistics. */
168 static ssize_t
kvmppc_exit_timing_write(struct file
*file
,
169 const char __user
*user_buf
,
170 size_t count
, loff_t
*ppos
)
179 if (get_user(c
, user_buf
)) {
185 struct seq_file
*seqf
= (struct seq_file
*)file
->private_data
;
186 struct kvm_vcpu
*vcpu
= seqf
->private;
187 /* Write does not affect our buffers previously generated with
188 * show. seq_file is locked here to prevent races of init with
190 mutex_lock(&seqf
->lock
);
191 kvmppc_init_timing_stats(vcpu
);
192 mutex_unlock(&seqf
->lock
);
200 static int kvmppc_exit_timing_open(struct inode
*inode
, struct file
*file
)
202 return single_open(file
, kvmppc_exit_timing_show
, inode
->i_private
);
205 static const struct file_operations kvmppc_exit_timing_fops
= {
206 .owner
= THIS_MODULE
,
207 .open
= kvmppc_exit_timing_open
,
209 .write
= kvmppc_exit_timing_write
,
211 .release
= single_release
,
214 void kvmppc_create_vcpu_debugfs(struct kvm_vcpu
*vcpu
, unsigned int id
)
216 static char dbg_fname
[50];
217 struct dentry
*debugfs_file
;
219 snprintf(dbg_fname
, sizeof(dbg_fname
), "vm%u_vcpu%u_timing",
221 debugfs_file
= debugfs_create_file(dbg_fname
, 0666,
222 kvm_debugfs_dir
, vcpu
,
223 &kvmppc_exit_timing_fops
);
226 printk(KERN_ERR
"%s: error creating debugfs file %s\n",
227 __func__
, dbg_fname
);
231 vcpu
->arch
.debugfs_exit_timing
= debugfs_file
;
234 void kvmppc_remove_vcpu_debugfs(struct kvm_vcpu
*vcpu
)
236 if (vcpu
->arch
.debugfs_exit_timing
) {
237 debugfs_remove(vcpu
->arch
.debugfs_exit_timing
);
238 vcpu
->arch
.debugfs_exit_timing
= NULL
;