diff mbox

Cycle count accuracy test

Message ID 1393338201-15643-1-git-send-email-petri.savolainen@linaro.org
State Accepted, archived
Commit 2da43ab537b39578c0a07af842d356a13930a7d3
Headers show

Commit Message

Petri Savolainen Feb. 25, 2014, 2:23 p.m. UTC
Signed-off-by: Petri Savolainen <petri.savolainen@linaro.org>
---
 platform/linux-generic/source/odp_time.c |  2 +-
 test/example/odp_example.c               | 61 +++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/source/odp_time.c b/platform/linux-generic/source/odp_time.c
index 38c1bdf..23ff8f5 100644
--- a/platform/linux-generic/source/odp_time.c
+++ b/platform/linux-generic/source/odp_time.c
@@ -85,7 +85,7 @@  uint64_t odp_time_cycles_to_ns(uint64_t cycles)
 {
 	uint64_t hz = odp_sys_cpu_hz();
 
-	if (cycles > hz)
+	if (cycles > (UINT64_MAX / 1000000000))
 		return 1000000000*(cycles/hz);
 
 	return (1000000000*cycles)/hz;
diff --git a/test/example/odp_example.c b/test/example/odp_example.c
index 12bb438..0f421a3 100644
--- a/test/example/odp_example.c
+++ b/test/example/odp_example.c
@@ -19,7 +19,7 @@ 
 /* ODP helper for Linux apps */
 #include <helper/odp_linux.h>
 
-/* Linux headers*/
+/* Needs librt*/
 #include <time.h>
 
 /* GNU lib C */
@@ -34,6 +34,7 @@ 
 #define ALLOC_ROUNDS          (1024*1024)
 #define MULTI_BUFS_MAX        4
 #define SCHED_RETRY           100
+#define TEST_SEC              2
 
 
 typedef struct {
@@ -527,6 +528,61 @@  static void *run_thread(void *arg)
 }
 
 
+static void test_time(void)
+{
+	struct timespec tp1, tp2;
+	uint64_t t1, t2;
+	uint64_t ns1, ns2, cycles;
+	double err;
+
+	if (clock_gettime(CLOCK_MONOTONIC, &tp2)) {
+		ODP_ERR("clock_gettime failed.\n");
+		return;
+	}
+
+	printf("\nTime accuracy test (%i sec)\n", TEST_SEC);
+
+	do {
+		if (clock_gettime(CLOCK_MONOTONIC, &tp1)) {
+			ODP_ERR("clock_gettime failed.\n");
+			return;
+		}
+
+	} while (tp1.tv_sec == tp2.tv_sec);
+
+	t1 = odp_time_get_cycles();
+
+	do {
+		if (clock_gettime(CLOCK_MONOTONIC, &tp2)) {
+			ODP_ERR("clock_gettime failed.\n");
+			return;
+		}
+
+	} while ((tp2.tv_sec - tp1.tv_sec) < TEST_SEC);
+
+	t2 = odp_time_get_cycles();
+
+	ns1 = (tp2.tv_sec - tp1.tv_sec)*1000000000;
+
+	if (tp2.tv_nsec > tp1.tv_nsec)
+		ns1 += tp2.tv_nsec - tp1.tv_nsec;
+	else
+		ns1 -= tp1.tv_nsec - tp2.tv_nsec;
+
+	cycles = odp_time_diff_cycles(t1, t2);
+	ns2    = odp_time_cycles_to_ns(cycles);
+
+	err = ((double)(ns2) - (double)ns1) / (double)ns1;
+
+	printf("clock_gettime         %"PRIu64" ns\n",    ns1);
+	printf("odp_time_get_cycles   %"PRIu64" cycles\n", cycles);
+	printf("odp_time_cycles_to_ns %"PRIu64" ns\n",    ns2);
+	printf("odp get cycle error   %f%%\n", err*100.0);
+
+	printf("\n");
+}
+
+
 static void print_usage(void)
 {
 	printf("\n\nUsage: ./odp_example [options]\n");
@@ -637,6 +693,9 @@  int main(int argc, char *argv[])
 	thr_id = odp_thread_create(0);
 	odp_init_local(thr_id);
 
+	/* Test cycle count accuracy */
+	test_time();
+
 	/*
 	 * Create message pool
 	 */