preemptirq_delay_test.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/kthread.h>
  13. #include <linux/module.h>
  14. #include <linux/printk.h>
  15. #include <linux/string.h>
  16. static ulong delay = 100;
  17. static char test_mode[10] = "irq";
  18. module_param_named(delay, delay, ulong, S_IRUGO);
  19. module_param_string(test_mode, test_mode, 10, S_IRUGO);
  20. MODULE_PARM_DESC(delay, "Period in microseconds (100 uS default)");
  21. MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt or irq (default irq)");
  22. static void busy_wait(ulong time)
  23. {
  24. u64 start, end;
  25. start = trace_clock_local();
  26. do {
  27. end = trace_clock_local();
  28. if (kthread_should_stop())
  29. break;
  30. } while ((end - start) < (time * 1000));
  31. }
  32. static int preemptirq_delay_run(void *data)
  33. {
  34. unsigned long flags;
  35. if (!strcmp(test_mode, "irq")) {
  36. local_irq_save(flags);
  37. busy_wait(delay);
  38. local_irq_restore(flags);
  39. } else if (!strcmp(test_mode, "preempt")) {
  40. preempt_disable();
  41. busy_wait(delay);
  42. preempt_enable();
  43. }
  44. return 0;
  45. }
  46. static int __init preemptirq_delay_init(void)
  47. {
  48. char task_name[50];
  49. struct task_struct *test_task;
  50. snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
  51. test_task = kthread_run(preemptirq_delay_run, NULL, task_name);
  52. return PTR_ERR_OR_ZERO(test_task);
  53. }
  54. static void __exit preemptirq_delay_exit(void)
  55. {
  56. return;
  57. }
  58. module_init(preemptirq_delay_init)
  59. module_exit(preemptirq_delay_exit)
  60. MODULE_LICENSE("GPL v2");