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