Use folly::Function in folly::EventBase
[folly.git] / folly / io / async / EventBase.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 #ifndef __STDC_FORMAT_MACROS
18 #define __STDC_FORMAT_MACROS
19 #endif
20
21 #include <folly/io/async/EventBase.h>
22
23 #include <folly/ThreadName.h>
24 #include <folly/io/async/NotificationQueue.h>
25
26 #include <boost/static_assert.hpp>
27 #include <condition_variable>
28 #include <fcntl.h>
29 #include <mutex>
30 #include <pthread.h>
31 #include <unistd.h>
32
33 namespace {
34
35 using folly::EventBase;
36
37 class FunctionLoopCallback : public EventBase::LoopCallback {
38  public:
39   explicit FunctionLoopCallback(EventBase::Func&& function)
40       : function_(std::move(function)) {}
41
42   void runLoopCallback() noexcept override {
43     function_();
44     delete this;
45   }
46
47  private:
48   EventBase::Func function_;
49 };
50 }
51
52 namespace folly {
53
54 /*
55  * EventBase::FunctionRunner
56  */
57
58 class EventBase::FunctionRunner
59     : public NotificationQueue<EventBase::Func>::Consumer {
60  public:
61   void messageAvailable(Func&& msg) override {
62     // In libevent2, internal events do not break the loop.
63     // Most users would expect loop(), followed by runInEventBaseThread(),
64     // to break the loop and check if it should exit or not.
65     // To have similar bejaviour to libevent1.4, tell the loop to break here.
66     // Note that loop() may still continue to loop, but it will also check the
67     // stop_ flag as well as runInLoop callbacks, etc.
68     event_base_loopbreak(getEventBase()->evb_);
69
70     if (!msg) {
71       // terminateLoopSoon() sends a null message just to
72       // wake up the loop.  We can ignore these messages.
73       return;
74     }
75
76     // The function should never throw an exception, because we have no
77     // way of knowing what sort of error handling to perform.
78     //
79     // If it does throw, log a message and abort the program.
80     try {
81       msg();
82     } catch (const std::exception& ex) {
83       LOG(ERROR) << "runInEventBaseThread() function threw a "
84                  << typeid(ex).name() << " exception: " << ex.what();
85       abort();
86     } catch (...) {
87       LOG(ERROR) << "runInEventBaseThread() function threw an exception";
88       abort();
89     }
90   }
91 };
92
93 /*
94  * EventBase::CobTimeout methods
95  */
96
97 void EventBase::CobTimeout::timeoutExpired() noexcept {
98   // For now, we just swallow any exceptions that the callback threw.
99   try {
100     cob_();
101   } catch (const std::exception& ex) {
102     LOG(ERROR) << "EventBase::runAfterDelay() callback threw "
103                << typeid(ex).name() << " exception: " << ex.what();
104   } catch (...) {
105     LOG(ERROR) << "EventBase::runAfterDelay() callback threw non-exception "
106                << "type";
107   }
108
109   // The CobTimeout object was allocated on the heap by runAfterDelay(),
110   // so delete it now that the it has fired.
111   delete this;
112 }
113
114
115 // The interface used to libevent is not thread-safe.  Calls to
116 // event_init() and event_base_free() directly modify an internal
117 // global 'current_base', so a mutex is required to protect this.
118 //
119 // event_init() should only ever be called once.  Subsequent calls
120 // should be made to event_base_new().  We can recognise that
121 // event_init() has already been called by simply inspecting current_base.
122 static std::mutex libevent_mutex_;
123
124 /*
125  * EventBase methods
126  */
127
128 EventBase::EventBase(bool enableTimeMeasurement)
129   : runOnceCallbacks_(nullptr)
130   , stop_(false)
131   , loopThread_()
132   , queue_(nullptr)
133   , fnRunner_(nullptr)
134   , maxLatency_(0)
135   , avgLoopTime_(2000000)
136   , maxLatencyLoopTime_(avgLoopTime_)
137   , enableTimeMeasurement_(enableTimeMeasurement)
138   , nextLoopCnt_(-40)       // Early wrap-around so bugs will manifest soon
139   , latestLoopCnt_(nextLoopCnt_)
140   , startWork_(0)
141   , observer_(nullptr)
142   , observerSampleCount_(0)
143   , executionObserver_(nullptr) {
144   struct event ev;
145   {
146     std::lock_guard<std::mutex> lock(libevent_mutex_);
147
148     // The value 'current_base' (libevent 1) or
149     // 'event_global_current_base_' (libevent 2) is filled in by event_set(),
150     // allowing examination of its value without an explicit reference here.
151     // If ev.ev_base is NULL, then event_init() must be called, otherwise
152     // call event_base_new().
153     event_set(&ev, 0, 0, nullptr, nullptr);
154     if (!ev.ev_base) {
155       evb_ = event_init();
156     }
157   }
158
159   if (ev.ev_base) {
160     evb_ = event_base_new();
161   }
162
163   if (UNLIKELY(evb_ == nullptr)) {
164     LOG(ERROR) << "EventBase(): Failed to init event base.";
165     folly::throwSystemError("error in EventBase::EventBase()");
166   }
167   VLOG(5) << "EventBase(): Created.";
168   initNotificationQueue();
169   RequestContext::saveContext();
170 }
171
172 // takes ownership of the event_base
173 EventBase::EventBase(event_base* evb, bool enableTimeMeasurement)
174   : runOnceCallbacks_(nullptr)
175   , stop_(false)
176   , loopThread_()
177   , evb_(evb)
178   , queue_(nullptr)
179   , fnRunner_(nullptr)
180   , maxLatency_(0)
181   , avgLoopTime_(2000000)
182   , maxLatencyLoopTime_(avgLoopTime_)
183   , enableTimeMeasurement_(enableTimeMeasurement)
184   , nextLoopCnt_(-40)       // Early wrap-around so bugs will manifest soon
185   , latestLoopCnt_(nextLoopCnt_)
186   , startWork_(0)
187   , observer_(nullptr)
188   , observerSampleCount_(0)
189   , executionObserver_(nullptr) {
190   if (UNLIKELY(evb_ == nullptr)) {
191     LOG(ERROR) << "EventBase(): Pass nullptr as event base.";
192     throw std::invalid_argument("EventBase(): event base cannot be nullptr");
193   }
194   initNotificationQueue();
195   RequestContext::saveContext();
196 }
197
198 EventBase::~EventBase() {
199   // Call all destruction callbacks, before we start cleaning up our state.
200   while (!onDestructionCallbacks_.empty()) {
201     LoopCallback* callback = &onDestructionCallbacks_.front();
202     onDestructionCallbacks_.pop_front();
203     callback->runLoopCallback();
204   }
205
206   // Delete any unfired callback objects, so that we don't leak memory
207   // (Note that we don't fire them.  The caller is responsible for cleaning up
208   // its own data structures if it destroys the EventBase with unfired events
209   // remaining.)
210   while (!pendingCobTimeouts_.empty()) {
211     CobTimeout* timeout = &pendingCobTimeouts_.front();
212     delete timeout;
213   }
214
215   while (!runBeforeLoopCallbacks_.empty()) {
216     delete &runBeforeLoopCallbacks_.front();
217   }
218
219   (void) runLoopCallbacks(false);
220
221   if (!fnRunner_->consumeUntilDrained()) {
222     LOG(ERROR) << "~EventBase(): Unable to drain notification queue";
223   }
224
225   // Stop consumer before deleting NotificationQueue
226   fnRunner_->stopConsuming();
227   {
228     std::lock_guard<std::mutex> lock(libevent_mutex_);
229     event_base_free(evb_);
230   }
231
232   while (!runAfterDrainCallbacks_.empty()) {
233     LoopCallback* callback = &runAfterDrainCallbacks_.front();
234     runAfterDrainCallbacks_.pop_front();
235     callback->runLoopCallback();
236   }
237
238   {
239     std::lock_guard<std::mutex> lock(localStorageMutex_);
240     for (auto storage : localStorageToDtor_) {
241       storage->onEventBaseDestruction(*this);
242     }
243   }
244   VLOG(5) << "EventBase(): Destroyed.";
245 }
246
247 int EventBase::getNotificationQueueSize() const {
248   return queue_->size();
249 }
250
251 void EventBase::setMaxReadAtOnce(uint32_t maxAtOnce) {
252   fnRunner_->setMaxReadAtOnce(maxAtOnce);
253 }
254
255 // Set smoothing coefficient for loop load average; input is # of milliseconds
256 // for exp(-1) decay.
257 void EventBase::setLoadAvgMsec(uint32_t ms) {
258   assert(enableTimeMeasurement_);
259   uint64_t us = 1000 * ms;
260   if (ms > 0) {
261     maxLatencyLoopTime_.setTimeInterval(us);
262     avgLoopTime_.setTimeInterval(us);
263   } else {
264     LOG(ERROR) << "non-positive arg to setLoadAvgMsec()";
265   }
266 }
267
268 void EventBase::resetLoadAvg(double value) {
269   assert(enableTimeMeasurement_);
270   avgLoopTime_.reset(value);
271   maxLatencyLoopTime_.reset(value);
272 }
273
274 static std::chrono::milliseconds
275 getTimeDelta(std::chrono::steady_clock::time_point* prev) {
276   auto result = std::chrono::steady_clock::now() - *prev;
277   *prev = std::chrono::steady_clock::now();
278
279   return std::chrono::duration_cast<std::chrono::milliseconds>(result);
280 }
281
282 void EventBase::waitUntilRunning() {
283   while (!isRunning()) {
284     sched_yield();
285   }
286 }
287
288 // enters the event_base loop -- will only exit when forced to
289 bool EventBase::loop() {
290   return loopBody();
291 }
292
293 bool EventBase::loopOnce(int flags) {
294   return loopBody(flags | EVLOOP_ONCE);
295 }
296
297 bool EventBase::loopBody(int flags) {
298   VLOG(5) << "EventBase(): Starting loop.";
299   int res = 0;
300   bool ranLoopCallbacks;
301   bool blocking = !(flags & EVLOOP_NONBLOCK);
302   bool once = (flags & EVLOOP_ONCE);
303
304   // time-measurement variables.
305   std::chrono::steady_clock::time_point prev;
306   int64_t idleStart = 0;
307   int64_t busy;
308   int64_t idle;
309
310   loopThread_.store(pthread_self(), std::memory_order_release);
311
312   if (!name_.empty()) {
313     setThreadName(name_);
314   }
315
316   if (enableTimeMeasurement_) {
317     prev = std::chrono::steady_clock::now();
318     idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
319       std::chrono::steady_clock::now().time_since_epoch()).count();
320   }
321
322   while (!stop_.load(std::memory_order_acquire)) {
323     ++nextLoopCnt_;
324
325     // Run the before loop callbacks
326     LoopCallbackList callbacks;
327     callbacks.swap(runBeforeLoopCallbacks_);
328
329     while(!callbacks.empty()) {
330       auto* item = &callbacks.front();
331       callbacks.pop_front();
332       item->runLoopCallback();
333     }
334
335     // nobody can add loop callbacks from within this thread if
336     // we don't have to handle anything to start with...
337     if (blocking && loopCallbacks_.empty()) {
338       res = event_base_loop(evb_, EVLOOP_ONCE);
339     } else {
340       res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK);
341     }
342
343     ranLoopCallbacks = runLoopCallbacks();
344
345     if (enableTimeMeasurement_) {
346       busy = std::chrono::duration_cast<std::chrono::microseconds>(
347         std::chrono::steady_clock::now().time_since_epoch()).count() -
348         startWork_;
349       idle = startWork_ - idleStart;
350
351       avgLoopTime_.addSample(idle, busy);
352       maxLatencyLoopTime_.addSample(idle, busy);
353
354       if (observer_) {
355         if (observerSampleCount_++ == observer_->getSampleRate()) {
356           observerSampleCount_ = 0;
357           observer_->loopSample(busy, idle);
358         }
359       }
360
361       VLOG(11) << "EventBase "  << this         << " did not timeout "
362         " loop time guess: "    << busy + idle  <<
363         " idle time: "          << idle         <<
364         " busy time: "          << busy         <<
365         " avgLoopTime: "        << avgLoopTime_.get() <<
366         " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() <<
367         " maxLatency_: "        << maxLatency_ <<
368         " notificationQueueSize: " << getNotificationQueueSize() <<
369         " nothingHandledYet(): "<< nothingHandledYet();
370
371       // see if our average loop time has exceeded our limit
372       if ((maxLatency_ > 0) &&
373           (maxLatencyLoopTime_.get() > double(maxLatency_))) {
374         maxLatencyCob_();
375         // back off temporarily -- don't keep spamming maxLatencyCob_
376         // if we're only a bit over the limit
377         maxLatencyLoopTime_.dampen(0.9);
378       }
379
380       // Our loop run did real work; reset the idle timer
381       idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
382         std::chrono::steady_clock::now().time_since_epoch()).count();
383     } else {
384       VLOG(11) << "EventBase "  << this << " did not timeout "
385         " time measurement is disabled "
386         " nothingHandledYet(): "<< nothingHandledYet();
387     }
388
389     // If the event loop indicate that there were no more events, and
390     // we also didn't have any loop callbacks to run, there is nothing left to
391     // do.
392     if (res != 0 && !ranLoopCallbacks) {
393       // Since Notification Queue is marked 'internal' some events may not have
394       // run.  Run them manually if so, and continue looping.
395       //
396       if (getNotificationQueueSize() > 0) {
397         fnRunner_->handlerReady(0);
398       } else {
399         break;
400       }
401     }
402
403     if (enableTimeMeasurement_) {
404       VLOG(5) << "EventBase " << this << " loop time: " <<
405         getTimeDelta(&prev).count();
406     }
407
408     if (once) {
409       break;
410     }
411   }
412   // Reset stop_ so loop() can be called again
413   stop_ = false;
414
415   if (res < 0) {
416     LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
417     return false;
418   } else if (res == 1) {
419     VLOG(5) << "EventBase: ran out of events (exiting loop)!";
420   } else if (res > 1) {
421     LOG(ERROR) << "EventBase: unknown event loop result = " << res;
422     return false;
423   }
424
425   loopThread_.store({}, std::memory_order_release);
426
427   VLOG(5) << "EventBase(): Done with loop.";
428   return true;
429 }
430
431 void EventBase::loopForever() {
432   // Update the notification queue event to treat it as a normal (non-internal)
433   // event.  The notification queue event always remains installed, and the main
434   // loop won't exit with it installed.
435   fnRunner_->stopConsuming();
436   fnRunner_->startConsuming(this, queue_.get());
437
438   bool ret = loop();
439
440   // Restore the notification queue internal flag
441   fnRunner_->stopConsuming();
442   fnRunner_->startConsumingInternal(this, queue_.get());
443
444   if (!ret) {
445     folly::throwSystemError("error in EventBase::loopForever()");
446   }
447 }
448
449 bool EventBase::bumpHandlingTime() {
450   VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
451     " (loop) latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
452   if(nothingHandledYet()) {
453     latestLoopCnt_ = nextLoopCnt_;
454     if (enableTimeMeasurement_) {
455       // set the time
456       startWork_ = std::chrono::duration_cast<std::chrono::microseconds>(
457         std::chrono::steady_clock::now().time_since_epoch()).count();
458
459       VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
460         " (loop) startWork_ " << startWork_;
461     }
462     return true;
463   }
464   return false;
465 }
466
467 void EventBase::terminateLoopSoon() {
468   VLOG(5) << "EventBase(): Received terminateLoopSoon() command.";
469
470   // Set stop to true, so the event loop will know to exit.
471   // TODO: We should really use an atomic operation here with a release
472   // barrier.
473   stop_ = true;
474
475   // Call event_base_loopbreak() so that libevent will exit the next time
476   // around the loop.
477   event_base_loopbreak(evb_);
478
479   // If terminateLoopSoon() is called from another thread,
480   // the EventBase thread might be stuck waiting for events.
481   // In this case, it won't wake up and notice that stop_ is set until it
482   // receives another event.  Send an empty frame to the notification queue
483   // so that the event loop will wake up even if there are no other events.
484   //
485   // We don't care about the return value of trySendFrame().  If it fails
486   // this likely means the EventBase already has lots of events waiting
487   // anyway.
488   try {
489     queue_->putMessage(nullptr);
490   } catch (...) {
491     // We don't care if putMessage() fails.  This likely means
492     // the EventBase already has lots of events waiting anyway.
493   }
494 }
495
496 void EventBase::runInLoop(LoopCallback* callback, bool thisIteration) {
497   DCHECK(isInEventBaseThread());
498   callback->cancelLoopCallback();
499   callback->context_ = RequestContext::saveContext();
500   if (runOnceCallbacks_ != nullptr && thisIteration) {
501     runOnceCallbacks_->push_back(*callback);
502   } else {
503     loopCallbacks_.push_back(*callback);
504   }
505 }
506
507 void EventBase::runInLoop(Func cob, bool thisIteration) {
508   DCHECK(isInEventBaseThread());
509   auto wrapper = new FunctionLoopCallback(std::move(cob));
510   wrapper->context_ = RequestContext::saveContext();
511   if (runOnceCallbacks_ != nullptr && thisIteration) {
512     runOnceCallbacks_->push_back(*wrapper);
513   } else {
514     loopCallbacks_.push_back(*wrapper);
515   }
516 }
517
518 void EventBase::runAfterDrain(Func cob) {
519   auto callback = new FunctionLoopCallback(std::move(cob));
520   std::lock_guard<std::mutex> lg(runAfterDrainCallbacksMutex_);
521   callback->cancelLoopCallback();
522   runAfterDrainCallbacks_.push_back(*callback);
523 }
524
525 void EventBase::runOnDestruction(LoopCallback* callback) {
526   std::lock_guard<std::mutex> lg(onDestructionCallbacksMutex_);
527   callback->cancelLoopCallback();
528   onDestructionCallbacks_.push_back(*callback);
529 }
530
531 void EventBase::runBeforeLoop(LoopCallback* callback) {
532   DCHECK(isInEventBaseThread());
533   callback->cancelLoopCallback();
534   runBeforeLoopCallbacks_.push_back(*callback);
535 }
536
537 bool EventBase::runInEventBaseThread(Func fn) {
538   // Send the message.
539   // It will be received by the FunctionRunner in the EventBase's thread.
540
541   // We try not to schedule nullptr callbacks
542   if (!fn) {
543     LOG(ERROR) << "EventBase " << this
544                << ": Scheduling nullptr callbacks is not allowed";
545     return false;
546   }
547
548   // Short-circuit if we are already in our event base
549   if (inRunningEventBaseThread()) {
550     runInLoop(std::move(fn));
551     return true;
552
553   }
554
555   try {
556     queue_->putMessage(std::move(fn));
557   } catch (const std::exception& ex) {
558     LOG(ERROR) << "EventBase " << this << ": failed to schedule function "
559                << "for EventBase thread: " << ex.what();
560     return false;
561   }
562
563   return true;
564 }
565
566 bool EventBase::runInEventBaseThreadAndWait(Func fn) {
567   if (inRunningEventBaseThread()) {
568     LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
569                << "allowed";
570     return false;
571   }
572
573   bool ready = false;
574   std::mutex m;
575   std::condition_variable cv;
576   runInEventBaseThread([&] {
577       SCOPE_EXIT {
578         std::unique_lock<std::mutex> l(m);
579         ready = true;
580         cv.notify_one();
581         // We cannot release the lock before notify_one, because a spurious
582         // wakeup in the waiting thread may lead to cv and m going out of scope
583         // prematurely.
584       };
585       fn();
586   });
587   std::unique_lock<std::mutex> l(m);
588   cv.wait(l, [&] { return ready; });
589
590   return true;
591 }
592
593 bool EventBase::runImmediatelyOrRunInEventBaseThreadAndWait(Func fn) {
594   if (isInEventBaseThread()) {
595     fn();
596     return true;
597   } else {
598     return runInEventBaseThreadAndWait(std::move(fn));
599   }
600 }
601
602 void EventBase::runAfterDelay(
603     Func cob,
604     uint32_t milliseconds,
605     TimeoutManager::InternalEnum in) {
606   if (!tryRunAfterDelay(std::move(cob), milliseconds, in)) {
607     folly::throwSystemError(
608       "error in EventBase::runAfterDelay(), failed to schedule timeout");
609   }
610 }
611
612 bool EventBase::tryRunAfterDelay(
613     Func cob,
614     uint32_t milliseconds,
615     TimeoutManager::InternalEnum in) {
616   CobTimeout* timeout = new CobTimeout(this, std::move(cob), in);
617   if (!timeout->scheduleTimeout(milliseconds)) {
618     delete timeout;
619     return false;
620   }
621   pendingCobTimeouts_.push_back(*timeout);
622   return true;
623 }
624
625 bool EventBase::runLoopCallbacks(bool setContext) {
626   if (!loopCallbacks_.empty()) {
627     bumpHandlingTime();
628     // Swap the loopCallbacks_ list with a temporary list on our stack.
629     // This way we will only run callbacks scheduled at the time
630     // runLoopCallbacks() was invoked.
631     //
632     // If any of these callbacks in turn call runInLoop() to schedule more
633     // callbacks, those new callbacks won't be run until the next iteration
634     // around the event loop.  This prevents runInLoop() callbacks from being
635     // able to start file descriptor and timeout based events.
636     LoopCallbackList currentCallbacks;
637     currentCallbacks.swap(loopCallbacks_);
638     runOnceCallbacks_ = &currentCallbacks;
639
640     while (!currentCallbacks.empty()) {
641       LoopCallback* callback = &currentCallbacks.front();
642       currentCallbacks.pop_front();
643       if (setContext) {
644         RequestContext::setContext(callback->context_);
645       }
646       callback->runLoopCallback();
647     }
648
649     runOnceCallbacks_ = nullptr;
650     return true;
651   }
652   return false;
653 }
654
655 void EventBase::initNotificationQueue() {
656   // Infinite size queue
657   queue_.reset(new NotificationQueue<Func>());
658
659   // We allocate fnRunner_ separately, rather than declaring it directly
660   // as a member of EventBase solely so that we don't need to include
661   // NotificationQueue.h from EventBase.h
662   fnRunner_.reset(new FunctionRunner());
663
664   // Mark this as an internal event, so event_base_loop() will return if
665   // there are no other events besides this one installed.
666   //
667   // Most callers don't care about the internal notification queue used by
668   // EventBase.  The queue is always installed, so if we did count the queue as
669   // an active event, loop() would never exit with no more events to process.
670   // Users can use loopForever() if they do care about the notification queue.
671   // (This is useful for EventBase threads that do nothing but process
672   // runInEventBaseThread() notifications.)
673   fnRunner_->startConsumingInternal(this, queue_.get());
674 }
675
676 void EventBase::SmoothLoopTime::setTimeInterval(uint64_t timeInterval) {
677   expCoeff_ = -1.0/timeInterval;
678   VLOG(11) << "expCoeff_ " << expCoeff_ << " " << __PRETTY_FUNCTION__;
679 }
680
681 void EventBase::SmoothLoopTime::reset(double value) {
682   value_ = value;
683 }
684
685 void EventBase::SmoothLoopTime::addSample(int64_t idle, int64_t busy) {
686     /*
687      * Position at which the busy sample is considered to be taken.
688      * (Allows to quickly skew our average without editing much code)
689      */
690     enum BusySamplePosition {
691       RIGHT = 0,  // busy sample placed at the end of the iteration
692       CENTER = 1, // busy sample placed at the middle point of the iteration
693       LEFT = 2,   // busy sample placed at the beginning of the iteration
694     };
695
696   // See http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
697   // and D676020 for more info on this calculation.
698   VLOG(11) << "idle " << idle << " oldBusyLeftover_ " << oldBusyLeftover_ <<
699               " idle + oldBusyLeftover_ " << idle + oldBusyLeftover_ <<
700               " busy " << busy << " " << __PRETTY_FUNCTION__;
701   idle += oldBusyLeftover_ + busy;
702   oldBusyLeftover_ = (busy * BusySamplePosition::CENTER) / 2;
703   idle -= oldBusyLeftover_;
704
705   double coeff = exp(idle * expCoeff_);
706   value_ *= coeff;
707   value_ += (1.0 - coeff) * busy;
708 }
709
710 bool EventBase::nothingHandledYet() {
711   VLOG(11) << "latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
712   return (nextLoopCnt_ != latestLoopCnt_);
713 }
714
715 /* static */
716 void EventBase::runFunctionPtr(Cob* fn) {
717   // The function should never throw an exception, because we have no
718   // way of knowing what sort of error handling to perform.
719   //
720   // If it does throw, log a message and abort the program.
721   try {
722     (*fn)();
723   } catch (const std::exception &ex) {
724     LOG(ERROR) << "runInEventBaseThread() std::function threw a "
725                << typeid(ex).name() << " exception: " << ex.what();
726     abort();
727   } catch (...) {
728     LOG(ERROR) << "runInEventBaseThread() std::function threw an exception";
729     abort();
730   }
731
732   // The function object was allocated by runInEventBaseThread().
733   // Delete it once it has been run.
734   delete fn;
735 }
736
737 void EventBase::attachTimeoutManager(AsyncTimeout* obj,
738                                       InternalEnum internal) {
739
740   struct event* ev = obj->getEvent();
741   assert(ev->ev_base == nullptr);
742
743   event_base_set(getLibeventBase(), ev);
744   if (internal == AsyncTimeout::InternalEnum::INTERNAL) {
745     // Set the EVLIST_INTERNAL flag
746     event_ref_flags(ev) |= EVLIST_INTERNAL;
747   }
748 }
749
750 void EventBase::detachTimeoutManager(AsyncTimeout* obj) {
751   cancelTimeout(obj);
752   struct event* ev = obj->getEvent();
753   ev->ev_base = nullptr;
754 }
755
756 bool EventBase::scheduleTimeout(AsyncTimeout* obj,
757                                  TimeoutManager::timeout_type timeout) {
758   assert(isInEventBaseThread());
759   // Set up the timeval and add the event
760   struct timeval tv;
761   tv.tv_sec = timeout.count() / 1000LL;
762   tv.tv_usec = (timeout.count() % 1000LL) * 1000LL;
763
764   struct event* ev = obj->getEvent();
765   if (event_add(ev, &tv) < 0) {
766     LOG(ERROR) << "EventBase: failed to schedule timeout: " << strerror(errno);
767     return false;
768   }
769
770   return true;
771 }
772
773 void EventBase::cancelTimeout(AsyncTimeout* obj) {
774   assert(isInEventBaseThread());
775   struct event* ev = obj->getEvent();
776   if (EventUtil::isEventRegistered(ev)) {
777     event_del(ev);
778   }
779 }
780
781 void EventBase::setName(const std::string& name) {
782   assert(isInEventBaseThread());
783   name_ = name;
784
785   if (isRunning()) {
786     setThreadName(loopThread_.load(std::memory_order_relaxed),
787                   name_);
788   }
789 }
790
791 const std::string& EventBase::getName() {
792   assert(isInEventBaseThread());
793   return name_;
794 }
795
796 const char* EventBase::getLibeventVersion() { return event_get_version(); }
797 const char* EventBase::getLibeventMethod() { return event_get_method(); }
798
799 } // folly