1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 16 * distribution. 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. 20 * 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. 32 */ 33 34 #include <stdio.h> 35 #include <inttypes.h> 36 37 #include <rte_lcore.h> 38 #include <rte_cycles.h> 39 #include <rte_malloc.h> 40 #include <rte_random.h> 41 #include <rte_efd.h> 42 #include <rte_memcpy.h> 43 #include <rte_thash.h> 44 45 #include "test.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; 53 54 static inline uint8_t efd_get_all_sockets_bitmask(void) 55 { 56 uint8_t all_cpu_sockets_bitmask = 0; 57 unsigned int i; 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); 63 } 64 65 return all_cpu_sockets_bitmask; 66 } 67 68 enum operations { 69 ADD = 0, 70 LOOKUP, 71 LOOKUP_MULTI, 72 DELETE, 73 NUM_OPERATIONS 74 }; 75 76 struct efd_perf_params { 77 struct rte_efd_table *efd_table; 78 uint32_t key_size; 79 unsigned int cycle; 80 }; 81 82 static uint32_t hashtest_key_lens[] = { 83 /* standard key sizes */ 84 4, 8, 16, 32, 48, 64, 85 /* IPv4 SRC + DST + protocol, unpadded */ 86 9, 87 /* IPv4 5-tuple, unpadded */ 88 13, 89 /* IPv6 5-tuple, unpadded */ 90 37, 91 /* IPv6 5-tuple, padded to 8-byte boundary */ 92 40 93 }; 94 95 /* Array to store number of cycles per operation */ 96 uint64_t cycles[NUM_KEYSIZES][NUM_OPERATIONS]; 97 98 /* Array to store the data */ 99 efd_value_t data[KEYS_TO_ADD]; 100 101 /* Array to store all input keys */ 102 uint8_t keys[KEYS_TO_ADD][MAX_KEYSIZE]; 103 104 /* Shuffle the keys that have been added, so lookups will be totally random */ 105 static void 106 shuffle_input_keys(struct efd_perf_params *params) 107 { 108 efd_value_t temp_data; 109 unsigned int i; 110 uint32_t swap_idx; 111 uint8_t temp_key[MAX_KEYSIZE]; 112 113 for (i = KEYS_TO_ADD - 1; i > 0; i--) { 114 swap_idx = rte_rand() % i; 115 116 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]); 117 temp_data = data[i]; 118 119 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]); 120 data[i] = data[swap_idx]; 121 122 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]); 123 data[swap_idx] = temp_data; 124 } 125 } 126 127 static int key_compare(const void *key1, const void *key2) 128 { 129 return memcmp(key1, key2, MAX_KEYSIZE); 130 } 131 132 /* 133 * TODO: we could "error proof" these as done in test_hash_perf.c ln 165: 134 * 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. 138 */ 139 static int 140 setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle) 141 { 142 unsigned int i, j; 143 int num_duplicates; 144 145 params->key_size = hashtest_key_lens[cycle]; 146 params->cycle = cycle; 147 148 /* Reset all arrays */ 149 for (i = 0; i < params->key_size; i++) 150 keys[0][i] = 0; 151 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; 156 157 data[i] = rte_rand() & ((1 << RTE_EFD_VALUE_NUM_BITS) - 1); 158 } 159 160 /* Remove duplicates from the keys array */ 161 do { 162 num_duplicates = 0; 163 164 /* Sort the list of keys to make it easier to find duplicates */ 165 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare); 166 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 */ 172 num_duplicates++; 173 for (j = 0; j < params->key_size; j++) 174 keys[i][j] = rte_rand() & 0xFF; 175 } 176 } 177 } while (num_duplicates != 0); 178 179 /* Shuffle the random values again */ 180 shuffle_input_keys(params); 181 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"); 186 187 return 0; 188 } 189 190 static int 191 timed_adds(struct efd_perf_params *params) 192 { 193 const uint64_t start_tsc = rte_rdtsc(); 194 unsigned int i, a; 195 int32_t ret; 196 197 for (i = 0; i < KEYS_TO_ADD; i++) { 198 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i], 199 data[i]); 200 if (ret != 0) { 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]); 205 206 return -1; 207 } 208 } 209 210 const uint64_t end_tsc = rte_rdtsc(); 211 const uint64_t time_taken = end_tsc - start_tsc; 212 213 cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD; 214 return 0; 215 } 216 217 static int 218 timed_lookups(struct efd_perf_params *params) 219 { 220 unsigned int i, j, a; 221 const uint64_t start_tsc = rte_rdtsc(); 222 efd_value_t ret_data; 223 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: " 230 "key #%d (0x", i); 231 for (a = 0; a < params->key_size; a++) 232 printf("%02x", keys[i][a]); 233 printf(")\n"); 234 printf(" Expected %d, got %d\n", data[i], 235 ret_data); 236 237 return -1; 238 } 239 240 } 241 } 242 243 const uint64_t end_tsc = rte_rdtsc(); 244 const uint64_t time_taken = end_tsc - start_tsc; 245 246 cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS; 247 248 return 0; 249 } 250 251 static int 252 timed_lookups_multi(struct efd_perf_params *params) 253 { 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(); 258 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]; 263 264 rte_efd_lookup_bulk(params->efd_table, test_socket_id, 265 RTE_EFD_BURST_MAX, 266 keys_burst, result); 267 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 " 273 "(0x", i); 274 for (a = 0; a < params->key_size; a++) 275 printf("%02x", 276 keys[data_idx][a]); 277 printf(")\n"); 278 printf(" Expected %d, got %d\n", 279 data[data_idx], result[k]); 280 281 return -1; 282 } 283 } 284 } 285 } 286 287 const uint64_t end_tsc = rte_rdtsc(); 288 const uint64_t time_taken = end_tsc - start_tsc; 289 290 cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS; 291 292 return 0; 293 } 294 295 static int 296 timed_deletes(struct efd_perf_params *params) 297 { 298 unsigned int i, a; 299 const uint64_t start_tsc = rte_rdtsc(); 300 int32_t ret; 301 302 for (i = 0; i < KEYS_TO_ADD; i++) { 303 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i], 304 NULL); 305 306 if (ret != 0) { 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]); 310 printf("\n"); 311 312 return -1; 313 } 314 } 315 316 const uint64_t end_tsc = rte_rdtsc(); 317 const uint64_t time_taken = end_tsc - start_tsc; 318 319 cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD; 320 321 return 0; 322 } 323 324 static void 325 perform_frees(struct efd_perf_params *params) 326 { 327 if (params->efd_table != NULL) { 328 rte_efd_free(params->efd_table); 329 params->efd_table = NULL; 330 } 331 } 332 333 static int 334 exit_with_fail(const char *testname, struct efd_perf_params *params, 335 unsigned int i) 336 { 337 338 printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n", 339 testname, hashtest_key_lens[params->cycle], i); 340 perform_frees(params); 341 return -1; 342 } 343 344 static int 345 run_all_tbl_perf_tests(void) 346 { 347 unsigned int i, j; 348 struct efd_perf_params params; 349 350 printf("Measuring performance, please wait\n"); 351 fflush(stdout); 352 353 test_socket_id = rte_socket_id(); 354 355 for (i = 0; i < NUM_KEYSIZES; i++) { 356 357 if (setup_keys_and_data(¶ms, i) < 0) { 358 printf("Could not create keys/data/table\n"); 359 return -1; 360 } 361 362 if (timed_adds(¶ms) < 0) 363 return exit_with_fail("timed_adds", ¶ms, i); 364 365 for (j = 0; j < NUM_SHUFFLES; j++) 366 shuffle_input_keys(¶ms); 367 368 if (timed_lookups(¶ms) < 0) 369 return exit_with_fail("timed_lookups", ¶ms, i); 370 371 if (timed_lookups_multi(¶ms) < 0) 372 return exit_with_fail("timed_lookups_multi", ¶ms, i); 373 374 if (timed_deletes(¶ms) < 0) 375 return exit_with_fail("timed_deletes", ¶ms, i); 376 377 /* Print a dot to show progress on operations */ 378 printf("."); 379 fflush(stdout); 380 381 perform_frees(¶ms); 382 } 383 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]); 392 printf("\n"); 393 } 394 return 0; 395 } 396 397 static int 398 test_efd_perf(void) 399 { 400 401 if (run_all_tbl_perf_tests() < 0) 402 return -1; 403 404 return 0; 405 } 406 407 REGISTER_TEST_COMMAND(efd_perf_autotest, test_efd_perf); 408