aboutsummaryrefslogtreecommitdiff
path: root/test/common_plat/validation/api/time/time.c
blob: e2ca2e17c4971c6d0cf02498e1ce40072c940dc6 (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
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <odp_api.h>
#include "odp_cunit_common.h"
#include "time_test.h"
#include <time.h>

#define BUSY_LOOP_CNT		30000000    /* used for t > min resolution */
#define BUSY_LOOP_CNT_LONG	6000000000  /* used for t > 4 sec */
#define MIN_TIME_RATE		32000
#define MAX_TIME_RATE		15000000000
#define DELAY_TOLERANCE		20000000	    /* deviation for delay */
#define WAIT_SECONDS            3

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);

void time_test_constants(void)
{
	uint64_t 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)++;
}

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

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);
}

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

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

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 ns1, ns2, ns3;

	l_t1 = odp_time_local();
	g_t1 = odp_time_global();

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

	l_t2 = odp_time_local();
	g_t2 = odp_time_global();

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

	l_t3 = odp_time_local();
	g_t3 = odp_time_global();

	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);
}

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);
}

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

void time_test_global_cmp(void)
{
	time_test_cmp(odp_time_global, 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 nsdiff, ns1, ns2, 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);

	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));

	/* 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);
	nsdiff = odp_time_to_ns(diff);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= 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);
	nsdiff = odp_time_to_ns(diff);

	upper_limit = ns + 2 * res;
	lower_limit = ns - 2 * res;
	CU_ASSERT((nsdiff <= upper_limit) && (nsdiff >= 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);
}

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

void time_test_global_diff(void)
{
	time_test_diff(odp_time_global, 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);
}

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

void time_test_global_sum(void)
{
	time_test_sum(odp_time_global, 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) {
		fprintf(stderr, "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) {
		fprintf(stderr, "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");
	}
}

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

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

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) {
		fprintf(stderr, "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) {
		fprintf(stderr, "Exceed upper limit: "
			"diff is %" PRIu64 ", upper_limit %" PRIu64 "\n",
			odp_time_to_ns(diff), odp_time_to_ns(lower_limit));
		CU_FAIL("Exceed upper limit\n");
	}
}

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;
	double sec_t, sec_c;
	odp_time_t sec = time_from_ns(ODP_TIME_SEC_IN_NS);

	c1 = clock();
	t1 = time_cur();

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

	t2 = time_cur();
	c2 = clock();

	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;

	/* Check that ODP time is within +-5% of system time */
	CU_ASSERT(sec_t < sec_c * 1.05);
	CU_ASSERT(sec_t > sec_c * 0.95);
}

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

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

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_monotony),
	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_local_wait_until),
	ODP_TEST_INFO(time_test_wait_ns),
	ODP_TEST_INFO(time_test_local_accuracy),
	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_global_wait_until),
	ODP_TEST_INFO(time_test_global_accuracy),
	ODP_TEST_INFO_NULL
};

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

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

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

	ret = odp_cunit_register(time_suites);

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

	return ret;
}