1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020 HiSilicon Limited.
4 */
5
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/ioctl.h>
12 #include <sys/mman.h>
13 #include <linux/map_benchmark.h>
14
15 #define NSEC_PER_MSEC 1000000L
16
17 static char *directions[] = {
18 "BIDIRECTIONAL",
19 "TO_DEVICE",
20 "FROM_DEVICE",
21 };
22
main(int argc,char ** argv)23 int main(int argc, char **argv)
24 {
25 struct map_benchmark map;
26 int fd, opt;
27 /* default single thread, run 20 seconds on NUMA_NO_NODE */
28 int threads = 1, seconds = 20, node = -1;
29 /* default dma mask 32bit, bidirectional DMA */
30 int bits = 32, xdelay = 0, dir = DMA_MAP_BIDIRECTIONAL;
31 /* default granule 1 PAGESIZE */
32 int granule = 1;
33
34 int cmd = DMA_MAP_BENCHMARK;
35
36 while ((opt = getopt(argc, argv, "t:s:n:b:d:x:g:")) != -1) {
37 switch (opt) {
38 case 't':
39 threads = atoi(optarg);
40 break;
41 case 's':
42 seconds = atoi(optarg);
43 break;
44 case 'n':
45 node = atoi(optarg);
46 break;
47 case 'b':
48 bits = atoi(optarg);
49 break;
50 case 'd':
51 dir = atoi(optarg);
52 break;
53 case 'x':
54 xdelay = atoi(optarg);
55 break;
56 case 'g':
57 granule = atoi(optarg);
58 break;
59 default:
60 return -1;
61 }
62 }
63
64 if (threads <= 0 || threads > DMA_MAP_MAX_THREADS) {
65 fprintf(stderr, "invalid number of threads, must be in 1-%d\n",
66 DMA_MAP_MAX_THREADS);
67 exit(1);
68 }
69
70 if (seconds <= 0 || seconds > DMA_MAP_MAX_SECONDS) {
71 fprintf(stderr, "invalid number of seconds, must be in 1-%d\n",
72 DMA_MAP_MAX_SECONDS);
73 exit(1);
74 }
75
76 if (xdelay < 0 || xdelay > DMA_MAP_MAX_TRANS_DELAY) {
77 fprintf(stderr, "invalid transmit delay, must be in 0-%ld\n",
78 DMA_MAP_MAX_TRANS_DELAY);
79 exit(1);
80 }
81
82 /* suppose the mininum DMA zone is 1MB in the world */
83 if (bits < 20 || bits > 64) {
84 fprintf(stderr, "invalid dma mask bit, must be in 20-64\n");
85 exit(1);
86 }
87
88 if (dir != DMA_MAP_BIDIRECTIONAL && dir != DMA_MAP_TO_DEVICE &&
89 dir != DMA_MAP_FROM_DEVICE) {
90 fprintf(stderr, "invalid dma direction\n");
91 exit(1);
92 }
93
94 if (granule < 1 || granule > 1024) {
95 fprintf(stderr, "invalid granule size\n");
96 exit(1);
97 }
98
99 fd = open("/sys/kernel/debug/dma_map_benchmark", O_RDWR);
100 if (fd == -1) {
101 perror("open");
102 exit(1);
103 }
104
105 memset(&map, 0, sizeof(map));
106 map.seconds = seconds;
107 map.threads = threads;
108 map.node = node;
109 map.dma_bits = bits;
110 map.dma_dir = dir;
111 map.dma_trans_ns = xdelay;
112 map.granule = granule;
113
114 if (ioctl(fd, cmd, &map)) {
115 perror("ioctl");
116 exit(1);
117 }
118
119 printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s granule: %d\n",
120 threads, seconds, node, directions[dir], granule);
121 printf("average map latency(us):%.1f standard deviation:%.1f\n",
122 map.avg_map_100ns/10.0, map.map_stddev/10.0);
123 printf("average unmap latency(us):%.1f standard deviation:%.1f\n",
124 map.avg_unmap_100ns/10.0, map.unmap_stddev/10.0);
125
126 return 0;
127 }
128