test: move to app directory
[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(__attribute__((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(__attribute__((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 lock_count[RTE_MAX_LCORE] = {0};
100
101 #define TIME_MS 100
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         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 (time_diff < hz * TIME_MS / 1000) {
118                 if (use_lock)
119                         rte_spinlock_lock(&lk);
120                 lcount++;
121                 if (use_lock)
122                         rte_spinlock_unlock(&lk);
123                 /* delay to make lock duty cycle slighlty realistic */
124                 rte_delay_us(1);
125                 time_diff = rte_get_timer_cycles() - begin;
126         }
127         lock_count[lcore] = lcount;
128         return 0;
129 }
130
131 static int
132 test_spinlock_perf(void)
133 {
134         unsigned int i;
135         uint64_t total = 0;
136         int lock = 0;
137         const unsigned lcore = rte_lcore_id();
138
139         printf("\nTest with no lock on single core...\n");
140         load_loop_fn(&lock);
141         printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
142         memset(lock_count, 0, sizeof(lock_count));
143
144         printf("\nTest with lock on single core...\n");
145         lock = 1;
146         load_loop_fn(&lock);
147         printf("Core [%u] count = %"PRIu64"\n", lcore, lock_count[lcore]);
148         memset(lock_count, 0, sizeof(lock_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] count = %"PRIu64"\n", i, lock_count[i]);
164                 total += lock_count[i];
165         }
166
167         printf("Total count = %"PRIu64"\n", total);
168
169         return 0;
170 }
171
172 /*
173  * Use rte_spinlock_trylock() to trylock a spinlock object,
174  * If it could not lock the object successfully, it would
175  * return immediately and the variable of "count" would be
176  * increased by one per times. the value of "count" could be
177  * checked as the result later.
178  */
179 static int
180 test_spinlock_try(__attribute__((unused)) void *arg)
181 {
182         if (rte_spinlock_trylock(&sl_try) == 0) {
183                 rte_spinlock_lock(&sl);
184                 count ++;
185                 rte_spinlock_unlock(&sl);
186         }
187
188         return 0;
189 }
190
191
192 /*
193  * Test rte_eal_get_lcore_state() in addition to spinlocks
194  * as we have "waiting" then "running" lcores.
195  */
196 static int
197 test_spinlock(void)
198 {
199         int ret = 0;
200         int i;
201
202         /* slave cores should be waiting: print it */
203         RTE_LCORE_FOREACH_SLAVE(i) {
204                 printf("lcore %d state: %d\n", i,
205                        (int) rte_eal_get_lcore_state(i));
206         }
207
208         rte_spinlock_init(&sl);
209         rte_spinlock_init(&sl_try);
210         rte_spinlock_recursive_init(&slr);
211         for (i=0; i<RTE_MAX_LCORE; i++)
212                 rte_spinlock_init(&sl_tab[i]);
213
214         rte_spinlock_lock(&sl);
215
216         RTE_LCORE_FOREACH_SLAVE(i) {
217                 rte_spinlock_lock(&sl_tab[i]);
218                 rte_eal_remote_launch(test_spinlock_per_core, NULL, i);
219         }
220
221         /* slave cores should be busy: print it */
222         RTE_LCORE_FOREACH_SLAVE(i) {
223                 printf("lcore %d state: %d\n", i,
224                        (int) rte_eal_get_lcore_state(i));
225         }
226         rte_spinlock_unlock(&sl);
227
228         RTE_LCORE_FOREACH_SLAVE(i) {
229                 rte_spinlock_unlock(&sl_tab[i]);
230                 rte_delay_ms(10);
231         }
232
233         rte_eal_mp_wait_lcore();
234
235         rte_spinlock_recursive_lock(&slr);
236
237         /*
238          * Try to acquire a lock that we already own
239          */
240         if(!rte_spinlock_recursive_trylock(&slr)) {
241                 printf("rte_spinlock_recursive_trylock failed on a lock that "
242                        "we already own\n");
243                 ret = -1;
244         } else
245                 rte_spinlock_recursive_unlock(&slr);
246
247         RTE_LCORE_FOREACH_SLAVE(i) {
248                 rte_eal_remote_launch(test_spinlock_recursive_per_core, NULL, i);
249         }
250         rte_spinlock_recursive_unlock(&slr);
251         rte_eal_mp_wait_lcore();
252
253         /*
254          * Test if it could return immediately from try-locking a locked object.
255          * Here it will lock the spinlock object first, then launch all the slave
256          * lcores to trylock the same spinlock object.
257          * All the slave lcores should give up try-locking a locked object and
258          * return immediately, and then increase the "count" initialized with zero
259          * by one per times.
260          * We can check if the "count" is finally equal to the number of all slave
261          * lcores to see if the behavior of try-locking a locked spinlock object
262          * is correct.
263          */
264         if (rte_spinlock_trylock(&sl_try) == 0) {
265                 return -1;
266         }
267         count = 0;
268         RTE_LCORE_FOREACH_SLAVE(i) {
269                 rte_eal_remote_launch(test_spinlock_try, NULL, i);
270         }
271         rte_eal_mp_wait_lcore();
272         rte_spinlock_unlock(&sl_try);
273         if (rte_spinlock_is_locked(&sl)) {
274                 printf("spinlock is locked but it should not be\n");
275                 return -1;
276         }
277         rte_spinlock_lock(&sl);
278         if (count != ( rte_lcore_count() - 1)) {
279                 ret = -1;
280         }
281         rte_spinlock_unlock(&sl);
282
283         /*
284          * Test if it can trylock recursively.
285          * Use rte_spinlock_recursive_trylock() to check if it can lock a spinlock
286          * object recursively. Here it will try to lock a spinlock object twice.
287          */
288         if (rte_spinlock_recursive_trylock(&slr) == 0) {
289                 printf("It failed to do the first spinlock_recursive_trylock but it should able to do\n");
290                 return -1;
291         }
292         if (rte_spinlock_recursive_trylock(&slr) == 0) {
293                 printf("It failed to do the second spinlock_recursive_trylock but it should able to do\n");
294                 return -1;
295         }
296         rte_spinlock_recursive_unlock(&slr);
297         rte_spinlock_recursive_unlock(&slr);
298
299         if (test_spinlock_perf() < 0)
300                 return -1;
301
302         return ret;
303 }
304
305 REGISTER_TEST_COMMAND(spinlock_autotest, test_spinlock);