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