4 * Copyright(c) 2010-2012 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>
61 * - There is a global spinlock and a table of spinlocks (one per lcore).
63 * - The test function takes all of these locks and launches the
64 * ``test_spinlock_per_core()`` function on each core (except the master).
66 * - The function takes the global lock, display something, then releases
68 * - The function takes the per-lcore lock, display something, then releases
71 * - The main function unlocks the per-lcore locks sequentially and
72 * waits between each lock. This triggers the display of a message
73 * for each core, in the correct order. The autotest script checks that
74 * this order is correct.
76 * - A load test is carried out, with all cores attempting to lock a single lock
80 static rte_spinlock_t sl, sl_try;
81 static rte_spinlock_t sl_tab[RTE_MAX_LCORE];
82 static rte_spinlock_recursive_t slr;
83 static unsigned count;
86 test_spinlock_per_core(__attribute__((unused)) void *arg)
88 rte_spinlock_lock(&sl);
89 printf("Global lock taken on core %u\n", rte_lcore_id());
90 rte_spinlock_unlock(&sl);
92 rte_spinlock_lock(&sl_tab[rte_lcore_id()]);
93 printf("Hello from core %u !\n", rte_lcore_id());
94 rte_spinlock_unlock(&sl_tab[rte_lcore_id()]);
100 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
102 unsigned id = rte_lcore_id();
104 rte_spinlock_recursive_lock(&slr);
105 printf("Global recursive lock taken on core %u - count = %d\n",
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",
114 printf("Hello from within recursive locks from core %u !\n", id);
116 rte_spinlock_recursive_unlock(&slr);
117 printf("Global recursive lock released on core %u - count = %d\n",
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",
129 static volatile int count1, count2;
130 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
131 static unsigned int max = 10000000; /* 10M */
132 static volatile uint64_t looptime[RTE_MAX_LCORE];
135 load_loop_fn(__attribute__((unused)) void *dummy)
138 begin = rte_get_hpet_cycles();
140 for ( i = 0; i < max; i++) {
141 rte_spinlock_lock(&lk);
143 rte_spinlock_unlock(&lk);
146 end = rte_get_hpet_cycles();
147 looptime[rte_lcore_id()] = end - begin;
152 test_spinlock_load(void)
154 if (rte_lcore_count()<= 1) {
155 printf("no cores counted\n");
158 printf ("Running %u tests.......\n", max);
159 printf ("Number of cores = %u\n", rte_lcore_count());
161 rte_eal_mp_remote_launch(load_loop_fn, NULL , CALL_MASTER);
162 rte_eal_mp_wait_lcore();
165 uint64_t avgtime = 0;
167 RTE_LCORE_FOREACH(k) {
168 printf("Core [%u] time = %"PRIu64"\n", k, looptime[k]);
169 avgtime += looptime[k];
172 avgtime = avgtime / rte_lcore_count();
173 printf("Average time = %"PRIu64"\n", avgtime);
176 check = max * rte_lcore_count();
177 if (count1 == check && count2 != check)
178 printf("Passed Load test\n");
180 printf("Failed load test\n");
187 * Use rte_spinlock_trylock() to trylock a spinlock object,
188 * If it could not lock the object sucessfully, it would
189 * return immediately and the variable of "count" would be
190 * increased by one per times. the value of "count" could be
191 * checked as the result later.
194 test_spinlock_try(__attribute__((unused)) void *arg)
196 if (rte_spinlock_trylock(&sl_try) == 0) {
197 rte_spinlock_lock(&sl);
199 rte_spinlock_unlock(&sl);
207 * Test rte_eal_get_lcore_state() in addition to spinlocks
208 * as we have "waiting" then "running" lcores.
216 /* slave cores should be waiting: print it */
217 RTE_LCORE_FOREACH_SLAVE(i) {
218 printf("lcore %d state: %d\n", i,
219 (int) rte_eal_get_lcore_state(i));
222 rte_spinlock_init(&sl);
223 rte_spinlock_init(&sl_try);
224 rte_spinlock_recursive_init(&slr);
225 for (i=0; i<RTE_MAX_LCORE; i++)
226 rte_spinlock_init(&sl_tab[i]);
228 rte_spinlock_lock(&sl);
230 RTE_LCORE_FOREACH_SLAVE(i) {
231 rte_spinlock_lock(&sl_tab[i]);
232 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
235 /* slave cores should be busy: print it */
236 RTE_LCORE_FOREACH_SLAVE(i) {
237 printf("lcore %d state: %d\n", i,
238 (int) rte_eal_get_lcore_state(i));
240 rte_spinlock_unlock(&sl);
242 RTE_LCORE_FOREACH_SLAVE(i) {
243 rte_spinlock_unlock(&sl_tab[i]);
247 rte_eal_mp_wait_lcore();
249 if (test_spinlock_load()<0)
252 rte_spinlock_recursive_lock(&slr);
255 * Try to acquire a lock that we already own
257 if(!rte_spinlock_recursive_trylock(&slr)) {
258 printf("rte_spinlock_recursive_trylock failed on a lock that "
262 rte_spinlock_recursive_unlock(&slr);
264 RTE_LCORE_FOREACH_SLAVE(i) {
265 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
267 rte_spinlock_recursive_unlock(&slr);
268 rte_eal_mp_wait_lcore();
271 * Test if it could return immediately from try-locking a locked object.
272 * Here it will lock the spinlock object first, then launch all the slave
273 * lcores to trylock the same spinlock object.
274 * All the slave lcores should give up try-locking a locked object and
275 * return immediately, and then increase the "count" initialized with zero
277 * We can check if the "count" is finally equal to the number of all slave
278 * lcores to see if the behavior of try-locking a locked spinlock object
281 if (rte_spinlock_trylock(&sl_try) == 0) {
285 RTE_LCORE_FOREACH_SLAVE(i) {
286 rte_eal_remote_launch(test_spinlock_try, NULL, i);
288 rte_eal_mp_wait_lcore();
289 rte_spinlock_unlock(&sl_try);
290 if (rte_spinlock_is_locked(&sl)) {
291 printf("spinlock is locked but it should not be\n");
294 rte_spinlock_lock(&sl);
295 if (count != ( rte_lcore_count() - 1)) {
298 rte_spinlock_unlock(&sl);
301 * Test if it can trylock recursively.
302 * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
303 * object recursively. Here it will try to lock a spinlock object twice.
305 if (rte_spinlock_recursive_trylock(&slr) == 0) {
306 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
309 if (rte_spinlock_recursive_trylock(&slr) == 0) {
310 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
313 rte_spinlock_recursive_unlock(&slr);
314 rte_spinlock_recursive_unlock(&slr);