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>
47 #define NUM_KEYSIZES 10
48 #define NUM_SHUFFLES 10
49 #define MAX_KEYSIZE 64
50 #define MAX_ENTRIES (1 << 19)
51 #define KEYS_TO_ADD (MAX_ENTRIES * 3 / 4) /* 75% table utilization */
52 #define NUM_LOOKUPS (KEYS_TO_ADD * 5) /* Loop among keys added, several times */
54 #if RTE_EFD_VALUE_NUM_BITS == 32
55 #define VALUE_BITMASK 0xffffffff
57 #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
59 static unsigned int test_socket_id;
61 static inline uint8_t efd_get_all_sockets_bitmask(void)
63 uint8_t all_cpu_sockets_bitmask = 0;
65 unsigned int next_lcore = rte_get_master_lcore();
66 const int val_true = 1, val_false = 0;
67 for (i = 0; i < rte_lcore_count(); i++) {
68 all_cpu_sockets_bitmask |= 1 << rte_lcore_to_socket_id(next_lcore);
69 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
72 return all_cpu_sockets_bitmask;
83 struct efd_perf_params {
84 struct rte_efd_table *efd_table;
89 static uint32_t hashtest_key_lens[] = {
90 /* standard key sizes */
92 /* IPv4 SRC + DST + protocol, unpadded */
94 /* IPv4 5-tuple, unpadded */
96 /* IPv6 5-tuple, unpadded */
98 /* IPv6 5-tuple, padded to 8-byte boundary */
102 /* Array to store number of cycles per operation */
103 uint64_t cycles[NUM_KEYSIZES][NUM_OPERATIONS];
105 /* Array to store the data */
106 efd_value_t data[KEYS_TO_ADD];
108 /* Array to store all input keys */
109 uint8_t keys[KEYS_TO_ADD][MAX_KEYSIZE];
111 /* Shuffle the keys that have been added, so lookups will be totally random */
113 shuffle_input_keys(struct efd_perf_params *params)
115 efd_value_t temp_data;
118 uint8_t temp_key[MAX_KEYSIZE];
120 for (i = KEYS_TO_ADD - 1; i > 0; i--) {
121 swap_idx = rte_rand() % i;
123 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]);
126 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]);
127 data[i] = data[swap_idx];
129 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]);
130 data[swap_idx] = temp_data;
134 static int key_compare(const void *key1, const void *key2)
136 return memcmp(key1, key2, MAX_KEYSIZE);
140 * TODO: we could "error proof" these as done in test_hash_perf.c ln 165:
142 * The current setup may give errors if too full in some cases which we check
143 * for. However, since EFD allows for ~99% capacity, these errors are rare for
144 * #"KEYS_TO_ADD" which is 75% capacity.
147 setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle)
152 params->key_size = hashtest_key_lens[cycle];
153 params->cycle = cycle;
155 /* Reset all arrays */
156 for (i = 0; i < params->key_size; i++)
159 /* Generate a list of keys, some of which may be duplicates */
160 for (i = 0; i < KEYS_TO_ADD; i++) {
161 for (j = 0; j < params->key_size; j++)
162 keys[i][j] = rte_rand() & 0xFF;
164 data[i] = rte_rand() & VALUE_BITMASK;
167 /* Remove duplicates from the keys array */
171 /* Sort the list of keys to make it easier to find duplicates */
172 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare);
174 /* Sift through the list of keys and look for duplicates */
175 int num_duplicates = 0;
176 for (i = 0; i < KEYS_TO_ADD - 1; i++) {
177 if (memcmp(keys[i], keys[i + 1], params->key_size) == 0) {
178 /* This key already exists, try again */
180 for (j = 0; j < params->key_size; j++)
181 keys[i][j] = rte_rand() & 0xFF;
184 } while (num_duplicates != 0);
186 /* Shuffle the random values again */
187 shuffle_input_keys(params);
189 params->efd_table = rte_efd_create("test_efd_perf",
190 MAX_ENTRIES, params->key_size,
191 efd_get_all_sockets_bitmask(), test_socket_id);
192 TEST_ASSERT_NOT_NULL(params->efd_table, "Error creating the efd table\n");
198 timed_adds(struct efd_perf_params *params)
200 const uint64_t start_tsc = rte_rdtsc();
204 for (i = 0; i < KEYS_TO_ADD; i++) {
205 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i],
208 printf("Error %d in rte_efd_update - key=0x", ret);
209 for (a = 0; a < params->key_size; a++)
210 printf("%02x", keys[i][a]);
211 printf(" value=%d\n", data[i]);
217 const uint64_t end_tsc = rte_rdtsc();
218 const uint64_t time_taken = end_tsc - start_tsc;
220 cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD;
225 timed_lookups(struct efd_perf_params *params)
227 unsigned int i, j, a;
228 const uint64_t start_tsc = rte_rdtsc();
229 efd_value_t ret_data;
231 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
232 for (j = 0; j < KEYS_TO_ADD; j++) {
233 ret_data = rte_efd_lookup(params->efd_table,
234 test_socket_id, keys[j]);
235 if (ret_data != data[j]) {
236 printf("Value mismatch using rte_efd_lookup: "
238 for (a = 0; a < params->key_size; a++)
239 printf("%02x", keys[i][a]);
241 printf(" Expected %d, got %d\n", data[i],
250 const uint64_t end_tsc = rte_rdtsc();
251 const uint64_t time_taken = end_tsc - start_tsc;
253 cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS;
259 timed_lookups_multi(struct efd_perf_params *params)
261 unsigned int i, j, k, a;
262 efd_value_t result[RTE_EFD_BURST_MAX] = {0};
263 const void *keys_burst[RTE_EFD_BURST_MAX];
264 const uint64_t start_tsc = rte_rdtsc();
266 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
267 for (j = 0; j < KEYS_TO_ADD / RTE_EFD_BURST_MAX; j++) {
268 for (k = 0; k < RTE_EFD_BURST_MAX; k++)
269 keys_burst[k] = keys[j * RTE_EFD_BURST_MAX + k];
271 rte_efd_lookup_bulk(params->efd_table, test_socket_id,
275 for (k = 0; k < RTE_EFD_BURST_MAX; k++) {
276 uint32_t data_idx = j * RTE_EFD_BURST_MAX + k;
277 if (result[k] != data[data_idx]) {
278 printf("Value mismatch using "
279 "rte_efd_lookup_bulk: key #%d "
281 for (a = 0; a < params->key_size; a++)
285 printf(" Expected %d, got %d\n",
286 data[data_idx], result[k]);
294 const uint64_t end_tsc = rte_rdtsc();
295 const uint64_t time_taken = end_tsc - start_tsc;
297 cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS;
303 timed_deletes(struct efd_perf_params *params)
306 const uint64_t start_tsc = rte_rdtsc();
309 for (i = 0; i < KEYS_TO_ADD; i++) {
310 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i],
314 printf("Error %d in rte_efd_delete - key=0x", ret);
315 for (a = 0; a < params->key_size; a++)
316 printf("%02x", keys[i][a]);
323 const uint64_t end_tsc = rte_rdtsc();
324 const uint64_t time_taken = end_tsc - start_tsc;
326 cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD;
332 perform_frees(struct efd_perf_params *params)
334 if (params->efd_table != NULL) {
335 rte_efd_free(params->efd_table);
336 params->efd_table = NULL;
341 exit_with_fail(const char *testname, struct efd_perf_params *params,
345 printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n",
346 testname, hashtest_key_lens[params->cycle], i);
347 perform_frees(params);
352 run_all_tbl_perf_tests(void)
355 struct efd_perf_params params;
357 printf("Measuring performance, please wait\n");
360 test_socket_id = rte_socket_id();
362 for (i = 0; i < NUM_KEYSIZES; i++) {
364 if (setup_keys_and_data(¶ms, i) < 0) {
365 printf("Could not create keys/data/table\n");
369 if (timed_adds(¶ms) < 0)
370 return exit_with_fail("timed_adds", ¶ms, i);
372 for (j = 0; j < NUM_SHUFFLES; j++)
373 shuffle_input_keys(¶ms);
375 if (timed_lookups(¶ms) < 0)
376 return exit_with_fail("timed_lookups", ¶ms, i);
378 if (timed_lookups_multi(¶ms) < 0)
379 return exit_with_fail("timed_lookups_multi", ¶ms, i);
381 if (timed_deletes(¶ms) < 0)
382 return exit_with_fail("timed_deletes", ¶ms, i);
384 /* Print a dot to show progress on operations */
388 perform_frees(¶ms);
391 printf("\nResults (in CPU cycles/operation)\n");
392 printf("-----------------------------------\n");
393 printf("\n%-18s%-18s%-18s%-18s%-18s\n",
394 "Keysize", "Add", "Lookup", "Lookup_bulk", "Delete");
395 for (i = 0; i < NUM_KEYSIZES; i++) {
396 printf("%-18d", hashtest_key_lens[i]);
397 for (j = 0; j < NUM_OPERATIONS; j++)
398 printf("%-18"PRIu64, cycles[i][j]);
408 if (run_all_tbl_perf_tests() < 0)
414 REGISTER_TEST_COMMAND(efd_perf_autotest, test_efd_perf);