1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 * The objective of the timer stress tests is to check that there are no
14 * race conditions in list and status management. This test launches,
15 * resets and stops the timer very often on many cores at the same
18 * - Only one timer is used for this test.
19 * - On each core, the rte_timer_manage() function is called from the main
20 * loop every 3 microseconds.
21 * - In the main loop, the timer may be reset (randomly, with a
22 * probability of 0.5 %) 100 microseconds later on a random core, or
23 * stopped (with a probability of 0.5 % also).
24 * - In callback, the timer is can be reset (randomly, with a
25 * probability of 0.5 %) 100 microseconds later on the same core or
26 * on another core (same probability), or stopped (same
31 * The objective of this test is similar to the first in that it attempts
32 * to find if there are any race conditions in the timer library. However,
33 * it is less complex in terms of operations performed and duration, as it
34 * is designed to have a predictable outcome that can be tested.
36 * - A set of timers is initialized for use by the test
37 * - All cores then simultaneously are set to schedule all the timers at
38 * the same time, so conflicts should occur.
39 * - Then there is a delay while we wait for the timers to expire
40 * - Then the main lcore calls timer_manage() and we check that all
41 * timers have had their callbacks called exactly once - no more no less.
42 * - Then we repeat the process, except after setting up the timers, we have
43 * all cores randomly reschedule them.
44 * - Again we check that the expected number of callbacks has occurred when
45 * we call timer-manage.
49 * This test performs basic functional checks of the timers. The test
50 * uses four different timers that are loaded and stopped under
51 * specific conditions in specific contexts.
53 * - Four timers are used for this test.
54 * - On each core, the rte_timer_manage() function is called from main loop
55 * every 3 microseconds.
57 * The autotest python script checks that the behavior is correct:
61 * - At initialization, timer0 is loaded by the main core, on main core
62 * in "single" mode (time = 1 second).
63 * - In the first 19 callbacks, timer0 is reloaded on the same core,
64 * then, it is explicitly stopped at the 20th call.
65 * - At t=25s, timer0 is reloaded once by timer2.
69 * - At initialization, timer1 is loaded by the main core, on the
70 * main core in "single" mode (time = 2 seconds).
71 * - In the first 9 callbacks, timer1 is reloaded on another
72 * core. After the 10th callback, timer1 is not reloaded anymore.
76 * - At initialization, timer2 is loaded by the main core, on the
77 * main core in "periodical" mode (time = 1 second).
78 * - In the callback, when t=25s, it stops timer3 and reloads timer0
79 * on the current core.
83 * - At initialization, timer3 is loaded by the main core, on
84 * another core in "periodical" mode (time = 1 second).
85 * - It is stopped at t=25s by timer2.
94 #include <sys/queue.h>
97 #include <rte_common.h>
99 #include <rte_memory.h>
100 #include <rte_launch.h>
101 #include <rte_cycles.h>
103 #include <rte_per_lcore.h>
104 #include <rte_lcore.h>
105 #include <rte_atomic.h>
106 #include <rte_timer.h>
107 #include <rte_random.h>
108 #include <rte_malloc.h>
109 #include <rte_pause.h>
111 #define TEST_DURATION_S 1 /* in seconds */
114 #define RTE_LOGTYPE_TESTTIMER RTE_LOGTYPE_USER3
116 static volatile uint64_t end_time;
117 static volatile int test_failed;
120 struct rte_timer tim;
125 static struct mytimerinfo mytiminfo[NB_TIMER];
127 static void timer_basic_cb(struct rte_timer *tim, void *arg);
130 mytimer_reset(struct mytimerinfo *timinfo, uint64_t ticks,
131 enum rte_timer_type type, unsigned tim_lcore,
134 rte_timer_reset_sync(&timinfo->tim, ticks, type, tim_lcore,
138 /* timer callback for stress tests */
140 timer_stress_cb(__rte_unused struct rte_timer *tim,
141 __rte_unused void *arg)
144 unsigned lcore_id = rte_lcore_id();
145 uint64_t hz = rte_get_timer_hz();
147 if (rte_timer_pending(tim))
151 if ((r & 0xff) == 0) {
152 mytimer_reset(&mytiminfo[0], hz, SINGLE, lcore_id,
155 else if ((r & 0xff) == 1) {
156 mytimer_reset(&mytiminfo[0], hz, SINGLE,
157 rte_get_next_lcore(lcore_id, 0, 1),
160 else if ((r & 0xff) == 2) {
161 rte_timer_stop(&mytiminfo[0].tim);
166 timer_stress_main_loop(__rte_unused void *arg)
168 uint64_t hz = rte_get_timer_hz();
169 unsigned lcore_id = rte_lcore_id();
176 /* call the timer handler on each core */
179 /* simulate the processing of a packet
180 * (1 us = 2000 cycles at 2 Ghz) */
183 /* randomly stop or reset timer */
185 lcore_id = rte_get_next_lcore(lcore_id, 0, 1);
186 if ((r & 0xff) == 0) {
188 mytimer_reset(&mytiminfo[0], hz/10000, SINGLE, lcore_id,
191 else if ((r & 0xff) == 1) {
192 rte_timer_stop_sync(&mytiminfo[0].tim);
194 cur_time = rte_get_timer_cycles();
195 diff = end_time - cur_time;
198 lcore_id = rte_lcore_id();
199 RTE_LOG(INFO, TESTTIMER, "core %u finished\n", lcore_id);
204 /* Need to synchronize worker lcores through multiple steps. */
205 enum { WORKER_WAITING = 1, WORKER_RUN_SIGNAL, WORKER_RUNNING, WORKER_FINISHED };
206 static rte_atomic16_t lcore_state[RTE_MAX_LCORE];
209 main_init_workers(void)
213 RTE_LCORE_FOREACH_WORKER(i) {
214 rte_atomic16_set(&lcore_state[i], WORKER_WAITING);
219 main_start_workers(void)
223 RTE_LCORE_FOREACH_WORKER(i) {
224 rte_atomic16_set(&lcore_state[i], WORKER_RUN_SIGNAL);
226 RTE_LCORE_FOREACH_WORKER(i) {
227 while (rte_atomic16_read(&lcore_state[i]) != WORKER_RUNNING)
233 main_wait_for_workers(void)
237 RTE_LCORE_FOREACH_WORKER(i) {
238 while (rte_atomic16_read(&lcore_state[i]) != WORKER_FINISHED)
244 worker_wait_to_start(void)
246 unsigned lcore_id = rte_lcore_id();
248 while (rte_atomic16_read(&lcore_state[lcore_id]) != WORKER_RUN_SIGNAL)
250 rte_atomic16_set(&lcore_state[lcore_id], WORKER_RUNNING);
256 unsigned lcore_id = rte_lcore_id();
258 rte_atomic16_set(&lcore_state[lcore_id], WORKER_FINISHED);
262 static volatile int cb_count = 0;
264 /* callback for second stress test. will only be called
268 timer_stress2_cb(struct rte_timer *tim __rte_unused, void *arg __rte_unused)
273 #define NB_STRESS2_TIMERS 8192
276 timer_stress2_main_loop(__rte_unused void *arg)
278 static struct rte_timer *timers;
280 uint64_t delay = rte_get_timer_hz() / 20;
281 unsigned int lcore_id = rte_lcore_id();
282 unsigned int main_lcore = rte_get_main_lcore();
283 int32_t my_collisions = 0;
284 static rte_atomic32_t collisions;
286 if (lcore_id == main_lcore) {
289 rte_atomic32_set(&collisions, 0);
291 timers = rte_malloc(NULL, sizeof(*timers) * NB_STRESS2_TIMERS, 0);
292 if (timers == NULL) {
293 printf("Test Failed\n");
294 printf("- Cannot allocate memory for timers\n" );
296 main_start_workers();
299 for (i = 0; i < NB_STRESS2_TIMERS; i++)
300 rte_timer_init(&timers[i]);
301 main_start_workers();
303 worker_wait_to_start();
308 /* have all cores schedule all timers on main lcore */
309 for (i = 0; i < NB_STRESS2_TIMERS; i++) {
310 ret = rte_timer_reset(&timers[i], delay, SINGLE, main_lcore,
311 timer_stress2_cb, NULL);
312 /* there will be collisions when multiple cores simultaneously
313 * configure the same timers */
317 if (my_collisions != 0)
318 rte_atomic32_add(&collisions, my_collisions);
320 /* wait long enough for timers to expire */
323 /* all cores rendezvous */
324 if (lcore_id == main_lcore) {
325 main_wait_for_workers();
330 /* now check that we get the right number of callbacks */
331 if (lcore_id == main_lcore) {
332 my_collisions = rte_atomic32_read(&collisions);
333 if (my_collisions != 0)
334 printf("- %d timer reset collisions (OK)\n", my_collisions);
336 if (cb_count != NB_STRESS2_TIMERS) {
337 printf("Test Failed\n");
338 printf("- Stress test 2, part 1 failed\n");
339 printf("- Expected %d callbacks, got %d\n", NB_STRESS2_TIMERS,
342 main_start_workers();
348 main_start_workers();
351 worker_wait_to_start();
356 /* now test again, just stop and restart timers at random after init*/
357 for (i = 0; i < NB_STRESS2_TIMERS; i++)
358 rte_timer_reset(&timers[i], delay, SINGLE, main_lcore,
359 timer_stress2_cb, NULL);
361 /* pick random timer to reset, stopping them first half the time */
362 for (i = 0; i < 100000; i++) {
363 int r = rand() % NB_STRESS2_TIMERS;
365 rte_timer_stop(&timers[r]);
366 rte_timer_reset(&timers[r], delay, SINGLE, main_lcore,
367 timer_stress2_cb, NULL);
370 /* wait long enough for timers to expire */
373 /* now check that we get the right number of callbacks */
374 if (lcore_id == main_lcore) {
375 main_wait_for_workers();
378 if (cb_count != NB_STRESS2_TIMERS) {
379 printf("Test Failed\n");
380 printf("- Stress test 2, part 2 failed\n");
381 printf("- Expected %d callbacks, got %d\n", NB_STRESS2_TIMERS,
390 if (lcore_id == main_lcore) {
391 main_wait_for_workers();
392 if (timers != NULL) {
403 /* timer callback for basic tests */
405 timer_basic_cb(struct rte_timer *tim, void *arg)
407 struct mytimerinfo *timinfo = arg;
408 uint64_t hz = rte_get_timer_hz();
409 unsigned lcore_id = rte_lcore_id();
410 uint64_t cur_time = rte_get_timer_cycles();
412 if (rte_timer_pending(tim))
417 RTE_LOG(INFO, TESTTIMER,
418 "%"PRIu64": callback id=%u count=%u on core %u\n",
419 cur_time, timinfo->id, timinfo->count, lcore_id);
421 /* reload timer 0 on same core */
422 if (timinfo->id == 0 && timinfo->count < 20) {
423 mytimer_reset(timinfo, hz, SINGLE, lcore_id, timer_basic_cb);
427 /* reload timer 1 on next core */
428 if (timinfo->id == 1 && timinfo->count < 10) {
429 mytimer_reset(timinfo, hz*2, SINGLE,
430 rte_get_next_lcore(lcore_id, 0, 1),
435 /* Explicitelly stop timer 0. Once stop() called, we can even
436 * erase the content of the structure: it is not referenced
437 * anymore by any code (in case of dynamic structure, it can
439 if (timinfo->id == 0 && timinfo->count == 20) {
441 /* stop_sync() is not needed, because we know that the
442 * status of timer is only modified by this core */
444 memset(tim, 0xAA, sizeof(struct rte_timer));
448 /* stop timer3, and restart a new timer0 (it was removed 5
449 * seconds ago) for a single shot */
450 if (timinfo->id == 2 && timinfo->count == 25) {
451 rte_timer_stop_sync(&mytiminfo[3].tim);
453 /* need to reinit because structure was erased with 0xAA */
454 rte_timer_init(&mytiminfo[0].tim);
455 mytimer_reset(&mytiminfo[0], hz, SINGLE, lcore_id,
461 timer_basic_main_loop(__rte_unused void *arg)
463 uint64_t hz = rte_get_timer_hz();
464 unsigned lcore_id = rte_lcore_id();
468 /* launch all timers on core 0 */
469 if (lcore_id == rte_get_main_lcore()) {
470 mytimer_reset(&mytiminfo[0], hz/4, SINGLE, lcore_id,
472 mytimer_reset(&mytiminfo[1], hz/2, SINGLE, lcore_id,
474 mytimer_reset(&mytiminfo[2], hz/4, PERIODICAL, lcore_id,
476 mytimer_reset(&mytiminfo[3], hz/4, PERIODICAL,
477 rte_get_next_lcore(lcore_id, 0, 1),
483 /* call the timer handler on each core */
486 /* simulate the processing of a packet
487 * (3 us = 6000 cycles at 2 Ghz) */
490 cur_time = rte_get_timer_cycles();
491 diff = end_time - cur_time;
493 RTE_LOG(INFO, TESTTIMER, "core %u finished\n", lcore_id);
499 timer_sanity_check(void)
501 #ifdef RTE_LIBEAL_USE_HPET
502 if (eal_timer_source != EAL_TIMER_HPET) {
503 printf("Not using HPET, can't sanity check timer sources\n");
507 const uint64_t t_hz = rte_get_tsc_hz();
508 const uint64_t h_hz = rte_get_hpet_hz();
509 printf("Hertz values: TSC = %"PRIu64", HPET = %"PRIu64"\n", t_hz, h_hz);
511 const uint64_t tsc_start = rte_get_tsc_cycles();
512 const uint64_t hpet_start = rte_get_hpet_cycles();
513 rte_delay_ms(100); /* delay 1/10 second */
514 const uint64_t tsc_end = rte_get_tsc_cycles();
515 const uint64_t hpet_end = rte_get_hpet_cycles();
516 printf("Measured cycles: TSC = %"PRIu64", HPET = %"PRIu64"\n",
517 tsc_end-tsc_start, hpet_end-hpet_start);
519 const double tsc_time = (double)(tsc_end - tsc_start)/t_hz;
520 const double hpet_time = (double)(hpet_end - hpet_start)/h_hz;
521 /* get the percentage that the times differ by */
522 const double time_diff = fabs(tsc_time - hpet_time)*100/tsc_time;
523 printf("Measured time: TSC = %.4f, HPET = %.4f\n", tsc_time, hpet_time);
525 printf("Elapsed time measured by TSC and HPET differ by %f%%\n",
527 if (time_diff > 0.1) {
528 printf("Error times differ by >0.1%%");
542 if (rte_lcore_count() < 2) {
543 printf("Not enough cores for timer_autotest, expecting at least 2\n");
547 /* sanity check our timer sources and timer config values */
548 if (timer_sanity_check() < 0) {
549 printf("Timer sanity checks failed\n");
554 for (i=0; i<NB_TIMER; i++) {
555 memset(&mytiminfo[i], 0, sizeof(struct mytimerinfo));
557 rte_timer_init(&mytiminfo[i].tim);
560 /* calculate the "end of test" time */
561 cur_time = rte_get_timer_cycles();
562 hz = rte_get_timer_hz();
563 end_time = cur_time + (hz * TEST_DURATION_S);
565 /* start other cores */
566 printf("Start timer stress tests\n");
567 rte_eal_mp_remote_launch(timer_stress_main_loop, NULL, CALL_MAIN);
568 rte_eal_mp_wait_lcore();
570 /* stop timer 0 used for stress test */
571 rte_timer_stop_sync(&mytiminfo[0].tim);
573 /* run a second, slightly different set of stress tests */
574 printf("\nStart timer stress tests 2\n");
576 rte_eal_mp_remote_launch(timer_stress2_main_loop, NULL, CALL_MAIN);
577 rte_eal_mp_wait_lcore();
581 /* calculate the "end of test" time */
582 cur_time = rte_get_timer_cycles();
583 hz = rte_get_timer_hz();
584 end_time = cur_time + (hz * TEST_DURATION_S);
586 /* start other cores */
587 printf("\nStart timer basic tests\n");
588 rte_eal_mp_remote_launch(timer_basic_main_loop, NULL, CALL_MAIN);
589 rte_eal_mp_wait_lcore();
591 /* stop all timers */
592 for (i=0; i<NB_TIMER; i++) {
593 rte_timer_stop_sync(&mytiminfo[i].tim);
596 rte_timer_dump_stats(stdout);
601 REGISTER_TEST_COMMAND(timer_autotest, test_timer);