YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/instrumented_mutex.h
Line
Count
Source (jump to first uncovered line)
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
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#ifndef YB_ROCKSDB_UTIL_INSTRUMENTED_MUTEX_H
22
#define YB_ROCKSDB_UTIL_INSTRUMENTED_MUTEX_H
23
24
#pragma once
25
26
#include "yb/rocksdb/env.h"
27
#include "yb/rocksdb/port/port.h"
28
#include "yb/rocksdb/statistics.h"
29
30
namespace rocksdb {
31
class InstrumentedCondVar;
32
33
// A wrapper class for port::Mutex that provides additional layer
34
// for collecting stats and instrumentation.
35
class InstrumentedMutex {
36
 public:
37
  explicit InstrumentedMutex(bool adaptive = false)
38
      : mutex_(adaptive), stats_(nullptr), env_(nullptr),
39
437k
        stats_code_(0) {}
40
41
  InstrumentedMutex(
42
      Statistics* stats, Env* env,
43
      int stats_code, bool adaptive = false)
44
      : mutex_(adaptive), stats_(stats), env_(env),
45
435k
        stats_code_(stats_code) {}
46
47
  void Lock();
48
49
199M
  void Unlock() {
50
199M
    mutex_.Unlock();
51
199M
  }
52
53
10.7M
  void AssertHeld() {
54
10.7M
    mutex_.AssertHeld();
55
10.7M
  }
56
57
  // For compatibility with std::lock_guard.
58
0
  void lock() { Lock(); }
59
0
  void unlock() { Unlock(); }
60
61
 private:
62
  void LockInternal();
63
  friend class InstrumentedCondVar;
64
  port::Mutex mutex_;
65
  Statistics* stats_;
66
  Env* env_;
67
  int stats_code_;
68
};
69
70
// A wrapper class for port::Mutex that provides additional layer
71
// for collecting stats and instrumentation.
72
class InstrumentedMutexLock {
73
 public:
74
161M
  explicit InstrumentedMutexLock(InstrumentedMutex* mutex) : mutex_(mutex) {
75
161M
    mutex_->Lock();
76
161M
  }
77
78
161M
  ~InstrumentedMutexLock() {
79
161M
    mutex_->Unlock();
80
161M
  }
81
82
 private:
83
  InstrumentedMutex* const mutex_;
84
  InstrumentedMutexLock(const InstrumentedMutexLock&) = delete;
85
  void operator=(const InstrumentedMutexLock&) = delete;
86
};
87
88
class InstrumentedCondVar {
89
 public:
90
  explicit InstrumentedCondVar(InstrumentedMutex* instrumented_mutex)
91
      : cond_(&(instrumented_mutex->mutex_)),
92
        stats_(instrumented_mutex->stats_),
93
        env_(instrumented_mutex->env_),
94
1.20M
        stats_code_(instrumented_mutex->stats_code_) {}
95
96
  void Wait();
97
98
  bool TimedWait(uint64_t abs_time_us);
99
100
1.31k
  void Signal() {
101
1.31k
    cond_.Signal();
102
1.31k
  }
103
104
486k
  void SignalAll() {
105
486k
    cond_.SignalAll();
106
486k
  }
107
108
 private:
109
  void WaitInternal();
110
  bool TimedWaitInternal(uint64_t abs_time_us);
111
  port::CondVar cond_;
112
  Statistics* stats_;
113
  Env* env_;
114
  int stats_code_;
115
};
116
117
}  // namespace rocksdb
118
119
#endif // YB_ROCKSDB_UTIL_INSTRUMENTED_MUTEX_H