test_udelay.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 struct dentry *udelay_test_debugfs_file;
  21. static int udelay_test_usecs;
  22. static int udelay_test_iterations = DEFAULT_ITERATIONS;
  23. static int udelay_test_single(struct seq_file *s, int usecs, uint32_t iters)
  24. {
  25. int min = 0, max = 0, fail_count = 0;
  26. uint64_t sum = 0;
  27. uint64_t avg;
  28. int i;
  29. /* Allow udelay to be up to 0.5% fast */
  30. int allowed_error_ns = usecs * 5;
  31. for (i = 0; i < iters; ++i) {
  32. s64 kt1, kt2;
  33. int time_passed;
  34. kt1 = ktime_get_ns();
  35. udelay(usecs);
  36. kt2 = ktime_get_ns();
  37. time_passed = kt2 - kt1;
  38. if (i == 0 || time_passed < min)
  39. min = time_passed;
  40. if (i == 0 || time_passed > max)
  41. max = time_passed;
  42. if ((time_passed + allowed_error_ns) / 1000 < usecs)
  43. ++fail_count;
  44. WARN_ON(time_passed < 0);
  45. sum += time_passed;
  46. }
  47. avg = sum;
  48. do_div(avg, iters);
  49. seq_printf(s, "%d usecs x %d: exp=%d allowed=%d min=%d avg=%lld max=%d",
  50. usecs, iters, usecs * 1000,
  51. (usecs * 1000) - allowed_error_ns, min, avg, max);
  52. if (fail_count)
  53. seq_printf(s, " FAIL=%d", fail_count);
  54. seq_puts(s, "\n");
  55. return 0;
  56. }
  57. static int udelay_test_show(struct seq_file *s, void *v)
  58. {
  59. int usecs;
  60. int iters;
  61. int ret = 0;
  62. mutex_lock(&udelay_test_lock);
  63. usecs = udelay_test_usecs;
  64. iters = udelay_test_iterations;
  65. mutex_unlock(&udelay_test_lock);
  66. if (usecs > 0 && iters > 0) {
  67. return udelay_test_single(s, usecs, iters);
  68. } else if (usecs == 0) {
  69. struct timespec64 ts;
  70. ktime_get_ts64(&ts);
  71. seq_printf(s, "udelay() test (lpj=%ld kt=%lld.%09ld)\n",
  72. loops_per_jiffy, (s64)ts.tv_sec, ts.tv_nsec);
  73. seq_puts(s, "usage:\n");
  74. seq_puts(s, "echo USECS [ITERS] > " DEBUGFS_FILENAME "\n");
  75. seq_puts(s, "cat " DEBUGFS_FILENAME "\n");
  76. }
  77. return ret;
  78. }
  79. static int udelay_test_open(struct inode *inode, struct file *file)
  80. {
  81. return single_open(file, udelay_test_show, inode->i_private);
  82. }
  83. static ssize_t udelay_test_write(struct file *file, const char __user *buf,
  84. size_t count, loff_t *pos)
  85. {
  86. char lbuf[32];
  87. int ret;
  88. int usecs;
  89. int iters;
  90. if (count >= sizeof(lbuf))
  91. return -EINVAL;
  92. if (copy_from_user(lbuf, buf, count))
  93. return -EFAULT;
  94. lbuf[count] = '\0';
  95. ret = sscanf(lbuf, "%d %d", &usecs, &iters);
  96. if (ret < 1)
  97. return -EINVAL;
  98. else if (ret < 2)
  99. iters = DEFAULT_ITERATIONS;
  100. mutex_lock(&udelay_test_lock);
  101. udelay_test_usecs = usecs;
  102. udelay_test_iterations = iters;
  103. mutex_unlock(&udelay_test_lock);
  104. return count;
  105. }
  106. static const struct file_operations udelay_test_debugfs_ops = {
  107. .owner = THIS_MODULE,
  108. .open = udelay_test_open,
  109. .read = seq_read,
  110. .write = udelay_test_write,
  111. .llseek = seq_lseek,
  112. .release = single_release,
  113. };
  114. static int __init udelay_test_init(void)
  115. {
  116. mutex_lock(&udelay_test_lock);
  117. udelay_test_debugfs_file = debugfs_create_file(DEBUGFS_FILENAME,
  118. S_IRUSR, NULL, NULL, &udelay_test_debugfs_ops);
  119. mutex_unlock(&udelay_test_lock);
  120. return 0;
  121. }
  122. module_init(udelay_test_init);
  123. static void __exit udelay_test_exit(void)
  124. {
  125. mutex_lock(&udelay_test_lock);
  126. debugfs_remove(udelay_test_debugfs_file);
  127. mutex_unlock(&udelay_test_lock);
  128. }
  129. module_exit(udelay_test_exit);
  130. MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
  131. MODULE_LICENSE("GPL");