11. Timer Sample Application

The Timer sample application demonstrates the use of a timer in a CNDP application. This application is implemented as a command that can be executed at the CNDP cli application. For more details about the CNDP cli application see CLI library guide.

11.1. Running the Application

After building CNDP, run the example:

$ ./buildir/examples/timer/timer

Launch the timer test:

cndp-cli:/> timer

Launch the perf test:

cndp-cli:/> perf

11.2. Explanation

The following sections provide some explanation of the code.

11.2.1. Initialization

In addition to CLI initialization, the timer subsystem must be initialized, by calling the cne_timer_subsystem_init() function.

Initialize CLI:

if (setup_cli() < 0)
      return -1;

Initialize CNE timer library:

cne_timer_subsystem_init();

11.2.2. Managing Timers

The call to cne_timer_init() is necessary before doing any other operation on the timer structure.

Initialize timer structure:

cne_timer_init(&tim0);

Then, the two timers are configured:

The first timer is for 2 seconds. The SINGLE flag means that the timer expires only once and must be reloaded manually if required. The callback function is single_timer(). The second timer expires every half a second. Since the PERIODICAL flag is provided, the timer is reloaded automatically by the timer subsystem. The callback function is periodical_timer().

Load single use timer for 2 seconds:

cne_timer_reset(&tim0, cne_get_timer_hz() * 2, SINGLE, cne_id(), single_timer, NULL);

Load second timer, every 1/2 second:

cne_timer_reset(&tim0, cne_get_timer_hz() / 2, PERIODICAL, cne_id(), periodical_timer, &count);

The timer is stopped using the cne_timer_stop() function.