preemptirq_delay_test.c 4.6 KB

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