aboutsummaryrefslogtreecommitdiff
path: root/test/validation/api/time/time.c
blob: 22189ce031dd9ddbb9e95fe92bce651224c74878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/* Copyright (c) 2015-2018, Linaro Limited
 * Copyright (c) 2019-2023, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <time.h>

#include <odp_api.h>
#include <odp/helper/odph_api.h>
#include "odp_cunit_common.h"

#define BUSY_LOOP_CNT		30000000    /* used for t > min resolution */
#define MIN_TIME_RATE		32000
#define MAX_TIME_RATE		15000000000
#define DELAY_TOLERANCE		40000000	    /* deviation for delay */
#define WAIT_SECONDS            3
#define MAX_WORKERS		32
#define TIME_SAMPLES		2
#define TIME_TOLERANCE_NS	1000000
#define TIME_TOLERANCE_CI_NS	40000000
#define GLOBAL_SHM_NAME		"GlobalTimeTest"

static uint64_t local_res;
static uint64_t global_res;

typedef odp_time_t time_cb(void);
typedef uint64_t time_res_cb(void);
typedef odp_time_t time_from_ns_cb(uint64_t ns);
typedef uint64_t time_nsec_cb(void);

typedef struct {
	uint32_t num_threads;
	odp_barrier_t test_barrier;
	odp_time_t time[MAX_WORKERS + 1][TIME_SAMPLES];
} global_shared_mem_t;

static global_shared_mem_t *global_mem;
static odp_instance_t *instance;

static int time_global_init(odp_instance_t *inst)
{
	odp_shm_t global_shm;
	odp_init_t init_param;
	odph_helper_options_t helper_options;
	uint32_t workers_count, max_threads;

	if (odph_options(&helper_options)) {
		ODPH_ERR("odph_options() failed\n");
		return -1;
	}

	odp_init_param_init(&init_param);
	init_param.mem_model = helper_options.mem_model;

	if (0 != odp_init_global(inst, &init_param, NULL)) {
		ODPH_ERR("odp_init_global() failed\n");
		return -1;
	}
	if (0 != odp_init_local(*inst, ODP_THREAD_CONTROL)) {
		ODPH_ERR("odp_init_local() failed\n");
		return -1;
	}

	global_shm = odp_shm_reserve(GLOBAL_SHM_NAME,
				     sizeof(global_shared_mem_t),
				     ODP_CACHE_LINE_SIZE, 0);
	if (global_shm == ODP_SHM_INVALID) {
		ODPH_ERR("Unable reserve memory for global_shm\n");
		return -1;
	}

	global_mem = odp_shm_addr(global_shm);
	memset(global_mem, 0, sizeof(global_shared_mem_t));

	global_mem->num_threads = MAX_WORKERS;

	workers_count = odp_cpumask_default_worker(NULL, 0);

	max_threads = (workers_count >= MAX_WORKERS) ?
			MAX_WORKERS : workers_count;

	if (max_threads < global_mem->num_threads) {
		printf("Requested num of threads is too large\n");
		printf("reducing from %" PRIu32 " to %" PRIu32 "\n",
		       global_mem->num_threads,
		       max_threads);
		global_mem->num_threads = max_threads;
	}

	printf("Num of threads used = %" PRIu32 "\n",
	       global_mem->num_threads);

	instance = inst;

	return 0;
}

static int time_global_term(odp_instance_t inst)
{
	odp_shm_t shm;

	shm = odp_shm_lookup(GLOBAL_SHM_NAME);
	if (0 != odp_shm_free(shm)) {
		ODPH_ERR("odp_shm_free() failed\n");
		return -1;
	}

	if (0 != odp_term_local()) {
		ODPH_ERR("odp_term_local() failed\n");
		return -1;
	}

	if (0 != odp_term_global(inst)) {
		ODPH_ERR("odp_term_global() failed\n");
		return -1;
	}

	return 0;
}

static void time_test_constants(void)
{
	uint64_t ns;

	CU_ASSERT(ODP_TIME_USEC_IN_NS == 1000);

	ns = ODP_TIME_HOUR_IN_NS;
	CU_ASSERT(ns == 60 * ODP_TIME_MIN_IN_NS);
	ns = ODP_TIME_MIN_IN_NS;
	CU_ASSERT(ns == 60 * ODP_TIME_SEC_IN_NS);
	ns = ODP_TIME_SEC_IN_NS;
	CU_ASSERT(ns == 1000 * ODP_TIME_MSEC_IN_NS);
	ns = ODP_TIME_MSEC_IN_NS;
	CU_ASSERT(ns == 1000 * ODP_TIME_USEC_IN_NS);

	ns = ODP_TIME_SEC_IN_NS / 1000;
	CU_ASSERT(ns == ODP_TIME_MSEC_IN_NS);
	ns /= 1000;
	CU_ASSERT(ns == ODP_TIME_USEC_IN_NS);
}

static void time_test_res(time_res_cb time_res, uint64_t *res)
{
	uint64_t rate;

	rate = time_res();
	CU_ASSERT(rate > MIN_TIME_RATE);
	CU_ASSERT(rate < MAX_TIME_RATE);

	*res = ODP_TIME_SEC_IN_NS / rate;
	if (ODP_TIME_SEC_IN_NS % rate)
		(*res)++;
}

static void time_test_local_res(void)
{
	time_test_res(odp_time_local_res, &local_res);
}

static void time_test_global_res(void)
{
	time_test_res(odp_time_global_res, &global_res);
}

/* check that related conversions come back to the same value */
static void time_test_conversion(time_from_ns_cb time_from_ns, uint64_t res)
{
	uint64_t ns1, ns2;
	odp_time_t time;
	uint64_t upper_limit, lower_limit;

	ns1 = 100;
	time = time_from_ns(ns1);

	ns2 = odp_time_to_ns(time);

	/* need to check within arithmetic tolerance that the same
	 * value in ns is returned after conversions */
	upper_limit = ns1 + res;
	lower_limit = ns1 - res;
	CU_ASSERT((ns2 <= upper_limit) && (ns2 >= lower_limit));

	ns1 = 60 * 11 * ODP_TIME_SEC_IN_NS;
	time = time_from_ns(ns1);

	ns2 = odp_time_to_ns(time);

	/* need to check within arithmetic tolerance that the same
	 * value in ns is returned after conversions */
	upper_limit = ns1 + res;
	lower_limit = ns1 - res;
	CU_ASSERT((ns2 <= upper_limit) && (ns2 >= lower_limit));

	/* test on 0 */
	ns1 = odp_time_to_ns(ODP_TIME_NULL);
	CU_ASSERT(ns1 == 0);
}

static void time_test_local_conversion(void)
{
	time_test_conversion(odp_time_local_from_ns, local_res);
}

static void time_test_global_conversion(void)
{
	time_test_conversion(odp_time_global_from_ns, global_res);
}

static void time_test_monotony(void)
{
	volatile uint64_t count = 0;
	odp_time_t l_t1, l_t2, l_t3;
	odp_time_t g_t1, g_t2, g_t3;
	uint64_t lns_t1, lns_t2, lns_t3;
	uint64_t gns_t1, gns_t2, gns_t3;
	uint64_t ns1, ns2, ns3;

	l_t1 = odp_time_local();
	g_t1 = odp_time_global();
	lns_t1 = odp_time_local_ns();
	gns_t1 = odp_time_global_ns();

	while (count < BUSY_LOOP_CNT) {
		count++;
	};

	l_t2 = odp_time_local();
	g_t2 = odp_time_global();
	lns_t2 = odp_time_local_ns();
	gns_t2 = odp_time_global_ns();

	count = 0;
	while (count < BUSY_LOOP_CNT) {
		count++;
	};

	l_t3 = odp_time_local();
	g_t3 = odp_time_global();
	lns_t3 = odp_time_local_ns();
	gns_t3 = odp_time_global_ns();

	ns1 = odp_time_to_ns(l_t1);
	ns2 = odp_time_to_ns(l_t2);
	ns3 = odp_time_to_ns(l_t3);

	/* Local time assertions */
	CU_ASSERT(ns2 > ns1);
	CU_ASSERT(ns3 > ns2);

	ns1 = odp_time_to_ns(g_t1);
	ns2 = odp_time_to_ns(g_t2);
	ns3 = odp_time_to_ns(g_t3);

	/* Global time assertions */
	CU_ASSERT(ns2 > ns1);
	CU_ASSERT(ns3 > ns2);

	/* Local time in nsec */
	CU_ASSERT(lns_t2 > lns_t1);
	CU_ASSERT(lns_t3 > lns_t2);

	/* Global time in nsec */
	CU_ASSERT(gns_t2 > gns_t1);
	CU_ASSERT(gns_t3 > gns_t2);
}

static void time_test_cmp(time_cb time_cur, time_from_ns_cb time_from_ns)
{
	/* volatile to stop optimization of busy loop */
	volatile int count = 0;
	odp_time_t t1, t2, t3;

	t1 = time_cur();

	while (count < BUSY_LOOP_CNT) {
		count++;
	};

	t2 = time_cur();

	while (count < BUSY_LOOP_CNT * 2) {
		count++;
	};

	t3 = time_cur();

	CU_ASSERT(odp_time_cmp(t2, t1) > 0);
	CU_ASSERT(odp_time_cmp(t3, t2) > 0);
	CU_ASSERT(odp_time_cmp(t3, t1) > 0);
	CU_ASSERT(odp_time_cmp(t1, t2) < 0);
	CU_ASSERT(odp_time_cmp(t2, t3) < 0);
	CU_ASSERT(odp_time_cmp(t1, t3) < 0);
	CU_ASSERT(odp_time_cmp(t1, t1) == 0);
	CU_ASSERT(odp_time_cmp(t2, t2) == 0);
	CU_ASSERT(odp_time_cmp(t3, t3) == 0);

	t2 = time_from_ns(60 * 10 * ODP_TIME_SEC_IN_NS);
	t1 = time_from_ns(3);

	CU_ASSERT(odp_time_cmp(t2, t1) > 0);
	CU_ASSERT(odp_time_cmp(t1, t2) < 0);

	t1 = time_from_ns(0);
	CU_ASSERT(odp_time_cmp(t1, ODP_TIME_NULL) == 0);
}

static void time_test_local_cmp(void)
{
	time_test_cmp(odp_time_local, odp_time_local_from_ns);
}

static void time_test_global_cmp(void)
{
	time_test_cmp(odp_time_global, odp_time_global_from_ns);
}

static void time_test_local_strict_cmp(void)
{
	time_test_cmp(odp_time_local_strict, odp_time_local_from_ns);
}

static void time_test_global_strict_cmp(void)
{
	time_test_cmp(odp_time_global_strict, odp_time_global_from_ns);
}

/* check that a time difference gives a reasonable result */
static void time_test_diff(time_cb time_cur,
			   time_from_ns_cb time_from_ns,
			   uint64_t res)
{
	/* volatile to stop optimization of busy loop */
	volatile int count = 0;
	odp_time_t diff, t1, t2;
	uint64_t ns1, ns2, ns;
	uint64_t nsdiff, diff_ns;
	uint64_t upper_limit, lower_limit;

	/* test timestamp diff */
	t1 = time_cur();

	while (count < BUSY_LOOP_CNT) {
		count++;
	};

	t2 = time_cur();
	CU_ASSERT(odp_time_cmp(t2, t1) > 0);

	diff = odp_time_diff(t2, t1);
	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) > 0);

	diff_ns = odp_time_diff_ns(t2, t1);
	CU_ASSERT(diff_ns > 0);

	ns1 = odp_time_to_ns(t1);
	ns2 = odp_time_to_ns(t2);
	ns = ns2 - ns1;
	nsdiff = odp_time_to_ns(diff);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= lower_limit));
	CU_ASSERT((diff_ns <= upper_limit) && (diff_ns >= lower_limit));

	/* test timestamp and interval diff */
	ns1 = 54;
	t1 = time_from_ns(ns1);
	ns = ns2 - ns1;

	diff = odp_time_diff(t2, t1);
	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) > 0);

	diff_ns = odp_time_diff_ns(t2, t1);
	CU_ASSERT(diff_ns > 0);

	nsdiff = odp_time_to_ns(diff);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= lower_limit));
	CU_ASSERT((diff_ns <= upper_limit) && (diff_ns >= lower_limit));

	/* test interval diff */
	ns2 = 60 * 10 * ODP_TIME_SEC_IN_NS;
	ns = ns2 - ns1;

	t2 = time_from_ns(ns2);
	diff = odp_time_diff(t2, t1);
	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) > 0);

	diff_ns = odp_time_diff_ns(t2, t1);
	CU_ASSERT(diff_ns > 0);

	nsdiff = odp_time_to_ns(diff);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= lower_limit));
	CU_ASSERT((diff_ns <= upper_limit) && (diff_ns >= lower_limit));

	/* same time has to diff to 0 */
	diff = odp_time_diff(t2, t2);
	CU_ASSERT(odp_time_cmp(diff, ODP_TIME_NULL) == 0);

	diff = odp_time_diff(t2, ODP_TIME_NULL);
	CU_ASSERT(odp_time_cmp(t2, diff) == 0);

	diff_ns = odp_time_diff_ns(t2, t2);
	CU_ASSERT(diff_ns == 0);
}

static void time_test_local_diff(void)
{
	time_test_diff(odp_time_local, odp_time_local_from_ns, local_res);
}

static void time_test_global_diff(void)
{
	time_test_diff(odp_time_global, odp_time_global_from_ns, global_res);
}

static void time_test_local_strict_diff(void)
{
	time_test_diff(odp_time_local_strict, odp_time_local_from_ns, local_res);
}

static void time_test_global_strict_diff(void)
{
	time_test_diff(odp_time_global_strict, odp_time_global_from_ns, global_res);
}

/* check that a time sum gives a reasonable result */
static void time_test_sum(time_cb time_cur,
			  time_from_ns_cb time_from_ns,
			  uint64_t res)
{
	odp_time_t sum, t1, t2;
	uint64_t nssum, ns1, ns2, ns;
	uint64_t upper_limit, lower_limit;

	/* sum timestamp and interval */
	t1 = time_cur();
	ns2 = 103;
	t2 = time_from_ns(ns2);
	ns1 = odp_time_to_ns(t1);
	ns = ns1 + ns2;

	sum = odp_time_sum(t2, t1);
	CU_ASSERT(odp_time_cmp(sum, ODP_TIME_NULL) > 0);
	nssum = odp_time_to_ns(sum);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nssum <= upper_limit) && (nssum >= lower_limit));

	/* sum intervals */
	ns1 = 60 * 13 * ODP_TIME_SEC_IN_NS;
	t1 = time_from_ns(ns1);
	ns = ns1 + ns2;

	sum = odp_time_sum(t2, t1);
	CU_ASSERT(odp_time_cmp(sum, ODP_TIME_NULL) > 0);
	nssum = odp_time_to_ns(sum);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nssum <= upper_limit) && (nssum >= lower_limit));

	/* test on 0 */
	sum = odp_time_sum(t2, ODP_TIME_NULL);
	CU_ASSERT(odp_time_cmp(t2, sum) == 0);
}

static void time_test_local_sum(void)
{
	time_test_sum(odp_time_local, odp_time_local_from_ns, local_res);
}

static void time_test_global_sum(void)
{
	time_test_sum(odp_time_global, odp_time_global_from_ns, global_res);
}

static void time_test_local_strict_sum(void)
{
	time_test_sum(odp_time_local_strict, odp_time_local_from_ns, local_res);
}

static void time_test_global_strict_sum(void)
{
	time_test_sum(odp_time_global_strict, odp_time_global_from_ns, global_res);
}

static void time_test_wait_until(time_cb time_cur, time_from_ns_cb time_from_ns)
{
	int i;
	odp_time_t lower_limit, upper_limit;
	odp_time_t start_time, end_time, wait;
	odp_time_t second = time_from_ns(ODP_TIME_SEC_IN_NS);

	start_time = time_cur();
	wait = start_time;
	for (i = 0; i < WAIT_SECONDS; i++) {
		wait = odp_time_sum(wait, second);
		odp_time_wait_until(wait);
	}
	end_time = time_cur();

	wait = odp_time_diff(end_time, start_time);
	lower_limit = time_from_ns(WAIT_SECONDS * ODP_TIME_SEC_IN_NS -
				   DELAY_TOLERANCE);
	upper_limit = time_from_ns(WAIT_SECONDS * ODP_TIME_SEC_IN_NS +
				   DELAY_TOLERANCE);

	if (odp_time_cmp(wait, lower_limit) < 0) {
		ODPH_ERR("Exceed lower limit: wait is %" PRIu64 ", lower_limit %" PRIu64 "\n",
			 odp_time_to_ns(wait), odp_time_to_ns(lower_limit));
		CU_FAIL("Exceed lower limit\n");
	}

	if (odp_time_cmp(wait, upper_limit) > 0) {
		ODPH_ERR("Exceed upper limit: wait is %" PRIu64 ", upper_limit %" PRIu64 "\n",
			 odp_time_to_ns(wait), odp_time_to_ns(lower_limit));
		CU_FAIL("Exceed upper limit\n");
	}
}

static void time_test_local_wait_until(void)
{
	time_test_wait_until(odp_time_local, odp_time_local_from_ns);
}

static void time_test_global_wait_until(void)
{
	time_test_wait_until(odp_time_global, odp_time_global_from_ns);
}

static void time_test_wait_ns(void)
{
	int i;
	odp_time_t lower_limit, upper_limit;
	odp_time_t start_time, end_time, diff;

	start_time = odp_time_local();
	for (i = 0; i < WAIT_SECONDS; i++)
		odp_time_wait_ns(ODP_TIME_SEC_IN_NS);
	end_time = odp_time_local();

	diff = odp_time_diff(end_time, start_time);

	lower_limit = odp_time_local_from_ns(WAIT_SECONDS * ODP_TIME_SEC_IN_NS -
					     DELAY_TOLERANCE);
	upper_limit = odp_time_local_from_ns(WAIT_SECONDS * ODP_TIME_SEC_IN_NS +
					     DELAY_TOLERANCE);

	if (odp_time_cmp(diff, lower_limit) < 0) {
		ODPH_ERR("Exceed lower limit: diff is %" PRIu64 ", lower_limit %" PRIu64 "\n",
			 odp_time_to_ns(diff), odp_time_to_ns(lower_limit));
		CU_FAIL("Exceed lower limit\n");
	}

	if (odp_time_cmp(diff, upper_limit) > 0) {
		ODPH_ERR("Exceed upper limit: diff is %" PRIu64 ", upper_limit %" PRIu64 "\n",
			 odp_time_to_ns(diff), odp_time_to_ns(upper_limit));
		CU_FAIL("Exceed upper limit\n");
	}
}

/* Check that ODP time is within +-5% of system time */
static void check_time_diff(double t_odp, double t_system,
			    const char *test, int id)
{
	if (t_odp > t_system * 1.05) {
		CU_FAIL("ODP time too high");
		ODPH_ERR("ODP time too high (%s/%d): t_odp: %f, t_system: %f\n",
			 test, id, t_odp, t_system);
	}
	if (t_odp < t_system * 0.95) {
		CU_FAIL("ODP time too low");
		ODPH_ERR("ODP time too low (%s/%d): t_odp: %f, t_system: %f\n",
			 test, id, t_odp, t_system);
	}
}

static void time_test_accuracy(time_cb time_cur,
			       time_cb time_cur_strict, time_from_ns_cb time_from_ns)
{
	int i;
	odp_time_t t1[2], t2[2], wait;
	struct timespec ts1, ts2, tsdiff;
	double sec_c;
	odp_time_t sec = time_from_ns(ODP_TIME_SEC_IN_NS);

	i = clock_gettime(CLOCK_MONOTONIC, &ts1);
	CU_ASSERT(i == 0);
	t1[0] = time_cur_strict();
	t1[1] = time_cur();

	wait = odp_time_sum(t1[0], sec);
	for (i = 0; i < 5; i++) {
		odp_time_wait_until(wait);
		wait = odp_time_sum(wait, sec);
	}

	i = clock_gettime(CLOCK_MONOTONIC, &ts2);
	CU_ASSERT(i == 0);
	t2[0] = time_cur_strict();
	t2[1] = time_cur();

	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;
	}
	sec_c = ((double)(tsdiff.tv_nsec) / 1000000000L) + tsdiff.tv_sec;

	for (i = 0; i < 2; i++) {
		odp_time_t diff  = odp_time_diff(t2[i], t1[i]);
		double sec_t = ((double)odp_time_to_ns(diff)) / ODP_TIME_SEC_IN_NS;

		check_time_diff(sec_t, sec_c, __func__, i);
	}
}

static void time_test_local_accuracy(void)
{
	time_test_accuracy(odp_time_local, odp_time_local_strict, odp_time_local_from_ns);
}

static void time_test_global_accuracy(void)
{
	time_test_accuracy(odp_time_global, odp_time_global_strict, odp_time_global_from_ns);
}

static void time_test_accuracy_nsec(void)
{
	uint64_t t1[4], t2[4];
	struct timespec ts1, ts2, tsdiff;
	double sec_c;
	int i, ret;

	ret = clock_gettime(CLOCK_MONOTONIC, &ts1);
	CU_ASSERT(ret == 0);
	t1[0] = odp_time_global_strict_ns();
	t1[1] = odp_time_local_strict_ns();
	t1[2] = odp_time_global_ns();
	t1[3] = odp_time_local_ns();

	for (i = 0; i < 5; i++)
		odp_time_wait_ns(ODP_TIME_SEC_IN_NS);

	ret = clock_gettime(CLOCK_MONOTONIC, &ts2);
	CU_ASSERT(ret == 0);
	t2[0] = odp_time_global_strict_ns();
	t2[1] = odp_time_local_strict_ns();
	t2[2] = odp_time_global_ns();
	t2[3] = odp_time_local_ns();

	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;
	}
	sec_c = ((double)(tsdiff.tv_nsec) / 1000000000L) + tsdiff.tv_sec;

	for (i = 0; i < 4; i++) {
		uint64_t diff  = t2[i] - t1[i];
		double sec_t = ((double)diff) / ODP_TIME_SEC_IN_NS;

		check_time_diff(sec_t, sec_c, __func__, i);
	}
}

static int time_test_global_sync_thr(void *arg ODP_UNUSED)
{
	int tid = odp_thread_id();
	odp_shm_t global_shm = odp_shm_lookup(GLOBAL_SHM_NAME);
	global_shared_mem_t *global_mem = odp_shm_addr(global_shm);

	if (!global_mem)
		return 1;

	odp_barrier_wait(&global_mem->test_barrier);
	global_mem->time[tid][0] = odp_time_global();
	odp_time_wait_ns(ODP_TIME_MSEC_IN_NS * 100);
	odp_barrier_wait(&global_mem->test_barrier);
	global_mem->time[tid][1] = odp_time_global();

	return 0;
}

static void time_test_global_sync(const int ctrl)
{
	odp_cpumask_t cpumask;
	odph_thread_common_param_t thr_common;
	odph_thread_param_t thr_param;
	odph_thread_t thread_tbl[MAX_WORKERS];
	const uint64_t tolerance =
		odp_cunit_ci() ? TIME_TOLERANCE_CI_NS : TIME_TOLERANCE_NS;
	const int num = ctrl ? 2 : global_mem->num_threads;

	if (num < 2) {
		printf(" number of threads is less than two, test skipped. ");
		return;
	}

	odp_barrier_init(&global_mem->test_barrier, num);

	odph_thread_param_init(&thr_param);
	thr_param.start = time_test_global_sync_thr;

	odph_thread_common_param_init(&thr_common);
	thr_common.instance = *instance;

	int thr = 0;

	if (ctrl) {
		/* Test sync between one control and one worker thread. */
		odp_cpumask_default_control(&cpumask, 1);
		thr_common.cpumask = &cpumask;
		thr_param.thr_type = ODP_THREAD_CONTROL;

		int r = odph_thread_create(&thread_tbl[thr++],
					   &thr_common, &thr_param, 1);
		CU_ASSERT_FATAL(r == 1);
		odp_cpumask_default_worker(&cpumask, 1);
	} else {
		/* Test sync between num worker threads. */
		odp_cpumask_default_worker(&cpumask, num);
	}

	int cpu = odp_cpumask_first(&cpumask);

	while (cpu >= 0) {
		odp_cpumask_t cpumask_one;

		/*
		 * Delay for more than the tolerance, so that we notice if the
		 * thread's view of global time is affected.
		 */
		odp_time_wait_ns(tolerance * 2);

		odp_cpumask_zero(&cpumask_one);
		odp_cpumask_set(&cpumask_one, cpu);
		thr_common.cpumask = &cpumask_one;
		thr_param.thr_type = ODP_THREAD_WORKER;

		int r = odph_thread_create(&thread_tbl[thr++],
					   &thr_common, &thr_param, 1);
		CU_ASSERT_FATAL(r == 1);

		cpu = odp_cpumask_next(&cpumask, cpu);
	}

	CU_ASSERT(odph_thread_join(thread_tbl, num) == num);

	for (int s = 0; s < TIME_SAMPLES; s++) {
		int min_idx = 0, max_idx = 0;
		uint64_t min = UINT64_MAX, max = 0;
		double avg = 0;

		for (int i = 1; i < num + 1; i++) {
			uint64_t t = odp_time_to_ns(global_mem->time[i][s]);

			if (t < min) {
				min = t;
				min_idx = i;
			}
		}

		printf("\nround %d\nthread time diffs: ", s);

		for (int i = 1; i < num + 1; i++) {
			uint64_t t = odp_time_to_ns(global_mem->time[i][s]) - min;

			printf("%" PRIu64 " ", t);

			if (t > max) {
				max = t;
				max_idx = i;
			}

			avg += t;
		}

		/* The min result itself is not included in the average. */
		avg /= num - 1;
		printf("\nmin: %" PRIu64 " (tid %d)  max diff: %" PRIu64
		       " (tid %d)  avg diff: %g", min, min_idx, max, max_idx, avg);
		CU_ASSERT(max < tolerance);
	}

	printf("\n");
}

static void time_test_global_sync_workers(void)
{
	time_test_global_sync(0);
}

static void time_test_global_sync_control(void)
{
	time_test_global_sync(1);
}

odp_testinfo_t time_suite_time[] = {
	ODP_TEST_INFO(time_test_constants),
	ODP_TEST_INFO(time_test_local_res),
	ODP_TEST_INFO(time_test_local_conversion),
	ODP_TEST_INFO(time_test_local_cmp),
	ODP_TEST_INFO(time_test_local_diff),
	ODP_TEST_INFO(time_test_local_sum),
	ODP_TEST_INFO(time_test_global_res),
	ODP_TEST_INFO(time_test_global_conversion),
	ODP_TEST_INFO(time_test_global_cmp),
	ODP_TEST_INFO(time_test_global_diff),
	ODP_TEST_INFO(time_test_global_sum),
	ODP_TEST_INFO(time_test_wait_ns),
	ODP_TEST_INFO(time_test_monotony),
	ODP_TEST_INFO(time_test_local_wait_until),
	ODP_TEST_INFO(time_test_global_wait_until),
	ODP_TEST_INFO(time_test_local_accuracy),
	ODP_TEST_INFO(time_test_global_accuracy),
	ODP_TEST_INFO(time_test_accuracy_nsec),
	ODP_TEST_INFO(time_test_local_strict_diff),
	ODP_TEST_INFO(time_test_local_strict_sum),
	ODP_TEST_INFO(time_test_local_strict_cmp),
	ODP_TEST_INFO(time_test_global_strict_diff),
	ODP_TEST_INFO(time_test_global_strict_sum),
	ODP_TEST_INFO(time_test_global_strict_cmp),
	ODP_TEST_INFO(time_test_global_sync_workers),
	ODP_TEST_INFO(time_test_global_sync_control),
	ODP_TEST_INFO_NULL
};

odp_suiteinfo_t time_suites[] = {
		{"Time", NULL, NULL, time_suite_time},
		ODP_SUITE_INFO_NULL
};

int main(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	odp_cunit_register_global_init(time_global_init);
	odp_cunit_register_global_term(time_global_term);

	ret = odp_cunit_register(time_suites);

	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}