aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_timer_wheel.c
blob: aee65e82d1a2ec606f335d9f170ad73f4b7bd13a (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2015 EZchip Semiconductor Ltd.
 * Copyright (c) 2015-2018 Linaro Limited
 */

#include <stdint.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <inttypes.h>
#include <odp_timer_wheel_internal.h>
#include <odp_traffic_mngr_internal.h>
#include <odp_debug_internal.h>
#include <odp_macros_internal.h>

/* The following constants can be changed either at compile time or run time
 * as long as the following constraints are met (by the way REV stands for
 * REVOLUTION, i.e. one complete sweep through a specific timer wheel):
 */
#define TIME_TO_TICKS_SHIFT    10
#define TIME_PER_TICK          BIT(TIME_TO_TICKS_SHIFT)
#define CURRENT_TIMER_SLOTS    1024
#define LEVEL1_TIMER_SLOTS     2048
#define LEVEL2_TIMER_SLOTS     1024
#define LEVEL3_TIMER_SLOTS     1024

/* The following values are derived and should generally not be changed.  The
 * basic idea is that the ticks per current timer wheel slot should always be
 * 1, since that defines what a tick is - namely the time period of a single
 * current timer wheel slot.  Then for all levels (including current), the
 * ticks per revolution is clearly equal to the ticks per slot times the
 * number of slots.  Finally the ticks per slot for levels 1 through 3, must be
 * the ticks per revolution of the previous level divided by a small power of
 * 2 - e.g. 2, 4, 8, 16 - (but not 1).  The idea being that when an upper
 * level slot is processed, its entries will be spread across this fraction of
 * the lower level wheel and we want to make sure that none of these entries
 * is near the lower level's current index.  Currently we use 4 for all
 * levels.
 */
#define CURRENT_GEAR_RATIO     4
#define LEVEL1_GEAR_RATIO      4
#define LEVEL2_GEAR_RATIO      4
#define TICKS_PER_CURRENT_SLOT 1
#define TICKS_PER_CURRENT_REV  (CURRENT_TIMER_SLOTS * TICKS_PER_CURRENT_SLOT)
#define TICKS_PER_LEVEL1_SLOT  (TICKS_PER_CURRENT_REV / CURRENT_GEAR_RATIO)
#define TICKS_PER_LEVEL1_REV   (LEVEL1_TIMER_SLOTS * TICKS_PER_LEVEL1_SLOT)
#define TICKS_PER_LEVEL2_SLOT  (TICKS_PER_LEVEL1_REV / LEVEL1_GEAR_RATIO)
#define TICKS_PER_LEVEL2_REV   (LEVEL2_TIMER_SLOTS * TICKS_PER_LEVEL2_SLOT)
#define TICKS_PER_LEVEL3_SLOT  (TICKS_PER_LEVEL2_REV / LEVEL2_GEAR_RATIO)
#define TICKS_PER_LEVEL3_REV   (LEVEL3_TIMER_SLOTS * TICKS_PER_LEVEL3_SLOT)

#define EXPIRED_RING_ENTRIES  256

typedef struct timer_blk_s timer_blk_t;

typedef struct { /* Should be 120 bytes long */
	uint64_t user_data[15];
} current_blk_t;

typedef struct { /* Should be 120 bytes long */
	uint64_t user_data[10];
	uint32_t wakeup32[10];
} general_blk_t;

/* Must be exactly 128 bytes long AND cacheline aligned! */
struct timer_blk_s {
	timer_blk_t *next_timer_blk;
	union {
		current_blk_t current_blk;
		general_blk_t general_blk;
	};
};

/* The following record type is only needed/used to free the timer_blks. */
typedef struct blk_of_timer_blks_desc_s blk_of_timer_blks_desc_t;
struct blk_of_timer_blks_desc_s {
	blk_of_timer_blks_desc_t *next;
	timer_blk_t              *block_of_timer_blks;
};

/* Each current_timer_slot is 8 bytes long. */
typedef union {
       /* The selection of which union element to use is based on the LSB
	* bit, under the required assumption that both of these pointers are
	* at least 8-byte aligned.  If the entire 8 bytes is zero, then this
	* entry is empty, otherwise a LSB bit of 0 indicates a timer_blk_list
	* and a LSB bit of 1 indicates a single entry with a 64-bit user_data
	* word.
	*/
	uint64_t     user_data;
	timer_blk_t *timer_blk_list;
} current_timer_slot_t;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
typedef struct {
	current_timer_slot_t slots[0];
} current_wheel_t;
#pragma GCC diagnostic pop

typedef struct {
	uint32_t             count;
	uint32_t             peak_count;
	uint32_t             expired_ring_full_cnt;
	uint32_t             head_idx;
	uint32_t             tail_idx;
	uint32_t             max_idx;
	current_timer_slot_t entries[];
} expired_ring_t;

typedef struct {
	uint32_t kind;                  /* 1 for single_entry_t */
	uint32_t wakeup32;
	uint64_t user_data;
} single_entry_t;

typedef struct {
	uint32_t     kind;              /* 2 for list_entry_t */
	uint32_t     num_blks;
	timer_blk_t *timer_blk_list;
} list_entry_t;

typedef union  { /* Each general_timer_slot is 16 bytes long. */
       /* The selection of which union element to use is based on the first 4
	* byte field which is always the "kind".  If kind is 0, then this
	* slot is empty, else if the kind is 1 then it is a single_entry and
	* if the kind is 2 then it is a list_entry.
	*/
	single_entry_t single_entry;
	list_entry_t   list_entry;
} general_timer_slot_t;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
typedef struct {
	general_timer_slot_t slots[0];
} general_wheel_t;
#pragma GCC diagnostic pop

typedef struct {
       /* Note that rev stands for revolution - one complete sweep through
	* this timer wheel.
	*/
	uint16_t num_slots;        /* Must be a power of 2. */
	uint16_t slot_idx;
	uint16_t gear_mask;        /* = (num_slots / gear_ratio) - 1 */
	uint16_t gear_ratio;       /* num of next level wheel updates per this
				    * rev
				    */
	uint32_t ticks_shift;
	uint32_t ticks_per_slot;   /* Must be a power of 2. */
	uint64_t ticks_per_rev;    /* = num_slots * ticks_per_slot */
	uint64_t max_ticks;        /* = current_ticks + ticks_per_rev */
} wheel_desc_t;

typedef struct {
	uint32_t          last_slot_served;
	uint32_t          free_list_size;
	uint32_t          min_free_list_size;
	uint32_t          peak_free_list_size;
	uint32_t          current_cnt;
	timer_blk_t      *free_list_head;
	uint64_t          total_timer_inserts;
	uint64_t          insert_fail_cnt;
	uint64_t          total_timer_removes;
	uint64_t          total_promote_cnt;
	uint64_t          promote_fail_cnt;
	uint64_t          current_ticks;
	wheel_desc_t      wheel_descs[4];
	current_wheel_t  *current_wheel;
	general_wheel_t  *general_wheels[3];
	expired_ring_t   *expired_timers_ring;
	tm_system_t      *tm_system;

	blk_of_timer_blks_desc_t *timer_blks_list_head;
} timer_wheels_t;

static uint32_t _odp_internal_ilog2(uint64_t value)
{
	uint64_t pwr_of_2;
	uint32_t bit_shift;

	if (value == 0)
		return 64;

	for (bit_shift = 0; bit_shift < 64; bit_shift++) {
		pwr_of_2 = 1ULL << bit_shift;
		if (value == pwr_of_2)
			return bit_shift;
		else if (value < pwr_of_2)
			return bit_shift - 1;
	}

	return 64;
}

static current_wheel_t *current_wheel_alloc(timer_wheels_t *timer_wheels,
					    uint32_t        desc_idx)
{
	current_wheel_t *current_wheel;
	wheel_desc_t    *wheel_desc;
	uint32_t         num_slots, malloc_len;

	wheel_desc    = &timer_wheels->wheel_descs[desc_idx];
	num_slots     = wheel_desc->num_slots;
	malloc_len    = num_slots * sizeof(current_timer_slot_t);
	current_wheel = malloc(malloc_len);
	memset(current_wheel, 0, malloc_len);

	wheel_desc->slot_idx      = 0;
	wheel_desc->ticks_per_rev = num_slots;
	wheel_desc->ticks_shift   = 0;
	wheel_desc->max_ticks     = 0;
	wheel_desc->gear_mask     = (num_slots / wheel_desc->gear_ratio) - 1;
	return current_wheel;
}

static void current_wheel_start(timer_wheels_t *timer_wheels,
				uint32_t        desc_idx,
				uint64_t        current_ticks)
{
	wheel_desc_t *wheel_desc;
	uint32_t      wheel_idx;

	wheel_desc = &timer_wheels->wheel_descs[desc_idx];
	wheel_idx  = current_ticks & (wheel_desc->num_slots - 1);

	wheel_desc->slot_idx  = wheel_idx;
	wheel_desc->max_ticks = wheel_idx + wheel_desc->ticks_per_rev;
}

static general_wheel_t *general_wheel_alloc(timer_wheels_t *timer_wheels,
					    uint32_t        desc_idx)
{
	general_wheel_t *general_wheel;
	wheel_desc_t    *wheel_desc;
	uint64_t         ticks_per_slot;
	uint32_t         num_slots, ticks_shift, malloc_len;

	wheel_desc     = &timer_wheels->wheel_descs[desc_idx];
	num_slots      = wheel_desc->num_slots;
	ticks_per_slot = (uint64_t)wheel_desc->ticks_per_slot;
	ticks_shift    = _odp_internal_ilog2(ticks_per_slot);
	malloc_len     = num_slots * sizeof(general_timer_slot_t);
	general_wheel  = malloc(malloc_len);
	memset(general_wheel, 0, malloc_len);

	wheel_desc->slot_idx      = 0;
	wheel_desc->ticks_per_rev = num_slots * ticks_per_slot;
	wheel_desc->ticks_shift   = ticks_shift;
	wheel_desc->max_ticks     = 0;
	wheel_desc->gear_mask     = (num_slots / wheel_desc->gear_ratio) - 1;
	return general_wheel;
}

static void general_wheel_start(timer_wheels_t *timer_wheels,
				uint32_t        desc_idx,
				uint64_t        current_ticks)
{
	wheel_desc_t *wheel_desc;
	uint32_t      num_slots, ticks_shift, wheel_idx;

	wheel_desc  = &timer_wheels->wheel_descs[desc_idx];
	num_slots   = wheel_desc->num_slots;
	ticks_shift = wheel_desc->ticks_shift;
	wheel_idx   = (current_ticks >> ticks_shift) & (num_slots - 1);

	wheel_desc->slot_idx  = wheel_idx;
	wheel_desc->max_ticks = wheel_idx + wheel_desc->ticks_per_rev;
}

static int expired_ring_create(timer_wheels_t *timer_wheels,
			       uint32_t        num_entries)
{
	expired_ring_t *expired_ring;
	uint32_t        malloc_len;

	malloc_len = sizeof(expired_ring_t) +
		(sizeof(current_timer_slot_t) * num_entries);
	expired_ring = malloc(malloc_len);
	memset(expired_ring, 0, malloc_len);
	expired_ring->max_idx = num_entries - 1;

	timer_wheels->expired_timers_ring = expired_ring;
	return 0;
}

static int free_list_add(timer_wheels_t *timer_wheels,
			 uint32_t        num_timer_blks)
{
	blk_of_timer_blks_desc_t *blk_of_timer_blks_desc;
	timer_blk_t              *block_of_timer_blks, *timer_blk;
	timer_blk_t              *next_timer_blk;
	uint32_t                  malloc_len, idx;

	/* Should be cacheline aligned! */
	malloc_len          = num_timer_blks * sizeof(timer_blk_t);
	block_of_timer_blks = malloc(malloc_len);
	if (!block_of_timer_blks)
		return -1;

	memset(block_of_timer_blks, 0, malloc_len);

	/* Now malloc, initialize and link a descriptor record describing this
	 * block_of_timer_blks allocation done above.  This is only used to
	 * free this memory. */
	blk_of_timer_blks_desc = malloc(sizeof(blk_of_timer_blks_desc_t));
	memset(blk_of_timer_blks_desc, 0, sizeof(blk_of_timer_blks_desc_t));
	blk_of_timer_blks_desc->block_of_timer_blks = block_of_timer_blks;
	blk_of_timer_blks_desc->next = timer_wheels->timer_blks_list_head;
	timer_wheels->timer_blks_list_head = blk_of_timer_blks_desc;

	/* Link these timer_blks together. */
	timer_blk      = block_of_timer_blks;
	next_timer_blk = timer_blk + 1;
	for (idx = 0; idx < num_timer_blks - 1; idx++) {
		timer_blk->next_timer_blk = next_timer_blk;
		timer_blk = next_timer_blk;
		next_timer_blk++;
	}

	timer_blk->next_timer_blk     = timer_wheels->free_list_head;
	timer_wheels->free_list_size += num_timer_blks;
	timer_wheels->free_list_head  = block_of_timer_blks;
	if (timer_wheels->peak_free_list_size < timer_wheels->free_list_size)
		timer_wheels->peak_free_list_size =
			timer_wheels->free_list_size;
	return 0;
}

static timer_blk_t *timer_blk_alloc(timer_wheels_t *timer_wheels)
{
	timer_blk_t *head_timer_blk;
	int         rc;

	if (timer_wheels->free_list_size <= 1) {
		/* Replenish the timer_blk_t free list. */
		timer_wheels->min_free_list_size = timer_wheels->free_list_size;
		rc = free_list_add(timer_wheels, 1024);
		if (rc < 0)
			return NULL;
	}

	head_timer_blk = timer_wheels->free_list_head;
	timer_wheels->free_list_size--;
	timer_wheels->free_list_head = head_timer_blk->next_timer_blk;
	if (timer_wheels->free_list_size < timer_wheels->min_free_list_size)
		timer_wheels->min_free_list_size = timer_wheels->free_list_size;

	memset(head_timer_blk, 0, sizeof(timer_blk_t));
	return head_timer_blk;
}

static void timer_blk_free(timer_wheels_t *timer_wheels,
			   timer_blk_t    *timer_blk)
{
	timer_blk->next_timer_blk = timer_wheels->free_list_head;
	timer_wheels->free_list_head = timer_blk;
	timer_wheels->free_list_size++;
	if (timer_wheels->peak_free_list_size < timer_wheels->free_list_size)
		timer_wheels->peak_free_list_size =
			timer_wheels->free_list_size;
}

static int current_wheel_insert(timer_wheels_t *timer_wheels,
				uint64_t        rel_ticks,
				uint64_t        user_data)
{
	current_timer_slot_t *timer_slot;
	current_wheel_t      *current_wheel;
	wheel_desc_t         *wheel_desc;
	timer_blk_t          *new_timer_blk, *timer_blk;
	uint64_t              num_slots, slot_idx;
	uint32_t              wheel_idx, idx;

       /* To reach here it must be the case that (wakeup_ticks -
	* current_ticks) is < 2 * current_wheel->num_slots.
	*/
	wheel_desc    = &timer_wheels->wheel_descs[0];
	current_wheel = timer_wheels->current_wheel;
	slot_idx      = wheel_desc->slot_idx;
	num_slots     = (uint64_t)wheel_desc->num_slots;
	wheel_idx     = (uint32_t)((slot_idx + rel_ticks) & (num_slots - 1));
	timer_slot    = &current_wheel->slots[wheel_idx];

       /* Three cases: (a) the timer_slot is currently empty, (b) the
	* timer_slot contains a single user_data word directly inside it or
	* (c) the timer_slot contains a pointer to a linked list of
	* timer_blk's.
	*/
	if (timer_slot->user_data == 0) {                    /* case (a) */
		timer_slot->user_data = user_data | 0x01;
	} else if ((timer_slot->user_data & 0x3) == 0x01)  { /* case (b) */
	       /* Need to promote this pre-existing single user_data plus the
		* new user_data into a timer_blk with two entries.
		*/
		new_timer_blk = timer_blk_alloc(timer_wheels);
		if (!new_timer_blk)
			return -1;

		new_timer_blk->next_timer_blk           = NULL;
		new_timer_blk->current_blk.user_data[0] = timer_slot->user_data;
		new_timer_blk->current_blk.user_data[1] = user_data;
		timer_slot->timer_blk_list              = new_timer_blk;
	} else {   /* case (c) */
		/* First try to find an empty slot in the current timer_blk. */
		timer_blk = timer_slot->timer_blk_list;
		for (idx = 0; idx < 15; idx++) {
			if (timer_blk->current_blk.user_data[idx] == 0) {
				timer_blk->current_blk.user_data[idx] =
					user_data;
				return 0;
			}
		}

	       /* current timer_blk is full, so we need to allocate a new one
		* and link it in.
		*/
		new_timer_blk = timer_blk_alloc(timer_wheels);
		if (!new_timer_blk)
			return -1;

		new_timer_blk->next_timer_blk           = timer_blk;
		new_timer_blk->current_blk.user_data[0] = user_data;
		timer_slot->timer_blk_list              = new_timer_blk;
	}

	return 0;
}

static int general_wheel_insert(timer_wheels_t *timer_wheels,
				uint32_t        desc_idx,
				uint64_t        wakeup_ticks,
				uint64_t        rel_ticks,
				uint64_t        user_data)
{
	general_timer_slot_t *timer_slot;
	general_wheel_t      *general_wheel;
	wheel_desc_t         *wheel_desc;
	timer_blk_t          *new_timer_blk, *timer_blk;
	uint64_t              num_slots, old_user_data;
	uint32_t              slot_idx, wheel_idx, wakeup32, kind;
	uint32_t              old_wakeup32, idx;

	/* To reach here it must be the case that
	 * "(wakeup_ticks - current_ticks) >> ticks_shift < 2 * num_slots".
	 */
	wheel_desc    = &timer_wheels->wheel_descs[desc_idx];
	general_wheel = timer_wheels->general_wheels[desc_idx - 1];
	slot_idx      = wheel_desc->slot_idx;
	num_slots     = (uint64_t)wheel_desc->num_slots;
	wakeup32      = (uint32_t)(wakeup_ticks & 0xFFFFFFFF);
	rel_ticks     = rel_ticks >> wheel_desc->ticks_shift;
	wheel_idx     = (uint32_t)((slot_idx + rel_ticks) & (num_slots - 1));
	timer_slot    = &general_wheel->slots[wheel_idx];
	kind          = timer_slot->single_entry.kind;

       /* Three cases: (a) the timer_slot is currently empty, (b) the
	* timer_slot contains a single user_data word directly inside it or
	* (c) the timer_slot contains a pointer to a linked list of
	* timer_blk's.
	*/
	if (kind == 0) {                 /* case (a) */
		timer_slot->single_entry.kind      = 1;
		timer_slot->single_entry.wakeup32  = wakeup32;
		timer_slot->single_entry.user_data = user_data;
	} else if (kind == 1) {          /* case (b) */
	       /* Need to promote this single entry plus the new user_data
		* into a timer_blk with two entries.
		*/
		new_timer_blk = timer_blk_alloc(timer_wheels);
		if (!new_timer_blk)
			return -1;

		old_user_data = timer_slot->single_entry.user_data;
		old_wakeup32  = timer_slot->single_entry.wakeup32;

		new_timer_blk->next_timer_blk           = NULL;
		new_timer_blk->general_blk.user_data[0] = old_user_data;
		new_timer_blk->general_blk.wakeup32[0]  = old_wakeup32;
		new_timer_blk->general_blk.user_data[1] = user_data;
		new_timer_blk->general_blk.wakeup32[1]  = wakeup32;
		timer_slot->list_entry.kind             = 2;
		timer_slot->list_entry.num_blks         = 1;
		timer_slot->list_entry.timer_blk_list   = new_timer_blk;
	} else  { /* case (c) */
		/* First try to find an empty slot in the first timer_blk. */
		timer_blk = timer_slot->list_entry.timer_blk_list;
		for (idx = 0; idx < 10; idx++) {
			if (timer_blk->general_blk.user_data[idx] == 0) {
				timer_blk->general_blk.user_data[idx] =
					user_data;
				timer_blk->general_blk.wakeup32[idx]  =
					wakeup32;
				return 0;
			}
		}

	       /* The first timer_blk is full, so we need to allocate a new
		* one and link it in.
		*/
		new_timer_blk = timer_blk_alloc(timer_wheels);
		if (!new_timer_blk)
			return -1;

		new_timer_blk->next_timer_blk           = timer_blk;
		new_timer_blk->general_blk.user_data[0] = user_data;
		new_timer_blk->general_blk.wakeup32[0]  = wakeup32;
		timer_slot->list_entry.kind             = 2;
		timer_slot->list_entry.num_blks++;
		timer_slot->list_entry.timer_blk_list   = new_timer_blk;
	}

	return 0;
}

static int expired_timers_append(timer_wheels_t       *timer_wheels,
				 current_timer_slot_t *timer_slot)
{
	expired_ring_t *expired_ring;
	uint32_t        tail_idx;

	/* Append either this single entry or the entire timer_blk_list to the
	 * ring of expired_timers.
	 */
	expired_ring = timer_wheels->expired_timers_ring;
	if (expired_ring->max_idx <= expired_ring->count)
		return -1;

	tail_idx = expired_ring->tail_idx;
	expired_ring->entries[tail_idx] = *timer_slot;
	tail_idx++;
	if (expired_ring->max_idx < tail_idx)
		tail_idx = 0;

	expired_ring->tail_idx = tail_idx;
	expired_ring->count++;
	if (expired_ring->peak_count < expired_ring->count)
		expired_ring->peak_count = expired_ring->count;
	return 0;
}

static int timer_blk_list_search(timer_wheels_t       *timer_wheels,
				 current_timer_slot_t *head_entry,
				 uint64_t             *user_data_ptr)
{
	timer_blk_t *timer_blk, *next_timer_blk;
	uint64_t     user_data;
	uint32_t     idx;

	timer_blk = head_entry->timer_blk_list;
	while (timer_blk) {
		for (idx = 0; idx < 15; idx++) {
			user_data = timer_blk->current_blk.user_data[idx];
			if (user_data != 0) {
				*user_data_ptr =
					timer_blk->current_blk.user_data[idx];
				timer_blk->current_blk.user_data[idx] = 0;
				return 1;
			}
		}

		next_timer_blk = timer_blk->next_timer_blk;
		timer_blk_free(timer_wheels, timer_blk);
		timer_blk = next_timer_blk;
	}

	return 0;
}

static int expired_timers_remove(timer_wheels_t *timer_wheels,
				 uint64_t       *user_data_ptr)
{
	current_timer_slot_t *head_entry;
	expired_ring_t       *expired_ring;
	uint32_t              count, head_idx;
	int                   rc;

	expired_ring = timer_wheels->expired_timers_ring;
	for (count = 1; count <= 2; count++) {
		if (expired_ring->count == 0)
			return 0;

		head_idx   = expired_ring->head_idx;
		head_entry = &expired_ring->entries[head_idx];
		if ((head_entry->user_data & 0x3) == 1) {
			*user_data_ptr = head_entry->user_data;
			expired_ring->entries[head_idx].user_data = 0;
			head_idx++;
			if (expired_ring->max_idx < head_idx)
				head_idx = 0;

			expired_ring->head_idx = head_idx;
			expired_ring->count--;
			return 1;
		}

		/* Search timer_blk_list for non-empty user_data values. */
		rc = timer_blk_list_search(timer_wheels, head_entry,
					   user_data_ptr);
		if (0 < rc)
			return 1;

		/* Advance to use the next ring entry. */
		expired_ring->entries[head_idx].user_data = 0;
		head_idx++;
		if (expired_ring->max_idx < head_idx)
			head_idx = 0;

		expired_ring->head_idx = head_idx;
		expired_ring->count--;
	}

	return 0;
}

static int timer_current_wheel_update(timer_wheels_t *timer_wheels,
				      uint32_t        elapsed_ticks)
{
	current_timer_slot_t *timer_slot;
	current_wheel_t      *current_wheel;
	wheel_desc_t         *wheel_desc;
	uint64_t              max_ticks;
	uint32_t              num_slots, slot_idx, max_cnt, cnt;
	int                   ret_code, rc;

	/* Advance current wheel for each elapsed tick, up to a maximum. */
	wheel_desc    = &timer_wheels->wheel_descs[0];
	slot_idx      = wheel_desc->slot_idx;
	num_slots     = wheel_desc->num_slots;
	max_ticks     = wheel_desc->max_ticks;
	max_cnt       = _ODP_MIN(elapsed_ticks, UINT32_C(32));
	current_wheel = timer_wheels->current_wheel;
	ret_code      = 0;
	rc            = -1;

	for (cnt = 1; cnt <= max_cnt; cnt++) {
		ret_code  |= (slot_idx & wheel_desc->gear_mask) == 0;
		timer_slot = &current_wheel->slots[slot_idx];
		if (timer_slot->user_data != 0) {
			rc = expired_timers_append(timer_wheels, timer_slot);
			if (rc < 0)
				timer_wheels->expired_timers_ring->expired_ring_full_cnt++;

			timer_slot->user_data = 0;
		}

		timer_wheels->current_ticks++;
		slot_idx++;
		max_ticks++;
		if (num_slots <= slot_idx) {
			slot_idx = 0;
			max_ticks = wheel_desc->ticks_per_rev;
		}
	}

	wheel_desc->slot_idx  = slot_idx;
	wheel_desc->max_ticks = max_ticks;
	return ret_code;
}

static int _odp_int_timer_wheel_promote(timer_wheels_t *timer_wheels,
					uint32_t        desc_idx,
					uint64_t        wakeup_ticks,
					uint64_t        user_data)
{
	uint64_t rel_ticks;

	rel_ticks = wakeup_ticks - timer_wheels->current_ticks;
	if (desc_idx == 0)
		return current_wheel_insert(timer_wheels, rel_ticks, user_data);
	else
		return general_wheel_insert(timer_wheels, desc_idx,
					    wakeup_ticks, rel_ticks, user_data);
}

static void timer_wheel_slot_promote(timer_wheels_t       *timer_wheels,
				     uint32_t              desc_idx,
				     general_timer_slot_t *timer_slot)
{
	general_blk_t *general_blk;
	timer_blk_t   *timer_blk, *next_timer_blk;
	uint64_t       user_data, current_ticks, current_ticks_msb;
	uint64_t       wakeup_ticks;
	uint32_t       idx, wakeup32;
	int            rc;

	current_ticks     = timer_wheels->current_ticks;
	current_ticks_msb = (current_ticks >> 32) << 32;
	if (timer_slot->single_entry.kind == 1) {
		user_data    = timer_slot->single_entry.user_data;
		wakeup32     = timer_slot->single_entry.wakeup32;
		wakeup_ticks = current_ticks_msb | (uint64_t)wakeup32;
		if (wakeup_ticks <= current_ticks) {
			if ((current_ticks - wakeup_ticks) <= (1ULL << 31))
				wakeup_ticks = current_ticks + 1;
			else
				wakeup_ticks += 1ULL << 32;
		}

		rc = _odp_int_timer_wheel_promote(timer_wheels, desc_idx,
						  wakeup_ticks, user_data);
		if (rc < 0)
			timer_wheels->promote_fail_cnt++;
		else
			timer_wheels->total_promote_cnt++;
		return;
	}

	timer_blk = timer_slot->list_entry.timer_blk_list;
	while (timer_blk) {
		general_blk = &timer_blk->general_blk;
		for (idx = 0; idx < 10; idx++) {
			user_data = general_blk->user_data[idx];
			if (user_data == 0)
				break;

			wakeup32     = general_blk->wakeup32[idx];
			wakeup_ticks = current_ticks_msb | (uint64_t)wakeup32;
			if (wakeup_ticks <= current_ticks) {
				if ((current_ticks - wakeup_ticks) <=
				    (1ULL << 31))
					wakeup_ticks = current_ticks + 1;
				else
					wakeup_ticks += 1ULL << 32;
			}

			rc = _odp_int_timer_wheel_promote(timer_wheels,
							  desc_idx,
							  wakeup_ticks,
							  user_data);
			if (rc < 0)
				timer_wheels->promote_fail_cnt++;
			else
				timer_wheels->total_promote_cnt++;
		}

		next_timer_blk = timer_blk->next_timer_blk;
		timer_blk_free(timer_wheels, timer_blk);
		timer_blk = next_timer_blk;
	}
}

static int timer_general_wheel_update(timer_wheels_t *timer_wheels,
				      uint32_t        desc_idx)
{
	general_timer_slot_t *timer_slot;
	general_wheel_t      *general_wheel;
	wheel_desc_t         *wheel_desc;
	uint64_t              max_ticks;
	uint32_t              num_slots, slot_idx;
	int                   ret_code;

	wheel_desc    = &timer_wheels->wheel_descs[desc_idx];
	slot_idx      = wheel_desc->slot_idx;
	num_slots     = wheel_desc->num_slots;
	max_ticks     = wheel_desc->max_ticks;
	general_wheel = timer_wheels->general_wheels[desc_idx - 1];
	timer_slot    = &general_wheel->slots[slot_idx];
	ret_code      = (slot_idx & wheel_desc->gear_mask) == 0;

	if (timer_slot->single_entry.kind != 0) {
		timer_wheel_slot_promote(timer_wheels,
					 desc_idx - 1, timer_slot);
		timer_slot->single_entry.kind = 0;
	}

	slot_idx++;
	max_ticks++;
	if (num_slots <= slot_idx) {
		slot_idx = 0;
		max_ticks = wheel_desc->ticks_per_rev;
	}

	wheel_desc->slot_idx  = slot_idx;
	wheel_desc->max_ticks = max_ticks;
	return ret_code;
}

_odp_timer_wheel_t _odp_timer_wheel_create(uint32_t max_concurrent_timers,
					   void    *tm_system)
{
	timer_wheels_t *timer_wheels;
	int             rc;

	timer_wheels  = malloc(sizeof(timer_wheels_t));
	memset(timer_wheels, 0, sizeof(timer_wheels_t));

	timer_wheels->wheel_descs[0].num_slots = CURRENT_TIMER_SLOTS;
	timer_wheels->wheel_descs[1].num_slots = LEVEL1_TIMER_SLOTS;
	timer_wheels->wheel_descs[2].num_slots = LEVEL2_TIMER_SLOTS;
	timer_wheels->wheel_descs[3].num_slots = LEVEL3_TIMER_SLOTS;

	timer_wheels->wheel_descs[0].gear_ratio = CURRENT_GEAR_RATIO;
	timer_wheels->wheel_descs[1].gear_ratio = LEVEL1_GEAR_RATIO;
	timer_wheels->wheel_descs[2].gear_ratio = LEVEL2_GEAR_RATIO;
	timer_wheels->wheel_descs[3].gear_ratio = 1;

	timer_wheels->wheel_descs[0].ticks_per_slot = TICKS_PER_CURRENT_SLOT;
	timer_wheels->wheel_descs[1].ticks_per_slot = TICKS_PER_LEVEL1_SLOT;
	timer_wheels->wheel_descs[2].ticks_per_slot = TICKS_PER_LEVEL2_SLOT;
	timer_wheels->wheel_descs[3].ticks_per_slot = TICKS_PER_LEVEL3_SLOT;

	timer_wheels->current_wheel     = current_wheel_alloc(timer_wheels, 0);
	timer_wheels->general_wheels[0] = general_wheel_alloc(timer_wheels, 1);
	timer_wheels->general_wheels[1] = general_wheel_alloc(timer_wheels, 2);
	timer_wheels->general_wheels[2] = general_wheel_alloc(timer_wheels, 3);

	timer_wheels->tm_system = tm_system;

	rc = expired_ring_create(timer_wheels, EXPIRED_RING_ENTRIES);
	if (rc < 0)
		return _ODP_INT_TIMER_WHEEL_INVALID;

	free_list_add(timer_wheels, max_concurrent_timers / 4);
	timer_wheels->min_free_list_size  = timer_wheels->free_list_size;
	timer_wheels->peak_free_list_size = timer_wheels->free_list_size;
	return (_odp_timer_wheel_t)(uintptr_t)timer_wheels;
}

void _odp_timer_wheel_start(_odp_timer_wheel_t timer_wheel,
			    uint64_t           current_time)
{
	timer_wheels_t *timer_wheels;
	uint64_t        current_ticks;

	timer_wheels  = (timer_wheels_t *)(uintptr_t)timer_wheel;
	current_ticks = current_time >> TIME_TO_TICKS_SHIFT;
	timer_wheels->current_ticks = current_ticks;

	current_wheel_start(timer_wheels, 0, current_ticks);
	general_wheel_start(timer_wheels, 1, current_ticks);
	general_wheel_start(timer_wheels, 2, current_ticks);
	general_wheel_start(timer_wheels, 3, current_ticks);
}

uint32_t _odp_timer_wheel_curr_time_update(_odp_timer_wheel_t timer_wheel,
					   uint64_t           current_time)
{
	timer_wheels_t *timer_wheels;
	uint64_t        new_current_ticks, elapsed_ticks;
	uint32_t        desc_idx;
	int             rc;

	timer_wheels      = (timer_wheels_t *)(uintptr_t)timer_wheel;
	new_current_ticks = current_time >> TIME_TO_TICKS_SHIFT;
	elapsed_ticks     = new_current_ticks - timer_wheels->current_ticks;
	if (elapsed_ticks == 0)
		return 0;

       /* Advance current wheel for each elapsed tick, up to a maximum. This
	* function returns a value > 0, then the next level's timer wheel
	* needs to be updated.
	*/
	rc = timer_current_wheel_update(timer_wheels, (uint32_t)elapsed_ticks);

	/* See if we need to do any higher wheel advancing or processing.*/
	desc_idx = 1;
	while ((0 < rc) && (desc_idx <= 3))
		rc = timer_general_wheel_update(timer_wheels, desc_idx++);

	return timer_wheels->expired_timers_ring->count;
}

int _odp_timer_wheel_insert(_odp_timer_wheel_t timer_wheel,
			    uint64_t           wakeup_time,
			    uint64_t           user_context)
{
	timer_wheels_t *timer_wheels;
	uint64_t        wakeup_ticks, rel_ticks;
	int             rc;

	if (user_context == 0)
		return -4;  /* user_context cannot be 0! */
	else if ((user_context & 0x3) != 0)
		return -5;  /* user_context must be at least 4-byte aligned. */

	timer_wheels = (timer_wheels_t *)(uintptr_t)timer_wheel;
	wakeup_ticks = (wakeup_time >> TIME_TO_TICKS_SHIFT) + 1;
	if (wakeup_time <= timer_wheels->current_ticks)
		return -6;

	rel_ticks = wakeup_ticks - timer_wheels->current_ticks;
	if (rel_ticks < timer_wheels->wheel_descs[0].max_ticks)
		rc = current_wheel_insert(timer_wheels, rel_ticks,
					  user_context);
	else if (rel_ticks < timer_wheels->wheel_descs[1].max_ticks)
		rc = general_wheel_insert(timer_wheels, 1, wakeup_ticks,
					  rel_ticks, user_context);
	else if (rel_ticks < timer_wheels->wheel_descs[2].max_ticks)
		rc = general_wheel_insert(timer_wheels, 2, wakeup_ticks,
					  rel_ticks, user_context);
	else if (rel_ticks < timer_wheels->wheel_descs[3].max_ticks)
		rc = general_wheel_insert(timer_wheels, 3, wakeup_ticks,
					  rel_ticks, user_context);
	else
		return -1;

	if (rc < 0) {
		timer_wheels->insert_fail_cnt++;
		return rc;
	}

	timer_wheels->total_timer_inserts++;
	timer_wheels->current_cnt++;
	return 0;
}

uint64_t _odp_timer_wheel_next_expired(_odp_timer_wheel_t timer_wheel)
{
	timer_wheels_t *timer_wheels;
	uint64_t        user_data;
	int             rc;

	/* Remove the head of the timer wheel. */
	timer_wheels = (timer_wheels_t *)(uintptr_t)timer_wheel;
	rc = expired_timers_remove(timer_wheels, &user_data);
	if (rc <= 0)
		return 0;

	user_data &= ~0x3;
	timer_wheels->total_timer_removes++;
	if (timer_wheels->current_cnt != 0)
		timer_wheels->current_cnt--;
	return user_data;
}

uint32_t _odp_timer_wheel_count(_odp_timer_wheel_t timer_wheel)
{
	timer_wheels_t *timer_wheels;

	timer_wheels = (timer_wheels_t *)(uintptr_t)timer_wheel;
	return timer_wheels->total_timer_inserts -
		timer_wheels->total_timer_removes;
}

static void _odp_int_timer_wheel_desc_print(wheel_desc_t *wheel_desc,
					    uint32_t      wheel_idx)
{
	_ODP_PRINT("    wheel=%u num_slots=%u ticks_shift=%u ticks_per_slot=%u"
		   " ticks_per_rev=%" PRIu64 "\n",
		   wheel_idx, wheel_desc->num_slots, wheel_desc->ticks_shift,
		   wheel_desc->ticks_per_slot, wheel_desc->ticks_per_rev);
}

void _odp_timer_wheel_stats_print(_odp_timer_wheel_t timer_wheel)
{
	timer_wheels_t *timer_wheels;
	expired_ring_t *expired_ring;
	uint32_t        wheel_idx;

	timer_wheels = (timer_wheels_t *)(uintptr_t)timer_wheel;
	expired_ring = timer_wheels->expired_timers_ring;

	_ODP_PRINT("  _odp_int_timer_wheel_stats current_ticks=%" PRIu64 "\n",
		   timer_wheels->current_ticks);
	for (wheel_idx = 0; wheel_idx < 4; wheel_idx++)
		_odp_int_timer_wheel_desc_print(&timer_wheels->wheel_descs[wheel_idx], wheel_idx);

	_ODP_PRINT("    total timer_inserts=%" PRIu64 " timer_removes=%" PRIu64
		   " insert_fails=%" PRIu64 "\n",
		   timer_wheels->total_timer_inserts,
		   timer_wheels->total_timer_removes,
		   timer_wheels->insert_fail_cnt);
	_ODP_PRINT("    total_promote_cnt=%" PRIu64 " promote_fail_cnt=%"
		   PRIu64 "\n", timer_wheels->total_promote_cnt,
		   timer_wheels->promote_fail_cnt);
	_ODP_PRINT("    free_list_size=%u min_size=%u peak_size=%u\n",
		   timer_wheels->free_list_size,
		   timer_wheels->min_free_list_size,
		   timer_wheels->peak_free_list_size);
	_ODP_PRINT("    expired_timers_ring size=%u count=%u "
		   "peak_count=%u full_cnt=%u\n",
		   expired_ring->max_idx + 1, expired_ring->count,
		   expired_ring->peak_count,
		   expired_ring->expired_ring_full_cnt);
}

void _odp_timer_wheel_destroy(_odp_timer_wheel_t timer_wheel)
{
	blk_of_timer_blks_desc_t *blk_of_timer_blks_desc, *next_desc;
	timer_wheels_t           *timer_wheels;
	expired_ring_t           *expired_ring;

	timer_wheels = (timer_wheels_t *)(uintptr_t)timer_wheel;
	expired_ring = timer_wheels->expired_timers_ring;

	/* First free all of the block_of_timer_blks */
	blk_of_timer_blks_desc = timer_wheels->timer_blks_list_head;
	while (blk_of_timer_blks_desc) {
		next_desc = blk_of_timer_blks_desc->next;
		free(blk_of_timer_blks_desc->block_of_timer_blks);
		free(blk_of_timer_blks_desc);
		blk_of_timer_blks_desc = next_desc;
	}

	free(timer_wheels->current_wheel);
	free(timer_wheels->general_wheels[0]);
	free(timer_wheels->general_wheels[1]);
	free(timer_wheels->general_wheels[2]);
	free(expired_ring);
	free(timer_wheels);
}