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