proxygen
|
#include <MemoryIdler.h>
Public Types | |
enum | { kDefaultStackToRetain = 1024 } |
Static Public Member Functions | |
static void | flushLocalMallocCaches () |
static void | unmapUnusedStack (size_t retain=kDefaultStackToRetain) |
template<typename IdleTime = std::chrono::steady_clock::duration> | |
static IdleTime | getVariationTimeout (IdleTime const &idleTimeout=defaultIdleTimeout.load(std::memory_order_acquire), float timeoutVariationFrac=0.5) |
template<typename Futex , typename IdleTime = std::chrono::steady_clock::duration> | |
static FutexResult | futexWait (Futex &fut, uint32_t expected, uint32_t waitMask=-1, IdleTime const &idleTimeout=defaultIdleTimeout.load(std::memory_order_acquire), size_t stackToRetain=kDefaultStackToRetain, float timeoutVariationFrac=0.5) |
template<typename Futex , typename Deadline , typename IdleTime = std::chrono::steady_clock::duration> | |
static FutexResult | futexWaitUntil (Futex &fut, uint32_t expected, Deadline const &deadline, uint32_t waitMask=-1, IdleTime const &idleTimeout=defaultIdleTimeout.load(std::memory_order_acquire), size_t stackToRetain=kDefaultStackToRetain, float timeoutVariationFrac=0.5) |
Static Public Attributes | |
static AtomicStruct< std::chrono::steady_clock::duration > | defaultIdleTimeout |
Static Private Member Functions | |
template<typename Futex , typename Deadline , typename IdleTime > | |
static bool | futexWaitPreIdle (FutexResult &_ret, Futex &fut, uint32_t expected, Deadline const &deadline, uint32_t waitMask, IdleTime idleTimeout, size_t stackToRetain, float timeoutVariationFrac) |
MemoryIdler provides helper routines that allow routines to return some assigned memory resources back to the system. The intended use is that when a thread is waiting for a long time (perhaps it is in a LIFO thread pool and hasn't been needed for a long time) it should release its thread-local malloc caches (both jemalloc and tcmalloc use these for better performance) and unmap the stack pages that contain no useful data.
Definition at line 38 of file MemoryIdler.h.
anonymous enum |
Definition at line 44 of file MemoryIdler.h.
|
static |
Returns memory from thread-local allocation pools to the global pool, if we know how to for the current malloc implementation. jemalloc is supported.
Definition at line 41 of file MemoryIdler.cpp.
References addr, folly::call_once(), folly::test::end(), FB_LOG_EVERY_MS, flag, FOLLY_NOINLINE, folly::kIsDebug, mallctl, mallctlbymib, mallctlnametomib, folly::mallctlRead(), max, SCOPE_EXIT, folly::CacheLocality::system(), unmapUnusedStack(), folly::usingJEMalloc(), and folly::WARNING.
Referenced by BENCHMARK(), futexWaitPreIdle(), folly::MemoryIdlerTimeout::runLoopCallback(), and TEST().
|
inlinestatic |
Equivalent to fut.futexWait(expected, waitMask), but calls flushLocalMallocCaches() and unmapUnusedStack(stackToRetain) after idleTimeout has passed (if it has passed). Internally uses fut.futexWait and fut.futexWaitUntil. The actual timeout will be pseudo-randomly chosen to be between idleTimeout and idleTimeout * (1 + timeoutVariationFraction), to smooth out the behavior in a system with bursty requests. The default is to wait up to 50% extra, so on average 25% extra.
Definition at line 102 of file MemoryIdler.h.
References folly::detail::futexWait(), futexWaitPreIdle(), and max.
Referenced by folly::fibers::Baton::waitThread().
|
inlinestaticprivate |
Definition at line 167 of file MemoryIdler.h.
References flushLocalMallocCaches(), folly::detail::futexWaitUntil(), futexWaitUntil(), getVariationTimeout(), max, now(), folly::detail::TIMEDOUT, and unmapUnusedStack().
Referenced by futexWait(), and futexWaitUntil().
|
inlinestatic |
Equivalent to fut.futexWaitUntil(expected, deadline, waitMask), but calls flushLocalMallocCaches() and unmapUnusedStack(stackToRetain) after idleTimeout has passed (if it has passed). Internally uses fut.futexWaitUntil. The actual timeout will be pseudo-randomly chosen to be between idleTimeout and idleTimeout * (1 + timeoutVariationFraction), to smooth out the behavior in a system with bursty requests. The default is to wait up to 50% extra, so on average 25% extra.
Definition at line 139 of file MemoryIdler.h.
References futexWaitPreIdle(), and folly::detail::futexWaitUntil().
Referenced by futexWaitPreIdle(), TEST(), folly::Baton< MayBlock, Atom >::tryWaitSlow(), and folly::SaturatingSemaphore< MayBlock, Atom >::tryWaitSlow().
|
inlinestatic |
Selects a timeout pseudo-randomly chosen to be between idleTimeout and idleTimeout * (1 + timeoutVariationFraction), to smooth out the behavior in a bursty system
Definition at line 70 of file MemoryIdler.h.
References count, folly::getCurrentThreadID(), h, folly::hash::hash_combine(), max, now(), folly::hash::twang_mix64(), and uint64_t.
Referenced by futexWaitPreIdle(), and folly::MemoryIdlerTimeout::runLoopCallback().
|
static |
Uses madvise to discard the portion of the thread's stack that currently doesn't hold any data, trying to ensure that no page faults will occur during the next retain bytes of stack allocation
Definition at line 199 of file MemoryIdler.cpp.
Referenced by BENCHMARK(), flushLocalMallocCaches(), futexWaitPreIdle(), folly::MemoryIdlerTimeout::runLoopCallback(), and TEST().
|
static |
The system-wide default for the amount of time a blocking thread should wait before reclaiming idle memory. Set this to Duration::max() to never wait. The default value is 5 seconds. Endpoints using this idle timeout might randomly wait longer to avoid synchronizing their flushes.
Definition at line 64 of file MemoryIdler.h.
Referenced by folly::MemoryIdlerTimeout::runLoopCallback(), and TEST().