1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
8 #include <rte_cycles.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>
19 * Check condition and return an error if true. Assumes that "handle" is the
20 * name of the hash structure pointer to be freed.
22 #define RETURN_IF_ERROR(cond, str, ...) do { \
24 printf("ERROR line %d: " str "\n", __LINE__, \
27 rte_hash_free(handle); \
32 #define RTE_APP_TEST_HASH_MULTIWRITER_FAILED 0
37 uint32_t nb_tsx_insertion;
39 } tbl_multiwriter_test_params;
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;
45 static rte_atomic64_t gcycles;
46 static rte_atomic64_t ginsertions;
51 test_hash_multiwriter_worker(__attribute__((unused)) void *arg)
54 uint32_t lcore_id = rte_lcore_id();
55 uint64_t begin, cycles;
57 offset = (lcore_id - rte_get_master_lcore())
58 * tbl_multiwriter_test_params.nb_tsx_insertion;
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);
64 begin = rte_rdtsc_precise();
67 i < offset + tbl_multiwriter_test_params.nb_tsx_insertion;
69 if (rte_hash_add_key(tbl_multiwriter_test_params.h,
70 tbl_multiwriter_test_params.keys + i) < 0)
74 cycles = rte_rdtsc_precise() - begin;
75 rte_atomic64_add(&gcycles, cycles);
76 rte_atomic64_add(&ginsertions, i - offset);
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;
87 test_hash_multiwriter(void)
89 unsigned int i, rounded_nb_total_tsx_insertion;
90 static unsigned calledCount = 1;
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(),
103 hash_params.extra_flag =
104 RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT
105 | RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
107 hash_params.extra_flag =
108 RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
110 struct rte_hash *handle;
111 char name[RTE_HASH_NAMESIZE];
113 const void *next_key;
117 uint32_t duplicated_keys = 0;
118 uint32_t lost_keys = 0;
121 snprintf(name, 32, "test%u", calledCount++);
122 hash_params.name = name;
124 handle = rte_hash_create(&hash_params);
125 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
127 tbl_multiwriter_test_params.h = handle;
128 tbl_multiwriter_test_params.nb_tsx_insertion =
129 nb_total_tsx_insertion / rte_lcore_count();
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;
135 rte_srand(rte_rdtsc());
137 keys = rte_malloc(NULL, sizeof(uint32_t) * nb_entries, 0);
140 printf("RTE_MALLOC failed\n");
144 found = rte_zmalloc(NULL, sizeof(uint32_t) * nb_entries, 0);
146 printf("RTE_ZMALLOC failed\n");
150 for (i = 0; i < nb_entries; i++)
153 tbl_multiwriter_test_params.keys = keys;
154 tbl_multiwriter_test_params.found = found;
156 rte_atomic64_init(&gcycles);
157 rte_atomic64_clear(&gcycles);
159 rte_atomic64_init(&ginsertions);
160 rte_atomic64_clear(&ginsertions);
162 /* Fire all threads. */
163 rte_eal_mp_remote_launch(test_hash_multiwriter_worker,
165 rte_eal_mp_wait_lcore();
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);
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]++;
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) {
187 if (tbl_multiwriter_test_params.found[i] == 0) {
189 printf("key %d is lost\n", i);
195 if (duplicated_keys > 0) {
196 printf("%d key duplicated\n", duplicated_keys);
201 printf("%d key lost\n", lost_keys);
205 printf("No key corrupted during multiwriter insertion.\n");
207 unsigned long long int cycles_per_insertion =
208 rte_atomic64_read(&gcycles)/
209 rte_atomic64_read(&ginsertions);
211 printf(" cycles per insertion: %llu\n", cycles_per_insertion);
213 rte_free(tbl_multiwriter_test_params.found);
214 rte_free(tbl_multiwriter_test_params.keys);
215 rte_hash_free(handle);
219 rte_free(tbl_multiwriter_test_params.found);
221 rte_free(tbl_multiwriter_test_params.keys);
223 rte_hash_free(handle);
228 test_hash_multiwriter_main(void)
230 if (rte_lcore_count() == 1) {
231 printf("More than one lcore is required to do multiwriter test\n");
236 setlocale(LC_NUMERIC, "");
239 if (!rte_tm_supported()) {
240 printf("Hardware transactional memory (lock elision) "
241 "is NOT supported\n");
243 printf("Hardware transactional memory (lock elision) "
246 printf("Test multi-writer with Hardware transactional memory\n");
249 if (test_hash_multiwriter() < 0)
253 printf("Test multi-writer without Hardware transactional memory\n");
255 if (test_hash_multiwriter() < 0)
261 REGISTER_TEST_COMMAND(hash_multiwriter_autotest, test_hash_multiwriter_main);