Path: blob/master/tools/testing/selftests/kvm/kvm_binary_stats_test.c
38189 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* kvm_binary_stats_test3*4* Copyright (C) 2021, Google LLC.5*6* Test the fd-based interface for KVM statistics.7*/8#include <fcntl.h>9#include <stdio.h>10#include <stdlib.h>11#include <string.h>12#include <errno.h>1314#include "test_util.h"1516#include "kvm_util.h"17#include "asm/kvm.h"18#include "linux/kvm.h"19#include "kselftest.h"2021static void stats_test(int stats_fd)22{23ssize_t ret;24int i;25size_t size_desc;26size_t size_data = 0;27struct kvm_stats_header header;28char *id;29struct kvm_stats_desc *stats_desc;30u64 *stats_data;31struct kvm_stats_desc *pdesc;32u32 type, unit, base;3334/* Read kvm stats header */35read_stats_header(stats_fd, &header);3637size_desc = get_stats_descriptor_size(&header);3839/* Read kvm stats id string */40id = malloc(header.name_size);41TEST_ASSERT(id, "Allocate memory for id string");4243ret = pread(stats_fd, id, header.name_size, sizeof(header));44TEST_ASSERT(ret == header.name_size,45"Expected header size '%u', read '%lu' bytes",46header.name_size, ret);4748/* Check id string, that should start with "kvm" */49TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,50"Invalid KVM stats type, id: %s", id);5152/* Sanity check for other fields in header */53if (header.num_desc == 0) {54ksft_print_msg("No KVM stats defined!\n");55return;56}57/*58* The descriptor and data offsets must be valid, they must not overlap59* the header, and the descriptor and data blocks must not overlap each60* other. Note, the data block is rechecked after its size is known.61*/62TEST_ASSERT(header.desc_offset && header.desc_offset >= sizeof(header) &&63header.data_offset && header.data_offset >= sizeof(header),64"Invalid offset fields in header");6566TEST_ASSERT(header.desc_offset > header.data_offset ||67(header.desc_offset + size_desc * header.num_desc <= header.data_offset),68"Descriptor block is overlapped with data block");6970/* Read kvm stats descriptors */71stats_desc = read_stats_descriptors(stats_fd, &header);7273/* Sanity check for fields in descriptors */74for (i = 0; i < header.num_desc; ++i) {75pdesc = get_stats_descriptor(stats_desc, i, &header);76type = pdesc->flags & KVM_STATS_TYPE_MASK;77unit = pdesc->flags & KVM_STATS_UNIT_MASK;78base = pdesc->flags & KVM_STATS_BASE_MASK;7980/* Check name string */81TEST_ASSERT(strlen(pdesc->name) < header.name_size,82"KVM stats name (index: %d) too long", i);8384/* Check type,unit,base boundaries */85TEST_ASSERT(type <= KVM_STATS_TYPE_MAX,86"Unknown KVM stats (%s) type: %u", pdesc->name, type);87TEST_ASSERT(unit <= KVM_STATS_UNIT_MAX,88"Unknown KVM stats (%s) unit: %u", pdesc->name, unit);89TEST_ASSERT(base <= KVM_STATS_BASE_MAX,90"Unknown KVM stats (%s) base: %u", pdesc->name, base);9192/*93* Check exponent for stats unit94* Exponent for counter should be greater than or equal to 095* Exponent for unit bytes should be greater than or equal to 096* Exponent for unit seconds should be less than or equal to 097* Exponent for unit clock cycles should be greater than or98* equal to 099* Exponent for unit boolean should be 0100*/101switch (pdesc->flags & KVM_STATS_UNIT_MASK) {102case KVM_STATS_UNIT_NONE:103case KVM_STATS_UNIT_BYTES:104case KVM_STATS_UNIT_CYCLES:105TEST_ASSERT(pdesc->exponent >= 0,106"Unsupported KVM stats (%s) exponent: %i",107pdesc->name, pdesc->exponent);108break;109case KVM_STATS_UNIT_SECONDS:110TEST_ASSERT(pdesc->exponent <= 0,111"Unsupported KVM stats (%s) exponent: %i",112pdesc->name, pdesc->exponent);113break;114case KVM_STATS_UNIT_BOOLEAN:115TEST_ASSERT(pdesc->exponent == 0,116"Unsupported KVM stats (%s) exponent: %d",117pdesc->name, pdesc->exponent);118break;119}120121/* Check size field, which should not be zero */122TEST_ASSERT(pdesc->size,123"KVM descriptor(%s) with size of 0", pdesc->name);124/* Check bucket_size field */125switch (pdesc->flags & KVM_STATS_TYPE_MASK) {126case KVM_STATS_TYPE_LINEAR_HIST:127TEST_ASSERT(pdesc->bucket_size,128"Bucket size of Linear Histogram stats (%s) is zero",129pdesc->name);130break;131default:132TEST_ASSERT(!pdesc->bucket_size,133"Bucket size of stats (%s) is not zero",134pdesc->name);135}136size_data = max(size_data, pdesc->offset + pdesc->size * sizeof(*stats_data));137}138139/*140* Now that the size of the data block is known, verify the data block141* doesn't overlap the descriptor block.142*/143TEST_ASSERT(header.data_offset >= header.desc_offset ||144header.data_offset + size_data <= header.desc_offset,145"Data block is overlapped with Descriptor block");146147/* Check validity of all stats data size */148TEST_ASSERT(size_data >= header.num_desc * sizeof(*stats_data),149"Data size is not correct");150151/* Allocate memory for stats data */152stats_data = malloc(size_data);153TEST_ASSERT(stats_data, "Allocate memory for stats data");154/* Read kvm stats data as a bulk */155ret = pread(stats_fd, stats_data, size_data, header.data_offset);156TEST_ASSERT(ret == size_data, "Read KVM stats data");157/* Read kvm stats data one by one */158for (i = 0; i < header.num_desc; ++i) {159pdesc = get_stats_descriptor(stats_desc, i, &header);160read_stat_data(stats_fd, &header, pdesc, stats_data,161pdesc->size);162}163164free(stats_data);165free(stats_desc);166free(id);167168close(stats_fd);169TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");170}171172#define DEFAULT_NUM_VM 4173#define DEFAULT_NUM_VCPU 4174175/*176* Usage: kvm_bin_form_stats [#vm] [#vcpu]177* The first parameter #vm set the number of VMs being created.178* The second parameter #vcpu set the number of VCPUs being created.179* By default, DEFAULT_NUM_VM VM and DEFAULT_NUM_VCPU VCPU for the VM would be180* created for testing.181*/182183int main(int argc, char *argv[])184{185int vm_stats_fds, *vcpu_stats_fds;186int i, j;187struct kvm_vcpu **vcpus;188struct kvm_vm **vms;189int max_vm = DEFAULT_NUM_VM;190int max_vcpu = DEFAULT_NUM_VCPU;191192/* Get the number of VMs and VCPUs that would be created for testing. */193if (argc > 1) {194max_vm = strtol(argv[1], NULL, 0);195if (max_vm <= 0)196max_vm = DEFAULT_NUM_VM;197}198if (argc > 2) {199max_vcpu = strtol(argv[2], NULL, 0);200if (max_vcpu <= 0)201max_vcpu = DEFAULT_NUM_VCPU;202}203204ksft_print_header();205206/* Check the extension for binary stats */207TEST_REQUIRE(kvm_has_cap(KVM_CAP_BINARY_STATS_FD));208209ksft_set_plan(max_vm);210211/* Create VMs and VCPUs */212vms = malloc(sizeof(vms[0]) * max_vm);213TEST_ASSERT(vms, "Allocate memory for storing VM pointers");214215vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);216TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");217218/*219* Not per-VM as the array is populated, used, and invalidated within a220* single for-loop iteration.221*/222vcpu_stats_fds = calloc(max_vm, sizeof(*vcpu_stats_fds));223TEST_ASSERT(vcpu_stats_fds, "Allocate memory for VM stats fds");224225for (i = 0; i < max_vm; ++i) {226vms[i] = vm_create_barebones();227for (j = 0; j < max_vcpu; ++j)228vcpus[i * max_vcpu + j] = __vm_vcpu_add(vms[i], j);229}230231/*232* Check stats read for every VM and vCPU, with a variety of flavors.233* Note, stats_test() closes the passed in stats fd.234*/235for (i = 0; i < max_vm; ++i) {236/*237* Verify that creating multiple userspace references to a238* single stats file works and doesn't cause explosions.239*/240vm_stats_fds = vm_get_stats_fd(vms[i]);241stats_test(kvm_dup(vm_stats_fds));242243/* Verify userspace can instantiate multiple stats files. */244stats_test(vm_get_stats_fd(vms[i]));245246for (j = 0; j < max_vcpu; ++j) {247vcpu_stats_fds[j] = vcpu_get_stats_fd(vcpus[i * max_vcpu + j]);248stats_test(kvm_dup(vcpu_stats_fds[j]));249stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j]));250}251252/*253* Close the VM fd and redo the stats tests. KVM should gift a254* reference (to the VM) to each stats fd, i.e. stats should255* still be accessible even after userspace has put its last256* _direct_ reference to the VM.257*/258kvm_vm_free(vms[i]);259260stats_test(vm_stats_fds);261for (j = 0; j < max_vcpu; ++j)262stats_test(vcpu_stats_fds[j]);263264ksft_test_result_pass("vm%i\n", i);265}266267free(vms);268free(vcpus);269free(vcpu_stats_fds);270271ksft_finished(); /* Print results and exit() accordingly */272}273274275