FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
tw_timer_template.c
Go to the documentation of this file.
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  */
21 
22 static inline u32
23 TW (make_internal_timer_handle) (u32 pool_index, u32 timer_id)
24 {
25  u32 handle;
26 
27  ASSERT (timer_id < TW_TIMERS_PER_OBJECT);
28  ASSERT (pool_index < (1 << (32 - LOG2_TW_TIMERS_PER_OBJECT)));
29 
30  handle = (timer_id << (32 - LOG2_TW_TIMERS_PER_OBJECT)) | (pool_index);
31  return handle;
32 }
33 
34 static inline void
35 timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
36 {
37  TWT (tw_timer) * head = pool_elt_at_index (pool, head_index);
38  TWT (tw_timer) * old_first;
39  u32 old_first_index;
40  TWT (tw_timer) * new;
41 
42  new = pool_elt_at_index (pool, new_index);
43 
44  if (PREDICT_FALSE (head->next == head_index))
45  {
46  head->next = head->prev = new_index;
47  new->next = new->prev = head_index;
48  return;
49  }
50 
51  old_first_index = head->next;
52  old_first = pool_elt_at_index (pool, old_first_index);
53 
54  new->next = old_first_index;
55  new->prev = old_first->prev;
56  old_first->prev = new_index;
57  head->next = new_index;
58 }
59 
60 static inline void
61 timer_remove (TWT (tw_timer) * pool, u32 index)
62 {
63  TWT (tw_timer) * elt = pool_elt_at_index (pool, index);
64  TWT (tw_timer) * next_elt, *prev_elt;
65 
66  ASSERT (elt->user_handle != ~0);
67 
68  next_elt = pool_elt_at_index (pool, elt->next);
69  prev_elt = pool_elt_at_index (pool, elt->prev);
70 
71  next_elt->prev = elt->prev;
72  prev_elt->next = elt->next;
73 
74  elt->prev = elt->next = ~0;
75 }
76 
77 /**
78  * @brief Start a Tw Timer
79  * @param tw_timer_wheel_t * tw timer wheel object pointer
80  * @param u32 pool_index user pool index, presumably for a tw session
81  * @param u32 timer_id app-specific timer ID. 4 bits.
82  * @param u32 interval timer interval in ticks
83  * @returns handle needed to cancel the timer
84  */
85 u32
86 TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
87  u32 interval)
88 {
89 #if TW_TIMER_WHEELS > 1
90  u16 slow_ring_offset;
91  u32 carry;
92 #endif
93  u16 fast_ring_offset;
95  TWT (tw_timer) * t;
96 
97  ASSERT (interval);
98 
99  pool_get (tw->timers, t);
100  t->next = t->prev = ~0;
101 #if TW_TIMER_WHEELS > 1
102  t->fast_ring_offset = ~0;
103 #endif
104  t->user_handle = TW (make_internal_timer_handle) (pool_index, timer_id);
105 
106  fast_ring_offset = interval & TW_RING_MASK;
107  fast_ring_offset += tw->current_index[TW_TIMER_RING_FAST];
108 #if TW_TIMER_WHEELS > 1
109  carry = fast_ring_offset >= TW_SLOTS_PER_RING ? 1 : 0;
110  fast_ring_offset %= TW_SLOTS_PER_RING;
111  slow_ring_offset = (interval >> TW_RING_SHIFT) + carry;
112 
113  /* Timer duration exceeds ~7 hrs? Oops */
114  ASSERT (slow_ring_offset < TW_SLOTS_PER_RING);
115 
116  /* Timer expires more than 51.2 seconds from now? */
117  if (slow_ring_offset)
118  {
119  slow_ring_offset += tw->current_index[TW_TIMER_RING_SLOW];
120  slow_ring_offset %= TW_SLOTS_PER_RING;
121 
122  /* We'll want the fast ring offset later... */
123  t->fast_ring_offset = fast_ring_offset;
124  ASSERT (t->fast_ring_offset < TW_SLOTS_PER_RING);
125 
126  ts = &tw->w[TW_TIMER_RING_SLOW][slow_ring_offset];
127 
128  timer_addhead (tw->timers, ts->head_index, t - tw->timers);
129 
130  return t - tw->timers;
131  }
132 #else
133  fast_ring_offset %= TW_SLOTS_PER_RING;
134  ASSERT (interval < TW_SLOTS_PER_RING);
135 #endif
136 
137  /* Timer expires less than one fast-ring revolution from now */
138  ts = &tw->w[TW_TIMER_RING_FAST][fast_ring_offset];
139 
140  timer_addhead (tw->timers, ts->head_index, t - tw->timers);
141  return t - tw->timers;
142 }
143 
144 /**
145  * @brief Stop a tw timer
146  * @param tw_timer_wheel_t * tw timer wheel object pointer
147  * @param u32 handle timer cancellation returned by tw_timer_start
148  */
149 void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
150 {
151  TWT (tw_timer) * t;
152 
153  t = pool_elt_at_index (tw->timers, handle);
154 
155  /* in case of idiotic handle (e.g. passing a listhead index) */
156  ASSERT (t->user_handle != ~0);
157 
158  timer_remove (tw->timers, handle);
159 
160  pool_put_index (tw->timers, handle);
161 }
162 
163 /**
164  * @brief Initialize a tw timer wheel template instance
165  * @param tw_timer_wheel_t * tw timer wheel object pointer
166  * @param void * expired_timer_callback. Passed a u32 * vector of
167  * expired timer handles.
168  * @param f64 timer_interval_in_seconds
169  */
170 void
171 TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
172  void *expired_timer_callback,
173  f64 timer_interval_in_seconds, u32 max_expirations)
174 {
175  int ring, slot;
177  TWT (tw_timer) * t;
178  memset (tw, 0, sizeof (*tw));
179  tw->expired_timer_callback = expired_timer_callback;
180  tw->max_expirations = max_expirations;
181  if (timer_interval_in_seconds == 0.0)
182  {
183  clib_warning ("timer interval is zero");
184  abort ();
185  }
186  tw->timer_interval = timer_interval_in_seconds;
187  tw->ticks_per_second = 1.0 / timer_interval_in_seconds;
188 
189  for (ring = 0; ring < TW_TIMER_WHEELS; ring++)
190  {
191  for (slot = 0; slot < TW_SLOTS_PER_RING; slot++)
192  {
193  ts = &tw->w[ring][slot];
194  pool_get (tw->timers, t);
195  memset (t, 0xff, sizeof (*t));
196  t->next = t->prev = t - tw->timers;
197  ts->head_index = t - tw->timers;
198  }
199  }
200 }
201 
202 /**
203  * @brief Free a tw timer wheel template instance
204  * @param tw_timer_wheel_t * tw timer wheel object pointer
205  */
206 void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
207 {
208  int i, j;
210  TWT (tw_timer) * head, *t;
211  u32 next_index;
212 
213  for (i = 0; i < TW_TIMER_WHEELS; i++)
214  {
215  for (j = 0; j < TW_SLOTS_PER_RING; j++)
216  {
217  ts = &tw->w[i][j];
218  head = pool_elt_at_index (tw->timers, ts->head_index);
219  next_index = head->next;
220 
221  while (next_index != ts->head_index)
222  {
223  t = pool_elt_at_index (tw->timers, next_index);
224  next_index = t->next;
225  pool_put (tw->timers, t);
226  }
227  pool_put (tw->timers, head);
228  }
229  }
230  memset (tw, 0, sizeof (*tw));
231 }
232 
233 /**
234  * @brief Advance a tw timer wheel. Calls the expired timer callback
235  * as needed. This routine should be called once every timer_interval seconds
236  * @param tw_timer_wheel_t * tw timer wheel template instance pointer
237  * @param f64 now the current time, e.g. from vlib_time_now(vm)
238  */
239 u32 TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now)
240 {
241  u32 nticks, i;
243  TWT (tw_timer) * t, *head;
244  u32 fast_wheel_index;
245  u32 next_index;
246  u32 nexpirations, total_nexpirations;
247 #if TW_TIMER_WHEELS > 1
248  u32 slow_wheel_index;
249 #endif
250 
251  /* Shouldn't happen */
252  if (PREDICT_FALSE (now < tw->next_run_time))
253  return 0;
254 
255  /* Number of ticks which have occurred */
256  nticks = tw->ticks_per_second * (now - tw->last_run_time);
257  if (nticks == 0)
258  return 0;
259 
260  /* Remember when we ran, compute next runtime */
261  tw->next_run_time = (now + tw->timer_interval);
262 
263  total_nexpirations = 0;
264  for (i = 0; i < nticks; i++)
265  {
266  fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST];
267 
268  /*
269  * If we've been around the fast ring once,
270  * process one slot in the slow ring before we handle
271  * the fast ring.
272  */
273  if (PREDICT_FALSE (fast_wheel_index == TW_SLOTS_PER_RING))
274  {
275  fast_wheel_index = tw->current_index[TW_TIMER_RING_FAST] = 0;
276 
277 #if TW_TIMER_WHEELS > 1
278  tw->current_index[TW_TIMER_RING_SLOW]++;
279  tw->current_index[TW_TIMER_RING_SLOW] %= TW_SLOTS_PER_RING;
280  slow_wheel_index = tw->current_index[TW_TIMER_RING_SLOW];
281 
282  ts = &tw->w[TW_TIMER_RING_SLOW][slow_wheel_index];
283 
284  head = pool_elt_at_index (tw->timers, ts->head_index);
285  next_index = head->next;
286 
287  /* Make slot empty */
288  head->next = head->prev = ts->head_index;
289 
290  /* traverse slot, deal timers into fast ring */
291  while (next_index != head - tw->timers)
292  {
293  t = pool_elt_at_index (tw->timers, next_index);
294  next_index = t->next;
295 
296  /* Remove from slow ring slot (hammer) */
297  t->next = t->prev = ~0;
298  ASSERT (t->fast_ring_offset < TW_SLOTS_PER_RING);
299  /* Add to fast ring */
300  ts = &tw->w[TW_TIMER_RING_FAST][t->fast_ring_offset];
301  timer_addhead (tw->timers, ts->head_index, t - tw->timers);
302  }
303 #endif
304  }
305 
306  /* Handle the fast ring */
307  vec_reset_length (tw->expired_timer_handles);
308 
309  ts = &tw->w[TW_TIMER_RING_FAST][fast_wheel_index];
310 
311  head = pool_elt_at_index (tw->timers, ts->head_index);
312  next_index = head->next;
313 
314  /* Make slot empty */
315  head->next = head->prev = ts->head_index;
316 
317  /* Construct vector of expired timer handles to give the user */
318  while (next_index != ts->head_index)
319  {
320  t = pool_elt_at_index (tw->timers, next_index);
321  next_index = t->next;
322  vec_add1 (tw->expired_timer_handles, t->user_handle);
323  pool_put (tw->timers, t);
324  }
325 
326  /* If any timers expired, tell the user */
327  nexpirations = vec_len (tw->expired_timer_handles);
328  if (nexpirations)
329  {
330  tw->expired_timer_callback (tw->expired_timer_handles);
331  total_nexpirations += nexpirations;
332  }
333  tw->current_index[TW_TIMER_RING_FAST]++;
334  tw->current_tick++;
335 
336  if (total_nexpirations >= tw->max_expirations)
337  break;
338  }
339 
340  tw->last_run_time += i * tw->timer_interval;
341  return total_nexpirations;
342 }
343 
344 /*
345  * fd.io coding-style-patch-verification: ON
346  *
347  * Local Variables:
348  * eval: (c-set-style "gnu")
349  * End:
350  */
void TW() tw_timer_wheel_init(TWT(tw_timer_wheel)*tw, void *expired_timer_callback, f64 timer_interval_in_seconds, u32 max_expirations)
Initialize a tw timer wheel template instance.
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define TW_TIMERS_PER_OBJECT
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
#define TW_TIMER_WHEELS
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define LOG2_TW_TIMERS_PER_OBJECT
static void timer_addhead(TWT(tw_timer)*pool, u32 head_index, u32 new_index)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
#define TW_SLOTS_PER_RING
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:241
#define TWT(a)
#define PREDICT_FALSE(x)
Definition: clib.h:97
u32 TW() tw_timer_start(TWT(tw_timer_wheel)*tw, u32 pool_index, u32 timer_id, u32 interval)
Start a Tw Timer.
#define TW_RING_SHIFT
u32 TW() tw_timer_expire_timers(TWT(tw_timer_wheel)*tw, f64 now)
Advance a tw timer wheel.
#define clib_warning(format, args...)
Definition: error.h:59
void TW() tw_timer_stop(TWT(tw_timer_wheel)*tw, u32 handle)
Stop a tw timer.
static void timer_remove(TWT(tw_timer)*pool, u32 index)
Fast timer ring ID.
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:255
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define TW_RING_MASK
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
#define TW(a)
static u32 TW() make_internal_timer_handle(u32 pool_index, u32 timer_id)
void TW() tw_timer_wheel_free(TWT(tw_timer_wheel)*tw)
Free a tw timer wheel template instance.
u32 head_index
Listhead of timers which expire in this interval.
Slow timer ring ID.