a93f1baa8addaa18a54e9dc00210ae03fffde65d
[deb_dpdk.git] / test / test / test_spinlock.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <inttypes.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/queue.h>
40
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>
46 #include <rte_eal.h>
47 #include <rte_lcore.h>
48 #include <rte_cycles.h>
49 #include <rte_spinlock.h>
50 #include <rte_atomic.h>
51
52 #include "test.h"
53
54 /*
55  * Spinlock test
56  * =============
57  *
58  * - There is a global spinlock and a table of spinlocks (one per lcore).
59  *
60  * - The test function takes all of these locks and launches the
61  *   ``test_spinlock_per_core()`` function on each core (except the master).
62  *
63  *   - The function takes the global lock, display something, then releases
64  *     the global lock.
65  *   - The function takes the per-lcore lock, display something, then releases
66  *     the per-core lock.
67  *
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.
72  *
73  * - A load test is carried out, with all cores attempting to lock a single lock
74  *   multiple times
75  */
76
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;
81
82 static rte_atomic32_t synchro;
83
84 static int
85 test_spinlock_per_core(__attribute__((unused)) void *arg)
86 {
87         rte_spinlock_lock(&sl);
88         printf("Global lock taken on core %u\n", rte_lcore_id());
89         rte_spinlock_unlock(&sl);
90
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()]);
94
95         return 0;
96 }
97
98 static int
99 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
100 {
101         unsigned id = rte_lcore_id();
102
103         rte_spinlock_recursive_lock(&slr);
104         printf("Global recursive lock taken on core %u - count = %d\n",
105                id, slr.count);
106         rte_spinlock_recursive_lock(&slr);
107         printf("Global recursive lock taken on core %u - count = %d\n",
108                id, slr.count);
109         rte_spinlock_recursive_lock(&slr);
110         printf("Global recursive lock taken on core %u - count = %d\n",
111                id, slr.count);
112
113         printf("Hello from within recursive locks from core %u !\n", id);
114
115         rte_spinlock_recursive_unlock(&slr);
116         printf("Global recursive lock released on core %u - count = %d\n",
117                id, slr.count);
118         rte_spinlock_recursive_unlock(&slr);
119         printf("Global recursive lock released on core %u - count = %d\n",
120                id, slr.count);
121         rte_spinlock_recursive_unlock(&slr);
122         printf("Global recursive lock released on core %u - count = %d\n",
123                id, slr.count);
124
125         return 0;
126 }
127
128 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
129 static uint64_t lock_count[RTE_MAX_LCORE] = {0};
130
131 #define TIME_MS 100
132
133 static int
134 load_loop_fn(void *func_param)
135 {
136         uint64_t time_diff = 0, begin;
137         uint64_t hz = rte_get_timer_hz();
138         uint64_t lcount = 0;
139         const int use_lock = *(int*)func_param;
140         const unsigned lcore = rte_lcore_id();
141
142         /* wait synchro for slaves */
143         if (lcore != rte_get_master_lcore())
144                 while (rte_atomic32_read(&synchro) == 0);
145
146         begin = rte_get_timer_cycles();
147         while (time_diff < hz * TIME_MS / 1000) {
148                 if (use_lock)
149                         rte_spinlock_lock(&lk);
150                 lcount++;
151                 if (use_lock)
152                         rte_spinlock_unlock(&lk);
153                 /* delay to make lock duty cycle slighlty realistic */
154                 rte_delay_us(1);
155                 time_diff = rte_get_timer_cycles() - begin;
156         }
157         lock_count[lcore] = lcount;
158         return 0;
159 }
160
161 static int
162 test_spinlock_perf(void)
163 {
164         unsigned int i;
165         uint64_t total = 0;
166         int lock = 0;
167         const unsigned lcore = rte_lcore_id();
168
169         printf("\nTest with no lock on single core...\n");
170         load_loop_fn(&lock);
171         printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
172         memset(lock_count, 0, sizeof(lock_count));
173
174         printf("\nTest with lock on single core...\n");
175         lock = 1;
176         load_loop_fn(&lock);
177         printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
178         memset(lock_count, 0, sizeof(lock_count));
179
180         printf("\nTest with lock on %u cores...\n", rte_lcore_count());
181
182         /* Clear synchro and start slaves */
183         rte_atomic32_set(&synchro, 0);
184         rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
185
186         /* start synchro and launch test on master */
187         rte_atomic32_set(&synchro, 1);
188         load_loop_fn(&lock);
189
190         rte_eal_mp_wait_lcore();
191
192         RTE_LCORE_FOREACH(i) {
193                 printf("Core [%u] count = %"PRIu64"\n", i, lock_count[i]);
194                 total += lock_count[i];
195         }
196
197         printf("Total count = %"PRIu64"\n", total);
198
199         return 0;
200 }
201
202 /*
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.
208  */
209 static int
210 test_spinlock_try(__attribute__((unused)) void *arg)
211 {
212         if (rte_spinlock_trylock(&sl_try) == 0) {
213                 rte_spinlock_lock(&sl);
214                 count ++;
215                 rte_spinlock_unlock(&sl);
216         }
217
218         return 0;
219 }
220
221
222 /*
223  * Test rte_eal_get_lcore_state() in addition to spinlocks
224  * as we have "waiting" then "running" lcores.
225  */
226 static int
227 test_spinlock(void)
228 {
229         int ret = 0;
230         int i;
231
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));
236         }
237
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]);
243
244         rte_spinlock_lock(&sl);
245
246         RTE_LCORE_FOREACH_SLAVE(i) {
247                 rte_spinlock_lock(&sl_tab[i]);
248                 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
249         }
250
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));
255         }
256         rte_spinlock_unlock(&sl);
257
258         RTE_LCORE_FOREACH_SLAVE(i) {
259                 rte_spinlock_unlock(&sl_tab[i]);
260                 rte_delay_ms(10);
261         }
262
263         rte_eal_mp_wait_lcore();
264
265         rte_spinlock_recursive_lock(&slr);
266
267         /*
268          * Try to acquire a lock that we already own
269          */
270         if(!rte_spinlock_recursive_trylock(&slr)) {
271                 printf("rte_spinlock_recursive_trylock failed on a lock that "
272                        "we already own\n");
273                 ret = -1;
274         } else
275                 rte_spinlock_recursive_unlock(&slr);
276
277         RTE_LCORE_FOREACH_SLAVE(i) {
278                 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
279         }
280         rte_spinlock_recursive_unlock(&slr);
281         rte_eal_mp_wait_lcore();
282
283         /*
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
289          * by one per times.
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
292          * is correct.
293          */
294         if (rte_spinlock_trylock(&sl_try) == 0) {
295                 return -1;
296         }
297         count = 0;
298         RTE_LCORE_FOREACH_SLAVE(i) {
299                 rte_eal_remote_launch(test_spinlock_try, NULL, i);
300         }
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");
305                 return -1;
306         }
307         rte_spinlock_lock(&sl);
308         if (count != ( rte_lcore_count() - 1)) {
309                 ret = -1;
310         }
311         rte_spinlock_unlock(&sl);
312
313         /*
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.
317          */
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");
320                 return -1;
321         }
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");
324                 return -1;
325         }
326         rte_spinlock_recursive_unlock(&slr);
327         rte_spinlock_recursive_unlock(&slr);
328
329         if (test_spinlock_perf() < 0)
330                 return -1;
331
332         return ret;
333 }
334
335 REGISTER_TEST_COMMAND(spinlock_autotest, test_spinlock);