xref: /linux/tools/testing/selftests/kvm/kvm_binary_stats_test.c (revision 768e9a61856b75de08f5efa5813bb3e7f16ec271)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kvm_binary_stats_test
4  *
5  * Copyright (C) 2021, Google LLC.
6  *
7  * Test the fd-based interface for KVM statistics.
8  */
9 
10 #define _GNU_SOURCE /* for program_invocation_short_name */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 
17 #include "test_util.h"
18 
19 #include "kvm_util.h"
20 #include "asm/kvm.h"
21 #include "linux/kvm.h"
22 
23 static void stats_test(int stats_fd)
24 {
25 	ssize_t ret;
26 	int i;
27 	size_t size_desc;
28 	size_t size_data = 0;
29 	struct kvm_stats_header *header;
30 	char *id;
31 	struct kvm_stats_desc *stats_desc;
32 	u64 *stats_data;
33 	struct kvm_stats_desc *pdesc;
34 
35 	/* Read kvm stats header */
36 	header = malloc(sizeof(*header));
37 	TEST_ASSERT(header, "Allocate memory for stats header");
38 
39 	ret = read(stats_fd, header, sizeof(*header));
40 	TEST_ASSERT(ret == sizeof(*header), "Read stats header");
41 	size_desc = sizeof(*stats_desc) + header->name_size;
42 
43 	/* Read kvm stats id string */
44 	id = malloc(header->name_size);
45 	TEST_ASSERT(id, "Allocate memory for id string");
46 	ret = read(stats_fd, id, header->name_size);
47 	TEST_ASSERT(ret == header->name_size, "Read id string");
48 
49 	/* Check id string, that should start with "kvm" */
50 	TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header->name_size,
51 				"Invalid KVM stats type, id: %s", id);
52 
53 	/* Sanity check for other fields in header */
54 	if (header->num_desc == 0) {
55 		printf("No KVM stats defined!");
56 		return;
57 	}
58 	/* Check overlap */
59 	TEST_ASSERT(header->desc_offset > 0 && header->data_offset > 0
60 			&& header->desc_offset >= sizeof(*header)
61 			&& header->data_offset >= sizeof(*header),
62 			"Invalid offset fields in header");
63 	TEST_ASSERT(header->desc_offset > header->data_offset ||
64 			(header->desc_offset + size_desc * header->num_desc <=
65 							header->data_offset),
66 			"Descriptor block is overlapped with data block");
67 
68 	/* Allocate memory for stats descriptors */
69 	stats_desc = calloc(header->num_desc, size_desc);
70 	TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
71 	/* Read kvm stats descriptors */
72 	ret = pread(stats_fd, stats_desc,
73 			size_desc * header->num_desc, header->desc_offset);
74 	TEST_ASSERT(ret == size_desc * header->num_desc,
75 			"Read KVM stats descriptors");
76 
77 	/* Sanity check for fields in descriptors */
78 	for (i = 0; i < header->num_desc; ++i) {
79 		pdesc = (void *)stats_desc + i * size_desc;
80 		/* Check type,unit,base boundaries */
81 		TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
82 				<= KVM_STATS_TYPE_MAX, "Unknown KVM stats type");
83 		TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK)
84 				<= KVM_STATS_UNIT_MAX, "Unknown KVM stats unit");
85 		TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK)
86 				<= KVM_STATS_BASE_MAX, "Unknown KVM stats base");
87 		/* Check exponent for stats unit
88 		 * Exponent for counter should be greater than or equal to 0
89 		 * Exponent for unit bytes should be greater than or equal to 0
90 		 * Exponent for unit seconds should be less than or equal to 0
91 		 * Exponent for unit clock cycles should be greater than or
92 		 * equal to 0
93 		 */
94 		switch (pdesc->flags & KVM_STATS_UNIT_MASK) {
95 		case KVM_STATS_UNIT_NONE:
96 		case KVM_STATS_UNIT_BYTES:
97 		case KVM_STATS_UNIT_CYCLES:
98 			TEST_ASSERT(pdesc->exponent >= 0,
99 					"Unsupported KVM stats unit");
100 			break;
101 		case KVM_STATS_UNIT_SECONDS:
102 			TEST_ASSERT(pdesc->exponent <= 0,
103 					"Unsupported KVM stats unit");
104 			break;
105 		}
106 		/* Check name string */
107 		TEST_ASSERT(strlen(pdesc->name) < header->name_size,
108 				"KVM stats name(%s) too long", pdesc->name);
109 		/* Check size field, which should not be zero */
110 		TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0",
111 				pdesc->name);
112 		/* Check bucket_size field */
113 		switch (pdesc->flags & KVM_STATS_TYPE_MASK) {
114 		case KVM_STATS_TYPE_LINEAR_HIST:
115 			TEST_ASSERT(pdesc->bucket_size,
116 			    "Bucket size of Linear Histogram stats (%s) is zero",
117 			    pdesc->name);
118 			break;
119 		default:
120 			TEST_ASSERT(!pdesc->bucket_size,
121 			    "Bucket size of stats (%s) is not zero",
122 			    pdesc->name);
123 		}
124 		size_data += pdesc->size * sizeof(*stats_data);
125 	}
126 	/* Check overlap */
127 	TEST_ASSERT(header->data_offset >= header->desc_offset
128 		|| header->data_offset + size_data <= header->desc_offset,
129 		"Data block is overlapped with Descriptor block");
130 	/* Check validity of all stats data size */
131 	TEST_ASSERT(size_data >= header->num_desc * sizeof(*stats_data),
132 			"Data size is not correct");
133 	/* Check stats offset */
134 	for (i = 0; i < header->num_desc; ++i) {
135 		pdesc = (void *)stats_desc + i * size_desc;
136 		TEST_ASSERT(pdesc->offset < size_data,
137 			"Invalid offset (%u) for stats: %s",
138 			pdesc->offset, pdesc->name);
139 	}
140 
141 	/* Allocate memory for stats data */
142 	stats_data = malloc(size_data);
143 	TEST_ASSERT(stats_data, "Allocate memory for stats data");
144 	/* Read kvm stats data as a bulk */
145 	ret = pread(stats_fd, stats_data, size_data, header->data_offset);
146 	TEST_ASSERT(ret == size_data, "Read KVM stats data");
147 	/* Read kvm stats data one by one */
148 	size_data = 0;
149 	for (i = 0; i < header->num_desc; ++i) {
150 		pdesc = (void *)stats_desc + i * size_desc;
151 		ret = pread(stats_fd, stats_data,
152 				pdesc->size * sizeof(*stats_data),
153 				header->data_offset + size_data);
154 		TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data),
155 				"Read data of KVM stats: %s", pdesc->name);
156 		size_data += pdesc->size * sizeof(*stats_data);
157 	}
158 
159 	free(stats_data);
160 	free(stats_desc);
161 	free(id);
162 	free(header);
163 }
164 
165 
166 static void vm_stats_test(struct kvm_vm *vm)
167 {
168 	int stats_fd = vm_get_stats_fd(vm);
169 
170 	stats_test(stats_fd);
171 	close(stats_fd);
172 	TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
173 }
174 
175 static void vcpu_stats_test(struct kvm_vcpu *vcpu)
176 {
177 	int stats_fd = vcpu_get_stats_fd(vcpu);
178 
179 	stats_test(stats_fd);
180 	close(stats_fd);
181 	TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
182 }
183 
184 #define DEFAULT_NUM_VM		4
185 #define DEFAULT_NUM_VCPU	4
186 
187 /*
188  * Usage: kvm_bin_form_stats [#vm] [#vcpu]
189  * The first parameter #vm set the number of VMs being created.
190  * The second parameter #vcpu set the number of VCPUs being created.
191  * By default, DEFAULT_NUM_VM VM and DEFAULT_NUM_VCPU VCPU for the VM would be
192  * created for testing.
193  */
194 
195 int main(int argc, char *argv[])
196 {
197 	int i, j;
198 	struct kvm_vcpu **vcpus;
199 	struct kvm_vm **vms;
200 	int max_vm = DEFAULT_NUM_VM;
201 	int max_vcpu = DEFAULT_NUM_VCPU;
202 
203 	/* Get the number of VMs and VCPUs that would be created for testing. */
204 	if (argc > 1) {
205 		max_vm = strtol(argv[1], NULL, 0);
206 		if (max_vm <= 0)
207 			max_vm = DEFAULT_NUM_VM;
208 	}
209 	if (argc > 2) {
210 		max_vcpu = strtol(argv[2], NULL, 0);
211 		if (max_vcpu <= 0)
212 			max_vcpu = DEFAULT_NUM_VCPU;
213 	}
214 
215 	/* Check the extension for binary stats */
216 	if (kvm_check_cap(KVM_CAP_BINARY_STATS_FD) <= 0) {
217 		print_skip("Binary form statistics interface is not supported");
218 		exit(KSFT_SKIP);
219 	}
220 
221 	/* Create VMs and VCPUs */
222 	vms = malloc(sizeof(vms[0]) * max_vm);
223 	TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
224 
225 	vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
226 	TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
227 
228 	for (i = 0; i < max_vm; ++i) {
229 		vms[i] = vm_create_barebones();
230 		for (j = 0; j < max_vcpu; ++j)
231 			vcpus[j * max_vcpu + i] = __vm_vcpu_add(vms[i], j);
232 	}
233 
234 	/* Check stats read for every VM and VCPU */
235 	for (i = 0; i < max_vm; ++i) {
236 		vm_stats_test(vms[i]);
237 		for (j = 0; j < max_vcpu; ++j)
238 			vcpu_stats_test(vcpus[j * max_vcpu + i]);
239 	}
240 
241 	for (i = 0; i < max_vm; ++i)
242 		kvm_vm_free(vms[i]);
243 	free(vms);
244 	return 0;
245 }
246