spinlock.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/base/internal/spinlock.h"
  15. #include <algorithm>
  16. #include <atomic>
  17. #include <limits>
  18. #include "absl/base/attributes.h"
  19. #include "absl/base/internal/atomic_hook.h"
  20. #include "absl/base/internal/cycleclock.h"
  21. #include "absl/base/internal/spinlock_wait.h"
  22. #include "absl/base/internal/sysinfo.h" /* For NumCPUs() */
  23. #include "absl/base/call_once.h"
  24. // Description of lock-word:
  25. // 31..00: [............................3][2][1][0]
  26. //
  27. // [0]: kSpinLockHeld
  28. // [1]: kSpinLockCooperative
  29. // [2]: kSpinLockDisabledScheduling
  30. // [31..3]: ONLY kSpinLockSleeper OR
  31. // Wait time in cycles >> PROFILE_TIMESTAMP_SHIFT
  32. //
  33. // Detailed descriptions:
  34. //
  35. // Bit [0]: The lock is considered held iff kSpinLockHeld is set.
  36. //
  37. // Bit [1]: Eligible waiters (e.g. Fibers) may co-operatively reschedule when
  38. // contended iff kSpinLockCooperative is set.
  39. //
  40. // Bit [2]: This bit is exclusive from bit [1]. It is used only by a
  41. // non-cooperative lock. When set, indicates that scheduling was
  42. // successfully disabled when the lock was acquired. May be unset,
  43. // even if non-cooperative, if a ThreadIdentity did not yet exist at
  44. // time of acquisition.
  45. //
  46. // Bit [3]: If this is the only upper bit ([31..3]) set then this lock was
  47. // acquired without contention, however, at least one waiter exists.
  48. //
  49. // Otherwise, bits [31..3] represent the time spent by the current lock
  50. // holder to acquire the lock. There may be outstanding waiter(s).
  51. namespace absl {
  52. ABSL_NAMESPACE_BEGIN
  53. namespace base_internal {
  54. ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static base_internal::AtomicHook<void (*)(
  55. const void *lock, int64_t wait_cycles)>
  56. submit_profile_data;
  57. void RegisterSpinLockProfiler(void (*fn)(const void *contendedlock,
  58. int64_t wait_cycles)) {
  59. submit_profile_data.Store(fn);
  60. }
  61. // Static member variable definitions.
  62. constexpr uint32_t SpinLock::kSpinLockHeld;
  63. constexpr uint32_t SpinLock::kSpinLockCooperative;
  64. constexpr uint32_t SpinLock::kSpinLockDisabledScheduling;
  65. constexpr uint32_t SpinLock::kSpinLockSleeper;
  66. constexpr uint32_t SpinLock::kWaitTimeMask;
  67. // Uncommon constructors.
  68. SpinLock::SpinLock(base_internal::SchedulingMode mode)
  69. : lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {
  70. ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
  71. }
  72. // Monitor the lock to see if its value changes within some time period
  73. // (adaptive_spin_count loop iterations). The last value read from the lock
  74. // is returned from the method.
  75. uint32_t SpinLock::SpinLoop() {
  76. // We are already in the slow path of SpinLock, initialize the
  77. // adaptive_spin_count here.
  78. ABSL_CONST_INIT static absl::once_flag init_adaptive_spin_count;
  79. ABSL_CONST_INIT static int adaptive_spin_count = 0;
  80. base_internal::LowLevelCallOnce(&init_adaptive_spin_count, []() {
  81. adaptive_spin_count = base_internal::NumCPUs() > 1 ? 1000 : 1;
  82. });
  83. int c = adaptive_spin_count;
  84. uint32_t lock_value;
  85. do {
  86. lock_value = lockword_.load(std::memory_order_relaxed);
  87. } while ((lock_value & kSpinLockHeld) != 0 && --c > 0);
  88. return lock_value;
  89. }
  90. void SpinLock::SlowLock() {
  91. uint32_t lock_value = SpinLoop();
  92. lock_value = TryLockInternal(lock_value, 0);
  93. if ((lock_value & kSpinLockHeld) == 0) {
  94. return;
  95. }
  96. base_internal::SchedulingMode scheduling_mode;
  97. if ((lock_value & kSpinLockCooperative) != 0) {
  98. scheduling_mode = base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL;
  99. } else {
  100. scheduling_mode = base_internal::SCHEDULE_KERNEL_ONLY;
  101. }
  102. // The lock was not obtained initially, so this thread needs to wait for
  103. // it. Record the current timestamp in the local variable wait_start_time
  104. // so the total wait time can be stored in the lockword once this thread
  105. // obtains the lock.
  106. int64_t wait_start_time = CycleClock::Now();
  107. uint32_t wait_cycles = 0;
  108. int lock_wait_call_count = 0;
  109. while ((lock_value & kSpinLockHeld) != 0) {
  110. // If the lock is currently held, but not marked as having a sleeper, mark
  111. // it as having a sleeper.
  112. if ((lock_value & kWaitTimeMask) == 0) {
  113. // Here, just "mark" that the thread is going to sleep. Don't store the
  114. // lock wait time in the lock -- the lock word stores the amount of time
  115. // that the current holder waited before acquiring the lock, not the wait
  116. // time of any thread currently waiting to acquire it.
  117. if (lockword_.compare_exchange_strong(
  118. lock_value, lock_value | kSpinLockSleeper,
  119. std::memory_order_relaxed, std::memory_order_relaxed)) {
  120. // Successfully transitioned to kSpinLockSleeper. Pass
  121. // kSpinLockSleeper to the SpinLockWait routine to properly indicate
  122. // the last lock_value observed.
  123. lock_value |= kSpinLockSleeper;
  124. } else if ((lock_value & kSpinLockHeld) == 0) {
  125. // Lock is free again, so try and acquire it before sleeping. The
  126. // new lock state will be the number of cycles this thread waited if
  127. // this thread obtains the lock.
  128. lock_value = TryLockInternal(lock_value, wait_cycles);
  129. continue; // Skip the delay at the end of the loop.
  130. } else if ((lock_value & kWaitTimeMask) == 0) {
  131. // The lock is still held, without a waiter being marked, but something
  132. // else about the lock word changed, causing our CAS to fail. For
  133. // example, a new lock holder may have acquired the lock with
  134. // kSpinLockDisabledScheduling set, whereas the previous holder had not
  135. // set that flag. In this case, attempt again to mark ourselves as a
  136. // waiter.
  137. continue;
  138. }
  139. }
  140. // SpinLockDelay() calls into fiber scheduler, we need to see
  141. // synchronization there to avoid false positives.
  142. ABSL_TSAN_MUTEX_PRE_DIVERT(this, 0);
  143. // Wait for an OS specific delay.
  144. base_internal::SpinLockDelay(&lockword_, lock_value, ++lock_wait_call_count,
  145. scheduling_mode);
  146. ABSL_TSAN_MUTEX_POST_DIVERT(this, 0);
  147. // Spin again after returning from the wait routine to give this thread
  148. // some chance of obtaining the lock.
  149. lock_value = SpinLoop();
  150. wait_cycles = EncodeWaitCycles(wait_start_time, CycleClock::Now());
  151. lock_value = TryLockInternal(lock_value, wait_cycles);
  152. }
  153. }
  154. void SpinLock::SlowUnlock(uint32_t lock_value) {
  155. base_internal::SpinLockWake(&lockword_,
  156. false); // wake waiter if necessary
  157. // If our acquisition was contended, collect contentionz profile info. We
  158. // reserve a unitary wait time to represent that a waiter exists without our
  159. // own acquisition having been contended.
  160. if ((lock_value & kWaitTimeMask) != kSpinLockSleeper) {
  161. const uint64_t wait_cycles = DecodeWaitCycles(lock_value);
  162. ABSL_TSAN_MUTEX_PRE_DIVERT(this, 0);
  163. submit_profile_data(this, wait_cycles);
  164. ABSL_TSAN_MUTEX_POST_DIVERT(this, 0);
  165. }
  166. }
  167. // We use the upper 29 bits of the lock word to store the time spent waiting to
  168. // acquire this lock. This is reported by contentionz profiling. Since the
  169. // lower bits of the cycle counter wrap very quickly on high-frequency
  170. // processors we divide to reduce the granularity to 2^kProfileTimestampShift
  171. // sized units. On a 4Ghz machine this will lose track of wait times greater
  172. // than (2^29/4 Ghz)*128 =~ 17.2 seconds. Such waits should be extremely rare.
  173. static constexpr int kProfileTimestampShift = 7;
  174. // We currently reserve the lower 3 bits.
  175. static constexpr int kLockwordReservedShift = 3;
  176. uint32_t SpinLock::EncodeWaitCycles(int64_t wait_start_time,
  177. int64_t wait_end_time) {
  178. static const int64_t kMaxWaitTime =
  179. std::numeric_limits<uint32_t>::max() >> kLockwordReservedShift;
  180. int64_t scaled_wait_time =
  181. (wait_end_time - wait_start_time) >> kProfileTimestampShift;
  182. // Return a representation of the time spent waiting that can be stored in
  183. // the lock word's upper bits.
  184. uint32_t clamped = static_cast<uint32_t>(
  185. std::min(scaled_wait_time, kMaxWaitTime) << kLockwordReservedShift);
  186. if (clamped == 0) {
  187. return kSpinLockSleeper; // Just wake waiters, but don't record contention.
  188. }
  189. // Bump up value if necessary to avoid returning kSpinLockSleeper.
  190. const uint32_t kMinWaitTime =
  191. kSpinLockSleeper + (1 << kLockwordReservedShift);
  192. if (clamped == kSpinLockSleeper) {
  193. return kMinWaitTime;
  194. }
  195. return clamped;
  196. }
  197. uint64_t SpinLock::DecodeWaitCycles(uint32_t lock_value) {
  198. // Cast to uint32_t first to ensure bits [63:32] are cleared.
  199. const uint64_t scaled_wait_time =
  200. static_cast<uint32_t>(lock_value & kWaitTimeMask);
  201. return scaled_wait_time << (kProfileTimestampShift - kLockwordReservedShift);
  202. }
  203. } // namespace base_internal
  204. ABSL_NAMESPACE_END
  205. } // namespace absl