1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Akamai Technologies.
11 #include <rte_cycles.h>
12 #include <rte_timer.h>
13 #include <rte_common.h>
14 #include <rte_lcore.h>
15 #include <rte_random.h>
16 #include <rte_malloc.h>
17 #include <rte_pause.h>
19 #ifdef RTE_EXEC_ENV_LINUX
20 #define usec_delay(us) usleep(us)
22 #define usec_delay(us) rte_delay_us(us)
25 #define BILLION (1UL << 30)
27 #define TEST_DURATION_S 4 /* in seconds */
30 static struct rte_timer timer[N_TIMERS];
31 static unsigned int timer_lcore_id[N_TIMERS];
33 static unsigned int main_lcore;
34 static volatile unsigned int stop_workers;
36 static int reload_timer(struct rte_timer *tim);
38 RTE_LOG_REGISTER(timer_logtype_test, test.timer, INFO);
41 timer_cb(struct rte_timer *tim, void *arg __rte_unused)
43 /* Simulate slow callback function, 100 us. */
46 rte_log(RTE_LOG_DEBUG, timer_logtype_test,
47 "------------------------------------------------\n");
48 rte_log(RTE_LOG_DEBUG, timer_logtype_test, "%s: core %u timer %"
49 PRIuPTR "\n", __func__, rte_lcore_id(), tim - timer);
50 (void)reload_timer(tim);
53 RTE_DEFINE_PER_LCORE(unsigned, n_reset_collisions);
56 reload_timer(struct rte_timer *tim)
58 /* Make timer expire roughly when the TSC hits the next BILLION
59 * multiple. Add in timer's index to make them expire in nearly
60 * sorted order. This makes all timers somewhat synchronized,
61 * firing ~2-3 times per second, assuming 2-3 GHz TSCs.
63 uint64_t ticks = BILLION - (rte_get_timer_cycles() % BILLION) +
67 ret = rte_timer_reset(tim, ticks, PERIODICAL, main_lcore, timer_cb, NULL);
69 rte_log(RTE_LOG_DEBUG, timer_logtype_test,
70 "- core %u failed to reset timer %" PRIuPTR " (OK)\n",
71 rte_lcore_id(), tim - timer);
72 RTE_PER_LCORE(n_reset_collisions) += 1;
78 worker_main_loop(__rte_unused void *arg)
80 unsigned lcore_id = rte_lcore_id();
83 RTE_PER_LCORE(n_reset_collisions) = 0;
85 printf("Starting main loop on core %u\n", lcore_id);
87 while (!stop_workers) {
88 /* Wait until the timer manager is running.
89 * We know it's running when we see timer[0] NOT pending.
91 if (rte_timer_pending(&timer[0])) {
96 /* Now, go cause some havoc!
99 for (i = 0; i < N_TIMERS; i++) {
100 if (timer_lcore_id[i] == lcore_id)
101 (void)reload_timer(&timer[i]);
103 usec_delay(100*1000); /* sleep 100 ms */
106 if (RTE_PER_LCORE(n_reset_collisions) != 0) {
107 printf("- core %u, %u reset collisions (OK)\n",
108 lcore_id, RTE_PER_LCORE(n_reset_collisions));
114 test_timer_racecond(void)
124 main_lcore = lcore_id = rte_lcore_id();
125 hz = rte_get_timer_hz();
127 /* init and start timers */
128 for (i = 0; i < N_TIMERS; i++) {
129 rte_timer_init(&timer[i]);
130 ret = reload_timer(&timer[i]);
131 TEST_ASSERT(ret == 0, "reload_timer failed");
133 /* Distribute timers to workers.
134 * Note that we assign timer[0] to the main.
136 timer_lcore_id[i] = lcore_id;
137 lcore_id = rte_get_next_lcore(lcore_id, 1, 1);
140 /* calculate the "end of test" time */
141 cur_time = rte_get_timer_cycles();
142 end_time = cur_time + (hz * TEST_DURATION_S);
144 /* start worker cores */
146 printf("Start timer manage race condition test (%u seconds)\n",
148 rte_eal_mp_remote_launch(worker_main_loop, NULL, SKIP_MAIN);
155 usec_delay(100*1000);
157 cur_time = rte_get_timer_cycles();
158 diff = end_time - cur_time;
161 /* stop worker cores */
162 printf("Stopping timer manage race condition test\n");
164 rte_eal_mp_wait_lcore();
167 for (i = 0; i < N_TIMERS; i++) {
168 ret = rte_timer_stop(&timer[i]);
169 TEST_ASSERT(ret == 0, "rte_timer_stop failed");
175 REGISTER_TEST_COMMAND(timer_racecond_autotest, test_timer_racecond);