2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __STDC_FORMAT_MACROS
18 #define __STDC_FORMAT_MACROS
21 #include <folly/io/async/EventBase.h>
23 #include <folly/ThreadName.h>
24 #include <folly/io/async/NotificationQueue.h>
26 #include <boost/static_assert.hpp>
27 #include <condition_variable>
35 using folly::EventBase;
37 class FunctionLoopCallback : public EventBase::LoopCallback {
39 explicit FunctionLoopCallback(EventBase::Func&& function)
40 : function_(std::move(function)) {}
42 void runLoopCallback() noexcept override {
48 EventBase::Func function_;
55 * EventBase::FunctionRunner
58 class EventBase::FunctionRunner
59 : public NotificationQueue<EventBase::Func>::Consumer {
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_);
71 // terminateLoopSoon() sends a null message just to
72 // wake up the loop. We can ignore these messages.
76 // The function should never throw an exception, because we have no
77 // way of knowing what sort of error handling to perform.
79 // If it does throw, log a message and abort the program.
82 } catch (const std::exception& ex) {
83 LOG(ERROR) << "runInEventBaseThread() function threw a "
84 << typeid(ex).name() << " exception: " << ex.what();
87 LOG(ERROR) << "runInEventBaseThread() function threw an exception";
94 * EventBase::CobTimeout methods
97 void EventBase::CobTimeout::timeoutExpired() noexcept {
98 // For now, we just swallow any exceptions that the callback threw.
101 } catch (const std::exception& ex) {
102 LOG(ERROR) << "EventBase::runAfterDelay() callback threw "
103 << typeid(ex).name() << " exception: " << ex.what();
105 LOG(ERROR) << "EventBase::runAfterDelay() callback threw non-exception "
109 // The CobTimeout object was allocated on the heap by runAfterDelay(),
110 // so delete it now that the it has fired.
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.
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_;
128 EventBase::EventBase(bool enableTimeMeasurement)
129 : runOnceCallbacks_(nullptr)
135 , avgLoopTime_(2000000)
136 , maxLatencyLoopTime_(avgLoopTime_)
137 , enableTimeMeasurement_(enableTimeMeasurement)
138 , nextLoopCnt_(-40) // Early wrap-around so bugs will manifest soon
139 , latestLoopCnt_(nextLoopCnt_)
142 , observerSampleCount_(0)
143 , executionObserver_(nullptr) {
146 std::lock_guard<std::mutex> lock(libevent_mutex_);
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);
160 evb_ = event_base_new();
163 if (UNLIKELY(evb_ == nullptr)) {
164 LOG(ERROR) << "EventBase(): Failed to init event base.";
165 folly::throwSystemError("error in EventBase::EventBase()");
167 VLOG(5) << "EventBase(): Created.";
168 initNotificationQueue();
169 RequestContext::saveContext();
172 // takes ownership of the event_base
173 EventBase::EventBase(event_base* evb, bool enableTimeMeasurement)
174 : runOnceCallbacks_(nullptr)
181 , avgLoopTime_(2000000)
182 , maxLatencyLoopTime_(avgLoopTime_)
183 , enableTimeMeasurement_(enableTimeMeasurement)
184 , nextLoopCnt_(-40) // Early wrap-around so bugs will manifest soon
185 , latestLoopCnt_(nextLoopCnt_)
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");
194 initNotificationQueue();
195 RequestContext::saveContext();
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();
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
210 while (!pendingCobTimeouts_.empty()) {
211 CobTimeout* timeout = &pendingCobTimeouts_.front();
215 while (!runBeforeLoopCallbacks_.empty()) {
216 delete &runBeforeLoopCallbacks_.front();
219 (void) runLoopCallbacks(false);
221 if (!fnRunner_->consumeUntilDrained()) {
222 LOG(ERROR) << "~EventBase(): Unable to drain notification queue";
225 // Stop consumer before deleting NotificationQueue
226 fnRunner_->stopConsuming();
228 std::lock_guard<std::mutex> lock(libevent_mutex_);
229 event_base_free(evb_);
232 while (!runAfterDrainCallbacks_.empty()) {
233 LoopCallback* callback = &runAfterDrainCallbacks_.front();
234 runAfterDrainCallbacks_.pop_front();
235 callback->runLoopCallback();
239 std::lock_guard<std::mutex> lock(localStorageMutex_);
240 for (auto storage : localStorageToDtor_) {
241 storage->onEventBaseDestruction(*this);
244 VLOG(5) << "EventBase(): Destroyed.";
247 int EventBase::getNotificationQueueSize() const {
248 return queue_->size();
251 void EventBase::setMaxReadAtOnce(uint32_t maxAtOnce) {
252 fnRunner_->setMaxReadAtOnce(maxAtOnce);
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;
261 maxLatencyLoopTime_.setTimeInterval(us);
262 avgLoopTime_.setTimeInterval(us);
264 LOG(ERROR) << "non-positive arg to setLoadAvgMsec()";
268 void EventBase::resetLoadAvg(double value) {
269 assert(enableTimeMeasurement_);
270 avgLoopTime_.reset(value);
271 maxLatencyLoopTime_.reset(value);
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();
279 return std::chrono::duration_cast<std::chrono::milliseconds>(result);
282 void EventBase::waitUntilRunning() {
283 while (!isRunning()) {
288 // enters the event_base loop -- will only exit when forced to
289 bool EventBase::loop() {
293 bool EventBase::loopOnce(int flags) {
294 return loopBody(flags | EVLOOP_ONCE);
297 bool EventBase::loopBody(int flags) {
298 VLOG(5) << "EventBase(): Starting loop.";
300 bool ranLoopCallbacks;
301 bool blocking = !(flags & EVLOOP_NONBLOCK);
302 bool once = (flags & EVLOOP_ONCE);
304 // time-measurement variables.
305 std::chrono::steady_clock::time_point prev;
306 int64_t idleStart = 0;
310 loopThread_.store(pthread_self(), std::memory_order_release);
312 if (!name_.empty()) {
313 setThreadName(name_);
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();
322 while (!stop_.load(std::memory_order_acquire)) {
325 // Run the before loop callbacks
326 LoopCallbackList callbacks;
327 callbacks.swap(runBeforeLoopCallbacks_);
329 while(!callbacks.empty()) {
330 auto* item = &callbacks.front();
331 callbacks.pop_front();
332 item->runLoopCallback();
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);
340 res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK);
343 ranLoopCallbacks = runLoopCallbacks();
345 if (enableTimeMeasurement_) {
346 busy = std::chrono::duration_cast<std::chrono::microseconds>(
347 std::chrono::steady_clock::now().time_since_epoch()).count() -
349 idle = startWork_ - idleStart;
351 avgLoopTime_.addSample(idle, busy);
352 maxLatencyLoopTime_.addSample(idle, busy);
355 if (observerSampleCount_++ == observer_->getSampleRate()) {
356 observerSampleCount_ = 0;
357 observer_->loopSample(busy, idle);
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();
371 // see if our average loop time has exceeded our limit
372 if ((maxLatency_ > 0) &&
373 (maxLatencyLoopTime_.get() > double(maxLatency_))) {
375 // back off temporarily -- don't keep spamming maxLatencyCob_
376 // if we're only a bit over the limit
377 maxLatencyLoopTime_.dampen(0.9);
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();
384 VLOG(11) << "EventBase " << this << " did not timeout "
385 " time measurement is disabled "
386 " nothingHandledYet(): "<< nothingHandledYet();
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
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.
396 if (getNotificationQueueSize() > 0) {
397 fnRunner_->handlerReady(0);
403 if (enableTimeMeasurement_) {
404 VLOG(5) << "EventBase " << this << " loop time: " <<
405 getTimeDelta(&prev).count();
412 // Reset stop_ so loop() can be called again
416 LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
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;
425 loopThread_.store({}, std::memory_order_release);
427 VLOG(5) << "EventBase(): Done with loop.";
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());
440 // Restore the notification queue internal flag
441 fnRunner_->stopConsuming();
442 fnRunner_->startConsumingInternal(this, queue_.get());
445 folly::throwSystemError("error in EventBase::loopForever()");
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_) {
456 startWork_ = std::chrono::duration_cast<std::chrono::microseconds>(
457 std::chrono::steady_clock::now().time_since_epoch()).count();
459 VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
460 " (loop) startWork_ " << startWork_;
467 void EventBase::terminateLoopSoon() {
468 VLOG(5) << "EventBase(): Received terminateLoopSoon() command.";
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
475 // Call event_base_loopbreak() so that libevent will exit the next time
477 event_base_loopbreak(evb_);
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.
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
489 queue_->putMessage(nullptr);
491 // We don't care if putMessage() fails. This likely means
492 // the EventBase already has lots of events waiting anyway.
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);
503 loopCallbacks_.push_back(*callback);
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);
514 loopCallbacks_.push_back(*wrapper);
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);
525 void EventBase::runOnDestruction(LoopCallback* callback) {
526 std::lock_guard<std::mutex> lg(onDestructionCallbacksMutex_);
527 callback->cancelLoopCallback();
528 onDestructionCallbacks_.push_back(*callback);
531 void EventBase::runBeforeLoop(LoopCallback* callback) {
532 DCHECK(isInEventBaseThread());
533 callback->cancelLoopCallback();
534 runBeforeLoopCallbacks_.push_back(*callback);
537 bool EventBase::runInEventBaseThread(Func fn) {
539 // It will be received by the FunctionRunner in the EventBase's thread.
541 // We try not to schedule nullptr callbacks
543 LOG(ERROR) << "EventBase " << this
544 << ": Scheduling nullptr callbacks is not allowed";
548 // Short-circuit if we are already in our event base
549 if (inRunningEventBaseThread()) {
550 runInLoop(std::move(fn));
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();
566 bool EventBase::runInEventBaseThreadAndWait(Func fn) {
567 if (inRunningEventBaseThread()) {
568 LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
575 std::condition_variable cv;
576 runInEventBaseThread([&] {
578 std::unique_lock<std::mutex> l(m);
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
587 std::unique_lock<std::mutex> l(m);
588 cv.wait(l, [&] { return ready; });
593 bool EventBase::runImmediatelyOrRunInEventBaseThreadAndWait(Func fn) {
594 if (isInEventBaseThread()) {
598 return runInEventBaseThreadAndWait(std::move(fn));
602 void EventBase::runAfterDelay(
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");
612 bool EventBase::tryRunAfterDelay(
614 uint32_t milliseconds,
615 TimeoutManager::InternalEnum in) {
616 CobTimeout* timeout = new CobTimeout(this, std::move(cob), in);
617 if (!timeout->scheduleTimeout(milliseconds)) {
621 pendingCobTimeouts_.push_back(*timeout);
625 bool EventBase::runLoopCallbacks(bool setContext) {
626 if (!loopCallbacks_.empty()) {
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.
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_ = ¤tCallbacks;
640 while (!currentCallbacks.empty()) {
641 LoopCallback* callback = ¤tCallbacks.front();
642 currentCallbacks.pop_front();
644 RequestContext::setContext(callback->context_);
646 callback->runLoopCallback();
649 runOnceCallbacks_ = nullptr;
655 void EventBase::initNotificationQueue() {
656 // Infinite size queue
657 queue_.reset(new NotificationQueue<Func>());
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());
664 // Mark this as an internal event, so event_base_loop() will return if
665 // there are no other events besides this one installed.
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());
676 void EventBase::SmoothLoopTime::setTimeInterval(uint64_t timeInterval) {
677 expCoeff_ = -1.0/timeInterval;
678 VLOG(11) << "expCoeff_ " << expCoeff_ << " " << __PRETTY_FUNCTION__;
681 void EventBase::SmoothLoopTime::reset(double value) {
685 void EventBase::SmoothLoopTime::addSample(int64_t idle, int64_t busy) {
687 * Position at which the busy sample is considered to be taken.
688 * (Allows to quickly skew our average without editing much code)
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
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_;
705 double coeff = exp(idle * expCoeff_);
707 value_ += (1.0 - coeff) * busy;
710 bool EventBase::nothingHandledYet() {
711 VLOG(11) << "latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
712 return (nextLoopCnt_ != latestLoopCnt_);
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.
720 // If it does throw, log a message and abort the program.
723 } catch (const std::exception &ex) {
724 LOG(ERROR) << "runInEventBaseThread() std::function threw a "
725 << typeid(ex).name() << " exception: " << ex.what();
728 LOG(ERROR) << "runInEventBaseThread() std::function threw an exception";
732 // The function object was allocated by runInEventBaseThread().
733 // Delete it once it has been run.
737 void EventBase::attachTimeoutManager(AsyncTimeout* obj,
738 InternalEnum internal) {
740 struct event* ev = obj->getEvent();
741 assert(ev->ev_base == nullptr);
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;
750 void EventBase::detachTimeoutManager(AsyncTimeout* obj) {
752 struct event* ev = obj->getEvent();
753 ev->ev_base = nullptr;
756 bool EventBase::scheduleTimeout(AsyncTimeout* obj,
757 TimeoutManager::timeout_type timeout) {
758 assert(isInEventBaseThread());
759 // Set up the timeval and add the event
761 tv.tv_sec = timeout.count() / 1000LL;
762 tv.tv_usec = (timeout.count() % 1000LL) * 1000LL;
764 struct event* ev = obj->getEvent();
765 if (event_add(ev, &tv) < 0) {
766 LOG(ERROR) << "EventBase: failed to schedule timeout: " << strerror(errno);
773 void EventBase::cancelTimeout(AsyncTimeout* obj) {
774 assert(isInEventBaseThread());
775 struct event* ev = obj->getEvent();
776 if (EventUtil::isEventRegistered(ev)) {
781 void EventBase::setName(const std::string& name) {
782 assert(isInEventBaseThread());
786 setThreadName(loopThread_.load(std::memory_order_relaxed),
791 const std::string& EventBase::getName() {
792 assert(isInEventBaseThread());
796 const char* EventBase::getLibeventVersion() { return event_get_version(); }
797 const char* EventBase::getLibeventMethod() { return event_get_method(); }