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 *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26221828Sgrehan * $FreeBSD: stable/11/sys/amd64/vmm/vmm_stat.c 348271 2019-05-25 11:27:56Z rgrimes $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#include <sys/cdefs.h>
30221828Sgrehan__FBSDID("$FreeBSD: stable/11/sys/amd64/vmm/vmm_stat.c 348271 2019-05-25 11:27:56Z rgrimes $");
31221828Sgrehan
32221828Sgrehan#include <sys/param.h>
33221828Sgrehan#include <sys/kernel.h>
34221828Sgrehan#include <sys/systm.h>
35221828Sgrehan#include <sys/malloc.h>
36221828Sgrehan
37221828Sgrehan#include <machine/vmm.h>
38248389Sneel#include "vmm_util.h"
39221828Sgrehan#include "vmm_stat.h"
40221828Sgrehan
41250427Sneel/*
42250427Sneel * 'vst_num_elems' is the total number of addressable statistic elements
43250427Sneel * 'vst_num_types' is the number of unique statistic types
44250427Sneel *
45250427Sneel * It is always true that 'vst_num_elems' is greater than or equal to
46250427Sneel * 'vst_num_types'. This is because a stat type may represent more than
47250427Sneel * one element (for e.g. VMM_STAT_ARRAY).
48250427Sneel */
49250427Sneelstatic int vst_num_elems, vst_num_types;
50250427Sneelstatic struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS];
51221828Sgrehan
52221828Sgrehanstatic MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
53221828Sgrehan
54267216Sneel#define	vst_size	((size_t)vst_num_elems * sizeof(uint64_t))
55267216Sneel
56221828Sgrehanvoid
57267216Sneelvmm_stat_register(void *arg)
58221828Sgrehan{
59221828Sgrehan	struct vmm_stat_type *vst = arg;
60221828Sgrehan
61221828Sgrehan	/* We require all stats to identify themselves with a description */
62221828Sgrehan	if (vst->desc == NULL)
63221828Sgrehan		return;
64221828Sgrehan
65248389Sneel	if (vst->scope == VMM_STAT_SCOPE_INTEL && !vmm_is_intel())
66248389Sneel		return;
67248389Sneel
68248389Sneel	if (vst->scope == VMM_STAT_SCOPE_AMD && !vmm_is_amd())
69248389Sneel		return;
70248389Sneel
71250427Sneel	if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) {
72299009Spfg		printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc);
73221828Sgrehan		return;
74221828Sgrehan	}
75221828Sgrehan
76250427Sneel	vst->index = vst_num_elems;
77250427Sneel	vst_num_elems += vst->nelems;
78250427Sneel
79250427Sneel	vsttab[vst_num_types++] = vst;
80221828Sgrehan}
81221828Sgrehan
82221828Sgrehanint
83221828Sgrehanvmm_stat_copy(struct vm *vm, int vcpu, int *num_stats, uint64_t *buf)
84221828Sgrehan{
85267884Sgrehan	struct vmm_stat_type *vst;
86267884Sgrehan	uint64_t *stats;
87221828Sgrehan	int i;
88221828Sgrehan
89348271Srgrimes	if (vcpu < 0 || vcpu >= vm_get_maxcpus(vm))
90221828Sgrehan		return (EINVAL);
91267884Sgrehan
92267884Sgrehan	/* Let stats functions update their counters */
93267884Sgrehan	for (i = 0; i < vst_num_types; i++) {
94267884Sgrehan		vst = vsttab[i];
95267884Sgrehan		if (vst->func != NULL)
96267884Sgrehan			(*vst->func)(vm, vcpu, vst);
97267884Sgrehan	}
98267884Sgrehan
99267884Sgrehan	/* Copy over the stats */
100221828Sgrehan	stats = vcpu_stats(vm, vcpu);
101250427Sneel	for (i = 0; i < vst_num_elems; i++)
102221828Sgrehan		buf[i] = stats[i];
103250427Sneel	*num_stats = vst_num_elems;
104221828Sgrehan	return (0);
105221828Sgrehan}
106221828Sgrehan
107221828Sgrehanvoid *
108221828Sgrehanvmm_stat_alloc(void)
109221828Sgrehan{
110221828Sgrehan
111267216Sneel	return (malloc(vst_size, M_VMM_STAT, M_WAITOK));
112221828Sgrehan}
113221828Sgrehan
114221828Sgrehanvoid
115267216Sneelvmm_stat_init(void *vp)
116267216Sneel{
117267216Sneel
118267216Sneel	bzero(vp, vst_size);
119267216Sneel}
120267216Sneel
121267216Sneelvoid
122221828Sgrehanvmm_stat_free(void *vp)
123221828Sgrehan{
124221828Sgrehan	free(vp, M_VMM_STAT);
125221828Sgrehan}
126221828Sgrehan
127250427Sneelint
128250427Sneelvmm_stat_desc_copy(int index, char *buf, int bufsize)
129221828Sgrehan{
130250427Sneel	int i;
131250427Sneel	struct vmm_stat_type *vst;
132221828Sgrehan
133250427Sneel	for (i = 0; i < vst_num_types; i++) {
134250427Sneel		vst = vsttab[i];
135250427Sneel		if (index >= vst->index && index < vst->index + vst->nelems) {
136250427Sneel			if (vst->nelems > 1) {
137250427Sneel				snprintf(buf, bufsize, "%s[%d]",
138250427Sneel					 vst->desc, index - vst->index);
139250427Sneel			} else {
140250427Sneel				strlcpy(buf, vst->desc, bufsize);
141250427Sneel			}
142250427Sneel			return (0);	/* found it */
143250427Sneel		}
144250427Sneel	}
145250427Sneel
146250427Sneel	return (EINVAL);
147221828Sgrehan}
148248389Sneel
149248389Sneel/* global statistics */
150248389SneelVMM_STAT(VCPU_MIGRATIONS, "vcpu migration across host cpus");
151248389SneelVMM_STAT(VMEXIT_COUNT, "total number of vm exits");
152248389SneelVMM_STAT(VMEXIT_EXTINT, "vm exits due to external interrupt");
153248389SneelVMM_STAT(VMEXIT_HLT, "number of times hlt was intercepted");
154248935SneelVMM_STAT(VMEXIT_CR_ACCESS, "number of times %cr access was intercepted");
155248935SneelVMM_STAT(VMEXIT_RDMSR, "number of times rdmsr was intercepted");
156248935SneelVMM_STAT(VMEXIT_WRMSR, "number of times wrmsr was intercepted");
157248935SneelVMM_STAT(VMEXIT_MTRAP, "number of monitor trap exits");
158248935SneelVMM_STAT(VMEXIT_PAUSE, "number of times pause was intercepted");
159248935SneelVMM_STAT(VMEXIT_INTR_WINDOW, "vm exits due to interrupt window opening");
160248935SneelVMM_STAT(VMEXIT_NMI_WINDOW, "vm exits due to nmi window opening");
161248935SneelVMM_STAT(VMEXIT_INOUT, "number of times in/out was intercepted");
162248935SneelVMM_STAT(VMEXIT_CPUID, "number of times cpuid was intercepted");
163261621SneelVMM_STAT(VMEXIT_NESTED_FAULT, "vm exits due to nested page fault");
164261621SneelVMM_STAT(VMEXIT_INST_EMUL, "vm exits for instruction emulation");
165248935SneelVMM_STAT(VMEXIT_UNKNOWN, "number of vm exits for unknown reason");
166248935SneelVMM_STAT(VMEXIT_ASTPENDING, "number of times astpending at exit");
167283657SneelVMM_STAT(VMEXIT_REQIDLE, "number of times idle requested at exit");
168248935SneelVMM_STAT(VMEXIT_USERSPACE, "number of vm exits handled in userspace");
169260619SneelVMM_STAT(VMEXIT_RENDEZVOUS, "number of times rendezvous pending at exit");
170261001SneelVMM_STAT(VMEXIT_EXCEPTION, "number of vm exits due to exceptions");
171