Skip to content

Commit

Permalink
Add metrics for Memory Cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-cf committed Sep 5, 2024
1 parent 485a7ba commit 4d5568d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/workerd/api/memory-cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,21 @@ SharedMemoryCache::Use::~Use() noexcept(false) {

kj::Maybe<kj::Own<CacheValue>> SharedMemoryCache::Use::getWithoutFallback(
const kj::String& key) const {
auto data = cache->data.lockExclusive();
kj::Locked<ThreadUnsafeData> data;
{
auto memoryCacheLockRecord = MemoryCacheLockRecord(IoContext::current().getMetrics().getMemoryCacheObserver());
data = cache->data.lockExclusive();
}
return cache->getWhileLocked(*data, key);
}

kj::OneOf<kj::Own<CacheValue>, kj::Promise<SharedMemoryCache::Use::GetWithFallbackOutcome>>
SharedMemoryCache::Use::getWithFallback(const kj::String& key) const {
auto data = cache->data.lockExclusive();
kj::Locked<ThreadUnsafeData> data;
{
auto memoryCacheLockRecord = MemoryCacheLockRecord(IoContext::current().getMetrics().getMemoryCacheObserver());
data = cache->data.lockExclusive();
}
KJ_IF_SOME(existingValue, cache->getWhileLocked(*data, key)) {
return kj::mv(existingValue);
} else KJ_IF_SOME(existingInProgress, data->inProgress.find(key)) {
Expand Down Expand Up @@ -314,7 +322,11 @@ void SharedMemoryCache::Use::handleFallbackFailure(InProgress& inProgress) const
// If there is another queued fallback, retrieve it and remove it from the
// queue. Otherwise, just delete the queue entirely.
{
auto data = cache->data.lockExclusive();
kj::Locked<ThreadUnsafeData> data;
{
auto memoryCacheLockRecord = MemoryCacheLockRecord(IoContext::current().getMetrics().getMemoryCacheObserver());
data = cache->data.lockExclusive();
}
auto next = inProgress.waiting.begin();
if (next != inProgress.waiting.end()) {
nextFulfiller = kj::mv(next->fulfiller);
Expand Down
34 changes: 34 additions & 0 deletions src/workerd/io/observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ class WebSocketObserver: public kj::Refcounted {
virtual void receivedMessage(size_t bytes) {};
};

// Collects metrics on memory cache, like lock wait time.
class MemoryCacheObserver : public kj::AtomicRefcounted {
public:
virtual void start() { };
virtual void end() { };
};
// turn into requestobserver
class MemoryCacheLockTiming {
public:
// Unlikely we'll get to report this without exposing internals of lock
//virtual void waitingForLock() {}
virtual void start() {}
virtual void stop() {}
virtual void locked() {}
};

class MemoryCacheLockRecord {
public:
explicit MemoryCacheLockRecord(kj::Maybe<kj::Own<MemoryCacheObserver>> observer)
: memoryCacheObserver(kj::mv(observer)) {
KJ_IF_SOME(o, memoryCacheObserver) o.get()->start();
}
~MemoryCacheLockRecord() noexcept(false) {
KJ_IF_SOME(o, memoryCacheObserver) o.get()->end();
}
KJ_DISALLOW_COPY_AND_MOVE(MemoryCacheLockRecord);

private:
kj::Maybe<kj::Own<MemoryCacheObserver>> memoryCacheObserver;
};


// Observes a specific request to a specific worker. Also observes outgoing subrequests.
//
// Observing anything is optional. Default implementations of all methods observe nothing.
Expand Down Expand Up @@ -108,6 +140,8 @@ class RequestObserver: public kj::Refcounted {
virtual uint64_t clockRead() {
return 0;
}

virtual kj::Maybe<kj::Own<MemoryCacheObserver>> getMemoryCacheObserver() { return kj::none; };
};

class IsolateObserver: public kj::AtomicRefcounted, public jsg::IsolateObserver {
Expand Down

0 comments on commit 4d5568d

Please sign in to comment.