summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/kvm/lib
diff options
context:
space:
mode:
authorAndrew Jones <drjones@redhat.com>2020-03-16 18:37:03 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2020-03-18 14:08:56 +0100
commitbfcaa84975fa0c75deca3e997533aaa35ffed12b (patch)
tree43be2463bbd76e3fdfc167b1aeca17ba5e4ed15a /tools/testing/selftests/kvm/lib
parentcf6c26ec7bf5386706cd6522708766eb6522995e (diff)
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>
Diffstat (limited to 'tools/testing/selftests/kvm/lib')
-rw-r--r--tools/testing/selftests/kvm/lib/test_util.c37
1 files changed, 16 insertions, 21 deletions
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index ee12c4b9ae05..689e97c27ee2 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -56,36 +56,31 @@ int64_t timespec_to_ns(struct timespec ts)
return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
}
-struct timespec timespec_diff(struct timespec start, struct timespec end)
-{
- struct timespec temp;
-
- if ((end.tv_nsec - start.tv_nsec) < 0) {
- temp.tv_sec = end.tv_sec - start.tv_sec - 1;
- temp.tv_nsec = 1000000000LL + end.tv_nsec - start.tv_nsec;
- } else {
- temp.tv_sec = end.tv_sec - start.tv_sec;
- temp.tv_nsec = end.tv_nsec - start.tv_nsec;
- }
-
- return temp;
-}
-
struct timespec timespec_add_ns(struct timespec ts, int64_t ns)
{
struct timespec res;
- res.tv_sec = ts.tv_sec;
res.tv_nsec = ts.tv_nsec + ns;
-
- if (res.tv_nsec > 1000000000UL) {
- res.tv_sec += 1;
- res.tv_nsec -= 1000000000UL;
- }
+ res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
+ res.tv_nsec %= 1000000000LL;
return res;
}
+struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
+{
+ int64_t ns1 = timespec_to_ns(ts1);
+ int64_t ns2 = timespec_to_ns(ts2);
+ return timespec_add_ns((struct timespec){0}, ns1 + ns2);
+}
+
+struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
+{
+ int64_t ns1 = timespec_to_ns(ts1);
+ int64_t ns2 = timespec_to_ns(ts2);
+ return timespec_add_ns((struct timespec){0}, ns1 - ns2);
+}
+
void print_skip(const char *fmt, ...)
{
va_list ap;