proxygen
|
#include <HHWheelTimer.h>
Classes | |
class | Callback |
Public Types | |
using | UniquePtr = std::unique_ptr< HHWheelTimer, Destructor > |
using | SharedPtr = std::shared_ptr< HHWheelTimer > |
Public Member Functions | |
HHWheelTimer (folly::TimeoutManager *timeoutManager, std::chrono::milliseconds intervalMS=std::chrono::milliseconds(DEFAULT_TICK_INTERVAL), AsyncTimeout::InternalEnum internal=AsyncTimeout::InternalEnum::NORMAL, std::chrono::milliseconds defaultTimeoutMS=std::chrono::milliseconds(-1)) | |
size_t | cancelAll () |
std::chrono::milliseconds | getTickInterval () const |
std::chrono::milliseconds | getDefaultTimeout () const |
void | setDefaultTimeout (std::chrono::milliseconds timeout) |
void | scheduleTimeout (Callback *callback, std::chrono::milliseconds timeout) |
void | scheduleTimeoutImpl (Callback *callback, std::chrono::milliseconds timeout) |
void | scheduleTimeout (Callback *callback) |
template<class F > | |
void | scheduleTimeoutFn (F fn, std::chrono::milliseconds timeout) |
std::size_t | count () const |
bool | isDetachable () const |
Public Member Functions inherited from folly::DelayedDestruction | |
virtual void | destroy () |
bool | getDestroyPending () const |
Public Member Functions inherited from folly::DelayedDestructionBase | |
virtual | ~DelayedDestructionBase ()=default |
Static Public Member Functions | |
template<typename... Args> | |
static UniquePtr | newTimer (Args &&...args) |
Static Public Attributes | |
static int | DEFAULT_TICK_INTERVAL = 10 |
Protected Member Functions | |
~HHWheelTimer () override | |
Protected Member Functions inherited from folly::DelayedDestruction | |
~DelayedDestruction () override=default | |
DelayedDestruction () | |
Protected Member Functions inherited from folly::DelayedDestructionBase | |
DelayedDestructionBase () | |
uint32_t | getDestructorGuardCount () const |
Private Types | |
typedef Callback::List | CallbackList |
Private Types inherited from folly::AsyncTimeout | |
typedef TimeoutManager::InternalEnum | InternalEnum |
Private Attributes | |
std::chrono::milliseconds | interval_ |
std::chrono::milliseconds | defaultTimeout_ |
CallbackList | buckets_ [WHEEL_BUCKETS][WHEEL_SIZE] |
std::vector< std::size_t > | bitmap_ |
int64_t | lastTick_ |
int64_t | expireTick_ |
std::size_t | count_ |
std::chrono::steady_clock::time_point | startTime_ |
bool * | processingCallbacksGuard_ |
CallbackList | timeouts |
Static Private Attributes | |
static constexpr int | WHEEL_BUCKETS = 4 |
static constexpr int | WHEEL_BITS = 8 |
static constexpr unsigned int | WHEEL_SIZE = (1 << WHEEL_BITS) |
static constexpr unsigned int | WHEEL_MASK = (WHEEL_SIZE - 1) |
static constexpr uint32_t | LARGEST_SLOT = 0xffffffffUL |
Additional Inherited Members | |
Static Private Member Functions inherited from folly::AsyncTimeout | |
template<typename TCallback > | |
static std::unique_ptr< AsyncTimeout > | make (TimeoutManager &manager, TCallback &&callback) |
template<typename TCallback > | |
static std::unique_ptr< AsyncTimeout > | schedule (TimeoutManager::timeout_type timeout, TimeoutManager &manager, TCallback &&callback) |
Hashed Hierarchical Wheel Timer
We model timers as the number of ticks until the next due event. We allow 32-bits of space to track this due interval, and break that into 4 regions of 8 bits. Each region indexes into a bucket of 256 lists.
Bucket 0 represents those events that are due the soonest. Each tick causes us to look at the next list in a bucket. The 0th list in a bucket is special; it means that it is time to flush the timers from the next higher bucket and schedule them into a different bucket.
This technique results in a very cheap mechanism for maintaining time and timers.
Unlike the original timer wheel paper, this implementation does not tick constantly, and instead calculates the exact next wakeup time.
Definition at line 54 of file HHWheelTimer.h.
|
private |
Definition at line 290 of file HHWheelTimer.h.
using folly::HHWheelTimer::SharedPtr = std::shared_ptr<HHWheelTimer> |
Definition at line 58 of file HHWheelTimer.h.
using folly::HHWheelTimer::UniquePtr = std::unique_ptr<HHWheelTimer, Destructor> |
Definition at line 57 of file HHWheelTimer.h.
|
explicit |
Definition at line 78 of file HHWheelTimer.cpp.
References bitmap_, and WHEEL_SIZE.
Referenced by newTimer().
|
overrideprotected |
Protected destructor.
Use destroy() instead. See the comments in DelayedDestruction for more details.
Definition at line 94 of file HHWheelTimer.cpp.
References cancelAll(), folly::makeGuard(), processingCallbacksGuard_, and timeouts.
Referenced by isDetachable().
|
privatedelete |
|
private |
Definition at line 307 of file HHWheelTimer.cpp.
References getCurTime(), interval_, lastTick_, processingCallbacksGuard_, and startTime_.
Referenced by scheduleNextTimeout(), scheduleTimeoutImpl(), and timeoutExpired().
size_t folly::HHWheelTimer::cancelAll | ( | ) |
Cancel all outstanding timeouts
Definition at line 247 of file HHWheelTimer.cpp.
References buckets_, count(), count_, i, min, folly::f14::swap(), WHEEL_BUCKETS, and WHEEL_SIZE.
Referenced by TEST_F(), and ~HHWheelTimer().
|
private |
Definition at line 171 of file HHWheelTimer.cpp.
References buckets_, getCurTime(), and scheduleTimeoutImpl().
Referenced by timeoutExpired(), and timeToWheelTicks().
|
inline |
Return the number of currently pending timeouts
Definition at line 252 of file HHWheelTimer.h.
References count_.
Referenced by cancelAll(), and TEST_F().
|
inlineprivate |
Definition at line 311 of file HHWheelTimer.h.
References now().
Referenced by calcNextTick(), and cascadeTimers().
|
inline |
Get the default timeout interval for this HHWheelTimer.
Returns the timeout interval in milliseconds.
Definition at line 193 of file HHWheelTimer.h.
References defaultTimeout_.
Referenced by TEST_F(), and proxygen::WheelTimerInstance::WheelTimerInstance().
|
inline |
Get the tick interval for this HHWheelTimer.
Returns the tick interval in milliseconds.
Definition at line 184 of file HHWheelTimer.h.
References interval_.
|
inline |
Definition at line 256 of file HHWheelTimer.h.
References folly::AsyncTimeout::attachEventBase(), folly::AsyncTimeout::detachEventBase(), folly::AsyncTimeout::getTimeoutManager(), folly::HHWheelTimer::Callback::HHWheelTimer, folly::AsyncTimeout::isScheduled(), folly::pushmi::__adl::noexcept(), operator=(), folly::HHWheelTimer::Callback::timeoutExpired(), and ~HHWheelTimer().
|
inlinestatic |
Definition at line 61 of file HHWheelTimer.h.
References HHWheelTimer().
Referenced by HTTPUpstreamTest< SPDY3CodecPair >::HTTPUpstreamTest(), proxygen::makeInternalTimeoutSet(), proxygen::makeTimeoutSet(), TEST_F(), folly::ThreadWheelTimekeeper::ThreadWheelTimekeeper(), TimeoutableHTTPUpstreamTest< C >::TimeoutableHTTPUpstreamTest(), and folly::EventBase::timer().
|
privatedelete |
Referenced by isDetachable().
|
private |
Definition at line 281 of file HHWheelTimer.cpp.
References bitmap_, calcNextTick(), folly::AsyncTimeout::cancelTimeout(), count_, expireTick_, folly::findFirstSet(), int64_t, interval_, folly::AsyncTimeout::isScheduled(), folly::makeBitIterator(), folly::AsyncTimeout::scheduleTimeout(), WHEEL_MASK, and WHEEL_SIZE.
Referenced by scheduleTimeout(), and timeoutExpired().
void folly::HHWheelTimer::scheduleTimeout | ( | Callback * | callback, |
std::chrono::milliseconds | timeout | ||
) |
Schedule the specified Callback to be invoked after the specified timeout interval.
If the callback is already scheduled, this cancels the existing timeout before scheduling the new timeout.
Definition at line 144 of file HHWheelTimer.cpp.
References folly::HHWheelTimer::Callback::cancelTimeout(), count_, processingCallbacksGuard_, folly::HHWheelTimer::Callback::requestContext_, folly::RequestContext::saveContext(), scheduleNextTimeout(), scheduleTimeoutImpl(), and folly::HHWheelTimer::Callback::setScheduled().
Referenced by proxygen::HTTPTransaction::maybeDelayForRateLimit(), proxygen::WheelTimerInstance::scheduleTimeout(), scheduleTimeout(), scheduleTimeoutFn(), setDefaultTimeout(), TEST_F(), and TestTimeout::TestTimeout().
void folly::HHWheelTimer::scheduleTimeout | ( | Callback * | callback | ) |
Schedule the specified Callback to be invoked after the default timeout interval.
If the callback is already scheduled, this cancels the existing timeout before scheduling the new timeout.
This method uses CHECK() to make sure that the default timeout was specified on the object initialization.
Definition at line 165 of file HHWheelTimer.cpp.
References defaultTimeout_, and scheduleTimeout().
|
inline |
Definition at line 229 of file HHWheelTimer.h.
References f, folly::gen::move, folly::pushmi::__adl::noexcept(), scheduleTimeout(), and folly::HHWheelTimer::Callback::timeoutExpired().
Referenced by folly::EventBase::scheduleAt(), and TEST_F().
void folly::HHWheelTimer::scheduleTimeoutImpl | ( | Callback * | callback, |
std::chrono::milliseconds | timeout | ||
) |
Definition at line 111 of file HHWheelTimer.cpp.
References bitmap_, folly::HHWheelTimer::Callback::bucket_, buckets_, calcNextTick(), diff(), int64_t, LARGEST_SLOT, bm::list, folly::makeBitIterator(), timeToWheelTicks(), WHEEL_BITS, WHEEL_MASK, and WHEEL_SIZE.
Referenced by cascadeTimers(), scheduleTimeout(), and setDefaultTimeout().
|
inline |
Set the default timeout interval for this HHWheelTimer.
Definition at line 200 of file HHWheelTimer.h.
References defaultTimeout_, scheduleTimeout(), scheduleTimeoutImpl(), and folly::detail::timeout.
Referenced by TEST_F().
|
overrideprivatevirtualnoexcept |
timeoutExpired() is invoked when the timeout period has expired.
Implements folly::AsyncTimeout.
Definition at line 184 of file HHWheelTimer.cpp.
References bitmap_, buckets_, calcNextTick(), cascadeTimers(), count_, expireTick_, lastTick_, folly::makeBitIterator(), folly::makeGuard(), processingCallbacksGuard_, scheduleNextTimeout(), timeouts, WHEEL_BITS, and WHEEL_MASK.
|
inlineprivate |
Definition at line 294 of file HHWheelTimer.h.
References cascadeTimers().
Referenced by scheduleTimeoutImpl().
|
private |
Definition at line 292 of file HHWheelTimer.h.
Referenced by folly::HHWheelTimer::Callback::cancelTimeoutImpl(), HHWheelTimer(), scheduleNextTimeout(), scheduleTimeoutImpl(), and timeoutExpired().
|
private |
Definition at line 291 of file HHWheelTimer.h.
Referenced by cancelAll(), folly::HHWheelTimer::Callback::cancelTimeoutImpl(), cascadeTimers(), scheduleTimeoutImpl(), and timeoutExpired().
|
private |
Definition at line 301 of file HHWheelTimer.h.
Referenced by cancelAll(), folly::HHWheelTimer::Callback::cancelTimeoutImpl(), count(), scheduleNextTimeout(), scheduleTimeout(), and timeoutExpired().
|
static |
Create a new HHWheelTimer with the specified interval and the default timeout value set.
Objects created using this version of constructor can be used to schedule both variable interval timeouts using scheduleTimeout(callback, timeout) method, and default interval timeouts using scheduleTimeout(callback) method.
We want to select the default interval carefully. An interval of 10ms will give us 10ms * WHEEL_SIZE^WHEEL_BUCKETS for the largest timeout possible, or about 497 days.
For a lower bound, we want a reasonable limit on local IO, 10ms seems short enough
A shorter interval also has CPU implications, less than 1ms might start showing up in cpu perf. Also, it might not be possible to set tick interval less than 10ms on older kernels.
Definition at line 163 of file HHWheelTimer.h.
Referenced by HTTPUpstreamTest< SPDY3CodecPair >::HTTPUpstreamTest(), proxygen::makeInternalTimeoutSet(), proxygen::makeTimeoutSet(), ScopedServerTest::SetUp(), TEST(), TEST_F(), and TimeoutableHTTPUpstreamTest< C >::TimeoutableHTTPUpstreamTest().
|
private |
Definition at line 282 of file HHWheelTimer.h.
Referenced by getDefaultTimeout(), scheduleTimeout(), and setDefaultTimeout().
|
private |
Definition at line 300 of file HHWheelTimer.h.
Referenced by scheduleNextTimeout(), and timeoutExpired().
|
private |
Definition at line 281 of file HHWheelTimer.h.
Referenced by calcNextTick(), getTickInterval(), and scheduleNextTimeout().
|
staticprivate |
Definition at line 288 of file HHWheelTimer.h.
Referenced by scheduleTimeoutImpl().
|
private |
Definition at line 299 of file HHWheelTimer.h.
Referenced by calcNextTick(), and timeoutExpired().
|
private |
Definition at line 308 of file HHWheelTimer.h.
Referenced by calcNextTick(), scheduleTimeout(), timeoutExpired(), and ~HHWheelTimer().
|
private |
Definition at line 302 of file HHWheelTimer.h.
Referenced by calcNextTick().
|
private |
Definition at line 309 of file HHWheelTimer.h.
Referenced by timeoutExpired(), and ~HHWheelTimer().
|
staticprivate |
Definition at line 285 of file HHWheelTimer.h.
Referenced by scheduleTimeoutImpl(), and timeoutExpired().
|
staticprivate |
Definition at line 284 of file HHWheelTimer.h.
Referenced by cancelAll().
|
staticprivate |
Definition at line 287 of file HHWheelTimer.h.
Referenced by scheduleNextTimeout(), scheduleTimeoutImpl(), and timeoutExpired().
|
staticprivate |
Definition at line 286 of file HHWheelTimer.h.
Referenced by cancelAll(), HHWheelTimer(), scheduleNextTimeout(), and scheduleTimeoutImpl().