#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <machine/vmm.h>
#include <dev/vmm/vmm_stat.h>
static int vst_num_elems, vst_num_types;
static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS];
static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat");
#define vst_size ((size_t)vst_num_elems * sizeof(uint64_t))
void
vmm_stat_register(void *arg)
{
struct vmm_stat_type *vst = arg;
if (vst->desc == NULL)
return;
if (vst->pred != NULL && !vst->pred())
return;
if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) {
printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc);
return;
}
vst->index = vst_num_elems;
vst_num_elems += vst->nelems;
vsttab[vst_num_types++] = vst;
}
int
vmm_stat_copy(struct vcpu *vcpu, int index, int count, int *num_stats,
uint64_t *buf)
{
struct vmm_stat_type *vst;
uint64_t *stats;
int i, tocopy;
if (index < 0 || count < 0)
return (EINVAL);
if (index > vst_num_elems)
return (ENOENT);
if (index == vst_num_elems) {
*num_stats = 0;
return (0);
}
tocopy = min(vst_num_elems - index, count);
for (i = 0; i < vst_num_types; i++) {
vst = vsttab[i];
if (vst->func != NULL)
(*vst->func)(vcpu, vst);
}
stats = vcpu_stats(vcpu);
memcpy(buf, stats + index, tocopy * sizeof(stats[0]));
*num_stats = tocopy;
return (0);
}
void *
vmm_stat_alloc(void)
{
return (malloc(vst_size, M_VMM_STAT, M_WAITOK));
}
void
vmm_stat_init(void *vp)
{
bzero(vp, vst_size);
}
void
vmm_stat_free(void *vp)
{
free(vp, M_VMM_STAT);
}
int
vmm_stat_desc_copy(int index, char *buf, int bufsize)
{
int i;
struct vmm_stat_type *vst;
for (i = 0; i < vst_num_types; i++) {
vst = vsttab[i];
if (index >= vst->index && index < vst->index + vst->nelems) {
if (vst->nelems > 1) {
snprintf(buf, bufsize, "%s[%d]",
vst->desc, index - vst->index);
} else {
strlcpy(buf, vst->desc, bufsize);
}
return (0);
}
}
return (EINVAL);
}