LCOV - code coverage report
Current view: top level - app/test - test_efd_perf.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 123 0.8 %
Date: 2025-02-01 18:54:23 Functions: 1 12 8.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 76 0.0 %

           Branch data     Line data    Source code
       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
       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                 :            : 
      42                 :          0 : static inline uint64_t efd_get_all_sockets_bitmask(void)
      43                 :            : {
      44                 :            :         uint64_t all_cpu_sockets_bitmask = 0;
      45                 :            :         unsigned int i;
      46                 :          0 :         unsigned int next_lcore = rte_get_main_lcore();
      47                 :            :         const int val_true = 1, val_false = 0;
      48         [ #  # ]:          0 :         for (i = 0; i < rte_lcore_count(); i++) {
      49                 :          0 :                 all_cpu_sockets_bitmask |= 1 << rte_lcore_to_socket_id(next_lcore);
      50                 :          0 :                 next_lcore = rte_get_next_lcore(next_lcore, val_false, val_true);
      51                 :            :         }
      52                 :            : 
      53                 :          0 :         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
      94                 :          0 : 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         [ #  # ]:          0 :         for (i = KEYS_TO_ADD - 1; i > 0; i--) {
     102                 :          0 :                 swap_idx = rte_rand() % i;
     103                 :            : 
     104                 :          0 :                 memcpy(temp_key, keys[i], hashtest_key_lens[params->cycle]);
     105                 :          0 :                 temp_data = data[i];
     106                 :            : 
     107                 :          0 :                 memcpy(keys[i], keys[swap_idx], hashtest_key_lens[params->cycle]);
     108                 :          0 :                 data[i] = data[swap_idx];
     109                 :            : 
     110                 :          0 :                 memcpy(keys[swap_idx], temp_key, hashtest_key_lens[params->cycle]);
     111                 :          0 :                 data[swap_idx] = temp_data;
     112                 :            :         }
     113                 :          0 : }
     114                 :            : 
     115                 :          0 : static int key_compare(const void *key1, const void *key2)
     116                 :            : {
     117                 :          0 :         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
     128                 :          0 : setup_keys_and_data(struct efd_perf_params *params, unsigned int cycle)
     129                 :            : {
     130                 :            :         unsigned int i, j;
     131                 :            :         int num_duplicates;
     132                 :            : 
     133                 :          0 :         params->key_size = hashtest_key_lens[cycle];
     134                 :          0 :         params->cycle = cycle;
     135                 :            : 
     136                 :            :         /* Reset all arrays */
     137         [ #  # ]:          0 :         for (i = 0; i < params->key_size; i++)
     138                 :          0 :                 keys[0][i] = 0;
     139                 :            : 
     140                 :            :         /* Generate a list of keys, some of which may be duplicates */
     141         [ #  # ]:          0 :         for (i = 0; i < KEYS_TO_ADD; i++) {
     142         [ #  # ]:          0 :                 for (j = 0; j < params->key_size; j++)
     143                 :          0 :                         keys[i][j] = rte_rand() & 0xFF;
     144                 :            : 
     145                 :          0 :                 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                 :          0 :                 qsort(keys, KEYS_TO_ADD, MAX_KEYSIZE, key_compare);
     154                 :            : 
     155                 :            :                 /* Sift through the list of keys and look for duplicates */
     156         [ #  # ]:          0 :                 for (i = 0; i < KEYS_TO_ADD - 1; i++) {
     157         [ #  # ]:          0 :                         if (memcmp(keys[i], keys[i + 1], params->key_size) == 0) {
     158                 :            :                                 /* This key already exists, try again */
     159                 :          0 :                                 num_duplicates++;
     160         [ #  # ]:          0 :                                 for (j = 0; j < params->key_size; j++)
     161                 :          0 :                                         keys[i][j] = rte_rand() & 0xFF;
     162                 :            :                         }
     163                 :            :                 }
     164         [ #  # ]:          0 :         } while (num_duplicates != 0);
     165                 :            : 
     166                 :            :         /* Shuffle the random values again */
     167                 :          0 :         shuffle_input_keys(params);
     168                 :            : 
     169                 :          0 :         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         [ #  # ]:          0 :         TEST_ASSERT_NOT_NULL(params->efd_table, "Error creating the efd table\n");
     173                 :            : 
     174                 :            :         return 0;
     175                 :            : }
     176                 :            : 
     177                 :            : static int
     178                 :          0 : 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         [ #  # ]:          0 :         for (i = 0; i < KEYS_TO_ADD; i++) {
     185                 :          0 :                 ret = rte_efd_update(params->efd_table, test_socket_id, keys[i],
     186                 :          0 :                                 data[i]);
     187         [ #  # ]:          0 :                 if (ret != 0) {
     188                 :            :                         printf("Error %d in rte_efd_update - key=0x", ret);
     189         [ #  # ]:          0 :                         for (a = 0; a < params->key_size; a++)
     190                 :          0 :                                 printf("%02x", keys[i][a]);
     191                 :          0 :                         printf(" value=%d\n", data[i]);
     192                 :            : 
     193                 :          0 :                         return -1;
     194                 :            :                 }
     195                 :            :         }
     196                 :            : 
     197                 :            :         const uint64_t end_tsc = rte_rdtsc();
     198                 :          0 :         const uint64_t time_taken = end_tsc - start_tsc;
     199                 :            : 
     200                 :          0 :         cycles[params->cycle][ADD] = time_taken / KEYS_TO_ADD;
     201                 :          0 :         return 0;
     202                 :            : }
     203                 :            : 
     204                 :            : static int
     205                 :          0 : 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         [ #  # ]:          0 :         for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
     212         [ #  # ]:          0 :                 for (j = 0; j < KEYS_TO_ADD; j++) {
     213                 :          0 :                         ret_data = rte_efd_lookup(params->efd_table,
     214                 :          0 :                                         test_socket_id, keys[j]);
     215         [ #  # ]:          0 :                         if (ret_data != data[j]) {
     216                 :            :                                 printf("Value mismatch using rte_efd_lookup: "
     217                 :            :                                                 "key #%d (0x", i);
     218         [ #  # ]:          0 :                                 for (a = 0; a < params->key_size; a++)
     219                 :          0 :                                         printf("%02x", keys[i][a]);
     220                 :            :                                 printf(")\n");
     221                 :          0 :                                 printf("  Expected %d, got %d\n", data[i],
     222                 :            :                                                 ret_data);
     223                 :            : 
     224                 :          0 :                                 return -1;
     225                 :            :                         }
     226                 :            : 
     227                 :            :                 }
     228                 :            :         }
     229                 :            : 
     230                 :            :         const uint64_t end_tsc = rte_rdtsc();
     231                 :          0 :         const uint64_t time_taken = end_tsc - start_tsc;
     232                 :            : 
     233                 :          0 :         cycles[params->cycle][LOOKUP] = time_taken / NUM_LOOKUPS;
     234                 :            : 
     235                 :          0 :         return 0;
     236                 :            : }
     237                 :            : 
     238                 :            : static int
     239                 :          0 : timed_lookups_multi(struct efd_perf_params *params)
     240                 :            : {
     241                 :            :         unsigned int i, j, k, a;
     242                 :          0 :         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         [ #  # ]:          0 :         for (i = 0; i < NUM_LOOKUPS / KEYS_TO_ADD; i++) {
     247         [ #  # ]:          0 :                 for (j = 0; j < KEYS_TO_ADD / RTE_EFD_BURST_MAX; j++) {
     248         [ #  # ]:          0 :                         for (k = 0; k < RTE_EFD_BURST_MAX; k++)
     249                 :          0 :                                 keys_burst[k] = keys[j * RTE_EFD_BURST_MAX + k];
     250                 :            : 
     251                 :          0 :                         rte_efd_lookup_bulk(params->efd_table, test_socket_id,
     252                 :            :                                         RTE_EFD_BURST_MAX,
     253                 :            :                                         keys_burst, result);
     254                 :            : 
     255         [ #  # ]:          0 :                         for (k = 0; k < RTE_EFD_BURST_MAX; k++) {
     256                 :          0 :                                 uint32_t data_idx = j * RTE_EFD_BURST_MAX + k;
     257         [ #  # ]:          0 :                                 if (result[k] != data[data_idx]) {
     258                 :            :                                         printf("Value mismatch using "
     259                 :            :                                                 "rte_efd_lookup_bulk: key #%d "
     260                 :            :                                                 "(0x", i);
     261         [ #  # ]:          0 :                                         for (a = 0; a < params->key_size; a++)
     262                 :          0 :                                                 printf("%02x",
     263                 :          0 :                                                         keys[data_idx][a]);
     264                 :            :                                         printf(")\n");
     265                 :          0 :                                         printf("  Expected %d, got %d\n",
     266                 :          0 :                                                 data[data_idx], result[k]);
     267                 :            : 
     268                 :          0 :                                         return -1;
     269                 :            :                                 }
     270                 :            :                         }
     271                 :            :                 }
     272                 :            :         }
     273                 :            : 
     274                 :            :         const uint64_t end_tsc = rte_rdtsc();
     275                 :          0 :         const uint64_t time_taken = end_tsc - start_tsc;
     276                 :            : 
     277                 :          0 :         cycles[params->cycle][LOOKUP_MULTI] = time_taken / NUM_LOOKUPS;
     278                 :            : 
     279                 :          0 :         return 0;
     280                 :            : }
     281                 :            : 
     282                 :            : static int
     283                 :          0 : 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         [ #  # ]:          0 :         for (i = 0; i < KEYS_TO_ADD; i++) {
     290                 :          0 :                 ret = rte_efd_delete(params->efd_table, test_socket_id, keys[i],
     291                 :            :                                 NULL);
     292                 :            : 
     293         [ #  # ]:          0 :                 if (ret != 0) {
     294                 :            :                         printf("Error %d in rte_efd_delete - key=0x", ret);
     295         [ #  # ]:          0 :                         for (a = 0; a < params->key_size; a++)
     296                 :          0 :                                 printf("%02x", keys[i][a]);
     297                 :            :                         printf("\n");
     298                 :            : 
     299                 :          0 :                         return -1;
     300                 :            :                 }
     301                 :            :         }
     302                 :            : 
     303                 :            :         const uint64_t end_tsc = rte_rdtsc();
     304                 :          0 :         const uint64_t time_taken = end_tsc - start_tsc;
     305                 :            : 
     306                 :          0 :         cycles[params->cycle][DELETE] = time_taken / KEYS_TO_ADD;
     307                 :            : 
     308                 :          0 :         return 0;
     309                 :            : }
     310                 :            : 
     311                 :            : static void
     312                 :            : perform_frees(struct efd_perf_params *params)
     313                 :            : {
     314   [ #  #  #  # ]:          0 :         if (params->efd_table != NULL) {
     315                 :          0 :                 rte_efd_free(params->efd_table);
     316                 :          0 :                 params->efd_table = NULL;
     317                 :            :         }
     318                 :            : }
     319                 :            : 
     320                 :            : static int
     321                 :          0 : exit_with_fail(const char *testname, struct efd_perf_params *params,
     322                 :            :                 unsigned int i)
     323                 :            : {
     324                 :            : 
     325                 :          0 :         printf("<<<<<Test %s failed at keysize %d iteration %d >>>>>\n",
     326                 :          0 :                         testname, hashtest_key_lens[params->cycle], i);
     327                 :            :         perform_frees(params);
     328                 :          0 :         return -1;
     329                 :            : }
     330                 :            : 
     331                 :            : static int
     332                 :          0 : 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                 :          0 :         fflush(stdout);
     339                 :            : 
     340                 :          0 :         test_socket_id = rte_socket_id();
     341                 :            : 
     342         [ #  # ]:          0 :         for (i = 0; i < NUM_KEYSIZES; i++) {
     343                 :            : 
     344         [ #  # ]:          0 :                 if (setup_keys_and_data(&params, i) < 0) {
     345                 :            :                         printf("Could not create keys/data/table\n");
     346                 :          0 :                         return -1;
     347                 :            :                 }
     348                 :            : 
     349         [ #  # ]:          0 :                 if (timed_adds(&params) < 0)
     350                 :          0 :                         return exit_with_fail("timed_adds", &params, i);
     351                 :            : 
     352         [ #  # ]:          0 :                 for (j = 0; j < NUM_SHUFFLES; j++)
     353                 :          0 :                         shuffle_input_keys(&params);
     354                 :            : 
     355         [ #  # ]:          0 :                 if (timed_lookups(&params) < 0)
     356                 :          0 :                         return exit_with_fail("timed_lookups", &params, i);
     357                 :            : 
     358         [ #  # ]:          0 :                 if (timed_lookups_multi(&params) < 0)
     359                 :          0 :                         return exit_with_fail("timed_lookups_multi", &params, i);
     360                 :            : 
     361         [ #  # ]:          0 :                 if (timed_deletes(&params) < 0)
     362                 :          0 :                         return exit_with_fail("timed_deletes", &params, i);
     363                 :            : 
     364                 :            :                 /* Print a dot to show progress on operations */
     365                 :            :                 printf(".");
     366                 :          0 :                 fflush(stdout);
     367                 :            : 
     368                 :            :                 perform_frees(&params);
     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         [ #  # ]:          0 :         for (i = 0; i < NUM_KEYSIZES; i++) {
     376                 :          0 :                 printf("%-18d", hashtest_key_lens[i]);
     377         [ #  # ]:          0 :                 for (j = 0; j < NUM_OPERATIONS; j++)
     378                 :          0 :                         printf("%-18"PRIu64, cycles[i][j]);
     379                 :            :                 printf("\n");
     380                 :            :         }
     381                 :            :         return 0;
     382                 :            : }
     383                 :            : 
     384                 :            : static int
     385                 :          0 : test_efd_perf(void)
     386                 :            : {
     387                 :            : 
     388         [ #  # ]:          0 :         if (run_all_tbl_perf_tests() < 0)
     389                 :          0 :                 return -1;
     390                 :            : 
     391                 :            :         return 0;
     392                 :            : }
     393                 :            : 
     394                 :            : #endif /* !RTE_EXEC_ENV_WINDOWS */
     395                 :            : 
     396                 :        252 : REGISTER_PERF_TEST(efd_perf_autotest, test_efd_perf);

Generated by: LCOV version 1.14