vmm_stat.c revision 223621
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/smp.h>
37
38#include <machine/vmm.h>
39#include "vmm_stat.h"
40
41static int vstnum;
42static struct vmm_stat_type *vsttab[MAX_VMM_STAT_TYPES];
43
44static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
45
46void
47vmm_stat_init(void *arg)
48{
49	struct vmm_stat_type *vst = arg;
50
51	/* We require all stats to identify themselves with a description */
52	if (vst->desc == NULL)
53		return;
54
55	if (vstnum >= MAX_VMM_STAT_TYPES) {
56		printf("Cannot accomodate vmm stat type \"%s\"!\n", vst->desc);
57		return;
58	}
59
60	vst->index = vstnum;
61	vsttab[vstnum++] = vst;
62}
63
64int
65vmm_stat_copy(struct vm *vm, int vcpu, int *num_stats, uint64_t *buf)
66{
67	int i;
68	uint64_t *stats;
69
70	if (vcpu < 0 || vcpu >= VM_MAXCPU)
71		return (EINVAL);
72
73	stats = vcpu_stats(vm, vcpu);
74	for (i = 0; i < vstnum; i++)
75		buf[i] = stats[i];
76	*num_stats = vstnum;
77	return (0);
78}
79
80void *
81vmm_stat_alloc(void)
82{
83	u_long size;
84
85	size = vstnum * sizeof(uint64_t);
86
87	return (malloc(size, M_VMM_STAT, M_ZERO | M_WAITOK));
88}
89
90void
91vmm_stat_free(void *vp)
92{
93	free(vp, M_VMM_STAT);
94}
95
96const char *
97vmm_stat_desc(int index)
98{
99
100	if (index >= 0 && index < vstnum)
101		return (vsttab[index]->desc);
102	else
103		return (NULL);
104}
105