xref: /freebsd/sys/dev/vmm/vmm_stat.c (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
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/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 
34 #include <machine/vmm.h>
35 
36 #include <dev/vmm/vmm_stat.h>
37 
38 /*
39  * 'vst_num_elems' is the total number of addressable statistic elements
40  * 'vst_num_types' is the number of unique statistic types
41  *
42  * It is always true that 'vst_num_elems' is greater than or equal to
43  * 'vst_num_types'. This is because a stat type may represent more than
44  * one element (for e.g. VMM_STAT_ARRAY).
45  */
46 static int vst_num_elems, vst_num_types;
47 static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS];
48 
49 static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
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->pred != NULL && !vst->pred())
63 		return;
64 
65 	if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) {
66 		printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc);
67 		return;
68 	}
69 
70 	vst->index = vst_num_elems;
71 	vst_num_elems += vst->nelems;
72 
73 	vsttab[vst_num_types++] = vst;
74 }
75 
76 int
77 vmm_stat_copy(struct vcpu *vcpu, int index, int count, int *num_stats,
78     uint64_t *buf)
79 {
80 	struct vmm_stat_type *vst;
81 	uint64_t *stats;
82 	int i, tocopy;
83 
84 	if (index < 0 || count < 0)
85 		return (EINVAL);
86 
87 	if (index > vst_num_elems)
88 		return (ENOENT);
89 
90 	if (index == vst_num_elems) {
91 		*num_stats = 0;
92 		return (0);
93 	}
94 
95 	tocopy = min(vst_num_elems - index, count);
96 
97 	/* Let stats functions update their counters */
98 	for (i = 0; i < vst_num_types; i++) {
99 		vst = vsttab[i];
100 		if (vst->func != NULL)
101 			(*vst->func)(vcpu, vst);
102 	}
103 
104 	/* Copy over the stats */
105 	stats = vcpu_stats(vcpu);
106 	memcpy(buf, stats + index, tocopy * sizeof(stats[0]));
107 	*num_stats = tocopy;
108 	return (0);
109 }
110 
111 void *
112 vmm_stat_alloc(void)
113 {
114 
115 	return (malloc(vst_size, M_VMM_STAT, M_WAITOK));
116 }
117 
118 void
119 vmm_stat_init(void *vp)
120 {
121 
122 	bzero(vp, vst_size);
123 }
124 
125 void
126 vmm_stat_free(void *vp)
127 {
128 	free(vp, M_VMM_STAT);
129 }
130 
131 int
132 vmm_stat_desc_copy(int index, char *buf, int bufsize)
133 {
134 	int i;
135 	struct vmm_stat_type *vst;
136 
137 	for (i = 0; i < vst_num_types; i++) {
138 		vst = vsttab[i];
139 		if (index >= vst->index && index < vst->index + vst->nelems) {
140 			if (vst->nelems > 1) {
141 				snprintf(buf, bufsize, "%s[%d]",
142 					 vst->desc, index - vst->index);
143 			} else {
144 				strlcpy(buf, vst->desc, bufsize);
145 			}
146 			return (0);	/* found it */
147 		}
148 	}
149 
150 	return (EINVAL);
151 }
152