1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38
39 #include <machine/vmm.h>
40 #include "vmm_util.h"
41 #include "vmm_stat.h"
42
43 /*
44 * 'vst_num_elems' is the total number of addressable statistic elements
45 * 'vst_num_types' is the number of unique statistic types
46 *
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).
50 */
51 static int vst_num_elems, vst_num_types;
52 static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS];
53
54 static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
55
56 #define vst_size ((size_t)vst_num_elems * sizeof(uint64_t))
57
58 void
59 vmm_stat_register(void *arg)
60 {
61 struct vmm_stat_type *vst = arg;
62
63 /* We require all stats to identify themselves with a description */
64 if (vst->desc == NULL)
65 return;
66
67 if (vst->scope == VMM_STAT_SCOPE_INTEL && !vmm_is_intel())
68 return;
69
70 if (vst->scope == VMM_STAT_SCOPE_AMD && !vmm_is_svm())
71 return;
72
73 if (vst->nelems == VMM_STAT_NELEMS_VCPU)
74 vst->nelems = vm_maxcpu;
75
76 if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) {
77 printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc);
78 return;
79 }
80
81 vst->index = vst_num_elems;
82 vst_num_elems += vst->nelems;
83
84 vsttab[vst_num_types++] = vst;
85 }
86
87 int
88 vmm_stat_copy(struct vcpu *vcpu, int index, int count, int *num_stats,
89 uint64_t *buf)
90 {
91 struct vmm_stat_type *vst;
92 uint64_t *stats;
93 int i, tocopy;
94
95 if (index < 0 || count < 0)
96 return (EINVAL);
97
98 if (index > vst_num_elems)
99 return (ENOENT);
100
101 if (index == vst_num_elems) {
102 *num_stats = 0;
103 return (0);
104 }
105
106 tocopy = min(vst_num_elems - index, count);
107
108 /* Let stats functions update their counters */
109 for (i = 0; i < vst_num_types; i++) {
110 vst = vsttab[i];
111 if (vst->func != NULL)
112 (*vst->func)(vcpu, vst);
113 }
114
115 /* Copy over the stats */
116 stats = vcpu_stats(vcpu);
117 memcpy(buf, stats + index, tocopy * sizeof(stats[0]));
118 *num_stats = tocopy;
119 return (0);
120 }
121
122 void *
123 vmm_stat_alloc(void)
124 {
125
126 return (malloc(vst_size, M_VMM_STAT, M_WAITOK));
127 }
128
129 void
130 vmm_stat_init(void *vp)
131 {
132
133 bzero(vp, vst_size);
134 }
135
136 void
137 vmm_stat_free(void *vp)
138 {
139 free(vp, M_VMM_STAT);
140 }
141
142 int
143 vmm_stat_desc_copy(int index, char *buf, int bufsize)
144 {
145 int i;
146 struct vmm_stat_type *vst;
147
148 for (i = 0; i < vst_num_types; i++) {
149 vst = vsttab[i];
150 if (index >= vst->index && index < vst->index + vst->nelems) {
151 if (vst->nelems > 1) {
152 snprintf(buf, bufsize, "%s[%d]",
153 vst->desc, index - vst->index);
154 } else {
155 strlcpy(buf, vst->desc, bufsize);
156 }
157 return (0); /* found it */
158 }
159 }
160
161 return (EINVAL);
162 }
163
164 /* global statistics */
165 VMM_STAT(VCPU_MIGRATIONS, "vcpu migration across host cpus");
166 VMM_STAT(VMEXIT_COUNT, "total number of vm exits");
167 VMM_STAT(VMEXIT_EXTINT, "vm exits due to external interrupt");
168 VMM_STAT(VMEXIT_HLT, "number of times hlt was intercepted");
169 VMM_STAT(VMEXIT_CR_ACCESS, "number of times %cr access was intercepted");
170 VMM_STAT(VMEXIT_RDMSR, "number of times rdmsr was intercepted");
171 VMM_STAT(VMEXIT_WRMSR, "number of times wrmsr was intercepted");
172 VMM_STAT(VMEXIT_MTRAP, "number of monitor trap exits");
173 VMM_STAT(VMEXIT_PAUSE, "number of times pause was intercepted");
174 VMM_STAT(VMEXIT_INTR_WINDOW, "vm exits due to interrupt window opening");
175 VMM_STAT(VMEXIT_NMI_WINDOW, "vm exits due to nmi window opening");
176 VMM_STAT(VMEXIT_INOUT, "number of times in/out was intercepted");
177 VMM_STAT(VMEXIT_CPUID, "number of times cpuid was intercepted");
178 VMM_STAT(VMEXIT_NESTED_FAULT, "vm exits due to nested page fault");
179 VMM_STAT(VMEXIT_INST_EMUL, "vm exits for instruction emulation");
180 VMM_STAT(VMEXIT_UNKNOWN, "number of vm exits for unknown reason");
181 VMM_STAT(VMEXIT_ASTPENDING, "number of times astpending at exit");
182 VMM_STAT(VMEXIT_REQIDLE, "number of times idle requested at exit");
183 VMM_STAT(VMEXIT_USERSPACE, "number of vm exits handled in userspace");
184 VMM_STAT(VMEXIT_RENDEZVOUS, "number of times rendezvous pending at exit");
185 VMM_STAT(VMEXIT_EXCEPTION, "number of vm exits due to exceptions");
Cache object: c0d239bf4f021886e44769dcee699072
|