1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2010-2014 Intel Corporation.
4 Timer Sample Application
5 ========================
7 The Timer sample application is a simple application that demonstrates the use of a timer in a DPDK application.
8 This application prints some messages from different lcores regularly, demonstrating the use of timers.
10 Compiling the Application
11 -------------------------
13 To compile the sample application see :doc:`compiling`.
15 The application is located in the ``timer`` sub-directory.
17 Running the Application
18 -----------------------
20 To run the example in linuxapp environment:
22 .. code-block:: console
24 $ ./build/timer -l 0-3 -n 4
26 Refer to the *DPDK Getting Started Guide* for general information on running applications and
27 the Environment Abstraction Layer (EAL) options.
32 The following sections provide some explanation of the code.
34 Initialization and Main Loop
35 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37 In addition to EAL initialization, the timer subsystem must be initialized, by calling the rte_timer_subsystem_init() function.
43 ret = rte_eal_init(argc, argv);
45 rte_panic("Cannot init EAL\n");
47 /* init RTE timer library */
49 rte_timer_subsystem_init();
51 After timer creation (see the next paragraph),
52 the main loop is executed on each slave lcore using the well-known rte_eal_remote_launch() and also on the master.
56 /* call lcore_mainloop() on every slave lcore */
58 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
59 rte_eal_remote_launch(lcore_mainloop, NULL, lcore_id);
62 /* call it on master lcore too */
64 (void) lcore_mainloop(NULL);
66 The main loop is very simple in this example:
72 * Call the timer handler on each core: as we don't
73 * need a very precise timer, so only call
74 * rte_timer_manage() every ~10ms (at 2 GHz). In a real
75 * application, this will enhance performances as
76 * reading the HPET timer is not efficient.
79 cur_tsc = rte_rdtsc();
81 diff_tsc = cur_tsc - prev_tsc;
83 if (diff_tsc > TIMER_RESOLUTION_CYCLES) {
89 As explained in the comment, it is better to use the TSC register (as it is a per-lcore register) to check if the
90 rte_timer_manage() function must be called or not.
91 In this example, the resolution of the timer is 10 milliseconds.
96 In the main() function, the two timers are initialized.
97 This call to rte_timer_init() is necessary before doing any other operation on the timer structure.
101 /* init timer structures */
103 rte_timer_init(&timer0);
104 rte_timer_init(&timer1);
106 Then, the two timers are configured:
108 * The first timer (timer0) is loaded on the master lcore and expires every second.
109 Since the PERIODICAL flag is provided, the timer is reloaded automatically by the timer subsystem.
110 The callback function is timer0_cb().
112 * The second timer (timer1) is loaded on the next available lcore every 333 ms.
113 The SINGLE flag means that the timer expires only once and must be reloaded manually if required.
114 The callback function is timer1_cb().
118 /* load timer0, every second, on master lcore, reloaded automatically */
120 hz = rte_get_hpet_hz();
122 lcore_id = rte_lcore_id();
124 rte_timer_reset(&timer0, hz, PERIODICAL, lcore_id, timer0_cb, NULL);
126 /* load timer1, every second/3, on next lcore, reloaded manually */
128 lcore_id = rte_get_next_lcore(lcore_id, 0, 1);
130 rte_timer_reset(&timer1, hz/3, SINGLE, lcore_id, timer1_cb, NULL);
132 The callback for the first timer (timer0) only displays a message until a global counter reaches 20 (after 20 seconds).
133 In this case, the timer is stopped using the rte_timer_stop() function.
137 /* timer0 callback */
140 timer0_cb( attribute ((unused)) struct rte_timer *tim, __attribute ((unused)) void *arg)
142 static unsigned counter = 0;
144 unsigned lcore_id = rte_lcore_id();
146 printf("%s() on lcore %u\n", FUNCTION , lcore_id);
148 /* this timer is automatically reloaded until we decide to stop it, when counter reaches 20. */
150 if ((counter ++) == 20)
154 The callback for the second timer (timer1) displays a message and reloads the timer on the next lcore, using the
155 rte_timer_reset() function:
159 /* timer1 callback */
162 timer1_cb( attribute ((unused)) struct rte_timer *tim, _attribute ((unused)) void *arg)
164 unsigned lcore_id = rte_lcore_id();
167 printf("%s() on lcore %u\\n", FUNCTION , lcore_id);
169 /* reload it on another lcore */
171 hz = rte_get_hpet_hz();
173 lcore_id = rte_get_next_lcore(lcore_id, 0, 1);
175 rte_timer_reset(&timer1, hz/3, SINGLE, lcore_id, timer1_cb, NULL);