c5f936bbfbfd103d4afb9335258a6cbc43896b5f
[dpdk.git] / lib / librte_timer / rte_timer.h
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 <stdint.h>
67 #include <stddef.h>
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73 #define RTE_TIMER_STOP    0 /**< State: timer is stopped. */
74 #define RTE_TIMER_PENDING 1 /**< State: timer is scheduled. */
75 #define RTE_TIMER_RUNNING 2 /**< State: timer function is running. */
76 #define RTE_TIMER_CONFIG  3 /**< State: timer is being configured. */
77
78 #define RTE_TIMER_NO_OWNER -1 /**< Timer has no owner. */
79
80 /**
81  * Timer type: Periodic or single (one-shot).
82  */
83 enum rte_timer_type {
84         SINGLE,
85         PERIODICAL
86 };
87
88 /**
89  * Timer status: A union of the state (stopped, pending, running,
90  * config) and an owner (the id of the lcore that owns the timer).
91  */
92 union rte_timer_status {
93         struct {
94                 uint16_t state;  /**< Stop, pending, running, config. */
95                 int16_t owner;   /**< The lcore that owns the timer. */
96         };
97         uint32_t u32;            /**< To atomic-set status + owner. */
98 };
99
100 #ifdef RTE_LIBRTE_TIMER_DEBUG
101 /**
102  * A structure that stores the timer statistics (per-lcore).
103  */
104 struct rte_timer_debug_stats {
105         uint64_t reset;   /**< Number of success calls to rte_timer_reset(). */
106         uint64_t stop;    /**< Number of success calls to rte_timer_stop(). */
107         uint64_t manage;  /**< Number of calls to rte_timer_manage(). */
108         uint64_t pending; /**< Number of pending/running timers. */
109 };
110 #endif
111
112 struct rte_timer;
113
114 /**
115  * Callback function type for timer expiry.
116  */
117 typedef void (rte_timer_cb_t)(struct rte_timer *, void *);
118
119 #define MAX_SKIPLIST_DEPTH 10
120
121 /**
122  * A structure describing a timer in RTE.
123  */
124 struct rte_timer
125 {
126         uint64_t expire;       /**< Time when timer expire. */
127         struct rte_timer *sl_next[MAX_SKIPLIST_DEPTH];
128         volatile union rte_timer_status status; /**< Status of timer. */
129         uint64_t period;       /**< Period of timer (0 if not periodic). */
130         rte_timer_cb_t *f;     /**< Callback function. */
131         void *arg;             /**< Argument to callback function. */
132 };
133
134
135 #ifdef __cplusplus
136 /**
137  * A C++ static initializer for a timer structure.
138  */
139 #define RTE_TIMER_INITIALIZER {             \
140         0,                                      \
141         {NULL},                                 \
142         {{RTE_TIMER_STOP, RTE_TIMER_NO_OWNER}}, \
143         0,                                      \
144         NULL,                                   \
145         NULL,                                   \
146         }
147 #else
148 /**
149  * A static initializer for a timer structure.
150  */
151 #define RTE_TIMER_INITIALIZER {                      \
152                 .status = {{                         \
153                         .state = RTE_TIMER_STOP,     \
154                         .owner = RTE_TIMER_NO_OWNER, \
155                 }},                                  \
156         }
157 #endif
158
159 /**
160  * Initialize the timer library.
161  *
162  * Initializes internal variables (list, locks and so on) for the RTE
163  * timer library.
164  */
165 void rte_timer_subsystem_init(void);
166
167 /**
168  * Initialize a timer handle.
169  *
170  * The rte_timer_init() function initializes the timer handle *tim*
171  * for use. No operations can be performed on a timer before it is
172  * initialized.
173  *
174  * @param tim
175  *   The timer to initialize.
176  */
177 void rte_timer_init(struct rte_timer *tim);
178
179 /**
180  * Reset and start the timer associated with the timer handle.
181  *
182  * The rte_timer_reset() function resets and starts the timer
183  * associated with the timer handle *tim*. When the timer expires after
184  * *ticks* HPET cycles, the function specified by *fct* will be called
185  * with the argument *arg* on core *tim_lcore*.
186  *
187  * If the timer associated with the timer handle is already running
188  * (in the RUNNING state), the function will fail. The user has to check
189  * the return value of the function to see if there is a chance that the
190  * timer is in the RUNNING state.
191  *
192  * If the timer is being configured on another core (the CONFIG state),
193  * it will also fail.
194  *
195  * If the timer is pending or stopped, it will be rescheduled with the
196  * new parameters.
197  *
198  * @param tim
199  *   The timer handle.
200  * @param ticks
201  *   The number of cycles (see rte_get_hpet_hz()) before the callback
202  *   function is called.
203  * @param type
204  *   The type can be either:
205  *   - PERIODICAL: The timer is automatically reloaded after execution
206  *     (returns to the PENDING state)
207  *   - SINGLE: The timer is one-shot, that is, the timer goes to a
208  *     STOPPED state after execution.
209  * @param tim_lcore
210  *   The ID of the lcore where the timer callback function has to be
211  *   executed. If tim_lcore is LCORE_ID_ANY, the timer library will
212  *   launch it on a different core for each call (round-robin).
213  * @param fct
214  *   The callback function of the timer.
215  * @param arg
216  *   The user argument of the callback function.
217  * @return
218  *   - 0: Success; the timer is scheduled.
219  *   - (-1): Timer is in the RUNNING or CONFIG state.
220  */
221 int rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
222                     enum rte_timer_type type, unsigned tim_lcore,
223                     rte_timer_cb_t fct, void *arg);
224
225
226 /**
227  * Loop until rte_timer_reset() succeeds.
228  *
229  * Reset and start the timer associated with the timer handle. Always
230  * succeed. See rte_timer_reset() for details.
231  *
232  * @param tim
233  *   The timer handle.
234  * @param ticks
235  *   The number of cycles (see rte_get_hpet_hz()) before the callback
236  *   function is called.
237  * @param type
238  *   The type can be either:
239  *   - PERIODICAL: The timer is automatically reloaded after execution
240  *     (returns to the PENDING state)
241  *   - SINGLE: The timer is one-shot, that is, the timer goes to a
242  *     STOPPED state after execution.
243  * @param tim_lcore
244  *   The ID of the lcore where the timer callback function has to be
245  *   executed. If tim_lcore is LCORE_ID_ANY, the timer library will
246  *   launch it on a different core for each call (round-robin).
247  * @param fct
248  *   The callback function of the timer.
249  * @param arg
250  *   The user argument of the callback function.
251  */
252 void
253 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
254                      enum rte_timer_type type, unsigned tim_lcore,
255                      rte_timer_cb_t fct, void *arg);
256
257 /**
258  * Stop a timer.
259  *
260  * The rte_timer_stop() function stops the timer associated with the
261  * timer handle *tim*. It may fail if the timer is currently running or
262  * being configured.
263  *
264  * If the timer is pending or stopped (for instance, already expired),
265  * the function will succeed. The timer handle tim must have been
266  * initialized using rte_timer_init(), otherwise, undefined behavior
267  * will occur.
268  *
269  * This function can be called safely from a timer callback. If it
270  * succeeds, the timer is not referenced anymore by the timer library
271  * and the timer structure can be freed (even in the callback
272  * function).
273  *
274  * @param tim
275  *   The timer handle.
276  * @return
277  *   - 0: Success; the timer is stopped.
278  *   - (-1): The timer is in the RUNNING or CONFIG state.
279  */
280 int rte_timer_stop(struct rte_timer *tim);
281
282
283 /**
284  * Loop until rte_timer_stop() succeeds.
285  *
286  * After a call to this function, the timer identified by *tim* is
287  * stopped. See rte_timer_stop() for details.
288  *
289  * @param tim
290  *   The timer handle.
291  */
292 void rte_timer_stop_sync(struct rte_timer *tim);
293
294 /**
295  * Test if a timer is pending.
296  *
297  * The rte_timer_pending() function tests the PENDING status
298  * of the timer handle *tim*. A PENDING timer is one that has been
299  * scheduled and whose function has not yet been called.
300  *
301  * @param tim
302  *   The timer handle.
303  * @return
304  *   - 0: The timer is not pending.
305  *   - 1: The timer is pending.
306  */
307 int rte_timer_pending(struct rte_timer *tim);
308
309 /**
310  * Manage the timer list and execute callback functions.
311  *
312  * This function must be called periodically from all cores
313  * main_loop(). It browses the list of pending timers and runs all
314  * timers that are expired.
315  *
316  * The precision of the timer depends on the call frequency of this
317  * function. However, the more often the function is called, the more
318  * CPU resources it will use.
319  */
320 void rte_timer_manage(void);
321
322 /**
323  * Dump statistics about timers.
324  */
325 void rte_timer_dump_stats(void);
326
327 #ifdef __cplusplus
328 }
329 #endif
330
331 #endif /* _RTE_TIMER_H_ */