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;
120 snprintf(name, 32, "test%u", calledCount++);
121 hash_params.name = name;
123 handle = rte_hash_create(&hash_params);
124 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
126 tbl_multiwriter_test_params.h = handle;
127 tbl_multiwriter_test_params.nb_tsx_insertion =
128 nb_total_tsx_insertion / rte_lcore_count();
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;
134 rte_srand(rte_rdtsc());
136 keys = rte_malloc(NULL, sizeof(uint32_t) * nb_entries, 0);
139 printf("RTE_MALLOC failed\n");
143 found = rte_zmalloc(NULL, sizeof(uint32_t) * nb_entries, 0);
145 printf("RTE_ZMALLOC failed\n");
149 for (i = 0; i < nb_entries; i++)
152 tbl_multiwriter_test_params.keys = keys;
153 tbl_multiwriter_test_params.found = found;
155 rte_atomic64_init(&gcycles);
156 rte_atomic64_clear(&gcycles);
158 rte_atomic64_init(&ginsertions);
159 rte_atomic64_clear(&ginsertions);
161 /* Fire all threads. */
162 rte_eal_mp_remote_launch(test_hash_multiwriter_worker,
164 rte_eal_mp_wait_lcore();
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]++;
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) {
179 if (tbl_multiwriter_test_params.found[i] == 0) {
181 printf("key %d is lost\n", i);
187 if (duplicated_keys > 0) {
188 printf("%d key duplicated\n", duplicated_keys);
193 printf("%d key lost\n", lost_keys);
197 printf("No key corrupted during multiwriter insertion.\n");
199 unsigned long long int cycles_per_insertion =
200 rte_atomic64_read(&gcycles)/
201 rte_atomic64_read(&ginsertions);
203 printf(" cycles per insertion: %llu\n", cycles_per_insertion);
205 rte_free(tbl_multiwriter_test_params.found);
206 rte_free(tbl_multiwriter_test_params.keys);
207 rte_hash_free(handle);
211 rte_free(tbl_multiwriter_test_params.found);
213 rte_free(tbl_multiwriter_test_params.keys);
215 rte_hash_free(handle);
220 test_hash_multiwriter_main(void)
222 if (rte_lcore_count() == 1) {
223 printf("More than one lcore is required to do multiwriter test\n");
228 setlocale(LC_NUMERIC, "");
231 if (!rte_tm_supported()) {
232 printf("Hardware transactional memory (lock elision) "
233 "is NOT supported\n");
235 printf("Hardware transactional memory (lock elision) "
238 printf("Test multi-writer with Hardware transactional memory\n");
241 if (test_hash_multiwriter() < 0)
245 printf("Test multi-writer without Hardware transactional memory\n");
247 if (test_hash_multiwriter() < 0)
253 REGISTER_TEST_COMMAND(hash_multiwriter_autotest, test_hash_multiwriter_main);