spinlock_test.c 2.3 KB

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