Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020 HiSilicon Limited.
0004  */
0005 
0006 #include <fcntl.h>
0007 #include <stdio.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include <unistd.h>
0011 #include <sys/ioctl.h>
0012 #include <sys/mman.h>
0013 #include <linux/types.h>
0014 #include <linux/map_benchmark.h>
0015 
0016 #define NSEC_PER_MSEC   1000000L
0017 
0018 static char *directions[] = {
0019     "BIDIRECTIONAL",
0020     "TO_DEVICE",
0021     "FROM_DEVICE",
0022 };
0023 
0024 int main(int argc, char **argv)
0025 {
0026     struct map_benchmark map;
0027     int fd, opt;
0028     /* default single thread, run 20 seconds on NUMA_NO_NODE */
0029     int threads = 1, seconds = 20, node = -1;
0030     /* default dma mask 32bit, bidirectional DMA */
0031     int bits = 32, xdelay = 0, dir = DMA_MAP_BIDIRECTIONAL;
0032     /* default granule 1 PAGESIZE */
0033     int granule = 1;
0034 
0035     int cmd = DMA_MAP_BENCHMARK;
0036     char *p;
0037 
0038     while ((opt = getopt(argc, argv, "t:s:n:b:d:x:g:")) != -1) {
0039         switch (opt) {
0040         case 't':
0041             threads = atoi(optarg);
0042             break;
0043         case 's':
0044             seconds = atoi(optarg);
0045             break;
0046         case 'n':
0047             node = atoi(optarg);
0048             break;
0049         case 'b':
0050             bits = atoi(optarg);
0051             break;
0052         case 'd':
0053             dir = atoi(optarg);
0054             break;
0055         case 'x':
0056             xdelay = atoi(optarg);
0057             break;
0058         case 'g':
0059             granule = atoi(optarg);
0060             break;
0061         default:
0062             return -1;
0063         }
0064     }
0065 
0066     if (threads <= 0 || threads > DMA_MAP_MAX_THREADS) {
0067         fprintf(stderr, "invalid number of threads, must be in 1-%d\n",
0068             DMA_MAP_MAX_THREADS);
0069         exit(1);
0070     }
0071 
0072     if (seconds <= 0 || seconds > DMA_MAP_MAX_SECONDS) {
0073         fprintf(stderr, "invalid number of seconds, must be in 1-%d\n",
0074             DMA_MAP_MAX_SECONDS);
0075         exit(1);
0076     }
0077 
0078     if (xdelay < 0 || xdelay > DMA_MAP_MAX_TRANS_DELAY) {
0079         fprintf(stderr, "invalid transmit delay, must be in 0-%ld\n",
0080             DMA_MAP_MAX_TRANS_DELAY);
0081         exit(1);
0082     }
0083 
0084     /* suppose the mininum DMA zone is 1MB in the world */
0085     if (bits < 20 || bits > 64) {
0086         fprintf(stderr, "invalid dma mask bit, must be in 20-64\n");
0087         exit(1);
0088     }
0089 
0090     if (dir != DMA_MAP_BIDIRECTIONAL && dir != DMA_MAP_TO_DEVICE &&
0091             dir != DMA_MAP_FROM_DEVICE) {
0092         fprintf(stderr, "invalid dma direction\n");
0093         exit(1);
0094     }
0095 
0096     if (granule < 1 || granule > 1024) {
0097         fprintf(stderr, "invalid granule size\n");
0098         exit(1);
0099     }
0100 
0101     fd = open("/sys/kernel/debug/dma_map_benchmark", O_RDWR);
0102     if (fd == -1) {
0103         perror("open");
0104         exit(1);
0105     }
0106 
0107     memset(&map, 0, sizeof(map));
0108     map.seconds = seconds;
0109     map.threads = threads;
0110     map.node = node;
0111     map.dma_bits = bits;
0112     map.dma_dir = dir;
0113     map.dma_trans_ns = xdelay;
0114     map.granule = granule;
0115 
0116     if (ioctl(fd, cmd, &map)) {
0117         perror("ioctl");
0118         exit(1);
0119     }
0120 
0121     printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s granule: %d\n",
0122             threads, seconds, node, dir[directions], granule);
0123     printf("average map latency(us):%.1f standard deviation:%.1f\n",
0124             map.avg_map_100ns/10.0, map.map_stddev/10.0);
0125     printf("average unmap latency(us):%.1f standard deviation:%.1f\n",
0126             map.avg_unmap_100ns/10.0, map.unmap_stddev/10.0);
0127 
0128     return 0;
0129 }