1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
9 #include <rte_cycles.h>
10 #include <rte_malloc.h>
11 #include <rte_random.h>
13 #include <rte_memcpy.h>
14 #include <rte_thash.h>
18 #define NUM_KEYSIZES 10
19 #define NUM_SHUFFLES 10
20 #define MAX_KEYSIZE 64
21 #define MAX_ENTRIES (1 << 19)
22 #define KEYS_TO_ADD (MAX_ENTRIES * 3 / 4) /* 75% table utilization */
23 #define NUM_LOOKUPS (KEYS_TO_ADD * 5) /* Loop among keys added, several times */
25 #if RTE_EFD_VALUE_NUM_BITS == 32
26 #define VALUE_BITMASK 0xffffffff
28 #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
30 static unsigned int test_socket_id;
32 static inline uint8_t efd_get_all_sockets_bitmask(void)
34 uint8_t all_cpu_sockets_bitmask = 0;
36 unsigned int next_lcore = rte_get_master_lcore();
37 const int val_true = 1, val_false = 0;
38 for (i = 0; i < rte_lcore_count(); i++) {
39 all_cpu_sockets_bitmask |= 1 << rte_lcore_to_socket_id(next_lcore);
40 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
43 return all_cpu_sockets_bitmask;
54 struct efd_perf_params {
55 struct rte_efd_table *efd_table;
60 static uint32_t hashtest_key_lens[] = {
61 /* standard key sizes */
63 /* IPv4 SRC + DST + protocol, unpadded */
65 /* IPv4 5-tuple, unpadded */
67 /* IPv6 5-tuple, unpadded */
69 /* IPv6 5-tuple, padded to 8-byte boundary */
73 /* Array to store number of cycles per operation */
74 static uint64_t cycles[NUM_KEYSIZES][NUM_OPERATIONS];
76 /* Array to store the data */
77 static efd_value_t data[KEYS_TO_ADD];
79 /* Array to store all input keys */
80 static uint8_t keys[KEYS_TO_ADD][MAX_KEYSIZE];
82 /* Shuffle the keys that have been added, so lookups will be totally random */
84 shuffle_input_keys(struct efd_perf_params *params)
86 efd_value_t temp_data;
89 uint8_t temp_key[MAX_KEYSIZE];
91 for (i = KEYS_TO_ADD - 1; i > 0; i--) {
92 swap_idx = rte_rand() % i;
94 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]);
97 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]);
98 data[i] = data[swap_idx];
100 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]);
101 data[swap_idx] = temp_data;
105 static int key_compare(const void *key1, const void *key2)
107 return memcmp(key1, key2, MAX_KEYSIZE);
111 * TODO: we could "error proof" these as done in test_hash_perf.c ln 165:
113 * The current setup may give errors if too full in some cases which we check
114 * for. However, since EFD allows for ~99% capacity, these errors are rare for
115 * #"KEYS_TO_ADD" which is 75% capacity.
118 setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle)
123 params->key_size = hashtest_key_lens[cycle];
124 params->cycle = cycle;
126 /* Reset all arrays */
127 for (i = 0; i < params->key_size; i++)
130 /* Generate a list of keys, some of which may be duplicates */
131 for (i = 0; i < KEYS_TO_ADD; i++) {
132 for (j = 0; j < params->key_size; j++)
133 keys[i][j] = rte_rand() & 0xFF;
135 data[i] = rte_rand() & VALUE_BITMASK;
138 /* Remove duplicates from the keys array */
142 /* Sort the list of keys to make it easier to find duplicates */
143 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare);
145 /* Sift through the list of keys and look for duplicates */
146 int num_duplicates = 0;
147 for (i = 0; i < KEYS_TO_ADD - 1; i++) {
148 if (memcmp(keys[i], keys[i + 1], params->key_size) == 0) {
149 /* This key already exists, try again */
151 for (j = 0; j < params->key_size; j++)
152 keys[i][j] = rte_rand() & 0xFF;
155 } while (num_duplicates != 0);
157 /* Shuffle the random values again */
158 shuffle_input_keys(params);
160 params->efd_table = rte_efd_create("test_efd_perf",
161 MAX_ENTRIES, params->key_size,
162 efd_get_all_sockets_bitmask(), test_socket_id);
163 TEST_ASSERT_NOT_NULL(params->efd_table, "Error creating the efd table\n");
169 timed_adds(struct efd_perf_params *params)
171 const uint64_t start_tsc = rte_rdtsc();
175 for (i = 0; i < KEYS_TO_ADD; i++) {
176 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i],
179 printf("Error %d in rte_efd_update - key=0x", ret);
180 for (a = 0; a < params->key_size; a++)
181 printf("%02x", keys[i][a]);
182 printf(" value=%d\n", data[i]);
188 const uint64_t end_tsc = rte_rdtsc();
189 const uint64_t time_taken = end_tsc - start_tsc;
191 cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD;
196 timed_lookups(struct efd_perf_params *params)
198 unsigned int i, j, a;
199 const uint64_t start_tsc = rte_rdtsc();
200 efd_value_t ret_data;
202 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
203 for (j = 0; j < KEYS_TO_ADD; j++) {
204 ret_data = rte_efd_lookup(params->efd_table,
205 test_socket_id, keys[j]);
206 if (ret_data != data[j]) {
207 printf("Value mismatch using rte_efd_lookup: "
209 for (a = 0; a < params->key_size; a++)
210 printf("%02x", keys[i][a]);
212 printf(" Expected %d, got %d\n", data[i],
221 const uint64_t end_tsc = rte_rdtsc();
222 const uint64_t time_taken = end_tsc - start_tsc;
224 cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS;
230 timed_lookups_multi(struct efd_perf_params *params)
232 unsigned int i, j, k, a;
233 efd_value_t result[RTE_EFD_BURST_MAX] = {0};
234 const void *keys_burst[RTE_EFD_BURST_MAX];
235 const uint64_t start_tsc = rte_rdtsc();
237 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
238 for (j = 0; j < KEYS_TO_ADD / RTE_EFD_BURST_MAX; j++) {
239 for (k = 0; k < RTE_EFD_BURST_MAX; k++)
240 keys_burst[k] = keys[j * RTE_EFD_BURST_MAX + k];
242 rte_efd_lookup_bulk(params->efd_table, test_socket_id,
246 for (k = 0; k < RTE_EFD_BURST_MAX; k++) {
247 uint32_t data_idx = j * RTE_EFD_BURST_MAX + k;
248 if (result[k] != data[data_idx]) {
249 printf("Value mismatch using "
250 "rte_efd_lookup_bulk: key #%d "
252 for (a = 0; a < params->key_size; a++)
256 printf(" Expected %d, got %d\n",
257 data[data_idx], result[k]);
265 const uint64_t end_tsc = rte_rdtsc();
266 const uint64_t time_taken = end_tsc - start_tsc;
268 cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS;
274 timed_deletes(struct efd_perf_params *params)
277 const uint64_t start_tsc = rte_rdtsc();
280 for (i = 0; i < KEYS_TO_ADD; i++) {
281 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i],
285 printf("Error %d in rte_efd_delete - key=0x", ret);
286 for (a = 0; a < params->key_size; a++)
287 printf("%02x", keys[i][a]);
294 const uint64_t end_tsc = rte_rdtsc();
295 const uint64_t time_taken = end_tsc - start_tsc;
297 cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD;
303 perform_frees(struct efd_perf_params *params)
305 if (params->efd_table != NULL) {
306 rte_efd_free(params->efd_table);
307 params->efd_table = NULL;
312 exit_with_fail(const char *testname, struct efd_perf_params *params,
316 printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n",
317 testname, hashtest_key_lens[params->cycle], i);
318 perform_frees(params);
323 run_all_tbl_perf_tests(void)
326 struct efd_perf_params params;
328 printf("Measuring performance, please wait\n");
331 test_socket_id = rte_socket_id();
333 for (i = 0; i < NUM_KEYSIZES; i++) {
335 if (setup_keys_and_data(¶ms, i) < 0) {
336 printf("Could not create keys/data/table\n");
340 if (timed_adds(¶ms) < 0)
341 return exit_with_fail("timed_adds", ¶ms, i);
343 for (j = 0; j < NUM_SHUFFLES; j++)
344 shuffle_input_keys(¶ms);
346 if (timed_lookups(¶ms) < 0)
347 return exit_with_fail("timed_lookups", ¶ms, i);
349 if (timed_lookups_multi(¶ms) < 0)
350 return exit_with_fail("timed_lookups_multi", ¶ms, i);
352 if (timed_deletes(¶ms) < 0)
353 return exit_with_fail("timed_deletes", ¶ms, i);
355 /* Print a dot to show progress on operations */
359 perform_frees(¶ms);
362 printf("\nResults (in CPU cycles/operation)\n");
363 printf("-----------------------------------\n");
364 printf("\n%-18s%-18s%-18s%-18s%-18s\n",
365 "Keysize", "Add", "Lookup", "Lookup_bulk", "Delete");
366 for (i = 0; i < NUM_KEYSIZES; i++) {
367 printf("%-18d", hashtest_key_lens[i]);
368 for (j = 0; j < NUM_OPERATIONS; j++)
369 printf("%-18"PRIu64, cycles[i][j]);
379 if (run_all_tbl_perf_tests() < 0)
385 REGISTER_TEST_COMMAND(efd_perf_autotest, test_efd_perf);