dma_map_benchmark.c 2.8 KB

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