instrumented_mutex.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "test_util/sync_point.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. namespace {
  11. Statistics* stats_for_report(Env* env, Statistics* stats) {
  12. if (env != nullptr && stats != nullptr &&
  13. stats->get_stats_level() > kExceptTimeForMutex) {
  14. return stats;
  15. } else {
  16. return nullptr;
  17. }
  18. }
  19. } // namespace
  20. void InstrumentedMutex::Lock() {
  21. PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
  22. db_mutex_lock_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
  23. stats_for_report(env_, stats_), stats_code_);
  24. LockInternal();
  25. }
  26. void InstrumentedMutex::LockInternal() {
  27. #ifndef NDEBUG
  28. ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
  29. #endif
  30. mutex_.Lock();
  31. }
  32. void InstrumentedCondVar::Wait() {
  33. PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
  34. db_condition_wait_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
  35. stats_for_report(env_, stats_), stats_code_);
  36. WaitInternal();
  37. }
  38. void InstrumentedCondVar::WaitInternal() {
  39. #ifndef NDEBUG
  40. ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
  41. #endif
  42. cond_.Wait();
  43. }
  44. bool InstrumentedCondVar::TimedWait(uint64_t abs_time_us) {
  45. PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
  46. db_condition_wait_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
  47. stats_for_report(env_, stats_), stats_code_);
  48. return TimedWaitInternal(abs_time_us);
  49. }
  50. bool InstrumentedCondVar::TimedWaitInternal(uint64_t abs_time_us) {
  51. #ifndef NDEBUG
  52. ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
  53. #endif
  54. TEST_SYNC_POINT_CALLBACK("InstrumentedCondVar::TimedWaitInternal",
  55. &abs_time_us);
  56. return cond_.TimedWait(abs_time_us);
  57. }
  58. } // namespace ROCKSDB_NAMESPACE