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