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