timing.c 7.2 KB

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