2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <machine/vmm.h>
44 * 'vst_num_elems' is the total number of addressable statistic elements
45 * 'vst_num_types' is the number of unique statistic types
47 * It is always true that 'vst_num_elems' is greater than or equal to
48 * 'vst_num_types'. This is because a stat type may represent more than
49 * one element (for e.g. VMM_STAT_ARRAY).
51 static int vst_num_elems, vst_num_types;
52 static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS];
54 static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
56 #define vst_size ((size_t)vst_num_elems * sizeof(uint64_t))
59 vmm_stat_register(void *arg)
61 struct vmm_stat_type *vst = arg;
63 /* We require all stats to identify themselves with a description */
64 if (vst->desc == NULL)
67 if (vst->scope == VMM_STAT_SCOPE_INTEL && !vmm_is_intel())
70 if (vst->scope == VMM_STAT_SCOPE_AMD && !vmm_is_amd())
73 if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) {
74 printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc);
78 vst->index = vst_num_elems;
79 vst_num_elems += vst->nelems;
81 vsttab[vst_num_types++] = vst;
85 vmm_stat_copy(struct vm *vm, int vcpu, int *num_stats, uint64_t *buf)
87 struct vmm_stat_type *vst;
91 if (vcpu < 0 || vcpu >= VM_MAXCPU)
94 /* Let stats functions update their counters */
95 for (i = 0; i < vst_num_types; i++) {
97 if (vst->func != NULL)
98 (*vst->func)(vm, vcpu, vst);
101 /* Copy over the stats */
102 stats = vcpu_stats(vm, vcpu);
103 for (i = 0; i < vst_num_elems; i++)
105 *num_stats = vst_num_elems;
113 return (malloc(vst_size, M_VMM_STAT, M_WAITOK));
117 vmm_stat_init(void *vp)
124 vmm_stat_free(void *vp)
126 free(vp, M_VMM_STAT);
130 vmm_stat_desc_copy(int index, char *buf, int bufsize)
133 struct vmm_stat_type *vst;
135 for (i = 0; i < vst_num_types; i++) {
137 if (index >= vst->index && index < vst->index + vst->nelems) {
138 if (vst->nelems > 1) {
139 snprintf(buf, bufsize, "%s[%d]",
140 vst->desc, index - vst->index);
142 strlcpy(buf, vst->desc, bufsize);
144 return (0); /* found it */
151 /* global statistics */
152 VMM_STAT(VCPU_MIGRATIONS, "vcpu migration across host cpus");
153 VMM_STAT(VMEXIT_COUNT, "total number of vm exits");
154 VMM_STAT(VMEXIT_EXTINT, "vm exits due to external interrupt");
155 VMM_STAT(VMEXIT_HLT, "number of times hlt was intercepted");
156 VMM_STAT(VMEXIT_CR_ACCESS, "number of times %cr access was intercepted");
157 VMM_STAT(VMEXIT_RDMSR, "number of times rdmsr was intercepted");
158 VMM_STAT(VMEXIT_WRMSR, "number of times wrmsr was intercepted");
159 VMM_STAT(VMEXIT_MTRAP, "number of monitor trap exits");
160 VMM_STAT(VMEXIT_PAUSE, "number of times pause was intercepted");
161 VMM_STAT(VMEXIT_INTR_WINDOW, "vm exits due to interrupt window opening");
162 VMM_STAT(VMEXIT_NMI_WINDOW, "vm exits due to nmi window opening");
163 VMM_STAT(VMEXIT_INOUT, "number of times in/out was intercepted");
164 VMM_STAT(VMEXIT_CPUID, "number of times cpuid was intercepted");
165 VMM_STAT(VMEXIT_NESTED_FAULT, "vm exits due to nested page fault");
166 VMM_STAT(VMEXIT_INST_EMUL, "vm exits for instruction emulation");
167 VMM_STAT(VMEXIT_UNKNOWN, "number of vm exits for unknown reason");
168 VMM_STAT(VMEXIT_ASTPENDING, "number of times astpending at exit");
169 VMM_STAT(VMEXIT_REQIDLE, "number of times idle requested at exit");
170 VMM_STAT(VMEXIT_USERSPACE, "number of vm exits handled in userspace");
171 VMM_STAT(VMEXIT_RENDEZVOUS, "number of times rendezvous pending at exit");
172 VMM_STAT(VMEXIT_EXCEPTION, "number of vm exits due to exceptions");