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(__rte_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(__rte_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 time_count[RTE_MAX_LCORE] = {0};
101 #define MAX_LOOP 10000
104 load_loop_fn(void *func_param)
106 uint64_t time_diff = 0, begin;
107 uint64_t hz = rte_get_timer_hz();
108 volatile uint64_t lcount = 0;
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 (lcount < MAX_LOOP) {
119 rte_spinlock_lock(&lk);
122 rte_spinlock_unlock(&lk);
124 time_diff = rte_get_timer_cycles() - begin;
125 time_count[lcore] = time_diff * 1000000 / hz;
130 test_spinlock_perf(void)
135 const unsigned lcore = rte_lcore_id();
137 printf("\nTest with no lock on single core...\n");
139 printf("Core [%u] Cost Time = %"PRIu64" us\n", lcore,
141 memset(time_count, 0, sizeof(time_count));
143 printf("\nTest with lock on single core...\n");
146 printf("Core [%u] Cost Time = %"PRIu64" us\n", lcore,
148 memset(time_count, 0, sizeof(time_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] Cost Time = %"PRIu64" us\n", i,
165 total += time_count[i];
168 printf("Total Cost Time = %"PRIu64" us\n", total);
174 * Use rte_spinlock_trylock() to trylock a spinlock object,
175 * If it could not lock the object successfully, it would
176 * return immediately and the variable of "count" would be
177 * increased by one per times. the value of "count" could be
178 * checked as the result later.
181 test_spinlock_try(__rte_unused void *arg)
183 if (rte_spinlock_trylock(&sl_try) == 0) {
184 rte_spinlock_lock(&sl);
186 rte_spinlock_unlock(&sl);
194 * Test rte_eal_get_lcore_state() in addition to spinlocks
195 * as we have "waiting" then "running" lcores.
203 /* slave cores should be waiting: print it */
204 RTE_LCORE_FOREACH_SLAVE(i) {
205 printf("lcore %d state: %d\n", i,
206 (int) rte_eal_get_lcore_state(i));
209 rte_spinlock_init(&sl);
210 rte_spinlock_init(&sl_try);
211 rte_spinlock_recursive_init(&slr);
212 for (i=0; i<RTE_MAX_LCORE; i++)
213 rte_spinlock_init(&sl_tab[i]);
215 rte_spinlock_lock(&sl);
217 RTE_LCORE_FOREACH_SLAVE(i) {
218 rte_spinlock_lock(&sl_tab[i]);
219 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
222 /* slave cores should be busy: print it */
223 RTE_LCORE_FOREACH_SLAVE(i) {
224 printf("lcore %d state: %d\n", i,
225 (int) rte_eal_get_lcore_state(i));
227 rte_spinlock_unlock(&sl);
229 RTE_LCORE_FOREACH_SLAVE(i) {
230 rte_spinlock_unlock(&sl_tab[i]);
234 rte_eal_mp_wait_lcore();
236 rte_spinlock_recursive_lock(&slr);
239 * Try to acquire a lock that we already own
241 if(!rte_spinlock_recursive_trylock(&slr)) {
242 printf("rte_spinlock_recursive_trylock failed on a lock that "
246 rte_spinlock_recursive_unlock(&slr);
248 RTE_LCORE_FOREACH_SLAVE(i) {
249 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
251 rte_spinlock_recursive_unlock(&slr);
252 rte_eal_mp_wait_lcore();
255 * Test if it could return immediately from try-locking a locked object.
256 * Here it will lock the spinlock object first, then launch all the slave
257 * lcores to trylock the same spinlock object.
258 * All the slave lcores should give up try-locking a locked object and
259 * return immediately, and then increase the "count" initialized with zero
261 * We can check if the "count" is finally equal to the number of all slave
262 * lcores to see if the behavior of try-locking a locked spinlock object
265 if (rte_spinlock_trylock(&sl_try) == 0) {
269 RTE_LCORE_FOREACH_SLAVE(i) {
270 rte_eal_remote_launch(test_spinlock_try, NULL, i);
272 rte_eal_mp_wait_lcore();
273 rte_spinlock_unlock(&sl_try);
274 if (rte_spinlock_is_locked(&sl)) {
275 printf("spinlock is locked but it should not be\n");
278 rte_spinlock_lock(&sl);
279 if (count != ( rte_lcore_count() - 1)) {
282 rte_spinlock_unlock(&sl);
285 * Test if it can trylock recursively.
286 * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
287 * object recursively. Here it will try to lock a spinlock object twice.
289 if (rte_spinlock_recursive_trylock(&slr) == 0) {
290 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
293 if (rte_spinlock_recursive_trylock(&slr) == 0) {
294 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
297 rte_spinlock_recursive_unlock(&slr);
298 rte_spinlock_recursive_unlock(&slr);
300 if (test_spinlock_perf() < 0)
306 REGISTER_TEST_COMMAND(spinlock_autotest, test_spinlock);