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