Skip to content

Commit db1a1da

Browse files
pbhandar2meta-codesync[bot]
authored andcommitted
expose tta through the handle
Summary: The diff adds TTA to read handles. We cannot rely on using lastAccessedTimestamp to compute TTA. The timestamp is bookkeeping for replacement policy and will represent the most recent update to the list which is caused by the user's request. This means users cannot know what is the TTA since it doesn't know what the last accessed timestamp before their request was. Reviewed By: rlyerly Differential Revision: D98871729 fbshipit-source-id: 5f149e35483014fe5b4c7f5c28f7eb3350459ad2
1 parent f622174 commit db1a1da

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

cachelib/allocator/CacheAllocator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,6 +4361,10 @@ CacheAllocator<CacheTrait>::findInternalWithExpiration(
43614361
if (needToBumpStats) {
43624362
recordEvent(event, key, AllocatorApiResult::FOUND, handle);
43634363
}
4364+
auto lastAccess = handle->getLastAccessTime();
4365+
if (lastAccess > 0) {
4366+
handle.setTTASecs(util::getCurrentTimeSec() - lastAccess);
4367+
}
43644368
return handle;
43654369
}
43664370

cachelib/allocator/Handle.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ struct ReadHandleImpl {
101101
: alloc_(other.alloc_),
102102
it_(other.releaseItem()),
103103
waitContext_(std::move(other.waitContext_)),
104-
flags_(other.getFlags()) {}
104+
flags_(other.getFlags()),
105+
ttaSecs_(other.ttaSecs_) {}
105106

106107
FOLLY_ALWAYS_INLINE ReadHandleImpl& operator=(
107108
ReadHandleImpl&& other) noexcept {
@@ -222,6 +223,13 @@ struct ReadHandleImpl {
222223
return getFlags() & static_cast<uint8_t>(HandleFlags::kExpired);
223224
}
224225

226+
// Return the time-to-access (TTA) in seconds — the time between the
227+
// previous access and the find()/peek() that returned this handle.
228+
// Returns 0 for newly allocated items.
229+
uint32_t getTTASecs() const {
230+
return waitContext_ ? waitContext_->getTTASecs() : ttaSecs_;
231+
}
232+
225233
// blocks until `isReady() == true`.
226234
void wait() const noexcept {
227235
if (isReady()) {
@@ -274,6 +282,7 @@ struct ReadHandleImpl {
274282
}
275283

276284
uint8_t getFlags() const { return flags_; }
285+
uint32_t getTTASecs() const { return ttaSecs_; }
277286

278287
// Assumes ownership of the item managed by hdl
279288
// and invokes the onReadyCallback_
@@ -322,6 +331,7 @@ struct ReadHandleImpl {
322331
SCOPE_EXIT { hdl.release(); };
323332

324333
flags_ = hdl.getFlags();
334+
ttaSecs_ = hdl.ttaSecs_;
325335
auto it = hdl.getInternal();
326336
it_.store(it, std::memory_order_release);
327337
// Handles are fulfilled by threads different from the owners. Adjust
@@ -416,7 +426,8 @@ struct ReadHandleImpl {
416426
ReadyCallback onReadyCallback_; //< callback invoked when "ready"
417427
std::atomic<Item*> it_{reinterpret_cast<Item*>(kItemNotReady)}; //< The item
418428
uint8_t flags_{}; //< flags associated with the handle generated by NvmCache
419-
CacheT& alloc_; //< allocator instance
429+
uint32_t ttaSecs_{0}; //< time-to-access in seconds
430+
CacheT& alloc_; //< allocator instance
420431
};
421432

422433
// Set the onReady callback which should be invoked once the item is ready.
@@ -468,11 +479,15 @@ struct ReadHandleImpl {
468479
void markWentToNvm() {
469480
flags_ |= static_cast<uint8_t>(HandleFlags::kWentToNvm);
470481
}
482+
void setTTASecs(uint32_t tta) { ttaSecs_ = tta; }
471483

472484
uint8_t getFlags() const {
473485
return waitContext_ ? waitContext_->getFlags() : flags_;
474486
}
475-
void cloneFlags(const ReadHandleImpl& other) { flags_ = other.getFlags(); }
487+
void cloneFlags(const ReadHandleImpl& other) {
488+
flags_ = other.getFlags();
489+
ttaSecs_ = other.getTTASecs();
490+
}
476491

477492
Item* releaseItem() noexcept { return std::exchange(it_, nullptr); }
478493

@@ -531,6 +546,7 @@ struct ReadHandleImpl {
531546
std::shared_ptr<ItemWaitContext> waitContext_;
532547

533548
mutable uint8_t flags_{};
549+
uint32_t ttaSecs_{0};
534550

535551
// Only CacheAllocator and NvmCache can create non-default constructed handles
536552
friend CacheT;

cachelib/allocator/nvmcache/NvmCache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,7 @@ void NvmCache<C>::onGetComplete(GetCtx& ctx,
13931393
if (latestLastAccessTimeSecs > 0) {
13941394
auto ttaSecs = util::getCurrentTimeSec() - latestLastAccessTimeSecs;
13951395
stats().nvmHitTTASecs_.trackValue(ttaSecs);
1396+
it.setTTASecs(static_cast<uint32_t>(ttaSecs));
13961397
}
13971398

13981399
// by the time we filled from navy, another thread inserted in RAM. We

0 commit comments

Comments
 (0)