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_atomic.h>
13 #include <rte_timer.h>
14 #include <rte_cycles.h>
15 #include <rte_mempool.h>
16 #include <rte_random.h>
21 #define NUM_TIMERS (1 << 20) /* ~1M timers */
22 #define NUM_LCORES_NEEDED 3
23 #define TEST_INFO_MZ_NAME "test_timer_info_mz"
24 #define MSECPERSEC 1E3
26 #define launch_proc(ARGV) \
27 process_dup(ARGV, sizeof(ARGV)/(sizeof(ARGV[0])), __func__)
30 unsigned int mstr_lcore;
31 unsigned int mgr_lcore;
32 unsigned int sec_lcore;
33 uint32_t timer_data_id;
34 volatile int expected_count;
35 volatile int expired_count;
36 struct rte_mempool *tim_mempool;
37 struct rte_timer *expired_timers[NUM_TIMERS];
38 int expired_timers_idx;
39 volatile int exit_flag;
43 timer_secondary_spawn_wait(unsigned int lcore)
46 #ifdef RTE_EXEC_ENV_LINUXAPP
47 char tmp[PATH_MAX] = {0};
48 char prefix[PATH_MAX] = {0};
50 get_current_prefix(tmp, sizeof(tmp));
52 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
54 const char *prefix = "";
56 char const *argv[] = {
59 "--proc-type=secondary",
63 snprintf(coremask, sizeof(coremask), "%x", (1 << lcore));
65 return launch_proc(argv);
69 handle_expired_timer(struct rte_timer *tim)
71 struct test_info *test_info = tim->arg;
73 test_info->expired_count++;
74 test_info->expired_timers[test_info->expired_timers_idx++] = tim;
78 timer_manage_loop(void *arg)
81 uint64_t tick_cycles = TICK_MSECS * rte_get_timer_hz() / MSECPERSEC;
82 uint64_t prev_tsc = 0, cur_tsc, diff_tsc;
83 struct test_info *test_info = arg;
85 while (!test_info->exit_flag) {
86 cur_tsc = rte_rdtsc();
87 diff_tsc = cur_tsc - prev_tsc;
89 if (diff_tsc > tick_cycles) {
90 /* Scan timer list for expired timers */
91 rte_timer_alt_manage(test_info->timer_data_id,
94 handle_expired_timer);
96 /* Return expired timer objects back to mempool */
97 rte_mempool_put_bulk(test_info->tim_mempool,
98 (void **)test_info->expired_timers,
99 test_info->expired_timers_idx);
101 test_info->expired_timers_idx = 0;
113 test_timer_secondary(void)
115 int proc_type = rte_eal_process_type();
116 const struct rte_memzone *mz;
117 struct test_info *test_info;
120 if (proc_type == RTE_PROC_PRIMARY) {
121 mz = rte_memzone_reserve(TEST_INFO_MZ_NAME, sizeof(*test_info),
123 test_info = mz->addr;
124 TEST_ASSERT_NOT_NULL(test_info, "Couldn't allocate memory for "
127 TEST_ASSERT(rte_lcore_count() >= NUM_LCORES_NEEDED,
128 "at least %d lcores needed to run tests",
131 test_info->tim_mempool = rte_mempool_create("test_timer_mp",
132 NUM_TIMERS, sizeof(struct rte_timer), 0, 0,
133 NULL, NULL, NULL, NULL, rte_socket_id(), 0);
135 ret = rte_timer_data_alloc(&test_info->timer_data_id);
136 TEST_ASSERT_SUCCESS(ret, "Failed to allocate timer data "
139 unsigned int *mstr_lcorep = &test_info->mstr_lcore;
140 unsigned int *mgr_lcorep = &test_info->mgr_lcore;
141 unsigned int *sec_lcorep = &test_info->sec_lcore;
143 *mstr_lcorep = rte_get_master_lcore();
144 *mgr_lcorep = rte_get_next_lcore(*mstr_lcorep, 1, 1);
145 *sec_lcorep = rte_get_next_lcore(*mgr_lcorep, 1, 1);
147 ret = rte_eal_remote_launch(timer_manage_loop,
150 TEST_ASSERT_SUCCESS(ret, "Failed to launch timer manage loop");
152 ret = timer_secondary_spawn_wait(*sec_lcorep);
153 TEST_ASSERT_SUCCESS(ret, "Secondary process execution failed");
157 test_info->exit_flag = 1;
158 rte_eal_wait_lcore(*mgr_lcorep);
160 #ifdef RTE_LIBRTE_TIMER_DEBUG
161 rte_timer_alt_dump_stats(test_info->timer_data_id, stdout);
164 return test_info->expected_count == test_info->expired_count ?
165 TEST_SUCCESS : TEST_FAILED;
167 } else if (proc_type == RTE_PROC_SECONDARY) {
168 uint64_t ticks, timeout_ms;
169 struct rte_timer *tim;
172 mz = rte_memzone_lookup(TEST_INFO_MZ_NAME);
173 test_info = mz->addr;
174 TEST_ASSERT_NOT_NULL(test_info, "Couldn't lookup memzone for "
177 for (i = 0; i < NUM_TIMERS; i++) {
178 rte_mempool_get(test_info->tim_mempool, (void **)&tim);
182 /* generate timeouts between 10 and 160 ms */
183 timeout_ms = ((rte_rand() & 0xF) + 1) * 10;
184 ticks = timeout_ms * rte_get_timer_hz() / MSECPERSEC;
186 ret = rte_timer_alt_reset(test_info->timer_data_id,
188 test_info->mgr_lcore, NULL,
193 test_info->expected_count++;
195 /* randomly leave timer running or stop it */
199 ret = rte_timer_alt_stop(test_info->timer_data_id,
202 test_info->expected_count--;
203 rte_mempool_put(test_info->tim_mempool,
215 REGISTER_TEST_COMMAND(timer_secondary_autotest, test_timer_secondary);