[Move timespec-diff to test_util.h] Signed-off-by: Andrew Jones <drjones@xxxxxxxxxx> --- .../selftests/kvm/demand_paging_test.c | 42 +++++-------------- .../testing/selftests/kvm/include/test_util.h | 3 ++ tools/testing/selftests/kvm/lib/test_util.c | 20 +++++++++ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c index 2e6e3db8418a..22a3011df62f 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -74,26 +74,6 @@ static uint64_t guest_test_phys_mem; */ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM; -int64_t to_ns(struct timespec ts) -{ - return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec; -} - -struct 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 = 1000000000 + 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 vcpu_args { uint64_t gva; uint64_t pages; @@ -157,8 +137,8 @@ static void *vcpu_worker(void *data) clock_gettime(CLOCK_MONOTONIC, &end); PER_VCPU_DEBUG("vCPU %d execution time: %lld.%.9lds\n", vcpu_id, - (long long)(diff(start, end).tv_sec), - diff(start, end).tv_nsec); + (long long)(timespec_diff(start, end).tv_sec), + timespec_diff(start, end).tv_nsec); return NULL; } @@ -226,7 +206,7 @@ static int handle_uffd_page_request(int uffd, uint64_t addr) clock_gettime(CLOCK_MONOTONIC, &end); PER_PAGE_DEBUG("UFFDIO_COPY %d \t%lld ns\n", tid, - (long long)to_ns(diff(start, end))); + (long long)timespec_to_ns(timespec_diff(start, end))); PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n", host_page_size, addr, tid); @@ -321,10 +301,10 @@ static void *uffd_handler_thread_fn(void *arg) clock_gettime(CLOCK_MONOTONIC, &end); PER_VCPU_DEBUG("userfaulted %ld pages over %lld.%.9lds. (%f/sec)\n", - pages, (long long)(diff(start, end).tv_sec), - diff(start, end).tv_nsec, pages / - ((double)diff(start, end).tv_sec + - (double)diff(start, end).tv_nsec / 100000000.0)); + pages, (long long)(timespec_diff(start, end).tv_sec), + timespec_diff(start, end).tv_nsec, pages / + ((double)timespec_diff(start, end).tv_sec + + (double)timespec_diff(start, end).tv_nsec / 100000000.0)); return NULL; } @@ -432,7 +412,6 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, DEBUG("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem); - /* Add an extra memory slot for testing demand paging */ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, guest_test_phys_mem, @@ -548,10 +527,11 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, } DEBUG("Total guest execution time: %lld.%.9lds\n", - (long long)(diff(start, end).tv_sec), diff(start, end).tv_nsec); + (long long)(timespec_diff(start, end).tv_sec), + timespec_diff(start, end).tv_nsec); DEBUG("Overall demand paging rate: %f pgs/sec\n", - guest_num_pages / ((double)diff(start, end).tv_sec + - (double)diff(start, end).tv_nsec / 100000000.0)); + guest_num_pages / ((double)timespec_diff(start, end).tv_sec + + (double)timespec_diff(start, end).tv_nsec / 100000000.0)); ucall_uninit(vm); kvm_vm_free(vm); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index e696c8219d69..920328ca5f7e 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -41,4 +41,7 @@ void test_assert(bool exp, const char *exp_str, size_t parse_size(const char *size); +int64_t timespec_to_ns(struct timespec ts); +struct timespec timespec_diff(struct timespec start, struct timespec end); + #endif /* SELFTEST_KVM_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index cbd7f51b07a1..1c0d45afdf36 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -49,3 +49,23 @@ size_t parse_size(const char *size) return base << shift; } + +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; +} -- 2.21.1