An intro to the upgrade mutex in the Synchronized docs
[folly.git] / folly / experimental / FunctionScheduler.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
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 <folly/experimental/FunctionScheduler.h>
18
19 #include <random>
20
21 #include <folly/Conv.h>
22 #include <folly/Random.h>
23 #include <folly/String.h>
24 #include <folly/ThreadName.h>
25
26 using std::chrono::milliseconds;
27 using std::chrono::steady_clock;
28
29 namespace folly {
30
31 namespace {
32
33 struct ConstIntervalFunctor {
34   const milliseconds constInterval;
35
36   explicit ConstIntervalFunctor(milliseconds interval)
37       : constInterval(interval) {
38     if (interval < milliseconds::zero()) {
39       throw std::invalid_argument(
40           "FunctionScheduler: "
41           "time interval must be non-negative");
42     }
43   }
44
45   milliseconds operator()() const { return constInterval; }
46 };
47
48 struct PoissonDistributionFunctor {
49   std::default_random_engine generator;
50   std::poisson_distribution<int> poissonRandom;
51
52   explicit PoissonDistributionFunctor(double meanPoissonMs)
53       : poissonRandom(meanPoissonMs) {
54     if (meanPoissonMs < 0.0) {
55       throw std::invalid_argument(
56           "FunctionScheduler: "
57           "Poisson mean interval must be non-negative");
58     }
59   }
60
61   milliseconds operator()() { return milliseconds(poissonRandom(generator)); }
62 };
63
64 struct UniformDistributionFunctor {
65   std::default_random_engine generator;
66   std::uniform_int_distribution<> dist;
67
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(
73           "FunctionScheduler: "
74           "min time interval must be less or equal than max interval");
75     }
76     if (minInterval < milliseconds::zero()) {
77       throw std::invalid_argument(
78           "FunctionScheduler: "
79           "time interval must be non-negative");
80     }
81   }
82
83   milliseconds operator()() { return milliseconds(dist(generator)); }
84 };
85
86 } // anonymous namespace
87
88 FunctionScheduler::FunctionScheduler() {}
89
90 FunctionScheduler::~FunctionScheduler() {
91   // make sure to stop the thread (if running)
92   shutdown();
93 }
94
95 void FunctionScheduler::addFunction(Function<void()>&& cb,
96                                     milliseconds interval,
97                                     StringPiece nameID,
98                                     milliseconds startDelay) {
99   addFunctionGenericDistribution(
100       std::move(cb),
101       ConstIntervalFunctor(interval),
102       nameID.str(),
103       to<std::string>(interval.count(), "ms"),
104       startDelay);
105 }
106
107 void FunctionScheduler::addFunction(Function<void()>&& cb,
108                                     milliseconds interval,
109                                     const LatencyDistribution& latencyDistr,
110                                     StringPiece nameID,
111                                     milliseconds startDelay) {
112   if (latencyDistr.isPoisson) {
113     addFunctionGenericDistribution(
114         std::move(cb),
115         PoissonDistributionFunctor(latencyDistr.poissonMean),
116         nameID.str(),
117         to<std::string>(latencyDistr.poissonMean, "ms (Poisson mean)"),
118         startDelay);
119   } else {
120     addFunction(std::move(cb), interval, nameID, startDelay);
121   }
122 }
123
124 void FunctionScheduler::addFunctionUniformDistribution(
125     Function<void()>&& cb,
126     milliseconds minInterval,
127     milliseconds maxInterval,
128     StringPiece nameID,
129     milliseconds startDelay) {
130   addFunctionGenericDistribution(
131       std::move(cb),
132       UniformDistributionFunctor(minInterval, maxInterval),
133       nameID.str(),
134       to<std::string>(
135           "[", minInterval.count(), " , ", maxInterval.count(), "] ms"),
136       startDelay);
137 }
138
139 void FunctionScheduler::addFunctionGenericDistribution(
140     Function<void()>&& cb,
141     IntervalDistributionFunc&& intervalFunc,
142     const std::string& nameID,
143     const std::string& intervalDescr,
144     milliseconds startDelay) {
145   if (!cb) {
146     throw std::invalid_argument(
147         "FunctionScheduler: Scheduled function must be set");
148   }
149   if (!intervalFunc) {
150     throw std::invalid_argument(
151         "FunctionScheduler: interval distribution function must be set");
152   }
153   if (startDelay < milliseconds::zero()) {
154     throw std::invalid_argument(
155         "FunctionScheduler: start delay must be non-negative");
156   }
157
158   std::unique_lock<std::mutex> l(mutex_);
159   // check if the nameID is unique
160   for (const auto& f : functions_) {
161     if (f.isValid() && f.name == nameID) {
162       throw std::invalid_argument(
163           to<std::string>("FunctionScheduler: a function named \"",
164                           nameID,
165                           "\" already exists"));
166     }
167   }
168   if (currentFunction_ && currentFunction_->name == nameID) {
169     throw std::invalid_argument(to<std::string>(
170         "FunctionScheduler: a function named \"", nameID, "\" already exists"));
171   }
172
173   addFunctionToHeap(
174       l,
175       RepeatFunc(
176           std::move(cb),
177           std::move(intervalFunc),
178           nameID,
179           intervalDescr,
180           startDelay));
181 }
182
183 bool FunctionScheduler::cancelFunction(StringPiece nameID) {
184   std::unique_lock<std::mutex> l(mutex_);
185
186   if (currentFunction_ && currentFunction_->name == nameID) {
187     // This function is currently being run. Clear currentFunction_
188     // The running thread will see this and won't reschedule the function.
189     currentFunction_ = nullptr;
190     return true;
191   }
192
193   for (auto it = functions_.begin(); it != functions_.end(); ++it) {
194     if (it->isValid() && it->name == nameID) {
195       cancelFunction(l, it);
196       return true;
197     }
198   }
199   return false;
200 }
201
202 void FunctionScheduler::cancelFunction(const std::unique_lock<std::mutex>& l,
203                                        FunctionHeap::iterator it) {
204   // This function should only be called with mutex_ already locked.
205   DCHECK(l.mutex() == &mutex_);
206   DCHECK(l.owns_lock());
207
208   if (running_) {
209     // Internally gcc has an __adjust_heap() function to fill in a hole in the
210     // heap.  Unfortunately it isn't part of the standard API.
211     //
212     // For now we just leave the RepeatFunc in our heap, but mark it as unused.
213     // When its nextTimeInterval comes up, the runner thread will pop it from
214     // the heap and simply throw it away.
215     it->cancel();
216   } else {
217     // We're not running, so functions_ doesn't need to be maintained in heap
218     // order.
219     functions_.erase(it);
220   }
221 }
222
223 void FunctionScheduler::cancelAllFunctions() {
224   std::unique_lock<std::mutex> l(mutex_);
225   functions_.clear();
226   currentFunction_ = nullptr;
227 }
228
229 bool FunctionScheduler::resetFunctionTimer(StringPiece nameID) {
230   std::unique_lock<std::mutex> l(mutex_);
231   if (currentFunction_ && currentFunction_->name == nameID) {
232     RepeatFunc* funcPtrCopy = currentFunction_;
233     // This function is currently being run. Clear currentFunction_
234     // to avoid rescheduling it, and add the function again to honor the
235     // startDelay.
236     currentFunction_ = nullptr;
237     addFunctionToHeap(l, std::move(*funcPtrCopy));
238     return true;
239   }
240
241   // Since __adjust_heap() isn't a part of the standard API, there's no way to
242   // fix the heap ordering if we adjust the key (nextRunTime) for the existing
243   // RepeatFunc. Instead, we just cancel it and add an identical object.
244   for (auto it = functions_.begin(); it != functions_.end(); ++it) {
245     if (it->isValid() && it->name == nameID) {
246       RepeatFunc funcCopy(std::move(*it));
247       cancelFunction(l, it);
248       addFunctionToHeap(l, std::move(funcCopy));
249       return true;
250     }
251   }
252   return false;
253 }
254
255 bool FunctionScheduler::start() {
256   std::unique_lock<std::mutex> l(mutex_);
257   if (running_) {
258     return false;
259   }
260
261   running_ = true;
262
263   VLOG(1) << "Starting FunctionScheduler with " << functions_.size()
264           << " functions.";
265   auto now = steady_clock::now();
266   // Reset the next run time. for all functions.
267   // note: this is needed since one can shutdown() and start() again
268   for (auto& f : functions_) {
269     f.resetNextRunTime(now);
270     VLOG(1) << "   - func: " << (f.name.empty() ? "(anon)" : f.name.c_str())
271             << ", period = " << f.intervalDescr
272             << ", delay = " << f.startDelay.count() << "ms";
273   }
274   std::make_heap(functions_.begin(), functions_.end(), fnCmp_);
275
276   thread_ = std::thread([&] { this->run(); });
277   return true;
278 }
279
280 void FunctionScheduler::shutdown() {
281   {
282     std::lock_guard<std::mutex> g(mutex_);
283     if (!running_) {
284       return;
285     }
286
287     running_ = false;
288     runningCondvar_.notify_one();
289   }
290   thread_.join();
291 }
292
293 void FunctionScheduler::run() {
294   std::unique_lock<std::mutex> lock(mutex_);
295
296   if (!threadName_.empty()) {
297     folly::setThreadName(threadName_);
298   }
299
300   while (running_) {
301     // If we have nothing to run, wait until a function is added or until we
302     // are stopped.
303     if (functions_.empty()) {
304       runningCondvar_.wait(lock);
305       continue;
306     }
307
308     auto now = steady_clock::now();
309
310     // Move the next function to run to the end of functions_
311     std::pop_heap(functions_.begin(), functions_.end(), fnCmp_);
312
313     // Check to see if the function was cancelled.
314     // If so, just remove it and continue around the loop.
315     if (!functions_.back().isValid()) {
316       functions_.pop_back();
317       continue;
318     }
319
320     auto sleepTime = functions_.back().getNextRunTime() - now;
321     if (sleepTime < milliseconds::zero()) {
322       // We need to run this function now
323       runOneFunction(lock, now);
324     } else {
325       // Re-add the function to the heap, and wait until we actually
326       // need to run it.
327       std::push_heap(functions_.begin(), functions_.end(), fnCmp_);
328       runningCondvar_.wait_for(lock, sleepTime);
329     }
330   }
331 }
332
333 void FunctionScheduler::runOneFunction(std::unique_lock<std::mutex>& lock,
334                                        steady_clock::time_point now) {
335   DCHECK(lock.mutex() == &mutex_);
336   DCHECK(lock.owns_lock());
337
338   // The function to run will be at the end of functions_ already.
339   //
340   // Fully remove it from functions_ now.
341   // We need to release mutex_ while we invoke this function, and we need to
342   // maintain the heap property on functions_ while mutex_ is unlocked.
343   RepeatFunc func(std::move(functions_.back()));
344   functions_.pop_back();
345   if (!func.cb) {
346     VLOG(5) << func.name << "function has been canceled while waiting";
347     return;
348   }
349   currentFunction_ = &func;
350
351   // Update the function's next run time.
352   if (steady_) {
353     // This allows scheduler to catch up
354     func.setNextRunTimeSteady();
355   } else {
356     // Note that we set nextRunTime based on the current time where we started
357     // the function call, rather than the time when the function finishes.
358     // This ensures that we call the function once every time interval, as
359     // opposed to waiting time interval seconds between calls.  (These can be
360     // different if the function takes a significant amount of time to run.)
361     func.setNextRunTimeStrict(now);
362   }
363
364   // Release the lock while we invoke the user's function
365   lock.unlock();
366
367   // Invoke the function
368   try {
369     VLOG(5) << "Now running " << func.name;
370     func.cb();
371   } catch (const std::exception& ex) {
372     LOG(ERROR) << "Error running the scheduled function <"
373       << func.name << ">: " << exceptionStr(ex);
374   }
375
376   // Re-acquire the lock
377   lock.lock();
378
379   if (!currentFunction_) {
380     // The function was cancelled while we were running it.
381     // We shouldn't reschedule it;
382     return;
383   }
384   // Clear currentFunction_
385   CHECK_EQ(currentFunction_, &func);
386   currentFunction_ = nullptr;
387
388   // Re-insert the function into our functions_ heap.
389   // We only maintain the heap property while running_ is set.  (running_ may
390   // have been cleared while we were invoking the user's function.)
391   functions_.push_back(std::move(func));
392   if (running_) {
393     std::push_heap(functions_.begin(), functions_.end(), fnCmp_);
394   }
395 }
396
397 void FunctionScheduler::addFunctionToHeap(
398     const std::unique_lock<std::mutex>& lock,
399     RepeatFunc&& func) {
400   // This function should only be called with mutex_ already locked.
401   DCHECK(lock.mutex() == &mutex_);
402   DCHECK(lock.owns_lock());
403
404   functions_.emplace_back(std::move(func));
405   if (running_) {
406     functions_.back().resetNextRunTime(steady_clock::now());
407     std::push_heap(functions_.begin(), functions_.end(), fnCmp_);
408     // Signal the running thread to wake up and see if it needs to change
409     // its current scheduling decision.
410     runningCondvar_.notify_one();
411   }
412 }
413
414 void FunctionScheduler::setThreadName(StringPiece threadName) {
415   std::unique_lock<std::mutex> l(mutex_);
416   threadName_ = threadName.str();
417 }
418
419 }