test: move unit tests to separate directory
[dpdk.git] / test / test / test_efd_perf.c
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
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 */
53
54 #if RTE_EFD_VALUE_NUM_BITS == 32
55 #define VALUE_BITMASK 0xffffffff
56 #else
57 #define VALUE_BITMASK ((1 << RTE_EFD_VALUE_NUM_BITS) - 1)
58 #endif
59 static unsigned int test_socket_id;
60
61 static inline uint8_t efd_get_all_sockets_bitmask(void)
62 {
63         uint8_t all_cpu_sockets_bitmask = 0;
64         unsigned int i;
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);
70         }
71
72         return all_cpu_sockets_bitmask;
73 }
74
75 enum operations {
76         ADD = 0,
77         LOOKUP,
78         LOOKUP_MULTI,
79         DELETE,
80         NUM_OPERATIONS
81 };
82
83 struct efd_perf_params {
84         struct rte_efd_table *efd_table;
85         uint32_t key_size;
86         unsigned int cycle;
87 };
88
89 static uint32_t hashtest_key_lens[] = {
90         /* standard key sizes */
91         4, 8, 16, 32, 48, 64,
92         /* IPv4 SRC + DST + protocol, unpadded */
93         9,
94         /* IPv4 5-tuple, unpadded */
95         13,
96         /* IPv6 5-tuple, unpadded */
97         37,
98         /* IPv6 5-tuple, padded to 8-byte boundary */
99         40
100 };
101
102 /* Array to store number of cycles per operation */
103 uint64_t cycles[NUM_KEYSIZES][NUM_OPERATIONS];
104
105 /* Array to store the data */
106 efd_value_t data[KEYS_TO_ADD];
107
108 /* Array to store all input keys */
109 uint8_t keys[KEYS_TO_ADD][MAX_KEYSIZE];
110
111 /* Shuffle the keys that have been added, so lookups will be totally random */
112 static void
113 shuffle_input_keys(struct efd_perf_params *params)
114 {
115         efd_value_t temp_data;
116         unsigned int i;
117         uint32_t swap_idx;
118         uint8_t temp_key[MAX_KEYSIZE];
119
120         for (i = KEYS_TO_ADD - 1; i > 0; i--) {
121                 swap_idx = rte_rand() % i;
122
123                 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]);
124                 temp_data = data[i];
125
126                 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]);
127                 data[i] = data[swap_idx];
128
129                 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]);
130                 data[swap_idx] = temp_data;
131         }
132 }
133
134 static int key_compare(const void *key1, const void *key2)
135 {
136         return memcmp(key1, key2, MAX_KEYSIZE);
137 }
138
139 /*
140  * TODO: we could "error proof" these as done in test_hash_perf.c ln 165:
141  *
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.
145  */
146 static int
147 setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle)
148 {
149         unsigned int i, j;
150         int num_duplicates;
151
152         params->key_size = hashtest_key_lens[cycle];
153         params->cycle = cycle;
154
155         /* Reset all arrays */
156         for (i = 0; i < params->key_size; i++)
157                 keys[0][i] = 0;
158
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;
163
164                 data[i] = rte_rand() & VALUE_BITMASK;
165         }
166
167         /* Remove duplicates from the keys array */
168         do {
169                 num_duplicates = 0;
170
171                 /* Sort the list of keys to make it easier to find duplicates */
172                 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare);
173
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 */
179                                 num_duplicates++;
180                                 for (j = 0; j < params->key_size; j++)
181                                         keys[i][j] = rte_rand() & 0xFF;
182                         }
183                 }
184         } while (num_duplicates != 0);
185
186         /* Shuffle the random values again */
187         shuffle_input_keys(params);
188
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");
193
194         return 0;
195 }
196
197 static int
198 timed_adds(struct efd_perf_params *params)
199 {
200         const uint64_t start_tsc = rte_rdtsc();
201         unsigned int i, a;
202         int32_t ret;
203
204         for (i = 0; i < KEYS_TO_ADD; i++) {
205                 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i],
206                                 data[i]);
207                 if (ret != 0) {
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]);
212
213                         return -1;
214                 }
215         }
216
217         const uint64_t end_tsc = rte_rdtsc();
218         const uint64_t time_taken = end_tsc - start_tsc;
219
220         cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD;
221         return 0;
222 }
223
224 static int
225 timed_lookups(struct efd_perf_params *params)
226 {
227         unsigned int i, j, a;
228         const uint64_t start_tsc = rte_rdtsc();
229         efd_value_t ret_data;
230
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: "
237                                                 "key #%d (0x", i);
238                                 for (a = 0; a < params->key_size; a++)
239                                         printf("%02x", keys[i][a]);
240                                 printf(")\n");
241                                 printf("  Expected %d, got %d\n", data[i],
242                                                 ret_data);
243
244                                 return -1;
245                         }
246
247                 }
248         }
249
250         const uint64_t end_tsc = rte_rdtsc();
251         const uint64_t time_taken = end_tsc - start_tsc;
252
253         cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS;
254
255         return 0;
256 }
257
258 static int
259 timed_lookups_multi(struct efd_perf_params *params)
260 {
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();
265
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];
270
271                         rte_efd_lookup_bulk(params->efd_table, test_socket_id,
272                                         RTE_EFD_BURST_MAX,
273                                         keys_burst, result);
274
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 "
280                                                 "(0x", i);
281                                         for (a = 0; a < params->key_size; a++)
282                                                 printf("%02x",
283                                                         keys[data_idx][a]);
284                                         printf(")\n");
285                                         printf("  Expected %d, got %d\n",
286                                                 data[data_idx], result[k]);
287
288                                         return -1;
289                                 }
290                         }
291                 }
292         }
293
294         const uint64_t end_tsc = rte_rdtsc();
295         const uint64_t time_taken = end_tsc - start_tsc;
296
297         cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS;
298
299         return 0;
300 }
301
302 static int
303 timed_deletes(struct efd_perf_params *params)
304 {
305         unsigned int i, a;
306         const uint64_t start_tsc = rte_rdtsc();
307         int32_t ret;
308
309         for (i = 0; i < KEYS_TO_ADD; i++) {
310                 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i],
311                                 NULL);
312
313                 if (ret != 0) {
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]);
317                         printf("\n");
318
319                         return -1;
320                 }
321         }
322
323         const uint64_t end_tsc = rte_rdtsc();
324         const uint64_t time_taken = end_tsc - start_tsc;
325
326         cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD;
327
328         return 0;
329 }
330
331 static void
332 perform_frees(struct efd_perf_params *params)
333 {
334         if (params->efd_table != NULL) {
335                 rte_efd_free(params->efd_table);
336                 params->efd_table = NULL;
337         }
338 }
339
340 static int
341 exit_with_fail(const char *testname, struct efd_perf_params *params,
342                 unsigned int i)
343 {
344
345         printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n",
346                         testname, hashtest_key_lens[params->cycle], i);
347         perform_frees(params);
348         return -1;
349 }
350
351 static int
352 run_all_tbl_perf_tests(void)
353 {
354         unsigned int i, j;
355         struct efd_perf_params params;
356
357         printf("Measuring performance, please wait\n");
358         fflush(stdout);
359
360         test_socket_id = rte_socket_id();
361
362         for (i = 0; i < NUM_KEYSIZES; i++) {
363
364                 if (setup_keys_and_data(&params, i) < 0) {
365                         printf("Could not create keys/data/table\n");
366                         return -1;
367                 }
368
369                 if (timed_adds(&params) < 0)
370                         return exit_with_fail("timed_adds", &params, i);
371
372                 for (j = 0; j < NUM_SHUFFLES; j++)
373                         shuffle_input_keys(&params);
374
375                 if (timed_lookups(&params) < 0)
376                         return exit_with_fail("timed_lookups", &params, i);
377
378                 if (timed_lookups_multi(&params) < 0)
379                         return exit_with_fail("timed_lookups_multi", &params, i);
380
381                 if (timed_deletes(&params) < 0)
382                         return exit_with_fail("timed_deletes", &params, i);
383
384                 /* Print a dot to show progress on operations */
385                 printf(".");
386                 fflush(stdout);
387
388                 perform_frees(&params);
389         }
390
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]);
399                 printf("\n");
400         }
401         return 0;
402 }
403
404 static int
405 test_efd_perf(void)
406 {
407
408         if (run_all_tbl_perf_tests() < 0)
409                 return -1;
410
411         return 0;
412 }
413
414 REGISTER_TEST_COMMAND(efd_perf_autotest, test_efd_perf);