blob: c0a9685aa82267e4312fc6ca5c18675954ca3743 [file] [log] [blame]
Dave Barach8e8f98c2017-02-03 11:58:53 -05001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16/** @file
17 * @brief TW timer implementation TEMPLATE ONLY, do not compile directly
18 *
19 *
20 */
Dave Barach8e8f98c2017-02-03 11:58:53 -050021static inline u32
22TW (make_internal_timer_handle) (u32 pool_index, u32 timer_id)
23{
24 u32 handle;
25
26 ASSERT (timer_id < TW_TIMERS_PER_OBJECT);
Dave Barach4af9ba12017-06-07 15:18:23 -040027#if LOG2_TW_TIMERS_PER_OBJECT > 0
Dave Barach8e8f98c2017-02-03 11:58:53 -050028 ASSERT (pool_index < (1 << (32 - LOG2_TW_TIMERS_PER_OBJECT)));
29
30 handle = (timer_id << (32 - LOG2_TW_TIMERS_PER_OBJECT)) | (pool_index);
Dave Barach4af9ba12017-06-07 15:18:23 -040031#else
32 handle = pool_index;
33#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -050034 return handle;
35}
36
37static inline void
38timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
39{
40 TWT (tw_timer) * head = pool_elt_at_index (pool, head_index);
41 TWT (tw_timer) * old_first;
42 u32 old_first_index;
43 TWT (tw_timer) * new;
44
45 new = pool_elt_at_index (pool, new_index);
46
47 if (PREDICT_FALSE (head->next == head_index))
48 {
49 head->next = head->prev = new_index;
50 new->next = new->prev = head_index;
51 return;
52 }
53
54 old_first_index = head->next;
55 old_first = pool_elt_at_index (pool, old_first_index);
56
57 new->next = old_first_index;
58 new->prev = old_first->prev;
59 old_first->prev = new_index;
60 head->next = new_index;
61}
62
63static inline void
64timer_remove (TWT (tw_timer) * pool, u32 index)
65{
66 TWT (tw_timer) * elt = pool_elt_at_index (pool, index);
67 TWT (tw_timer) * next_elt, *prev_elt;
68
69 ASSERT (elt->user_handle != ~0);
70
71 next_elt = pool_elt_at_index (pool, elt->next);
72 prev_elt = pool_elt_at_index (pool, elt->prev);
73
74 next_elt->prev = elt->prev;
75 prev_elt->next = elt->next;
76
77 elt->prev = elt->next = ~0;
78}
79
80/**
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.
Dave Barach4af9ba12017-06-07 15:18:23 -040085 * @param u64 interval timer interval in ticks
Dave Barach8e8f98c2017-02-03 11:58:53 -050086 * @returns handle needed to cancel the timer
87 */
88u32
89TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
Dave Barach4af9ba12017-06-07 15:18:23 -040090 u64 interval)
Dave Barach8e8f98c2017-02-03 11:58:53 -050091{
92#if TW_TIMER_WHEELS > 1
93 u16 slow_ring_offset;
94 u32 carry;
95#endif
Dave Barach4af9ba12017-06-07 15:18:23 -040096#if TW_TIMER_WHEELS > 2
97 u16 glacier_ring_offset;
98#endif
99#if TW_OVERFLOW_VECTOR > 0
100 u64 interval_plus_time_to_wrap, triple_wrap_mask;
101#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500102 u16 fast_ring_offset;
103 tw_timer_wheel_slot_t *ts;
104 TWT (tw_timer) * t;
105
106 ASSERT (interval);
107
108 pool_get (tw->timers, t);
Dave Barach4af9ba12017-06-07 15:18:23 -0400109 memset (t, 0xff, sizeof (*t));
110
Dave Barach8e8f98c2017-02-03 11:58:53 -0500111 t->user_handle = TW (make_internal_timer_handle) (pool_index, timer_id);
112
Dave Barach4af9ba12017-06-07 15:18:23 -0400113 /* Factor interval into 1..3 wheel offsets */
114#if TW_TIMER_WHEELS > 2
115#if TW_OVERFLOW_VECTOR > 0
116 /*
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.
121 */
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)))
126 {
127 t->expiration_time = tw->current_tick + interval;
128 ts = &tw->overflow;
129 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
130 return t - tw->timers;
131 }
132#endif
133
134 glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
Dave Barach28b70af2017-06-13 17:04:28 -0400135 ASSERT ((u64) glacier_ring_offset < TW_SLOTS_PER_RING);
136 interval -= (((u64) glacier_ring_offset) << (2 * TW_RING_SHIFT));
Dave Barach4af9ba12017-06-07 15:18:23 -0400137#endif
138#if TW_TIMER_WHEELS > 1
139 slow_ring_offset = interval >> TW_RING_SHIFT;
Dave Barach28b70af2017-06-13 17:04:28 -0400140 ASSERT ((u64) slow_ring_offset < TW_SLOTS_PER_RING);
141 interval -= (((u64) slow_ring_offset) << TW_RING_SHIFT);
Dave Barach4af9ba12017-06-07 15:18:23 -0400142#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500143 fast_ring_offset = interval & TW_RING_MASK;
Dave Barach4af9ba12017-06-07 15:18:23 -0400144
145 /*
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, ...)
150 */
151
152 fast_ring_offset += tw->current_index[TW_TIMER_RING_FAST] & TW_RING_MASK;
153
Dave Barach8e8f98c2017-02-03 11:58:53 -0500154#if TW_TIMER_WHEELS > 1
155 carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
156 fast_ring_offset %= TW_SLOTS_PER_RING;
Dave Barach4af9ba12017-06-07 15:18:23 -0400157 slow_ring_offset += (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK)
158 + carry;
159 carry = slow_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
160 slow_ring_offset %= TW_SLOTS_PER_RING;
161#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500162
Dave Barach4af9ba12017-06-07 15:18:23 -0400163#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;
167#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500168
Dave Barach4af9ba12017-06-07 15:18:23 -0400169#if TW_TIMER_WHEELS > 2
170 if (glacier_ring_offset !=
171 (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK))
Dave Barach8e8f98c2017-02-03 11:58:53 -0500172 {
Dave Barach4af9ba12017-06-07 15:18:23 -0400173 /* We'll need slow and fast ring offsets later */
174 t->slow_ring_offset = slow_ring_offset;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500175 t->fast_ring_offset = fast_ring_offset;
Dave Barach4af9ba12017-06-07 15:18:23 -0400176
177 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_ring_offset];
178
179 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
180
181 return t - tw->timers;
182 }
183#endif
184
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))
189 {
190 /* We'll need the fast ring offset later... */
191 t->fast_ring_offset = fast_ring_offset;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500192
193 ts = &tw->w[TW_TIMER_RING_SLOW][slow_ring_offset];
194
195 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
196
197 return t - tw->timers;
198 }
199#else
200 fast_ring_offset %= TW_SLOTS_PER_RING;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500201#endif
202
203 /* Timer expires less than one fast-ring revolution from now */
204 ts = &tw->w[TW_TIMER_RING_FAST][fast_ring_offset];
205
206 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400207
208#if TW_FAST_WHEEL_BITMAP
209 tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap,
210 fast_ring_offset, 1);
211#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500212 return t - tw->timers;
213}
214
Dave Barach4af9ba12017-06-07 15:18:23 -0400215#if TW_TIMER_SCAN_FOR_HANDLE > 0
216int TW (scan_for_handle) (TWT (tw_timer_wheel) * tw, u32 handle)
217{
218 int i, j;
219 tw_timer_wheel_slot_t *ts;
220 TWT (tw_timer) * t, *head;
221 u32 next_index;
222 int rv = 0;
223
224 for (i = 0; i < TW_TIMER_WHEELS; i++)
225 {
226 for (j = 0; j < TW_SLOTS_PER_RING; j++)
227 {
228 ts = &tw->w[i][j];
229 head = pool_elt_at_index (tw->timers, ts->head_index);
230 next_index = head->next;
231
232 while (next_index != ts->head_index)
233 {
234 t = pool_elt_at_index (tw->timers, next_index);
235 if (next_index == handle)
236 {
237 clib_warning ("handle %d found in ring %d slot %d",
238 handle, i, j);
239 clib_warning ("user handle 0x%x", t->user_handle);
240 rv = 1;
241 }
242 next_index = t->next;
243 }
244 }
245 }
246 return rv;
247}
248#endif /* TW_TIMER_SCAN_FOR_HANDLE */
249
Dave Barach8e8f98c2017-02-03 11:58:53 -0500250/**
251 * @brief Stop a tw timer
252 * @param tw_timer_wheel_t * tw timer wheel object pointer
Dave Barach8e8f98c2017-02-03 11:58:53 -0500253 * @param u32 handle timer cancellation returned by tw_timer_start
254 */
Dave Barach8e8f98c2017-02-03 11:58:53 -0500255void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
256{
257 TWT (tw_timer) * t;
258
Dave Barach5c20a012017-06-13 08:48:31 -0400259#if TW_TIMER_ALLOW_DUPLICATE_STOP
260 /*
261 * A vlib process may have its timer expire, and receive
262 * an event before the expiration is processed.
263 * That results in a duplicate tw_timer_stop.
264 */
265 if (pool_is_free_index (tw->timers, handle))
266 return;
267#endif
268
Dave Barach8e8f98c2017-02-03 11:58:53 -0500269 t = pool_elt_at_index (tw->timers, handle);
270
271 /* in case of idiotic handle (e.g. passing a listhead index) */
272 ASSERT (t->user_handle != ~0);
273
274 timer_remove (tw->timers, handle);
275
276 pool_put_index (tw->timers, handle);
277}
278
279/**
280 * @brief Initialize a tw timer wheel template instance
281 * @param tw_timer_wheel_t * tw timer wheel object pointer
282 * @param void * expired_timer_callback. Passed a u32 * vector of
Dave Barach4af9ba12017-06-07 15:18:23 -0400283 * expired timer handles. The callback is optional.
Dave Barach8e8f98c2017-02-03 11:58:53 -0500284 * @param f64 timer_interval_in_seconds
285 */
286void
287TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
288 void *expired_timer_callback,
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100289 f64 timer_interval_in_seconds, u32 max_expirations)
Dave Barach8e8f98c2017-02-03 11:58:53 -0500290{
291 int ring, slot;
292 tw_timer_wheel_slot_t *ts;
293 TWT (tw_timer) * t;
294 memset (tw, 0, sizeof (*tw));
295 tw->expired_timer_callback = expired_timer_callback;
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100296 tw->max_expirations = max_expirations;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500297 if (timer_interval_in_seconds == 0.0)
298 {
299 clib_warning ("timer interval is zero");
300 abort ();
301 }
302 tw->timer_interval = timer_interval_in_seconds;
303 tw->ticks_per_second = 1.0 / timer_interval_in_seconds;
Dave Barach4af9ba12017-06-07 15:18:23 -0400304 tw->first_expires_tick = ~0ULL;
305 vec_validate (tw->expired_timer_handles, 0);
306 _vec_len (tw->expired_timer_handles) = 0;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500307
308 for (ring = 0; ring < TW_TIMER_WHEELS; ring++)
309 {
310 for (slot = 0; slot < TW_SLOTS_PER_RING; slot++)
311 {
312 ts = &tw->w[ring][slot];
313 pool_get (tw->timers, t);
314 memset (t, 0xff, sizeof (*t));
315 t->next = t->prev = t - tw->timers;
316 ts->head_index = t - tw->timers;
317 }
318 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400319
320#if TW_OVERFLOW_VECTOR > 0
321 ts = &tw->overflow;
322 pool_get (tw->timers, t);
323 memset (t, 0xff, sizeof (*t));
324 t->next = t->prev = t - tw->timers;
325 ts->head_index = t - tw->timers;
326#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500327}
328
329/**
330 * @brief Free a tw timer wheel template instance
331 * @param tw_timer_wheel_t * tw timer wheel object pointer
332 */
333void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
334{
335 int i, j;
336 tw_timer_wheel_slot_t *ts;
337 TWT (tw_timer) * head, *t;
338 u32 next_index;
339
340 for (i = 0; i < TW_TIMER_WHEELS; i++)
341 {
342 for (j = 0; j < TW_SLOTS_PER_RING; j++)
343 {
344 ts = &tw->w[i][j];
345 head = pool_elt_at_index (tw->timers, ts->head_index);
346 next_index = head->next;
347
348 while (next_index != ts->head_index)
349 {
350 t = pool_elt_at_index (tw->timers, next_index);
351 next_index = t->next;
352 pool_put (tw->timers, t);
353 }
354 pool_put (tw->timers, head);
355 }
356 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400357
358#if TW_OVERFLOW_VECVOR > 0
359 ts = &tw->overflow;
360 head = pool_elt_at_index (tw->timers, ts->head_index);
361 next_index = head->next;
362
363 while (next_index != ts->head_index)
364 {
365 t = pool_elt_at_index (tw->timers, next_index);
366 next_index = t->next;
367 pool_put (tw->timers, t);
368 }
369 pool_put (tw->timers, head);
370#endif
371
Dave Barach8e8f98c2017-02-03 11:58:53 -0500372 memset (tw, 0, sizeof (*tw));
373}
374
375/**
376 * @brief Advance a tw timer wheel. Calls the expired timer callback
377 * as needed. This routine should be called once every timer_interval seconds
378 * @param tw_timer_wheel_t * tw timer wheel template instance pointer
379 * @param f64 now the current time, e.g. from vlib_time_now(vm)
Dave Barach4af9ba12017-06-07 15:18:23 -0400380 * @returns u32 * vector of expired user handles
Dave Barach8e8f98c2017-02-03 11:58:53 -0500381 */
Dave Barach4af9ba12017-06-07 15:18:23 -0400382static inline
383 u32 * TW (tw_timer_expire_timers_internal) (TWT (tw_timer_wheel) * tw,
384 f64 now,
385 u32 * callback_vector_arg)
Dave Barach8e8f98c2017-02-03 11:58:53 -0500386{
387 u32 nticks, i;
388 tw_timer_wheel_slot_t *ts;
389 TWT (tw_timer) * t, *head;
Dave Barach4af9ba12017-06-07 15:18:23 -0400390 u32 *callback_vector;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500391 u32 fast_wheel_index;
392 u32 next_index;
Dave Barach4af9ba12017-06-07 15:18:23 -0400393 u32 slow_wheel_index __attribute__ ((unused));
394 u32 glacier_wheel_index __attribute__ ((unused));
Dave Barach8e8f98c2017-02-03 11:58:53 -0500395
396 /* Shouldn't happen */
397 if (PREDICT_FALSE (now < tw->next_run_time))
Dave Barach4af9ba12017-06-07 15:18:23 -0400398 return callback_vector_arg;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500399
400 /* Number of ticks which have occurred */
401 nticks = tw->ticks_per_second * (now - tw->last_run_time);
402 if (nticks == 0)
Dave Barach4af9ba12017-06-07 15:18:23 -0400403 return callback_vector_arg;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500404
405 /* Remember when we ran, compute next runtime */
406 tw->next_run_time = (now + tw->timer_interval);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500407
Dave Barach4af9ba12017-06-07 15:18:23 -0400408 if (callback_vector_arg == 0)
409 {
410 _vec_len (tw->expired_timer_handles) = 0;
411 callback_vector = tw->expired_timer_handles;
412 }
413 else
414 callback_vector = callback_vector_arg;
415
Dave Barach8e8f98c2017-02-03 11:58:53 -0500416 for (i = 0; i < nticks; i++)
417 {
418 fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST];
Dave Barach4af9ba12017-06-07 15:18:23 -0400419 if (TW_TIMER_WHEELS > 1)
420 slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW];
421 if (TW_TIMER_WHEELS > 2)
422 glacier_wheel_index = tw->current_index[TW_TIMER_RING_GLACIER];
Dave Barach8e8f98c2017-02-03 11:58:53 -0500423
Dave Barach4af9ba12017-06-07 15:18:23 -0400424#if TW_OVERFLOW_VECTOR > 0
425 /* Triple odometer-click? Process the overflow vector... */
426 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
427 && slow_wheel_index == TW_SLOTS_PER_RING
428 && glacier_wheel_index == TW_SLOTS_PER_RING))
429 {
430 u64 interval;
431 u32 new_glacier_ring_offset, new_slow_ring_offset;
432 u32 new_fast_ring_offset;
433
434 ts = &tw->overflow;
435 head = pool_elt_at_index (tw->timers, ts->head_index);
436 next_index = head->next;
437
438 /* Make slot empty */
439 head->next = head->prev = ts->head_index;
440
441 /* traverse slot, place timers wherever they go */
442 while (next_index != head - tw->timers)
443 {
444 t = pool_elt_at_index (tw->timers, next_index);
445 next_index = t->next;
446
447 /* Remove from the overflow vector (hammer) */
448 t->next = t->prev = ~0;
449
450 ASSERT (t->expiration_time >= tw->current_tick);
451
452 interval = t->expiration_time - tw->current_tick;
453
454 /* Right back onto the overflow vector? */
455 if (interval >= (1 << (3 * TW_RING_SHIFT)))
456 {
457 ts = &tw->overflow;
458 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
459 continue;
460 }
461 /* Compute ring offsets */
462 new_glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
463
464 interval -= (new_glacier_ring_offset << (2 * TW_RING_SHIFT));
465
466 /* Note: the wheels are at (0,0,0), no add-with-carry needed */
467 new_slow_ring_offset = interval >> TW_RING_SHIFT;
468 interval -= (new_slow_ring_offset << TW_RING_SHIFT);
469 new_fast_ring_offset = interval & TW_RING_MASK;
470 t->slow_ring_offset = new_slow_ring_offset;
471 t->fast_ring_offset = new_fast_ring_offset;
472
473 /* Timer expires Right Now */
474 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
475 t->fast_ring_offset == 0 &&
476 new_glacier_ring_offset == 0))
477 {
478 vec_add1 (callback_vector, t->user_handle);
479 pool_put (tw->timers, t);
480 }
481 /* Timer moves to the glacier ring */
482 else if (new_glacier_ring_offset)
483 {
484 ts = &tw->w[TW_TIMER_RING_GLACIER][new_glacier_ring_offset];
485 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
486 }
487 /* Timer moves to the slow ring */
488 else if (t->slow_ring_offset)
489 {
490 /* Add to slow ring */
491 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
492 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
493 }
494 /* Timer timer moves to the fast ring */
495 else
496 {
497 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
498 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400499#if TW_FAST_WHEEL_BITMAP
500 tw->fast_slot_bitmap =
501 clib_bitmap_set (tw->fast_slot_bitmap,
502 t->fast_ring_offset, 1);
503#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400504 }
505 }
506 }
507#endif
508
509#if TW_TIMER_WHEELS > 2
Dave Barach8e8f98c2017-02-03 11:58:53 -0500510 /*
Dave Barach4af9ba12017-06-07 15:18:23 -0400511 * Double odometer-click? Process one slot in the glacier ring...
512 */
513 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
514 && slow_wheel_index == TW_SLOTS_PER_RING))
515 {
516 glacier_wheel_index %= TW_SLOTS_PER_RING;
517 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_wheel_index];
518
519 head = pool_elt_at_index (tw->timers, ts->head_index);
520 next_index = head->next;
521
522 /* Make slot empty */
523 head->next = head->prev = ts->head_index;
524
525 /* traverse slot, deal timers into slow ring */
526 while (next_index != head - tw->timers)
527 {
528 t = pool_elt_at_index (tw->timers, next_index);
529 next_index = t->next;
530
531 /* Remove from glacier ring slot (hammer) */
532 t->next = t->prev = ~0;
533
534 /* Timer expires Right Now */
535 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
536 t->fast_ring_offset == 0))
537 {
538 vec_add1 (callback_vector, t->user_handle);
539 pool_put (tw->timers, t);
540 }
541 /* Timer expires during slow-wheel tick 0 */
542 else if (PREDICT_FALSE (t->slow_ring_offset == 0))
543 {
544 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
545 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400546#if TW_FAST_WHEEL_BITMAP
547 tw->fast_slot_bitmap =
548 clib_bitmap_set (tw->fast_slot_bitmap,
549 t->fast_ring_offset, 1);
550#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400551 }
552 else /* typical case */
553 {
554 /* Add to slow ring */
555 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
556 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
557 }
558 }
559 }
560#endif
561
562#if TW_TIMER_WHEELS > 1
563 /*
564 * Single odometer-click? Process a slot in the slow ring,
Dave Barach8e8f98c2017-02-03 11:58:53 -0500565 */
566 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
567 {
Dave Barach4af9ba12017-06-07 15:18:23 -0400568 slow_wheel_index %= TW_SLOTS_PER_RING;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500569 ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index];
570
571 head = pool_elt_at_index (tw->timers, ts->head_index);
572 next_index = head->next;
573
574 /* Make slot empty */
575 head->next = head->prev = ts->head_index;
576
577 /* traverse slot, deal timers into fast ring */
578 while (next_index != head - tw->timers)
579 {
580 t = pool_elt_at_index (tw->timers, next_index);
581 next_index = t->next;
582
Dave Barach4af9ba12017-06-07 15:18:23 -0400583 /* Remove from sloe ring slot (hammer) */
Dave Barach8e8f98c2017-02-03 11:58:53 -0500584 t->next = t->prev = ~0;
Dave Barach4af9ba12017-06-07 15:18:23 -0400585
586 /* Timer expires Right Now */
587 if (PREDICT_FALSE (t->fast_ring_offset == 0))
588 {
589 vec_add1 (callback_vector, t->user_handle);
590 pool_put (tw->timers, t);
591 }
592 else /* typical case */
593 {
594 /* Add to fast ring */
595 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
596 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400597#if TW_FAST_WHEEL_BITMAP
598 tw->fast_slot_bitmap =
599 clib_bitmap_set (tw->fast_slot_bitmap,
600 t->fast_ring_offset, 1);
601#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400602 }
Dave Barach8e8f98c2017-02-03 11:58:53 -0500603 }
Dave Barach8e8f98c2017-02-03 11:58:53 -0500604 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400605#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500606
607 /* Handle the fast ring */
Dave Barach4af9ba12017-06-07 15:18:23 -0400608 fast_wheel_index %= TW_SLOTS_PER_RING;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500609 ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index];
610
611 head = pool_elt_at_index (tw->timers, ts->head_index);
612 next_index = head->next;
613
614 /* Make slot empty */
615 head->next = head->prev = ts->head_index;
616
617 /* Construct vector of expired timer handles to give the user */
618 while (next_index != ts->head_index)
619 {
620 t = pool_elt_at_index (tw->timers, next_index);
621 next_index = t->next;
Dave Barach4af9ba12017-06-07 15:18:23 -0400622 vec_add1 (callback_vector, t->user_handle);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500623 pool_put (tw->timers, t);
624 }
625
626 /* If any timers expired, tell the user */
Dave Barach4af9ba12017-06-07 15:18:23 -0400627 if (callback_vector_arg == 0 && vec_len (callback_vector))
Gabriel Ganne581b0722017-02-13 10:27:15 +0100628 {
Dave Barach4af9ba12017-06-07 15:18:23 -0400629 /* The callback is optional. We return the u32 * handle vector */
630 if (tw->expired_timer_callback)
631 {
632 tw->expired_timer_callback (callback_vector);
633 _vec_len (callback_vector) = 0;
634 }
635 tw->expired_timer_handles = callback_vector;
Gabriel Ganne581b0722017-02-13 10:27:15 +0100636 }
Dave Barach5c20a012017-06-13 08:48:31 -0400637
638#if TW_FAST_WHEEL_BITMAP
639 tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap,
640 fast_wheel_index, 0);
641#endif
642
Dave Barach8e8f98c2017-02-03 11:58:53 -0500643 tw->current_tick++;
Dave Barach4af9ba12017-06-07 15:18:23 -0400644 fast_wheel_index++;
645 tw->current_index[TW_TIMER_RING_FAST] = fast_wheel_index;
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100646
Dave Barach4af9ba12017-06-07 15:18:23 -0400647#if TW_TIMER_WHEELS > 1
648 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
649 slow_wheel_index++;
650 tw->current_index[TW_TIMER_RING_SLOW] = slow_wheel_index;
651#endif
652
653#if TW_TIMER_WHEELS > 2
654 if (PREDICT_FALSE (slow_wheel_index == TW_SLOTS_PER_RING))
655 glacier_wheel_index++;
656 tw->current_index[TW_TIMER_RING_GLACIER] = glacier_wheel_index;
657#endif
658
659 if (vec_len (callback_vector) >= tw->max_expirations)
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100660 break;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500661 }
Gabriel Ganne581b0722017-02-13 10:27:15 +0100662
Dave Barach4af9ba12017-06-07 15:18:23 -0400663 if (callback_vector_arg == 0)
664 tw->expired_timer_handles = callback_vector;
665
Florin Coras954898f2017-02-21 19:26:51 -0800666 tw->last_run_time += i * tw->timer_interval;
Dave Barach4af9ba12017-06-07 15:18:23 -0400667 return callback_vector;
668}
669
670u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now)
671{
672 return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ );
673}
674
675u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now,
676 u32 * vec)
677{
678 return TW (tw_timer_expire_timers_internal) (tw, now, vec);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500679}
680
Dave Barach5c20a012017-06-13 08:48:31 -0400681#if TW_FAST_WHEEL_BITMAP
682/** Returns an approximation to the first timer expiration in
683 * timer-ticks from "now". To avoid wasting an unjustifiable
684 * amount of time on the problem, we maintain an approximate fast-wheel slot
685 * occupancy bitmap. We don't worry about clearing fast wheel bits
686 * when timers are removed from fast wheel slots.
687 */
688
689u32 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw)
690{
691 u32 first_expiring_index, fast_ring_index;
692 i32 delta;
693
694 if (clib_bitmap_is_zero (tw->fast_slot_bitmap))
695 return TW_SLOTS_PER_RING;
696
697 fast_ring_index = tw->current_index[TW_TIMER_RING_FAST];
698 if (fast_ring_index == TW_SLOTS_PER_RING)
699 fast_ring_index = 0;
700
701 first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap,
702 fast_ring_index);
703 if (first_expiring_index == ~0 && fast_ring_index != 0)
704 first_expiring_index = clib_bitmap_first_set (tw->fast_slot_bitmap);
705
706 ASSERT (first_expiring_index != ~0);
707
708 delta = (i32) first_expiring_index - (i32) fast_ring_index;
709 if (delta < 0)
710 delta += TW_SLOTS_PER_RING;
711
712 ASSERT (delta >= 0);
713
714 return (u32) delta;
715}
716
717#endif
718
Dave Barach8e8f98c2017-02-03 11:58:53 -0500719/*
720 * fd.io coding-style-patch-verification: ON
721 *
722 * Local Variables:
723 * eval: (c-set-style "gnu")
724 * End:
725 */