Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 1 | /* |
| 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 Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 21 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 22 | |
| 23 | void 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 | |
| 40 | void 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 Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 102 | static inline u32 |
| 103 | TW (make_internal_timer_handle) (u32 pool_index, u32 timer_id) |
| 104 | { |
| 105 | u32 handle; |
| 106 | |
| 107 | ASSERT (timer_id < TW_TIMERS_PER_OBJECT); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 108 | #if LOG2_TW_TIMERS_PER_OBJECT > 0 |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 109 | ASSERT (pool_index < (1 << (32 - LOG2_TW_TIMERS_PER_OBJECT))); |
| 110 | |
| 111 | handle = (timer_id << (32 - LOG2_TW_TIMERS_PER_OBJECT)) | (pool_index); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 112 | #else |
| 113 | handle = pool_index; |
| 114 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 115 | return handle; |
| 116 | } |
| 117 | |
| 118 | static inline void |
| 119 | timer_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 | |
| 144 | static inline void |
| 145 | timer_remove (TWT (tw_timer) * pool, u32 index) |
| 146 | { |
| 147 | TWT (tw_timer) * elt = pool_elt_at_index (pool, index); |
| 148 | TWT (tw_timer) * next_elt, *prev_elt; |
| 149 | |
| 150 | ASSERT (elt->user_handle != ~0); |
| 151 | |
| 152 | next_elt = pool_elt_at_index (pool, elt->next); |
| 153 | prev_elt = pool_elt_at_index (pool, elt->prev); |
| 154 | |
| 155 | next_elt->prev = elt->prev; |
| 156 | prev_elt->next = elt->next; |
| 157 | |
| 158 | elt->prev = elt->next = ~0; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @brief Start a Tw Timer |
| 163 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
| 164 | * @param u32 pool_index user pool index, presumably for a tw session |
| 165 | * @param u32 timer_id app-specific timer ID. 4 bits. |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 166 | * @param u64 interval timer interval in ticks |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 167 | * @returns handle needed to cancel the timer |
| 168 | */ |
| 169 | u32 |
| 170 | TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id, |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 171 | u64 interval) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 172 | { |
| 173 | #if TW_TIMER_WHEELS > 1 |
| 174 | u16 slow_ring_offset; |
| 175 | u32 carry; |
| 176 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 177 | #if TW_TIMER_WHEELS > 2 |
| 178 | u16 glacier_ring_offset; |
| 179 | #endif |
| 180 | #if TW_OVERFLOW_VECTOR > 0 |
| 181 | u64 interval_plus_time_to_wrap, triple_wrap_mask; |
| 182 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 183 | u16 fast_ring_offset; |
| 184 | tw_timer_wheel_slot_t *ts; |
| 185 | TWT (tw_timer) * t; |
| 186 | |
| 187 | ASSERT (interval); |
| 188 | |
| 189 | pool_get (tw->timers, t); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 190 | memset (t, 0xff, sizeof (*t)); |
| 191 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 192 | t->user_handle = TW (make_internal_timer_handle) (pool_index, timer_id); |
| 193 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 194 | /* Factor interval into 1..3 wheel offsets */ |
| 195 | #if TW_TIMER_WHEELS > 2 |
| 196 | #if TW_OVERFLOW_VECTOR > 0 |
| 197 | /* |
| 198 | * This is tricky. Put a timer onto the overflow |
| 199 | * vector if the interval PLUS the time |
| 200 | * until the next triple-wrap exceeds one full revolution |
| 201 | * of all three wheels. |
| 202 | */ |
| 203 | triple_wrap_mask = (1 << (3 * TW_RING_SHIFT)) - 1; |
| 204 | interval_plus_time_to_wrap = |
| 205 | interval + (tw->current_tick & triple_wrap_mask); |
| 206 | if ((interval_plus_time_to_wrap >= 1 << (3 * TW_RING_SHIFT))) |
| 207 | { |
| 208 | t->expiration_time = tw->current_tick + interval; |
| 209 | ts = &tw->overflow; |
| 210 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 211 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 212 | TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers); |
| 213 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 214 | return t - tw->timers; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | glacier_ring_offset = interval >> (2 * TW_RING_SHIFT); |
Dave Barach | 28b70af | 2017-06-13 17:04:28 -0400 | [diff] [blame] | 219 | ASSERT ((u64) glacier_ring_offset < TW_SLOTS_PER_RING); |
| 220 | interval -= (((u64) glacier_ring_offset) << (2 * TW_RING_SHIFT)); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 221 | #endif |
| 222 | #if TW_TIMER_WHEELS > 1 |
| 223 | slow_ring_offset = interval >> TW_RING_SHIFT; |
Dave Barach | 28b70af | 2017-06-13 17:04:28 -0400 | [diff] [blame] | 224 | ASSERT ((u64) slow_ring_offset < TW_SLOTS_PER_RING); |
| 225 | interval -= (((u64) slow_ring_offset) << TW_RING_SHIFT); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 226 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 227 | fast_ring_offset = interval & TW_RING_MASK; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Account for the current wheel positions(s) |
| 231 | * This is made slightly complicated by the fact that the current |
| 232 | * index vector will contain (TW_SLOTS_PER_RING, ...) when |
| 233 | * the actual position is (0, ...) |
| 234 | */ |
| 235 | |
| 236 | fast_ring_offset += tw->current_index[TW_TIMER_RING_FAST] & TW_RING_MASK; |
| 237 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 238 | #if TW_TIMER_WHEELS > 1 |
| 239 | carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0; |
| 240 | fast_ring_offset %= TW_SLOTS_PER_RING; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 241 | slow_ring_offset += (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK) |
| 242 | + carry; |
| 243 | carry = slow_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0; |
| 244 | slow_ring_offset %= TW_SLOTS_PER_RING; |
| 245 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 246 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 247 | #if TW_TIMER_WHEELS > 2 |
| 248 | glacier_ring_offset += |
| 249 | (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK) + carry; |
| 250 | glacier_ring_offset %= TW_SLOTS_PER_RING; |
| 251 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 252 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 253 | #if TW_TIMER_WHEELS > 2 |
| 254 | if (glacier_ring_offset != |
| 255 | (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK)) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 256 | { |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 257 | /* We'll need slow and fast ring offsets later */ |
| 258 | t->slow_ring_offset = slow_ring_offset; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 259 | t->fast_ring_offset = fast_ring_offset; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 260 | |
| 261 | ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_ring_offset]; |
| 262 | |
| 263 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 264 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 265 | TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers); |
| 266 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 267 | return t - tw->timers; |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | #if TW_TIMER_WHEELS > 1 |
| 272 | /* Timer expires more than 51.2 seconds from now? */ |
| 273 | if (slow_ring_offset != |
| 274 | (tw->current_index[TW_TIMER_RING_SLOW] & TW_RING_MASK)) |
| 275 | { |
| 276 | /* We'll need the fast ring offset later... */ |
| 277 | t->fast_ring_offset = fast_ring_offset; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 278 | |
| 279 | ts = &tw->w[TW_TIMER_RING_SLOW][slow_ring_offset]; |
| 280 | |
| 281 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 282 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 283 | TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers); |
| 284 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 285 | return t - tw->timers; |
| 286 | } |
| 287 | #else |
| 288 | fast_ring_offset %= TW_SLOTS_PER_RING; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 289 | #endif |
| 290 | |
| 291 | /* Timer expires less than one fast-ring revolution from now */ |
| 292 | ts = &tw->w[TW_TIMER_RING_FAST][fast_ring_offset]; |
| 293 | |
| 294 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 295 | |
| 296 | #if TW_FAST_WHEEL_BITMAP |
| 297 | tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap, |
| 298 | fast_ring_offset, 1); |
| 299 | #endif |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 300 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 301 | TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers); |
| 302 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 303 | return t - tw->timers; |
| 304 | } |
| 305 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 306 | #if TW_TIMER_SCAN_FOR_HANDLE > 0 |
| 307 | int TW (scan_for_handle) (TWT (tw_timer_wheel) * tw, u32 handle) |
| 308 | { |
| 309 | int i, j; |
| 310 | tw_timer_wheel_slot_t *ts; |
| 311 | TWT (tw_timer) * t, *head; |
| 312 | u32 next_index; |
| 313 | int rv = 0; |
| 314 | |
| 315 | for (i = 0; i < TW_TIMER_WHEELS; i++) |
| 316 | { |
| 317 | for (j = 0; j < TW_SLOTS_PER_RING; j++) |
| 318 | { |
| 319 | ts = &tw->w[i][j]; |
| 320 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 321 | next_index = head->next; |
| 322 | |
| 323 | while (next_index != ts->head_index) |
| 324 | { |
| 325 | t = pool_elt_at_index (tw->timers, next_index); |
| 326 | if (next_index == handle) |
| 327 | { |
| 328 | clib_warning ("handle %d found in ring %d slot %d", |
| 329 | handle, i, j); |
| 330 | clib_warning ("user handle 0x%x", t->user_handle); |
| 331 | rv = 1; |
| 332 | } |
| 333 | next_index = t->next; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | return rv; |
| 338 | } |
| 339 | #endif /* TW_TIMER_SCAN_FOR_HANDLE */ |
| 340 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 341 | /** |
| 342 | * @brief Stop a tw timer |
| 343 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 344 | * @param u32 handle timer cancellation returned by tw_timer_start |
| 345 | */ |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 346 | void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle) |
| 347 | { |
| 348 | TWT (tw_timer) * t; |
| 349 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 350 | #if TW_TIMER_ALLOW_DUPLICATE_STOP |
| 351 | /* |
| 352 | * A vlib process may have its timer expire, and receive |
| 353 | * an event before the expiration is processed. |
| 354 | * That results in a duplicate tw_timer_stop. |
| 355 | */ |
| 356 | if (pool_is_free_index (tw->timers, handle)) |
| 357 | return; |
| 358 | #endif |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 359 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 360 | TW (tw_timer_trace) (tw, ~0, ~0, handle); |
| 361 | #endif |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 362 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 363 | t = pool_elt_at_index (tw->timers, handle); |
| 364 | |
| 365 | /* in case of idiotic handle (e.g. passing a listhead index) */ |
| 366 | ASSERT (t->user_handle != ~0); |
| 367 | |
| 368 | timer_remove (tw->timers, handle); |
| 369 | |
| 370 | pool_put_index (tw->timers, handle); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @brief Initialize a tw timer wheel template instance |
| 375 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
| 376 | * @param void * expired_timer_callback. Passed a u32 * vector of |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 377 | * expired timer handles. The callback is optional. |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 378 | * @param f64 timer_interval_in_seconds |
| 379 | */ |
| 380 | void |
| 381 | TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw, |
| 382 | void *expired_timer_callback, |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 383 | f64 timer_interval_in_seconds, u32 max_expirations) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 384 | { |
| 385 | int ring, slot; |
| 386 | tw_timer_wheel_slot_t *ts; |
| 387 | TWT (tw_timer) * t; |
| 388 | memset (tw, 0, sizeof (*tw)); |
| 389 | tw->expired_timer_callback = expired_timer_callback; |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 390 | tw->max_expirations = max_expirations; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 391 | if (timer_interval_in_seconds == 0.0) |
| 392 | { |
| 393 | clib_warning ("timer interval is zero"); |
| 394 | abort (); |
| 395 | } |
| 396 | tw->timer_interval = timer_interval_in_seconds; |
| 397 | tw->ticks_per_second = 1.0 / timer_interval_in_seconds; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 398 | tw->first_expires_tick = ~0ULL; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 399 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 400 | vec_validate (tw->expired_timer_handles, 0); |
| 401 | _vec_len (tw->expired_timer_handles) = 0; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 402 | |
| 403 | for (ring = 0; ring < TW_TIMER_WHEELS; ring++) |
| 404 | { |
| 405 | for (slot = 0; slot < TW_SLOTS_PER_RING; slot++) |
| 406 | { |
| 407 | ts = &tw->w[ring][slot]; |
| 408 | pool_get (tw->timers, t); |
| 409 | memset (t, 0xff, sizeof (*t)); |
| 410 | t->next = t->prev = t - tw->timers; |
| 411 | ts->head_index = t - tw->timers; |
| 412 | } |
| 413 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 414 | |
| 415 | #if TW_OVERFLOW_VECTOR > 0 |
| 416 | ts = &tw->overflow; |
| 417 | pool_get (tw->timers, t); |
| 418 | memset (t, 0xff, sizeof (*t)); |
| 419 | t->next = t->prev = t - tw->timers; |
| 420 | ts->head_index = t - tw->timers; |
| 421 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
| 425 | * @brief Free a tw timer wheel template instance |
| 426 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
| 427 | */ |
| 428 | void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw) |
| 429 | { |
| 430 | int i, j; |
| 431 | tw_timer_wheel_slot_t *ts; |
| 432 | TWT (tw_timer) * head, *t; |
| 433 | u32 next_index; |
| 434 | |
| 435 | for (i = 0; i < TW_TIMER_WHEELS; i++) |
| 436 | { |
| 437 | for (j = 0; j < TW_SLOTS_PER_RING; j++) |
| 438 | { |
| 439 | ts = &tw->w[i][j]; |
| 440 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 441 | next_index = head->next; |
| 442 | |
| 443 | while (next_index != ts->head_index) |
| 444 | { |
| 445 | t = pool_elt_at_index (tw->timers, next_index); |
| 446 | next_index = t->next; |
| 447 | pool_put (tw->timers, t); |
| 448 | } |
| 449 | pool_put (tw->timers, head); |
| 450 | } |
| 451 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 452 | |
| 453 | #if TW_OVERFLOW_VECVOR > 0 |
| 454 | ts = &tw->overflow; |
| 455 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 456 | next_index = head->next; |
| 457 | |
| 458 | while (next_index != ts->head_index) |
| 459 | { |
| 460 | t = pool_elt_at_index (tw->timers, next_index); |
| 461 | next_index = t->next; |
| 462 | pool_put (tw->timers, t); |
| 463 | } |
| 464 | pool_put (tw->timers, head); |
| 465 | #endif |
| 466 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 467 | memset (tw, 0, sizeof (*tw)); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * @brief Advance a tw timer wheel. Calls the expired timer callback |
| 472 | * as needed. This routine should be called once every timer_interval seconds |
| 473 | * @param tw_timer_wheel_t * tw timer wheel template instance pointer |
| 474 | * @param f64 now the current time, e.g. from vlib_time_now(vm) |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 475 | * @returns u32 * vector of expired user handles |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 476 | */ |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 477 | static inline |
| 478 | u32 * TW (tw_timer_expire_timers_internal) (TWT (tw_timer_wheel) * tw, |
| 479 | f64 now, |
| 480 | u32 * callback_vector_arg) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 481 | { |
| 482 | u32 nticks, i; |
| 483 | tw_timer_wheel_slot_t *ts; |
| 484 | TWT (tw_timer) * t, *head; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 485 | u32 *callback_vector; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 486 | u32 fast_wheel_index; |
| 487 | u32 next_index; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 488 | u32 slow_wheel_index __attribute__ ((unused)); |
| 489 | u32 glacier_wheel_index __attribute__ ((unused)); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 490 | |
| 491 | /* Shouldn't happen */ |
| 492 | if (PREDICT_FALSE (now < tw->next_run_time)) |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 493 | return callback_vector_arg; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 494 | |
| 495 | /* Number of ticks which have occurred */ |
| 496 | nticks = tw->ticks_per_second * (now - tw->last_run_time); |
| 497 | if (nticks == 0) |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 498 | return callback_vector_arg; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 499 | |
| 500 | /* Remember when we ran, compute next runtime */ |
| 501 | tw->next_run_time = (now + tw->timer_interval); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 502 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 503 | if (callback_vector_arg == 0) |
| 504 | { |
| 505 | _vec_len (tw->expired_timer_handles) = 0; |
| 506 | callback_vector = tw->expired_timer_handles; |
| 507 | } |
| 508 | else |
| 509 | callback_vector = callback_vector_arg; |
| 510 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 511 | for (i = 0; i < nticks; i++) |
| 512 | { |
| 513 | fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST]; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 514 | if (TW_TIMER_WHEELS > 1) |
| 515 | slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW]; |
| 516 | if (TW_TIMER_WHEELS > 2) |
| 517 | glacier_wheel_index = tw->current_index[TW_TIMER_RING_GLACIER]; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 518 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 519 | #if TW_OVERFLOW_VECTOR > 0 |
| 520 | /* Triple odometer-click? Process the overflow vector... */ |
| 521 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING |
| 522 | && slow_wheel_index == TW_SLOTS_PER_RING |
| 523 | && glacier_wheel_index == TW_SLOTS_PER_RING)) |
| 524 | { |
| 525 | u64 interval; |
| 526 | u32 new_glacier_ring_offset, new_slow_ring_offset; |
| 527 | u32 new_fast_ring_offset; |
| 528 | |
| 529 | ts = &tw->overflow; |
| 530 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 531 | next_index = head->next; |
| 532 | |
| 533 | /* Make slot empty */ |
| 534 | head->next = head->prev = ts->head_index; |
| 535 | |
| 536 | /* traverse slot, place timers wherever they go */ |
| 537 | while (next_index != head - tw->timers) |
| 538 | { |
| 539 | t = pool_elt_at_index (tw->timers, next_index); |
| 540 | next_index = t->next; |
| 541 | |
| 542 | /* Remove from the overflow vector (hammer) */ |
| 543 | t->next = t->prev = ~0; |
| 544 | |
| 545 | ASSERT (t->expiration_time >= tw->current_tick); |
| 546 | |
| 547 | interval = t->expiration_time - tw->current_tick; |
| 548 | |
| 549 | /* Right back onto the overflow vector? */ |
| 550 | if (interval >= (1 << (3 * TW_RING_SHIFT))) |
| 551 | { |
| 552 | ts = &tw->overflow; |
| 553 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 554 | continue; |
| 555 | } |
| 556 | /* Compute ring offsets */ |
| 557 | new_glacier_ring_offset = interval >> (2 * TW_RING_SHIFT); |
| 558 | |
| 559 | interval -= (new_glacier_ring_offset << (2 * TW_RING_SHIFT)); |
| 560 | |
| 561 | /* Note: the wheels are at (0,0,0), no add-with-carry needed */ |
| 562 | new_slow_ring_offset = interval >> TW_RING_SHIFT; |
| 563 | interval -= (new_slow_ring_offset << TW_RING_SHIFT); |
| 564 | new_fast_ring_offset = interval & TW_RING_MASK; |
| 565 | t->slow_ring_offset = new_slow_ring_offset; |
| 566 | t->fast_ring_offset = new_fast_ring_offset; |
| 567 | |
| 568 | /* Timer expires Right Now */ |
| 569 | if (PREDICT_FALSE (t->slow_ring_offset == 0 && |
| 570 | t->fast_ring_offset == 0 && |
| 571 | new_glacier_ring_offset == 0)) |
| 572 | { |
| 573 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 574 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 575 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, |
| 576 | t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 577 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 578 | pool_put (tw->timers, t); |
| 579 | } |
| 580 | /* Timer moves to the glacier ring */ |
| 581 | else if (new_glacier_ring_offset) |
| 582 | { |
| 583 | ts = &tw->w[TW_TIMER_RING_GLACIER][new_glacier_ring_offset]; |
| 584 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 585 | } |
| 586 | /* Timer moves to the slow ring */ |
| 587 | else if (t->slow_ring_offset) |
| 588 | { |
| 589 | /* Add to slow ring */ |
| 590 | ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset]; |
| 591 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 592 | } |
| 593 | /* Timer timer moves to the fast ring */ |
| 594 | else |
| 595 | { |
| 596 | ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset]; |
| 597 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 598 | #if TW_FAST_WHEEL_BITMAP |
| 599 | tw->fast_slot_bitmap = |
| 600 | clib_bitmap_set (tw->fast_slot_bitmap, |
| 601 | t->fast_ring_offset, 1); |
| 602 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | #if TW_TIMER_WHEELS > 2 |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 609 | /* |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 610 | * Double odometer-click? Process one slot in the glacier ring... |
| 611 | */ |
| 612 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING |
| 613 | && slow_wheel_index == TW_SLOTS_PER_RING)) |
| 614 | { |
| 615 | glacier_wheel_index %= TW_SLOTS_PER_RING; |
| 616 | ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_wheel_index]; |
| 617 | |
| 618 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 619 | next_index = head->next; |
| 620 | |
| 621 | /* Make slot empty */ |
| 622 | head->next = head->prev = ts->head_index; |
| 623 | |
| 624 | /* traverse slot, deal timers into slow ring */ |
| 625 | while (next_index != head - tw->timers) |
| 626 | { |
| 627 | t = pool_elt_at_index (tw->timers, next_index); |
| 628 | next_index = t->next; |
| 629 | |
| 630 | /* Remove from glacier ring slot (hammer) */ |
| 631 | t->next = t->prev = ~0; |
| 632 | |
| 633 | /* Timer expires Right Now */ |
| 634 | if (PREDICT_FALSE (t->slow_ring_offset == 0 && |
| 635 | t->fast_ring_offset == 0)) |
| 636 | { |
| 637 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 638 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 639 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, |
| 640 | t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 641 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 642 | pool_put (tw->timers, t); |
| 643 | } |
| 644 | /* Timer expires during slow-wheel tick 0 */ |
| 645 | else if (PREDICT_FALSE (t->slow_ring_offset == 0)) |
| 646 | { |
| 647 | ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset]; |
| 648 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 649 | #if TW_FAST_WHEEL_BITMAP |
| 650 | tw->fast_slot_bitmap = |
| 651 | clib_bitmap_set (tw->fast_slot_bitmap, |
| 652 | t->fast_ring_offset, 1); |
| 653 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 654 | } |
| 655 | else /* typical case */ |
| 656 | { |
| 657 | /* Add to slow ring */ |
| 658 | ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset]; |
| 659 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | #endif |
| 664 | |
| 665 | #if TW_TIMER_WHEELS > 1 |
| 666 | /* |
| 667 | * Single odometer-click? Process a slot in the slow ring, |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 668 | */ |
| 669 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING)) |
| 670 | { |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 671 | slow_wheel_index %= TW_SLOTS_PER_RING; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 672 | ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index]; |
| 673 | |
| 674 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 675 | next_index = head->next; |
| 676 | |
| 677 | /* Make slot empty */ |
| 678 | head->next = head->prev = ts->head_index; |
| 679 | |
| 680 | /* traverse slot, deal timers into fast ring */ |
| 681 | while (next_index != head - tw->timers) |
| 682 | { |
| 683 | t = pool_elt_at_index (tw->timers, next_index); |
| 684 | next_index = t->next; |
| 685 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 686 | /* Remove from sloe ring slot (hammer) */ |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 687 | t->next = t->prev = ~0; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 688 | |
| 689 | /* Timer expires Right Now */ |
| 690 | if (PREDICT_FALSE (t->fast_ring_offset == 0)) |
| 691 | { |
| 692 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 693 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 694 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, |
| 695 | t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 696 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 697 | pool_put (tw->timers, t); |
| 698 | } |
| 699 | else /* typical case */ |
| 700 | { |
| 701 | /* Add to fast ring */ |
| 702 | ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset]; |
| 703 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 704 | #if TW_FAST_WHEEL_BITMAP |
| 705 | tw->fast_slot_bitmap = |
| 706 | clib_bitmap_set (tw->fast_slot_bitmap, |
| 707 | t->fast_ring_offset, 1); |
| 708 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 709 | } |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 710 | } |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 711 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 712 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 713 | |
| 714 | /* Handle the fast ring */ |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 715 | fast_wheel_index %= TW_SLOTS_PER_RING; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 716 | ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index]; |
| 717 | |
| 718 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 719 | next_index = head->next; |
| 720 | |
| 721 | /* Make slot empty */ |
| 722 | head->next = head->prev = ts->head_index; |
| 723 | |
| 724 | /* Construct vector of expired timer handles to give the user */ |
| 725 | while (next_index != ts->head_index) |
| 726 | { |
| 727 | t = pool_elt_at_index (tw->timers, next_index); |
| 728 | next_index = t->next; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 729 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 730 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 731 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 732 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 733 | pool_put (tw->timers, t); |
| 734 | } |
| 735 | |
| 736 | /* If any timers expired, tell the user */ |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 737 | if (callback_vector_arg == 0 && vec_len (callback_vector)) |
Gabriel Ganne | 581b072 | 2017-02-13 10:27:15 +0100 | [diff] [blame] | 738 | { |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 739 | /* The callback is optional. We return the u32 * handle vector */ |
| 740 | if (tw->expired_timer_callback) |
Dave Barach | ad09933 | 2018-02-20 08:34:48 -0500 | [diff] [blame] | 741 | { |
| 742 | tw->expired_timer_callback (callback_vector); |
| 743 | vec_reset_length (callback_vector); |
| 744 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 745 | tw->expired_timer_handles = callback_vector; |
Gabriel Ganne | 581b072 | 2017-02-13 10:27:15 +0100 | [diff] [blame] | 746 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 747 | |
| 748 | #if TW_FAST_WHEEL_BITMAP |
| 749 | tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap, |
| 750 | fast_wheel_index, 0); |
| 751 | #endif |
| 752 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 753 | tw->current_tick++; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 754 | fast_wheel_index++; |
| 755 | tw->current_index[TW_TIMER_RING_FAST] = fast_wheel_index; |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 756 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 757 | #if TW_TIMER_WHEELS > 1 |
| 758 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING)) |
| 759 | slow_wheel_index++; |
| 760 | tw->current_index[TW_TIMER_RING_SLOW] = slow_wheel_index; |
| 761 | #endif |
| 762 | |
| 763 | #if TW_TIMER_WHEELS > 2 |
| 764 | if (PREDICT_FALSE (slow_wheel_index == TW_SLOTS_PER_RING)) |
| 765 | glacier_wheel_index++; |
| 766 | tw->current_index[TW_TIMER_RING_GLACIER] = glacier_wheel_index; |
| 767 | #endif |
| 768 | |
| 769 | if (vec_len (callback_vector) >= tw->max_expirations) |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 770 | break; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 771 | } |
Gabriel Ganne | 581b072 | 2017-02-13 10:27:15 +0100 | [diff] [blame] | 772 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 773 | if (callback_vector_arg == 0) |
| 774 | tw->expired_timer_handles = callback_vector; |
| 775 | |
Florin Coras | 954898f | 2017-02-21 19:26:51 -0800 | [diff] [blame] | 776 | tw->last_run_time += i * tw->timer_interval; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 777 | return callback_vector; |
| 778 | } |
| 779 | |
| 780 | u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now) |
| 781 | { |
| 782 | return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ ); |
| 783 | } |
| 784 | |
| 785 | u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now, |
| 786 | u32 * vec) |
| 787 | { |
| 788 | return TW (tw_timer_expire_timers_internal) (tw, now, vec); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 789 | } |
| 790 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 791 | #if TW_FAST_WHEEL_BITMAP |
| 792 | /** Returns an approximation to the first timer expiration in |
| 793 | * timer-ticks from "now". To avoid wasting an unjustifiable |
| 794 | * amount of time on the problem, we maintain an approximate fast-wheel slot |
| 795 | * occupancy bitmap. We don't worry about clearing fast wheel bits |
| 796 | * when timers are removed from fast wheel slots. |
| 797 | */ |
| 798 | |
| 799 | u32 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw) |
| 800 | { |
| 801 | u32 first_expiring_index, fast_ring_index; |
| 802 | i32 delta; |
| 803 | |
| 804 | if (clib_bitmap_is_zero (tw->fast_slot_bitmap)) |
| 805 | return TW_SLOTS_PER_RING; |
| 806 | |
| 807 | fast_ring_index = tw->current_index[TW_TIMER_RING_FAST]; |
| 808 | if (fast_ring_index == TW_SLOTS_PER_RING) |
| 809 | fast_ring_index = 0; |
| 810 | |
| 811 | first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap, |
| 812 | fast_ring_index); |
| 813 | if (first_expiring_index == ~0 && fast_ring_index != 0) |
| 814 | first_expiring_index = clib_bitmap_first_set (tw->fast_slot_bitmap); |
| 815 | |
| 816 | ASSERT (first_expiring_index != ~0); |
| 817 | |
| 818 | delta = (i32) first_expiring_index - (i32) fast_ring_index; |
| 819 | if (delta < 0) |
| 820 | delta += TW_SLOTS_PER_RING; |
| 821 | |
| 822 | ASSERT (delta >= 0); |
| 823 | |
| 824 | return (u32) delta; |
| 825 | } |
| 826 | |
| 827 | #endif |
| 828 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 829 | /* |
| 830 | * fd.io coding-style-patch-verification: ON |
| 831 | * |
| 832 | * Local Variables: |
| 833 | * eval: (c-set-style "gnu") |
| 834 | * End: |
| 835 | */ |