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 |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 145 | timer_remove (TWT (tw_timer) * pool, TWT (tw_timer) * elt) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 146 | { |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 147 | 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 Coras | 208daa1 | 2018-07-02 01:30:51 -0700 | [diff] [blame] | 160 | static inline void |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 161 | timer_add (TWT (tw_timer_wheel) * tw, TWT (tw_timer) * t, u64 interval) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 162 | { |
| 163 | #if TW_TIMER_WHEELS > 1 |
| 164 | u16 slow_ring_offset; |
| 165 | u32 carry; |
| 166 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 167 | #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 Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 173 | u16 fast_ring_offset; |
| 174 | tw_timer_wheel_slot_t *ts; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 175 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 176 | /* 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 Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 193 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 194 | TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 195 | #endif |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 196 | return; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 197 | } |
| 198 | #endif |
| 199 | |
| 200 | glacier_ring_offset = interval >> (2 * TW_RING_SHIFT); |
Dave Barach | 28b70af | 2017-06-13 17:04:28 -0400 | [diff] [blame] | 201 | ASSERT ((u64) glacier_ring_offset < TW_SLOTS_PER_RING); |
| 202 | interval -= (((u64) glacier_ring_offset) << (2 * TW_RING_SHIFT)); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 203 | #endif |
| 204 | #if TW_TIMER_WHEELS > 1 |
| 205 | slow_ring_offset = interval >> TW_RING_SHIFT; |
Dave Barach | 28b70af | 2017-06-13 17:04:28 -0400 | [diff] [blame] | 206 | ASSERT ((u64) slow_ring_offset < TW_SLOTS_PER_RING); |
| 207 | interval -= (((u64) slow_ring_offset) << TW_RING_SHIFT); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 208 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 209 | fast_ring_offset = interval & TW_RING_MASK; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 210 | |
| 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 Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 220 | #if TW_TIMER_WHEELS > 1 |
| 221 | carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0; |
| 222 | fast_ring_offset %= TW_SLOTS_PER_RING; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 223 | 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 Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 228 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 229 | #if TW_TIMER_WHEELS > 2 |
| 230 | glacier_ring_offset += |
| 231 | (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK) + carry; |
| 232 | glacier_ring_offset %= TW_SLOTS_PER_RING; |
| 233 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 234 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 235 | #if TW_TIMER_WHEELS > 2 |
| 236 | if (glacier_ring_offset != |
| 237 | (tw->current_index[TW_TIMER_RING_GLACIER] & TW_RING_MASK)) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 238 | { |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 239 | /* We'll need slow and fast ring offsets later */ |
| 240 | t->slow_ring_offset = slow_ring_offset; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 241 | t->fast_ring_offset = fast_ring_offset; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 242 | |
| 243 | ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_ring_offset]; |
| 244 | |
| 245 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 246 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 247 | TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 248 | #endif |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 249 | return; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 250 | } |
| 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 Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 260 | |
| 261 | ts = &tw->w[TW_TIMER_RING_SLOW][slow_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 |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 265 | TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 266 | #endif |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 267 | return; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 268 | } |
| 269 | #else |
| 270 | fast_ring_offset %= TW_SLOTS_PER_RING; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 271 | #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 Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 277 | |
| 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 Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 282 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 283 | TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 284 | #endif |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 285 | } |
| 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 | */ |
| 295 | u32 |
| 296 | TW (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); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 304 | clib_memset (t, 0xff, sizeof (*t)); |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 305 | |
| 306 | t->user_handle = TW (make_internal_timer_handle) (user_id, timer_id); |
| 307 | |
| 308 | timer_add (tw, t, interval); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 309 | return t - tw->timers; |
| 310 | } |
| 311 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 312 | #if TW_TIMER_SCAN_FOR_HANDLE > 0 |
| 313 | int 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 Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 347 | /** |
| 348 | * @brief Stop a tw timer |
| 349 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 350 | * @param u32 handle timer cancellation returned by tw_timer_start |
| 351 | */ |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 352 | void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle) |
| 353 | { |
| 354 | TWT (tw_timer) * t; |
| 355 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 356 | #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 Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 365 | #if TW_START_STOP_TRACE_SIZE > 0 |
| 366 | TW (tw_timer_trace) (tw, ~0, ~0, handle); |
| 367 | #endif |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 368 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 369 | 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 Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 374 | timer_remove (tw->timers, t); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 375 | |
| 376 | pool_put_index (tw->timers, handle); |
| 377 | } |
| 378 | |
Florin Coras | 40f9246 | 2018-08-01 16:25:45 -0700 | [diff] [blame] | 379 | int TW (tw_timer_handle_is_free) (TWT (tw_timer_wheel) * tw, u32 handle) |
| 380 | { |
| 381 | return pool_is_free_index (tw->timers, handle); |
| 382 | } |
| 383 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 384 | /** |
Florin Coras | adb5bd5 | 2018-06-22 15:29:38 -0700 | [diff] [blame] | 385 | * @brief Update a tw timer |
| 386 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
| 387 | * @param u32 handle timer returned by tw_timer_start |
| 388 | * @param u32 interval timer interval in ticks |
| 389 | */ |
| 390 | void TW (tw_timer_update) (TWT (tw_timer_wheel) * tw, u32 handle, |
| 391 | u64 interval) |
| 392 | { |
| 393 | TWT (tw_timer) * t; |
| 394 | t = pool_elt_at_index (tw->timers, handle); |
| 395 | timer_remove (tw->timers, t); |
| 396 | timer_add (tw, t, interval); |
| 397 | } |
| 398 | |
| 399 | /** |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 400 | * @brief Initialize a tw timer wheel template instance |
| 401 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
| 402 | * @param void * expired_timer_callback. Passed a u32 * vector of |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 403 | * expired timer handles. The callback is optional. |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 404 | * @param f64 timer_interval_in_seconds |
| 405 | */ |
| 406 | void |
| 407 | TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw, |
| 408 | void *expired_timer_callback, |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 409 | f64 timer_interval_in_seconds, u32 max_expirations) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 410 | { |
| 411 | int ring, slot; |
| 412 | tw_timer_wheel_slot_t *ts; |
| 413 | TWT (tw_timer) * t; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 414 | clib_memset (tw, 0, sizeof (*tw)); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 415 | tw->expired_timer_callback = expired_timer_callback; |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 416 | tw->max_expirations = max_expirations; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 417 | if (timer_interval_in_seconds == 0.0) |
| 418 | { |
| 419 | clib_warning ("timer interval is zero"); |
| 420 | abort (); |
| 421 | } |
| 422 | tw->timer_interval = timer_interval_in_seconds; |
| 423 | tw->ticks_per_second = 1.0 / timer_interval_in_seconds; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 424 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 425 | vec_validate (tw->expired_timer_handles, 0); |
| 426 | _vec_len (tw->expired_timer_handles) = 0; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 427 | |
| 428 | for (ring = 0; ring < TW_TIMER_WHEELS; ring++) |
| 429 | { |
| 430 | for (slot = 0; slot < TW_SLOTS_PER_RING; slot++) |
| 431 | { |
| 432 | ts = &tw->w[ring][slot]; |
| 433 | pool_get (tw->timers, t); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 434 | clib_memset (t, 0xff, sizeof (*t)); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 435 | t->next = t->prev = t - tw->timers; |
| 436 | ts->head_index = t - tw->timers; |
| 437 | } |
| 438 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 439 | |
| 440 | #if TW_OVERFLOW_VECTOR > 0 |
| 441 | ts = &tw->overflow; |
| 442 | pool_get (tw->timers, t); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 443 | clib_memset (t, 0xff, sizeof (*t)); |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 444 | t->next = t->prev = t - tw->timers; |
| 445 | ts->head_index = t - tw->timers; |
| 446 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
| 450 | * @brief Free a tw timer wheel template instance |
| 451 | * @param tw_timer_wheel_t * tw timer wheel object pointer |
| 452 | */ |
| 453 | void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw) |
| 454 | { |
| 455 | int i, j; |
| 456 | tw_timer_wheel_slot_t *ts; |
| 457 | TWT (tw_timer) * head, *t; |
| 458 | u32 next_index; |
| 459 | |
| 460 | for (i = 0; i < TW_TIMER_WHEELS; i++) |
| 461 | { |
| 462 | for (j = 0; j < TW_SLOTS_PER_RING; j++) |
| 463 | { |
| 464 | ts = &tw->w[i][j]; |
| 465 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 466 | next_index = head->next; |
| 467 | |
| 468 | while (next_index != ts->head_index) |
| 469 | { |
| 470 | t = pool_elt_at_index (tw->timers, next_index); |
| 471 | next_index = t->next; |
| 472 | pool_put (tw->timers, t); |
| 473 | } |
| 474 | pool_put (tw->timers, head); |
| 475 | } |
| 476 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 477 | |
Dave Barach | 0f96673 | 2020-02-03 11:57:41 -0500 | [diff] [blame] | 478 | #if TW_OVERFLOW_VECTOR > 0 |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 479 | ts = &tw->overflow; |
| 480 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 481 | next_index = head->next; |
| 482 | |
| 483 | while (next_index != ts->head_index) |
| 484 | { |
| 485 | t = pool_elt_at_index (tw->timers, next_index); |
| 486 | next_index = t->next; |
| 487 | pool_put (tw->timers, t); |
| 488 | } |
| 489 | pool_put (tw->timers, head); |
| 490 | #endif |
| 491 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 492 | clib_memset (tw, 0, sizeof (*tw)); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @brief Advance a tw timer wheel. Calls the expired timer callback |
| 497 | * as needed. This routine should be called once every timer_interval seconds |
| 498 | * @param tw_timer_wheel_t * tw timer wheel template instance pointer |
| 499 | * @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] | 500 | * @returns u32 * vector of expired user handles |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 501 | */ |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 502 | static inline |
| 503 | u32 * TW (tw_timer_expire_timers_internal) (TWT (tw_timer_wheel) * tw, |
| 504 | f64 now, |
| 505 | u32 * callback_vector_arg) |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 506 | { |
| 507 | u32 nticks, i; |
| 508 | tw_timer_wheel_slot_t *ts; |
| 509 | TWT (tw_timer) * t, *head; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 510 | u32 *callback_vector; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 511 | u32 fast_wheel_index; |
| 512 | u32 next_index; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 513 | u32 slow_wheel_index __attribute__ ((unused)); |
| 514 | u32 glacier_wheel_index __attribute__ ((unused)); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 515 | |
Dave Barach | 3d9f134 | 2020-03-08 08:27:19 -0400 | [diff] [blame] | 516 | /* Called too soon to process new timer expirations? */ |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 517 | if (PREDICT_FALSE (now < tw->next_run_time)) |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 518 | return callback_vector_arg; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 519 | |
| 520 | /* Number of ticks which have occurred */ |
| 521 | nticks = tw->ticks_per_second * (now - tw->last_run_time); |
| 522 | if (nticks == 0) |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 523 | return callback_vector_arg; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 524 | |
| 525 | /* Remember when we ran, compute next runtime */ |
| 526 | tw->next_run_time = (now + tw->timer_interval); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 527 | |
Dave Barach | 3d9f134 | 2020-03-08 08:27:19 -0400 | [diff] [blame] | 528 | /* First call, or time jumped backwards? */ |
| 529 | if (PREDICT_FALSE |
| 530 | ((tw->last_run_time == 0.0) || (now <= tw->last_run_time))) |
| 531 | { |
| 532 | tw->last_run_time = now; |
| 533 | return callback_vector_arg; |
| 534 | } |
| 535 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 536 | if (callback_vector_arg == 0) |
| 537 | { |
| 538 | _vec_len (tw->expired_timer_handles) = 0; |
| 539 | callback_vector = tw->expired_timer_handles; |
| 540 | } |
| 541 | else |
| 542 | callback_vector = callback_vector_arg; |
| 543 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 544 | for (i = 0; i < nticks; i++) |
| 545 | { |
| 546 | fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST]; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 547 | if (TW_TIMER_WHEELS > 1) |
| 548 | slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW]; |
| 549 | if (TW_TIMER_WHEELS > 2) |
| 550 | glacier_wheel_index = tw->current_index[TW_TIMER_RING_GLACIER]; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 551 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 552 | #if TW_OVERFLOW_VECTOR > 0 |
| 553 | /* Triple odometer-click? Process the overflow vector... */ |
| 554 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING |
| 555 | && slow_wheel_index == TW_SLOTS_PER_RING |
| 556 | && glacier_wheel_index == TW_SLOTS_PER_RING)) |
| 557 | { |
| 558 | u64 interval; |
| 559 | u32 new_glacier_ring_offset, new_slow_ring_offset; |
| 560 | u32 new_fast_ring_offset; |
| 561 | |
| 562 | ts = &tw->overflow; |
| 563 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 564 | next_index = head->next; |
| 565 | |
| 566 | /* Make slot empty */ |
| 567 | head->next = head->prev = ts->head_index; |
| 568 | |
| 569 | /* traverse slot, place timers wherever they go */ |
| 570 | while (next_index != head - tw->timers) |
| 571 | { |
| 572 | t = pool_elt_at_index (tw->timers, next_index); |
| 573 | next_index = t->next; |
| 574 | |
| 575 | /* Remove from the overflow vector (hammer) */ |
| 576 | t->next = t->prev = ~0; |
| 577 | |
| 578 | ASSERT (t->expiration_time >= tw->current_tick); |
| 579 | |
| 580 | interval = t->expiration_time - tw->current_tick; |
| 581 | |
| 582 | /* Right back onto the overflow vector? */ |
| 583 | if (interval >= (1 << (3 * TW_RING_SHIFT))) |
| 584 | { |
| 585 | ts = &tw->overflow; |
| 586 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 587 | continue; |
| 588 | } |
| 589 | /* Compute ring offsets */ |
| 590 | new_glacier_ring_offset = interval >> (2 * TW_RING_SHIFT); |
| 591 | |
| 592 | interval -= (new_glacier_ring_offset << (2 * TW_RING_SHIFT)); |
| 593 | |
| 594 | /* Note: the wheels are at (0,0,0), no add-with-carry needed */ |
| 595 | new_slow_ring_offset = interval >> TW_RING_SHIFT; |
| 596 | interval -= (new_slow_ring_offset << TW_RING_SHIFT); |
| 597 | new_fast_ring_offset = interval & TW_RING_MASK; |
| 598 | t->slow_ring_offset = new_slow_ring_offset; |
| 599 | t->fast_ring_offset = new_fast_ring_offset; |
| 600 | |
| 601 | /* Timer expires Right Now */ |
| 602 | if (PREDICT_FALSE (t->slow_ring_offset == 0 && |
| 603 | t->fast_ring_offset == 0 && |
| 604 | new_glacier_ring_offset == 0)) |
| 605 | { |
| 606 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 607 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 608 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, |
| 609 | t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 610 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 611 | pool_put (tw->timers, t); |
| 612 | } |
| 613 | /* Timer moves to the glacier ring */ |
| 614 | else if (new_glacier_ring_offset) |
| 615 | { |
| 616 | ts = &tw->w[TW_TIMER_RING_GLACIER][new_glacier_ring_offset]; |
| 617 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 618 | } |
| 619 | /* Timer moves to the slow ring */ |
| 620 | else if (t->slow_ring_offset) |
| 621 | { |
| 622 | /* Add to slow ring */ |
| 623 | ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset]; |
| 624 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 625 | } |
| 626 | /* Timer timer moves to the fast ring */ |
| 627 | else |
| 628 | { |
| 629 | ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset]; |
| 630 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 631 | #if TW_FAST_WHEEL_BITMAP |
| 632 | tw->fast_slot_bitmap = |
| 633 | clib_bitmap_set (tw->fast_slot_bitmap, |
| 634 | t->fast_ring_offset, 1); |
| 635 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | } |
| 639 | #endif |
| 640 | |
| 641 | #if TW_TIMER_WHEELS > 2 |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 642 | /* |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 643 | * Double odometer-click? Process one slot in the glacier ring... |
| 644 | */ |
| 645 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING |
| 646 | && slow_wheel_index == TW_SLOTS_PER_RING)) |
| 647 | { |
| 648 | glacier_wheel_index %= TW_SLOTS_PER_RING; |
| 649 | ts = &tw->w[TW_TIMER_RING_GLACIER][glacier_wheel_index]; |
| 650 | |
| 651 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 652 | next_index = head->next; |
| 653 | |
| 654 | /* Make slot empty */ |
| 655 | head->next = head->prev = ts->head_index; |
| 656 | |
| 657 | /* traverse slot, deal timers into slow ring */ |
| 658 | while (next_index != head - tw->timers) |
| 659 | { |
| 660 | t = pool_elt_at_index (tw->timers, next_index); |
| 661 | next_index = t->next; |
| 662 | |
| 663 | /* Remove from glacier ring slot (hammer) */ |
| 664 | t->next = t->prev = ~0; |
| 665 | |
| 666 | /* Timer expires Right Now */ |
| 667 | if (PREDICT_FALSE (t->slow_ring_offset == 0 && |
| 668 | t->fast_ring_offset == 0)) |
| 669 | { |
| 670 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 671 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 672 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, |
| 673 | t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 674 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 675 | pool_put (tw->timers, t); |
| 676 | } |
| 677 | /* Timer expires during slow-wheel tick 0 */ |
| 678 | else if (PREDICT_FALSE (t->slow_ring_offset == 0)) |
| 679 | { |
| 680 | ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset]; |
| 681 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 682 | #if TW_FAST_WHEEL_BITMAP |
| 683 | tw->fast_slot_bitmap = |
| 684 | clib_bitmap_set (tw->fast_slot_bitmap, |
| 685 | t->fast_ring_offset, 1); |
| 686 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 687 | } |
| 688 | else /* typical case */ |
| 689 | { |
| 690 | /* Add to slow ring */ |
| 691 | ts = &tw->w[TW_TIMER_RING_SLOW][t->slow_ring_offset]; |
| 692 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | #endif |
| 697 | |
| 698 | #if TW_TIMER_WHEELS > 1 |
| 699 | /* |
| 700 | * Single odometer-click? Process a slot in the slow ring, |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 701 | */ |
| 702 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING)) |
| 703 | { |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 704 | slow_wheel_index %= TW_SLOTS_PER_RING; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 705 | ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index]; |
| 706 | |
| 707 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 708 | next_index = head->next; |
| 709 | |
| 710 | /* Make slot empty */ |
| 711 | head->next = head->prev = ts->head_index; |
| 712 | |
| 713 | /* traverse slot, deal timers into fast ring */ |
| 714 | while (next_index != head - tw->timers) |
| 715 | { |
| 716 | t = pool_elt_at_index (tw->timers, next_index); |
| 717 | next_index = t->next; |
| 718 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 719 | /* Remove from sloe ring slot (hammer) */ |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 720 | t->next = t->prev = ~0; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 721 | |
| 722 | /* Timer expires Right Now */ |
| 723 | if (PREDICT_FALSE (t->fast_ring_offset == 0)) |
| 724 | { |
| 725 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 726 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 727 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, |
| 728 | t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 729 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 730 | pool_put (tw->timers, t); |
| 731 | } |
| 732 | else /* typical case */ |
| 733 | { |
| 734 | /* Add to fast ring */ |
| 735 | ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset]; |
| 736 | timer_addhead (tw->timers, ts->head_index, t - tw->timers); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 737 | #if TW_FAST_WHEEL_BITMAP |
| 738 | tw->fast_slot_bitmap = |
| 739 | clib_bitmap_set (tw->fast_slot_bitmap, |
| 740 | t->fast_ring_offset, 1); |
| 741 | #endif |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 742 | } |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 743 | } |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 744 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 745 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 746 | |
| 747 | /* Handle the fast ring */ |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 748 | fast_wheel_index %= TW_SLOTS_PER_RING; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 749 | ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index]; |
| 750 | |
| 751 | head = pool_elt_at_index (tw->timers, ts->head_index); |
| 752 | next_index = head->next; |
| 753 | |
| 754 | /* Make slot empty */ |
| 755 | head->next = head->prev = ts->head_index; |
| 756 | |
| 757 | /* Construct vector of expired timer handles to give the user */ |
| 758 | while (next_index != ts->head_index) |
| 759 | { |
| 760 | t = pool_elt_at_index (tw->timers, next_index); |
| 761 | next_index = t->next; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 762 | vec_add1 (callback_vector, t->user_handle); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 763 | #if TW_START_STOP_TRACE_SIZE > 0 |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 764 | TW (tw_timer_trace) (tw, 0xfe, t->user_handle, t - tw->timers); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 765 | #endif |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 766 | pool_put (tw->timers, t); |
| 767 | } |
| 768 | |
| 769 | /* If any timers expired, tell the user */ |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 770 | if (callback_vector_arg == 0 && vec_len (callback_vector)) |
Gabriel Ganne | 581b072 | 2017-02-13 10:27:15 +0100 | [diff] [blame] | 771 | { |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 772 | /* The callback is optional. We return the u32 * handle vector */ |
| 773 | if (tw->expired_timer_callback) |
Dave Barach | ad09933 | 2018-02-20 08:34:48 -0500 | [diff] [blame] | 774 | { |
| 775 | tw->expired_timer_callback (callback_vector); |
| 776 | vec_reset_length (callback_vector); |
| 777 | } |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 778 | tw->expired_timer_handles = callback_vector; |
Gabriel Ganne | 581b072 | 2017-02-13 10:27:15 +0100 | [diff] [blame] | 779 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 780 | |
| 781 | #if TW_FAST_WHEEL_BITMAP |
| 782 | tw->fast_slot_bitmap = clib_bitmap_set (tw->fast_slot_bitmap, |
| 783 | fast_wheel_index, 0); |
| 784 | #endif |
| 785 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 786 | tw->current_tick++; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 787 | fast_wheel_index++; |
| 788 | tw->current_index[TW_TIMER_RING_FAST] = fast_wheel_index; |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 789 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 790 | #if TW_TIMER_WHEELS > 1 |
| 791 | if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING)) |
| 792 | slow_wheel_index++; |
| 793 | tw->current_index[TW_TIMER_RING_SLOW] = slow_wheel_index; |
| 794 | #endif |
| 795 | |
| 796 | #if TW_TIMER_WHEELS > 2 |
| 797 | if (PREDICT_FALSE (slow_wheel_index == TW_SLOTS_PER_RING)) |
| 798 | glacier_wheel_index++; |
| 799 | tw->current_index[TW_TIMER_RING_GLACIER] = glacier_wheel_index; |
| 800 | #endif |
| 801 | |
| 802 | if (vec_len (callback_vector) >= tw->max_expirations) |
Gabriel Ganne | 83ed1f4 | 2017-02-15 16:55:30 +0100 | [diff] [blame] | 803 | break; |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 804 | } |
Gabriel Ganne | 581b072 | 2017-02-13 10:27:15 +0100 | [diff] [blame] | 805 | |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 806 | if (callback_vector_arg == 0) |
| 807 | tw->expired_timer_handles = callback_vector; |
| 808 | |
Florin Coras | 954898f | 2017-02-21 19:26:51 -0800 | [diff] [blame] | 809 | tw->last_run_time += i * tw->timer_interval; |
Dave Barach | 4af9ba1 | 2017-06-07 15:18:23 -0400 | [diff] [blame] | 810 | return callback_vector; |
| 811 | } |
| 812 | |
| 813 | u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now) |
| 814 | { |
| 815 | return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ ); |
| 816 | } |
| 817 | |
| 818 | u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now, |
| 819 | u32 * vec) |
| 820 | { |
| 821 | return TW (tw_timer_expire_timers_internal) (tw, now, vec); |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 822 | } |
| 823 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 824 | #if TW_FAST_WHEEL_BITMAP |
| 825 | /** Returns an approximation to the first timer expiration in |
| 826 | * timer-ticks from "now". To avoid wasting an unjustifiable |
| 827 | * amount of time on the problem, we maintain an approximate fast-wheel slot |
| 828 | * occupancy bitmap. We don't worry about clearing fast wheel bits |
| 829 | * when timers are removed from fast wheel slots. |
| 830 | */ |
| 831 | |
| 832 | u32 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw) |
| 833 | { |
| 834 | u32 first_expiring_index, fast_ring_index; |
| 835 | i32 delta; |
| 836 | |
Andreas Schultz | 217c62a | 2019-06-25 15:29:06 +0200 | [diff] [blame] | 837 | #if TW_TIMER_WHEELS > 1 |
| 838 | fast_ring_index = tw->current_index[TW_TIMER_RING_FAST]; |
| 839 | if (fast_ring_index == TW_SLOTS_PER_RING) |
| 840 | return 1; |
| 841 | |
| 842 | first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap, |
| 843 | fast_ring_index); |
| 844 | if (first_expiring_index == ~0) |
| 845 | first_expiring_index = TW_SLOTS_PER_RING; |
| 846 | |
| 847 | #else |
| 848 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 849 | if (clib_bitmap_is_zero (tw->fast_slot_bitmap)) |
| 850 | return TW_SLOTS_PER_RING; |
| 851 | |
| 852 | fast_ring_index = tw->current_index[TW_TIMER_RING_FAST]; |
| 853 | if (fast_ring_index == TW_SLOTS_PER_RING) |
| 854 | fast_ring_index = 0; |
| 855 | |
| 856 | first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap, |
| 857 | fast_ring_index); |
| 858 | if (first_expiring_index == ~0 && fast_ring_index != 0) |
| 859 | first_expiring_index = clib_bitmap_first_set (tw->fast_slot_bitmap); |
Andreas Schultz | 217c62a | 2019-06-25 15:29:06 +0200 | [diff] [blame] | 860 | #endif |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 861 | |
| 862 | ASSERT (first_expiring_index != ~0); |
| 863 | |
| 864 | delta = (i32) first_expiring_index - (i32) fast_ring_index; |
| 865 | if (delta < 0) |
| 866 | delta += TW_SLOTS_PER_RING; |
| 867 | |
| 868 | ASSERT (delta >= 0); |
| 869 | |
| 870 | return (u32) delta; |
| 871 | } |
| 872 | |
| 873 | #endif |
| 874 | |
Dave Barach | 8e8f98c | 2017-02-03 11:58:53 -0500 | [diff] [blame] | 875 | /* |
| 876 | * fd.io coding-style-patch-verification: ON |
| 877 | * |
| 878 | * Local Variables: |
| 879 | * eval: (c-set-style "gnu") |
| 880 | * End: |
| 881 | */ |