spinlock_test.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/kthread.h>
  4. #include <linux/hrtimer.h>
  5. #include <linux/fs.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/export.h>
  8. #include <linux/spinlock.h>
  9. #include <asm/debug.h>
  10. static int ss_get(void *data, u64 *val)
  11. {
  12. ktime_t start, finish;
  13. int loops;
  14. int cont;
  15. DEFINE_RAW_SPINLOCK(ss_spin);
  16. loops = 1000000;
  17. cont = 1;
  18. start = ktime_get();
  19. while (cont) {
  20. raw_spin_lock(&ss_spin);
  21. loops--;
  22. if (loops == 0)
  23. cont = 0;
  24. raw_spin_unlock(&ss_spin);
  25. }
  26. finish = ktime_get();
  27. *val = ktime_us_delta(finish, start);
  28. return 0;
  29. }
  30. DEFINE_DEBUGFS_ATTRIBUTE(fops_ss, ss_get, NULL, "%llu\n");
  31. struct spin_multi_state {
  32. raw_spinlock_t lock;
  33. atomic_t start_wait;
  34. atomic_t enter_wait;
  35. atomic_t exit_wait;
  36. int loops;
  37. };
  38. struct spin_multi_per_thread {
  39. struct spin_multi_state *state;
  40. ktime_t start;
  41. };
  42. static int multi_other(void *data)
  43. {
  44. int loops;
  45. int cont;
  46. struct spin_multi_per_thread *pt = data;
  47. struct spin_multi_state *s = pt->state;
  48. loops = s->loops;
  49. cont = 1;
  50. atomic_dec(&s->enter_wait);
  51. while (atomic_read(&s->enter_wait))
  52. ; /* spin */
  53. pt->start = ktime_get();
  54. atomic_dec(&s->start_wait);
  55. while (atomic_read(&s->start_wait))
  56. ; /* spin */
  57. while (cont) {
  58. raw_spin_lock(&s->lock);
  59. loops--;
  60. if (loops == 0)
  61. cont = 0;
  62. raw_spin_unlock(&s->lock);
  63. }
  64. atomic_dec(&s->exit_wait);
  65. while (atomic_read(&s->exit_wait))
  66. ; /* spin */
  67. return 0;
  68. }
  69. static int multi_get(void *data, u64 *val)
  70. {
  71. ktime_t finish;
  72. struct spin_multi_state ms;
  73. struct spin_multi_per_thread t1, t2;
  74. ms.lock = __RAW_SPIN_LOCK_UNLOCKED("multi_get");
  75. ms.loops = 1000000;
  76. atomic_set(&ms.start_wait, 2);
  77. atomic_set(&ms.enter_wait, 2);
  78. atomic_set(&ms.exit_wait, 2);
  79. t1.state = &ms;
  80. t2.state = &ms;
  81. kthread_run(multi_other, &t2, "multi_get");
  82. multi_other(&t1);
  83. finish = ktime_get();
  84. *val = ktime_us_delta(finish, t1.start);
  85. return 0;
  86. }
  87. DEFINE_DEBUGFS_ATTRIBUTE(fops_multi, multi_get, NULL, "%llu\n");
  88. static int __init spinlock_test(void)
  89. {
  90. debugfs_create_file_unsafe("spin_single", S_IRUGO, mips_debugfs_dir, NULL,
  91. &fops_ss);
  92. debugfs_create_file_unsafe("spin_multi", S_IRUGO, mips_debugfs_dir, NULL,
  93. &fops_multi);
  94. return 0;
  95. }
  96. device_initcall(spinlock_test);