2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 #ifndef __STDC_FORMAT_MACROS
21 #define __STDC_FORMAT_MACROS
24 #include "folly/io/async/EventBase.h"
26 #include "folly/io/async/NotificationQueue.h"
28 #include <boost/static_assert.hpp>
36 using folly::EventBase;
38 class Tr1FunctionLoopCallback : public EventBase::LoopCallback {
40 explicit Tr1FunctionLoopCallback(const Cob& function)
41 : function_(function) {}
43 virtual void runLoopCallback() noexcept {
59 * EventBase::FunctionRunner
62 class EventBase::FunctionRunner
63 : public NotificationQueue<std::pair<void (*)(void*), void*>>::Consumer {
65 void messageAvailable(std::pair<void (*)(void*), void*>&& msg) {
67 // In libevent2, internal events do not break the loop.
68 // Most users would expect loop(), followed by runInEventBaseThread(),
69 // to break the loop and check if it should exit or not.
70 // To have similar bejaviour to libevent1.4, tell the loop to break here.
71 // Note that loop() may still continue to loop, but it will also check the
72 // stop_ flag as well as runInLoop callbacks, etc.
73 event_base_loopbreak(getEventBase()->evb_);
75 if (msg.first == nullptr && msg.second == nullptr) {
76 // terminateLoopSoon() sends a null message just to
77 // wake up the loop. We can ignore these messages.
81 // If function is nullptr, just log and move on
83 LOG(ERROR) << "nullptr callback registered to be run in "
84 << "event base thread";
88 // The function should never throw an exception, because we have no
89 // way of knowing what sort of error handling to perform.
91 // If it does throw, log a message and abort the program.
93 msg.first(msg.second);
94 } catch (const std::exception& ex) {
95 LOG(ERROR) << "runInEventBaseThread() function threw a "
96 << typeid(ex).name() << " exception: " << ex.what();
99 LOG(ERROR) << "runInEventBaseThread() function threw an exception";
106 * EventBase::CobTimeout methods
109 void EventBase::CobTimeout::timeoutExpired() noexcept {
110 // For now, we just swallow any exceptions that the callback threw.
113 } catch (const std::exception& ex) {
114 LOG(ERROR) << "EventBase::runAfterDelay() callback threw "
115 << typeid(ex).name() << " exception: " << ex.what();
117 LOG(ERROR) << "EventBase::runAfterDelay() callback threw non-exception "
121 // The CobTimeout object was allocated on the heap by runAfterDelay(),
122 // so delete it now that the it has fired.
130 EventBase::EventBase()
131 : runOnceCallbacks_(nullptr)
134 , evb_(static_cast<event_base*>(event_init()))
138 , avgLoopTime_(2000000)
139 , maxLatencyLoopTime_(avgLoopTime_)
140 , nextLoopCnt_(-40) // Early wrap-around so bugs will manifest soon
141 , latestLoopCnt_(nextLoopCnt_)
144 , observerSampleCount_(0) {
145 VLOG(5) << "EventBase(): Created.";
146 initNotificationQueue();
147 RequestContext::getStaticContext();
150 // takes ownership of the event_base
151 EventBase::EventBase(event_base* evb)
152 : runOnceCallbacks_(nullptr)
159 , avgLoopTime_(2000000)
160 , maxLatencyLoopTime_(avgLoopTime_)
161 , nextLoopCnt_(-40) // Early wrap-around so bugs will manifest soon
162 , latestLoopCnt_(nextLoopCnt_)
165 , observerSampleCount_(0) {
166 initNotificationQueue();
167 RequestContext::getStaticContext();
170 EventBase::~EventBase() {
171 // Delete any unfired CobTimeout objects, so that we don't leak memory
172 // (Note that we don't fire them. The caller is responsible for cleaning up
173 // its own data structures if it destroys the EventBase with unfired events
175 while (!pendingCobTimeouts_.empty()) {
176 CobTimeout* timeout = &pendingCobTimeouts_.front();
180 (void) runLoopCallbacks(false);
182 // Stop consumer before deleting NotificationQueue
183 fnRunner_->stopConsuming();
184 event_base_free(evb_);
185 VLOG(5) << "EventBase(): Destroyed.";
188 int EventBase::getNotificationQueueSize() const {
189 return queue_->size();
192 // Set smoothing coefficient for loop load average; input is # of milliseconds
193 // for exp(-1) decay.
194 void EventBase::setLoadAvgMsec(uint32_t ms) {
195 uint64_t us = 1000 * ms;
197 maxLatencyLoopTime_.setTimeInterval(us);
198 avgLoopTime_.setTimeInterval(us);
200 LOG(ERROR) << "non-positive arg to setLoadAvgMsec()";
204 void EventBase::resetLoadAvg(double value) {
205 avgLoopTime_.reset(value);
206 maxLatencyLoopTime_.reset(value);
209 static int64_t getTimeDelta(int64_t *prev) {
210 int64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(
211 std::chrono::steady_clock::now().time_since_epoch()).count();
212 int64_t delta = now - *prev;
217 void EventBase::waitUntilRunning() {
218 while (!isRunning()) {
223 // enters the event_base loop -- will only exit when forced to
224 bool EventBase::loop() {
225 VLOG(5) << "EventBase(): Starting loop.";
227 bool ranLoopCallbacks;
230 loopThread_.store(pthread_self(), std::memory_order_release);
232 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12)
233 if (!name_.empty()) {
234 pthread_setname_np(pthread_self(), name_.c_str());
238 int64_t prev = std::chrono::duration_cast<std::chrono::milliseconds>(
239 std::chrono::steady_clock::now().time_since_epoch()).count();
240 int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
241 std::chrono::steady_clock::now().time_since_epoch()).count();
243 // TODO: Read stop_ atomically with an acquire barrier.
247 // nobody can add loop callbacks from within this thread if
248 // we don't have to handle anything to start with...
249 nonBlocking = (loopCallbacks_.empty() ? 0 : EVLOOP_NONBLOCK);
250 res = event_base_loop(evb_, EVLOOP_ONCE | nonBlocking);
251 ranLoopCallbacks = runLoopCallbacks();
253 int64_t busy = std::chrono::duration_cast<std::chrono::microseconds>(
254 std::chrono::steady_clock::now().time_since_epoch()).count() - startWork_;
255 int64_t idle = startWork_ - idleStart;
257 avgLoopTime_.addSample(idle, busy);
258 maxLatencyLoopTime_.addSample(idle, busy);
261 if (observerSampleCount_++ == observer_->getSampleRate()) {
262 observerSampleCount_ = 0;
263 observer_->loopSample(busy, idle);
267 VLOG(11) << "EventBase " << this << " did not timeout "
268 " loop time guess: " << busy + idle <<
269 " idle time: " << idle <<
270 " busy time: " << busy <<
271 " avgLoopTime: " << avgLoopTime_.get() <<
272 " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() <<
273 " maxLatency_: " << maxLatency_ <<
274 " nothingHandledYet(): "<< nothingHandledYet();
276 // see if our average loop time has exceeded our limit
277 if ((maxLatency_ > 0) &&
278 (maxLatencyLoopTime_.get() > double(maxLatency_))) {
280 // back off temporarily -- don't keep spamming maxLatencyCob_
281 // if we're only a bit over the limit
282 maxLatencyLoopTime_.dampen(0.9);
285 // Our loop run did real work; reset the idle timer
286 idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
287 std::chrono::steady_clock::now().time_since_epoch()).count();
289 // If the event loop indicate that there were no more events, and
290 // we also didn't have any loop callbacks to run, there is nothing left to
292 if (res != 0 && !ranLoopCallbacks) {
293 // Since Notification Queue is marked 'internal' some events may not have
294 // run. Run them manually if so, and continue looping.
296 if (getNotificationQueueSize() > 0) {
297 fnRunner_->handlerReady(0);
303 VLOG(5) << "EventBase " << this << " loop time: " << getTimeDelta(&prev);
305 // Reset stop_ so loop() can be called again
309 LOG(ERROR) << "EventBase: -- error in event loop, res = " << res;
311 } else if (res == 1) {
312 VLOG(5) << "EventBase: ran out of events (exiting loop)!";
313 } else if (res > 1) {
314 LOG(ERROR) << "EventBase: unknown event loop result = " << res;
318 loopThread_.store(0, std::memory_order_release);
320 VLOG(5) << "EventBase(): Done with loop.";
324 void EventBase::loopForever() {
325 // Update the notification queue event to treat it as a normal (non-internal)
326 // event. The notification queue event always remains installed, and the main
327 // loop won't exit with it installed.
328 fnRunner_->stopConsuming();
329 fnRunner_->startConsuming(this, queue_.get());
333 // Restore the notification queue internal flag
334 fnRunner_->stopConsuming();
335 fnRunner_->startConsumingInternal(this, queue_.get());
338 folly::throwSystemError("error in EventBase::loopForever()");
342 bool EventBase::bumpHandlingTime() {
343 VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
344 " (loop) latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
345 if(nothingHandledYet()) {
346 latestLoopCnt_ = nextLoopCnt_;
348 startWork_ = std::chrono::duration_cast<std::chrono::microseconds>(
349 std::chrono::steady_clock::now().time_since_epoch()).count();
351 VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ <<
352 " (loop) startWork_ " << startWork_;
358 void EventBase::terminateLoopSoon() {
359 VLOG(5) << "EventBase(): Received terminateLoopSoon() command.";
365 // Set stop to true, so the event loop will know to exit.
366 // TODO: We should really use an atomic operation here with a release
370 // Call event_base_loopbreak() so that libevent will exit the next time
372 event_base_loopbreak(evb_);
374 // If terminateLoopSoon() is called from another thread,
375 // the EventBase thread might be stuck waiting for events.
376 // In this case, it won't wake up and notice that stop_ is set until it
377 // receives another event. Send an empty frame to the notification queue
378 // so that the event loop will wake up even if there are no other events.
380 // We don't care about the return value of trySendFrame(). If it fails
381 // this likely means the EventBase already has lots of events waiting
384 queue_->putMessage(std::make_pair(nullptr, nullptr));
386 // We don't care if putMessage() fails. This likely means
387 // the EventBase already has lots of events waiting anyway.
391 void EventBase::runInLoop(LoopCallback* callback, bool thisIteration) {
392 DCHECK(isInEventBaseThread());
393 callback->cancelLoopCallback();
394 callback->context_ = RequestContext::saveContext();
395 if (runOnceCallbacks_ != nullptr && thisIteration) {
396 runOnceCallbacks_->push_back(*callback);
398 loopCallbacks_.push_back(*callback);
402 void EventBase::runInLoop(const Cob& cob, bool thisIteration) {
403 DCHECK(isInEventBaseThread());
404 Tr1FunctionLoopCallback* wrapper = new Tr1FunctionLoopCallback(cob);
405 wrapper->context_ = RequestContext::saveContext();
406 if (runOnceCallbacks_ != nullptr && thisIteration) {
407 runOnceCallbacks_->push_back(*wrapper);
409 loopCallbacks_.push_back(*wrapper);
413 bool EventBase::runInEventBaseThread(void (*fn)(void*), void* arg) {
415 // It will be received by the FunctionRunner in the EventBase's thread.
417 // We try not to schedule nullptr callbacks
419 LOG(ERROR) << "EventBase " << this
420 << ": Scheduling nullptr callbacks is not allowed";
424 // Short-circuit if we are already in our event base
425 if (inRunningEventBaseThread()) {
426 runInLoop(new RunInLoopCallback(fn, arg));
432 queue_->putMessage(std::make_pair(fn, arg));
433 } catch (const std::exception& ex) {
434 LOG(ERROR) << "EventBase " << this << ": failed to schedule function "
435 << fn << "for EventBase thread: " << ex.what();
442 bool EventBase::runInEventBaseThread(const Cob& fn) {
443 // Short-circuit if we are already in our event base
444 if (inRunningEventBaseThread()) {
450 // Allocate a copy of the function so we can pass it to the other thread
451 // The other thread will delete this copy once the function has been run
453 fnCopy = new Cob(fn);
454 } catch (const std::bad_alloc& ex) {
455 LOG(ERROR) << "failed to allocate tr::function copy "
456 << "for runInEventBaseThread()";
460 if (!runInEventBaseThread(&EventBase::runTr1FunctionPtr, fnCopy)) {
468 bool EventBase::runAfterDelay(const Cob& cob,
470 TimeoutManager::InternalEnum in) {
471 CobTimeout* timeout = new CobTimeout(this, cob, in);
472 if (!timeout->scheduleTimeout(milliseconds)) {
477 pendingCobTimeouts_.push_back(*timeout);
481 bool EventBase::runLoopCallbacks(bool setContext) {
482 if (!loopCallbacks_.empty()) {
484 // Swap the loopCallbacks_ list with a temporary list on our stack.
485 // This way we will only run callbacks scheduled at the time
486 // runLoopCallbacks() was invoked.
488 // If any of these callbacks in turn call runInLoop() to schedule more
489 // callbacks, those new callbacks won't be run until the next iteration
490 // around the event loop. This prevents runInLoop() callbacks from being
491 // able to start file descriptor and timeout based events.
492 LoopCallbackList currentCallbacks;
493 currentCallbacks.swap(loopCallbacks_);
494 runOnceCallbacks_ = ¤tCallbacks;
496 while (!currentCallbacks.empty()) {
497 LoopCallback* callback = ¤tCallbacks.front();
498 currentCallbacks.pop_front();
500 RequestContext::setContext(callback->context_);
502 callback->runLoopCallback();
505 runOnceCallbacks_ = nullptr;
511 void EventBase::initNotificationQueue() {
512 // Infinite size queue
513 queue_.reset(new NotificationQueue<std::pair<void (*)(void*), void*>>());
515 // We allocate fnRunner_ separately, rather than declaring it directly
516 // as a member of EventBase solely so that we don't need to include
517 // NotificationQueue.h from EventBase.h
518 fnRunner_.reset(new FunctionRunner());
520 // Mark this as an internal event, so event_base_loop() will return if
521 // there are no other events besides this one installed.
523 // Most callers don't care about the internal notification queue used by
524 // EventBase. The queue is always installed, so if we did count the queue as
525 // an active event, loop() would never exit with no more events to process.
526 // Users can use loopForever() if they do care about the notification queue.
527 // (This is useful for EventBase threads that do nothing but process
528 // runInEventBaseThread() notifications.)
529 fnRunner_->startConsumingInternal(this, queue_.get());
532 void EventBase::SmoothLoopTime::setTimeInterval(uint64_t timeInterval) {
533 expCoeff_ = -1.0/timeInterval;
534 VLOG(11) << "expCoeff_ " << expCoeff_ << " " << __PRETTY_FUNCTION__;
537 void EventBase::SmoothLoopTime::reset(double value) {
541 void EventBase::SmoothLoopTime::addSample(int64_t idle, int64_t busy) {
543 * Position at which the busy sample is considered to be taken.
544 * (Allows to quickly skew our average without editing much code)
546 enum BusySamplePosition {
547 RIGHT = 0, // busy sample placed at the end of the iteration
548 CENTER = 1, // busy sample placed at the middle point of the iteration
549 LEFT = 2, // busy sample placed at the beginning of the iteration
552 VLOG(11) << "idle " << idle << " oldBusyLeftover_ " << oldBusyLeftover_ <<
553 " idle + oldBusyLeftover_ " << idle + oldBusyLeftover_ <<
554 " busy " << busy << " " << __PRETTY_FUNCTION__;
555 idle += oldBusyLeftover_ + busy;
556 oldBusyLeftover_ = (busy * BusySamplePosition::CENTER) / 2;
557 idle -= oldBusyLeftover_;
559 double coeff = exp(idle * expCoeff_);
561 value_ += (1.0 - coeff) * busy;
564 bool EventBase::nothingHandledYet() {
565 VLOG(11) << "latest " << latestLoopCnt_ << " next " << nextLoopCnt_;
566 return (nextLoopCnt_ != latestLoopCnt_);
570 void EventBase::runTr1FunctionPtr(Cob* fn) {
571 // The function should never throw an exception, because we have no
572 // way of knowing what sort of error handling to perform.
574 // If it does throw, log a message and abort the program.
577 } catch (const std::exception &ex) {
578 LOG(ERROR) << "runInEventBaseThread() std::function threw a "
579 << typeid(ex).name() << " exception: " << ex.what();
582 LOG(ERROR) << "runInEventBaseThread() std::function threw an exception";
586 // The function object was allocated by runInEventBaseThread().
587 // Delete it once it has been run.
591 EventBase::RunInLoopCallback::RunInLoopCallback(void (*fn)(void*), void* arg)
595 void EventBase::RunInLoopCallback::runLoopCallback() noexcept {
600 void EventBase::attachTimeoutManager(AsyncTimeout* obj,
601 InternalEnum internal) {
603 struct event* ev = obj->getEvent();
604 assert(ev->ev_base == nullptr);
606 event_base_set(getLibeventBase(), ev);
607 if (internal == AsyncTimeout::InternalEnum::INTERNAL) {
608 // Set the EVLIST_INTERNAL flag
609 ev->ev_flags |= EVLIST_INTERNAL;
613 void EventBase::detachTimeoutManager(AsyncTimeout* obj) {
615 struct event* ev = obj->getEvent();
616 ev->ev_base = nullptr;
619 bool EventBase::scheduleTimeout(AsyncTimeout* obj,
620 std::chrono::milliseconds timeout) {
621 assert(isInEventBaseThread());
622 // Set up the timeval and add the event
624 tv.tv_sec = timeout.count() / 1000LL;
625 tv.tv_usec = (timeout.count() % 1000LL) * 1000LL;
627 struct event* ev = obj->getEvent();
628 if (event_add(ev, &tv) < 0) {
629 LOG(ERROR) << "EventBase: failed to schedule timeout: " << strerror(errno);
636 void EventBase::cancelTimeout(AsyncTimeout* obj) {
637 assert(isInEventBaseThread());
638 struct event* ev = obj->getEvent();
639 if (EventUtil::isEventRegistered(ev)) {
644 void EventBase::setName(const std::string& name) {
645 assert(isInEventBaseThread());
647 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12)
649 pthread_setname_np(loopThread_.load(std::memory_order_relaxed),
655 const std::string& EventBase::getName() {
656 assert(isInEventBaseThread());