Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics for Memory Cache. #2661

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/workerd/api/memory-cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,23 @@ 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
35 changes: 35 additions & 0 deletions src/workerd/io/observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@ 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 +139,10 @@ 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
Loading