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/types.h> 14 15 #define NSEC_PER_MSEC 1000000L 16 17 #define DMA_MAP_BENCHMARK _IOWR('d', 1, struct map_benchmark) 18 #define DMA_MAP_MAX_THREADS 1024 19 #define DMA_MAP_MAX_SECONDS 300 20 #define DMA_MAP_MAX_TRANS_DELAY (10 * NSEC_PER_MSEC) 21 22 #define DMA_MAP_BIDIRECTIONAL 0 23 #define DMA_MAP_TO_DEVICE 1 24 #define DMA_MAP_FROM_DEVICE 2 25 26 static char *directions[] = { 27 "BIDIRECTIONAL", 28 "TO_DEVICE", 29 "FROM_DEVICE", 30 }; 31 32 struct map_benchmark { 33 __u64 avg_map_100ns; /* average map latency in 100ns */ 34 __u64 map_stddev; /* standard deviation of map latency */ 35 __u64 avg_unmap_100ns; /* as above */ 36 __u64 unmap_stddev; 37 __u32 threads; /* how many threads will do map/unmap in parallel */ 38 __u32 seconds; /* how long the test will last */ 39 __s32 node; /* which numa node this benchmark will run on */ 40 __u32 dma_bits; /* DMA addressing capability */ 41 __u32 dma_dir; /* DMA data direction */ 42 __u32 dma_trans_ns; /* time for DMA transmission in ns */ 43 __u8 expansion[80]; /* For future use */ 44 }; 45 46 int main(int argc, char **argv) 47 { 48 struct map_benchmark map; 49 int fd, opt; 50 /* default single thread, run 20 seconds on NUMA_NO_NODE */ 51 int threads = 1, seconds = 20, node = -1; 52 /* default dma mask 32bit, bidirectional DMA */ 53 int bits = 32, xdelay = 0, dir = DMA_MAP_BIDIRECTIONAL; 54 55 int cmd = DMA_MAP_BENCHMARK; 56 char *p; 57 58 while ((opt = getopt(argc, argv, "t:s:n:b:d:x:")) != -1) { 59 switch (opt) { 60 case 't': 61 threads = atoi(optarg); 62 break; 63 case 's': 64 seconds = atoi(optarg); 65 break; 66 case 'n': 67 node = atoi(optarg); 68 break; 69 case 'b': 70 bits = atoi(optarg); 71 break; 72 case 'd': 73 dir = atoi(optarg); 74 break; 75 case 'x': 76 xdelay = atoi(optarg); 77 break; 78 default: 79 return -1; 80 } 81 } 82 83 if (threads <= 0 || threads > DMA_MAP_MAX_THREADS) { 84 fprintf(stderr, "invalid number of threads, must be in 1-%d\n", 85 DMA_MAP_MAX_THREADS); 86 exit(1); 87 } 88 89 if (seconds <= 0 || seconds > DMA_MAP_MAX_SECONDS) { 90 fprintf(stderr, "invalid number of seconds, must be in 1-%d\n", 91 DMA_MAP_MAX_SECONDS); 92 exit(1); 93 } 94 95 if (xdelay < 0 || xdelay > DMA_MAP_MAX_TRANS_DELAY) { 96 fprintf(stderr, "invalid transmit delay, must be in 0-%ld\n", 97 DMA_MAP_MAX_TRANS_DELAY); 98 exit(1); 99 } 100 101 /* suppose the mininum DMA zone is 1MB in the world */ 102 if (bits < 20 || bits > 64) { 103 fprintf(stderr, "invalid dma mask bit, must be in 20-64\n"); 104 exit(1); 105 } 106 107 if (dir != DMA_MAP_BIDIRECTIONAL && dir != DMA_MAP_TO_DEVICE && 108 dir != DMA_MAP_FROM_DEVICE) { 109 fprintf(stderr, "invalid dma direction\n"); 110 exit(1); 111 } 112 113 fd = open("/sys/kernel/debug/dma_map_benchmark", O_RDWR); 114 if (fd == -1) { 115 perror("open"); 116 exit(1); 117 } 118 119 memset(&map, 0, sizeof(map)); 120 map.seconds = seconds; 121 map.threads = threads; 122 map.node = node; 123 map.dma_bits = bits; 124 map.dma_dir = dir; 125 map.dma_trans_ns = xdelay; 126 127 if (ioctl(fd, cmd, &map)) { 128 perror("ioctl"); 129 exit(1); 130 } 131 132 printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s\n", 133 threads, seconds, node, dir[directions]); 134 printf("average map latency(us):%.1f standard deviation:%.1f\n", 135 map.avg_map_100ns/10.0, map.map_stddev/10.0); 136 printf("average unmap latency(us):%.1f standard deviation:%.1f\n", 137 map.avg_unmap_100ns/10.0, map.unmap_stddev/10.0); 138 139 return 0; 140 } 141