]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*- |
2 | * BSD LICENSE | |
3 | * | |
4 | * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * | |
11 | * * Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * * Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in | |
15 | * the documentation and/or other materials provided with the | |
16 | * distribution. | |
17 | * * Neither the name of Intel Corporation nor the names of its | |
18 | * contributors may be used to endorse or promote products derived | |
19 | * from this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef _RTE_TIMER_H_ | |
35 | #define _RTE_TIMER_H_ | |
36 | ||
37 | /** | |
38 | * @file | |
39 | RTE Timer | |
40 | * | |
41 | * This library provides a timer service to RTE Data Plane execution | |
42 | * units that allows the execution of callback functions asynchronously. | |
43 | * | |
44 | * - Timers can be periodic or single (one-shot). | |
45 | * - The timers can be loaded from one core and executed on another. This has | |
46 | * to be specified in the call to rte_timer_reset(). | |
47 | * - High precision is possible. NOTE: this depends on the call frequency to | |
48 | * rte_timer_manage() that check the timer expiration for the local core. | |
49 | * - If not used in an application, for improved performance, it can be | |
50 | * disabled at compilation time by not calling the rte_timer_manage() | |
51 | * to improve performance. | |
52 | * | |
53 | * The timer library uses the rte_get_hpet_cycles() function that | |
54 | * uses the HPET, when available, to provide a reliable time reference. [HPET | |
55 | * routines are provided by EAL, which falls back to using the chip TSC (time- | |
56 | * stamp counter) as fallback when HPET is not available] | |
57 | * | |
58 | * This library provides an interface to add, delete and restart a | |
59 | * timer. The API is based on the BSD callout(9) API with a few | |
60 | * differences. | |
61 | * | |
62 | * See the RTE architecture documentation for more information about the | |
63 | * design of this library. | |
64 | */ | |
65 | ||
66 | #include <stdio.h> | |
67 | #include <stdint.h> | |
68 | #include <stddef.h> | |
69 | #include <rte_common.h> | |
70 | ||
71 | #ifdef __cplusplus | |
72 | extern "C" { | |
73 | #endif | |
74 | ||
75 | #define RTE_TIMER_STOP 0 /**< State: timer is stopped. */ | |
76 | #define RTE_TIMER_PENDING 1 /**< State: timer is scheduled. */ | |
77 | #define RTE_TIMER_RUNNING 2 /**< State: timer function is running. */ | |
78 | #define RTE_TIMER_CONFIG 3 /**< State: timer is being configured. */ | |
79 | ||
80 | #define RTE_TIMER_NO_OWNER -2 /**< Timer has no owner. */ | |
81 | ||
82 | /** | |
83 | * Timer type: Periodic or single (one-shot). | |
84 | */ | |
85 | enum rte_timer_type { | |
86 | SINGLE, | |
87 | PERIODICAL | |
88 | }; | |
89 | ||
90 | /** | |
91 | * Timer status: A union of the state (stopped, pending, running, | |
92 | * config) and an owner (the id of the lcore that owns the timer). | |
93 | */ | |
94 | union rte_timer_status { | |
95 | RTE_STD_C11 | |
96 | struct { | |
97 | uint16_t state; /**< Stop, pending, running, config. */ | |
98 | int16_t owner; /**< The lcore that owns the timer. */ | |
99 | }; | |
100 | uint32_t u32; /**< To atomic-set status + owner. */ | |
101 | }; | |
102 | ||
103 | #ifdef RTE_LIBRTE_TIMER_DEBUG | |
104 | /** | |
105 | * A structure that stores the timer statistics (per-lcore). | |
106 | */ | |
107 | struct rte_timer_debug_stats { | |
108 | uint64_t reset; /**< Number of success calls to rte_timer_reset(). */ | |
109 | uint64_t stop; /**< Number of success calls to rte_timer_stop(). */ | |
110 | uint64_t manage; /**< Number of calls to rte_timer_manage(). */ | |
111 | uint64_t pending; /**< Number of pending/running timers. */ | |
112 | }; | |
113 | #endif | |
114 | ||
115 | struct rte_timer; | |
116 | ||
117 | /** | |
118 | * Callback function type for timer expiry. | |
119 | */ | |
120 | typedef void (*rte_timer_cb_t)(struct rte_timer *, void *); | |
121 | ||
122 | #define MAX_SKIPLIST_DEPTH 10 | |
123 | ||
124 | /** | |
125 | * A structure describing a timer in RTE. | |
126 | */ | |
127 | struct rte_timer | |
128 | { | |
129 | uint64_t expire; /**< Time when timer expire. */ | |
130 | struct rte_timer *sl_next[MAX_SKIPLIST_DEPTH]; | |
131 | volatile union rte_timer_status status; /**< Status of timer. */ | |
132 | uint64_t period; /**< Period of timer (0 if not periodic). */ | |
133 | rte_timer_cb_t f; /**< Callback function. */ | |
134 | void *arg; /**< Argument to callback function. */ | |
135 | }; | |
136 | ||
137 | ||
138 | #ifdef __cplusplus | |
139 | /** | |
140 | * A C++ static initializer for a timer structure. | |
141 | */ | |
142 | #define RTE_TIMER_INITIALIZER { \ | |
143 | 0, \ | |
144 | {NULL}, \ | |
145 | {{RTE_TIMER_STOP, RTE_TIMER_NO_OWNER}}, \ | |
146 | 0, \ | |
147 | NULL, \ | |
148 | NULL, \ | |
149 | } | |
150 | #else | |
151 | /** | |
152 | * A static initializer for a timer structure. | |
153 | */ | |
154 | #define RTE_TIMER_INITIALIZER { \ | |
155 | .status = {{ \ | |
156 | .state = RTE_TIMER_STOP, \ | |
157 | .owner = RTE_TIMER_NO_OWNER, \ | |
158 | }}, \ | |
159 | } | |
160 | #endif | |
161 | ||
162 | /** | |
163 | * Initialize the timer library. | |
164 | * | |
165 | * Initializes internal variables (list, locks and so on) for the RTE | |
166 | * timer library. | |
167 | */ | |
168 | void rte_timer_subsystem_init(void); | |
169 | ||
170 | /** | |
171 | * Initialize a timer handle. | |
172 | * | |
173 | * The rte_timer_init() function initializes the timer handle *tim* | |
174 | * for use. No operations can be performed on a timer before it is | |
175 | * initialized. | |
176 | * | |
177 | * @param tim | |
178 | * The timer to initialize. | |
179 | */ | |
180 | void rte_timer_init(struct rte_timer *tim); | |
181 | ||
182 | /** | |
183 | * Reset and start the timer associated with the timer handle. | |
184 | * | |
185 | * The rte_timer_reset() function resets and starts the timer | |
186 | * associated with the timer handle *tim*. When the timer expires after | |
187 | * *ticks* HPET cycles, the function specified by *fct* will be called | |
188 | * with the argument *arg* on core *tim_lcore*. | |
189 | * | |
190 | * If the timer associated with the timer handle is already running | |
191 | * (in the RUNNING state), the function will fail. The user has to check | |
192 | * the return value of the function to see if there is a chance that the | |
193 | * timer is in the RUNNING state. | |
194 | * | |
195 | * If the timer is being configured on another core (the CONFIG state), | |
196 | * it will also fail. | |
197 | * | |
198 | * If the timer is pending or stopped, it will be rescheduled with the | |
199 | * new parameters. | |
200 | * | |
201 | * @param tim | |
202 | * The timer handle. | |
203 | * @param ticks | |
204 | * The number of cycles (see rte_get_hpet_hz()) before the callback | |
205 | * function is called. | |
206 | * @param type | |
207 | * The type can be either: | |
208 | * - PERIODICAL: The timer is automatically reloaded after execution | |
209 | * (returns to the PENDING state) | |
210 | * - SINGLE: The timer is one-shot, that is, the timer goes to a | |
211 | * STOPPED state after execution. | |
212 | * @param tim_lcore | |
213 | * The ID of the lcore where the timer callback function has to be | |
214 | * executed. If tim_lcore is LCORE_ID_ANY, the timer library will | |
215 | * launch it on a different core for each call (round-robin). | |
216 | * @param fct | |
217 | * The callback function of the timer. | |
218 | * @param arg | |
219 | * The user argument of the callback function. | |
220 | * @return | |
221 | * - 0: Success; the timer is scheduled. | |
222 | * - (-1): Timer is in the RUNNING or CONFIG state. | |
223 | */ | |
224 | int rte_timer_reset(struct rte_timer *tim, uint64_t ticks, | |
225 | enum rte_timer_type type, unsigned tim_lcore, | |
226 | rte_timer_cb_t fct, void *arg); | |
227 | ||
228 | ||
229 | /** | |
230 | * Loop until rte_timer_reset() succeeds. | |
231 | * | |
232 | * Reset and start the timer associated with the timer handle. Always | |
233 | * succeed. See rte_timer_reset() for details. | |
234 | * | |
235 | * @param tim | |
236 | * The timer handle. | |
237 | * @param ticks | |
238 | * The number of cycles (see rte_get_hpet_hz()) before the callback | |
239 | * function is called. | |
240 | * @param type | |
241 | * The type can be either: | |
242 | * - PERIODICAL: The timer is automatically reloaded after execution | |
243 | * (returns to the PENDING state) | |
244 | * - SINGLE: The timer is one-shot, that is, the timer goes to a | |
245 | * STOPPED state after execution. | |
246 | * @param tim_lcore | |
247 | * The ID of the lcore where the timer callback function has to be | |
248 | * executed. If tim_lcore is LCORE_ID_ANY, the timer library will | |
249 | * launch it on a different core for each call (round-robin). | |
250 | * @param fct | |
251 | * The callback function of the timer. | |
252 | * @param arg | |
253 | * The user argument of the callback function. | |
254 | */ | |
255 | void | |
256 | rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks, | |
257 | enum rte_timer_type type, unsigned tim_lcore, | |
258 | rte_timer_cb_t fct, void *arg); | |
259 | ||
260 | /** | |
261 | * Stop a timer. | |
262 | * | |
263 | * The rte_timer_stop() function stops the timer associated with the | |
264 | * timer handle *tim*. It may fail if the timer is currently running or | |
265 | * being configured. | |
266 | * | |
267 | * If the timer is pending or stopped (for instance, already expired), | |
268 | * the function will succeed. The timer handle tim must have been | |
269 | * initialized using rte_timer_init(), otherwise, undefined behavior | |
270 | * will occur. | |
271 | * | |
272 | * This function can be called safely from a timer callback. If it | |
273 | * succeeds, the timer is not referenced anymore by the timer library | |
274 | * and the timer structure can be freed (even in the callback | |
275 | * function). | |
276 | * | |
277 | * @param tim | |
278 | * The timer handle. | |
279 | * @return | |
280 | * - 0: Success; the timer is stopped. | |
281 | * - (-1): The timer is in the RUNNING or CONFIG state. | |
282 | */ | |
283 | int rte_timer_stop(struct rte_timer *tim); | |
284 | ||
285 | ||
286 | /** | |
287 | * Loop until rte_timer_stop() succeeds. | |
288 | * | |
289 | * After a call to this function, the timer identified by *tim* is | |
290 | * stopped. See rte_timer_stop() for details. | |
291 | * | |
292 | * @param tim | |
293 | * The timer handle. | |
294 | */ | |
295 | void rte_timer_stop_sync(struct rte_timer *tim); | |
296 | ||
297 | /** | |
298 | * Test if a timer is pending. | |
299 | * | |
300 | * The rte_timer_pending() function tests the PENDING status | |
301 | * of the timer handle *tim*. A PENDING timer is one that has been | |
302 | * scheduled and whose function has not yet been called. | |
303 | * | |
304 | * @param tim | |
305 | * The timer handle. | |
306 | * @return | |
307 | * - 0: The timer is not pending. | |
308 | * - 1: The timer is pending. | |
309 | */ | |
310 | int rte_timer_pending(struct rte_timer *tim); | |
311 | ||
312 | /** | |
313 | * Manage the timer list and execute callback functions. | |
314 | * | |
315 | * This function must be called periodically from EAL lcores | |
316 | * main_loop(). It browses the list of pending timers and runs all | |
317 | * timers that are expired. | |
318 | * | |
319 | * The precision of the timer depends on the call frequency of this | |
320 | * function. However, the more often the function is called, the more | |
321 | * CPU resources it will use. | |
322 | */ | |
323 | void rte_timer_manage(void); | |
324 | ||
325 | /** | |
326 | * Dump statistics about timers. | |
327 | * | |
328 | * @param f | |
329 | * A pointer to a file for output | |
330 | */ | |
331 | void rte_timer_dump_stats(FILE *f); | |
332 | ||
333 | #ifdef __cplusplus | |
334 | } | |
335 | #endif | |
336 | ||
337 | #endif /* _RTE_TIMER_H_ */ |