|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "inject_pause.h" |
| 18 | + |
| 19 | +#include <folly/Indestructible.h> |
| 20 | +#include <folly/Synchronized.h> |
| 21 | +#include <folly/fibers/Baton.h> |
| 22 | +#include <folly/logging/xlog.h> |
| 23 | + |
| 24 | +#include <condition_variable> |
| 25 | +#include <deque> |
| 26 | +#include <mutex> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +namespace facebook { |
| 30 | +namespace cachelib { |
| 31 | + |
| 32 | +namespace { |
| 33 | +struct BatonSet { |
| 34 | + std::deque<folly::fibers::Baton*> batons; |
| 35 | + std::condition_variable cv; |
| 36 | + bool enabled{false}; |
| 37 | + size_t limit{0}; |
| 38 | + std::shared_ptr<PauseCallback> callback; |
| 39 | +}; |
| 40 | + |
| 41 | +using PausePointsMap = |
| 42 | + folly::Synchronized<std::unordered_map<std::string, BatonSet>, std::mutex>; |
| 43 | +PausePointsMap& pausePoints() { |
| 44 | + static folly::Indestructible<PausePointsMap> pausePoints; |
| 45 | + return *pausePoints; |
| 46 | +} |
| 47 | + |
| 48 | +size_t wakeupBatonSet(BatonSet& batonSet, size_t numThreads) { |
| 49 | + size_t numWaked = 0; |
| 50 | + if (numThreads == 0) { |
| 51 | + numThreads = batonSet.batons.size(); |
| 52 | + } |
| 53 | + |
| 54 | + while (!batonSet.batons.empty() && numWaked < numThreads) { |
| 55 | + auto* b = batonSet.batons.front(); |
| 56 | + batonSet.batons.pop_front(); |
| 57 | + b->post(); |
| 58 | + numWaked++; |
| 59 | + } |
| 60 | + |
| 61 | + return numWaked; |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace |
| 65 | + |
| 66 | +bool& injectPauseEnabled() { |
| 67 | + static bool enabled = false; |
| 68 | + return enabled; |
| 69 | +} |
| 70 | + |
| 71 | +// Flag controls the debug logging |
| 72 | +bool& injectPauseLogEnabled() { |
| 73 | + static bool enabled = false; |
| 74 | + return enabled; |
| 75 | +} |
| 76 | + |
| 77 | +namespace detail { |
| 78 | + |
| 79 | +void injectPause(folly::StringPiece name) { |
| 80 | + if (!injectPauseEnabled()) { |
| 81 | + return; |
| 82 | + } |
| 83 | + folly::fibers::Baton baton; |
| 84 | + std::shared_ptr<PauseCallback> callback; |
| 85 | + { |
| 86 | + auto ptr = pausePoints().lock(); |
| 87 | + auto it = ptr->find(name.str()); |
| 88 | + if (it == ptr->end() || !it->second.enabled || |
| 89 | + (it->second.limit > 0 && |
| 90 | + it->second.batons.size() == it->second.limit)) { |
| 91 | + if (injectPauseLogEnabled()) { |
| 92 | + XLOGF(ERR, "[{}] injectPause not set", name); |
| 93 | + } |
| 94 | + return; |
| 95 | + } |
| 96 | + if (injectPauseLogEnabled()) { |
| 97 | + XLOGF(ERR, "[{}] injectPause begin", name); |
| 98 | + } |
| 99 | + callback = it->second.callback; |
| 100 | + if (!callback) { |
| 101 | + it->second.batons.push_back(&baton); |
| 102 | + it->second.cv.notify_one(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (callback) { |
| 107 | + (*callback)(); |
| 108 | + } else { |
| 109 | + baton.wait(); |
| 110 | + } |
| 111 | + |
| 112 | + /* Avoid potential protect-its-own-lifetime bug: |
| 113 | + Wait for the post() to finish before destroying the Baton. */ |
| 114 | + auto ptr = pausePoints().lock(); |
| 115 | + if (injectPauseLogEnabled()) { |
| 116 | + XLOGF(ERR, "[{}] injectPause end", name); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace detail |
| 121 | + |
| 122 | +void injectPauseSet(folly::StringPiece name, size_t numThreads) { |
| 123 | + if (!injectPauseEnabled()) { |
| 124 | + return; |
| 125 | + } |
| 126 | + auto ptr = pausePoints().lock(); |
| 127 | + if (injectPauseLogEnabled()) { |
| 128 | + XLOGF(ERR, "[{}] injectPauseSet threads {}", name, numThreads); |
| 129 | + } |
| 130 | + auto res = ptr->emplace( |
| 131 | + std::piecewise_construct, std::make_tuple(name.str()), std::make_tuple()); |
| 132 | + res.first->second.limit = numThreads; |
| 133 | + res.first->second.enabled = true; |
| 134 | +} |
| 135 | + |
| 136 | +void injectPauseSet(folly::StringPiece name, PauseCallback&& callback) { |
| 137 | + if (!injectPauseEnabled()) { |
| 138 | + return; |
| 139 | + } |
| 140 | + auto ptr = pausePoints().lock(); |
| 141 | + if (injectPauseLogEnabled()) { |
| 142 | + XLOGF(ERR, "[{}] injectPauseSet callback", name); |
| 143 | + } |
| 144 | + auto res = ptr->emplace( |
| 145 | + std::piecewise_construct, std::make_tuple(name.str()), std::make_tuple()); |
| 146 | + res.first->second.limit = 0; |
| 147 | + res.first->second.enabled = true; |
| 148 | + res.first->second.callback = |
| 149 | + std::make_shared<PauseCallback>(std::move(callback)); |
| 150 | +} |
| 151 | + |
| 152 | +bool injectPauseWait(folly::StringPiece name, |
| 153 | + size_t numThreads, |
| 154 | + bool wakeup, |
| 155 | + uint32_t timeoutMs) { |
| 156 | + if (!injectPauseEnabled() || !numThreads) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + auto ptr = pausePoints().lock(); |
| 160 | + if (injectPauseLogEnabled()) { |
| 161 | + XLOGF(ERR, "[{}] injectPauseWait begin {}", name, numThreads); |
| 162 | + } |
| 163 | + auto it = ptr->find(name.str()); |
| 164 | + if (it == ptr->end() || !it->second.enabled) { |
| 165 | + if (injectPauseLogEnabled()) { |
| 166 | + XLOGF(ERR, "[{}] injectPauseWait: ERROR not set", name); |
| 167 | + } |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + if (!timeoutMs || timeoutMs > kInjectPauseMaxWaitTimeoutMs) { |
| 172 | + timeoutMs = kInjectPauseMaxWaitTimeoutMs; |
| 173 | + } |
| 174 | + |
| 175 | + auto& batonSet = it->second; |
| 176 | + bool status = batonSet.cv.wait_for( |
| 177 | + ptr.as_lock(), |
| 178 | + std::chrono::milliseconds(timeoutMs), |
| 179 | + [&batonSet, numThreads]() { |
| 180 | + return !batonSet.enabled || batonSet.batons.size() >= numThreads; |
| 181 | + }); |
| 182 | + |
| 183 | + if (status && wakeup) { |
| 184 | + wakeupBatonSet(batonSet, numThreads); |
| 185 | + } |
| 186 | + |
| 187 | + if (injectPauseLogEnabled()) { |
| 188 | + std::string errStr; |
| 189 | + if (!status) { |
| 190 | + errStr = fmt::format(" with ERR (paused {})", batonSet.batons.size()); |
| 191 | + } |
| 192 | + XLOGF(ERR, "[{}] injectPauseWait end {}", name, errStr); |
| 193 | + } |
| 194 | + return status; |
| 195 | +} |
| 196 | + |
| 197 | +size_t injectPauseClear(folly::StringPiece name) { |
| 198 | + if (!injectPauseEnabled()) { |
| 199 | + return false; |
| 200 | + } |
| 201 | + |
| 202 | + size_t numPaused = 0; |
| 203 | + auto ptr = pausePoints().lock(); |
| 204 | + if (injectPauseLogEnabled()) { |
| 205 | + XLOGF(ERR, "[{}] injectPauseClear ", name); |
| 206 | + } |
| 207 | + |
| 208 | + if (name.empty()) { |
| 209 | + for (auto& it : *ptr) { |
| 210 | + auto& batonSet = it.second; |
| 211 | + numPaused += wakeupBatonSet(batonSet, 0); |
| 212 | + batonSet.enabled = false; |
| 213 | + batonSet.cv.notify_all(); |
| 214 | + } |
| 215 | + } else { |
| 216 | + auto it = ptr->find(name.str()); |
| 217 | + if (it != ptr->end()) { |
| 218 | + auto& batonSet = it->second; |
| 219 | + numPaused += wakeupBatonSet(batonSet, 0) > 0 ? 1 : 0; |
| 220 | + batonSet.enabled = false; |
| 221 | + batonSet.cv.notify_all(); |
| 222 | + } |
| 223 | + } |
| 224 | + return numPaused; |
| 225 | +} |
| 226 | + |
| 227 | +} // namespace cachelib |
| 228 | +} // namespace facebook |
0 commit comments