build: remove makefiles
[dpdk.git] / app / test / test_spinlock.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <inttypes.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/queue.h>
11
12 #include <rte_common.h>
13 #include <rte_memory.h>
14 #include <rte_per_lcore.h>
15 #include <rte_launch.h>
16 #include <rte_eal.h>
17 #include <rte_lcore.h>
18 #include <rte_cycles.h>
19 #include <rte_spinlock.h>
20 #include <rte_atomic.h>
21
22 #include "test.h"
23
24 /*
25  * Spinlock test
26  * =============
27  *
28  * - There is a global spinlock and a table of spinlocks (one per lcore).
29  *
30  * - The test function takes all of these locks and launches the
31  *   ``test_spinlock_per_core()`` function on each core (except the master).
32  *
33  *   - The function takes the global lock, display something, then releases
34  *     the global lock.
35  *   - The function takes the per-lcore lock, display something, then releases
36  *     the per-core lock.
37  *
38  * - The main function unlocks the per-lcore locks sequentially and
39  *   waits between each lock. This triggers the display of a message
40  *   for each core, in the correct order. The autotest script checks that
41  *   this order is correct.
42  *
43  * - A load test is carried out, with all cores attempting to lock a single lock
44  *   multiple times
45  */
46
47 static rte_spinlock_t sl, sl_try;
48 static rte_spinlock_t sl_tab[RTE_MAX_LCORE];
49 static rte_spinlock_recursive_t slr;
50 static unsigned count = 0;
51
52 static rte_atomic32_t synchro;
53
54 static int
55 test_spinlock_per_core(__rte_unused void *arg)
56 {
57         rte_spinlock_lock(&sl);
58         printf("Global lock taken on core %u\n", rte_lcore_id());
59         rte_spinlock_unlock(&sl);
60
61         rte_spinlock_lock(&sl_tab[rte_lcore_id()]);
62         printf("Hello from core %u !\n", rte_lcore_id());
63         rte_spinlock_unlock(&sl_tab[rte_lcore_id()]);
64
65         return 0;
66 }
67
68 static int
69 test_spinlock_recursive_per_core(__rte_unused void *arg)
70 {
71         unsigned id = rte_lcore_id();
72
73         rte_spinlock_recursive_lock(&slr);
74         printf("Global recursive lock taken on core %u - count = %d\n",
75                id, slr.count);
76         rte_spinlock_recursive_lock(&slr);
77         printf("Global recursive lock taken on core %u - count = %d\n",
78                id, slr.count);
79         rte_spinlock_recursive_lock(&slr);
80         printf("Global recursive lock taken on core %u - count = %d\n",
81                id, slr.count);
82
83         printf("Hello from within recursive locks from core %u !\n", id);
84
85         rte_spinlock_recursive_unlock(&slr);
86         printf("Global recursive lock released on core %u - count = %d\n",
87                id, slr.count);
88         rte_spinlock_recursive_unlock(&slr);
89         printf("Global recursive lock released on core %u - count = %d\n",
90                id, slr.count);
91         rte_spinlock_recursive_unlock(&slr);
92         printf("Global recursive lock released on core %u - count = %d\n",
93                id, slr.count);
94
95         return 0;
96 }
97
98 static rte_spinlock_t lk = RTE_SPINLOCK_INITIALIZER;
99 static uint64_t time_count[RTE_MAX_LCORE] = {0};
100
101 #define MAX_LOOP 10000
102
103 static int
104 load_loop_fn(void *func_param)
105 {
106         uint64_t time_diff = 0, begin;
107         uint64_t hz = rte_get_timer_hz();
108         volatile uint64_t lcount = 0;
109         const int use_lock = *(int*)func_param;
110         const unsigned lcore = rte_lcore_id();
111
112         /* wait synchro for slaves */
113         if (lcore != rte_get_master_lcore())
114                 while (rte_atomic32_read(&synchro) == 0);
115
116         begin = rte_get_timer_cycles();
117         while (lcount < MAX_LOOP) {
118                 if (use_lock)
119                         rte_spinlock_lock(&lk);
120                 lcount++;
121                 if (use_lock)
122                         rte_spinlock_unlock(&lk);
123         }
124         time_diff = rte_get_timer_cycles() - begin;
125         time_count[lcore] = time_diff * 1000000 / hz;
126         return 0;
127 }
128
129 static int
130 test_spinlock_perf(void)
131 {
132         unsigned int i;
133         uint64_t total = 0;
134         int lock = 0;
135         const unsigned lcore = rte_lcore_id();
136
137         printf("\nTest with no lock on single core...\n");
138         load_loop_fn(&lock);
139         printf("Core [%u] Cost Time = %"PRIu64" us\n", lcore,
140                                                 time_count[lcore]);
141         memset(time_count, 0, sizeof(time_count));
142
143         printf("\nTest with lock on single core...\n");
144         lock = 1;
145         load_loop_fn(&lock);
146         printf("Core [%u] Cost Time = %"PRIu64" us\n", lcore,
147                                                 time_count[lcore]);
148         memset(time_count, 0, sizeof(time_count));
149
150         printf("\nTest with lock on %u cores...\n", rte_lcore_count());
151
152         /* Clear synchro and start slaves */
153         rte_atomic32_set(&synchro, 0);
154         rte_eal_mp_remote_launch(load_loop_fn, &lock, SKIP_MASTER);
155
156         /* start synchro and launch test on master */
157         rte_atomic32_set(&synchro, 1);
158         load_loop_fn(&lock);
159
160         rte_eal_mp_wait_lcore();
161
162         RTE_LCORE_FOREACH(i) {
163                 printf("Core [%u] Cost Time = %"PRIu64" us\n", i,
164                                                 time_count[i]);
165                 total += time_count[i];
166         }
167
168         printf("Total Cost Time = %"PRIu64" us\n", total);
169
170         return 0;
171 }
172
173 /*
174  * Use rte_spinlock_trylock() to trylock a spinlock object,
175  * If it could not lock the object successfully, it would
176  * return immediately and the variable of "count" would be
177  * increased by one per times. the value of "count" could be
178  * checked as the result later.
179  */
180 static int
181 test_spinlock_try(__rte_unused void *arg)
182 {
183         if (rte_spinlock_trylock(&sl_try) == 0) {
184                 rte_spinlock_lock(&sl);
185                 count ++;
186                 rte_spinlock_unlock(&sl);
187         }
188
189         return 0;
190 }
191
192
193 /*
194  * Test rte_eal_get_lcore_state() in addition to spinlocks
195  * as we have "waiting" then "running" lcores.
196  */
197 static int
198 test_spinlock(void)
199 {
200         int ret = 0;
201         int i;
202
203         /* slave cores should be waiting: print it */
204         RTE_LCORE_FOREACH_SLAVE(i) {
205                 printf("lcore %d state: %d\n", i,
206                        (int) rte_eal_get_lcore_state(i));
207         }
208
209         rte_spinlock_init(&sl);
210         rte_spinlock_init(&sl_try);
211         rte_spinlock_recursive_init(&slr);
212         for (i=0; i<RTE_MAX_LCORE; i++)
213                 rte_spinlock_init(&sl_tab[i]);
214
215         rte_spinlock_lock(&sl);
216
217         RTE_LCORE_FOREACH_SLAVE(i) {
218                 rte_spinlock_lock(&sl_tab[i]);
219                 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
220         }
221
222         /* slave cores should be busy: print it */
223         RTE_LCORE_FOREACH_SLAVE(i) {
224                 printf("lcore %d state: %d\n", i,
225                        (int) rte_eal_get_lcore_state(i));
226         }
227         rte_spinlock_unlock(&sl);
228
229         RTE_LCORE_FOREACH_SLAVE(i) {
230                 rte_spinlock_unlock(&sl_tab[i]);
231                 rte_delay_ms(10);
232         }
233
234         rte_eal_mp_wait_lcore();
235
236         rte_spinlock_recursive_lock(&slr);
237
238         /*
239          * Try to acquire a lock that we already own
240          */
241         if(!rte_spinlock_recursive_trylock(&slr)) {
242                 printf("rte_spinlock_recursive_trylock failed on a lock that "
243                        "we already own\n");
244                 ret = -1;
245         } else
246                 rte_spinlock_recursive_unlock(&slr);
247
248         RTE_LCORE_FOREACH_SLAVE(i) {
249                 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
250         }
251         rte_spinlock_recursive_unlock(&slr);
252         rte_eal_mp_wait_lcore();
253
254         /*
255          * Test if it could return immediately from try-locking a locked object.
256          * Here it will lock the spinlock object first, then launch all the slave
257          * lcores to trylock the same spinlock object.
258          * All the slave lcores should give up try-locking a locked object and
259          * return immediately, and then increase the "count" initialized with zero
260          * by one per times.
261          * We can check if the "count" is finally equal to the number of all slave
262          * lcores to see if the behavior of try-locking a locked spinlock object
263          * is correct.
264          */
265         if (rte_spinlock_trylock(&sl_try) == 0) {
266                 return -1;
267         }
268         count = 0;
269         RTE_LCORE_FOREACH_SLAVE(i) {
270                 rte_eal_remote_launch(test_spinlock_try, NULL, i);
271         }
272         rte_eal_mp_wait_lcore();
273         rte_spinlock_unlock(&sl_try);
274         if (rte_spinlock_is_locked(&sl)) {
275                 printf("spinlock is locked but it should not be\n");
276                 return -1;
277         }
278         rte_spinlock_lock(&sl);
279         if (count != ( rte_lcore_count() - 1)) {
280                 ret = -1;
281         }
282         rte_spinlock_unlock(&sl);
283
284         /*
285          * Test if it can trylock recursively.
286          * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
287          * object recursively. Here it will try to lock a spinlock object twice.
288          */
289         if (rte_spinlock_recursive_trylock(&slr) == 0) {
290                 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
291                 return -1;
292         }
293         if (rte_spinlock_recursive_trylock(&slr) == 0) {
294                 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
295                 return -1;
296         }
297         rte_spinlock_recursive_unlock(&slr);
298         rte_spinlock_recursive_unlock(&slr);
299
300         if (test_spinlock_perf() < 0)
301                 return -1;
302
303         return ret;
304 }
305
306 REGISTER_TEST_COMMAND(spinlock_autotest, test_spinlock);