1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
3 */
4
5 #include "test.h"
6
7 #ifdef RTE_EXEC_ENV_WINDOWS
8 static int
test_efd_perf(void)9 test_efd_perf(void)
10 {
11 printf("EFD not supported on Windows, skipping test\n");
12 return TEST_SKIPPED;
13 }
14
15 #else
16
17 #include <stdio.h>
18 #include <inttypes.h>
19
20 #include <rte_lcore.h>
21 #include <rte_cycles.h>
22 #include <rte_malloc.h>
23 #include <rte_random.h>
24 #include <rte_efd.h>
25 #include <rte_memcpy.h>
26 #include <rte_thash.h>
27
28 #define NUM_KEYSIZES 10
29 #define NUM_SHUFFLES 10
30 #define MAX_KEYSIZE 64
31 #define MAX_ENTRIES (1 << 19)
32 #define KEYS_TO_ADD (MAX_ENTRIES * 3 / 4) /* 75% table utilization */
33 #define NUM_LOOKUPS (KEYS_TO_ADD * 5) /* Loop among keys added, several times */
34
35 #if RTE_EFD_VALUE_NUM_BITS == 32
36 #define VALUE_BITMASK 0xffffffff
37 #else
38 #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
39 #endif
40 static unsigned int test_socket_id;
41
efd_get_all_sockets_bitmask(void)42 static inline uint64_t efd_get_all_sockets_bitmask(void)
43 {
44 uint64_t all_cpu_sockets_bitmask = 0;
45 unsigned int i;
46 unsigned int next_lcore = rte_get_main_lcore();
47 const int val_true = 1, val_false = 0;
48 for (i = 0; i < rte_lcore_count(); i++) {
49 all_cpu_sockets_bitmask |= 1 << rte_lcore_to_socket_id(next_lcore);
50 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
51 }
52
53 return all_cpu_sockets_bitmask;
54 }
55
56 enum operations {
57 ADD = 0,
58 LOOKUP,
59 LOOKUP_MULTI,
60 DELETE,
61 NUM_OPERATIONS
62 };
63
64 struct efd_perf_params {
65 struct rte_efd_table *efd_table;
66 uint32_t key_size;
67 unsigned int cycle;
68 };
69
70 static uint32_t hashtest_key_lens[] = {
71 /* standard key sizes */
72 4, 8, 16, 32, 48, 64,
73 /* IPv4 SRC + DST + protocol, unpadded */
74 9,
75 /* IPv4 5-tuple, unpadded */
76 13,
77 /* IPv6 5-tuple, unpadded */
78 37,
79 /* IPv6 5-tuple, padded to 8-byte boundary */
80 40
81 };
82
83 /* Array to store number of cycles per operation */
84 static uint64_t cycles[NUM_KEYSIZES][NUM_OPERATIONS];
85
86 /* Array to store the data */
87 static efd_value_t data[KEYS_TO_ADD];
88
89 /* Array to store all input keys */
90 static uint8_t keys[KEYS_TO_ADD][MAX_KEYSIZE];
91
92 /* Shuffle the keys that have been added, so lookups will be totally random */
93 static void
shuffle_input_keys(struct efd_perf_params * params)94 shuffle_input_keys(struct efd_perf_params *params)
95 {
96 efd_value_t temp_data;
97 unsigned int i;
98 uint32_t swap_idx;
99 uint8_t temp_key[MAX_KEYSIZE];
100
101 for (i = KEYS_TO_ADD - 1; i > 0; i--) {
102 swap_idx = rte_rand() % i;
103
104 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]);
105 temp_data = data[i];
106
107 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]);
108 data[i] = data[swap_idx];
109
110 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]);
111 data[swap_idx] = temp_data;
112 }
113 }
114
key_compare(const void * key1,const void * key2)115 static int key_compare(const void *key1, const void *key2)
116 {
117 return memcmp(key1, key2, MAX_KEYSIZE);
118 }
119
120 /*
121 * TODO: we could "error proof" these as done in test_hash_perf.c ln 165:
122 *
123 * The current setup may give errors if too full in some cases which we check
124 * for. However, since EFD allows for ~99% capacity, these errors are rare for
125 * #"KEYS_TO_ADD" which is 75% capacity.
126 */
127 static int
setup_keys_and_data(struct efd_perf_params * params,unsigned int cycle)128 setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle)
129 {
130 unsigned int i, j;
131 int num_duplicates;
132
133 params->key_size = hashtest_key_lens[cycle];
134 params->cycle = cycle;
135
136 /* Reset all arrays */
137 for (i = 0; i < params->key_size; i++)
138 keys[0][i] = 0;
139
140 /* Generate a list of keys, some of which may be duplicates */
141 for (i = 0; i < KEYS_TO_ADD; i++) {
142 for (j = 0; j < params->key_size; j++)
143 keys[i][j] = rte_rand() & 0xFF;
144
145 data[i] = rte_rand() & VALUE_BITMASK;
146 }
147
148 /* Remove duplicates from the keys array */
149 do {
150 num_duplicates = 0;
151
152 /* Sort the list of keys to make it easier to find duplicates */
153 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare);
154
155 /* Sift through the list of keys and look for duplicates */
156 for (i = 0; i < KEYS_TO_ADD - 1; i++) {
157 if (memcmp(keys[i], keys[i + 1], params->key_size) == 0) {
158 /* This key already exists, try again */
159 num_duplicates++;
160 for (j = 0; j < params->key_size; j++)
161 keys[i][j] = rte_rand() & 0xFF;
162 }
163 }
164 } while (num_duplicates != 0);
165
166 /* Shuffle the random values again */
167 shuffle_input_keys(params);
168
169 params->efd_table = rte_efd_create("test_efd_perf",
170 MAX_ENTRIES, params->key_size,
171 efd_get_all_sockets_bitmask(), test_socket_id);
172 TEST_ASSERT_NOT_NULL(params->efd_table, "Error creating the efd table\n");
173
174 return 0;
175 }
176
177 static int
timed_adds(struct efd_perf_params * params)178 timed_adds(struct efd_perf_params *params)
179 {
180 const uint64_t start_tsc = rte_rdtsc();
181 unsigned int i, a;
182 int32_t ret;
183
184 for (i = 0; i < KEYS_TO_ADD; i++) {
185 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i],
186 data[i]);
187 if (ret != 0) {
188 printf("Error %d in rte_efd_update - key=0x", ret);
189 for (a = 0; a < params->key_size; a++)
190 printf("%02x", keys[i][a]);
191 printf(" value=%d\n", data[i]);
192
193 return -1;
194 }
195 }
196
197 const uint64_t end_tsc = rte_rdtsc();
198 const uint64_t time_taken = end_tsc - start_tsc;
199
200 cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD;
201 return 0;
202 }
203
204 static int
timed_lookups(struct efd_perf_params * params)205 timed_lookups(struct efd_perf_params *params)
206 {
207 unsigned int i, j, a;
208 const uint64_t start_tsc = rte_rdtsc();
209 efd_value_t ret_data;
210
211 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
212 for (j = 0; j < KEYS_TO_ADD; j++) {
213 ret_data = rte_efd_lookup(params->efd_table,
214 test_socket_id, keys[j]);
215 if (ret_data != data[j]) {
216 printf("Value mismatch using rte_efd_lookup: "
217 "key #%d (0x", i);
218 for (a = 0; a < params->key_size; a++)
219 printf("%02x", keys[i][a]);
220 printf(")\n");
221 printf(" Expected %d, got %d\n", data[i],
222 ret_data);
223
224 return -1;
225 }
226
227 }
228 }
229
230 const uint64_t end_tsc = rte_rdtsc();
231 const uint64_t time_taken = end_tsc - start_tsc;
232
233 cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS;
234
235 return 0;
236 }
237
238 static int
timed_lookups_multi(struct efd_perf_params * params)239 timed_lookups_multi(struct efd_perf_params *params)
240 {
241 unsigned int i, j, k, a;
242 efd_value_t result[RTE_EFD_BURST_MAX] = {0};
243 const void *keys_burst[RTE_EFD_BURST_MAX];
244 const uint64_t start_tsc = rte_rdtsc();
245
246 for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
247 for (j = 0; j < KEYS_TO_ADD / RTE_EFD_BURST_MAX; j++) {
248 for (k = 0; k < RTE_EFD_BURST_MAX; k++)
249 keys_burst[k] = keys[j * RTE_EFD_BURST_MAX + k];
250
251 rte_efd_lookup_bulk(params->efd_table, test_socket_id,
252 RTE_EFD_BURST_MAX,
253 keys_burst, result);
254
255 for (k = 0; k < RTE_EFD_BURST_MAX; k++) {
256 uint32_t data_idx = j * RTE_EFD_BURST_MAX + k;
257 if (result[k] != data[data_idx]) {
258 printf("Value mismatch using "
259 "rte_efd_lookup_bulk: key #%d "
260 "(0x", i);
261 for (a = 0; a < params->key_size; a++)
262 printf("%02x",
263 keys[data_idx][a]);
264 printf(")\n");
265 printf(" Expected %d, got %d\n",
266 data[data_idx], result[k]);
267
268 return -1;
269 }
270 }
271 }
272 }
273
274 const uint64_t end_tsc = rte_rdtsc();
275 const uint64_t time_taken = end_tsc - start_tsc;
276
277 cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS;
278
279 return 0;
280 }
281
282 static int
timed_deletes(struct efd_perf_params * params)283 timed_deletes(struct efd_perf_params *params)
284 {
285 unsigned int i, a;
286 const uint64_t start_tsc = rte_rdtsc();
287 int32_t ret;
288
289 for (i = 0; i < KEYS_TO_ADD; i++) {
290 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i],
291 NULL);
292
293 if (ret != 0) {
294 printf("Error %d in rte_efd_delete - key=0x", ret);
295 for (a = 0; a < params->key_size; a++)
296 printf("%02x", keys[i][a]);
297 printf("\n");
298
299 return -1;
300 }
301 }
302
303 const uint64_t end_tsc = rte_rdtsc();
304 const uint64_t time_taken = end_tsc - start_tsc;
305
306 cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD;
307
308 return 0;
309 }
310
311 static void
perform_frees(struct efd_perf_params * params)312 perform_frees(struct efd_perf_params *params)
313 {
314 if (params->efd_table != NULL) {
315 rte_efd_free(params->efd_table);
316 params->efd_table = NULL;
317 }
318 }
319
320 static int
exit_with_fail(const char * testname,struct efd_perf_params * params,unsigned int i)321 exit_with_fail(const char *testname, struct efd_perf_params *params,
322 unsigned int i)
323 {
324
325 printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n",
326 testname, hashtest_key_lens[params->cycle], i);
327 perform_frees(params);
328 return -1;
329 }
330
331 static int
run_all_tbl_perf_tests(void)332 run_all_tbl_perf_tests(void)
333 {
334 unsigned int i, j;
335 struct efd_perf_params params;
336
337 printf("Measuring performance, please wait\n");
338 fflush(stdout);
339
340 test_socket_id = rte_socket_id();
341
342 for (i = 0; i < NUM_KEYSIZES; i++) {
343
344 if (setup_keys_and_data(¶ms, i) < 0) {
345 printf("Could not create keys/data/table\n");
346 return -1;
347 }
348
349 if (timed_adds(¶ms) < 0)
350 return exit_with_fail("timed_adds", ¶ms, i);
351
352 for (j = 0; j < NUM_SHUFFLES; j++)
353 shuffle_input_keys(¶ms);
354
355 if (timed_lookups(¶ms) < 0)
356 return exit_with_fail("timed_lookups", ¶ms, i);
357
358 if (timed_lookups_multi(¶ms) < 0)
359 return exit_with_fail("timed_lookups_multi", ¶ms, i);
360
361 if (timed_deletes(¶ms) < 0)
362 return exit_with_fail("timed_deletes", ¶ms, i);
363
364 /* Print a dot to show progress on operations */
365 printf(".");
366 fflush(stdout);
367
368 perform_frees(¶ms);
369 }
370
371 printf("\nResults (in CPU cycles/operation)\n");
372 printf("-----------------------------------\n");
373 printf("\n%-18s%-18s%-18s%-18s%-18s\n",
374 "Keysize", "Add", "Lookup", "Lookup_bulk", "Delete");
375 for (i = 0; i < NUM_KEYSIZES; i++) {
376 printf("%-18d", hashtest_key_lens[i]);
377 for (j = 0; j < NUM_OPERATIONS; j++)
378 printf("%-18"PRIu64, cycles[i][j]);
379 printf("\n");
380 }
381 return 0;
382 }
383
384 static int
test_efd_perf(void)385 test_efd_perf(void)
386 {
387
388 if (run_all_tbl_perf_tests() < 0)
389 return -1;
390
391 return 0;
392 }
393
394 #endif /* !RTE_EXEC_ENV_WINDOWS */
395
396 REGISTER_PERF_TEST(efd_perf_autotest, test_efd_perf);
397