]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/monitoring/instrumented_mutex.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / monitoring / instrumented_mutex.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
5
6 #include "monitoring/instrumented_mutex.h"
7 #include "monitoring/perf_context_imp.h"
8 #include "monitoring/thread_status_util.h"
9 #include "util/sync_point.h"
10
11 namespace rocksdb {
12 namespace {
13 bool ShouldReportToStats(Env* env, Statistics* stats) {
14 return env != nullptr && stats != nullptr &&
15 stats->stats_level_ > kExceptTimeForMutex;
16 }
17 } // namespace
18
19 void InstrumentedMutex::Lock() {
20 PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(db_mutex_lock_nanos,
21 stats_code_ == DB_MUTEX_WAIT_MICROS);
22 uint64_t wait_time_micros = 0;
23 if (ShouldReportToStats(env_, stats_)) {
24 {
25 StopWatch sw(env_, nullptr, 0, &wait_time_micros);
26 LockInternal();
27 }
28 RecordTick(stats_, stats_code_, wait_time_micros);
29 } else {
30 LockInternal();
31 }
32 }
33
34 void InstrumentedMutex::LockInternal() {
35 #ifndef NDEBUG
36 ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
37 #endif
38 mutex_.Lock();
39 }
40
41 void InstrumentedCondVar::Wait() {
42 PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(db_condition_wait_nanos,
43 stats_code_ == DB_MUTEX_WAIT_MICROS);
44 uint64_t wait_time_micros = 0;
45 if (ShouldReportToStats(env_, stats_)) {
46 {
47 StopWatch sw(env_, nullptr, 0, &wait_time_micros);
48 WaitInternal();
49 }
50 RecordTick(stats_, stats_code_, wait_time_micros);
51 } else {
52 WaitInternal();
53 }
54 }
55
56 void InstrumentedCondVar::WaitInternal() {
57 #ifndef NDEBUG
58 ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
59 #endif
60 cond_.Wait();
61 }
62
63 bool InstrumentedCondVar::TimedWait(uint64_t abs_time_us) {
64 PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(db_condition_wait_nanos,
65 stats_code_ == DB_MUTEX_WAIT_MICROS);
66 uint64_t wait_time_micros = 0;
67 bool result = false;
68 if (ShouldReportToStats(env_, stats_)) {
69 {
70 StopWatch sw(env_, nullptr, 0, &wait_time_micros);
71 result = TimedWaitInternal(abs_time_us);
72 }
73 RecordTick(stats_, stats_code_, wait_time_micros);
74 } else {
75 result = TimedWaitInternal(abs_time_us);
76 }
77 return result;
78 }
79
80 bool InstrumentedCondVar::TimedWaitInternal(uint64_t abs_time_us) {
81 #ifndef NDEBUG
82 ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
83 #endif
84
85 TEST_SYNC_POINT_CALLBACK("InstrumentedCondVar::TimedWaitInternal",
86 &abs_time_us);
87
88 return cond_.TimedWait(abs_time_us);
89 }
90
91 } // namespace rocksdb