blob: c96c329c93190693944648e7825774b9fa08d7c4 [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 Barachb7f1faa2017-08-29 11:43:37 -040021#if TW_START_STOP_TRACE_SIZE > 0
22
23void TW (tw_timer_trace) (TWT (tw_timer_wheel) * tw, u32 timer_id,
24 u32 pool_index, u32 handle)
25{
26 TWT (trace) * t = &tw->traces[tw->trace_index];
27
28 t->timer_id = timer_id;
29 t->pool_index = pool_index;
30 t->handle = handle;
31
32 tw->trace_index++;
33 if (tw->trace_index == TW_START_STOP_TRACE_SIZE)
34 {
35 tw->trace_index = 0;
36 tw->trace_wrapped++;
37 }
38}
39
40void TW (tw_search_trace) (TWT (tw_timer_wheel) * tw, u32 handle)
41{
42 u32 i, start_pos;
43 TWT (trace) * t;
44 char *s = "bogus!";
45
46 /* reverse search for the supplied handle */
47
48 start_pos = tw->trace_index;
49 if (start_pos == 0)
50 start_pos = TW_START_STOP_TRACE_SIZE - 1;
51 else
52 start_pos--;
53
54 for (i = start_pos; i > 0; i--)
55 {
56 t = &tw->traces[i];
57 if (t->handle == handle)
58 {
59 switch (t->timer_id)
60 {
61 case 0xFF:
62 s = "stopped";
63 break;
64 case 0xFE:
65 s = "expired";
66 break;
67 default:
68 s = "started";
69 break;
70 }
71 fformat (stderr, "handle 0x%x (%d) %s at trace %d\n",
72 handle, handle, s, i);
73 }
74 }
75 if (tw->trace_wrapped > 0)
76 {
77 for (i = TW_START_STOP_TRACE_SIZE; i >= tw->trace_index; i--)
78 {
79 t = &tw->traces[i];
80 if (t->handle == handle)
81 {
82 switch (t->timer_id)
83 {
84 case 0xFF:
85 s = "stopped";
86 break;
87 case 0xFE:
88 s = "expired";
89 break;
90 default:
91 s = "started";
92 break;
93 }
94 fformat (stderr, "handle 0x%x (%d) %s at trace %d\n",
95 handle, handle, s, i);
96 }
97 }
98 }
99}
100#endif /* TW_START_STOP_TRACE_SIZE > 0 */
101
Dave Barach8e8f98c2017-02-03 11:58:53 -0500102static inline u32
103TW (make_internal_timer_handle) (u32 pool_index, u32 timer_id)
104{
105 u32 handle;
106
107 ASSERT (timer_id < TW_TIMERS_PER_OBJECT);
Dave Barach4af9ba12017-06-07 15:18:23 -0400108#if LOG2_TW_TIMERS_PER_OBJECT > 0
Dave Barach8e8f98c2017-02-03 11:58:53 -0500109 ASSERT (pool_index < (1 << (32 - LOG2_TW_TIMERS_PER_OBJECT)));
110
111 handle = (timer_id << (32 - LOG2_TW_TIMERS_PER_OBJECT)) | (pool_index);
Dave Barach4af9ba12017-06-07 15:18:23 -0400112#else
113 handle = pool_index;
114#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500115 return handle;
116}
117
118static inline void
119timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
120{
121 TWT (tw_timer) * head = pool_elt_at_index (pool, head_index);
122 TWT (tw_timer) * old_first;
123 u32 old_first_index;
124 TWT (tw_timer) * new;
125
126 new = pool_elt_at_index (pool, new_index);
127
128 if (PREDICT_FALSE (head->next == head_index))
129 {
130 head->next = head->prev = new_index;
131 new->next = new->prev = head_index;
132 return;
133 }
134
135 old_first_index = head->next;
136 old_first = pool_elt_at_index (pool, old_first_index);
137
138 new->next = old_first_index;
139 new->prev = old_first->prev;
140 old_first->prev = new_index;
141 head->next = new_index;
142}
143
144static inline void
Florin Corasadb5bd52018-06-22 15:29:38 -0700145timer_remove (TWT (tw_timer) * pool, TWT (tw_timer) * elt)
Dave Barach8e8f98c2017-02-03 11:58:53 -0500146{
Dave Barach8e8f98c2017-02-03 11:58:53 -0500147 TWT (tw_timer) * next_elt, *prev_elt;
148
149 ASSERT (elt->user_handle != ~0);
150
151 next_elt = pool_elt_at_index (pool, elt->next);
152 prev_elt = pool_elt_at_index (pool, elt->prev);
153
154 next_elt->prev = elt->prev;
155 prev_elt->next = elt->next;
156
157 elt->prev = elt->next = ~0;
158}
159
Florin Coras208daa12018-07-02 01:30:51 -0700160static inline void
Florin Corasadb5bd52018-06-22 15:29:38 -0700161timer_add (TWT (tw_timer_wheel) * tw, TWT (tw_timer) * t, u64 interval)
Dave Barach8e8f98c2017-02-03 11:58:53 -0500162{
163#if TW_TIMER_WHEELS > 1
164 u16 slow_ring_offset;
165 u32 carry;
166#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400167#if TW_TIMER_WHEELS > 2
168 u16 glacier_ring_offset;
169#endif
170#if TW_OVERFLOW_VECTOR > 0
171 u64 interval_plus_time_to_wrap, triple_wrap_mask;
172#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500173 u16 fast_ring_offset;
174 tw_timer_wheel_slot_t *ts;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500175
Dave Barach4af9ba12017-06-07 15:18:23 -0400176 /* Factor interval into 1..3 wheel offsets */
177#if TW_TIMER_WHEELS > 2
178#if TW_OVERFLOW_VECTOR > 0
179 /*
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.
184 */
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)))
189 {
190 t->expiration_time = tw->current_tick + interval;
191 ts = &tw->overflow;
192 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400193#if TW_START_STOP_TRACE_SIZE > 0
Florin Corasadb5bd52018-06-22 15:29:38 -0700194 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400195#endif
Florin Corasadb5bd52018-06-22 15:29:38 -0700196 return;
Dave Barach4af9ba12017-06-07 15:18:23 -0400197 }
198#endif
199
200 glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
Dave Barach28b70af2017-06-13 17:04:28 -0400201 ASSERT ((u64) glacier_ring_offset < TW_SLOTS_PER_RING);
202 interval -= (((u64) glacier_ring_offset) << (2 * TW_RING_SHIFT));
Dave Barach4af9ba12017-06-07 15:18:23 -0400203#endif
204#if TW_TIMER_WHEELS > 1
205 slow_ring_offset = interval >> TW_RING_SHIFT;
Dave Barach28b70af2017-06-13 17:04:28 -0400206 ASSERT ((u64) slow_ring_offset < TW_SLOTS_PER_RING);
207 interval -= (((u64) slow_ring_offset) << TW_RING_SHIFT);
Dave Barach4af9ba12017-06-07 15:18:23 -0400208#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500209 fast_ring_offset = interval & TW_RING_MASK;
Dave Barach4af9ba12017-06-07 15:18:23 -0400210
211 /*
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, ...)
216 */
217
218 fast_ring_offset += tw->current_index[TW_TIMER_RING_FAST] & TW_RING_MASK;
219
Dave Barach8e8f98c2017-02-03 11:58:53 -0500220#if TW_TIMER_WHEELS > 1
221 carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
222 fast_ring_offset %= TW_SLOTS_PER_RING;
Dave Barach4af9ba12017-06-07 15:18:23 -0400223 slow_ring_offset += (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK)
224 + carry;
225 carry = slow_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
226 slow_ring_offset %= TW_SLOTS_PER_RING;
227#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500228
Dave Barach4af9ba12017-06-07 15:18:23 -0400229#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;
233#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500234
Dave Barach4af9ba12017-06-07 15:18:23 -0400235#if TW_TIMER_WHEELS > 2
236 if (glacier_ring_offset !=
237 (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK))
Dave Barach8e8f98c2017-02-03 11:58:53 -0500238 {
Dave Barach4af9ba12017-06-07 15:18:23 -0400239 /* We'll need slow and fast ring offsets later */
240 t->slow_ring_offset = slow_ring_offset;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500241 t->fast_ring_offset = fast_ring_offset;
Dave Barach4af9ba12017-06-07 15:18:23 -0400242
243 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_ring_offset];
244
245 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400246#if TW_START_STOP_TRACE_SIZE > 0
Florin Corasadb5bd52018-06-22 15:29:38 -0700247 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400248#endif
Florin Corasadb5bd52018-06-22 15:29:38 -0700249 return;
Dave Barach4af9ba12017-06-07 15:18:23 -0400250 }
251#endif
252
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))
257 {
258 /* We'll need the fast ring offset later... */
259 t->fast_ring_offset = fast_ring_offset;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500260
261 ts = &tw->w[TW_TIMER_RING_SLOW][slow_ring_offset];
262
263 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400264#if TW_START_STOP_TRACE_SIZE > 0
Florin Corasadb5bd52018-06-22 15:29:38 -0700265 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400266#endif
Florin Corasadb5bd52018-06-22 15:29:38 -0700267 return;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500268 }
269#else
270 fast_ring_offset %= TW_SLOTS_PER_RING;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500271#endif
272
273 /* Timer expires less than one fast-ring revolution from now */
274 ts = &tw->w[TW_TIMER_RING_FAST][fast_ring_offset];
275
276 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400277
278#if TW_FAST_WHEEL_BITMAP
279 tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap,
280 fast_ring_offset, 1);
281#endif
Dave Barachb7f1faa2017-08-29 11:43:37 -0400282#if TW_START_STOP_TRACE_SIZE > 0
Florin Corasadb5bd52018-06-22 15:29:38 -0700283 TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400284#endif
Florin Corasadb5bd52018-06-22 15:29:38 -0700285}
286
287/**
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
294 */
295u32
296TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 user_id, u32 timer_id,
297 u64 interval)
298{
299 TWT (tw_timer) * t;
300
301 ASSERT (interval);
302
303 pool_get (tw->timers, t);
304 memset (t, 0xff, sizeof (*t));
305
306 t->user_handle = TW (make_internal_timer_handle) (user_id, timer_id);
307
308 timer_add (tw, t, interval);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500309 return t - tw->timers;
310}
311
Dave Barach4af9ba12017-06-07 15:18:23 -0400312#if TW_TIMER_SCAN_FOR_HANDLE > 0
313int TW (scan_for_handle) (TWT (tw_timer_wheel) * tw, u32 handle)
314{
315 int i, j;
316 tw_timer_wheel_slot_t *ts;
317 TWT (tw_timer) * t, *head;
318 u32 next_index;
319 int rv = 0;
320
321 for (i = 0; i < TW_TIMER_WHEELS; i++)
322 {
323 for (j = 0; j < TW_SLOTS_PER_RING; j++)
324 {
325 ts = &tw->w[i][j];
326 head = pool_elt_at_index (tw->timers, ts->head_index);
327 next_index = head->next;
328
329 while (next_index != ts->head_index)
330 {
331 t = pool_elt_at_index (tw->timers, next_index);
332 if (next_index == handle)
333 {
334 clib_warning ("handle %d found in ring %d slot %d",
335 handle, i, j);
336 clib_warning ("user handle 0x%x", t->user_handle);
337 rv = 1;
338 }
339 next_index = t->next;
340 }
341 }
342 }
343 return rv;
344}
345#endif /* TW_TIMER_SCAN_FOR_HANDLE */
346
Dave Barach8e8f98c2017-02-03 11:58:53 -0500347/**
348 * @brief Stop a tw timer
349 * @param tw_timer_wheel_t * tw timer wheel object pointer
Dave Barach8e8f98c2017-02-03 11:58:53 -0500350 * @param u32 handle timer cancellation returned by tw_timer_start
351 */
Dave Barach8e8f98c2017-02-03 11:58:53 -0500352void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
353{
354 TWT (tw_timer) * t;
355
Dave Barach5c20a012017-06-13 08:48:31 -0400356#if TW_TIMER_ALLOW_DUPLICATE_STOP
357 /*
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.
361 */
362 if (pool_is_free_index (tw->timers, handle))
363 return;
364#endif
Dave Barachb7f1faa2017-08-29 11:43:37 -0400365#if TW_START_STOP_TRACE_SIZE > 0
366 TW (tw_timer_trace) (tw, ~0, ~0, handle);
367#endif
Dave Barach5c20a012017-06-13 08:48:31 -0400368
Dave Barach8e8f98c2017-02-03 11:58:53 -0500369 t = pool_elt_at_index (tw->timers, handle);
370
371 /* in case of idiotic handle (e.g. passing a listhead index) */
372 ASSERT (t->user_handle != ~0);
373
Florin Corasadb5bd52018-06-22 15:29:38 -0700374 timer_remove (tw->timers, t);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500375
376 pool_put_index (tw->timers, handle);
377}
378
379/**
Florin Corasadb5bd52018-06-22 15:29:38 -0700380 * @brief Update a tw timer
381 * @param tw_timer_wheel_t * tw timer wheel object pointer
382 * @param u32 handle timer returned by tw_timer_start
383 * @param u32 interval timer interval in ticks
384 */
385void TW (tw_timer_update) (TWT (tw_timer_wheel) * tw, u32 handle,
386 u64 interval)
387{
388 TWT (tw_timer) * t;
389 t = pool_elt_at_index (tw->timers, handle);
390 timer_remove (tw->timers, t);
391 timer_add (tw, t, interval);
392}
393
394/**
Dave Barach8e8f98c2017-02-03 11:58:53 -0500395 * @brief Initialize a tw timer wheel template instance
396 * @param tw_timer_wheel_t * tw timer wheel object pointer
397 * @param void * expired_timer_callback. Passed a u32 * vector of
Dave Barach4af9ba12017-06-07 15:18:23 -0400398 * expired timer handles. The callback is optional.
Dave Barach8e8f98c2017-02-03 11:58:53 -0500399 * @param f64 timer_interval_in_seconds
400 */
401void
402TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
403 void *expired_timer_callback,
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100404 f64 timer_interval_in_seconds, u32 max_expirations)
Dave Barach8e8f98c2017-02-03 11:58:53 -0500405{
406 int ring, slot;
407 tw_timer_wheel_slot_t *ts;
408 TWT (tw_timer) * t;
409 memset (tw, 0, sizeof (*tw));
410 tw->expired_timer_callback = expired_timer_callback;
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100411 tw->max_expirations = max_expirations;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500412 if (timer_interval_in_seconds == 0.0)
413 {
414 clib_warning ("timer interval is zero");
415 abort ();
416 }
417 tw->timer_interval = timer_interval_in_seconds;
418 tw->ticks_per_second = 1.0 / timer_interval_in_seconds;
Dave Barach4af9ba12017-06-07 15:18:23 -0400419 tw->first_expires_tick = ~0ULL;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400420
Dave Barach4af9ba12017-06-07 15:18:23 -0400421 vec_validate (tw->expired_timer_handles, 0);
422 _vec_len (tw->expired_timer_handles) = 0;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500423
424 for (ring = 0; ring < TW_TIMER_WHEELS; ring++)
425 {
426 for (slot = 0; slot < TW_SLOTS_PER_RING; slot++)
427 {
428 ts = &tw->w[ring][slot];
429 pool_get (tw->timers, t);
430 memset (t, 0xff, sizeof (*t));
431 t->next = t->prev = t - tw->timers;
432 ts->head_index = t - tw->timers;
433 }
434 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400435
436#if TW_OVERFLOW_VECTOR > 0
437 ts = &tw->overflow;
438 pool_get (tw->timers, t);
439 memset (t, 0xff, sizeof (*t));
440 t->next = t->prev = t - tw->timers;
441 ts->head_index = t - tw->timers;
442#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500443}
444
445/**
446 * @brief Free a tw timer wheel template instance
447 * @param tw_timer_wheel_t * tw timer wheel object pointer
448 */
449void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
450{
451 int i, j;
452 tw_timer_wheel_slot_t *ts;
453 TWT (tw_timer) * head, *t;
454 u32 next_index;
455
456 for (i = 0; i < TW_TIMER_WHEELS; i++)
457 {
458 for (j = 0; j < TW_SLOTS_PER_RING; j++)
459 {
460 ts = &tw->w[i][j];
461 head = pool_elt_at_index (tw->timers, ts->head_index);
462 next_index = head->next;
463
464 while (next_index != ts->head_index)
465 {
466 t = pool_elt_at_index (tw->timers, next_index);
467 next_index = t->next;
468 pool_put (tw->timers, t);
469 }
470 pool_put (tw->timers, head);
471 }
472 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400473
474#if TW_OVERFLOW_VECVOR > 0
475 ts = &tw->overflow;
476 head = pool_elt_at_index (tw->timers, ts->head_index);
477 next_index = head->next;
478
479 while (next_index != ts->head_index)
480 {
481 t = pool_elt_at_index (tw->timers, next_index);
482 next_index = t->next;
483 pool_put (tw->timers, t);
484 }
485 pool_put (tw->timers, head);
486#endif
487
Dave Barach8e8f98c2017-02-03 11:58:53 -0500488 memset (tw, 0, sizeof (*tw));
489}
490
491/**
492 * @brief Advance a tw timer wheel. Calls the expired timer callback
493 * as needed. This routine should be called once every timer_interval seconds
494 * @param tw_timer_wheel_t * tw timer wheel template instance pointer
495 * @param f64 now the current time, e.g. from vlib_time_now(vm)
Dave Barach4af9ba12017-06-07 15:18:23 -0400496 * @returns u32 * vector of expired user handles
Dave Barach8e8f98c2017-02-03 11:58:53 -0500497 */
Dave Barach4af9ba12017-06-07 15:18:23 -0400498static inline
499 u32 * TW (tw_timer_expire_timers_internal) (TWT (tw_timer_wheel) * tw,
500 f64 now,
501 u32 * callback_vector_arg)
Dave Barach8e8f98c2017-02-03 11:58:53 -0500502{
503 u32 nticks, i;
504 tw_timer_wheel_slot_t *ts;
505 TWT (tw_timer) * t, *head;
Dave Barach4af9ba12017-06-07 15:18:23 -0400506 u32 *callback_vector;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500507 u32 fast_wheel_index;
508 u32 next_index;
Dave Barach4af9ba12017-06-07 15:18:23 -0400509 u32 slow_wheel_index __attribute__ ((unused));
510 u32 glacier_wheel_index __attribute__ ((unused));
Dave Barach8e8f98c2017-02-03 11:58:53 -0500511
512 /* Shouldn't happen */
513 if (PREDICT_FALSE (now < tw->next_run_time))
Dave Barach4af9ba12017-06-07 15:18:23 -0400514 return callback_vector_arg;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500515
516 /* Number of ticks which have occurred */
517 nticks = tw->ticks_per_second * (now - tw->last_run_time);
518 if (nticks == 0)
Dave Barach4af9ba12017-06-07 15:18:23 -0400519 return callback_vector_arg;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500520
521 /* Remember when we ran, compute next runtime */
522 tw->next_run_time = (now + tw->timer_interval);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500523
Dave Barach4af9ba12017-06-07 15:18:23 -0400524 if (callback_vector_arg == 0)
525 {
526 _vec_len (tw->expired_timer_handles) = 0;
527 callback_vector = tw->expired_timer_handles;
528 }
529 else
530 callback_vector = callback_vector_arg;
531
Dave Barach8e8f98c2017-02-03 11:58:53 -0500532 for (i = 0; i < nticks; i++)
533 {
534 fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST];
Dave Barach4af9ba12017-06-07 15:18:23 -0400535 if (TW_TIMER_WHEELS > 1)
536 slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW];
537 if (TW_TIMER_WHEELS > 2)
538 glacier_wheel_index = tw->current_index[TW_TIMER_RING_GLACIER];
Dave Barach8e8f98c2017-02-03 11:58:53 -0500539
Dave Barach4af9ba12017-06-07 15:18:23 -0400540#if TW_OVERFLOW_VECTOR > 0
541 /* Triple odometer-click? Process the overflow vector... */
542 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
543 && slow_wheel_index == TW_SLOTS_PER_RING
544 && glacier_wheel_index == TW_SLOTS_PER_RING))
545 {
546 u64 interval;
547 u32 new_glacier_ring_offset, new_slow_ring_offset;
548 u32 new_fast_ring_offset;
549
550 ts = &tw->overflow;
551 head = pool_elt_at_index (tw->timers, ts->head_index);
552 next_index = head->next;
553
554 /* Make slot empty */
555 head->next = head->prev = ts->head_index;
556
557 /* traverse slot, place timers wherever they go */
558 while (next_index != head - tw->timers)
559 {
560 t = pool_elt_at_index (tw->timers, next_index);
561 next_index = t->next;
562
563 /* Remove from the overflow vector (hammer) */
564 t->next = t->prev = ~0;
565
566 ASSERT (t->expiration_time >= tw->current_tick);
567
568 interval = t->expiration_time - tw->current_tick;
569
570 /* Right back onto the overflow vector? */
571 if (interval >= (1 << (3 * TW_RING_SHIFT)))
572 {
573 ts = &tw->overflow;
574 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
575 continue;
576 }
577 /* Compute ring offsets */
578 new_glacier_ring_offset = interval >> (2 * TW_RING_SHIFT);
579
580 interval -= (new_glacier_ring_offset << (2 * TW_RING_SHIFT));
581
582 /* Note: the wheels are at (0,0,0), no add-with-carry needed */
583 new_slow_ring_offset = interval >> TW_RING_SHIFT;
584 interval -= (new_slow_ring_offset << TW_RING_SHIFT);
585 new_fast_ring_offset = interval & TW_RING_MASK;
586 t->slow_ring_offset = new_slow_ring_offset;
587 t->fast_ring_offset = new_fast_ring_offset;
588
589 /* Timer expires Right Now */
590 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
591 t->fast_ring_offset == 0 &&
592 new_glacier_ring_offset == 0))
593 {
594 vec_add1 (callback_vector, t->user_handle);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400595#if TW_START_STOP_TRACE_SIZE > 0
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400596 TW (tw_timer_trace) (tw, 0xfe, t->user_handle,
597 t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400598#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400599 pool_put (tw->timers, t);
600 }
601 /* Timer moves to the glacier ring */
602 else if (new_glacier_ring_offset)
603 {
604 ts = &tw->w[TW_TIMER_RING_GLACIER][new_glacier_ring_offset];
605 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
606 }
607 /* Timer moves to the slow ring */
608 else if (t->slow_ring_offset)
609 {
610 /* Add to slow ring */
611 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
612 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
613 }
614 /* Timer timer moves to the fast ring */
615 else
616 {
617 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
618 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400619#if TW_FAST_WHEEL_BITMAP
620 tw->fast_slot_bitmap =
621 clib_bitmap_set (tw->fast_slot_bitmap,
622 t->fast_ring_offset, 1);
623#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400624 }
625 }
626 }
627#endif
628
629#if TW_TIMER_WHEELS > 2
Dave Barach8e8f98c2017-02-03 11:58:53 -0500630 /*
Dave Barach4af9ba12017-06-07 15:18:23 -0400631 * Double odometer-click? Process one slot in the glacier ring...
632 */
633 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING
634 && slow_wheel_index == TW_SLOTS_PER_RING))
635 {
636 glacier_wheel_index %= TW_SLOTS_PER_RING;
637 ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_wheel_index];
638
639 head = pool_elt_at_index (tw->timers, ts->head_index);
640 next_index = head->next;
641
642 /* Make slot empty */
643 head->next = head->prev = ts->head_index;
644
645 /* traverse slot, deal timers into slow ring */
646 while (next_index != head - tw->timers)
647 {
648 t = pool_elt_at_index (tw->timers, next_index);
649 next_index = t->next;
650
651 /* Remove from glacier ring slot (hammer) */
652 t->next = t->prev = ~0;
653
654 /* Timer expires Right Now */
655 if (PREDICT_FALSE (t->slow_ring_offset == 0 &&
656 t->fast_ring_offset == 0))
657 {
658 vec_add1 (callback_vector, t->user_handle);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400659#if TW_START_STOP_TRACE_SIZE > 0
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400660 TW (tw_timer_trace) (tw, 0xfe, t->user_handle,
661 t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400662#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400663 pool_put (tw->timers, t);
664 }
665 /* Timer expires during slow-wheel tick 0 */
666 else if (PREDICT_FALSE (t->slow_ring_offset == 0))
667 {
668 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
669 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400670#if TW_FAST_WHEEL_BITMAP
671 tw->fast_slot_bitmap =
672 clib_bitmap_set (tw->fast_slot_bitmap,
673 t->fast_ring_offset, 1);
674#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400675 }
676 else /* typical case */
677 {
678 /* Add to slow ring */
679 ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset];
680 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
681 }
682 }
683 }
684#endif
685
686#if TW_TIMER_WHEELS > 1
687 /*
688 * Single odometer-click? Process a slot in the slow ring,
Dave Barach8e8f98c2017-02-03 11:58:53 -0500689 */
690 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
691 {
Dave Barach4af9ba12017-06-07 15:18:23 -0400692 slow_wheel_index %= TW_SLOTS_PER_RING;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500693 ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index];
694
695 head = pool_elt_at_index (tw->timers, ts->head_index);
696 next_index = head->next;
697
698 /* Make slot empty */
699 head->next = head->prev = ts->head_index;
700
701 /* traverse slot, deal timers into fast ring */
702 while (next_index != head - tw->timers)
703 {
704 t = pool_elt_at_index (tw->timers, next_index);
705 next_index = t->next;
706
Dave Barach4af9ba12017-06-07 15:18:23 -0400707 /* Remove from sloe ring slot (hammer) */
Dave Barach8e8f98c2017-02-03 11:58:53 -0500708 t->next = t->prev = ~0;
Dave Barach4af9ba12017-06-07 15:18:23 -0400709
710 /* Timer expires Right Now */
711 if (PREDICT_FALSE (t->fast_ring_offset == 0))
712 {
713 vec_add1 (callback_vector, t->user_handle);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400714#if TW_START_STOP_TRACE_SIZE > 0
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400715 TW (tw_timer_trace) (tw, 0xfe, t->user_handle,
716 t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400717#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400718 pool_put (tw->timers, t);
719 }
720 else /* typical case */
721 {
722 /* Add to fast ring */
723 ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
724 timer_addhead (tw->timers, ts->head_index, t - tw->timers);
Dave Barach5c20a012017-06-13 08:48:31 -0400725#if TW_FAST_WHEEL_BITMAP
726 tw->fast_slot_bitmap =
727 clib_bitmap_set (tw->fast_slot_bitmap,
728 t->fast_ring_offset, 1);
729#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400730 }
Dave Barach8e8f98c2017-02-03 11:58:53 -0500731 }
Dave Barach8e8f98c2017-02-03 11:58:53 -0500732 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400733#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500734
735 /* Handle the fast ring */
Dave Barach4af9ba12017-06-07 15:18:23 -0400736 fast_wheel_index %= TW_SLOTS_PER_RING;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500737 ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index];
738
739 head = pool_elt_at_index (tw->timers, ts->head_index);
740 next_index = head->next;
741
742 /* Make slot empty */
743 head->next = head->prev = ts->head_index;
744
745 /* Construct vector of expired timer handles to give the user */
746 while (next_index != ts->head_index)
747 {
748 t = pool_elt_at_index (tw->timers, next_index);
749 next_index = t->next;
Dave Barach4af9ba12017-06-07 15:18:23 -0400750 vec_add1 (callback_vector, t->user_handle);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400751#if TW_START_STOP_TRACE_SIZE > 0
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400752 TW (tw_timer_trace) (tw, 0xfe, t->user_handle, t - tw->timers);
Dave Barachb7f1faa2017-08-29 11:43:37 -0400753#endif
Dave Barach8e8f98c2017-02-03 11:58:53 -0500754 pool_put (tw->timers, t);
755 }
756
757 /* If any timers expired, tell the user */
Dave Barach4af9ba12017-06-07 15:18:23 -0400758 if (callback_vector_arg == 0 && vec_len (callback_vector))
Gabriel Ganne581b0722017-02-13 10:27:15 +0100759 {
Dave Barach4af9ba12017-06-07 15:18:23 -0400760 /* The callback is optional. We return the u32 * handle vector */
761 if (tw->expired_timer_callback)
Dave Barachad099332018-02-20 08:34:48 -0500762 {
763 tw->expired_timer_callback (callback_vector);
764 vec_reset_length (callback_vector);
765 }
Dave Barach4af9ba12017-06-07 15:18:23 -0400766 tw->expired_timer_handles = callback_vector;
Gabriel Ganne581b0722017-02-13 10:27:15 +0100767 }
Dave Barach5c20a012017-06-13 08:48:31 -0400768
769#if TW_FAST_WHEEL_BITMAP
770 tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap,
771 fast_wheel_index, 0);
772#endif
773
Dave Barach8e8f98c2017-02-03 11:58:53 -0500774 tw->current_tick++;
Dave Barach4af9ba12017-06-07 15:18:23 -0400775 fast_wheel_index++;
776 tw->current_index[TW_TIMER_RING_FAST] = fast_wheel_index;
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100777
Dave Barach4af9ba12017-06-07 15:18:23 -0400778#if TW_TIMER_WHEELS > 1
779 if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
780 slow_wheel_index++;
781 tw->current_index[TW_TIMER_RING_SLOW] = slow_wheel_index;
782#endif
783
784#if TW_TIMER_WHEELS > 2
785 if (PREDICT_FALSE (slow_wheel_index == TW_SLOTS_PER_RING))
786 glacier_wheel_index++;
787 tw->current_index[TW_TIMER_RING_GLACIER] = glacier_wheel_index;
788#endif
789
790 if (vec_len (callback_vector) >= tw->max_expirations)
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100791 break;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500792 }
Gabriel Ganne581b0722017-02-13 10:27:15 +0100793
Dave Barach4af9ba12017-06-07 15:18:23 -0400794 if (callback_vector_arg == 0)
795 tw->expired_timer_handles = callback_vector;
796
Florin Coras954898f2017-02-21 19:26:51 -0800797 tw->last_run_time += i * tw->timer_interval;
Dave Barach4af9ba12017-06-07 15:18:23 -0400798 return callback_vector;
799}
800
801u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now)
802{
803 return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ );
804}
805
806u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now,
807 u32 * vec)
808{
809 return TW (tw_timer_expire_timers_internal) (tw, now, vec);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500810}
811
Dave Barach5c20a012017-06-13 08:48:31 -0400812#if TW_FAST_WHEEL_BITMAP
813/** Returns an approximation to the first timer expiration in
814 * timer-ticks from "now". To avoid wasting an unjustifiable
815 * amount of time on the problem, we maintain an approximate fast-wheel slot
816 * occupancy bitmap. We don't worry about clearing fast wheel bits
817 * when timers are removed from fast wheel slots.
818 */
819
820u32 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw)
821{
822 u32 first_expiring_index, fast_ring_index;
823 i32 delta;
824
825 if (clib_bitmap_is_zero (tw->fast_slot_bitmap))
826 return TW_SLOTS_PER_RING;
827
828 fast_ring_index = tw->current_index[TW_TIMER_RING_FAST];
829 if (fast_ring_index == TW_SLOTS_PER_RING)
830 fast_ring_index = 0;
831
832 first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap,
833 fast_ring_index);
834 if (first_expiring_index == ~0 && fast_ring_index != 0)
835 first_expiring_index = clib_bitmap_first_set (tw->fast_slot_bitmap);
836
837 ASSERT (first_expiring_index != ~0);
838
839 delta = (i32) first_expiring_index - (i32) fast_ring_index;
840 if (delta < 0)
841 delta += TW_SLOTS_PER_RING;
842
843 ASSERT (delta >= 0);
844
845 return (u32) delta;
846}
847
848#endif
849
Dave Barach8e8f98c2017-02-03 11:58:53 -0500850/*
851 * fd.io coding-style-patch-verification: ON
852 *
853 * Local Variables:
854 * eval: (c-set-style "gnu")
855 * End:
856 */