1bf21cd93STycho Nightingale /*-
24c87aefeSPatrick Mooney * SPDX-License-Identifier: BSD-3-Clause
34c87aefeSPatrick Mooney *
4bf21cd93STycho Nightingale * Copyright (c) 2011 NetApp, Inc.
5bf21cd93STycho Nightingale * All rights reserved.
6bf21cd93STycho Nightingale *
7bf21cd93STycho Nightingale * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale * are met:
10bf21cd93STycho Nightingale * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale * notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale * notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale * documentation and/or other materials provided with the distribution.
154c87aefeSPatrick Mooney * 3. Neither the name of the University nor the names of its contributors
16bf21cd93STycho Nightingale * may be used to endorse or promote products derived from this software
17bf21cd93STycho Nightingale * without specific prior written permission.
18bf21cd93STycho Nightingale *
19bf21cd93STycho Nightingale * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20bf21cd93STycho Nightingale * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bf21cd93STycho Nightingale * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bf21cd93STycho Nightingale * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23bf21cd93STycho Nightingale * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bf21cd93STycho Nightingale * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bf21cd93STycho Nightingale * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bf21cd93STycho Nightingale * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bf21cd93STycho Nightingale * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bf21cd93STycho Nightingale * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bf21cd93STycho Nightingale * SUCH DAMAGE.
304c87aefeSPatrick Mooney */
314c87aefeSPatrick Mooney /*
324c87aefeSPatrick Mooney * Copyright 2018 Joyent, Inc.
33bf21cd93STycho Nightingale */
34bf21cd93STycho Nightingale
35bf21cd93STycho Nightingale #ifndef _VMM_STAT_H_
36bf21cd93STycho Nightingale #define _VMM_STAT_H_
37bf21cd93STycho Nightingale
384c87aefeSPatrick Mooney #include <machine/vmm.h>
394c87aefeSPatrick Mooney
40bf21cd93STycho Nightingale struct vm;
41bf21cd93STycho Nightingale
424c87aefeSPatrick Mooney #ifdef __FreeBSD__
434c87aefeSPatrick Mooney #define MAX_VMM_STAT_ELEMS 64 /* arbitrary */
444c87aefeSPatrick Mooney #else
454c87aefeSPatrick Mooney #define MAX_VMM_STAT_ELEMS (64 + VM_MAXCPU) /* arbitrary */
464c87aefeSPatrick Mooney #endif
47bf21cd93STycho Nightingale
48bf21cd93STycho Nightingale enum vmm_stat_scope {
49bf21cd93STycho Nightingale VMM_STAT_SCOPE_ANY,
50bf21cd93STycho Nightingale VMM_STAT_SCOPE_INTEL, /* Intel VMX specific statistic */
51bf21cd93STycho Nightingale VMM_STAT_SCOPE_AMD, /* AMD SVM specific statistic */
52bf21cd93STycho Nightingale };
53bf21cd93STycho Nightingale
544c87aefeSPatrick Mooney struct vmm_stat_type;
554c87aefeSPatrick Mooney typedef void (*vmm_stat_func_t)(struct vm *vm, int vcpu,
564c87aefeSPatrick Mooney struct vmm_stat_type *stat);
574c87aefeSPatrick Mooney
58bf21cd93STycho Nightingale struct vmm_stat_type {
59bf21cd93STycho Nightingale int index; /* position in the stats buffer */
60bf21cd93STycho Nightingale int nelems; /* standalone or array */
61bf21cd93STycho Nightingale const char *desc; /* description of statistic */
624c87aefeSPatrick Mooney vmm_stat_func_t func;
63bf21cd93STycho Nightingale enum vmm_stat_scope scope;
64bf21cd93STycho Nightingale };
65bf21cd93STycho Nightingale
664c87aefeSPatrick Mooney void vmm_stat_register(void *arg);
67bf21cd93STycho Nightingale
684c87aefeSPatrick Mooney #define VMM_STAT_FDEFINE(type, nelems, desc, func, scope) \
69bf21cd93STycho Nightingale struct vmm_stat_type type[1] = { \
704c87aefeSPatrick Mooney { -1, nelems, desc, func, scope } \
71bf21cd93STycho Nightingale }; \
724c87aefeSPatrick Mooney SYSINIT(type##_stat, SI_SUB_KLD, SI_ORDER_ANY, vmm_stat_register, type)
734c87aefeSPatrick Mooney
7484971882SPatrick Mooney #define VMM_STAT_DEFINE(type, nelems, desc, scope) \
754c87aefeSPatrick Mooney VMM_STAT_FDEFINE(type, nelems, desc, NULL, scope)
76bf21cd93STycho Nightingale
77bf21cd93STycho Nightingale #define VMM_STAT_DECLARE(type) \
78bf21cd93STycho Nightingale extern struct vmm_stat_type type[1]
79bf21cd93STycho Nightingale
80bf21cd93STycho Nightingale #define VMM_STAT(type, desc) \
81bf21cd93STycho Nightingale VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_ANY)
82bf21cd93STycho Nightingale #define VMM_STAT_INTEL(type, desc) \
83bf21cd93STycho Nightingale VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_INTEL)
84bf21cd93STycho Nightingale #define VMM_STAT_AMD(type, desc) \
85bf21cd93STycho Nightingale VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_AMD)
86bf21cd93STycho Nightingale
874c87aefeSPatrick Mooney #define VMM_STAT_FUNC(type, desc, func) \
884c87aefeSPatrick Mooney VMM_STAT_FDEFINE(type, 1, desc, func, VMM_STAT_SCOPE_ANY)
894c87aefeSPatrick Mooney
90bf21cd93STycho Nightingale #define VMM_STAT_ARRAY(type, nelems, desc) \
91bf21cd93STycho Nightingale VMM_STAT_DEFINE(type, nelems, desc, VMM_STAT_SCOPE_ANY)
92bf21cd93STycho Nightingale
93bf21cd93STycho Nightingale void *vmm_stat_alloc(void);
944c87aefeSPatrick Mooney void vmm_stat_init(void *vp);
9584971882SPatrick Mooney void vmm_stat_free(void *vp);
96bf21cd93STycho Nightingale
97*d7b72f7bSAndy Fiddaman int vmm_stat_copy(struct vm *vm, int vcpu, int index, int count,
98*d7b72f7bSAndy Fiddaman int *num_stats, uint64_t *buf);
99bf21cd93STycho Nightingale int vmm_stat_desc_copy(int index, char *buf, int buflen);
100bf21cd93STycho Nightingale
1014c87aefeSPatrick Mooney static __inline void
vmm_stat_array_incr(struct vm * vm,int vcpu,struct vmm_stat_type * vst,int statidx,uint64_t x)102bf21cd93STycho Nightingale vmm_stat_array_incr(struct vm *vm, int vcpu, struct vmm_stat_type *vst,
1032699b94cSPatrick Mooney int statidx, uint64_t x)
104bf21cd93STycho Nightingale {
105bf21cd93STycho Nightingale #ifdef VMM_KEEP_STATS
106bf21cd93STycho Nightingale uint64_t *stats;
10784971882SPatrick Mooney
108bf21cd93STycho Nightingale stats = vcpu_stats(vm, vcpu);
109bf21cd93STycho Nightingale
110bf21cd93STycho Nightingale if (vst->index >= 0 && statidx < vst->nelems)
111bf21cd93STycho Nightingale stats[vst->index + statidx] += x;
112bf21cd93STycho Nightingale #endif
113bf21cd93STycho Nightingale }
114bf21cd93STycho Nightingale
1154c87aefeSPatrick Mooney static __inline void
vmm_stat_array_set(struct vm * vm,int vcpu,struct vmm_stat_type * vst,int statidx,uint64_t val)1164c87aefeSPatrick Mooney vmm_stat_array_set(struct vm *vm, int vcpu, struct vmm_stat_type *vst,
1172699b94cSPatrick Mooney int statidx, uint64_t val)
1184c87aefeSPatrick Mooney {
1194c87aefeSPatrick Mooney #ifdef VMM_KEEP_STATS
1204c87aefeSPatrick Mooney uint64_t *stats;
12184971882SPatrick Mooney
1224c87aefeSPatrick Mooney stats = vcpu_stats(vm, vcpu);
1234c87aefeSPatrick Mooney
1244c87aefeSPatrick Mooney if (vst->index >= 0 && statidx < vst->nelems)
1254c87aefeSPatrick Mooney stats[vst->index + statidx] = val;
1264c87aefeSPatrick Mooney #endif
1274c87aefeSPatrick Mooney }
1284c87aefeSPatrick Mooney
1294c87aefeSPatrick Mooney static __inline void
vmm_stat_incr(struct vm * vm,int vcpu,struct vmm_stat_type * vst,uint64_t x)130bf21cd93STycho Nightingale vmm_stat_incr(struct vm *vm, int vcpu, struct vmm_stat_type *vst, uint64_t x)
131bf21cd93STycho Nightingale {
132bf21cd93STycho Nightingale
133bf21cd93STycho Nightingale #ifdef VMM_KEEP_STATS
134bf21cd93STycho Nightingale vmm_stat_array_incr(vm, vcpu, vst, 0, x);
135bf21cd93STycho Nightingale #endif
136bf21cd93STycho Nightingale }
137bf21cd93STycho Nightingale
1384c87aefeSPatrick Mooney static __inline void
vmm_stat_set(struct vm * vm,int vcpu,struct vmm_stat_type * vst,uint64_t val)1394c87aefeSPatrick Mooney vmm_stat_set(struct vm *vm, int vcpu, struct vmm_stat_type *vst, uint64_t val)
1404c87aefeSPatrick Mooney {
1414c87aefeSPatrick Mooney
1424c87aefeSPatrick Mooney #ifdef VMM_KEEP_STATS
1434c87aefeSPatrick Mooney vmm_stat_array_set(vm, vcpu, vst, 0, val);
1444c87aefeSPatrick Mooney #endif
1454c87aefeSPatrick Mooney }
1464c87aefeSPatrick Mooney
147bf21cd93STycho Nightingale VMM_STAT_DECLARE(VCPU_MIGRATIONS);
148bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_COUNT);
149bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_EXTINT);
150bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_HLT);
151bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_CR_ACCESS);
152bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_RDMSR);
153bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_WRMSR);
154bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_MTRAP);
155bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_PAUSE);
156bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_INTR_WINDOW);
157bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_NMI_WINDOW);
158bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_INOUT);
159bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_CPUID);
160bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_NESTED_FAULT);
161e0c0d44eSPatrick Mooney VMM_STAT_DECLARE(VMEXIT_MMIO_EMUL);
162bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_UNKNOWN);
163bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_ASTPENDING);
164bf21cd93STycho Nightingale VMM_STAT_DECLARE(VMEXIT_EXCEPTION);
1654c87aefeSPatrick Mooney VMM_STAT_DECLARE(VMEXIT_REQIDLE);
1662606939dSPatrick Mooney VMM_STAT_DECLARE(VMEXIT_RUN_STATE);
167bf21cd93STycho Nightingale #endif
168