update copyright date to 2013
[dpdk.git] / app / test / test_spinlock.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/queue.h>
41
42 #include <cmdline_parse.h>
43
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>
50 #include <rte_eal.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>
56
57 #include "test.h"
58
59 /*
60  * Spinlock test
61  * =============
62  *
63  * - There is a global spinlock and a table of spinlocks (one per lcore).
64  *
65  * - The test function takes all of these locks and launches the
66  *   ``test_spinlock_per_core()`` function on each core (except the master).
67  *
68  *   - The function takes the global lock, display something, then releases
69  *     the global lock.
70  *   - The function takes the per-lcore lock, display something, then releases
71  *     the per-core lock.
72  *
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.
77  *
78  * - A load test is carried out, with all cores attempting to lock a single lock
79  *   multiple times
80  */
81
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;
86
87 static rte_atomic32_t synchro;
88
89 static int
90 test_spinlock_per_core(__attribute__((unused)) void *arg)
91 {
92         rte_spinlock_lock(&sl);
93         printf("Global lock taken on core %u\n", rte_lcore_id());
94         rte_spinlock_unlock(&sl);
95
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()]);
99
100         return 0;
101 }
102
103 static int
104 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
105 {
106         unsigned id = rte_lcore_id();
107
108         rte_spinlock_recursive_lock(&slr);
109         printf("Global recursive lock taken on core %u - count = %d\n",
110                id, slr.count);
111         rte_spinlock_recursive_lock(&slr);
112         printf("Global recursive lock taken on core %u - count = %d\n",
113                id, slr.count);
114         rte_spinlock_recursive_lock(&slr);
115         printf("Global recursive lock taken on core %u - count = %d\n",
116                id, slr.count);
117
118         printf("Hello from within recursive locks from core %u !\n", id);
119
120         rte_spinlock_recursive_unlock(&slr);
121         printf("Global recursive lock released on core %u - count = %d\n",
122                id, slr.count);
123         rte_spinlock_recursive_unlock(&slr);
124         printf("Global recursive lock released on core %u - count = %d\n",
125                id, slr.count);
126         rte_spinlock_recursive_unlock(&slr);
127         printf("Global recursive lock released on core %u - count = %d\n",
128                id, slr.count);
129
130         return 0;
131 }
132
133 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
134 static uint64_t lock_count[RTE_MAX_LCORE] = {0};
135
136 #define TIME_S 5
137
138 static int
139 load_loop_fn(void *func_param)
140 {
141         uint64_t time_diff = 0, begin;
142         uint64_t hz = rte_get_hpet_hz();
143         uint64_t lcount = 0;
144         const int use_lock = *(int*)func_param;
145         const unsigned lcore = rte_lcore_id();
146
147         /* wait synchro for slaves */
148         if (lcore != rte_get_master_lcore())
149                 while (rte_atomic32_read(&synchro) == 0);
150
151         begin = rte_get_hpet_cycles();
152         while (time_diff / hz < TIME_S) {
153                 if (use_lock)
154                         rte_spinlock_lock(&lk);
155                 lcount++;
156                 if (use_lock)
157                         rte_spinlock_unlock(&lk);
158                 /* delay to make lock duty cycle slighlty realistic */
159                 rte_delay_us(1);
160                 time_diff = rte_get_hpet_cycles() - begin;
161         }
162         lock_count[lcore] = lcount;
163         return 0;
164 }
165
166 static int
167 test_spinlock_perf(void)
168 {
169         unsigned int i;
170         uint64_t total = 0;
171         int lock = 0;
172         const unsigned lcore = rte_lcore_id();
173
174         printf("\nTest with no lock on single core...\n");
175         load_loop_fn(&lock);
176         printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
177         memset(lock_count, 0, sizeof(lock_count));
178
179         printf("\nTest with lock on single core...\n");
180         lock = 1;
181         load_loop_fn(&lock);
182         printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
183         memset(lock_count, 0, sizeof(lock_count));
184
185         printf("\nTest with lock on %u cores...\n", rte_lcore_count());
186
187         /* Clear synchro and start slaves */
188         rte_atomic32_set(&synchro, 0);
189         rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
190
191         /* start synchro and launch test on master */
192         rte_atomic32_set(&synchro, 1);
193         load_loop_fn(&lock);
194
195         rte_eal_mp_wait_lcore();
196
197         RTE_LCORE_FOREACH(i) {
198                 printf("Core [%u] count = %"PRIu64"\n", i, lock_count[i]);
199                 total += lock_count[i];
200         }
201
202         printf("Total count = %"PRIu64"\n", total);
203
204         return 0;
205 }
206
207 /*
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.
213  */
214 static int
215 test_spinlock_try(__attribute__((unused)) void *arg)
216 {
217         if (rte_spinlock_trylock(&sl_try) == 0) {
218                 rte_spinlock_lock(&sl);
219                 count ++;
220                 rte_spinlock_unlock(&sl);
221         }
222
223         return 0;
224 }
225
226
227 /*
228  * Test rte_eal_get_lcore_state() in addition to spinlocks
229  * as we have "waiting" then "running" lcores.
230  */
231 int
232 test_spinlock(void)
233 {
234         int ret = 0;
235         int i;
236
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));
241         }
242
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]);
248
249         rte_spinlock_lock(&sl);
250
251         RTE_LCORE_FOREACH_SLAVE(i) {
252                 rte_spinlock_lock(&sl_tab[i]);
253                 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
254         }
255
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));
260         }
261         rte_spinlock_unlock(&sl);
262
263         RTE_LCORE_FOREACH_SLAVE(i) {
264                 rte_spinlock_unlock(&sl_tab[i]);
265                 rte_delay_ms(100);
266         }
267
268         rte_eal_mp_wait_lcore();
269
270         rte_spinlock_recursive_lock(&slr);
271
272         /*
273          * Try to acquire a lock that we already own
274          */
275         if(!rte_spinlock_recursive_trylock(&slr)) {
276                 printf("rte_spinlock_recursive_trylock failed on a lock that "
277                        "we already own\n");
278                 ret = -1;
279         } else
280                 rte_spinlock_recursive_unlock(&slr);
281
282         RTE_LCORE_FOREACH_SLAVE(i) {
283                 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
284         }
285         rte_spinlock_recursive_unlock(&slr);
286         rte_eal_mp_wait_lcore();
287
288         /*
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
294          * by one per times.
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
297          * is correct.
298          */
299         if (rte_spinlock_trylock(&sl_try) == 0) {
300                 return -1;
301         }
302         count = 0;
303         RTE_LCORE_FOREACH_SLAVE(i) {
304                 rte_eal_remote_launch(test_spinlock_try, NULL, i);
305         }
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");
310                 return -1;
311         }
312         rte_spinlock_lock(&sl);
313         if (count != ( rte_lcore_count() - 1)) {
314                 ret = -1;
315         }
316         rte_spinlock_unlock(&sl);
317
318         /*
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.
322          */
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");
325                 return -1;
326         }
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");
329                 return -1;
330         }
331         rte_spinlock_recursive_unlock(&slr);
332         rte_spinlock_recursive_unlock(&slr);
333
334         if (test_spinlock_perf() < 0)
335                 return -1;
336
337         return ret;
338 }