1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan * 4. Neither the name of the University nor the names of its contributors
14221828Sgrehan *    may be used to endorse or promote products derived from this software
15221828Sgrehan *    without specific prior written permission.
16221828Sgrehan *
17221828Sgrehan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27221828Sgrehan * SUCH DAMAGE.
28221828Sgrehan *
29221828Sgrehan * $FreeBSD$
30221828Sgrehan */
31221828Sgrehan
32221828Sgrehan#ifndef _VMM_STAT_H_
33221828Sgrehan#define	_VMM_STAT_H_
34221828Sgrehan
35221828Sgrehanstruct vm;
36221828Sgrehan
37250427Sneel#define	MAX_VMM_STAT_ELEMS	64		/* arbitrary */
38221828Sgrehan
39248389Sneelenum vmm_stat_scope {
40248389Sneel	VMM_STAT_SCOPE_ANY,
41248389Sneel	VMM_STAT_SCOPE_INTEL,		/* Intel VMX specific statistic */
42248389Sneel	VMM_STAT_SCOPE_AMD,		/* AMD SVM specific statistic */
43248389Sneel};
44248389Sneel
45270074Sgrehanstruct vmm_stat_type;
46270074Sgrehantypedef void (*vmm_stat_func_t)(struct vm *vm, int vcpu,
47270074Sgrehan    struct vmm_stat_type *stat);
48270074Sgrehan
49221828Sgrehanstruct vmm_stat_type {
50221828Sgrehan	int	index;			/* position in the stats buffer */
51250427Sneel	int	nelems;			/* standalone or array */
52248389Sneel	const char *desc;		/* description of statistic */
53270074Sgrehan	vmm_stat_func_t func;
54248389Sneel	enum vmm_stat_scope scope;
55221828Sgrehan};
56221828Sgrehan
57270071Sgrehanvoid	vmm_stat_register(void *arg);
58221828Sgrehan
59270074Sgrehan#define	VMM_STAT_FDEFINE(type, nelems, desc, func, scope)		\
60221828Sgrehan	struct vmm_stat_type type[1] = {				\
61270074Sgrehan		{ -1, nelems, desc, func, scope }			\
62221828Sgrehan	};								\
63270071Sgrehan	SYSINIT(type##_stat, SI_SUB_KLD, SI_ORDER_ANY, vmm_stat_register, type)
64221828Sgrehan
65270074Sgrehan#define VMM_STAT_DEFINE(type, nelems, desc, scope) 			\
66270074Sgrehan	VMM_STAT_FDEFINE(type, nelems, desc, NULL, scope)
67270074Sgrehan
68248389Sneel#define	VMM_STAT_DECLARE(type)						\
69248389Sneel	extern struct vmm_stat_type type[1]
70248389Sneel
71248389Sneel#define	VMM_STAT(type, desc)		\
72250427Sneel	VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_ANY)
73248389Sneel#define	VMM_STAT_INTEL(type, desc)	\
74250427Sneel	VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_INTEL)
75248389Sneel#define	VMM_STAT_AMD(type, desc)	\
76250427Sneel	VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_AMD)
77248389Sneel
78270074Sgrehan#define	VMM_STAT_FUNC(type, desc, func)	\
79270074Sgrehan	VMM_STAT_FDEFINE(type, 1, desc, func, VMM_STAT_SCOPE_ANY)
80270074Sgrehan
81250427Sneel#define	VMM_STAT_ARRAY(type, nelems, desc)	\
82250427Sneel	VMM_STAT_DEFINE(type, nelems, desc, VMM_STAT_SCOPE_ANY)
83250427Sneel
84221828Sgrehanvoid	*vmm_stat_alloc(void);
85270071Sgrehanvoid	vmm_stat_init(void *vp);
86221828Sgrehanvoid 	vmm_stat_free(void *vp);
87221828Sgrehan
88221828Sgrehan/*
89221828Sgrehan * 'buf' should be at least fit 'MAX_VMM_STAT_TYPES' entries
90221828Sgrehan */
91221828Sgrehanint	vmm_stat_copy(struct vm *vm, int vcpu, int *num_stats, uint64_t *buf);
92250427Sneelint	vmm_stat_desc_copy(int index, char *buf, int buflen);
93221828Sgrehan
94221828Sgrehanstatic void __inline
95250427Sneelvmm_stat_array_incr(struct vm *vm, int vcpu, struct vmm_stat_type *vst,
96250427Sneel		    int statidx, uint64_t x)
97250427Sneel{
98250427Sneel#ifdef VMM_KEEP_STATS
99250427Sneel	uint64_t *stats;
100250427Sneel
101250427Sneel	stats = vcpu_stats(vm, vcpu);
102250427Sneel
103250427Sneel	if (vst->index >= 0 && statidx < vst->nelems)
104250427Sneel		stats[vst->index + statidx] += x;
105250427Sneel#endif
106250427Sneel}
107250427Sneel
108250427Sneelstatic void __inline
109270074Sgrehanvmm_stat_array_set(struct vm *vm, int vcpu, struct vmm_stat_type *vst,
110270074Sgrehan		   int statidx, uint64_t val)
111270074Sgrehan{
112270074Sgrehan#ifdef VMM_KEEP_STATS
113270074Sgrehan	uint64_t *stats;
114270074Sgrehan
115270074Sgrehan	stats = vcpu_stats(vm, vcpu);
116270074Sgrehan
117270074Sgrehan	if (vst->index >= 0 && statidx < vst->nelems)
118270074Sgrehan		stats[vst->index + statidx] = val;
119270074Sgrehan#endif
120270074Sgrehan}
121270074Sgrehan
122270074Sgrehanstatic void __inline
123221828Sgrehanvmm_stat_incr(struct vm *vm, int vcpu, struct vmm_stat_type *vst, uint64_t x)
124221828Sgrehan{
125250427Sneel
126250427Sneel#ifdef VMM_KEEP_STATS
127250427Sneel	vmm_stat_array_incr(vm, vcpu, vst, 0, x);
128221828Sgrehan#endif
129221828Sgrehan}
130221828Sgrehan
131270074Sgrehanstatic void __inline
132270074Sgrehanvmm_stat_set(struct vm *vm, int vcpu, struct vmm_stat_type *vst, uint64_t val)
133270074Sgrehan{
134270074Sgrehan
135270074Sgrehan#ifdef VMM_KEEP_STATS
136270074Sgrehan	vmm_stat_array_set(vm, vcpu, vst, 0, val);
137270074Sgrehan#endif
138270074Sgrehan}
139270074Sgrehan
140248389SneelVMM_STAT_DECLARE(VCPU_MIGRATIONS);
141248389SneelVMM_STAT_DECLARE(VMEXIT_COUNT);
142248389SneelVMM_STAT_DECLARE(VMEXIT_EXTINT);
143248389SneelVMM_STAT_DECLARE(VMEXIT_HLT);
144248935SneelVMM_STAT_DECLARE(VMEXIT_CR_ACCESS);
145248935SneelVMM_STAT_DECLARE(VMEXIT_RDMSR);
146248935SneelVMM_STAT_DECLARE(VMEXIT_WRMSR);
147248935SneelVMM_STAT_DECLARE(VMEXIT_MTRAP);
148248935SneelVMM_STAT_DECLARE(VMEXIT_PAUSE);
149248935SneelVMM_STAT_DECLARE(VMEXIT_INTR_WINDOW);
150248935SneelVMM_STAT_DECLARE(VMEXIT_NMI_WINDOW);
151248935SneelVMM_STAT_DECLARE(VMEXIT_INOUT);
152248935SneelVMM_STAT_DECLARE(VMEXIT_CPUID);
153266339SjhbVMM_STAT_DECLARE(VMEXIT_NESTED_FAULT);
154266339SjhbVMM_STAT_DECLARE(VMEXIT_INST_EMUL);
155248935SneelVMM_STAT_DECLARE(VMEXIT_UNKNOWN);
156248935SneelVMM_STAT_DECLARE(VMEXIT_ASTPENDING);
157248935SneelVMM_STAT_DECLARE(VMEXIT_USERSPACE);
158266339SjhbVMM_STAT_DECLARE(VMEXIT_RENDEZVOUS);
159266593SjhbVMM_STAT_DECLARE(VMEXIT_EXCEPTION);
160284900SneelVMM_STAT_DECLARE(VMEXIT_REQIDLE);
161221828Sgrehan#endif
162