4 * Copyright(c) 2010-2013 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.
40 #include <sys/queue.h>
42 #include <cmdline_parse.h>
44 #include <rte_common.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_per_lcore.h>
48 #include <rte_launch.h>
49 #include <rte_tailq.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_cycles.h>
54 #include <rte_spinlock.h>
55 #include <rte_atomic.h>
63 * - There is a global spinlock and a table of spinlocks (one per lcore).
65 * - The test function takes all of these locks and launches the
66 * ``test_spinlock_per_core()`` function on each core (except the master).
68 * - The function takes the global lock, display something, then releases
70 * - The function takes the per-lcore lock, display something, then releases
73 * - The main function unlocks the per-lcore locks sequentially and
74 * waits between each lock. This triggers the display of a message
75 * for each core, in the correct order. The autotest script checks that
76 * this order is correct.
78 * - A load test is carried out, with all cores attempting to lock a single lock
82 static rte_spinlock_t sl, sl_try;
83 static rte_spinlock_t sl_tab[RTE_MAX_LCORE];
84 static rte_spinlock_recursive_t slr;
85 static unsigned count = 0;
87 static rte_atomic32_t synchro;
90 test_spinlock_per_core(__attribute__((unused)) void *arg)
92 rte_spinlock_lock(&sl);
93 printf("Global lock taken on core %u\n", rte_lcore_id());
94 rte_spinlock_unlock(&sl);
96 rte_spinlock_lock(&sl_tab[rte_lcore_id()]);
97 printf("Hello from core %u !\n", rte_lcore_id());
98 rte_spinlock_unlock(&sl_tab[rte_lcore_id()]);
104 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
106 unsigned id = rte_lcore_id();
108 rte_spinlock_recursive_lock(&slr);
109 printf("Global recursive lock taken on core %u - count = %d\n",
111 rte_spinlock_recursive_lock(&slr);
112 printf("Global recursive lock taken on core %u - count = %d\n",
114 rte_spinlock_recursive_lock(&slr);
115 printf("Global recursive lock taken on core %u - count = %d\n",
118 printf("Hello from within recursive locks from core %u !\n", id);
120 rte_spinlock_recursive_unlock(&slr);
121 printf("Global recursive lock released on core %u - count = %d\n",
123 rte_spinlock_recursive_unlock(&slr);
124 printf("Global recursive lock released on core %u - count = %d\n",
126 rte_spinlock_recursive_unlock(&slr);
127 printf("Global recursive lock released on core %u - count = %d\n",
133 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
134 static uint64_t lock_count[RTE_MAX_LCORE] = {0};
139 load_loop_fn(void *func_param)
141 uint64_t time_diff = 0, begin;
142 uint64_t hz = rte_get_timer_hz();
144 const int use_lock = *(int*)func_param;
145 const unsigned lcore = rte_lcore_id();
147 /* wait synchro for slaves */
148 if (lcore != rte_get_master_lcore())
149 while (rte_atomic32_read(&synchro) == 0);
151 begin = rte_get_timer_cycles();
152 while (time_diff / hz < TIME_S) {
154 rte_spinlock_lock(&lk);
157 rte_spinlock_unlock(&lk);
158 /* delay to make lock duty cycle slighlty realistic */
160 time_diff = rte_get_timer_cycles() - begin;
162 lock_count[lcore] = lcount;
167 test_spinlock_perf(void)
172 const unsigned lcore = rte_lcore_id();
174 printf("\nTest with no lock on single core...\n");
176 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
177 memset(lock_count, 0, sizeof(lock_count));
179 printf("\nTest with lock on single core...\n");
182 printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
183 memset(lock_count, 0, sizeof(lock_count));
185 printf("\nTest with lock on %u cores...\n", rte_lcore_count());
187 /* Clear synchro and start slaves */
188 rte_atomic32_set(&synchro, 0);
189 rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
191 /* start synchro and launch test on master */
192 rte_atomic32_set(&synchro, 1);
195 rte_eal_mp_wait_lcore();
197 RTE_LCORE_FOREACH(i) {
198 printf("Core [%u] count = %"PRIu64"\n", i, lock_count[i]);
199 total += lock_count[i];
202 printf("Total count = %"PRIu64"\n", total);
208 * Use rte_spinlock_trylock() to trylock a spinlock object,
209 * If it could not lock the object sucessfully, it would
210 * return immediately and the variable of "count" would be
211 * increased by one per times. the value of "count" could be
212 * checked as the result later.
215 test_spinlock_try(__attribute__((unused)) void *arg)
217 if (rte_spinlock_trylock(&sl_try) == 0) {
218 rte_spinlock_lock(&sl);
220 rte_spinlock_unlock(&sl);
228 * Test rte_eal_get_lcore_state() in addition to spinlocks
229 * as we have "waiting" then "running" lcores.
237 /* slave cores should be waiting: print it */
238 RTE_LCORE_FOREACH_SLAVE(i) {
239 printf("lcore %d state: %d\n", i,
240 (int) rte_eal_get_lcore_state(i));
243 rte_spinlock_init(&sl);
244 rte_spinlock_init(&sl_try);
245 rte_spinlock_recursive_init(&slr);
246 for (i=0; i<RTE_MAX_LCORE; i++)
247 rte_spinlock_init(&sl_tab[i]);
249 rte_spinlock_lock(&sl);
251 RTE_LCORE_FOREACH_SLAVE(i) {
252 rte_spinlock_lock(&sl_tab[i]);
253 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
256 /* slave cores should be busy: print it */
257 RTE_LCORE_FOREACH_SLAVE(i) {
258 printf("lcore %d state: %d\n", i,
259 (int) rte_eal_get_lcore_state(i));
261 rte_spinlock_unlock(&sl);
263 RTE_LCORE_FOREACH_SLAVE(i) {
264 rte_spinlock_unlock(&sl_tab[i]);
268 rte_eal_mp_wait_lcore();
270 rte_spinlock_recursive_lock(&slr);
273 * Try to acquire a lock that we already own
275 if(!rte_spinlock_recursive_trylock(&slr)) {
276 printf("rte_spinlock_recursive_trylock failed on a lock that "
280 rte_spinlock_recursive_unlock(&slr);
282 RTE_LCORE_FOREACH_SLAVE(i) {
283 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
285 rte_spinlock_recursive_unlock(&slr);
286 rte_eal_mp_wait_lcore();
289 * Test if it could return immediately from try-locking a locked object.
290 * Here it will lock the spinlock object first, then launch all the slave
291 * lcores to trylock the same spinlock object.
292 * All the slave lcores should give up try-locking a locked object and
293 * return immediately, and then increase the "count" initialized with zero
295 * We can check if the "count" is finally equal to the number of all slave
296 * lcores to see if the behavior of try-locking a locked spinlock object
299 if (rte_spinlock_trylock(&sl_try) == 0) {
303 RTE_LCORE_FOREACH_SLAVE(i) {
304 rte_eal_remote_launch(test_spinlock_try, NULL, i);
306 rte_eal_mp_wait_lcore();
307 rte_spinlock_unlock(&sl_try);
308 if (rte_spinlock_is_locked(&sl)) {
309 printf("spinlock is locked but it should not be\n");
312 rte_spinlock_lock(&sl);
313 if (count != ( rte_lcore_count() - 1)) {
316 rte_spinlock_unlock(&sl);
319 * Test if it can trylock recursively.
320 * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
321 * object recursively. Here it will try to lock a spinlock object twice.
323 if (rte_spinlock_recursive_trylock(&slr) == 0) {
324 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
327 if (rte_spinlock_recursive_trylock(&slr) == 0) {
328 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
331 rte_spinlock_recursive_unlock(&slr);
332 rte_spinlock_recursive_unlock(&slr);
334 if (test_spinlock_perf() < 0)