test_udelay.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * udelay() test kernel module
  4. *
  5. * Test is executed by writing and reading to /sys/kernel/debug/udelay_test
  6. * Tests are configured by writing: USECS ITERATIONS
  7. * Tests are executed by reading from the same file.
  8. * Specifying usecs of 0 or negative values will run multiples tests.
  9. *
  10. * Copyright (C) 2014 Google, Inc.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/delay.h>
  14. #include <linux/ktime.h>
  15. #include <linux/module.h>
  16. #include <linux/uaccess.h>
  17. #define DEFAULT_ITERATIONS 100
  18. #define DEBUGFS_FILENAME "udelay_test"
  19. static DEFINE_MUTEX(udelay_test_lock);
  20. static int udelay_test_usecs;
  21. static int udelay_test_iterations = DEFAULT_ITERATIONS;
  22. static int udelay_test_single(struct seq_file *s, int usecs, uint32_t iters)
  23. {
  24. int min = 0, max = 0, fail_count = 0;
  25. uint64_t sum = 0;
  26. uint64_t avg;
  27. int i;
  28. /* Allow udelay to be up to 0.5% fast */
  29. int allowed_error_ns = usecs * 5;
  30. for (i = 0; i < iters; ++i) {
  31. s64 kt1, kt2;
  32. int time_passed;
  33. kt1 = ktime_get_ns();
  34. udelay(usecs);
  35. kt2 = ktime_get_ns();
  36. time_passed = kt2 - kt1;
  37. if (i == 0 || time_passed < min)
  38. min = time_passed;
  39. if (i == 0 || time_passed > max)
  40. max = time_passed;
  41. if ((time_passed + allowed_error_ns) / 1000 < usecs)
  42. ++fail_count;
  43. WARN_ON(time_passed < 0);
  44. sum += time_passed;
  45. }
  46. avg = sum;
  47. do_div(avg, iters);
  48. seq_printf(s, "%d usecs x %d: exp=%d allowed=%d min=%d avg=%lld max=%d",
  49. usecs, iters, usecs * 1000,
  50. (usecs * 1000) - allowed_error_ns, min, avg, max);
  51. if (fail_count)
  52. seq_printf(s, " FAIL=%d", fail_count);
  53. seq_puts(s, "\n");
  54. return 0;
  55. }
  56. static int udelay_test_show(struct seq_file *s, void *v)
  57. {
  58. int usecs;
  59. int iters;
  60. int ret = 0;
  61. mutex_lock(&udelay_test_lock);
  62. usecs = udelay_test_usecs;
  63. iters = udelay_test_iterations;
  64. mutex_unlock(&udelay_test_lock);
  65. if (usecs > 0 && iters > 0) {
  66. return udelay_test_single(s, usecs, iters);
  67. } else if (usecs == 0) {
  68. struct timespec64 ts;
  69. ktime_get_ts64(&ts);
  70. seq_printf(s, "udelay() test (lpj=%ld kt=%lld.%09ld)\n",
  71. loops_per_jiffy, (s64)ts.tv_sec, ts.tv_nsec);
  72. seq_puts(s, "usage:\n");
  73. seq_puts(s, "echo USECS [ITERS] > " DEBUGFS_FILENAME "\n");
  74. seq_puts(s, "cat " DEBUGFS_FILENAME "\n");
  75. }
  76. return ret;
  77. }
  78. static int udelay_test_open(struct inode *inode, struct file *file)
  79. {
  80. return single_open(file, udelay_test_show, inode->i_private);
  81. }
  82. static ssize_t udelay_test_write(struct file *file, const char __user *buf,
  83. size_t count, loff_t *pos)
  84. {
  85. char lbuf[32];
  86. int ret;
  87. int usecs;
  88. int iters;
  89. if (count >= sizeof(lbuf))
  90. return -EINVAL;
  91. if (copy_from_user(lbuf, buf, count))
  92. return -EFAULT;
  93. lbuf[count] = '\0';
  94. ret = sscanf(lbuf, "%d %d", &usecs, &iters);
  95. if (ret < 1)
  96. return -EINVAL;
  97. else if (ret < 2)
  98. iters = DEFAULT_ITERATIONS;
  99. mutex_lock(&udelay_test_lock);
  100. udelay_test_usecs = usecs;
  101. udelay_test_iterations = iters;
  102. mutex_unlock(&udelay_test_lock);
  103. return count;
  104. }
  105. static const struct file_operations udelay_test_debugfs_ops = {
  106. .owner = THIS_MODULE,
  107. .open = udelay_test_open,
  108. .read = seq_read,
  109. .write = udelay_test_write,
  110. .llseek = seq_lseek,
  111. .release = single_release,
  112. };
  113. static int __init udelay_test_init(void)
  114. {
  115. mutex_lock(&udelay_test_lock);
  116. debugfs_create_file(DEBUGFS_FILENAME, S_IRUSR, NULL, NULL,
  117. &udelay_test_debugfs_ops);
  118. mutex_unlock(&udelay_test_lock);
  119. return 0;
  120. }
  121. module_init(udelay_test_init);
  122. static void __exit udelay_test_exit(void)
  123. {
  124. mutex_lock(&udelay_test_lock);
  125. debugfs_lookup_and_remove(DEBUGFS_FILENAME, NULL);
  126. mutex_unlock(&udelay_test_lock);
  127. }
  128. module_exit(udelay_test_exit);
  129. MODULE_AUTHOR("David Riley <[email protected]>");
  130. MODULE_LICENSE("GPL");