Skip to content

Commit 1eafbd2

Browse files
Ben Gardonbonzini
authored andcommitted
KVM: selftests: Simplify demand_paging_test with timespec_diff_now
Add a helper function to get the current time and return the time since a given start time. Use that function to simplify the timekeeping in the demand paging test. This series was tested by running the following invocations on an Intel Skylake machine: dirty_log_perf_test -b 20m -i 100 -v 64 dirty_log_perf_test -b 20g -i 5 -v 4 dirty_log_perf_test -b 4g -i 5 -v 32 demand_paging_test -b 20m -v 64 demand_paging_test -b 20g -v 4 demand_paging_test -b 4g -v 32 All behaved as expected. Signed-off-by: Ben Gardon <bgardon@google.com> Message-Id: <20201027233733.1484855-4-bgardon@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 2fe5149 commit 1eafbd2

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

tools/testing/selftests/kvm/demand_paging_test.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ static void *vcpu_worker(void *data)
5050
int vcpu_id = vcpu_args->vcpu_id;
5151
struct kvm_vm *vm = perf_test_args.vm;
5252
struct kvm_run *run;
53-
struct timespec start, end, ts_diff;
53+
struct timespec start;
54+
struct timespec ts_diff;
5455

5556
vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
5657
run = vcpu_state(vm, vcpu_id);
@@ -66,8 +67,7 @@ static void *vcpu_worker(void *data)
6667
exit_reason_str(run->exit_reason));
6768
}
6869

69-
clock_gettime(CLOCK_MONOTONIC, &end);
70-
ts_diff = timespec_sub(end, start);
70+
ts_diff = timespec_diff_now(start);
7171
PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_id,
7272
ts_diff.tv_sec, ts_diff.tv_nsec);
7373

@@ -78,7 +78,7 @@ static int handle_uffd_page_request(int uffd, uint64_t addr)
7878
{
7979
pid_t tid;
8080
struct timespec start;
81-
struct timespec end;
81+
struct timespec ts_diff;
8282
struct uffdio_copy copy;
8383
int r;
8484

@@ -98,10 +98,10 @@ static int handle_uffd_page_request(int uffd, uint64_t addr)
9898
return r;
9999
}
100100

101-
clock_gettime(CLOCK_MONOTONIC, &end);
101+
ts_diff = timespec_diff_now(start);
102102

103103
PER_PAGE_DEBUG("UFFDIO_COPY %d \t%ld ns\n", tid,
104-
timespec_to_ns(timespec_sub(end, start)));
104+
timespec_to_ns(ts_diff));
105105
PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
106106
perf_test_args.host_page_size, addr, tid);
107107

@@ -123,7 +123,8 @@ static void *uffd_handler_thread_fn(void *arg)
123123
int pipefd = uffd_args->pipefd;
124124
useconds_t delay = uffd_args->delay;
125125
int64_t pages = 0;
126-
struct timespec start, end, ts_diff;
126+
struct timespec start;
127+
struct timespec ts_diff;
127128

128129
clock_gettime(CLOCK_MONOTONIC, &start);
129130
while (!quit_uffd_thread) {
@@ -192,8 +193,7 @@ static void *uffd_handler_thread_fn(void *arg)
192193
pages++;
193194
}
194195

195-
clock_gettime(CLOCK_MONOTONIC, &end);
196-
ts_diff = timespec_sub(end, start);
196+
ts_diff = timespec_diff_now(start);
197197
PER_VCPU_DEBUG("userfaulted %ld pages over %ld.%.9lds. (%f/sec)\n",
198198
pages, ts_diff.tv_sec, ts_diff.tv_nsec,
199199
pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
@@ -257,7 +257,8 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
257257
pthread_t *vcpu_threads;
258258
pthread_t *uffd_handler_threads = NULL;
259259
struct uffd_handler_args *uffd_args = NULL;
260-
struct timespec start, end, ts_diff;
260+
struct timespec start;
261+
struct timespec ts_diff;
261262
int *pipefds = NULL;
262263
struct kvm_vm *vm;
263264
int vcpu_id;
@@ -335,9 +336,9 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
335336
PER_VCPU_DEBUG("Joined thread for vCPU %d\n", vcpu_id);
336337
}
337338

338-
pr_info("All vCPU threads joined\n");
339+
ts_diff = timespec_diff_now(start);
339340

340-
clock_gettime(CLOCK_MONOTONIC, &end);
341+
pr_info("All vCPU threads joined\n");
341342

342343
if (use_uffd) {
343344
char c;
@@ -351,7 +352,6 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
351352
}
352353
}
353354

354-
ts_diff = timespec_sub(end, start);
355355
pr_info("Total guest execution time: %ld.%.9lds\n",
356356
ts_diff.tv_sec, ts_diff.tv_nsec);
357357
pr_info("Overall demand paging rate: %f pgs/sec\n",

tools/testing/selftests/kvm/include/test_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ int64_t timespec_to_ns(struct timespec ts);
6464
struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
6565
struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
6666
struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
67+
struct timespec timespec_diff_now(struct timespec start);
6768

6869
#endif /* SELFTEST_KVM_TEST_UTIL_H */

tools/testing/selftests/kvm/lib/test_util.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
*
55
* Copyright (C) 2020, Google LLC.
66
*/
7-
#include <stdlib.h>
7+
8+
#include <assert.h>
89
#include <ctype.h>
910
#include <limits.h>
10-
#include <assert.h>
11+
#include <stdlib.h>
12+
#include <time.h>
13+
1114
#include "test_util.h"
1215

1316
/*
@@ -81,6 +84,14 @@ struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
8184
return timespec_add_ns((struct timespec){0}, ns1 - ns2);
8285
}
8386

87+
struct timespec timespec_diff_now(struct timespec start)
88+
{
89+
struct timespec end;
90+
91+
clock_gettime(CLOCK_MONOTONIC, &end);
92+
return timespec_sub(end, start);
93+
}
94+
8495
void print_skip(const char *fmt, ...)
8596
{
8697
va_list ap;

0 commit comments

Comments
 (0)