4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * The objective of the timer stress tests is to check that there are no
42 * race conditions in list and status management. This test launches,
43 * resets and stops the timer very often on many cores at the same
46 * - Only one timer is used for this test.
47 * - On each core, the rte_timer_manage() function is called from the main
48 * loop every 3 microseconds.
49 * - In the main loop, the timer may be reset (randomly, with a
50 * probability of 0.5 %) 100 microseconds later on a random core, or
51 * stopped (with a probability of 0.5 % also).
52 * - In callback, the timer is can be reset (randomly, with a
53 * probability of 0.5 %) 100 microseconds later on the same core or
54 * on another core (same probability), or stopped (same
60 * This test performs basic functional checks of the timers. The test
61 * uses four different timers that are loaded and stopped under
62 * specific conditions in specific contexts.
64 * - Four timers are used for this test.
65 * - On each core, the rte_timer_manage() function is called from main loop
66 * every 3 microseconds.
68 * The autotest python script checks that the behavior is correct:
72 * - At initialization, timer0 is loaded by the master core, on master core
73 * in "single" mode (time = 1 second).
74 * - In the first 19 callbacks, timer0 is reloaded on the same core,
75 * then, it is explicitly stopped at the 20th call.
76 * - At t=25s, timer0 is reloaded once by timer2.
80 * - At initialization, timer1 is loaded by the master core, on the
81 * master core in "single" mode (time = 2 seconds).
82 * - In the first 9 callbacks, timer1 is reloaded on another
83 * core. After the 10th callback, timer1 is not reloaded anymore.
87 * - At initialization, timer2 is loaded by the master core, on the
88 * master core in "periodical" mode (time = 1 second).
89 * - In the callback, when t=25s, it stops timer3 and reloads timer0
90 * on the current core.
94 * - At initialization, timer3 is loaded by the master core, on
95 * another core in "periodical" mode (time = 1 second).
96 * - It is stopped at t=25s by timer2.
104 #include <inttypes.h>
105 #include <sys/queue.h>
107 #include <cmdline_parse.h>
109 #include <rte_common.h>
111 #include <rte_memory.h>
112 #include <rte_memzone.h>
113 #include <rte_launch.h>
114 #include <rte_cycles.h>
115 #include <rte_tailq.h>
117 #include <rte_per_lcore.h>
118 #include <rte_lcore.h>
119 #include <rte_atomic.h>
120 #include <rte_timer.h>
121 #include <rte_random.h>
125 #define TEST_DURATION_S 30 /* in seconds */
128 #define RTE_LOGTYPE_TESTTIMER RTE_LOGTYPE_USER3
130 static volatile uint64_t end_time;
133 struct rte_timer tim;
138 static struct mytimerinfo mytiminfo[NB_TIMER];
140 static void timer_basic_cb(struct rte_timer *tim, void *arg);
143 mytimer_reset(struct mytimerinfo *timinfo, unsigned ticks,
144 enum rte_timer_type type, unsigned tim_lcore,
147 rte_timer_reset_sync(&timinfo->tim, ticks, type, tim_lcore,
151 /* timer callback for stress tests */
153 timer_stress_cb(__attribute__((unused)) struct rte_timer *tim,
154 __attribute__((unused)) void *arg)
157 unsigned lcore_id = rte_lcore_id();
158 uint64_t hz = rte_get_hpet_hz();
160 if (rte_timer_pending(tim))
164 if ((r & 0xff) == 0) {
165 mytimer_reset(&mytiminfo[0], hz, SINGLE, lcore_id,
168 else if ((r & 0xff) == 1) {
169 mytimer_reset(&mytiminfo[0], hz, SINGLE,
170 rte_get_next_lcore(lcore_id, 0, 1),
173 else if ((r & 0xff) == 2) {
174 rte_timer_stop(&mytiminfo[0].tim);
179 timer_stress_main_loop(__attribute__((unused)) void *arg)
181 uint64_t hz = rte_get_hpet_hz();
182 unsigned lcore_id = rte_lcore_id();
189 /* call the timer handler on each core */
192 /* simulate the processing of a packet
193 * (3 us = 6000 cycles at 2 Ghz) */
196 /* randomly stop or reset timer */
198 lcore_id = rte_get_next_lcore(lcore_id, 0, 1);
199 if ((r & 0xff) == 0) {
201 mytimer_reset(&mytiminfo[0], hz/10000, SINGLE, lcore_id,
204 else if ((r & 0xff) == 1) {
205 rte_timer_stop_sync(&mytiminfo[0].tim);
207 cur_time = rte_get_hpet_cycles();
208 diff = end_time - cur_time;
211 lcore_id = rte_lcore_id();
212 RTE_LOG(INFO, TESTTIMER, "core %u finished\n", lcore_id);
217 /* timer callback for basic tests */
219 timer_basic_cb(struct rte_timer *tim, void *arg)
221 struct mytimerinfo *timinfo = arg;
222 uint64_t hz = rte_get_hpet_hz();
223 unsigned lcore_id = rte_lcore_id();
224 uint64_t cur_time = rte_get_hpet_cycles();
226 if (rte_timer_pending(tim))
231 RTE_LOG(INFO, TESTTIMER,
232 "%"PRIu64": callback id=%u count=%u on core %u\n",
233 cur_time, timinfo->id, timinfo->count, lcore_id);
235 /* reload timer 0 on same core */
236 if (timinfo->id == 0 && timinfo->count < 20) {
237 mytimer_reset(timinfo, hz, SINGLE, lcore_id, timer_basic_cb);
241 /* reload timer 1 on next core */
242 if (timinfo->id == 1 && timinfo->count < 10) {
243 mytimer_reset(timinfo, hz*2, SINGLE,
244 rte_get_next_lcore(lcore_id, 0, 1),
249 /* Explicitelly stop timer 0. Once stop() called, we can even
250 * erase the content of the structure: it is not referenced
251 * anymore by any code (in case of dynamic structure, it can
253 if (timinfo->id == 0 && timinfo->count == 20) {
255 /* stop_sync() is not needed, because we know that the
256 * status of timer is only modified by this core */
258 memset(tim, 0xAA, sizeof(struct rte_timer));
262 /* stop timer3, and restart a new timer0 (it was removed 5
263 * seconds ago) for a single shot */
264 if (timinfo->id == 2 && timinfo->count == 25) {
265 rte_timer_stop_sync(&mytiminfo[3].tim);
267 /* need to reinit because structure was erased with 0xAA */
268 rte_timer_init(&mytiminfo[0].tim);
269 mytimer_reset(&mytiminfo[0], hz, SINGLE, lcore_id,
275 timer_basic_main_loop(__attribute__((unused)) void *arg)
277 uint64_t hz = rte_get_hpet_hz();
278 unsigned lcore_id = rte_lcore_id();
282 /* launch all timers on core 0 */
283 if (lcore_id == rte_get_master_lcore()) {
284 mytimer_reset(&mytiminfo[0], hz, SINGLE, lcore_id,
286 mytimer_reset(&mytiminfo[1], hz*2, SINGLE, lcore_id,
288 mytimer_reset(&mytiminfo[2], hz, PERIODICAL, lcore_id,
290 mytimer_reset(&mytiminfo[3], hz, PERIODICAL,
291 rte_get_next_lcore(lcore_id, 0, 1),
297 /* call the timer handler on each core */
300 /* simulate the processing of a packet
301 * (3 us = 6000 cycles at 2 Ghz) */
304 cur_time = rte_get_hpet_cycles();
305 diff = end_time - cur_time;
307 RTE_LOG(INFO, TESTTIMER, "core %u finished\n", lcore_id);
319 if (rte_lcore_count() < 2) {
320 printf("not enough lcores for this test\n");
325 for (i=0; i<NB_TIMER; i++) {
326 memset(&mytiminfo[i], 0, sizeof(struct mytimerinfo));
328 rte_timer_init(&mytiminfo[i].tim);
331 /* calculate the "end of test" time */
332 cur_time = rte_get_hpet_cycles();
333 hz = rte_get_hpet_hz();
334 end_time = cur_time + (hz * TEST_DURATION_S);
336 /* start other cores */
337 printf("Start timer stress tests (%d seconds)\n", TEST_DURATION_S);
338 rte_eal_mp_remote_launch(timer_stress_main_loop, NULL, CALL_MASTER);
339 rte_eal_mp_wait_lcore();
341 /* stop timer 0 used for stress test */
342 rte_timer_stop_sync(&mytiminfo[0].tim);
344 /* calculate the "end of test" time */
345 cur_time = rte_get_hpet_cycles();
346 hz = rte_get_hpet_hz();
347 end_time = cur_time + (hz * TEST_DURATION_S);
349 /* start other cores */
350 printf("Start timer basic tests (%d seconds)\n", TEST_DURATION_S);
351 rte_eal_mp_remote_launch(timer_basic_main_loop, NULL, CALL_MASTER);
352 rte_eal_mp_wait_lcore();
354 /* stop all timers */
355 for (i=0; i<NB_TIMER; i++) {
356 rte_timer_stop_sync(&mytiminfo[i].tim);
359 rte_timer_dump_stats();