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