Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2023 Intel Corporation
3 : : */
4 : :
5 : : #include <inttypes.h>
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <unistd.h>
9 : :
10 : : #include <rte_time.h>
11 : : #include <rte_mbuf.h>
12 : : #include <rte_dmadev.h>
13 : : #include <rte_malloc.h>
14 : : #include <rte_lcore.h>
15 : : #include <rte_random.h>
16 : :
17 : : #include "main.h"
18 : :
19 : : #define MAX_DMA_CPL_NB 255
20 : :
21 : : #define TEST_WAIT_U_SECOND 10000
22 : : #define POLL_MAX 1000
23 : :
24 : : #define CSV_LINE_DMA_FMT "Scenario %u,%u,%s,%u,%u,%u,%u,%.2lf,%" PRIu64 ",%.3lf,%.3lf\n"
25 : : #define CSV_LINE_CPU_FMT "Scenario %u,%u,NA,NA,NA,%u,%u,%.2lf,%" PRIu64 ",%.3lf,%.3lf\n"
26 : :
27 : : #define CSV_TOTAL_LINE_FMT "Scenario %u Summary, , , , , ,%u,%.2lf,%.1lf,%.3lf,%.3lf\n"
28 : :
29 : : struct worker_info {
30 : : bool ready_flag;
31 : : bool start_flag;
32 : : bool stop_flag;
33 : : uint32_t total_cpl;
34 : : uint32_t test_cpl;
35 : : };
36 : :
37 : : struct sge_info {
38 : : struct rte_dma_sge *srcs;
39 : : struct rte_dma_sge *dsts;
40 : : uint8_t nb_srcs;
41 : : uint8_t nb_dsts;
42 : : };
43 : :
44 : : struct lcore_params {
45 : : uint8_t scenario_id;
46 : : unsigned int lcore_id;
47 : : char *dma_name;
48 : : uint16_t worker_id;
49 : : uint16_t dev_id;
50 : : uint32_t nr_buf;
51 : : uint16_t kick_batch;
52 : : uint32_t buf_size;
53 : : uint16_t test_secs;
54 : : struct rte_mbuf **srcs;
55 : : struct rte_mbuf **dsts;
56 : : struct sge_info sge;
57 : : volatile struct worker_info worker_info;
58 : : };
59 : :
60 : : static struct rte_mempool *src_pool;
61 : : static struct rte_mempool *dst_pool;
62 : :
63 : : static struct lcore_params *lcores[MAX_WORKER_NB];
64 : :
65 : : #define PRINT_ERR(...) print_err(__func__, __LINE__, __VA_ARGS__)
66 : :
67 : : static inline int
68 : : __rte_format_printf(3, 4)
69 : 0 : print_err(const char *func, int lineno, const char *format, ...)
70 : : {
71 : : va_list ap;
72 : : int ret;
73 : :
74 : 0 : ret = fprintf(stderr, "In %s:%d - ", func, lineno);
75 : 0 : va_start(ap, format);
76 : 0 : ret += vfprintf(stderr, format, ap);
77 : 0 : va_end(ap);
78 : :
79 : 0 : return ret;
80 : : }
81 : :
82 : : static inline void
83 : 0 : calc_result(uint32_t buf_size, uint32_t nr_buf, uint16_t nb_workers, uint16_t test_secs,
84 : : uint32_t total_cnt, float *memory, uint32_t *ave_cycle,
85 : : float *bandwidth, float *mops)
86 : : {
87 : : float ops;
88 : :
89 : 0 : *memory = (float)(buf_size * (nr_buf / nb_workers) * 2) / (1024 * 1024);
90 : 0 : *ave_cycle = test_secs * rte_get_timer_hz() / total_cnt;
91 : 0 : ops = (float)total_cnt / test_secs;
92 : 0 : *mops = ops / (1000 * 1000);
93 : 0 : *bandwidth = (ops * buf_size * 8) / (1000 * 1000 * 1000);
94 : 0 : }
95 : :
96 : : static void
97 : 0 : output_result(struct test_configure *cfg, struct lcore_params *para,
98 : : uint16_t kick_batch, uint64_t ave_cycle, uint32_t buf_size,
99 : : uint32_t nr_buf, float memory, float bandwidth, float mops)
100 : : {
101 : 0 : uint16_t ring_size = cfg->ring_size.cur;
102 : 0 : uint8_t scenario_id = cfg->scenario_id;
103 : 0 : uint32_t lcore_id = para->lcore_id;
104 : 0 : char *dma_name = para->dma_name;
105 : :
106 : 0 : if (cfg->is_dma) {
107 : 0 : printf("lcore %u, DMA %s, DMA Ring Size: %u, Kick Batch Size: %u", lcore_id,
108 : : dma_name, ring_size, kick_batch);
109 : 0 : if (cfg->is_sg)
110 : 0 : printf(" DMA src sges: %u, dst sges: %u",
111 : 0 : para->sge.nb_srcs, para->sge.nb_dsts);
112 : : printf(".\n");
113 : : } else {
114 : : printf("lcore %u\n", lcore_id);
115 : : }
116 : :
117 : 0 : printf("Average Cycles/op: %" PRIu64 ", Buffer Size: %u B, Buffer Number: %u, Memory: %.2lf MB, Frequency: %.3lf Ghz.\n",
118 : : ave_cycle, buf_size, nr_buf, memory, rte_get_timer_hz()/1000000000.0);
119 : 0 : printf("Average Bandwidth: %.3lf Gbps, MOps: %.3lf\n", bandwidth, mops);
120 : :
121 : 0 : if (cfg->is_dma)
122 : 0 : snprintf(output_str[lcore_id], MAX_OUTPUT_STR_LEN, CSV_LINE_DMA_FMT,
123 : : scenario_id, lcore_id, dma_name, ring_size, kick_batch, buf_size,
124 : : nr_buf, memory, ave_cycle, bandwidth, mops);
125 : : else
126 : 0 : snprintf(output_str[lcore_id], MAX_OUTPUT_STR_LEN, CSV_LINE_CPU_FMT,
127 : : scenario_id, lcore_id, buf_size,
128 : : nr_buf, memory, ave_cycle, bandwidth, mops);
129 : 0 : }
130 : :
131 : : static inline void
132 : : cache_flush_buf(__rte_unused struct rte_mbuf **array,
133 : : __rte_unused uint32_t buf_size,
134 : : __rte_unused uint32_t nr_buf)
135 : : {
136 : : #ifdef RTE_ARCH_X86_64
137 : : char *data;
138 : : struct rte_mbuf **srcs = array;
139 : : uint32_t i, offset;
140 : :
141 : 0 : for (i = 0; i < nr_buf; i++) {
142 : 0 : data = rte_pktmbuf_mtod(srcs[i], char *);
143 : 0 : for (offset = 0; offset < buf_size; offset += 64)
144 : 0 : __builtin_ia32_clflush(data + offset);
145 : : }
146 : : #endif
147 : : }
148 : :
149 : : static int
150 : 0 : vchan_data_populate(uint32_t dev_id, struct rte_dma_vchan_conf *qconf,
151 : : struct test_configure *cfg, uint16_t dev_num)
152 : : {
153 : : struct vchan_dev_config *vchan_dconfig;
154 : : struct rte_dma_info info;
155 : :
156 : 0 : vchan_dconfig = &cfg->dma_config[dev_num].vchan_dev;
157 : 0 : qconf->direction = vchan_dconfig->tdir;
158 : :
159 : 0 : rte_dma_info_get(dev_id, &info);
160 : 0 : if (!(RTE_BIT64(qconf->direction) & info.dev_capa))
161 : : return -1;
162 : :
163 : 0 : qconf->nb_desc = cfg->ring_size.cur;
164 : :
165 : 0 : switch (qconf->direction) {
166 : 0 : case RTE_DMA_DIR_MEM_TO_DEV:
167 : 0 : qconf->dst_port.pcie.vfen = 1;
168 : 0 : qconf->dst_port.port_type = RTE_DMA_PORT_PCIE;
169 : 0 : qconf->dst_port.pcie.coreid = vchan_dconfig->port.pcie.coreid;
170 : 0 : qconf->dst_port.pcie.vfid = vchan_dconfig->port.pcie.vfid;
171 : 0 : qconf->dst_port.pcie.pfid = vchan_dconfig->port.pcie.pfid;
172 : 0 : break;
173 : 0 : case RTE_DMA_DIR_DEV_TO_MEM:
174 : 0 : qconf->src_port.pcie.vfen = 1;
175 : 0 : qconf->src_port.port_type = RTE_DMA_PORT_PCIE;
176 : 0 : qconf->src_port.pcie.coreid = vchan_dconfig->port.pcie.coreid;
177 : 0 : qconf->src_port.pcie.vfid = vchan_dconfig->port.pcie.vfid;
178 : 0 : qconf->src_port.pcie.pfid = vchan_dconfig->port.pcie.pfid;
179 : 0 : break;
180 : : case RTE_DMA_DIR_MEM_TO_MEM:
181 : : case RTE_DMA_DIR_DEV_TO_DEV:
182 : : break;
183 : : }
184 : :
185 : : return 0;
186 : : }
187 : :
188 : : /* Configuration of device. */
189 : : static void
190 : 0 : configure_dmadev_queue(uint32_t dev_id, struct test_configure *cfg, uint8_t sges_max,
191 : : uint16_t dev_num)
192 : : {
193 : : uint16_t vchan = 0;
194 : : struct rte_dma_info info;
195 : 0 : struct rte_dma_conf dev_config = { .nb_vchans = 1 };
196 : 0 : struct rte_dma_vchan_conf qconf = { 0 };
197 : :
198 : 0 : if (vchan_data_populate(dev_id, &qconf, cfg, dev_num) != 0)
199 : 0 : rte_exit(EXIT_FAILURE, "Error with vchan data populate.\n");
200 : :
201 : 0 : if (rte_dma_configure(dev_id, &dev_config) != 0)
202 : 0 : rte_exit(EXIT_FAILURE, "Error with dma configure.\n");
203 : :
204 : 0 : if (rte_dma_vchan_setup(dev_id, vchan, &qconf) != 0)
205 : 0 : rte_exit(EXIT_FAILURE, "Error with queue configuration.\n");
206 : :
207 : 0 : if (rte_dma_info_get(dev_id, &info) != 0)
208 : 0 : rte_exit(EXIT_FAILURE, "Error with getting device info.\n");
209 : :
210 : 0 : if (info.nb_vchans != 1)
211 : 0 : rte_exit(EXIT_FAILURE, "Error, no configured queues reported on device id. %u\n",
212 : : dev_id);
213 : :
214 : 0 : if (info.max_sges < sges_max)
215 : 0 : rte_exit(EXIT_FAILURE, "Error with unsupported max_sges on device id %u.\n",
216 : : dev_id);
217 : :
218 : 0 : if (rte_dma_start(dev_id) != 0)
219 : 0 : rte_exit(EXIT_FAILURE, "Error with dma start.\n");
220 : 0 : }
221 : :
222 : : static int
223 : 0 : config_dmadevs(struct test_configure *cfg)
224 : : {
225 : 0 : uint32_t nb_workers = cfg->num_worker;
226 : : struct lcore_dma_map_t *ldm;
227 : : uint32_t i;
228 : : int dev_id;
229 : : uint16_t nb_dmadevs = 0;
230 : : uint8_t nb_sges = 0;
231 : : char *dma_name;
232 : :
233 : 0 : if (cfg->is_sg)
234 : 0 : nb_sges = RTE_MAX(cfg->nb_src_sges, cfg->nb_dst_sges);
235 : :
236 : 0 : for (i = 0; i < nb_workers; i++) {
237 : : ldm = &cfg->dma_config[i].lcore_dma_map;
238 : 0 : dma_name = ldm->dma_names;
239 : 0 : dev_id = rte_dma_get_dev_id_by_name(dma_name);
240 : 0 : if (dev_id < 0) {
241 : 0 : fprintf(stderr, "Error: Fail to find DMA %s.\n", dma_name);
242 : 0 : goto end;
243 : : }
244 : :
245 : 0 : ldm->dma_id = dev_id;
246 : 0 : configure_dmadev_queue(dev_id, cfg, nb_sges, nb_dmadevs);
247 : 0 : ++nb_dmadevs;
248 : : }
249 : :
250 : 0 : end:
251 : 0 : if (nb_dmadevs < nb_workers) {
252 : 0 : printf("Not enough dmadevs (%u) for all workers (%u).\n", nb_dmadevs, nb_workers);
253 : 0 : return -1;
254 : : }
255 : :
256 : 0 : printf("Number of used dmadevs: %u.\n", nb_dmadevs);
257 : :
258 : 0 : return 0;
259 : : }
260 : :
261 : : static void
262 : 0 : error_exit(int dev_id)
263 : : {
264 : 0 : rte_dma_stop(dev_id);
265 : 0 : rte_dma_close(dev_id);
266 : 0 : rte_exit(EXIT_FAILURE, "DMA error\n");
267 : : }
268 : :
269 : : static inline void
270 : 0 : do_dma_submit_and_poll(uint16_t dev_id, uint64_t *async_cnt,
271 : : volatile struct worker_info *worker_info)
272 : : {
273 : : int ret;
274 : : uint16_t nr_cpl;
275 : :
276 : 0 : ret = rte_dma_submit(dev_id, 0);
277 : 0 : if (ret < 0)
278 : 0 : error_exit(dev_id);
279 : :
280 : : nr_cpl = rte_dma_completed(dev_id, 0, MAX_DMA_CPL_NB, NULL, NULL);
281 : 0 : *async_cnt -= nr_cpl;
282 : 0 : worker_info->total_cpl += nr_cpl;
283 : 0 : }
284 : :
285 : : static inline int
286 : 0 : do_dma_plain_mem_copy(void *p)
287 : : {
288 : : struct lcore_params *para = (struct lcore_params *)p;
289 : 0 : volatile struct worker_info *worker_info = &(para->worker_info);
290 : 0 : const uint16_t dev_id = para->dev_id;
291 : 0 : const uint32_t nr_buf = para->nr_buf;
292 : 0 : const uint16_t kick_batch = para->kick_batch;
293 : 0 : const uint32_t buf_size = para->buf_size;
294 : 0 : struct rte_mbuf **srcs = para->srcs;
295 : 0 : struct rte_mbuf **dsts = para->dsts;
296 : : uint16_t nr_cpl;
297 : 0 : uint64_t async_cnt = 0;
298 : : uint32_t i;
299 : : uint32_t poll_cnt = 0;
300 : : int ret;
301 : :
302 : 0 : worker_info->stop_flag = false;
303 : 0 : worker_info->ready_flag = true;
304 : :
305 : 0 : while (!worker_info->start_flag)
306 : : ;
307 : :
308 : : while (1) {
309 : 0 : for (i = 0; i < nr_buf; i++) {
310 : 0 : dma_copy:
311 : 0 : ret = rte_dma_copy(dev_id, 0, rte_mbuf_data_iova(srcs[i]),
312 : 0 : rte_mbuf_data_iova(dsts[i]), buf_size, 0);
313 : 0 : if (unlikely(ret < 0)) {
314 : 0 : if (ret == -ENOSPC) {
315 : 0 : do_dma_submit_and_poll(dev_id, &async_cnt, worker_info);
316 : 0 : goto dma_copy;
317 : : } else
318 : 0 : error_exit(dev_id);
319 : : }
320 : 0 : async_cnt++;
321 : :
322 : 0 : if ((async_cnt % kick_batch) == 0)
323 : 0 : do_dma_submit_and_poll(dev_id, &async_cnt, worker_info);
324 : : }
325 : :
326 : 0 : if (worker_info->stop_flag)
327 : : break;
328 : : }
329 : :
330 : 0 : rte_dma_submit(dev_id, 0);
331 : 0 : while ((async_cnt > 0) && (poll_cnt++ < POLL_MAX)) {
332 : : nr_cpl = rte_dma_completed(dev_id, 0, MAX_DMA_CPL_NB, NULL, NULL);
333 : 0 : async_cnt -= nr_cpl;
334 : : }
335 : :
336 : 0 : return 0;
337 : : }
338 : :
339 : : static inline int
340 : 0 : do_dma_sg_mem_copy(void *p)
341 : : {
342 : : struct lcore_params *para = (struct lcore_params *)p;
343 : 0 : volatile struct worker_info *worker_info = &(para->worker_info);
344 : 0 : struct rte_dma_sge *src_sges = para->sge.srcs;
345 : 0 : struct rte_dma_sge *dst_sges = para->sge.dsts;
346 : 0 : const uint8_t nb_src_sges = para->sge.nb_srcs;
347 : 0 : const uint8_t nb_dst_sges = para->sge.nb_dsts;
348 : 0 : const uint16_t kick_batch = para->kick_batch;
349 : 0 : const uint16_t dev_id = para->dev_id;
350 : 0 : uint32_t nr_buf = para->nr_buf;
351 : 0 : uint64_t async_cnt = 0;
352 : : uint32_t poll_cnt = 0;
353 : : uint16_t nr_cpl;
354 : : uint32_t i, j;
355 : : int ret;
356 : :
357 : 0 : nr_buf /= RTE_MAX(nb_src_sges, nb_dst_sges);
358 : 0 : worker_info->stop_flag = false;
359 : 0 : worker_info->ready_flag = true;
360 : :
361 : 0 : while (!worker_info->start_flag)
362 : : ;
363 : :
364 : : while (1) {
365 : : j = 0;
366 : 0 : for (i = 0; i < nr_buf; i++) {
367 : 0 : dma_copy:
368 : 0 : ret = rte_dma_copy_sg(dev_id, 0,
369 : 0 : &src_sges[i * nb_src_sges], &dst_sges[j * nb_dst_sges],
370 : : nb_src_sges, nb_dst_sges, 0);
371 : 0 : if (unlikely(ret < 0)) {
372 : 0 : if (ret == -ENOSPC) {
373 : 0 : do_dma_submit_and_poll(dev_id, &async_cnt, worker_info);
374 : 0 : goto dma_copy;
375 : : } else
376 : 0 : error_exit(dev_id);
377 : : }
378 : 0 : async_cnt++;
379 : 0 : j++;
380 : :
381 : 0 : if ((async_cnt % kick_batch) == 0)
382 : 0 : do_dma_submit_and_poll(dev_id, &async_cnt, worker_info);
383 : : }
384 : :
385 : 0 : if (worker_info->stop_flag)
386 : : break;
387 : : }
388 : :
389 : 0 : rte_dma_submit(dev_id, 0);
390 : 0 : while ((async_cnt > 0) && (poll_cnt++ < POLL_MAX)) {
391 : : nr_cpl = rte_dma_completed(dev_id, 0, MAX_DMA_CPL_NB, NULL, NULL);
392 : 0 : async_cnt -= nr_cpl;
393 : : }
394 : :
395 : 0 : return 0;
396 : : }
397 : :
398 : : static inline int
399 : 0 : do_cpu_mem_copy(void *p)
400 : : {
401 : : struct lcore_params *para = (struct lcore_params *)p;
402 : : volatile struct worker_info *worker_info = &(para->worker_info);
403 : 0 : const uint32_t nr_buf = para->nr_buf;
404 : 0 : const uint32_t buf_size = para->buf_size;
405 : 0 : struct rte_mbuf **srcs = para->srcs;
406 : 0 : struct rte_mbuf **dsts = para->dsts;
407 : : uint32_t i;
408 : :
409 : 0 : worker_info->stop_flag = false;
410 : 0 : worker_info->ready_flag = true;
411 : :
412 : 0 : while (!worker_info->start_flag)
413 : : ;
414 : :
415 : : while (1) {
416 : 0 : for (i = 0; i < nr_buf; i++) {
417 : 0 : const void *src = rte_pktmbuf_mtod(dsts[i], void *);
418 : 0 : void *dst = rte_pktmbuf_mtod(srcs[i], void *);
419 : :
420 : : /* copy buffer form src to dst */
421 : 0 : rte_memcpy(dst, src, (size_t)buf_size);
422 : 0 : worker_info->total_cpl++;
423 : : }
424 : 0 : if (worker_info->stop_flag)
425 : : break;
426 : : }
427 : :
428 : 0 : return 0;
429 : : }
430 : :
431 : : static void
432 : 0 : dummy_free_ext_buf(void *addr, void *opaque)
433 : : {
434 : : RTE_SET_USED(addr);
435 : : RTE_SET_USED(opaque);
436 : 0 : }
437 : :
438 : : static int
439 : 0 : setup_memory_env(struct test_configure *cfg,
440 : : struct rte_mbuf ***srcs, struct rte_mbuf ***dsts,
441 : : struct rte_dma_sge **src_sges, struct rte_dma_sge **dst_sges)
442 : : {
443 : 0 : unsigned int cur_buf_size = cfg->buf_size.cur;
444 : 0 : unsigned int buf_size = cur_buf_size + RTE_PKTMBUF_HEADROOM;
445 : : unsigned int nr_sockets;
446 : 0 : uint32_t nr_buf = cfg->nr_buf;
447 : : uint32_t i;
448 : : bool is_src_numa_incorrect, is_dst_numa_incorrect;
449 : :
450 : 0 : nr_sockets = rte_socket_count();
451 : 0 : is_src_numa_incorrect = (cfg->src_numa_node >= nr_sockets);
452 : 0 : is_dst_numa_incorrect = (cfg->dst_numa_node >= nr_sockets);
453 : :
454 : 0 : if (is_src_numa_incorrect || is_dst_numa_incorrect) {
455 : 0 : PRINT_ERR("Error: Incorrect NUMA config for %s.\n",
456 : : (is_src_numa_incorrect && is_dst_numa_incorrect) ? "source & destination" :
457 : : (is_src_numa_incorrect) ? "source" : "destination");
458 : 0 : return -1;
459 : : }
460 : :
461 : 0 : src_pool = rte_pktmbuf_pool_create("Benchmark_DMA_SRC",
462 : : nr_buf,
463 : : 0,
464 : : 0,
465 : : buf_size,
466 : : cfg->src_numa_node);
467 : 0 : if (src_pool == NULL) {
468 : 0 : PRINT_ERR("Error with source mempool creation.\n");
469 : 0 : return -1;
470 : : }
471 : :
472 : 0 : dst_pool = rte_pktmbuf_pool_create("Benchmark_DMA_DST",
473 : : nr_buf,
474 : : 0,
475 : : 0,
476 : : buf_size,
477 : 0 : cfg->dst_numa_node);
478 : 0 : if (dst_pool == NULL) {
479 : 0 : PRINT_ERR("Error with destination mempool creation.\n");
480 : 0 : return -1;
481 : : }
482 : :
483 : 0 : *srcs = rte_malloc(NULL, nr_buf * sizeof(struct rte_mbuf *), 0);
484 : 0 : if (*srcs == NULL) {
485 : : printf("Error: srcs malloc failed.\n");
486 : 0 : return -1;
487 : : }
488 : :
489 : 0 : *dsts = rte_malloc(NULL, nr_buf * sizeof(struct rte_mbuf *), 0);
490 : 0 : if (*dsts == NULL) {
491 : : printf("Error: dsts malloc failed.\n");
492 : 0 : return -1;
493 : : }
494 : :
495 : 0 : if (rte_pktmbuf_alloc_bulk(src_pool, *srcs, nr_buf) != 0) {
496 : : printf("alloc src mbufs failed.\n");
497 : 0 : return -1;
498 : : }
499 : :
500 : 0 : if (rte_pktmbuf_alloc_bulk(dst_pool, *dsts, nr_buf) != 0) {
501 : : printf("alloc dst mbufs failed.\n");
502 : 0 : return -1;
503 : : }
504 : :
505 : 0 : for (i = 0; i < nr_buf; i++) {
506 : 0 : memset(rte_pktmbuf_mtod((*srcs)[i], void *), rte_rand(), cur_buf_size);
507 : 0 : memset(rte_pktmbuf_mtod((*dsts)[i], void *), 0, cur_buf_size);
508 : : }
509 : :
510 : 0 : if (cfg->is_sg) {
511 : 0 : uint8_t nb_src_sges = cfg->nb_src_sges;
512 : 0 : uint8_t nb_dst_sges = cfg->nb_dst_sges;
513 : : uint32_t sglen_src, sglen_dst;
514 : :
515 : 0 : *src_sges = rte_zmalloc(NULL, nr_buf * sizeof(struct rte_dma_sge),
516 : : RTE_CACHE_LINE_SIZE);
517 : 0 : if (*src_sges == NULL) {
518 : : printf("Error: src_sges array malloc failed.\n");
519 : 0 : return -1;
520 : : }
521 : :
522 : 0 : *dst_sges = rte_zmalloc(NULL, nr_buf * sizeof(struct rte_dma_sge),
523 : : RTE_CACHE_LINE_SIZE);
524 : 0 : if (*dst_sges == NULL) {
525 : : printf("Error: dst_sges array malloc failed.\n");
526 : 0 : return -1;
527 : : }
528 : :
529 : 0 : sglen_src = cur_buf_size / nb_src_sges;
530 : 0 : sglen_dst = cur_buf_size / nb_dst_sges;
531 : :
532 : 0 : for (i = 0; i < nr_buf; i++) {
533 : 0 : (*src_sges)[i].addr = rte_pktmbuf_iova((*srcs)[i]);
534 : 0 : (*src_sges)[i].length = sglen_src;
535 : 0 : if (!((i+1) % nb_src_sges))
536 : 0 : (*src_sges)[i].length += (cur_buf_size % nb_src_sges);
537 : :
538 : 0 : (*dst_sges)[i].addr = rte_pktmbuf_iova((*dsts)[i]);
539 : 0 : (*dst_sges)[i].length = sglen_dst;
540 : 0 : if (!((i+1) % nb_dst_sges))
541 : 0 : (*dst_sges)[i].length += (cur_buf_size % nb_dst_sges);
542 : : }
543 : : }
544 : :
545 : : return 0;
546 : : }
547 : :
548 : : static uint32_t
549 : 0 : align_buffer_count(struct test_configure *cfg, uint32_t *nr_sgsrc, uint32_t *nr_sgdst)
550 : : {
551 : 0 : uint16_t nb_workers = cfg->num_worker;
552 : : uint32_t nr_buf;
553 : :
554 : 0 : nr_buf = (cfg->mem_size.cur * 1024 * 1024) / (cfg->buf_size.cur * 2);
555 : 0 : nr_buf -= (nr_buf % nb_workers);
556 : :
557 : 0 : if (nr_sgsrc == NULL || nr_sgdst == NULL)
558 : : return nr_buf;
559 : :
560 : 0 : if (cfg->is_sg) {
561 : 0 : nr_buf /= nb_workers;
562 : 0 : nr_buf -= nr_buf % (cfg->nb_src_sges * cfg->nb_dst_sges);
563 : 0 : nr_buf *= nb_workers;
564 : :
565 : 0 : if (cfg->nb_dst_sges > cfg->nb_src_sges) {
566 : 0 : *nr_sgsrc = (nr_buf / cfg->nb_dst_sges * cfg->nb_src_sges);
567 : 0 : *nr_sgdst = nr_buf;
568 : : } else {
569 : 0 : *nr_sgsrc = nr_buf;
570 : 0 : *nr_sgdst = (nr_buf / cfg->nb_src_sges * cfg->nb_dst_sges);
571 : : }
572 : : }
573 : :
574 : : return nr_buf;
575 : : }
576 : :
577 : : static lcore_function_t *
578 : : get_work_function(struct test_configure *cfg)
579 : : {
580 : : lcore_function_t *fn;
581 : :
582 : 0 : if (cfg->is_dma) {
583 : 0 : if (!cfg->is_sg)
584 : : fn = do_dma_plain_mem_copy;
585 : : else
586 : : fn = do_dma_sg_mem_copy;
587 : : } else {
588 : : fn = do_cpu_mem_copy;
589 : : }
590 : :
591 : : return fn;
592 : : }
593 : :
594 : : static int
595 : 0 : attach_ext_buffer(struct vchan_dev_config *vchan_dev, struct lcore_params *lcore, bool is_sg,
596 : : uint32_t nr_sgsrc, uint32_t nr_sgdst)
597 : : {
598 : : static struct rte_mbuf_ext_shared_info *ext_buf_info;
599 : : struct rte_dma_sge **src_sges, **dst_sges;
600 : : struct rte_mbuf **srcs, **dsts;
601 : : unsigned int cur_buf_size;
602 : : unsigned int buf_size;
603 : : uint32_t nr_buf;
604 : : uint32_t i;
605 : :
606 : 0 : cur_buf_size = lcore->buf_size;
607 : 0 : buf_size = cur_buf_size + RTE_PKTMBUF_HEADROOM;
608 : 0 : nr_buf = lcore->nr_buf;
609 : 0 : srcs = lcore->srcs;
610 : 0 : dsts = lcore->dsts;
611 : :
612 : 0 : ext_buf_info = rte_malloc(NULL, sizeof(struct rte_mbuf_ext_shared_info), 0);
613 : 0 : if (ext_buf_info == NULL) {
614 : : printf("Error: ext_buf_info malloc failed.\n");
615 : 0 : return -1;
616 : : }
617 : 0 : ext_buf_info->free_cb = dummy_free_ext_buf;
618 : 0 : ext_buf_info->fcb_opaque = NULL;
619 : :
620 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_DEV_TO_MEM) {
621 : 0 : for (i = 0; i < nr_buf; i++) {
622 : : /* Using mbuf structure to hold remote iova address. */
623 : 0 : rte_pktmbuf_attach_extbuf(srcs[i],
624 : : (void *)(vchan_dev->raddr + (i * buf_size)),
625 : 0 : (rte_iova_t)(vchan_dev->raddr + (i * buf_size)), 0, ext_buf_info);
626 : 0 : rte_mbuf_ext_refcnt_update(ext_buf_info, 1);
627 : : }
628 : : }
629 : :
630 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_MEM_TO_DEV) {
631 : 0 : for (i = 0; i < nr_buf; i++) {
632 : : /* Using mbuf structure to hold remote iova address. */
633 : 0 : rte_pktmbuf_attach_extbuf(dsts[i],
634 : : (void *)(vchan_dev->raddr + (i * buf_size)),
635 : 0 : (rte_iova_t)(vchan_dev->raddr + (i * buf_size)), 0, ext_buf_info);
636 : 0 : rte_mbuf_ext_refcnt_update(ext_buf_info, 1);
637 : : }
638 : : }
639 : :
640 : 0 : if (is_sg) {
641 : 0 : uint8_t nb_src_sges = lcore->sge.nb_srcs;
642 : 0 : uint8_t nb_dst_sges = lcore->sge.nb_dsts;
643 : : uint32_t sglen_src, sglen_dst;
644 : :
645 : : src_sges = &lcore->sge.srcs;
646 : : dst_sges = &lcore->sge.dsts;
647 : :
648 : 0 : sglen_src = cur_buf_size / nb_src_sges;
649 : 0 : sglen_dst = cur_buf_size / nb_dst_sges;
650 : :
651 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_DEV_TO_MEM) {
652 : 0 : for (i = 0; i < nr_sgsrc; i++) {
653 : 0 : (*src_sges)[i].addr = rte_pktmbuf_iova(srcs[i]);
654 : 0 : (*src_sges)[i].length = sglen_src;
655 : 0 : if (!((i+1) % nb_src_sges))
656 : 0 : (*src_sges)[i].length += (cur_buf_size % nb_src_sges);
657 : : }
658 : : }
659 : :
660 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_MEM_TO_DEV) {
661 : 0 : for (i = 0; i < nr_sgdst; i++) {
662 : 0 : (*dst_sges)[i].addr = rte_pktmbuf_iova(dsts[i]);
663 : 0 : (*dst_sges)[i].length = sglen_dst;
664 : 0 : if (!((i+1) % nb_dst_sges))
665 : 0 : (*dst_sges)[i].length += (cur_buf_size % nb_dst_sges);
666 : : }
667 : : }
668 : : }
669 : :
670 : : return 0;
671 : : }
672 : :
673 : : int
674 : 0 : mem_copy_benchmark(struct test_configure *cfg)
675 : : {
676 : : uint32_t i, j, k;
677 : : uint32_t offset;
678 : : unsigned int lcore_id = 0;
679 : 0 : struct rte_mbuf **srcs = NULL, **dsts = NULL, **m = NULL;
680 : 0 : struct rte_dma_sge *src_sges = NULL, *dst_sges = NULL;
681 : : struct vchan_dev_config *vchan_dev = NULL;
682 : : struct lcore_dma_map_t *lcore_dma_map = NULL;
683 : 0 : unsigned int buf_size = cfg->buf_size.cur;
684 : 0 : uint16_t kick_batch = cfg->kick_batch.cur;
685 : 0 : uint16_t nb_workers = cfg->num_worker;
686 : 0 : uint16_t test_secs = cfg->test_secs;
687 : 0 : float memory = 0;
688 : 0 : uint32_t avg_cycles = 0;
689 : : uint32_t avg_cycles_total;
690 : : float mops, mops_total;
691 : : float bandwidth, bandwidth_total;
692 : 0 : uint32_t nr_sgsrc = 0, nr_sgdst = 0;
693 : : uint32_t nr_buf;
694 : : int ret = 0;
695 : :
696 : 0 : nr_buf = align_buffer_count(cfg, &nr_sgsrc, &nr_sgdst);
697 : 0 : cfg->nr_buf = nr_buf;
698 : :
699 : 0 : if (setup_memory_env(cfg, &srcs, &dsts, &src_sges, &dst_sges) < 0)
700 : 0 : goto out;
701 : :
702 : 0 : if (cfg->is_dma)
703 : 0 : if (config_dmadevs(cfg) < 0)
704 : 0 : goto out;
705 : :
706 : 0 : if (cfg->cache_flush == 1) {
707 : 0 : cache_flush_buf(srcs, buf_size, nr_buf);
708 : 0 : cache_flush_buf(dsts, buf_size, nr_buf);
709 : : rte_mb();
710 : : }
711 : :
712 : : printf("Start testing....\n");
713 : :
714 : 0 : for (i = 0; i < nb_workers; i++) {
715 : : lcore_dma_map = &cfg->dma_config[i].lcore_dma_map;
716 : 0 : vchan_dev = &cfg->dma_config[i].vchan_dev;
717 : :
718 : 0 : lcore_id = lcore_dma_map->lcore;
719 : 0 : offset = nr_buf / nb_workers * i;
720 : 0 : lcores[i] = rte_malloc(NULL, sizeof(struct lcore_params), 0);
721 : 0 : if (lcores[i] == NULL) {
722 : : printf("lcore parameters malloc failure for lcore %d\n", lcore_id);
723 : : break;
724 : : }
725 : 0 : if (cfg->is_dma) {
726 : 0 : lcores[i]->dma_name = lcore_dma_map->dma_names;
727 : 0 : lcores[i]->dev_id = lcore_dma_map->dma_id;
728 : 0 : lcores[i]->kick_batch = kick_batch;
729 : : }
730 : :
731 : 0 : lcores[i]->worker_id = i;
732 : 0 : lcores[i]->nr_buf = (uint32_t)(nr_buf / nb_workers);
733 : 0 : lcores[i]->buf_size = buf_size;
734 : 0 : lcores[i]->test_secs = test_secs;
735 : 0 : lcores[i]->srcs = srcs + offset;
736 : 0 : lcores[i]->dsts = dsts + offset;
737 : 0 : lcores[i]->scenario_id = cfg->scenario_id;
738 : 0 : lcores[i]->lcore_id = lcore_id;
739 : :
740 : 0 : if (cfg->is_sg) {
741 : 0 : lcores[i]->sge.nb_srcs = cfg->nb_src_sges;
742 : 0 : lcores[i]->sge.nb_dsts = cfg->nb_dst_sges;
743 : 0 : lcores[i]->sge.srcs = src_sges + (nr_sgsrc / nb_workers * i);
744 : 0 : lcores[i]->sge.dsts = dst_sges + (nr_sgdst / nb_workers * i);
745 : : }
746 : :
747 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_DEV_TO_MEM ||
748 : : vchan_dev->tdir == RTE_DMA_DIR_MEM_TO_DEV) {
749 : 0 : if (attach_ext_buffer(vchan_dev, lcores[i], cfg->is_sg,
750 : : (nr_sgsrc/nb_workers), (nr_sgdst/nb_workers)) < 0)
751 : 0 : goto out;
752 : : }
753 : :
754 : 0 : rte_eal_remote_launch(get_work_function(cfg), (void *)(lcores[i]), lcore_id);
755 : : }
756 : :
757 : : while (1) {
758 : : bool ready = true;
759 : 0 : for (i = 0; i < nb_workers; i++) {
760 : 0 : if (lcores[i]->worker_info.ready_flag == false) {
761 : : ready = 0;
762 : : break;
763 : : }
764 : : }
765 : 0 : if (ready)
766 : : break;
767 : : }
768 : :
769 : 0 : for (i = 0; i < nb_workers; i++)
770 : 0 : lcores[i]->worker_info.start_flag = true;
771 : :
772 : 0 : usleep(TEST_WAIT_U_SECOND);
773 : 0 : for (i = 0; i < nb_workers; i++)
774 : 0 : lcores[i]->worker_info.test_cpl = lcores[i]->worker_info.total_cpl;
775 : :
776 : 0 : usleep(test_secs * 1000 * 1000);
777 : 0 : for (i = 0; i < nb_workers; i++)
778 : 0 : lcores[i]->worker_info.test_cpl = lcores[i]->worker_info.total_cpl -
779 : 0 : lcores[i]->worker_info.test_cpl;
780 : :
781 : 0 : for (i = 0; i < nb_workers; i++)
782 : 0 : lcores[i]->worker_info.stop_flag = true;
783 : :
784 : 0 : rte_eal_mp_wait_lcore();
785 : :
786 : 0 : for (k = 0; k < nb_workers; k++) {
787 : : struct rte_mbuf **src_buf = NULL, **dst_buf = NULL;
788 : 0 : uint32_t nr_buf_pt = nr_buf / nb_workers;
789 : : vchan_dev = &cfg->dma_config[k].vchan_dev;
790 : 0 : offset = nr_buf / nb_workers * k;
791 : 0 : src_buf = srcs + offset;
792 : 0 : dst_buf = dsts + offset;
793 : :
794 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_MEM_TO_MEM && !cfg->is_sg) {
795 : 0 : for (i = 0; i < nr_buf_pt; i++) {
796 : 0 : if (memcmp(rte_pktmbuf_mtod(src_buf[i], void *),
797 : 0 : rte_pktmbuf_mtod(dst_buf[i], void *),
798 : 0 : cfg->buf_size.cur) != 0) {
799 : : printf("Copy validation fails for buffer number %d\n", i);
800 : : ret = -1;
801 : 0 : goto out;
802 : : }
803 : : }
804 : 0 : } else if (vchan_dev->tdir == RTE_DMA_DIR_MEM_TO_MEM && cfg->is_sg) {
805 : 0 : size_t src_remsz = buf_size % cfg->nb_src_sges;
806 : 0 : size_t dst_remsz = buf_size % cfg->nb_dst_sges;
807 : 0 : size_t src_sz = buf_size / cfg->nb_src_sges;
808 : 0 : size_t dst_sz = buf_size / cfg->nb_dst_sges;
809 : 0 : uint8_t src[buf_size], dst[buf_size];
810 : : uint8_t *sbuf, *dbuf, *ptr;
811 : :
812 : 0 : for (i = 0; i < (nr_buf_pt / RTE_MAX(cfg->nb_src_sges, cfg->nb_dst_sges));
813 : 0 : i++) {
814 : : sbuf = src;
815 : : dbuf = dst;
816 : : ptr = NULL;
817 : :
818 : 0 : for (j = 0; j < cfg->nb_src_sges; j++) {
819 : 0 : ptr = rte_pktmbuf_mtod(src_buf[i * cfg->nb_src_sges + j],
820 : : uint8_t *);
821 : : memcpy(sbuf, ptr, src_sz);
822 : 0 : sbuf += src_sz;
823 : : }
824 : :
825 : 0 : if (src_remsz)
826 : 0 : memcpy(sbuf, ptr + src_sz, src_remsz);
827 : :
828 : 0 : for (j = 0; j < cfg->nb_dst_sges; j++) {
829 : 0 : ptr = rte_pktmbuf_mtod(dst_buf[i * cfg->nb_dst_sges + j],
830 : : uint8_t *);
831 : : memcpy(dbuf, ptr, dst_sz);
832 : 0 : dbuf += dst_sz;
833 : : }
834 : :
835 : 0 : if (dst_remsz)
836 : 0 : memcpy(dbuf, ptr + dst_sz, dst_remsz);
837 : :
838 : 0 : if (memcmp(src, dst, buf_size) != 0) {
839 : 0 : printf("SG Copy validation fails for buffer number %d\n",
840 : : i * cfg->nb_src_sges);
841 : : ret = -1;
842 : 0 : goto out;
843 : : }
844 : : }
845 : : }
846 : : }
847 : :
848 : : mops_total = 0;
849 : : bandwidth_total = 0;
850 : : avg_cycles_total = 0;
851 : 0 : for (i = 0; i < nb_workers; i++) {
852 : : vchan_dev = &cfg->dma_config[i].vchan_dev;
853 : 0 : calc_result(buf_size, nr_buf, nb_workers, test_secs,
854 : 0 : lcores[i]->worker_info.test_cpl,
855 : : &memory, &avg_cycles, &bandwidth, &mops);
856 : 0 : printf("Direction: %s\n", vchan_dev->tdir == 0 ? "mem2mem" :
857 : 0 : vchan_dev->tdir == 1 ? "mem2dev" : "dev2mem");
858 : 0 : output_result(cfg, lcores[i], kick_batch, avg_cycles, buf_size,
859 : : nr_buf / nb_workers, memory, bandwidth, mops);
860 : 0 : mops_total += mops;
861 : 0 : bandwidth_total += bandwidth;
862 : 0 : avg_cycles_total += avg_cycles;
863 : : }
864 : 0 : printf("\nAverage Cycles/op per worker: %.1lf, Total Bandwidth: %.3lf Gbps, Total MOps: %.3lf\n",
865 : 0 : (avg_cycles_total * (float) 1.0) / nb_workers, bandwidth_total, mops_total);
866 : 0 : snprintf(output_str[MAX_WORKER_NB], MAX_OUTPUT_STR_LEN, CSV_TOTAL_LINE_FMT,
867 : 0 : cfg->scenario_id, nr_buf, memory * nb_workers,
868 : : (avg_cycles_total * (float) 1.0) / nb_workers, bandwidth_total, mops_total);
869 : :
870 : 0 : out:
871 : :
872 : 0 : for (k = 0; k < nb_workers; k++) {
873 : : struct rte_mbuf **sbuf = NULL, **dbuf = NULL;
874 : : vchan_dev = &cfg->dma_config[k].vchan_dev;
875 : 0 : offset = nr_buf / nb_workers * k;
876 : : m = NULL;
877 : 0 : if (vchan_dev->tdir == RTE_DMA_DIR_DEV_TO_MEM) {
878 : 0 : sbuf = srcs + offset;
879 : : m = sbuf;
880 : 0 : } else if (vchan_dev->tdir == RTE_DMA_DIR_MEM_TO_DEV) {
881 : 0 : dbuf = dsts + offset;
882 : : m = dbuf;
883 : : }
884 : :
885 : 0 : if (m) {
886 : 0 : for (i = 0; i < (nr_buf / nb_workers); i++)
887 : 0 : rte_pktmbuf_detach_extbuf(m[i]);
888 : :
889 : 0 : if (m[0]->shinfo && rte_mbuf_ext_refcnt_read(m[0]->shinfo) == 0)
890 : 0 : rte_free(m[0]->shinfo);
891 : : }
892 : : }
893 : :
894 : : /* free mbufs used in the test */
895 : 0 : if (srcs != NULL)
896 : 0 : rte_pktmbuf_free_bulk(srcs, nr_buf);
897 : 0 : if (dsts != NULL)
898 : 0 : rte_pktmbuf_free_bulk(dsts, nr_buf);
899 : :
900 : : /* free the points for the mbufs */
901 : 0 : rte_free(srcs);
902 : 0 : srcs = NULL;
903 : 0 : rte_free(dsts);
904 : 0 : dsts = NULL;
905 : :
906 : 0 : rte_mempool_free(src_pool);
907 : 0 : src_pool = NULL;
908 : :
909 : 0 : rte_mempool_free(dst_pool);
910 : 0 : dst_pool = NULL;
911 : :
912 : : /* free sges for mbufs */
913 : 0 : rte_free(src_sges);
914 : 0 : src_sges = NULL;
915 : :
916 : 0 : rte_free(dst_sges);
917 : 0 : dst_sges = NULL;
918 : :
919 : : /* free the worker parameters */
920 : 0 : for (i = 0; i < nb_workers; i++) {
921 : 0 : rte_free(lcores[i]);
922 : 0 : lcores[i] = NULL;
923 : : }
924 : :
925 : 0 : if (cfg->is_dma) {
926 : 0 : for (i = 0; i < nb_workers; i++) {
927 : : lcore_dma_map = &cfg->dma_config[i].lcore_dma_map;
928 : 0 : printf("Stopping dmadev %d\n", lcore_dma_map->dma_id);
929 : 0 : rte_dma_stop(lcore_dma_map->dma_id);
930 : : }
931 : : }
932 : :
933 : 0 : return ret;
934 : : }
|