spinlock_test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/module.h>
  7. #include <linux/spinlock.h>
  8. static int ss_get(void *data, u64 *val)
  9. {
  10. ktime_t start, finish;
  11. int loops;
  12. int cont;
  13. DEFINE_RAW_SPINLOCK(ss_spin);
  14. loops = 1000000;
  15. cont = 1;
  16. start = ktime_get();
  17. while (cont) {
  18. raw_spin_lock(&ss_spin);
  19. loops--;
  20. if (loops == 0)
  21. cont = 0;
  22. raw_spin_unlock(&ss_spin);
  23. }
  24. finish = ktime_get();
  25. *val = ktime_us_delta(finish, start);
  26. return 0;
  27. }
  28. DEFINE_SIMPLE_ATTRIBUTE(fops_ss, ss_get, NULL, "%llu\n");
  29. struct spin_multi_state {
  30. raw_spinlock_t lock;
  31. atomic_t start_wait;
  32. atomic_t enter_wait;
  33. atomic_t exit_wait;
  34. int loops;
  35. };
  36. struct spin_multi_per_thread {
  37. struct spin_multi_state *state;
  38. ktime_t start;
  39. };
  40. static int multi_other(void *data)
  41. {
  42. int loops;
  43. int cont;
  44. struct spin_multi_per_thread *pt = data;
  45. struct spin_multi_state *s = pt->state;
  46. loops = s->loops;
  47. cont = 1;
  48. atomic_dec(&s->enter_wait);
  49. while (atomic_read(&s->enter_wait))
  50. ; /* spin */
  51. pt->start = ktime_get();
  52. atomic_dec(&s->start_wait);
  53. while (atomic_read(&s->start_wait))
  54. ; /* spin */
  55. while (cont) {
  56. raw_spin_lock(&s->lock);
  57. loops--;
  58. if (loops == 0)
  59. cont = 0;
  60. raw_spin_unlock(&s->lock);
  61. }
  62. atomic_dec(&s->exit_wait);
  63. while (atomic_read(&s->exit_wait))
  64. ; /* spin */
  65. return 0;
  66. }
  67. static int multi_get(void *data, u64 *val)
  68. {
  69. ktime_t finish;
  70. struct spin_multi_state ms;
  71. struct spin_multi_per_thread t1, t2;
  72. ms.lock = __RAW_SPIN_LOCK_UNLOCKED("multi_get");
  73. ms.loops = 1000000;
  74. atomic_set(&ms.start_wait, 2);
  75. atomic_set(&ms.enter_wait, 2);
  76. atomic_set(&ms.exit_wait, 2);
  77. t1.state = &ms;
  78. t2.state = &ms;
  79. kthread_run(multi_other, &t2, "multi_get");
  80. multi_other(&t1);
  81. finish = ktime_get();
  82. *val = ktime_us_delta(finish, t1.start);
  83. return 0;
  84. }
  85. DEFINE_SIMPLE_ATTRIBUTE(fops_multi, multi_get, NULL, "%llu\n");
  86. extern struct dentry *mips_debugfs_dir;
  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);