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