2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief TW timer implementation TEMPLATE ONLY, do not compile directly
21 #if TW_START_STOP_TRACE_SIZE > 0
23 void TW (tw_timer_trace) (TWT (tw_timer_wheel) * tw, u32 timer_id,
24 u32 pool_index, u32 handle)
26 TWT (trace) * t = &tw->traces[tw->trace_index];
28 t->timer_id = timer_id;
29 t->pool_index = pool_index;
33 if (tw->trace_index == TW_START_STOP_TRACE_SIZE)
40 void TW (tw_search_trace) (TWT (tw_timer_wheel) * tw, u32 handle)
46 /* reverse search for the supplied handle */
48 start_pos = tw->trace_index;
50 start_pos = TW_START_STOP_TRACE_SIZE - 1;
54 for (i = start_pos; i > 0; i--)
57 if (t->handle == handle)
71 fformat (stderr, "handle 0x%x (%d) %s at trace %d\n",
72 handle, handle, s, i);
75 if (tw->trace_wrapped > 0)
77 for (i = TW_START_STOP_TRACE_SIZE; i >= tw->trace_index; i--)
80 if (t->handle == handle)
94 fformat (stderr, "handle 0x%x (%d) %s at trace %d\n",
95 handle, handle, s, i);
100 #endif /* TW_START_STOP_TRACE_SIZE > 0 */
103 TW (make_internal_timer_handle) (u32 pool_index, u32 timer_id)
107 ASSERT (timer_id < TW_TIMERS_PER_OBJECT);
108 #if LOG2_TW_TIMERS_PER_OBJECT > 0
109 ASSERT (pool_index < (1 << (32 - LOG2_TW_TIMERS_PER_OBJECT)));
111 handle = (timer_id << (32 - LOG2_TW_TIMERS_PER_OBJECT)) | (pool_index);
119 timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
121 TWT (tw_timer) * head = pool_elt_at_index (pool, head_index);
122 TWT (tw_timer) * old_first;
124 TWT (tw_timer) * new;
126 new = pool_elt_at_index (pool, new_index);
128 if (PREDICT_FALSE (head->next == head_index))
130 head->next = head->prev = new_index;
131 new->next = new->prev = head_index;
135 old_first_index = head->next;
136 old_first = pool_elt_at_index (pool, old_first_index);
138 new->next = old_first_index;
139 new->prev = old_first->prev;
140 old_first->prev = new_index;
141 head->next = new_index;
145 timer_remove (TWT (tw_timer) * pool, TWT (tw_timer) * elt)
147 TWT (tw_timer) * next_elt, *prev_elt;
149 ASSERT (elt->user_handle != ~0);
151 next_elt = pool_elt_at_index (pool, elt->next);
152 prev_elt = pool_elt_at_index (pool, elt->prev);
154 next_elt->prev = elt->prev;
155 prev_elt->next = elt->next;
157 elt->prev = elt->next = ~0;
161 timer_add (TWT (tw_timer_wheel) * tw, TWT (tw_timer) * t, u64 interval)
163 #if TW_TIMER_WHEELS > 1
164 u16 slow_ring_offset;
167 #if TW_TIMER_WHEELS > 2
168 u16 glacier_ring_offset;
170 #if TW_OVERFLOW_VECTOR > 0
171 u64 interval_plus_time_to_wrap, triple_wrap_mask;
173 u16 fast_ring_offset;
174 tw_timer_wheel_slot_t *ts;
176 /* Factor interval into 1..3 wheel offsets */
177 #if TW_TIMER_WHEELS > 2
178 #if TW_OVERFLOW_VECTOR > 0
180 * This is tricky. Put a timer onto the overflow
181 * vector if the interval PLUS the time
182 * until the next triple-wrap exceeds one full revolution
183 * of all three wheels.
185 triple_wrap_mask = (1 << (3 * TW_RING_SHIFT)) - 1;
186 interval_plus_time_to_wrap =
187 interval + (tw->current_tick & triple_wrap_mask);
188 if ((interval_plus_time_to_wrap >= 1 << (3 * TW_RING_SHIFT)))
190 t->expiration_time = tw->current_tick + interval;
192 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
193 #if TW_START_STOP_TRACE_SIZE > 0
194 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
200 glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
201 ASSERT ((u64) glacier_ring_offset < TW_SLOTS_PER_RING);
202 interval -= (((u64) glacier_ring_offset) << (2 * TW_RING_SHIFT));
204 #if TW_TIMER_WHEELS > 1
205 slow_ring_offset = interval >> TW_RING_SHIFT;
206 ASSERT ((u64) slow_ring_offset < TW_SLOTS_PER_RING);
207 interval -= (((u64) slow_ring_offset) << TW_RING_SHIFT);
209 fast_ring_offset = interval & TW_RING_MASK;
212 * Account for the current wheel positions(s)
213 * This is made slightly complicated by the fact that the current
214 * index vector will contain (TW_SLOTS_PER_RING, ...) when
215 * the actual position is (0, ...)
218 fast_ring_offset += tw->current_index[TW_TIMER_RING_FAST] & TW_RING_MASK;
220 #if TW_TIMER_WHEELS > 1
221 carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
222 fast_ring_offset %= TW_SLOTS_PER_RING;
223 slow_ring_offset += (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK)
225 carry = slow_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
226 slow_ring_offset %= TW_SLOTS_PER_RING;
229 #if TW_TIMER_WHEELS > 2
230 glacier_ring_offset +=
231 (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK) + carry;
232 glacier_ring_offset %= TW_SLOTS_PER_RING;
235 #if TW_TIMER_WHEELS > 2
236 if (glacier_ring_offset !=
237 (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK))
239 /* We'll need slow and fast ring offsets later */
240 t->slow_ring_offset = slow_ring_offset;
241 t->fast_ring_offset = fast_ring_offset;
243 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_ring_offset];
245 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
246 #if TW_START_STOP_TRACE_SIZE > 0
247 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
253 #if TW_TIMER_WHEELS > 1
254 /* Timer expires more than 51.2 seconds from now? */
255 if (slow_ring_offset !=
256 (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK))
258 /* We'll need the fast ring offset later... */
259 t->fast_ring_offset = fast_ring_offset;
261 ts = &tw->w[TW_TIMER_RING_SLOW][slow_ring_offset];
263 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
264 #if TW_START_STOP_TRACE_SIZE > 0
265 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
270 fast_ring_offset %= TW_SLOTS_PER_RING;
273 /* Timer expires less than one fast-ring revolution from now */
274 ts = &tw->w[TW_TIMER_RING_FAST][fast_ring_offset];
276 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
278 #if TW_FAST_WHEEL_BITMAP
279 tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap,
280 fast_ring_offset, 1);
282 #if TW_START_STOP_TRACE_SIZE > 0
283 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
288 * @brief Start a Tw Timer
289 * @param tw_timer_wheel_t * tw timer wheel object pointer
290 * @param u32 user_id user defined timer id, presumably for a tw session
291 * @param u32 timer_id app-specific timer ID. 4 bits.
292 * @param u64 interval timer interval in ticks
293 * @returns handle needed to cancel the timer
296 TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 user_id, u32 timer_id,
303 pool_get (tw->timers, t);
304 clib_memset (t, 0xff, sizeof (*t));
306 t->user_handle = TW (make_internal_timer_handle) (user_id, timer_id);
308 timer_add (tw, t, interval);
309 return t - tw->timers;
312 #if TW_TIMER_SCAN_FOR_HANDLE > 0
313 int TW (scan_for_handle) (TWT (tw_timer_wheel) * tw, u32 handle)
316 tw_timer_wheel_slot_t *ts;
317 TWT (tw_timer) * t, *head;
321 for (i = 0; i < TW_TIMER_WHEELS; i++)
323 for (j = 0; j < TW_SLOTS_PER_RING; j++)
326 head = pool_elt_at_index (tw->timers, ts->head_index);
327 next_index = head->next;
329 while (next_index != ts->head_index)
331 t = pool_elt_at_index (tw->timers, next_index);
332 if (next_index == handle)
334 clib_warning ("handle %d found in ring %d slot %d",
336 clib_warning ("user handle 0x%x", t->user_handle);
339 next_index = t->next;
345 #endif /* TW_TIMER_SCAN_FOR_HANDLE */
348 * @brief Stop a tw timer
349 * @param tw_timer_wheel_t * tw timer wheel object pointer
350 * @param u32 handle timer cancellation returned by tw_timer_start
352 __clib_export void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
356 #if TW_TIMER_ALLOW_DUPLICATE_STOP
358 * A vlib process may have its timer expire, and receive
359 * an event before the expiration is processed.
360 * That results in a duplicate tw_timer_stop.
362 if (pool_is_free_index (tw->timers, handle))
365 #if TW_START_STOP_TRACE_SIZE > 0
366 TW (tw_timer_trace) (tw, ~0, ~0, handle);
369 t = pool_elt_at_index (tw->timers, handle);
371 /* in case of idiotic handle (e.g. passing a listhead index) */
372 ASSERT (t->user_handle != ~0);
374 timer_remove (tw->timers, t);
376 pool_put_index (tw->timers, handle);
380 TW (tw_timer_handle_is_free) (TWT (tw_timer_wheel) * tw, u32 handle)
382 return pool_is_free_index (tw->timers, handle);
386 * @brief Update a tw timer
387 * @param tw_timer_wheel_t * tw timer wheel object pointer
388 * @param u32 handle timer returned by tw_timer_start
389 * @param u32 interval timer interval in ticks
392 TW (tw_timer_update) (TWT (tw_timer_wheel) * tw, u32 handle, u64 interval)
395 t = pool_elt_at_index (tw->timers, handle);
396 timer_remove (tw->timers, t);
397 timer_add (tw, t, interval);
401 * @brief Initialize a tw timer wheel template instance
402 * @param tw_timer_wheel_t * tw timer wheel object pointer
403 * @param void * expired_timer_callback. Passed a u32 * vector of
404 * expired timer handles. The callback is optional.
405 * @param f64 timer_interval_in_seconds
408 TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
409 void *expired_timer_callback,
410 f64 timer_interval_in_seconds, u32 max_expirations)
413 tw_timer_wheel_slot_t *ts;
415 clib_memset (tw, 0, sizeof (*tw));
416 tw->expired_timer_callback = expired_timer_callback;
417 tw->max_expirations = max_expirations;
418 if (timer_interval_in_seconds == 0.0)
420 clib_warning ("timer interval is zero");
423 tw->timer_interval = timer_interval_in_seconds;
424 tw->ticks_per_second = 1.0 / timer_interval_in_seconds;
426 vec_validate (tw->expired_timer_handles, 0);
427 vec_set_len (tw->expired_timer_handles, 0);
429 for (ring = 0; ring < TW_TIMER_WHEELS; ring++)
431 for (slot = 0; slot < TW_SLOTS_PER_RING; slot++)
433 ts = &tw->w[ring][slot];
434 pool_get (tw->timers, t);
435 clib_memset (t, 0xff, sizeof (*t));
436 t->next = t->prev = t - tw->timers;
437 ts->head_index = t - tw->timers;
441 #if TW_OVERFLOW_VECTOR > 0
443 pool_get (tw->timers, t);
444 clib_memset (t, 0xff, sizeof (*t));
445 t->next = t->prev = t - tw->timers;
446 ts->head_index = t - tw->timers;
451 * @brief Free a tw timer wheel template instance
452 * @param tw_timer_wheel_t * tw timer wheel object pointer
454 __clib_export void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
457 tw_timer_wheel_slot_t *ts;
458 TWT (tw_timer) * head, *t;
461 for (i = 0; i < TW_TIMER_WHEELS; i++)
463 for (j = 0; j < TW_SLOTS_PER_RING; j++)
466 head = pool_elt_at_index (tw->timers, ts->head_index);
467 next_index = head->next;
469 while (next_index != ts->head_index)
471 t = pool_elt_at_index (tw->timers, next_index);
472 next_index = t->next;
473 pool_put (tw->timers, t);
475 pool_put (tw->timers, head);
479 #if TW_OVERFLOW_VECTOR > 0
481 head = pool_elt_at_index (tw->timers, ts->head_index);
482 next_index = head->next;
484 while (next_index != ts->head_index)
486 t = pool_elt_at_index (tw->timers, next_index);
487 next_index = t->next;
488 pool_put (tw->timers, t);
490 pool_put (tw->timers, head);
493 clib_memset (tw, 0, sizeof (*tw));
497 * @brief Advance a tw timer wheel. Calls the expired timer callback
498 * as needed. This routine should be called once every timer_interval seconds
499 * @param tw_timer_wheel_t * tw timer wheel template instance pointer
500 * @param f64 now the current time, e.g. from vlib_time_now(vm)
501 * @returns u32 * vector of expired user handles
504 u32 * TW (tw_timer_expire_timers_internal) (TWT (tw_timer_wheel) * tw,
506 u32 * callback_vector_arg)
509 tw_timer_wheel_slot_t *ts;
510 TWT (tw_timer) * t, *head;
511 u32 *callback_vector;
512 u32 fast_wheel_index;
514 u32 slow_wheel_index __attribute__ ((unused));
515 u32 glacier_wheel_index __attribute__ ((unused));
517 /* Called too soon to process new timer expirations? */
518 if (PREDICT_FALSE (now < tw->next_run_time))
519 return callback_vector_arg;
521 /* Number of ticks which have occurred */
522 nticks = tw->ticks_per_second * (now - tw->last_run_time);
524 return callback_vector_arg;
526 /* Remember when we ran, compute next runtime */
527 tw->next_run_time = (now + tw->timer_interval);
529 /* First call, or time jumped backwards? */
531 ((tw->last_run_time == 0.0) || (now <= tw->last_run_time)))
533 tw->last_run_time = now;
534 return callback_vector_arg;
537 if (callback_vector_arg == 0)
539 vec_set_len (tw->expired_timer_handles, 0);
540 callback_vector = tw->expired_timer_handles;
543 callback_vector = callback_vector_arg;
545 for (i = 0; i < nticks; i++)
547 fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST];
548 if (TW_TIMER_WHEELS > 1)
549 slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW];
550 if (TW_TIMER_WHEELS > 2)
551 glacier_wheel_index = tw->current_index[TW_TIMER_RING_GLACIER];
553 #if TW_OVERFLOW_VECTOR > 0
554 /* Triple odometer-click? Process the overflow vector... */
555 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
556 && slow_wheel_index == TW_SLOTS_PER_RING
557 && glacier_wheel_index == TW_SLOTS_PER_RING))
560 u32 new_glacier_ring_offset, new_slow_ring_offset;
561 u32 new_fast_ring_offset;
564 head = pool_elt_at_index (tw->timers, ts->head_index);
565 next_index = head->next;
567 /* Make slot empty */
568 head->next = head->prev = ts->head_index;
570 /* traverse slot, place timers wherever they go */
571 while (next_index != head - tw->timers)
573 t = pool_elt_at_index (tw->timers, next_index);
574 next_index = t->next;
576 /* Remove from the overflow vector (hammer) */
577 t->next = t->prev = ~0;
579 ASSERT (t->expiration_time >= tw->current_tick);
581 interval = t->expiration_time - tw->current_tick;
583 /* Right back onto the overflow vector? */
584 if (interval >= (1 << (3 * TW_RING_SHIFT)))
587 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
590 /* Compute ring offsets */
591 new_glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
593 interval -= (new_glacier_ring_offset << (2 * TW_RING_SHIFT));
595 /* Note: the wheels are at (0,0,0), no add-with-carry needed */
596 new_slow_ring_offset = interval >> TW_RING_SHIFT;
597 interval -= (new_slow_ring_offset << TW_RING_SHIFT);
598 new_fast_ring_offset = interval & TW_RING_MASK;
599 t->slow_ring_offset = new_slow_ring_offset;
600 t->fast_ring_offset = new_fast_ring_offset;
602 /* Timer expires Right Now */
603 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
604 t->fast_ring_offset == 0 &&
605 new_glacier_ring_offset == 0))
607 vec_add1 (callback_vector, t->user_handle);
608 #if TW_START_STOP_TRACE_SIZE > 0
609 TW (tw_timer_trace) (tw, 0xfe, t->user_handle,
612 pool_put (tw->timers, t);
614 /* Timer moves to the glacier ring */
615 else if (new_glacier_ring_offset)
617 ts = &tw->w[TW_TIMER_RING_GLACIER][new_glacier_ring_offset];
618 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
620 /* Timer moves to the slow ring */
621 else if (t->slow_ring_offset)
623 /* Add to slow ring */
624 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
625 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
627 /* Timer timer moves to the fast ring */
630 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
631 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
632 #if TW_FAST_WHEEL_BITMAP
633 tw->fast_slot_bitmap =
634 clib_bitmap_set (tw->fast_slot_bitmap,
635 t->fast_ring_offset, 1);
642 #if TW_TIMER_WHEELS > 2
644 * Double odometer-click? Process one slot in the glacier ring...
646 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
647 && slow_wheel_index == TW_SLOTS_PER_RING))
649 glacier_wheel_index %= TW_SLOTS_PER_RING;
650 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_wheel_index];
652 head = pool_elt_at_index (tw->timers, ts->head_index);
653 next_index = head->next;
655 /* Make slot empty */
656 head->next = head->prev = ts->head_index;
658 /* traverse slot, deal timers into slow ring */
659 while (next_index != head - tw->timers)
661 t = pool_elt_at_index (tw->timers, next_index);
662 next_index = t->next;
664 /* Remove from glacier ring slot (hammer) */
665 t->next = t->prev = ~0;
667 /* Timer expires Right Now */
668 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
669 t->fast_ring_offset == 0))
671 vec_add1 (callback_vector, t->user_handle);
672 #if TW_START_STOP_TRACE_SIZE > 0
673 TW (tw_timer_trace) (tw, 0xfe, t->user_handle,
676 pool_put (tw->timers, t);
678 /* Timer expires during slow-wheel tick 0 */
679 else if (PREDICT_FALSE (t->slow_ring_offset == 0))
681 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
682 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
683 #if TW_FAST_WHEEL_BITMAP
684 tw->fast_slot_bitmap =
685 clib_bitmap_set (tw->fast_slot_bitmap,
686 t->fast_ring_offset, 1);
689 else /* typical case */
691 /* Add to slow ring */
692 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
693 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
699 #if TW_TIMER_WHEELS > 1
701 * Single odometer-click? Process a slot in the slow ring,
703 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
705 slow_wheel_index %= TW_SLOTS_PER_RING;
706 ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index];
708 head = pool_elt_at_index (tw->timers, ts->head_index);
709 next_index = head->next;
711 /* Make slot empty */
712 head->next = head->prev = ts->head_index;
714 /* traverse slot, deal timers into fast ring */
715 while (next_index != head - tw->timers)
717 t = pool_elt_at_index (tw->timers, next_index);
718 next_index = t->next;
720 /* Remove from sloe ring slot (hammer) */
721 t->next = t->prev = ~0;
723 /* Timer expires Right Now */
724 if (PREDICT_FALSE (t->fast_ring_offset == 0))
726 vec_add1 (callback_vector, t->user_handle);
727 #if TW_START_STOP_TRACE_SIZE > 0
728 TW (tw_timer_trace) (tw, 0xfe, t->user_handle,
731 pool_put (tw->timers, t);
733 else /* typical case */
735 /* Add to fast ring */
736 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
737 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
738 #if TW_FAST_WHEEL_BITMAP
739 tw->fast_slot_bitmap =
740 clib_bitmap_set (tw->fast_slot_bitmap,
741 t->fast_ring_offset, 1);
748 /* Handle the fast ring */
749 fast_wheel_index %= TW_SLOTS_PER_RING;
750 ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index];
752 head = pool_elt_at_index (tw->timers, ts->head_index);
753 next_index = head->next;
755 /* Make slot empty */
756 head->next = head->prev = ts->head_index;
758 /* Construct vector of expired timer handles to give the user */
759 while (next_index != ts->head_index)
761 t = pool_elt_at_index (tw->timers, next_index);
762 next_index = t->next;
763 vec_add1 (callback_vector, t->user_handle);
764 #if TW_START_STOP_TRACE_SIZE > 0
765 TW (tw_timer_trace) (tw, 0xfe, t->user_handle, t - tw->timers);
767 pool_put (tw->timers, t);
770 /* If any timers expired, tell the user */
771 if (callback_vector_arg == 0 && vec_len (callback_vector))
773 /* The callback is optional. We return the u32 * handle vector */
774 if (tw->expired_timer_callback)
776 tw->expired_timer_callback (callback_vector);
777 vec_reset_length (callback_vector);
779 tw->expired_timer_handles = callback_vector;
782 #if TW_FAST_WHEEL_BITMAP
783 tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap,
784 fast_wheel_index, 0);
789 tw->current_index[TW_TIMER_RING_FAST] = fast_wheel_index;
791 #if TW_TIMER_WHEELS > 1
792 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
794 tw->current_index[TW_TIMER_RING_SLOW] = slow_wheel_index;
797 #if TW_TIMER_WHEELS > 2
798 if (PREDICT_FALSE (slow_wheel_index == TW_SLOTS_PER_RING))
799 glacier_wheel_index++;
800 tw->current_index[TW_TIMER_RING_GLACIER] = glacier_wheel_index;
803 if (vec_len (callback_vector) >= tw->max_expirations)
807 if (callback_vector_arg == 0)
808 tw->expired_timer_handles = callback_vector;
810 tw->last_run_time += i * tw->timer_interval;
811 return callback_vector;
814 __clib_export u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw,
817 return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ );
820 __clib_export u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw,
823 return TW (tw_timer_expire_timers_internal) (tw, now, vec);
826 #if TW_FAST_WHEEL_BITMAP
827 /** Returns an approximation to the first timer expiration in
828 * timer-ticks from "now". To avoid wasting an unjustifiable
829 * amount of time on the problem, we maintain an approximate fast-wheel slot
830 * occupancy bitmap. We don't worry about clearing fast wheel bits
831 * when timers are removed from fast wheel slots.
835 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw)
837 u32 first_expiring_index, fast_ring_index;
840 #if TW_TIMER_WHEELS > 1
841 fast_ring_index = tw->current_index[TW_TIMER_RING_FAST];
842 if (fast_ring_index == TW_SLOTS_PER_RING)
845 first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap,
847 if (first_expiring_index == ~0)
848 first_expiring_index = TW_SLOTS_PER_RING;
852 if (clib_bitmap_is_zero (tw->fast_slot_bitmap))
853 return TW_SLOTS_PER_RING;
855 fast_ring_index = tw->current_index[TW_TIMER_RING_FAST];
856 if (fast_ring_index == TW_SLOTS_PER_RING)
859 first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap,
861 if (first_expiring_index == ~0 && fast_ring_index != 0)
862 first_expiring_index = clib_bitmap_first_set (tw->fast_slot_bitmap);
865 ASSERT (first_expiring_index != ~0);
867 delta = (i32) first_expiring_index - (i32) fast_ring_index;
869 delta += TW_SLOTS_PER_RING;
879 * fd.io coding-style-patch-verification: ON
882 * eval: (c-set-style "gnu")