timing.c 7.3 KB

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