1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/kmem.h> 35 36 #include <machine/vmm.h> 37 #include "vmm_util.h" 38 #include "vmm_stat.h" 39 40 /* 41 * 'vst_num_elems' is the total number of addressable statistic elements 42 * 'vst_num_types' is the number of unique statistic types 43 * 44 * It is always true that 'vst_num_elems' is greater than or equal to 45 * 'vst_num_types'. This is because a stat type may represent more than 46 * one element (for e.g. VMM_STAT_ARRAY). 47 */ 48 static int vst_num_elems, vst_num_types; 49 static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS]; 50 51 #define vst_size ((size_t)vst_num_elems * sizeof (uint64_t)) 52 53 void 54 vmm_stat_register(void *arg) 55 { 56 struct vmm_stat_type *vst = arg; 57 58 /* We require all stats to identify themselves with a description */ 59 if (vst->desc == NULL) 60 return; 61 62 if (vst->scope == VMM_STAT_SCOPE_INTEL && !vmm_is_intel()) 63 return; 64 65 if (vst->scope == VMM_STAT_SCOPE_AMD && !vmm_is_svm()) 66 return; 67 68 if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) { 69 printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc); 70 return; 71 } 72 73 vst->index = vst_num_elems; 74 vst_num_elems += vst->nelems; 75 76 vsttab[vst_num_types++] = vst; 77 } 78 79 int 80 vmm_stat_copy(struct vm *vm, int vcpu, int index, int count, int *num_stats, 81 uint64_t *buf) 82 { 83 struct vmm_stat_type *vst; 84 uint64_t *stats; 85 int i, tocopy; 86 87 if (vcpu < 0 || vcpu >= vm_get_maxcpus(vm)) 88 return (EINVAL); 89 90 if (index < 0 || count < 0) 91 return (EINVAL); 92 93 if (index > vst_num_elems) 94 return (ENOENT); 95 96 if (index == vst_num_elems) { 97 *num_stats = 0; 98 return (0); 99 } 100 101 tocopy = min(vst_num_elems - index, count); 102 103 /* Let stats functions update their counters */ 104 for (i = 0; i < vst_num_types; i++) { 105 vst = vsttab[i]; 106 if (vst->func != NULL) 107 (*vst->func)(vm, vcpu, vst); 108 } 109 110 /* Copy over the stats */ 111 stats = vcpu_stats(vm, vcpu); 112 memcpy(buf, stats + index, tocopy * sizeof (stats[0])); 113 *num_stats = tocopy; 114 return (0); 115 } 116 117 void * 118 vmm_stat_alloc(void) 119 { 120 return (kmem_alloc(vst_size, KM_SLEEP)); 121 } 122 123 void 124 vmm_stat_init(void *vp) 125 { 126 bzero(vp, vst_size); 127 } 128 129 void 130 vmm_stat_free(void *vp) 131 { 132 kmem_free(vp, vst_size); 133 } 134 135 int 136 vmm_stat_desc_copy(int index, char *buf, int bufsize) 137 { 138 int i; 139 struct vmm_stat_type *vst; 140 141 for (i = 0; i < vst_num_types; i++) { 142 vst = vsttab[i]; 143 if (index >= vst->index && index < vst->index + vst->nelems) { 144 if (vst->nelems > 1) { 145 (void) snprintf(buf, bufsize, "%s[%d]", 146 vst->desc, index - vst->index); 147 } else { 148 (void) strlcpy(buf, vst->desc, bufsize); 149 } 150 return (0); /* found it */ 151 } 152 } 153 154 return (EINVAL); 155 } 156 157 /* global statistics */ 158 VMM_STAT(VCPU_MIGRATIONS, "vcpu migration across host cpus"); 159 VMM_STAT(VMEXIT_COUNT, "total number of vm exits"); 160 VMM_STAT(VMEXIT_EXTINT, "vm exits due to external interrupt"); 161 VMM_STAT(VMEXIT_HLT, "number of times hlt was intercepted"); 162 VMM_STAT(VMEXIT_CR_ACCESS, "number of times %cr access was intercepted"); 163 VMM_STAT(VMEXIT_RDMSR, "number of times rdmsr was intercepted"); 164 VMM_STAT(VMEXIT_WRMSR, "number of times wrmsr was intercepted"); 165 VMM_STAT(VMEXIT_MTRAP, "number of monitor trap exits"); 166 VMM_STAT(VMEXIT_PAUSE, "number of times pause was intercepted"); 167 VMM_STAT(VMEXIT_INTR_WINDOW, "vm exits due to interrupt window opening"); 168 VMM_STAT(VMEXIT_NMI_WINDOW, "vm exits due to nmi window opening"); 169 VMM_STAT(VMEXIT_INOUT, "number of times in/out was intercepted"); 170 VMM_STAT(VMEXIT_CPUID, "number of times cpuid was intercepted"); 171 VMM_STAT(VMEXIT_NESTED_FAULT, "vm exits due to nested page fault"); 172 VMM_STAT(VMEXIT_MMIO_EMUL, "vm exits for mmio emulation"); 173 VMM_STAT(VMEXIT_UNKNOWN, "number of vm exits for unknown reason"); 174 VMM_STAT(VMEXIT_ASTPENDING, "number of times astpending at exit"); 175 VMM_STAT(VMEXIT_REQIDLE, "number of times idle requested at exit"); 176 VMM_STAT(VMEXIT_EXCEPTION, "number of vm exits due to exceptions"); 177 VMM_STAT(VMEXIT_RUN_STATE, "number of vm exits due to run_state change"); 178