Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * KVM dirty page logging performance test
0004  *
0005  * Based on dirty_log_test.c
0006  *
0007  * Copyright (C) 2018, Red Hat, Inc.
0008  * Copyright (C) 2020, Google, Inc.
0009  */
0010 
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <time.h>
0014 #include <pthread.h>
0015 #include <linux/bitmap.h>
0016 
0017 #include "kvm_util.h"
0018 #include "test_util.h"
0019 #include "perf_test_util.h"
0020 #include "guest_modes.h"
0021 
0022 #ifdef __aarch64__
0023 #include "aarch64/vgic.h"
0024 
0025 #define GICD_BASE_GPA           0x8000000ULL
0026 #define GICR_BASE_GPA           0x80A0000ULL
0027 
0028 static int gic_fd;
0029 
0030 static void arch_setup_vm(struct kvm_vm *vm, unsigned int nr_vcpus)
0031 {
0032     /*
0033      * The test can still run even if hardware does not support GICv3, as it
0034      * is only an optimization to reduce guest exits.
0035      */
0036     gic_fd = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
0037 }
0038 
0039 static void arch_cleanup_vm(struct kvm_vm *vm)
0040 {
0041     if (gic_fd > 0)
0042         close(gic_fd);
0043 }
0044 
0045 #else /* __aarch64__ */
0046 
0047 static void arch_setup_vm(struct kvm_vm *vm, unsigned int nr_vcpus)
0048 {
0049 }
0050 
0051 static void arch_cleanup_vm(struct kvm_vm *vm)
0052 {
0053 }
0054 
0055 #endif
0056 
0057 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
0058 #define TEST_HOST_LOOP_N        2UL
0059 
0060 static int nr_vcpus = 1;
0061 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
0062 static bool run_vcpus_while_disabling_dirty_logging;
0063 
0064 /* Host variables */
0065 static u64 dirty_log_manual_caps;
0066 static bool host_quit;
0067 static int iteration;
0068 static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
0069 
0070 static void vcpu_worker(struct perf_test_vcpu_args *vcpu_args)
0071 {
0072     struct kvm_vcpu *vcpu = vcpu_args->vcpu;
0073     int vcpu_idx = vcpu_args->vcpu_idx;
0074     uint64_t pages_count = 0;
0075     struct kvm_run *run;
0076     struct timespec start;
0077     struct timespec ts_diff;
0078     struct timespec total = (struct timespec){0};
0079     struct timespec avg;
0080     int ret;
0081 
0082     run = vcpu->run;
0083 
0084     while (!READ_ONCE(host_quit)) {
0085         int current_iteration = READ_ONCE(iteration);
0086 
0087         clock_gettime(CLOCK_MONOTONIC, &start);
0088         ret = _vcpu_run(vcpu);
0089         ts_diff = timespec_elapsed(start);
0090 
0091         TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
0092         TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC,
0093                 "Invalid guest sync status: exit_reason=%s\n",
0094                 exit_reason_str(run->exit_reason));
0095 
0096         pr_debug("Got sync event from vCPU %d\n", vcpu_idx);
0097         vcpu_last_completed_iteration[vcpu_idx] = current_iteration;
0098         pr_debug("vCPU %d updated last completed iteration to %d\n",
0099              vcpu_idx, vcpu_last_completed_iteration[vcpu_idx]);
0100 
0101         if (current_iteration) {
0102             pages_count += vcpu_args->pages;
0103             total = timespec_add(total, ts_diff);
0104             pr_debug("vCPU %d iteration %d dirty memory time: %ld.%.9lds\n",
0105                 vcpu_idx, current_iteration, ts_diff.tv_sec,
0106                 ts_diff.tv_nsec);
0107         } else {
0108             pr_debug("vCPU %d iteration %d populate memory time: %ld.%.9lds\n",
0109                 vcpu_idx, current_iteration, ts_diff.tv_sec,
0110                 ts_diff.tv_nsec);
0111         }
0112 
0113         /*
0114          * Keep running the guest while dirty logging is being disabled
0115          * (iteration is negative) so that vCPUs are accessing memory
0116          * for the entire duration of zapping collapsible SPTEs.
0117          */
0118         while (current_iteration == READ_ONCE(iteration) &&
0119                READ_ONCE(iteration) >= 0 && !READ_ONCE(host_quit)) {}
0120     }
0121 
0122     avg = timespec_div(total, vcpu_last_completed_iteration[vcpu_idx]);
0123     pr_debug("\nvCPU %d dirtied 0x%lx pages over %d iterations in %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
0124         vcpu_idx, pages_count, vcpu_last_completed_iteration[vcpu_idx],
0125         total.tv_sec, total.tv_nsec, avg.tv_sec, avg.tv_nsec);
0126 }
0127 
0128 struct test_params {
0129     unsigned long iterations;
0130     uint64_t phys_offset;
0131     int wr_fract;
0132     bool partition_vcpu_memory_access;
0133     enum vm_mem_backing_src_type backing_src;
0134     int slots;
0135 };
0136 
0137 static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable)
0138 {
0139     int i;
0140 
0141     for (i = 0; i < slots; i++) {
0142         int slot = PERF_TEST_MEM_SLOT_INDEX + i;
0143         int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0;
0144 
0145         vm_mem_region_set_flags(vm, slot, flags);
0146     }
0147 }
0148 
0149 static inline void enable_dirty_logging(struct kvm_vm *vm, int slots)
0150 {
0151     toggle_dirty_logging(vm, slots, true);
0152 }
0153 
0154 static inline void disable_dirty_logging(struct kvm_vm *vm, int slots)
0155 {
0156     toggle_dirty_logging(vm, slots, false);
0157 }
0158 
0159 static void get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)
0160 {
0161     int i;
0162 
0163     for (i = 0; i < slots; i++) {
0164         int slot = PERF_TEST_MEM_SLOT_INDEX + i;
0165 
0166         kvm_vm_get_dirty_log(vm, slot, bitmaps[i]);
0167     }
0168 }
0169 
0170 static void clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
0171                 int slots, uint64_t pages_per_slot)
0172 {
0173     int i;
0174 
0175     for (i = 0; i < slots; i++) {
0176         int slot = PERF_TEST_MEM_SLOT_INDEX + i;
0177 
0178         kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot);
0179     }
0180 }
0181 
0182 static unsigned long **alloc_bitmaps(int slots, uint64_t pages_per_slot)
0183 {
0184     unsigned long **bitmaps;
0185     int i;
0186 
0187     bitmaps = malloc(slots * sizeof(bitmaps[0]));
0188     TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array.");
0189 
0190     for (i = 0; i < slots; i++) {
0191         bitmaps[i] = bitmap_zalloc(pages_per_slot);
0192         TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap.");
0193     }
0194 
0195     return bitmaps;
0196 }
0197 
0198 static void free_bitmaps(unsigned long *bitmaps[], int slots)
0199 {
0200     int i;
0201 
0202     for (i = 0; i < slots; i++)
0203         free(bitmaps[i]);
0204 
0205     free(bitmaps);
0206 }
0207 
0208 static void run_test(enum vm_guest_mode mode, void *arg)
0209 {
0210     struct test_params *p = arg;
0211     struct kvm_vm *vm;
0212     unsigned long **bitmaps;
0213     uint64_t guest_num_pages;
0214     uint64_t host_num_pages;
0215     uint64_t pages_per_slot;
0216     struct timespec start;
0217     struct timespec ts_diff;
0218     struct timespec get_dirty_log_total = (struct timespec){0};
0219     struct timespec vcpu_dirty_total = (struct timespec){0};
0220     struct timespec avg;
0221     struct timespec clear_dirty_log_total = (struct timespec){0};
0222     int i;
0223 
0224     vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
0225                  p->slots, p->backing_src,
0226                  p->partition_vcpu_memory_access);
0227 
0228     perf_test_set_wr_fract(vm, p->wr_fract);
0229 
0230     guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift;
0231     guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
0232     host_num_pages = vm_num_host_pages(mode, guest_num_pages);
0233     pages_per_slot = host_num_pages / p->slots;
0234 
0235     bitmaps = alloc_bitmaps(p->slots, pages_per_slot);
0236 
0237     if (dirty_log_manual_caps)
0238         vm_enable_cap(vm, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2,
0239                   dirty_log_manual_caps);
0240 
0241     arch_setup_vm(vm, nr_vcpus);
0242 
0243     /* Start the iterations */
0244     iteration = 0;
0245     host_quit = false;
0246 
0247     clock_gettime(CLOCK_MONOTONIC, &start);
0248     for (i = 0; i < nr_vcpus; i++)
0249         vcpu_last_completed_iteration[i] = -1;
0250 
0251     perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker);
0252 
0253     /* Allow the vCPUs to populate memory */
0254     pr_debug("Starting iteration %d - Populating\n", iteration);
0255     for (i = 0; i < nr_vcpus; i++) {
0256         while (READ_ONCE(vcpu_last_completed_iteration[i]) !=
0257                iteration)
0258             ;
0259     }
0260 
0261     ts_diff = timespec_elapsed(start);
0262     pr_info("Populate memory time: %ld.%.9lds\n",
0263         ts_diff.tv_sec, ts_diff.tv_nsec);
0264 
0265     /* Enable dirty logging */
0266     clock_gettime(CLOCK_MONOTONIC, &start);
0267     enable_dirty_logging(vm, p->slots);
0268     ts_diff = timespec_elapsed(start);
0269     pr_info("Enabling dirty logging time: %ld.%.9lds\n\n",
0270         ts_diff.tv_sec, ts_diff.tv_nsec);
0271 
0272     while (iteration < p->iterations) {
0273         /*
0274          * Incrementing the iteration number will start the vCPUs
0275          * dirtying memory again.
0276          */
0277         clock_gettime(CLOCK_MONOTONIC, &start);
0278         iteration++;
0279 
0280         pr_debug("Starting iteration %d\n", iteration);
0281         for (i = 0; i < nr_vcpus; i++) {
0282             while (READ_ONCE(vcpu_last_completed_iteration[i])
0283                    != iteration)
0284                 ;
0285         }
0286 
0287         ts_diff = timespec_elapsed(start);
0288         vcpu_dirty_total = timespec_add(vcpu_dirty_total, ts_diff);
0289         pr_info("Iteration %d dirty memory time: %ld.%.9lds\n",
0290             iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
0291 
0292         clock_gettime(CLOCK_MONOTONIC, &start);
0293         get_dirty_log(vm, bitmaps, p->slots);
0294         ts_diff = timespec_elapsed(start);
0295         get_dirty_log_total = timespec_add(get_dirty_log_total,
0296                            ts_diff);
0297         pr_info("Iteration %d get dirty log time: %ld.%.9lds\n",
0298             iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
0299 
0300         if (dirty_log_manual_caps) {
0301             clock_gettime(CLOCK_MONOTONIC, &start);
0302             clear_dirty_log(vm, bitmaps, p->slots, pages_per_slot);
0303             ts_diff = timespec_elapsed(start);
0304             clear_dirty_log_total = timespec_add(clear_dirty_log_total,
0305                                  ts_diff);
0306             pr_info("Iteration %d clear dirty log time: %ld.%.9lds\n",
0307                 iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
0308         }
0309     }
0310 
0311     /*
0312      * Run vCPUs while dirty logging is being disabled to stress disabling
0313      * in terms of both performance and correctness.  Opt-in via command
0314      * line as this significantly increases time to disable dirty logging.
0315      */
0316     if (run_vcpus_while_disabling_dirty_logging)
0317         WRITE_ONCE(iteration, -1);
0318 
0319     /* Disable dirty logging */
0320     clock_gettime(CLOCK_MONOTONIC, &start);
0321     disable_dirty_logging(vm, p->slots);
0322     ts_diff = timespec_elapsed(start);
0323     pr_info("Disabling dirty logging time: %ld.%.9lds\n",
0324         ts_diff.tv_sec, ts_diff.tv_nsec);
0325 
0326     /*
0327      * Tell the vCPU threads to quit.  No need to manually check that vCPUs
0328      * have stopped running after disabling dirty logging, the join will
0329      * wait for them to exit.
0330      */
0331     host_quit = true;
0332     perf_test_join_vcpu_threads(nr_vcpus);
0333 
0334     avg = timespec_div(get_dirty_log_total, p->iterations);
0335     pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
0336         p->iterations, get_dirty_log_total.tv_sec,
0337         get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);
0338 
0339     if (dirty_log_manual_caps) {
0340         avg = timespec_div(clear_dirty_log_total, p->iterations);
0341         pr_info("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
0342             p->iterations, clear_dirty_log_total.tv_sec,
0343             clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);
0344     }
0345 
0346     free_bitmaps(bitmaps, p->slots);
0347     arch_cleanup_vm(vm);
0348     perf_test_destroy_vm(vm);
0349 }
0350 
0351 static void help(char *name)
0352 {
0353     puts("");
0354     printf("usage: %s [-h] [-i iterations] [-p offset] [-g] "
0355            "[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-s mem type]"
0356            "[-x memslots]\n", name);
0357     puts("");
0358     printf(" -i: specify iteration counts (default: %"PRIu64")\n",
0359            TEST_HOST_LOOP_N);
0360     printf(" -g: Do not enable KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. This\n"
0361            "     makes KVM_GET_DIRTY_LOG clear the dirty log (i.e.\n"
0362            "     KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE is not enabled)\n"
0363            "     and writes will be tracked as soon as dirty logging is\n"
0364            "     enabled on the memslot (i.e. KVM_DIRTY_LOG_INITIALLY_SET\n"
0365            "     is not enabled).\n");
0366     printf(" -p: specify guest physical test memory offset\n"
0367            "     Warning: a low offset can conflict with the loaded test code.\n");
0368     guest_modes_help();
0369     printf(" -n: Run the vCPUs in nested mode (L2)\n");
0370     printf(" -e: Run vCPUs while dirty logging is being disabled.  This\n"
0371            "     can significantly increase runtime, especially if there\n"
0372            "     isn't a dedicated pCPU for the main thread.\n");
0373     printf(" -b: specify the size of the memory region which should be\n"
0374            "     dirtied by each vCPU. e.g. 10M or 3G.\n"
0375            "     (default: 1G)\n");
0376     printf(" -f: specify the fraction of pages which should be written to\n"
0377            "     as opposed to simply read, in the form\n"
0378            "     1/<fraction of pages to write>.\n"
0379            "     (default: 1 i.e. all pages are written to.)\n");
0380     printf(" -v: specify the number of vCPUs to run.\n");
0381     printf(" -o: Overlap guest memory accesses instead of partitioning\n"
0382            "     them into a separate region of memory for each vCPU.\n");
0383     backing_src_help("-s");
0384     printf(" -x: Split the memory region into this number of memslots.\n"
0385            "     (default: 1)\n");
0386     puts("");
0387     exit(0);
0388 }
0389 
0390 int main(int argc, char *argv[])
0391 {
0392     int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
0393     struct test_params p = {
0394         .iterations = TEST_HOST_LOOP_N,
0395         .wr_fract = 1,
0396         .partition_vcpu_memory_access = true,
0397         .backing_src = DEFAULT_VM_MEM_SRC,
0398         .slots = 1,
0399     };
0400     int opt;
0401 
0402     dirty_log_manual_caps =
0403         kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
0404     dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
0405                   KVM_DIRTY_LOG_INITIALLY_SET);
0406 
0407     guest_modes_append_default();
0408 
0409     while ((opt = getopt(argc, argv, "eghi:p:m:nb:f:v:os:x:")) != -1) {
0410         switch (opt) {
0411         case 'e':
0412             /* 'e' is for evil. */
0413             run_vcpus_while_disabling_dirty_logging = true;
0414         case 'g':
0415             dirty_log_manual_caps = 0;
0416             break;
0417         case 'i':
0418             p.iterations = atoi(optarg);
0419             break;
0420         case 'p':
0421             p.phys_offset = strtoull(optarg, NULL, 0);
0422             break;
0423         case 'm':
0424             guest_modes_cmdline(optarg);
0425             break;
0426         case 'n':
0427             perf_test_args.nested = true;
0428             break;
0429         case 'b':
0430             guest_percpu_mem_size = parse_size(optarg);
0431             break;
0432         case 'f':
0433             p.wr_fract = atoi(optarg);
0434             TEST_ASSERT(p.wr_fract >= 1,
0435                     "Write fraction cannot be less than one");
0436             break;
0437         case 'v':
0438             nr_vcpus = atoi(optarg);
0439             TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
0440                     "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
0441             break;
0442         case 'o':
0443             p.partition_vcpu_memory_access = false;
0444             break;
0445         case 's':
0446             p.backing_src = parse_backing_src_type(optarg);
0447             break;
0448         case 'x':
0449             p.slots = atoi(optarg);
0450             break;
0451         case 'h':
0452         default:
0453             help(argv[0]);
0454             break;
0455         }
0456     }
0457 
0458     TEST_ASSERT(p.iterations >= 2, "The test should have at least two iterations");
0459 
0460     pr_info("Test iterations: %"PRIu64"\n", p.iterations);
0461 
0462     for_each_guest_mode(run_test, &p);
0463 
0464     return 0;
0465 }