New upstream version 17.11.4
[deb_dpdk.git] / test / test / test_hash_multiwriter.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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 #include <inttypes.h>
34 #include <locale.h>
35
36 #include <rte_cycles.h>
37 #include <rte_hash.h>
38 #include <rte_hash_crc.h>
39 #include <rte_launch.h>
40 #include <rte_malloc.h>
41 #include <rte_random.h>
42 #include <rte_spinlock.h>
43
44 #include "test.h"
45
46 /*
47  * Check condition and return an error if true. Assumes that "handle" is the
48  * name of the hash structure pointer to be freed.
49  */
50 #define RETURN_IF_ERROR(cond, str, ...) do {                            \
51         if (cond) {                                                     \
52                 printf("ERROR line %d: " str "\n", __LINE__,            \
53                                                         ##__VA_ARGS__); \
54                 if (handle)                                             \
55                         rte_hash_free(handle);                          \
56                 return -1;                                              \
57         }                                                               \
58 } while (0)
59
60 #define RTE_APP_TEST_HASH_MULTIWRITER_FAILED 0
61
62 struct {
63         uint32_t *keys;
64         uint32_t *found;
65         uint32_t nb_tsx_insertion;
66         struct rte_hash *h;
67 } tbl_multiwriter_test_params;
68
69 const uint32_t nb_entries = 16*1024*1024;
70 const uint32_t nb_total_tsx_insertion = 15*1024*1024;
71 uint32_t rounded_nb_total_tsx_insertion;
72
73 static rte_atomic64_t gcycles;
74 static rte_atomic64_t ginsertions;
75
76 static int use_htm;
77
78 static int
79 test_hash_multiwriter_worker(void *arg)
80 {
81         uint64_t i, offset;
82         uint16_t pos_core;
83         uint32_t lcore_id = rte_lcore_id();
84         uint64_t begin, cycles;
85         uint16_t *enabled_core_ids = (uint16_t *)arg;
86
87         for (pos_core = 0; pos_core < rte_lcore_count(); pos_core++) {
88                 if (enabled_core_ids[pos_core] == lcore_id)
89                         break;
90         }
91
92         /*
93          * Calculate offset for entries based on the position of the
94          * logical core, from the master core (not counting not enabled cores)
95          */
96         offset = pos_core * tbl_multiwriter_test_params.nb_tsx_insertion;
97
98         printf("Core #%d inserting %d: %'"PRId64" - %'"PRId64"\n",
99                lcore_id, tbl_multiwriter_test_params.nb_tsx_insertion,
100                offset,
101                offset + tbl_multiwriter_test_params.nb_tsx_insertion - 1);
102
103         begin = rte_rdtsc_precise();
104
105         for (i = offset;
106              i < offset + tbl_multiwriter_test_params.nb_tsx_insertion;
107              i++) {
108                 if (rte_hash_add_key(tbl_multiwriter_test_params.h,
109                                      tbl_multiwriter_test_params.keys + i) < 0)
110                         break;
111         }
112
113         cycles = rte_rdtsc_precise() - begin;
114         rte_atomic64_add(&gcycles, cycles);
115         rte_atomic64_add(&ginsertions, i - offset);
116
117         for (; i < offset + tbl_multiwriter_test_params.nb_tsx_insertion; i++)
118                 tbl_multiwriter_test_params.keys[i]
119                         = RTE_APP_TEST_HASH_MULTIWRITER_FAILED;
120
121         return 0;
122 }
123
124
125 static int
126 test_hash_multiwriter(void)
127 {
128         unsigned int i, rounded_nb_total_tsx_insertion;
129         static unsigned calledCount = 1;
130         uint16_t enabled_core_ids[RTE_MAX_LCORE];
131         uint16_t core_id;
132
133         uint32_t *keys;
134         uint32_t *found;
135
136         struct rte_hash_parameters hash_params = {
137                 .entries = nb_entries,
138                 .key_len = sizeof(uint32_t),
139                 .hash_func = rte_hash_crc,
140                 .hash_func_init_val = 0,
141                 .socket_id = rte_socket_id(),
142         };
143         if (use_htm)
144                 hash_params.extra_flag =
145                         RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT
146                                 | RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
147         else
148                 hash_params.extra_flag =
149                         RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
150
151         struct rte_hash *handle;
152         char name[RTE_HASH_NAMESIZE];
153
154         const void *next_key;
155         void *next_data;
156         uint32_t iter = 0;
157
158         uint32_t duplicated_keys = 0;
159         uint32_t lost_keys = 0;
160
161         snprintf(name, 32, "test%u", calledCount++);
162         hash_params.name = name;
163
164         handle = rte_hash_create(&hash_params);
165         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
166
167         tbl_multiwriter_test_params.h = handle;
168         tbl_multiwriter_test_params.nb_tsx_insertion =
169                 nb_total_tsx_insertion / rte_lcore_count();
170
171         rounded_nb_total_tsx_insertion = (nb_total_tsx_insertion /
172                 tbl_multiwriter_test_params.nb_tsx_insertion)
173                 * tbl_multiwriter_test_params.nb_tsx_insertion;
174
175         rte_srand(rte_rdtsc());
176
177         keys = rte_malloc(NULL, sizeof(uint32_t) * nb_entries, 0);
178
179         if (keys == NULL) {
180                 printf("RTE_MALLOC failed\n");
181                 goto err1;
182         }
183
184         for (i = 0; i < nb_entries; i++)
185                 keys[i] = i;
186
187         tbl_multiwriter_test_params.keys = keys;
188
189         found = rte_zmalloc(NULL, sizeof(uint32_t) * nb_entries, 0);
190         if (found == NULL) {
191                 printf("RTE_ZMALLOC failed\n");
192                 goto err2;
193         }
194
195         tbl_multiwriter_test_params.found = found;
196
197         rte_atomic64_init(&gcycles);
198         rte_atomic64_clear(&gcycles);
199
200         rte_atomic64_init(&ginsertions);
201         rte_atomic64_clear(&ginsertions);
202
203         /* Get list of enabled cores */
204         i = 0;
205         for (core_id = 0; core_id < RTE_MAX_LCORE; core_id++) {
206                 if (i == rte_lcore_count())
207                         break;
208
209                 if (rte_lcore_is_enabled(core_id)) {
210                         enabled_core_ids[i] = core_id;
211                         i++;
212                 }
213         }
214
215         if (i != rte_lcore_count()) {
216                 printf("Number of enabled cores in list is different from "
217                                 "number given by rte_lcore_count()\n");
218                 goto err3;
219         }
220
221         /* Fire all threads. */
222         rte_eal_mp_remote_launch(test_hash_multiwriter_worker,
223                                  enabled_core_ids, CALL_MASTER);
224         rte_eal_mp_wait_lcore();
225
226         while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
227                 /* Search for the key in the list of keys added .*/
228                 i = *(const uint32_t *)next_key;
229                 tbl_multiwriter_test_params.found[i]++;
230         }
231
232         for (i = 0; i < rounded_nb_total_tsx_insertion; i++) {
233                 if (tbl_multiwriter_test_params.keys[i]
234                     != RTE_APP_TEST_HASH_MULTIWRITER_FAILED) {
235                         if (tbl_multiwriter_test_params.found[i] > 1) {
236                                 duplicated_keys++;
237                                 break;
238                         }
239                         if (tbl_multiwriter_test_params.found[i] == 0) {
240                                 lost_keys++;
241                                 printf("key %d is lost\n", i);
242                                 break;
243                         }
244                 }
245         }
246
247         if (duplicated_keys > 0) {
248                 printf("%d key duplicated\n", duplicated_keys);
249                 goto err3;
250         }
251
252         if (lost_keys > 0) {
253                 printf("%d key lost\n", lost_keys);
254                 goto err3;
255         }
256
257         printf("No key corrupted during multiwriter insertion.\n");
258
259         unsigned long long int cycles_per_insertion =
260                 rte_atomic64_read(&gcycles)/
261                 rte_atomic64_read(&ginsertions);
262
263         printf(" cycles per insertion: %llu\n", cycles_per_insertion);
264
265         rte_free(tbl_multiwriter_test_params.found);
266         rte_free(tbl_multiwriter_test_params.keys);
267         rte_hash_free(handle);
268         return 0;
269
270 err3:
271         rte_free(tbl_multiwriter_test_params.found);
272 err2:
273         rte_free(tbl_multiwriter_test_params.keys);
274 err1:
275         rte_hash_free(handle);
276         return -1;
277 }
278
279 static int
280 test_hash_multiwriter_main(void)
281 {
282         if (rte_lcore_count() == 1) {
283                 printf("More than one lcore is required to do multiwriter test\n");
284                 return 0;
285         }
286
287
288         setlocale(LC_NUMERIC, "");
289
290
291         if (!rte_tm_supported()) {
292                 printf("Hardware transactional memory (lock elision) "
293                         "is NOT supported\n");
294         } else {
295                 printf("Hardware transactional memory (lock elision) "
296                         "is supported\n");
297
298                 printf("Test multi-writer with Hardware transactional memory\n");
299
300                 use_htm = 1;
301                 if (test_hash_multiwriter() < 0)
302                         return -1;
303         }
304
305         printf("Test multi-writer without Hardware transactional memory\n");
306         use_htm = 0;
307         if (test_hash_multiwriter() < 0)
308                 return -1;
309
310         return 0;
311 }
312
313 REGISTER_TEST_COMMAND(hash_multiwriter_autotest, test_hash_multiwriter_main);