dirty_log_perf_test.c 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// SPDX-License-Identifier: GPL-2.0
/*
 * KVM dirty page logging performance test
 *
 * Based on dirty_log_test.c
 *
 * Copyright (C) 2018, Red Hat, Inc.
 * Copyright (C) 2020, Google, Inc.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <linux/bitmap.h>

#include "kvm_util.h"
#include "test_util.h"
19 20
#include "perf_test_util.h"
#include "guest_modes.h"
21 22 23 24

/* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
#define TEST_HOST_LOOP_N		2UL

25 26 27
static int nr_vcpus = 1;
static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;

28
/* Host variables */
29
static u64 dirty_log_manual_caps;
30
static bool host_quit;
31 32
static int iteration;
static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
33 34 35 36 37 38 39 40 41 42 43

static void *vcpu_worker(void *data)
{
	int ret;
	struct kvm_vm *vm = perf_test_args.vm;
	uint64_t pages_count = 0;
	struct kvm_run *run;
	struct timespec start;
	struct timespec ts_diff;
	struct timespec total = (struct timespec){0};
	struct timespec avg;
44
	struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;
45 46 47 48 49 50
	int vcpu_id = vcpu_args->vcpu_id;

	vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
	run = vcpu_state(vm, vcpu_id);

	while (!READ_ONCE(host_quit)) {
51
		int current_iteration = READ_ONCE(iteration);
52 53 54

		clock_gettime(CLOCK_MONOTONIC, &start);
		ret = _vcpu_run(vm, vcpu_id);
55
		ts_diff = timespec_elapsed(start);
56 57 58 59 60 61 62 63

		TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
		TEST_ASSERT(get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC,
			    "Invalid guest sync status: exit_reason=%s\n",
			    exit_reason_str(run->exit_reason));

		pr_debug("Got sync event from vCPU %d\n", vcpu_id);
		vcpu_last_completed_iteration[vcpu_id] = current_iteration;
64
		pr_debug("vCPU %d updated last completed iteration to %d\n",
65 66 67 68 69
			 vcpu_id, vcpu_last_completed_iteration[vcpu_id]);

		if (current_iteration) {
			pages_count += vcpu_args->pages;
			total = timespec_add(total, ts_diff);
70
			pr_debug("vCPU %d iteration %d dirty memory time: %ld.%.9lds\n",
71 72 73
				vcpu_id, current_iteration, ts_diff.tv_sec,
				ts_diff.tv_nsec);
		} else {
74
			pr_debug("vCPU %d iteration %d populate memory time: %ld.%.9lds\n",
75 76 77 78 79 80 81 82 83
				vcpu_id, current_iteration, ts_diff.tv_sec,
				ts_diff.tv_nsec);
		}

		while (current_iteration == READ_ONCE(iteration) &&
		       !READ_ONCE(host_quit)) {}
	}

	avg = timespec_div(total, vcpu_last_completed_iteration[vcpu_id]);
84
	pr_debug("\nvCPU %d dirtied 0x%lx pages over %d iterations in %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
85 86 87 88 89 90
		vcpu_id, pages_count, vcpu_last_completed_iteration[vcpu_id],
		total.tv_sec, total.tv_nsec, avg.tv_sec, avg.tv_nsec);

	return NULL;
}

91 92 93 94
struct test_params {
	unsigned long iterations;
	uint64_t phys_offset;
	int wr_fract;
95
	bool partition_vcpu_memory_access;
96 97 98
};

static void run_test(enum vm_guest_mode mode, void *arg)
99
{
100
	struct test_params *p = arg;
101 102 103 104 105 106 107 108 109 110 111 112 113 114
	pthread_t *vcpu_threads;
	struct kvm_vm *vm;
	unsigned long *bmap;
	uint64_t guest_num_pages;
	uint64_t host_num_pages;
	int vcpu_id;
	struct timespec start;
	struct timespec ts_diff;
	struct timespec get_dirty_log_total = (struct timespec){0};
	struct timespec vcpu_dirty_total = (struct timespec){0};
	struct timespec avg;
	struct kvm_enable_cap cap = {};
	struct timespec clear_dirty_log_total = (struct timespec){0};

115
	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size);
116

117
	perf_test_args.wr_fract = p->wr_fract;
118 119 120 121 122 123

	guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
	guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
	host_num_pages = vm_num_host_pages(mode, guest_num_pages);
	bmap = bitmap_alloc(host_num_pages);

124 125 126 127 128
	if (dirty_log_manual_caps) {
		cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
		cap.args[0] = dirty_log_manual_caps;
		vm_enable_cap(vm, &cap);
	}
129 130 131 132

	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
	TEST_ASSERT(vcpu_threads, "Memory allocation failed");

133 134
	perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
			      p->partition_vcpu_memory_access);
135 136 137 138 139 140 141 142 143

	sync_global_to_guest(vm, perf_test_args);

	/* Start the iterations */
	iteration = 0;
	host_quit = false;

	clock_gettime(CLOCK_MONOTONIC, &start);
	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
144 145
		vcpu_last_completed_iteration[vcpu_id] = -1;

146 147 148 149
		pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
			       &perf_test_args.vcpu_args[vcpu_id]);
	}

150
	/* Allow the vCPUs to populate memory */
151
	pr_debug("Starting iteration %d - Populating\n", iteration);
152 153 154 155 156
	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
		while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id]) !=
		       iteration)
			;
	}
157

158
	ts_diff = timespec_elapsed(start);
159 160 161 162 163
	pr_info("Populate memory time: %ld.%.9lds\n",
		ts_diff.tv_sec, ts_diff.tv_nsec);

	/* Enable dirty logging */
	clock_gettime(CLOCK_MONOTONIC, &start);
164
	vm_mem_region_set_flags(vm, PERF_TEST_MEM_SLOT_INDEX,
165
				KVM_MEM_LOG_DIRTY_PAGES);
166
	ts_diff = timespec_elapsed(start);
167 168 169
	pr_info("Enabling dirty logging time: %ld.%.9lds\n\n",
		ts_diff.tv_sec, ts_diff.tv_nsec);

170
	while (iteration < p->iterations) {
171 172 173 174 175 176 177
		/*
		 * Incrementing the iteration number will start the vCPUs
		 * dirtying memory again.
		 */
		clock_gettime(CLOCK_MONOTONIC, &start);
		iteration++;

178
		pr_debug("Starting iteration %d\n", iteration);
179
		for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
180 181 182
			while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
			       != iteration)
				;
183 184
		}

185
		ts_diff = timespec_elapsed(start);
186
		vcpu_dirty_total = timespec_add(vcpu_dirty_total, ts_diff);
187
		pr_info("Iteration %d dirty memory time: %ld.%.9lds\n",
188 189 190
			iteration, ts_diff.tv_sec, ts_diff.tv_nsec);

		clock_gettime(CLOCK_MONOTONIC, &start);
191
		kvm_vm_get_dirty_log(vm, PERF_TEST_MEM_SLOT_INDEX, bmap);
192

193
		ts_diff = timespec_elapsed(start);
194 195
		get_dirty_log_total = timespec_add(get_dirty_log_total,
						   ts_diff);
196
		pr_info("Iteration %d get dirty log time: %ld.%.9lds\n",
197 198
			iteration, ts_diff.tv_sec, ts_diff.tv_nsec);

199 200
		if (dirty_log_manual_caps) {
			clock_gettime(CLOCK_MONOTONIC, &start);
201
			kvm_vm_clear_dirty_log(vm, PERF_TEST_MEM_SLOT_INDEX, bmap, 0,
202
					       host_num_pages);
203

204
			ts_diff = timespec_elapsed(start);
205 206
			clear_dirty_log_total = timespec_add(clear_dirty_log_total,
							     ts_diff);
207
			pr_info("Iteration %d clear dirty log time: %ld.%.9lds\n",
208 209
				iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
		}
210 211 212 213 214 215 216 217 218
	}

	/* Tell the vcpu thread to quit */
	host_quit = true;
	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
		pthread_join(vcpu_threads[vcpu_id], NULL);

	/* Disable dirty logging */
	clock_gettime(CLOCK_MONOTONIC, &start);
219
	vm_mem_region_set_flags(vm, PERF_TEST_MEM_SLOT_INDEX, 0);
220
	ts_diff = timespec_elapsed(start);
221 222 223
	pr_info("Disabling dirty logging time: %ld.%.9lds\n",
		ts_diff.tv_sec, ts_diff.tv_nsec);

224
	avg = timespec_div(get_dirty_log_total, p->iterations);
225
	pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
226
		p->iterations, get_dirty_log_total.tv_sec,
227 228
		get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);

229
	if (dirty_log_manual_caps) {
230
		avg = timespec_div(clear_dirty_log_total, p->iterations);
231
		pr_info("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
232
			p->iterations, clear_dirty_log_total.tv_sec,
233 234
			clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);
	}
235 236 237

	free(bmap);
	free(vcpu_threads);
238
	perf_test_destroy_vm(vm);
239 240 241 242 243 244
}

static void help(char *name)
{
	puts("");
	printf("usage: %s [-h] [-i iterations] [-p offset] "
245
	       "[-m mode] [-b vcpu bytes] [-v vcpus] [-o]\n", name);
246 247 248 249 250
	puts("");
	printf(" -i: specify iteration counts (default: %"PRIu64")\n",
	       TEST_HOST_LOOP_N);
	printf(" -p: specify guest physical test memory offset\n"
	       "     Warning: a low offset can conflict with the loaded test code.\n");
251
	guest_modes_help();
252 253 254 255 256 257 258 259
	printf(" -b: specify the size of the memory region which should be\n"
	       "     dirtied by each vCPU. e.g. 10M or 3G.\n"
	       "     (default: 1G)\n");
	printf(" -f: specify the fraction of pages which should be written to\n"
	       "     as opposed to simply read, in the form\n"
	       "     1/<fraction of pages to write>.\n"
	       "     (default: 1 i.e. all pages are written to.)\n");
	printf(" -v: specify the number of vCPUs to run.\n");
260 261
	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
	       "     them into a separate region of memory for each vCPU.\n");
262 263 264 265 266 267
	puts("");
	exit(0);
}

int main(int argc, char *argv[])
{
268 269 270 271
	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
	struct test_params p = {
		.iterations = TEST_HOST_LOOP_N,
		.wr_fract = 1,
272
		.partition_vcpu_memory_access = true,
273 274
	};
	int opt;
275 276 277 278 279 280

	dirty_log_manual_caps =
		kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
	dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
				  KVM_DIRTY_LOG_INITIALLY_SET);

281
	guest_modes_append_default();
282

283
	while ((opt = getopt(argc, argv, "hi:p:m:b:f:v:o")) != -1) {
284 285
		switch (opt) {
		case 'i':
286
			p.iterations = atoi(optarg);
287 288
			break;
		case 'p':
289
			p.phys_offset = strtoull(optarg, NULL, 0);
290 291
			break;
		case 'm':
292
			guest_modes_cmdline(optarg);
293 294 295 296 297
			break;
		case 'b':
			guest_percpu_mem_size = parse_size(optarg);
			break;
		case 'f':
298 299
			p.wr_fract = atoi(optarg);
			TEST_ASSERT(p.wr_fract >= 1,
300 301 302 303
				    "Write fraction cannot be less than one");
			break;
		case 'v':
			nr_vcpus = atoi(optarg);
304 305
			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
				    "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
306
			break;
307 308 309
		case 'o':
			p.partition_vcpu_memory_access = false;
			break;
310 311 312 313 314 315 316
		case 'h':
		default:
			help(argv[0]);
			break;
		}
	}

317
	TEST_ASSERT(p.iterations >= 2, "The test should have at least two iterations");
318

319
	pr_info("Test iterations: %"PRIu64"\n",	p.iterations);
320

321
	for_each_guest_mode(run_test, &p);
322 323 324

	return 0;
}