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
22 TW (make_internal_timer_handle) (u32 pool_index, u32 timer_id)
26 ASSERT (timer_id < TW_TIMERS_PER_OBJECT);
27 #if LOG2_TW_TIMERS_PER_OBJECT > 0
28 ASSERT (pool_index < (1 << (32 - LOG2_TW_TIMERS_PER_OBJECT)));
30 handle = (timer_id << (32 - LOG2_TW_TIMERS_PER_OBJECT)) | (pool_index);
38 timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
40 TWT (tw_timer) * head = pool_elt_at_index (pool, head_index);
41 TWT (tw_timer) * old_first;
45 new = pool_elt_at_index (pool, new_index);
47 if (PREDICT_FALSE (head->next == head_index))
49 head->next = head->prev = new_index;
50 new->next = new->prev = head_index;
54 old_first_index = head->next;
55 old_first = pool_elt_at_index (pool, old_first_index);
57 new->next = old_first_index;
58 new->prev = old_first->prev;
59 old_first->prev = new_index;
60 head->next = new_index;
64 timer_remove (TWT (tw_timer) * pool, u32 index)
66 TWT (tw_timer) * elt = pool_elt_at_index (pool, index);
67 TWT (tw_timer) * next_elt, *prev_elt;
69 ASSERT (elt->user_handle != ~0);
71 next_elt = pool_elt_at_index (pool, elt->next);
72 prev_elt = pool_elt_at_index (pool, elt->prev);
74 next_elt->prev = elt->prev;
75 prev_elt->next = elt->next;
77 elt->prev = elt->next = ~0;
81 * @brief Start a Tw Timer
82 * @param tw_timer_wheel_t * tw timer wheel object pointer
83 * @param u32 pool_index user pool index, presumably for a tw session
84 * @param u32 timer_id app-specific timer ID. 4 bits.
85 * @param u64 interval timer interval in ticks
86 * @returns handle needed to cancel the timer
89 TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
92 #if TW_TIMER_WHEELS > 1
96 #if TW_TIMER_WHEELS > 2
97 u16 glacier_ring_offset;
99 #if TW_OVERFLOW_VECTOR > 0
100 u64 interval_plus_time_to_wrap, triple_wrap_mask;
102 u16 fast_ring_offset;
103 tw_timer_wheel_slot_t *ts;
108 pool_get (tw->timers, t);
109 memset (t, 0xff, sizeof (*t));
111 t->user_handle = TW (make_internal_timer_handle) (pool_index, timer_id);
113 /* Factor interval into 1..3 wheel offsets */
114 #if TW_TIMER_WHEELS > 2
115 #if TW_OVERFLOW_VECTOR > 0
117 * This is tricky. Put a timer onto the overflow
118 * vector if the interval PLUS the time
119 * until the next triple-wrap exceeds one full revolution
120 * of all three wheels.
122 triple_wrap_mask = (1 << (3 * TW_RING_SHIFT)) - 1;
123 interval_plus_time_to_wrap =
124 interval + (tw->current_tick & triple_wrap_mask);
125 if ((interval_plus_time_to_wrap >= 1 << (3 * TW_RING_SHIFT)))
127 t->expiration_time = tw->current_tick + interval;
129 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
130 return t - tw->timers;
134 glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
135 ASSERT (glacier_ring_offset < TW_SLOTS_PER_RING);
136 interval -= (glacier_ring_offset << (2 * TW_RING_SHIFT));
138 #if TW_TIMER_WHEELS > 1
139 slow_ring_offset = interval >> TW_RING_SHIFT;
140 ASSERT (slow_ring_offset < TW_SLOTS_PER_RING);
141 interval -= (slow_ring_offset << TW_RING_SHIFT);
143 fast_ring_offset = interval & TW_RING_MASK;
146 * Account for the current wheel positions(s)
147 * This is made slightly complicated by the fact that the current
148 * index vector will contain (TW_SLOTS_PER_RING, ...) when
149 * the actual position is (0, ...)
152 fast_ring_offset += tw->current_index[TW_TIMER_RING_FAST] & TW_RING_MASK;
154 #if TW_TIMER_WHEELS > 1
155 carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
156 fast_ring_offset %= TW_SLOTS_PER_RING;
157 slow_ring_offset += (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK)
159 carry = slow_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
160 slow_ring_offset %= TW_SLOTS_PER_RING;
163 #if TW_TIMER_WHEELS > 2
164 glacier_ring_offset +=
165 (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK) + carry;
166 glacier_ring_offset %= TW_SLOTS_PER_RING;
169 #if TW_TIMER_WHEELS > 2
170 if (glacier_ring_offset !=
171 (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK))
173 /* We'll need slow and fast ring offsets later */
174 t->slow_ring_offset = slow_ring_offset;
175 t->fast_ring_offset = fast_ring_offset;
177 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_ring_offset];
179 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
181 return t - tw->timers;
185 #if TW_TIMER_WHEELS > 1
186 /* Timer expires more than 51.2 seconds from now? */
187 if (slow_ring_offset !=
188 (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK))
190 /* We'll need the fast ring offset later... */
191 t->fast_ring_offset = fast_ring_offset;
193 ts = &tw->w[TW_TIMER_RING_SLOW][slow_ring_offset];
195 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
197 return t - tw->timers;
200 fast_ring_offset %= TW_SLOTS_PER_RING;
203 /* Timer expires less than one fast-ring revolution from now */
204 ts = &tw->w[TW_TIMER_RING_FAST][fast_ring_offset];
206 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
207 return t - tw->timers;
210 #if TW_TIMER_SCAN_FOR_HANDLE > 0
211 int TW (scan_for_handle) (TWT (tw_timer_wheel) * tw, u32 handle)
214 tw_timer_wheel_slot_t *ts;
215 TWT (tw_timer) * t, *head;
219 for (i = 0; i < TW_TIMER_WHEELS; i++)
221 for (j = 0; j < TW_SLOTS_PER_RING; j++)
224 head = pool_elt_at_index (tw->timers, ts->head_index);
225 next_index = head->next;
227 while (next_index != ts->head_index)
229 t = pool_elt_at_index (tw->timers, next_index);
230 if (next_index == handle)
232 clib_warning ("handle %d found in ring %d slot %d",
234 clib_warning ("user handle 0x%x", t->user_handle);
237 next_index = t->next;
243 #endif /* TW_TIMER_SCAN_FOR_HANDLE */
246 * @brief Stop a tw timer
247 * @param tw_timer_wheel_t * tw timer wheel object pointer
248 * @param u32 handle timer cancellation returned by tw_timer_start
250 void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
254 t = pool_elt_at_index (tw->timers, handle);
256 /* in case of idiotic handle (e.g. passing a listhead index) */
257 ASSERT (t->user_handle != ~0);
259 timer_remove (tw->timers, handle);
261 pool_put_index (tw->timers, handle);
265 * @brief Initialize a tw timer wheel template instance
266 * @param tw_timer_wheel_t * tw timer wheel object pointer
267 * @param void * expired_timer_callback. Passed a u32 * vector of
268 * expired timer handles. The callback is optional.
269 * @param f64 timer_interval_in_seconds
272 TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
273 void *expired_timer_callback,
274 f64 timer_interval_in_seconds, u32 max_expirations)
277 tw_timer_wheel_slot_t *ts;
279 memset (tw, 0, sizeof (*tw));
280 tw->expired_timer_callback = expired_timer_callback;
281 tw->max_expirations = max_expirations;
282 if (timer_interval_in_seconds == 0.0)
284 clib_warning ("timer interval is zero");
287 tw->timer_interval = timer_interval_in_seconds;
288 tw->ticks_per_second = 1.0 / timer_interval_in_seconds;
289 tw->first_expires_tick = ~0ULL;
290 vec_validate (tw->expired_timer_handles, 0);
291 _vec_len (tw->expired_timer_handles) = 0;
293 for (ring = 0; ring < TW_TIMER_WHEELS; ring++)
295 for (slot = 0; slot < TW_SLOTS_PER_RING; slot++)
297 ts = &tw->w[ring][slot];
298 pool_get (tw->timers, t);
299 memset (t, 0xff, sizeof (*t));
300 t->next = t->prev = t - tw->timers;
301 ts->head_index = t - tw->timers;
305 #if TW_OVERFLOW_VECTOR > 0
307 pool_get (tw->timers, t);
308 memset (t, 0xff, sizeof (*t));
309 t->next = t->prev = t - tw->timers;
310 ts->head_index = t - tw->timers;
315 * @brief Free a tw timer wheel template instance
316 * @param tw_timer_wheel_t * tw timer wheel object pointer
318 void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
321 tw_timer_wheel_slot_t *ts;
322 TWT (tw_timer) * head, *t;
325 for (i = 0; i < TW_TIMER_WHEELS; i++)
327 for (j = 0; j < TW_SLOTS_PER_RING; j++)
330 head = pool_elt_at_index (tw->timers, ts->head_index);
331 next_index = head->next;
333 while (next_index != ts->head_index)
335 t = pool_elt_at_index (tw->timers, next_index);
336 next_index = t->next;
337 pool_put (tw->timers, t);
339 pool_put (tw->timers, head);
343 #if TW_OVERFLOW_VECVOR > 0
345 head = pool_elt_at_index (tw->timers, ts->head_index);
346 next_index = head->next;
348 while (next_index != ts->head_index)
350 t = pool_elt_at_index (tw->timers, next_index);
351 next_index = t->next;
352 pool_put (tw->timers, t);
354 pool_put (tw->timers, head);
357 memset (tw, 0, sizeof (*tw));
361 * @brief Advance a tw timer wheel. Calls the expired timer callback
362 * as needed. This routine should be called once every timer_interval seconds
363 * @param tw_timer_wheel_t * tw timer wheel template instance pointer
364 * @param f64 now the current time, e.g. from vlib_time_now(vm)
365 * @returns u32 * vector of expired user handles
368 u32 * TW (tw_timer_expire_timers_internal) (TWT (tw_timer_wheel) * tw,
370 u32 * callback_vector_arg)
373 tw_timer_wheel_slot_t *ts;
374 TWT (tw_timer) * t, *head;
375 u32 *callback_vector;
376 u32 fast_wheel_index;
378 u32 slow_wheel_index __attribute__ ((unused));
379 u32 glacier_wheel_index __attribute__ ((unused));
381 /* Shouldn't happen */
382 if (PREDICT_FALSE (now < tw->next_run_time))
383 return callback_vector_arg;
385 /* Number of ticks which have occurred */
386 nticks = tw->ticks_per_second * (now - tw->last_run_time);
388 return callback_vector_arg;
390 /* Remember when we ran, compute next runtime */
391 tw->next_run_time = (now + tw->timer_interval);
393 if (callback_vector_arg == 0)
395 _vec_len (tw->expired_timer_handles) = 0;
396 callback_vector = tw->expired_timer_handles;
399 callback_vector = callback_vector_arg;
401 for (i = 0; i < nticks; i++)
403 fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST];
404 if (TW_TIMER_WHEELS > 1)
405 slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW];
406 if (TW_TIMER_WHEELS > 2)
407 glacier_wheel_index = tw->current_index[TW_TIMER_RING_GLACIER];
409 #if TW_OVERFLOW_VECTOR > 0
410 /* Triple odometer-click? Process the overflow vector... */
411 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
412 && slow_wheel_index == TW_SLOTS_PER_RING
413 && glacier_wheel_index == TW_SLOTS_PER_RING))
416 u32 new_glacier_ring_offset, new_slow_ring_offset;
417 u32 new_fast_ring_offset;
420 head = pool_elt_at_index (tw->timers, ts->head_index);
421 next_index = head->next;
423 /* Make slot empty */
424 head->next = head->prev = ts->head_index;
426 /* traverse slot, place timers wherever they go */
427 while (next_index != head - tw->timers)
429 t = pool_elt_at_index (tw->timers, next_index);
430 next_index = t->next;
432 /* Remove from the overflow vector (hammer) */
433 t->next = t->prev = ~0;
435 ASSERT (t->expiration_time >= tw->current_tick);
437 interval = t->expiration_time - tw->current_tick;
439 /* Right back onto the overflow vector? */
440 if (interval >= (1 << (3 * TW_RING_SHIFT)))
443 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
446 /* Compute ring offsets */
447 new_glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
449 interval -= (new_glacier_ring_offset << (2 * TW_RING_SHIFT));
451 /* Note: the wheels are at (0,0,0), no add-with-carry needed */
452 new_slow_ring_offset = interval >> TW_RING_SHIFT;
453 interval -= (new_slow_ring_offset << TW_RING_SHIFT);
454 new_fast_ring_offset = interval & TW_RING_MASK;
455 t->slow_ring_offset = new_slow_ring_offset;
456 t->fast_ring_offset = new_fast_ring_offset;
458 /* Timer expires Right Now */
459 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
460 t->fast_ring_offset == 0 &&
461 new_glacier_ring_offset == 0))
463 vec_add1 (callback_vector, t->user_handle);
464 pool_put (tw->timers, t);
466 /* Timer moves to the glacier ring */
467 else if (new_glacier_ring_offset)
469 ts = &tw->w[TW_TIMER_RING_GLACIER][new_glacier_ring_offset];
470 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
472 /* Timer moves to the slow ring */
473 else if (t->slow_ring_offset)
475 /* Add to slow ring */
476 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
477 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
479 /* Timer timer moves to the fast ring */
482 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
483 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
489 #if TW_TIMER_WHEELS > 2
491 * Double odometer-click? Process one slot in the glacier ring...
493 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
494 && slow_wheel_index == TW_SLOTS_PER_RING))
496 glacier_wheel_index %= TW_SLOTS_PER_RING;
497 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_wheel_index];
499 head = pool_elt_at_index (tw->timers, ts->head_index);
500 next_index = head->next;
502 /* Make slot empty */
503 head->next = head->prev = ts->head_index;
505 /* traverse slot, deal timers into slow ring */
506 while (next_index != head - tw->timers)
508 t = pool_elt_at_index (tw->timers, next_index);
509 next_index = t->next;
511 /* Remove from glacier ring slot (hammer) */
512 t->next = t->prev = ~0;
514 /* Timer expires Right Now */
515 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
516 t->fast_ring_offset == 0))
518 vec_add1 (callback_vector, t->user_handle);
519 pool_put (tw->timers, t);
521 /* Timer expires during slow-wheel tick 0 */
522 else if (PREDICT_FALSE (t->slow_ring_offset == 0))
524 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
525 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
527 else /* typical case */
529 /* Add to slow ring */
530 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
531 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
537 #if TW_TIMER_WHEELS > 1
539 * Single odometer-click? Process a slot in the slow ring,
541 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
543 slow_wheel_index %= TW_SLOTS_PER_RING;
544 ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index];
546 head = pool_elt_at_index (tw->timers, ts->head_index);
547 next_index = head->next;
549 /* Make slot empty */
550 head->next = head->prev = ts->head_index;
552 /* traverse slot, deal timers into fast ring */
553 while (next_index != head - tw->timers)
555 t = pool_elt_at_index (tw->timers, next_index);
556 next_index = t->next;
558 /* Remove from sloe ring slot (hammer) */
559 t->next = t->prev = ~0;
561 /* Timer expires Right Now */
562 if (PREDICT_FALSE (t->fast_ring_offset == 0))
564 vec_add1 (callback_vector, t->user_handle);
565 pool_put (tw->timers, t);
567 else /* typical case */
569 /* Add to fast ring */
570 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
571 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
577 /* Handle the fast ring */
578 fast_wheel_index %= TW_SLOTS_PER_RING;
579 ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index];
581 head = pool_elt_at_index (tw->timers, ts->head_index);
582 next_index = head->next;
584 /* Make slot empty */
585 head->next = head->prev = ts->head_index;
587 /* Construct vector of expired timer handles to give the user */
588 while (next_index != ts->head_index)
590 t = pool_elt_at_index (tw->timers, next_index);
591 next_index = t->next;
592 vec_add1 (callback_vector, t->user_handle);
593 pool_put (tw->timers, t);
596 /* If any timers expired, tell the user */
597 if (callback_vector_arg == 0 && vec_len (callback_vector))
599 /* The callback is optional. We return the u32 * handle vector */
600 if (tw->expired_timer_callback)
602 tw->expired_timer_callback (callback_vector);
603 _vec_len (callback_vector) = 0;
605 tw->expired_timer_handles = callback_vector;
609 tw->current_index[TW_TIMER_RING_FAST] = fast_wheel_index;
611 #if TW_TIMER_WHEELS > 1
612 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
614 tw->current_index[TW_TIMER_RING_SLOW] = slow_wheel_index;
617 #if TW_TIMER_WHEELS > 2
618 if (PREDICT_FALSE (slow_wheel_index == TW_SLOTS_PER_RING))
619 glacier_wheel_index++;
620 tw->current_index[TW_TIMER_RING_GLACIER] = glacier_wheel_index;
623 if (vec_len (callback_vector) >= tw->max_expirations)
627 if (callback_vector_arg == 0)
628 tw->expired_timer_handles = callback_vector;
630 tw->last_run_time += i * tw->timer_interval;
631 return callback_vector;
634 u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now)
636 return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ );
639 u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now,
642 return TW (tw_timer_expire_timers_internal) (tw, now, vec);
646 * fd.io coding-style-patch-verification: ON
649 * eval: (c-set-style "gnu")