Coverage Report

Created: 2025-07-23 07:17

/src/rocksdb/monitoring/instrumented_mutex.h
Line
Count
Source
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
6
#pragma once
7
8
#include "monitoring/statistics_impl.h"
9
#include "port/port.h"
10
#include "rocksdb/statistics.h"
11
#include "rocksdb/system_clock.h"
12
#include "rocksdb/thread_status.h"
13
#include "util/stop_watch.h"
14
15
namespace ROCKSDB_NAMESPACE {
16
class InstrumentedCondVar;
17
18
// A wrapper class for port::Mutex that provides additional layer
19
// for collecting stats and instrumentation.
20
class InstrumentedMutex {
21
 public:
22
  explicit InstrumentedMutex(bool adaptive = false)
23
545k
      : mutex_(adaptive), stats_(nullptr), clock_(nullptr), stats_code_(0) {}
24
25
  explicit InstrumentedMutex(SystemClock* clock, bool adaptive = false)
26
2
      : mutex_(adaptive), stats_(nullptr), clock_(clock), stats_code_(0) {}
27
28
  InstrumentedMutex(Statistics* stats, SystemClock* clock, int stats_code,
29
                    bool adaptive = false)
30
58.3k
      : mutex_(adaptive),
31
58.3k
        stats_(stats),
32
58.3k
        clock_(clock),
33
58.3k
        stats_code_(stats_code) {}
34
35
#ifdef COERCE_CONTEXT_SWITCH
36
  InstrumentedMutex(Statistics* stats, SystemClock* clock, int stats_code,
37
                    InstrumentedCondVar* bg_cv, bool adaptive = false)
38
      : mutex_(adaptive),
39
        stats_(stats),
40
        clock_(clock),
41
        stats_code_(stats_code),
42
        bg_cv_(bg_cv) {}
43
#endif
44
45
  void Lock();
46
47
5.07M
  void Unlock() { mutex_.Unlock(); }
48
49
2.98M
  void AssertHeld() const { mutex_.AssertHeld(); }
50
51
 private:
52
  void LockInternal();
53
  friend class InstrumentedCondVar;
54
  port::Mutex mutex_;
55
  Statistics* stats_;
56
  SystemClock* clock_;
57
  int stats_code_;
58
#ifdef COERCE_CONTEXT_SWITCH
59
  InstrumentedCondVar* bg_cv_ = nullptr;
60
#endif
61
};
62
63
class ALIGN_AS(CACHE_LINE_SIZE) CacheAlignedInstrumentedMutex
64
    : public InstrumentedMutex {
65
  using InstrumentedMutex::InstrumentedMutex;
66
};
67
static_assert(alignof(CacheAlignedInstrumentedMutex) != CACHE_LINE_SIZE ||
68
              sizeof(CacheAlignedInstrumentedMutex) % CACHE_LINE_SIZE == 0);
69
70
// RAII wrapper for InstrumentedMutex
71
class InstrumentedMutexLock {
72
 public:
73
3.58M
  explicit InstrumentedMutexLock(InstrumentedMutex* mutex) : mutex_(mutex) {
74
3.58M
    mutex_->Lock();
75
3.58M
  }
76
77
3.58M
  ~InstrumentedMutexLock() { mutex_->Unlock(); }
78
79
 private:
80
  InstrumentedMutex* const mutex_;
81
  InstrumentedMutexLock(const InstrumentedMutexLock&) = delete;
82
  void operator=(const InstrumentedMutexLock&) = delete;
83
};
84
85
// RAII wrapper for temporary releasing InstrumentedMutex inside
86
// InstrumentedMutexLock
87
class InstrumentedMutexUnlock {
88
 public:
89
512
  explicit InstrumentedMutexUnlock(InstrumentedMutex* mutex) : mutex_(mutex) {
90
512
    mutex_->Unlock();
91
512
  }
92
93
512
  ~InstrumentedMutexUnlock() { mutex_->Lock(); }
94
95
 private:
96
  InstrumentedMutex* const mutex_;
97
  InstrumentedMutexUnlock(const InstrumentedMutexUnlock&) = delete;
98
  void operator=(const InstrumentedMutexUnlock&) = delete;
99
};
100
101
class InstrumentedCondVar {
102
 public:
103
  explicit InstrumentedCondVar(InstrumentedMutex* instrumented_mutex)
104
435k
      : cond_(&(instrumented_mutex->mutex_)),
105
435k
        stats_(instrumented_mutex->stats_),
106
435k
        clock_(instrumented_mutex->clock_),
107
435k
        stats_code_(instrumented_mutex->stats_code_) {}
108
109
  void Wait();
110
111
  bool TimedWait(uint64_t abs_time_us);
112
113
1.07k
  void Signal() { cond_.Signal(); }
114
115
596k
  void SignalAll() { cond_.SignalAll(); }
116
117
 private:
118
  void WaitInternal();
119
  bool TimedWaitInternal(uint64_t abs_time_us);
120
  port::CondVar cond_;
121
  Statistics* stats_;
122
  SystemClock* clock_;
123
  int stats_code_;
124
};
125
126
}  // namespace ROCKSDB_NAMESPACE