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(void *data) 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 struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data; 45 int vcpu_id = vcpu_args->vcpu_id; 46 47 vcpu_args_set(vm, vcpu_id, 1, vcpu_id); 48 run = vcpu_state(vm, vcpu_id); 49 50 while (!READ_ONCE(host_quit)) { 51 int current_iteration = READ_ONCE(iteration); 52 53 clock_gettime(CLOCK_MONOTONIC, &start); 54 ret = _vcpu_run(vm, vcpu_id); 55 ts_diff = timespec_elapsed(start); 56 57 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret); 58 TEST_ASSERT(get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC, 59 "Invalid guest sync status: exit_reason=%s\n", 60 exit_reason_str(run->exit_reason)); 61 62 pr_debug("Got sync event from vCPU %d\n", vcpu_id); 63 vcpu_last_completed_iteration[vcpu_id] = current_iteration; 64 pr_debug("vCPU %d updated last completed iteration to %d\n", 65 vcpu_id, vcpu_last_completed_iteration[vcpu_id]); 66 67 if (current_iteration) { 68 pages_count += vcpu_args->pages; 69 total = timespec_add(total, ts_diff); 70 pr_debug("vCPU %d iteration %d dirty memory time: %ld.%.9lds\n", 71 vcpu_id, current_iteration, ts_diff.tv_sec, 72 ts_diff.tv_nsec); 73 } else { 74 pr_debug("vCPU %d iteration %d populate memory time: %ld.%.9lds\n", 75 vcpu_id, current_iteration, ts_diff.tv_sec, 76 ts_diff.tv_nsec); 77 } 78 79 while (current_iteration == READ_ONCE(iteration) && 80 !READ_ONCE(host_quit)) {} 81 } 82 83 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 vcpu_id, pages_count, vcpu_last_completed_iteration[vcpu_id], 86 total.tv_sec, total.tv_nsec, avg.tv_sec, avg.tv_nsec); 87 88 return NULL; 89 } 90 91 struct test_params { 92 unsigned long iterations; 93 uint64_t phys_offset; 94 int wr_fract; 95 bool partition_vcpu_memory_access; 96 enum vm_mem_backing_src_type backing_src; 97 }; 98 99 static void run_test(enum vm_guest_mode mode, void *arg) 100 { 101 struct test_params *p = arg; 102 pthread_t *vcpu_threads; 103 struct kvm_vm *vm; 104 unsigned long *bmap; 105 uint64_t guest_num_pages; 106 uint64_t host_num_pages; 107 int vcpu_id; 108 struct timespec start; 109 struct timespec ts_diff; 110 struct timespec get_dirty_log_total = (struct timespec){0}; 111 struct timespec vcpu_dirty_total = (struct timespec){0}; 112 struct timespec avg; 113 struct kvm_enable_cap cap = {}; 114 struct timespec clear_dirty_log_total = (struct timespec){0}; 115 116 vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 117 p->backing_src); 118 119 perf_test_args.wr_fract = p->wr_fract; 120 121 guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm); 122 guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages); 123 host_num_pages = vm_num_host_pages(mode, guest_num_pages); 124 bmap = bitmap_alloc(host_num_pages); 125 126 if (dirty_log_manual_caps) { 127 cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2; 128 cap.args[0] = dirty_log_manual_caps; 129 vm_enable_cap(vm, &cap); 130 } 131 132 vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads)); 133 TEST_ASSERT(vcpu_threads, "Memory allocation failed"); 134 135 perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size, 136 p->partition_vcpu_memory_access); 137 138 sync_global_to_guest(vm, perf_test_args); 139 140 /* Start the iterations */ 141 iteration = 0; 142 host_quit = false; 143 144 clock_gettime(CLOCK_MONOTONIC, &start); 145 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { 146 vcpu_last_completed_iteration[vcpu_id] = -1; 147 148 pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker, 149 &perf_test_args.vcpu_args[vcpu_id]); 150 } 151 152 /* Allow the vCPUs to populate memory */ 153 pr_debug("Starting iteration %d - Populating\n", iteration); 154 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { 155 while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id]) != 156 iteration) 157 ; 158 } 159 160 ts_diff = timespec_elapsed(start); 161 pr_info("Populate memory time: %ld.%.9lds\n", 162 ts_diff.tv_sec, ts_diff.tv_nsec); 163 164 /* Enable dirty logging */ 165 clock_gettime(CLOCK_MONOTONIC, &start); 166 vm_mem_region_set_flags(vm, PERF_TEST_MEM_SLOT_INDEX, 167 KVM_MEM_LOG_DIRTY_PAGES); 168 ts_diff = timespec_elapsed(start); 169 pr_info("Enabling dirty logging time: %ld.%.9lds\n\n", 170 ts_diff.tv_sec, ts_diff.tv_nsec); 171 172 while (iteration < p->iterations) { 173 /* 174 * Incrementing the iteration number will start the vCPUs 175 * dirtying memory again. 176 */ 177 clock_gettime(CLOCK_MONOTONIC, &start); 178 iteration++; 179 180 pr_debug("Starting iteration %d\n", iteration); 181 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { 182 while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id]) 183 != iteration) 184 ; 185 } 186 187 ts_diff = timespec_elapsed(start); 188 vcpu_dirty_total = timespec_add(vcpu_dirty_total, ts_diff); 189 pr_info("Iteration %d dirty memory time: %ld.%.9lds\n", 190 iteration, ts_diff.tv_sec, ts_diff.tv_nsec); 191 192 clock_gettime(CLOCK_MONOTONIC, &start); 193 kvm_vm_get_dirty_log(vm, PERF_TEST_MEM_SLOT_INDEX, bmap); 194 195 ts_diff = timespec_elapsed(start); 196 get_dirty_log_total = timespec_add(get_dirty_log_total, 197 ts_diff); 198 pr_info("Iteration %d get dirty log time: %ld.%.9lds\n", 199 iteration, ts_diff.tv_sec, ts_diff.tv_nsec); 200 201 if (dirty_log_manual_caps) { 202 clock_gettime(CLOCK_MONOTONIC, &start); 203 kvm_vm_clear_dirty_log(vm, PERF_TEST_MEM_SLOT_INDEX, bmap, 0, 204 host_num_pages); 205 206 ts_diff = timespec_elapsed(start); 207 clear_dirty_log_total = timespec_add(clear_dirty_log_total, 208 ts_diff); 209 pr_info("Iteration %d clear dirty log time: %ld.%.9lds\n", 210 iteration, ts_diff.tv_sec, ts_diff.tv_nsec); 211 } 212 } 213 214 /* Disable dirty logging */ 215 clock_gettime(CLOCK_MONOTONIC, &start); 216 vm_mem_region_set_flags(vm, PERF_TEST_MEM_SLOT_INDEX, 0); 217 ts_diff = timespec_elapsed(start); 218 pr_info("Disabling dirty logging time: %ld.%.9lds\n", 219 ts_diff.tv_sec, ts_diff.tv_nsec); 220 221 /* Tell the vcpu thread to quit */ 222 host_quit = true; 223 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) 224 pthread_join(vcpu_threads[vcpu_id], NULL); 225 226 avg = timespec_div(get_dirty_log_total, p->iterations); 227 pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n", 228 p->iterations, get_dirty_log_total.tv_sec, 229 get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); 230 231 if (dirty_log_manual_caps) { 232 avg = timespec_div(clear_dirty_log_total, p->iterations); 233 pr_info("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n", 234 p->iterations, clear_dirty_log_total.tv_sec, 235 clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); 236 } 237 238 free(bmap); 239 free(vcpu_threads); 240 perf_test_destroy_vm(vm); 241 } 242 243 static void help(char *name) 244 { 245 puts(""); 246 printf("usage: %s [-h] [-i iterations] [-p offset] " 247 "[-m mode] [-b vcpu bytes] [-v vcpus] [-o] [-s mem type]\n", name); 248 puts(""); 249 printf(" -i: specify iteration counts (default: %"PRIu64")\n", 250 TEST_HOST_LOOP_N); 251 printf(" -p: specify guest physical test memory offset\n" 252 " Warning: a low offset can conflict with the loaded test code.\n"); 253 guest_modes_help(); 254 printf(" -b: specify the size of the memory region which should be\n" 255 " dirtied by each vCPU. e.g. 10M or 3G.\n" 256 " (default: 1G)\n"); 257 printf(" -f: specify the fraction of pages which should be written to\n" 258 " as opposed to simply read, in the form\n" 259 " 1/<fraction of pages to write>.\n" 260 " (default: 1 i.e. all pages are written to.)\n"); 261 printf(" -v: specify the number of vCPUs to run.\n"); 262 printf(" -o: Overlap guest memory accesses instead of partitioning\n" 263 " them into a separate region of memory for each vCPU.\n"); 264 printf(" -s: specify the type of memory that should be used to\n" 265 " back the guest data region.\n\n"); 266 backing_src_help(); 267 puts(""); 268 exit(0); 269 } 270 271 int main(int argc, char *argv[]) 272 { 273 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); 274 struct test_params p = { 275 .iterations = TEST_HOST_LOOP_N, 276 .wr_fract = 1, 277 .partition_vcpu_memory_access = true, 278 .backing_src = VM_MEM_SRC_ANONYMOUS, 279 }; 280 int opt; 281 282 dirty_log_manual_caps = 283 kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); 284 dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | 285 KVM_DIRTY_LOG_INITIALLY_SET); 286 287 guest_modes_append_default(); 288 289 while ((opt = getopt(argc, argv, "hi:p:m:b:f:v:os:")) != -1) { 290 switch (opt) { 291 case 'i': 292 p.iterations = atoi(optarg); 293 break; 294 case 'p': 295 p.phys_offset = strtoull(optarg, NULL, 0); 296 break; 297 case 'm': 298 guest_modes_cmdline(optarg); 299 break; 300 case 'b': 301 guest_percpu_mem_size = parse_size(optarg); 302 break; 303 case 'f': 304 p.wr_fract = atoi(optarg); 305 TEST_ASSERT(p.wr_fract >= 1, 306 "Write fraction cannot be less than one"); 307 break; 308 case 'v': 309 nr_vcpus = atoi(optarg); 310 TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus, 311 "Invalid number of vcpus, must be between 1 and %d", max_vcpus); 312 break; 313 case 'o': 314 p.partition_vcpu_memory_access = false; 315 case 's': 316 p.backing_src = parse_backing_src_type(optarg); 317 break; 318 case 'h': 319 default: 320 help(argv[0]); 321 break; 322 } 323 } 324 325 TEST_ASSERT(p.iterations >= 2, "The test should have at least two iterations"); 326 327 pr_info("Test iterations: %"PRIu64"\n", p.iterations); 328 329 for_each_guest_mode(run_test, &p); 330 331 return 0; 332 } 333