diff mbox series

[v13,10/15] validation: time: use clock_gettime() instead of clock()

Message ID 1516010417-3323-11-git-send-email-odpbot@yandex.ru
State New
Headers show
Series [v13,1/15] helper: link against libpthread and libodp-linux | expand

Commit Message

Github ODP bot Jan. 15, 2018, 10 a.m. UTC
From: Dmitry Eremin-Solenikov <dmitry.ereminsolenikov@linaro.org>


ODP test for time API uses clock() to compare time against. However
clock() returns processor time used by program, which can differ between
runs. Use clock_gettime() as a time source to compare against.

Signed-off-by: Dmitry Eremin-Solenikov <dmitry.ereminsolenikov@linaro.org>

Fixes: https://bugs.linaro.org/show_bug.cgi?id=3572
---
/** Email created from pull request 377 (lumag:misc-fixes)
 ** https://github.com/Linaro/odp/pull/377
 ** Patch: https://github.com/Linaro/odp/pull/377.patch
 ** Base sha: 634b380b63de53c65b92c214d91aaf03785d69db
 ** Merge commit sha: 9dbaba7b2769979792f04d62a6c8accf0c6b13df
 **/
 test/validation/api/time/time.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/test/validation/api/time/time.c b/test/validation/api/time/time.c
index c8f90dbac..d7d5b790e 100644
--- a/test/validation/api/time/time.c
+++ b/test/validation/api/time/time.c
@@ -423,11 +423,12 @@  static void time_test_accuracy(time_cb time_cur, time_from_ns_cb time_from_ns)
 {
 	int i;
 	odp_time_t t1, t2, wait, diff;
-	clock_t c1, c2;
+	struct timespec ts1, ts2, tsdiff;
 	double sec_t, sec_c;
 	odp_time_t sec = time_from_ns(ODP_TIME_SEC_IN_NS);
 
-	c1 = clock();
+	i = clock_gettime(CLOCK_MONOTONIC, &ts1);
+	CU_ASSERT(i == 0);
 	t1 = time_cur();
 
 	wait = odp_time_sum(t1, sec);
@@ -436,12 +437,21 @@  static void time_test_accuracy(time_cb time_cur, time_from_ns_cb time_from_ns)
 		wait = odp_time_sum(wait, sec);
 	}
 
+	i = clock_gettime(CLOCK_MONOTONIC, &ts2);
+	CU_ASSERT(i == 0);
 	t2 = time_cur();
-	c2 = clock();
+
+	if (ts2.tv_nsec < ts1.tv_nsec) {
+		tsdiff.tv_nsec = 1000000000L + ts2.tv_nsec - ts1.tv_nsec;
+		tsdiff.tv_sec = ts2.tv_sec - 1 - ts1.tv_sec;
+	} else {
+		tsdiff.tv_nsec = ts2.tv_nsec - ts1.tv_nsec;
+		tsdiff.tv_sec = ts2.tv_sec - ts1.tv_sec;
+	}
 
 	diff  = odp_time_diff(t2, t1);
 	sec_t = ((double)odp_time_to_ns(diff)) / ODP_TIME_SEC_IN_NS;
-	sec_c = ((double)(c2 - c1)) / CLOCKS_PER_SEC;
+	sec_c = ((double)(tsdiff.tv_nsec) / 1000000000L) + tsdiff.tv_sec;
 
 	/* Check that ODP time is within +-5% of system time */
 	CU_ASSERT(sec_t < sec_c * 1.05);