1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2015 Akamai Technologies. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Intel Corporation nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "test.h" 33 34 #include <stdio.h> 35 #include <unistd.h> 36 #include <inttypes.h> 37 #include <rte_cycles.h> 38 #include <rte_timer.h> 39 #include <rte_common.h> 40 #include <rte_lcore.h> 41 #include <rte_random.h> 42 #include <rte_malloc.h> 43 #include <rte_pause.h> 44 45 #ifdef RTE_EXEC_ENV_LINUX 46 #define usec_delay(us) usleep(us) 47 #else 48 #define usec_delay(us) rte_delay_us(us) 49 #endif 50 51 #define BILLION (1UL << 30) 52 53 #define TEST_DURATION_S 4 /* in seconds */ 54 #define N_TIMERS 50 55 56 static struct rte_timer timer[N_TIMERS]; 57 static unsigned int timer_lcore_id[N_TIMERS]; 58 59 static unsigned int main_lcore; 60 static volatile unsigned int stop_workers; 61 62 static int reload_timer(struct rte_timer *tim); 63 64 RTE_LOG_REGISTER(timer_logtype_test, test.timer, INFO); 65 66 static void 67 timer_cb(struct rte_timer *tim, void *arg __rte_unused) 68 { 69 /* Simulate slow callback function, 100 us. */ 70 rte_delay_us(100); 71 if (tim == &timer[0]) 72 rte_log(RTE_LOG_DEBUG, timer_logtype_test, 73 "------------------------------------------------\n"); 74 rte_log(RTE_LOG_DEBUG, timer_logtype_test, "%s: core %u timer %" 75 PRIuPTR "\n", __func__, rte_lcore_id(), tim - timer); 76 (void)reload_timer(tim); 77 } 78 79 RTE_DEFINE_PER_LCORE(unsigned, n_reset_collisions); 80 81 static int 82 reload_timer(struct rte_timer *tim) 83 { 84 /* Make timer expire roughly when the TSC hits the next BILLION 85 * multiple. Add in timer's index to make them expire in nearly 86 * sorted order. This makes all timers somewhat synchronized, 87 * firing ~2-3 times per second, assuming 2-3 GHz TSCs. 88 */ 89 uint64_t ticks = BILLION - (rte_get_timer_cycles() % BILLION) + 90 (tim - timer); 91 int ret; 92 93 ret = rte_timer_reset(tim, ticks, PERIODICAL, main_lcore, timer_cb, NULL); 94 if (ret != 0) { 95 rte_log(RTE_LOG_DEBUG, timer_logtype_test, 96 "- core %u failed to reset timer %" PRIuPTR " (OK)\n", 97 rte_lcore_id(), tim - timer); 98 RTE_PER_LCORE(n_reset_collisions) += 1; 99 } 100 return ret; 101 } 102 103 static int 104 worker_main_loop(__rte_unused void *arg) 105 { 106 unsigned lcore_id = rte_lcore_id(); 107 unsigned i; 108 109 RTE_PER_LCORE(n_reset_collisions) = 0; 110 111 printf("Starting main loop on core %u\n", lcore_id); 112 113 while (!stop_workers) { 114 /* Wait until the timer manager is running. 115 * We know it's running when we see timer[0] NOT pending. 116 */ 117 if (rte_timer_pending(&timer[0])) { 118 rte_pause(); 119 continue; 120 } 121 122 /* Now, go cause some havoc! 123 * Reload our timers. 124 */ 125 for (i = 0; i < N_TIMERS; i++) { 126 if (timer_lcore_id[i] == lcore_id) 127 (void)reload_timer(&timer[i]); 128 } 129 usec_delay(100*1000); /* sleep 100 ms */ 130 } 131 132 if (RTE_PER_LCORE(n_reset_collisions) != 0) { 133 printf("- core %u, %u reset collisions (OK)\n", 134 lcore_id, RTE_PER_LCORE(n_reset_collisions)); 135 } 136 return 0; 137 } 138 139 static int 140 test_timer_racecond(void) 141 { 142 int ret; 143 uint64_t hz; 144 uint64_t cur_time; 145 uint64_t end_time; 146 int64_t diff = 0; 147 unsigned lcore_id; 148 unsigned i; 149 150 main_lcore = lcore_id = rte_lcore_id(); 151 hz = rte_get_timer_hz(); 152 153 /* init and start timers */ 154 for (i = 0; i < N_TIMERS; i++) { 155 rte_timer_init(&timer[i]); 156 ret = reload_timer(&timer[i]); 157 TEST_ASSERT(ret == 0, "reload_timer failed"); 158 159 /* Distribute timers to workers. 160 * Note that we assign timer[0] to the main. 161 */ 162 timer_lcore_id[i] = lcore_id; 163 lcore_id = rte_get_next_lcore(lcore_id, 1, 1); 164 } 165 166 /* calculate the "end of test" time */ 167 cur_time = rte_get_timer_cycles(); 168 end_time = cur_time + (hz * TEST_DURATION_S); 169 170 /* start worker cores */ 171 stop_workers = 0; 172 printf("Start timer manage race condition test (%u seconds)\n", 173 TEST_DURATION_S); 174 rte_eal_mp_remote_launch(worker_main_loop, NULL, SKIP_MAIN); 175 176 while (diff >= 0) { 177 /* run the timers */ 178 rte_timer_manage(); 179 180 /* wait 100 ms */ 181 usec_delay(100*1000); 182 183 cur_time = rte_get_timer_cycles(); 184 diff = end_time - cur_time; 185 } 186 187 /* stop worker cores */ 188 printf("Stopping timer manage race condition test\n"); 189 stop_workers = 1; 190 rte_eal_mp_wait_lcore(); 191 192 /* stop timers */ 193 for (i = 0; i < N_TIMERS; i++) { 194 ret = rte_timer_stop(&timer[i]); 195 TEST_ASSERT(ret == 0, "rte_timer_stop failed"); 196 } 197 198 return TEST_SUCCESS; 199 } 200 201 REGISTER_TEST_COMMAND(timer_racecond_autotest, test_timer_racecond); 202