f182f40525965ed38d68fda4a16b66e8a23c9b99
[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(__attribute__((unused)) void *arg)
52 {
53         uint64_t i, offset;
54         uint32_t lcore_id = rte_lcore_id();
55         uint64_t begin, cycles;
56
57         offset = (lcore_id - rte_get_master_lcore())
58                 * tbl_multiwriter_test_params.nb_tsx_insertion;
59
60         printf("Core #%d inserting %d: %'"PRId64" - %'"PRId64"\n",
61                lcore_id, tbl_multiwriter_test_params.nb_tsx_insertion,
62                offset, offset + tbl_multiwriter_test_params.nb_tsx_insertion);
63
64         begin = rte_rdtsc_precise();
65
66         for (i = offset;
67              i < offset + tbl_multiwriter_test_params.nb_tsx_insertion;
68              i++) {
69                 if (rte_hash_add_key(tbl_multiwriter_test_params.h,
70                                      tbl_multiwriter_test_params.keys + i) < 0)
71                         break;
72         }
73
74         cycles = rte_rdtsc_precise() - begin;
75         rte_atomic64_add(&gcycles, cycles);
76         rte_atomic64_add(&ginsertions, i - offset);
77
78         for (; i < offset + tbl_multiwriter_test_params.nb_tsx_insertion; i++)
79                 tbl_multiwriter_test_params.keys[i]
80                         = RTE_APP_TEST_HASH_MULTIWRITER_FAILED;
81
82         return 0;
83 }
84
85
86 static int
87 test_hash_multiwriter(void)
88 {
89         unsigned int i, rounded_nb_total_tsx_insertion;
90         static unsigned calledCount = 1;
91
92         uint32_t *keys;
93         uint32_t *found;
94
95         struct rte_hash_parameters hash_params = {
96                 .entries = nb_entries,
97                 .key_len = sizeof(uint32_t),
98                 .hash_func = rte_hash_crc,
99                 .hash_func_init_val = 0,
100                 .socket_id = rte_socket_id(),
101         };
102         if (use_htm)
103                 hash_params.extra_flag =
104                         RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT
105                                 | RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
106         else
107                 hash_params.extra_flag =
108                         RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
109
110         struct rte_hash *handle;
111         char name[RTE_HASH_NAMESIZE];
112
113         const void *next_key;
114         void *next_data;
115         uint32_t iter = 0;
116
117         uint32_t duplicated_keys = 0;
118         uint32_t lost_keys = 0;
119         uint32_t count;
120
121         snprintf(name, 32, "test%u", calledCount++);
122         hash_params.name = name;
123
124         handle = rte_hash_create(&hash_params);
125         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
126
127         tbl_multiwriter_test_params.h = handle;
128         tbl_multiwriter_test_params.nb_tsx_insertion =
129                 nb_total_tsx_insertion / rte_lcore_count();
130
131         rounded_nb_total_tsx_insertion = (nb_total_tsx_insertion /
132                 tbl_multiwriter_test_params.nb_tsx_insertion)
133                 * tbl_multiwriter_test_params.nb_tsx_insertion;
134
135         rte_srand(rte_rdtsc());
136
137         keys = rte_malloc(NULL, sizeof(uint32_t) * nb_entries, 0);
138
139         if (keys == NULL) {
140                 printf("RTE_MALLOC failed\n");
141                 goto err1;
142         }
143
144         found = rte_zmalloc(NULL, sizeof(uint32_t) * nb_entries, 0);
145         if (found == NULL) {
146                 printf("RTE_ZMALLOC failed\n");
147                 goto err2;
148         }
149
150         for (i = 0; i < nb_entries; i++)
151                 keys[i] = i;
152
153         tbl_multiwriter_test_params.keys = keys;
154         tbl_multiwriter_test_params.found = found;
155
156         rte_atomic64_init(&gcycles);
157         rte_atomic64_clear(&gcycles);
158
159         rte_atomic64_init(&ginsertions);
160         rte_atomic64_clear(&ginsertions);
161
162         /* Fire all threads. */
163         rte_eal_mp_remote_launch(test_hash_multiwriter_worker,
164                                  NULL, CALL_MASTER);
165         rte_eal_mp_wait_lcore();
166
167         count = rte_hash_count(handle);
168         if (count != rounded_nb_total_tsx_insertion) {
169                 printf("rte_hash_count returned wrong value %u, %d\n",
170                                 rounded_nb_total_tsx_insertion, count);
171                 goto err3;
172         }
173
174         while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
175                 /* Search for the key in the list of keys added .*/
176                 i = *(const uint32_t *)next_key;
177                 tbl_multiwriter_test_params.found[i]++;
178         }
179
180         for (i = 0; i < rounded_nb_total_tsx_insertion; i++) {
181                 if (tbl_multiwriter_test_params.keys[i]
182                     != RTE_APP_TEST_HASH_MULTIWRITER_FAILED) {
183                         if (tbl_multiwriter_test_params.found[i] > 1) {
184                                 duplicated_keys++;
185                                 break;
186                         }
187                         if (tbl_multiwriter_test_params.found[i] == 0) {
188                                 lost_keys++;
189                                 printf("key %d is lost\n", i);
190                                 break;
191                         }
192                 }
193         }
194
195         if (duplicated_keys > 0) {
196                 printf("%d key duplicated\n", duplicated_keys);
197                 goto err3;
198         }
199
200         if (lost_keys > 0) {
201                 printf("%d key lost\n", lost_keys);
202                 goto err3;
203         }
204
205         printf("No key corrupted during multiwriter insertion.\n");
206
207         unsigned long long int cycles_per_insertion =
208                 rte_atomic64_read(&gcycles)/
209                 rte_atomic64_read(&ginsertions);
210
211         printf(" cycles per insertion: %llu\n", cycles_per_insertion);
212
213         rte_free(tbl_multiwriter_test_params.found);
214         rte_free(tbl_multiwriter_test_params.keys);
215         rte_hash_free(handle);
216         return 0;
217
218 err3:
219         rte_free(tbl_multiwriter_test_params.found);
220 err2:
221         rte_free(tbl_multiwriter_test_params.keys);
222 err1:
223         rte_hash_free(handle);
224         return -1;
225 }
226
227 static int
228 test_hash_multiwriter_main(void)
229 {
230         if (rte_lcore_count() == 1) {
231                 printf("More than one lcore is required to do multiwriter test\n");
232                 return 0;
233         }
234
235
236         setlocale(LC_NUMERIC, "");
237
238
239         if (!rte_tm_supported()) {
240                 printf("Hardware transactional memory (lock elision) "
241                         "is NOT supported\n");
242         } else {
243                 printf("Hardware transactional memory (lock elision) "
244                         "is supported\n");
245
246                 printf("Test multi-writer with Hardware transactional memory\n");
247
248                 use_htm = 1;
249                 if (test_hash_multiwriter() < 0)
250                         return -1;
251         }
252
253         printf("Test multi-writer without Hardware transactional memory\n");
254         use_htm = 0;
255         if (test_hash_multiwriter() < 0)
256                 return -1;
257
258         return 0;
259 }
260
261 REGISTER_TEST_COMMAND(hash_multiwriter_autotest, test_hash_multiwriter_main);