Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 1 | /* |
| 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. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 14 | * |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 15 | * Copyright IBM Corp. 2008 |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 16 | * |
| 17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> |
| 18 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kvm_host.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/uaccess.h> |
Benjamin Herrenschmidt | e0ea8b2 | 2009-11-05 17:17:12 +1100 | [diff] [blame] | 26 | #include <linux/module.h> |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 27 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 28 | #include <asm/time.h> |
| 29 | #include <asm-generic/div64.h> |
| 30 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 31 | #include "timing.h" |
| 32 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 33 | void kvmppc_init_timing_stats(struct kvm_vcpu *vcpu) |
| 34 | { |
| 35 | int i; |
| 36 | |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 37 | /* Take a lock to avoid concurrent updates */ |
| 38 | mutex_lock(&vcpu->arch.exit_timing_lock); |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 39 | |
| 40 | vcpu->arch.last_exit_type = 0xDEAD; |
| 41 | for (i = 0; i < __NUMBER_OF_KVM_EXIT_TYPES; i++) { |
| 42 | vcpu->arch.timing_count_type[i] = 0; |
| 43 | vcpu->arch.timing_max_duration[i] = 0; |
| 44 | vcpu->arch.timing_min_duration[i] = 0xFFFFFFFF; |
| 45 | vcpu->arch.timing_sum_duration[i] = 0; |
| 46 | vcpu->arch.timing_sum_quad_duration[i] = 0; |
| 47 | } |
| 48 | vcpu->arch.timing_last_exit = 0; |
| 49 | vcpu->arch.timing_exit.tv64 = 0; |
| 50 | vcpu->arch.timing_last_enter.tv64 = 0; |
| 51 | |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 52 | mutex_unlock(&vcpu->arch.exit_timing_lock); |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 53 | } |
| 54 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 55 | static void add_exit_timing(struct kvm_vcpu *vcpu, u64 duration, int type) |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 56 | { |
| 57 | u64 old; |
| 58 | |
| 59 | do_div(duration, tb_ticks_per_usec); |
| 60 | if (unlikely(duration > 0xFFFFFFFF)) { |
| 61 | printk(KERN_ERR"%s - duration too big -> overflow" |
| 62 | " duration %lld type %d exit #%d\n", |
| 63 | __func__, duration, type, |
| 64 | vcpu->arch.timing_count_type[type]); |
| 65 | return; |
| 66 | } |
| 67 | |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 68 | mutex_lock(&vcpu->arch.exit_timing_lock); |
| 69 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 70 | vcpu->arch.timing_count_type[type]++; |
| 71 | |
| 72 | /* sum */ |
| 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]); |
| 80 | } |
| 81 | |
| 82 | /* square sum */ |
| 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", |
| 88 | __func__, old, |
| 89 | vcpu->arch.timing_sum_quad_duration[type], |
| 90 | type, vcpu->arch.timing_count_type[type]); |
| 91 | } |
| 92 | |
| 93 | /* set min/max */ |
| 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; |
Bharat Bhushan | 09000ad | 2011-03-25 10:32:13 +0530 | [diff] [blame] | 98 | |
| 99 | mutex_unlock(&vcpu->arch.exit_timing_lock); |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void kvmppc_update_timing_stats(struct kvm_vcpu *vcpu) |
| 103 | { |
| 104 | u64 exit = vcpu->arch.timing_last_exit; |
| 105 | u64 enter = vcpu->arch.timing_last_enter.tv64; |
| 106 | |
| 107 | /* save exit time, used next exit when the reenter time is known */ |
| 108 | vcpu->arch.timing_last_exit = vcpu->arch.timing_exit.tv64; |
| 109 | |
| 110 | if (unlikely(vcpu->arch.last_exit_type == 0xDEAD || exit == 0)) |
| 111 | return; /* skip incomplete cycle (e.g. after reset) */ |
| 112 | |
| 113 | /* update statistics for average and standard deviation */ |
| 114 | add_exit_timing(vcpu, (enter - exit), vcpu->arch.last_exit_type); |
| 115 | /* enter -> timing_last_exit is time spent in guest - log this too */ |
| 116 | add_exit_timing(vcpu, (vcpu->arch.timing_last_exit - enter), |
| 117 | TIMEINGUEST); |
| 118 | } |
| 119 | |
| 120 | static const char *kvm_exit_names[__NUMBER_OF_KVM_EXIT_TYPES] = { |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 121 | [MMIO_EXITS] = "MMIO", |
| 122 | [DCR_EXITS] = "DCR", |
| 123 | [SIGNAL_EXITS] = "SIGNAL", |
| 124 | [ITLB_REAL_MISS_EXITS] = "ITLBREAL", |
| 125 | [ITLB_VIRT_MISS_EXITS] = "ITLBVIRT", |
| 126 | [DTLB_REAL_MISS_EXITS] = "DTLBREAL", |
| 127 | [DTLB_VIRT_MISS_EXITS] = "DTLBVIRT", |
| 128 | [SYSCALL_EXITS] = "SYSCALL", |
| 129 | [ISI_EXITS] = "ISI", |
| 130 | [DSI_EXITS] = "DSI", |
| 131 | [EMULATED_INST_EXITS] = "EMULINST", |
| 132 | [EMULATED_MTMSRWE_EXITS] = "EMUL_WAIT", |
| 133 | [EMULATED_WRTEE_EXITS] = "EMUL_WRTEE", |
| 134 | [EMULATED_MTSPR_EXITS] = "EMUL_MTSPR", |
| 135 | [EMULATED_MFSPR_EXITS] = "EMUL_MFSPR", |
| 136 | [EMULATED_MTMSR_EXITS] = "EMUL_MTMSR", |
| 137 | [EMULATED_MFMSR_EXITS] = "EMUL_MFMSR", |
| 138 | [EMULATED_TLBSX_EXITS] = "EMUL_TLBSX", |
| 139 | [EMULATED_TLBWE_EXITS] = "EMUL_TLBWE", |
| 140 | [EMULATED_RFI_EXITS] = "EMUL_RFI", |
| 141 | [DEC_EXITS] = "DEC", |
| 142 | [EXT_INTR_EXITS] = "EXTINT", |
| 143 | [HALT_WAKEUP] = "HALT", |
| 144 | [USR_PR_INST] = "USR_PR_INST", |
| 145 | [FP_UNAVAIL] = "FP_UNAVAIL", |
| 146 | [DEBUG_EXITS] = "DEBUG", |
| 147 | [TIMEINGUEST] = "TIMEINGUEST" |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static int kvmppc_exit_timing_show(struct seq_file *m, void *private) |
| 151 | { |
| 152 | struct kvm_vcpu *vcpu = m->private; |
| 153 | int i; |
Stuart Yoder | 1a040b2 | 2011-03-28 15:01:56 -0500 | [diff] [blame^] | 154 | u64 min, max, sum, sum_quad; |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 155 | |
| 156 | seq_printf(m, "%s", "type count min max sum sum_squared\n"); |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 157 | |
Stuart Yoder | 1a040b2 | 2011-03-28 15:01:56 -0500 | [diff] [blame^] | 158 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 159 | for (i = 0; i < __NUMBER_OF_KVM_EXIT_TYPES; i++) { |
Stuart Yoder | 1a040b2 | 2011-03-28 15:01:56 -0500 | [diff] [blame^] | 160 | |
| 161 | min = vcpu->arch.timing_min_duration[i]; |
| 162 | do_div(min, tb_ticks_per_usec); |
| 163 | max = vcpu->arch.timing_max_duration[i]; |
| 164 | do_div(max, tb_ticks_per_usec); |
| 165 | sum = vcpu->arch.timing_sum_duration[i]; |
| 166 | do_div(sum, tb_ticks_per_usec); |
| 167 | sum_quad = vcpu->arch.timing_sum_quad_duration[i]; |
| 168 | do_div(sum_quad, tb_ticks_per_usec); |
| 169 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 170 | seq_printf(m, "%12s %10d %10lld %10lld %20lld %20lld\n", |
| 171 | kvm_exit_names[i], |
| 172 | vcpu->arch.timing_count_type[i], |
Stuart Yoder | 1a040b2 | 2011-03-28 15:01:56 -0500 | [diff] [blame^] | 173 | min, |
| 174 | max, |
| 175 | sum, |
| 176 | sum_quad); |
| 177 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 182 | /* Write 'c' to clear the timing statistics. */ |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 183 | static ssize_t kvmppc_exit_timing_write(struct file *file, |
| 184 | const char __user *user_buf, |
| 185 | size_t count, loff_t *ppos) |
| 186 | { |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 187 | int err = -EINVAL; |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 188 | char c; |
| 189 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 190 | if (count > 1) { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 191 | goto done; |
| 192 | } |
| 193 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 194 | if (get_user(c, user_buf)) { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 195 | err = -EFAULT; |
| 196 | goto done; |
| 197 | } |
| 198 | |
| 199 | if (c == 'c') { |
Joe Perches | ea01c6b | 2010-07-12 10:49:55 +0000 | [diff] [blame] | 200 | struct seq_file *seqf = file->private_data; |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 201 | struct kvm_vcpu *vcpu = seqf->private; |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 202 | /* Write does not affect our buffers previously generated with |
| 203 | * show. seq_file is locked here to prevent races of init with |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 204 | * a show call */ |
| 205 | mutex_lock(&seqf->lock); |
| 206 | kvmppc_init_timing_stats(vcpu); |
| 207 | mutex_unlock(&seqf->lock); |
| 208 | err = count; |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | done: |
| 212 | return err; |
| 213 | } |
| 214 | |
| 215 | static int kvmppc_exit_timing_open(struct inode *inode, struct file *file) |
| 216 | { |
| 217 | return single_open(file, kvmppc_exit_timing_show, inode->i_private); |
| 218 | } |
| 219 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 220 | static const struct file_operations kvmppc_exit_timing_fops = { |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 221 | .owner = THIS_MODULE, |
| 222 | .open = kvmppc_exit_timing_open, |
| 223 | .read = seq_read, |
| 224 | .write = kvmppc_exit_timing_write, |
| 225 | .llseek = seq_lseek, |
| 226 | .release = single_release, |
| 227 | }; |
| 228 | |
| 229 | void kvmppc_create_vcpu_debugfs(struct kvm_vcpu *vcpu, unsigned int id) |
| 230 | { |
| 231 | static char dbg_fname[50]; |
| 232 | struct dentry *debugfs_file; |
| 233 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 234 | snprintf(dbg_fname, sizeof(dbg_fname), "vm%u_vcpu%u_timing", |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 235 | current->pid, id); |
| 236 | debugfs_file = debugfs_create_file(dbg_fname, 0666, |
| 237 | kvm_debugfs_dir, vcpu, |
| 238 | &kvmppc_exit_timing_fops); |
| 239 | |
| 240 | if (!debugfs_file) { |
| 241 | printk(KERN_ERR"%s: error creating debugfs file %s\n", |
| 242 | __func__, dbg_fname); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | vcpu->arch.debugfs_exit_timing = debugfs_file; |
| 247 | } |
| 248 | |
| 249 | void kvmppc_remove_vcpu_debugfs(struct kvm_vcpu *vcpu) |
| 250 | { |
| 251 | if (vcpu->arch.debugfs_exit_timing) { |
| 252 | debugfs_remove(vcpu->arch.debugfs_exit_timing); |
| 253 | vcpu->arch.debugfs_exit_timing = NULL; |
| 254 | } |
| 255 | } |