LCOV - code coverage report
Current view: top level - app/test-crypto-perf - cperf_test_throughput.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 109 0.0 %
Date: 2024-12-01 18:57:19 Functions: 0 5 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 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 <stdlib.h>
       6                 :            : 
       7                 :            : #include <rte_malloc.h>
       8                 :            : #include <rte_cycles.h>
       9                 :            : #include <rte_crypto.h>
      10                 :            : #include <rte_cryptodev.h>
      11                 :            : 
      12                 :            : #include "cperf_test_throughput.h"
      13                 :            : #include "cperf_ops.h"
      14                 :            : #include "cperf_test_common.h"
      15                 :            : 
      16                 :            : struct cperf_throughput_ctx {
      17                 :            :         uint8_t dev_id;
      18                 :            :         uint16_t qp_id;
      19                 :            :         uint8_t lcore_id;
      20                 :            : 
      21                 :            :         struct rte_mempool *pool;
      22                 :            : 
      23                 :            :         void *sess;
      24                 :            :         uint8_t sess_owner;
      25                 :            : 
      26                 :            :         cperf_populate_ops_t populate_ops;
      27                 :            : 
      28                 :            :         uint32_t src_buf_offset;
      29                 :            :         uint32_t dst_buf_offset;
      30                 :            : 
      31                 :            :         const struct cperf_options *options;
      32                 :            :         const struct cperf_test_vector *test_vector;
      33                 :            : };
      34                 :            : 
      35                 :            : static void
      36                 :          0 : cperf_throughput_test_free(struct cperf_throughput_ctx *ctx)
      37                 :            : {
      38                 :          0 :         if (!ctx)
      39                 :            :                 return;
      40                 :          0 :         if (ctx->sess != NULL && ctx->sess_owner) {
      41                 :          0 :                 if (cperf_is_asym_test(ctx->options))
      42                 :          0 :                         rte_cryptodev_asym_session_free(ctx->dev_id,
      43                 :            :                                         (void *)ctx->sess);
      44                 :            : #ifdef RTE_LIB_SECURITY
      45                 :          0 :                 else if (ctx->options->op_type == CPERF_PDCP ||
      46                 :          0 :                          ctx->options->op_type == CPERF_DOCSIS ||
      47                 :          0 :                          ctx->options->op_type == CPERF_TLS ||
      48                 :          0 :                          ctx->options->op_type == CPERF_IPSEC) {
      49                 :          0 :                         void *sec_ctx = rte_cryptodev_get_sec_ctx(ctx->dev_id);
      50                 :            : 
      51                 :          0 :                         rte_security_session_destroy(sec_ctx, (void *)ctx->sess);
      52                 :            :                 }
      53                 :            : #endif
      54                 :            :                 else
      55                 :          0 :                         rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
      56                 :            :         }
      57                 :          0 :         rte_mempool_free(ctx->pool);
      58                 :            : 
      59                 :          0 :         rte_free(ctx);
      60                 :            : }
      61                 :            : 
      62                 :            : void *
      63                 :          0 : cperf_throughput_test_constructor(struct rte_mempool *sess_mp,
      64                 :            :                 uint8_t dev_id, uint16_t qp_id,
      65                 :            :                 const struct cperf_options *options,
      66                 :            :                 const struct cperf_test_vector *test_vector,
      67                 :            :                 const struct cperf_op_fns *op_fns,
      68                 :            :                 void **sess)
      69                 :            : {
      70                 :            :         struct cperf_throughput_ctx *ctx = NULL;
      71                 :            : 
      72                 :          0 :         ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0);
      73                 :          0 :         if (ctx == NULL)
      74                 :          0 :                 goto err;
      75                 :            : 
      76                 :          0 :         ctx->dev_id = dev_id;
      77                 :          0 :         ctx->qp_id = qp_id;
      78                 :            : 
      79                 :          0 :         ctx->populate_ops = op_fns->populate_ops;
      80                 :          0 :         ctx->options = options;
      81                 :          0 :         ctx->test_vector = test_vector;
      82                 :            : 
      83                 :            :         /* IV goes at the end of the crypto operation */
      84                 :            :         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
      85                 :            :                 sizeof(struct rte_crypto_sym_op);
      86                 :            : 
      87                 :          0 :         if (*sess != NULL) {
      88                 :          0 :                 ctx->sess = *sess;
      89                 :          0 :                 ctx->sess_owner = false;
      90                 :            :         } else {
      91                 :          0 :                 ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
      92                 :            :                         iv_offset);
      93                 :          0 :                 if (ctx->sess == NULL)
      94                 :          0 :                         goto err;
      95                 :          0 :                 *sess = ctx->sess;
      96                 :          0 :                 ctx->sess_owner = true;
      97                 :            :         }
      98                 :            : 
      99                 :          0 :         if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
     100                 :            :                         &ctx->src_buf_offset, &ctx->dst_buf_offset,
     101                 :            :                         &ctx->pool) < 0)
     102                 :          0 :                 goto err;
     103                 :            : 
     104                 :            :         return ctx;
     105                 :          0 : err:
     106                 :          0 :         cperf_throughput_test_free(ctx);
     107                 :            : 
     108                 :          0 :         return NULL;
     109                 :            : }
     110                 :            : 
     111                 :            : static void
     112                 :          0 : cperf_verify_init_ops(struct rte_mempool *mp __rte_unused,
     113                 :            :                       void *opaque_arg,
     114                 :            :                       void *obj,
     115                 :            :                       __rte_unused unsigned int i)
     116                 :            : {
     117                 :            :         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
     118                 :            :                 sizeof(struct rte_crypto_sym_op);
     119                 :          0 :         uint32_t imix_idx = 0;
     120                 :            :         struct cperf_throughput_ctx *ctx = opaque_arg;
     121                 :          0 :         struct rte_crypto_op *op = obj;
     122                 :            : 
     123                 :          0 :         (ctx->populate_ops)(&op, ctx->src_buf_offset,
     124                 :            :                         ctx->dst_buf_offset,
     125                 :            :                         1, ctx->sess, ctx->options,
     126                 :            :                         ctx->test_vector, iv_offset, &imix_idx, NULL);
     127                 :            : 
     128                 :          0 :         cperf_mbuf_set(op->sym->m_src, ctx->options, ctx->test_vector);
     129                 :          0 : }
     130                 :            : 
     131                 :            : int
     132                 :          0 : cperf_throughput_test_runner(void *test_ctx)
     133                 :          0 : {
     134                 :            :         struct cperf_throughput_ctx *ctx = test_ctx;
     135                 :            :         uint16_t test_burst_size;
     136                 :            :         uint8_t burst_size_idx = 0;
     137                 :          0 :         uint32_t imix_idx = 0;
     138                 :            : 
     139                 :            :         static RTE_ATOMIC(uint16_t) display_once;
     140                 :            : 
     141                 :          0 :         struct rte_crypto_op *ops[ctx->options->max_burst_size];
     142                 :          0 :         struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
     143                 :            :         uint64_t i;
     144                 :            : 
     145                 :            :         uint32_t lcore = rte_lcore_id();
     146                 :            : 
     147                 :            : #ifdef CPERF_LINEARIZATION_ENABLE
     148                 :            :         struct rte_cryptodev_info dev_info;
     149                 :            :         int linearize = 0;
     150                 :            : 
     151                 :            :         /* Check if source mbufs require coalescing */
     152                 :            :         if ((ctx->options->op_type != CPERF_ASYM_MODEX) &&
     153                 :            :             (ctx->options->segment_sz < ctx->options->max_buffer_size)) {
     154                 :            :                 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
     155                 :            :                 if ((dev_info.feature_flags &
     156                 :            :                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
     157                 :            :                         linearize = 1;
     158                 :            :         }
     159                 :            : #endif /* CPERF_LINEARIZATION_ENABLE */
     160                 :            : 
     161                 :          0 :         ctx->lcore_id = lcore;
     162                 :            : 
     163                 :            :         /* Warm up the host CPU before starting the test */
     164                 :          0 :         for (i = 0; i < ctx->options->total_ops; i++)
     165                 :          0 :                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
     166                 :            : 
     167                 :            :         /* Get first size from range or list */
     168                 :          0 :         if (ctx->options->inc_burst_size != 0)
     169                 :          0 :                 test_burst_size = ctx->options->min_burst_size;
     170                 :            :         else
     171                 :          0 :                 test_burst_size = ctx->options->burst_size_list[0];
     172                 :            : 
     173                 :            :         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
     174                 :            :                 sizeof(struct rte_crypto_sym_op);
     175                 :            : 
     176                 :          0 :         if (ctx->options->out_of_place)
     177                 :          0 :                 rte_mempool_obj_iter(ctx->pool, cperf_verify_init_ops, (void *)ctx);
     178                 :            : 
     179                 :          0 :         while (test_burst_size <= ctx->options->max_burst_size) {
     180                 :            :                 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
     181                 :            :                 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
     182                 :            : 
     183                 :            :                 uint64_t tsc_start, tsc_end, tsc_duration;
     184                 :            : 
     185                 :            :                 uint16_t ops_unused = 0;
     186                 :            : 
     187                 :          0 :                 tsc_start = rte_rdtsc_precise();
     188                 :            : 
     189                 :          0 :                 while (ops_enqd_total < ctx->options->total_ops) {
     190                 :            : 
     191                 :          0 :                         uint16_t burst_size = ((ops_enqd_total + test_burst_size)
     192                 :            :                                         <= ctx->options->total_ops) ?
     193                 :            :                                                         test_burst_size :
     194                 :          0 :                                                         ctx->options->total_ops -
     195                 :            :                                                         ops_enqd_total;
     196                 :            : 
     197                 :          0 :                         uint16_t ops_needed = burst_size - ops_unused;
     198                 :            : 
     199                 :            :                         /* Allocate objects containing crypto operations and mbufs */
     200                 :          0 :                         if (rte_mempool_get_bulk(ctx->pool, (void **)ops,
     201                 :            :                                                 ops_needed) != 0) {
     202                 :          0 :                                 RTE_LOG(ERR, USER1,
     203                 :            :                                         "Failed to allocate more crypto operations "
     204                 :            :                                         "from the crypto operation pool.\n"
     205                 :            :                                         "Consider increasing the pool size "
     206                 :            :                                         "with --pool-sz\n");
     207                 :          0 :                                 return -1;
     208                 :            :                         }
     209                 :            : 
     210                 :            :                         /* Setup crypto op, attach mbuf etc */
     211                 :          0 :                         if (!ctx->options->out_of_place)
     212                 :          0 :                                 (ctx->populate_ops)(ops, ctx->src_buf_offset,
     213                 :            :                                                 ctx->dst_buf_offset,
     214                 :            :                                                 ops_needed, ctx->sess,
     215                 :            :                                                 ctx->options, ctx->test_vector,
     216                 :            :                                                 iv_offset, &imix_idx, &tsc_start);
     217                 :            : 
     218                 :            :                         /**
     219                 :            :                          * When ops_needed is smaller than ops_enqd, the
     220                 :            :                          * unused ops need to be moved to the front for
     221                 :            :                          * next round use.
     222                 :            :                          */
     223                 :          0 :                         if (unlikely(ops_enqd > ops_needed)) {
     224                 :          0 :                                 size_t nb_b_to_mov = ops_unused * sizeof(
     225                 :            :                                                 struct rte_crypto_op *);
     226                 :            : 
     227                 :          0 :                                 memmove(&ops[ops_needed], &ops[ops_enqd],
     228                 :            :                                         nb_b_to_mov);
     229                 :            :                         }
     230                 :            : 
     231                 :            : #ifdef CPERF_LINEARIZATION_ENABLE
     232                 :            :                         if (linearize) {
     233                 :            :                                 /* PMD doesn't support scatter-gather and source buffer
     234                 :            :                                  * is segmented.
     235                 :            :                                  * We need to linearize it before enqueuing.
     236                 :            :                                  */
     237                 :            :                                 for (i = 0; i < burst_size; i++)
     238                 :            :                                         rte_pktmbuf_linearize(
     239                 :            :                                                 ops[i]->sym->m_src);
     240                 :            :                         }
     241                 :            : #endif /* CPERF_LINEARIZATION_ENABLE */
     242                 :            : 
     243                 :            :                         /* Enqueue burst of ops on crypto device */
     244                 :          0 :                         ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
     245                 :            :                                         ops, burst_size);
     246                 :          0 :                         if (ops_enqd < burst_size)
     247                 :          0 :                                 ops_enqd_failed++;
     248                 :            : 
     249                 :            :                         /**
     250                 :            :                          * Calculate number of ops not enqueued (mainly for hw
     251                 :            :                          * accelerators whose ingress queue can fill up).
     252                 :            :                          */
     253                 :          0 :                         ops_unused = burst_size - ops_enqd;
     254                 :          0 :                         ops_enqd_total += ops_enqd;
     255                 :            : 
     256                 :            : 
     257                 :            :                         /* Dequeue processed burst of ops from crypto device */
     258                 :          0 :                         ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
     259                 :            :                                         ops_processed, test_burst_size);
     260                 :            : 
     261                 :          0 :                         if (likely(ops_deqd))  {
     262                 :            :                                 /* Free crypto ops so they can be reused. */
     263                 :          0 :                                 rte_mempool_put_bulk(ctx->pool,
     264                 :            :                                                 (void **)ops_processed, ops_deqd);
     265                 :            : 
     266                 :          0 :                                 ops_deqd_total += ops_deqd;
     267                 :            :                         } else {
     268                 :            :                                 /**
     269                 :            :                                  * Count dequeue polls which didn't return any
     270                 :            :                                  * processed operations. This statistic is mainly
     271                 :            :                                  * relevant to hw accelerators.
     272                 :            :                                  */
     273                 :          0 :                                 ops_deqd_failed++;
     274                 :            :                         }
     275                 :            : 
     276                 :            :                 }
     277                 :            : 
     278                 :            :                 /* Dequeue any operations still in the crypto device */
     279                 :            : 
     280                 :          0 :                 while (ops_deqd_total < ctx->options->total_ops) {
     281                 :            :                         /* Sending 0 length burst to flush sw crypto device */
     282                 :          0 :                         rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
     283                 :            : 
     284                 :            :                         /* dequeue burst */
     285                 :          0 :                         ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
     286                 :            :                                         ops_processed, test_burst_size);
     287                 :          0 :                         if (ops_deqd == 0)
     288                 :          0 :                                 ops_deqd_failed++;
     289                 :            :                         else {
     290                 :          0 :                                 rte_mempool_put_bulk(ctx->pool,
     291                 :            :                                                 (void **)ops_processed, ops_deqd);
     292                 :          0 :                                 ops_deqd_total += ops_deqd;
     293                 :            :                         }
     294                 :            :                 }
     295                 :            : 
     296                 :            :                 tsc_end = rte_rdtsc_precise();
     297                 :          0 :                 tsc_duration = (tsc_end - tsc_start);
     298                 :            : 
     299                 :            :                 /* Calculate average operations processed per second */
     300                 :          0 :                 double ops_per_second = ((double)ctx->options->total_ops /
     301                 :          0 :                                 tsc_duration) * rte_get_tsc_hz();
     302                 :            : 
     303                 :            :                 /* Calculate average throughput (Gbps) in bits per second */
     304                 :          0 :                 double throughput_gbps = ((ops_per_second *
     305                 :          0 :                                 ctx->options->test_buffer_size * 8) / 1000000000);
     306                 :            : 
     307                 :            :                 /* Calculate average cycles per packet */
     308                 :          0 :                 double cycles_per_packet = ((double)tsc_duration /
     309                 :          0 :                                 ctx->options->total_ops);
     310                 :            : 
     311                 :            :                 uint16_t exp = 0;
     312                 :          0 :                 if (!ctx->options->csv) {
     313                 :          0 :                         if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
     314                 :            :                                         rte_memory_order_relaxed, rte_memory_order_relaxed))
     315                 :            :                                 printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
     316                 :            :                                         "lcore id", "Buf Size", "Burst Size",
     317                 :            :                                         "Enqueued", "Dequeued", "Failed Enq",
     318                 :            :                                         "Failed Deq", "MOps", "Gbps",
     319                 :            :                                         "Cycles/Buf");
     320                 :            : 
     321                 :          0 :                         printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
     322                 :            :                                         "%12"PRIu64"%12.4f%12.4f%12.2f\n",
     323                 :          0 :                                         ctx->lcore_id,
     324                 :          0 :                                         ctx->options->test_buffer_size,
     325                 :            :                                         test_burst_size,
     326                 :            :                                         ops_enqd_total,
     327                 :            :                                         ops_deqd_total,
     328                 :            :                                         ops_enqd_failed,
     329                 :            :                                         ops_deqd_failed,
     330                 :            :                                         ops_per_second/1000000,
     331                 :            :                                         throughput_gbps,
     332                 :            :                                         cycles_per_packet);
     333                 :            :                 } else {
     334                 :          0 :                         if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
     335                 :            :                                         rte_memory_order_relaxed, rte_memory_order_relaxed))
     336                 :            :                                 printf("#lcore id,Buffer Size(B),"
     337                 :            :                                         "Burst Size,Enqueued,Dequeued,Failed Enq,"
     338                 :            :                                         "Failed Deq,Ops(Millions),Throughput(Gbps),"
     339                 :            :                                         "Cycles/Buf\n\n");
     340                 :            : 
     341                 :          0 :                         printf("%u,%u,%u,%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
     342                 :            :                                         "%.3f,%.3f,%.3f\n",
     343                 :          0 :                                         ctx->lcore_id,
     344                 :          0 :                                         ctx->options->test_buffer_size,
     345                 :            :                                         test_burst_size,
     346                 :            :                                         ops_enqd_total,
     347                 :            :                                         ops_deqd_total,
     348                 :            :                                         ops_enqd_failed,
     349                 :            :                                         ops_deqd_failed,
     350                 :            :                                         ops_per_second/1000000,
     351                 :            :                                         throughput_gbps,
     352                 :            :                                         cycles_per_packet);
     353                 :            :                 }
     354                 :            : 
     355                 :            :                 /* Get next size from range or list */
     356                 :          0 :                 if (ctx->options->inc_burst_size != 0)
     357                 :          0 :                         test_burst_size += ctx->options->inc_burst_size;
     358                 :            :                 else {
     359                 :          0 :                         if (++burst_size_idx == ctx->options->burst_size_count)
     360                 :            :                                 break;
     361                 :          0 :                         test_burst_size = ctx->options->burst_size_list[burst_size_idx];
     362                 :            :                 }
     363                 :            : 
     364                 :            :         }
     365                 :            : 
     366                 :            :         return 0;
     367                 :            : }
     368                 :            : 
     369                 :            : 
     370                 :            : void
     371                 :          0 : cperf_throughput_test_destructor(void *arg)
     372                 :            : {
     373                 :            :         struct cperf_throughput_ctx *ctx = arg;
     374                 :            : 
     375                 :          0 :         if (ctx == NULL)
     376                 :            :                 return;
     377                 :            : 
     378                 :          0 :         cperf_throughput_test_free(ctx);
     379                 :            : }

Generated by: LCOV version 1.14