1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
10 #include <rte_debug.h>
11 #include <rte_memzone.h>
12 #include <rte_timer.h>
13 #include <rte_cycles.h>
14 #include <rte_mempool.h>
15 #include <rte_random.h>
19 #ifdef RTE_EXEC_ENV_WINDOWS
21 test_timer_secondary(void)
23 printf("timer_secondary not supported on Windows, skipping test\n");
30 #define NUM_TIMERS (1 << 20) /* ~1M timers */
31 #define NUM_LCORES_NEEDED 3
32 #define TEST_INFO_MZ_NAME "test_timer_info_mz"
33 #define MSECPERSEC 1E3
35 #define launch_proc(ARGV) process_dup(ARGV, RTE_DIM(ARGV), __func__)
38 unsigned int main_lcore;
39 unsigned int mgr_lcore;
40 unsigned int sec_lcore;
41 uint32_t timer_data_id;
42 volatile int expected_count;
43 volatile int expired_count;
44 struct rte_mempool *tim_mempool;
45 struct rte_timer *expired_timers[NUM_TIMERS];
46 int expired_timers_idx;
47 volatile int exit_flag;
51 timer_secondary_spawn_wait(unsigned int lcore)
54 #ifdef RTE_EXEC_ENV_LINUXAPP
55 char tmp[PATH_MAX] = {0};
56 char prefix[PATH_MAX] = {0};
58 get_current_prefix(tmp, sizeof(tmp));
60 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
62 const char *prefix = "";
64 char const *argv[] = {
67 "--proc-type=secondary",
71 snprintf(coremask, sizeof(coremask), "%x", (1 << lcore));
73 return launch_proc(argv);
77 handle_expired_timer(struct rte_timer *tim)
79 struct test_info *test_info = tim->arg;
81 test_info->expired_count++;
82 test_info->expired_timers[test_info->expired_timers_idx++] = tim;
86 timer_manage_loop(void *arg)
89 uint64_t tick_cycles = TICK_MSECS * rte_get_timer_hz() / MSECPERSEC;
90 uint64_t prev_tsc = 0, cur_tsc, diff_tsc;
91 struct test_info *test_info = arg;
93 while (!test_info->exit_flag) {
94 cur_tsc = rte_rdtsc();
95 diff_tsc = cur_tsc - prev_tsc;
97 if (diff_tsc > tick_cycles) {
98 /* Scan timer list for expired timers */
99 rte_timer_alt_manage(test_info->timer_data_id,
102 handle_expired_timer);
104 /* Return expired timer objects back to mempool */
105 rte_mempool_put_bulk(test_info->tim_mempool,
106 (void **)test_info->expired_timers,
107 test_info->expired_timers_idx);
109 test_info->expired_timers_idx = 0;
121 test_timer_secondary(void)
123 int proc_type = rte_eal_process_type();
124 const struct rte_memzone *mz;
125 struct test_info *test_info;
128 if (proc_type == RTE_PROC_PRIMARY) {
129 if (rte_lcore_count() < NUM_LCORES_NEEDED) {
130 printf("Not enough cores for test_timer_secondary, expecting at least %u\n",
135 mz = rte_memzone_reserve(TEST_INFO_MZ_NAME, sizeof(*test_info),
137 TEST_ASSERT_NOT_NULL(mz, "Couldn't allocate memory for "
139 test_info = mz->addr;
141 test_info->tim_mempool = rte_mempool_create("test_timer_mp",
142 NUM_TIMERS, sizeof(struct rte_timer), 0, 0,
143 NULL, NULL, NULL, NULL, rte_socket_id(), 0);
145 ret = rte_timer_data_alloc(&test_info->timer_data_id);
146 TEST_ASSERT_SUCCESS(ret, "Failed to allocate timer data "
149 unsigned int *main_lcorep = &test_info->main_lcore;
150 unsigned int *mgr_lcorep = &test_info->mgr_lcore;
151 unsigned int *sec_lcorep = &test_info->sec_lcore;
153 *main_lcorep = rte_get_main_lcore();
154 *mgr_lcorep = rte_get_next_lcore(*main_lcorep, 1, 1);
155 *sec_lcorep = rte_get_next_lcore(*mgr_lcorep, 1, 1);
157 ret = rte_eal_remote_launch(timer_manage_loop,
160 TEST_ASSERT_SUCCESS(ret, "Failed to launch timer manage loop");
162 ret = timer_secondary_spawn_wait(*sec_lcorep);
163 TEST_ASSERT_SUCCESS(ret, "Secondary process execution failed");
167 test_info->exit_flag = 1;
168 rte_eal_wait_lcore(*mgr_lcorep);
170 #ifdef RTE_LIBRTE_TIMER_DEBUG
171 rte_timer_alt_dump_stats(test_info->timer_data_id, stdout);
174 return test_info->expected_count == test_info->expired_count ?
175 TEST_SUCCESS : TEST_FAILED;
177 } else if (proc_type == RTE_PROC_SECONDARY) {
178 uint64_t ticks, timeout_ms;
179 struct rte_timer *tim;
182 mz = rte_memzone_lookup(TEST_INFO_MZ_NAME);
183 TEST_ASSERT_NOT_NULL(mz, "Couldn't lookup memzone for "
185 test_info = mz->addr;
187 for (i = 0; i < NUM_TIMERS; i++) {
188 rte_mempool_get(test_info->tim_mempool, (void **)&tim);
192 /* generate timeouts between 10 and 160 ms */
193 timeout_ms = ((rte_rand() & 0xF) + 1) * 10;
194 ticks = timeout_ms * rte_get_timer_hz() / MSECPERSEC;
196 ret = rte_timer_alt_reset(test_info->timer_data_id,
198 test_info->mgr_lcore, NULL,
203 test_info->expected_count++;
205 /* randomly leave timer running or stop it */
209 ret = rte_timer_alt_stop(test_info->timer_data_id,
212 test_info->expected_count--;
213 rte_mempool_put(test_info->tim_mempool,
225 #endif /* !RTE_EXEC_ENV_WINDOWS */
227 REGISTER_TEST_COMMAND(timer_secondary_autotest, test_timer_secondary);