preemptirq_delay_test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. cpumask_var_t cpu_mask;
  105. if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  106. return -ENOMEM;
  107. if (cpu_affinity > -1) {
  108. cpumask_clear(cpu_mask);
  109. cpumask_set_cpu(cpu_affinity, cpu_mask);
  110. if (set_cpus_allowed_ptr(current, cpu_mask))
  111. pr_err("cpu_affinity:%d, failed\n", cpu_affinity);
  112. }
  113. for (i = 0; i < s; i++)
  114. (testfuncs[i])(i);
  115. complete(&done);
  116. set_current_state(TASK_INTERRUPTIBLE);
  117. while (!kthread_should_stop()) {
  118. schedule();
  119. set_current_state(TASK_INTERRUPTIBLE);
  120. }
  121. __set_current_state(TASK_RUNNING);
  122. free_cpumask_var(cpu_mask);
  123. return 0;
  124. }
  125. static int preemptirq_run_test(void)
  126. {
  127. struct task_struct *task;
  128. char task_name[50];
  129. init_completion(&done);
  130. snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
  131. task = kthread_run(preemptirq_delay_run, NULL, task_name);
  132. if (IS_ERR(task))
  133. return PTR_ERR(task);
  134. if (task) {
  135. wait_for_completion(&done);
  136. kthread_stop(task);
  137. }
  138. return 0;
  139. }
  140. static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
  141. const char *buf, size_t count)
  142. {
  143. ssize_t ret;
  144. ret = preemptirq_run_test();
  145. if (ret)
  146. return ret;
  147. return count;
  148. }
  149. static struct kobj_attribute trigger_attribute =
  150. __ATTR(trigger, 0200, NULL, trigger_store);
  151. static struct attribute *attrs[] = {
  152. &trigger_attribute.attr,
  153. NULL,
  154. };
  155. static struct attribute_group attr_group = {
  156. .attrs = attrs,
  157. };
  158. static struct kobject *preemptirq_delay_kobj;
  159. static int __init preemptirq_delay_init(void)
  160. {
  161. int retval;
  162. retval = preemptirq_run_test();
  163. if (retval != 0)
  164. return retval;
  165. preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
  166. kernel_kobj);
  167. if (!preemptirq_delay_kobj)
  168. return -ENOMEM;
  169. retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
  170. if (retval)
  171. kobject_put(preemptirq_delay_kobj);
  172. return retval;
  173. }
  174. static void __exit preemptirq_delay_exit(void)
  175. {
  176. kobject_put(preemptirq_delay_kobj);
  177. }
  178. module_init(preemptirq_delay_init)
  179. module_exit(preemptirq_delay_exit)
  180. MODULE_DESCRIPTION("Preempt / IRQ disable delay thread to test latency tracers");
  181. MODULE_LICENSE("GPL v2");