xref: /freebsd/sys/dev/vmm/vmm_stat.c (revision 93e81baa1c1baccf4eca9e3dbfe5ac9378e9623a)
1*93e81baaSMark Johnston /*-
2*93e81baaSMark Johnston  * SPDX-License-Identifier: BSD-2-Clause
3*93e81baaSMark Johnston  *
4*93e81baaSMark Johnston  * Copyright (c) 2011 NetApp, Inc.
5*93e81baaSMark Johnston  * All rights reserved.
6*93e81baaSMark Johnston  *
7*93e81baaSMark Johnston  * Redistribution and use in source and binary forms, with or without
8*93e81baaSMark Johnston  * modification, are permitted provided that the following conditions
9*93e81baaSMark Johnston  * are met:
10*93e81baaSMark Johnston  * 1. Redistributions of source code must retain the above copyright
11*93e81baaSMark Johnston  *    notice, this list of conditions and the following disclaimer.
12*93e81baaSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
13*93e81baaSMark Johnston  *    notice, this list of conditions and the following disclaimer in the
14*93e81baaSMark Johnston  *    documentation and/or other materials provided with the distribution.
15*93e81baaSMark Johnston  *
16*93e81baaSMark Johnston  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17*93e81baaSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*93e81baaSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*93e81baaSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20*93e81baaSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*93e81baaSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*93e81baaSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*93e81baaSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*93e81baaSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*93e81baaSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*93e81baaSMark Johnston  * SUCH DAMAGE.
27*93e81baaSMark Johnston  */
28*93e81baaSMark Johnston 
29*93e81baaSMark Johnston #include <sys/param.h>
30*93e81baaSMark Johnston #include <sys/kernel.h>
31*93e81baaSMark Johnston #include <sys/systm.h>
32*93e81baaSMark Johnston #include <sys/malloc.h>
33*93e81baaSMark Johnston 
34*93e81baaSMark Johnston #include <machine/vmm.h>
35*93e81baaSMark Johnston 
36*93e81baaSMark Johnston #include <dev/vmm/vmm_stat.h>
37*93e81baaSMark Johnston 
38*93e81baaSMark Johnston /*
39*93e81baaSMark Johnston  * 'vst_num_elems' is the total number of addressable statistic elements
40*93e81baaSMark Johnston  * 'vst_num_types' is the number of unique statistic types
41*93e81baaSMark Johnston  *
42*93e81baaSMark Johnston  * It is always true that 'vst_num_elems' is greater than or equal to
43*93e81baaSMark Johnston  * 'vst_num_types'. This is because a stat type may represent more than
44*93e81baaSMark Johnston  * one element (for e.g. VMM_STAT_ARRAY).
45*93e81baaSMark Johnston  */
46*93e81baaSMark Johnston static int vst_num_elems, vst_num_types;
47*93e81baaSMark Johnston static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS];
48*93e81baaSMark Johnston 
49*93e81baaSMark Johnston static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
50*93e81baaSMark Johnston 
51*93e81baaSMark Johnston #define	vst_size	((size_t)vst_num_elems * sizeof(uint64_t))
52*93e81baaSMark Johnston 
53*93e81baaSMark Johnston void
vmm_stat_register(void * arg)54*93e81baaSMark Johnston vmm_stat_register(void *arg)
55*93e81baaSMark Johnston {
56*93e81baaSMark Johnston 	struct vmm_stat_type *vst = arg;
57*93e81baaSMark Johnston 
58*93e81baaSMark Johnston 	/* We require all stats to identify themselves with a description */
59*93e81baaSMark Johnston 	if (vst->desc == NULL)
60*93e81baaSMark Johnston 		return;
61*93e81baaSMark Johnston 
62*93e81baaSMark Johnston 	if (vst->pred != NULL && !vst->pred())
63*93e81baaSMark Johnston 		return;
64*93e81baaSMark Johnston 
65*93e81baaSMark Johnston 	if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) {
66*93e81baaSMark Johnston 		printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc);
67*93e81baaSMark Johnston 		return;
68*93e81baaSMark Johnston 	}
69*93e81baaSMark Johnston 
70*93e81baaSMark Johnston 	vst->index = vst_num_elems;
71*93e81baaSMark Johnston 	vst_num_elems += vst->nelems;
72*93e81baaSMark Johnston 
73*93e81baaSMark Johnston 	vsttab[vst_num_types++] = vst;
74*93e81baaSMark Johnston }
75*93e81baaSMark Johnston 
76*93e81baaSMark Johnston int
vmm_stat_copy(struct vcpu * vcpu,int index,int count,int * num_stats,uint64_t * buf)77*93e81baaSMark Johnston vmm_stat_copy(struct vcpu *vcpu, int index, int count, int *num_stats,
78*93e81baaSMark Johnston     uint64_t *buf)
79*93e81baaSMark Johnston {
80*93e81baaSMark Johnston 	struct vmm_stat_type *vst;
81*93e81baaSMark Johnston 	uint64_t *stats;
82*93e81baaSMark Johnston 	int i, tocopy;
83*93e81baaSMark Johnston 
84*93e81baaSMark Johnston 	if (index < 0 || count < 0)
85*93e81baaSMark Johnston 		return (EINVAL);
86*93e81baaSMark Johnston 
87*93e81baaSMark Johnston 	if (index > vst_num_elems)
88*93e81baaSMark Johnston 		return (ENOENT);
89*93e81baaSMark Johnston 
90*93e81baaSMark Johnston 	if (index == vst_num_elems) {
91*93e81baaSMark Johnston 		*num_stats = 0;
92*93e81baaSMark Johnston 		return (0);
93*93e81baaSMark Johnston 	}
94*93e81baaSMark Johnston 
95*93e81baaSMark Johnston 	tocopy = min(vst_num_elems - index, count);
96*93e81baaSMark Johnston 
97*93e81baaSMark Johnston 	/* Let stats functions update their counters */
98*93e81baaSMark Johnston 	for (i = 0; i < vst_num_types; i++) {
99*93e81baaSMark Johnston 		vst = vsttab[i];
100*93e81baaSMark Johnston 		if (vst->func != NULL)
101*93e81baaSMark Johnston 			(*vst->func)(vcpu, vst);
102*93e81baaSMark Johnston 	}
103*93e81baaSMark Johnston 
104*93e81baaSMark Johnston 	/* Copy over the stats */
105*93e81baaSMark Johnston 	stats = vcpu_stats(vcpu);
106*93e81baaSMark Johnston 	memcpy(buf, stats + index, tocopy * sizeof(stats[0]));
107*93e81baaSMark Johnston 	*num_stats = tocopy;
108*93e81baaSMark Johnston 	return (0);
109*93e81baaSMark Johnston }
110*93e81baaSMark Johnston 
111*93e81baaSMark Johnston void *
vmm_stat_alloc(void)112*93e81baaSMark Johnston vmm_stat_alloc(void)
113*93e81baaSMark Johnston {
114*93e81baaSMark Johnston 
115*93e81baaSMark Johnston 	return (malloc(vst_size, M_VMM_STAT, M_WAITOK));
116*93e81baaSMark Johnston }
117*93e81baaSMark Johnston 
118*93e81baaSMark Johnston void
vmm_stat_init(void * vp)119*93e81baaSMark Johnston vmm_stat_init(void *vp)
120*93e81baaSMark Johnston {
121*93e81baaSMark Johnston 
122*93e81baaSMark Johnston 	bzero(vp, vst_size);
123*93e81baaSMark Johnston }
124*93e81baaSMark Johnston 
125*93e81baaSMark Johnston void
vmm_stat_free(void * vp)126*93e81baaSMark Johnston vmm_stat_free(void *vp)
127*93e81baaSMark Johnston {
128*93e81baaSMark Johnston 	free(vp, M_VMM_STAT);
129*93e81baaSMark Johnston }
130*93e81baaSMark Johnston 
131*93e81baaSMark Johnston int
vmm_stat_desc_copy(int index,char * buf,int bufsize)132*93e81baaSMark Johnston vmm_stat_desc_copy(int index, char *buf, int bufsize)
133*93e81baaSMark Johnston {
134*93e81baaSMark Johnston 	int i;
135*93e81baaSMark Johnston 	struct vmm_stat_type *vst;
136*93e81baaSMark Johnston 
137*93e81baaSMark Johnston 	for (i = 0; i < vst_num_types; i++) {
138*93e81baaSMark Johnston 		vst = vsttab[i];
139*93e81baaSMark Johnston 		if (index >= vst->index && index < vst->index + vst->nelems) {
140*93e81baaSMark Johnston 			if (vst->nelems > 1) {
141*93e81baaSMark Johnston 				snprintf(buf, bufsize, "%s[%d]",
142*93e81baaSMark Johnston 					 vst->desc, index - vst->index);
143*93e81baaSMark Johnston 			} else {
144*93e81baaSMark Johnston 				strlcpy(buf, vst->desc, bufsize);
145*93e81baaSMark Johnston 			}
146*93e81baaSMark Johnston 			return (0);	/* found it */
147*93e81baaSMark Johnston 		}
148*93e81baaSMark Johnston 	}
149*93e81baaSMark Johnston 
150*93e81baaSMark Johnston 	return (EINVAL);
151*93e81baaSMark Johnston }
152