Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/dma: add test application for DMA_MAP_BENCHMARK

This patch provides the test application for DMA_MAP_BENCHMARK.

Before running the test application, we need to bind a device to dma_map_
benchmark driver. For example, unbind "xxx" from its original driver and
bind to dma_map_benchmark:

echo dma_map_benchmark > /sys/bus/platform/devices/xxx/driver_override
echo xxx > /sys/bus/platform/drivers/xxx/unbind
echo xxx > /sys/bus/platform/drivers/dma_map_benchmark/bind

Another example for PCI devices:
echo dma_map_benchmark > /sys/bus/pci/devices/0000:00:01.0/driver_override
echo 0000:00:01.0 > /sys/bus/pci/drivers/xxx/unbind
echo 0000:00:01.0 > /sys/bus/pci/drivers/dma_map_benchmark/bind

The below command will run 16 threads on numa node 0 for 10 seconds on
the device bound to dma_map_benchmark platform_driver or pci_driver:
./dma_map_benchmark -t 16 -s 10 -n 0
dma mapping benchmark: threads:16 seconds:10
average map latency(us):1.1 standard deviation:1.9
average unmap latency(us):0.5 standard deviation:0.8

Cc: Will Deacon <will@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>

authored by

Barry Song and committed by
Christoph Hellwig
76793257 65789daa

+136
+6
MAINTAINERS
··· 5237 5237 F: include/linux/dma-map-ops.h 5238 5238 F: kernel/dma/ 5239 5239 5240 + DMA MAPPING BENCHMARK 5241 + M: Barry Song <song.bao.hua@hisilicon.com> 5242 + L: iommu@lists.linux-foundation.org 5243 + F: kernel/dma/map_benchmark.c 5244 + F: tools/testing/selftests/dma/ 5245 + 5240 5246 DMA-BUF HEAPS FRAMEWORK 5241 5247 M: Sumit Semwal <sumit.semwal@linaro.org> 5242 5248 R: Benjamin Gaignard <benjamin.gaignard@linaro.org>
+6
tools/testing/selftests/dma/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + CFLAGS += -I../../../../usr/include/ 3 + 4 + TEST_GEN_PROGS := dma_map_benchmark 5 + 6 + include ../lib.mk
+1
tools/testing/selftests/dma/config
··· 1 + CONFIG_DMA_MAP_BENCHMARK=y
+123
tools/testing/selftests/dma/dma_map_benchmark.c
··· 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 <unistd.h> 10 + #include <sys/ioctl.h> 11 + #include <sys/mman.h> 12 + #include <linux/types.h> 13 + 14 + #define DMA_MAP_BENCHMARK _IOWR('d', 1, struct map_benchmark) 15 + #define DMA_MAP_MAX_THREADS 1024 16 + #define DMA_MAP_MAX_SECONDS 300 17 + 18 + #define DMA_MAP_BIDIRECTIONAL 0 19 + #define DMA_MAP_TO_DEVICE 1 20 + #define DMA_MAP_FROM_DEVICE 2 21 + 22 + static char *directions[] = { 23 + "BIDIRECTIONAL", 24 + "TO_DEVICE", 25 + "FROM_DEVICE", 26 + }; 27 + 28 + struct map_benchmark { 29 + __u64 avg_map_100ns; /* average map latency in 100ns */ 30 + __u64 map_stddev; /* standard deviation of map latency */ 31 + __u64 avg_unmap_100ns; /* as above */ 32 + __u64 unmap_stddev; 33 + __u32 threads; /* how many threads will do map/unmap in parallel */ 34 + __u32 seconds; /* how long the test will last */ 35 + __s32 node; /* which numa node this benchmark will run on */ 36 + __u32 dma_bits; /* DMA addressing capability */ 37 + __u32 dma_dir; /* DMA data direction */ 38 + __u64 expansion[10]; /* For future use */ 39 + }; 40 + 41 + int main(int argc, char **argv) 42 + { 43 + struct map_benchmark map; 44 + int fd, opt; 45 + /* default single thread, run 20 seconds on NUMA_NO_NODE */ 46 + int threads = 1, seconds = 20, node = -1; 47 + /* default dma mask 32bit, bidirectional DMA */ 48 + int bits = 32, dir = DMA_MAP_BIDIRECTIONAL; 49 + 50 + int cmd = DMA_MAP_BENCHMARK; 51 + char *p; 52 + 53 + while ((opt = getopt(argc, argv, "t:s:n:b:d:")) != -1) { 54 + switch (opt) { 55 + case 't': 56 + threads = atoi(optarg); 57 + break; 58 + case 's': 59 + seconds = atoi(optarg); 60 + break; 61 + case 'n': 62 + node = atoi(optarg); 63 + break; 64 + case 'b': 65 + bits = atoi(optarg); 66 + break; 67 + case 'd': 68 + dir = atoi(optarg); 69 + break; 70 + default: 71 + return -1; 72 + } 73 + } 74 + 75 + if (threads <= 0 || threads > DMA_MAP_MAX_THREADS) { 76 + fprintf(stderr, "invalid number of threads, must be in 1-%d\n", 77 + DMA_MAP_MAX_THREADS); 78 + exit(1); 79 + } 80 + 81 + if (seconds <= 0 || seconds > DMA_MAP_MAX_SECONDS) { 82 + fprintf(stderr, "invalid number of seconds, must be in 1-%d\n", 83 + DMA_MAP_MAX_SECONDS); 84 + exit(1); 85 + } 86 + 87 + /* suppose the mininum DMA zone is 1MB in the world */ 88 + if (bits < 20 || bits > 64) { 89 + fprintf(stderr, "invalid dma mask bit, must be in 20-64\n"); 90 + exit(1); 91 + } 92 + 93 + if (dir != DMA_MAP_BIDIRECTIONAL && dir != DMA_MAP_TO_DEVICE && 94 + dir != DMA_MAP_FROM_DEVICE) { 95 + fprintf(stderr, "invalid dma direction\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 + map.seconds = seconds; 106 + map.threads = threads; 107 + map.node = node; 108 + map.dma_bits = bits; 109 + map.dma_dir = dir; 110 + if (ioctl(fd, cmd, &map)) { 111 + perror("ioctl"); 112 + exit(1); 113 + } 114 + 115 + printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s\n", 116 + threads, seconds, node, dir[directions]); 117 + printf("average map latency(us):%.1f standard deviation:%.1f\n", 118 + map.avg_map_100ns/10.0, map.map_stddev/10.0); 119 + printf("average unmap latency(us):%.1f standard deviation:%.1f\n", 120 + map.avg_unmap_100ns/10.0, map.unmap_stddev/10.0); 121 + 122 + return 0; 123 + }