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