---
name: "rte_timer.h"
section: "3"
platform: "linux"
package: "dpdk-doc"
version: "24.11.4-0+deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/rte_timer.h"
---

# rte\_timer.h(3)

## NAME

rte\_timer.h

## SYNOPSIS

`#include <stdio.h>`\
 `#include <stdint.h>`\
 `#include <rte_common.h>`\
 `#include <rte_spinlock.h>`\

### 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)\
 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.

- •

  Timers can be periodic or single (one-shot).

- •

  The timers can be loaded from one core and executed on another. This has to be specified in the call to **rte\_timer\_reset()**.

- •

  High precision is possible. NOTE: this depends on the call frequency to **rte\_timer\_manage()** that check the timer expiration for the local core.

- •

  If not used in an application, for improved performance, it can be disabled at compilation time by not calling the **rte\_timer\_manage()** to improve performance.

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)](/callout/9.md) 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

### #define RTE\_TIMER\_STOP 0

State: timer is stopped.

Definition at line **47** of file **rte\_timer.h**.

### #define RTE\_TIMER\_PENDING 1

State: timer is scheduled.

Definition at line **48** of file **rte\_timer.h**.

### #define RTE\_TIMER\_RUNNING 2

State: timer function is running.

Definition at line **49** of file **rte\_timer.h**.

### #define RTE\_TIMER\_CONFIG 3

State: timer is being configured.

Definition at line **50** of file **rte\_timer.h**.

### #define RTE\_TIMER\_NO\_OWNER -2

Timer has no owner.

Definition at line **52** of file **rte\_timer.h**.

### #define RTE\_TIMER\_INITIALIZER

**Value:**.PP

```
        {                      \
        .status = {{                         \
            .state = RTE_TIMER_STOP,     \
            .owner = RTE_TIMER_NO_OWNER, \
        }},                                  \
    }
```

A static initializer for a timer structure.

Definition at line **125** of file **rte\_timer.h**.

## Typedef Documentation

### typedef void(\* rte\_timer\_cb\_t) (struct rte\_timer \*, void \*)

Callback function type for timer expiry.

Definition at line **91** of file **rte\_timer.h**.

### typedef void(\* rte\_timer\_alt\_manage\_cb\_t) (struct rte\_timer \*tim)

Callback function type for **rte\_timer\_alt\_manage()**.

Definition at line **436** of file **rte\_timer.h**.

### 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 **470** of file **rte\_timer.h**.

## Enumeration Type Documentation

### enum rte\_timer\_type

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

Definition at line **57** of file **rte\_timer.h**.

## Function Documentation

### 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\_ptr* Pointer 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

### int rte\_timer\_data\_dealloc (uint32\_t id)

Deallocate a timer data instance.

**Parameters**

*id* Identifier of the timer data instance to deallocate.

**Returns**

- •

  0: Success

- •

  \-EINVAL: invalid timer data instance identifier

### 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.

### void rte\_timer\_subsystem\_finalize (void)

Free timer subsystem resources.

### 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**

*tim* The timer to initialize.

### 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**

*tim* The timer handle. \
 *ticks* The number of cycles (see rte\_get\_hpet\_hz()) before the callback function is called. \
 *type* The 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\_lcore* The 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). \
 *fct* The callback function of the timer. \
 *arg* The user argument of the callback function.

**Returns**

- •

  0: Success; the timer is scheduled.

- •

  (-1): Timer is in the RUNNING or CONFIG state.

### 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**

*tim* The timer handle. \
 *ticks* The number of cycles (see rte\_get\_hpet\_hz()) before the callback function is called. \
 *type* The 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\_lcore* The 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). \
 *fct* The callback function of the timer. \
 *arg* The 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.

### 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**

*tim* The timer handle.

**Returns**

- •

  0: Success; the timer is stopped.

- •

  (-1): The timer is in the RUNNING or CONFIG state.

### 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**

*tim* The 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.

### 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**

*tim* The timer handle.

**Returns**

- •

  0: The timer is not pending.

- •

  1: The timer is pending.

### int64\_t rte\_timer\_next\_ticks (void)

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

### 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

### int rte\_timer\_dump\_stats (FILE \* f)

Dump statistics about timers.

**Parameters**

*f* A pointer to a file for output

**Returns**

- •

  0: Success

- •

  \-EINVAL: timer subsystem not yet initialized

### 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\_id* An identifier indicating which instance of timer data should be used for this operation. \
 *tim* The timer handle. \
 *ticks* The number of cycles (see rte\_get\_hpet\_hz()) before the callback function is called. \
 *type* The 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\_lcore* The 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). \
 *fct* The 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. \
 *arg* The 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

### 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\_id* An identifier indicating which instance of timer data should be used for this operation. \
 *tim* The timer handle.

**Returns**

- •

  0: Success; the timer is stopped.

- •

  (-1): The timer is in the RUNNING or CONFIG state.

- •

  \-EINVAL: invalid timer\_data\_id

### 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\_id* An identifier indicating which instance of timer data should be used for this operation. \
 *poll\_lcores* An 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\_lcores* The size of the poll\_lcores array. If 'poll\_lcores' is NULL, this parameter is ignored. \
 *f* The callback function which should be called for all expired timers.

**Returns**

- •

  0: success

- •

  \-EINVAL: invalid timer\_data\_id

### 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\_id* An identifier indicating which instance of timer data should be used for this operation. \
 *walk\_lcores* An array of lcore ids identifying the timer lists that should be processed. \
 *nb\_walk\_lcores* The size of the walk\_lcores array. \
 *f* The callback function which should be called for each timers. Can be NULL. \
 *f\_arg* An arbitrary argument that will be passed to f, if it is called.

**Returns**

- •

  0: success

- •

  EINVAL: invalid timer\_data\_id

### 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\_id* An identifier indicating which instance of timer data should be used for this operation. \
 *f* A pointer to a file for output

**Returns**

- •

  0: success

- •

  \-EINVAL: invalid timer\_data\_id

## Author

Generated automatically by Doxygen for DPDK from the source code.
