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 <cmdline_parse.h>
43 #include <rte_common.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_per_lcore.h>
47 #include <rte_launch.h>
48 #include <rte_tailq.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_cycles.h>
53 #include <rte_spinlock.h>
54 #include <rte_atomic.h>
62 * - There is a global spinlock and a table of spinlocks (one per lcore).
64 * - The test function takes all of these locks and launches the
65 * ``test_spinlock_per_core()`` function on each core (except the master).
67 * - The function takes the global lock, display something, then releases
69 * - The function takes the per-lcore lock, display something, then releases
72 * - The main function unlocks the per-lcore locks sequentially and
73 * waits between each lock. This triggers the display of a message
74 * for each core, in the correct order. The autotest script checks that
75 * this order is correct.
77 * - A load test is carried out, with all cores attempting to lock a single lock
81 static rte_spinlock_t sl, sl_try;
82 static rte_spinlock_t sl_tab[RTE_MAX_LCORE];
83 static rte_spinlock_recursive_t slr;
84 static unsigned count = 0;
86 static rte_atomic32_t synchro;
89 test_spinlock_per_core(__attribute__((unused)) void *arg)
91 rte_spinlock_lock(&sl);
92 printf("Global lock taken on core %u\n", rte_lcore_id());
93 rte_spinlock_unlock(&sl);
95 rte_spinlock_lock(&sl_tab[rte_lcore_id()]);
96 printf("Hello from core %u !\n", rte_lcore_id());
97 rte_spinlock_unlock(&sl_tab[rte_lcore_id()]);
103 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
105 unsigned id = rte_lcore_id();
107 rte_spinlock_recursive_lock(&slr);
108 printf("Global recursive lock taken on core %u - count = %d\n",
110 rte_spinlock_recursive_lock(&slr);
111 printf("Global recursive lock taken on core %u - count = %d\n",
113 rte_spinlock_recursive_lock(&slr);
114 printf("Global recursive lock taken on core %u - count = %d\n",
117 printf("Hello from within recursive locks from core %u !\n", id);
119 rte_spinlock_recursive_unlock(&slr);
120 printf("Global recursive lock released on core %u - count = %d\n",
122 rte_spinlock_recursive_unlock(&slr);
123 printf("Global recursive lock released on core %u - count = %d\n",
125 rte_spinlock_recursive_unlock(&slr);
126 printf("Global recursive lock released on core %u - count = %d\n",
132 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
133 static uint64_t lock_count[RTE_MAX_LCORE] = {0};
138 load_loop_fn(void *func_param)
140 uint64_t time_diff = 0, begin;
141 uint64_t hz = rte_get_timer_hz();
143 const int use_lock = *(int*)func_param;
144 const unsigned lcore = rte_lcore_id();
146 /* wait synchro for slaves */
147 if (lcore != rte_get_master_lcore())
148 while (rte_atomic32_read(&synchro) == 0);
150 begin = rte_get_timer_cycles();
151 while (time_diff / hz < TIME_S) {
153 rte_spinlock_lock(&lk);
156 rte_spinlock_unlock(&lk);
157 /* delay to make lock duty cycle slighlty realistic */
159 time_diff = rte_get_timer_cycles() - begin;
161 lock_count[lcore] = lcount;
166 test_spinlock_perf(void)
171 const unsigned lcore = rte_lcore_id();
173 printf("\nTest with no lock on single core...\n");
175 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
176 memset(lock_count, 0, sizeof(lock_count));
178 printf("\nTest with lock on single core...\n");
181 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
182 memset(lock_count, 0, sizeof(lock_count));
184 printf("\nTest with lock on %u cores...\n", rte_lcore_count());
186 /* Clear synchro and start slaves */
187 rte_atomic32_set(&synchro, 0);
188 rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
190 /* start synchro and launch test on master */
191 rte_atomic32_set(&synchro, 1);
194 rte_eal_mp_wait_lcore();
196 RTE_LCORE_FOREACH(i) {
197 printf("Core [%u] count = %"PRIu64"\n", i, lock_count[i]);
198 total += lock_count[i];
201 printf("Total count = %"PRIu64"\n", total);
207 * Use rte_spinlock_trylock() to trylock a spinlock object,
208 * If it could not lock the object sucessfully, it would
209 * return immediately and the variable of "count" would be
210 * increased by one per times. the value of "count" could be
211 * checked as the result later.
214 test_spinlock_try(__attribute__((unused)) void *arg)
216 if (rte_spinlock_trylock(&sl_try) == 0) {
217 rte_spinlock_lock(&sl);
219 rte_spinlock_unlock(&sl);
227 * Test rte_eal_get_lcore_state() in addition to spinlocks
228 * as we have "waiting" then "running" lcores.
236 /* slave cores should be waiting: print it */
237 RTE_LCORE_FOREACH_SLAVE(i) {
238 printf("lcore %d state: %d\n", i,
239 (int) rte_eal_get_lcore_state(i));
242 rte_spinlock_init(&sl);
243 rte_spinlock_init(&sl_try);
244 rte_spinlock_recursive_init(&slr);
245 for (i=0; i<RTE_MAX_LCORE; i++)
246 rte_spinlock_init(&sl_tab[i]);
248 rte_spinlock_lock(&sl);
250 RTE_LCORE_FOREACH_SLAVE(i) {
251 rte_spinlock_lock(&sl_tab[i]);
252 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
255 /* slave cores should be busy: print it */
256 RTE_LCORE_FOREACH_SLAVE(i) {
257 printf("lcore %d state: %d\n", i,
258 (int) rte_eal_get_lcore_state(i));
260 rte_spinlock_unlock(&sl);
262 RTE_LCORE_FOREACH_SLAVE(i) {
263 rte_spinlock_unlock(&sl_tab[i]);
267 rte_eal_mp_wait_lcore();
269 rte_spinlock_recursive_lock(&slr);
272 * Try to acquire a lock that we already own
274 if(!rte_spinlock_recursive_trylock(&slr)) {
275 printf("rte_spinlock_recursive_trylock failed on a lock that "
279 rte_spinlock_recursive_unlock(&slr);
281 RTE_LCORE_FOREACH_SLAVE(i) {
282 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
284 rte_spinlock_recursive_unlock(&slr);
285 rte_eal_mp_wait_lcore();
288 * Test if it could return immediately from try-locking a locked object.
289 * Here it will lock the spinlock object first, then launch all the slave
290 * lcores to trylock the same spinlock object.
291 * All the slave lcores should give up try-locking a locked object and
292 * return immediately, and then increase the "count" initialized with zero
294 * We can check if the "count" is finally equal to the number of all slave
295 * lcores to see if the behavior of try-locking a locked spinlock object
298 if (rte_spinlock_trylock(&sl_try) == 0) {
302 RTE_LCORE_FOREACH_SLAVE(i) {
303 rte_eal_remote_launch(test_spinlock_try, NULL, i);
305 rte_eal_mp_wait_lcore();
306 rte_spinlock_unlock(&sl_try);
307 if (rte_spinlock_is_locked(&sl)) {
308 printf("spinlock is locked but it should not be\n");
311 rte_spinlock_lock(&sl);
312 if (count != ( rte_lcore_count() - 1)) {
315 rte_spinlock_unlock(&sl);
318 * Test if it can trylock recursively.
319 * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
320 * object recursively. Here it will try to lock a spinlock object twice.
322 if (rte_spinlock_recursive_trylock(&slr) == 0) {
323 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
326 if (rte_spinlock_recursive_trylock(&slr) == 0) {
327 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
330 rte_spinlock_recursive_unlock(&slr);
331 rte_spinlock_recursive_unlock(&slr);
333 if (test_spinlock_perf() < 0)