1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * KVM dirty page logging performance test 4 * 5 * Based on dirty_log_test.c 6 * 7 * Copyright (C) 2018, Red Hat, Inc. 8 * Copyright (C) 2020, Google, Inc. 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <time.h> 14 #include <pthread.h> 15 #include <linux/bitmap.h> 16 17 #include "kvm_util.h" 18 #include "test_util.h" 19 #include "perf_test_util.h" 20 #include "guest_modes.h" 21 22 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ 23 #define TEST_HOST_LOOP_N 2UL 24 25 static int nr_vcpus = 1; 26 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE; 27 28 /* Host variables */ 29 static u64 dirty_log_manual_caps; 30 static bool host_quit; 31 static int iteration; 32 static int vcpu_last_completed_iteration[KVM_MAX_VCPUS]; 33 34 static void vcpu_worker(struct perf_test_vcpu_args *vcpu_args) 35 { 36 int ret; 37 struct kvm_vm *vm = perf_test_args.vm; 38 uint64_t pages_count = 0; 39 struct kvm_run *run; 40 struct timespec start; 41 struct timespec ts_diff; 42 struct timespec total = (struct timespec){0}; 43 struct timespec avg; 44 int vcpu_id = vcpu_args->vcpu_id; 45 46 run = vcpu_state(vm, vcpu_id); 47 48 while (!READ_ONCE(host_quit)) { 49 int current_iteration = READ_ONCE(iteration); 50 51 clock_gettime(CLOCK_MONOTONIC, &start); 52 ret = _vcpu_run(vm, vcpu_id); 53 ts_diff = timespec_elapsed(start); 54 55 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret); 56 TEST_ASSERT(get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC, 57 "Invalid guest sync status: exit_reason=%s\n", 58 exit_reason_str(run->exit_reason)); 59 60 pr_debug("Got sync event from vCPU %d\n", vcpu_id); 61 vcpu_last_completed_iteration[vcpu_id] = current_iteration; 62 pr_debug("vCPU %d updated last completed iteration to %d\n", 63 vcpu_id, vcpu_last_completed_iteration[vcpu_id]); 64 65 if (current_iteration) { 66 pages_count += vcpu_args->pages; 67 total = timespec_add(total, ts_diff); 68 pr_debug("vCPU %d iteration %d dirty memory time: %ld.%.9lds\n", 69 vcpu_id, current_iteration, ts_diff.tv_sec, 70 ts_diff.tv_nsec); 71 } else { 72 pr_debug("vCPU %d iteration %d populate memory time: %ld.%.9lds\n", 73 vcpu_id, current_iteration, ts_diff.tv_sec, 74 ts_diff.tv_nsec); 75 } 76 77 while (current_iteration == READ_ONCE(iteration) && 78 !READ_ONCE(host_quit)) {} 79 } 80 81 avg = timespec_div(total, vcpu_last_completed_iteration[vcpu_id]); 82 pr_debug("\nvCPU %d dirtied 0x%lx pages over %d iterations in %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n", 83 vcpu_id, pages_count, vcpu_last_completed_iteration[vcpu_id], 84 total.tv_sec, total.tv_nsec, avg.tv_sec, avg.tv_nsec); 85 } 86 87 struct test_params { 88 unsigned long iterations; 89 uint64_t phys_offset; 90 int wr_fract; 91 bool partition_vcpu_memory_access; 92 enum vm_mem_backing_src_type backing_src; 93 int slots; 94 }; 95 96 static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable) 97 { 98 int i; 99 100 for (i = 0; i < slots; i++) { 101 int slot = PERF_TEST_MEM_SLOT_INDEX + i; 102 int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0; 103 104 vm_mem_region_set_flags(vm, slot, flags); 105 } 106 } 107 108 static inline void enable_dirty_logging(struct kvm_vm *vm, int slots) 109 { 110 toggle_dirty_logging(vm, slots, true); 111 } 112 113 static inline void disable_dirty_logging(struct kvm_vm *vm, int slots) 114 { 115 toggle_dirty_logging(vm, slots, false); 116 } 117 118 static void get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots) 119 { 120 int i; 121 122 for (i = 0; i < slots; i++) { 123 int slot = PERF_TEST_MEM_SLOT_INDEX + i; 124 125 kvm_vm_get_dirty_log(vm, slot, bitmaps[i]); 126 } 127 } 128 129 static void clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], 130 int slots, uint64_t pages_per_slot) 131 { 132 int i; 133 134 for (i = 0; i < slots; i++) { 135 int slot = PERF_TEST_MEM_SLOT_INDEX + i; 136 137 kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot); 138 } 139 } 140 141 static unsigned long **alloc_bitmaps(int slots, uint64_t pages_per_slot) 142 { 143 unsigned long **bitmaps; 144 int i; 145 146 bitmaps = malloc(slots * sizeof(bitmaps[0])); 147 TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array."); 148 149 for (i = 0; i < slots; i++) { 150 bitmaps[i] = bitmap_zalloc(pages_per_slot); 151 TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap."); 152 } 153 154 return bitmaps; 155 } 156 157 static void free_bitmaps(unsigned long *bitmaps[], int slots) 158 { 159 int i; 160 161 for (i = 0; i < slots; i++) 162 free(bitmaps[i]); 163 164 free(bitmaps); 165 } 166 167 static void run_test(enum vm_guest_mode mode, void *arg) 168 { 169 struct test_params *p = arg; 170 struct kvm_vm *vm; 171 unsigned long **bitmaps; 172 uint64_t guest_num_pages; 173 uint64_t host_num_pages; 174 uint64_t pages_per_slot; 175 int vcpu_id; 176 struct timespec start; 177 struct timespec ts_diff; 178 struct timespec get_dirty_log_total = (struct timespec){0}; 179 struct timespec vcpu_dirty_total = (struct timespec){0}; 180 struct timespec avg; 181 struct kvm_enable_cap cap = {}; 182 struct timespec clear_dirty_log_total = (struct timespec){0}; 183 184 vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 185 p->slots, p->backing_src, 186 p->partition_vcpu_memory_access); 187 188 perf_test_set_wr_fract(vm, p->wr_fract); 189 190 guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm); 191 guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages); 192 host_num_pages = vm_num_host_pages(mode, guest_num_pages); 193 pages_per_slot = host_num_pages / p->slots; 194 195 bitmaps = alloc_bitmaps(p->slots, pages_per_slot); 196 197 if (dirty_log_manual_caps) { 198 cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2; 199 cap.args[0] = dirty_log_manual_caps; 200 vm_enable_cap(vm, &cap); 201 } 202 203 /* Start the iterations */ 204 iteration = 0; 205 host_quit = false; 206 207 clock_gettime(CLOCK_MONOTONIC, &start); 208 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) 209 vcpu_last_completed_iteration[vcpu_id] = -1; 210 211 perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker); 212 213 /* Allow the vCPUs to populate memory */ 214 pr_debug("Starting iteration %d - Populating\n", iteration); 215 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { 216 while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id]) != 217 iteration) 218 ; 219 } 220 221 ts_diff = timespec_elapsed(start); 222 pr_info("Populate memory time: %ld.%.9lds\n", 223 ts_diff.tv_sec, ts_diff.tv_nsec); 224 225 /* Enable dirty logging */ 226 clock_gettime(CLOCK_MONOTONIC, &start); 227 enable_dirty_logging(vm, p->slots); 228 ts_diff = timespec_elapsed(start); 229 pr_info("Enabling dirty logging time: %ld.%.9lds\n\n", 230 ts_diff.tv_sec, ts_diff.tv_nsec); 231 232 while (iteration < p->iterations) { 233 /* 234 * Incrementing the iteration number will start the vCPUs 235 * dirtying memory again. 236 */ 237 clock_gettime(CLOCK_MONOTONIC, &start); 238 iteration++; 239 240 pr_debug("Starting iteration %d\n", iteration); 241 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { 242 while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id]) 243 != iteration) 244 ; 245 } 246 247 ts_diff = timespec_elapsed(start); 248 vcpu_dirty_total = timespec_add(vcpu_dirty_total, ts_diff); 249 pr_info("Iteration %d dirty memory time: %ld.%.9lds\n", 250 iteration, ts_diff.tv_sec, ts_diff.tv_nsec); 251 252 clock_gettime(CLOCK_MONOTONIC, &start); 253 get_dirty_log(vm, bitmaps, p->slots); 254 ts_diff = timespec_elapsed(start); 255 get_dirty_log_total = timespec_add(get_dirty_log_total, 256 ts_diff); 257 pr_info("Iteration %d get dirty log time: %ld.%.9lds\n", 258 iteration, ts_diff.tv_sec, ts_diff.tv_nsec); 259 260 if (dirty_log_manual_caps) { 261 clock_gettime(CLOCK_MONOTONIC, &start); 262 clear_dirty_log(vm, bitmaps, p->slots, pages_per_slot); 263 ts_diff = timespec_elapsed(start); 264 clear_dirty_log_total = timespec_add(clear_dirty_log_total, 265 ts_diff); 266 pr_info("Iteration %d clear dirty log time: %ld.%.9lds\n", 267 iteration, ts_diff.tv_sec, ts_diff.tv_nsec); 268 } 269 } 270 271 /* Disable dirty logging */ 272 clock_gettime(CLOCK_MONOTONIC, &start); 273 disable_dirty_logging(vm, p->slots); 274 ts_diff = timespec_elapsed(start); 275 pr_info("Disabling dirty logging time: %ld.%.9lds\n", 276 ts_diff.tv_sec, ts_diff.tv_nsec); 277 278 /* Tell the vcpu thread to quit */ 279 host_quit = true; 280 perf_test_join_vcpu_threads(nr_vcpus); 281 282 avg = timespec_div(get_dirty_log_total, p->iterations); 283 pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n", 284 p->iterations, get_dirty_log_total.tv_sec, 285 get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); 286 287 if (dirty_log_manual_caps) { 288 avg = timespec_div(clear_dirty_log_total, p->iterations); 289 pr_info("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n", 290 p->iterations, clear_dirty_log_total.tv_sec, 291 clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); 292 } 293 294 free_bitmaps(bitmaps, p->slots); 295 perf_test_destroy_vm(vm); 296 } 297 298 static void help(char *name) 299 { 300 puts(""); 301 printf("usage: %s [-h] [-i iterations] [-p offset] " 302 "[-m mode] [-b vcpu bytes] [-v vcpus] [-o] [-s mem type]" 303 "[-x memslots]\n", name); 304 puts(""); 305 printf(" -i: specify iteration counts (default: %"PRIu64")\n", 306 TEST_HOST_LOOP_N); 307 printf(" -p: specify guest physical test memory offset\n" 308 " Warning: a low offset can conflict with the loaded test code.\n"); 309 guest_modes_help(); 310 printf(" -b: specify the size of the memory region which should be\n" 311 " dirtied by each vCPU. e.g. 10M or 3G.\n" 312 " (default: 1G)\n"); 313 printf(" -f: specify the fraction of pages which should be written to\n" 314 " as opposed to simply read, in the form\n" 315 " 1/<fraction of pages to write>.\n" 316 " (default: 1 i.e. all pages are written to.)\n"); 317 printf(" -v: specify the number of vCPUs to run.\n"); 318 printf(" -o: Overlap guest memory accesses instead of partitioning\n" 319 " them into a separate region of memory for each vCPU.\n"); 320 backing_src_help("-s"); 321 printf(" -x: Split the memory region into this number of memslots.\n" 322 " (default: 1)\n"); 323 puts(""); 324 exit(0); 325 } 326 327 int main(int argc, char *argv[]) 328 { 329 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); 330 struct test_params p = { 331 .iterations = TEST_HOST_LOOP_N, 332 .wr_fract = 1, 333 .partition_vcpu_memory_access = true, 334 .backing_src = DEFAULT_VM_MEM_SRC, 335 .slots = 1, 336 }; 337 int opt; 338 339 dirty_log_manual_caps = 340 kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); 341 dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | 342 KVM_DIRTY_LOG_INITIALLY_SET); 343 344 guest_modes_append_default(); 345 346 while ((opt = getopt(argc, argv, "hi:p:m:b:f:v:os:x:")) != -1) { 347 switch (opt) { 348 case 'i': 349 p.iterations = atoi(optarg); 350 break; 351 case 'p': 352 p.phys_offset = strtoull(optarg, NULL, 0); 353 break; 354 case 'm': 355 guest_modes_cmdline(optarg); 356 break; 357 case 'b': 358 guest_percpu_mem_size = parse_size(optarg); 359 break; 360 case 'f': 361 p.wr_fract = atoi(optarg); 362 TEST_ASSERT(p.wr_fract >= 1, 363 "Write fraction cannot be less than one"); 364 break; 365 case 'v': 366 nr_vcpus = atoi(optarg); 367 TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, 368 "Invalid number of vcpus, must be between 1 and %d", max_vcpus); 369 break; 370 case 'o': 371 p.partition_vcpu_memory_access = false; 372 break; 373 case 's': 374 p.backing_src = parse_backing_src_type(optarg); 375 break; 376 case 'x': 377 p.slots = atoi(optarg); 378 break; 379 case 'h': 380 default: 381 help(argv[0]); 382 break; 383 } 384 } 385 386 TEST_ASSERT(p.iterations >= 2, "The test should have at least two iterations"); 387 388 pr_info("Test iterations: %"PRIu64"\n", p.iterations); 389 390 for_each_guest_mode(run_test, &p); 391 392 return 0; 393 } 394