1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_common.h>
13 #include <rte_memory.h>
14 #include <rte_per_lcore.h>
15 #include <rte_launch.h>
17 #include <rte_lcore.h>
18 #include <rte_cycles.h>
19 #include <rte_spinlock.h>
20 #include <rte_atomic.h>
28 * - There is a global spinlock and a table of spinlocks (one per lcore).
30 * - The test function takes all of these locks and launches the
31 * ``test_spinlock_per_core()`` function on each core (except the master).
33 * - The function takes the global lock, display something, then releases
35 * - The function takes the per-lcore lock, display something, then releases
38 * - The main function unlocks the per-lcore locks sequentially and
39 * waits between each lock. This triggers the display of a message
40 * for each core, in the correct order. The autotest script checks that
41 * this order is correct.
43 * - A load test is carried out, with all cores attempting to lock a single lock
47 static rte_spinlock_t sl, sl_try;
48 static rte_spinlock_t sl_tab[RTE_MAX_LCORE];
49 static rte_spinlock_recursive_t slr;
50 static unsigned count = 0;
52 static rte_atomic32_t synchro;
55 test_spinlock_per_core(__attribute__((unused)) void *arg)
57 rte_spinlock_lock(&sl);
58 printf("Global lock taken on core %u\n", rte_lcore_id());
59 rte_spinlock_unlock(&sl);
61 rte_spinlock_lock(&sl_tab[rte_lcore_id()]);
62 printf("Hello from core %u !\n", rte_lcore_id());
63 rte_spinlock_unlock(&sl_tab[rte_lcore_id()]);
69 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
71 unsigned id = rte_lcore_id();
73 rte_spinlock_recursive_lock(&slr);
74 printf("Global recursive lock taken on core %u - count = %d\n",
76 rte_spinlock_recursive_lock(&slr);
77 printf("Global recursive lock taken on core %u - count = %d\n",
79 rte_spinlock_recursive_lock(&slr);
80 printf("Global recursive lock taken on core %u - count = %d\n",
83 printf("Hello from within recursive locks from core %u !\n", id);
85 rte_spinlock_recursive_unlock(&slr);
86 printf("Global recursive lock released on core %u - count = %d\n",
88 rte_spinlock_recursive_unlock(&slr);
89 printf("Global recursive lock released on core %u - count = %d\n",
91 rte_spinlock_recursive_unlock(&slr);
92 printf("Global recursive lock released on core %u - count = %d\n",
98 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
99 static uint64_t lock_count[RTE_MAX_LCORE] = {0};
104 load_loop_fn(void *func_param)
106 uint64_t time_diff = 0, begin;
107 uint64_t hz = rte_get_timer_hz();
109 const int use_lock = *(int*)func_param;
110 const unsigned lcore = rte_lcore_id();
112 /* wait synchro for slaves */
113 if (lcore != rte_get_master_lcore())
114 while (rte_atomic32_read(&synchro) == 0);
116 begin = rte_get_timer_cycles();
117 while (time_diff < hz * TIME_MS / 1000) {
119 rte_spinlock_lock(&lk);
122 rte_spinlock_unlock(&lk);
123 /* delay to make lock duty cycle slighlty realistic */
125 time_diff = rte_get_timer_cycles() - begin;
127 lock_count[lcore] = lcount;
132 test_spinlock_perf(void)
137 const unsigned lcore = rte_lcore_id();
139 printf("\nTest with no lock on single core...\n");
141 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
142 memset(lock_count, 0, sizeof(lock_count));
144 printf("\nTest with lock on single core...\n");
147 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
148 memset(lock_count, 0, sizeof(lock_count));
150 printf("\nTest with lock on %u cores...\n", rte_lcore_count());
152 /* Clear synchro and start slaves */
153 rte_atomic32_set(&synchro, 0);
154 rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
156 /* start synchro and launch test on master */
157 rte_atomic32_set(&synchro, 1);
160 rte_eal_mp_wait_lcore();
162 RTE_LCORE_FOREACH(i) {
163 printf("Core [%u] count = %"PRIu64"\n", i, lock_count[i]);
164 total += lock_count[i];
167 printf("Total count = %"PRIu64"\n", total);
173 * Use rte_spinlock_trylock() to trylock a spinlock object,
174 * If it could not lock the object successfully, it would
175 * return immediately and the variable of "count" would be
176 * increased by one per times. the value of "count" could be
177 * checked as the result later.
180 test_spinlock_try(__attribute__((unused)) void *arg)
182 if (rte_spinlock_trylock(&sl_try) == 0) {
183 rte_spinlock_lock(&sl);
185 rte_spinlock_unlock(&sl);
193 * Test rte_eal_get_lcore_state() in addition to spinlocks
194 * as we have "waiting" then "running" lcores.
202 /* slave cores should be waiting: print it */
203 RTE_LCORE_FOREACH_SLAVE(i) {
204 printf("lcore %d state: %d\n", i,
205 (int) rte_eal_get_lcore_state(i));
208 rte_spinlock_init(&sl);
209 rte_spinlock_init(&sl_try);
210 rte_spinlock_recursive_init(&slr);
211 for (i=0; i<RTE_MAX_LCORE; i++)
212 rte_spinlock_init(&sl_tab[i]);
214 rte_spinlock_lock(&sl);
216 RTE_LCORE_FOREACH_SLAVE(i) {
217 rte_spinlock_lock(&sl_tab[i]);
218 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
221 /* slave cores should be busy: print it */
222 RTE_LCORE_FOREACH_SLAVE(i) {
223 printf("lcore %d state: %d\n", i,
224 (int) rte_eal_get_lcore_state(i));
226 rte_spinlock_unlock(&sl);
228 RTE_LCORE_FOREACH_SLAVE(i) {
229 rte_spinlock_unlock(&sl_tab[i]);
233 rte_eal_mp_wait_lcore();
235 rte_spinlock_recursive_lock(&slr);
238 * Try to acquire a lock that we already own
240 if(!rte_spinlock_recursive_trylock(&slr)) {
241 printf("rte_spinlock_recursive_trylock failed on a lock that "
245 rte_spinlock_recursive_unlock(&slr);
247 RTE_LCORE_FOREACH_SLAVE(i) {
248 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
250 rte_spinlock_recursive_unlock(&slr);
251 rte_eal_mp_wait_lcore();
254 * Test if it could return immediately from try-locking a locked object.
255 * Here it will lock the spinlock object first, then launch all the slave
256 * lcores to trylock the same spinlock object.
257 * All the slave lcores should give up try-locking a locked object and
258 * return immediately, and then increase the "count" initialized with zero
260 * We can check if the "count" is finally equal to the number of all slave
261 * lcores to see if the behavior of try-locking a locked spinlock object
264 if (rte_spinlock_trylock(&sl_try) == 0) {
268 RTE_LCORE_FOREACH_SLAVE(i) {
269 rte_eal_remote_launch(test_spinlock_try, NULL, i);
271 rte_eal_mp_wait_lcore();
272 rte_spinlock_unlock(&sl_try);
273 if (rte_spinlock_is_locked(&sl)) {
274 printf("spinlock is locked but it should not be\n");
277 rte_spinlock_lock(&sl);
278 if (count != ( rte_lcore_count() - 1)) {
281 rte_spinlock_unlock(&sl);
284 * Test if it can trylock recursively.
285 * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
286 * object recursively. Here it will try to lock a spinlock object twice.
288 if (rte_spinlock_recursive_trylock(&slr) == 0) {
289 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
292 if (rte_spinlock_recursive_trylock(&slr) == 0) {
293 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
296 rte_spinlock_recursive_unlock(&slr);
297 rte_spinlock_recursive_unlock(&slr);
299 if (test_spinlock_perf() < 0)
305 REGISTER_TEST_COMMAND(spinlock_autotest, test_spinlock);