remove version in all files
[dpdk.git] / app / test / test_spinlock.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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 <unistd.h>
39 #include <sys/queue.h>
40
41 #include <cmdline_parse.h>
42
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>
49 #include <rte_eal.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_cycles.h>
53 #include <rte_spinlock.h>
54
55 #include "test.h"
56
57 /*
58  * Spinlock test
59  * =============
60  *
61  * - There is a global spinlock and a table of spinlocks (one per lcore).
62  *
63  * - The test function takes all of these locks and launches the
64  *   ``test_spinlock_per_core()`` function on each core (except the master).
65  *
66  *   - The function takes the global lock, display something, then releases
67  *     the global lock.
68  *   - The function takes the per-lcore lock, display something, then releases
69  *     the per-core lock.
70  *
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.
75  *
76  * - A load test is carried out, with all cores attempting to lock a single lock
77  *   multiple times
78  */
79
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;
84
85 static int
86 test_spinlock_per_core(__attribute__((unused)) void *arg)
87 {
88         rte_spinlock_lock(&sl);
89         printf("Global lock taken on core %u\n", rte_lcore_id());
90         rte_spinlock_unlock(&sl);
91
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()]);
95
96         return 0;
97 }
98
99 static int
100 test_spinlock_recursive_per_core(__attribute__((unused)) void *arg)
101 {
102         unsigned id = rte_lcore_id();
103
104         rte_spinlock_recursive_lock(&slr);
105         printf("Global recursive lock taken on core %u - count = %d\n",
106                id, slr.count);
107         rte_spinlock_recursive_lock(&slr);
108         printf("Global recursive lock taken on core %u - count = %d\n",
109                id, slr.count);
110         rte_spinlock_recursive_lock(&slr);
111         printf("Global recursive lock taken on core %u - count = %d\n",
112                id, slr.count);
113
114         printf("Hello from within recursive locks from core %u !\n", id);
115
116         rte_spinlock_recursive_unlock(&slr);
117         printf("Global recursive lock released on core %u - count = %d\n",
118                id, slr.count);
119         rte_spinlock_recursive_unlock(&slr);
120         printf("Global recursive lock released on core %u - count = %d\n",
121                id, slr.count);
122         rte_spinlock_recursive_unlock(&slr);
123         printf("Global recursive lock released on core %u - count = %d\n",
124                id, slr.count);
125
126         return 0;
127 }
128
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];
133
134 static int
135 load_loop_fn(__attribute__((unused)) void *dummy)
136 {
137         uint64_t end, begin;
138         begin = rte_get_hpet_cycles();
139         unsigned int i = 0;
140         for ( i = 0; i < max; i++) {
141                 rte_spinlock_lock(&lk);
142                 count1++;
143                 rte_spinlock_unlock(&lk);
144                 count2++;
145         }
146         end = rte_get_hpet_cycles();
147         looptime[rte_lcore_id()] = end - begin;
148         return 0;
149 }
150
151 static int
152 test_spinlock_load(void)
153 {
154         if (rte_lcore_count()<= 1) {
155                 printf("no cores counted\n");
156                 return -1;
157         }
158         printf ("Running %u tests.......\n", max);
159         printf ("Number of cores = %u\n", rte_lcore_count());
160
161         rte_eal_mp_remote_launch(load_loop_fn, NULL , CALL_MASTER);
162         rte_eal_mp_wait_lcore();
163
164         unsigned int k = 0;
165         uint64_t avgtime = 0;
166
167         RTE_LCORE_FOREACH(k) {
168                 printf("Core [%u] time = %"PRIu64"\n", k, looptime[k]);
169                 avgtime += looptime[k];
170         }
171
172         avgtime = avgtime / rte_lcore_count();
173         printf("Average time = %"PRIu64"\n", avgtime);
174
175         int check = 0;
176         check =  max * rte_lcore_count();
177         if (count1 == check && count2 != check)
178                 printf("Passed Load test\n");
179         else {
180                 printf("Failed load test\n");
181                 return -1;
182         }
183         return 0;
184 }
185
186 /*
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.
192  */
193 static int
194 test_spinlock_try(__attribute__((unused)) void *arg)
195 {
196         if (rte_spinlock_trylock(&sl_try) == 0) {
197                 rte_spinlock_lock(&sl);
198                 count ++;
199                 rte_spinlock_unlock(&sl);
200         }
201
202         return 0;
203 }
204
205
206 /*
207  * Test rte_eal_get_lcore_state() in addition to spinlocks
208  * as we have "waiting" then "running" lcores.
209  */
210 int
211 test_spinlock(void)
212 {
213         int ret = 0;
214         int i;
215
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));
220         }
221
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]);
227
228         rte_spinlock_lock(&sl);
229
230         RTE_LCORE_FOREACH_SLAVE(i) {
231                 rte_spinlock_lock(&sl_tab[i]);
232                 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
233         }
234
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));
239         }
240         rte_spinlock_unlock(&sl);
241
242         RTE_LCORE_FOREACH_SLAVE(i) {
243                 rte_spinlock_unlock(&sl_tab[i]);
244                 rte_delay_ms(100);
245         }
246
247         rte_eal_mp_wait_lcore();
248
249         if (test_spinlock_load()<0)
250                 return -1;
251
252         rte_spinlock_recursive_lock(&slr);
253
254         /*
255          * Try to acquire a lock that we already own
256          */
257         if(!rte_spinlock_recursive_trylock(&slr)) {
258                 printf("rte_spinlock_recursive_trylock failed on a lock that "
259                        "we already own\n");
260                 ret = -1;
261         } else
262                 rte_spinlock_recursive_unlock(&slr);
263
264         RTE_LCORE_FOREACH_SLAVE(i) {
265                 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
266         }
267         rte_spinlock_recursive_unlock(&slr);
268         rte_eal_mp_wait_lcore();
269
270         /*
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
276          * by one per times.
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
279          * is correct.
280          */
281         if (rte_spinlock_trylock(&sl_try) == 0) {
282                 return -1;
283         }
284         count = 0;
285         RTE_LCORE_FOREACH_SLAVE(i) {
286                 rte_eal_remote_launch(test_spinlock_try, NULL, i);
287         }
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");
292                 return -1;
293         }
294         rte_spinlock_lock(&sl);
295         if (count != ( rte_lcore_count() - 1)) {
296                 ret = -1;
297         }
298         rte_spinlock_unlock(&sl);
299
300         /*
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.
304          */
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");
307                 return -1;
308         }
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");
311                 return -1;
312         }
313         rte_spinlock_recursive_unlock(&slr);
314         rte_spinlock_recursive_unlock(&slr);
315
316         return ret;
317 }