1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 * This library provides a timer service to RTE Data Plane execution
13 * units that allows the execution of callback functions asynchronously.
15 * - Timers can be periodic or single (one-shot).
16 * - The timers can be loaded from one core and executed on another. This has
17 * to be specified in the call to rte_timer_reset().
18 * - High precision is possible. NOTE: this depends on the call frequency to
19 * rte_timer_manage() that check the timer expiration for the local core.
20 * - If not used in an application, for improved performance, it can be
21 * disabled at compilation time by not calling the rte_timer_manage()
22 * to improve performance.
24 * The timer library uses the rte_get_hpet_cycles() function that
25 * uses the HPET, when available, to provide a reliable time reference. [HPET
26 * routines are provided by EAL, which falls back to using the chip TSC (time-
27 * stamp counter) as fallback when HPET is not available]
29 * This library provides an interface to add, delete and restart a
30 * timer. The API is based on the BSD callout(9) API with a few
33 * See the RTE architecture documentation for more information about the
34 * design of this library.
40 #include <rte_common.h>
41 #include <rte_config.h>
42 #include <rte_spinlock.h>
48 #define RTE_TIMER_STOP 0 /**< State: timer is stopped. */
49 #define RTE_TIMER_PENDING 1 /**< State: timer is scheduled. */
50 #define RTE_TIMER_RUNNING 2 /**< State: timer function is running. */
51 #define RTE_TIMER_CONFIG 3 /**< State: timer is being configured. */
53 #define RTE_TIMER_NO_OWNER -2 /**< Timer has no owner. */
56 * Timer type: Periodic or single (one-shot).
64 * Timer status: A union of the state (stopped, pending, running,
65 * config) and an owner (the id of the lcore that owns the timer).
67 union rte_timer_status {
70 uint16_t state; /**< Stop, pending, running, config. */
71 int16_t owner; /**< The lcore that owns the timer. */
73 uint32_t u32; /**< To atomic-set status + owner. */
76 #ifdef RTE_LIBRTE_TIMER_DEBUG
78 * A structure that stores the timer statistics (per-lcore).
80 struct rte_timer_debug_stats {
81 uint64_t reset; /**< Number of success calls to rte_timer_reset(). */
82 uint64_t stop; /**< Number of success calls to rte_timer_stop(). */
83 uint64_t manage; /**< Number of calls to rte_timer_manage(). */
84 uint64_t pending; /**< Number of pending/running timers. */
91 * Callback function type for timer expiry.
93 typedef void (*rte_timer_cb_t)(struct rte_timer *, void *);
95 #define MAX_SKIPLIST_DEPTH 10
98 * A structure describing a timer in RTE.
102 uint64_t expire; /**< Time when timer expire. */
103 struct rte_timer *sl_next[MAX_SKIPLIST_DEPTH];
104 volatile union rte_timer_status status; /**< Status of timer. */
105 uint64_t period; /**< Period of timer (0 if not periodic). */
106 rte_timer_cb_t f; /**< Callback function. */
107 void *arg; /**< Argument to callback function. */
113 * A C++ static initializer for a timer structure.
115 #define RTE_TIMER_INITIALIZER { \
118 {{RTE_TIMER_STOP, RTE_TIMER_NO_OWNER}}, \
125 * A static initializer for a timer structure.
127 #define RTE_TIMER_INITIALIZER { \
129 .state = RTE_TIMER_STOP, \
130 .owner = RTE_TIMER_NO_OWNER, \
137 * @b EXPERIMENTAL: this API may change without prior notice
139 * Allocate a timer data instance in shared memory to track a set of pending
143 * Pointer to variable into which to write the identifier of the allocated
144 * timer data instance.
148 * - -ENOSPC: maximum number of timer data instances already allocated
151 int rte_timer_data_alloc(uint32_t *id_ptr);
155 * @b EXPERIMENTAL: this API may change without prior notice
157 * Deallocate a timer data instance.
160 * Identifier of the timer data instance to deallocate.
164 * - -EINVAL: invalid timer data instance identifier
167 int rte_timer_data_dealloc(uint32_t id);
170 * Initialize the timer library.
172 * Initializes internal variables (list, locks and so on) for the RTE
176 * This function must be called in every process before using the library.
180 * - -ENOMEM: Unable to allocate memory needed to initialize timer
183 int rte_timer_subsystem_init(void);
187 * @b EXPERIMENTAL: this API may change without prior notice
189 * Free timer subsystem resources.
192 void rte_timer_subsystem_finalize(void);
195 * Initialize a timer handle.
197 * The rte_timer_init() function initializes the timer handle *tim*
198 * for use. No operations can be performed on a timer before it is
202 * The timer to initialize.
204 void rte_timer_init(struct rte_timer *tim);
207 * Reset and start the timer associated with the timer handle.
209 * The rte_timer_reset() function resets and starts the timer
210 * associated with the timer handle *tim*. When the timer expires after
211 * *ticks* HPET cycles, the function specified by *fct* will be called
212 * with the argument *arg* on core *tim_lcore*.
214 * If the timer associated with the timer handle is already running
215 * (in the RUNNING state), the function will fail. The user has to check
216 * the return value of the function to see if there is a chance that the
217 * timer is in the RUNNING state.
219 * If the timer is being configured on another core (the CONFIG state),
222 * If the timer is pending or stopped, it will be rescheduled with the
228 * The number of cycles (see rte_get_hpet_hz()) before the callback
229 * function is called.
231 * The type can be either:
232 * - PERIODICAL: The timer is automatically reloaded after execution
233 * (returns to the PENDING state)
234 * - SINGLE: The timer is one-shot, that is, the timer goes to a
235 * STOPPED state after execution.
237 * The ID of the lcore where the timer callback function has to be
238 * executed. If tim_lcore is LCORE_ID_ANY, the timer library will
239 * launch it on a different core for each call (round-robin).
241 * The callback function of the timer.
243 * The user argument of the callback function.
245 * - 0: Success; the timer is scheduled.
246 * - (-1): Timer is in the RUNNING or CONFIG state.
248 int rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
249 enum rte_timer_type type, unsigned tim_lcore,
250 rte_timer_cb_t fct, void *arg);
253 * Loop until rte_timer_reset() succeeds.
255 * Reset and start the timer associated with the timer handle. Always
256 * succeed. See rte_timer_reset() for details.
261 * The number of cycles (see rte_get_hpet_hz()) before the callback
262 * function is called.
264 * The type can be either:
265 * - PERIODICAL: The timer is automatically reloaded after execution
266 * (returns to the PENDING state)
267 * - SINGLE: The timer is one-shot, that is, the timer goes to a
268 * STOPPED state after execution.
270 * The ID of the lcore where the timer callback function has to be
271 * executed. If tim_lcore is LCORE_ID_ANY, the timer library will
272 * launch it on a different core for each call (round-robin).
274 * The callback function of the timer.
276 * The user argument of the callback function.
279 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
280 enum rte_timer_type type, unsigned tim_lcore,
281 rte_timer_cb_t fct, void *arg);
286 * The rte_timer_stop() function stops the timer associated with the
287 * timer handle *tim*. It may fail if the timer is currently running or
290 * If the timer is pending or stopped (for instance, already expired),
291 * the function will succeed. The timer handle tim must have been
292 * initialized using rte_timer_init(), otherwise, undefined behavior
295 * This function can be called safely from a timer callback. If it
296 * succeeds, the timer is not referenced anymore by the timer library
297 * and the timer structure can be freed (even in the callback
303 * - 0: Success; the timer is stopped.
304 * - (-1): The timer is in the RUNNING or CONFIG state.
306 int rte_timer_stop(struct rte_timer *tim);
309 * Loop until rte_timer_stop() succeeds.
311 * After a call to this function, the timer identified by *tim* is
312 * stopped. See rte_timer_stop() for details.
317 void rte_timer_stop_sync(struct rte_timer *tim);
320 * Test if a timer is pending.
322 * The rte_timer_pending() function tests the PENDING status
323 * of the timer handle *tim*. A PENDING timer is one that has been
324 * scheduled and whose function has not yet been called.
329 * - 0: The timer is not pending.
330 * - 1: The timer is pending.
332 int rte_timer_pending(struct rte_timer *tim);
336 * @b EXPERIMENTAL: this API may change without prior notice
338 * Time until the next timer on the current lcore
339 * This function gives the ticks until the next timer will be active.
342 * - -EINVAL: invalid timer data instance identifier
343 * - -ENOENT: no timer pending
344 * - 0: a timer is pending and will run at next rte_timer_manage()
345 * - >0: ticks until the next timer is ready
348 int64_t rte_timer_next_ticks(void);
351 * Manage the timer list and execute callback functions.
353 * This function must be called periodically from EAL lcores
354 * main_loop(). It browses the list of pending timers and runs all
355 * timers that are expired.
357 * The precision of the timer depends on the call frequency of this
358 * function. However, the more often the function is called, the more
359 * CPU resources it will use.
363 * - -EINVAL: timer subsystem not yet initialized
365 int rte_timer_manage(void);
368 * Dump statistics about timers.
371 * A pointer to a file for output
374 * - -EINVAL: timer subsystem not yet initialized
376 int rte_timer_dump_stats(FILE *f);
380 * @b EXPERIMENTAL: this API may change without prior notice
382 * This function is the same as rte_timer_reset(), except that it allows a
383 * caller to specify the rte_timer_data instance containing the list to which
384 * the timer should be added.
386 * @see rte_timer_reset()
388 * @param timer_data_id
389 * An identifier indicating which instance of timer data should be used for
394 * The number of cycles (see rte_get_hpet_hz()) before the callback
395 * function is called.
397 * The type can be either:
398 * - PERIODICAL: The timer is automatically reloaded after execution
399 * (returns to the PENDING state)
400 * - SINGLE: The timer is one-shot, that is, the timer goes to a
401 * STOPPED state after execution.
403 * The ID of the lcore where the timer callback function has to be
404 * executed. If tim_lcore is LCORE_ID_ANY, the timer library will
405 * launch it on a different core for each call (round-robin).
407 * The callback function of the timer. This parameter can be NULL if (and
408 * only if) rte_timer_alt_manage() will be used to manage this timer.
410 * The user argument of the callback function.
412 * - 0: Success; the timer is scheduled.
413 * - (-1): Timer is in the RUNNING or CONFIG state.
414 * - -EINVAL: invalid timer_data_id
418 rte_timer_alt_reset(uint32_t timer_data_id, struct rte_timer *tim,
419 uint64_t ticks, enum rte_timer_type type,
420 unsigned int tim_lcore, rte_timer_cb_t fct, void *arg);
424 * @b EXPERIMENTAL: this API may change without prior notice
426 * This function is the same as rte_timer_stop(), except that it allows a
427 * caller to specify the rte_timer_data instance containing the list from which
428 * this timer should be removed.
430 * @see rte_timer_stop()
432 * @param timer_data_id
433 * An identifier indicating which instance of timer data should be used for
438 * - 0: Success; the timer is stopped.
439 * - (-1): The timer is in the RUNNING or CONFIG state.
440 * - -EINVAL: invalid timer_data_id
444 rte_timer_alt_stop(uint32_t timer_data_id, struct rte_timer *tim);
447 * Callback function type for rte_timer_alt_manage().
449 typedef void (*rte_timer_alt_manage_cb_t)(struct rte_timer *tim);
453 * @b EXPERIMENTAL: this API may change without prior notice
455 * Manage a set of timer lists and execute the specified callback function for
456 * all expired timers. This function is similar to rte_timer_manage(), except
457 * that it allows a caller to specify the timer_data instance that should
458 * be operated on, as well as a set of lcore IDs identifying which timer lists
459 * should be processed. Callback functions of individual timers are ignored.
461 * @see rte_timer_manage()
463 * @param timer_data_id
464 * An identifier indicating which instance of timer data should be used for
467 * An array of lcore ids identifying the timer lists that should be processed.
468 * NULL is allowed - if NULL, the timer list corresponding to the lcore
469 * calling this routine is processed (same as rte_timer_manage()).
470 * @param n_poll_lcores
471 * The size of the poll_lcores array. If 'poll_lcores' is NULL, this parameter
474 * The callback function which should be called for all expired timers.
477 * - -EINVAL: invalid timer_data_id
481 rte_timer_alt_manage(uint32_t timer_data_id, unsigned int *poll_lcores,
482 int n_poll_lcores, rte_timer_alt_manage_cb_t f);
485 * Callback function type for rte_timer_stop_all().
487 typedef void (*rte_timer_stop_all_cb_t)(struct rte_timer *tim, void *arg);
491 * @b EXPERIMENTAL: this API may change without prior notice
493 * Walk the pending timer lists for the specified lcore IDs, and for each timer
494 * that is encountered, stop it and call the specified callback function to
495 * process it further.
497 * @param timer_data_id
498 * An identifier indicating which instance of timer data should be used for
501 * An array of lcore ids identifying the timer lists that should be processed.
502 * @param nb_walk_lcores
503 * The size of the walk_lcores array.
505 * The callback function which should be called for each timers. Can be NULL.
507 * An arbitrary argument that will be passed to f, if it is called.
510 * - EINVAL: invalid timer_data_id
514 rte_timer_stop_all(uint32_t timer_data_id, unsigned int *walk_lcores,
515 int nb_walk_lcores, rte_timer_stop_all_cb_t f, void *f_arg);
519 * @b EXPERIMENTAL: this API may change without prior notice
521 * This function is the same as rte_timer_dump_stats(), except that it allows
522 * the caller to specify the rte_timer_data instance that should be used.
524 * @see rte_timer_dump_stats()
526 * @param timer_data_id
527 * An identifier indicating which instance of timer data should be used for
530 * A pointer to a file for output
533 * - -EINVAL: invalid timer_data_id
537 rte_timer_alt_dump_stats(uint32_t timer_data_id, FILE *f);
543 #endif /* _RTE_TIMER_H_ */