New upstream version 18.02
[deb_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
120         snprintf(name, 32, "test%u", calledCount++);
121         hash_params.name = name;
122
123         handle = rte_hash_create(&hash_params);
124         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
125
126         tbl_multiwriter_test_params.h = handle;
127         tbl_multiwriter_test_params.nb_tsx_insertion =
128                 nb_total_tsx_insertion / rte_lcore_count();
129
130         rounded_nb_total_tsx_insertion = (nb_total_tsx_insertion /
131                 tbl_multiwriter_test_params.nb_tsx_insertion)
132                 * tbl_multiwriter_test_params.nb_tsx_insertion;
133
134         rte_srand(rte_rdtsc());
135
136         keys = rte_malloc(NULL, sizeof(uint32_t) * nb_entries, 0);
137
138         if (keys == NULL) {
139                 printf("RTE_MALLOC failed\n");
140                 goto err1;
141         }
142
143         found = rte_zmalloc(NULL, sizeof(uint32_t) * nb_entries, 0);
144         if (found == NULL) {
145                 printf("RTE_ZMALLOC failed\n");
146                 goto err2;
147         }
148
149         for (i = 0; i < nb_entries; i++)
150                 keys[i] = i;
151
152         tbl_multiwriter_test_params.keys = keys;
153         tbl_multiwriter_test_params.found = found;
154
155         rte_atomic64_init(&gcycles);
156         rte_atomic64_clear(&gcycles);
157
158         rte_atomic64_init(&ginsertions);
159         rte_atomic64_clear(&ginsertions);
160
161         /* Fire all threads. */
162         rte_eal_mp_remote_launch(test_hash_multiwriter_worker,
163                                  NULL, CALL_MASTER);
164         rte_eal_mp_wait_lcore();
165
166         while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) {
167                 /* Search for the key in the list of keys added .*/
168                 i = *(const uint32_t *)next_key;
169                 tbl_multiwriter_test_params.found[i]++;
170         }
171
172         for (i = 0; i < rounded_nb_total_tsx_insertion; i++) {
173                 if (tbl_multiwriter_test_params.keys[i]
174                     != RTE_APP_TEST_HASH_MULTIWRITER_FAILED) {
175                         if (tbl_multiwriter_test_params.found[i] > 1) {
176                                 duplicated_keys++;
177                                 break;
178                         }
179                         if (tbl_multiwriter_test_params.found[i] == 0) {
180                                 lost_keys++;
181                                 printf("key %d is lost\n", i);
182                                 break;
183                         }
184                 }
185         }
186
187         if (duplicated_keys > 0) {
188                 printf("%d key duplicated\n", duplicated_keys);
189                 goto err3;
190         }
191
192         if (lost_keys > 0) {
193                 printf("%d key lost\n", lost_keys);
194                 goto err3;
195         }
196
197         printf("No key corrupted during multiwriter insertion.\n");
198
199         unsigned long long int cycles_per_insertion =
200                 rte_atomic64_read(&gcycles)/
201                 rte_atomic64_read(&ginsertions);
202
203         printf(" cycles per insertion: %llu\n", cycles_per_insertion);
204
205         rte_free(tbl_multiwriter_test_params.found);
206         rte_free(tbl_multiwriter_test_params.keys);
207         rte_hash_free(handle);
208         return 0;
209
210 err3:
211         rte_free(tbl_multiwriter_test_params.found);
212 err2:
213         rte_free(tbl_multiwriter_test_params.keys);
214 err1:
215         rte_hash_free(handle);
216         return -1;
217 }
218
219 static int
220 test_hash_multiwriter_main(void)
221 {
222         if (rte_lcore_count() == 1) {
223                 printf("More than one lcore is required to do multiwriter test\n");
224                 return 0;
225         }
226
227
228         setlocale(LC_NUMERIC, "");
229
230
231         if (!rte_tm_supported()) {
232                 printf("Hardware transactional memory (lock elision) "
233                         "is NOT supported\n");
234         } else {
235                 printf("Hardware transactional memory (lock elision) "
236                         "is supported\n");
237
238                 printf("Test multi-writer with Hardware transactional memory\n");
239
240                 use_htm = 1;
241                 if (test_hash_multiwriter() < 0)
242                         return -1;
243         }
244
245         printf("Test multi-writer without Hardware transactional memory\n");
246         use_htm = 0;
247         if (test_hash_multiwriter() < 0)
248                 return -1;
249
250         return 0;
251 }
252
253 REGISTER_TEST_COMMAND(hash_multiwriter_autotest, test_hash_multiwriter_main);