test_udelay.c 4.1 KB

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