Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2017 Intel Corporation
3 : : */
4 : :
5 : : #include <stdbool.h>
6 : : #include <stdlib.h>
7 : :
8 : : #include <rte_crypto.h>
9 : : #include <rte_cryptodev.h>
10 : : #include <rte_cycles.h>
11 : : #include <rte_malloc.h>
12 : :
13 : : #include "cperf_ops.h"
14 : : #include "cperf_test_pmd_cyclecount.h"
15 : : #include "cperf_test_common.h"
16 : :
17 : : #define PRETTY_HDR_FMT "%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n"
18 : : #define PRETTY_LINE_FMT "%12u%12u%12u%12u%12u%12u%12u%12.0f%12.0f%12.0f\n"
19 : : #define CSV_HDR_FMT "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"
20 : : #define CSV_LINE_FMT "%10u,%10u,%u,%u,%u,%u,%u,%.3f,%.3f,%.3f\n"
21 : :
22 : : struct cperf_pmd_cyclecount_ctx {
23 : : uint8_t dev_id;
24 : : uint16_t qp_id;
25 : : uint8_t lcore_id;
26 : :
27 : : struct rte_mempool *pool;
28 : : struct rte_crypto_op **ops;
29 : : struct rte_crypto_op **ops_processed;
30 : :
31 : : void *sess;
32 : : uint8_t sess_owner;
33 : :
34 : : cperf_populate_ops_t populate_ops;
35 : :
36 : : uint32_t src_buf_offset;
37 : : uint32_t dst_buf_offset;
38 : :
39 : : const struct cperf_options *options;
40 : : const struct cperf_test_vector *test_vector;
41 : : };
42 : :
43 : : struct pmd_cyclecount_state {
44 : : struct cperf_pmd_cyclecount_ctx *ctx;
45 : : const struct cperf_options *opts;
46 : : uint32_t lcore;
47 : : uint64_t delay;
48 : : int linearize;
49 : : uint32_t ops_enqd;
50 : : uint32_t ops_deqd;
51 : : uint32_t ops_enq_retries;
52 : : uint32_t ops_deq_retries;
53 : : double cycles_per_build;
54 : : double cycles_per_enq;
55 : : double cycles_per_deq;
56 : : };
57 : :
58 : : static const uint16_t iv_offset =
59 : : sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op);
60 : :
61 : : static void
62 : 0 : cperf_pmd_cyclecount_test_free(struct cperf_pmd_cyclecount_ctx *ctx)
63 : : {
64 : 0 : if (!ctx)
65 : : return;
66 : :
67 : 0 : if (ctx->sess != NULL && ctx->sess_owner) {
68 : : #ifdef RTE_LIB_SECURITY
69 : 0 : if (ctx->options->op_type == CPERF_PDCP ||
70 : : ctx->options->op_type == CPERF_DOCSIS) {
71 : 0 : void *sec_ctx = rte_cryptodev_get_sec_ctx(ctx->dev_id);
72 : :
73 : 0 : rte_security_session_destroy(sec_ctx, (void *)ctx->sess);
74 : : } else
75 : : #endif
76 : 0 : rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
77 : : }
78 : :
79 : 0 : rte_mempool_free(ctx->pool);
80 : :
81 : 0 : rte_free(ctx->ops);
82 : :
83 : 0 : rte_free(ctx->ops_processed);
84 : :
85 : 0 : rte_free(ctx);
86 : : }
87 : :
88 : : void *
89 : 0 : cperf_pmd_cyclecount_test_constructor(struct rte_mempool *sess_mp,
90 : : uint8_t dev_id, uint16_t qp_id,
91 : : const struct cperf_options *options,
92 : : const struct cperf_test_vector *test_vector,
93 : : const struct cperf_op_fns *op_fns,
94 : : void **sess)
95 : : {
96 : : struct cperf_pmd_cyclecount_ctx *ctx = NULL;
97 : :
98 : : /* preallocate buffers for crypto ops as they can get quite big */
99 : 0 : size_t alloc_sz = sizeof(struct rte_crypto_op *) *
100 : 0 : options->nb_descriptors;
101 : :
102 : 0 : ctx = rte_malloc(NULL, sizeof(struct cperf_pmd_cyclecount_ctx), 0);
103 : 0 : if (ctx == NULL)
104 : 0 : goto err;
105 : :
106 : 0 : ctx->dev_id = dev_id;
107 : 0 : ctx->qp_id = qp_id;
108 : :
109 : 0 : ctx->populate_ops = op_fns->populate_ops;
110 : 0 : ctx->options = options;
111 : 0 : ctx->test_vector = test_vector;
112 : :
113 : : /* IV goes at the end of the crypto operation */
114 : : uint16_t iv_ofs = sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op);
115 : :
116 : 0 : if (*sess != NULL) {
117 : 0 : ctx->sess = *sess;
118 : 0 : ctx->sess_owner = false;
119 : : } else {
120 : 0 : ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector, iv_ofs);
121 : 0 : if (ctx->sess == NULL)
122 : 0 : goto err;
123 : 0 : *sess = ctx->sess;
124 : 0 : ctx->sess_owner = true;
125 : : }
126 : :
127 : 0 : if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
128 : : &ctx->src_buf_offset, &ctx->dst_buf_offset,
129 : : &ctx->pool) < 0)
130 : 0 : goto err;
131 : :
132 : 0 : ctx->ops = rte_malloc("ops", alloc_sz, 0);
133 : 0 : if (!ctx->ops)
134 : 0 : goto err;
135 : :
136 : 0 : ctx->ops_processed = rte_malloc("ops_processed", alloc_sz, 0);
137 : 0 : if (!ctx->ops_processed)
138 : 0 : goto err;
139 : :
140 : : return ctx;
141 : :
142 : 0 : err:
143 : 0 : cperf_pmd_cyclecount_test_free(ctx);
144 : :
145 : 0 : return NULL;
146 : : }
147 : :
148 : : /* benchmark alloc-build-free of ops */
149 : : static inline int
150 : 0 : pmd_cyclecount_bench_ops(struct pmd_cyclecount_state *state, uint32_t cur_op,
151 : : uint16_t test_burst_size)
152 : : {
153 : 0 : uint32_t iter_ops_left = state->opts->total_ops - cur_op;
154 : : uint32_t iter_ops_needed =
155 : 0 : RTE_MIN(state->opts->nb_descriptors, iter_ops_left);
156 : : uint32_t cur_iter_op;
157 : 0 : uint32_t imix_idx = 0;
158 : :
159 : 0 : for (cur_iter_op = 0; cur_iter_op < iter_ops_needed;
160 : 0 : cur_iter_op += test_burst_size) {
161 : 0 : uint32_t burst_size = RTE_MIN(iter_ops_needed - cur_iter_op,
162 : : test_burst_size);
163 : 0 : struct rte_crypto_op **ops = &state->ctx->ops[cur_iter_op];
164 : :
165 : : /* Allocate objects containing crypto operations and mbufs */
166 : 0 : if (rte_mempool_get_bulk(state->ctx->pool, (void **)ops,
167 : : burst_size) != 0) {
168 : 0 : RTE_LOG(ERR, USER1,
169 : : "Failed to allocate more crypto operations "
170 : : "from the crypto operation pool.\n"
171 : : "Consider increasing the pool size "
172 : : "with --pool-sz\n");
173 : 0 : return -1;
174 : : }
175 : :
176 : : /* Setup crypto op, attach mbuf etc */
177 : 0 : (state->ctx->populate_ops)(ops,
178 : : state->ctx->src_buf_offset,
179 : : state->ctx->dst_buf_offset,
180 : : burst_size,
181 : : state->ctx->sess, state->opts,
182 : : state->ctx->test_vector, iv_offset,
183 : : &imix_idx, NULL);
184 : :
185 : : #ifdef CPERF_LINEARIZATION_ENABLE
186 : : /* Check if source mbufs require coalescing */
187 : : if (state->linearize) {
188 : : uint8_t i;
189 : : for (i = 0; i < burst_size; i++) {
190 : : struct rte_mbuf *src = ops[i]->sym->m_src;
191 : : rte_pktmbuf_linearize(src);
192 : : }
193 : : }
194 : : #endif /* CPERF_LINEARIZATION_ENABLE */
195 : 0 : rte_mempool_put_bulk(state->ctx->pool, (void **)ops,
196 : : burst_size);
197 : : }
198 : :
199 : : return 0;
200 : : }
201 : :
202 : : /* allocate and build ops (no free) */
203 : : static int
204 : 0 : pmd_cyclecount_build_ops(struct pmd_cyclecount_state *state,
205 : : uint32_t iter_ops_needed, uint16_t test_burst_size)
206 : : {
207 : : uint32_t cur_iter_op;
208 : 0 : uint32_t imix_idx = 0;
209 : :
210 : 0 : for (cur_iter_op = 0; cur_iter_op < iter_ops_needed;
211 : 0 : cur_iter_op += test_burst_size) {
212 : 0 : uint32_t burst_size = RTE_MIN(
213 : : iter_ops_needed - cur_iter_op, test_burst_size);
214 : 0 : struct rte_crypto_op **ops = &state->ctx->ops[cur_iter_op];
215 : :
216 : : /* Allocate objects containing crypto operations and mbufs */
217 : 0 : if (rte_mempool_get_bulk(state->ctx->pool, (void **)ops,
218 : : burst_size) != 0) {
219 : 0 : RTE_LOG(ERR, USER1,
220 : : "Failed to allocate more crypto operations "
221 : : "from the crypto operation pool.\n"
222 : : "Consider increasing the pool size "
223 : : "with --pool-sz\n");
224 : 0 : return -1;
225 : : }
226 : :
227 : : /* Setup crypto op, attach mbuf etc */
228 : 0 : (state->ctx->populate_ops)(ops,
229 : : state->ctx->src_buf_offset,
230 : : state->ctx->dst_buf_offset,
231 : : burst_size,
232 : : state->ctx->sess, state->opts,
233 : : state->ctx->test_vector, iv_offset,
234 : : &imix_idx, NULL);
235 : : }
236 : : return 0;
237 : : }
238 : :
239 : : /* benchmark enqueue, returns number of ops enqueued */
240 : : static uint32_t
241 : 0 : pmd_cyclecount_bench_enq(struct pmd_cyclecount_state *state,
242 : : uint32_t iter_ops_needed, uint16_t test_burst_size)
243 : : {
244 : : /* Enqueue full descriptor ring of ops on crypto device */
245 : : uint32_t cur_iter_op = 0;
246 : 0 : while (cur_iter_op < iter_ops_needed) {
247 : 0 : uint32_t burst_size = RTE_MIN(iter_ops_needed - cur_iter_op,
248 : : test_burst_size);
249 : 0 : struct rte_crypto_op **ops = &state->ctx->ops[cur_iter_op];
250 : : uint32_t burst_enqd;
251 : :
252 : 0 : burst_enqd = rte_cryptodev_enqueue_burst(state->ctx->dev_id,
253 : 0 : state->ctx->qp_id, ops, burst_size);
254 : :
255 : : /* if we couldn't enqueue anything, the queue is full */
256 : 0 : if (!burst_enqd) {
257 : : /* don't try to dequeue anything we didn't enqueue */
258 : 0 : return cur_iter_op;
259 : : }
260 : :
261 : 0 : if (burst_enqd < burst_size)
262 : 0 : state->ops_enq_retries++;
263 : 0 : state->ops_enqd += burst_enqd;
264 : 0 : cur_iter_op += burst_enqd;
265 : : }
266 : : return iter_ops_needed;
267 : : }
268 : :
269 : : /* benchmark dequeue */
270 : : static void
271 : 0 : pmd_cyclecount_bench_deq(struct pmd_cyclecount_state *state,
272 : : uint32_t iter_ops_needed, uint16_t test_burst_size)
273 : : {
274 : : /* Dequeue full descriptor ring of ops on crypto device */
275 : : uint32_t cur_iter_op = 0;
276 : 0 : while (cur_iter_op < iter_ops_needed) {
277 : 0 : uint32_t burst_size = RTE_MIN(iter_ops_needed - cur_iter_op,
278 : : test_burst_size);
279 : 0 : struct rte_crypto_op **ops_processed =
280 : 0 : &state->ctx->ops[cur_iter_op];
281 : : uint32_t burst_deqd;
282 : :
283 : 0 : burst_deqd = rte_cryptodev_dequeue_burst(state->ctx->dev_id,
284 : 0 : state->ctx->qp_id, ops_processed, burst_size);
285 : :
286 : 0 : if (burst_deqd < burst_size)
287 : 0 : state->ops_deq_retries++;
288 : 0 : state->ops_deqd += burst_deqd;
289 : 0 : cur_iter_op += burst_deqd;
290 : : }
291 : 0 : }
292 : :
293 : : /* run benchmark per burst size */
294 : : static inline int
295 : 0 : pmd_cyclecount_bench_burst_sz(
296 : : struct pmd_cyclecount_state *state, uint16_t test_burst_size)
297 : : {
298 : : uint64_t tsc_start;
299 : : uint64_t tsc_end;
300 : : uint64_t tsc_op;
301 : : uint64_t tsc_enq;
302 : : uint64_t tsc_deq;
303 : : uint32_t cur_op;
304 : :
305 : : /* reset all counters */
306 : : tsc_enq = 0;
307 : : tsc_deq = 0;
308 : 0 : state->ops_enqd = 0;
309 : 0 : state->ops_enq_retries = 0;
310 : 0 : state->ops_deqd = 0;
311 : 0 : state->ops_deq_retries = 0;
312 : :
313 : : /*
314 : : * Benchmark crypto op alloc-build-free separately.
315 : : */
316 : : tsc_start = rte_rdtsc_precise();
317 : :
318 : 0 : for (cur_op = 0; cur_op < state->opts->total_ops;
319 : 0 : cur_op += state->opts->nb_descriptors) {
320 : 0 : if (unlikely(pmd_cyclecount_bench_ops(
321 : : state, cur_op, test_burst_size)))
322 : : return -1;
323 : : }
324 : :
325 : : tsc_end = rte_rdtsc_precise();
326 : 0 : tsc_op = tsc_end - tsc_start;
327 : :
328 : :
329 : : /*
330 : : * Hardware acceleration cyclecount benchmarking loop.
331 : : *
332 : : * We're benchmarking raw enq/deq performance by filling up the device
333 : : * queue, so we never get any failed enqs unless the driver won't accept
334 : : * the exact number of descriptors we requested, or the driver won't
335 : : * wrap around the end of the TX ring. However, since we're only
336 : : * dequeuing once we've filled up the queue, we have to benchmark it
337 : : * piecemeal and then average out the results.
338 : : */
339 : : cur_op = 0;
340 : 0 : while (cur_op < state->opts->total_ops) {
341 : 0 : uint32_t iter_ops_left = state->opts->total_ops - cur_op;
342 : 0 : uint32_t iter_ops_needed = RTE_MIN(
343 : : state->opts->nb_descriptors, iter_ops_left);
344 : : uint32_t iter_ops_allocd = iter_ops_needed;
345 : :
346 : : /* allocate and build ops */
347 : 0 : if (unlikely(pmd_cyclecount_build_ops(state, iter_ops_needed,
348 : : test_burst_size)))
349 : : return -1;
350 : :
351 : : tsc_start = rte_rdtsc_precise();
352 : :
353 : : /* fill up TX ring */
354 : 0 : iter_ops_needed = pmd_cyclecount_bench_enq(state,
355 : : iter_ops_needed, test_burst_size);
356 : :
357 : : tsc_end = rte_rdtsc_precise();
358 : :
359 : 0 : tsc_enq += tsc_end - tsc_start;
360 : :
361 : : /* allow for HW to catch up */
362 : 0 : if (state->delay)
363 : 0 : rte_delay_us_block(state->delay);
364 : :
365 : : tsc_start = rte_rdtsc_precise();
366 : :
367 : : /* drain RX ring */
368 : 0 : pmd_cyclecount_bench_deq(state, iter_ops_needed,
369 : : test_burst_size);
370 : :
371 : : tsc_end = rte_rdtsc_precise();
372 : :
373 : 0 : tsc_deq += tsc_end - tsc_start;
374 : :
375 : 0 : cur_op += iter_ops_needed;
376 : :
377 : : /*
378 : : * we may not have processed all ops that we allocated, so
379 : : * free everything we've allocated.
380 : : */
381 : 0 : rte_mempool_put_bulk(state->ctx->pool,
382 : 0 : (void **)state->ctx->ops, iter_ops_allocd);
383 : : }
384 : :
385 : 0 : state->cycles_per_build = (double)tsc_op / state->opts->total_ops;
386 : 0 : state->cycles_per_enq = (double)tsc_enq / state->ops_enqd;
387 : 0 : state->cycles_per_deq = (double)tsc_deq / state->ops_deqd;
388 : :
389 : 0 : return 0;
390 : : }
391 : :
392 : : int
393 : 0 : cperf_pmd_cyclecount_test_runner(void *test_ctx)
394 : : {
395 : 0 : struct pmd_cyclecount_state state = {0};
396 : : const struct cperf_options *opts;
397 : : uint16_t test_burst_size;
398 : : uint8_t burst_size_idx = 0;
399 : :
400 : 0 : state.ctx = test_ctx;
401 : 0 : opts = state.ctx->options;
402 : 0 : state.opts = opts;
403 : 0 : state.lcore = rte_lcore_id();
404 : : state.linearize = 0;
405 : :
406 : : static RTE_ATOMIC(uint16_t) display_once;
407 : : static bool warmup = true;
408 : :
409 : : /*
410 : : * We need a small delay to allow for hardware to process all the crypto
411 : : * operations. We can't automatically figure out what the delay should
412 : : * be, so we leave it up to the user (by default it's 0).
413 : : */
414 : 0 : state.delay = 1000 * opts->pmdcc_delay;
415 : :
416 : : #ifdef CPERF_LINEARIZATION_ENABLE
417 : : struct rte_cryptodev_info dev_info;
418 : :
419 : : /* Check if source mbufs require coalescing */
420 : : if (opts->segments_sz < ctx->options->max_buffer_size) {
421 : : rte_cryptodev_info_get(state.ctx->dev_id, &dev_info);
422 : : if ((dev_info.feature_flags &
423 : : RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) ==
424 : : 0) {
425 : : state.linearize = 1;
426 : : }
427 : : }
428 : : #endif /* CPERF_LINEARIZATION_ENABLE */
429 : :
430 : 0 : state.ctx->lcore_id = state.lcore;
431 : :
432 : : /* Get first size from range or list */
433 : 0 : if (opts->inc_burst_size != 0)
434 : 0 : test_burst_size = opts->min_burst_size;
435 : : else
436 : 0 : test_burst_size = opts->burst_size_list[0];
437 : :
438 : 0 : while (test_burst_size <= opts->max_burst_size) {
439 : : /* do a benchmark run */
440 : 0 : if (pmd_cyclecount_bench_burst_sz(&state, test_burst_size))
441 : : return -1;
442 : :
443 : : /*
444 : : * First run is always a warm up run.
445 : : */
446 : 0 : if (warmup) {
447 : 0 : warmup = false;
448 : : continue;
449 : : }
450 : :
451 : : uint16_t exp = 0;
452 : 0 : if (!opts->csv) {
453 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
454 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
455 : : printf(PRETTY_HDR_FMT, "lcore id", "Buf Size",
456 : : "Burst Size", "Enqueued",
457 : : "Dequeued", "Enq Retries",
458 : : "Deq Retries", "Cycles/Op",
459 : : "Cycles/Enq", "Cycles/Deq");
460 : :
461 : 0 : printf(PRETTY_LINE_FMT, state.ctx->lcore_id,
462 : 0 : opts->test_buffer_size, test_burst_size,
463 : : state.ops_enqd, state.ops_deqd,
464 : : state.ops_enq_retries,
465 : : state.ops_deq_retries,
466 : : state.cycles_per_build,
467 : : state.cycles_per_enq,
468 : : state.cycles_per_deq);
469 : : } else {
470 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
471 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
472 : : printf(CSV_HDR_FMT, "# lcore id", "Buf Size",
473 : : "Burst Size", "Enqueued",
474 : : "Dequeued", "Enq Retries",
475 : : "Deq Retries", "Cycles/Op",
476 : : "Cycles/Enq", "Cycles/Deq");
477 : :
478 : 0 : printf(CSV_LINE_FMT, state.ctx->lcore_id,
479 : 0 : opts->test_buffer_size, test_burst_size,
480 : : state.ops_enqd, state.ops_deqd,
481 : : state.ops_enq_retries,
482 : : state.ops_deq_retries,
483 : : state.cycles_per_build,
484 : : state.cycles_per_enq,
485 : : state.cycles_per_deq);
486 : : }
487 : :
488 : : /* Get next size from range or list */
489 : 0 : if (opts->inc_burst_size != 0)
490 : 0 : test_burst_size += opts->inc_burst_size;
491 : : else {
492 : 0 : if (++burst_size_idx == opts->burst_size_count)
493 : : break;
494 : 0 : test_burst_size = opts->burst_size_list[burst_size_idx];
495 : : }
496 : : }
497 : :
498 : : return 0;
499 : : }
500 : :
501 : : void
502 : 0 : cperf_pmd_cyclecount_test_destructor(void *arg)
503 : : {
504 : : struct cperf_pmd_cyclecount_ctx *ctx = arg;
505 : :
506 : 0 : if (ctx == NULL)
507 : : return;
508 : :
509 : 0 : cperf_pmd_cyclecount_test_free(ctx);
510 : : }
|