4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/queue.h>
41 #include <rte_common.h>
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_per_lcore.h>
45 #include <rte_launch.h>
47 #include <rte_lcore.h>
48 #include <rte_cycles.h>
49 #include <rte_spinlock.h>
50 #include <rte_atomic.h>
58 * - There is a global spinlock and a table of spinlocks (one per lcore).
60 * - The test function takes all of these locks and launches the
61 * ``test_spinlock_per_core()`` function on each core (except the master).
63 * - The function takes the global lock, display something, then releases
65 * - The function takes the per-lcore lock, display something, then releases
68 * - The main function unlocks the per-lcore locks sequentially and
69 * waits between each lock. This triggers the display of a message
70 * for each core, in the correct order. The autotest script checks that
71 * this order is correct.
73 * - A load test is carried out, with all cores attempting to lock a single lock
77 static rte_spinlock_t sl, sl_try;
78 static rte_spinlock_t sl_tab[RTE_MAX_LCORE];
79 static rte_spinlock_recursive_t slr;
80 static unsigned count = 0;
82 static rte_atomic32_t synchro;
85 test_spinlock_per_core(__attribute__((unused)) void *arg)
87 rte_spinlock_lock(&sl);
88 printf("Global lock taken on core %u\n", rte_lcore_id());
89 rte_spinlock_unlock(&sl);
91 rte_spinlock_lock(&sl_tab[rte_lcore_id()]);
92 printf("Hello from core %u !\n", rte_lcore_id());
93 rte_spinlock_unlock(&sl_tab[rte_lcore_id()]);
99 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
101 unsigned id = rte_lcore_id();
103 rte_spinlock_recursive_lock(&slr);
104 printf("Global recursive lock taken on core %u - count = %d\n",
106 rte_spinlock_recursive_lock(&slr);
107 printf("Global recursive lock taken on core %u - count = %d\n",
109 rte_spinlock_recursive_lock(&slr);
110 printf("Global recursive lock taken on core %u - count = %d\n",
113 printf("Hello from within recursive locks from core %u !\n", id);
115 rte_spinlock_recursive_unlock(&slr);
116 printf("Global recursive lock released on core %u - count = %d\n",
118 rte_spinlock_recursive_unlock(&slr);
119 printf("Global recursive lock released on core %u - count = %d\n",
121 rte_spinlock_recursive_unlock(&slr);
122 printf("Global recursive lock released on core %u - count = %d\n",
128 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
129 static uint64_t lock_count[RTE_MAX_LCORE] = {0};
134 load_loop_fn(void *func_param)
136 uint64_t time_diff = 0, begin;
137 uint64_t hz = rte_get_timer_hz();
139 const int use_lock = *(int*)func_param;
140 const unsigned lcore = rte_lcore_id();
142 /* wait synchro for slaves */
143 if (lcore != rte_get_master_lcore())
144 while (rte_atomic32_read(&synchro) == 0);
146 begin = rte_get_timer_cycles();
147 while (time_diff < hz * TIME_MS / 1000) {
149 rte_spinlock_lock(&lk);
152 rte_spinlock_unlock(&lk);
153 /* delay to make lock duty cycle slighlty realistic */
155 time_diff = rte_get_timer_cycles() - begin;
157 lock_count[lcore] = lcount;
162 test_spinlock_perf(void)
167 const unsigned lcore = rte_lcore_id();
169 printf("\nTest with no lock on single core...\n");
171 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
172 memset(lock_count, 0, sizeof(lock_count));
174 printf("\nTest with lock on single core...\n");
177 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
178 memset(lock_count, 0, sizeof(lock_count));
180 printf("\nTest with lock on %u cores...\n", rte_lcore_count());
182 /* Clear synchro and start slaves */
183 rte_atomic32_set(&synchro, 0);
184 rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
186 /* start synchro and launch test on master */
187 rte_atomic32_set(&synchro, 1);
190 rte_eal_mp_wait_lcore();
192 RTE_LCORE_FOREACH(i) {
193 printf("Core [%u] count = %"PRIu64"\n", i, lock_count[i]);
194 total += lock_count[i];
197 printf("Total count = %"PRIu64"\n", total);
203 * Use rte_spinlock_trylock() to trylock a spinlock object,
204 * If it could not lock the object successfully, it would
205 * return immediately and the variable of "count" would be
206 * increased by one per times. the value of "count" could be
207 * checked as the result later.
210 test_spinlock_try(__attribute__((unused)) void *arg)
212 if (rte_spinlock_trylock(&sl_try) == 0) {
213 rte_spinlock_lock(&sl);
215 rte_spinlock_unlock(&sl);
223 * Test rte_eal_get_lcore_state() in addition to spinlocks
224 * as we have "waiting" then "running" lcores.
232 /* slave cores should be waiting: print it */
233 RTE_LCORE_FOREACH_SLAVE(i) {
234 printf("lcore %d state: %d\n", i,
235 (int) rte_eal_get_lcore_state(i));
238 rte_spinlock_init(&sl);
239 rte_spinlock_init(&sl_try);
240 rte_spinlock_recursive_init(&slr);
241 for (i=0; i<RTE_MAX_LCORE; i++)
242 rte_spinlock_init(&sl_tab[i]);
244 rte_spinlock_lock(&sl);
246 RTE_LCORE_FOREACH_SLAVE(i) {
247 rte_spinlock_lock(&sl_tab[i]);
248 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
251 /* slave cores should be busy: print it */
252 RTE_LCORE_FOREACH_SLAVE(i) {
253 printf("lcore %d state: %d\n", i,
254 (int) rte_eal_get_lcore_state(i));
256 rte_spinlock_unlock(&sl);
258 RTE_LCORE_FOREACH_SLAVE(i) {
259 rte_spinlock_unlock(&sl_tab[i]);
263 rte_eal_mp_wait_lcore();
265 rte_spinlock_recursive_lock(&slr);
268 * Try to acquire a lock that we already own
270 if(!rte_spinlock_recursive_trylock(&slr)) {
271 printf("rte_spinlock_recursive_trylock failed on a lock that "
275 rte_spinlock_recursive_unlock(&slr);
277 RTE_LCORE_FOREACH_SLAVE(i) {
278 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
280 rte_spinlock_recursive_unlock(&slr);
281 rte_eal_mp_wait_lcore();
284 * Test if it could return immediately from try-locking a locked object.
285 * Here it will lock the spinlock object first, then launch all the slave
286 * lcores to trylock the same spinlock object.
287 * All the slave lcores should give up try-locking a locked object and
288 * return immediately, and then increase the "count" initialized with zero
290 * We can check if the "count" is finally equal to the number of all slave
291 * lcores to see if the behavior of try-locking a locked spinlock object
294 if (rte_spinlock_trylock(&sl_try) == 0) {
298 RTE_LCORE_FOREACH_SLAVE(i) {
299 rte_eal_remote_launch(test_spinlock_try, NULL, i);
301 rte_eal_mp_wait_lcore();
302 rte_spinlock_unlock(&sl_try);
303 if (rte_spinlock_is_locked(&sl)) {
304 printf("spinlock is locked but it should not be\n");
307 rte_spinlock_lock(&sl);
308 if (count != ( rte_lcore_count() - 1)) {
311 rte_spinlock_unlock(&sl);
314 * Test if it can trylock recursively.
315 * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
316 * object recursively. Here it will try to lock a spinlock object twice.
318 if (rte_spinlock_recursive_trylock(&slr) == 0) {
319 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
322 if (rte_spinlock_recursive_trylock(&slr) == 0) {
323 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
326 rte_spinlock_recursive_unlock(&slr);
327 rte_spinlock_recursive_unlock(&slr);
329 if (test_spinlock_perf() < 0)
335 REGISTER_TEST_COMMAND(spinlock_autotest, test_spinlock);