2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/FunctionScheduler.h>
21 #include <folly/Conv.h>
22 #include <folly/Random.h>
23 #include <folly/String.h>
24 #include <folly/ThreadName.h>
26 using std::chrono::milliseconds;
27 using std::chrono::steady_clock;
33 struct ConstIntervalFunctor {
34 const milliseconds constInterval;
36 explicit ConstIntervalFunctor(milliseconds interval)
37 : constInterval(interval) {
38 if (interval < milliseconds::zero()) {
39 throw std::invalid_argument(
41 "time interval must be non-negative");
45 milliseconds operator()() const { return constInterval; }
48 struct PoissonDistributionFunctor {
49 std::default_random_engine generator;
50 std::poisson_distribution<int> poissonRandom;
52 explicit PoissonDistributionFunctor(double meanPoissonMs)
53 : poissonRandom(meanPoissonMs) {
54 if (meanPoissonMs < 0.0) {
55 throw std::invalid_argument(
57 "Poisson mean interval must be non-negative");
61 milliseconds operator()() { return milliseconds(poissonRandom(generator)); }
64 struct UniformDistributionFunctor {
65 std::default_random_engine generator;
66 std::uniform_int_distribution<milliseconds::rep> dist;
68 UniformDistributionFunctor(milliseconds minInterval, milliseconds maxInterval)
69 : generator(Random::rand32()),
70 dist(minInterval.count(), maxInterval.count()) {
71 if (minInterval > maxInterval) {
72 throw std::invalid_argument(
74 "min time interval must be less or equal than max interval");
76 if (minInterval < milliseconds::zero()) {
77 throw std::invalid_argument(
79 "time interval must be non-negative");
83 milliseconds operator()() { return milliseconds(dist(generator)); }
86 } // anonymous namespace
88 FunctionScheduler::FunctionScheduler() {}
90 FunctionScheduler::~FunctionScheduler() {
91 // make sure to stop the thread (if running)
95 void FunctionScheduler::addFunction(Function<void()>&& cb,
96 milliseconds interval,
98 milliseconds startDelay) {
101 ConstIntervalFunctor(interval),
103 to<std::string>(interval.count(), "ms"),
108 void FunctionScheduler::addFunction(Function<void()>&& cb,
109 milliseconds interval,
110 const LatencyDistribution& latencyDistr,
112 milliseconds startDelay) {
113 if (latencyDistr.isPoisson) {
116 PoissonDistributionFunctor(latencyDistr.poissonMean),
118 to<std::string>(latencyDistr.poissonMean, "ms (Poisson mean)"),
122 addFunction(std::move(cb), interval, nameID, startDelay);
126 void FunctionScheduler::addFunctionOnce(
127 Function<void()>&& cb,
129 milliseconds startDelay) {
132 ConstIntervalFunctor(milliseconds::zero()),
139 void FunctionScheduler::addFunctionUniformDistribution(
140 Function<void()>&& cb,
141 milliseconds minInterval,
142 milliseconds maxInterval,
144 milliseconds startDelay) {
147 UniformDistributionFunctor(minInterval, maxInterval),
150 "[", minInterval.count(), " , ", maxInterval.count(), "] ms"),
155 void FunctionScheduler::addFunctionGenericDistribution(
156 Function<void()>&& cb,
157 IntervalDistributionFunc&& intervalFunc,
158 const std::string& nameID,
159 const std::string& intervalDescr,
160 milliseconds startDelay) {
163 std::move(intervalFunc),
170 void FunctionScheduler::addFunctionInternal(
171 Function<void()>&& cb,
172 IntervalDistributionFunc&& intervalFunc,
173 const std::string& nameID,
174 const std::string& intervalDescr,
175 milliseconds startDelay,
178 throw std::invalid_argument(
179 "FunctionScheduler: Scheduled function must be set");
182 throw std::invalid_argument(
183 "FunctionScheduler: interval distribution function must be set");
185 if (startDelay < milliseconds::zero()) {
186 throw std::invalid_argument(
187 "FunctionScheduler: start delay must be non-negative");
190 std::unique_lock<std::mutex> l(mutex_);
191 // check if the nameID is unique
192 for (const auto& f : functions_) {
193 if (f.isValid() && f.name == nameID) {
194 throw std::invalid_argument(
195 to<std::string>("FunctionScheduler: a function named \"",
197 "\" already exists"));
200 if (currentFunction_ && currentFunction_->name == nameID) {
201 throw std::invalid_argument(to<std::string>(
202 "FunctionScheduler: a function named \"", nameID, "\" already exists"));
209 std::move(intervalFunc),
216 bool FunctionScheduler::cancelFunctionWithLock(
217 std::unique_lock<std::mutex>& lock,
218 StringPiece nameID) {
219 CHECK_EQ(lock.owns_lock(), true);
220 if (currentFunction_ && currentFunction_->name == nameID) {
221 // This function is currently being run. Clear currentFunction_
222 // The running thread will see this and won't reschedule the function.
223 currentFunction_ = nullptr;
224 cancellingCurrentFunction_ = true;
230 bool FunctionScheduler::cancelFunction(StringPiece nameID) {
231 std::unique_lock<std::mutex> l(mutex_);
233 if (cancelFunctionWithLock(l, nameID)) {
237 for (auto it = functions_.begin(); it != functions_.end(); ++it) {
238 if (it->isValid() && it->name == nameID) {
239 cancelFunction(l, it);
246 bool FunctionScheduler::cancelFunctionAndWait(StringPiece nameID) {
247 std::unique_lock<std::mutex> l(mutex_);
249 if (cancelFunctionWithLock(l, nameID)) {
250 runningCondvar_.wait(l, [this]() { return !cancellingCurrentFunction_; });
254 for (auto it = functions_.begin(); it != functions_.end(); ++it) {
255 if (it->isValid() && it->name == nameID) {
256 cancelFunction(l, it);
263 void FunctionScheduler::cancelFunction(const std::unique_lock<std::mutex>& l,
264 FunctionHeap::iterator it) {
265 // This function should only be called with mutex_ already locked.
266 DCHECK(l.mutex() == &mutex_);
267 DCHECK(l.owns_lock());
270 // Internally gcc has an __adjust_heap() function to fill in a hole in the
271 // heap. Unfortunately it isn't part of the standard API.
273 // For now we just leave the RepeatFunc in our heap, but mark it as unused.
274 // When its nextTimeInterval comes up, the runner thread will pop it from
275 // the heap and simply throw it away.
278 // We're not running, so functions_ doesn't need to be maintained in heap
280 functions_.erase(it);
284 bool FunctionScheduler::cancelAllFunctionsWithLock(
285 std::unique_lock<std::mutex>& lock) {
286 CHECK_EQ(lock.owns_lock(), true);
288 if (currentFunction_) {
289 cancellingCurrentFunction_ = true;
291 currentFunction_ = nullptr;
292 return cancellingCurrentFunction_;
295 void FunctionScheduler::cancelAllFunctions() {
296 std::unique_lock<std::mutex> l(mutex_);
297 cancelAllFunctionsWithLock(l);
300 void FunctionScheduler::cancelAllFunctionsAndWait() {
301 std::unique_lock<std::mutex> l(mutex_);
302 if (cancelAllFunctionsWithLock(l)) {
303 runningCondvar_.wait(l, [this]() { return !cancellingCurrentFunction_; });
307 bool FunctionScheduler::resetFunctionTimer(StringPiece nameID) {
308 std::unique_lock<std::mutex> l(mutex_);
309 if (currentFunction_ && currentFunction_->name == nameID) {
310 RepeatFunc* funcPtrCopy = currentFunction_;
311 // This function is currently being run. Clear currentFunction_
312 // to avoid rescheduling it, and add the function again to honor the
314 currentFunction_ = nullptr;
315 addFunctionToHeap(l, std::move(*funcPtrCopy));
319 // Since __adjust_heap() isn't a part of the standard API, there's no way to
320 // fix the heap ordering if we adjust the key (nextRunTime) for the existing
321 // RepeatFunc. Instead, we just cancel it and add an identical object.
322 for (auto it = functions_.begin(); it != functions_.end(); ++it) {
323 if (it->isValid() && it->name == nameID) {
324 RepeatFunc funcCopy(std::move(*it));
325 cancelFunction(l, it);
326 addFunctionToHeap(l, std::move(funcCopy));
333 bool FunctionScheduler::start() {
334 std::unique_lock<std::mutex> l(mutex_);
339 VLOG(1) << "Starting FunctionScheduler with " << functions_.size()
341 auto now = steady_clock::now();
342 // Reset the next run time. for all functions.
343 // note: this is needed since one can shutdown() and start() again
344 for (auto& f : functions_) {
345 f.resetNextRunTime(now);
346 VLOG(1) << " - func: " << (f.name.empty() ? "(anon)" : f.name.c_str())
347 << ", period = " << f.intervalDescr
348 << ", delay = " << f.startDelay.count() << "ms";
350 std::make_heap(functions_.begin(), functions_.end(), fnCmp_);
352 thread_ = std::thread([&] { this->run(); });
358 bool FunctionScheduler::shutdown() {
360 std::lock_guard<std::mutex> g(mutex_);
366 runningCondvar_.notify_one();
372 void FunctionScheduler::run() {
373 std::unique_lock<std::mutex> lock(mutex_);
375 if (!threadName_.empty()) {
376 folly::setThreadName(threadName_);
380 // If we have nothing to run, wait until a function is added or until we
382 if (functions_.empty()) {
383 runningCondvar_.wait(lock);
387 auto now = steady_clock::now();
389 // Move the next function to run to the end of functions_
390 std::pop_heap(functions_.begin(), functions_.end(), fnCmp_);
392 // Check to see if the function was cancelled.
393 // If so, just remove it and continue around the loop.
394 if (!functions_.back().isValid()) {
395 functions_.pop_back();
399 auto sleepTime = functions_.back().getNextRunTime() - now;
400 if (sleepTime < milliseconds::zero()) {
401 // We need to run this function now
402 runOneFunction(lock, now);
403 runningCondvar_.notify_all();
405 // Re-add the function to the heap, and wait until we actually
407 std::push_heap(functions_.begin(), functions_.end(), fnCmp_);
408 runningCondvar_.wait_for(lock, sleepTime);
413 void FunctionScheduler::runOneFunction(std::unique_lock<std::mutex>& lock,
414 steady_clock::time_point now) {
415 DCHECK(lock.mutex() == &mutex_);
416 DCHECK(lock.owns_lock());
418 // The function to run will be at the end of functions_ already.
420 // Fully remove it from functions_ now.
421 // We need to release mutex_ while we invoke this function, and we need to
422 // maintain the heap property on functions_ while mutex_ is unlocked.
423 RepeatFunc func(std::move(functions_.back()));
424 functions_.pop_back();
426 VLOG(5) << func.name << "function has been canceled while waiting";
429 currentFunction_ = &func;
431 // Update the function's next run time.
433 // This allows scheduler to catch up
434 func.setNextRunTimeSteady();
436 // Note that we set nextRunTime based on the current time where we started
437 // the function call, rather than the time when the function finishes.
438 // This ensures that we call the function once every time interval, as
439 // opposed to waiting time interval seconds between calls. (These can be
440 // different if the function takes a significant amount of time to run.)
441 func.setNextRunTimeStrict(now);
444 // Release the lock while we invoke the user's function
447 // Invoke the function
449 VLOG(5) << "Now running " << func.name;
451 } catch (const std::exception& ex) {
452 LOG(ERROR) << "Error running the scheduled function <"
453 << func.name << ">: " << exceptionStr(ex);
456 // Re-acquire the lock
459 if (!currentFunction_) {
460 // The function was cancelled while we were running it.
461 // We shouldn't reschedule it;
462 cancellingCurrentFunction_ = false;
465 if (currentFunction_->runOnce) {
466 // Don't reschedule if the function only needed to run once.
467 currentFunction_ = nullptr;
470 // Clear currentFunction_
471 CHECK_EQ(currentFunction_, &func);
472 currentFunction_ = nullptr;
474 // Re-insert the function into our functions_ heap.
475 // We only maintain the heap property while running_ is set. (running_ may
476 // have been cleared while we were invoking the user's function.)
477 functions_.push_back(std::move(func));
479 std::push_heap(functions_.begin(), functions_.end(), fnCmp_);
483 void FunctionScheduler::addFunctionToHeap(
484 const std::unique_lock<std::mutex>& lock,
486 // This function should only be called with mutex_ already locked.
487 DCHECK(lock.mutex() == &mutex_);
488 DCHECK(lock.owns_lock());
490 functions_.emplace_back(std::move(func));
492 functions_.back().resetNextRunTime(steady_clock::now());
493 std::push_heap(functions_.begin(), functions_.end(), fnCmp_);
494 // Signal the running thread to wake up and see if it needs to change
495 // its current scheduling decision.
496 runningCondvar_.notify_one();
500 void FunctionScheduler::setThreadName(StringPiece threadName) {
501 std::unique_lock<std::mutex> l(mutex_);
502 threadName_ = threadName.str();