Skip to content

Commit bfcaa84

Browse files
Andrew Jonesbonzini
authored andcommitted
KVM: selftests: Rework timespec functions and usage
The steal_time test's timespec stop condition was wrong and should have used the timespec functions instead to avoid being wrong, but timespec_diff had a strange interface. Rework all the timespec API and its use. Signed-off-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent cf6c26e commit bfcaa84

4 files changed

Lines changed: 35 additions & 44 deletions

File tree

‎tools/testing/selftests/kvm/demand_paging_test.c‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ static void *vcpu_worker(void *data)
117117
struct kvm_vm *vm = args->vm;
118118
int vcpu_id = args->vcpu_id;
119119
struct kvm_run *run;
120-
struct timespec start;
121-
struct timespec end;
120+
struct timespec start, end, ts_diff;
122121

123122
vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
124123
run = vcpu_state(vm, vcpu_id);
@@ -135,9 +134,9 @@ static void *vcpu_worker(void *data)
135134
}
136135

137136
clock_gettime(CLOCK_MONOTONIC, &end);
138-
PER_VCPU_DEBUG("vCPU %d execution time: %lld.%.9lds\n", vcpu_id,
139-
(long long)(timespec_diff(start, end).tv_sec),
140-
timespec_diff(start, end).tv_nsec);
137+
ts_diff = timespec_sub(end, start);
138+
PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_id,
139+
ts_diff.tv_sec, ts_diff.tv_nsec);
141140

142141
return NULL;
143142
}
@@ -201,8 +200,8 @@ static int handle_uffd_page_request(int uffd, uint64_t addr)
201200

202201
clock_gettime(CLOCK_MONOTONIC, &end);
203202

204-
PER_PAGE_DEBUG("UFFDIO_COPY %d \t%lld ns\n", tid,
205-
(long long)timespec_to_ns(timespec_diff(start, end)));
203+
PER_PAGE_DEBUG("UFFDIO_COPY %d \t%ld ns\n", tid,
204+
timespec_to_ns(timespec_sub(end, start)));
206205
PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
207206
host_page_size, addr, tid);
208207

@@ -224,8 +223,7 @@ static void *uffd_handler_thread_fn(void *arg)
224223
int pipefd = uffd_args->pipefd;
225224
useconds_t delay = uffd_args->delay;
226225
int64_t pages = 0;
227-
struct timespec start;
228-
struct timespec end;
226+
struct timespec start, end, ts_diff;
229227

230228
clock_gettime(CLOCK_MONOTONIC, &start);
231229
while (!quit_uffd_thread) {
@@ -295,11 +293,10 @@ static void *uffd_handler_thread_fn(void *arg)
295293
}
296294

297295
clock_gettime(CLOCK_MONOTONIC, &end);
298-
PER_VCPU_DEBUG("userfaulted %ld pages over %lld.%.9lds. (%f/sec)\n",
299-
pages, (long long)(timespec_diff(start, end).tv_sec),
300-
timespec_diff(start, end).tv_nsec, pages /
301-
((double)timespec_diff(start, end).tv_sec +
302-
(double)timespec_diff(start, end).tv_nsec / 100000000.0));
296+
ts_diff = timespec_sub(end, start);
297+
PER_VCPU_DEBUG("userfaulted %ld pages over %ld.%.9lds. (%f/sec)\n",
298+
pages, ts_diff.tv_sec, ts_diff.tv_nsec,
299+
pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
303300

304301
return NULL;
305302
}
@@ -360,13 +357,12 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
360357
pthread_t *vcpu_threads;
361358
pthread_t *uffd_handler_threads = NULL;
362359
struct uffd_handler_args *uffd_args = NULL;
360+
struct timespec start, end, ts_diff;
363361
int *pipefds = NULL;
364362
struct kvm_vm *vm;
365363
uint64_t guest_num_pages;
366364
int vcpu_id;
367365
int r;
368-
struct timespec start;
369-
struct timespec end;
370366

371367
vm = create_vm(mode, vcpus, vcpu_memory_bytes);
372368

@@ -514,12 +510,11 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
514510
}
515511
}
516512

517-
pr_info("Total guest execution time: %lld.%.9lds\n",
518-
(long long)(timespec_diff(start, end).tv_sec),
519-
timespec_diff(start, end).tv_nsec);
513+
ts_diff = timespec_sub(end, start);
514+
pr_info("Total guest execution time: %ld.%.9lds\n",
515+
ts_diff.tv_sec, ts_diff.tv_nsec);
520516
pr_info("Overall demand paging rate: %f pgs/sec\n",
521-
guest_num_pages / ((double)timespec_diff(start, end).tv_sec +
522-
(double)timespec_diff(start, end).tv_nsec / 100000000.0));
517+
guest_num_pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
523518

524519
ucall_uninit(vm);
525520
kvm_vm_free(vm);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void test_assert(bool exp, const char *exp_str,
6161
size_t parse_size(const char *size);
6262

6363
int64_t timespec_to_ns(struct timespec ts);
64-
struct timespec timespec_diff(struct timespec start, struct timespec end);
6564
struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
65+
struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
66+
struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
6667

6768
#endif /* SELFTEST_KVM_TEST_UTIL_H */

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,31 @@ int64_t timespec_to_ns(struct timespec ts)
5656
return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
5757
}
5858

59-
struct timespec timespec_diff(struct timespec start, struct timespec end)
60-
{
61-
struct timespec temp;
62-
63-
if ((end.tv_nsec - start.tv_nsec) < 0) {
64-
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
65-
temp.tv_nsec = 1000000000LL + end.tv_nsec - start.tv_nsec;
66-
} else {
67-
temp.tv_sec = end.tv_sec - start.tv_sec;
68-
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
69-
}
70-
71-
return temp;
72-
}
73-
7459
struct timespec timespec_add_ns(struct timespec ts, int64_t ns)
7560
{
7661
struct timespec res;
7762

78-
res.tv_sec = ts.tv_sec;
7963
res.tv_nsec = ts.tv_nsec + ns;
80-
81-
if (res.tv_nsec > 1000000000UL) {
82-
res.tv_sec += 1;
83-
res.tv_nsec -= 1000000000UL;
84-
}
64+
res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
65+
res.tv_nsec %= 1000000000LL;
8566

8667
return res;
8768
}
8869

70+
struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
71+
{
72+
int64_t ns1 = timespec_to_ns(ts1);
73+
int64_t ns2 = timespec_to_ns(ts2);
74+
return timespec_add_ns((struct timespec){0}, ns1 + ns2);
75+
}
76+
77+
struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
78+
{
79+
int64_t ns1 = timespec_to_ns(ts1);
80+
int64_t ns2 = timespec_to_ns(ts2);
81+
return timespec_add_ns((struct timespec){0}, ns1 - ns2);
82+
}
83+
8984
void print_skip(const char *fmt, ...)
9085
{
9186
va_list ap;

‎tools/testing/selftests/kvm/steal_time.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static void *do_steal_time(void *arg)
242242

243243
while (1) {
244244
clock_gettime(CLOCK_MONOTONIC, &ts);
245-
if (ts.tv_sec > stop.tv_sec || ts.tv_nsec >= stop.tv_nsec)
245+
if (timespec_to_ns(timespec_sub(ts, stop)) >= 0)
246246
break;
247247
}
248248

0 commit comments

Comments
 (0)