2 * Copyright 2014 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/Baton.h>
24 #include <folly/ThreadName.h>
25 #include <folly/io/async/NotificationQueue.h>
27 #include <boost/static_assert.hpp>
35 using folly::EventBase;
37 template <typename Callback>
38 class FunctionLoopCallback : public EventBase::LoopCallback {
40 explicit FunctionLoopCallback(Cob&& function)
41 : function_(std::move(function)) {}
43 explicit FunctionLoopCallback(const Cob& function)
44 : function_(function) {}
46 virtual void runLoopCallback() noexcept {
62 * EventBase::FunctionRunner
65 class EventBase::FunctionRunner
66 : public NotificationQueue<std::pair<void (*)(void*), void*>>::Consumer {
68 void messageAvailable(std::pair<void (*)(void*), void*>&& msg) {
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_);
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.
84 // If function is nullptr, just log and move on
86 LOG(ERROR) << "nullptr callback registered to be run in "
87 << "event base thread";
91 // The function should never throw an exception, because we have no
92 // way of knowing what sort of error handling to perform.
94 // If it does throw, log a message and abort the program.
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();
102 LOG(ERROR) << "runInEventBaseThread() function threw an exception";
109 * EventBase::CobTimeout methods
112 void EventBase::CobTimeout::timeoutExpired() noexcept {
113 // For now, we just swallow any exceptions that the callback threw.
116 } catch (const std::exception& ex) {
117 LOG(ERROR) << "EventBase::runAfterDelay() callback threw "
118 << typeid(ex).name() << " exception: " << ex.what();
120 LOG(ERROR) << "EventBase::runAfterDelay() callback threw non-exception "
124 // The CobTimeout object was allocated on the heap by runAfterDelay(),
125 // so delete it now that the it has fired.
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.
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_;
143 EventBase::EventBase()
144 : runOnceCallbacks_(nullptr)
150 , avgLoopTime_(2000000)
151 , maxLatencyLoopTime_(avgLoopTime_)
152 , nextLoopCnt_(-40) // Early wrap-around so bugs will manifest soon
153 , latestLoopCnt_(nextLoopCnt_)
156 , observerSampleCount_(0) {
158 std::lock_guard<std::mutex> lock(libevent_mutex_);
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().
166 event_set(&ev, 0, 0, nullptr, nullptr);
167 evb_ = (ev.ev_base) ? event_base_new() : event_init();
169 if (UNLIKELY(evb_ == nullptr)) {
170 LOG(ERROR) << "EventBase(): Failed to init event base.";
171 folly::throwSystemError("error in EventBase::EventBase()");
173 VLOG(5) << "EventBase(): Created.";
174 initNotificationQueue();
175 RequestContext::getStaticContext();
178 // takes ownership of the event_base
179 EventBase::EventBase(event_base* evb)
180 : runOnceCallbacks_(nullptr)
187 , avgLoopTime_(2000000)
188 , maxLatencyLoopTime_(avgLoopTime_)
189 , nextLoopCnt_(-40) // Early wrap-around so bugs will manifest soon
190 , latestLoopCnt_(nextLoopCnt_)
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");
198 initNotificationQueue();
199 RequestContext::getStaticContext();
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();
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
214 while (!pendingCobTimeouts_.empty()) {
215 CobTimeout* timeout = &pendingCobTimeouts_.front();
219 while (!runBeforeLoopCallbacks_.empty()) {
220 delete &runBeforeLoopCallbacks_.front();
223 (void) runLoopCallbacks(false);
225 if (!fnRunner_->consumeUntilDrained()) {
226 LOG(ERROR) << "~EventBase(): Unable to drain notification queue";
229 // Stop consumer before deleting NotificationQueue
230 fnRunner_->stopConsuming();
232 std::lock_guard<std::mutex> lock(libevent_mutex_);
233 event_base_free(evb_);
235 VLOG(5) << "EventBase(): Destroyed.";
238 int EventBase::getNotificationQueueSize() const {
239 return queue_->size();
242 void EventBase::setMaxReadAtOnce(uint32_t maxAtOnce) {
243 fnRunner_->setMaxReadAtOnce(maxAtOnce);
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;
251 maxLatencyLoopTime_.setTimeInterval(us);
252 avgLoopTime_.setTimeInterval(us);
254 LOG(ERROR) << "non-positive arg to setLoadAvgMsec()";
258 void EventBase::resetLoadAvg(double value) {
259 avgLoopTime_.reset(value);
260 maxLatencyLoopTime_.reset(value);
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();
268 return std::chrono::duration_cast<std::chrono::milliseconds>(result);
271 void EventBase::waitUntilRunning() {
272 while (!isRunning()) {
277 // enters the event_base loop -- will only exit when forced to
278 bool EventBase::loop() {
282 bool EventBase::loopOnce(int flags) {
283 return loopBody(flags | EVLOOP_ONCE);
286 bool EventBase::loopBody(int flags) {
287 VLOG(5) << "EventBase(): Starting loop.";
289 bool ranLoopCallbacks;
290 bool blocking = !(flags & EVLOOP_NONBLOCK);
291 bool once = (flags & EVLOOP_ONCE);
293 loopThread_.store(pthread_self(), std::memory_order_release);
295 if (!name_.empty()) {
296 setThreadName(name_);
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();
303 // TODO: Read stop_ atomically with an acquire barrier.
307 // Run the before loop callbacks
308 LoopCallbackList callbacks;
309 callbacks.swap(runBeforeLoopCallbacks_);
311 while(!callbacks.empty()) {
312 auto* item = &callbacks.front();
313 callbacks.pop_front();
314 item->runLoopCallback();
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);
322 res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK);
325 ranLoopCallbacks = runLoopCallbacks();
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;
331 avgLoopTime_.addSample(idle, busy);
332 maxLatencyLoopTime_.addSample(idle, busy);
335 if (observerSampleCount_++ == observer_->getSampleRate()) {
336 observerSampleCount_ = 0;
337 observer_->loopSample(busy, idle);
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();
350 // see if our average loop time has exceeded our limit
351 if ((maxLatency_ > 0) &&
352 (maxLatencyLoopTime_.get() > double(maxLatency_))) {
354 // back off temporarily -- don't keep spamming maxLatencyCob_
355 // if we're only a bit over the limit
356 maxLatencyLoopTime_.dampen(0.9);
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();
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
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.
370 if (getNotificationQueueSize() > 0) {
371 fnRunner_->handlerReady(0);
377 VLOG(5) << "EventBase " << this << " loop time: " <<
378 getTimeDelta(&prev).count();
384 // Reset stop_ so loop() can be called again
388 LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
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;
397 loopThread_.store(0, std::memory_order_release);
399 VLOG(5) << "EventBase(): Done with loop.";
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());
412 // Restore the notification queue internal flag
413 fnRunner_->stopConsuming();
414 fnRunner_->startConsumingInternal(this, queue_.get());
417 folly::throwSystemError("error in EventBase::loopForever()");
421 bool EventBase::bumpHandlingTime() {
422 VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
423 " (loop) latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
424 if(nothingHandledYet()) {
425 latestLoopCnt_ = nextLoopCnt_;
427 startWork_ = std::chrono::duration_cast<std::chrono::microseconds>(
428 std::chrono::steady_clock::now().time_since_epoch()).count();
430 VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
431 " (loop) startWork_ " << startWork_;
437 void EventBase::terminateLoopSoon() {
438 VLOG(5) << "EventBase(): Received terminateLoopSoon() command.";
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
445 // Call event_base_loopbreak() so that libevent will exit the next time
447 event_base_loopbreak(evb_);
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.
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
459 queue_->putMessage(std::make_pair(nullptr, nullptr));
461 // We don't care if putMessage() fails. This likely means
462 // the EventBase already has lots of events waiting anyway.
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);
473 loopCallbacks_.push_back(*callback);
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);
484 loopCallbacks_.push_back(*wrapper);
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);
495 loopCallbacks_.push_back(*wrapper);
499 void EventBase::runOnDestruction(LoopCallback* callback) {
500 DCHECK(isInEventBaseThread());
501 callback->cancelLoopCallback();
502 onDestructionCallbacks_.push_back(*callback);
505 void EventBase::runBeforeLoop(LoopCallback* callback) {
506 DCHECK(isInEventBaseThread());
507 callback->cancelLoopCallback();
508 runBeforeLoopCallbacks_.push_back(*callback);
511 bool EventBase::runInEventBaseThread(void (*fn)(void*), void* arg) {
513 // It will be received by the FunctionRunner in the EventBase's thread.
515 // We try not to schedule nullptr callbacks
517 LOG(ERROR) << "EventBase " << this
518 << ": Scheduling nullptr callbacks is not allowed";
522 // Short-circuit if we are already in our event base
523 if (inRunningEventBaseThread()) {
524 runInLoop(new RunInLoopCallback(fn, arg));
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();
540 bool EventBase::runInEventBaseThread(const Cob& fn) {
541 // Short-circuit if we are already in our event base
542 if (inRunningEventBaseThread()) {
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
551 fnCopy = new Cob(fn);
552 } catch (const std::bad_alloc& ex) {
553 LOG(ERROR) << "failed to allocate tr::function copy "
554 << "for runInEventBaseThread()";
558 if (!runInEventBaseThread(&EventBase::runFunctionPtr, fnCopy)) {
566 bool EventBase::runInEventBaseThreadAndWait(void (*fn)(void*), void* arg) {
567 if (inRunningEventBaseThread()) {
568 LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
574 runInEventBaseThread([&] {
583 bool EventBase::runInEventBaseThreadAndWait(const Cob& fn) {
584 if (inRunningEventBaseThread()) {
585 LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
591 runInEventBaseThread([&] {
600 bool EventBase::runAfterDelay(const Cob& cob,
602 TimeoutManager::InternalEnum in) {
603 CobTimeout* timeout = new CobTimeout(this, cob, in);
604 if (!timeout->scheduleTimeout(milliseconds)) {
609 pendingCobTimeouts_.push_back(*timeout);
613 bool EventBase::runLoopCallbacks(bool setContext) {
614 if (!loopCallbacks_.empty()) {
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.
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_ = ¤tCallbacks;
628 while (!currentCallbacks.empty()) {
629 LoopCallback* callback = ¤tCallbacks.front();
630 currentCallbacks.pop_front();
632 RequestContext::setContext(callback->context_);
634 callback->runLoopCallback();
637 runOnceCallbacks_ = nullptr;
643 void EventBase::initNotificationQueue() {
644 // Infinite size queue
645 queue_.reset(new NotificationQueue<std::pair<void (*)(void*), void*>>());
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());
652 // Mark this as an internal event, so event_base_loop() will return if
653 // there are no other events besides this one installed.
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());
664 void EventBase::SmoothLoopTime::setTimeInterval(uint64_t timeInterval) {
665 expCoeff_ = -1.0/timeInterval;
666 VLOG(11) << "expCoeff_ " << expCoeff_ << " " << __PRETTY_FUNCTION__;
669 void EventBase::SmoothLoopTime::reset(double value) {
673 void EventBase::SmoothLoopTime::addSample(int64_t idle, int64_t busy) {
675 * Position at which the busy sample is considered to be taken.
676 * (Allows to quickly skew our average without editing much code)
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
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_;
693 double coeff = exp(idle * expCoeff_);
695 value_ += (1.0 - coeff) * busy;
698 bool EventBase::nothingHandledYet() {
699 VLOG(11) << "latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
700 return (nextLoopCnt_ != latestLoopCnt_);
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.
708 // If it does throw, log a message and abort the program.
711 } catch (const std::exception &ex) {
712 LOG(ERROR) << "runInEventBaseThread() std::function threw a "
713 << typeid(ex).name() << " exception: " << ex.what();
716 LOG(ERROR) << "runInEventBaseThread() std::function threw an exception";
720 // The function object was allocated by runInEventBaseThread().
721 // Delete it once it has been run.
725 EventBase::RunInLoopCallback::RunInLoopCallback(void (*fn)(void*), void* arg)
729 void EventBase::RunInLoopCallback::runLoopCallback() noexcept {
734 void EventBase::attachTimeoutManager(AsyncTimeout* obj,
735 InternalEnum internal) {
737 struct event* ev = obj->getEvent();
738 assert(ev->ev_base == nullptr);
740 event_base_set(getLibeventBase(), ev);
741 if (internal == AsyncTimeout::InternalEnum::INTERNAL) {
742 // Set the EVLIST_INTERNAL flag
743 ev->ev_flags |= EVLIST_INTERNAL;
747 void EventBase::detachTimeoutManager(AsyncTimeout* obj) {
749 struct event* ev = obj->getEvent();
750 ev->ev_base = nullptr;
753 bool EventBase::scheduleTimeout(AsyncTimeout* obj,
754 std::chrono::milliseconds timeout) {
755 assert(isInEventBaseThread());
756 // Set up the timeval and add the event
758 tv.tv_sec = timeout.count() / 1000LL;
759 tv.tv_usec = (timeout.count() % 1000LL) * 1000LL;
761 struct event* ev = obj->getEvent();
762 if (event_add(ev, &tv) < 0) {
763 LOG(ERROR) << "EventBase: failed to schedule timeout: " << strerror(errno);
770 void EventBase::cancelTimeout(AsyncTimeout* obj) {
771 assert(isInEventBaseThread());
772 struct event* ev = obj->getEvent();
773 if (EventUtil::isEventRegistered(ev)) {
778 void EventBase::setName(const std::string& name) {
779 assert(isInEventBaseThread());
783 setThreadName(loopThread_.load(std::memory_order_relaxed),
788 const std::string& EventBase::getName() {
789 assert(isInEventBaseThread());
793 const char* EventBase::getLibeventVersion() { return event_get_version(); }
794 const char* EventBase::getLibeventMethod() { return event_get_method(); }