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