DPDK 22.11.7
Data Structures | Macros | Typedefs | Enumerations | Functions
rte_timer.h File Reference
#include <stdio.h>
#include <stdint.h>
#include <rte_compat.h>
#include <rte_common.h>
#include <rte_spinlock.h>

Go to the source code of this file.

Data Structures

union  rte_timer_status
 
struct  rte_timer
 

Macros

#define RTE_TIMER_STOP   0
 
#define RTE_TIMER_PENDING   1
 
#define RTE_TIMER_RUNNING   2
 
#define RTE_TIMER_CONFIG   3
 
#define RTE_TIMER_NO_OWNER   -2
 
#define RTE_TIMER_INITIALIZER
 

Typedefs

typedef void(* rte_timer_cb_t) (struct rte_timer *, void *)
 
typedef void(* rte_timer_alt_manage_cb_t) (struct rte_timer *tim)
 
typedef void(* rte_timer_stop_all_cb_t) (struct rte_timer *tim, void *arg)
 

Enumerations

enum  rte_timer_type
 

Functions

int rte_timer_data_alloc (uint32_t *id_ptr)
 
int rte_timer_data_dealloc (uint32_t id)
 
int rte_timer_subsystem_init (void)
 
void rte_timer_subsystem_finalize (void)
 
void rte_timer_init (struct rte_timer *tim)
 
int rte_timer_reset (struct rte_timer *tim, uint64_t ticks, enum rte_timer_type type, unsigned tim_lcore, rte_timer_cb_t fct, void *arg)
 
void rte_timer_reset_sync (struct rte_timer *tim, uint64_t ticks, enum rte_timer_type type, unsigned tim_lcore, rte_timer_cb_t fct, void *arg)
 
int rte_timer_stop (struct rte_timer *tim)
 
void rte_timer_stop_sync (struct rte_timer *tim)
 
int rte_timer_pending (struct rte_timer *tim)
 
__rte_experimental int64_t rte_timer_next_ticks (void)
 
int rte_timer_manage (void)
 
int rte_timer_dump_stats (FILE *f)
 
int rte_timer_alt_reset (uint32_t timer_data_id, struct rte_timer *tim, uint64_t ticks, enum rte_timer_type type, unsigned int tim_lcore, rte_timer_cb_t fct, void *arg)
 
int rte_timer_alt_stop (uint32_t timer_data_id, struct rte_timer *tim)
 
int rte_timer_alt_manage (uint32_t timer_data_id, unsigned int *poll_lcores, int n_poll_lcores, rte_timer_alt_manage_cb_t f)
 
int rte_timer_stop_all (uint32_t timer_data_id, unsigned int *walk_lcores, int nb_walk_lcores, rte_timer_stop_all_cb_t f, void *f_arg)
 
int rte_timer_alt_dump_stats (uint32_t timer_data_id, FILE *f)
 

Detailed Description

RTE Timer

This library provides a timer service to RTE Data Plane execution units that allows the execution of callback functions asynchronously.

The timer library uses the rte_get_hpet_cycles() function that uses the HPET, when available, to provide a reliable time reference. [HPET routines are provided by EAL, which falls back to using the chip TSC (time- stamp counter) as fallback when HPET is not available]

This library provides an interface to add, delete and restart a timer. The API is based on the BSD callout(9) API with a few differences.

See the RTE architecture documentation for more information about the design of this library.

Definition in file rte_timer.h.

Macro Definition Documentation

◆ RTE_TIMER_STOP

#define RTE_TIMER_STOP   0

State: timer is stopped.

Definition at line 47 of file rte_timer.h.

◆ RTE_TIMER_PENDING

#define RTE_TIMER_PENDING   1

State: timer is scheduled.

Definition at line 48 of file rte_timer.h.

◆ RTE_TIMER_RUNNING

#define RTE_TIMER_RUNNING   2

State: timer function is running.

Definition at line 49 of file rte_timer.h.

◆ RTE_TIMER_CONFIG

#define RTE_TIMER_CONFIG   3

State: timer is being configured.

Definition at line 50 of file rte_timer.h.

◆ RTE_TIMER_NO_OWNER

#define RTE_TIMER_NO_OWNER   -2

Timer has no owner.

Definition at line 52 of file rte_timer.h.

◆ RTE_TIMER_INITIALIZER

#define RTE_TIMER_INITIALIZER
Value:
{ \
.status = {{ \
.state = RTE_TIMER_STOP, \
.owner = RTE_TIMER_NO_OWNER, \
}}, \
}
#define RTE_TIMER_STOP
Definition: rte_timer.h:47
#define RTE_TIMER_NO_OWNER
Definition: rte_timer.h:52

A static initializer for a timer structure.

Definition at line 126 of file rte_timer.h.

Typedef Documentation

◆ rte_timer_cb_t

typedef void(* rte_timer_cb_t) (struct rte_timer *, void *)

Callback function type for timer expiry.

Definition at line 92 of file rte_timer.h.

◆ rte_timer_alt_manage_cb_t

typedef void(* rte_timer_alt_manage_cb_t) (struct rte_timer *tim)

Callback function type for rte_timer_alt_manage().

Definition at line 441 of file rte_timer.h.

◆ rte_timer_stop_all_cb_t

typedef void(* rte_timer_stop_all_cb_t) (struct rte_timer *tim, void *arg)

Callback function type for rte_timer_stop_all().

Definition at line 475 of file rte_timer.h.

Enumeration Type Documentation

◆ rte_timer_type

Timer type: Periodic or single (one-shot).

Definition at line 57 of file rte_timer.h.

Function Documentation

◆ rte_timer_data_alloc()

int rte_timer_data_alloc ( uint32_t *  id_ptr)

Allocate a timer data instance in shared memory to track a set of pending timer lists.

Parameters
id_ptrPointer to variable into which to write the identifier of the allocated timer data instance.
Returns
  • 0: Success
  • -ENOSPC: maximum number of timer data instances already allocated

◆ rte_timer_data_dealloc()

int rte_timer_data_dealloc ( uint32_t  id)

Deallocate a timer data instance.

Parameters
idIdentifier of the timer data instance to deallocate.
Returns
  • 0: Success
  • -EINVAL: invalid timer data instance identifier

◆ rte_timer_subsystem_init()

int rte_timer_subsystem_init ( void  )

Initialize the timer library.

Initializes internal variables (list, locks and so on) for the RTE timer library.

Note
This function must be called in every process before using the library.
Returns
  • 0: Success
  • -ENOMEM: Unable to allocate memory needed to initialize timer subsystem
  • -EALREADY: timer subsystem was already initialized. Not an error.
Examples
examples/l2fwd-jobstats/main.c, examples/l2fwd-keepalive/main.c, examples/l3fwd-power/main.c, and examples/timer/main.c.

◆ rte_timer_subsystem_finalize()

void rte_timer_subsystem_finalize ( void  )

Free timer subsystem resources.

◆ rte_timer_init()

void rte_timer_init ( struct rte_timer tim)

Initialize a timer handle.

The rte_timer_init() function initializes the timer handle tim for use. No operations can be performed on a timer before it is initialized.

Parameters
timThe timer to initialize.
Examples
examples/l2fwd-jobstats/main.c, examples/l2fwd-keepalive/main.c, examples/l3fwd-power/main.c, and examples/timer/main.c.

◆ rte_timer_reset()

int rte_timer_reset ( struct rte_timer tim,
uint64_t  ticks,
enum rte_timer_type  type,
unsigned  tim_lcore,
rte_timer_cb_t  fct,
void *  arg 
)

Reset and start the timer associated with the timer handle.

The rte_timer_reset() function resets and starts the timer associated with the timer handle tim. When the timer expires after ticks HPET cycles, the function specified by fct will be called with the argument arg on core tim_lcore.

If the timer associated with the timer handle is already running (in the RUNNING state), the function will fail. The user has to check the return value of the function to see if there is a chance that the timer is in the RUNNING state.

If the timer is being configured on another core (the CONFIG state), it will also fail.

If the timer is pending or stopped, it will be rescheduled with the new parameters.

Parameters
timThe timer handle.
ticksThe number of cycles (see rte_get_hpet_hz()) before the callback function is called.
typeThe type can be either:
  • PERIODICAL: The timer is automatically reloaded after execution (returns to the PENDING state)
  • SINGLE: The timer is one-shot, that is, the timer goes to a STOPPED state after execution.
tim_lcoreThe ID of the lcore where the timer callback function has to be executed. If tim_lcore is LCORE_ID_ANY, the timer library will launch it on a different core for each call (round-robin).
fctThe callback function of the timer.
argThe user argument of the callback function.
Returns
  • 0: Success; the timer is scheduled.
  • (-1): Timer is in the RUNNING or CONFIG state.
Examples
examples/l2fwd-jobstats/main.c, examples/l2fwd-keepalive/main.c, examples/l3fwd-power/main.c, and examples/timer/main.c.

◆ rte_timer_reset_sync()

void rte_timer_reset_sync ( struct rte_timer tim,
uint64_t  ticks,
enum rte_timer_type  type,
unsigned  tim_lcore,
rte_timer_cb_t  fct,
void *  arg 
)

Loop until rte_timer_reset() succeeds.

Reset and start the timer associated with the timer handle. Always succeed. See rte_timer_reset() for details.

Parameters
timThe timer handle.
ticksThe number of cycles (see rte_get_hpet_hz()) before the callback function is called.
typeThe type can be either:
  • PERIODICAL: The timer is automatically reloaded after execution (returns to the PENDING state)
  • SINGLE: The timer is one-shot, that is, the timer goes to a STOPPED state after execution.
tim_lcoreThe ID of the lcore where the timer callback function has to be executed. If tim_lcore is LCORE_ID_ANY, the timer library will launch it on a different core for each call (round-robin).
fctThe callback function of the timer.
argThe user argument of the callback function.
Note
This API should not be called inside a timer's callback function to reset another timer; doing so could hang in certain scenarios. Instead, the rte_timer_reset() API can be called directly and its return code can be checked for success or failure.
Examples
examples/l3fwd-power/main.c.

◆ rte_timer_stop()

int rte_timer_stop ( struct rte_timer tim)

Stop a timer.

The rte_timer_stop() function stops the timer associated with the timer handle tim. It may fail if the timer is currently running or being configured.

If the timer is pending or stopped (for instance, already expired), the function will succeed. The timer handle tim must have been initialized using rte_timer_init(), otherwise, undefined behavior will occur.

This function can be called safely from a timer callback. If it succeeds, the timer is not referenced anymore by the timer library and the timer structure can be freed (even in the callback function).

Parameters
timThe timer handle.
Returns
  • 0: Success; the timer is stopped.
  • (-1): The timer is in the RUNNING or CONFIG state.
Examples
examples/timer/main.c.

◆ rte_timer_stop_sync()

void rte_timer_stop_sync ( struct rte_timer tim)

Loop until rte_timer_stop() succeeds.

After a call to this function, the timer identified by tim is stopped. See rte_timer_stop() for details.

Parameters
timThe timer handle.
Note
This API should not be called inside a timer's callback function to stop another timer; doing so could hang in certain scenarios. Instead, the rte_timer_stop() API can be called directly and its return code can be checked for success or failure.

◆ rte_timer_pending()

int rte_timer_pending ( struct rte_timer tim)

Test if a timer is pending.

The rte_timer_pending() function tests the PENDING status of the timer handle tim. A PENDING timer is one that has been scheduled and whose function has not yet been called.

Parameters
timThe timer handle.
Returns
  • 0: The timer is not pending.
  • 1: The timer is pending.

◆ rte_timer_next_ticks()

__rte_experimental int64_t rte_timer_next_ticks ( void  )
Warning
EXPERIMENTAL: this API may change without prior notice

Time until the next timer on the current lcore This function gives the ticks until the next timer will be active.

Returns
  • -EINVAL: invalid timer data instance identifier
  • -ENOENT: no timer pending
  • 0: a timer is pending and will run at next rte_timer_manage()
  • >0: ticks until the next timer is ready

◆ rte_timer_manage()

int rte_timer_manage ( void  )

Manage the timer list and execute callback functions.

This function must be called periodically from EAL lcores main_loop(). It browses the list of pending timers and runs all timers that are expired.

The precision of the timer depends on the call frequency of this function. However, the more often the function is called, the more CPU resources it will use.

Returns
  • 0: Success
  • -EINVAL: timer subsystem not yet initialized
Examples
examples/l2fwd-jobstats/main.c, examples/l2fwd-keepalive/main.c, examples/l3fwd-power/main.c, and examples/timer/main.c.

◆ rte_timer_dump_stats()

int rte_timer_dump_stats ( FILE *  f)

Dump statistics about timers.

Parameters
fA pointer to a file for output
Returns
  • 0: Success
  • -EINVAL: timer subsystem not yet initialized

◆ rte_timer_alt_reset()

int rte_timer_alt_reset ( uint32_t  timer_data_id,
struct rte_timer tim,
uint64_t  ticks,
enum rte_timer_type  type,
unsigned int  tim_lcore,
rte_timer_cb_t  fct,
void *  arg 
)

This function is the same as rte_timer_reset(), except that it allows a caller to specify the rte_timer_data instance containing the list to which the timer should be added.

See also
rte_timer_reset()
Parameters
timer_data_idAn identifier indicating which instance of timer data should be used for this operation.
timThe timer handle.
ticksThe number of cycles (see rte_get_hpet_hz()) before the callback function is called.
typeThe type can be either:
  • PERIODICAL: The timer is automatically reloaded after execution (returns to the PENDING state)
  • SINGLE: The timer is one-shot, that is, the timer goes to a STOPPED state after execution.
tim_lcoreThe ID of the lcore where the timer callback function has to be executed. If tim_lcore is LCORE_ID_ANY, the timer library will launch it on a different core for each call (round-robin).
fctThe callback function of the timer. This parameter can be NULL if (and only if) rte_timer_alt_manage() will be used to manage this timer.
argThe user argument of the callback function.
Returns
  • 0: Success; the timer is scheduled.
  • (-1): Timer is in the RUNNING or CONFIG state.
  • -EINVAL: invalid timer_data_id

◆ rte_timer_alt_stop()

int rte_timer_alt_stop ( uint32_t  timer_data_id,
struct rte_timer tim 
)

This function is the same as rte_timer_stop(), except that it allows a caller to specify the rte_timer_data instance containing the list from which this timer should be removed.

See also
rte_timer_stop()
Parameters
timer_data_idAn identifier indicating which instance of timer data should be used for this operation.
timThe timer handle.
Returns
  • 0: Success; the timer is stopped.
  • (-1): The timer is in the RUNNING or CONFIG state.
  • -EINVAL: invalid timer_data_id

◆ rte_timer_alt_manage()

int rte_timer_alt_manage ( uint32_t  timer_data_id,
unsigned int *  poll_lcores,
int  n_poll_lcores,
rte_timer_alt_manage_cb_t  f 
)

Manage a set of timer lists and execute the specified callback function for all expired timers. This function is similar to rte_timer_manage(), except that it allows a caller to specify the timer_data instance that should be operated on, as well as a set of lcore IDs identifying which timer lists should be processed. Callback functions of individual timers are ignored.

See also
rte_timer_manage()
Parameters
timer_data_idAn identifier indicating which instance of timer data should be used for this operation.
poll_lcoresAn array of lcore ids identifying the timer lists that should be processed. NULL is allowed - if NULL, the timer list corresponding to the lcore calling this routine is processed (same as rte_timer_manage()).
n_poll_lcoresThe size of the poll_lcores array. If 'poll_lcores' is NULL, this parameter is ignored.
fThe callback function which should be called for all expired timers.
Returns
  • 0: success
  • -EINVAL: invalid timer_data_id

◆ rte_timer_stop_all()

int rte_timer_stop_all ( uint32_t  timer_data_id,
unsigned int *  walk_lcores,
int  nb_walk_lcores,
rte_timer_stop_all_cb_t  f,
void *  f_arg 
)

Walk the pending timer lists for the specified lcore IDs, and for each timer that is encountered, stop it and call the specified callback function to process it further.

Parameters
timer_data_idAn identifier indicating which instance of timer data should be used for this operation.
walk_lcoresAn array of lcore ids identifying the timer lists that should be processed.
nb_walk_lcoresThe size of the walk_lcores array.
fThe callback function which should be called for each timers. Can be NULL.
f_argAn arbitrary argument that will be passed to f, if it is called.
Returns
  • 0: success
  • EINVAL: invalid timer_data_id

◆ rte_timer_alt_dump_stats()

int rte_timer_alt_dump_stats ( uint32_t  timer_data_id,
FILE *  f 
)

This function is the same as rte_timer_dump_stats(), except that it allows the caller to specify the rte_timer_data instance that should be used.

See also
rte_timer_dump_stats()
Parameters
timer_data_idAn identifier indicating which instance of timer data should be used for this operation.
fA pointer to a file for output
Returns
  • 0: success
  • -EINVAL: invalid timer_data_id