4 * Copyright(c) 2010-2013 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.
40 * The objective of the timer stress tests is to check that there are no
41 * race conditions in list and status management. This test launches,
42 * resets and stops the timer very often on many cores at the same
45 * - Only one timer is used for this test.
46 * - On each core, the rte_timer_manage() function is called from the main
47 * loop every 3 microseconds.
48 * - In the main loop, the timer may be reset (randomly, with a
49 * probability of 0.5 %) 100 microseconds later on a random core, or
50 * stopped (with a probability of 0.5 % also).
51 * - In callback, the timer is can be reset (randomly, with a
52 * probability of 0.5 %) 100 microseconds later on the same core or
53 * on another core (same probability), or stopped (same
59 * This test performs basic functional checks of the timers. The test
60 * uses four different timers that are loaded and stopped under
61 * specific conditions in specific contexts.
63 * - Four timers are used for this test.
64 * - On each core, the rte_timer_manage() function is called from main loop
65 * every 3 microseconds.
67 * The autotest python script checks that the behavior is correct:
71 * - At initialization, timer0 is loaded by the master core, on master core
72 * in "single" mode (time = 1 second).
73 * - In the first 19 callbacks, timer0 is reloaded on the same core,
74 * then, it is explicitly stopped at the 20th call.
75 * - At t=25s, timer0 is reloaded once by timer2.
79 * - At initialization, timer1 is loaded by the master core, on the
80 * master core in "single" mode (time = 2 seconds).
81 * - In the first 9 callbacks, timer1 is reloaded on another
82 * core. After the 10th callback, timer1 is not reloaded anymore.
86 * - At initialization, timer2 is loaded by the master core, on the
87 * master core in "periodical" mode (time = 1 second).
88 * - In the callback, when t=25s, it stops timer3 and reloads timer0
89 * on the current core.
93 * - At initialization, timer3 is loaded by the master core, on
94 * another core in "periodical" mode (time = 1 second).
95 * - It is stopped at t=25s by timer2.
103 #include <inttypes.h>
104 #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 20 /* 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, uint64_t 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_timer_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_timer_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_timer_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_timer_hz();
223 unsigned lcore_id = rte_lcore_id();
224 uint64_t cur_time = rte_get_timer_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_timer_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_timer_cycles();
305 diff = end_time - cur_time;
307 RTE_LOG(INFO, TESTTIMER, "core %u finished\n", lcore_id);
313 timer_sanity_check(void)
315 #ifdef RTE_LIBEAL_USE_HPET
316 if (eal_timer_source != EAL_TIMER_HPET) {
317 printf("Not using HPET, can't sanity check timer sources\n");
321 const uint64_t t_hz = rte_get_tsc_hz();
322 const uint64_t h_hz = rte_get_hpet_hz();
323 printf("Hertz values: TSC = %"PRIu64", HPET = %"PRIu64"\n", t_hz, h_hz);
325 const uint64_t tsc_start = rte_get_tsc_cycles();
326 const uint64_t hpet_start = rte_get_hpet_cycles();
327 rte_delay_ms(100); /* delay 1/10 second */
328 const uint64_t tsc_end = rte_get_tsc_cycles();
329 const uint64_t hpet_end = rte_get_hpet_cycles();
330 printf("Measured cycles: TSC = %"PRIu64", HPET = %"PRIu64"\n",
331 tsc_end-tsc_start, hpet_end-hpet_start);
333 const double tsc_time = (double)(tsc_end - tsc_start)/t_hz;
334 const double hpet_time = (double)(hpet_end - hpet_start)/h_hz;
335 /* get the percentage that the times differ by */
336 const double time_diff = fabs(tsc_time - hpet_time)*100/tsc_time;
337 printf("Measured time: TSC = %.4f, HPET = %.4f\n", tsc_time, hpet_time);
339 printf("Elapsed time measured by TSC and HPET differ by %f%%\n",
341 if (time_diff > 0.1) {
342 printf("Error times differ by >0.1%%");
356 /* sanity check our timer sources and timer config values */
357 if (timer_sanity_check() < 0) {
358 printf("Timer sanity checks failed\n");
362 if (rte_lcore_count() < 2) {
363 printf("not enough lcores for this test\n");
368 for (i=0; i<NB_TIMER; i++) {
369 memset(&mytiminfo[i], 0, sizeof(struct mytimerinfo));
371 rte_timer_init(&mytiminfo[i].tim);
374 /* calculate the "end of test" time */
375 cur_time = rte_get_timer_cycles();
376 hz = rte_get_timer_hz();
377 end_time = cur_time + (hz * TEST_DURATION_S);
379 /* start other cores */
380 printf("Start timer stress tests (%d seconds)\n", TEST_DURATION_S);
381 rte_eal_mp_remote_launch(timer_stress_main_loop, NULL, CALL_MASTER);
382 rte_eal_mp_wait_lcore();
384 /* stop timer 0 used for stress test */
385 rte_timer_stop_sync(&mytiminfo[0].tim);
387 /* calculate the "end of test" time */
388 cur_time = rte_get_timer_cycles();
389 hz = rte_get_timer_hz();
390 end_time = cur_time + (hz * TEST_DURATION_S);
392 /* start other cores */
393 printf("Start timer basic tests (%d seconds)\n", TEST_DURATION_S);
394 rte_eal_mp_remote_launch(timer_basic_main_loop, NULL, CALL_MASTER);
395 rte_eal_mp_wait_lcore();
397 /* stop all timers */
398 for (i=0; i<NB_TIMER; i++) {
399 rte_timer_stop_sync(&mytiminfo[i].tim);
402 rte_timer_dump_stats();