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