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