dma_map_benchmark.c 2.8 KB

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