Commit 77249d4d authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

std::chrono'ize EventBase::loopBody

Summary: Because modern is good, and this allows for cleaner interaction with the other APIs being `std::chrono`ized.

Reviewed By: yfeldblum

Differential Revision: D4377773

fbshipit-source-id: 42ad5ad8476a3678e02e9314f9592f5449102f00
parent aee08623
...@@ -95,7 +95,7 @@ EventBase::EventBase(bool enableTimeMeasurement) ...@@ -95,7 +95,7 @@ EventBase::EventBase(bool enableTimeMeasurement)
, enableTimeMeasurement_(enableTimeMeasurement) , enableTimeMeasurement_(enableTimeMeasurement)
, nextLoopCnt_(uint64_t(-40)) // Early wrap-around so bugs will manifest soon , nextLoopCnt_(uint64_t(-40)) // Early wrap-around so bugs will manifest soon
, latestLoopCnt_(nextLoopCnt_) , latestLoopCnt_(nextLoopCnt_)
, startWork_(0) , startWork_()
, observer_(nullptr) , observer_(nullptr)
, observerSampleCount_(0) , observerSampleCount_(0)
, executionObserver_(nullptr) { , executionObserver_(nullptr) {
...@@ -141,7 +141,7 @@ EventBase::EventBase(event_base* evb, bool enableTimeMeasurement) ...@@ -141,7 +141,7 @@ EventBase::EventBase(event_base* evb, bool enableTimeMeasurement)
, enableTimeMeasurement_(enableTimeMeasurement) , enableTimeMeasurement_(enableTimeMeasurement)
, nextLoopCnt_(uint64_t(-40)) // Early wrap-around so bugs will manifest soon , nextLoopCnt_(uint64_t(-40)) // Early wrap-around so bugs will manifest soon
, latestLoopCnt_(nextLoopCnt_) , latestLoopCnt_(nextLoopCnt_)
, startWork_(0) , startWork_()
, observer_(nullptr) , observer_(nullptr)
, observerSampleCount_(0) , observerSampleCount_(0)
, executionObserver_(nullptr) { , executionObserver_(nullptr) {
...@@ -267,9 +267,9 @@ bool EventBase::loopBody(int flags) { ...@@ -267,9 +267,9 @@ bool EventBase::loopBody(int flags) {
// time-measurement variables. // time-measurement variables.
std::chrono::steady_clock::time_point prev; std::chrono::steady_clock::time_point prev;
int64_t idleStart = 0; std::chrono::steady_clock::time_point idleStart = {};
int64_t busy; std::chrono::microseconds busy;
int64_t idle; std::chrono::microseconds idle;
loopThread_.store(pthread_self(), std::memory_order_release); loopThread_.store(pthread_self(), std::memory_order_release);
...@@ -279,8 +279,7 @@ bool EventBase::loopBody(int flags) { ...@@ -279,8 +279,7 @@ bool EventBase::loopBody(int flags) {
if (enableTimeMeasurement_) { if (enableTimeMeasurement_) {
prev = std::chrono::steady_clock::now(); prev = std::chrono::steady_clock::now();
idleStart = std::chrono::duration_cast<std::chrono::microseconds>( idleStart = std::chrono::steady_clock::now();
std::chrono::steady_clock::now().time_since_epoch()).count();
} }
while (!stop_.load(std::memory_order_acquire)) { while (!stop_.load(std::memory_order_acquire)) {
...@@ -309,9 +308,9 @@ bool EventBase::loopBody(int flags) { ...@@ -309,9 +308,9 @@ bool EventBase::loopBody(int flags) {
if (enableTimeMeasurement_) { if (enableTimeMeasurement_) {
busy = std::chrono::duration_cast<std::chrono::microseconds>( busy = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count() - std::chrono::steady_clock::now() - startWork_);
startWork_; idle = std::chrono::duration_cast<std::chrono::microseconds>(
idle = startWork_ - idleStart; startWork_ - idleStart);
avgLoopTime_.addSample(std::chrono::microseconds(idle), avgLoopTime_.addSample(std::chrono::microseconds(idle),
std::chrono::microseconds(busy)); std::chrono::microseconds(busy));
...@@ -321,19 +320,19 @@ bool EventBase::loopBody(int flags) { ...@@ -321,19 +320,19 @@ bool EventBase::loopBody(int flags) {
if (observer_) { if (observer_) {
if (observerSampleCount_++ == observer_->getSampleRate()) { if (observerSampleCount_++ == observer_->getSampleRate()) {
observerSampleCount_ = 0; observerSampleCount_ = 0;
observer_->loopSample(busy, idle); observer_->loopSample(busy.count(), idle.count());
} }
} }
VLOG(11) << "EventBase " << this << " did not timeout " VLOG(11) << "EventBase " << this << " did not timeout " <<
" loop time guess: " << busy + idle << " loop time guess: " << (busy + idle).count() <<
" idle time: " << idle << " idle time: " << idle.count() <<
" busy time: " << busy << " busy time: " << busy.count() <<
" avgLoopTime: " << avgLoopTime_.get() << " avgLoopTime: " << avgLoopTime_.get() <<
" maxLatencyLoopTime: " << maxLatencyLoopTime_.get() << " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() <<
" maxLatency_: " << maxLatency_.count() << "us" << " maxLatency_: " << maxLatency_.count() << "us" <<
" notificationQueueSize: " << getNotificationQueueSize() << " notificationQueueSize: " << getNotificationQueueSize() <<
" nothingHandledYet(): "<< nothingHandledYet(); " nothingHandledYet(): " << nothingHandledYet();
// see if our average loop time has exceeded our limit // see if our average loop time has exceeded our limit
if ((maxLatency_ > std::chrono::microseconds::zero()) && if ((maxLatency_ > std::chrono::microseconds::zero()) &&
...@@ -345,8 +344,7 @@ bool EventBase::loopBody(int flags) { ...@@ -345,8 +344,7 @@ bool EventBase::loopBody(int flags) {
} }
// Our loop run did real work; reset the idle timer // Our loop run did real work; reset the idle timer
idleStart = std::chrono::duration_cast<std::chrono::microseconds>( idleStart = std::chrono::steady_clock::now();
std::chrono::steady_clock::now().time_since_epoch()).count();
} else { } else {
VLOG(11) << "EventBase " << this << " did not timeout"; VLOG(11) << "EventBase " << this << " did not timeout";
} }
...@@ -449,12 +447,10 @@ void EventBase::bumpHandlingTime() { ...@@ -449,12 +447,10 @@ void EventBase::bumpHandlingTime() {
if (nothingHandledYet()) { if (nothingHandledYet()) {
latestLoopCnt_ = nextLoopCnt_; latestLoopCnt_ = nextLoopCnt_;
// set the time // set the time
startWork_ = std::chrono::duration_cast<std::chrono::microseconds>( startWork_ = std::chrono::steady_clock::now();
std::chrono::steady_clock::now().time_since_epoch())
.count();
VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__ VLOG(11) << "EventBase " << this << " " << __PRETTY_FUNCTION__
<< " (loop) startWork_ " << startWork_; << " (loop) startWork_ " << startWork_.time_since_epoch().count();
} }
} }
......
...@@ -724,7 +724,7 @@ class EventBase : private boost::noncopyable, ...@@ -724,7 +724,7 @@ class EventBase : private boost::noncopyable,
// Wrap-around loop counter to detect beginning of each loop // Wrap-around loop counter to detect beginning of each loop
uint64_t nextLoopCnt_; uint64_t nextLoopCnt_;
uint64_t latestLoopCnt_; uint64_t latestLoopCnt_;
uint64_t startWork_; std::chrono::steady_clock::time_point startWork_;
// Prevent undefined behavior from invoking event_base_loop() reentrantly. // Prevent undefined behavior from invoking event_base_loop() reentrantly.
// This is needed since many projects use libevent-1.4, which lacks commit // This is needed since many projects use libevent-1.4, which lacks commit
// b557b175c00dc462c1fce25f6e7dd67121d2c001 from // b557b175c00dc462c1fce25f6e7dd67121d2c001 from
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment