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