/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/sync_point.cc
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 | | #include "yb/rocksdb/util/sync_point.h" |
22 | | #include "yb/rocksdb/port/port.h" |
23 | | #include "yb/rocksdb/util/random.h" |
24 | | |
25 | | int rocksdb_kill_odds = 0; |
26 | | std::vector<std::string> rocksdb_kill_prefix_blacklist; |
27 | | |
28 | | #ifndef NDEBUG |
29 | | namespace rocksdb { |
30 | | |
31 | | void TestKillRandom(std::string kill_point, int odds, |
32 | 0 | const std::string& srcfile, int srcline) { |
33 | 0 | for (auto& p : rocksdb_kill_prefix_blacklist) { |
34 | 0 | if (kill_point.substr(0, p.length()) == p) { |
35 | 0 | return; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | 0 | assert(odds > 0); |
40 | 0 | if (odds % 7 == 0) { |
41 | | // class Random uses multiplier 16807, which is 7^5. If odds are |
42 | | // multiplier of 7, there might be limited values generated. |
43 | 0 | odds++; |
44 | 0 | } |
45 | 0 | auto* r = Random::GetTLSInstance(); |
46 | 0 | bool crash = r->OneIn(odds); |
47 | 0 | if (crash) { |
48 | 0 | port::Crash(srcfile, srcline); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | 186M | SyncPoint* SyncPoint::GetInstance() { |
53 | 186M | static SyncPoint sync_point; |
54 | 186M | return &sync_point; |
55 | 186M | } |
56 | | |
57 | 593 | void SyncPoint::LoadDependency(const std::vector<Dependency>& dependencies) { |
58 | 593 | std::unique_lock<std::mutex> lock(mutex_); |
59 | 593 | successors_.clear(); |
60 | 593 | predecessors_.clear(); |
61 | 593 | cleared_points_.clear(); |
62 | 593 | for (const auto& dependency : dependencies) { |
63 | 82 | successors_[dependency.predecessor].push_back(dependency.successor); |
64 | 82 | predecessors_[dependency.successor].push_back(dependency.predecessor); |
65 | 82 | } |
66 | 593 | cv_.notify_all(); |
67 | 593 | } |
68 | | |
69 | 36.0M | bool SyncPoint::PredecessorsAllCleared(const std::string& point) { |
70 | 36.0M | for (const auto& pred : predecessors_[point]) { |
71 | 17.3k | if (cleared_points_.count(pred) == 0) { |
72 | 17.1k | return false; |
73 | 17.1k | } |
74 | 17.3k | } |
75 | 36.0M | return true; |
76 | 36.0M | } |
77 | | |
78 | | void SyncPoint::SetCallBack(const std::string point, |
79 | 395 | std::function<void(void*)> callback) { |
80 | 395 | std::unique_lock<std::mutex> lock(mutex_); |
81 | 395 | callbacks_[point] = callback; |
82 | 395 | } |
83 | | |
84 | 573 | void SyncPoint::ClearAllCallBacks() { |
85 | 573 | std::unique_lock<std::mutex> lock(mutex_); |
86 | 573 | while (num_callbacks_running_ > 0) { |
87 | 0 | cv_.wait(lock); |
88 | 0 | } |
89 | 573 | callbacks_.clear(); |
90 | 573 | } |
91 | | |
92 | 249 | void SyncPoint::EnableProcessing() { |
93 | 249 | std::unique_lock<std::mutex> lock(mutex_); |
94 | 249 | enabled_ = true; |
95 | 249 | } |
96 | | |
97 | 818 | void SyncPoint::DisableProcessing() { |
98 | 818 | std::unique_lock<std::mutex> lock(mutex_); |
99 | 818 | enabled_ = false; |
100 | 818 | } |
101 | | |
102 | 28 | void SyncPoint::ClearTrace() { |
103 | 28 | std::unique_lock<std::mutex> lock(mutex_); |
104 | 28 | cleared_points_.clear(); |
105 | 28 | } |
106 | | |
107 | 174M | void SyncPoint::Process(const std::string& point, void* cb_arg) { |
108 | 174M | std::unique_lock<std::mutex> lock(mutex_); |
109 | | |
110 | 174M | if (!enabled_) return138M ; |
111 | | |
112 | 35.8M | auto callback_pair = callbacks_.find(point); |
113 | 35.8M | if (callback_pair != callbacks_.end()) { |
114 | 11.0k | num_callbacks_running_++; |
115 | 11.0k | lock.unlock(); |
116 | 11.0k | callback_pair->second(cb_arg); |
117 | 11.0k | lock.lock(); |
118 | 11.0k | num_callbacks_running_--; |
119 | 11.0k | cv_.notify_all(); |
120 | 11.0k | } |
121 | | |
122 | 35.8M | while (!PredecessorsAllCleared(point)) { |
123 | 17.1k | cv_.wait(lock); |
124 | 17.1k | } |
125 | | |
126 | 35.8M | cleared_points_.insert(point); |
127 | 35.8M | cv_.notify_all(); |
128 | 35.8M | } |
129 | | } // namespace rocksdb |
130 | | #endif // NDEBUG |