1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 HiSilicon Limited.
11 #include <sys/ioctl.h>
13 #include <linux/types.h>
14 #include <linux/map_benchmark.h>
16 #define NSEC_PER_MSEC 1000000L
18 static char *directions
[] = {
24 int main(int argc
, char **argv
)
26 struct map_benchmark map
;
28 /* default single thread, run 20 seconds on NUMA_NO_NODE */
29 int threads
= 1, seconds
= 20, node
= -1;
30 /* default dma mask 32bit, bidirectional DMA */
31 int bits
= 32, xdelay
= 0, dir
= DMA_MAP_BIDIRECTIONAL
;
32 /* default granule 1 PAGESIZE */
35 int cmd
= DMA_MAP_BENCHMARK
;
37 while ((opt
= getopt(argc
, argv
, "t:s:n:b:d:x:g:")) != -1) {
40 threads
= atoi(optarg
);
43 seconds
= atoi(optarg
);
55 xdelay
= atoi(optarg
);
58 granule
= atoi(optarg
);
65 if (threads
<= 0 || threads
> DMA_MAP_MAX_THREADS
) {
66 fprintf(stderr
, "invalid number of threads, must be in 1-%d\n",
71 if (seconds
<= 0 || seconds
> DMA_MAP_MAX_SECONDS
) {
72 fprintf(stderr
, "invalid number of seconds, must be in 1-%d\n",
77 if (xdelay
< 0 || xdelay
> DMA_MAP_MAX_TRANS_DELAY
) {
78 fprintf(stderr
, "invalid transmit delay, must be in 0-%ld\n",
79 DMA_MAP_MAX_TRANS_DELAY
);
83 /* suppose the mininum DMA zone is 1MB in the world */
84 if (bits
< 20 || bits
> 64) {
85 fprintf(stderr
, "invalid dma mask bit, must be in 20-64\n");
89 if (dir
!= DMA_MAP_BIDIRECTIONAL
&& dir
!= DMA_MAP_TO_DEVICE
&&
90 dir
!= DMA_MAP_FROM_DEVICE
) {
91 fprintf(stderr
, "invalid dma direction\n");
95 if (granule
< 1 || granule
> 1024) {
96 fprintf(stderr
, "invalid granule size\n");
100 fd
= open("/sys/kernel/debug/dma_map_benchmark", O_RDWR
);
106 memset(&map
, 0, sizeof(map
));
107 map
.seconds
= seconds
;
108 map
.threads
= threads
;
112 map
.dma_trans_ns
= xdelay
;
113 map
.granule
= granule
;
115 if (ioctl(fd
, cmd
, &map
)) {
120 printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s granule: %d\n",
121 threads
, seconds
, node
, dir
[directions
], granule
);
122 printf("average map latency(us):%.1f standard deviation:%.1f\n",
123 map
.avg_map_100ns
/10.0, map
.map_stddev
/10.0);
124 printf("average unmap latency(us):%.1f standard deviation:%.1f\n",
125 map
.avg_unmap_100ns
/10.0, map
.unmap_stddev
/10.0);