Skip to content

Commit

Permalink
Merge pull request #2494 from cloudflare/jsnell/jsg-manual-external-m…
Browse files Browse the repository at this point in the history
…emory-accounting
  • Loading branch information
jasnell committed Aug 9, 2024
2 parents 7a101e4 + 983b4ec commit 16c7353
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/workerd/jsg/jsg.c++
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ JsSymbol Lock::symbolAsyncDispose() {
return IsolateBase::from(v8Isolate).getSymbolAsyncDispose();
}

void Lock::adjustExternalMemory(ssize_t amount) {
v8Isolate->AdjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(amount));
}

namespace {
struct ExternalMemoryAdjuster {
int64_t size;
v8::Isolate* isolate;
ExternalMemoryAdjuster(v8::Isolate* isolate, size_t size)
: size(static_cast<int64_t>(size)), isolate(isolate) {
isolate->AdjustAmountOfExternalAllocatedMemory(size);
}

~ExternalMemoryAdjuster() noexcept(false) {
isolate->AdjustAmountOfExternalAllocatedMemory(-size);
}
};
} // namespace

kj::Own<void> Lock::getExternalMemoryAdjuster(size_t amount) {
return kj::heap<ExternalMemoryAdjuster>(v8Isolate, amount);
}

Name::Name(kj::String string)
: hash(kj::hashCode(string)),
inner(kj::mv(string)) {}
Expand Down
14 changes: 14 additions & 0 deletions src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,20 @@ class Lock {
return *reinterpret_cast<Lock*>(v8Isolate->GetData(2));
}

// Manually make adjustments to the amount of external memory reported to V8.
// This is useful when we have a large amount of external memory allocated that
// typically would not be visible to v8's memory tracking. In the future we hope
// to make most memory accounting automatic, making most direct uses of this
// API unnecessary but there will always be times when manual adjustments are
// necessary.
void adjustExternalMemory(ssize_t amount);

// Reports amount of external memory to be manually attributed to the isolate.
// When the returned kj::Own<void> is dropped, the amount will be subtracted
// from the isolate's external memory accounting. It is important that these
// be held onto only during the lifetime of the isolate.
kj::Own<void> getExternalMemoryAdjuster(size_t amount);

Value parseJson(kj::ArrayPtr<const char> data);
Value parseJson(v8::Local<v8::String> text);
template <typename T>
Expand Down

0 comments on commit 16c7353

Please sign in to comment.