1 // SPDX-License-Identifier: GPL-2.0-only
3 * tools/testing/selftests/kvm/lib/test_util.c
5 * Copyright (C) 2020, Google LLC.
14 #include "test_util.h"
17 * Parses "[0-9]+[kmgt]?".
19 size_t parse_size(const char *size
)
25 TEST_ASSERT(size
&& isdigit(size
[0]), "Need at least one digit in '%s'", size
);
27 base
= strtoull(size
, &scale
, 0);
29 TEST_ASSERT(base
!= ULLONG_MAX
, "Overflow parsing size!");
31 switch (tolower(*scale
)) {
49 TEST_ASSERT(false, "Unknown size letter %c", *scale
);
52 TEST_ASSERT((base
<< shift
) >> shift
== base
, "Overflow scaling size!");
57 int64_t timespec_to_ns(struct timespec ts
)
59 return (int64_t)ts
.tv_nsec
+ 1000000000LL * (int64_t)ts
.tv_sec
;
62 struct timespec
timespec_add_ns(struct timespec ts
, int64_t ns
)
66 res
.tv_nsec
= ts
.tv_nsec
+ ns
;
67 res
.tv_sec
= ts
.tv_sec
+ res
.tv_nsec
/ 1000000000LL;
68 res
.tv_nsec
%= 1000000000LL;
73 struct timespec
timespec_add(struct timespec ts1
, struct timespec ts2
)
75 int64_t ns1
= timespec_to_ns(ts1
);
76 int64_t ns2
= timespec_to_ns(ts2
);
77 return timespec_add_ns((struct timespec
){0}, ns1
+ ns2
);
80 struct timespec
timespec_sub(struct timespec ts1
, struct timespec ts2
)
82 int64_t ns1
= timespec_to_ns(ts1
);
83 int64_t ns2
= timespec_to_ns(ts2
);
84 return timespec_add_ns((struct timespec
){0}, ns1
- ns2
);
87 struct timespec
timespec_diff_now(struct timespec start
)
91 clock_gettime(CLOCK_MONOTONIC
, &end
);
92 return timespec_sub(end
, start
);
95 struct timespec
timespec_div(struct timespec ts
, int divisor
)
97 int64_t ns
= timespec_to_ns(ts
) / divisor
;
99 return timespec_add_ns((struct timespec
){0}, ns
);
102 void print_skip(const char *fmt
, ...)
110 puts(", skipping test");