preemptirq_delay_test.c 4.5 KB

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