4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_lcore.h>
38 #include <rte_cycles.h>
39 #include <rte_malloc.h>
40 #include <rte_random.h>
42 #include <rte_memcpy.h>
43 #include <rte_thash.h>
46 #define NUM_KEYSIZES 10
47 #define NUM_SHUFFLES 10
48 #define MAX_KEYSIZE 64
49 #define MAX_ENTRIES (1 << 19)
50 #define KEYS_TO_ADD (MAX_ENTRIES * 3 / 4) /* 75% table utilization */
51 #define NUM_LOOKUPS (KEYS_TO_ADD * 5) /* Loop among keys added, several times */
52 static unsigned int test_socket_id;
54 static inline uint8_t efd_get_all_sockets_bitmask(void)
56 uint8_t all_cpu_sockets_bitmask = 0;
58 unsigned int next_lcore = rte_get_master_lcore();
59 const int val_true = 1, val_false = 0;
60 for (i = 0; i < rte_lcore_count(); i++) {
61 all_cpu_sockets_bitmask |= 1 << rte_lcore_to_socket_id(next_lcore);
62 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
65 return all_cpu_sockets_bitmask;
76 struct efd_perf_params {
77 struct rte_efd_table *efd_table;
82 static uint32_t hashtest_key_lens[] = {
83 /* standard key sizes */
85 /* IPv4 SRC + DST + protocol, unpadded */
87 /* IPv4 5-tuple, unpadded */
89 /* IPv6 5-tuple, unpadded */
91 /* IPv6 5-tuple, padded to 8-byte boundary */
95 /* Array to store number of cycles per operation */
96 uint64_t cycles[NUM_KEYSIZES][NUM_OPERATIONS];
98 /* Array to store the data */
99 efd_value_t data[KEYS_TO_ADD];
101 /* Array to store all input keys */
102 uint8_t keys[KEYS_TO_ADD][MAX_KEYSIZE];
104 /* Shuffle the keys that have been added, so lookups will be totally random */
106 shuffle_input_keys(struct efd_perf_params *params)
108 efd_value_t temp_data;
111 uint8_t temp_key[MAX_KEYSIZE];
113 for (i = KEYS_TO_ADD - 1; i > 0; i--) {
114 swap_idx = rte_rand() % i;
116 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]);
119 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]);
120 data[i] = data[swap_idx];
122 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]);
123 data[swap_idx] = temp_data;
127 static int key_compare(const void *key1, const void *key2)
129 return memcmp(key1, key2, MAX_KEYSIZE);
133 * TODO: we could "error proof" these as done in test_hash_perf.c ln 165:
135 * The current setup may give errors if too full in some cases which we check
136 * for. However, since EFD allows for ~99% capacity, these errors are rare for
137 * #"KEYS_TO_ADD" which is 75% capacity.
140 setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle)
145 params->key_size = hashtest_key_lens[cycle];
146 params->cycle = cycle;
148 /* Reset all arrays */
149 for (i = 0; i < params->key_size; i++)
152 /* Generate a list of keys, some of which may be duplicates */
153 for (i = 0; i < KEYS_TO_ADD; i++) {
154 for (j = 0; j < params->key_size; j++)
155 keys[i][j] = rte_rand() & 0xFF;
157 data[i] = rte_rand() & ((1 << RTE_EFD_VALUE_NUM_BITS) - 1);
160 /* Remove duplicates from the keys array */
164 /* Sort the list of keys to make it easier to find duplicates */
165 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare);
167 /* Sift through the list of keys and look for duplicates */
168 int num_duplicates = 0;
169 for (i = 0; i < KEYS_TO_ADD - 1; i++) {
170 if (memcmp(keys[i], keys[i + 1], params->key_size) == 0) {
171 /* This key already exists, try again */
173 for (j = 0; j < params->key_size; j++)
174 keys[i][j] = rte_rand() & 0xFF;
177 } while (num_duplicates != 0);
179 /* Shuffle the random values again */
180 shuffle_input_keys(params);
182 params->efd_table = rte_efd_create("test_efd_perf",
183 MAX_ENTRIES, params->key_size,
184 efd_get_all_sockets_bitmask(), test_socket_id);
185 TEST_ASSERT_NOT_NULL(params->efd_table, "Error creating the efd table\n");
191 timed_adds(struct efd_perf_params *params)
193 const uint64_t start_tsc = rte_rdtsc();
197 for (i = 0; i < KEYS_TO_ADD; i++) {
198 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i],
201 printf("Error %d in rte_efd_update - key=0x", ret);
202 for (a = 0; a < params->key_size; a++)
203 printf("%02x", keys[i][a]);
204 printf(" value=%d\n", data[i]);
210 const uint64_t end_tsc = rte_rdtsc();
211 const uint64_t time_taken = end_tsc - start_tsc;
213 cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD;
218 timed_lookups(struct efd_perf_params *params)
220 unsigned int i, j, a;
221 const uint64_t start_tsc = rte_rdtsc();
222 efd_value_t ret_data;
224 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
225 for (j = 0; j < KEYS_TO_ADD; j++) {
226 ret_data = rte_efd_lookup(params->efd_table,
227 test_socket_id, keys[j]);
228 if (ret_data != data[j]) {
229 printf("Value mismatch using rte_efd_lookup: "
231 for (a = 0; a < params->key_size; a++)
232 printf("%02x", keys[i][a]);
234 printf(" Expected %d, got %d\n", data[i],
243 const uint64_t end_tsc = rte_rdtsc();
244 const uint64_t time_taken = end_tsc - start_tsc;
246 cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS;
252 timed_lookups_multi(struct efd_perf_params *params)
254 unsigned int i, j, k, a;
255 efd_value_t result[RTE_EFD_BURST_MAX] = {0};
256 const void *keys_burst[RTE_EFD_BURST_MAX];
257 const uint64_t start_tsc = rte_rdtsc();
259 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
260 for (j = 0; j < KEYS_TO_ADD / RTE_EFD_BURST_MAX; j++) {
261 for (k = 0; k < RTE_EFD_BURST_MAX; k++)
262 keys_burst[k] = keys[j * RTE_EFD_BURST_MAX + k];
264 rte_efd_lookup_bulk(params->efd_table, test_socket_id,
268 for (k = 0; k < RTE_EFD_BURST_MAX; k++) {
269 uint32_t data_idx = j * RTE_EFD_BURST_MAX + k;
270 if (result[k] != data[data_idx]) {
271 printf("Value mismatch using "
272 "rte_efd_lookup_bulk: key #%d "
274 for (a = 0; a < params->key_size; a++)
278 printf(" Expected %d, got %d\n",
279 data[data_idx], result[k]);
287 const uint64_t end_tsc = rte_rdtsc();
288 const uint64_t time_taken = end_tsc - start_tsc;
290 cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS;
296 timed_deletes(struct efd_perf_params *params)
299 const uint64_t start_tsc = rte_rdtsc();
302 for (i = 0; i < KEYS_TO_ADD; i++) {
303 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i],
307 printf("Error %d in rte_efd_delete - key=0x", ret);
308 for (a = 0; a < params->key_size; a++)
309 printf("%02x", keys[i][a]);
316 const uint64_t end_tsc = rte_rdtsc();
317 const uint64_t time_taken = end_tsc - start_tsc;
319 cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD;
325 perform_frees(struct efd_perf_params *params)
327 if (params->efd_table != NULL) {
328 rte_efd_free(params->efd_table);
329 params->efd_table = NULL;
334 exit_with_fail(const char *testname, struct efd_perf_params *params,
338 printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n",
339 testname, hashtest_key_lens[params->cycle], i);
340 perform_frees(params);
345 run_all_tbl_perf_tests(void)
348 struct efd_perf_params params;
350 printf("Measuring performance, please wait\n");
353 test_socket_id = rte_socket_id();
355 for (i = 0; i < NUM_KEYSIZES; i++) {
357 if (setup_keys_and_data(¶ms, i) < 0) {
358 printf("Could not create keys/data/table\n");
362 if (timed_adds(¶ms) < 0)
363 return exit_with_fail("timed_adds", ¶ms, i);
365 for (j = 0; j < NUM_SHUFFLES; j++)
366 shuffle_input_keys(¶ms);
368 if (timed_lookups(¶ms) < 0)
369 return exit_with_fail("timed_lookups", ¶ms, i);
371 if (timed_lookups_multi(¶ms) < 0)
372 return exit_with_fail("timed_lookups_multi", ¶ms, i);
374 if (timed_deletes(¶ms) < 0)
375 return exit_with_fail("timed_deletes", ¶ms, i);
377 /* Print a dot to show progress on operations */
381 perform_frees(¶ms);
384 printf("\nResults (in CPU cycles/operation)\n");
385 printf("-----------------------------------\n");
386 printf("\n%-18s%-18s%-18s%-18s%-18s\n",
387 "Keysize", "Add", "Lookup", "Lookup_bulk", "Delete");
388 for (i = 0; i < NUM_KEYSIZES; i++) {
389 printf("%-18d", hashtest_key_lens[i]);
390 for (j = 0; j < NUM_OPERATIONS; j++)
391 printf("%-18"PRIu64, cycles[i][j]);
401 if (run_all_tbl_perf_tests() < 0)
407 REGISTER_TEST_COMMAND(efd_perf_autotest, test_efd_perf);