dirty_log_perf_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <pthread.h>
  14. #include <linux/bitmap.h>
  15. #include "kvm_util.h"
  16. #include "test_util.h"
  17. #include "memstress.h"
  18. #include "guest_modes.h"
  19. #include "ucall_common.h"
  20. /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
  21. #define TEST_HOST_LOOP_N 2UL
  22. static int nr_vcpus = 1;
  23. static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
  24. static bool run_vcpus_while_disabling_dirty_logging;
  25. /* Host variables */
  26. static u64 dirty_log_manual_caps;
  27. static bool host_quit;
  28. static int iteration;
  29. static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
  30. static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
  31. {
  32. struct kvm_vcpu *vcpu = vcpu_args->vcpu;
  33. int vcpu_idx = vcpu_args->vcpu_idx;
  34. uint64_t pages_count = 0;
  35. struct kvm_run *run;
  36. struct timespec start;
  37. struct timespec ts_diff;
  38. struct timespec total = (struct timespec){0};
  39. struct timespec avg;
  40. int ret;
  41. run = vcpu->run;
  42. while (!READ_ONCE(host_quit)) {
  43. int current_iteration = READ_ONCE(iteration);
  44. clock_gettime(CLOCK_MONOTONIC, &start);
  45. ret = _vcpu_run(vcpu);
  46. ts_diff = timespec_elapsed(start);
  47. TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);
  48. TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC,
  49. "Invalid guest sync status: exit_reason=%s",
  50. exit_reason_str(run->exit_reason));
  51. pr_debug("Got sync event from vCPU %d\n", vcpu_idx);
  52. vcpu_last_completed_iteration[vcpu_idx] = current_iteration;
  53. pr_debug("vCPU %d updated last completed iteration to %d\n",
  54. vcpu_idx, vcpu_last_completed_iteration[vcpu_idx]);
  55. if (current_iteration) {
  56. pages_count += vcpu_args->pages;
  57. total = timespec_add(total, ts_diff);
  58. pr_debug("vCPU %d iteration %d dirty memory time: %ld.%.9lds\n",
  59. vcpu_idx, current_iteration, ts_diff.tv_sec,
  60. ts_diff.tv_nsec);
  61. } else {
  62. pr_debug("vCPU %d iteration %d populate memory time: %ld.%.9lds\n",
  63. vcpu_idx, current_iteration, ts_diff.tv_sec,
  64. ts_diff.tv_nsec);
  65. }
  66. /*
  67. * Keep running the guest while dirty logging is being disabled
  68. * (iteration is negative) so that vCPUs are accessing memory
  69. * for the entire duration of zapping collapsible SPTEs.
  70. */
  71. while (current_iteration == READ_ONCE(iteration) &&
  72. READ_ONCE(iteration) >= 0 && !READ_ONCE(host_quit)) {}
  73. }
  74. avg = timespec_div(total, vcpu_last_completed_iteration[vcpu_idx]);
  75. pr_debug("\nvCPU %d dirtied 0x%lx pages over %d iterations in %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
  76. vcpu_idx, pages_count, vcpu_last_completed_iteration[vcpu_idx],
  77. total.tv_sec, total.tv_nsec, avg.tv_sec, avg.tv_nsec);
  78. }
  79. struct test_params {
  80. unsigned long iterations;
  81. uint64_t phys_offset;
  82. bool partition_vcpu_memory_access;
  83. enum vm_mem_backing_src_type backing_src;
  84. int slots;
  85. uint32_t write_percent;
  86. bool random_access;
  87. };
  88. static void run_test(enum vm_guest_mode mode, void *arg)
  89. {
  90. struct test_params *p = arg;
  91. struct kvm_vm *vm;
  92. unsigned long **bitmaps;
  93. uint64_t guest_num_pages;
  94. uint64_t host_num_pages;
  95. uint64_t pages_per_slot;
  96. struct timespec start;
  97. struct timespec ts_diff;
  98. struct timespec get_dirty_log_total = (struct timespec){0};
  99. struct timespec vcpu_dirty_total = (struct timespec){0};
  100. struct timespec avg;
  101. struct timespec clear_dirty_log_total = (struct timespec){0};
  102. int i;
  103. vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
  104. p->slots, p->backing_src,
  105. p->partition_vcpu_memory_access);
  106. memstress_set_write_percent(vm, p->write_percent);
  107. guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift;
  108. guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
  109. host_num_pages = vm_num_host_pages(mode, guest_num_pages);
  110. pages_per_slot = host_num_pages / p->slots;
  111. bitmaps = memstress_alloc_bitmaps(p->slots, pages_per_slot);
  112. if (dirty_log_manual_caps)
  113. vm_enable_cap(vm, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2,
  114. dirty_log_manual_caps);
  115. /* Start the iterations */
  116. iteration = 0;
  117. host_quit = false;
  118. clock_gettime(CLOCK_MONOTONIC, &start);
  119. for (i = 0; i < nr_vcpus; i++)
  120. vcpu_last_completed_iteration[i] = -1;
  121. /*
  122. * Use 100% writes during the population phase to ensure all
  123. * memory is actually populated and not just mapped to the zero
  124. * page. The prevents expensive copy-on-write faults from
  125. * occurring during the dirty memory iterations below, which
  126. * would pollute the performance results.
  127. */
  128. memstress_set_write_percent(vm, 100);
  129. memstress_set_random_access(vm, false);
  130. memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
  131. /* Allow the vCPUs to populate memory */
  132. pr_debug("Starting iteration %d - Populating\n", iteration);
  133. for (i = 0; i < nr_vcpus; i++) {
  134. while (READ_ONCE(vcpu_last_completed_iteration[i]) !=
  135. iteration)
  136. ;
  137. }
  138. ts_diff = timespec_elapsed(start);
  139. pr_info("Populate memory time: %ld.%.9lds\n",
  140. ts_diff.tv_sec, ts_diff.tv_nsec);
  141. /* Enable dirty logging */
  142. clock_gettime(CLOCK_MONOTONIC, &start);
  143. memstress_enable_dirty_logging(vm, p->slots);
  144. ts_diff = timespec_elapsed(start);
  145. pr_info("Enabling dirty logging time: %ld.%.9lds\n\n",
  146. ts_diff.tv_sec, ts_diff.tv_nsec);
  147. memstress_set_write_percent(vm, p->write_percent);
  148. memstress_set_random_access(vm, p->random_access);
  149. while (iteration < p->iterations) {
  150. /*
  151. * Incrementing the iteration number will start the vCPUs
  152. * dirtying memory again.
  153. */
  154. clock_gettime(CLOCK_MONOTONIC, &start);
  155. iteration++;
  156. pr_debug("Starting iteration %d\n", iteration);
  157. for (i = 0; i < nr_vcpus; i++) {
  158. while (READ_ONCE(vcpu_last_completed_iteration[i])
  159. != iteration)
  160. ;
  161. }
  162. ts_diff = timespec_elapsed(start);
  163. vcpu_dirty_total = timespec_add(vcpu_dirty_total, ts_diff);
  164. pr_info("Iteration %d dirty memory time: %ld.%.9lds\n",
  165. iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
  166. clock_gettime(CLOCK_MONOTONIC, &start);
  167. memstress_get_dirty_log(vm, bitmaps, p->slots);
  168. ts_diff = timespec_elapsed(start);
  169. get_dirty_log_total = timespec_add(get_dirty_log_total,
  170. ts_diff);
  171. pr_info("Iteration %d get dirty log time: %ld.%.9lds\n",
  172. iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
  173. if (dirty_log_manual_caps) {
  174. clock_gettime(CLOCK_MONOTONIC, &start);
  175. memstress_clear_dirty_log(vm, bitmaps, p->slots,
  176. pages_per_slot);
  177. ts_diff = timespec_elapsed(start);
  178. clear_dirty_log_total = timespec_add(clear_dirty_log_total,
  179. ts_diff);
  180. pr_info("Iteration %d clear dirty log time: %ld.%.9lds\n",
  181. iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
  182. }
  183. }
  184. /*
  185. * Run vCPUs while dirty logging is being disabled to stress disabling
  186. * in terms of both performance and correctness. Opt-in via command
  187. * line as this significantly increases time to disable dirty logging.
  188. */
  189. if (run_vcpus_while_disabling_dirty_logging)
  190. WRITE_ONCE(iteration, -1);
  191. /* Disable dirty logging */
  192. clock_gettime(CLOCK_MONOTONIC, &start);
  193. memstress_disable_dirty_logging(vm, p->slots);
  194. ts_diff = timespec_elapsed(start);
  195. pr_info("Disabling dirty logging time: %ld.%.9lds\n",
  196. ts_diff.tv_sec, ts_diff.tv_nsec);
  197. /*
  198. * Tell the vCPU threads to quit. No need to manually check that vCPUs
  199. * have stopped running after disabling dirty logging, the join will
  200. * wait for them to exit.
  201. */
  202. host_quit = true;
  203. memstress_join_vcpu_threads(nr_vcpus);
  204. avg = timespec_div(get_dirty_log_total, p->iterations);
  205. pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
  206. p->iterations, get_dirty_log_total.tv_sec,
  207. get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);
  208. if (dirty_log_manual_caps) {
  209. avg = timespec_div(clear_dirty_log_total, p->iterations);
  210. pr_info("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n",
  211. p->iterations, clear_dirty_log_total.tv_sec,
  212. clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);
  213. }
  214. memstress_free_bitmaps(bitmaps, p->slots);
  215. memstress_destroy_vm(vm);
  216. }
  217. static void help(char *name)
  218. {
  219. puts("");
  220. printf("usage: %s [-h] [-a] [-i iterations] [-p offset] [-g] "
  221. "[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-r random seed ] [-s mem type]"
  222. "[-x memslots] [-w percentage] [-c physical cpus to run test on]\n", name);
  223. puts("");
  224. printf(" -a: access memory randomly rather than in order.\n");
  225. printf(" -i: specify iteration counts (default: %"PRIu64")\n",
  226. TEST_HOST_LOOP_N);
  227. printf(" -g: Do not enable KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. This\n"
  228. " makes KVM_GET_DIRTY_LOG clear the dirty log (i.e.\n"
  229. " KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE is not enabled)\n"
  230. " and writes will be tracked as soon as dirty logging is\n"
  231. " enabled on the memslot (i.e. KVM_DIRTY_LOG_INITIALLY_SET\n"
  232. " is not enabled).\n");
  233. printf(" -p: specify guest physical test memory offset\n"
  234. " Warning: a low offset can conflict with the loaded test code.\n");
  235. guest_modes_help();
  236. printf(" -n: Run the vCPUs in nested mode (L2)\n");
  237. printf(" -e: Run vCPUs while dirty logging is being disabled. This\n"
  238. " can significantly increase runtime, especially if there\n"
  239. " isn't a dedicated pCPU for the main thread.\n");
  240. printf(" -b: specify the size of the memory region which should be\n"
  241. " dirtied by each vCPU. e.g. 10M or 3G.\n"
  242. " (default: 1G)\n");
  243. printf(" -v: specify the number of vCPUs to run.\n");
  244. printf(" -o: Overlap guest memory accesses instead of partitioning\n"
  245. " them into a separate region of memory for each vCPU.\n");
  246. printf(" -r: specify the starting random seed.\n");
  247. backing_src_help("-s");
  248. printf(" -x: Split the memory region into this number of memslots.\n"
  249. " (default: 1)\n");
  250. printf(" -w: specify the percentage of pages which should be written to\n"
  251. " as an integer from 0-100 inclusive. This is probabilistic,\n"
  252. " so -w X means each page has an X%% chance of writing\n"
  253. " and a (100-X)%% chance of reading.\n"
  254. " (default: 100 i.e. all pages are written to.)\n");
  255. kvm_print_vcpu_pinning_help();
  256. puts("");
  257. exit(0);
  258. }
  259. int main(int argc, char *argv[])
  260. {
  261. int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
  262. const char *pcpu_list = NULL;
  263. struct test_params p = {
  264. .iterations = TEST_HOST_LOOP_N,
  265. .partition_vcpu_memory_access = true,
  266. .backing_src = DEFAULT_VM_MEM_SRC,
  267. .slots = 1,
  268. .write_percent = 100,
  269. };
  270. int opt;
  271. /* Override the seed to be deterministic by default. */
  272. guest_random_seed = 1;
  273. dirty_log_manual_caps =
  274. kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
  275. dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
  276. KVM_DIRTY_LOG_INITIALLY_SET);
  277. guest_modes_append_default();
  278. while ((opt = getopt(argc, argv, "ab:c:eghi:m:nop:r:s:v:x:w:")) != -1) {
  279. switch (opt) {
  280. case 'a':
  281. p.random_access = true;
  282. break;
  283. case 'b':
  284. guest_percpu_mem_size = parse_size(optarg);
  285. break;
  286. case 'c':
  287. pcpu_list = optarg;
  288. break;
  289. case 'e':
  290. /* 'e' is for evil. */
  291. run_vcpus_while_disabling_dirty_logging = true;
  292. break;
  293. case 'g':
  294. dirty_log_manual_caps = 0;
  295. break;
  296. case 'h':
  297. help(argv[0]);
  298. break;
  299. case 'i':
  300. p.iterations = atoi_positive("Number of iterations", optarg);
  301. break;
  302. case 'm':
  303. guest_modes_cmdline(optarg);
  304. break;
  305. case 'n':
  306. memstress_args.nested = true;
  307. break;
  308. case 'o':
  309. p.partition_vcpu_memory_access = false;
  310. break;
  311. case 'p':
  312. p.phys_offset = strtoull(optarg, NULL, 0);
  313. break;
  314. case 'r':
  315. guest_random_seed = atoi_positive("Random seed", optarg);
  316. break;
  317. case 's':
  318. p.backing_src = parse_backing_src_type(optarg);
  319. break;
  320. case 'v':
  321. nr_vcpus = atoi_positive("Number of vCPUs", optarg);
  322. TEST_ASSERT(nr_vcpus <= max_vcpus,
  323. "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
  324. break;
  325. case 'w':
  326. p.write_percent = atoi_non_negative("Write percentage", optarg);
  327. TEST_ASSERT(p.write_percent <= 100,
  328. "Write percentage must be between 0 and 100");
  329. break;
  330. case 'x':
  331. p.slots = atoi_positive("Number of slots", optarg);
  332. break;
  333. default:
  334. help(argv[0]);
  335. break;
  336. }
  337. }
  338. if (pcpu_list) {
  339. kvm_parse_vcpu_pinning(pcpu_list, memstress_args.vcpu_to_pcpu,
  340. nr_vcpus);
  341. memstress_args.pin_vcpus = true;
  342. }
  343. TEST_ASSERT(p.iterations >= 2, "The test should have at least two iterations");
  344. pr_info("Test iterations: %"PRIu64"\n", p.iterations);
  345. for_each_guest_mode(run_test, &p);
  346. return 0;
  347. }