xref: /freebsd/sys/dev/vmm/vmm_stat.h (revision 93e81baa1c1baccf4eca9e3dbfe5ac9378e9623a)
1*93e81baaSMark Johnston /*-
2*93e81baaSMark Johnston  * SPDX-License-Identifier: BSD-3-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  * 3. Neither the name of the University nor the names of its contributors
16*93e81baaSMark Johnston  *    may be used to endorse or promote products derived from this software
17*93e81baaSMark Johnston  *    without specific prior written permission.
18*93e81baaSMark Johnston  *
19*93e81baaSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*93e81baaSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*93e81baaSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*93e81baaSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*93e81baaSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*93e81baaSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*93e81baaSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*93e81baaSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*93e81baaSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*93e81baaSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*93e81baaSMark Johnston  * SUCH DAMAGE.
30*93e81baaSMark Johnston  */
31*93e81baaSMark Johnston 
32*93e81baaSMark Johnston #ifndef _DEV_VMM_STAT_H_
33*93e81baaSMark Johnston #define	_DEV_VMM_STAT_H_
34*93e81baaSMark Johnston 
35*93e81baaSMark Johnston struct vm;
36*93e81baaSMark Johnston 
37*93e81baaSMark Johnston #define	MAX_VMM_STAT_ELEMS	64		/* arbitrary */
38*93e81baaSMark Johnston 
39*93e81baaSMark Johnston struct vmm_stat_type;
40*93e81baaSMark Johnston typedef void (*vmm_stat_func_t)(struct vcpu *vcpu,
41*93e81baaSMark Johnston     struct vmm_stat_type *stat);
42*93e81baaSMark Johnston typedef bool (*vmm_stat_func_pred_t)(void);
43*93e81baaSMark Johnston 
44*93e81baaSMark Johnston struct vmm_stat_type {
45*93e81baaSMark Johnston 	int	index;			/* position in the stats buffer */
46*93e81baaSMark Johnston 	int	nelems;			/* standalone or array */
47*93e81baaSMark Johnston 	const char *desc;		/* description of statistic */
48*93e81baaSMark Johnston 	vmm_stat_func_t func;
49*93e81baaSMark Johnston 	vmm_stat_func_pred_t pred;	/* predicate to check during registration */
50*93e81baaSMark Johnston };
51*93e81baaSMark Johnston 
52*93e81baaSMark Johnston void	vmm_stat_register(void *arg);
53*93e81baaSMark Johnston 
54*93e81baaSMark Johnston #define	VMM_STAT_FDEFINE(type, _nelems, _desc, _func, _pred)		\
55*93e81baaSMark Johnston 	struct vmm_stat_type type[1] = {				\
56*93e81baaSMark Johnston 		{							\
57*93e81baaSMark Johnston 			.index = -1,					\
58*93e81baaSMark Johnston 			.nelems = _nelems,				\
59*93e81baaSMark Johnston 			.desc = _desc,					\
60*93e81baaSMark Johnston 			.func = _func,					\
61*93e81baaSMark Johnston 			.pred = _pred,					\
62*93e81baaSMark Johnston 		}							\
63*93e81baaSMark Johnston 	};								\
64*93e81baaSMark Johnston 	SYSINIT(type##_stat, SI_SUB_KLD, SI_ORDER_ANY, vmm_stat_register, type)
65*93e81baaSMark Johnston 
66*93e81baaSMark Johnston #define VMM_STAT_DEFINE(type, nelems, desc, pred) 			\
67*93e81baaSMark Johnston 	VMM_STAT_FDEFINE(type, nelems, desc, NULL, pred)
68*93e81baaSMark Johnston 
69*93e81baaSMark Johnston #define	VMM_STAT_DECLARE(type)						\
70*93e81baaSMark Johnston 	extern struct vmm_stat_type type[1]
71*93e81baaSMark Johnston 
72*93e81baaSMark Johnston #define	VMM_STAT(type, desc)		\
73*93e81baaSMark Johnston 	VMM_STAT_DEFINE(type, 1, desc, NULL)
74*93e81baaSMark Johnston 
75*93e81baaSMark Johnston #define	VMM_STAT_FUNC(type, desc, func)	\
76*93e81baaSMark Johnston 	VMM_STAT_FDEFINE(type, 1, desc, func, NULL)
77*93e81baaSMark Johnston 
78*93e81baaSMark Johnston #define	VMM_STAT_ARRAY(type, nelems, desc)	\
79*93e81baaSMark Johnston 	VMM_STAT_DEFINE(type, nelems, desc, NULL)
80*93e81baaSMark Johnston 
81*93e81baaSMark Johnston void	*vmm_stat_alloc(void);
82*93e81baaSMark Johnston void	vmm_stat_init(void *vp);
83*93e81baaSMark Johnston void 	vmm_stat_free(void *vp);
84*93e81baaSMark Johnston 
85*93e81baaSMark Johnston int	vmm_stat_copy(struct vcpu *vcpu, int index, int count,
86*93e81baaSMark Johnston 	    int *num_stats, uint64_t *buf);
87*93e81baaSMark Johnston int	vmm_stat_desc_copy(int index, char *buf, int buflen);
88*93e81baaSMark Johnston 
89*93e81baaSMark Johnston static void __inline
vmm_stat_array_incr(struct vcpu * vcpu,struct vmm_stat_type * vst,int statidx,uint64_t x)90*93e81baaSMark Johnston vmm_stat_array_incr(struct vcpu *vcpu, struct vmm_stat_type *vst, int statidx,
91*93e81baaSMark Johnston     uint64_t x)
92*93e81baaSMark Johnston {
93*93e81baaSMark Johnston #ifdef VMM_KEEP_STATS
94*93e81baaSMark Johnston 	uint64_t *stats;
95*93e81baaSMark Johnston 
96*93e81baaSMark Johnston 	stats = vcpu_stats(vcpu);
97*93e81baaSMark Johnston 
98*93e81baaSMark Johnston 	if (vst->index >= 0 && statidx < vst->nelems)
99*93e81baaSMark Johnston 		stats[vst->index + statidx] += x;
100*93e81baaSMark Johnston #endif
101*93e81baaSMark Johnston }
102*93e81baaSMark Johnston 
103*93e81baaSMark Johnston static void __inline
vmm_stat_array_set(struct vcpu * vcpu,struct vmm_stat_type * vst,int statidx,uint64_t val)104*93e81baaSMark Johnston vmm_stat_array_set(struct vcpu *vcpu, struct vmm_stat_type *vst, int statidx,
105*93e81baaSMark Johnston     uint64_t val)
106*93e81baaSMark Johnston {
107*93e81baaSMark Johnston #ifdef VMM_KEEP_STATS
108*93e81baaSMark Johnston 	uint64_t *stats;
109*93e81baaSMark Johnston 
110*93e81baaSMark Johnston 	stats = vcpu_stats(vcpu);
111*93e81baaSMark Johnston 
112*93e81baaSMark Johnston 	if (vst->index >= 0 && statidx < vst->nelems)
113*93e81baaSMark Johnston 		stats[vst->index + statidx] = val;
114*93e81baaSMark Johnston #endif
115*93e81baaSMark Johnston }
116*93e81baaSMark Johnston 
117*93e81baaSMark Johnston static void __inline
vmm_stat_incr(struct vcpu * vcpu,struct vmm_stat_type * vst,uint64_t x)118*93e81baaSMark Johnston vmm_stat_incr(struct vcpu *vcpu, struct vmm_stat_type *vst, uint64_t x)
119*93e81baaSMark Johnston {
120*93e81baaSMark Johnston 
121*93e81baaSMark Johnston #ifdef VMM_KEEP_STATS
122*93e81baaSMark Johnston 	vmm_stat_array_incr(vcpu, vst, 0, x);
123*93e81baaSMark Johnston #endif
124*93e81baaSMark Johnston }
125*93e81baaSMark Johnston 
126*93e81baaSMark Johnston static void __inline
vmm_stat_set(struct vcpu * vcpu,struct vmm_stat_type * vst,uint64_t val)127*93e81baaSMark Johnston vmm_stat_set(struct vcpu *vcpu, struct vmm_stat_type *vst, uint64_t val)
128*93e81baaSMark Johnston {
129*93e81baaSMark Johnston 
130*93e81baaSMark Johnston #ifdef VMM_KEEP_STATS
131*93e81baaSMark Johnston 	vmm_stat_array_set(vcpu, vst, 0, val);
132*93e81baaSMark Johnston #endif
133*93e81baaSMark Johnston }
134*93e81baaSMark Johnston 
135*93e81baaSMark Johnston #endif /* !_DEV_VMM_STAT_H_ */
136