preemptirq_delay_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Preempt / IRQ disable delay thread to test latency tracers
  4. *
  5. * Copyright (C) 2018 Joel Fernandes (Google) <joel@joelfernandes.org>
  6. */
  7. #include <linux/trace_clock.h>
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kobject.h>
  13. #include <linux/kthread.h>
  14. #include <linux/module.h>
  15. #include <linux/printk.h>
  16. #include <linux/string.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/completion.h>
  19. static ulong delay = 100;
  20. static char test_mode[12] = "irq";
  21. static uint burst_size = 1;
  22. module_param_named(delay, delay, ulong, 0444);
  23. module_param_string(test_mode, test_mode, 12, 0444);
  24. module_param_named(burst_size, burst_size, uint, 0444);
  25. MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
  26. MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
  27. MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
  28. static struct completion done;
  29. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  30. static void busy_wait(ulong time)
  31. {
  32. u64 start, end;
  33. start = trace_clock_local();
  34. do {
  35. end = trace_clock_local();
  36. if (kthread_should_stop())
  37. break;
  38. } while ((end - start) < (time * 1000));
  39. }
  40. static __always_inline void irqoff_test(void)
  41. {
  42. unsigned long flags;
  43. local_irq_save(flags);
  44. busy_wait(delay);
  45. local_irq_restore(flags);
  46. }
  47. static __always_inline void preemptoff_test(void)
  48. {
  49. preempt_disable();
  50. busy_wait(delay);
  51. preempt_enable();
  52. }
  53. static void execute_preemptirqtest(int idx)
  54. {
  55. if (!strcmp(test_mode, "irq"))
  56. irqoff_test();
  57. else if (!strcmp(test_mode, "preempt"))
  58. preemptoff_test();
  59. else if (!strcmp(test_mode, "alternate")) {
  60. if (idx % 2 == 0)
  61. irqoff_test();
  62. else
  63. preemptoff_test();
  64. }
  65. }
  66. #define DECLARE_TESTFN(POSTFIX) \
  67. static void preemptirqtest_##POSTFIX(int idx) \
  68. { \
  69. execute_preemptirqtest(idx); \
  70. } \
  71. /*
  72. * We create 10 different functions, so that we can get 10 different
  73. * backtraces.
  74. */
  75. DECLARE_TESTFN(0)
  76. DECLARE_TESTFN(1)
  77. DECLARE_TESTFN(2)
  78. DECLARE_TESTFN(3)
  79. DECLARE_TESTFN(4)
  80. DECLARE_TESTFN(5)
  81. DECLARE_TESTFN(6)
  82. DECLARE_TESTFN(7)
  83. DECLARE_TESTFN(8)
  84. DECLARE_TESTFN(9)
  85. static void (*testfuncs[])(int) = {
  86. preemptirqtest_0,
  87. preemptirqtest_1,
  88. preemptirqtest_2,
  89. preemptirqtest_3,
  90. preemptirqtest_4,
  91. preemptirqtest_5,
  92. preemptirqtest_6,
  93. preemptirqtest_7,
  94. preemptirqtest_8,
  95. preemptirqtest_9,
  96. };
  97. #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
  98. static int preemptirq_delay_run(void *data)
  99. {
  100. int i;
  101. int s = MIN(burst_size, NR_TEST_FUNCS);
  102. for (i = 0; i < s; i++)
  103. (testfuncs[i])(i);
  104. complete(&done);
  105. set_current_state(TASK_INTERRUPTIBLE);
  106. while (!kthread_should_stop()) {
  107. schedule();
  108. set_current_state(TASK_INTERRUPTIBLE);
  109. }
  110. __set_current_state(TASK_RUNNING);
  111. return 0;
  112. }
  113. static int preemptirq_run_test(void)
  114. {
  115. struct task_struct *task;
  116. char task_name[50];
  117. init_completion(&done);
  118. snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
  119. task = kthread_run(preemptirq_delay_run, NULL, task_name);
  120. if (IS_ERR(task))
  121. return PTR_ERR(task);
  122. if (task) {
  123. wait_for_completion(&done);
  124. kthread_stop(task);
  125. }
  126. return 0;
  127. }
  128. static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
  129. const char *buf, size_t count)
  130. {
  131. ssize_t ret;
  132. ret = preemptirq_run_test();
  133. if (ret)
  134. return ret;
  135. return count;
  136. }
  137. static struct kobj_attribute trigger_attribute =
  138. __ATTR(trigger, 0200, NULL, trigger_store);
  139. static struct attribute *attrs[] = {
  140. &trigger_attribute.attr,
  141. NULL,
  142. };
  143. static struct attribute_group attr_group = {
  144. .attrs = attrs,
  145. };
  146. static struct kobject *preemptirq_delay_kobj;
  147. static int __init preemptirq_delay_init(void)
  148. {
  149. int retval;
  150. retval = preemptirq_run_test();
  151. if (retval != 0)
  152. return retval;
  153. preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
  154. kernel_kobj);
  155. if (!preemptirq_delay_kobj)
  156. return -ENOMEM;
  157. retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
  158. if (retval)
  159. kobject_put(preemptirq_delay_kobj);
  160. return retval;
  161. }
  162. static void __exit preemptirq_delay_exit(void)
  163. {
  164. kobject_put(preemptirq_delay_kobj);
  165. }
  166. module_init(preemptirq_delay_init)
  167. module_exit(preemptirq_delay_exit)
  168. MODULE_LICENSE("GPL v2");