4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_crypto.h>
36 #include <rte_cryptodev.h>
37 #include <rte_cycles.h>
38 #include <rte_malloc.h>
40 #include "cperf_ops.h"
41 #include "cperf_test_pmd_cyclecount.h"
42 #include "cperf_test_common.h"
44 #define PRETTY_HDR_FMT "%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n"
45 #define PRETTY_LINE_FMT "%12u%12u%12u%12u%12u%12u%12u%12.0f%12.0f%12.0f\n"
46 #define CSV_HDR_FMT "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"
47 #define CSV_LINE_FMT "%10u;%10u;%u;%u;%u;%u;%u;%.f3;%.f3;%.f3\n"
49 struct cperf_pmd_cyclecount_ctx {
54 struct rte_mempool *pool;
55 struct rte_crypto_op **ops;
56 struct rte_crypto_op **ops_processed;
58 struct rte_cryptodev_sym_session *sess;
60 cperf_populate_ops_t populate_ops;
62 uint32_t src_buf_offset;
63 uint32_t dst_buf_offset;
65 const struct cperf_options *options;
66 const struct cperf_test_vector *test_vector;
69 struct pmd_cyclecount_state {
70 struct cperf_pmd_cyclecount_ctx *ctx;
71 const struct cperf_options *opts;
77 uint32_t ops_enq_retries;
78 uint32_t ops_deq_retries;
79 double cycles_per_build;
80 double cycles_per_enq;
81 double cycles_per_deq;
84 static const uint16_t iv_offset =
85 sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op);
88 cperf_pmd_cyclecount_test_free(struct cperf_pmd_cyclecount_ctx *ctx)
92 rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
93 rte_cryptodev_sym_session_free(ctx->sess);
97 rte_mempool_free(ctx->pool);
102 if (ctx->ops_processed)
103 rte_free(ctx->ops_processed);
110 cperf_pmd_cyclecount_test_constructor(struct rte_mempool *sess_mp,
111 uint8_t dev_id, uint16_t qp_id,
112 const struct cperf_options *options,
113 const struct cperf_test_vector *test_vector,
114 const struct cperf_op_fns *op_fns)
116 struct cperf_pmd_cyclecount_ctx *ctx = NULL;
118 /* preallocate buffers for crypto ops as they can get quite big */
119 size_t alloc_sz = sizeof(struct rte_crypto_op *) *
120 options->nb_descriptors;
122 ctx = rte_malloc(NULL, sizeof(struct cperf_pmd_cyclecount_ctx), 0);
126 ctx->dev_id = dev_id;
129 ctx->populate_ops = op_fns->populate_ops;
130 ctx->options = options;
131 ctx->test_vector = test_vector;
133 /* IV goes at the end of the crypto operation */
134 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
135 sizeof(struct rte_crypto_sym_op);
137 ctx->sess = op_fns->sess_create(
138 sess_mp, dev_id, options, test_vector, iv_offset);
139 if (ctx->sess == NULL)
142 if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
143 &ctx->src_buf_offset, &ctx->dst_buf_offset,
147 ctx->ops = rte_malloc("ops", alloc_sz, 0);
151 ctx->ops_processed = rte_malloc("ops_processed", alloc_sz, 0);
152 if (!ctx->ops_processed)
158 cperf_pmd_cyclecount_test_free(ctx);
163 /* benchmark alloc-build-free of ops */
165 pmd_cyclecount_bench_ops(struct pmd_cyclecount_state *state, uint32_t cur_op,
166 uint16_t test_burst_size)
168 uint32_t iter_ops_left = state->opts->total_ops - cur_op;
169 uint32_t iter_ops_needed =
170 RTE_MIN(state->opts->nb_descriptors, iter_ops_left);
171 uint32_t cur_iter_op;
173 for (cur_iter_op = 0; cur_iter_op < iter_ops_needed;
174 cur_iter_op += test_burst_size) {
175 uint32_t burst_size = RTE_MIN(state->opts->total_ops - cur_op,
177 struct rte_crypto_op **ops = &state->ctx->ops[cur_iter_op];
179 /* Allocate objects containing crypto operations and mbufs */
180 if (rte_mempool_get_bulk(state->ctx->pool, (void **)ops,
183 "Failed to allocate more crypto operations "
184 "from the the crypto operation pool.\n"
185 "Consider increasing the pool size "
190 /* Setup crypto op, attach mbuf etc */
191 (state->ctx->populate_ops)(ops,
192 state->ctx->src_buf_offset,
193 state->ctx->dst_buf_offset,
195 state->ctx->sess, state->opts,
196 state->ctx->test_vector, iv_offset);
198 #ifdef CPERF_LINEARIZATION_ENABLE
199 /* Check if source mbufs require coalescing */
200 if (state->linearize) {
202 for (i = 0; i < burst_size; i++) {
203 struct rte_mbuf *src = ops[i]->sym->m_src;
204 rte_pktmbuf_linearize(src);
207 #endif /* CPERF_LINEARIZATION_ENABLE */
208 rte_mempool_put_bulk(state->ctx->pool, (void **)ops,
215 /* allocate and build ops (no free) */
217 pmd_cyclecount_build_ops(struct pmd_cyclecount_state *state,
218 uint32_t iter_ops_needed, uint16_t test_burst_size)
220 uint32_t cur_iter_op;
222 for (cur_iter_op = 0; cur_iter_op < iter_ops_needed;
223 cur_iter_op += test_burst_size) {
224 uint32_t burst_size = RTE_MIN(
225 iter_ops_needed - cur_iter_op, test_burst_size);
226 struct rte_crypto_op **ops = &state->ctx->ops[cur_iter_op];
228 /* Allocate objects containing crypto operations and mbufs */
229 if (rte_mempool_get_bulk(state->ctx->pool, (void **)ops,
232 "Failed to allocate more crypto operations "
233 "from the the crypto operation pool.\n"
234 "Consider increasing the pool size "
239 /* Setup crypto op, attach mbuf etc */
240 (state->ctx->populate_ops)(ops,
241 state->ctx->src_buf_offset,
242 state->ctx->dst_buf_offset,
244 state->ctx->sess, state->opts,
245 state->ctx->test_vector, iv_offset);
250 /* benchmark enqueue, returns number of ops enqueued */
252 pmd_cyclecount_bench_enq(struct pmd_cyclecount_state *state,
253 uint32_t iter_ops_needed, uint16_t test_burst_size)
255 /* Enqueue full descriptor ring of ops on crypto device */
256 uint32_t cur_iter_op = 0;
257 while (cur_iter_op < iter_ops_needed) {
258 uint32_t burst_size = RTE_MIN(iter_ops_needed - cur_iter_op,
260 struct rte_crypto_op **ops = &state->ctx->ops[cur_iter_op];
263 burst_enqd = rte_cryptodev_enqueue_burst(state->ctx->dev_id,
264 state->ctx->qp_id, ops, burst_size);
266 /* if we couldn't enqueue anything, the queue is full */
268 /* don't try to dequeue anything we didn't enqueue */
272 if (burst_enqd < burst_size)
273 state->ops_enq_retries++;
274 state->ops_enqd += burst_enqd;
275 cur_iter_op += burst_enqd;
277 return iter_ops_needed;
280 /* benchmark dequeue */
282 pmd_cyclecount_bench_deq(struct pmd_cyclecount_state *state,
283 uint32_t iter_ops_needed, uint16_t test_burst_size)
285 /* Dequeue full descriptor ring of ops on crypto device */
286 uint32_t cur_iter_op = 0;
287 while (cur_iter_op < iter_ops_needed) {
288 uint32_t burst_size = RTE_MIN(iter_ops_needed - cur_iter_op,
290 struct rte_crypto_op **ops_processed =
291 &state->ctx->ops[cur_iter_op];
294 burst_deqd = rte_cryptodev_dequeue_burst(state->ctx->dev_id,
295 state->ctx->qp_id, ops_processed, burst_size);
297 if (burst_deqd < burst_size)
298 state->ops_deq_retries++;
299 state->ops_deqd += burst_deqd;
300 cur_iter_op += burst_deqd;
304 /* run benchmark per burst size */
306 pmd_cyclecount_bench_burst_sz(
307 struct pmd_cyclecount_state *state, uint16_t test_burst_size)
316 /* reset all counters */
320 state->ops_enq_retries = 0;
322 state->ops_deq_retries = 0;
325 * Benchmark crypto op alloc-build-free separately.
327 tsc_start = rte_rdtsc_precise();
329 for (cur_op = 0; cur_op < state->opts->total_ops;
330 cur_op += state->opts->nb_descriptors) {
331 if (unlikely(pmd_cyclecount_bench_ops(
332 state, cur_op, test_burst_size)))
336 tsc_end = rte_rdtsc_precise();
337 tsc_op = tsc_end - tsc_start;
341 * Hardware acceleration cyclecount benchmarking loop.
343 * We're benchmarking raw enq/deq performance by filling up the device
344 * queue, so we never get any failed enqs unless the driver won't accept
345 * the exact number of descriptors we requested, or the driver won't
346 * wrap around the end of the TX ring. However, since we're only
347 * dequeueing once we've filled up the queue, we have to benchmark it
348 * piecemeal and then average out the results.
351 while (cur_op < state->opts->total_ops) {
352 uint32_t iter_ops_left = state->opts->total_ops - cur_op;
353 uint32_t iter_ops_needed = RTE_MIN(
354 state->opts->nb_descriptors, iter_ops_left);
355 uint32_t iter_ops_allocd = iter_ops_needed;
357 /* allocate and build ops */
358 if (unlikely(pmd_cyclecount_build_ops(state, iter_ops_needed,
362 tsc_start = rte_rdtsc_precise();
364 /* fill up TX ring */
365 iter_ops_needed = pmd_cyclecount_bench_enq(state,
366 iter_ops_needed, test_burst_size);
368 tsc_end = rte_rdtsc_precise();
370 tsc_enq += tsc_end - tsc_start;
372 /* allow for HW to catch up */
374 rte_delay_us_block(state->delay);
376 tsc_start = rte_rdtsc_precise();
379 pmd_cyclecount_bench_deq(state, iter_ops_needed,
382 tsc_end = rte_rdtsc_precise();
384 tsc_deq += tsc_end - tsc_start;
386 cur_op += iter_ops_needed;
389 * we may not have processed all ops that we allocated, so
390 * free everything we've allocated.
392 rte_mempool_put_bulk(state->ctx->pool,
393 (void **)state->ctx->ops, iter_ops_allocd);
396 state->cycles_per_build = (double)tsc_op / state->opts->total_ops;
397 state->cycles_per_enq = (double)tsc_enq / state->ops_enqd;
398 state->cycles_per_deq = (double)tsc_deq / state->ops_deqd;
404 cperf_pmd_cyclecount_test_runner(void *test_ctx)
406 struct pmd_cyclecount_state state = {0};
407 const struct cperf_options *opts;
408 uint16_t test_burst_size;
409 uint8_t burst_size_idx = 0;
411 state.ctx = test_ctx;
412 opts = state.ctx->options;
414 state.lcore = rte_lcore_id();
417 static int only_once;
418 static bool warmup = true;
421 * We need a small delay to allow for hardware to process all the crypto
422 * operations. We can't automatically figure out what the delay should
423 * be, so we leave it up to the user (by default it's 0).
425 state.delay = 1000 * opts->pmdcc_delay;
427 #ifdef CPERF_LINEARIZATION_ENABLE
428 struct rte_cryptodev_info dev_info;
430 /* Check if source mbufs require coalescing */
431 if (opts->segments_sz < ctx->options->max_buffer_size) {
432 rte_cryptodev_info_get(state.ctx->dev_id, &dev_info);
433 if ((dev_info.feature_flags &
434 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) ==
439 #endif /* CPERF_LINEARIZATION_ENABLE */
441 state.ctx->lcore_id = state.lcore;
443 /* Get first size from range or list */
444 if (opts->inc_burst_size != 0)
445 test_burst_size = opts->min_burst_size;
447 test_burst_size = opts->burst_size_list[0];
449 while (test_burst_size <= opts->max_burst_size) {
450 /* do a benchmark run */
451 if (pmd_cyclecount_bench_burst_sz(&state, test_burst_size))
455 * First run is always a warm up run.
464 printf(PRETTY_HDR_FMT, "lcore id", "Buf Size",
465 "Burst Size", "Enqueued",
466 "Dequeued", "Enq Retries",
467 "Deq Retries", "Cycles/Op",
468 "Cycles/Enq", "Cycles/Deq");
471 printf(PRETTY_LINE_FMT, state.ctx->lcore_id,
472 opts->test_buffer_size, test_burst_size,
473 state.ops_enqd, state.ops_deqd,
474 state.ops_enq_retries,
475 state.ops_deq_retries,
476 state.cycles_per_build,
477 state.cycles_per_enq,
478 state.cycles_per_deq);
481 printf(CSV_HDR_FMT, "# lcore id", "Buf Size",
482 "Burst Size", "Enqueued",
483 "Dequeued", "Enq Retries",
484 "Deq Retries", "Cycles/Op",
485 "Cycles/Enq", "Cycles/Deq");
488 printf(CSV_LINE_FMT, state.ctx->lcore_id,
489 opts->test_buffer_size, test_burst_size,
490 state.ops_enqd, state.ops_deqd,
491 state.ops_enq_retries,
492 state.ops_deq_retries,
493 state.cycles_per_build,
494 state.cycles_per_enq,
495 state.cycles_per_deq);
498 /* Get next size from range or list */
499 if (opts->inc_burst_size != 0)
500 test_burst_size += opts->inc_burst_size;
502 if (++burst_size_idx == opts->burst_size_count)
504 test_burst_size = opts->burst_size_list[burst_size_idx];
512 cperf_pmd_cyclecount_test_destructor(void *arg)
514 struct cperf_pmd_cyclecount_ctx *ctx = arg;
519 cperf_pmd_cyclecount_test_free(ctx);