instrumented_mutex.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "monitoring/instrumented_mutex.h"
  6. #include "monitoring/perf_context_imp.h"
  7. #include "monitoring/thread_status_util.h"
  8. #include "rocksdb/system_clock.h"
  9. #include "test_util/sync_point.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. namespace {
  12. #ifndef NPERF_CONTEXT
  13. Statistics* stats_for_report(SystemClock* clock, Statistics* stats) {
  14. if (clock != nullptr && stats != nullptr &&
  15. stats->get_stats_level() > kExceptTimeForMutex) {
  16. return stats;
  17. } else {
  18. return nullptr;
  19. }
  20. }
  21. #endif // NPERF_CONTEXT
  22. } // namespace
  23. void InstrumentedMutex::Lock() {
  24. PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
  25. db_mutex_lock_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
  26. stats_for_report(clock_, stats_), stats_code_);
  27. LockInternal();
  28. }
  29. void InstrumentedMutex::LockInternal() {
  30. #ifndef NDEBUG
  31. ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
  32. #endif
  33. #ifdef COERCE_CONTEXT_SWITCH
  34. if (stats_code_ == DB_MUTEX_WAIT_MICROS) {
  35. thread_local Random rnd(301);
  36. if (rnd.OneIn(2)) {
  37. if (bg_cv_) {
  38. bg_cv_->SignalAll();
  39. }
  40. sched_yield();
  41. } else {
  42. uint32_t sleep_us = rnd.Uniform(11) * 1000;
  43. if (bg_cv_) {
  44. bg_cv_->SignalAll();
  45. }
  46. SystemClock::Default()->SleepForMicroseconds(sleep_us);
  47. }
  48. }
  49. #endif
  50. mutex_.Lock();
  51. }
  52. void InstrumentedCondVar::Wait() {
  53. PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
  54. db_condition_wait_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
  55. stats_for_report(clock_, stats_), stats_code_);
  56. WaitInternal();
  57. }
  58. void InstrumentedCondVar::WaitInternal() {
  59. #ifndef NDEBUG
  60. ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
  61. #endif
  62. cond_.Wait();
  63. }
  64. bool InstrumentedCondVar::TimedWait(uint64_t abs_time_us) {
  65. PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
  66. db_condition_wait_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
  67. stats_for_report(clock_, stats_), stats_code_);
  68. return TimedWaitInternal(abs_time_us);
  69. }
  70. bool InstrumentedCondVar::TimedWaitInternal(uint64_t abs_time_us) {
  71. #ifndef NDEBUG
  72. ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
  73. #endif
  74. TEST_SYNC_POINT_CALLBACK("InstrumentedCondVar::TimedWaitInternal",
  75. &abs_time_us);
  76. return cond_.TimedWait(abs_time_us);
  77. }
  78. } // namespace ROCKSDB_NAMESPACE