vmm_stat.c revision 270074
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/10/sys/amd64/vmm/vmm_stat.c 270074 2014-08-17 01:23:52Z grehan $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#include <sys/cdefs.h> 30221828Sgrehan__FBSDID("$FreeBSD: stable/10/sys/amd64/vmm/vmm_stat.c 270074 2014-08-17 01:23:52Z grehan $"); 31221828Sgrehan 32221828Sgrehan#include <sys/param.h> 33221828Sgrehan#include <sys/kernel.h> 34221828Sgrehan#include <sys/systm.h> 35221828Sgrehan#include <sys/malloc.h> 36223621Sgrehan#include <sys/smp.h> 37221828Sgrehan 38221828Sgrehan#include <machine/vmm.h> 39248389Sneel#include "vmm_util.h" 40221828Sgrehan#include "vmm_stat.h" 41221828Sgrehan 42250427Sneel/* 43250427Sneel * 'vst_num_elems' is the total number of addressable statistic elements 44250427Sneel * 'vst_num_types' is the number of unique statistic types 45250427Sneel * 46250427Sneel * It is always true that 'vst_num_elems' is greater than or equal to 47250427Sneel * 'vst_num_types'. This is because a stat type may represent more than 48250427Sneel * one element (for e.g. VMM_STAT_ARRAY). 49250427Sneel */ 50250427Sneelstatic int vst_num_elems, vst_num_types; 51250427Sneelstatic struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS]; 52221828Sgrehan 53221828Sgrehanstatic MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat"); 54221828Sgrehan 55270071Sgrehan#define vst_size ((size_t)vst_num_elems * sizeof(uint64_t)) 56270071Sgrehan 57221828Sgrehanvoid 58270071Sgrehanvmm_stat_register(void *arg) 59221828Sgrehan{ 60221828Sgrehan struct vmm_stat_type *vst = arg; 61221828Sgrehan 62221828Sgrehan /* We require all stats to identify themselves with a description */ 63221828Sgrehan if (vst->desc == NULL) 64221828Sgrehan return; 65221828Sgrehan 66248389Sneel if (vst->scope == VMM_STAT_SCOPE_INTEL && !vmm_is_intel()) 67248389Sneel return; 68248389Sneel 69248389Sneel if (vst->scope == VMM_STAT_SCOPE_AMD && !vmm_is_amd()) 70248389Sneel return; 71248389Sneel 72250427Sneel if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) { 73221828Sgrehan printf("Cannot accomodate vmm stat type \"%s\"!\n", vst->desc); 74221828Sgrehan return; 75221828Sgrehan } 76221828Sgrehan 77250427Sneel vst->index = vst_num_elems; 78250427Sneel vst_num_elems += vst->nelems; 79250427Sneel 80250427Sneel vsttab[vst_num_types++] = vst; 81221828Sgrehan} 82221828Sgrehan 83221828Sgrehanint 84221828Sgrehanvmm_stat_copy(struct vm *vm, int vcpu, int *num_stats, uint64_t *buf) 85221828Sgrehan{ 86270074Sgrehan struct vmm_stat_type *vst; 87270074Sgrehan uint64_t *stats; 88221828Sgrehan int i; 89221828Sgrehan 90221828Sgrehan if (vcpu < 0 || vcpu >= VM_MAXCPU) 91221828Sgrehan return (EINVAL); 92270074Sgrehan 93270074Sgrehan /* Let stats functions update their counters */ 94270074Sgrehan for (i = 0; i < vst_num_types; i++) { 95270074Sgrehan vst = vsttab[i]; 96270074Sgrehan if (vst->func != NULL) 97270074Sgrehan (*vst->func)(vm, vcpu, vst); 98270074Sgrehan } 99270074Sgrehan 100270074Sgrehan /* Copy over the stats */ 101221828Sgrehan stats = vcpu_stats(vm, vcpu); 102250427Sneel for (i = 0; i < vst_num_elems; i++) 103221828Sgrehan buf[i] = stats[i]; 104250427Sneel *num_stats = vst_num_elems; 105221828Sgrehan return (0); 106221828Sgrehan} 107221828Sgrehan 108221828Sgrehanvoid * 109221828Sgrehanvmm_stat_alloc(void) 110221828Sgrehan{ 111221828Sgrehan 112270071Sgrehan return (malloc(vst_size, M_VMM_STAT, M_WAITOK)); 113221828Sgrehan} 114221828Sgrehan 115221828Sgrehanvoid 116270071Sgrehanvmm_stat_init(void *vp) 117270071Sgrehan{ 118270071Sgrehan 119270071Sgrehan bzero(vp, vst_size); 120270071Sgrehan} 121270071Sgrehan 122270071Sgrehanvoid 123221828Sgrehanvmm_stat_free(void *vp) 124221828Sgrehan{ 125221828Sgrehan free(vp, M_VMM_STAT); 126221828Sgrehan} 127221828Sgrehan 128250427Sneelint 129250427Sneelvmm_stat_desc_copy(int index, char *buf, int bufsize) 130221828Sgrehan{ 131250427Sneel int i; 132250427Sneel struct vmm_stat_type *vst; 133221828Sgrehan 134250427Sneel for (i = 0; i < vst_num_types; i++) { 135250427Sneel vst = vsttab[i]; 136250427Sneel if (index >= vst->index && index < vst->index + vst->nelems) { 137250427Sneel if (vst->nelems > 1) { 138250427Sneel snprintf(buf, bufsize, "%s[%d]", 139250427Sneel vst->desc, index - vst->index); 140250427Sneel } else { 141250427Sneel strlcpy(buf, vst->desc, bufsize); 142250427Sneel } 143250427Sneel return (0); /* found it */ 144250427Sneel } 145250427Sneel } 146250427Sneel 147250427Sneel return (EINVAL); 148221828Sgrehan} 149248389Sneel 150248389Sneel/* global statistics */ 151248389SneelVMM_STAT(VCPU_MIGRATIONS, "vcpu migration across host cpus"); 152248389SneelVMM_STAT(VMEXIT_COUNT, "total number of vm exits"); 153248389SneelVMM_STAT(VMEXIT_EXTINT, "vm exits due to external interrupt"); 154248389SneelVMM_STAT(VMEXIT_HLT, "number of times hlt was intercepted"); 155248935SneelVMM_STAT(VMEXIT_CR_ACCESS, "number of times %cr access was intercepted"); 156248935SneelVMM_STAT(VMEXIT_RDMSR, "number of times rdmsr was intercepted"); 157248935SneelVMM_STAT(VMEXIT_WRMSR, "number of times wrmsr was intercepted"); 158248935SneelVMM_STAT(VMEXIT_MTRAP, "number of monitor trap exits"); 159248935SneelVMM_STAT(VMEXIT_PAUSE, "number of times pause was intercepted"); 160248935SneelVMM_STAT(VMEXIT_INTR_WINDOW, "vm exits due to interrupt window opening"); 161248935SneelVMM_STAT(VMEXIT_NMI_WINDOW, "vm exits due to nmi window opening"); 162248935SneelVMM_STAT(VMEXIT_INOUT, "number of times in/out was intercepted"); 163248935SneelVMM_STAT(VMEXIT_CPUID, "number of times cpuid was intercepted"); 164266339SjhbVMM_STAT(VMEXIT_NESTED_FAULT, "vm exits due to nested page fault"); 165266339SjhbVMM_STAT(VMEXIT_INST_EMUL, "vm exits for instruction emulation"); 166248935SneelVMM_STAT(VMEXIT_UNKNOWN, "number of vm exits for unknown reason"); 167248935SneelVMM_STAT(VMEXIT_ASTPENDING, "number of times astpending at exit"); 168248935SneelVMM_STAT(VMEXIT_USERSPACE, "number of vm exits handled in userspace"); 169266339SjhbVMM_STAT(VMEXIT_RENDEZVOUS, "number of times rendezvous pending at exit"); 170266593SjhbVMM_STAT(VMEXIT_EXCEPTION, "number of vm exits due to exceptions"); 171