aboutsummaryrefslogtreecommitdiff
path: root/test/performance/odp_scheduling.c
blob: 99ff7724481e84d1f4e15f6fccee90470a41df32 (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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * @example  odp_example.c ODP example application
 */

/* enable clock_gettime */
#define _POSIX_C_SOURCE 200112L

#include <string.h>
#include <stdlib.h>

#include <test_debug.h>

/* ODP main header */
#include <odp.h>

/* ODP helper for Linux apps */
#include <odp/helper/linux.h>

/* Needs librt*/
#include <time.h>

/* GNU lib C */
#include <getopt.h>


#define MAX_WORKERS           32            /**< Max worker threads */
#define MSG_POOL_SIZE         (4*1024*1024) /**< Message pool size */
#define MAX_ALLOCS            35            /**< Alloc burst size */
#define QUEUES_PER_PRIO       64            /**< Queue per priority */
#define QUEUE_ROUNDS          (512*1024)    /**< Queue test rounds */
#define ALLOC_ROUNDS          (1024*1024)   /**< Alloc test rounds */
#define MULTI_BUFS_MAX        4             /**< Buffer burst size */
#define TEST_SEC              2             /**< Time test duration in sec */

/** Dummy message */
typedef struct {
	int msg_id; /**< Message ID */
	int seq;    /**< Sequence number */
} test_message_t;

#define MSG_HELLO 1  /**< Hello */
#define MSG_ACK   2  /**< Ack */

/** Test arguments */
typedef struct {
	int cpu_count;  /**< CPU count */
	int proc_mode;  /**< Process mode */
} test_args_t;


/** Test global variables */
typedef struct {
	odp_barrier_t barrier;/**< @private Barrier for test synchronisation */
} test_globals_t;


/**
 * @internal Clear all scheduled queues. Retry to be sure that all
 * buffers have been scheduled.
 */
static void clear_sched_queues(void)
{
	odp_event_t ev;
	odp_buffer_t buf;

	while (1) {
		ev = odp_schedule(NULL, ODP_SCHED_NO_WAIT);

		if (ev == ODP_EVENT_INVALID)
			break;

		buf = odp_buffer_from_event(ev);
		odp_buffer_free(buf);
	}
}

/**
 * @internal Create a single queue from a pool of buffers
 *
 * @param thr  Thread
 * @param msg_pool  Buffer pool
 * @param prio   Queue priority
 *
 * @return 0 if successful
 */
static int create_queue(int thr, odp_pool_t msg_pool, int prio)
{
	char name[] = "sched_XX_00";
	odp_buffer_t buf;
	odp_queue_t queue;

	buf = odp_buffer_alloc(msg_pool);

	if (!odp_buffer_is_valid(buf)) {
		LOG_ERR("  [%i] msg_pool alloc failed\n", thr);
		return -1;
	}

	name[6] = '0' + prio/10;
	name[7] = '0' + prio - 10*(prio/10);

	queue = odp_queue_lookup(name);

	if (queue == ODP_QUEUE_INVALID) {
		LOG_ERR("  [%i] Queue %s lookup failed.\n", thr, name);
		return -1;
	}

	if (odp_queue_enq(queue, odp_buffer_to_event(buf))) {
		LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
		odp_buffer_free(buf);
		return -1;
	}

	return 0;
}

/**
 * @internal Create multiple queues from a pool of buffers
 *
 * @param thr  Thread
 * @param msg_pool  Buffer pool
 * @param prio   Queue priority
 *
 * @return 0 if successful
 */
static int create_queues(int thr, odp_pool_t msg_pool, int prio)
{
	char name[] = "sched_XX_YY";
	odp_buffer_t buf;
	odp_queue_t queue;
	int i;

	name[6] = '0' + prio/10;
	name[7] = '0' + prio - 10*(prio/10);

	/* Alloc and enqueue a buffer per queue */
	for (i = 0; i < QUEUES_PER_PRIO; i++) {
		name[9]  = '0' + i/10;
		name[10] = '0' + i - 10*(i/10);

		queue = odp_queue_lookup(name);

		if (queue == ODP_QUEUE_INVALID) {
			LOG_ERR("  [%i] Queue %s lookup failed.\n", thr,
				name);
			return -1;
		}

		buf = odp_buffer_alloc(msg_pool);

		if (!odp_buffer_is_valid(buf)) {
			LOG_ERR("  [%i] msg_pool alloc failed\n", thr);
			return -1;
		}

		if (odp_queue_enq(queue, odp_buffer_to_event(buf))) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			odp_buffer_free(buf);
			return -1;
		}
	}

	return 0;
}


/**
 * @internal Test single buffer alloc and free
 *
 * @param thr  Thread
 * @param pool Buffer pool
 *
 * @return 0 if successful
 */
static int test_alloc_single(int thr, odp_pool_t pool)
{
	int i;
	odp_buffer_t temp_buf;
	uint64_t t1, t2, cycles, ns;

	t1 = odp_time_cycles();

	for (i = 0; i < ALLOC_ROUNDS; i++) {
		temp_buf = odp_buffer_alloc(pool);

		if (!odp_buffer_is_valid(temp_buf)) {
			LOG_ERR("  [%i] alloc_single failed\n", thr);
			return -1;
		}

		odp_buffer_free(temp_buf);
	}

	t2     = odp_time_cycles();
	cycles = odp_time_diff_cycles(t1, t2);
	ns     = odp_time_cycles_to_ns(cycles);

	printf("  [%i] alloc_sng alloc+free   %"PRIu64" cycles, %"PRIu64" ns\n",
	       thr, cycles/ALLOC_ROUNDS, ns/ALLOC_ROUNDS);

	return 0;
}

/**
 * @internal Test multiple buffers alloc and free
 *
 * @param thr  Thread
 * @param pool Buffer pool
 *
 * @return 0 if successful
 */
static int test_alloc_multi(int thr, odp_pool_t pool)
{
	int i, j;
	odp_buffer_t temp_buf[MAX_ALLOCS];
	uint64_t t1, t2, cycles, ns;

	t1 = odp_time_cycles();

	for (i = 0; i < ALLOC_ROUNDS; i++) {
		for (j = 0; j < MAX_ALLOCS; j++) {
			temp_buf[j] = odp_buffer_alloc(pool);

			if (!odp_buffer_is_valid(temp_buf[j])) {
				LOG_ERR("  [%i] alloc_multi failed\n", thr);
				return -1;
			}
		}

		for (; j > 0; j--)
			odp_buffer_free(temp_buf[j-1]);
	}

	t2     = odp_time_cycles();
	cycles = odp_time_diff_cycles(t1, t2);
	ns     = odp_time_cycles_to_ns(cycles);

	printf("  [%i] alloc_multi alloc+free %"PRIu64" cycles, %"PRIu64" ns\n",
	       thr, cycles/(ALLOC_ROUNDS*MAX_ALLOCS),
	       ns/(ALLOC_ROUNDS*MAX_ALLOCS));

	return 0;
}

/**
 * @internal Test queue polling
 *
 * Enqueue to and dequeue to/from a single shared queue.
 *
 * @param thr      Thread
 * @param msg_pool Buffer pool
 *
 * @return 0 if successful
 */
static int test_poll_queue(int thr, odp_pool_t msg_pool)
{
	odp_event_t ev;
	odp_buffer_t buf;
	test_message_t *t_msg;
	odp_queue_t queue;
	uint64_t t1, t2, cycles, ns;
	int i;

	/* Alloc test message */
	buf = odp_buffer_alloc(msg_pool);

	if (!odp_buffer_is_valid(buf)) {
		LOG_ERR("  [%i] msg_pool alloc failed\n", thr);
		return -1;
	}

	/* odp_buffer_print(buf); */

	t_msg = odp_buffer_addr(buf);
	t_msg->msg_id = MSG_HELLO;
	t_msg->seq    = 0;

	queue = odp_queue_lookup("poll_queue");

	if (queue == ODP_QUEUE_INVALID) {
		printf("  [%i] Queue lookup failed.\n", thr);
		return -1;
	}

	t1 = odp_time_cycles();

	for (i = 0; i < QUEUE_ROUNDS; i++) {
		ev = odp_buffer_to_event(buf);

		if (odp_queue_enq(queue, ev)) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			odp_buffer_free(buf);
			return -1;
		}

		ev = odp_queue_deq(queue);

		buf = odp_buffer_from_event(ev);

		if (!odp_buffer_is_valid(buf)) {
			LOG_ERR("  [%i] Queue empty.\n", thr);
			return -1;
		}
	}

	t2     = odp_time_cycles();
	cycles = odp_time_diff_cycles(t1, t2);
	ns     = odp_time_cycles_to_ns(cycles);

	printf("  [%i] poll_queue enq+deq     %"PRIu64" cycles, %"PRIu64" ns\n",
	       thr, cycles/QUEUE_ROUNDS, ns/QUEUE_ROUNDS);

	odp_buffer_free(buf);
	return 0;
}

/**
 * @internal Test scheduling of a single queue - with odp_schedule()
 *
 * Enqueue a buffer to the shared queue. Schedule and enqueue the received
 * buffer back into the queue.
 *
 * @param str      Test case name string
 * @param thr      Thread
 * @param msg_pool Buffer pool
 * @param prio     Priority
 * @param barrier  Barrier
 *
 * @return 0 if successful
 */
static int test_schedule_single(const char *str, int thr,
				odp_pool_t msg_pool,
				int prio, odp_barrier_t *barrier)
{
	odp_event_t ev;
	odp_queue_t queue;
	uint64_t t1, t2, cycles, ns;
	uint32_t i;
	uint32_t tot = 0;

	if (create_queue(thr, msg_pool, prio))
		return -1;

	t1 = odp_time_cycles();

	for (i = 0; i < QUEUE_ROUNDS; i++) {
		ev = odp_schedule(&queue, ODP_SCHED_WAIT);

		if (odp_queue_enq(queue, ev)) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			odp_event_free(ev);
			return -1;
		}
	}

	/* Clear possible locally stored buffers */
	odp_schedule_pause();

	tot = i;

	while (1) {
		ev = odp_schedule(&queue, ODP_SCHED_NO_WAIT);

		if (ev == ODP_EVENT_INVALID)
			break;

		tot++;

		if (odp_queue_enq(queue, ev)) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			odp_event_free(ev);
			return -1;
		}
	}

	odp_schedule_resume();

	t2     = odp_time_cycles();
	cycles = odp_time_diff_cycles(t1, t2);
	ns     = odp_time_cycles_to_ns(cycles);

	odp_barrier_wait(barrier);
	clear_sched_queues();

	cycles = cycles/tot;
	ns     = ns/tot;

	printf("  [%i] %s enq+deq %"PRIu64" cycles, %"PRIu64" ns\n",
	       thr, str, cycles, ns);

	return 0;
}


/**
 * @internal Test scheduling of multiple queues - with odp_schedule()
 *
 * Enqueue a buffer to each queue. Schedule and enqueue the received
 * buffer back into the queue it came from.
 *
 * @param str      Test case name string
 * @param thr      Thread
 * @param msg_pool Buffer pool
 * @param prio     Priority
 * @param barrier  Barrier
 *
 * @return 0 if successful
 */
static int test_schedule_many(const char *str, int thr,
			      odp_pool_t msg_pool,
			      int prio, odp_barrier_t *barrier)
{
	odp_event_t ev;
	odp_queue_t queue;
	uint64_t t1 = 0;
	uint64_t t2 = 0;
	uint64_t cycles, ns;
	uint32_t i;
	uint32_t tot = 0;

	if (create_queues(thr, msg_pool, prio))
		return -1;

	/* Start sched-enq loop */
	t1 = odp_time_cycles();

	for (i = 0; i < QUEUE_ROUNDS; i++) {
		ev = odp_schedule(&queue, ODP_SCHED_WAIT);

		if (odp_queue_enq(queue, ev)) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			odp_event_free(ev);
			return -1;
		}
	}

	/* Clear possible locally stored buffers */
	odp_schedule_pause();

	tot = i;

	while (1) {
		ev = odp_schedule(&queue, ODP_SCHED_NO_WAIT);

		if (ev == ODP_EVENT_INVALID)
			break;

		tot++;

		if (odp_queue_enq(queue, ev)) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			odp_event_free(ev);
			return -1;
		}
	}

	odp_schedule_resume();

	t2     = odp_time_cycles();
	cycles = odp_time_diff_cycles(t1, t2);
	ns     = odp_time_cycles_to_ns(cycles);

	odp_barrier_wait(barrier);
	clear_sched_queues();

	cycles = cycles/tot;
	ns     = ns/tot;

	printf("  [%i] %s enq+deq %"PRIu64" cycles, %"PRIu64" ns\n",
	       thr, str, cycles, ns);

	return 0;
}

/**
 * @internal Test scheduling of multiple queues with multi_sched and multi_enq
 *
 * @param str      Test case name string
 * @param thr      Thread
 * @param msg_pool Buffer pool
 * @param prio     Priority
 * @param barrier  Barrier
 *
 * @return 0 if successful
 */
static int test_schedule_multi(const char *str, int thr,
			       odp_pool_t msg_pool,
			       int prio, odp_barrier_t *barrier)
{
	odp_event_t ev[MULTI_BUFS_MAX];
	odp_queue_t queue;
	uint64_t t1 = 0;
	uint64_t t2 = 0;
	uint64_t cycles, ns;
	int i, j;
	int num;
	uint32_t tot = 0;
	char name[] = "sched_XX_YY";

	name[6] = '0' + prio/10;
	name[7] = '0' + prio - 10*(prio/10);

	/* Alloc and enqueue a buffer per queue */
	for (i = 0; i < QUEUES_PER_PRIO; i++) {
		name[9]  = '0' + i/10;
		name[10] = '0' + i - 10*(i/10);

		queue = odp_queue_lookup(name);

		if (queue == ODP_QUEUE_INVALID) {
			LOG_ERR("  [%i] Queue %s lookup failed.\n", thr,
				name);
			return -1;
		}

		for (j = 0; j < MULTI_BUFS_MAX; j++) {
			odp_buffer_t buf;

			buf = odp_buffer_alloc(msg_pool);

			if (!odp_buffer_is_valid(buf)) {
				LOG_ERR("  [%i] msg_pool alloc failed\n",
					thr);
				return -1;
			}

			ev[j] = odp_buffer_to_event(buf);
		}

		/* Assume we can enqueue all events */
		num = odp_queue_enq_multi(queue, ev, MULTI_BUFS_MAX);
		if (num != MULTI_BUFS_MAX) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			j = num < 0 ? 0 : num;
			for ( ; j < MULTI_BUFS_MAX; j++)
				odp_event_free(ev[j]);

			return -1;
		}
	}

	/* Start sched-enq loop */
	t1 = odp_time_cycles();

	for (i = 0; i < QUEUE_ROUNDS; i++) {
		num = odp_schedule_multi(&queue, ODP_SCHED_WAIT, ev,
					 MULTI_BUFS_MAX);

		tot += num;

		/* Assume we can enqueue all events */
		if (odp_queue_enq_multi(queue, ev, num) != num) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			return -1;
		}
	}

	/* Clear possible locally stored events */
	odp_schedule_pause();

	while (1) {
		num = odp_schedule_multi(&queue, ODP_SCHED_NO_WAIT, ev,
					 MULTI_BUFS_MAX);

		if (num == 0)
			break;

		tot += num;

		/* Assume we can enqueue all events */
		if (odp_queue_enq_multi(queue, ev, num) != num) {
			LOG_ERR("  [%i] Queue enqueue failed.\n", thr);
			return -1;
		}
	}

	odp_schedule_resume();


	t2     = odp_time_cycles();
	cycles = odp_time_diff_cycles(t1, t2);
	ns     = odp_time_cycles_to_ns(cycles);

	odp_barrier_wait(barrier);
	clear_sched_queues();

	if (tot) {
		cycles = cycles/tot;
		ns     = ns/tot;
	} else {
		cycles = 0;
		ns     = 0;
	}

	printf("  [%i] %s enq+deq %"PRIu64" cycles, %"PRIu64" ns\n",
	       thr, str, cycles, ns);

	return 0;
}

/**
 * @internal Worker thread
 *
 * @param arg  Arguments
 *
 * @return NULL on failure
 */
static void *run_thread(void *arg)
{
	int thr;
	odp_pool_t msg_pool;
	odp_shm_t shm;
	test_globals_t *globals;
	odp_barrier_t *barrier;

	thr = odp_thread_id();

	printf("Thread %i starts on CPU %i\n", thr, odp_cpu_id());

	shm     = odp_shm_lookup("test_globals");
	globals = odp_shm_addr(shm);

	if (globals == NULL) {
		LOG_ERR("Shared mem lookup failed\n");
		return NULL;
	}

	barrier = &globals->barrier;

	/*
	 * Test barriers back-to-back
	 */
	odp_barrier_wait(barrier);
	odp_barrier_wait(barrier);
	odp_barrier_wait(barrier);
	odp_barrier_wait(barrier);

	/*
	 * Find the buffer pool
	 */
	msg_pool = odp_pool_lookup("msg_pool");

	if (msg_pool == ODP_POOL_INVALID) {
		LOG_ERR("  [%i] msg_pool not found\n", thr);
		return NULL;
	}

	odp_barrier_wait(barrier);

	if (test_alloc_single(thr, msg_pool))
		return NULL;

	odp_barrier_wait(barrier);

	if (test_alloc_multi(thr, msg_pool))
		return NULL;

	odp_barrier_wait(barrier);

	if (test_poll_queue(thr, msg_pool))
		return NULL;

	/* Low prio */

	odp_barrier_wait(barrier);

	if (test_schedule_single("sched_____s_lo", thr, msg_pool,
				 ODP_SCHED_PRIO_LOWEST, barrier))
		return NULL;

	odp_barrier_wait(barrier);

	if (test_schedule_many("sched_____m_lo", thr, msg_pool,
			       ODP_SCHED_PRIO_LOWEST, barrier))
		return NULL;

	odp_barrier_wait(barrier);

	if (test_schedule_multi("sched_multi_lo", thr, msg_pool,
				ODP_SCHED_PRIO_LOWEST, barrier))
		return NULL;

	/* High prio */

	odp_barrier_wait(barrier);

	if (test_schedule_single("sched_____s_hi", thr, msg_pool,
				 ODP_SCHED_PRIO_HIGHEST, barrier))
		return NULL;

	odp_barrier_wait(barrier);

	if (test_schedule_many("sched_____m_hi", thr, msg_pool,
			       ODP_SCHED_PRIO_HIGHEST, barrier))
		return NULL;

	odp_barrier_wait(barrier);

	if (test_schedule_multi("sched_multi_hi", thr, msg_pool,
				ODP_SCHED_PRIO_HIGHEST, barrier))
		return NULL;


	printf("Thread %i exits\n", thr);
	fflush(NULL);
	return arg;
}

/**
 * @internal Test cycle counter accuracy
 */
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)) {
		LOG_ERR("clock_gettime failed.\n");
		return;
	}

	printf("\nTime accuracy test (%i sec)\n", TEST_SEC);

	do {
		if (clock_gettime(CLOCK_MONOTONIC, &tp1)) {
			LOG_ERR("clock_gettime failed.\n");
			return;
		}

	} while (tp1.tv_sec == tp2.tv_sec);

	t1 = odp_time_cycles();

	do {
		if (clock_gettime(CLOCK_MONOTONIC, &tp2)) {
			LOG_ERR("clock_gettime failed.\n");
			return;
		}

	} while ((tp2.tv_sec - tp1.tv_sec) < TEST_SEC);

	t2 = odp_time_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_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");
}

/**
 * @internal Print help
 */
static void print_usage(void)
{
	printf("\n\nUsage: ./odp_example [options]\n");
	printf("Options:\n");
	printf("  -c, --count <number>    CPU count\n");
	printf("  -h, --help              this help\n");
	printf("  --proc                  process mode\n");
	printf("\n\n");
}

/**
 * @internal Parse arguments
 *
 * @param argc  Argument count
 * @param argv  Argument vector
 * @param args  Test arguments
 */
static void parse_args(int argc, char *argv[], test_args_t *args)
{
	int opt;
	int long_index;

	static struct option longopts[] = {
		{"count", required_argument, NULL, 'c'},
		{"help", no_argument, NULL, 'h'},
		{"proc", no_argument, NULL, 0},
		{NULL, 0, NULL, 0}
	};

	while (1) {
		opt = getopt_long(argc, argv, "+c:h", longopts, &long_index);

		if (opt == -1)
			break;	/* No more options */

		switch (opt) {
		case 0:
			args->proc_mode = 1;
			break;

		case 'c':
			args->cpu_count = atoi(optarg);
			break;

		case 'h':
			print_usage();
			exit(EXIT_SUCCESS);
			break;

		default:
			break;
		}
	}
}


/**
 * Test main function
 */
int main(int argc, char *argv[])
{
	odph_linux_pthread_t thread_tbl[MAX_WORKERS];
	test_args_t args;
	int num_workers;
	odp_cpumask_t cpumask;
	odp_pool_t pool;
	odp_queue_t queue;
	int i, j;
	int prios;
	odp_shm_t shm;
	test_globals_t *globals;
	char cpumaskstr[ODP_CPUMASK_STR_SIZE];
	odp_pool_param_t params;

	printf("\nODP example starts\n\n");

	memset(&args, 0, sizeof(args));
	parse_args(argc, argv, &args);

	if (args.proc_mode)
		printf("Process mode\n");
	else
		printf("Thread mode\n");

	memset(thread_tbl, 0, sizeof(thread_tbl));

	/* ODP global init */
	if (odp_init_global(NULL, NULL)) {
		LOG_ERR("ODP global init failed.\n");
		return -1;
	}

	/*
	 * Init this thread. It makes also ODP calls when
	 * setting up resources for worker threads.
	 */
	if (odp_init_local(ODP_THREAD_CONTROL)) {
		LOG_ERR("ODP global init failed.\n");
		return -1;
	}

	printf("\n");
	printf("ODP system info\n");
	printf("---------------\n");
	printf("ODP API version: %s\n",        odp_version_api_str());
	printf("CPU model:       %s\n",        odp_sys_cpu_model_str());
	printf("CPU freq (hz):   %"PRIu64"\n", odp_sys_cpu_hz());
	printf("Cache line size: %i\n",        odp_sys_cache_line_size());
	printf("Max CPU count:   %i\n",        odp_cpu_count());

	printf("\n");

	/* Default to system CPU count unless user specified */
	num_workers = MAX_WORKERS;
	if (args.cpu_count)
		num_workers = args.cpu_count;

	/*
	 * By default CPU #0 runs Linux kernel background tasks.
	 * Start mapping thread from CPU #1
	 */
	num_workers = odph_linux_cpumask_default(&cpumask, num_workers);
	(void)odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr));

	printf("num worker threads: %i\n", num_workers);
	printf("first CPU:          %i\n", odp_cpumask_first(&cpumask));
	printf("cpu mask:           %s\n", cpumaskstr);

	/* Test cycle count accuracy */
	test_time();

	shm = odp_shm_reserve("test_globals",
			      sizeof(test_globals_t), ODP_CACHE_LINE_SIZE, 0);

	globals = odp_shm_addr(shm);

	if (globals == NULL) {
		LOG_ERR("Shared memory reserve failed.\n");
		return -1;
	}

	memset(globals, 0, sizeof(test_globals_t));

	/*
	 * Create message pool
	 */

	params.buf.size  = sizeof(test_message_t);
	params.buf.align = 0;
	params.buf.num   = MSG_POOL_SIZE/sizeof(test_message_t);
	params.type      = ODP_POOL_BUFFER;

	pool = odp_pool_create("msg_pool", &params);

	if (pool == ODP_POOL_INVALID) {
		LOG_ERR("Pool create failed.\n");
		return -1;
	}

	/* odp_pool_print(pool); */

	/*
	 * Create a queue for direct poll test
	 */
	queue = odp_queue_create("poll_queue", ODP_QUEUE_TYPE_POLL, NULL);

	if (queue == ODP_QUEUE_INVALID) {
		LOG_ERR("Poll queue create failed.\n");
		return -1;
	}

	/*
	 * Create queues for schedule test. QUEUES_PER_PRIO per priority.
	 */
	prios = odp_schedule_num_prio();

	for (i = 0; i < prios; i++) {
		if (i != ODP_SCHED_PRIO_HIGHEST &&
		    i != ODP_SCHED_PRIO_LOWEST)
			continue;

		odp_queue_param_t param;
		char name[] = "sched_XX_YY";

		name[6] = '0' + i/10;
		name[7] = '0' + i - 10*(i/10);

		param.sched.prio  = i;
		param.sched.sync  = ODP_SCHED_SYNC_ATOMIC;
		param.sched.group = ODP_SCHED_GROUP_DEFAULT;

		for (j = 0; j < QUEUES_PER_PRIO; j++) {
			name[9]  = '0' + j/10;
			name[10] = '0' + j - 10*(j/10);

			queue = odp_queue_create(name, ODP_QUEUE_TYPE_SCHED,
						 &param);

			if (queue == ODP_QUEUE_INVALID) {
				LOG_ERR("Schedule queue create failed.\n");
				return -1;
			}
		}
	}

	odp_shm_print_all();

	/* Barrier to sync test case execution */
	odp_barrier_init(&globals->barrier, num_workers);

	if (args.proc_mode) {
		int ret;
		odph_linux_process_t proc[MAX_WORKERS];

		/* Fork worker processes */
		ret = odph_linux_process_fork_n(proc, &cpumask);

		if (ret < 0) {
			LOG_ERR("Fork workers failed %i\n", ret);
			return -1;
		}

		if (ret == 0) {
			/* Child process */
			run_thread(NULL);
		} else {
			/* Parent process */
			odph_linux_process_wait_n(proc, num_workers);
			printf("ODP example complete\n\n");
		}

	} else {
		/* Create and launch worker threads */
		odph_linux_pthread_create(thread_tbl, &cpumask,
					  run_thread, NULL);

		/* Wait for worker threads to terminate */
		odph_linux_pthread_join(thread_tbl, num_workers);

		printf("ODP example complete\n\n");
	}

	return 0;
}