15630257fSFerruh Yigit.. SPDX-License-Identifier: BSD-3-Clause 25630257fSFerruh Yigit Copyright(c) 2010-2014 Intel Corporation. 3d0dff9baSBernard Iremonger 4d0dff9baSBernard IremongerTimer Sample Application 5d0dff9baSBernard Iremonger======================== 6d0dff9baSBernard Iremonger 7e0c7c473SSiobhan ButlerThe Timer sample application is a simple application that demonstrates the use of a timer in a DPDK application. 8d0dff9baSBernard IremongerThis application prints some messages from different lcores regularly, demonstrating the use of timers. 9d0dff9baSBernard Iremonger 10d0dff9baSBernard IremongerCompiling the Application 11d0dff9baSBernard Iremonger------------------------- 12d0dff9baSBernard Iremonger 137cacb056SHerakliusz LipiecTo compile the sample application see :doc:`compiling`. 14d0dff9baSBernard Iremonger 157cacb056SHerakliusz LipiecThe application is located in the ``timer`` sub-directory. 16d0dff9baSBernard Iremonger 17d0dff9baSBernard IremongerRunning the Application 18d0dff9baSBernard Iremonger----------------------- 19d0dff9baSBernard Iremonger 20218c4e68SBruce RichardsonTo run the example in linux environment: 21d0dff9baSBernard Iremonger 22d0dff9baSBernard Iremonger.. code-block:: console 23d0dff9baSBernard Iremonger 24e2a94f9aSCiara Power $ ./<build_dir>/examples/dpdk-timer -l 0-3 -n 4 25d0dff9baSBernard Iremonger 26e0c7c473SSiobhan ButlerRefer to the *DPDK Getting Started Guide* for general information on running applications and 27d0dff9baSBernard Iremongerthe Environment Abstraction Layer (EAL) options. 28d0dff9baSBernard Iremonger 29d0dff9baSBernard IremongerExplanation 30d0dff9baSBernard Iremonger----------- 31d0dff9baSBernard Iremonger 32d0dff9baSBernard IremongerThe following sections provide some explanation of the code. 33d0dff9baSBernard Iremonger 34d0dff9baSBernard IremongerInitialization and Main Loop 35d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 36d0dff9baSBernard Iremonger 37d0dff9baSBernard IremongerIn addition to EAL initialization, the timer subsystem must be initialized, by calling the rte_timer_subsystem_init() function. 38d0dff9baSBernard Iremonger 39*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 40*9a212dc0SConor Fogarty :language: c 41*9a212dc0SConor Fogarty :start-after: Init EAL. 8< 42*9a212dc0SConor Fogarty :end-before: >8 End of init EAL. 43*9a212dc0SConor Fogarty :dedent: 1 44d0dff9baSBernard Iremonger 45cb056611SStephen HemmingerAfter timer creation (see the next paragraph), the main loop is 46cb056611SStephen Hemmingerexecuted on each worker lcore using the well-known 47cb056611SStephen Hemmingerrte_eal_remote_launch() and also on the main. 48d0dff9baSBernard Iremonger 49*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 50*9a212dc0SConor Fogarty :language: c 51*9a212dc0SConor Fogarty :start-after: Call lcore_mainloop() on every worker lcore. 8< 52*9a212dc0SConor Fogarty :end-before: >8 End of call lcore_mainloop() on every worker lcore. 53*9a212dc0SConor Fogarty :dedent: 1 54d0dff9baSBernard Iremonger 55d0dff9baSBernard IremongerThe main loop is very simple in this example: 56d0dff9baSBernard Iremonger 57*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 58*9a212dc0SConor Fogarty :language: c 59*9a212dc0SConor Fogarty :start-after: Main loop. 8< 60*9a212dc0SConor Fogarty :end-before: >8 End of main loop. 61*9a212dc0SConor Fogarty :dedent: 1 62d0dff9baSBernard Iremonger 63d0dff9baSBernard IremongerAs explained in the comment, it is better to use the TSC register (as it is a per-lcore register) to check if the 64d0dff9baSBernard Iremongerrte_timer_manage() function must be called or not. 65d0dff9baSBernard IremongerIn this example, the resolution of the timer is 10 milliseconds. 66d0dff9baSBernard Iremonger 67d0dff9baSBernard IremongerManaging Timers 68d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~ 69d0dff9baSBernard Iremonger 70d0dff9baSBernard IremongerIn the main() function, the two timers are initialized. 71d0dff9baSBernard IremongerThis call to rte_timer_init() is necessary before doing any other operation on the timer structure. 72d0dff9baSBernard Iremonger 73*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 74*9a212dc0SConor Fogarty :language: c 75*9a212dc0SConor Fogarty :start-after: Init timer structures. 8< 76*9a212dc0SConor Fogarty :end-before: >8 End of init timer structures. 77*9a212dc0SConor Fogarty :dedent: 1 78d0dff9baSBernard Iremonger 79d0dff9baSBernard IremongerThen, the two timers are configured: 80d0dff9baSBernard Iremonger 81cb056611SStephen Hemminger* The first timer (timer0) is loaded on the main lcore and expires every second. 82d0dff9baSBernard Iremonger Since the PERIODICAL flag is provided, the timer is reloaded automatically by the timer subsystem. 83d0dff9baSBernard Iremonger The callback function is timer0_cb(). 84d0dff9baSBernard Iremonger 85d0dff9baSBernard Iremonger* The second timer (timer1) is loaded on the next available lcore every 333 ms. 86d0dff9baSBernard Iremonger The SINGLE flag means that the timer expires only once and must be reloaded manually if required. 87d0dff9baSBernard Iremonger The callback function is timer1_cb(). 88d0dff9baSBernard Iremonger 89*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 90*9a212dc0SConor Fogarty :language: c 91*9a212dc0SConor Fogarty :start-after: Load timer0, every second, on main lcore, reloaded automatically. 8< 92*9a212dc0SConor Fogarty :end-before: >8 End of two timers configured. 93*9a212dc0SConor Fogarty :dedent: 1 94d0dff9baSBernard Iremonger 95d0dff9baSBernard IremongerThe callback for the first timer (timer0) only displays a message until a global counter reaches 20 (after 20 seconds). 96d0dff9baSBernard IremongerIn this case, the timer is stopped using the rte_timer_stop() function. 97d0dff9baSBernard Iremonger 98*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 99*9a212dc0SConor Fogarty :language: c 100*9a212dc0SConor Fogarty :start-after: timer0 callback. 8< 101*9a212dc0SConor Fogarty :end-before: >8 End of timer0 callback. 102d0dff9baSBernard Iremonger 103d0dff9baSBernard IremongerThe callback for the second timer (timer1) displays a message and reloads the timer on the next lcore, using the 104d0dff9baSBernard Iremongerrte_timer_reset() function: 105d0dff9baSBernard Iremonger 106*9a212dc0SConor Fogarty.. literalinclude:: ../../../examples/timer/main.c 107*9a212dc0SConor Fogarty :language: c 108*9a212dc0SConor Fogarty :start-after: timer1 callback. 8< 109*9a212dc0SConor Fogarty :end-before: >8 End of timer1 callback. 110