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