kvm_binary_stats_test.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// 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;
29
	struct kvm_stats_header header;
30 31 32 33 34 35
	char *id;
	struct kvm_stats_desc *stats_desc;
	u64 *stats_data;
	struct kvm_stats_desc *pdesc;

	/* Read kvm stats header */
36 37
	read_stats_header(stats_fd, &header);

38
	size_desc = get_stats_descriptor_size(&header);
39 40

	/* Read kvm stats id string */
41
	id = malloc(header.name_size);
42
	TEST_ASSERT(id, "Allocate memory for id string");
43 44
	ret = read(stats_fd, id, header.name_size);
	TEST_ASSERT(ret == header.name_size, "Read id string");
45 46

	/* Check id string, that should start with "kvm" */
47
	TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,
48 49 50
				"Invalid KVM stats type, id: %s", id);

	/* Sanity check for other fields in header */
51
	if (header.num_desc == 0) {
52 53 54 55
		printf("No KVM stats defined!");
		return;
	}
	/* Check overlap */
56 57 58
	TEST_ASSERT(header.desc_offset > 0 && header.data_offset > 0
			&& header.desc_offset >= sizeof(header)
			&& header.data_offset >= sizeof(header),
59
			"Invalid offset fields in header");
60 61 62
	TEST_ASSERT(header.desc_offset > header.data_offset ||
			(header.desc_offset + size_desc * header.num_desc <=
							header.data_offset),
63 64 65
			"Descriptor block is overlapped with data block");

	/* Read kvm stats descriptors */
66
	stats_desc = read_stats_descriptors(stats_fd, &header);
67 68

	/* Sanity check for fields in descriptors */
69
	for (i = 0; i < header.num_desc; ++i) {
70
		pdesc = get_stats_descriptor(stats_desc, i, &header);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		/* Check type,unit,base boundaries */
		TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
				<= KVM_STATS_TYPE_MAX, "Unknown KVM stats type");
		TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK)
				<= KVM_STATS_UNIT_MAX, "Unknown KVM stats unit");
		TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK)
				<= KVM_STATS_BASE_MAX, "Unknown KVM stats base");
		/* Check exponent for stats unit
		 * 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
		 */
		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,
					"Unsupported KVM stats unit");
			break;
		case KVM_STATS_UNIT_SECONDS:
			TEST_ASSERT(pdesc->exponent <= 0,
					"Unsupported KVM stats unit");
			break;
		}
		/* Check name string */
98
		TEST_ASSERT(strlen(pdesc->name) < header.name_size,
99 100 101 102
				"KVM stats name(%s) too long", pdesc->name);
		/* Check size field, which should not be zero */
		TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0",
				pdesc->name);
103 104 105 106 107 108 109 110 111 112 113 114
		/* Check bucket_size field */
		switch (pdesc->flags & KVM_STATS_TYPE_MASK) {
		case KVM_STATS_TYPE_LINEAR_HIST:
			TEST_ASSERT(pdesc->bucket_size,
			    "Bucket size of Linear Histogram stats (%s) is zero",
			    pdesc->name);
			break;
		default:
			TEST_ASSERT(!pdesc->bucket_size,
			    "Bucket size of stats (%s) is not zero",
			    pdesc->name);
		}
115 116 117
		size_data += pdesc->size * sizeof(*stats_data);
	}
	/* Check overlap */
118 119
	TEST_ASSERT(header.data_offset >= header.desc_offset
		|| header.data_offset + size_data <= header.desc_offset,
120 121
		"Data block is overlapped with Descriptor block");
	/* Check validity of all stats data size */
122
	TEST_ASSERT(size_data >= header.num_desc * sizeof(*stats_data),
123 124
			"Data size is not correct");
	/* Check stats offset */
125
	for (i = 0; i < header.num_desc; ++i) {
126
		pdesc = get_stats_descriptor(stats_desc, i, &header);
127 128 129 130 131 132 133 134 135
		TEST_ASSERT(pdesc->offset < size_data,
			"Invalid offset (%u) for stats: %s",
			pdesc->offset, pdesc->name);
	}

	/* 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 */
136
	ret = pread(stats_fd, stats_data, size_data, header.data_offset);
137 138 139
	TEST_ASSERT(ret == size_data, "Read KVM stats data");
	/* Read kvm stats data one by one */
	size_data = 0;
140
	for (i = 0; i < header.num_desc; ++i) {
141
		pdesc = get_stats_descriptor(stats_desc, i, &header);
142 143
		ret = pread(stats_fd, stats_data,
				pdesc->size * sizeof(*stats_data),
144
				header.data_offset + size_data);
145 146 147 148 149 150 151 152 153 154 155 156 157
		TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data),
				"Read data of KVM stats: %s", pdesc->name);
		size_data += pdesc->size * sizeof(*stats_data);
	}

	free(stats_data);
	free(stats_desc);
	free(id);
}


static void vm_stats_test(struct kvm_vm *vm)
{
158
	int stats_fd = vm_get_stats_fd(vm);
159 160 161 162 163 164

	stats_test(stats_fd);
	close(stats_fd);
	TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
}

165
static void vcpu_stats_test(struct kvm_vcpu *vcpu)
166
{
167
	int stats_fd = vcpu_get_stats_fd(vcpu);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

	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;
188
	struct kvm_vcpu **vcpus;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	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 */
206
	TEST_REQUIRE(kvm_has_cap(KVM_CAP_BINARY_STATS_FD));
207 208 209 210

	/* Create VMs and VCPUs */
	vms = malloc(sizeof(vms[0]) * max_vm);
	TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
211 212 213 214

	vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
	TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");

215
	for (i = 0; i < max_vm; ++i) {
216
		vms[i] = vm_create_barebones();
217
		for (j = 0; j < max_vcpu; ++j)
218
			vcpus[i * max_vcpu + j] = __vm_vcpu_add(vms[i], j);
219 220 221 222 223 224
	}

	/* 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)
225
			vcpu_stats_test(vcpus[i * max_vcpu + j]);
226 227 228 229 230 231 232
	}

	for (i = 0; i < max_vm; ++i)
		kvm_vm_free(vms[i]);
	free(vms);
	return 0;
}