3
0
mirror of https://github.com/Qortal/Brooklyn.git synced 2025-01-31 07:12:18 +00:00
Brooklyn/tools/testing/selftests/kvm/kvm_binary_stats_test.c

255 lines
7.3 KiB
C
Raw Normal View History

2022-04-02 13:24:21 +00:00
// SPDX-License-Identifier: GPL-2.0-only
/*
* kvm_binary_stats_test
*
* Copyright (C) 2021, Google LLC.
*
* Test the fd-based interface for KVM statistics.
*/
#define _GNU_SOURCE /* for program_invocation_short_name */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "test_util.h"
#include "kvm_util.h"
#include "asm/kvm.h"
#include "linux/kvm.h"
static void stats_test(int stats_fd)
{
ssize_t ret;
int i;
size_t size_desc;
size_t size_data = 0;
2022-09-09 09:21:57 +00:00
struct kvm_stats_header header;
2022-04-02 13:24:21 +00:00
char *id;
struct kvm_stats_desc *stats_desc;
u64 *stats_data;
struct kvm_stats_desc *pdesc;
2022-09-09 09:21:57 +00:00
u32 type, unit, base;
2022-04-02 13:24:21 +00:00
/* Read kvm stats header */
2022-09-09 09:21:57 +00:00
read_stats_header(stats_fd, &header);
2022-04-02 13:24:21 +00:00
2022-09-09 09:21:57 +00:00
size_desc = get_stats_descriptor_size(&header);
2022-04-02 13:24:21 +00:00
/* Read kvm stats id string */
2022-09-09 09:21:57 +00:00
id = malloc(header.name_size);
2022-04-02 13:24:21 +00:00
TEST_ASSERT(id, "Allocate memory for id string");
2022-09-09 09:21:57 +00:00
ret = read(stats_fd, id, header.name_size);
TEST_ASSERT(ret == header.name_size, "Read id string");
2022-04-02 13:24:21 +00:00
/* Check id string, that should start with "kvm" */
2022-09-09 09:21:57 +00:00
TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,
"Invalid KVM stats type, id: %s", id);
2022-04-02 13:24:21 +00:00
/* Sanity check for other fields in header */
2022-09-09 09:21:57 +00:00
if (header.num_desc == 0) {
2022-04-02 13:24:21 +00:00
printf("No KVM stats defined!");
return;
}
2022-09-09 09:21:57 +00:00
/*
* The descriptor and data offsets must be valid, they must not overlap
* the header, and the descriptor and data blocks must not overlap each
* other. Note, the data block is rechecked after its size is known.
*/
TEST_ASSERT(header.desc_offset && header.desc_offset >= sizeof(header) &&
header.data_offset && header.data_offset >= sizeof(header),
"Invalid offset fields in header");
TEST_ASSERT(header.desc_offset > header.data_offset ||
(header.desc_offset + size_desc * header.num_desc <= header.data_offset),
"Descriptor block is overlapped with data block");
2022-04-02 13:24:21 +00:00
/* Read kvm stats descriptors */
2022-09-09 09:21:57 +00:00
stats_desc = read_stats_descriptors(stats_fd, &header);
2022-04-02 13:24:21 +00:00
/* Sanity check for fields in descriptors */
2022-09-09 09:21:57 +00:00
for (i = 0; i < header.num_desc; ++i) {
pdesc = get_stats_descriptor(stats_desc, i, &header);
type = pdesc->flags & KVM_STATS_TYPE_MASK;
unit = pdesc->flags & KVM_STATS_UNIT_MASK;
base = pdesc->flags & KVM_STATS_BASE_MASK;
/* Check name string */
TEST_ASSERT(strlen(pdesc->name) < header.name_size,
"KVM stats name (index: %d) too long", i);
2022-04-02 13:24:21 +00:00
/* Check type,unit,base boundaries */
2022-09-09 09:21:57 +00:00
TEST_ASSERT(type <= KVM_STATS_TYPE_MAX,
"Unknown KVM stats (%s) type: %u", pdesc->name, type);
TEST_ASSERT(unit <= KVM_STATS_UNIT_MAX,
"Unknown KVM stats (%s) unit: %u", pdesc->name, unit);
TEST_ASSERT(base <= KVM_STATS_BASE_MAX,
"Unknown KVM stats (%s) base: %u", pdesc->name, base);
/*
* Check exponent for stats unit
2022-04-02 13:24:21 +00:00
* Exponent for counter should be greater than or equal to 0
* Exponent for unit bytes should be greater than or equal to 0
* Exponent for unit seconds should be less than or equal to 0
* Exponent for unit clock cycles should be greater than or
* equal to 0
2022-09-09 09:21:57 +00:00
* Exponent for unit boolean should be 0
2022-04-02 13:24:21 +00:00
*/
switch (pdesc->flags & KVM_STATS_UNIT_MASK) {
case KVM_STATS_UNIT_NONE:
case KVM_STATS_UNIT_BYTES:
case KVM_STATS_UNIT_CYCLES:
TEST_ASSERT(pdesc->exponent >= 0,
2022-09-09 09:21:57 +00:00
"Unsupported KVM stats (%s) exponent: %i",
pdesc->name, pdesc->exponent);
2022-04-02 13:24:21 +00:00
break;
case KVM_STATS_UNIT_SECONDS:
TEST_ASSERT(pdesc->exponent <= 0,
2022-09-09 09:21:57 +00:00
"Unsupported KVM stats (%s) exponent: %i",
pdesc->name, pdesc->exponent);
break;
case KVM_STATS_UNIT_BOOLEAN:
TEST_ASSERT(pdesc->exponent == 0,
"Unsupported KVM stats (%s) exponent: %d",
pdesc->name, pdesc->exponent);
2022-04-02 13:24:21 +00:00
break;
}
2022-09-09 09:21:57 +00:00
2022-04-02 13:24:21 +00:00
/* Check size field, which should not be zero */
2022-09-09 09:21:57 +00:00
TEST_ASSERT(pdesc->size,
"KVM descriptor(%s) with size of 0", pdesc->name);
2022-04-02 13:24:21 +00:00
/* Check bucket_size field */
switch (pdesc->flags & KVM_STATS_TYPE_MASK) {
case KVM_STATS_TYPE_LINEAR_HIST:
TEST_ASSERT(pdesc->bucket_size,
2022-09-09 09:21:57 +00:00
"Bucket size of Linear Histogram stats (%s) is zero",
pdesc->name);
2022-04-02 13:24:21 +00:00
break;
default:
TEST_ASSERT(!pdesc->bucket_size,
2022-09-09 09:21:57 +00:00
"Bucket size of stats (%s) is not zero",
pdesc->name);
2022-04-02 13:24:21 +00:00
}
size_data += pdesc->size * sizeof(*stats_data);
}
2022-09-09 09:21:57 +00:00
/*
* Now that the size of the data block is known, verify the data block
* doesn't overlap the descriptor block.
*/
TEST_ASSERT(header.data_offset >= header.desc_offset ||
header.data_offset + size_data <= header.desc_offset,
"Data block is overlapped with Descriptor block");
2022-04-02 13:24:21 +00:00
/* Check validity of all stats data size */
2022-09-09 09:21:57 +00:00
TEST_ASSERT(size_data >= header.num_desc * sizeof(*stats_data),
"Data size is not correct");
2022-04-02 13:24:21 +00:00
/* Check stats offset */
2022-09-09 09:21:57 +00:00
for (i = 0; i < header.num_desc; ++i) {
pdesc = get_stats_descriptor(stats_desc, i, &header);
2022-04-02 13:24:21 +00:00
TEST_ASSERT(pdesc->offset < size_data,
2022-09-09 09:21:57 +00:00
"Invalid offset (%u) for stats: %s",
pdesc->offset, pdesc->name);
2022-04-02 13:24:21 +00:00
}
/* Allocate memory for stats data */
stats_data = malloc(size_data);
TEST_ASSERT(stats_data, "Allocate memory for stats data");
/* Read kvm stats data as a bulk */
2022-09-09 09:21:57 +00:00
ret = pread(stats_fd, stats_data, size_data, header.data_offset);
2022-04-02 13:24:21 +00:00
TEST_ASSERT(ret == size_data, "Read KVM stats data");
/* Read kvm stats data one by one */
2022-09-09 09:21:57 +00:00
for (i = 0; i < header.num_desc; ++i) {
pdesc = get_stats_descriptor(stats_desc, i, &header);
read_stat_data(stats_fd, &header, pdesc, stats_data,
pdesc->size);
2022-04-02 13:24:21 +00:00
}
free(stats_data);
free(stats_desc);
free(id);
}
static void vm_stats_test(struct kvm_vm *vm)
{
2022-09-09 09:21:57 +00:00
int stats_fd = vm_get_stats_fd(vm);
2022-04-02 13:24:21 +00:00
stats_test(stats_fd);
close(stats_fd);
TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
}
2022-09-09 09:21:57 +00:00
static void vcpu_stats_test(struct kvm_vcpu *vcpu)
2022-04-02 13:24:21 +00:00
{
2022-09-09 09:21:57 +00:00
int stats_fd = vcpu_get_stats_fd(vcpu);
2022-04-02 13:24:21 +00:00
stats_test(stats_fd);
close(stats_fd);
TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
}
#define DEFAULT_NUM_VM 4
#define DEFAULT_NUM_VCPU 4
/*
* Usage: kvm_bin_form_stats [#vm] [#vcpu]
* The first parameter #vm set the number of VMs being created.
* The second parameter #vcpu set the number of VCPUs being created.
* By default, DEFAULT_NUM_VM VM and DEFAULT_NUM_VCPU VCPU for the VM would be
* created for testing.
*/
int main(int argc, char *argv[])
{
int i, j;
2022-09-09 09:21:57 +00:00
struct kvm_vcpu **vcpus;
2022-04-02 13:24:21 +00:00
struct kvm_vm **vms;
int max_vm = DEFAULT_NUM_VM;
int max_vcpu = DEFAULT_NUM_VCPU;
/* Get the number of VMs and VCPUs that would be created for testing. */
if (argc > 1) {
max_vm = strtol(argv[1], NULL, 0);
if (max_vm <= 0)
max_vm = DEFAULT_NUM_VM;
}
if (argc > 2) {
max_vcpu = strtol(argv[2], NULL, 0);
if (max_vcpu <= 0)
max_vcpu = DEFAULT_NUM_VCPU;
}
/* Check the extension for binary stats */
2022-09-09 09:21:57 +00:00
TEST_REQUIRE(kvm_has_cap(KVM_CAP_BINARY_STATS_FD));
2022-04-02 13:24:21 +00:00
/* Create VMs and VCPUs */
vms = malloc(sizeof(vms[0]) * max_vm);
TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
2022-09-09 09:21:57 +00:00
vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
2022-04-02 13:24:21 +00:00
for (i = 0; i < max_vm; ++i) {
2022-09-09 09:21:57 +00:00
vms[i] = vm_create_barebones();
2022-04-02 13:24:21 +00:00
for (j = 0; j < max_vcpu; ++j)
2022-09-09 09:21:57 +00:00
vcpus[i * max_vcpu + j] = __vm_vcpu_add(vms[i], j);
2022-04-02 13:24:21 +00:00
}
/* Check stats read for every VM and VCPU */
for (i = 0; i < max_vm; ++i) {
vm_stats_test(vms[i]);
for (j = 0; j < max_vcpu; ++j)
2022-09-09 09:21:57 +00:00
vcpu_stats_test(vcpus[i * max_vcpu + j]);
2022-04-02 13:24:21 +00:00
}
for (i = 0; i < max_vm; ++i)
kvm_vm_free(vms[i]);
free(vms);
return 0;
}