c68723ae0d872e66f938d8471b3d447d32891b80
[deb_dpdk.git] / test / test / test_atomic.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 <unistd.h>
37 #include <sys/queue.h>
38
39 #include <rte_memory.h>
40 #include <rte_memzone.h>
41 #include <rte_per_lcore.h>
42 #include <rte_launch.h>
43 #include <rte_atomic.h>
44 #include <rte_eal.h>
45 #include <rte_lcore.h>
46
47 #include "test.h"
48
49 /*
50  * Atomic Variables
51  * ================
52  *
53  * - The main test function performs three subtests. The first test
54  *   checks that the usual inc/dec/add/sub functions are working
55  *   correctly:
56  *
57  *   - Initialize 16-bit, 32-bit and 64-bit atomic variables to specific
58  *     values.
59  *
60  *   - These variables are incremented and decremented on each core at
61  *     the same time in ``test_atomic_usual()``.
62  *
63  *   - The function checks that once all lcores finish their function,
64  *     the value of the atomic variables are still the same.
65  *
66  * - The second test verifies the behavior of "test and set" functions.
67  *
68  *   - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
69  *
70  *   - Invoke ``test_atomic_tas()`` on each lcore: before doing anything
71  *     else. The cores are waiting a synchro using ``while
72  *     (rte_atomic32_read(&val) == 0)`` which is triggered by the main test
73  *     function. Then all cores do a
74  *     ``rte_atomicXX_test_and_set()`` at the same time. If it is successful,
75  *     it increments another atomic counter.
76  *
77  *   - The main function checks that the atomic counter was incremented
78  *     twice only (one for 16-bit, one for 32-bit and one for 64-bit values).
79  *
80  * - Test "add/sub and return"
81  *
82  *   - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
83  *
84  *   - Invoke ``test_atomic_addsub_return()`` on each lcore. Before doing
85  *     anything else, the cores are waiting a synchro. Each lcore does
86  *     this operation several times::
87  *
88  *       tmp = rte_atomicXX_add_return(&a, 1);
89  *       atomic_add(&count, tmp);
90  *       tmp = rte_atomicXX_sub_return(&a, 1);
91  *       atomic_sub(&count, tmp+1);
92  *
93  *   - At the end of the test, the *count* value must be 0.
94  */
95
96 #define NUM_ATOMIC_TYPES 3
97
98 #define N 10000
99
100 static rte_atomic16_t a16;
101 static rte_atomic32_t a32;
102 static rte_atomic64_t a64;
103 static rte_atomic64_t count;
104 static rte_atomic32_t synchro;
105
106 static int
107 test_atomic_usual(__attribute__((unused)) void *arg)
108 {
109         unsigned i;
110
111         while (rte_atomic32_read(&synchro) == 0)
112                 ;
113
114         for (i = 0; i < N; i++)
115                 rte_atomic16_inc(&a16);
116         for (i = 0; i < N; i++)
117                 rte_atomic16_dec(&a16);
118         for (i = 0; i < (N / 5); i++)
119                 rte_atomic16_add(&a16, 5);
120         for (i = 0; i < (N / 5); i++)
121                 rte_atomic16_sub(&a16, 5);
122
123         for (i = 0; i < N; i++)
124                 rte_atomic32_inc(&a32);
125         for (i = 0; i < N; i++)
126                 rte_atomic32_dec(&a32);
127         for (i = 0; i < (N / 5); i++)
128                 rte_atomic32_add(&a32, 5);
129         for (i = 0; i < (N / 5); i++)
130                 rte_atomic32_sub(&a32, 5);
131
132         for (i = 0; i < N; i++)
133                 rte_atomic64_inc(&a64);
134         for (i = 0; i < N; i++)
135                 rte_atomic64_dec(&a64);
136         for (i = 0; i < (N / 5); i++)
137                 rte_atomic64_add(&a64, 5);
138         for (i = 0; i < (N / 5); i++)
139                 rte_atomic64_sub(&a64, 5);
140
141         return 0;
142 }
143
144 static int
145 test_atomic_tas(__attribute__((unused)) void *arg)
146 {
147         while (rte_atomic32_read(&synchro) == 0)
148                 ;
149
150         if (rte_atomic16_test_and_set(&a16))
151                 rte_atomic64_inc(&count);
152         if (rte_atomic32_test_and_set(&a32))
153                 rte_atomic64_inc(&count);
154         if (rte_atomic64_test_and_set(&a64))
155                 rte_atomic64_inc(&count);
156
157         return 0;
158 }
159
160 static int
161 test_atomic_addsub_and_return(__attribute__((unused)) void *arg)
162 {
163         uint32_t tmp16;
164         uint32_t tmp32;
165         uint64_t tmp64;
166         unsigned i;
167
168         while (rte_atomic32_read(&synchro) == 0)
169                 ;
170
171         for (i = 0; i < N; i++) {
172                 tmp16 = rte_atomic16_add_return(&a16, 1);
173                 rte_atomic64_add(&count, tmp16);
174
175                 tmp16 = rte_atomic16_sub_return(&a16, 1);
176                 rte_atomic64_sub(&count, tmp16+1);
177
178                 tmp32 = rte_atomic32_add_return(&a32, 1);
179                 rte_atomic64_add(&count, tmp32);
180
181                 tmp32 = rte_atomic32_sub_return(&a32, 1);
182                 rte_atomic64_sub(&count, tmp32+1);
183
184                 tmp64 = rte_atomic64_add_return(&a64, 1);
185                 rte_atomic64_add(&count, tmp64);
186
187                 tmp64 = rte_atomic64_sub_return(&a64, 1);
188                 rte_atomic64_sub(&count, tmp64+1);
189         }
190
191         return 0;
192 }
193
194 /*
195  * rte_atomic32_inc_and_test() would increase a 32 bits counter by one and then
196  * test if that counter is equal to 0. It would return true if the counter is 0
197  * and false if the counter is not 0. rte_atomic64_inc_and_test() could do the
198  * same thing but for a 64 bits counter.
199  * Here checks that if the 32/64 bits counter is equal to 0 after being atomically
200  * increased by one. If it is, increase the variable of "count" by one which would
201  * be checked as the result later.
202  *
203  */
204 static int
205 test_atomic_inc_and_test(__attribute__((unused)) void *arg)
206 {
207         while (rte_atomic32_read(&synchro) == 0)
208                 ;
209
210         if (rte_atomic16_inc_and_test(&a16)) {
211                 rte_atomic64_inc(&count);
212         }
213         if (rte_atomic32_inc_and_test(&a32)) {
214                 rte_atomic64_inc(&count);
215         }
216         if (rte_atomic64_inc_and_test(&a64)) {
217                 rte_atomic64_inc(&count);
218         }
219
220         return 0;
221 }
222
223 /*
224  * rte_atomicXX_dec_and_test() should decrease a 32 bits counter by one and then
225  * test if that counter is equal to 0. It should return true if the counter is 0
226  * and false if the counter is not 0.
227  * This test checks if the counter is equal to 0 after being atomically
228  * decreased by one. If it is, increase the value of "count" by one which is to
229  * be checked as the result later.
230  */
231 static int
232 test_atomic_dec_and_test(__attribute__((unused)) void *arg)
233 {
234         while (rte_atomic32_read(&synchro) == 0)
235                 ;
236
237         if (rte_atomic16_dec_and_test(&a16))
238                 rte_atomic64_inc(&count);
239
240         if (rte_atomic32_dec_and_test(&a32))
241                 rte_atomic64_inc(&count);
242
243         if (rte_atomic64_dec_and_test(&a64))
244                 rte_atomic64_inc(&count);
245
246         return 0;
247 }
248
249 static int
250 test_atomic(void)
251 {
252         rte_atomic16_init(&a16);
253         rte_atomic32_init(&a32);
254         rte_atomic64_init(&a64);
255         rte_atomic64_init(&count);
256         rte_atomic32_init(&synchro);
257
258         rte_atomic16_set(&a16, 1UL << 10);
259         rte_atomic32_set(&a32, 1UL << 10);
260         rte_atomic64_set(&a64, 1ULL << 33);
261
262         printf("usual inc/dec/add/sub functions\n");
263
264         rte_eal_mp_remote_launch(test_atomic_usual, NULL, SKIP_MASTER);
265         rte_atomic32_set(&synchro, 1);
266         rte_eal_mp_wait_lcore();
267         rte_atomic32_set(&synchro, 0);
268
269         if (rte_atomic16_read(&a16) != 1UL << 10) {
270                 printf("Atomic16 usual functions failed\n");
271                 return -1;
272         }
273
274         if (rte_atomic32_read(&a32) != 1UL << 10) {
275                 printf("Atomic32 usual functions failed\n");
276                 return -1;
277         }
278
279         if (rte_atomic64_read(&a64) != 1ULL << 33) {
280                 printf("Atomic64 usual functions failed\n");
281                 return -1;
282         }
283
284         printf("test and set\n");
285
286         rte_atomic64_set(&a64, 0);
287         rte_atomic32_set(&a32, 0);
288         rte_atomic16_set(&a16, 0);
289         rte_atomic64_set(&count, 0);
290         rte_eal_mp_remote_launch(test_atomic_tas, NULL, SKIP_MASTER);
291         rte_atomic32_set(&synchro, 1);
292         rte_eal_mp_wait_lcore();
293         rte_atomic32_set(&synchro, 0);
294
295         if (rte_atomic64_read(&count) != NUM_ATOMIC_TYPES) {
296                 printf("Atomic test and set failed\n");
297                 return -1;
298         }
299
300         printf("add/sub and return\n");
301
302         rte_atomic64_set(&a64, 0);
303         rte_atomic32_set(&a32, 0);
304         rte_atomic16_set(&a16, 0);
305         rte_atomic64_set(&count, 0);
306         rte_eal_mp_remote_launch(test_atomic_addsub_and_return, NULL,
307                                  SKIP_MASTER);
308         rte_atomic32_set(&synchro, 1);
309         rte_eal_mp_wait_lcore();
310         rte_atomic32_set(&synchro, 0);
311
312         if (rte_atomic64_read(&count) != 0) {
313                 printf("Atomic add/sub+return failed\n");
314                 return -1;
315         }
316
317         /*
318          * Set a64, a32 and a16 with the same value of minus "number of slave
319          * lcores", launch all slave lcores to atomically increase by one and
320          * test them respectively.
321          * Each lcore should have only one chance to increase a64 by one and
322          * then check if it is equal to 0, but there should be only one lcore
323          * that finds that it is 0. It is similar for a32 and a16.
324          * Then a variable of "count", initialized to zero, is increased by
325          * one if a64, a32 or a16 is 0 after being increased and tested
326          * atomically.
327          * We can check if "count" is finally equal to 3 to see if all slave
328          * lcores performed "atomic inc and test" right.
329          */
330         printf("inc and test\n");
331
332         rte_atomic64_clear(&a64);
333         rte_atomic32_clear(&a32);
334         rte_atomic16_clear(&a16);
335         rte_atomic32_clear(&synchro);
336         rte_atomic64_clear(&count);
337
338         rte_atomic64_set(&a64, (int64_t)(1 - (int64_t)rte_lcore_count()));
339         rte_atomic32_set(&a32, (int32_t)(1 - (int32_t)rte_lcore_count()));
340         rte_atomic16_set(&a16, (int16_t)(1 - (int16_t)rte_lcore_count()));
341         rte_eal_mp_remote_launch(test_atomic_inc_and_test, NULL, SKIP_MASTER);
342         rte_atomic32_set(&synchro, 1);
343         rte_eal_mp_wait_lcore();
344         rte_atomic32_clear(&synchro);
345
346         if (rte_atomic64_read(&count) != NUM_ATOMIC_TYPES) {
347                 printf("Atomic inc and test failed %d\n", (int)count.cnt);
348                 return -1;
349         }
350
351         /*
352          * Same as above, but this time we set the values to "number of slave
353          * lcores", and decrement instead of increment.
354          */
355         printf("dec and test\n");
356
357         rte_atomic32_clear(&synchro);
358         rte_atomic64_clear(&count);
359
360         rte_atomic64_set(&a64, (int64_t)(rte_lcore_count() - 1));
361         rte_atomic32_set(&a32, (int32_t)(rte_lcore_count() - 1));
362         rte_atomic16_set(&a16, (int16_t)(rte_lcore_count() - 1));
363         rte_eal_mp_remote_launch(test_atomic_dec_and_test, NULL, SKIP_MASTER);
364         rte_atomic32_set(&synchro, 1);
365         rte_eal_mp_wait_lcore();
366         rte_atomic32_clear(&synchro);
367
368         if (rte_atomic64_read(&count) != NUM_ATOMIC_TYPES) {
369                 printf("Atomic dec and test failed\n");
370                 return -1;
371         }
372
373         return 0;
374 }
375
376 REGISTER_TEST_COMMAND(atomic_autotest, test_atomic);