Commit bfcaa849 authored by Andrew Jones's avatar Andrew Jones Committed by Paolo Bonzini
Browse files

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: default avatarAndrew Jones <drjones@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent cf6c26ec
Loading
Loading
Loading
Loading
+16 −21
Original line number Original line Diff line number Diff line
@@ -117,8 +117,7 @@ static void *vcpu_worker(void *data)
	struct kvm_vm *vm = args->vm;
	struct kvm_vm *vm = args->vm;
	int vcpu_id = args->vcpu_id;
	int vcpu_id = args->vcpu_id;
	struct kvm_run *run;
	struct kvm_run *run;
	struct timespec start;
	struct timespec start, end, ts_diff;
	struct timespec end;


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


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


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


	clock_gettime(CLOCK_MONOTONIC, &end);
	clock_gettime(CLOCK_MONOTONIC, &end);


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


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


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


	clock_gettime(CLOCK_MONOTONIC, &end);
	clock_gettime(CLOCK_MONOTONIC, &end);
	PER_VCPU_DEBUG("userfaulted %ld pages over %lld.%.9lds. (%f/sec)\n",
	ts_diff = timespec_sub(end, start);
		       pages, (long long)(timespec_diff(start, end).tv_sec),
	PER_VCPU_DEBUG("userfaulted %ld pages over %ld.%.9lds. (%f/sec)\n",
		       timespec_diff(start, end).tv_nsec, pages /
		       pages, ts_diff.tv_sec, ts_diff.tv_nsec,
		       ((double)timespec_diff(start, end).tv_sec +
		       pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
			(double)timespec_diff(start, end).tv_nsec / 100000000.0));


	return NULL;
	return NULL;
}
}
@@ -360,13 +357,12 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
	pthread_t *vcpu_threads;
	pthread_t *vcpu_threads;
	pthread_t *uffd_handler_threads = NULL;
	pthread_t *uffd_handler_threads = NULL;
	struct uffd_handler_args *uffd_args = NULL;
	struct uffd_handler_args *uffd_args = NULL;
	struct timespec start, end, ts_diff;
	int *pipefds = NULL;
	int *pipefds = NULL;
	struct kvm_vm *vm;
	struct kvm_vm *vm;
	uint64_t guest_num_pages;
	uint64_t guest_num_pages;
	int vcpu_id;
	int vcpu_id;
	int r;
	int r;
	struct timespec start;
	struct timespec end;


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


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


	pr_info("Total guest execution time: %lld.%.9lds\n",
	ts_diff = timespec_sub(end, start);
		(long long)(timespec_diff(start, end).tv_sec),
	pr_info("Total guest execution time: %ld.%.9lds\n",
		timespec_diff(start, end).tv_nsec);
		ts_diff.tv_sec, ts_diff.tv_nsec);
	pr_info("Overall demand paging rate: %f pgs/sec\n",
	pr_info("Overall demand paging rate: %f pgs/sec\n",
		guest_num_pages / ((double)timespec_diff(start, end).tv_sec +
		guest_num_pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
		(double)timespec_diff(start, end).tv_nsec / 100000000.0));


	ucall_uninit(vm);
	ucall_uninit(vm);
	kvm_vm_free(vm);
	kvm_vm_free(vm);
+2 −1
Original line number Original line Diff line number Diff line
@@ -61,7 +61,8 @@ void test_assert(bool exp, const char *exp_str,
size_t parse_size(const char *size);
size_t parse_size(const char *size);


int64_t timespec_to_ns(struct timespec ts);
int64_t timespec_to_ns(struct timespec ts);
struct timespec timespec_diff(struct timespec start, struct timespec end);
struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);


#endif /* SELFTEST_KVM_TEST_UTIL_H */
#endif /* SELFTEST_KVM_TEST_UTIL_H */
+16 −21
Original line number Original line Diff line number Diff line
@@ -56,34 +56,29 @@ int64_t timespec_to_ns(struct timespec ts)
	return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
	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 timespec_add_ns(struct timespec ts, int64_t ns)
{
{
	struct timespec res;
	struct timespec res;


	res.tv_sec = ts.tv_sec;
	res.tv_nsec = ts.tv_nsec + ns;
	res.tv_nsec = ts.tv_nsec + ns;
	res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
	res.tv_nsec %= 1000000000LL;


	if (res.tv_nsec > 1000000000UL) {
	return res;
		res.tv_sec += 1;
		res.tv_nsec -= 1000000000UL;
}
}


	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, ...)
void print_skip(const char *fmt, ...)
+1 −1
Original line number Original line Diff line number Diff line
@@ -242,7 +242,7 @@ static void *do_steal_time(void *arg)


	while (1) {
	while (1) {
		clock_gettime(CLOCK_MONOTONIC, &ts);
		clock_gettime(CLOCK_MONOTONIC, &ts);
		if (ts.tv_sec > stop.tv_sec || ts.tv_nsec >= stop.tv_nsec)
		if (timespec_to_ns(timespec_sub(ts, stop)) >= 0)
			break;
			break;
	}
	}