FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
tw_timer_template.h
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 #ifndef TW_SUFFIX
17 #error do not include tw_timer_template.h directly
18 #endif
19 
20 #include <vppinfra/clib.h>
21 #include <vppinfra/pool.h>
22 
23 #ifndef _twt
24 #define _twt(a,b) a##b##_t
25 #define __twt(a,b) _twt(a,b)
26 #define TWT(a) __twt(a,TW_SUFFIX)
27 
28 #define _tw(a,b) a##b
29 #define __tw(a,b) _tw(a,b)
30 #define TW(a) __tw(a,TW_SUFFIX)
31 #endif
32 
33 /** @file
34  @brief TW timer template header file, do not compile directly
35 
36 Instantiation of tw_timer_template.h generates named structures to
37 implement specific timer wheel geometries. Choices include: number of
38 timer wheels (currently, 1 or 2), number of slots per ring (a power of
39 two), and the number of timers per "object handle".
40 
41 Internally, user object/timer handles are 32-bit integers, so if one
42 selects 16 timers/object (4 bits), the resulting timer wheel handle is
43 limited to 2**28 objects.
44 
45 Here are the specific settings required to generate a single 2048 slot
46 wheel which supports 2 timers per object:
47 
48  #define TW_TIMER_WHEELS 1
49  #define TW_SLOTS_PER_RING 2048
50  #define TW_RING_SHIFT 11
51  #define TW_RING_MASK (TW_SLOTS_PER_RING -1)
52  #define TW_TIMERS_PER_OBJECT 2
53  #define LOG2_TW_TIMERS_PER_OBJECT 1
54  #define TW_SUFFIX _2t_1w_2048sl
55 
56 See tw_timer_2t_1w_2048sl.h for a complete
57 example.
58 
59 tw_timer_template.h is not intended to be #included directly. Client
60 codes can include multiple timer geometry header files, although
61 extreme caution would required to use the TW and TWT macros in such a
62 case.
63 
64 API usage example:
65 
66 Initialize a two-timer, single 2048-slot wheel w/ a 1-second
67 timer granularity:
68 
69  tw_timer_wheel_init_2t_1w_2048sl (&tm->single_wheel,
70  expired_timer_single_callback,
71  1.0 / * timer interval * / );
72 
73 Start a timer:
74 
75  handle = tw_timer_start_2t_1w_2048sl (&tm->single_wheel, elt_index,
76  [0 | 1] / * timer id * / ,
77  expiration_time_in_u32_ticks);
78 
79 Stop a timer:
80 
81  tw_timer_stop_2t_1w_2048sl (&tm->single_wheel, handle);
82 
83 Expired timer callback:
84 
85  static void
86  expired_timer_single_callback (u32 * expired_timers)
87  {
88  int i;
89  u32 pool_index, timer_id;
90  tw_timer_test_elt_t *e;
91  tw_timer_test_main_t *tm = &tw_timer_test_main;
92 
93  for (i = 0; i < vec_len (expired_timers);
94  {
95  pool_index = expired_timers[i] & 0x7FFFFFFF;
96  timer_id = expired_timers[i] >> 31;
97 
98  ASSERT (timer_id == 1);
99 
100  e = pool_elt_at_index (tm->test_elts, pool_index);
101 
102  if (e->expected_to_expire != tm->single_wheel.current_tick)
103  {
104  fformat (stdout, "[%d] expired at %d not %d\n",
105  e - tm->test_elts, tm->single_wheel.current_tick,
106  e->expected_to_expire);
107  }
108  pool_put (tm->test_elts, e);
109  }
110  }
111  */
112 
113 typedef struct
114 {
115  /** next, previous pool indices */
118 #if TW_TIMER_WHEELS > 0
119  /** fast ring offset, only valid in the slow ring */
120  u16 fast_ring_offset;
121  u16 pad;
122 #endif
123  /** user timer handle */
125 } TWT (tw_timer);
126 
127 /*
128  * These structures ar used by all geometries,
129  * so they need a private #include block...
130  */
131 #ifndef __defined_tw_timer_wheel_slot__
132 #define __defined_tw_timer_wheel_slot__
133 typedef struct
134 {
135  /** Listhead of timers which expire in this interval */
138 typedef enum
139 {
140  /** Fast timer ring ID */
142  /** Slow timer ring ID */
145 #endif /* __defined_tw_timer_wheel_slot__ */
146 
147 typedef struct
148 {
149  /** Timer pool */
150  TWT (tw_timer) * timers;
151 
152  /** Next time the wheel should run */
154 
155  /** Last time the wheel ran */
157 
158  /** Timer ticks per second */
160 
161  /** Timer interval, also needed to avoid fp divide in speed path */
163 
164  /** current tick */
166 
167  /** current wheel indices */
168  u32 current_index[TW_TIMER_WHEELS];
169 
170  /** wheel arrays */
172 
173  /** expired timer callback, receives a vector of handles */
174  void (*expired_timer_callback) (u32 * expired_timer_handles);
175 
176  /** vector of expired timers */
178 
179  /** maximum expirations */
181 } TWT (tw_timer_wheel);
182 
183 u32 TW (tw_timer_start) (TWT (tw_timer_wheel) * tw,
184  u32 pool_index, u32 timer_id, u32 interval);
185 
186 void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle);
187 
188 void TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
189  void *expired_timer_callback,
190  f64 timer_interval, u32 max_expirations);
191 
192 void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw);
193 
194 u32 TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now);
195 
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */
void TW() tw_timer_wheel_free(TWT(tw_timer_wheel)*tw)
Free a tw timer wheel template instance.
u8 pad[3]
log2 (size of the packing page block)
Definition: bihash_doc.h:61
void TW() tw_timer_wheel_init(TWT(tw_timer_wheel)*tw, void *expired_timer_callback, f64 timer_interval, u32 max_expirations)
Initialize a tw timer wheel template instance.
Fixed length block allocator.
#define TW_TIMER_WHEELS
u32 max_expirations
maximum expirations
f64 timer_interval
Timer interval, also needed to avoid fp divide in speed path.
u32 current_tick
current tick
static timer_callback_t * timers
Definition: timer.c:56
#define TW_SLOTS_PER_RING
u32 TW() tw_timer_start(TWT(tw_timer_wheel)*tw, u32 pool_index, u32 timer_id, u32 interval)
Start a Tw Timer.
#define TWT(a)
tw_ring_index_t
void TW() tw_timer_stop(TWT(tw_timer_wheel)*tw, u32 handle)
Stop a tw timer.
f64 next_run_time
Next time the wheel should run.
u32 next
next, previous pool indices
Fast timer ring ID.
unsigned int u32
Definition: types.h:88
u32 * expired_timer_handles
vector of expired timers
u32 TW() tw_timer_expire_timers(TWT(tw_timer_wheel)*tw, f64 now)
Advance a tw timer wheel.
f64 ticks_per_second
Timer ticks per second.
unsigned short u16
Definition: types.h:57
double f64
Definition: types.h:142
f64 last_run_time
Last time the wheel ran.
u32 user_handle
user timer handle
#define TW(a)
u32 head_index
Listhead of timers which expire in this interval.
Slow timer ring ID.