blob: 76755609451c76336f8105c7b19a8c7ec697d485 [file] [log] [blame]
Dave Barach8e8f98c2017-02-03 11:58:53 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#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
36Instantiation of tw_timer_template.h generates named structures to
37implement specific timer wheel geometries. Choices include: number of
38timer wheels (currently, 1 or 2), number of slots per ring (a power of
39two), and the number of timers per "object handle".
40
41Internally, user object/timer handles are 32-bit integers, so if one
42selects 16 timers/object (4 bits), the resulting timer wheel handle is
43limited to 2**28 objects.
44
45Here are the specific settings required to generate a single 2048 slot
46wheel 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
56See tw_timer_2t_1w_2048sl.h for a complete
57example.
58
59tw_timer_template.h is not intended to be #included directly. Client
60codes can include multiple timer geometry header files, although
61extreme caution would required to use the TW and TWT macros in such a
62case.
63
64API usage example:
65
66Initialize a two-timer, single 2048-slot wheel w/ a 1-second
67timer granularity:
68
69 tw_timer_wheel_init_2t_1w_2048sl (&tm->single_wheel,
70 expired_timer_single_callback,
71 1.0 / * timer interval * / );
72
73Start 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
79Stop a timer:
80
81 tw_timer_stop_2t_1w_2048sl (&tm->single_wheel, handle);
82
83Expired 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
Dave Barach4af9ba12017-06-07 15:18:23 -0400113#if (TW_TIMER_WHEELS != 1 && TW_TIMER_WHEELS != 2 && TW_TIMER_WHEELS != 3)
114#error TW_TIMER_WHEELS must be 1, 2 or 3
115#endif
116
Dave Barach8e8f98c2017-02-03 11:58:53 -0500117typedef struct
118{
119 /** next, previous pool indices */
120 u32 next;
121 u32 prev;
Dave Barach4af9ba12017-06-07 15:18:23 -0400122
123 union
124 {
125 struct
126 {
127#if (TW_TIMER_WHEELS == 3)
128 /** fast ring offset, only valid in the slow ring */
129 u16 fast_ring_offset;
130 /** slow ring offset, only valid in the glacier ring */
131 u16 slow_ring_offset;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500132#endif
Dave Barach4af9ba12017-06-07 15:18:23 -0400133#if (TW_TIMER_WHEELS == 2)
134 /** fast ring offset, only valid in the slow ring */
135 u16 fast_ring_offset;
136 /** slow ring offset, only valid in the glacier ring */
137 u16 pad;
138#endif
139 };
140
141#if (TW_OVERFLOW_VECTOR > 0)
142 u64 expiration_time;
143#endif
144 };
145
Dave Barach8e8f98c2017-02-03 11:58:53 -0500146 /** user timer handle */
147 u32 user_handle;
148} TWT (tw_timer);
149
150/*
151 * These structures ar used by all geometries,
152 * so they need a private #include block...
153 */
154#ifndef __defined_tw_timer_wheel_slot__
155#define __defined_tw_timer_wheel_slot__
156typedef struct
157{
158 /** Listhead of timers which expire in this interval */
159 u32 head_index;
160} tw_timer_wheel_slot_t;
161typedef enum
162{
163 /** Fast timer ring ID */
164 TW_TIMER_RING_FAST,
165 /** Slow timer ring ID */
166 TW_TIMER_RING_SLOW,
Dave Barach4af9ba12017-06-07 15:18:23 -0400167 /** Glacier ring ID */
168 TW_TIMER_RING_GLACIER,
Dave Barach8e8f98c2017-02-03 11:58:53 -0500169} tw_ring_index_t;
170#endif /* __defined_tw_timer_wheel_slot__ */
171
172typedef struct
173{
174 /** Timer pool */
175 TWT (tw_timer) * timers;
176
177 /** Next time the wheel should run */
178 f64 next_run_time;
179
180 /** Last time the wheel ran */
181 f64 last_run_time;
182
183 /** Timer ticks per second */
184 f64 ticks_per_second;
185
186 /** Timer interval, also needed to avoid fp divide in speed path */
187 f64 timer_interval;
188
189 /** current tick */
Dave Barach4af9ba12017-06-07 15:18:23 -0400190 u64 current_tick;
191
192 /** first expiration time */
193 u64 first_expires_tick;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500194
195 /** current wheel indices */
196 u32 current_index[TW_TIMER_WHEELS];
197
198 /** wheel arrays */
199 tw_timer_wheel_slot_t w[TW_TIMER_WHEELS][TW_SLOTS_PER_RING];
200
Dave Barach4af9ba12017-06-07 15:18:23 -0400201#if TW_OVERFLOW_VECTOR > 0
202 tw_timer_wheel_slot_t overflow;
203#endif
204
Dave Barach8e8f98c2017-02-03 11:58:53 -0500205 /** expired timer callback, receives a vector of handles */
206 void (*expired_timer_callback) (u32 * expired_timer_handles);
207
208 /** vector of expired timers */
209 u32 *expired_timer_handles;
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100210
211 /** maximum expirations */
212 u32 max_expirations;
Dave Barach8e8f98c2017-02-03 11:58:53 -0500213} TWT (tw_timer_wheel);
214
215u32 TW (tw_timer_start) (TWT (tw_timer_wheel) * tw,
Dave Barach4af9ba12017-06-07 15:18:23 -0400216 u32 pool_index, u32 timer_id, u64 interval);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500217
218void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle);
219
220void TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
221 void *expired_timer_callback,
Gabriel Ganne83ed1f42017-02-15 16:55:30 +0100222 f64 timer_interval, u32 max_expirations);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500223
224void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw);
225
Dave Barach4af9ba12017-06-07 15:18:23 -0400226u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now);
227u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now,
228 u32 * vec);
Dave Barach8e8f98c2017-02-03 11:58:53 -0500229
230/*
231 * fd.io coding-style-patch-verification: ON
232 *
233 * Local Variables:
234 * eval: (c-set-style "gnu")
235 * End:
236 */