Skip to content

Commit

Permalink
Implement memory tracking for handful of classes
Browse files Browse the repository at this point in the history
* cache
* cf-property
* form-data
* hyperdrive
* kv
* queue
  • Loading branch information
jasnell committed Feb 6, 2024
1 parent 99b1fd2 commit 54bcc23
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/workerd/api/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class Cache: public jsg::Object {
// Use RequestInfo type alias to allow `URL`s as cache keys
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("cacheName", cacheName);
}

private:
kj::Maybe<kj::String> cacheName;

Expand Down Expand Up @@ -109,6 +113,10 @@ class CacheStorage: public jsg::Object {
JSG_READONLY_INSTANCE_PROPERTY(default, getDefault);
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("default", default_);
}

private:
jsg::Ref<Cache> default_;
};
Expand Down
13 changes: 13 additions & 0 deletions src/workerd/api/cf-property.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ class CfProperty {

void visitForGc(jsg::GcVisitor& visitor);

JSG_MEMORY_INFO(CfProperty) {
KJ_IF_SOME(v, value) {
KJ_SWITCH_ONEOF(v) {
KJ_CASE_ONEOF(str, kj::String) {
tracker.trackField("value", str);
}
KJ_CASE_ONEOF(obj, jsg::JsRef<jsg::JsObject>) {
tracker.trackField("value", obj);
}
}
}
}

private:
kj::Maybe<kj::OneOf<kj::String, jsg::JsRef<jsg::JsObject>>> value;
};
Expand Down
20 changes: 20 additions & 0 deletions src/workerd/api/form-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class FormData: public jsg::Object {
void visitForGc(jsg::GcVisitor& visitor) {
visitor.visit(parent);
}

JSG_MEMORY_INFO(IteratorState) {
tracker.trackField("parent", parent);
}
};

public:
Expand All @@ -51,6 +55,18 @@ class FormData: public jsg::Object {
struct Entry {
kj::String name;
kj::OneOf<jsg::Ref<File>, kj::String> value;

JSG_MEMORY_INFO(Entry) {
tracker.trackField("name", name);
KJ_SWITCH_ONEOF(value) {
KJ_CASE_ONEOF(file, jsg::Ref<File>) {
tracker.trackField("value", file);
}
KJ_CASE_ONEOF(str, kj::String) {
tracker.trackField("value", str);
}
}
}
};

kj::ArrayPtr<const Entry> getData() { return data; }
Expand Down Expand Up @@ -142,6 +158,10 @@ class FormData: public jsg::Object {
}
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("data", data.asPtr());
}

private:
kj::Vector<Entry> data;

Expand Down
8 changes: 8 additions & 0 deletions src/workerd/api/hyperdrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class Hyperdrive : public jsg::Object {
JSG_METHOD(connect);
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("randomHost", randomHost);
tracker.trackField("database", database);
tracker.trackField("user", user);
tracker.trackField("password", password);
tracker.trackField("scheme", scheme);
}

private:
uint clientIndex;
kj::String randomHost;
Expand Down
9 changes: 9 additions & 0 deletions src/workerd/api/kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class KvNamespace: public jsg::Object {
struct AdditionalHeader {
kj::String name;
kj::String value;

JSG_MEMORY_INFO(AdditionalHeader) {
tracker.trackField("name", name);
tracker.trackField("value", value);
}
};

// `subrequestChannel` is what to pass to IoContext::getHttpClient() to get an HttpClient
Expand Down Expand Up @@ -155,6 +160,10 @@ class KvNamespace: public jsg::Object {
});
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("additionalHeaders", additionalHeaders.asPtr());
}

protected:
// Do the boilerplate work of constructing an HTTP client to KV. Setting a KvOptType causes
// the limiter for that op type to be checked. If a string is used, that's used as the operation
Expand Down
18 changes: 18 additions & 0 deletions src/workerd/api/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ class QueueMessage final: public jsg::Object {
});
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("id", id);
tracker.trackField("body", body);
tracker.trackFieldWithSize("IoPtr<QueueEventResult>", sizeof(IoPtr<QueueEventResult>));
}

private:
kj::String id;
kj::Date timestamp;
Expand Down Expand Up @@ -182,6 +188,14 @@ class QueueEvent final: public ExtendableEvent {
});
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
for (auto& message: messages) {
tracker.trackField("message", message);
}
tracker.trackField("queueName", queueName);
tracker.trackFieldWithSize("IoPtr<QueueEventResult>", sizeof(IoPtr<QueueEventResult>));
}

private:
// TODO(perf): Should we store these in a v8 array directly rather than this intermediate kj
// array to avoid one intermediate copy?
Expand Down Expand Up @@ -218,6 +232,10 @@ class QueueController final: public jsg::Object {
});
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
tracker.trackField("event", event);
}

private:
jsg::Ref<QueueEvent> event;

Expand Down

0 comments on commit 54bcc23

Please sign in to comment.